Remove obsolete test from gdb.cp/var-tag.exp
[binutils-gdb.git] / bfd / elflink.c
blob309b4d75094baf91b993f92c406100b6c8a36852
1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2025 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
21 #include "sysdep.h"
22 #include "bfd.h"
23 #include "bfdlink.h"
24 #include "libbfd.h"
25 #define ARCH_SIZE 0
26 #include "elf-bfd.h"
27 #include "safe-ctype.h"
28 #include "libiberty.h"
29 #include "objalloc.h"
30 #if BFD_SUPPORTS_PLUGINS
31 #include "plugin-api.h"
32 #include "plugin.h"
33 #endif
35 #include <limits.h>
36 #ifndef CHAR_BIT
37 #define CHAR_BIT 8
38 #endif
40 /* This struct is used to pass information to routines called via
41 elf_link_hash_traverse which must return failure. */
43 struct elf_info_failed
45 struct bfd_link_info *info;
46 bool failed;
49 static bool _bfd_elf_fix_symbol_flags
50 (struct elf_link_hash_entry *, struct elf_info_failed *);
52 /* Return false if linker should avoid caching relocation information
53 and symbol tables of input files in memory. */
55 static bool
56 _bfd_elf_link_keep_memory (struct bfd_link_info *info)
58 #ifdef USE_MMAP
59 /* Don't cache symbol nor relocation tables if they are mapped in.
60 NB: Since the --no-keep-memory linker option causes:
62 https://sourceware.org/bugzilla/show_bug.cgi?id=31458
64 this is opt-in by each backend. */
65 const struct elf_backend_data *bed
66 = get_elf_backend_data (info->output_bfd);
67 if (bed != NULL && bed->use_mmap)
68 return false;
69 #endif
70 bfd *abfd;
71 bfd_size_type size;
73 if (!info->keep_memory)
74 return false;
76 if (info->max_cache_size == (bfd_size_type) -1)
77 return true;
79 abfd = info->input_bfds;
80 size = info->cache_size;
83 if (size >= info->max_cache_size)
85 /* Over the limit. Reduce the memory usage. */
86 info->keep_memory = false;
87 return false;
89 if (!abfd)
90 break;
91 size += abfd->alloc_size;
92 abfd = abfd->link.next;
94 while (1);
96 return true;
99 asection *
100 _bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
101 unsigned long r_symndx,
102 bool discard)
104 if (r_symndx >= cookie->locsymcount
105 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
107 struct elf_link_hash_entry *h;
109 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
111 while (h->root.type == bfd_link_hash_indirect
112 || h->root.type == bfd_link_hash_warning)
113 h = (struct elf_link_hash_entry *) h->root.u.i.link;
115 if ((h->root.type == bfd_link_hash_defined
116 || h->root.type == bfd_link_hash_defweak)
117 && discarded_section (h->root.u.def.section))
118 return h->root.u.def.section;
119 else
120 return NULL;
122 else
124 /* It's not a relocation against a global symbol,
125 but it could be a relocation against a local
126 symbol for a discarded section. */
127 asection *isec;
128 Elf_Internal_Sym *isym;
130 /* Need to: get the symbol; get the section. */
131 isym = &cookie->locsyms[r_symndx];
132 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
133 if (isec != NULL
134 && discard ? discarded_section (isec) : 1)
135 return isec;
137 return NULL;
140 /* Define a symbol in a dynamic linkage section. */
142 struct elf_link_hash_entry *
143 _bfd_elf_define_linkage_sym (bfd *abfd,
144 struct bfd_link_info *info,
145 asection *sec,
146 const char *name)
148 struct elf_link_hash_entry *h;
149 struct bfd_link_hash_entry *bh;
150 const struct elf_backend_data *bed;
152 h = elf_link_hash_lookup (elf_hash_table (info), name, false, false, false);
153 if (h != NULL)
155 /* Zap symbol defined in an as-needed lib that wasn't linked.
156 This is a symptom of a larger problem: Absolute symbols
157 defined in shared libraries can't be overridden, because we
158 lose the link to the bfd which is via the symbol section. */
159 h->root.type = bfd_link_hash_new;
160 bh = &h->root;
162 else
163 bh = NULL;
165 bed = get_elf_backend_data (abfd);
166 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
167 sec, 0, NULL, false, bed->collect,
168 &bh))
169 return NULL;
170 h = (struct elf_link_hash_entry *) bh;
171 BFD_ASSERT (h != NULL);
172 h->def_regular = 1;
173 h->non_elf = 0;
174 h->root.linker_def = 1;
175 h->type = STT_OBJECT;
176 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
177 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
179 (*bed->elf_backend_hide_symbol) (info, h, true);
180 return h;
183 bool
184 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
186 flagword flags;
187 asection *s;
188 struct elf_link_hash_entry *h;
189 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
190 struct elf_link_hash_table *htab = elf_hash_table (info);
192 /* This function may be called more than once. */
193 if (htab->sgot != NULL)
194 return true;
196 flags = bed->dynamic_sec_flags;
198 s = bfd_make_section_anyway_with_flags (abfd,
199 (bed->rela_plts_and_copies_p
200 ? ".rela.got" : ".rel.got"),
201 (bed->dynamic_sec_flags
202 | SEC_READONLY));
203 if (s == NULL
204 || !bfd_set_section_alignment (s, bed->s->log_file_align))
205 return false;
206 htab->srelgot = s;
208 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
209 if (s == NULL
210 || !bfd_set_section_alignment (s, bed->s->log_file_align))
211 return false;
212 htab->sgot = s;
214 if (bed->want_got_plt)
216 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
217 if (s == NULL
218 || !bfd_set_section_alignment (s, bed->s->log_file_align))
219 return false;
220 htab->sgotplt = s;
223 /* The first bit of the global offset table is the header. */
224 s->size += bed->got_header_size;
226 if (bed->want_got_sym)
228 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
229 (or .got.plt) section. We don't do this in the linker script
230 because we don't want to define the symbol if we are not creating
231 a global offset table. */
232 h = _bfd_elf_define_linkage_sym (abfd, info, s,
233 "_GLOBAL_OFFSET_TABLE_");
234 elf_hash_table (info)->hgot = h;
235 if (h == NULL)
236 return false;
239 return true;
242 /* Create a strtab to hold the dynamic symbol names. */
243 static bool
244 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
246 struct elf_link_hash_table *hash_table;
248 hash_table = elf_hash_table (info);
249 if (hash_table->dynobj == NULL)
251 /* We may not set dynobj, an input file holding linker created
252 dynamic sections to abfd, which may be a dynamic object with
253 its own dynamic sections. We need to find a normal input file
254 to hold linker created sections if possible. */
255 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
257 bfd *ibfd;
258 asection *s;
259 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
260 if ((ibfd->flags
261 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
262 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
263 && elf_object_id (ibfd) == elf_hash_table_id (hash_table)
264 && !((s = ibfd->sections) != NULL
265 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS))
267 abfd = ibfd;
268 break;
271 hash_table->dynobj = abfd;
274 if (hash_table->dynstr == NULL)
276 hash_table->dynstr = _bfd_elf_strtab_init ();
277 if (hash_table->dynstr == NULL)
278 return false;
280 return true;
283 /* Create some sections which will be filled in with dynamic linking
284 information. ABFD is an input file which requires dynamic sections
285 to be created. The dynamic sections take up virtual memory space
286 when the final executable is run, so we need to create them before
287 addresses are assigned to the output sections. We work out the
288 actual contents and size of these sections later. */
290 bool
291 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
293 flagword flags;
294 asection *s;
295 const struct elf_backend_data *bed;
296 struct elf_link_hash_entry *h;
298 if (! is_elf_hash_table (info->hash))
299 return false;
301 if (elf_hash_table (info)->dynamic_sections_created)
302 return true;
304 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
305 return false;
307 abfd = elf_hash_table (info)->dynobj;
308 bed = get_elf_backend_data (abfd);
310 flags = bed->dynamic_sec_flags;
312 /* A dynamically linked executable has a .interp section, but a
313 shared library does not. */
314 if (bfd_link_executable (info) && !info->nointerp)
316 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
317 flags | SEC_READONLY);
318 if (s == NULL)
319 return false;
322 /* Create sections to hold version informations. These are removed
323 if they are not needed. */
324 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
325 flags | SEC_READONLY);
326 if (s == NULL
327 || !bfd_set_section_alignment (s, bed->s->log_file_align))
328 return false;
330 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
331 flags | SEC_READONLY);
332 if (s == NULL
333 || !bfd_set_section_alignment (s, 1))
334 return false;
336 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
337 flags | SEC_READONLY);
338 if (s == NULL
339 || !bfd_set_section_alignment (s, bed->s->log_file_align))
340 return false;
342 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
343 flags | SEC_READONLY);
344 if (s == NULL
345 || !bfd_set_section_alignment (s, bed->s->log_file_align))
346 return false;
347 elf_hash_table (info)->dynsym = s;
349 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
350 flags | SEC_READONLY);
351 if (s == NULL)
352 return false;
354 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
355 if (s == NULL
356 || !bfd_set_section_alignment (s, bed->s->log_file_align))
357 return false;
358 elf_hash_table (info)->dynamic = s;
360 /* The special symbol _DYNAMIC is always set to the start of the
361 .dynamic section. We could set _DYNAMIC in a linker script, but we
362 only want to define it if we are, in fact, creating a .dynamic
363 section. We don't want to define it if there is no .dynamic
364 section, since on some ELF platforms the start up code examines it
365 to decide how to initialize the process. */
366 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
367 elf_hash_table (info)->hdynamic = h;
368 if (h == NULL)
369 return false;
371 if (info->emit_hash)
373 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
374 flags | SEC_READONLY);
375 if (s == NULL
376 || !bfd_set_section_alignment (s, bed->s->log_file_align))
377 return false;
378 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
381 if (info->emit_gnu_hash && bed->record_xhash_symbol == NULL)
383 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
384 flags | SEC_READONLY);
385 if (s == NULL
386 || !bfd_set_section_alignment (s, bed->s->log_file_align))
387 return false;
388 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
389 4 32-bit words followed by variable count of 64-bit words, then
390 variable count of 32-bit words. */
391 if (bed->s->arch_size == 64)
392 elf_section_data (s)->this_hdr.sh_entsize = 0;
393 else
394 elf_section_data (s)->this_hdr.sh_entsize = 4;
397 if (info->enable_dt_relr)
399 s = bfd_make_section_anyway_with_flags (abfd, ".relr.dyn",
400 (bed->dynamic_sec_flags
401 | SEC_READONLY));
402 if (s == NULL
403 || !bfd_set_section_alignment (s, bed->s->log_file_align))
404 return false;
405 elf_hash_table (info)->srelrdyn = s;
408 /* Let the backend create the rest of the sections. This lets the
409 backend set the right flags. The backend will normally create
410 the .got and .plt sections. */
411 if (bed->elf_backend_create_dynamic_sections == NULL
412 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
413 return false;
415 elf_hash_table (info)->dynamic_sections_created = true;
417 return true;
420 /* Create dynamic sections when linking against a dynamic object. */
422 bool
423 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
425 flagword flags, pltflags;
426 struct elf_link_hash_entry *h;
427 asection *s;
428 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
429 struct elf_link_hash_table *htab = elf_hash_table (info);
431 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
432 .rel[a].bss sections. */
433 flags = bed->dynamic_sec_flags;
435 pltflags = flags;
436 if (bed->plt_not_loaded)
437 /* We do not clear SEC_ALLOC here because we still want the OS to
438 allocate space for the section; it's just that there's nothing
439 to read in from the object file. */
440 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
441 else
442 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
443 if (bed->plt_readonly)
444 pltflags |= SEC_READONLY;
446 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
447 if (s == NULL
448 || !bfd_set_section_alignment (s, bed->plt_alignment))
449 return false;
450 htab->splt = s;
452 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
453 .plt section. */
454 if (bed->want_plt_sym)
456 h = _bfd_elf_define_linkage_sym (abfd, info, s,
457 "_PROCEDURE_LINKAGE_TABLE_");
458 elf_hash_table (info)->hplt = h;
459 if (h == NULL)
460 return false;
463 s = bfd_make_section_anyway_with_flags (abfd,
464 (bed->rela_plts_and_copies_p
465 ? ".rela.plt" : ".rel.plt"),
466 flags | SEC_READONLY);
467 if (s == NULL
468 || !bfd_set_section_alignment (s, bed->s->log_file_align))
469 return false;
470 htab->srelplt = s;
472 if (! _bfd_elf_create_got_section (abfd, info))
473 return false;
475 if (bed->want_dynbss)
477 /* The .dynbss section is a place to put symbols which are defined
478 by dynamic objects, are referenced by regular objects, and are
479 not functions. We must allocate space for them in the process
480 image and use a R_*_COPY reloc to tell the dynamic linker to
481 initialize them at run time. The linker script puts the .dynbss
482 section into the .bss section of the final image. */
483 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
484 SEC_ALLOC | SEC_LINKER_CREATED);
485 if (s == NULL)
486 return false;
487 htab->sdynbss = s;
489 if (bed->want_dynrelro)
491 /* Similarly, but for symbols that were originally in read-only
492 sections. This section doesn't really need to have contents,
493 but make it like other .data.rel.ro sections. */
494 s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
495 flags);
496 if (s == NULL)
497 return false;
498 htab->sdynrelro = s;
501 /* The .rel[a].bss section holds copy relocs. This section is not
502 normally needed. We need to create it here, though, so that the
503 linker will map it to an output section. We can't just create it
504 only if we need it, because we will not know whether we need it
505 until we have seen all the input files, and the first time the
506 main linker code calls BFD after examining all the input files
507 (size_dynamic_sections) the input sections have already been
508 mapped to the output sections. If the section turns out not to
509 be needed, we can discard it later. We will never need this
510 section when generating a shared object, since they do not use
511 copy relocs. */
512 if (bfd_link_executable (info))
514 s = bfd_make_section_anyway_with_flags (abfd,
515 (bed->rela_plts_and_copies_p
516 ? ".rela.bss" : ".rel.bss"),
517 flags | SEC_READONLY);
518 if (s == NULL
519 || !bfd_set_section_alignment (s, bed->s->log_file_align))
520 return false;
521 htab->srelbss = s;
523 if (bed->want_dynrelro)
525 s = (bfd_make_section_anyway_with_flags
526 (abfd, (bed->rela_plts_and_copies_p
527 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
528 flags | SEC_READONLY));
529 if (s == NULL
530 || !bfd_set_section_alignment (s, bed->s->log_file_align))
531 return false;
532 htab->sreldynrelro = s;
537 return true;
540 /* Record a new dynamic symbol. We record the dynamic symbols as we
541 read the input files, since we need to have a list of all of them
542 before we can determine the final sizes of the output sections.
543 Note that we may actually call this function even though we are not
544 going to output any dynamic symbols; in some cases we know that a
545 symbol should be in the dynamic symbol table, but only if there is
546 one. */
548 bool
549 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
550 struct elf_link_hash_entry *h)
552 if (h->dynindx == -1)
554 struct elf_strtab_hash *dynstr;
555 char *p;
556 const char *name;
557 size_t indx;
559 if (h->root.type == bfd_link_hash_defined
560 || h->root.type == bfd_link_hash_defweak)
562 /* An IR symbol should not be made dynamic. */
563 if (h->root.u.def.section != NULL
564 && h->root.u.def.section->owner != NULL
565 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)
566 return true;
569 /* XXX: The ABI draft says the linker must turn hidden and
570 internal symbols into STB_LOCAL symbols when producing the
571 DSO. However, if ld.so honors st_other in the dynamic table,
572 this would not be necessary. */
573 switch (ELF_ST_VISIBILITY (h->other))
575 case STV_INTERNAL:
576 case STV_HIDDEN:
577 if (h->root.type != bfd_link_hash_undefined
578 && h->root.type != bfd_link_hash_undefweak)
580 h->forced_local = 1;
581 return true;
584 default:
585 break;
588 h->dynindx = elf_hash_table (info)->dynsymcount;
589 ++elf_hash_table (info)->dynsymcount;
591 dynstr = elf_hash_table (info)->dynstr;
592 if (dynstr == NULL)
594 /* Create a strtab to hold the dynamic symbol names. */
595 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
596 if (dynstr == NULL)
597 return false;
600 char *unversioned_name = NULL;
602 /* We don't put any version information in the dynamic string
603 table. */
604 name = h->root.root.string;
605 p = strchr (name, ELF_VER_CHR);
606 if (p != NULL)
608 unversioned_name = bfd_malloc (p - name + 1);
609 memcpy (unversioned_name, name, p - name);
610 unversioned_name[p - name] = 0;
611 name = unversioned_name;
614 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
616 if (p != NULL)
617 free (unversioned_name);
619 if (indx == (size_t) -1)
620 return false;
621 h->dynstr_index = indx;
624 return true;
627 /* Mark a symbol dynamic. */
629 static void
630 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
631 struct elf_link_hash_entry *h,
632 Elf_Internal_Sym *sym)
634 struct bfd_elf_dynamic_list *d = info->dynamic_list;
636 /* It may be called more than once on the same H. */
637 if(h->dynamic || bfd_link_relocatable (info))
638 return;
640 if ((info->dynamic_data
641 && (h->type == STT_OBJECT
642 || h->type == STT_COMMON
643 || (sym != NULL
644 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
645 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
646 || (d != NULL
647 && h->non_elf
648 && (*d->match) (&d->head, NULL, h->root.root.string)))
650 h->dynamic = 1;
651 /* NB: If a symbol is made dynamic by --dynamic-list, it has
652 non-IR reference. */
653 h->root.non_ir_ref_dynamic = 1;
657 /* Record an assignment to a symbol made by a linker script. We need
658 this in case some dynamic object refers to this symbol. */
660 bool
661 bfd_elf_record_link_assignment (bfd *output_bfd,
662 struct bfd_link_info *info,
663 const char *name,
664 bool provide,
665 bool hidden)
667 struct elf_link_hash_entry *h, *hv;
668 struct elf_link_hash_table *htab;
669 const struct elf_backend_data *bed;
671 if (!is_elf_hash_table (info->hash))
672 return true;
674 htab = elf_hash_table (info);
675 h = elf_link_hash_lookup (htab, name, !provide, true, false);
676 if (h == NULL)
677 return provide;
679 if (h->root.type == bfd_link_hash_warning)
680 h = (struct elf_link_hash_entry *) h->root.u.i.link;
682 if (h->versioned == unknown)
684 /* Set versioned if symbol version is unknown. */
685 char *version = strrchr (name, ELF_VER_CHR);
686 if (version)
688 if (version > name && version[-1] != ELF_VER_CHR)
689 h->versioned = versioned_hidden;
690 else
691 h->versioned = versioned;
695 /* Symbols defined in a linker script but not referenced anywhere
696 else will have non_elf set. */
697 if (h->non_elf)
699 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
700 h->non_elf = 0;
703 switch (h->root.type)
705 case bfd_link_hash_defined:
706 case bfd_link_hash_defweak:
707 case bfd_link_hash_common:
708 break;
709 case bfd_link_hash_undefweak:
710 case bfd_link_hash_undefined:
711 /* Since we're defining the symbol, don't let it seem to have not
712 been defined. record_dynamic_symbol and size_dynamic_sections
713 may depend on this. */
714 h->root.type = bfd_link_hash_new;
715 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
716 bfd_link_repair_undef_list (&htab->root);
717 break;
718 case bfd_link_hash_new:
719 break;
720 case bfd_link_hash_indirect:
721 /* We had a versioned symbol in a dynamic library. We make the
722 the versioned symbol point to this one. */
723 bed = get_elf_backend_data (output_bfd);
724 hv = h;
725 while (hv->root.type == bfd_link_hash_indirect
726 || hv->root.type == bfd_link_hash_warning)
727 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
728 /* We don't need to update h->root.u since linker will set them
729 later. */
730 h->root.type = bfd_link_hash_undefined;
731 hv->root.type = bfd_link_hash_indirect;
732 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
733 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
734 break;
735 default:
736 BFD_FAIL ();
737 return false;
740 /* If this symbol is being provided by the linker script, and it is
741 currently defined by a dynamic object, but not by a regular
742 object, then mark it as undefined so that the generic linker will
743 force the correct value. */
744 if (provide
745 && h->def_dynamic
746 && !h->def_regular)
747 h->root.type = bfd_link_hash_undefined;
749 /* If this symbol is currently defined by a dynamic object, but not
750 by a regular object, then clear out any version information because
751 the symbol will not be associated with the dynamic object any
752 more. */
753 if (h->def_dynamic && !h->def_regular)
754 h->verinfo.verdef = NULL;
756 /* Make sure this symbol is not garbage collected. */
757 h->mark = 1;
759 h->def_regular = 1;
761 if (hidden)
763 bed = get_elf_backend_data (output_bfd);
764 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
765 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
766 (*bed->elf_backend_hide_symbol) (info, h, true);
769 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
770 and executables. */
771 if (!bfd_link_relocatable (info)
772 && h->dynindx != -1
773 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
774 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
775 h->forced_local = 1;
777 if ((h->def_dynamic
778 || h->ref_dynamic
779 || bfd_link_dll (info))
780 && !h->forced_local
781 && h->dynindx == -1)
783 if (! bfd_elf_link_record_dynamic_symbol (info, h))
784 return false;
786 /* If this is a weak defined symbol, and we know a corresponding
787 real symbol from the same dynamic object, make sure the real
788 symbol is also made into a dynamic symbol. */
789 if (h->is_weakalias)
791 struct elf_link_hash_entry *def = weakdef (h);
793 if (def->dynindx == -1
794 && !bfd_elf_link_record_dynamic_symbol (info, def))
795 return false;
799 return true;
802 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
803 success, and 2 on a failure caused by attempting to record a symbol
804 in a discarded section, eg. a discarded link-once section symbol. */
807 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
808 bfd *input_bfd,
809 long input_indx)
811 size_t amt;
812 struct elf_link_local_dynamic_entry *entry;
813 struct elf_link_hash_table *eht;
814 struct elf_strtab_hash *dynstr;
815 size_t dynstr_index;
816 char *name;
817 Elf_External_Sym_Shndx eshndx;
818 char esym[sizeof (Elf64_External_Sym)];
820 if (! is_elf_hash_table (info->hash))
821 return 0;
823 /* See if the entry exists already. */
824 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
825 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
826 return 1;
828 amt = sizeof (*entry);
829 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
830 if (entry == NULL)
831 return 0;
833 /* Go find the symbol, so that we can find it's name. */
834 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
835 1, input_indx, &entry->isym, esym, &eshndx))
837 bfd_release (input_bfd, entry);
838 return 0;
841 if (entry->isym.st_shndx != SHN_UNDEF
842 && entry->isym.st_shndx < SHN_LORESERVE)
844 asection *s;
846 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
847 if (s == NULL || bfd_is_abs_section (s->output_section))
849 /* We can still bfd_release here as nothing has done another
850 bfd_alloc. We can't do this later in this function. */
851 bfd_release (input_bfd, entry);
852 return 2;
856 name = (bfd_elf_string_from_elf_section
857 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
858 entry->isym.st_name));
860 dynstr = elf_hash_table (info)->dynstr;
861 if (dynstr == NULL)
863 /* Create a strtab to hold the dynamic symbol names. */
864 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
865 if (dynstr == NULL)
866 return 0;
869 dynstr_index = _bfd_elf_strtab_add (dynstr, name, false);
870 if (dynstr_index == (size_t) -1)
871 return 0;
872 entry->isym.st_name = dynstr_index;
874 eht = elf_hash_table (info);
876 entry->next = eht->dynlocal;
877 eht->dynlocal = entry;
878 entry->input_bfd = input_bfd;
879 entry->input_indx = input_indx;
880 eht->dynsymcount++;
882 /* Whatever binding the symbol had before, it's now local. */
883 entry->isym.st_info
884 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
886 /* The dynindx will be set at the end of size_dynamic_sections. */
888 return 1;
891 /* Return the dynindex of a local dynamic symbol. */
893 long
894 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
895 bfd *input_bfd,
896 long input_indx)
898 struct elf_link_local_dynamic_entry *e;
900 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
901 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
902 return e->dynindx;
903 return -1;
906 /* This function is used to renumber the dynamic symbols, if some of
907 them are removed because they are marked as local. This is called
908 via elf_link_hash_traverse. */
910 static bool
911 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
912 void *data)
914 size_t *count = (size_t *) data;
916 if (h->forced_local)
917 return true;
919 if (h->dynindx != -1)
920 h->dynindx = ++(*count);
922 return true;
926 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
927 STB_LOCAL binding. */
929 static bool
930 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
931 void *data)
933 size_t *count = (size_t *) data;
935 if (!h->forced_local)
936 return true;
938 if (h->dynindx != -1)
939 h->dynindx = ++(*count);
941 return true;
944 /* Return true if the dynamic symbol for a given section should be
945 omitted when creating a shared library. */
946 bool
947 _bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED,
948 struct bfd_link_info *info,
949 asection *p)
951 struct elf_link_hash_table *htab;
952 asection *ip;
954 switch (elf_section_data (p)->this_hdr.sh_type)
956 case SHT_PROGBITS:
957 case SHT_NOBITS:
958 /* If sh_type is yet undecided, assume it could be
959 SHT_PROGBITS/SHT_NOBITS. */
960 case SHT_NULL:
961 htab = elf_hash_table (info);
962 if (htab->text_index_section != NULL)
963 return p != htab->text_index_section && p != htab->data_index_section;
965 return (htab->dynobj != NULL
966 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
967 && ip->output_section == p);
969 /* There shouldn't be section relative relocations
970 against any other section. */
971 default:
972 return true;
976 bool
977 _bfd_elf_omit_section_dynsym_all
978 (bfd *output_bfd ATTRIBUTE_UNUSED,
979 struct bfd_link_info *info ATTRIBUTE_UNUSED,
980 asection *p ATTRIBUTE_UNUSED)
982 return true;
985 /* Assign dynsym indices. In a shared library we generate a section
986 symbol for each output section, which come first. Next come symbols
987 which have been forced to local binding. Then all of the back-end
988 allocated local dynamic syms, followed by the rest of the global
989 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
990 (This prevents the early call before elf_backend_init_index_section
991 and strip_excluded_output_sections setting dynindx for sections
992 that are stripped.) */
994 static unsigned long
995 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
996 struct bfd_link_info *info,
997 unsigned long *section_sym_count)
999 unsigned long dynsymcount = 0;
1000 bool do_sec = section_sym_count != NULL;
1002 if (bfd_link_pic (info)
1003 || elf_hash_table (info)->is_relocatable_executable)
1005 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
1006 asection *p;
1007 for (p = output_bfd->sections; p ; p = p->next)
1008 if ((p->flags & SEC_EXCLUDE) == 0
1009 && (p->flags & SEC_ALLOC) != 0
1010 && elf_hash_table (info)->dynamic_relocs
1011 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
1013 ++dynsymcount;
1014 if (do_sec)
1015 elf_section_data (p)->dynindx = dynsymcount;
1017 else if (do_sec)
1018 elf_section_data (p)->dynindx = 0;
1020 if (do_sec)
1021 *section_sym_count = dynsymcount;
1023 elf_link_hash_traverse (elf_hash_table (info),
1024 elf_link_renumber_local_hash_table_dynsyms,
1025 &dynsymcount);
1027 if (elf_hash_table (info)->dynlocal)
1029 struct elf_link_local_dynamic_entry *p;
1030 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
1031 p->dynindx = ++dynsymcount;
1033 elf_hash_table (info)->local_dynsymcount = dynsymcount;
1035 elf_link_hash_traverse (elf_hash_table (info),
1036 elf_link_renumber_hash_table_dynsyms,
1037 &dynsymcount);
1039 /* There is an unused NULL entry at the head of the table which we
1040 must account for in our count even if the table is empty since it
1041 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
1042 .dynamic section. */
1043 dynsymcount++;
1045 elf_hash_table (info)->dynsymcount = dynsymcount;
1046 return dynsymcount;
1049 /* Merge st_other field. */
1051 static void
1052 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
1053 unsigned int st_other, asection *sec,
1054 bool definition, bool dynamic)
1056 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1058 /* If st_other has a processor-specific meaning, specific
1059 code might be needed here. */
1060 if (bed->elf_backend_merge_symbol_attribute)
1061 (*bed->elf_backend_merge_symbol_attribute) (h, st_other, definition,
1062 dynamic);
1064 if (!dynamic)
1066 unsigned symvis = ELF_ST_VISIBILITY (st_other);
1067 unsigned hvis = ELF_ST_VISIBILITY (h->other);
1069 /* Keep the most constraining visibility. Leave the remainder
1070 of the st_other field to elf_backend_merge_symbol_attribute. */
1071 if (symvis - 1 < hvis - 1)
1072 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
1074 else if (definition
1075 && ELF_ST_VISIBILITY (st_other) != STV_DEFAULT
1076 && (sec->flags & SEC_READONLY) == 0)
1077 h->protected_def = 1;
1080 /* This function is called when we want to merge a new symbol with an
1081 existing symbol. It handles the various cases which arise when we
1082 find a definition in a dynamic object, or when there is already a
1083 definition in a dynamic object. The new symbol is described by
1084 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1085 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1086 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1087 of an old common symbol. We set OVERRIDE if the old symbol is
1088 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1089 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1090 to change. By OK to change, we mean that we shouldn't warn if the
1091 type or size does change. */
1093 static bool
1094 _bfd_elf_merge_symbol (bfd *abfd,
1095 struct bfd_link_info *info,
1096 const char *name,
1097 Elf_Internal_Sym *sym,
1098 asection **psec,
1099 bfd_vma *pvalue,
1100 struct elf_link_hash_entry **sym_hash,
1101 bfd **poldbfd,
1102 bool *pold_weak,
1103 unsigned int *pold_alignment,
1104 bool *skip,
1105 bfd **override,
1106 bool *type_change_ok,
1107 bool *size_change_ok,
1108 bool *matched)
1110 asection *sec, *oldsec;
1111 struct elf_link_hash_entry *h;
1112 struct elf_link_hash_entry *hi;
1113 struct elf_link_hash_entry *flip;
1114 int bind;
1115 bfd *oldbfd;
1116 bool newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
1117 bool newweak, oldweak, newfunc, oldfunc;
1118 const struct elf_backend_data *bed;
1119 char *new_version;
1120 bool default_sym = *matched;
1121 struct elf_link_hash_table *htab;
1123 *skip = false;
1124 *override = NULL;
1126 sec = *psec;
1127 bind = ELF_ST_BIND (sym->st_info);
1129 if (! bfd_is_und_section (sec))
1130 h = elf_link_hash_lookup (elf_hash_table (info), name, true, false, false);
1131 else
1132 h = ((struct elf_link_hash_entry *)
1133 bfd_wrapped_link_hash_lookup (abfd, info, name, true, false, false));
1134 if (h == NULL)
1135 return false;
1136 *sym_hash = h;
1138 bed = get_elf_backend_data (abfd);
1140 /* NEW_VERSION is the symbol version of the new symbol. */
1141 if (h->versioned != unversioned)
1143 /* Symbol version is unknown or versioned. */
1144 new_version = strrchr (name, ELF_VER_CHR);
1145 if (new_version)
1147 if (h->versioned == unknown)
1149 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1150 h->versioned = versioned_hidden;
1151 else
1152 h->versioned = versioned;
1154 new_version += 1;
1155 if (new_version[0] == '\0')
1156 new_version = NULL;
1158 else
1159 h->versioned = unversioned;
1161 else
1162 new_version = NULL;
1164 /* For merging, we only care about real symbols. But we need to make
1165 sure that indirect symbol dynamic flags are updated. */
1166 hi = h;
1167 while (h->root.type == bfd_link_hash_indirect
1168 || h->root.type == bfd_link_hash_warning)
1169 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1171 if (!*matched)
1173 if (hi == h || h->root.type == bfd_link_hash_new)
1174 *matched = true;
1175 else
1177 /* OLD_HIDDEN is true if the existing symbol is only visible
1178 to the symbol with the same symbol version. NEW_HIDDEN is
1179 true if the new symbol is only visible to the symbol with
1180 the same symbol version. */
1181 bool old_hidden = h->versioned == versioned_hidden;
1182 bool new_hidden = hi->versioned == versioned_hidden;
1183 if (!old_hidden && !new_hidden)
1184 /* The new symbol matches the existing symbol if both
1185 aren't hidden. */
1186 *matched = true;
1187 else
1189 /* OLD_VERSION is the symbol version of the existing
1190 symbol. */
1191 char *old_version;
1193 if (h->versioned >= versioned)
1194 old_version = strrchr (h->root.root.string,
1195 ELF_VER_CHR) + 1;
1196 else
1197 old_version = NULL;
1199 /* The new symbol matches the existing symbol if they
1200 have the same symbol version. */
1201 *matched = (old_version == new_version
1202 || (old_version != NULL
1203 && new_version != NULL
1204 && strcmp (old_version, new_version) == 0));
1209 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1210 existing symbol. */
1212 oldbfd = NULL;
1213 oldsec = NULL;
1214 switch (h->root.type)
1216 default:
1217 break;
1219 case bfd_link_hash_undefined:
1220 case bfd_link_hash_undefweak:
1221 oldbfd = h->root.u.undef.abfd;
1222 break;
1224 case bfd_link_hash_defined:
1225 case bfd_link_hash_defweak:
1226 oldbfd = h->root.u.def.section->owner;
1227 oldsec = h->root.u.def.section;
1228 break;
1230 case bfd_link_hash_common:
1231 oldbfd = h->root.u.c.p->section->owner;
1232 oldsec = h->root.u.c.p->section;
1233 if (pold_alignment)
1234 *pold_alignment = h->root.u.c.p->alignment_power;
1235 break;
1237 if (poldbfd && *poldbfd == NULL)
1238 *poldbfd = oldbfd;
1240 /* Differentiate strong and weak symbols. */
1241 newweak = bind == STB_WEAK;
1242 oldweak = (h->root.type == bfd_link_hash_defweak
1243 || h->root.type == bfd_link_hash_undefweak);
1244 if (pold_weak)
1245 *pold_weak = oldweak;
1247 /* We have to check it for every instance since the first few may be
1248 references and not all compilers emit symbol type for undefined
1249 symbols. */
1250 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1252 htab = elf_hash_table (info);
1254 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1255 respectively, is from a dynamic object. */
1257 newdyn = (abfd->flags & DYNAMIC) != 0;
1259 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1260 syms and defined syms in dynamic libraries respectively.
1261 ref_dynamic on the other hand can be set for a symbol defined in
1262 a dynamic library, and def_dynamic may not be set; When the
1263 definition in a dynamic lib is overridden by a definition in the
1264 executable use of the symbol in the dynamic lib becomes a
1265 reference to the executable symbol. */
1266 if (newdyn)
1268 if (bfd_is_und_section (sec))
1270 if (bind != STB_WEAK)
1272 h->ref_dynamic_nonweak = 1;
1273 hi->ref_dynamic_nonweak = 1;
1276 else
1278 /* Update the existing symbol only if they match. */
1279 if (*matched)
1280 h->dynamic_def = 1;
1281 hi->dynamic_def = 1;
1285 /* If we just created the symbol, mark it as being an ELF symbol.
1286 Other than that, there is nothing to do--there is no merge issue
1287 with a newly defined symbol--so we just return. */
1289 if (h->root.type == bfd_link_hash_new)
1291 h->non_elf = 0;
1292 return true;
1295 /* In cases involving weak versioned symbols, we may wind up trying
1296 to merge a symbol with itself. Catch that here, to avoid the
1297 confusion that results if we try to override a symbol with
1298 itself. The additional tests catch cases like
1299 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1300 dynamic object, which we do want to handle here. */
1301 if (abfd == oldbfd
1302 && (newweak || oldweak)
1303 && ((abfd->flags & DYNAMIC) == 0
1304 || !h->def_regular))
1305 return true;
1307 olddyn = false;
1308 if (oldbfd != NULL)
1309 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1310 else if (oldsec != NULL)
1312 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1313 indices used by MIPS ELF. */
1314 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1317 /* Set non_ir_ref_dynamic only when not handling DT_NEEDED entries. */
1318 if (!htab->handling_dt_needed
1319 && oldbfd != NULL
1320 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN))
1322 if (newdyn != olddyn)
1324 /* Handle a case where plugin_notice won't be called and thus
1325 won't set the non_ir_ref flags on the first pass over
1326 symbols. */
1327 h->root.non_ir_ref_dynamic = true;
1328 hi->root.non_ir_ref_dynamic = true;
1330 else if ((oldbfd->flags & BFD_PLUGIN) != 0
1331 && hi->root.type == bfd_link_hash_indirect)
1333 /* Change indirect symbol from IR to undefined. */
1334 hi->root.type = bfd_link_hash_undefined;
1335 hi->root.u.undef.abfd = oldbfd;
1339 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1340 respectively, appear to be a definition rather than reference. */
1342 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1344 olddef = (h->root.type != bfd_link_hash_undefined
1345 && h->root.type != bfd_link_hash_undefweak
1346 && h->root.type != bfd_link_hash_common);
1348 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1349 respectively, appear to be a function. */
1351 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1352 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1354 oldfunc = (h->type != STT_NOTYPE
1355 && bed->is_function_type (h->type));
1357 if (!(newfunc && oldfunc)
1358 && ELF_ST_TYPE (sym->st_info) != h->type
1359 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1360 && h->type != STT_NOTYPE
1361 && (newdef || bfd_is_com_section (sec))
1362 && (olddef || h->root.type == bfd_link_hash_common))
1364 /* If creating a default indirect symbol ("foo" or "foo@") from
1365 a dynamic versioned definition ("foo@@") skip doing so if
1366 there is an existing regular definition with a different
1367 type. We don't want, for example, a "time" variable in the
1368 executable overriding a "time" function in a shared library. */
1369 if (newdyn
1370 && !olddyn)
1372 *skip = true;
1373 return true;
1376 /* When adding a symbol from a regular object file after we have
1377 created indirect symbols, undo the indirection and any
1378 dynamic state. */
1379 if (hi != h
1380 && !newdyn
1381 && olddyn)
1383 h = hi;
1384 (*bed->elf_backend_hide_symbol) (info, h, true);
1385 h->forced_local = 0;
1386 h->ref_dynamic = 0;
1387 h->def_dynamic = 0;
1388 h->dynamic_def = 0;
1389 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1391 h->root.type = bfd_link_hash_undefined;
1392 h->root.u.undef.abfd = abfd;
1394 else
1396 h->root.type = bfd_link_hash_new;
1397 h->root.u.undef.abfd = NULL;
1399 return true;
1403 /* Check TLS symbols. We don't check undefined symbols introduced
1404 by "ld -u" which have no type (and oldbfd NULL), and we don't
1405 check symbols from plugins because they also have no type. */
1406 if (oldbfd != NULL
1407 && (oldbfd->flags & BFD_PLUGIN) == 0
1408 && (abfd->flags & BFD_PLUGIN) == 0
1409 && ELF_ST_TYPE (sym->st_info) != h->type
1410 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1412 bfd *ntbfd, *tbfd;
1413 bool ntdef, tdef;
1414 asection *ntsec, *tsec;
1416 if (h->type == STT_TLS)
1418 ntbfd = abfd;
1419 ntsec = sec;
1420 ntdef = newdef;
1421 tbfd = oldbfd;
1422 tsec = oldsec;
1423 tdef = olddef;
1425 else
1427 ntbfd = oldbfd;
1428 ntsec = oldsec;
1429 ntdef = olddef;
1430 tbfd = abfd;
1431 tsec = sec;
1432 tdef = newdef;
1435 if (tdef && ntdef)
1436 _bfd_error_handler
1437 /* xgettext:c-format */
1438 (_("%s: TLS definition in %pB section %pA "
1439 "mismatches non-TLS definition in %pB section %pA"),
1440 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
1441 else if (!tdef && !ntdef)
1442 _bfd_error_handler
1443 /* xgettext:c-format */
1444 (_("%s: TLS reference in %pB "
1445 "mismatches non-TLS reference in %pB"),
1446 h->root.root.string, tbfd, ntbfd);
1447 else if (tdef)
1448 _bfd_error_handler
1449 /* xgettext:c-format */
1450 (_("%s: TLS definition in %pB section %pA "
1451 "mismatches non-TLS reference in %pB"),
1452 h->root.root.string, tbfd, tsec, ntbfd);
1453 else
1454 _bfd_error_handler
1455 /* xgettext:c-format */
1456 (_("%s: TLS reference in %pB "
1457 "mismatches non-TLS definition in %pB section %pA"),
1458 h->root.root.string, tbfd, ntbfd, ntsec);
1460 bfd_set_error (bfd_error_bad_value);
1461 return false;
1464 /* If the old symbol has non-default visibility, we ignore the new
1465 definition from a dynamic object. */
1466 if (newdyn
1467 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1468 && !bfd_is_und_section (sec))
1470 *skip = true;
1471 /* Make sure this symbol is dynamic. */
1472 h->ref_dynamic = 1;
1473 hi->ref_dynamic = 1;
1474 /* A protected symbol has external availability. Make sure it is
1475 recorded as dynamic.
1477 FIXME: Should we check type and size for protected symbol? */
1478 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1479 return bfd_elf_link_record_dynamic_symbol (info, h);
1480 else
1481 return true;
1483 else if (!newdyn
1484 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1485 && h->def_dynamic)
1487 /* If the new symbol with non-default visibility comes from a
1488 relocatable file and the old definition comes from a dynamic
1489 object, we remove the old definition. */
1490 if (hi->root.type == bfd_link_hash_indirect)
1492 /* Handle the case where the old dynamic definition is
1493 default versioned. We need to copy the symbol info from
1494 the symbol with default version to the normal one if it
1495 was referenced before. */
1496 if (h->ref_regular)
1498 hi->root.type = h->root.type;
1499 h->root.type = bfd_link_hash_indirect;
1500 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1502 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1503 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1505 /* If the new symbol is hidden or internal, completely undo
1506 any dynamic link state. */
1507 (*bed->elf_backend_hide_symbol) (info, h, true);
1508 h->forced_local = 0;
1509 h->ref_dynamic = 0;
1511 else
1512 h->ref_dynamic = 1;
1514 h->def_dynamic = 0;
1515 /* FIXME: Should we check type and size for protected symbol? */
1516 h->size = 0;
1517 h->type = 0;
1519 h = hi;
1521 else
1522 h = hi;
1525 /* If the old symbol was undefined before, then it will still be
1526 on the undefs list. If the new symbol is undefined or
1527 common, we can't make it bfd_link_hash_new here, because new
1528 undefined or common symbols will be added to the undefs list
1529 by _bfd_generic_link_add_one_symbol. Symbols may not be
1530 added twice to the undefs list. Also, if the new symbol is
1531 undefweak then we don't want to lose the strong undef. */
1532 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1534 h->root.type = bfd_link_hash_undefined;
1535 h->root.u.undef.abfd = abfd;
1537 else
1539 h->root.type = bfd_link_hash_new;
1540 h->root.u.undef.abfd = NULL;
1543 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1545 /* If the new symbol is hidden or internal, completely undo
1546 any dynamic link state. */
1547 (*bed->elf_backend_hide_symbol) (info, h, true);
1548 h->forced_local = 0;
1549 h->ref_dynamic = 0;
1551 else
1552 h->ref_dynamic = 1;
1553 h->def_dynamic = 0;
1554 /* FIXME: Should we check type and size for protected symbol? */
1555 h->size = 0;
1556 h->type = 0;
1557 return true;
1560 /* If a new weak symbol definition comes from a regular file and the
1561 old symbol comes from a dynamic library, we treat the new one as
1562 strong. Similarly, an old weak symbol definition from a regular
1563 file is treated as strong when the new symbol comes from a dynamic
1564 library. Further, an old weak symbol from a dynamic library is
1565 treated as strong if the new symbol is from a dynamic library.
1566 This reflects the way glibc's ld.so works.
1568 Also allow a weak symbol to override a linker script symbol
1569 defined by an early pass over the script. This is done so the
1570 linker knows the symbol is defined in an object file, for the
1571 DEFINED script function.
1573 Do this before setting *type_change_ok or *size_change_ok so that
1574 we warn properly when dynamic library symbols are overridden. */
1576 if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
1577 newweak = false;
1578 if (olddef && newdyn)
1579 oldweak = false;
1581 /* Allow changes between different types of function symbol. */
1582 if (newfunc && oldfunc)
1583 *type_change_ok = true;
1585 /* It's OK to change the type if either the existing symbol or the
1586 new symbol is weak. A type change is also OK if the old symbol
1587 is undefined and the new symbol is defined. */
1589 if (oldweak
1590 || newweak
1591 || (newdef
1592 && h->root.type == bfd_link_hash_undefined))
1593 *type_change_ok = true;
1595 /* It's OK to change the size if either the existing symbol or the
1596 new symbol is weak, or if the old symbol is undefined. */
1598 if (*type_change_ok
1599 || h->root.type == bfd_link_hash_undefined)
1600 *size_change_ok = true;
1602 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1603 symbol, respectively, appears to be a common symbol in a dynamic
1604 object. If a symbol appears in an uninitialized section, and is
1605 not weak, and is not a function, then it may be a common symbol
1606 which was resolved when the dynamic object was created. We want
1607 to treat such symbols specially, because they raise special
1608 considerations when setting the symbol size: if the symbol
1609 appears as a common symbol in a regular object, and the size in
1610 the regular object is larger, we must make sure that we use the
1611 larger size. This problematic case can always be avoided in C,
1612 but it must be handled correctly when using Fortran shared
1613 libraries.
1615 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1616 likewise for OLDDYNCOMMON and OLDDEF.
1618 Note that this test is just a heuristic, and that it is quite
1619 possible to have an uninitialized symbol in a shared object which
1620 is really a definition, rather than a common symbol. This could
1621 lead to some minor confusion when the symbol really is a common
1622 symbol in some regular object. However, I think it will be
1623 harmless. */
1625 if (newdyn
1626 && newdef
1627 && !newweak
1628 && (sec->flags & SEC_ALLOC) != 0
1629 && (sec->flags & SEC_LOAD) == 0
1630 && sym->st_size > 0
1631 && !newfunc)
1632 newdyncommon = true;
1633 else
1634 newdyncommon = false;
1636 if (olddyn
1637 && olddef
1638 && h->root.type == bfd_link_hash_defined
1639 && h->def_dynamic
1640 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1641 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1642 && h->size > 0
1643 && !oldfunc)
1644 olddyncommon = true;
1645 else
1646 olddyncommon = false;
1648 /* We now know everything about the old and new symbols. We ask the
1649 backend to check if we can merge them. */
1650 if (bed->merge_symbol != NULL)
1652 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1653 return false;
1654 sec = *psec;
1657 /* There are multiple definitions of a normal symbol. Skip the
1658 default symbol as well as definition from an IR object. */
1659 if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
1660 && !default_sym && h->def_regular
1661 && !(oldbfd != NULL
1662 && (oldbfd->flags & BFD_PLUGIN) != 0
1663 && (abfd->flags & BFD_PLUGIN) == 0))
1665 /* Handle a multiple definition. */
1666 (*info->callbacks->multiple_definition) (info, &h->root,
1667 abfd, sec, *pvalue);
1668 *skip = true;
1669 return true;
1672 /* If both the old and the new symbols look like common symbols in a
1673 dynamic object, set the size of the symbol to the larger of the
1674 two. */
1676 if (olddyncommon
1677 && newdyncommon
1678 && sym->st_size != h->size)
1680 /* Since we think we have two common symbols, issue a multiple
1681 common warning if desired. Note that we only warn if the
1682 size is different. If the size is the same, we simply let
1683 the old symbol override the new one as normally happens with
1684 symbols defined in dynamic objects. */
1686 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1687 bfd_link_hash_common, sym->st_size);
1688 if (sym->st_size > h->size)
1689 h->size = sym->st_size;
1691 *size_change_ok = true;
1694 /* If we are looking at a dynamic object, and we have found a
1695 definition, we need to see if the symbol was already defined by
1696 some other object. If so, we want to use the existing
1697 definition, and we do not want to report a multiple symbol
1698 definition error; we do this by clobbering *PSEC to be
1699 bfd_und_section_ptr.
1701 We treat a common symbol as a definition if the symbol in the
1702 shared library is a function, since common symbols always
1703 represent variables; this can cause confusion in principle, but
1704 any such confusion would seem to indicate an erroneous program or
1705 shared library. We also permit a common symbol in a regular
1706 object to override a weak symbol in a shared object. */
1708 if (newdyn
1709 && newdef
1710 && (olddef
1711 || (h->root.type == bfd_link_hash_common
1712 && (newweak || newfunc))))
1714 *override = abfd;
1715 newdef = false;
1716 newdyncommon = false;
1718 *psec = sec = bfd_und_section_ptr;
1719 *size_change_ok = true;
1721 /* If we get here when the old symbol is a common symbol, then
1722 we are explicitly letting it override a weak symbol or
1723 function in a dynamic object, and we don't want to warn about
1724 a type change. If the old symbol is a defined symbol, a type
1725 change warning may still be appropriate. */
1727 if (h->root.type == bfd_link_hash_common)
1728 *type_change_ok = true;
1731 /* Handle the special case of an old common symbol merging with a
1732 new symbol which looks like a common symbol in a shared object.
1733 We change *PSEC and *PVALUE to make the new symbol look like a
1734 common symbol, and let _bfd_generic_link_add_one_symbol do the
1735 right thing. */
1737 if (newdyncommon
1738 && h->root.type == bfd_link_hash_common)
1740 *override = oldbfd;
1741 newdef = false;
1742 newdyncommon = false;
1743 *pvalue = sym->st_size;
1744 *psec = sec = bed->common_section (oldsec);
1745 *size_change_ok = true;
1748 /* Skip weak definitions of symbols that are already defined. */
1749 if (newdef && olddef && newweak)
1751 /* Don't skip new non-IR weak syms. */
1752 if (!(oldbfd != NULL
1753 && (oldbfd->flags & BFD_PLUGIN) != 0
1754 && (abfd->flags & BFD_PLUGIN) == 0))
1756 newdef = false;
1757 *skip = true;
1760 /* Merge st_other. If the symbol already has a dynamic index,
1761 but visibility says it should not be visible, turn it into a
1762 local symbol. */
1763 elf_merge_st_other (abfd, h, sym->st_other, sec, newdef, newdyn);
1764 if (h->dynindx != -1)
1765 switch (ELF_ST_VISIBILITY (h->other))
1767 case STV_INTERNAL:
1768 case STV_HIDDEN:
1769 (*bed->elf_backend_hide_symbol) (info, h, true);
1770 break;
1774 /* If the old symbol is from a dynamic object, and the new symbol is
1775 a definition which is not from a dynamic object, then the new
1776 symbol overrides the old symbol. Symbols from regular files
1777 always take precedence over symbols from dynamic objects, even if
1778 they are defined after the dynamic object in the link.
1780 As above, we again permit a common symbol in a regular object to
1781 override a definition in a shared object if the shared object
1782 symbol is a function or is weak. */
1784 flip = NULL;
1785 if (!newdyn
1786 && (newdef
1787 || (bfd_is_com_section (sec)
1788 && (oldweak || oldfunc)))
1789 && olddyn
1790 && olddef
1791 && h->def_dynamic)
1793 /* Change the hash table entry to undefined, and let
1794 _bfd_generic_link_add_one_symbol do the right thing with the
1795 new definition. */
1797 h->root.type = bfd_link_hash_undefined;
1798 h->root.u.undef.abfd = h->root.u.def.section->owner;
1799 *size_change_ok = true;
1801 olddef = false;
1802 olddyncommon = false;
1804 /* We again permit a type change when a common symbol may be
1805 overriding a function. */
1807 if (bfd_is_com_section (sec))
1809 if (oldfunc)
1811 /* If a common symbol overrides a function, make sure
1812 that it isn't defined dynamically nor has type
1813 function. */
1814 h->def_dynamic = 0;
1815 h->type = STT_NOTYPE;
1817 *type_change_ok = true;
1820 if (hi->root.type == bfd_link_hash_indirect)
1821 flip = hi;
1822 else
1823 /* This union may have been set to be non-NULL when this symbol
1824 was seen in a dynamic object. We must force the union to be
1825 NULL, so that it is correct for a regular symbol. */
1826 h->verinfo.vertree = NULL;
1829 /* Handle the special case of a new common symbol merging with an
1830 old symbol that looks like it might be a common symbol defined in
1831 a shared object. Note that we have already handled the case in
1832 which a new common symbol should simply override the definition
1833 in the shared library. */
1835 if (! newdyn
1836 && bfd_is_com_section (sec)
1837 && olddyncommon)
1839 /* It would be best if we could set the hash table entry to a
1840 common symbol, but we don't know what to use for the section
1841 or the alignment. */
1842 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1843 bfd_link_hash_common, sym->st_size);
1845 /* If the presumed common symbol in the dynamic object is
1846 larger, pretend that the new symbol has its size. */
1848 if (h->size > *pvalue)
1849 *pvalue = h->size;
1851 /* We need to remember the alignment required by the symbol
1852 in the dynamic object. */
1853 BFD_ASSERT (pold_alignment);
1854 *pold_alignment = h->root.u.def.section->alignment_power;
1856 olddef = false;
1857 olddyncommon = false;
1859 h->root.type = bfd_link_hash_undefined;
1860 h->root.u.undef.abfd = h->root.u.def.section->owner;
1862 *size_change_ok = true;
1863 *type_change_ok = true;
1865 if (hi->root.type == bfd_link_hash_indirect)
1866 flip = hi;
1867 else
1868 h->verinfo.vertree = NULL;
1871 if (flip != NULL)
1873 /* Handle the case where we had a versioned symbol in a dynamic
1874 library and now find a definition in a normal object. In this
1875 case, we make the versioned symbol point to the normal one. */
1876 flip->root.type = h->root.type;
1877 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1878 h->root.type = bfd_link_hash_indirect;
1879 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1880 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1881 if (h->def_dynamic)
1883 h->def_dynamic = 0;
1884 flip->ref_dynamic = 1;
1888 return true;
1891 /* This function is called to create an indirect symbol from the
1892 default for the symbol with the default version if needed. The
1893 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1894 set DYNSYM if the new indirect symbol is dynamic. */
1896 static bool
1897 _bfd_elf_add_default_symbol (bfd *abfd,
1898 struct bfd_link_info *info,
1899 struct elf_link_hash_entry *h,
1900 const char *name,
1901 Elf_Internal_Sym *sym,
1902 asection *sec,
1903 bfd_vma value,
1904 bfd **poldbfd,
1905 bool *dynsym)
1907 bool type_change_ok;
1908 bool size_change_ok;
1909 bool skip;
1910 char *shortname;
1911 struct elf_link_hash_entry *hi;
1912 struct bfd_link_hash_entry *bh;
1913 const struct elf_backend_data *bed;
1914 bool collect;
1915 bool dynamic;
1916 bfd *override;
1917 char *p;
1918 size_t len, shortlen;
1919 asection *tmp_sec;
1920 bool matched;
1922 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1923 return true;
1925 /* If this symbol has a version, and it is the default version, we
1926 create an indirect symbol from the default name to the fully
1927 decorated name. This will cause external references which do not
1928 specify a version to be bound to this version of the symbol. */
1929 p = strchr (name, ELF_VER_CHR);
1930 if (h->versioned == unknown)
1932 if (p == NULL)
1934 h->versioned = unversioned;
1935 return true;
1937 else
1939 if (p[1] != ELF_VER_CHR)
1941 h->versioned = versioned_hidden;
1942 return true;
1944 else
1945 h->versioned = versioned;
1948 else
1950 /* PR ld/19073: We may see an unversioned definition after the
1951 default version. */
1952 if (p == NULL)
1953 return true;
1956 bed = get_elf_backend_data (abfd);
1957 collect = bed->collect;
1958 dynamic = (abfd->flags & DYNAMIC) != 0;
1960 shortlen = p - name;
1961 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1962 if (shortname == NULL)
1963 return false;
1964 memcpy (shortname, name, shortlen);
1965 shortname[shortlen] = '\0';
1967 /* We are going to create a new symbol. Merge it with any existing
1968 symbol with this name. For the purposes of the merge, act as
1969 though we were defining the symbol we just defined, although we
1970 actually going to define an indirect symbol. */
1971 type_change_ok = false;
1972 size_change_ok = false;
1973 matched = true;
1974 tmp_sec = sec;
1975 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1976 &hi, poldbfd, NULL, NULL, &skip, &override,
1977 &type_change_ok, &size_change_ok, &matched))
1978 return false;
1980 if (skip)
1981 goto nondefault;
1983 if (hi->def_regular || ELF_COMMON_DEF_P (hi))
1985 /* If the undecorated symbol will have a version added by a
1986 script different to H, then don't indirect to/from the
1987 undecorated symbol. This isn't ideal because we may not yet
1988 have seen symbol versions, if given by a script on the
1989 command line rather than via --version-script. */
1990 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1992 bool hide;
1994 hi->verinfo.vertree
1995 = bfd_find_version_for_sym (info->version_info,
1996 hi->root.root.string, &hide);
1997 if (hi->verinfo.vertree != NULL && hide)
1999 (*bed->elf_backend_hide_symbol) (info, hi, true);
2000 goto nondefault;
2003 if (hi->verinfo.vertree != NULL
2004 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
2005 goto nondefault;
2008 if (! override)
2010 /* Add the default symbol if not performing a relocatable link. */
2011 if (! bfd_link_relocatable (info))
2013 bh = &hi->root;
2014 if (bh->type == bfd_link_hash_defined
2015 && bh->u.def.section->owner != NULL
2016 && (bh->u.def.section->owner->flags & BFD_PLUGIN) != 0)
2018 /* Mark the previous definition from IR object as
2019 undefined so that the generic linker will override
2020 it. */
2021 bh->type = bfd_link_hash_undefined;
2022 bh->u.undef.abfd = bh->u.def.section->owner;
2024 if (! (_bfd_generic_link_add_one_symbol
2025 (info, abfd, shortname, BSF_INDIRECT,
2026 bfd_ind_section_ptr,
2027 0, name, false, collect, &bh)))
2028 return false;
2029 hi = (struct elf_link_hash_entry *) bh;
2032 else
2034 /* In this case the symbol named SHORTNAME is overriding the
2035 indirect symbol we want to add. We were planning on making
2036 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
2037 is the name without a version. NAME is the fully versioned
2038 name, and it is the default version.
2040 Overriding means that we already saw a definition for the
2041 symbol SHORTNAME in a regular object, and it is overriding
2042 the symbol defined in the dynamic object.
2044 When this happens, we actually want to change NAME, the
2045 symbol we just added, to refer to SHORTNAME. This will cause
2046 references to NAME in the shared object to become references
2047 to SHORTNAME in the regular object. This is what we expect
2048 when we override a function in a shared object: that the
2049 references in the shared object will be mapped to the
2050 definition in the regular object. */
2052 while (hi->root.type == bfd_link_hash_indirect
2053 || hi->root.type == bfd_link_hash_warning)
2054 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2056 h->root.type = bfd_link_hash_indirect;
2057 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
2058 if (h->def_dynamic)
2060 h->def_dynamic = 0;
2061 hi->ref_dynamic = 1;
2062 if (hi->ref_regular
2063 || hi->def_regular)
2065 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
2066 return false;
2070 /* Now set HI to H, so that the following code will set the
2071 other fields correctly. */
2072 hi = h;
2075 /* Check if HI is a warning symbol. */
2076 if (hi->root.type == bfd_link_hash_warning)
2077 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2079 /* If there is a duplicate definition somewhere, then HI may not
2080 point to an indirect symbol. We will have reported an error to
2081 the user in that case. */
2083 if (hi->root.type == bfd_link_hash_indirect)
2085 struct elf_link_hash_entry *ht;
2087 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
2088 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
2090 /* If we first saw a reference to SHORTNAME with non-default
2091 visibility, merge that visibility to the @@VER symbol. */
2092 elf_merge_st_other (abfd, ht, hi->other, sec, true, dynamic);
2094 /* A reference to the SHORTNAME symbol from a dynamic library
2095 will be satisfied by the versioned symbol at runtime. In
2096 effect, we have a reference to the versioned symbol. */
2097 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2098 hi->dynamic_def |= ht->dynamic_def;
2100 /* See if the new flags lead us to realize that the symbol must
2101 be dynamic. */
2102 if (! *dynsym)
2104 if (! dynamic)
2106 if (! bfd_link_executable (info)
2107 || hi->def_dynamic
2108 || hi->ref_dynamic)
2109 *dynsym = true;
2111 else
2113 if (hi->ref_regular)
2114 *dynsym = true;
2119 /* We also need to define an indirection from the nondefault version
2120 of the symbol. */
2122 nondefault:
2123 len = strlen (name);
2124 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
2125 if (shortname == NULL)
2126 return false;
2127 memcpy (shortname, name, shortlen);
2128 memcpy (shortname + shortlen, p + 1, len - shortlen);
2130 /* Once again, merge with any existing symbol. */
2131 type_change_ok = false;
2132 size_change_ok = false;
2133 tmp_sec = sec;
2134 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
2135 &hi, poldbfd, NULL, NULL, &skip, &override,
2136 &type_change_ok, &size_change_ok, &matched))
2137 return false;
2139 if (skip)
2141 if (!dynamic
2142 && h->root.type == bfd_link_hash_defweak
2143 && hi->root.type == bfd_link_hash_defined)
2145 /* We are handling a weak sym@@ver and attempting to define
2146 a weak sym@ver, but _bfd_elf_merge_symbol said to skip the
2147 new weak sym@ver because there is already a strong sym@ver.
2148 However, sym@ver and sym@@ver are really the same symbol.
2149 The existing strong sym@ver ought to override sym@@ver. */
2150 h->root.type = bfd_link_hash_defined;
2151 h->root.u.def.section = hi->root.u.def.section;
2152 h->root.u.def.value = hi->root.u.def.value;
2153 hi->root.type = bfd_link_hash_indirect;
2154 hi->root.u.i.link = &h->root;
2156 else
2157 return true;
2159 else if (override)
2161 /* Here SHORTNAME is a versioned name, so we don't expect to see
2162 the type of override we do in the case above unless it is
2163 overridden by a versioned definition. */
2164 if (hi->root.type != bfd_link_hash_defined
2165 && hi->root.type != bfd_link_hash_defweak)
2166 _bfd_error_handler
2167 /* xgettext:c-format */
2168 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
2169 abfd, shortname);
2170 return true;
2172 else
2174 bh = &hi->root;
2175 if (! (_bfd_generic_link_add_one_symbol
2176 (info, abfd, shortname, BSF_INDIRECT,
2177 bfd_ind_section_ptr, 0, name, false, collect, &bh)))
2178 return false;
2179 hi = (struct elf_link_hash_entry *) bh;
2182 /* If there is a duplicate definition somewhere, then HI may not
2183 point to an indirect symbol. We will have reported an error
2184 to the user in that case. */
2185 if (hi->root.type == bfd_link_hash_indirect)
2187 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
2188 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2189 hi->dynamic_def |= h->dynamic_def;
2191 /* If we first saw a reference to @VER symbol with
2192 non-default visibility, merge that visibility to the
2193 @@VER symbol. */
2194 elf_merge_st_other (abfd, h, hi->other, sec, true, dynamic);
2196 /* See if the new flags lead us to realize that the symbol
2197 must be dynamic. */
2198 if (! *dynsym)
2200 if (! dynamic)
2202 if (! bfd_link_executable (info)
2203 || hi->ref_dynamic)
2204 *dynsym = true;
2206 else
2208 if (hi->ref_regular)
2209 *dynsym = true;
2214 return true;
2217 /* This routine is used to export all defined symbols into the dynamic
2218 symbol table. It is called via elf_link_hash_traverse. */
2220 static bool
2221 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
2223 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2225 /* Ignore indirect symbols. These are added by the versioning code. */
2226 if (h->root.type == bfd_link_hash_indirect)
2227 return true;
2229 /* Ignore this if we won't export it. */
2230 if (!eif->info->export_dynamic && !h->dynamic)
2231 return true;
2233 if (h->dynindx == -1
2234 && (h->def_regular || h->ref_regular)
2235 && ! bfd_hide_sym_by_version (eif->info->version_info,
2236 h->root.root.string))
2238 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2240 eif->failed = true;
2241 return false;
2245 return true;
2248 /* Return the glibc version reference if VERSION_DEP is added to the
2249 list of glibc version dependencies successfully. VERSION_DEP will
2250 be put into the .gnu.version_r section. GLIBC_MINOR_BASE is the
2251 pointer to the glibc minor base version. */
2253 static Elf_Internal_Verneed *
2254 elf_link_add_glibc_verneed (struct elf_find_verdep_info *rinfo,
2255 Elf_Internal_Verneed *glibc_verref,
2256 const char *version_dep,
2257 int *glibc_minor_base)
2259 Elf_Internal_Verneed *t;
2260 Elf_Internal_Vernaux *a;
2261 size_t amt;
2262 int minor_version = -1;
2264 if (glibc_verref != NULL)
2266 t = glibc_verref;
2268 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2270 /* Return if VERSION_DEP dependency has been added. */
2271 if (a->vna_nodename == version_dep
2272 || strcmp (a->vna_nodename, version_dep) == 0)
2273 return t;
2276 else
2278 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2279 t != NULL;
2280 t = t->vn_nextref)
2282 const char *soname = bfd_elf_get_dt_soname (t->vn_bfd);
2283 if (soname != NULL && startswith (soname, "libc.so."))
2284 break;
2287 /* Skip the shared library if it isn't libc.so. */
2288 if (t == NULL)
2289 return t;
2291 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2293 /* Return if VERSION_DEP dependency has been added. */
2294 if (a->vna_nodename == version_dep
2295 || strcmp (a->vna_nodename, version_dep) == 0)
2296 return t;
2298 /* Check if libc.so provides GLIBC_2.XX version. */
2299 if (startswith (a->vna_nodename, "GLIBC_2."))
2301 minor_version = strtol (a->vna_nodename + 8, NULL, 10);
2302 if (minor_version < *glibc_minor_base)
2303 *glibc_minor_base = minor_version;
2307 /* Skip if it isn't linked against glibc. */
2308 if (minor_version < 0)
2309 return NULL;
2312 /* Skip if 2.GLIBC_MINOR_BASE includes VERSION_DEP. */
2313 if (startswith (version_dep, "GLIBC_2."))
2315 minor_version = strtol (version_dep + 8, NULL, 10);
2316 if (minor_version <= *glibc_minor_base)
2317 return NULL;
2320 amt = sizeof *a;
2321 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2322 if (a == NULL)
2324 rinfo->failed = true;
2325 return NULL;
2328 a->vna_nodename = version_dep;
2329 a->vna_flags = 0;
2330 a->vna_nextptr = t->vn_auxptr;
2331 a->vna_other = rinfo->vers + 1;
2332 ++rinfo->vers;
2334 t->vn_auxptr = a;
2336 return t;
2339 /* Add VERSION_DEP to the list of version dependencies when linked
2340 against glibc. */
2342 void
2343 _bfd_elf_link_add_glibc_version_dependency
2344 (struct elf_find_verdep_info *rinfo,
2345 const char *version_dep[])
2347 Elf_Internal_Verneed *t = NULL;
2348 int glibc_minor_base = INT_MAX;
2352 t = elf_link_add_glibc_verneed (rinfo, t, *version_dep,
2353 &glibc_minor_base);
2354 /* Return if there is no glibc version reference. */
2355 if (t == NULL)
2356 return;
2357 version_dep++;
2359 while (*version_dep != NULL);
2362 /* Add GLIBC_ABI_DT_RELR to the list of version dependencies when
2363 linked against glibc. */
2365 void
2366 _bfd_elf_link_add_dt_relr_dependency (struct elf_find_verdep_info *rinfo)
2368 if (rinfo->info->enable_dt_relr)
2370 const char *version[] =
2372 "GLIBC_ABI_DT_RELR",
2373 NULL
2375 _bfd_elf_link_add_glibc_version_dependency (rinfo, version);
2379 /* Look through the symbols which are defined in other shared
2380 libraries and referenced here. Update the list of version
2381 dependencies. This will be put into the .gnu.version_r section.
2382 This function is called via elf_link_hash_traverse. */
2384 static bool
2385 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2386 void *data)
2388 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2389 Elf_Internal_Verneed *t;
2390 Elf_Internal_Vernaux *a;
2391 size_t amt;
2393 /* We only care about symbols defined in shared objects with version
2394 information. */
2395 if (!h->def_dynamic
2396 || h->def_regular
2397 || h->dynindx == -1
2398 || h->verinfo.verdef == NULL
2399 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2400 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2401 return true;
2403 /* See if we already know about this version. */
2404 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2405 t != NULL;
2406 t = t->vn_nextref)
2408 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2409 continue;
2411 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2412 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2413 return true;
2415 break;
2418 /* This is a new version. Add it to tree we are building. */
2420 if (t == NULL)
2422 amt = sizeof *t;
2423 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2424 if (t == NULL)
2426 rinfo->failed = true;
2427 return false;
2430 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2431 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2432 elf_tdata (rinfo->info->output_bfd)->verref = t;
2435 amt = sizeof *a;
2436 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2437 if (a == NULL)
2439 rinfo->failed = true;
2440 return false;
2443 /* Note that we are copying a string pointer here, and testing it
2444 above. If bfd_elf_string_from_elf_section is ever changed to
2445 discard the string data when low in memory, this will have to be
2446 fixed. */
2447 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2449 a->vna_flags = h->verinfo.verdef->vd_flags;
2450 a->vna_nextptr = t->vn_auxptr;
2452 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2453 ++rinfo->vers;
2455 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2457 t->vn_auxptr = a;
2459 return true;
2462 /* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2463 hidden. Set *T_P to NULL if there is no match. */
2465 static bool
2466 _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
2467 struct elf_link_hash_entry *h,
2468 const char *version_p,
2469 struct bfd_elf_version_tree **t_p,
2470 bool *hide)
2472 struct bfd_elf_version_tree *t;
2474 /* Look for the version. If we find it, it is no longer weak. */
2475 for (t = info->version_info; t != NULL; t = t->next)
2477 if (strcmp (t->name, version_p) == 0)
2479 size_t len;
2480 char *alc;
2481 struct bfd_elf_version_expr *d;
2483 len = version_p - h->root.root.string;
2484 alc = (char *) bfd_malloc (len);
2485 if (alc == NULL)
2486 return false;
2487 memcpy (alc, h->root.root.string, len - 1);
2488 alc[len - 1] = '\0';
2489 if (alc[len - 2] == ELF_VER_CHR)
2490 alc[len - 2] = '\0';
2492 h->verinfo.vertree = t;
2493 t->used = true;
2494 d = NULL;
2496 if (t->globals.list != NULL)
2497 d = (*t->match) (&t->globals, NULL, alc);
2499 /* See if there is anything to force this symbol to
2500 local scope. */
2501 if (d == NULL && t->locals.list != NULL)
2503 d = (*t->match) (&t->locals, NULL, alc);
2504 if (d != NULL
2505 && h->dynindx != -1
2506 && ! info->export_dynamic)
2507 *hide = true;
2510 free (alc);
2511 break;
2515 *t_p = t;
2517 return true;
2520 /* Return TRUE if the symbol H is hidden by version script. */
2522 bool
2523 _bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
2524 struct elf_link_hash_entry *h)
2526 const char *p;
2527 bool hide = false;
2528 const struct elf_backend_data *bed
2529 = get_elf_backend_data (info->output_bfd);
2531 /* Version script only hides symbols defined in regular objects. */
2532 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2533 return true;
2535 p = strchr (h->root.root.string, ELF_VER_CHR);
2536 if (p != NULL && h->verinfo.vertree == NULL)
2538 struct bfd_elf_version_tree *t;
2540 ++p;
2541 if (*p == ELF_VER_CHR)
2542 ++p;
2544 if (*p != '\0'
2545 && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
2546 && hide)
2548 if (hide)
2549 (*bed->elf_backend_hide_symbol) (info, h, true);
2550 return true;
2554 /* If we don't have a version for this symbol, see if we can find
2555 something. */
2556 if (h->verinfo.vertree == NULL && info->version_info != NULL)
2558 h->verinfo.vertree
2559 = bfd_find_version_for_sym (info->version_info,
2560 h->root.root.string, &hide);
2561 if (h->verinfo.vertree != NULL && hide)
2563 (*bed->elf_backend_hide_symbol) (info, h, true);
2564 return true;
2568 return false;
2571 /* Figure out appropriate versions for all the symbols. We may not
2572 have the version number script until we have read all of the input
2573 files, so until that point we don't know which symbols should be
2574 local. This function is called via elf_link_hash_traverse. */
2576 static bool
2577 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2579 struct elf_info_failed *sinfo;
2580 struct bfd_link_info *info;
2581 const struct elf_backend_data *bed;
2582 struct elf_info_failed eif;
2583 char *p;
2584 bool hide;
2586 sinfo = (struct elf_info_failed *) data;
2587 info = sinfo->info;
2589 /* Fix the symbol flags. */
2590 eif.failed = false;
2591 eif.info = info;
2592 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2594 if (eif.failed)
2595 sinfo->failed = true;
2596 return false;
2599 bed = get_elf_backend_data (info->output_bfd);
2601 /* We only need version numbers for symbols defined in regular
2602 objects. */
2603 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2605 /* Hide symbols defined in discarded input sections. */
2606 if ((h->root.type == bfd_link_hash_defined
2607 || h->root.type == bfd_link_hash_defweak)
2608 && discarded_section (h->root.u.def.section))
2609 (*bed->elf_backend_hide_symbol) (info, h, true);
2610 return true;
2613 hide = false;
2614 p = strchr (h->root.root.string, ELF_VER_CHR);
2615 if (p != NULL && h->verinfo.vertree == NULL)
2617 struct bfd_elf_version_tree *t;
2619 ++p;
2620 if (*p == ELF_VER_CHR)
2621 ++p;
2623 /* If there is no version string, we can just return out. */
2624 if (*p == '\0')
2625 return true;
2627 if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
2629 sinfo->failed = true;
2630 return false;
2633 if (hide)
2634 (*bed->elf_backend_hide_symbol) (info, h, true);
2636 /* If we are building an application, we need to create a
2637 version node for this version. */
2638 if (t == NULL && bfd_link_executable (info))
2640 struct bfd_elf_version_tree **pp;
2641 int version_index;
2643 /* If we aren't going to export this symbol, we don't need
2644 to worry about it. */
2645 if (h->dynindx == -1)
2646 return true;
2648 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2649 sizeof *t);
2650 if (t == NULL)
2652 sinfo->failed = true;
2653 return false;
2656 t->name = p;
2657 t->name_indx = (unsigned int) -1;
2658 t->used = true;
2660 version_index = 1;
2661 /* Don't count anonymous version tag. */
2662 if (sinfo->info->version_info != NULL
2663 && sinfo->info->version_info->vernum == 0)
2664 version_index = 0;
2665 for (pp = &sinfo->info->version_info;
2666 *pp != NULL;
2667 pp = &(*pp)->next)
2668 ++version_index;
2669 t->vernum = version_index;
2671 *pp = t;
2673 h->verinfo.vertree = t;
2675 else if (t == NULL)
2677 /* We could not find the version for a symbol when
2678 generating a shared archive. Return an error. */
2679 _bfd_error_handler
2680 /* xgettext:c-format */
2681 (_("%pB: version node not found for symbol %s"),
2682 info->output_bfd, h->root.root.string);
2683 bfd_set_error (bfd_error_bad_value);
2684 sinfo->failed = true;
2685 return false;
2689 /* If we don't have a version for this symbol, see if we can find
2690 something. */
2691 if (!hide
2692 && h->verinfo.vertree == NULL
2693 && sinfo->info->version_info != NULL)
2695 h->verinfo.vertree
2696 = bfd_find_version_for_sym (sinfo->info->version_info,
2697 h->root.root.string, &hide);
2698 if (h->verinfo.vertree != NULL && hide)
2699 (*bed->elf_backend_hide_symbol) (info, h, true);
2702 return true;
2705 /* Read and swap the relocs from the section indicated by SHDR. This
2706 may be either a REL or a RELA section. The relocations are
2707 translated into RELA relocations and stored in INTERNAL_RELOCS,
2708 which should have already been allocated to contain enough space.
2709 The *EXTERNAL_RELOCS_P are a buffer where the external form of the
2710 relocations should be stored. If *EXTERNAL_RELOCS_ADDR is NULL,
2711 *EXTERNAL_RELOCS_ADDR and *EXTERNAL_RELOCS_SIZE returns the mmap
2712 memory address and size. Otherwise, *EXTERNAL_RELOCS_ADDR is
2713 unchanged and *EXTERNAL_RELOCS_SIZE returns 0.
2715 Returns FALSE if something goes wrong. */
2717 static bool
2718 elf_link_read_relocs_from_section (bfd *abfd,
2719 const asection *sec,
2720 Elf_Internal_Shdr *shdr,
2721 void **external_relocs_addr,
2722 size_t *external_relocs_size,
2723 Elf_Internal_Rela *internal_relocs)
2725 const struct elf_backend_data *bed;
2726 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2727 const bfd_byte *erela;
2728 const bfd_byte *erelaend;
2729 Elf_Internal_Rela *irela;
2730 Elf_Internal_Shdr *symtab_hdr;
2731 size_t nsyms;
2732 void *external_relocs = *external_relocs_addr;
2734 /* Position ourselves at the start of the section. */
2735 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2736 return false;
2738 /* Read the relocations. */
2739 *external_relocs_size = shdr->sh_size;
2740 if (!_bfd_mmap_read_temporary (&external_relocs,
2741 external_relocs_size,
2742 external_relocs_addr, abfd, true))
2743 return false;
2745 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2746 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2748 bed = get_elf_backend_data (abfd);
2750 /* Convert the external relocations to the internal format. */
2751 if (shdr->sh_entsize == bed->s->sizeof_rel)
2752 swap_in = bed->s->swap_reloc_in;
2753 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2754 swap_in = bed->s->swap_reloca_in;
2755 else
2757 bfd_set_error (bfd_error_wrong_format);
2758 return false;
2761 erela = (const bfd_byte *) external_relocs;
2762 /* Setting erelaend like this and comparing with <= handles case of
2763 a fuzzed object with sh_size not a multiple of sh_entsize. */
2764 erelaend = erela + shdr->sh_size - shdr->sh_entsize;
2765 irela = internal_relocs;
2766 while (erela <= erelaend)
2768 bfd_vma r_symndx;
2770 (*swap_in) (abfd, erela, irela);
2771 r_symndx = ELF32_R_SYM (irela->r_info);
2772 if (bed->s->arch_size == 64)
2773 r_symndx >>= 24;
2774 if (nsyms > 0)
2776 if ((size_t) r_symndx >= nsyms)
2778 _bfd_error_handler
2779 /* xgettext:c-format */
2780 (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
2781 " for offset %#" PRIx64 " in section `%pA'"),
2782 abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
2783 (uint64_t) irela->r_offset, sec);
2784 bfd_set_error (bfd_error_bad_value);
2785 return false;
2788 else if (r_symndx != STN_UNDEF)
2790 _bfd_error_handler
2791 /* xgettext:c-format */
2792 (_("%pB: non-zero symbol index (%#" PRIx64 ")"
2793 " for offset %#" PRIx64 " in section `%pA'"
2794 " when the object file has no symbol table"),
2795 abfd, (uint64_t) r_symndx,
2796 (uint64_t) irela->r_offset, sec);
2797 bfd_set_error (bfd_error_bad_value);
2798 return false;
2800 irela += bed->s->int_rels_per_ext_rel;
2801 erela += shdr->sh_entsize;
2804 return true;
2807 /* Read and swap the relocs for a section O. They may have been
2808 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2809 not NULL, they are used as buffers to read into. They are known to
2810 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2811 the return value is allocated using either malloc or bfd_alloc,
2812 according to the KEEP_MEMORY argument. If O has two relocation
2813 sections (both REL and RELA relocations), then the REL_HDR
2814 relocations will appear first in INTERNAL_RELOCS, followed by the
2815 RELA_HDR relocations. If INFO isn't NULL and KEEP_MEMORY is true,
2816 update cache_size. */
2818 Elf_Internal_Rela *
2819 _bfd_elf_link_info_read_relocs (bfd *abfd,
2820 struct bfd_link_info *info,
2821 const asection *o,
2822 void *external_relocs,
2823 Elf_Internal_Rela *internal_relocs,
2824 bool keep_memory)
2826 void *alloc1 = NULL;
2827 size_t alloc1_size;
2828 Elf_Internal_Rela *alloc2 = NULL;
2829 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2830 struct bfd_elf_section_data *esdo = elf_section_data (o);
2831 Elf_Internal_Rela *internal_rela_relocs;
2833 if (esdo->relocs != NULL)
2834 return esdo->relocs;
2836 if (o->reloc_count == 0)
2837 return NULL;
2839 if (internal_relocs == NULL)
2841 bfd_size_type size;
2843 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
2844 if (keep_memory && info)
2845 info->cache_size += size;
2846 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2847 if (internal_relocs == NULL)
2848 return NULL;
2851 alloc1 = external_relocs;
2852 internal_rela_relocs = internal_relocs;
2853 if (esdo->rel.hdr)
2855 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2856 &alloc1, &alloc1_size,
2857 internal_relocs))
2858 goto error_return;
2859 external_relocs = (((bfd_byte *) external_relocs)
2860 + esdo->rel.hdr->sh_size);
2861 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2862 * bed->s->int_rels_per_ext_rel);
2865 if (esdo->rela.hdr
2866 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2867 &alloc1, &alloc1_size,
2868 internal_rela_relocs)))
2869 goto error_return;
2871 /* Cache the results for next time, if we can. */
2872 if (keep_memory)
2873 esdo->relocs = internal_relocs;
2875 _bfd_munmap_temporary (alloc1, alloc1_size);
2877 /* Don't free alloc2, since if it was allocated we are passing it
2878 back (under the name of internal_relocs). */
2880 return internal_relocs;
2882 error_return:
2883 _bfd_munmap_temporary (alloc1, alloc1_size);
2884 free (alloc2);
2885 return NULL;
2888 /* This is similar to _bfd_elf_link_info_read_relocs, except for that
2889 NULL is passed to _bfd_elf_link_info_read_relocs for pointer to
2890 struct bfd_link_info. */
2892 Elf_Internal_Rela *
2893 _bfd_elf_link_read_relocs (bfd *abfd,
2894 const asection *o,
2895 void *external_relocs,
2896 Elf_Internal_Rela *internal_relocs,
2897 bool keep_memory)
2899 return _bfd_elf_link_info_read_relocs (abfd, NULL, o, external_relocs,
2900 internal_relocs, keep_memory);
2904 /* Compute the size of, and allocate space for, REL_HDR which is the
2905 section header for a section containing relocations for O. */
2907 static bool
2908 _bfd_elf_link_size_reloc_section (bfd *abfd,
2909 struct bfd_elf_section_reloc_data *reldata)
2911 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2913 /* That allows us to calculate the size of the section. */
2914 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2916 /* The contents field must last into write_object_contents, so we
2917 allocate it with bfd_alloc rather than malloc. Also since we
2918 cannot be sure that the contents will actually be filled in,
2919 we zero the allocated space. */
2920 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2921 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2922 return false;
2924 if (reldata->hashes == NULL && reldata->count)
2926 struct elf_link_hash_entry **p;
2928 p = ((struct elf_link_hash_entry **)
2929 bfd_zmalloc (reldata->count * sizeof (*p)));
2930 if (p == NULL)
2931 return false;
2933 reldata->hashes = p;
2936 return true;
2939 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2940 originated from the section given by INPUT_REL_HDR) to the
2941 OUTPUT_BFD. */
2943 bool
2944 _bfd_elf_link_output_relocs (bfd *output_bfd,
2945 asection *input_section,
2946 Elf_Internal_Shdr *input_rel_hdr,
2947 Elf_Internal_Rela *internal_relocs,
2948 struct elf_link_hash_entry **rel_hash)
2950 Elf_Internal_Rela *irela;
2951 Elf_Internal_Rela *irelaend;
2952 bfd_byte *erel;
2953 struct bfd_elf_section_reloc_data *output_reldata;
2954 asection *output_section;
2955 const struct elf_backend_data *bed;
2956 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2957 struct bfd_elf_section_data *esdo;
2959 output_section = input_section->output_section;
2961 bed = get_elf_backend_data (output_bfd);
2962 esdo = elf_section_data (output_section);
2963 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2965 output_reldata = &esdo->rel;
2966 swap_out = bed->s->swap_reloc_out;
2968 else if (esdo->rela.hdr
2969 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2971 output_reldata = &esdo->rela;
2972 swap_out = bed->s->swap_reloca_out;
2974 else
2976 _bfd_error_handler
2977 /* xgettext:c-format */
2978 (_("%pB: relocation size mismatch in %pB section %pA"),
2979 output_bfd, input_section->owner, input_section);
2980 bfd_set_error (bfd_error_wrong_format);
2981 return false;
2984 erel = output_reldata->hdr->contents;
2985 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2986 irela = internal_relocs;
2987 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2988 * bed->s->int_rels_per_ext_rel);
2989 while (irela < irelaend)
2991 if (rel_hash && *rel_hash)
2992 (*rel_hash)->has_reloc = 1;
2993 (*swap_out) (output_bfd, irela, erel);
2994 irela += bed->s->int_rels_per_ext_rel;
2995 erel += input_rel_hdr->sh_entsize;
2996 if (rel_hash)
2997 rel_hash++;
3000 /* Bump the counter, so that we know where to add the next set of
3001 relocations. */
3002 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
3004 return true;
3007 /* Make weak undefined symbols in PIE dynamic. */
3009 bool
3010 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
3011 struct elf_link_hash_entry *h)
3013 if (bfd_link_pie (info)
3014 && h->dynindx == -1
3015 && h->root.type == bfd_link_hash_undefweak)
3016 return bfd_elf_link_record_dynamic_symbol (info, h);
3018 return true;
3021 /* Fix up the flags for a symbol. This handles various cases which
3022 can only be fixed after all the input files are seen. This is
3023 currently called by both adjust_dynamic_symbol and
3024 assign_sym_version, which is unnecessary but perhaps more robust in
3025 the face of future changes. */
3027 static bool
3028 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
3029 struct elf_info_failed *eif)
3031 const struct elf_backend_data *bed;
3033 /* If this symbol was mentioned in a non-ELF file, try to set
3034 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
3035 permit a non-ELF file to correctly refer to a symbol defined in
3036 an ELF dynamic object. */
3037 if (h->non_elf)
3039 while (h->root.type == bfd_link_hash_indirect)
3040 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3042 if (h->root.type != bfd_link_hash_defined
3043 && h->root.type != bfd_link_hash_defweak)
3045 h->ref_regular = 1;
3046 h->ref_regular_nonweak = 1;
3048 else
3050 if (h->root.u.def.section->owner != NULL
3051 && (bfd_get_flavour (h->root.u.def.section->owner)
3052 == bfd_target_elf_flavour))
3054 h->ref_regular = 1;
3055 h->ref_regular_nonweak = 1;
3057 else
3058 h->def_regular = 1;
3061 if (h->dynindx == -1
3062 && (h->def_dynamic
3063 || h->ref_dynamic))
3065 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
3067 eif->failed = true;
3068 return false;
3072 else
3074 /* Unfortunately, NON_ELF is only correct if the symbol
3075 was first seen in a non-ELF file. Fortunately, if the symbol
3076 was first seen in an ELF file, we're probably OK unless the
3077 symbol was defined in a non-ELF file. Catch that case here.
3078 FIXME: We're still in trouble if the symbol was first seen in
3079 a dynamic object, and then later in a non-ELF regular object. */
3080 if ((h->root.type == bfd_link_hash_defined
3081 || h->root.type == bfd_link_hash_defweak)
3082 && !h->def_regular
3083 && (h->root.u.def.section->owner != NULL
3084 ? (bfd_get_flavour (h->root.u.def.section->owner)
3085 != bfd_target_elf_flavour)
3086 : (bfd_is_abs_section (h->root.u.def.section)
3087 && !h->def_dynamic)))
3088 h->def_regular = 1;
3091 /* Backend specific symbol fixup. */
3092 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
3093 if (bed->elf_backend_fixup_symbol
3094 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
3095 return false;
3097 /* If this is a final link, and the symbol was defined as a common
3098 symbol in a regular object file, and there was no definition in
3099 any dynamic object, then the linker will have allocated space for
3100 the symbol in a common section but the DEF_REGULAR
3101 flag will not have been set. */
3102 if (h->root.type == bfd_link_hash_defined
3103 && !h->def_regular
3104 && h->ref_regular
3105 && !h->def_dynamic
3106 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
3107 h->def_regular = 1;
3109 /* Symbols defined in discarded sections shouldn't be dynamic. */
3110 if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
3111 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3113 /* If a weak undefined symbol has non-default visibility, we also
3114 hide it from the dynamic linker. */
3115 else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3116 && h->root.type == bfd_link_hash_undefweak)
3117 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3119 /* A hidden versioned symbol in executable should be forced local if
3120 it is is locally defined, not referenced by shared library and not
3121 exported. */
3122 else if (bfd_link_executable (eif->info)
3123 && h->versioned == versioned_hidden
3124 && !eif->info->export_dynamic
3125 && !h->dynamic
3126 && !h->ref_dynamic
3127 && h->def_regular)
3128 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3130 /* If -Bsymbolic was used (which means to bind references to global
3131 symbols to the definition within the shared object), and this
3132 symbol was defined in a regular object, then it actually doesn't
3133 need a PLT entry. Likewise, if the symbol has non-default
3134 visibility. If the symbol has hidden or internal visibility, we
3135 will force it local. */
3136 else if (h->needs_plt
3137 && bfd_link_pic (eif->info)
3138 && is_elf_hash_table (eif->info->hash)
3139 && (SYMBOLIC_BIND (eif->info, h)
3140 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
3141 && h->def_regular)
3143 bool force_local;
3145 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
3146 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
3147 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
3150 /* If this is a weak defined symbol in a dynamic object, and we know
3151 the real definition in the dynamic object, copy interesting flags
3152 over to the real definition. */
3153 if (h->is_weakalias)
3155 struct elf_link_hash_entry *def = weakdef (h);
3157 /* If the real definition is defined by a regular object file,
3158 don't do anything special. See the longer description in
3159 _bfd_elf_adjust_dynamic_symbol, below. If the def is not
3160 bfd_link_hash_defined as it was when put on the alias list
3161 then it must have originally been a versioned symbol (for
3162 which a non-versioned indirect symbol is created) and later
3163 a definition for the non-versioned symbol is found. In that
3164 case the indirection is flipped with the versioned symbol
3165 becoming an indirect pointing at the non-versioned symbol.
3166 Thus, not an alias any more. */
3167 if (def->def_regular
3168 || def->root.type != bfd_link_hash_defined)
3170 h = def;
3171 while ((h = h->u.alias) != def)
3172 h->is_weakalias = 0;
3174 else
3176 while (h->root.type == bfd_link_hash_indirect)
3177 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3178 BFD_ASSERT (h->root.type == bfd_link_hash_defined
3179 || h->root.type == bfd_link_hash_defweak);
3180 BFD_ASSERT (def->def_dynamic);
3181 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
3185 return true;
3188 /* Make the backend pick a good value for a dynamic symbol. This is
3189 called via elf_link_hash_traverse, and also calls itself
3190 recursively. */
3192 static bool
3193 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
3195 struct elf_info_failed *eif = (struct elf_info_failed *) data;
3196 struct elf_link_hash_table *htab;
3197 const struct elf_backend_data *bed;
3199 if (! is_elf_hash_table (eif->info->hash))
3200 return false;
3202 /* Ignore indirect symbols. These are added by the versioning code. */
3203 if (h->root.type == bfd_link_hash_indirect)
3204 return true;
3206 /* Fix the symbol flags. */
3207 if (! _bfd_elf_fix_symbol_flags (h, eif))
3208 return false;
3210 htab = elf_hash_table (eif->info);
3211 bed = get_elf_backend_data (htab->dynobj);
3213 if (h->root.type == bfd_link_hash_undefweak)
3215 if (eif->info->dynamic_undefined_weak == 0)
3216 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3217 else if (eif->info->dynamic_undefined_weak > 0
3218 && h->ref_regular
3219 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3220 && !bfd_hide_sym_by_version (eif->info->version_info,
3221 h->root.root.string))
3223 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
3225 eif->failed = true;
3226 return false;
3231 /* If this symbol does not require a PLT entry, and it is not
3232 defined by a dynamic object, or is not referenced by a regular
3233 object, ignore it. We do have to handle a weak defined symbol,
3234 even if no regular object refers to it, if we decided to add it
3235 to the dynamic symbol table. FIXME: Do we normally need to worry
3236 about symbols which are defined by one dynamic object and
3237 referenced by another one? */
3238 if (!h->needs_plt
3239 && h->type != STT_GNU_IFUNC
3240 && (h->def_regular
3241 || !h->def_dynamic
3242 || (!h->ref_regular
3243 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
3245 h->plt = elf_hash_table (eif->info)->init_plt_offset;
3246 return true;
3249 /* If we've already adjusted this symbol, don't do it again. This
3250 can happen via a recursive call. */
3251 if (h->dynamic_adjusted)
3252 return true;
3254 /* Don't look at this symbol again. Note that we must set this
3255 after checking the above conditions, because we may look at a
3256 symbol once, decide not to do anything, and then get called
3257 recursively later after REF_REGULAR is set below. */
3258 h->dynamic_adjusted = 1;
3260 /* If this is a weak definition, and we know a real definition, and
3261 the real symbol is not itself defined by a regular object file,
3262 then get a good value for the real definition. We handle the
3263 real symbol first, for the convenience of the backend routine.
3265 Note that there is a confusing case here. If the real definition
3266 is defined by a regular object file, we don't get the real symbol
3267 from the dynamic object, but we do get the weak symbol. If the
3268 processor backend uses a COPY reloc, then if some routine in the
3269 dynamic object changes the real symbol, we will not see that
3270 change in the corresponding weak symbol. This is the way other
3271 ELF linkers work as well, and seems to be a result of the shared
3272 library model.
3274 I will clarify this issue. Most SVR4 shared libraries define the
3275 variable _timezone and define timezone as a weak synonym. The
3276 tzset call changes _timezone. If you write
3277 extern int timezone;
3278 int _timezone = 5;
3279 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3280 you might expect that, since timezone is a synonym for _timezone,
3281 the same number will print both times. However, if the processor
3282 backend uses a COPY reloc, then actually timezone will be copied
3283 into your process image, and, since you define _timezone
3284 yourself, _timezone will not. Thus timezone and _timezone will
3285 wind up at different memory locations. The tzset call will set
3286 _timezone, leaving timezone unchanged. */
3288 if (h->is_weakalias)
3290 struct elf_link_hash_entry *def = weakdef (h);
3292 /* If we get to this point, there is an implicit reference to
3293 the alias by a regular object file via the weak symbol H. */
3294 def->ref_regular = 1;
3296 /* Ensure that the backend adjust_dynamic_symbol function sees
3297 the strong alias before H by recursively calling ourselves. */
3298 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
3299 return false;
3302 /* If a symbol has no type and no size and does not require a PLT
3303 entry, then we are probably about to do the wrong thing here: we
3304 are probably going to create a COPY reloc for an empty object.
3305 This case can arise when a shared object is built with assembly
3306 code, and the assembly code fails to set the symbol type. */
3307 if (h->size == 0
3308 && h->type == STT_NOTYPE
3309 && !h->needs_plt)
3310 _bfd_error_handler
3311 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3312 h->root.root.string);
3314 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3316 eif->failed = true;
3317 return false;
3320 return true;
3323 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3324 DYNBSS. */
3326 bool
3327 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3328 struct elf_link_hash_entry *h,
3329 asection *dynbss)
3331 unsigned int power_of_two;
3332 bfd_vma mask;
3333 asection *sec = h->root.u.def.section;
3335 /* The section alignment of the definition is the maximum alignment
3336 requirement of symbols defined in the section. Since we don't
3337 know the symbol alignment requirement, we start with the
3338 maximum alignment and check low bits of the symbol address
3339 for the minimum alignment. */
3340 power_of_two = bfd_section_alignment (sec);
3341 mask = ((bfd_vma) 1 << power_of_two) - 1;
3342 while ((h->root.u.def.value & mask) != 0)
3344 mask >>= 1;
3345 --power_of_two;
3348 if (power_of_two > bfd_section_alignment (dynbss))
3350 /* Adjust the section alignment if needed. */
3351 if (!bfd_set_section_alignment (dynbss, power_of_two))
3352 return false;
3355 /* We make sure that the symbol will be aligned properly. */
3356 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3358 /* Define the symbol as being at this point in DYNBSS. */
3359 h->root.u.def.section = dynbss;
3360 h->root.u.def.value = dynbss->size;
3362 /* Increment the size of DYNBSS to make room for the symbol. */
3363 dynbss->size += h->size;
3365 /* No error if extern_protected_data is true. */
3366 if (h->protected_def
3367 && (!info->extern_protected_data
3368 || (info->extern_protected_data < 0
3369 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
3370 info->callbacks->einfo
3371 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3372 h->root.root.string);
3374 return true;
3377 /* Adjust all external symbols pointing into SEC_MERGE sections
3378 to reflect the object merging within the sections. */
3380 static bool
3381 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
3383 asection *sec;
3385 if ((h->root.type == bfd_link_hash_defined
3386 || h->root.type == bfd_link_hash_defweak)
3387 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
3388 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3390 bfd *output_bfd = (bfd *) data;
3392 h->root.u.def.value =
3393 _bfd_merged_section_offset (output_bfd,
3394 &h->root.u.def.section,
3395 elf_section_data (sec)->sec_info,
3396 h->root.u.def.value);
3399 return true;
3402 /* Returns false if the symbol referred to by H should be considered
3403 to resolve local to the current module, and true if it should be
3404 considered to bind dynamically. */
3406 bool
3407 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3408 struct bfd_link_info *info,
3409 bool not_local_protected)
3411 bool binding_stays_local_p;
3412 const struct elf_backend_data *bed;
3413 struct elf_link_hash_table *hash_table;
3415 if (h == NULL)
3416 return false;
3418 while (h->root.type == bfd_link_hash_indirect
3419 || h->root.type == bfd_link_hash_warning)
3420 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3422 /* If it was forced local, then clearly it's not dynamic. */
3423 if (h->dynindx == -1)
3424 return false;
3425 if (h->forced_local)
3426 return false;
3428 /* Identify the cases where name binding rules say that a
3429 visible symbol resolves locally. */
3430 binding_stays_local_p = (bfd_link_executable (info)
3431 || SYMBOLIC_BIND (info, h));
3433 switch (ELF_ST_VISIBILITY (h->other))
3435 case STV_INTERNAL:
3436 case STV_HIDDEN:
3437 return false;
3439 case STV_PROTECTED:
3440 hash_table = elf_hash_table (info);
3441 if (!is_elf_hash_table (&hash_table->root))
3442 return false;
3444 bed = get_elf_backend_data (hash_table->dynobj);
3446 /* Proper resolution for function pointer equality may require
3447 that these symbols perhaps be resolved dynamically, even though
3448 we should be resolving them to the current module. */
3449 if (!not_local_protected || !bed->is_function_type (h->type))
3450 binding_stays_local_p = true;
3451 break;
3453 default:
3454 break;
3457 /* If it isn't defined locally, then clearly it's dynamic. */
3458 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
3459 return true;
3461 /* Otherwise, the symbol is dynamic if binding rules don't tell
3462 us that it remains local. */
3463 return !binding_stays_local_p;
3466 /* Return true if the symbol referred to by H should be considered
3467 to resolve local to the current module, and false otherwise. Differs
3468 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3469 undefined symbols. The two functions are virtually identical except
3470 for the place where dynindx == -1 is tested. If that test is true,
3471 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3472 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3473 defined symbols.
3474 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3475 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3476 treatment of undefined weak symbols. For those that do not make
3477 undefined weak symbols dynamic, both functions may return false. */
3479 bool
3480 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3481 struct bfd_link_info *info,
3482 bool local_protected)
3484 const struct elf_backend_data *bed;
3485 struct elf_link_hash_table *hash_table;
3487 /* If it's a local sym, of course we resolve locally. */
3488 if (h == NULL)
3489 return true;
3491 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3492 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3493 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3494 return true;
3496 /* Forced local symbols resolve locally. */
3497 if (h->forced_local)
3498 return true;
3500 /* Common symbols that become definitions don't get the DEF_REGULAR
3501 flag set, so test it first, and don't bail out. */
3502 if (ELF_COMMON_DEF_P (h))
3503 /* Do nothing. */;
3504 /* If we don't have a definition in a regular file, then we can't
3505 resolve locally. The sym is either undefined or dynamic. */
3506 else if (!h->def_regular)
3507 return false;
3509 /* Non-dynamic symbols resolve locally. */
3510 if (h->dynindx == -1)
3511 return true;
3513 /* At this point, we know the symbol is defined and dynamic. In an
3514 executable it must resolve locally, likewise when building symbolic
3515 shared libraries. */
3516 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3517 return true;
3519 /* Now deal with defined dynamic symbols in shared libraries. Ones
3520 with default visibility might not resolve locally. */
3521 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3522 return false;
3524 hash_table = elf_hash_table (info);
3525 if (!is_elf_hash_table (&hash_table->root))
3526 return true;
3528 /* STV_PROTECTED symbols with indirect external access are local. */
3529 if (info->indirect_extern_access > 0)
3530 return true;
3532 bed = get_elf_backend_data (hash_table->dynobj);
3534 /* If extern_protected_data is false, STV_PROTECTED non-function
3535 symbols are local. */
3536 if ((!info->extern_protected_data
3537 || (info->extern_protected_data < 0
3538 && !bed->extern_protected_data))
3539 && !bed->is_function_type (h->type))
3540 return true;
3542 /* Function pointer equality tests may require that STV_PROTECTED
3543 symbols be treated as dynamic symbols. If the address of a
3544 function not defined in an executable is set to that function's
3545 plt entry in the executable, then the address of the function in
3546 a shared library must also be the plt entry in the executable. */
3547 return local_protected;
3550 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3551 aligned. Returns the first TLS output section. */
3553 struct bfd_section *
3554 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3556 struct bfd_section *sec, *tls;
3557 unsigned int align = 0;
3559 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3560 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3561 break;
3562 tls = sec;
3564 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3565 if (sec->alignment_power > align)
3566 align = sec->alignment_power;
3568 elf_hash_table (info)->tls_sec = tls;
3570 /* Ensure the alignment of the first section (usually .tdata) is the largest
3571 alignment, so that the tls segment starts aligned. */
3572 if (tls != NULL)
3573 tls->alignment_power = align;
3575 return tls;
3578 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3579 static bool
3580 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3581 Elf_Internal_Sym *sym)
3583 const struct elf_backend_data *bed;
3585 /* Local symbols do not count, but target specific ones might. */
3586 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3587 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3588 return false;
3590 bed = get_elf_backend_data (abfd);
3591 /* Function symbols do not count. */
3592 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3593 return false;
3595 /* If the section is undefined, then so is the symbol. */
3596 if (sym->st_shndx == SHN_UNDEF)
3597 return false;
3599 /* If the symbol is defined in the common section, then
3600 it is a common definition and so does not count. */
3601 if (bed->common_definition (sym))
3602 return false;
3604 /* If the symbol is in a target specific section then we
3605 must rely upon the backend to tell us what it is. */
3606 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3607 /* FIXME - this function is not coded yet:
3609 return _bfd_is_global_symbol_definition (abfd, sym);
3611 Instead for now assume that the definition is not global,
3612 Even if this is wrong, at least the linker will behave
3613 in the same way that it used to do. */
3614 return false;
3616 return true;
3619 /* Search the symbol table of the archive element of the archive ABFD
3620 whose archive map contains a mention of SYMDEF, and determine if
3621 the symbol is defined in this element. */
3622 static bool
3623 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3625 Elf_Internal_Shdr * hdr;
3626 size_t symcount;
3627 size_t extsymcount;
3628 size_t extsymoff;
3629 Elf_Internal_Sym *isymbuf;
3630 Elf_Internal_Sym *isym;
3631 Elf_Internal_Sym *isymend;
3632 bool result;
3634 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset, NULL);
3635 if (abfd == NULL)
3636 return false;
3638 if (! bfd_check_format (abfd, bfd_object))
3639 return false;
3641 /* Select the appropriate symbol table. If we don't know if the
3642 object file is an IR object, give linker LTO plugin a chance to
3643 get the correct symbol table. */
3644 if (abfd->plugin_format == bfd_plugin_yes
3645 || abfd->plugin_format == bfd_plugin_yes_unused
3646 #if BFD_SUPPORTS_PLUGINS
3647 || (abfd->plugin_format == bfd_plugin_unknown
3648 && bfd_link_plugin_object_p (abfd))
3649 #endif
3652 /* Use the IR symbol table if the object has been claimed by
3653 plugin. */
3654 abfd = abfd->plugin_dummy_bfd;
3655 hdr = &elf_tdata (abfd)->symtab_hdr;
3657 else
3659 if (elf_use_dt_symtab_p (abfd))
3661 bfd_set_error (bfd_error_wrong_format);
3662 return false;
3665 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3666 hdr = &elf_tdata (abfd)->symtab_hdr;
3667 else
3668 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3671 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3673 /* The sh_info field of the symtab header tells us where the
3674 external symbols start. We don't care about the local symbols. */
3675 if (elf_bad_symtab (abfd))
3677 extsymcount = symcount;
3678 extsymoff = 0;
3680 else
3682 extsymcount = symcount - hdr->sh_info;
3683 extsymoff = hdr->sh_info;
3686 if (extsymcount == 0)
3687 return false;
3689 /* Read in the symbol table. */
3690 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3691 NULL, NULL, NULL);
3692 if (isymbuf == NULL)
3693 return false;
3695 /* Scan the symbol table looking for SYMDEF. */
3696 result = false;
3697 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3699 const char *name;
3701 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3702 isym->st_name);
3703 if (name == NULL)
3704 break;
3706 if (strcmp (name, symdef->name) == 0)
3708 result = is_global_data_symbol_definition (abfd, isym);
3709 break;
3713 free (isymbuf);
3715 return result;
3718 /* Add an entry to the .dynamic table. */
3720 bool
3721 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3722 bfd_vma tag,
3723 bfd_vma val)
3725 struct elf_link_hash_table *hash_table;
3726 const struct elf_backend_data *bed;
3727 asection *s;
3728 bfd_size_type newsize;
3729 bfd_byte *newcontents;
3730 Elf_Internal_Dyn dyn;
3732 hash_table = elf_hash_table (info);
3733 if (! is_elf_hash_table (&hash_table->root))
3734 return false;
3736 if (tag == DT_RELA || tag == DT_REL)
3737 hash_table->dynamic_relocs = true;
3739 bed = get_elf_backend_data (hash_table->dynobj);
3740 s = hash_table->dynamic;
3741 BFD_ASSERT (s != NULL);
3743 newsize = s->size + bed->s->sizeof_dyn;
3744 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3745 if (newcontents == NULL)
3746 return false;
3748 dyn.d_tag = tag;
3749 dyn.d_un.d_val = val;
3750 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3752 s->size = newsize;
3753 s->contents = newcontents;
3755 return true;
3758 /* Strip zero-sized dynamic sections. */
3760 bool
3761 _bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info *info)
3763 struct elf_link_hash_table *hash_table;
3764 const struct elf_backend_data *bed;
3765 asection *s, *sdynamic, **pp;
3766 asection *rela_dyn, *rel_dyn;
3767 Elf_Internal_Dyn dyn;
3768 bfd_byte *extdyn, *next;
3769 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3770 bool strip_zero_sized;
3771 bool strip_zero_sized_plt;
3773 if (bfd_link_relocatable (info))
3774 return true;
3776 hash_table = elf_hash_table (info);
3777 if (!is_elf_hash_table (&hash_table->root))
3778 return false;
3780 if (!hash_table->dynobj)
3781 return true;
3783 sdynamic= hash_table->dynamic;
3784 if (!sdynamic)
3785 return true;
3787 bed = get_elf_backend_data (hash_table->dynobj);
3788 swap_dyn_in = bed->s->swap_dyn_in;
3790 strip_zero_sized = false;
3791 strip_zero_sized_plt = false;
3793 /* Strip zero-sized dynamic sections. */
3794 rela_dyn = bfd_get_section_by_name (info->output_bfd, ".rela.dyn");
3795 rel_dyn = bfd_get_section_by_name (info->output_bfd, ".rel.dyn");
3796 for (pp = &info->output_bfd->sections; (s = *pp) != NULL;)
3797 if (s->size == 0
3798 && (s == rela_dyn
3799 || s == rel_dyn
3800 || s == hash_table->srelplt->output_section
3801 || s == hash_table->splt->output_section))
3803 *pp = s->next;
3804 info->output_bfd->section_count--;
3805 strip_zero_sized = true;
3806 if (s == rela_dyn)
3807 s = rela_dyn;
3808 if (s == rel_dyn)
3809 s = rel_dyn;
3810 else if (s == hash_table->splt->output_section)
3812 s = hash_table->splt;
3813 strip_zero_sized_plt = true;
3815 else
3816 s = hash_table->srelplt;
3817 s->flags |= SEC_EXCLUDE;
3818 s->output_section = bfd_abs_section_ptr;
3820 else
3821 pp = &s->next;
3823 if (strip_zero_sized_plt && sdynamic->size != 0)
3824 for (extdyn = sdynamic->contents;
3825 extdyn < sdynamic->contents + sdynamic->size;
3826 extdyn = next)
3828 next = extdyn + bed->s->sizeof_dyn;
3829 swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3830 switch (dyn.d_tag)
3832 default:
3833 break;
3834 case DT_JMPREL:
3835 case DT_PLTRELSZ:
3836 case DT_PLTREL:
3837 /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
3838 the procedure linkage table (the .plt section) has been
3839 removed. */
3840 memmove (extdyn, next,
3841 sdynamic->size - (next - sdynamic->contents));
3842 next = extdyn;
3846 if (strip_zero_sized)
3848 /* Regenerate program headers. */
3849 elf_seg_map (info->output_bfd) = NULL;
3850 return _bfd_elf_map_sections_to_segments (info->output_bfd, info,
3851 NULL);
3854 return true;
3857 /* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error,
3858 1 if a DT_NEEDED tag already exists, and 0 on success. */
3861 bfd_elf_add_dt_needed_tag (bfd *abfd, struct bfd_link_info *info)
3863 struct elf_link_hash_table *hash_table;
3864 size_t strindex;
3865 const char *soname;
3867 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3868 return -1;
3870 hash_table = elf_hash_table (info);
3871 soname = elf_dt_name (abfd);
3872 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, false);
3873 if (strindex == (size_t) -1)
3874 return -1;
3876 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3878 asection *sdyn;
3879 const struct elf_backend_data *bed;
3880 bfd_byte *extdyn;
3882 bed = get_elf_backend_data (hash_table->dynobj);
3883 sdyn = hash_table->dynamic;
3884 if (sdyn != NULL && sdyn->size != 0)
3885 for (extdyn = sdyn->contents;
3886 extdyn < sdyn->contents + sdyn->size;
3887 extdyn += bed->s->sizeof_dyn)
3889 Elf_Internal_Dyn dyn;
3891 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3892 if (dyn.d_tag == DT_NEEDED
3893 && dyn.d_un.d_val == strindex)
3895 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3896 return 1;
3901 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3902 return -1;
3904 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3905 return -1;
3907 return 0;
3910 /* Return true if SONAME is on the needed list between NEEDED and STOP
3911 (or the end of list if STOP is NULL), and needed by a library that
3912 will be loaded. */
3914 static bool
3915 on_needed_list (const char *soname,
3916 struct bfd_link_needed_list *needed,
3917 struct bfd_link_needed_list *stop)
3919 struct bfd_link_needed_list *look;
3920 for (look = needed; look != stop; look = look->next)
3921 if (strcmp (soname, look->name) == 0
3922 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3923 /* If needed by a library that itself is not directly
3924 needed, recursively check whether that library is
3925 indirectly needed. Since we add DT_NEEDED entries to
3926 the end of the list, library dependencies appear after
3927 the library. Therefore search prior to the current
3928 LOOK, preventing possible infinite recursion. */
3929 || on_needed_list (elf_dt_name (look->by), needed, look)))
3930 return true;
3932 return false;
3935 /* Sort symbol by value, section, size, and type. */
3936 static int
3937 elf_sort_symbol (const void *arg1, const void *arg2)
3939 const struct elf_link_hash_entry *h1;
3940 const struct elf_link_hash_entry *h2;
3941 bfd_signed_vma vdiff;
3942 int sdiff;
3943 const char *n1;
3944 const char *n2;
3946 h1 = *(const struct elf_link_hash_entry **) arg1;
3947 h2 = *(const struct elf_link_hash_entry **) arg2;
3948 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3949 if (vdiff != 0)
3950 return vdiff > 0 ? 1 : -1;
3952 sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3953 if (sdiff != 0)
3954 return sdiff;
3956 /* Sort so that sized symbols are selected over zero size symbols. */
3957 vdiff = h1->size - h2->size;
3958 if (vdiff != 0)
3959 return vdiff > 0 ? 1 : -1;
3961 /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */
3962 if (h1->type != h2->type)
3963 return h1->type - h2->type;
3965 /* If symbols are properly sized and typed, and multiple strong
3966 aliases are not defined in a shared library by the user we
3967 shouldn't get here. Unfortunately linker script symbols like
3968 __bss_start sometimes match a user symbol defined at the start of
3969 .bss without proper size and type. We'd like to preference the
3970 user symbol over reserved system symbols. Sort on leading
3971 underscores. */
3972 n1 = h1->root.root.string;
3973 n2 = h2->root.root.string;
3974 while (*n1 == *n2)
3976 if (*n1 == 0)
3977 break;
3978 ++n1;
3979 ++n2;
3981 if (*n1 == '_')
3982 return -1;
3983 if (*n2 == '_')
3984 return 1;
3986 /* Final sort on name selects user symbols like '_u' over reserved
3987 system symbols like '_Z' and also will avoid qsort instability. */
3988 return *n1 - *n2;
3991 /* This function is used to adjust offsets into .dynstr for
3992 dynamic symbols. This is called via elf_link_hash_traverse. */
3994 static bool
3995 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3997 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3999 if (h->dynindx != -1)
4000 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
4001 return true;
4004 /* Assign string offsets in .dynstr, update all structures referencing
4005 them. */
4007 static bool
4008 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
4010 struct elf_link_hash_table *hash_table = elf_hash_table (info);
4011 struct elf_link_local_dynamic_entry *entry;
4012 struct elf_strtab_hash *dynstr = hash_table->dynstr;
4013 bfd *dynobj = hash_table->dynobj;
4014 asection *sdyn;
4015 bfd_size_type size;
4016 const struct elf_backend_data *bed;
4017 bfd_byte *extdyn;
4019 _bfd_elf_strtab_finalize (dynstr);
4020 size = _bfd_elf_strtab_size (dynstr);
4022 /* Allow the linker to examine the dynsymtab now it's fully populated. */
4024 if (info->callbacks->examine_strtab)
4025 info->callbacks->examine_strtab (dynstr);
4027 bed = get_elf_backend_data (dynobj);
4028 sdyn = hash_table->dynamic;
4029 BFD_ASSERT (sdyn != NULL);
4031 /* Update all .dynamic entries referencing .dynstr strings. */
4032 for (extdyn = sdyn->contents;
4033 extdyn < PTR_ADD (sdyn->contents, sdyn->size);
4034 extdyn += bed->s->sizeof_dyn)
4036 Elf_Internal_Dyn dyn;
4038 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
4039 switch (dyn.d_tag)
4041 case DT_STRSZ:
4042 dyn.d_un.d_val = size;
4043 break;
4044 case DT_NEEDED:
4045 case DT_SONAME:
4046 case DT_RPATH:
4047 case DT_RUNPATH:
4048 case DT_FILTER:
4049 case DT_AUXILIARY:
4050 case DT_AUDIT:
4051 case DT_DEPAUDIT:
4052 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
4053 break;
4054 default:
4055 continue;
4057 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
4060 /* Now update local dynamic symbols. */
4061 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
4062 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
4063 entry->isym.st_name);
4065 /* And the rest of dynamic symbols. */
4066 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
4068 /* Adjust version definitions. */
4069 if (elf_tdata (output_bfd)->cverdefs)
4071 asection *s;
4072 bfd_byte *p;
4073 size_t i;
4074 Elf_Internal_Verdef def;
4075 Elf_Internal_Verdaux defaux;
4077 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
4078 p = s->contents;
4081 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
4082 &def);
4083 p += sizeof (Elf_External_Verdef);
4084 if (def.vd_aux != sizeof (Elf_External_Verdef))
4085 continue;
4086 for (i = 0; i < def.vd_cnt; ++i)
4088 _bfd_elf_swap_verdaux_in (output_bfd,
4089 (Elf_External_Verdaux *) p, &defaux);
4090 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
4091 defaux.vda_name);
4092 _bfd_elf_swap_verdaux_out (output_bfd,
4093 &defaux, (Elf_External_Verdaux *) p);
4094 p += sizeof (Elf_External_Verdaux);
4097 while (def.vd_next);
4100 /* Adjust version references. */
4101 if (elf_tdata (output_bfd)->verref)
4103 asection *s;
4104 bfd_byte *p;
4105 size_t i;
4106 Elf_Internal_Verneed need;
4107 Elf_Internal_Vernaux needaux;
4109 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
4110 p = s->contents;
4113 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
4114 &need);
4115 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
4116 _bfd_elf_swap_verneed_out (output_bfd, &need,
4117 (Elf_External_Verneed *) p);
4118 p += sizeof (Elf_External_Verneed);
4119 for (i = 0; i < need.vn_cnt; ++i)
4121 _bfd_elf_swap_vernaux_in (output_bfd,
4122 (Elf_External_Vernaux *) p, &needaux);
4123 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
4124 needaux.vna_name);
4125 _bfd_elf_swap_vernaux_out (output_bfd,
4126 &needaux,
4127 (Elf_External_Vernaux *) p);
4128 p += sizeof (Elf_External_Vernaux);
4131 while (need.vn_next);
4134 return true;
4137 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4138 The default is to only match when the INPUT and OUTPUT are exactly
4139 the same target. */
4141 bool
4142 _bfd_elf_default_relocs_compatible (const bfd_target *input,
4143 const bfd_target *output)
4145 return input == output;
4148 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4149 This version is used when different targets for the same architecture
4150 are virtually identical. */
4152 bool
4153 _bfd_elf_relocs_compatible (const bfd_target *input,
4154 const bfd_target *output)
4156 const struct elf_backend_data *obed, *ibed;
4158 if (input == output)
4159 return true;
4161 ibed = xvec_get_elf_backend_data (input);
4162 obed = xvec_get_elf_backend_data (output);
4164 if (ibed->arch != obed->arch)
4165 return false;
4167 /* If both backends are using this function, deem them compatible. */
4168 return ibed->relocs_compatible == obed->relocs_compatible;
4171 /* Make a special call to the linker "notice" function to tell it that
4172 we are about to handle an as-needed lib, or have finished
4173 processing the lib. */
4175 bool
4176 _bfd_elf_notice_as_needed (bfd *ibfd,
4177 struct bfd_link_info *info,
4178 enum notice_asneeded_action act)
4180 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
4183 /* Call ACTION on each relocation in an ELF object file. */
4185 bool
4186 _bfd_elf_link_iterate_on_relocs
4187 (bfd *abfd, struct bfd_link_info *info,
4188 bool (*action) (bfd *, struct bfd_link_info *, asection *,
4189 const Elf_Internal_Rela *))
4191 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4192 struct elf_link_hash_table *htab = elf_hash_table (info);
4194 /* If this object is the same format as the output object, and it is
4195 not a shared library, then let the backend look through the
4196 relocs.
4198 This is required to build global offset table entries and to
4199 arrange for dynamic relocs. It is not required for the
4200 particular common case of linking non PIC code, even when linking
4201 against shared libraries, but unfortunately there is no way of
4202 knowing whether an object file has been compiled PIC or not.
4203 Looking through the relocs is not particularly time consuming.
4204 The problem is that we must either (1) keep the relocs in memory,
4205 which causes the linker to require additional runtime memory or
4206 (2) read the relocs twice from the input file, which wastes time.
4207 This would be a good case for using mmap.
4209 I have no idea how to handle linking PIC code into a file of a
4210 different format. It probably can't be done. */
4211 if ((abfd->flags & DYNAMIC) == 0
4212 && is_elf_hash_table (&htab->root)
4213 && elf_object_id (abfd) == elf_hash_table_id (htab)
4214 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4216 asection *o;
4218 for (o = abfd->sections; o != NULL; o = o->next)
4220 Elf_Internal_Rela *internal_relocs;
4221 bool ok;
4223 /* Don't check relocations in excluded sections. Don't do
4224 anything special with non-loaded, non-alloced sections.
4225 In particular, any relocs in such sections should not
4226 affect GOT and PLT reference counting (ie. we don't
4227 allow them to create GOT or PLT entries), there's no
4228 possibility or desire to optimize TLS relocs, and
4229 there's not much point in propagating relocs to shared
4230 libs that the dynamic linker won't relocate. */
4231 if ((o->flags & SEC_ALLOC) == 0
4232 || (o->flags & SEC_RELOC) == 0
4233 || (o->flags & SEC_EXCLUDE) != 0
4234 || o->reloc_count == 0
4235 || ((info->strip == strip_all || info->strip == strip_debugger)
4236 && (o->flags & SEC_DEBUGGING) != 0)
4237 || bfd_is_abs_section (o->output_section))
4238 continue;
4240 internal_relocs = _bfd_elf_link_info_read_relocs
4241 (abfd, info, o, NULL, NULL,
4242 _bfd_elf_link_keep_memory (info));
4243 if (internal_relocs == NULL)
4244 return false;
4246 ok = action (abfd, info, o, internal_relocs);
4248 if (elf_section_data (o)->relocs != internal_relocs)
4249 free (internal_relocs);
4251 if (! ok)
4252 return false;
4256 return true;
4259 /* Check relocations in an ELF object file. This is called after
4260 all input files have been opened. */
4262 bool
4263 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
4265 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4266 if (bed->check_relocs != NULL)
4267 return _bfd_elf_link_iterate_on_relocs (abfd, info,
4268 bed->check_relocs);
4269 return true;
4272 /* An entry in the first definition hash table. */
4274 struct elf_link_first_hash_entry
4276 struct bfd_hash_entry root;
4277 /* The object of the first definition. */
4278 bfd *abfd;
4281 /* The function to create a new entry in the first definition hash
4282 table. */
4284 static struct bfd_hash_entry *
4285 elf_link_first_hash_newfunc (struct bfd_hash_entry *entry,
4286 struct bfd_hash_table *table,
4287 const char *string)
4289 struct elf_link_first_hash_entry *ret =
4290 (struct elf_link_first_hash_entry *) entry;
4292 /* Allocate the structure if it has not already been allocated by a
4293 subclass. */
4294 if (ret == NULL)
4295 ret = (struct elf_link_first_hash_entry *)
4296 bfd_hash_allocate (table,
4297 sizeof (struct elf_link_first_hash_entry));
4298 if (ret == NULL)
4299 return NULL;
4301 /* Call the allocation method of the superclass. */
4302 ret = ((struct elf_link_first_hash_entry *)
4303 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table,
4304 string));
4305 if (ret != NULL)
4306 ret->abfd = NULL;
4308 return (struct bfd_hash_entry *) ret;
4311 /* Add the symbol NAME from ABFD to first hash. */
4313 static void
4314 elf_link_add_to_first_hash (bfd *abfd, struct bfd_link_info *info,
4315 const char *name, bool copy)
4317 struct elf_link_hash_table *htab = elf_hash_table (info);
4318 /* Skip if there is no first hash. */
4319 if (htab->first_hash == NULL)
4320 return;
4322 struct elf_link_first_hash_entry *e
4323 = ((struct elf_link_first_hash_entry *)
4324 bfd_hash_lookup (htab->first_hash, name, true, copy));
4325 if (e == NULL)
4326 info->callbacks->einfo
4327 (_("%F%P: %pB: failed to add %s to first hash\n"), abfd, name);
4329 if (e->abfd == NULL)
4330 /* Store ABFD in abfd. */
4331 e->abfd = abfd;
4334 /* Add symbols from an ELF object file to the linker hash table. */
4336 static bool
4337 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
4339 Elf_Internal_Ehdr *ehdr;
4340 Elf_Internal_Shdr *hdr;
4341 size_t symcount;
4342 size_t extsymcount;
4343 size_t extsymoff;
4344 struct elf_link_hash_entry **sym_hash;
4345 bool dynamic;
4346 Elf_External_Versym *extversym = NULL;
4347 Elf_External_Versym *extversym_end = NULL;
4348 Elf_External_Versym *ever;
4349 struct elf_link_hash_entry *weaks;
4350 struct elf_link_hash_entry **nondeflt_vers = NULL;
4351 size_t nondeflt_vers_cnt = 0;
4352 Elf_Internal_Sym *isymbuf = NULL;
4353 Elf_Internal_Sym *isym;
4354 Elf_Internal_Sym *isymend;
4355 const struct elf_backend_data *bed;
4356 bool add_needed;
4357 struct elf_link_hash_table *htab;
4358 void *alloc_mark = NULL;
4359 struct bfd_hash_entry **old_table = NULL;
4360 unsigned int old_size = 0;
4361 unsigned int old_count = 0;
4362 void *old_tab = NULL;
4363 void *old_ent;
4364 struct bfd_link_hash_entry *old_undefs = NULL;
4365 struct bfd_link_hash_entry *old_undefs_tail = NULL;
4366 void *old_strtab = NULL;
4367 size_t tabsize = 0;
4368 asection *s;
4369 bool just_syms;
4371 htab = elf_hash_table (info);
4372 bed = get_elf_backend_data (abfd);
4374 if (elf_use_dt_symtab_p (abfd))
4376 bfd_set_error (bfd_error_wrong_format);
4377 return false;
4380 if ((abfd->flags & DYNAMIC) == 0)
4382 dynamic = false;
4383 if ((abfd->flags & BFD_PLUGIN) != 0
4384 && is_elf_hash_table (&htab->root)
4385 && htab->first_hash == NULL)
4387 /* Initialize first_hash for an IR input. */
4388 htab->first_hash = (struct bfd_hash_table *)
4389 bfd_malloc (sizeof (struct bfd_hash_table));
4390 if (htab->first_hash == NULL
4391 || !bfd_hash_table_init
4392 (htab->first_hash, elf_link_first_hash_newfunc,
4393 sizeof (struct elf_link_first_hash_entry)))
4394 info->callbacks->einfo
4395 (_("%F%P: first_hash failed to create: %E\n"));
4398 else
4400 dynamic = true;
4402 /* You can't use -r against a dynamic object. Also, there's no
4403 hope of using a dynamic object which does not exactly match
4404 the format of the output file. */
4405 if (bfd_link_relocatable (info)
4406 || !is_elf_hash_table (&htab->root)
4407 || info->output_bfd->xvec != abfd->xvec)
4409 if (bfd_link_relocatable (info))
4410 bfd_set_error (bfd_error_invalid_operation);
4411 else
4412 bfd_set_error (bfd_error_wrong_format);
4413 goto error_return;
4417 ehdr = elf_elfheader (abfd);
4418 if (info->warn_alternate_em
4419 && bed->elf_machine_code != ehdr->e_machine
4420 && ((bed->elf_machine_alt1 != 0
4421 && ehdr->e_machine == bed->elf_machine_alt1)
4422 || (bed->elf_machine_alt2 != 0
4423 && ehdr->e_machine == bed->elf_machine_alt2)))
4424 _bfd_error_handler
4425 /* xgettext:c-format */
4426 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
4427 ehdr->e_machine, abfd, bed->elf_machine_code);
4429 /* As a GNU extension, any input sections which are named
4430 .gnu.warning.SYMBOL are treated as warning symbols for the given
4431 symbol. This differs from .gnu.warning sections, which generate
4432 warnings when they are included in an output file. */
4433 /* PR 12761: Also generate this warning when building shared libraries. */
4434 for (s = abfd->sections; s != NULL; s = s->next)
4436 const char *name;
4438 name = bfd_section_name (s);
4439 if (startswith (name, ".gnu.warning."))
4441 char *msg;
4442 bfd_size_type sz;
4444 name += sizeof ".gnu.warning." - 1;
4446 /* If this is a shared object, then look up the symbol
4447 in the hash table. If it is there, and it is already
4448 been defined, then we will not be using the entry
4449 from this shared object, so we don't need to warn.
4450 FIXME: If we see the definition in a regular object
4451 later on, we will warn, but we shouldn't. The only
4452 fix is to keep track of what warnings we are supposed
4453 to emit, and then handle them all at the end of the
4454 link. */
4455 if (dynamic)
4457 struct elf_link_hash_entry *h;
4459 h = elf_link_hash_lookup (htab, name, false, false, true);
4461 /* FIXME: What about bfd_link_hash_common? */
4462 if (h != NULL
4463 && (h->root.type == bfd_link_hash_defined
4464 || h->root.type == bfd_link_hash_defweak))
4465 continue;
4468 sz = s->size;
4469 msg = (char *) bfd_alloc (abfd, sz + 1);
4470 if (msg == NULL)
4471 goto error_return;
4473 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
4474 goto error_return;
4476 msg[sz] = '\0';
4478 if (! (_bfd_generic_link_add_one_symbol
4479 (info, abfd, name, BSF_WARNING, s, 0, msg,
4480 false, bed->collect, NULL)))
4481 goto error_return;
4483 if (bfd_link_executable (info))
4485 /* Clobber the section size so that the warning does
4486 not get copied into the output file. */
4487 s->size = 0;
4489 /* Also set SEC_EXCLUDE, so that symbols defined in
4490 the warning section don't get copied to the output. */
4491 s->flags |= SEC_EXCLUDE;
4496 just_syms = ((s = abfd->sections) != NULL
4497 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
4499 add_needed = true;
4500 if (! dynamic)
4502 /* If we are creating a shared library, create all the dynamic
4503 sections immediately. We need to attach them to something,
4504 so we attach them to this BFD, provided it is the right
4505 format and is not from ld --just-symbols. Always create the
4506 dynamic sections for -E/--dynamic-list. FIXME: If there
4507 are no input BFD's of the same format as the output, we can't
4508 make a shared library. */
4509 if (!just_syms
4510 && (bfd_link_pic (info)
4511 || (!bfd_link_relocatable (info)
4512 && info->nointerp
4513 && (info->export_dynamic || info->dynamic)))
4514 && is_elf_hash_table (&htab->root)
4515 && info->output_bfd->xvec == abfd->xvec
4516 && !htab->dynamic_sections_created)
4518 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4519 goto error_return;
4522 else if (!is_elf_hash_table (&htab->root))
4523 goto error_return;
4524 else
4526 const char *soname = NULL;
4527 char *audit = NULL;
4528 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
4529 const Elf_Internal_Phdr *phdr;
4530 struct elf_link_loaded_list *loaded_lib;
4532 /* ld --just-symbols and dynamic objects don't mix very well.
4533 ld shouldn't allow it. */
4534 if (just_syms)
4535 abort ();
4537 /* If this dynamic lib was specified on the command line with
4538 --as-needed in effect, then we don't want to add a DT_NEEDED
4539 tag unless the lib is actually used. Similary for libs brought
4540 in by another lib's DT_NEEDED. When --no-add-needed is used
4541 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4542 any dynamic library in DT_NEEDED tags in the dynamic lib at
4543 all. */
4544 add_needed = (elf_dyn_lib_class (abfd)
4545 & (DYN_AS_NEEDED | DYN_DT_NEEDED
4546 | DYN_NO_NEEDED)) == 0;
4548 s = bfd_get_section_by_name (abfd, ".dynamic");
4549 if (s != NULL && s->size != 0 && (s->flags & SEC_HAS_CONTENTS) != 0)
4551 bfd_byte *dynbuf;
4552 bfd_byte *extdyn;
4553 unsigned int elfsec;
4554 unsigned long shlink;
4556 if (!_bfd_elf_mmap_section_contents (abfd, s, &dynbuf))
4558 error_free_dyn:
4559 _bfd_elf_munmap_section_contents (s, dynbuf);
4560 goto error_return;
4563 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
4564 if (elfsec == SHN_BAD)
4565 goto error_free_dyn;
4566 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4568 for (extdyn = dynbuf;
4569 (size_t) (dynbuf + s->size - extdyn) >= bed->s->sizeof_dyn;
4570 extdyn += bed->s->sizeof_dyn)
4572 Elf_Internal_Dyn dyn;
4574 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4575 if (dyn.d_tag == DT_SONAME)
4577 unsigned int tagv = dyn.d_un.d_val;
4578 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4579 if (soname == NULL)
4580 goto error_free_dyn;
4582 if (dyn.d_tag == DT_NEEDED)
4584 struct bfd_link_needed_list *n, **pn;
4585 char *fnm, *anm;
4586 unsigned int tagv = dyn.d_un.d_val;
4587 size_t amt = sizeof (struct bfd_link_needed_list);
4589 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4590 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4591 if (n == NULL || fnm == NULL)
4592 goto error_free_dyn;
4593 amt = strlen (fnm) + 1;
4594 anm = (char *) bfd_alloc (abfd, amt);
4595 if (anm == NULL)
4596 goto error_free_dyn;
4597 memcpy (anm, fnm, amt);
4598 n->name = anm;
4599 n->by = abfd;
4600 n->next = NULL;
4601 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4603 *pn = n;
4605 if (dyn.d_tag == DT_RUNPATH)
4607 struct bfd_link_needed_list *n, **pn;
4608 char *fnm, *anm;
4609 unsigned int tagv = dyn.d_un.d_val;
4610 size_t amt = sizeof (struct bfd_link_needed_list);
4612 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4613 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4614 if (n == NULL || fnm == NULL)
4615 goto error_free_dyn;
4616 amt = strlen (fnm) + 1;
4617 anm = (char *) bfd_alloc (abfd, amt);
4618 if (anm == NULL)
4619 goto error_free_dyn;
4620 memcpy (anm, fnm, amt);
4621 n->name = anm;
4622 n->by = abfd;
4623 n->next = NULL;
4624 for (pn = & runpath;
4625 *pn != NULL;
4626 pn = &(*pn)->next)
4628 *pn = n;
4630 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4631 if (!runpath && dyn.d_tag == DT_RPATH)
4633 struct bfd_link_needed_list *n, **pn;
4634 char *fnm, *anm;
4635 unsigned int tagv = dyn.d_un.d_val;
4636 size_t amt = sizeof (struct bfd_link_needed_list);
4638 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4639 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4640 if (n == NULL || fnm == NULL)
4641 goto error_free_dyn;
4642 amt = strlen (fnm) + 1;
4643 anm = (char *) bfd_alloc (abfd, amt);
4644 if (anm == NULL)
4645 goto error_free_dyn;
4646 memcpy (anm, fnm, amt);
4647 n->name = anm;
4648 n->by = abfd;
4649 n->next = NULL;
4650 for (pn = & rpath;
4651 *pn != NULL;
4652 pn = &(*pn)->next)
4654 *pn = n;
4656 if (dyn.d_tag == DT_AUDIT)
4658 unsigned int tagv = dyn.d_un.d_val;
4659 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4661 if (dyn.d_tag == DT_FLAGS_1)
4662 elf_tdata (abfd)->is_pie = (dyn.d_un.d_val & DF_1_PIE) != 0;
4665 _bfd_elf_munmap_section_contents (s, dynbuf);
4668 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4669 frees all more recently bfd_alloc'd blocks as well. */
4670 if (runpath)
4671 rpath = runpath;
4673 if (rpath)
4675 struct bfd_link_needed_list **pn;
4676 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4678 *pn = rpath;
4681 /* If we have a PT_GNU_RELRO program header, mark as read-only
4682 all sections contained fully therein. This makes relro
4683 shared library sections appear as they will at run-time. */
4684 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4685 while (phdr-- > elf_tdata (abfd)->phdr)
4686 if (phdr->p_type == PT_GNU_RELRO)
4688 for (s = abfd->sections; s != NULL; s = s->next)
4690 unsigned int opb = bfd_octets_per_byte (abfd, s);
4692 if ((s->flags & SEC_ALLOC) != 0
4693 && s->vma * opb >= phdr->p_vaddr
4694 && s->vma * opb + s->size <= phdr->p_vaddr + phdr->p_memsz)
4695 s->flags |= SEC_READONLY;
4697 break;
4700 /* We do not want to include any of the sections in a dynamic
4701 object in the output file. We hack by simply clobbering the
4702 list of sections in the BFD. This could be handled more
4703 cleanly by, say, a new section flag; the existing
4704 SEC_NEVER_LOAD flag is not the one we want, because that one
4705 still implies that the section takes up space in the output
4706 file. */
4707 bfd_section_list_clear (abfd);
4709 /* Find the name to use in a DT_NEEDED entry that refers to this
4710 object. If the object has a DT_SONAME entry, we use it.
4711 Otherwise, if the generic linker stuck something in
4712 elf_dt_name, we use that. Otherwise, we just use the file
4713 name. */
4714 if (soname == NULL || *soname == '\0')
4716 soname = elf_dt_name (abfd);
4717 if (soname == NULL || *soname == '\0')
4718 soname = bfd_get_filename (abfd);
4721 /* Save the SONAME because sometimes the linker emulation code
4722 will need to know it. */
4723 elf_dt_name (abfd) = soname;
4725 /* If we have already included this dynamic object in the
4726 link, just ignore it. There is no reason to include a
4727 particular dynamic object more than once. */
4728 for (loaded_lib = htab->dyn_loaded;
4729 loaded_lib != NULL;
4730 loaded_lib = loaded_lib->next)
4732 if (strcmp (elf_dt_name (loaded_lib->abfd), soname) == 0)
4733 return true;
4736 /* Create dynamic sections for backends that require that be done
4737 before setup_gnu_properties. */
4738 if (add_needed
4739 && !_bfd_elf_link_create_dynamic_sections (abfd, info))
4740 return false;
4742 /* Save the DT_AUDIT entry for the linker emulation code. */
4743 elf_dt_audit (abfd) = audit;
4746 /* If this is a dynamic object, we always link against the .dynsym
4747 symbol table, not the .symtab symbol table. The dynamic linker
4748 will only see the .dynsym symbol table, so there is no reason to
4749 look at .symtab for a dynamic object. */
4751 if (! dynamic || elf_dynsymtab (abfd) == 0)
4752 hdr = &elf_tdata (abfd)->symtab_hdr;
4753 else
4754 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4756 symcount = hdr->sh_size / bed->s->sizeof_sym;
4758 /* The sh_info field of the symtab header tells us where the
4759 external symbols start. We don't care about the local symbols at
4760 this point. */
4761 if (elf_bad_symtab (abfd))
4763 extsymcount = symcount;
4764 extsymoff = 0;
4766 else
4768 extsymcount = symcount - hdr->sh_info;
4769 extsymoff = hdr->sh_info;
4772 sym_hash = elf_sym_hashes (abfd);
4773 if (extsymcount != 0)
4775 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4776 NULL, NULL, NULL);
4777 if (isymbuf == NULL)
4778 goto error_return;
4780 if (sym_hash == NULL)
4782 /* We store a pointer to the hash table entry for each
4783 external symbol. */
4784 size_t amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4785 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4786 if (sym_hash == NULL)
4787 goto error_free_sym;
4788 elf_sym_hashes (abfd) = sym_hash;
4792 if (dynamic)
4794 /* Read in any version definitions. */
4795 if (!_bfd_elf_slurp_version_tables (abfd,
4796 info->default_imported_symver))
4797 goto error_free_sym;
4799 /* Read in the symbol versions, but don't bother to convert them
4800 to internal format. */
4801 if (elf_dynversym (abfd) != 0)
4803 Elf_Internal_Shdr *versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4804 bfd_size_type amt = versymhdr->sh_size;
4806 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0)
4807 goto error_free_sym;
4808 extversym = (Elf_External_Versym *)
4809 _bfd_malloc_and_read (abfd, amt, amt);
4810 if (extversym == NULL)
4811 goto error_free_sym;
4812 extversym_end = extversym + amt / sizeof (*extversym);
4816 /* If we are loading an as-needed shared lib, save the symbol table
4817 state before we start adding symbols. If the lib turns out
4818 to be unneeded, restore the state. */
4819 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4821 unsigned int i;
4822 size_t entsize;
4824 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4826 struct bfd_hash_entry *p;
4827 struct elf_link_hash_entry *h;
4829 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4831 h = (struct elf_link_hash_entry *) p;
4832 entsize += htab->root.table.entsize;
4833 if (h->root.type == bfd_link_hash_warning)
4835 entsize += htab->root.table.entsize;
4836 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4838 if (h->root.type == bfd_link_hash_common)
4839 entsize += sizeof (*h->root.u.c.p);
4843 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4844 old_tab = bfd_malloc (tabsize + entsize);
4845 if (old_tab == NULL)
4846 goto error_free_vers;
4848 /* Remember the current objalloc pointer, so that all mem for
4849 symbols added can later be reclaimed. */
4850 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4851 if (alloc_mark == NULL)
4852 goto error_free_vers;
4854 /* Make a special call to the linker "notice" function to
4855 tell it that we are about to handle an as-needed lib. */
4856 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4857 goto error_free_vers;
4859 /* Clone the symbol table. Remember some pointers into the
4860 symbol table, and dynamic symbol count. */
4861 old_ent = (char *) old_tab + tabsize;
4862 memcpy (old_tab, htab->root.table.table, tabsize);
4863 old_undefs = htab->root.undefs;
4864 old_undefs_tail = htab->root.undefs_tail;
4865 old_table = htab->root.table.table;
4866 old_size = htab->root.table.size;
4867 old_count = htab->root.table.count;
4868 old_strtab = NULL;
4869 if (htab->dynstr != NULL)
4871 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4872 if (old_strtab == NULL)
4873 goto error_free_vers;
4876 for (i = 0; i < htab->root.table.size; i++)
4878 struct bfd_hash_entry *p;
4879 struct elf_link_hash_entry *h;
4881 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4883 h = (struct elf_link_hash_entry *) p;
4884 memcpy (old_ent, h, htab->root.table.entsize);
4885 old_ent = (char *) old_ent + htab->root.table.entsize;
4886 if (h->root.type == bfd_link_hash_warning)
4888 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4889 memcpy (old_ent, h, htab->root.table.entsize);
4890 old_ent = (char *) old_ent + htab->root.table.entsize;
4892 if (h->root.type == bfd_link_hash_common)
4894 memcpy (old_ent, h->root.u.c.p, sizeof (*h->root.u.c.p));
4895 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
4901 weaks = NULL;
4902 if (extversym == NULL)
4903 ever = NULL;
4904 else if (extversym + extsymoff < extversym_end)
4905 ever = extversym + extsymoff;
4906 else
4908 /* xgettext:c-format */
4909 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4910 abfd, (long) extsymoff,
4911 (long) (extversym_end - extversym) / sizeof (* extversym));
4912 bfd_set_error (bfd_error_bad_value);
4913 goto error_free_vers;
4916 if (!bfd_link_relocatable (info)
4917 && bfd_get_lto_type (abfd) == lto_slim_ir_object)
4919 _bfd_error_handler
4920 (_("%pB: plugin needed to handle lto object"), abfd);
4923 for (isym = isymbuf, isymend = PTR_ADD (isymbuf, extsymcount);
4924 isym < isymend;
4925 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4927 int bind;
4928 bfd_vma value;
4929 asection *sec, *new_sec;
4930 flagword flags;
4931 const char *name;
4932 bool must_copy_name = false;
4933 struct elf_link_hash_entry *h;
4934 struct elf_link_hash_entry *hi;
4935 bool definition;
4936 bool size_change_ok;
4937 bool type_change_ok;
4938 bool new_weak;
4939 bool old_weak;
4940 bfd *override;
4941 bool common;
4942 bool discarded;
4943 unsigned int old_alignment;
4944 unsigned int shindex;
4945 bfd *old_bfd;
4946 bool matched;
4948 override = NULL;
4950 flags = BSF_NO_FLAGS;
4951 sec = NULL;
4952 value = isym->st_value;
4953 common = bed->common_definition (isym);
4954 if (common && info->inhibit_common_definition)
4956 /* Treat common symbol as undefined for --no-define-common. */
4957 isym->st_shndx = SHN_UNDEF;
4958 common = false;
4960 discarded = false;
4962 bind = ELF_ST_BIND (isym->st_info);
4963 switch (bind)
4965 case STB_LOCAL:
4966 /* This should be impossible, since ELF requires that all
4967 global symbols follow all local symbols, and that sh_info
4968 point to the first global symbol. Unfortunately, Irix 5
4969 screws this up. */
4970 if (elf_bad_symtab (abfd))
4971 continue;
4973 /* If we aren't prepared to handle locals within the globals
4974 then we'll likely segfault on a NULL symbol hash if the
4975 symbol is ever referenced in relocations. */
4976 shindex = elf_elfheader (abfd)->e_shstrndx;
4977 name = bfd_elf_string_from_elf_section (abfd, shindex, hdr->sh_name);
4978 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4979 " (>= sh_info of %lu)"),
4980 abfd, name, (long) (isym - isymbuf + extsymoff),
4981 (long) extsymoff);
4983 /* Dynamic object relocations are not processed by ld, so
4984 ld won't run into the problem mentioned above. */
4985 if (dynamic)
4986 continue;
4987 bfd_set_error (bfd_error_bad_value);
4988 goto error_free_vers;
4990 case STB_GLOBAL:
4991 if (isym->st_shndx != SHN_UNDEF && !common)
4992 flags = BSF_GLOBAL;
4993 break;
4995 case STB_WEAK:
4996 flags = BSF_WEAK;
4997 break;
4999 case STB_GNU_UNIQUE:
5000 flags = BSF_GNU_UNIQUE;
5001 break;
5003 default:
5004 /* Leave it up to the processor backend. */
5005 break;
5008 if (isym->st_shndx == SHN_UNDEF)
5009 sec = bfd_und_section_ptr;
5010 else if (isym->st_shndx == SHN_ABS)
5011 sec = bfd_abs_section_ptr;
5012 else if (isym->st_shndx == SHN_COMMON)
5014 sec = bfd_com_section_ptr;
5015 /* What ELF calls the size we call the value. What ELF
5016 calls the value we call the alignment. */
5017 value = isym->st_size;
5019 else
5021 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
5022 if (sec == NULL)
5023 sec = bfd_abs_section_ptr;
5024 else if (discarded_section (sec))
5026 /* Symbols from discarded section are undefined. We keep
5027 its visibility. */
5028 sec = bfd_und_section_ptr;
5029 discarded = true;
5030 isym->st_shndx = SHN_UNDEF;
5032 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
5033 value -= sec->vma;
5036 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5037 isym->st_name);
5038 if (name == NULL)
5039 goto error_free_vers;
5041 if (isym->st_shndx == SHN_COMMON
5042 && (abfd->flags & BFD_PLUGIN) != 0)
5044 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
5046 if (xc == NULL)
5048 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
5049 | SEC_EXCLUDE);
5050 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
5051 if (xc == NULL)
5052 goto error_free_vers;
5054 sec = xc;
5056 else if (isym->st_shndx == SHN_COMMON
5057 && ELF_ST_TYPE (isym->st_info) == STT_TLS
5058 && !bfd_link_relocatable (info))
5060 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
5062 if (tcomm == NULL)
5064 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
5065 | SEC_LINKER_CREATED);
5066 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
5067 if (tcomm == NULL)
5068 goto error_free_vers;
5070 sec = tcomm;
5072 else if (bed->elf_add_symbol_hook)
5074 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
5075 &sec, &value))
5076 goto error_free_vers;
5078 /* The hook function sets the name to NULL if this symbol
5079 should be skipped for some reason. */
5080 if (name == NULL)
5081 continue;
5084 /* Sanity check that all possibilities were handled. */
5085 if (sec == NULL)
5086 abort ();
5088 /* Silently discard TLS symbols from --just-syms. There's
5089 no way to combine a static TLS block with a new TLS block
5090 for this executable. */
5091 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
5092 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
5093 continue;
5095 if (bfd_is_und_section (sec)
5096 || bfd_is_com_section (sec))
5097 definition = false;
5098 else
5099 definition = true;
5101 size_change_ok = false;
5102 type_change_ok = bed->type_change_ok;
5103 old_weak = false;
5104 matched = false;
5105 old_alignment = 0;
5106 old_bfd = NULL;
5107 new_sec = sec;
5109 if (is_elf_hash_table (&htab->root))
5111 Elf_Internal_Versym iver;
5112 unsigned int vernum = 0;
5113 bool skip;
5115 if (ever == NULL)
5117 if (info->default_imported_symver)
5118 /* Use the default symbol version created earlier. */
5119 iver.vs_vers = elf_tdata (abfd)->cverdefs;
5120 else
5121 iver.vs_vers = 0;
5123 else if (ever >= extversym_end)
5125 /* xgettext:c-format */
5126 _bfd_error_handler (_("%pB: not enough version information"),
5127 abfd);
5128 bfd_set_error (bfd_error_bad_value);
5129 goto error_free_vers;
5131 else
5132 _bfd_elf_swap_versym_in (abfd, ever, &iver);
5134 vernum = iver.vs_vers & VERSYM_VERSION;
5136 /* If this is a hidden symbol, or if it is not version
5137 1, we append the version name to the symbol name.
5138 However, we do not modify a non-hidden absolute symbol
5139 if it is not a function, because it might be the version
5140 symbol itself. FIXME: What if it isn't? */
5141 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
5142 || (vernum > 1
5143 && (!bfd_is_abs_section (sec)
5144 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
5146 const char *verstr;
5147 size_t namelen, verlen, newlen;
5148 char *newname, *p;
5150 if (isym->st_shndx != SHN_UNDEF)
5152 if (vernum > elf_tdata (abfd)->cverdefs)
5153 verstr = NULL;
5154 else if (vernum > 1)
5155 verstr =
5156 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
5157 else
5158 verstr = "";
5160 if (verstr == NULL)
5162 _bfd_error_handler
5163 /* xgettext:c-format */
5164 (_("%pB: %s: invalid version %u (max %d)"),
5165 abfd, name, vernum,
5166 elf_tdata (abfd)->cverdefs);
5167 bfd_set_error (bfd_error_bad_value);
5168 goto error_free_vers;
5171 else
5173 /* We cannot simply test for the number of
5174 entries in the VERNEED section since the
5175 numbers for the needed versions do not start
5176 at 0. */
5177 Elf_Internal_Verneed *t;
5179 verstr = NULL;
5180 for (t = elf_tdata (abfd)->verref;
5181 t != NULL;
5182 t = t->vn_nextref)
5184 Elf_Internal_Vernaux *a;
5186 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5188 if (a->vna_other == vernum)
5190 verstr = a->vna_nodename;
5191 break;
5194 if (a != NULL)
5195 break;
5197 if (verstr == NULL)
5199 _bfd_error_handler
5200 /* xgettext:c-format */
5201 (_("%pB: %s: invalid needed version %d"),
5202 abfd, name, vernum);
5203 bfd_set_error (bfd_error_bad_value);
5204 goto error_free_vers;
5208 namelen = strlen (name);
5209 verlen = strlen (verstr);
5210 newlen = namelen + verlen + 2;
5211 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
5212 && isym->st_shndx != SHN_UNDEF)
5213 ++newlen;
5215 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
5216 if (newname == NULL)
5217 goto error_free_vers;
5218 memcpy (newname, name, namelen);
5219 p = newname + namelen;
5220 *p++ = ELF_VER_CHR;
5221 /* If this is a defined non-hidden version symbol,
5222 we add another @ to the name. This indicates the
5223 default version of the symbol. */
5224 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
5225 && isym->st_shndx != SHN_UNDEF)
5226 *p++ = ELF_VER_CHR;
5227 memcpy (p, verstr, verlen + 1);
5229 name = newname;
5230 /* Since bfd_hash_alloc is used for "name", the string
5231 must be copied if added to first_hash. The string
5232 memory can be freed when an --as-needed library is
5233 not needed. */
5234 must_copy_name = true;
5237 /* If this symbol has default visibility and the user has
5238 requested we not re-export it, then mark it as hidden. */
5239 if (!bfd_is_und_section (sec)
5240 && !dynamic
5241 && abfd->no_export
5242 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
5243 isym->st_other = (STV_HIDDEN
5244 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
5246 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
5247 sym_hash, &old_bfd, &old_weak,
5248 &old_alignment, &skip, &override,
5249 &type_change_ok, &size_change_ok,
5250 &matched))
5251 goto error_free_vers;
5253 if (skip)
5254 continue;
5256 h = *sym_hash;
5257 while (h->root.type == bfd_link_hash_indirect
5258 || h->root.type == bfd_link_hash_warning)
5259 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5261 /* Override a definition only if the new symbol matches the
5262 existing one. */
5263 if (override && matched)
5265 definition = false;
5266 if (htab->first_hash != NULL
5267 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5268 && h->root.non_ir_ref_regular)
5270 /* When reloading --as-needed shared objects for new
5271 symbols added from IR inputs, if this shared object
5272 has the first definition, use it. */
5273 struct elf_link_first_hash_entry *e
5274 = ((struct elf_link_first_hash_entry *)
5275 bfd_hash_lookup (htab->first_hash, name, false,
5276 false));
5277 if (e != NULL && e->abfd == abfd)
5278 definition = true;
5282 if (h->versioned != unversioned
5283 && elf_tdata (abfd)->verdef != NULL
5284 && vernum > 1
5285 && definition)
5286 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
5289 if (! (_bfd_generic_link_add_one_symbol
5290 (info, override ? override : abfd, name, flags, sec, value,
5291 NULL, false, bed->collect,
5292 (struct bfd_link_hash_entry **) sym_hash)))
5293 goto error_free_vers;
5295 h = *sym_hash;
5296 /* We need to make sure that indirect symbol dynamic flags are
5297 updated. */
5298 hi = h;
5299 while (h->root.type == bfd_link_hash_indirect
5300 || h->root.type == bfd_link_hash_warning)
5301 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5303 *sym_hash = h;
5305 /* Setting the index to -3 tells elf_link_output_extsym that
5306 this symbol is defined in a discarded section. */
5307 if (discarded && is_elf_hash_table (&htab->root))
5308 h->indx = -3;
5310 new_weak = (flags & BSF_WEAK) != 0;
5311 if (dynamic
5312 && definition
5313 && new_weak
5314 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
5315 && is_elf_hash_table (&htab->root)
5316 && h->u.alias == NULL)
5318 /* Keep a list of all weak defined non function symbols from
5319 a dynamic object, using the alias field. Later in this
5320 function we will set the alias field to the correct
5321 value. We only put non-function symbols from dynamic
5322 objects on this list, because that happens to be the only
5323 time we need to know the normal symbol corresponding to a
5324 weak symbol, and the information is time consuming to
5325 figure out. If the alias field is not already NULL,
5326 then this symbol was already defined by some previous
5327 dynamic object, and we will be using that previous
5328 definition anyhow. */
5330 h->u.alias = weaks;
5331 weaks = h;
5334 /* Set the alignment of a common symbol. */
5335 if ((common || bfd_is_com_section (sec))
5336 && h->root.type == bfd_link_hash_common)
5338 unsigned int align;
5340 if (common)
5341 align = bfd_log2 (isym->st_value);
5342 else
5344 /* The new symbol is a common symbol in a shared object.
5345 We need to get the alignment from the section. */
5346 align = new_sec->alignment_power;
5348 if (align > old_alignment)
5349 h->root.u.c.p->alignment_power = align;
5350 else
5351 h->root.u.c.p->alignment_power = old_alignment;
5354 if (is_elf_hash_table (&htab->root))
5356 /* Set a flag in the hash table entry indicating the type of
5357 reference or definition we just found. A dynamic symbol
5358 is one which is referenced or defined by both a regular
5359 object and a shared object. */
5360 bool dynsym = false;
5362 /* Plugin symbols aren't normal. Don't set def/ref flags. */
5363 if ((abfd->flags & BFD_PLUGIN) != 0)
5365 /* Except for this flag to track nonweak references. */
5366 if (!definition
5367 && bind != STB_WEAK)
5368 h->ref_ir_nonweak = 1;
5370 else if (!dynamic)
5372 if (! definition)
5374 h->ref_regular = 1;
5375 if (bind != STB_WEAK)
5376 h->ref_regular_nonweak = 1;
5378 else
5380 h->def_regular = 1;
5381 if (h->def_dynamic)
5383 h->def_dynamic = 0;
5384 h->ref_dynamic = 1;
5388 else
5390 if (! definition)
5392 h->ref_dynamic = 1;
5393 hi->ref_dynamic = 1;
5395 else
5397 h->def_dynamic = 1;
5398 hi->def_dynamic = 1;
5402 /* If an indirect symbol has been forced local, don't
5403 make the real symbol dynamic. */
5404 if (h != hi && hi->forced_local)
5406 else if (!dynamic)
5408 if (bfd_link_dll (info)
5409 || h->def_dynamic
5410 || h->ref_dynamic)
5411 dynsym = true;
5413 else
5415 if (h->def_regular
5416 || h->ref_regular
5417 || (h->is_weakalias
5418 && weakdef (h)->dynindx != -1))
5419 dynsym = true;
5422 /* Check to see if we need to add an indirect symbol for
5423 the default name. */
5424 if ((definition
5425 || (!override && h->root.type == bfd_link_hash_common))
5426 && !(hi != h
5427 && hi->versioned == versioned_hidden))
5428 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
5429 sec, value, &old_bfd, &dynsym))
5430 goto error_free_vers;
5432 /* Check the alignment when a common symbol is involved. This
5433 can change when a common symbol is overridden by a normal
5434 definition or a common symbol is ignored due to the old
5435 normal definition. We need to make sure the maximum
5436 alignment is maintained. */
5437 if ((old_alignment || common)
5438 && h->root.type != bfd_link_hash_common)
5440 unsigned int common_align;
5441 unsigned int normal_align;
5442 unsigned int symbol_align;
5443 bfd *normal_bfd;
5444 bfd *common_bfd;
5446 BFD_ASSERT (h->root.type == bfd_link_hash_defined
5447 || h->root.type == bfd_link_hash_defweak);
5449 symbol_align = ffs (h->root.u.def.value) - 1;
5450 if (h->root.u.def.section->owner != NULL
5451 && (h->root.u.def.section->owner->flags
5452 & (DYNAMIC | BFD_PLUGIN)) == 0)
5454 normal_align = h->root.u.def.section->alignment_power;
5455 if (normal_align > symbol_align)
5456 normal_align = symbol_align;
5458 else
5459 normal_align = symbol_align;
5461 if (old_alignment)
5463 common_align = old_alignment;
5464 common_bfd = old_bfd;
5465 normal_bfd = abfd;
5467 else
5469 common_align = bfd_log2 (isym->st_value);
5470 common_bfd = abfd;
5471 normal_bfd = old_bfd;
5474 if (normal_align < common_align)
5476 /* PR binutils/2735 */
5477 if (normal_bfd == NULL)
5478 _bfd_error_handler
5479 /* xgettext:c-format */
5480 (_("warning: alignment %u of common symbol `%s' in %pB is"
5481 " greater than the alignment (%u) of its section %pA"),
5482 1 << common_align, name, common_bfd,
5483 1 << normal_align, h->root.u.def.section);
5484 else
5485 _bfd_error_handler
5486 /* xgettext:c-format */
5487 (_("warning: alignment %u of normal symbol `%s' in %pB"
5488 " is smaller than %u used by the common definition in %pB"),
5489 1 << normal_align, name, normal_bfd,
5490 1 << common_align, common_bfd);
5492 /* PR 30499: make sure that users understand that this warning is serious. */
5493 _bfd_error_handler
5494 (_("warning: NOTE: alignment discrepancies can cause real problems. Investigation is advised."));
5498 /* Remember the symbol size if it isn't undefined. */
5499 if (isym->st_size != 0
5500 && isym->st_shndx != SHN_UNDEF
5501 && (definition || h->size == 0))
5503 if (h->size != 0
5504 && h->size != isym->st_size
5505 && ! size_change_ok)
5507 _bfd_error_handler
5508 /* xgettext:c-format */
5509 (_("warning: size of symbol `%s' changed"
5510 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
5511 name, (uint64_t) h->size, old_bfd,
5512 (uint64_t) isym->st_size, abfd);
5514 /* PR 30499: make sure that users understand that this warning is serious. */
5515 _bfd_error_handler
5516 (_("warning: NOTE: size discrepancies can cause real problems. Investigation is advised."));
5519 h->size = isym->st_size;
5522 /* If this is a common symbol, then we always want H->SIZE
5523 to be the size of the common symbol. The code just above
5524 won't fix the size if a common symbol becomes larger. We
5525 don't warn about a size change here, because that is
5526 covered by --warn-common. Allow changes between different
5527 function types. */
5528 if (h->root.type == bfd_link_hash_common)
5529 h->size = h->root.u.c.size;
5531 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
5532 && ((definition && !new_weak)
5533 || (old_weak && h->root.type == bfd_link_hash_common)
5534 || h->type == STT_NOTYPE))
5536 unsigned int type = ELF_ST_TYPE (isym->st_info);
5538 /* Turn an IFUNC symbol from a DSO into a normal FUNC
5539 symbol. */
5540 if (type == STT_GNU_IFUNC
5541 && (abfd->flags & DYNAMIC) != 0)
5542 type = STT_FUNC;
5544 if (h->type != type)
5546 if (h->type != STT_NOTYPE && ! type_change_ok)
5547 /* xgettext:c-format */
5548 _bfd_error_handler
5549 (_("warning: type of symbol `%s' changed"
5550 " from %d to %d in %pB"),
5551 name, h->type, type, abfd);
5553 h->type = type;
5557 /* Merge st_other field. */
5558 elf_merge_st_other (abfd, h, isym->st_other, sec,
5559 definition, dynamic);
5561 /* We don't want to make debug symbol dynamic. */
5562 if (definition
5563 && (sec->flags & SEC_DEBUGGING)
5564 && !bfd_link_relocatable (info))
5565 dynsym = false;
5567 /* Nor should we make plugin symbols dynamic. */
5568 if ((abfd->flags & BFD_PLUGIN) != 0)
5569 dynsym = false;
5571 if (definition)
5573 h->target_internal = isym->st_target_internal;
5574 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5577 /* Don't add indirect symbols for .symver x, x@FOO aliases
5578 in IR. Since all data or text symbols in IR have the
5579 same type, value and section, we can't tell if a symbol
5580 is an alias of another symbol by their types, values and
5581 sections. */
5582 if (definition
5583 && !dynamic
5584 && (abfd->flags & BFD_PLUGIN) == 0)
5586 char *p = strchr (name, ELF_VER_CHR);
5587 if (p != NULL && p[1] != ELF_VER_CHR)
5589 /* Queue non-default versions so that .symver x, x@FOO
5590 aliases can be checked. */
5591 if (!nondeflt_vers)
5593 size_t amt = ((isymend - isym + 1)
5594 * sizeof (struct elf_link_hash_entry *));
5595 nondeflt_vers
5596 = (struct elf_link_hash_entry **) bfd_malloc (amt);
5597 if (!nondeflt_vers)
5598 goto error_free_vers;
5600 nondeflt_vers[nondeflt_vers_cnt++] = h;
5604 if (dynsym && h->dynindx == -1)
5606 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5607 goto error_free_vers;
5608 if (h->is_weakalias
5609 && weakdef (h)->dynindx == -1)
5611 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
5612 goto error_free_vers;
5615 else if (h->dynindx != -1)
5616 /* If the symbol already has a dynamic index, but
5617 visibility says it should not be visible, turn it into
5618 a local symbol. */
5619 switch (ELF_ST_VISIBILITY (h->other))
5621 case STV_INTERNAL:
5622 case STV_HIDDEN:
5623 (*bed->elf_backend_hide_symbol) (info, h, true);
5624 dynsym = false;
5625 break;
5628 if (!add_needed
5629 && matched
5630 && definition
5631 && h->root.type != bfd_link_hash_indirect)
5633 if ((dynsym
5634 && h->ref_regular_nonweak)
5635 || (old_bfd != NULL
5636 && (old_bfd->flags & BFD_PLUGIN) != 0
5637 && h->ref_ir_nonweak
5638 && !info->lto_all_symbols_read)
5639 || (h->ref_dynamic_nonweak
5640 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5641 && !on_needed_list (elf_dt_name (abfd),
5642 htab->needed, NULL)))
5644 const char *soname = elf_dt_name (abfd);
5646 info->callbacks->minfo ("%!", soname, old_bfd,
5647 h->root.root.string);
5649 /* A symbol from a library loaded via DT_NEEDED of some
5650 other library is referenced by a regular object.
5651 Add a DT_NEEDED entry for it. Issue an error if
5652 --no-add-needed is used and the reference was not
5653 a weak one. */
5654 if (old_bfd != NULL
5655 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
5657 _bfd_error_handler
5658 /* xgettext:c-format */
5659 (_("%pB: undefined reference to symbol '%s'"),
5660 old_bfd, name);
5661 bfd_set_error (bfd_error_missing_dso);
5662 goto error_free_vers;
5665 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
5666 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
5668 /* Create dynamic sections for backends that require
5669 that be done before setup_gnu_properties. */
5670 if (!_bfd_elf_link_create_dynamic_sections (abfd, info))
5671 goto error_free_vers;
5672 add_needed = true;
5674 else if (dynamic
5675 && h->root.u.def.section->owner == abfd)
5676 /* Add this symbol to first hash if this shared
5677 object has the first definition. */
5678 elf_link_add_to_first_hash (abfd, info, name, must_copy_name);
5683 if (info->lto_plugin_active
5684 && !bfd_link_relocatable (info)
5685 && (abfd->flags & BFD_PLUGIN) == 0
5686 && !just_syms
5687 && extsymcount != 0
5688 && is_elf_hash_table (&htab->root))
5690 int r_sym_shift;
5692 if (bed->s->arch_size == 32)
5693 r_sym_shift = 8;
5694 else
5695 r_sym_shift = 32;
5697 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5698 referenced in regular objects so that linker plugin will get
5699 the correct symbol resolution. */
5701 sym_hash = elf_sym_hashes (abfd);
5702 for (s = abfd->sections; s != NULL; s = s->next)
5704 Elf_Internal_Rela *internal_relocs;
5705 Elf_Internal_Rela *rel, *relend;
5707 /* Don't check relocations in excluded sections. */
5708 if ((s->flags & SEC_RELOC) == 0
5709 || s->reloc_count == 0
5710 || (s->flags & SEC_EXCLUDE) != 0
5711 || (s->flags & SEC_DEBUGGING) != 0)
5712 continue;
5714 internal_relocs = _bfd_elf_link_info_read_relocs
5715 (abfd, info, s, NULL, NULL,
5716 _bfd_elf_link_keep_memory (info));
5717 if (internal_relocs == NULL)
5718 goto error_free_vers;
5720 rel = internal_relocs;
5721 relend = rel + s->reloc_count;
5722 for ( ; rel < relend; rel++)
5724 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5725 struct elf_link_hash_entry *h;
5727 /* Skip local symbols. */
5728 if (r_symndx < extsymoff)
5729 continue;
5731 h = sym_hash[r_symndx - extsymoff];
5732 if (h != NULL)
5733 h->root.non_ir_ref_regular = 1;
5736 if (elf_section_data (s)->relocs != internal_relocs)
5737 free (internal_relocs);
5741 free (extversym);
5742 extversym = NULL;
5743 free (isymbuf);
5744 isymbuf = NULL;
5746 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5748 unsigned int i;
5750 /* Restore the symbol table. */
5751 old_ent = (char *) old_tab + tabsize;
5752 memset (elf_sym_hashes (abfd), 0,
5753 extsymcount * sizeof (struct elf_link_hash_entry *));
5754 htab->root.table.table = old_table;
5755 htab->root.table.size = old_size;
5756 htab->root.table.count = old_count;
5757 memcpy (htab->root.table.table, old_tab, tabsize);
5758 htab->root.undefs = old_undefs;
5759 htab->root.undefs_tail = old_undefs_tail;
5760 if (htab->dynstr != NULL)
5761 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5762 free (old_strtab);
5763 old_strtab = NULL;
5764 for (i = 0; i < htab->root.table.size; i++)
5766 struct bfd_hash_entry *p;
5767 struct elf_link_hash_entry *h;
5768 unsigned int non_ir_ref_dynamic;
5770 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5772 /* Preserve non_ir_ref_dynamic so that this symbol
5773 will be exported when the dynamic lib becomes needed
5774 in the second pass. */
5775 h = (struct elf_link_hash_entry *) p;
5776 if (h->root.type == bfd_link_hash_warning)
5777 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5778 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
5780 h = (struct elf_link_hash_entry *) p;
5781 memcpy (h, old_ent, htab->root.table.entsize);
5782 old_ent = (char *) old_ent + htab->root.table.entsize;
5783 if (h->root.type == bfd_link_hash_warning)
5785 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5786 memcpy (h, old_ent, htab->root.table.entsize);
5787 old_ent = (char *) old_ent + htab->root.table.entsize;
5789 if (h->root.type == bfd_link_hash_common)
5791 memcpy (h->root.u.c.p, old_ent, sizeof (*h->root.u.c.p));
5792 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
5794 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
5798 /* Make a special call to the linker "notice" function to
5799 tell it that symbols added for crefs may need to be removed. */
5800 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
5801 goto error_free_vers;
5803 free (old_tab);
5804 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5805 alloc_mark);
5806 free (nondeflt_vers);
5807 return true;
5810 free (old_strtab);
5811 old_strtab = NULL;
5812 if (old_tab != NULL)
5814 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
5815 goto error_free_vers;
5816 free (old_tab);
5817 old_tab = NULL;
5820 /* Now that all the symbols from this input file are created, if
5821 not performing a relocatable link, handle .symver foo, foo@BAR
5822 such that any relocs against foo become foo@BAR. */
5823 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
5825 size_t cnt, symidx;
5827 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5829 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5830 char *shortname, *p;
5831 size_t amt;
5833 p = strchr (h->root.root.string, ELF_VER_CHR);
5834 if (p == NULL
5835 || (h->root.type != bfd_link_hash_defined
5836 && h->root.type != bfd_link_hash_defweak))
5837 continue;
5839 amt = p - h->root.root.string;
5840 shortname = (char *) bfd_malloc (amt + 1);
5841 if (!shortname)
5842 goto error_free_vers;
5843 memcpy (shortname, h->root.root.string, amt);
5844 shortname[amt] = '\0';
5846 hi = (struct elf_link_hash_entry *)
5847 bfd_link_hash_lookup (&htab->root, shortname,
5848 false, false, false);
5849 if (hi != NULL
5850 && hi->root.type == h->root.type
5851 && hi->root.u.def.value == h->root.u.def.value
5852 && hi->root.u.def.section == h->root.u.def.section)
5854 (*bed->elf_backend_hide_symbol) (info, hi, true);
5855 hi->root.type = bfd_link_hash_indirect;
5856 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
5857 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
5858 sym_hash = elf_sym_hashes (abfd);
5859 if (sym_hash)
5860 for (symidx = 0; symidx < extsymcount; ++symidx)
5861 if (sym_hash[symidx] == hi)
5863 sym_hash[symidx] = h;
5864 break;
5867 free (shortname);
5870 free (nondeflt_vers);
5871 nondeflt_vers = NULL;
5873 /* Now set the alias field correctly for all the weak defined
5874 symbols we found. The only way to do this is to search all the
5875 symbols. Since we only need the information for non functions in
5876 dynamic objects, that's the only time we actually put anything on
5877 the list WEAKS. We need this information so that if a regular
5878 object refers to a symbol defined weakly in a dynamic object, the
5879 real symbol in the dynamic object is also put in the dynamic
5880 symbols; we also must arrange for both symbols to point to the
5881 same memory location. We could handle the general case of symbol
5882 aliasing, but a general symbol alias can only be generated in
5883 assembler code, handling it correctly would be very time
5884 consuming, and other ELF linkers don't handle general aliasing
5885 either. */
5886 if (weaks != NULL)
5888 struct elf_link_hash_entry **hpp;
5889 struct elf_link_hash_entry **hppend;
5890 struct elf_link_hash_entry **sorted_sym_hash;
5891 struct elf_link_hash_entry *h;
5892 size_t sym_count, amt;
5894 /* Since we have to search the whole symbol list for each weak
5895 defined symbol, search time for N weak defined symbols will be
5896 O(N^2). Binary search will cut it down to O(NlogN). */
5897 amt = extsymcount * sizeof (*sorted_sym_hash);
5898 sorted_sym_hash = bfd_malloc (amt);
5899 if (sorted_sym_hash == NULL)
5900 goto error_return;
5901 sym_hash = sorted_sym_hash;
5902 hpp = elf_sym_hashes (abfd);
5903 hppend = hpp + extsymcount;
5904 sym_count = 0;
5905 for (; hpp < hppend; hpp++)
5907 h = *hpp;
5908 if (h != NULL
5909 && h->root.type == bfd_link_hash_defined
5910 && !bed->is_function_type (h->type))
5912 *sym_hash = h;
5913 sym_hash++;
5914 sym_count++;
5918 qsort (sorted_sym_hash, sym_count, sizeof (*sorted_sym_hash),
5919 elf_sort_symbol);
5921 while (weaks != NULL)
5923 struct elf_link_hash_entry *hlook;
5924 asection *slook;
5925 bfd_vma vlook;
5926 size_t i, j, idx = 0;
5928 hlook = weaks;
5929 weaks = hlook->u.alias;
5930 hlook->u.alias = NULL;
5932 if (hlook->root.type != bfd_link_hash_defined
5933 && hlook->root.type != bfd_link_hash_defweak)
5934 continue;
5936 slook = hlook->root.u.def.section;
5937 vlook = hlook->root.u.def.value;
5939 i = 0;
5940 j = sym_count;
5941 while (i != j)
5943 bfd_signed_vma vdiff;
5944 idx = (i + j) / 2;
5945 h = sorted_sym_hash[idx];
5946 vdiff = vlook - h->root.u.def.value;
5947 if (vdiff < 0)
5948 j = idx;
5949 else if (vdiff > 0)
5950 i = idx + 1;
5951 else
5953 int sdiff = slook->id - h->root.u.def.section->id;
5954 if (sdiff < 0)
5955 j = idx;
5956 else if (sdiff > 0)
5957 i = idx + 1;
5958 else
5959 break;
5963 /* We didn't find a value/section match. */
5964 if (i == j)
5965 continue;
5967 /* With multiple aliases, or when the weak symbol is already
5968 strongly defined, we have multiple matching symbols and
5969 the binary search above may land on any of them. Step
5970 one past the matching symbol(s). */
5971 while (++idx != j)
5973 h = sorted_sym_hash[idx];
5974 if (h->root.u.def.section != slook
5975 || h->root.u.def.value != vlook)
5976 break;
5979 /* Now look back over the aliases. Since we sorted by size
5980 as well as value and section, we'll choose the one with
5981 the largest size. */
5982 while (idx-- != i)
5984 h = sorted_sym_hash[idx];
5986 /* Stop if value or section doesn't match. */
5987 if (h->root.u.def.section != slook
5988 || h->root.u.def.value != vlook)
5989 break;
5990 else if (h != hlook)
5992 struct elf_link_hash_entry *t;
5994 hlook->u.alias = h;
5995 hlook->is_weakalias = 1;
5996 t = h;
5997 if (t->u.alias != NULL)
5998 while (t->u.alias != h)
5999 t = t->u.alias;
6000 t->u.alias = hlook;
6002 /* If the weak definition is in the list of dynamic
6003 symbols, make sure the real definition is put
6004 there as well. */
6005 if (hlook->dynindx != -1 && h->dynindx == -1)
6007 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6009 err_free_sym_hash:
6010 free (sorted_sym_hash);
6011 goto error_return;
6015 /* If the real definition is in the list of dynamic
6016 symbols, make sure the weak definition is put
6017 there as well. If we don't do this, then the
6018 dynamic loader might not merge the entries for the
6019 real definition and the weak definition. */
6020 if (h->dynindx != -1 && hlook->dynindx == -1)
6022 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
6023 goto err_free_sym_hash;
6025 break;
6030 free (sorted_sym_hash);
6033 if (bed->check_directives
6034 && !(*bed->check_directives) (abfd, info))
6035 goto error_return;
6037 /* If this is a non-traditional link, try to optimize the handling
6038 of the .stab/.stabstr sections. */
6039 if (! dynamic
6040 && ! info->traditional_format
6041 && is_elf_hash_table (&htab->root)
6042 && (info->strip != strip_all && info->strip != strip_debugger))
6044 asection *stabstr;
6046 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
6047 if (stabstr != NULL)
6049 bfd_size_type string_offset = 0;
6050 asection *stab;
6052 for (stab = abfd->sections; stab; stab = stab->next)
6053 if (startswith (stab->name, ".stab")
6054 && (!stab->name[5] ||
6055 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
6056 && (stab->flags & SEC_MERGE) == 0
6057 && !bfd_is_abs_section (stab->output_section))
6059 struct bfd_elf_section_data *secdata;
6061 secdata = elf_section_data (stab);
6062 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
6063 stabstr, &secdata->sec_info,
6064 &string_offset))
6065 goto error_return;
6066 if (secdata->sec_info)
6067 stab->sec_info_type = SEC_INFO_TYPE_STABS;
6072 if (dynamic && add_needed)
6074 /* Add this bfd to the loaded list. */
6075 struct elf_link_loaded_list *n;
6077 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
6078 if (n == NULL)
6079 goto error_return;
6080 n->abfd = abfd;
6081 n->next = htab->dyn_loaded;
6082 htab->dyn_loaded = n;
6084 if (dynamic && !add_needed
6085 && (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) != 0)
6086 elf_dyn_lib_class (abfd) |= DYN_NO_NEEDED;
6088 return true;
6090 error_free_vers:
6091 free (old_tab);
6092 free (old_strtab);
6093 free (nondeflt_vers);
6094 free (extversym);
6095 error_free_sym:
6096 free (isymbuf);
6097 error_return:
6098 return false;
6101 /* Return the linker hash table entry of a symbol that might be
6102 satisfied by an archive symbol. Return -1 on error. */
6104 struct bfd_link_hash_entry *
6105 _bfd_elf_archive_symbol_lookup (bfd *abfd,
6106 struct bfd_link_info *info,
6107 const char *name)
6109 struct bfd_link_hash_entry *h;
6110 char *p, *copy;
6111 size_t len, first;
6113 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
6114 if (h != NULL)
6115 return h;
6117 /* If this is a default version (the name contains @@), look up the
6118 symbol again with only one `@' as well as without the version.
6119 The effect is that references to the symbol with and without the
6120 version will be matched by the default symbol in the archive. */
6122 p = strchr (name, ELF_VER_CHR);
6123 if (p == NULL || p[1] != ELF_VER_CHR)
6125 /* Add this symbol to first hash if this archive has the first
6126 definition. */
6127 if (is_elf_hash_table (info->hash))
6128 elf_link_add_to_first_hash (abfd, info, name, false);
6129 return h;
6132 /* First check with only one `@'. */
6133 len = strlen (name);
6134 copy = (char *) bfd_alloc (abfd, len);
6135 if (copy == NULL)
6136 return (struct bfd_link_hash_entry *) -1;
6138 first = p - name + 1;
6139 memcpy (copy, name, first);
6140 memcpy (copy + first, name + first + 1, len - first);
6142 h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
6143 if (h == NULL)
6145 /* We also need to check references to the symbol without the
6146 version. */
6147 copy[first - 1] = '\0';
6148 h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
6151 bfd_release (abfd, copy);
6152 return h;
6155 /* Add symbols from an ELF archive file to the linker hash table. We
6156 don't use _bfd_generic_link_add_archive_symbols because we need to
6157 handle versioned symbols.
6159 Fortunately, ELF archive handling is simpler than that done by
6160 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
6161 oddities. In ELF, if we find a symbol in the archive map, and the
6162 symbol is currently undefined, we know that we must pull in that
6163 object file.
6165 Unfortunately, we do have to make multiple passes over the symbol
6166 table until nothing further is resolved. */
6168 static bool
6169 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
6171 symindex c;
6172 unsigned char *included = NULL;
6173 carsym *symdefs;
6174 bool loop;
6175 size_t amt;
6176 const struct elf_backend_data *bed;
6177 struct bfd_link_hash_entry * (*archive_symbol_lookup)
6178 (bfd *, struct bfd_link_info *, const char *);
6180 if (! bfd_has_map (abfd))
6182 /* An empty archive is a special case. */
6183 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
6184 return true;
6185 bfd_set_error (bfd_error_no_armap);
6186 return false;
6189 /* Keep track of all symbols we know to be already defined, and all
6190 files we know to be already included. This is to speed up the
6191 second and subsequent passes. */
6192 c = bfd_ardata (abfd)->symdef_count;
6193 if (c == 0)
6194 return true;
6195 amt = c * sizeof (*included);
6196 included = (unsigned char *) bfd_zmalloc (amt);
6197 if (included == NULL)
6198 return false;
6200 symdefs = bfd_ardata (abfd)->symdefs;
6201 bed = get_elf_backend_data (abfd);
6202 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
6206 file_ptr last;
6207 symindex i;
6208 carsym *symdef;
6209 carsym *symdefend;
6211 loop = false;
6212 last = -1;
6214 symdef = symdefs;
6215 symdefend = symdef + c;
6216 for (i = 0; symdef < symdefend; symdef++, i++)
6218 struct bfd_link_hash_entry *h;
6219 bfd *element;
6220 struct bfd_link_hash_entry *undefs_tail;
6221 symindex mark;
6223 if (included[i])
6224 continue;
6225 if (symdef->file_offset == last)
6227 included[i] = true;
6228 continue;
6231 h = archive_symbol_lookup (abfd, info, symdef->name);
6232 if (h == (struct bfd_link_hash_entry *) -1)
6233 goto error_return;
6235 if (h == NULL)
6236 continue;
6238 if (h->type == bfd_link_hash_undefined)
6240 /* If the archive element has already been loaded then one
6241 of the symbols defined by that element might have been
6242 made undefined due to being in a discarded section. */
6243 if (is_elf_hash_table (info->hash)
6244 && ((struct elf_link_hash_entry *) h)->indx == -3)
6245 continue;
6247 else if (h->type == bfd_link_hash_common)
6249 /* We currently have a common symbol. The archive map contains
6250 a reference to this symbol, so we may want to include it. We
6251 only want to include it however, if this archive element
6252 contains a definition of the symbol, not just another common
6253 declaration of it.
6255 Unfortunately some archivers (including GNU ar) will put
6256 declarations of common symbols into their archive maps, as
6257 well as real definitions, so we cannot just go by the archive
6258 map alone. Instead we must read in the element's symbol
6259 table and check that to see what kind of symbol definition
6260 this is. */
6261 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
6262 continue;
6264 else
6266 if (h->type != bfd_link_hash_undefweak)
6267 /* Symbol must be defined. Don't check it again. */
6268 included[i] = true;
6270 if (!is_elf_hash_table (info->hash))
6271 continue;
6272 struct elf_link_hash_entry *eh
6273 = (struct elf_link_hash_entry *) h;
6274 /* Ignore the archive if the symbol isn't referenced by a
6275 regular object or isn't defined in a shared object. */
6276 if (!eh->ref_regular || !eh->def_dynamic)
6277 continue;
6278 /* Ignore the dynamic definition if symbol is first
6279 defined in this archive. */
6280 struct elf_link_hash_table *htab = elf_hash_table (info);
6281 if (htab->first_hash == NULL)
6282 continue;
6283 struct elf_link_first_hash_entry *e
6284 = ((struct elf_link_first_hash_entry *)
6285 bfd_hash_lookup (htab->first_hash, symdef->name,
6286 false, false));
6287 if (e == NULL || e->abfd != abfd)
6288 continue;
6291 /* We need to include this archive member. */
6292 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset,
6293 info);
6294 if (element == NULL)
6295 goto error_return;
6297 if (! bfd_check_format (element, bfd_object))
6298 goto error_return;
6300 undefs_tail = info->hash->undefs_tail;
6302 if (!(*info->callbacks
6303 ->add_archive_element) (info, element, symdef->name, &element))
6304 continue;
6305 if (!bfd_link_add_symbols (element, info))
6306 goto error_return;
6308 /* If there are any new undefined symbols, we need to make
6309 another pass through the archive in order to see whether
6310 they can be defined. FIXME: This isn't perfect, because
6311 common symbols wind up on undefs_tail and because an
6312 undefined symbol which is defined later on in this pass
6313 does not require another pass. This isn't a bug, but it
6314 does make the code less efficient than it could be. */
6315 if (undefs_tail != info->hash->undefs_tail)
6316 loop = true;
6318 /* Look backward to mark all symbols from this object file
6319 which we have already seen in this pass. */
6320 mark = i;
6323 included[mark] = true;
6324 if (mark == 0)
6325 break;
6326 --mark;
6328 while (symdefs[mark].file_offset == symdef->file_offset);
6330 /* We mark subsequent symbols from this object file as we go
6331 on through the loop. */
6332 last = symdef->file_offset;
6335 while (loop);
6337 free (included);
6338 return true;
6340 error_return:
6341 free (included);
6342 return false;
6345 /* Given an ELF BFD, add symbols to the global hash table as
6346 appropriate. */
6348 bool
6349 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
6351 switch (bfd_get_format (abfd))
6353 case bfd_object:
6354 return elf_link_add_object_symbols (abfd, info);
6355 case bfd_archive:
6356 return elf_link_add_archive_symbols (abfd, info);
6357 default:
6358 bfd_set_error (bfd_error_wrong_format);
6359 return false;
6363 struct hash_codes_info
6365 unsigned long *hashcodes;
6366 bool error;
6369 /* This function will be called though elf_link_hash_traverse to store
6370 all hash value of the exported symbols in an array. */
6372 static bool
6373 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
6375 struct hash_codes_info *inf = (struct hash_codes_info *) data;
6376 const char *name;
6377 unsigned long ha;
6378 char *alc = NULL;
6380 /* Ignore indirect symbols. These are added by the versioning code. */
6381 if (h->dynindx == -1)
6382 return true;
6384 name = h->root.root.string;
6385 if (h->versioned >= versioned)
6387 char *p = strchr (name, ELF_VER_CHR);
6388 if (p != NULL)
6390 alc = (char *) bfd_malloc (p - name + 1);
6391 if (alc == NULL)
6393 inf->error = true;
6394 return false;
6396 memcpy (alc, name, p - name);
6397 alc[p - name] = '\0';
6398 name = alc;
6402 /* Compute the hash value. */
6403 ha = bfd_elf_hash (name);
6405 /* Store the found hash value in the array given as the argument. */
6406 *(inf->hashcodes)++ = ha;
6408 /* And store it in the struct so that we can put it in the hash table
6409 later. */
6410 h->u.elf_hash_value = ha;
6412 free (alc);
6413 return true;
6416 struct collect_gnu_hash_codes
6418 bfd *output_bfd;
6419 const struct elf_backend_data *bed;
6420 unsigned long int nsyms;
6421 unsigned long int maskbits;
6422 unsigned long int *hashcodes;
6423 unsigned long int *hashval;
6424 unsigned long int *indx;
6425 unsigned long int *counts;
6426 bfd_vma *bitmask;
6427 bfd_byte *contents;
6428 bfd_size_type xlat;
6429 long int min_dynindx;
6430 unsigned long int bucketcount;
6431 unsigned long int symindx;
6432 long int local_indx;
6433 long int shift1, shift2;
6434 unsigned long int mask;
6435 bool error;
6438 /* This function will be called though elf_link_hash_traverse to store
6439 all hash value of the exported symbols in an array. */
6441 static bool
6442 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
6444 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6445 const char *name;
6446 unsigned long ha;
6447 char *alc = NULL;
6449 /* Ignore indirect symbols. These are added by the versioning code. */
6450 if (h->dynindx == -1)
6451 return true;
6453 /* Ignore also local symbols and undefined symbols. */
6454 if (! (*s->bed->elf_hash_symbol) (h))
6455 return true;
6457 name = h->root.root.string;
6458 if (h->versioned >= versioned)
6460 char *p = strchr (name, ELF_VER_CHR);
6461 if (p != NULL)
6463 alc = (char *) bfd_malloc (p - name + 1);
6464 if (alc == NULL)
6466 s->error = true;
6467 return false;
6469 memcpy (alc, name, p - name);
6470 alc[p - name] = '\0';
6471 name = alc;
6475 /* Compute the hash value. */
6476 ha = bfd_elf_gnu_hash (name);
6478 /* Store the found hash value in the array for compute_bucket_count,
6479 and also for .dynsym reordering purposes. */
6480 s->hashcodes[s->nsyms] = ha;
6481 s->hashval[h->dynindx] = ha;
6482 ++s->nsyms;
6483 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
6484 s->min_dynindx = h->dynindx;
6486 free (alc);
6487 return true;
6490 /* This function will be called though elf_link_hash_traverse to do
6491 final dynamic symbol renumbering in case of .gnu.hash.
6492 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6493 to the translation table. */
6495 static bool
6496 elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data)
6498 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6499 unsigned long int bucket;
6500 unsigned long int val;
6502 /* Ignore indirect symbols. */
6503 if (h->dynindx == -1)
6504 return true;
6506 /* Ignore also local symbols and undefined symbols. */
6507 if (! (*s->bed->elf_hash_symbol) (h))
6509 if (h->dynindx >= s->min_dynindx)
6511 if (s->bed->record_xhash_symbol != NULL)
6513 (*s->bed->record_xhash_symbol) (h, 0);
6514 s->local_indx++;
6516 else
6517 h->dynindx = s->local_indx++;
6519 return true;
6522 bucket = s->hashval[h->dynindx] % s->bucketcount;
6523 val = (s->hashval[h->dynindx] >> s->shift1)
6524 & ((s->maskbits >> s->shift1) - 1);
6525 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
6526 s->bitmask[val]
6527 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
6528 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
6529 if (s->counts[bucket] == 1)
6530 /* Last element terminates the chain. */
6531 val |= 1;
6532 bfd_put_32 (s->output_bfd, val,
6533 s->contents + (s->indx[bucket] - s->symindx) * 4);
6534 --s->counts[bucket];
6535 if (s->bed->record_xhash_symbol != NULL)
6537 bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4;
6539 (*s->bed->record_xhash_symbol) (h, xlat_loc);
6541 else
6542 h->dynindx = s->indx[bucket]++;
6543 return true;
6546 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6548 bool
6549 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
6551 return !(h->forced_local
6552 || h->root.type == bfd_link_hash_undefined
6553 || h->root.type == bfd_link_hash_undefweak
6554 || ((h->root.type == bfd_link_hash_defined
6555 || h->root.type == bfd_link_hash_defweak)
6556 && h->root.u.def.section->output_section == NULL));
6559 /* Array used to determine the number of hash table buckets to use
6560 based on the number of symbols there are. If there are fewer than
6561 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6562 fewer than 37 we use 17 buckets, and so forth. We never use more
6563 than 32771 buckets. */
6565 static const size_t elf_buckets[] =
6567 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6568 16411, 32771, 0
6571 /* Compute bucket count for hashing table. We do not use a static set
6572 of possible tables sizes anymore. Instead we determine for all
6573 possible reasonable sizes of the table the outcome (i.e., the
6574 number of collisions etc) and choose the best solution. The
6575 weighting functions are not too simple to allow the table to grow
6576 without bounds. Instead one of the weighting factors is the size.
6577 Therefore the result is always a good payoff between few collisions
6578 (= short chain lengths) and table size. */
6579 static size_t
6580 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
6581 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
6582 unsigned long int nsyms,
6583 int gnu_hash)
6585 size_t best_size = 0;
6586 unsigned long int i;
6588 if (info->optimize)
6590 size_t minsize;
6591 size_t maxsize;
6592 uint64_t best_chlen = ~((uint64_t) 0);
6593 bfd *dynobj = elf_hash_table (info)->dynobj;
6594 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
6595 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
6596 unsigned long int *counts;
6597 bfd_size_type amt;
6598 unsigned int no_improvement_count = 0;
6600 /* Possible optimization parameters: if we have NSYMS symbols we say
6601 that the hashing table must at least have NSYMS/4 and at most
6602 2*NSYMS buckets. */
6603 minsize = nsyms / 4;
6604 if (minsize == 0)
6605 minsize = 1;
6606 best_size = maxsize = nsyms * 2;
6607 if (gnu_hash)
6609 if (minsize < 2)
6610 minsize = 2;
6611 if ((best_size & 31) == 0)
6612 ++best_size;
6615 /* Create array where we count the collisions in. We must use bfd_malloc
6616 since the size could be large. */
6617 amt = maxsize;
6618 amt *= sizeof (unsigned long int);
6619 counts = (unsigned long int *) bfd_malloc (amt);
6620 if (counts == NULL)
6621 return 0;
6623 /* Compute the "optimal" size for the hash table. The criteria is a
6624 minimal chain length. The minor criteria is (of course) the size
6625 of the table. */
6626 for (i = minsize; i < maxsize; ++i)
6628 /* Walk through the array of hashcodes and count the collisions. */
6629 uint64_t max;
6630 unsigned long int j;
6631 unsigned long int fact;
6633 if (gnu_hash && (i & 31) == 0)
6634 continue;
6636 memset (counts, '\0', i * sizeof (unsigned long int));
6638 /* Determine how often each hash bucket is used. */
6639 for (j = 0; j < nsyms; ++j)
6640 ++counts[hashcodes[j] % i];
6642 /* For the weight function we need some information about the
6643 pagesize on the target. This is information need not be 100%
6644 accurate. Since this information is not available (so far) we
6645 define it here to a reasonable default value. If it is crucial
6646 to have a better value some day simply define this value. */
6647 # ifndef BFD_TARGET_PAGESIZE
6648 # define BFD_TARGET_PAGESIZE (4096)
6649 # endif
6651 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6652 and the chains. */
6653 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
6655 # if 1
6656 /* Variant 1: optimize for short chains. We add the squares
6657 of all the chain lengths (which favors many small chain
6658 over a few long chains). */
6659 for (j = 0; j < i; ++j)
6660 max += counts[j] * counts[j];
6662 /* This adds penalties for the overall size of the table. */
6663 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6664 max *= fact * fact;
6665 # else
6666 /* Variant 2: Optimize a lot more for small table. Here we
6667 also add squares of the size but we also add penalties for
6668 empty slots (the +1 term). */
6669 for (j = 0; j < i; ++j)
6670 max += (1 + counts[j]) * (1 + counts[j]);
6672 /* The overall size of the table is considered, but not as
6673 strong as in variant 1, where it is squared. */
6674 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6675 max *= fact;
6676 # endif
6678 /* Compare with current best results. */
6679 if (max < best_chlen)
6681 best_chlen = max;
6682 best_size = i;
6683 no_improvement_count = 0;
6685 /* PR 11843: Avoid futile long searches for the best bucket size
6686 when there are a large number of symbols. */
6687 else if (++no_improvement_count == 100)
6688 break;
6691 free (counts);
6693 else
6695 for (i = 0; elf_buckets[i] != 0; i++)
6697 best_size = elf_buckets[i];
6698 if (nsyms < elf_buckets[i + 1])
6699 break;
6701 if (gnu_hash && best_size < 2)
6702 best_size = 2;
6705 return best_size;
6708 /* Size any SHT_GROUP section for ld -r. */
6710 bool
6711 _bfd_elf_size_group_sections (struct bfd_link_info *info)
6713 bfd *ibfd;
6714 asection *s;
6716 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6717 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6718 && (s = ibfd->sections) != NULL
6719 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
6720 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6721 return false;
6722 return true;
6725 /* Set a default stack segment size. The value in INFO wins. If it
6726 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6727 undefined it is initialized. */
6729 bool
6730 bfd_elf_stack_segment_size (bfd *output_bfd,
6731 struct bfd_link_info *info,
6732 const char *legacy_symbol,
6733 bfd_vma default_size)
6735 struct elf_link_hash_entry *h = NULL;
6737 /* Look for legacy symbol. */
6738 if (legacy_symbol)
6739 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6740 false, false, false);
6741 if (h && (h->root.type == bfd_link_hash_defined
6742 || h->root.type == bfd_link_hash_defweak)
6743 && h->def_regular
6744 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6746 /* The symbol has no type if specified on the command line. */
6747 h->type = STT_OBJECT;
6748 if (info->stacksize)
6749 /* xgettext:c-format */
6750 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6751 output_bfd, legacy_symbol);
6752 else if (h->root.u.def.section != bfd_abs_section_ptr)
6753 /* xgettext:c-format */
6754 _bfd_error_handler (_("%pB: %s not absolute"),
6755 output_bfd, legacy_symbol);
6756 else
6757 info->stacksize = h->root.u.def.value;
6760 if (!info->stacksize)
6761 /* If the user didn't set a size, or explicitly inhibit the
6762 size, set it now. */
6763 info->stacksize = default_size;
6765 /* Provide the legacy symbol, if it is referenced. */
6766 if (h && (h->root.type == bfd_link_hash_undefined
6767 || h->root.type == bfd_link_hash_undefweak))
6769 struct bfd_link_hash_entry *bh = NULL;
6771 if (!(_bfd_generic_link_add_one_symbol
6772 (info, output_bfd, legacy_symbol,
6773 BSF_GLOBAL, bfd_abs_section_ptr,
6774 info->stacksize >= 0 ? info->stacksize : 0,
6775 NULL, false, get_elf_backend_data (output_bfd)->collect, &bh)))
6776 return false;
6778 h = (struct elf_link_hash_entry *) bh;
6779 h->def_regular = 1;
6780 h->type = STT_OBJECT;
6783 return true;
6786 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6788 struct elf_gc_sweep_symbol_info
6790 struct bfd_link_info *info;
6791 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6792 bool);
6795 static bool
6796 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6798 if (!h->mark
6799 && (((h->root.type == bfd_link_hash_defined
6800 || h->root.type == bfd_link_hash_defweak)
6801 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6802 && h->root.u.def.section->gc_mark))
6803 || h->root.type == bfd_link_hash_undefined
6804 || h->root.type == bfd_link_hash_undefweak))
6806 struct elf_gc_sweep_symbol_info *inf;
6808 inf = (struct elf_gc_sweep_symbol_info *) data;
6809 (*inf->hide_symbol) (inf->info, h, true);
6810 h->def_regular = 0;
6811 h->ref_regular = 0;
6812 h->ref_regular_nonweak = 0;
6815 return true;
6818 /* Set up the sizes and contents of the ELF dynamic sections. This is
6819 called by the ELF linker emulation before_allocation routine. We
6820 must set the sizes of the sections before the linker sets the
6821 addresses of the various sections. */
6823 bool
6824 bfd_elf_size_dynamic_sections (bfd *output_bfd,
6825 const char *soname,
6826 const char *rpath,
6827 const char *filter_shlib,
6828 const char *audit,
6829 const char *depaudit,
6830 const char * const *auxiliary_filters,
6831 struct bfd_link_info *info,
6832 asection **sinterpptr)
6834 bfd *dynobj;
6835 const struct elf_backend_data *bed;
6837 *sinterpptr = NULL;
6839 if (!is_elf_hash_table (info->hash))
6840 return true;
6842 /* Any syms created from now on start with -1 in
6843 got.refcount/offset and plt.refcount/offset. */
6844 elf_hash_table (info)->init_got_refcount
6845 = elf_hash_table (info)->init_got_offset;
6846 elf_hash_table (info)->init_plt_refcount
6847 = elf_hash_table (info)->init_plt_offset;
6849 bed = get_elf_backend_data (output_bfd);
6851 /* The backend may have to create some sections regardless of whether
6852 we're dynamic or not. */
6853 if (bed->elf_backend_early_size_sections
6854 && !bed->elf_backend_early_size_sections (output_bfd, info))
6855 return false;
6857 dynobj = elf_hash_table (info)->dynobj;
6859 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6861 struct bfd_elf_version_tree *verdefs;
6862 struct elf_info_failed asvinfo;
6863 struct bfd_elf_version_tree *t;
6864 struct bfd_elf_version_expr *d;
6865 asection *s;
6866 size_t soname_indx;
6868 /* If we are supposed to export all symbols into the dynamic symbol
6869 table (this is not the normal case), then do so. */
6870 if (info->export_dynamic
6871 || (bfd_link_executable (info) && info->dynamic))
6873 struct elf_info_failed eif;
6875 eif.info = info;
6876 eif.failed = false;
6877 elf_link_hash_traverse (elf_hash_table (info),
6878 _bfd_elf_export_symbol,
6879 &eif);
6880 if (eif.failed)
6881 return false;
6884 if (soname != NULL)
6886 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6887 soname, true);
6888 if (soname_indx == (size_t) -1
6889 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6890 return false;
6892 else
6893 soname_indx = (size_t) -1;
6895 /* Make all global versions with definition. */
6896 for (t = info->version_info; t != NULL; t = t->next)
6897 for (d = t->globals.list; d != NULL; d = d->next)
6898 if (!d->symver && d->literal)
6900 const char *verstr, *name;
6901 size_t namelen, verlen, newlen;
6902 char *newname, *p, leading_char;
6903 struct elf_link_hash_entry *newh;
6905 leading_char = bfd_get_symbol_leading_char (output_bfd);
6906 name = d->pattern;
6907 namelen = strlen (name) + (leading_char != '\0');
6908 verstr = t->name;
6909 verlen = strlen (verstr);
6910 newlen = namelen + verlen + 3;
6912 newname = (char *) bfd_malloc (newlen);
6913 if (newname == NULL)
6914 return false;
6915 newname[0] = leading_char;
6916 memcpy (newname + (leading_char != '\0'), name, namelen);
6918 /* Check the hidden versioned definition. */
6919 p = newname + namelen;
6920 *p++ = ELF_VER_CHR;
6921 memcpy (p, verstr, verlen + 1);
6922 newh = elf_link_hash_lookup (elf_hash_table (info),
6923 newname, false, false,
6924 false);
6925 if (newh == NULL
6926 || (newh->root.type != bfd_link_hash_defined
6927 && newh->root.type != bfd_link_hash_defweak))
6929 /* Check the default versioned definition. */
6930 *p++ = ELF_VER_CHR;
6931 memcpy (p, verstr, verlen + 1);
6932 newh = elf_link_hash_lookup (elf_hash_table (info),
6933 newname, false, false,
6934 false);
6936 free (newname);
6938 /* Mark this version if there is a definition and it is
6939 not defined in a shared object. */
6940 if (newh != NULL
6941 && !newh->def_dynamic
6942 && (newh->root.type == bfd_link_hash_defined
6943 || newh->root.type == bfd_link_hash_defweak))
6944 d->symver = 1;
6947 /* Attach all the symbols to their version information. */
6948 asvinfo.info = info;
6949 asvinfo.failed = false;
6951 elf_link_hash_traverse (elf_hash_table (info),
6952 _bfd_elf_link_assign_sym_version,
6953 &asvinfo);
6954 if (asvinfo.failed)
6955 return false;
6957 if (!info->allow_undefined_version)
6959 /* Check if all global versions have a definition. */
6960 bool all_defined = true;
6961 for (t = info->version_info; t != NULL; t = t->next)
6962 for (d = t->globals.list; d != NULL; d = d->next)
6963 if (d->literal && !d->symver && !d->script)
6965 _bfd_error_handler
6966 (_("%s: undefined version: %s"),
6967 d->pattern, t->name);
6968 all_defined = false;
6971 if (!all_defined)
6973 bfd_set_error (bfd_error_bad_value);
6974 return false;
6978 /* Set up the version definition section. */
6979 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6980 BFD_ASSERT (s != NULL);
6982 /* We may have created additional version definitions if we are
6983 just linking a regular application. */
6984 verdefs = info->version_info;
6986 /* Skip anonymous version tag. */
6987 if (verdefs != NULL && verdefs->vernum == 0)
6988 verdefs = verdefs->next;
6990 if (verdefs == NULL && !info->create_default_symver)
6991 s->flags |= SEC_EXCLUDE;
6992 else
6994 unsigned int cdefs;
6995 bfd_size_type size;
6996 bfd_byte *p;
6997 Elf_Internal_Verdef def;
6998 Elf_Internal_Verdaux defaux;
6999 struct bfd_link_hash_entry *bh;
7000 struct elf_link_hash_entry *h;
7001 const char *name;
7003 cdefs = 0;
7004 size = 0;
7006 /* Make space for the base version. */
7007 size += sizeof (Elf_External_Verdef);
7008 size += sizeof (Elf_External_Verdaux);
7009 ++cdefs;
7011 /* Make space for the default version. */
7012 if (info->create_default_symver)
7014 size += sizeof (Elf_External_Verdef);
7015 ++cdefs;
7018 for (t = verdefs; t != NULL; t = t->next)
7020 struct bfd_elf_version_deps *n;
7022 /* Don't emit base version twice. */
7023 if (t->vernum == 0)
7024 continue;
7026 size += sizeof (Elf_External_Verdef);
7027 size += sizeof (Elf_External_Verdaux);
7028 ++cdefs;
7030 for (n = t->deps; n != NULL; n = n->next)
7031 size += sizeof (Elf_External_Verdaux);
7034 s->size = size;
7035 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7036 if (s->contents == NULL && s->size != 0)
7037 return false;
7038 s->alloced = 1;
7040 /* Fill in the version definition section. */
7042 p = s->contents;
7044 def.vd_version = VER_DEF_CURRENT;
7045 def.vd_flags = VER_FLG_BASE;
7046 def.vd_ndx = 1;
7047 def.vd_cnt = 1;
7048 if (info->create_default_symver)
7050 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
7051 def.vd_next = sizeof (Elf_External_Verdef);
7053 else
7055 def.vd_aux = sizeof (Elf_External_Verdef);
7056 def.vd_next = (sizeof (Elf_External_Verdef)
7057 + sizeof (Elf_External_Verdaux));
7060 if (soname_indx != (size_t) -1)
7062 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
7063 soname_indx);
7064 def.vd_hash = bfd_elf_hash (soname);
7065 defaux.vda_name = soname_indx;
7066 name = soname;
7068 else
7070 size_t indx;
7072 name = lbasename (bfd_get_filename (output_bfd));
7073 def.vd_hash = bfd_elf_hash (name);
7074 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7075 name, false);
7076 if (indx == (size_t) -1)
7077 return false;
7078 defaux.vda_name = indx;
7080 defaux.vda_next = 0;
7082 _bfd_elf_swap_verdef_out (output_bfd, &def,
7083 (Elf_External_Verdef *) p);
7084 p += sizeof (Elf_External_Verdef);
7085 if (info->create_default_symver)
7087 /* Add a symbol representing this version. */
7088 bh = NULL;
7089 if (! (_bfd_generic_link_add_one_symbol
7090 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
7091 0, NULL, false,
7092 get_elf_backend_data (dynobj)->collect, &bh)))
7093 return false;
7094 h = (struct elf_link_hash_entry *) bh;
7095 h->non_elf = 0;
7096 h->def_regular = 1;
7097 h->type = STT_OBJECT;
7098 h->verinfo.vertree = NULL;
7100 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7101 return false;
7103 /* Create a duplicate of the base version with the same
7104 aux block, but different flags. */
7105 def.vd_flags = 0;
7106 def.vd_ndx = 2;
7107 def.vd_aux = sizeof (Elf_External_Verdef);
7108 if (verdefs)
7109 def.vd_next = (sizeof (Elf_External_Verdef)
7110 + sizeof (Elf_External_Verdaux));
7111 else
7112 def.vd_next = 0;
7113 _bfd_elf_swap_verdef_out (output_bfd, &def,
7114 (Elf_External_Verdef *) p);
7115 p += sizeof (Elf_External_Verdef);
7117 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
7118 (Elf_External_Verdaux *) p);
7119 p += sizeof (Elf_External_Verdaux);
7121 for (t = verdefs; t != NULL; t = t->next)
7123 unsigned int cdeps;
7124 struct bfd_elf_version_deps *n;
7126 /* Don't emit the base version twice. */
7127 if (t->vernum == 0)
7128 continue;
7130 cdeps = 0;
7131 for (n = t->deps; n != NULL; n = n->next)
7132 ++cdeps;
7134 /* Add a symbol representing this version. */
7135 bh = NULL;
7136 if (! (_bfd_generic_link_add_one_symbol
7137 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
7138 0, NULL, false,
7139 get_elf_backend_data (dynobj)->collect, &bh)))
7140 return false;
7141 h = (struct elf_link_hash_entry *) bh;
7142 h->non_elf = 0;
7143 h->def_regular = 1;
7144 h->type = STT_OBJECT;
7145 h->verinfo.vertree = t;
7147 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7148 return false;
7150 def.vd_version = VER_DEF_CURRENT;
7151 def.vd_flags = 0;
7152 if (t->globals.list == NULL
7153 && t->locals.list == NULL
7154 && ! t->used)
7155 def.vd_flags |= VER_FLG_WEAK;
7156 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
7157 def.vd_cnt = cdeps + 1;
7158 def.vd_hash = bfd_elf_hash (t->name);
7159 def.vd_aux = sizeof (Elf_External_Verdef);
7160 def.vd_next = 0;
7162 /* If a basever node is next, it *must* be the last node in
7163 the chain, otherwise Verdef construction breaks. */
7164 if (t->next != NULL && t->next->vernum == 0)
7165 BFD_ASSERT (t->next->next == NULL);
7167 if (t->next != NULL && t->next->vernum != 0)
7168 def.vd_next = (sizeof (Elf_External_Verdef)
7169 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
7171 _bfd_elf_swap_verdef_out (output_bfd, &def,
7172 (Elf_External_Verdef *) p);
7173 p += sizeof (Elf_External_Verdef);
7175 defaux.vda_name = h->dynstr_index;
7176 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
7177 h->dynstr_index);
7178 defaux.vda_next = 0;
7179 if (t->deps != NULL)
7180 defaux.vda_next = sizeof (Elf_External_Verdaux);
7181 t->name_indx = defaux.vda_name;
7183 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
7184 (Elf_External_Verdaux *) p);
7185 p += sizeof (Elf_External_Verdaux);
7187 for (n = t->deps; n != NULL; n = n->next)
7189 if (n->version_needed == NULL)
7191 /* This can happen if there was an error in the
7192 version script. */
7193 defaux.vda_name = 0;
7195 else
7197 defaux.vda_name = n->version_needed->name_indx;
7198 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
7199 defaux.vda_name);
7201 if (n->next == NULL)
7202 defaux.vda_next = 0;
7203 else
7204 defaux.vda_next = sizeof (Elf_External_Verdaux);
7206 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
7207 (Elf_External_Verdaux *) p);
7208 p += sizeof (Elf_External_Verdaux);
7212 elf_tdata (output_bfd)->cverdefs = cdefs;
7216 if (info->gc_sections && bed->can_gc_sections)
7218 struct elf_gc_sweep_symbol_info sweep_info;
7220 /* Remove the symbols that were in the swept sections from the
7221 dynamic symbol table. */
7222 sweep_info.info = info;
7223 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
7224 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
7225 &sweep_info);
7228 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7230 asection *s;
7231 struct elf_find_verdep_info sinfo;
7233 /* Work out the size of the version reference section. */
7235 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
7236 BFD_ASSERT (s != NULL);
7238 sinfo.info = info;
7239 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
7240 if (sinfo.vers == 0)
7241 sinfo.vers = 1;
7242 sinfo.failed = false;
7244 elf_link_hash_traverse (elf_hash_table (info),
7245 _bfd_elf_link_find_version_dependencies,
7246 &sinfo);
7247 if (sinfo.failed)
7248 return false;
7250 bed->elf_backend_add_glibc_version_dependency (&sinfo);
7251 if (sinfo.failed)
7252 return false;
7254 if (elf_tdata (output_bfd)->verref == NULL)
7255 s->flags |= SEC_EXCLUDE;
7256 else
7258 Elf_Internal_Verneed *vn;
7259 unsigned int size;
7260 unsigned int crefs;
7261 bfd_byte *p;
7263 /* Build the version dependency section. */
7264 size = 0;
7265 crefs = 0;
7266 for (vn = elf_tdata (output_bfd)->verref;
7267 vn != NULL;
7268 vn = vn->vn_nextref)
7270 Elf_Internal_Vernaux *a;
7272 size += sizeof (Elf_External_Verneed);
7273 ++crefs;
7274 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7275 size += sizeof (Elf_External_Vernaux);
7278 s->size = size;
7279 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7280 if (s->contents == NULL)
7281 return false;
7282 s->alloced = 1;
7284 p = s->contents;
7285 for (vn = elf_tdata (output_bfd)->verref;
7286 vn != NULL;
7287 vn = vn->vn_nextref)
7289 unsigned int caux;
7290 Elf_Internal_Vernaux *a;
7291 size_t indx;
7293 caux = 0;
7294 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7295 ++caux;
7297 vn->vn_version = VER_NEED_CURRENT;
7298 vn->vn_cnt = caux;
7299 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7300 elf_dt_name (vn->vn_bfd) != NULL
7301 ? elf_dt_name (vn->vn_bfd)
7302 : lbasename (bfd_get_filename
7303 (vn->vn_bfd)),
7304 false);
7305 if (indx == (size_t) -1)
7306 return false;
7307 vn->vn_file = indx;
7308 vn->vn_aux = sizeof (Elf_External_Verneed);
7309 if (vn->vn_nextref == NULL)
7310 vn->vn_next = 0;
7311 else
7312 vn->vn_next = (sizeof (Elf_External_Verneed)
7313 + caux * sizeof (Elf_External_Vernaux));
7315 _bfd_elf_swap_verneed_out (output_bfd, vn,
7316 (Elf_External_Verneed *) p);
7317 p += sizeof (Elf_External_Verneed);
7319 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7321 a->vna_hash = bfd_elf_hash (a->vna_nodename);
7322 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7323 a->vna_nodename, false);
7324 if (indx == (size_t) -1)
7325 return false;
7326 a->vna_name = indx;
7327 if (a->vna_nextptr == NULL)
7328 a->vna_next = 0;
7329 else
7330 a->vna_next = sizeof (Elf_External_Vernaux);
7332 _bfd_elf_swap_vernaux_out (output_bfd, a,
7333 (Elf_External_Vernaux *) p);
7334 p += sizeof (Elf_External_Vernaux);
7338 elf_tdata (output_bfd)->cverrefs = crefs;
7342 if (bfd_link_relocatable (info)
7343 && !_bfd_elf_size_group_sections (info))
7344 return false;
7346 /* Determine any GNU_STACK segment requirements, after the backend
7347 has had a chance to set a default segment size. */
7348 if (info->execstack)
7350 /* If the user has explicitly requested warnings, then generate one even
7351 though the choice is the result of another command line option. */
7352 if (info->warn_execstack == 1)
7354 if (info->error_execstack)
7356 _bfd_error_handler
7357 (_("\
7358 error: creating an executable stack because of -z execstack command line option"));
7359 return false;
7362 _bfd_error_handler
7363 (_("\
7364 warning: enabling an executable stack because of -z execstack command line option"));
7367 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
7369 else if (info->noexecstack)
7370 elf_stack_flags (output_bfd) = PF_R | PF_W;
7371 else
7373 bfd *inputobj;
7374 asection *notesec = NULL;
7375 bfd *noteobj = NULL;
7376 bfd *emptyobj = NULL;
7377 int exec = 0;
7379 for (inputobj = info->input_bfds;
7380 inputobj;
7381 inputobj = inputobj->link.next)
7383 asection *s;
7385 if (inputobj->flags
7386 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
7387 continue;
7388 s = inputobj->sections;
7389 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7390 continue;
7392 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
7393 if (s)
7395 notesec = s;
7396 if (s->flags & SEC_CODE)
7398 noteobj = inputobj;
7399 exec = PF_X;
7400 /* There is no point in scanning the remaining bfds. */
7401 break;
7404 else if (bed->default_execstack && info->default_execstack)
7406 exec = PF_X;
7407 emptyobj = inputobj;
7411 if (notesec || info->stacksize > 0)
7413 if (exec)
7415 if (info->warn_execstack != 0)
7417 /* PR 29072: Because an executable stack is a serious
7418 security risk, make sure that the user knows that it is
7419 being enabled despite the fact that it was not requested
7420 on the command line. */
7421 if (noteobj)
7423 if (info->error_execstack)
7425 _bfd_error_handler (_("\
7426 error: %s: is triggering the generation of an executable stack (because it has an executable .note.GNU-stack section)"),
7427 bfd_get_filename (noteobj));
7428 return false;
7431 _bfd_error_handler (_("\
7432 warning: %s: requires executable stack (because the .note.GNU-stack section is executable)"),
7433 bfd_get_filename (noteobj));
7435 else if (emptyobj)
7437 if (info->error_execstack)
7439 _bfd_error_handler (_("\
7440 error: %s: is triggering the generation of an executable stack because it does not have a .note.GNU-stack section"),
7441 bfd_get_filename (emptyobj));
7442 return false;
7445 _bfd_error_handler (_("\
7446 warning: %s: missing .note.GNU-stack section implies executable stack"),
7447 bfd_get_filename (emptyobj));
7448 _bfd_error_handler (_("\
7449 NOTE: This behaviour is deprecated and will be removed in a future version of the linker"));
7453 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
7456 if (notesec && exec && bfd_link_relocatable (info)
7457 && notesec->output_section != bfd_abs_section_ptr)
7458 notesec->output_section->flags |= SEC_CODE;
7461 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7463 struct elf_info_failed eif;
7464 struct elf_link_hash_entry *h;
7465 asection *dynstr;
7466 asection *s;
7468 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
7469 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
7471 if (info->symbolic)
7473 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
7474 return false;
7475 info->flags |= DF_SYMBOLIC;
7478 if (rpath != NULL)
7480 size_t indx;
7481 bfd_vma tag;
7483 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
7484 true);
7485 if (indx == (size_t) -1)
7486 return false;
7488 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
7489 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
7490 return false;
7493 if (filter_shlib != NULL)
7495 size_t indx;
7497 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7498 filter_shlib, true);
7499 if (indx == (size_t) -1
7500 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
7501 return false;
7504 if (auxiliary_filters != NULL)
7506 const char * const *p;
7508 for (p = auxiliary_filters; *p != NULL; p++)
7510 size_t indx;
7512 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7513 *p, true);
7514 if (indx == (size_t) -1
7515 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
7516 return false;
7520 if (audit != NULL)
7522 size_t indx;
7524 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
7525 true);
7526 if (indx == (size_t) -1
7527 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
7528 return false;
7531 if (depaudit != NULL)
7533 size_t indx;
7535 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
7536 true);
7537 if (indx == (size_t) -1
7538 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
7539 return false;
7542 eif.info = info;
7543 eif.failed = false;
7545 /* Find all symbols which were defined in a dynamic object and make
7546 the backend pick a reasonable value for them. */
7547 elf_link_hash_traverse (elf_hash_table (info),
7548 _bfd_elf_adjust_dynamic_symbol,
7549 &eif);
7550 if (eif.failed)
7551 return false;
7553 /* Add some entries to the .dynamic section. We fill in some of the
7554 values later, in bfd_elf_final_link, but we must add the entries
7555 now so that we know the final size of the .dynamic section. */
7557 /* If there are initialization and/or finalization functions to
7558 call then add the corresponding DT_INIT/DT_FINI entries. */
7559 h = (info->init_function
7560 ? elf_link_hash_lookup (elf_hash_table (info),
7561 info->init_function, false,
7562 false, false)
7563 : NULL);
7564 if (h != NULL
7565 && (h->ref_regular
7566 || h->def_regular))
7568 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
7569 return false;
7571 h = (info->fini_function
7572 ? elf_link_hash_lookup (elf_hash_table (info),
7573 info->fini_function, false,
7574 false, false)
7575 : NULL);
7576 if (h != NULL
7577 && (h->ref_regular
7578 || h->def_regular))
7580 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
7581 return false;
7584 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
7585 if (s != NULL && s->linker_has_input)
7587 /* DT_PREINIT_ARRAY is not allowed in shared library. */
7588 if (! bfd_link_executable (info))
7590 bfd *sub;
7591 asection *o;
7593 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
7594 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
7595 && (o = sub->sections) != NULL
7596 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
7597 for (o = sub->sections; o != NULL; o = o->next)
7598 if (elf_section_data (o)->this_hdr.sh_type
7599 == SHT_PREINIT_ARRAY)
7601 _bfd_error_handler
7602 (_("%pB: .preinit_array section is not allowed in DSO"),
7603 sub);
7604 break;
7607 bfd_set_error (bfd_error_nonrepresentable_section);
7608 return false;
7611 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
7612 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
7613 return false;
7615 s = bfd_get_section_by_name (output_bfd, ".init_array");
7616 if (s != NULL && s->linker_has_input)
7618 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
7619 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
7620 return false;
7622 s = bfd_get_section_by_name (output_bfd, ".fini_array");
7623 if (s != NULL && s->linker_has_input)
7625 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
7626 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
7627 return false;
7630 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
7631 /* If .dynstr is excluded from the link, we don't want any of
7632 these tags. Strictly, we should be checking each section
7633 individually; This quick check covers for the case where
7634 someone does a /DISCARD/ : { *(*) }. */
7635 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
7637 bfd_size_type strsize;
7639 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7640 if ((info->emit_hash
7641 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
7642 || (info->emit_gnu_hash
7643 && (bed->record_xhash_symbol == NULL
7644 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)))
7645 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
7646 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
7647 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
7648 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
7649 bed->s->sizeof_sym)
7650 || (info->gnu_flags_1
7651 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_FLAGS_1,
7652 info->gnu_flags_1)))
7653 return false;
7657 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
7658 return false;
7660 /* The backend must work out the sizes of all the other dynamic
7661 sections. */
7662 if (bed->elf_backend_late_size_sections != NULL
7663 && !bed->elf_backend_late_size_sections (output_bfd, info))
7664 return false;
7666 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7668 if (elf_tdata (output_bfd)->cverdefs)
7670 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
7672 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
7673 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
7674 return false;
7677 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
7679 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
7680 return false;
7682 else if (info->flags & DF_BIND_NOW)
7684 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
7685 return false;
7688 if (info->flags_1)
7690 if (bfd_link_executable (info))
7691 info->flags_1 &= ~ (DF_1_INITFIRST
7692 | DF_1_NODELETE
7693 | DF_1_NOOPEN);
7694 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
7695 return false;
7698 if (elf_tdata (output_bfd)->cverrefs)
7700 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
7702 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
7703 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
7704 return false;
7707 if ((elf_tdata (output_bfd)->cverrefs == 0
7708 && elf_tdata (output_bfd)->cverdefs == 0)
7709 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
7711 asection *s;
7713 s = bfd_get_linker_section (dynobj, ".gnu.version");
7714 s->flags |= SEC_EXCLUDE;
7717 return true;
7720 /* Find the first non-excluded output section. We'll use its
7721 section symbol for some emitted relocs. */
7722 void
7723 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7725 asection *s;
7726 asection *found = NULL;
7728 for (s = output_bfd->sections; s != NULL; s = s->next)
7729 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7730 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7732 found = s;
7733 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7734 break;
7736 elf_hash_table (info)->text_index_section = found;
7739 /* Find two non-excluded output sections, one for code, one for data.
7740 We'll use their section symbols for some emitted relocs. */
7741 void
7742 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7744 asection *s;
7745 asection *found = NULL;
7747 /* Data first, since setting text_index_section changes
7748 _bfd_elf_omit_section_dynsym_default. */
7749 for (s = output_bfd->sections; s != NULL; s = s->next)
7750 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7751 && !(s->flags & SEC_READONLY)
7752 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7754 found = s;
7755 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7756 break;
7758 elf_hash_table (info)->data_index_section = found;
7760 for (s = output_bfd->sections; s != NULL; s = s->next)
7761 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7762 && (s->flags & SEC_READONLY)
7763 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7765 found = s;
7766 break;
7768 elf_hash_table (info)->text_index_section = found;
7771 #define GNU_HASH_SECTION_NAME(bed) \
7772 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7774 bool
7775 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7777 const struct elf_backend_data *bed;
7778 unsigned long section_sym_count;
7779 bfd_size_type dynsymcount = 0;
7781 if (!is_elf_hash_table (info->hash))
7782 return true;
7784 bed = get_elf_backend_data (output_bfd);
7785 (*bed->elf_backend_init_index_section) (output_bfd, info);
7787 /* Assign dynsym indices. In a shared library we generate a section
7788 symbol for each output section, which come first. Next come all
7789 of the back-end allocated local dynamic syms, followed by the rest
7790 of the global symbols.
7792 This is usually not needed for static binaries, however backends
7793 can request to always do it, e.g. the MIPS backend uses dynamic
7794 symbol counts to lay out GOT, which will be produced in the
7795 presence of GOT relocations even in static binaries (holding fixed
7796 data in that case, to satisfy those relocations). */
7798 if (elf_hash_table (info)->dynamic_sections_created
7799 || bed->always_renumber_dynsyms)
7800 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7801 &section_sym_count);
7803 if (elf_hash_table (info)->dynamic_sections_created)
7805 bfd *dynobj;
7806 asection *s;
7807 unsigned int dtagcount;
7809 dynobj = elf_hash_table (info)->dynobj;
7811 /* Work out the size of the symbol version section. */
7812 s = bfd_get_linker_section (dynobj, ".gnu.version");
7813 BFD_ASSERT (s != NULL);
7814 if ((s->flags & SEC_EXCLUDE) == 0)
7816 s->size = dynsymcount * sizeof (Elf_External_Versym);
7817 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7818 if (s->contents == NULL)
7819 return false;
7820 s->alloced = 1;
7822 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7823 return false;
7826 /* Set the size of the .dynsym and .hash sections. We counted
7827 the number of dynamic symbols in elf_link_add_object_symbols.
7828 We will build the contents of .dynsym and .hash when we build
7829 the final symbol table, because until then we do not know the
7830 correct value to give the symbols. We built the .dynstr
7831 section as we went along in elf_link_add_object_symbols. */
7832 s = elf_hash_table (info)->dynsym;
7833 BFD_ASSERT (s != NULL);
7834 s->size = dynsymcount * bed->s->sizeof_sym;
7836 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7837 if (s->contents == NULL)
7838 return false;
7839 s->alloced = 1;
7841 /* The first entry in .dynsym is a dummy symbol. Clear all the
7842 section syms, in case we don't output them all. */
7843 ++section_sym_count;
7844 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
7846 elf_hash_table (info)->bucketcount = 0;
7848 /* Compute the size of the hashing table. As a side effect this
7849 computes the hash values for all the names we export. */
7850 if (info->emit_hash)
7852 unsigned long int *hashcodes;
7853 struct hash_codes_info hashinf;
7854 bfd_size_type amt;
7855 unsigned long int nsyms;
7856 size_t bucketcount;
7857 size_t hash_entry_size;
7859 /* Compute the hash values for all exported symbols. At the same
7860 time store the values in an array so that we could use them for
7861 optimizations. */
7862 amt = dynsymcount * sizeof (unsigned long int);
7863 hashcodes = (unsigned long int *) bfd_malloc (amt);
7864 if (hashcodes == NULL)
7865 return false;
7866 hashinf.hashcodes = hashcodes;
7867 hashinf.error = false;
7869 /* Put all hash values in HASHCODES. */
7870 elf_link_hash_traverse (elf_hash_table (info),
7871 elf_collect_hash_codes, &hashinf);
7872 if (hashinf.error)
7874 free (hashcodes);
7875 return false;
7878 nsyms = hashinf.hashcodes - hashcodes;
7879 bucketcount
7880 = compute_bucket_count (info, hashcodes, nsyms, 0);
7881 free (hashcodes);
7883 if (bucketcount == 0 && nsyms > 0)
7884 return false;
7886 elf_hash_table (info)->bucketcount = bucketcount;
7888 s = bfd_get_linker_section (dynobj, ".hash");
7889 BFD_ASSERT (s != NULL);
7890 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7891 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
7892 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7893 if (s->contents == NULL)
7894 return false;
7895 s->alloced = 1;
7897 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7898 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7899 s->contents + hash_entry_size);
7902 if (info->emit_gnu_hash)
7904 size_t i, cnt;
7905 unsigned char *contents;
7906 struct collect_gnu_hash_codes cinfo;
7907 bfd_size_type amt;
7908 size_t bucketcount;
7910 memset (&cinfo, 0, sizeof (cinfo));
7912 /* Compute the hash values for all exported symbols. At the same
7913 time store the values in an array so that we could use them for
7914 optimizations. */
7915 amt = dynsymcount * 2 * sizeof (unsigned long int);
7916 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
7917 if (cinfo.hashcodes == NULL)
7918 return false;
7920 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7921 cinfo.min_dynindx = -1;
7922 cinfo.output_bfd = output_bfd;
7923 cinfo.bed = bed;
7925 /* Put all hash values in HASHCODES. */
7926 elf_link_hash_traverse (elf_hash_table (info),
7927 elf_collect_gnu_hash_codes, &cinfo);
7928 if (cinfo.error)
7930 free (cinfo.hashcodes);
7931 return false;
7934 bucketcount
7935 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7937 if (bucketcount == 0)
7939 free (cinfo.hashcodes);
7940 return false;
7943 s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (bed));
7944 BFD_ASSERT (s != NULL);
7946 if (cinfo.nsyms == 0)
7948 /* Empty .gnu.hash or .MIPS.xhash section is special. */
7949 BFD_ASSERT (cinfo.min_dynindx == -1);
7950 free (cinfo.hashcodes);
7951 s->size = 5 * 4 + bed->s->arch_size / 8;
7952 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7953 if (contents == NULL)
7954 return false;
7955 s->contents = contents;
7956 s->alloced = 1;
7957 /* 1 empty bucket. */
7958 bfd_put_32 (output_bfd, 1, contents);
7959 /* SYMIDX above the special symbol 0. */
7960 bfd_put_32 (output_bfd, 1, contents + 4);
7961 /* Just one word for bitmask. */
7962 bfd_put_32 (output_bfd, 1, contents + 8);
7963 /* Only hash fn bloom filter. */
7964 bfd_put_32 (output_bfd, 0, contents + 12);
7965 /* No hashes are valid - empty bitmask. */
7966 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7967 /* No hashes in the only bucket. */
7968 bfd_put_32 (output_bfd, 0,
7969 contents + 16 + bed->s->arch_size / 8);
7971 else
7973 unsigned long int maskwords, maskbitslog2, x;
7974 BFD_ASSERT (cinfo.min_dynindx != -1);
7976 x = cinfo.nsyms;
7977 maskbitslog2 = 1;
7978 while ((x >>= 1) != 0)
7979 ++maskbitslog2;
7980 if (maskbitslog2 < 3)
7981 maskbitslog2 = 5;
7982 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7983 maskbitslog2 = maskbitslog2 + 3;
7984 else
7985 maskbitslog2 = maskbitslog2 + 2;
7986 if (bed->s->arch_size == 64)
7988 if (maskbitslog2 == 5)
7989 maskbitslog2 = 6;
7990 cinfo.shift1 = 6;
7992 else
7993 cinfo.shift1 = 5;
7994 cinfo.mask = (1 << cinfo.shift1) - 1;
7995 cinfo.shift2 = maskbitslog2;
7996 cinfo.maskbits = 1 << maskbitslog2;
7997 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7998 amt = bucketcount * sizeof (unsigned long int) * 2;
7999 amt += maskwords * sizeof (bfd_vma);
8000 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
8001 if (cinfo.bitmask == NULL)
8003 free (cinfo.hashcodes);
8004 return false;
8007 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
8008 cinfo.indx = cinfo.counts + bucketcount;
8009 cinfo.symindx = dynsymcount - cinfo.nsyms;
8010 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
8012 /* Determine how often each hash bucket is used. */
8013 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
8014 for (i = 0; i < cinfo.nsyms; ++i)
8015 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
8017 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
8018 if (cinfo.counts[i] != 0)
8020 cinfo.indx[i] = cnt;
8021 cnt += cinfo.counts[i];
8023 BFD_ASSERT (cnt == dynsymcount);
8024 cinfo.bucketcount = bucketcount;
8025 cinfo.local_indx = cinfo.min_dynindx;
8027 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
8028 s->size += cinfo.maskbits / 8;
8029 if (bed->record_xhash_symbol != NULL)
8030 s->size += cinfo.nsyms * 4;
8031 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
8032 if (contents == NULL)
8034 free (cinfo.bitmask);
8035 free (cinfo.hashcodes);
8036 return false;
8039 s->contents = contents;
8040 s->alloced = 1;
8041 bfd_put_32 (output_bfd, bucketcount, contents);
8042 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
8043 bfd_put_32 (output_bfd, maskwords, contents + 8);
8044 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
8045 contents += 16 + cinfo.maskbits / 8;
8047 for (i = 0; i < bucketcount; ++i)
8049 if (cinfo.counts[i] == 0)
8050 bfd_put_32 (output_bfd, 0, contents);
8051 else
8052 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
8053 contents += 4;
8056 cinfo.contents = contents;
8058 cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents;
8059 /* Renumber dynamic symbols, if populating .gnu.hash section.
8060 If using .MIPS.xhash, populate the translation table. */
8061 elf_link_hash_traverse (elf_hash_table (info),
8062 elf_gnu_hash_process_symidx, &cinfo);
8064 contents = s->contents + 16;
8065 for (i = 0; i < maskwords; ++i)
8067 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
8068 contents);
8069 contents += bed->s->arch_size / 8;
8072 free (cinfo.bitmask);
8073 free (cinfo.hashcodes);
8077 s = bfd_get_linker_section (dynobj, ".dynstr");
8078 BFD_ASSERT (s != NULL);
8080 elf_finalize_dynstr (output_bfd, info);
8082 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
8084 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
8085 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
8086 return false;
8089 return true;
8092 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
8094 static void
8095 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
8096 asection *sec)
8098 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
8099 sec->sec_info_type = SEC_INFO_TYPE_NONE;
8102 /* Finish SHF_MERGE section merging. */
8104 bool
8105 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
8107 bfd *ibfd;
8108 asection *sec;
8110 if (ENABLE_CHECKING && !is_elf_hash_table (info->hash))
8111 abort ();
8113 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8114 if ((ibfd->flags & DYNAMIC) == 0
8115 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
8116 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
8117 == get_elf_backend_data (obfd)->s->elfclass))
8118 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8119 if ((sec->flags & SEC_MERGE) != 0
8120 && !bfd_is_abs_section (sec->output_section))
8122 struct bfd_elf_section_data *secdata;
8124 secdata = elf_section_data (sec);
8125 if (! _bfd_add_merge_section (obfd,
8126 &elf_hash_table (info)->merge_info,
8127 sec, &secdata->sec_info))
8128 return false;
8129 else if (secdata->sec_info)
8130 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
8133 if (elf_hash_table (info)->merge_info != NULL)
8134 return _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
8135 merge_sections_remove_hook);
8136 return true;
8139 /* Create an entry in an ELF linker hash table. */
8141 struct bfd_hash_entry *
8142 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
8143 struct bfd_hash_table *table,
8144 const char *string)
8146 /* Allocate the structure if it has not already been allocated by a
8147 subclass. */
8148 if (entry == NULL)
8150 entry = (struct bfd_hash_entry *)
8151 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
8152 if (entry == NULL)
8153 return entry;
8156 /* Call the allocation method of the superclass. */
8157 entry = _bfd_link_hash_newfunc (entry, table, string);
8158 if (entry != NULL)
8160 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
8161 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
8163 /* Set local fields. */
8164 ret->indx = -1;
8165 ret->dynindx = -1;
8166 ret->got = htab->init_got_refcount;
8167 ret->plt = htab->init_plt_refcount;
8168 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
8169 - offsetof (struct elf_link_hash_entry, size)));
8170 /* Assume that we have been called by a non-ELF symbol reader.
8171 This flag is then reset by the code which reads an ELF input
8172 file. This ensures that a symbol created by a non-ELF symbol
8173 reader will have the flag set correctly. */
8174 ret->non_elf = 1;
8177 return entry;
8180 /* Copy data from an indirect symbol to its direct symbol, hiding the
8181 old indirect symbol. Also used for copying flags to a weakdef. */
8183 void
8184 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
8185 struct elf_link_hash_entry *dir,
8186 struct elf_link_hash_entry *ind)
8188 struct elf_link_hash_table *htab;
8190 if (ind->dyn_relocs != NULL)
8192 if (dir->dyn_relocs != NULL)
8194 struct elf_dyn_relocs **pp;
8195 struct elf_dyn_relocs *p;
8197 /* Add reloc counts against the indirect sym to the direct sym
8198 list. Merge any entries against the same section. */
8199 for (pp = &ind->dyn_relocs; (p = *pp) != NULL; )
8201 struct elf_dyn_relocs *q;
8203 for (q = dir->dyn_relocs; q != NULL; q = q->next)
8204 if (q->sec == p->sec)
8206 q->pc_count += p->pc_count;
8207 q->count += p->count;
8208 *pp = p->next;
8209 break;
8211 if (q == NULL)
8212 pp = &p->next;
8214 *pp = dir->dyn_relocs;
8217 dir->dyn_relocs = ind->dyn_relocs;
8218 ind->dyn_relocs = NULL;
8221 /* Copy down any references that we may have already seen to the
8222 symbol which just became indirect. */
8224 if (dir->versioned != versioned_hidden)
8225 dir->ref_dynamic |= ind->ref_dynamic;
8226 dir->ref_regular |= ind->ref_regular;
8227 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
8228 dir->non_got_ref |= ind->non_got_ref;
8229 dir->needs_plt |= ind->needs_plt;
8230 dir->pointer_equality_needed |= ind->pointer_equality_needed;
8232 if (ind->root.type != bfd_link_hash_indirect)
8233 return;
8235 /* Copy over the global and procedure linkage table refcount entries.
8236 These may have been already set up by a check_relocs routine. */
8237 htab = elf_hash_table (info);
8238 if (ind->got.refcount > htab->init_got_refcount.refcount)
8240 if (dir->got.refcount < 0)
8241 dir->got.refcount = 0;
8242 dir->got.refcount += ind->got.refcount;
8243 ind->got.refcount = htab->init_got_refcount.refcount;
8246 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
8248 if (dir->plt.refcount < 0)
8249 dir->plt.refcount = 0;
8250 dir->plt.refcount += ind->plt.refcount;
8251 ind->plt.refcount = htab->init_plt_refcount.refcount;
8254 if (ind->dynindx != -1)
8256 if (dir->dynindx != -1)
8257 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
8258 dir->dynindx = ind->dynindx;
8259 dir->dynstr_index = ind->dynstr_index;
8260 ind->dynindx = -1;
8261 ind->dynstr_index = 0;
8265 void
8266 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
8267 struct elf_link_hash_entry *h,
8268 bool force_local)
8270 /* STT_GNU_IFUNC symbol must go through PLT. */
8271 if (h->type != STT_GNU_IFUNC)
8273 h->plt = elf_hash_table (info)->init_plt_offset;
8274 h->needs_plt = 0;
8276 if (force_local)
8278 h->forced_local = 1;
8279 if (h->dynindx != -1)
8281 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8282 h->dynstr_index);
8283 h->dynindx = -1;
8284 h->dynstr_index = 0;
8289 /* Hide a symbol. */
8291 void
8292 _bfd_elf_link_hide_symbol (bfd *output_bfd,
8293 struct bfd_link_info *info,
8294 struct bfd_link_hash_entry *h)
8296 if (is_elf_hash_table (info->hash))
8298 const struct elf_backend_data *bed
8299 = get_elf_backend_data (output_bfd);
8300 struct elf_link_hash_entry *eh
8301 = (struct elf_link_hash_entry *) h;
8302 bed->elf_backend_hide_symbol (info, eh, true);
8303 eh->def_dynamic = 0;
8304 eh->ref_dynamic = 0;
8305 eh->dynamic_def = 0;
8309 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
8310 caller. */
8312 bool
8313 _bfd_elf_link_hash_table_init
8314 (struct elf_link_hash_table *table,
8315 bfd *abfd,
8316 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
8317 struct bfd_hash_table *,
8318 const char *),
8319 unsigned int entsize)
8321 bool ret;
8322 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8323 int can_refcount = bed->can_refcount;
8325 table->init_got_refcount.refcount = can_refcount - 1;
8326 table->init_plt_refcount.refcount = can_refcount - 1;
8327 table->init_got_offset.offset = -(bfd_vma) 1;
8328 table->init_plt_offset.offset = -(bfd_vma) 1;
8329 /* The first dynamic symbol is a dummy. */
8330 table->dynsymcount = 1;
8332 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
8334 table->root.type = bfd_link_elf_hash_table;
8335 table->hash_table_id = bed->target_id;
8336 table->target_os = bed->target_os;
8337 table->root.hash_table_free = _bfd_elf_link_hash_table_free;
8339 return ret;
8342 /* Create an ELF linker hash table. */
8344 struct bfd_link_hash_table *
8345 _bfd_elf_link_hash_table_create (bfd *abfd)
8347 struct elf_link_hash_table *ret;
8348 size_t amt = sizeof (struct elf_link_hash_table);
8350 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
8351 if (ret == NULL)
8352 return NULL;
8354 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
8355 sizeof (struct elf_link_hash_entry)))
8357 free (ret);
8358 return NULL;
8361 return &ret->root;
8364 /* Destroy an ELF linker hash table. */
8366 void
8367 _bfd_elf_link_hash_table_free (bfd *obfd)
8369 struct elf_link_hash_table *htab;
8371 htab = (struct elf_link_hash_table *) obfd->link.hash;
8372 if (htab->dynstr != NULL)
8373 _bfd_elf_strtab_free (htab->dynstr);
8374 _bfd_merge_sections_free (htab->merge_info);
8375 /* NB: htab->dynamic->contents is always allocated by bfd_realloc. */
8376 if (htab->dynamic != NULL)
8378 free (htab->dynamic->contents);
8379 htab->dynamic->contents = NULL;
8381 if (htab->first_hash != NULL)
8383 bfd_hash_table_free (htab->first_hash);
8384 free (htab->first_hash);
8386 if (htab->eh_info.frame_hdr_is_compact)
8387 free (htab->eh_info.u.compact.entries);
8388 else
8389 free (htab->eh_info.u.dwarf.array);
8390 _bfd_generic_link_hash_table_free (obfd);
8393 /* This is a hook for the ELF emulation code in the generic linker to
8394 tell the backend linker what file name to use for the DT_NEEDED
8395 entry for a dynamic object. */
8397 void
8398 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
8400 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8401 && bfd_get_format (abfd) == bfd_object)
8402 elf_dt_name (abfd) = name;
8406 bfd_elf_get_dyn_lib_class (bfd *abfd)
8408 int lib_class;
8409 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8410 && bfd_get_format (abfd) == bfd_object)
8411 lib_class = elf_dyn_lib_class (abfd);
8412 else
8413 lib_class = 0;
8414 return lib_class;
8417 void
8418 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
8420 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8421 && bfd_get_format (abfd) == bfd_object)
8422 elf_dyn_lib_class (abfd) = lib_class;
8425 /* Get the list of DT_NEEDED entries for a link. This is a hook for
8426 the linker ELF emulation code. */
8428 struct bfd_link_needed_list *
8429 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
8430 struct bfd_link_info *info)
8432 if (! is_elf_hash_table (info->hash))
8433 return NULL;
8434 return elf_hash_table (info)->needed;
8437 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
8438 hook for the linker ELF emulation code. */
8440 struct bfd_link_needed_list *
8441 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
8442 struct bfd_link_info *info)
8444 if (! is_elf_hash_table (info->hash))
8445 return NULL;
8446 return elf_hash_table (info)->runpath;
8449 /* Get the name actually used for a dynamic object for a link. This
8450 is the SONAME entry if there is one. Otherwise, it is the string
8451 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
8453 const char *
8454 bfd_elf_get_dt_soname (bfd *abfd)
8456 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8457 && bfd_get_format (abfd) == bfd_object)
8458 return elf_dt_name (abfd);
8459 return NULL;
8462 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
8463 the ELF linker emulation code. */
8465 bool
8466 bfd_elf_get_bfd_needed_list (bfd *abfd,
8467 struct bfd_link_needed_list **pneeded)
8469 asection *s;
8470 bfd_byte *dynbuf = NULL;
8471 unsigned int elfsec;
8472 unsigned long shlink;
8473 bfd_byte *extdyn, *extdynend;
8474 size_t extdynsize;
8475 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
8477 *pneeded = NULL;
8479 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
8480 || bfd_get_format (abfd) != bfd_object)
8481 return true;
8483 s = bfd_get_section_by_name (abfd, ".dynamic");
8484 if (s == NULL || s->size == 0 || (s->flags & SEC_HAS_CONTENTS) == 0)
8485 return true;
8487 if (!_bfd_elf_mmap_section_contents (abfd, s, &dynbuf))
8488 goto error_return;
8490 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
8491 if (elfsec == SHN_BAD)
8492 goto error_return;
8494 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
8496 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
8497 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
8499 for (extdyn = dynbuf, extdynend = dynbuf + s->size;
8500 (size_t) (extdynend - extdyn) >= extdynsize;
8501 extdyn += extdynsize)
8503 Elf_Internal_Dyn dyn;
8505 (*swap_dyn_in) (abfd, extdyn, &dyn);
8507 if (dyn.d_tag == DT_NULL)
8508 break;
8510 if (dyn.d_tag == DT_NEEDED)
8512 const char *string;
8513 struct bfd_link_needed_list *l;
8514 unsigned int tagv = dyn.d_un.d_val;
8515 size_t amt;
8517 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
8518 if (string == NULL)
8519 goto error_return;
8521 amt = sizeof *l;
8522 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
8523 if (l == NULL)
8524 goto error_return;
8526 l->by = abfd;
8527 l->name = string;
8528 l->next = *pneeded;
8529 *pneeded = l;
8533 _bfd_elf_munmap_section_contents (s, dynbuf);
8535 return true;
8537 error_return:
8538 _bfd_elf_munmap_section_contents (s, dynbuf);
8539 return false;
8542 struct elf_symbuf_symbol
8544 unsigned long st_name; /* Symbol name, index in string tbl */
8545 unsigned char st_info; /* Type and binding attributes */
8546 unsigned char st_other; /* Visibilty, and target specific */
8549 struct elf_symbuf_head
8551 struct elf_symbuf_symbol *ssym;
8552 size_t count;
8553 unsigned int st_shndx;
8556 struct elf_symbol
8558 union
8560 Elf_Internal_Sym *isym;
8561 struct elf_symbuf_symbol *ssym;
8562 void *p;
8563 } u;
8564 const char *name;
8567 /* Sort references to symbols by ascending section number. */
8569 static int
8570 elf_sort_elf_symbol (const void *arg1, const void *arg2)
8572 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
8573 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
8575 if (s1->st_shndx != s2->st_shndx)
8576 return s1->st_shndx > s2->st_shndx ? 1 : -1;
8577 /* Final sort by the address of the sym in the symbuf ensures
8578 a stable sort. */
8579 if (s1 != s2)
8580 return s1 > s2 ? 1 : -1;
8581 return 0;
8584 static int
8585 elf_sym_name_compare (const void *arg1, const void *arg2)
8587 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
8588 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
8589 int ret = strcmp (s1->name, s2->name);
8590 if (ret != 0)
8591 return ret;
8592 if (s1->u.p != s2->u.p)
8593 return s1->u.p > s2->u.p ? 1 : -1;
8594 return 0;
8597 static struct elf_symbuf_head *
8598 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
8600 Elf_Internal_Sym **ind, **indbufend, **indbuf;
8601 struct elf_symbuf_symbol *ssym;
8602 struct elf_symbuf_head *ssymbuf, *ssymhead;
8603 size_t i, shndx_count, total_size, amt;
8605 amt = symcount * sizeof (*indbuf);
8606 indbuf = (Elf_Internal_Sym **) bfd_malloc (amt);
8607 if (indbuf == NULL)
8608 return NULL;
8610 for (ind = indbuf, i = 0; i < symcount; i++)
8611 if (isymbuf[i].st_shndx != SHN_UNDEF)
8612 *ind++ = &isymbuf[i];
8613 indbufend = ind;
8615 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
8616 elf_sort_elf_symbol);
8618 shndx_count = 0;
8619 if (indbufend > indbuf)
8620 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
8621 if (ind[0]->st_shndx != ind[1]->st_shndx)
8622 shndx_count++;
8624 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
8625 + (indbufend - indbuf) * sizeof (*ssym));
8626 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
8627 if (ssymbuf == NULL)
8629 free (indbuf);
8630 return NULL;
8633 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
8634 ssymbuf->ssym = NULL;
8635 ssymbuf->count = shndx_count;
8636 ssymbuf->st_shndx = 0;
8637 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
8639 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
8641 ssymhead++;
8642 ssymhead->ssym = ssym;
8643 ssymhead->count = 0;
8644 ssymhead->st_shndx = (*ind)->st_shndx;
8646 ssym->st_name = (*ind)->st_name;
8647 ssym->st_info = (*ind)->st_info;
8648 ssym->st_other = (*ind)->st_other;
8649 ssymhead->count++;
8651 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
8652 && (uintptr_t) ssym - (uintptr_t) ssymbuf == total_size);
8654 free (indbuf);
8655 return ssymbuf;
8658 /* Check if 2 sections define the same set of local and global
8659 symbols. */
8661 static bool
8662 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
8663 struct bfd_link_info *info)
8665 bfd *bfd1, *bfd2;
8666 const struct elf_backend_data *bed1, *bed2;
8667 Elf_Internal_Shdr *hdr1, *hdr2;
8668 size_t symcount1, symcount2;
8669 Elf_Internal_Sym *isymbuf1, *isymbuf2;
8670 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
8671 Elf_Internal_Sym *isym, *isymend;
8672 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
8673 size_t count1, count2, sec_count1, sec_count2, i;
8674 unsigned int shndx1, shndx2;
8675 bool result;
8676 bool ignore_section_symbol_p;
8678 bfd1 = sec1->owner;
8679 bfd2 = sec2->owner;
8681 /* Both sections have to be in ELF. */
8682 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
8683 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
8684 return false;
8686 if (elf_section_type (sec1) != elf_section_type (sec2))
8687 return false;
8689 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
8690 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
8691 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
8692 return false;
8694 bed1 = get_elf_backend_data (bfd1);
8695 bed2 = get_elf_backend_data (bfd2);
8696 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
8697 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
8698 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
8699 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
8701 if (symcount1 == 0 || symcount2 == 0)
8702 return false;
8704 result = false;
8705 isymbuf1 = NULL;
8706 isymbuf2 = NULL;
8707 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
8708 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
8710 /* Ignore section symbols only when matching non-debugging sections
8711 or linkonce section with comdat section. */
8712 ignore_section_symbol_p
8713 = ((sec1->flags & SEC_DEBUGGING) == 0
8714 || ((elf_section_flags (sec1) & SHF_GROUP)
8715 != (elf_section_flags (sec2) & SHF_GROUP)));
8717 if (ssymbuf1 == NULL)
8719 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
8720 NULL, NULL, NULL);
8721 if (isymbuf1 == NULL)
8722 goto done;
8724 if (info != NULL && !info->reduce_memory_overheads)
8726 ssymbuf1 = elf_create_symbuf (symcount1, isymbuf1);
8727 elf_tdata (bfd1)->symbuf = ssymbuf1;
8731 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
8733 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
8734 NULL, NULL, NULL);
8735 if (isymbuf2 == NULL)
8736 goto done;
8738 if (ssymbuf1 != NULL && info != NULL && !info->reduce_memory_overheads)
8740 ssymbuf2 = elf_create_symbuf (symcount2, isymbuf2);
8741 elf_tdata (bfd2)->symbuf = ssymbuf2;
8745 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
8747 /* Optimized faster version. */
8748 size_t lo, hi, mid;
8749 struct elf_symbol *symp;
8750 struct elf_symbuf_symbol *ssym, *ssymend;
8752 lo = 0;
8753 hi = ssymbuf1->count;
8754 ssymbuf1++;
8755 count1 = 0;
8756 sec_count1 = 0;
8757 while (lo < hi)
8759 mid = (lo + hi) / 2;
8760 if (shndx1 < ssymbuf1[mid].st_shndx)
8761 hi = mid;
8762 else if (shndx1 > ssymbuf1[mid].st_shndx)
8763 lo = mid + 1;
8764 else
8766 count1 = ssymbuf1[mid].count;
8767 ssymbuf1 += mid;
8768 break;
8771 if (ignore_section_symbol_p)
8773 for (i = 0; i < count1; i++)
8774 if (ELF_ST_TYPE (ssymbuf1->ssym[i].st_info) == STT_SECTION)
8775 sec_count1++;
8776 count1 -= sec_count1;
8779 lo = 0;
8780 hi = ssymbuf2->count;
8781 ssymbuf2++;
8782 count2 = 0;
8783 sec_count2 = 0;
8784 while (lo < hi)
8786 mid = (lo + hi) / 2;
8787 if (shndx2 < ssymbuf2[mid].st_shndx)
8788 hi = mid;
8789 else if (shndx2 > ssymbuf2[mid].st_shndx)
8790 lo = mid + 1;
8791 else
8793 count2 = ssymbuf2[mid].count;
8794 ssymbuf2 += mid;
8795 break;
8798 if (ignore_section_symbol_p)
8800 for (i = 0; i < count2; i++)
8801 if (ELF_ST_TYPE (ssymbuf2->ssym[i].st_info) == STT_SECTION)
8802 sec_count2++;
8803 count2 -= sec_count2;
8806 if (count1 == 0 || count2 == 0 || count1 != count2)
8807 goto done;
8809 symtable1
8810 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
8811 symtable2
8812 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
8813 if (symtable1 == NULL || symtable2 == NULL)
8814 goto done;
8816 symp = symtable1;
8817 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1 + sec_count1;
8818 ssym < ssymend; ssym++)
8819 if (sec_count1 == 0
8820 || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
8822 symp->u.ssym = ssym;
8823 symp->name = bfd_elf_string_from_elf_section (bfd1,
8824 hdr1->sh_link,
8825 ssym->st_name);
8826 if (symp->name == NULL)
8827 goto done;
8828 symp++;
8831 symp = symtable2;
8832 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2 + sec_count2;
8833 ssym < ssymend; ssym++)
8834 if (sec_count2 == 0
8835 || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
8837 symp->u.ssym = ssym;
8838 symp->name = bfd_elf_string_from_elf_section (bfd2,
8839 hdr2->sh_link,
8840 ssym->st_name);
8841 if (symp->name == NULL)
8842 goto done;
8843 symp++;
8846 /* Sort symbol by name. */
8847 qsort (symtable1, count1, sizeof (struct elf_symbol),
8848 elf_sym_name_compare);
8849 qsort (symtable2, count1, sizeof (struct elf_symbol),
8850 elf_sym_name_compare);
8852 for (i = 0; i < count1; i++)
8853 /* Two symbols must have the same binding, type and name. */
8854 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8855 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8856 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8857 goto done;
8859 result = true;
8860 goto done;
8863 symtable1 = (struct elf_symbol *)
8864 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8865 symtable2 = (struct elf_symbol *)
8866 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
8867 if (symtable1 == NULL || symtable2 == NULL)
8868 goto done;
8870 /* Count definitions in the section. */
8871 count1 = 0;
8872 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
8873 if (isym->st_shndx == shndx1
8874 && (!ignore_section_symbol_p
8875 || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
8876 symtable1[count1++].u.isym = isym;
8878 count2 = 0;
8879 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
8880 if (isym->st_shndx == shndx2
8881 && (!ignore_section_symbol_p
8882 || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
8883 symtable2[count2++].u.isym = isym;
8885 if (count1 == 0 || count2 == 0 || count1 != count2)
8886 goto done;
8888 for (i = 0; i < count1; i++)
8890 symtable1[i].name
8891 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8892 symtable1[i].u.isym->st_name);
8893 if (symtable1[i].name == NULL)
8894 goto done;
8897 for (i = 0; i < count2; i++)
8899 symtable2[i].name
8900 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8901 symtable2[i].u.isym->st_name);
8902 if (symtable2[i].name == NULL)
8903 goto done;
8906 /* Sort symbol by name. */
8907 qsort (symtable1, count1, sizeof (struct elf_symbol),
8908 elf_sym_name_compare);
8909 qsort (symtable2, count1, sizeof (struct elf_symbol),
8910 elf_sym_name_compare);
8912 for (i = 0; i < count1; i++)
8913 /* Two symbols must have the same binding, type and name. */
8914 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8915 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8916 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8917 goto done;
8919 result = true;
8921 done:
8922 free (symtable1);
8923 free (symtable2);
8924 free (isymbuf1);
8925 free (isymbuf2);
8927 return result;
8930 /* Return TRUE if 2 section types are compatible. */
8932 bool
8933 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8934 bfd *bbfd, const asection *bsec)
8936 if (asec == NULL
8937 || bsec == NULL
8938 || abfd->xvec->flavour != bfd_target_elf_flavour
8939 || bbfd->xvec->flavour != bfd_target_elf_flavour)
8940 return true;
8942 return elf_section_type (asec) == elf_section_type (bsec);
8945 /* Final phase of ELF linker. */
8947 /* A structure we use to avoid passing large numbers of arguments. */
8949 struct elf_final_link_info
8951 /* General link information. */
8952 struct bfd_link_info *info;
8953 /* Output BFD. */
8954 bfd *output_bfd;
8955 /* Symbol string table. */
8956 struct elf_strtab_hash *symstrtab;
8957 /* .hash section. */
8958 asection *hash_sec;
8959 /* symbol version section (.gnu.version). */
8960 asection *symver_sec;
8961 /* Buffer large enough to hold contents of any section. */
8962 bfd_byte *contents;
8963 /* Buffer large enough to hold external relocs of any section. */
8964 void *external_relocs;
8965 /* Buffer large enough to hold internal relocs of any section. */
8966 Elf_Internal_Rela *internal_relocs;
8967 /* Buffer large enough to hold external local symbols of any input
8968 BFD. */
8969 bfd_byte *external_syms;
8970 /* And a buffer for symbol section indices. */
8971 Elf_External_Sym_Shndx *locsym_shndx;
8972 /* Buffer large enough to hold internal local symbols of any input
8973 BFD. */
8974 Elf_Internal_Sym *internal_syms;
8975 /* Array large enough to hold a symbol index for each local symbol
8976 of any input BFD. */
8977 long *indices;
8978 /* Array large enough to hold a section pointer for each local
8979 symbol of any input BFD. */
8980 asection **sections;
8981 /* Buffer for SHT_SYMTAB_SHNDX section. */
8982 Elf_External_Sym_Shndx *symshndxbuf;
8983 /* Number of STT_FILE syms seen. */
8984 size_t filesym_count;
8985 /* Local symbol hash table. */
8986 struct bfd_hash_table local_hash_table;
8989 struct local_hash_entry
8991 /* Base hash table entry structure. */
8992 struct bfd_hash_entry root;
8993 /* Size of the local symbol name. */
8994 size_t size;
8995 /* Number of the duplicated local symbol names. */
8996 long count;
8999 /* Create an entry in the local symbol hash table. */
9001 static struct bfd_hash_entry *
9002 local_hash_newfunc (struct bfd_hash_entry *entry,
9003 struct bfd_hash_table *table,
9004 const char *string)
9007 /* Allocate the structure if it has not already been allocated by a
9008 subclass. */
9009 if (entry == NULL)
9011 entry = bfd_hash_allocate (table,
9012 sizeof (struct local_hash_entry));
9013 if (entry == NULL)
9014 return entry;
9017 /* Call the allocation method of the superclass. */
9018 entry = bfd_hash_newfunc (entry, table, string);
9019 if (entry != NULL)
9021 ((struct local_hash_entry *) entry)->count = 0;
9022 ((struct local_hash_entry *) entry)->size = 0;
9025 return entry;
9028 /* This struct is used to pass information to elf_link_output_extsym. */
9030 struct elf_outext_info
9032 bool failed;
9033 bool localsyms;
9034 bool file_sym_done;
9035 struct elf_final_link_info *flinfo;
9039 /* Support for evaluating a complex relocation.
9041 Complex relocations are generalized, self-describing relocations. The
9042 implementation of them consists of two parts: complex symbols, and the
9043 relocations themselves.
9045 The relocations use a reserved elf-wide relocation type code (R_RELC
9046 external / BFD_RELOC_RELC internal) and an encoding of relocation field
9047 information (start bit, end bit, word width, etc) into the addend. This
9048 information is extracted from CGEN-generated operand tables within gas.
9050 Complex symbols are mangled symbols (STT_RELC external / BSF_RELC
9051 internal) representing prefix-notation expressions, including but not
9052 limited to those sorts of expressions normally encoded as addends in the
9053 addend field. The symbol mangling format is:
9055 <node> := <literal>
9056 | <unary-operator> ':' <node>
9057 | <binary-operator> ':' <node> ':' <node>
9060 <literal> := 's' <digits=N> ':' <N character symbol name>
9061 | 'S' <digits=N> ':' <N character section name>
9062 | '#' <hexdigits>
9065 <binary-operator> := as in C
9066 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
9068 static void
9069 set_symbol_value (bfd *bfd_with_globals,
9070 Elf_Internal_Sym *isymbuf,
9071 size_t locsymcount,
9072 size_t symidx,
9073 bfd_vma val)
9075 struct elf_link_hash_entry **sym_hashes;
9076 struct elf_link_hash_entry *h;
9077 size_t extsymoff = locsymcount;
9079 if (symidx < locsymcount)
9081 Elf_Internal_Sym *sym;
9083 sym = isymbuf + symidx;
9084 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
9086 /* It is a local symbol: move it to the
9087 "absolute" section and give it a value. */
9088 sym->st_shndx = SHN_ABS;
9089 sym->st_value = val;
9090 return;
9092 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
9093 extsymoff = 0;
9096 /* It is a global symbol: set its link type
9097 to "defined" and give it a value. */
9099 sym_hashes = elf_sym_hashes (bfd_with_globals);
9100 h = sym_hashes [symidx - extsymoff];
9101 while (h->root.type == bfd_link_hash_indirect
9102 || h->root.type == bfd_link_hash_warning)
9103 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9104 h->root.type = bfd_link_hash_defined;
9105 h->root.u.def.value = val;
9106 h->root.u.def.section = bfd_abs_section_ptr;
9109 static bool
9110 resolve_symbol (const char *name,
9111 bfd *input_bfd,
9112 struct elf_final_link_info *flinfo,
9113 bfd_vma *result,
9114 Elf_Internal_Sym *isymbuf,
9115 size_t locsymcount)
9117 Elf_Internal_Sym *sym;
9118 struct bfd_link_hash_entry *global_entry;
9119 const char *candidate = NULL;
9120 Elf_Internal_Shdr *symtab_hdr;
9121 size_t i;
9123 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
9125 for (i = 0; i < locsymcount; ++ i)
9127 sym = isymbuf + i;
9129 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
9130 continue;
9132 candidate = bfd_elf_string_from_elf_section (input_bfd,
9133 symtab_hdr->sh_link,
9134 sym->st_name);
9135 #ifdef DEBUG
9136 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
9137 name, candidate, (unsigned long) sym->st_value);
9138 #endif
9139 if (candidate && strcmp (candidate, name) == 0)
9141 asection *sec = flinfo->sections [i];
9143 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
9144 *result += sec->output_offset + sec->output_section->vma;
9145 #ifdef DEBUG
9146 printf ("Found symbol with value %8.8lx\n",
9147 (unsigned long) *result);
9148 #endif
9149 return true;
9153 /* Hmm, haven't found it yet. perhaps it is a global. */
9154 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
9155 false, false, true);
9156 if (!global_entry)
9157 return false;
9159 if (global_entry->type == bfd_link_hash_defined
9160 || global_entry->type == bfd_link_hash_defweak)
9162 *result = (global_entry->u.def.value
9163 + global_entry->u.def.section->output_section->vma
9164 + global_entry->u.def.section->output_offset);
9165 #ifdef DEBUG
9166 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
9167 global_entry->root.string, (unsigned long) *result);
9168 #endif
9169 return true;
9172 return false;
9175 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
9176 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
9177 names like "foo.end" which is the end address of section "foo". */
9179 static bool
9180 resolve_section (const char *name,
9181 asection *sections,
9182 bfd_vma *result,
9183 bfd * abfd)
9185 asection *curr;
9186 unsigned int len;
9188 for (curr = sections; curr; curr = curr->next)
9189 if (strcmp (curr->name, name) == 0)
9191 *result = curr->vma;
9192 return true;
9195 /* Hmm. still haven't found it. try pseudo-section names. */
9196 /* FIXME: This could be coded more efficiently... */
9197 for (curr = sections; curr; curr = curr->next)
9199 len = strlen (curr->name);
9200 if (len > strlen (name))
9201 continue;
9203 if (strncmp (curr->name, name, len) == 0)
9205 if (startswith (name + len, ".end"))
9207 *result = (curr->vma
9208 + curr->size / bfd_octets_per_byte (abfd, curr));
9209 return true;
9212 /* Insert more pseudo-section names here, if you like. */
9216 return false;
9219 static void
9220 undefined_reference (const char *reftype, const char *name)
9222 /* xgettext:c-format */
9223 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
9224 reftype, name);
9225 bfd_set_error (bfd_error_bad_value);
9228 static bool
9229 eval_symbol (bfd_vma *result,
9230 const char **symp,
9231 bfd *input_bfd,
9232 struct elf_final_link_info *flinfo,
9233 bfd_vma dot,
9234 Elf_Internal_Sym *isymbuf,
9235 size_t locsymcount,
9236 int signed_p)
9238 size_t len;
9239 size_t symlen;
9240 bfd_vma a;
9241 bfd_vma b;
9242 char symbuf[4096];
9243 const char *sym = *symp;
9244 const char *symend;
9245 bool symbol_is_section = false;
9247 len = strlen (sym);
9248 symend = sym + len;
9250 if (len < 1 || len > sizeof (symbuf))
9252 bfd_set_error (bfd_error_invalid_operation);
9253 return false;
9256 switch (* sym)
9258 case '.':
9259 *result = dot;
9260 *symp = sym + 1;
9261 return true;
9263 case '#':
9264 ++sym;
9265 *result = strtoul (sym, (char **) symp, 16);
9266 return true;
9268 case 'S':
9269 symbol_is_section = true;
9270 /* Fall through. */
9271 case 's':
9272 ++sym;
9273 symlen = strtol (sym, (char **) symp, 10);
9274 sym = *symp + 1; /* Skip the trailing ':'. */
9276 if (symend < sym || symlen + 1 > sizeof (symbuf))
9278 bfd_set_error (bfd_error_invalid_operation);
9279 return false;
9282 memcpy (symbuf, sym, symlen);
9283 symbuf[symlen] = '\0';
9284 *symp = sym + symlen;
9286 /* Is it always possible, with complex symbols, that gas "mis-guessed"
9287 the symbol as a section, or vice-versa. so we're pretty liberal in our
9288 interpretation here; section means "try section first", not "must be a
9289 section", and likewise with symbol. */
9291 if (symbol_is_section)
9293 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
9294 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
9295 isymbuf, locsymcount))
9297 undefined_reference ("section", symbuf);
9298 return false;
9301 else
9303 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
9304 isymbuf, locsymcount)
9305 && !resolve_section (symbuf, flinfo->output_bfd->sections,
9306 result, input_bfd))
9308 undefined_reference ("symbol", symbuf);
9309 return false;
9313 return true;
9315 /* All that remains are operators. */
9317 #define UNARY_OP(op) \
9318 if (startswith (sym, #op)) \
9320 sym += strlen (#op); \
9321 if (*sym == ':') \
9322 ++sym; \
9323 *symp = sym; \
9324 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
9325 isymbuf, locsymcount, signed_p)) \
9326 return false; \
9327 if (signed_p) \
9328 *result = op ((bfd_signed_vma) a); \
9329 else \
9330 *result = op a; \
9331 return true; \
9334 #define BINARY_OP_HEAD(op) \
9335 if (startswith (sym, #op)) \
9337 sym += strlen (#op); \
9338 if (*sym == ':') \
9339 ++sym; \
9340 *symp = sym; \
9341 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
9342 isymbuf, locsymcount, signed_p)) \
9343 return false; \
9344 ++*symp; \
9345 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
9346 isymbuf, locsymcount, signed_p)) \
9347 return false;
9348 #define BINARY_OP_TAIL(op) \
9349 if (signed_p) \
9350 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
9351 else \
9352 *result = a op b; \
9353 return true; \
9355 #define BINARY_OP(op) BINARY_OP_HEAD(op) BINARY_OP_TAIL(op)
9357 default:
9358 UNARY_OP (0-);
9359 BINARY_OP_HEAD (<<);
9360 if (b >= sizeof (a) * CHAR_BIT)
9362 *result = 0;
9363 return true;
9365 signed_p = 0;
9366 BINARY_OP_TAIL (<<);
9367 BINARY_OP_HEAD (>>);
9368 if (b >= sizeof (a) * CHAR_BIT)
9370 *result = signed_p && (bfd_signed_vma) a < 0 ? -1 : 0;
9371 return true;
9373 BINARY_OP_TAIL (>>);
9374 BINARY_OP (==);
9375 BINARY_OP (!=);
9376 BINARY_OP (<=);
9377 BINARY_OP (>=);
9378 BINARY_OP (&&);
9379 BINARY_OP (||);
9380 UNARY_OP (~);
9381 UNARY_OP (!);
9382 BINARY_OP (*);
9383 BINARY_OP_HEAD (/);
9384 if (b == 0)
9386 _bfd_error_handler (_("division by zero"));
9387 bfd_set_error (bfd_error_bad_value);
9388 return false;
9390 BINARY_OP_TAIL (/);
9391 BINARY_OP_HEAD (%);
9392 if (b == 0)
9394 _bfd_error_handler (_("division by zero"));
9395 bfd_set_error (bfd_error_bad_value);
9396 return false;
9398 BINARY_OP_TAIL (%);
9399 BINARY_OP (^);
9400 BINARY_OP (|);
9401 BINARY_OP (&);
9402 BINARY_OP (+);
9403 BINARY_OP (-);
9404 BINARY_OP (<);
9405 BINARY_OP (>);
9406 #undef UNARY_OP
9407 #undef BINARY_OP
9408 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
9409 bfd_set_error (bfd_error_invalid_operation);
9410 return false;
9414 static void
9415 put_value (bfd_vma size,
9416 unsigned long chunksz,
9417 bfd *input_bfd,
9418 bfd_vma x,
9419 bfd_byte *location)
9421 location += (size - chunksz);
9423 for (; size; size -= chunksz, location -= chunksz)
9425 switch (chunksz)
9427 case 1:
9428 bfd_put_8 (input_bfd, x, location);
9429 x >>= 8;
9430 break;
9431 case 2:
9432 bfd_put_16 (input_bfd, x, location);
9433 x >>= 16;
9434 break;
9435 case 4:
9436 bfd_put_32 (input_bfd, x, location);
9437 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
9438 x >>= 16;
9439 x >>= 16;
9440 break;
9441 #ifdef BFD64
9442 case 8:
9443 bfd_put_64 (input_bfd, x, location);
9444 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
9445 x >>= 32;
9446 x >>= 32;
9447 break;
9448 #endif
9449 default:
9450 abort ();
9451 break;
9456 static bfd_vma
9457 get_value (bfd_vma size,
9458 unsigned long chunksz,
9459 bfd *input_bfd,
9460 bfd_byte *location)
9462 int shift;
9463 bfd_vma x = 0;
9465 /* Sanity checks. */
9466 BFD_ASSERT (chunksz <= sizeof (x)
9467 && size >= chunksz
9468 && chunksz != 0
9469 && (size % chunksz) == 0
9470 && input_bfd != NULL
9471 && location != NULL);
9473 if (chunksz == sizeof (x))
9475 BFD_ASSERT (size == chunksz);
9477 /* Make sure that we do not perform an undefined shift operation.
9478 We know that size == chunksz so there will only be one iteration
9479 of the loop below. */
9480 shift = 0;
9482 else
9483 shift = 8 * chunksz;
9485 for (; size; size -= chunksz, location += chunksz)
9487 switch (chunksz)
9489 case 1:
9490 x = (x << shift) | bfd_get_8 (input_bfd, location);
9491 break;
9492 case 2:
9493 x = (x << shift) | bfd_get_16 (input_bfd, location);
9494 break;
9495 case 4:
9496 x = (x << shift) | bfd_get_32 (input_bfd, location);
9497 break;
9498 #ifdef BFD64
9499 case 8:
9500 x = (x << shift) | bfd_get_64 (input_bfd, location);
9501 break;
9502 #endif
9503 default:
9504 abort ();
9507 return x;
9510 static void
9511 decode_complex_addend (unsigned long *start, /* in bits */
9512 unsigned long *oplen, /* in bits */
9513 unsigned long *len, /* in bits */
9514 unsigned long *wordsz, /* in bytes */
9515 unsigned long *chunksz, /* in bytes */
9516 unsigned long *lsb0_p,
9517 unsigned long *signed_p,
9518 unsigned long *trunc_p,
9519 unsigned long encoded)
9521 * start = encoded & 0x3F;
9522 * len = (encoded >> 6) & 0x3F;
9523 * oplen = (encoded >> 12) & 0x3F;
9524 * wordsz = (encoded >> 18) & 0xF;
9525 * chunksz = (encoded >> 22) & 0xF;
9526 * lsb0_p = (encoded >> 27) & 1;
9527 * signed_p = (encoded >> 28) & 1;
9528 * trunc_p = (encoded >> 29) & 1;
9531 bfd_reloc_status_type
9532 bfd_elf_perform_complex_relocation (bfd *input_bfd,
9533 asection *input_section,
9534 bfd_byte *contents,
9535 Elf_Internal_Rela *rel,
9536 bfd_vma relocation)
9538 bfd_vma shift, x, mask;
9539 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
9540 bfd_reloc_status_type r;
9541 bfd_size_type octets;
9543 /* Perform this reloc, since it is complex.
9544 (this is not to say that it necessarily refers to a complex
9545 symbol; merely that it is a self-describing CGEN based reloc.
9546 i.e. the addend has the complete reloc information (bit start, end,
9547 word size, etc) encoded within it.). */
9549 decode_complex_addend (&start, &oplen, &len, &wordsz,
9550 &chunksz, &lsb0_p, &signed_p,
9551 &trunc_p, rel->r_addend);
9553 mask = (((1L << (len - 1)) - 1) << 1) | 1;
9555 if (lsb0_p)
9556 shift = (start + 1) - len;
9557 else
9558 shift = (8 * wordsz) - (start + len);
9560 octets = rel->r_offset * bfd_octets_per_byte (input_bfd, input_section);
9561 x = get_value (wordsz, chunksz, input_bfd, contents + octets);
9563 #ifdef DEBUG
9564 printf ("Doing complex reloc: "
9565 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
9566 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
9567 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
9568 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9569 oplen, (unsigned long) x, (unsigned long) mask,
9570 (unsigned long) relocation);
9571 #endif
9573 r = bfd_reloc_ok;
9574 if (! trunc_p)
9575 /* Now do an overflow check. */
9576 r = bfd_check_overflow ((signed_p
9577 ? complain_overflow_signed
9578 : complain_overflow_unsigned),
9579 len, 0, (8 * wordsz),
9580 relocation);
9582 /* Do the deed. */
9583 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
9585 #ifdef DEBUG
9586 printf (" relocation: %8.8lx\n"
9587 " shifted mask: %8.8lx\n"
9588 " shifted/masked reloc: %8.8lx\n"
9589 " result: %8.8lx\n",
9590 (unsigned long) relocation, (unsigned long) (mask << shift),
9591 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
9592 #endif
9593 put_value (wordsz, chunksz, input_bfd, x, contents + octets);
9594 return r;
9597 /* Functions to read r_offset from external (target order) reloc
9598 entry. Faster than bfd_getl32 et al, because we let the compiler
9599 know the value is aligned. */
9601 static bfd_vma
9602 ext32l_r_offset (const void *p)
9604 union aligned32
9606 uint32_t v;
9607 unsigned char c[4];
9609 const union aligned32 *a
9610 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9612 uint32_t aval = ( (uint32_t) a->c[0]
9613 | (uint32_t) a->c[1] << 8
9614 | (uint32_t) a->c[2] << 16
9615 | (uint32_t) a->c[3] << 24);
9616 return aval;
9619 static bfd_vma
9620 ext32b_r_offset (const void *p)
9622 union aligned32
9624 uint32_t v;
9625 unsigned char c[4];
9627 const union aligned32 *a
9628 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9630 uint32_t aval = ( (uint32_t) a->c[0] << 24
9631 | (uint32_t) a->c[1] << 16
9632 | (uint32_t) a->c[2] << 8
9633 | (uint32_t) a->c[3]);
9634 return aval;
9637 static bfd_vma
9638 ext64l_r_offset (const void *p)
9640 union aligned64
9642 uint64_t v;
9643 unsigned char c[8];
9645 const union aligned64 *a
9646 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9648 uint64_t aval = ( (uint64_t) a->c[0]
9649 | (uint64_t) a->c[1] << 8
9650 | (uint64_t) a->c[2] << 16
9651 | (uint64_t) a->c[3] << 24
9652 | (uint64_t) a->c[4] << 32
9653 | (uint64_t) a->c[5] << 40
9654 | (uint64_t) a->c[6] << 48
9655 | (uint64_t) a->c[7] << 56);
9656 return aval;
9659 static bfd_vma
9660 ext64b_r_offset (const void *p)
9662 union aligned64
9664 uint64_t v;
9665 unsigned char c[8];
9667 const union aligned64 *a
9668 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9670 uint64_t aval = ( (uint64_t) a->c[0] << 56
9671 | (uint64_t) a->c[1] << 48
9672 | (uint64_t) a->c[2] << 40
9673 | (uint64_t) a->c[3] << 32
9674 | (uint64_t) a->c[4] << 24
9675 | (uint64_t) a->c[5] << 16
9676 | (uint64_t) a->c[6] << 8
9677 | (uint64_t) a->c[7]);
9678 return aval;
9681 /* When performing a relocatable link, the input relocations are
9682 preserved. But, if they reference global symbols, the indices
9683 referenced must be updated. Update all the relocations found in
9684 RELDATA. */
9686 static bool
9687 elf_link_adjust_relocs (bfd *abfd,
9688 asection *sec,
9689 struct bfd_elf_section_reloc_data *reldata,
9690 bool sort,
9691 struct bfd_link_info *info)
9693 unsigned int i;
9694 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9695 bfd_byte *erela;
9696 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9697 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9698 bfd_vma r_type_mask;
9699 int r_sym_shift;
9700 unsigned int count = reldata->count;
9701 struct elf_link_hash_entry **rel_hash = reldata->hashes;
9703 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
9705 swap_in = bed->s->swap_reloc_in;
9706 swap_out = bed->s->swap_reloc_out;
9708 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
9710 swap_in = bed->s->swap_reloca_in;
9711 swap_out = bed->s->swap_reloca_out;
9713 else
9714 abort ();
9716 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
9717 abort ();
9719 if (bed->s->arch_size == 32)
9721 r_type_mask = 0xff;
9722 r_sym_shift = 8;
9724 else
9726 r_type_mask = 0xffffffff;
9727 r_sym_shift = 32;
9730 erela = reldata->hdr->contents;
9731 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
9733 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
9734 unsigned int j;
9736 if (*rel_hash == NULL)
9737 continue;
9739 if ((*rel_hash)->indx == -2
9740 && info->gc_sections
9741 && ! info->gc_keep_exported)
9743 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9744 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
9745 abfd, sec,
9746 (*rel_hash)->root.root.string);
9747 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
9748 abfd, sec);
9749 bfd_set_error (bfd_error_invalid_operation);
9750 return false;
9752 BFD_ASSERT ((*rel_hash)->indx >= 0);
9754 (*swap_in) (abfd, erela, irela);
9755 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
9756 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
9757 | (irela[j].r_info & r_type_mask));
9758 (*swap_out) (abfd, irela, erela);
9761 if (bed->elf_backend_update_relocs)
9762 (*bed->elf_backend_update_relocs) (sec, reldata);
9764 if (sort && count != 0)
9766 bfd_vma (*ext_r_off) (const void *);
9767 bfd_vma r_off;
9768 size_t elt_size;
9769 bfd_byte *base, *end, *p, *loc;
9770 bfd_byte *buf = NULL;
9772 if (bed->s->arch_size == 32)
9774 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9775 ext_r_off = ext32l_r_offset;
9776 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9777 ext_r_off = ext32b_r_offset;
9778 else
9779 abort ();
9781 else
9783 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9784 ext_r_off = ext64l_r_offset;
9785 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9786 ext_r_off = ext64b_r_offset;
9787 else
9788 abort ();
9791 /* Must use a stable sort here. A modified insertion sort,
9792 since the relocs are mostly sorted already. */
9793 elt_size = reldata->hdr->sh_entsize;
9794 base = reldata->hdr->contents;
9795 end = base + count * elt_size;
9796 if (elt_size > sizeof (Elf64_External_Rela))
9797 abort ();
9799 /* Ensure the first element is lowest. This acts as a sentinel,
9800 speeding the main loop below. */
9801 r_off = (*ext_r_off) (base);
9802 for (p = loc = base; (p += elt_size) < end; )
9804 bfd_vma r_off2 = (*ext_r_off) (p);
9805 if (r_off > r_off2)
9807 r_off = r_off2;
9808 loc = p;
9811 if (loc != base)
9813 /* Don't just swap *base and *loc as that changes the order
9814 of the original base[0] and base[1] if they happen to
9815 have the same r_offset. */
9816 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
9817 memcpy (onebuf, loc, elt_size);
9818 memmove (base + elt_size, base, loc - base);
9819 memcpy (base, onebuf, elt_size);
9822 for (p = base + elt_size; (p += elt_size) < end; )
9824 /* base to p is sorted, *p is next to insert. */
9825 r_off = (*ext_r_off) (p);
9826 /* Search the sorted region for location to insert. */
9827 loc = p - elt_size;
9828 while (r_off < (*ext_r_off) (loc))
9829 loc -= elt_size;
9830 loc += elt_size;
9831 if (loc != p)
9833 /* Chances are there is a run of relocs to insert here,
9834 from one of more input files. Files are not always
9835 linked in order due to the way elf_link_input_bfd is
9836 called. See pr17666. */
9837 size_t sortlen = p - loc;
9838 bfd_vma r_off2 = (*ext_r_off) (loc);
9839 size_t runlen = elt_size;
9840 bfd_vma r_off_runend = r_off;
9841 bfd_vma r_off_runend_next;
9842 size_t buf_size = 96 * 1024;
9843 while (p + runlen < end
9844 && (sortlen <= buf_size
9845 || runlen + elt_size <= buf_size)
9846 /* run must not break the ordering of base..loc+1 */
9847 && r_off2 > (r_off_runend_next = (*ext_r_off) (p + runlen))
9848 /* run must be already sorted */
9849 && r_off_runend_next >= r_off_runend)
9851 runlen += elt_size;
9852 r_off_runend = r_off_runend_next;
9854 if (buf == NULL)
9856 buf = bfd_malloc (buf_size);
9857 if (buf == NULL)
9858 return false;
9860 if (runlen < sortlen)
9862 memcpy (buf, p, runlen);
9863 memmove (loc + runlen, loc, sortlen);
9864 memcpy (loc, buf, runlen);
9866 else
9868 memcpy (buf, loc, sortlen);
9869 memmove (loc, p, runlen);
9870 memcpy (loc + runlen, buf, sortlen);
9872 p += runlen - elt_size;
9875 /* Hashes are no longer valid. */
9876 free (reldata->hashes);
9877 reldata->hashes = NULL;
9878 free (buf);
9880 return true;
9883 struct elf_link_sort_rela
9885 union {
9886 bfd_vma offset;
9887 bfd_vma sym_mask;
9888 } u;
9889 enum elf_reloc_type_class type;
9890 /* We use this as an array of size int_rels_per_ext_rel. */
9891 Elf_Internal_Rela rela[1];
9894 /* qsort stability here and for cmp2 is only an issue if multiple
9895 dynamic relocations are emitted at the same address. But targets
9896 that apply a series of dynamic relocations each operating on the
9897 result of the prior relocation can't use -z combreloc as
9898 implemented anyway. Such schemes tend to be broken by sorting on
9899 symbol index. That leaves dynamic NONE relocs as the only other
9900 case where ld might emit multiple relocs at the same address, and
9901 those are only emitted due to target bugs. */
9903 static int
9904 elf_link_sort_cmp1 (const void *A, const void *B)
9906 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9907 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9908 int relativea, relativeb;
9910 relativea = a->type == reloc_class_relative;
9911 relativeb = b->type == reloc_class_relative;
9913 if (relativea < relativeb)
9914 return 1;
9915 if (relativea > relativeb)
9916 return -1;
9917 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
9918 return -1;
9919 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
9920 return 1;
9921 if (a->rela->r_offset < b->rela->r_offset)
9922 return -1;
9923 if (a->rela->r_offset > b->rela->r_offset)
9924 return 1;
9925 return 0;
9928 static int
9929 elf_link_sort_cmp2 (const void *A, const void *B)
9931 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9932 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9934 if (a->type < b->type)
9935 return -1;
9936 if (a->type > b->type)
9937 return 1;
9938 if (a->u.offset < b->u.offset)
9939 return -1;
9940 if (a->u.offset > b->u.offset)
9941 return 1;
9942 if (a->rela->r_offset < b->rela->r_offset)
9943 return -1;
9944 if (a->rela->r_offset > b->rela->r_offset)
9945 return 1;
9946 return 0;
9949 static size_t
9950 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9952 asection *dynamic_relocs;
9953 asection *rela_dyn;
9954 asection *rel_dyn;
9955 bfd_size_type count, size;
9956 size_t i, ret, sort_elt, ext_size;
9957 bfd_byte *sort, *s_non_relative, *p;
9958 struct elf_link_sort_rela *sq;
9959 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9960 int i2e = bed->s->int_rels_per_ext_rel;
9961 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
9962 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9963 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9964 struct bfd_link_order *lo;
9965 bfd_vma r_sym_mask;
9966 bool use_rela;
9968 /* Find a dynamic reloc section. */
9969 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9970 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
9971 if (rela_dyn != NULL && rela_dyn->size > 0
9972 && rel_dyn != NULL && rel_dyn->size > 0)
9974 bool use_rela_initialised = false;
9976 /* This is just here to stop gcc from complaining.
9977 Its initialization checking code is not perfect. */
9978 use_rela = true;
9980 /* Both sections are present. Examine the sizes
9981 of the indirect sections to help us choose. */
9982 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9983 if (lo->type == bfd_indirect_link_order)
9985 asection *o = lo->u.indirect.section;
9987 if ((o->size % bed->s->sizeof_rela) == 0)
9989 if ((o->size % bed->s->sizeof_rel) == 0)
9990 /* Section size is divisible by both rel and rela sizes.
9991 It is of no help to us. */
9993 else
9995 /* Section size is only divisible by rela. */
9996 if (use_rela_initialised && !use_rela)
9998 _bfd_error_handler (_("%pB: unable to sort relocs - "
9999 "they are in more than one size"),
10000 abfd);
10001 bfd_set_error (bfd_error_invalid_operation);
10002 return 0;
10004 else
10006 use_rela = true;
10007 use_rela_initialised = true;
10011 else if ((o->size % bed->s->sizeof_rel) == 0)
10013 /* Section size is only divisible by rel. */
10014 if (use_rela_initialised && use_rela)
10016 _bfd_error_handler (_("%pB: unable to sort relocs - "
10017 "they are in more than one size"),
10018 abfd);
10019 bfd_set_error (bfd_error_invalid_operation);
10020 return 0;
10022 else
10024 use_rela = false;
10025 use_rela_initialised = true;
10028 else
10030 /* The section size is not divisible by either -
10031 something is wrong. */
10032 _bfd_error_handler (_("%pB: unable to sort relocs - "
10033 "they are of an unknown size"), abfd);
10034 bfd_set_error (bfd_error_invalid_operation);
10035 return 0;
10039 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
10040 if (lo->type == bfd_indirect_link_order)
10042 asection *o = lo->u.indirect.section;
10044 if ((o->size % bed->s->sizeof_rela) == 0)
10046 if ((o->size % bed->s->sizeof_rel) == 0)
10047 /* Section size is divisible by both rel and rela sizes.
10048 It is of no help to us. */
10050 else
10052 /* Section size is only divisible by rela. */
10053 if (use_rela_initialised && !use_rela)
10055 _bfd_error_handler (_("%pB: unable to sort relocs - "
10056 "they are in more than one size"),
10057 abfd);
10058 bfd_set_error (bfd_error_invalid_operation);
10059 return 0;
10061 else
10063 use_rela = true;
10064 use_rela_initialised = true;
10068 else if ((o->size % bed->s->sizeof_rel) == 0)
10070 /* Section size is only divisible by rel. */
10071 if (use_rela_initialised && use_rela)
10073 _bfd_error_handler (_("%pB: unable to sort relocs - "
10074 "they are in more than one size"),
10075 abfd);
10076 bfd_set_error (bfd_error_invalid_operation);
10077 return 0;
10079 else
10081 use_rela = false;
10082 use_rela_initialised = true;
10085 else
10087 /* The section size is not divisible by either -
10088 something is wrong. */
10089 _bfd_error_handler (_("%pB: unable to sort relocs - "
10090 "they are of an unknown size"), abfd);
10091 bfd_set_error (bfd_error_invalid_operation);
10092 return 0;
10096 if (! use_rela_initialised)
10097 /* Make a guess. */
10098 use_rela = true;
10100 else if (rela_dyn != NULL && rela_dyn->size > 0)
10101 use_rela = true;
10102 else if (rel_dyn != NULL && rel_dyn->size > 0)
10103 use_rela = false;
10104 else
10105 return 0;
10107 if (use_rela)
10109 dynamic_relocs = rela_dyn;
10110 ext_size = bed->s->sizeof_rela;
10111 swap_in = bed->s->swap_reloca_in;
10112 swap_out = bed->s->swap_reloca_out;
10114 else
10116 dynamic_relocs = rel_dyn;
10117 ext_size = bed->s->sizeof_rel;
10118 swap_in = bed->s->swap_reloc_in;
10119 swap_out = bed->s->swap_reloc_out;
10122 size = 0;
10123 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
10124 if (lo->type == bfd_indirect_link_order)
10125 size += lo->u.indirect.section->size;
10127 if (size != dynamic_relocs->size)
10128 return 0;
10130 sort_elt = (sizeof (struct elf_link_sort_rela)
10131 + (i2e - 1) * sizeof (Elf_Internal_Rela));
10133 count = dynamic_relocs->size / ext_size;
10134 if (count == 0)
10135 return 0;
10136 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
10138 if (sort == NULL)
10140 (*info->callbacks->warning)
10141 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
10142 return 0;
10145 if (bed->s->arch_size == 32)
10146 r_sym_mask = ~(bfd_vma) 0xff;
10147 else
10148 r_sym_mask = ~(bfd_vma) 0xffffffff;
10150 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
10151 if (lo->type == bfd_indirect_link_order)
10153 bfd_byte *erel, *erelend;
10154 asection *o = lo->u.indirect.section;
10156 if (o->contents == NULL && o->size != 0)
10158 /* This is a reloc section that is being handled as a normal
10159 section. See bfd_section_from_shdr. We can't combine
10160 relocs in this case. */
10161 free (sort);
10162 return 0;
10164 erel = o->contents;
10165 erelend = o->contents + o->size;
10166 p = sort + o->output_offset * opb / ext_size * sort_elt;
10168 while (erel < erelend)
10170 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
10172 (*swap_in) (abfd, erel, s->rela);
10173 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
10174 s->u.sym_mask = r_sym_mask;
10175 p += sort_elt;
10176 erel += ext_size;
10180 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
10182 for (i = 0, p = sort; i < count; i++, p += sort_elt)
10184 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
10185 if (s->type != reloc_class_relative)
10186 break;
10188 ret = i;
10189 s_non_relative = p;
10191 sq = (struct elf_link_sort_rela *) s_non_relative;
10192 for (; i < count; i++, p += sort_elt)
10194 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
10195 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
10196 sq = sp;
10197 sp->u.offset = sq->rela->r_offset;
10200 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
10202 struct elf_link_hash_table *htab = elf_hash_table (info);
10203 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
10205 /* We have plt relocs in .rela.dyn. */
10206 sq = (struct elf_link_sort_rela *) sort;
10207 for (i = 0; i < count; i++)
10208 if (sq[count - i - 1].type != reloc_class_plt)
10209 break;
10210 if (i != 0 && htab->srelplt->size == i * ext_size)
10212 struct bfd_link_order **plo;
10213 /* Put srelplt link_order last. This is so the output_offset
10214 set in the next loop is correct for DT_JMPREL. */
10215 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
10216 if ((*plo)->type == bfd_indirect_link_order
10217 && (*plo)->u.indirect.section == htab->srelplt)
10219 lo = *plo;
10220 *plo = lo->next;
10222 else
10223 plo = &(*plo)->next;
10224 *plo = lo;
10225 lo->next = NULL;
10226 dynamic_relocs->map_tail.link_order = lo;
10230 p = sort;
10231 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
10232 if (lo->type == bfd_indirect_link_order)
10234 bfd_byte *erel, *erelend;
10235 asection *o = lo->u.indirect.section;
10237 erel = o->contents;
10238 erelend = o->contents + o->size;
10239 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
10240 while (erel < erelend)
10242 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
10243 (*swap_out) (abfd, s->rela, erel);
10244 p += sort_elt;
10245 erel += ext_size;
10249 free (sort);
10250 *psec = dynamic_relocs;
10251 return ret;
10254 /* Add a symbol to the output symbol string table. */
10256 static int
10257 elf_link_output_symstrtab (void *finf,
10258 const char *name,
10259 Elf_Internal_Sym *elfsym,
10260 asection *input_sec,
10261 struct elf_link_hash_entry *h)
10263 struct elf_final_link_info *flinfo = finf;
10264 int (*output_symbol_hook)
10265 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
10266 struct elf_link_hash_entry *);
10267 struct elf_link_hash_table *hash_table;
10268 const struct elf_backend_data *bed;
10269 bfd_size_type strtabsize;
10271 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
10273 bed = get_elf_backend_data (flinfo->output_bfd);
10274 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
10275 if (output_symbol_hook != NULL)
10277 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
10278 if (ret != 1)
10279 return ret;
10282 if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC)
10283 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
10284 if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE)
10285 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique;
10287 if (name == NULL || *name == '\0')
10288 elfsym->st_name = (unsigned long) -1;
10289 else
10291 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
10292 to get the final offset for st_name. */
10293 char *versioned_name = (char *) name;
10294 if (h != NULL)
10296 if (h->versioned == versioned && h->def_dynamic)
10298 /* Keep only one '@' for versioned symbols defined in
10299 shared objects. */
10300 char *version = strrchr (name, ELF_VER_CHR);
10301 char *base_end = strchr (name, ELF_VER_CHR);
10302 if (version != base_end)
10304 size_t base_len;
10305 size_t len = strlen (name);
10306 versioned_name = bfd_alloc (flinfo->output_bfd, len);
10307 if (versioned_name == NULL)
10308 return 0;
10309 base_len = base_end - name;
10310 memcpy (versioned_name, name, base_len);
10311 memcpy (versioned_name + base_len, version,
10312 len - base_len);
10316 else if (flinfo->info->unique_symbol
10317 && ELF_ST_BIND (elfsym->st_info) == STB_LOCAL)
10319 struct local_hash_entry *lh;
10320 size_t count_len;
10321 size_t base_len;
10322 char buf[30];
10323 switch (ELF_ST_TYPE (elfsym->st_info))
10325 case STT_FILE:
10326 case STT_SECTION:
10327 break;
10328 default:
10329 lh = (struct local_hash_entry *) bfd_hash_lookup
10330 (&flinfo->local_hash_table, name, true, false);
10331 if (lh == NULL)
10332 return 0;
10333 /* Always append ".COUNT" to local symbols to avoid
10334 potential conflicts with local symbol "XXX.COUNT". */
10335 sprintf (buf, "%lx", lh->count);
10336 base_len = lh->size;
10337 if (!base_len)
10339 base_len = strlen (name);
10340 lh->size = base_len;
10342 count_len = strlen (buf);
10343 versioned_name = bfd_alloc (flinfo->output_bfd,
10344 base_len + count_len + 2);
10345 if (versioned_name == NULL)
10346 return 0;
10347 memcpy (versioned_name, name, base_len);
10348 versioned_name[base_len] = '.';
10349 memcpy (versioned_name + base_len + 1, buf,
10350 count_len + 1);
10351 lh->count++;
10352 break;
10355 elfsym->st_name
10356 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
10357 versioned_name, false);
10358 if (elfsym->st_name == (unsigned long) -1)
10359 return 0;
10362 hash_table = elf_hash_table (flinfo->info);
10363 strtabsize = hash_table->strtabsize;
10364 if (strtabsize <= flinfo->output_bfd->symcount)
10366 strtabsize += strtabsize;
10367 hash_table->strtabsize = strtabsize;
10368 strtabsize *= sizeof (*hash_table->strtab);
10369 hash_table->strtab
10370 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
10371 strtabsize);
10372 if (hash_table->strtab == NULL)
10373 return 0;
10375 hash_table->strtab[flinfo->output_bfd->symcount].sym = *elfsym;
10376 hash_table->strtab[flinfo->output_bfd->symcount].dest_index
10377 = flinfo->output_bfd->symcount;
10378 flinfo->output_bfd->symcount += 1;
10380 return 1;
10383 /* Swap symbols out to the symbol table and flush the output symbols to
10384 the file. */
10386 static bool
10387 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
10389 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
10390 size_t amt;
10391 size_t i;
10392 const struct elf_backend_data *bed;
10393 bfd_byte *symbuf;
10394 Elf_Internal_Shdr *hdr;
10395 file_ptr pos;
10396 bool ret;
10398 if (flinfo->output_bfd->symcount == 0)
10399 return true;
10401 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
10403 bed = get_elf_backend_data (flinfo->output_bfd);
10405 amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount;
10406 symbuf = (bfd_byte *) bfd_malloc (amt);
10407 if (symbuf == NULL)
10408 return false;
10410 if (flinfo->symshndxbuf)
10412 amt = sizeof (Elf_External_Sym_Shndx);
10413 amt *= bfd_get_symcount (flinfo->output_bfd);
10414 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
10415 if (flinfo->symshndxbuf == NULL)
10417 free (symbuf);
10418 return false;
10422 /* Now swap out the symbols. */
10423 for (i = 0; i < flinfo->output_bfd->symcount; i++)
10425 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
10426 if (elfsym->sym.st_name == (unsigned long) -1)
10427 elfsym->sym.st_name = 0;
10428 else
10429 elfsym->sym.st_name
10430 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
10431 elfsym->sym.st_name);
10433 /* Inform the linker of the addition of this symbol. */
10435 if (flinfo->info->callbacks->ctf_new_symbol)
10436 flinfo->info->callbacks->ctf_new_symbol (elfsym->dest_index,
10437 &elfsym->sym);
10439 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
10440 ((bfd_byte *) symbuf
10441 + (elfsym->dest_index
10442 * bed->s->sizeof_sym)),
10443 NPTR_ADD (flinfo->symshndxbuf,
10444 elfsym->dest_index));
10447 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
10448 pos = hdr->sh_offset + hdr->sh_size;
10449 amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount;
10450 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
10451 && bfd_write (symbuf, amt, flinfo->output_bfd) == amt)
10453 hdr->sh_size += amt;
10454 ret = true;
10456 else
10457 ret = false;
10459 free (symbuf);
10460 return ret;
10463 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
10465 static bool
10466 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
10468 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
10469 && sym->st_shndx < SHN_LORESERVE)
10471 /* The gABI doesn't support dynamic symbols in output sections
10472 beyond 64k. */
10473 _bfd_error_handler
10474 /* xgettext:c-format */
10475 (_("%pB: too many sections: %d (>= %d)"),
10476 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
10477 bfd_set_error (bfd_error_nonrepresentable_section);
10478 return false;
10480 return true;
10483 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
10484 allowing an unsatisfied unversioned symbol in the DSO to match a
10485 versioned symbol that would normally require an explicit version.
10486 We also handle the case that a DSO references a hidden symbol
10487 which may be satisfied by a versioned symbol in another DSO. */
10489 static bool
10490 elf_link_check_versioned_symbol (struct bfd_link_info *info,
10491 const struct elf_backend_data *bed,
10492 struct elf_link_hash_entry *h)
10494 bfd *abfd;
10495 struct elf_link_loaded_list *loaded;
10497 if (!is_elf_hash_table (info->hash))
10498 return false;
10500 /* Check indirect symbol. */
10501 while (h->root.type == bfd_link_hash_indirect)
10502 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10504 switch (h->root.type)
10506 default:
10507 abfd = NULL;
10508 break;
10510 case bfd_link_hash_undefined:
10511 case bfd_link_hash_undefweak:
10512 abfd = h->root.u.undef.abfd;
10513 if (abfd == NULL
10514 || (abfd->flags & DYNAMIC) == 0
10515 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
10516 return false;
10517 break;
10519 case bfd_link_hash_defined:
10520 case bfd_link_hash_defweak:
10521 abfd = h->root.u.def.section->owner;
10522 break;
10524 case bfd_link_hash_common:
10525 abfd = h->root.u.c.p->section->owner;
10526 break;
10528 BFD_ASSERT (abfd != NULL);
10530 for (loaded = elf_hash_table (info)->dyn_loaded;
10531 loaded != NULL;
10532 loaded = loaded->next)
10534 bfd *input;
10535 Elf_Internal_Shdr *hdr;
10536 size_t symcount;
10537 size_t extsymcount;
10538 size_t extsymoff;
10539 Elf_Internal_Shdr *versymhdr;
10540 Elf_Internal_Sym *isym;
10541 Elf_Internal_Sym *isymend;
10542 Elf_Internal_Sym *isymbuf;
10543 Elf_External_Versym *ever;
10544 Elf_External_Versym *extversym;
10546 input = loaded->abfd;
10548 /* We check each DSO for a possible hidden versioned definition. */
10549 if (input == abfd
10550 || elf_dynversym (input) == 0)
10551 continue;
10553 hdr = &elf_tdata (input)->dynsymtab_hdr;
10555 symcount = hdr->sh_size / bed->s->sizeof_sym;
10556 if (elf_bad_symtab (input))
10558 extsymcount = symcount;
10559 extsymoff = 0;
10561 else
10563 extsymcount = symcount - hdr->sh_info;
10564 extsymoff = hdr->sh_info;
10567 if (extsymcount == 0)
10568 continue;
10570 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
10571 NULL, NULL, NULL);
10572 if (isymbuf == NULL)
10573 return false;
10575 /* Read in any version definitions. */
10576 versymhdr = &elf_tdata (input)->dynversym_hdr;
10577 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
10578 || (extversym = (Elf_External_Versym *)
10579 _bfd_malloc_and_read (input, versymhdr->sh_size,
10580 versymhdr->sh_size)) == NULL)
10582 free (isymbuf);
10583 return false;
10586 ever = extversym + extsymoff;
10587 isymend = isymbuf + extsymcount;
10588 for (isym = isymbuf; isym < isymend; isym++, ever++)
10590 const char *name;
10591 Elf_Internal_Versym iver;
10592 unsigned short version_index;
10594 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
10595 || isym->st_shndx == SHN_UNDEF)
10596 continue;
10598 name = bfd_elf_string_from_elf_section (input,
10599 hdr->sh_link,
10600 isym->st_name);
10601 if (strcmp (name, h->root.root.string) != 0)
10602 continue;
10604 _bfd_elf_swap_versym_in (input, ever, &iver);
10606 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
10607 && !(h->def_regular
10608 && h->forced_local))
10610 /* If we have a non-hidden versioned sym, then it should
10611 have provided a definition for the undefined sym unless
10612 it is defined in a non-shared object and forced local.
10614 abort ();
10617 version_index = iver.vs_vers & VERSYM_VERSION;
10618 if (version_index == 1 || version_index == 2)
10620 /* This is the base or first version. We can use it. */
10621 free (extversym);
10622 free (isymbuf);
10623 return true;
10627 free (extversym);
10628 free (isymbuf);
10631 return false;
10634 /* Convert ELF common symbol TYPE. */
10636 static int
10637 elf_link_convert_common_type (struct bfd_link_info *info, int type)
10639 /* Commom symbol can only appear in relocatable link. */
10640 if (!bfd_link_relocatable (info))
10641 abort ();
10642 switch (info->elf_stt_common)
10644 case unchanged:
10645 break;
10646 case elf_stt_common:
10647 type = STT_COMMON;
10648 break;
10649 case no_elf_stt_common:
10650 type = STT_OBJECT;
10651 break;
10653 return type;
10656 /* Add an external symbol to the symbol table. This is called from
10657 the hash table traversal routine. When generating a shared object,
10658 we go through the symbol table twice. The first time we output
10659 anything that might have been forced to local scope in a version
10660 script. The second time we output the symbols that are still
10661 global symbols. */
10663 static bool
10664 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
10666 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
10667 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
10668 struct elf_final_link_info *flinfo = eoinfo->flinfo;
10669 bool strip;
10670 Elf_Internal_Sym sym;
10671 asection *input_sec;
10672 const struct elf_backend_data *bed;
10673 long indx;
10674 int ret;
10675 unsigned int type;
10677 if (h->root.type == bfd_link_hash_warning)
10679 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10680 if (h->root.type == bfd_link_hash_new)
10681 return true;
10684 /* Decide whether to output this symbol in this pass. */
10685 if (eoinfo->localsyms)
10687 if (!h->forced_local)
10688 return true;
10690 else
10692 if (h->forced_local)
10693 return true;
10696 bed = get_elf_backend_data (flinfo->output_bfd);
10698 if (h->root.type == bfd_link_hash_undefined)
10700 /* If we have an undefined symbol reference here then it must have
10701 come from a shared library that is being linked in. (Undefined
10702 references in regular files have already been handled unless
10703 they are in unreferenced sections which are removed by garbage
10704 collection). */
10705 bool ignore_undef = false;
10707 /* Some symbols may be special in that the fact that they're
10708 undefined can be safely ignored - let backend determine that. */
10709 if (bed->elf_backend_ignore_undef_symbol)
10710 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
10712 /* If we are reporting errors for this situation then do so now. */
10713 if (!ignore_undef
10714 && h->ref_dynamic_nonweak
10715 && (!h->ref_regular || flinfo->info->gc_sections)
10716 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
10717 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
10719 flinfo->info->callbacks->undefined_symbol
10720 (flinfo->info, h->root.root.string,
10721 h->ref_regular ? NULL : h->root.u.undef.abfd, NULL, 0,
10722 flinfo->info->unresolved_syms_in_shared_libs == RM_DIAGNOSE
10723 && !flinfo->info->warn_unresolved_syms);
10726 /* Strip a global symbol defined in a discarded section. */
10727 if (h->indx == -3)
10728 return true;
10731 /* We should also warn if a forced local symbol is referenced from
10732 shared libraries. */
10733 if (bfd_link_executable (flinfo->info)
10734 && h->forced_local
10735 && h->ref_dynamic
10736 && h->def_regular
10737 && !h->dynamic_def
10738 && h->ref_dynamic_nonweak
10739 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
10741 bfd *def_bfd;
10742 const char *msg;
10743 struct elf_link_hash_entry *hi = h;
10745 /* Check indirect symbol. */
10746 while (hi->root.type == bfd_link_hash_indirect)
10747 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
10749 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
10750 /* xgettext:c-format */
10751 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
10752 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
10753 /* xgettext:c-format */
10754 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
10755 else
10756 /* xgettext:c-format */
10757 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
10758 def_bfd = flinfo->output_bfd;
10759 if (hi->root.u.def.section != bfd_abs_section_ptr)
10760 def_bfd = hi->root.u.def.section->owner;
10761 _bfd_error_handler (msg, flinfo->output_bfd,
10762 h->root.root.string, def_bfd);
10763 bfd_set_error (bfd_error_bad_value);
10764 eoinfo->failed = true;
10765 return false;
10768 /* We don't want to output symbols that have never been mentioned by
10769 a regular file, or that we have been told to strip. However, if
10770 h->indx is set to -2, the symbol is used by a reloc and we must
10771 output it. */
10772 strip = false;
10773 if (h->indx == -2)
10775 else if ((h->def_dynamic
10776 || h->ref_dynamic
10777 || h->root.type == bfd_link_hash_new)
10778 && !h->def_regular
10779 && !h->ref_regular)
10780 strip = true;
10781 else if (flinfo->info->strip == strip_all)
10782 strip = true;
10783 else if (flinfo->info->strip == strip_some
10784 && bfd_hash_lookup (flinfo->info->keep_hash,
10785 h->root.root.string, false, false) == NULL)
10786 strip = true;
10787 else if ((h->root.type == bfd_link_hash_defined
10788 || h->root.type == bfd_link_hash_defweak)
10789 && ((flinfo->info->strip_discarded
10790 && discarded_section (h->root.u.def.section))
10791 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
10792 && h->root.u.def.section->owner != NULL
10793 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
10794 strip = true;
10795 else if ((h->root.type == bfd_link_hash_undefined
10796 || h->root.type == bfd_link_hash_undefweak)
10797 && h->root.u.undef.abfd != NULL
10798 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
10799 strip = true;
10801 /* Remember if this symbol should be stripped. */
10802 bool should_strip = strip;
10804 /* Strip undefined weak symbols link if they don't have relocation. */
10805 if (!strip)
10806 strip = !h->has_reloc && h->root.type == bfd_link_hash_undefweak;
10808 type = h->type;
10810 /* If we're stripping it, and it's not a dynamic symbol, there's
10811 nothing else to do. However, if it is a forced local symbol or
10812 an ifunc symbol we need to give the backend finish_dynamic_symbol
10813 function a chance to make it dynamic. */
10814 if (strip
10815 && h->dynindx == -1
10816 && type != STT_GNU_IFUNC
10817 && !h->forced_local)
10818 return true;
10820 sym.st_value = 0;
10821 sym.st_size = h->size;
10822 sym.st_other = h->other;
10823 switch (h->root.type)
10825 default:
10826 case bfd_link_hash_new:
10827 case bfd_link_hash_warning:
10828 abort ();
10829 return false;
10831 case bfd_link_hash_undefined:
10832 case bfd_link_hash_undefweak:
10833 input_sec = bfd_und_section_ptr;
10834 sym.st_shndx = SHN_UNDEF;
10835 break;
10837 case bfd_link_hash_defined:
10838 case bfd_link_hash_defweak:
10840 input_sec = h->root.u.def.section;
10841 if (input_sec->output_section != NULL)
10843 sym.st_shndx =
10844 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
10845 input_sec->output_section);
10846 if (sym.st_shndx == SHN_BAD)
10848 _bfd_error_handler
10849 /* xgettext:c-format */
10850 (_("%pB: could not find output section %pA for input section %pA"),
10851 flinfo->output_bfd, input_sec->output_section, input_sec);
10852 bfd_set_error (bfd_error_nonrepresentable_section);
10853 eoinfo->failed = true;
10854 return false;
10857 /* ELF symbols in relocatable files are section relative,
10858 but in nonrelocatable files they are virtual
10859 addresses. */
10860 sym.st_value = h->root.u.def.value + input_sec->output_offset;
10861 if (!bfd_link_relocatable (flinfo->info))
10863 sym.st_value += input_sec->output_section->vma;
10864 if (h->type == STT_TLS)
10866 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
10867 if (tls_sec != NULL)
10868 sym.st_value -= tls_sec->vma;
10872 else
10874 BFD_ASSERT (input_sec->owner == NULL
10875 || (input_sec->owner->flags & DYNAMIC) != 0);
10876 sym.st_shndx = SHN_UNDEF;
10877 input_sec = bfd_und_section_ptr;
10880 break;
10882 case bfd_link_hash_common:
10883 input_sec = h->root.u.c.p->section;
10884 sym.st_shndx = bed->common_section_index (input_sec);
10885 sym.st_value = 1 << h->root.u.c.p->alignment_power;
10886 break;
10888 case bfd_link_hash_indirect:
10889 /* These symbols are created by symbol versioning. They point
10890 to the decorated version of the name. For example, if the
10891 symbol foo@@GNU_1.2 is the default, which should be used when
10892 foo is used with no version, then we add an indirect symbol
10893 foo which points to foo@@GNU_1.2. We ignore these symbols,
10894 since the indirected symbol is already in the hash table. */
10895 return true;
10898 if (type == STT_COMMON || type == STT_OBJECT)
10899 switch (h->root.type)
10901 case bfd_link_hash_common:
10902 type = elf_link_convert_common_type (flinfo->info, type);
10903 break;
10904 case bfd_link_hash_defined:
10905 case bfd_link_hash_defweak:
10906 if (bed->common_definition (&sym))
10907 type = elf_link_convert_common_type (flinfo->info, type);
10908 else
10909 type = STT_OBJECT;
10910 break;
10911 case bfd_link_hash_undefined:
10912 case bfd_link_hash_undefweak:
10913 break;
10914 default:
10915 abort ();
10918 if (h->forced_local)
10920 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
10921 /* Turn off visibility on local symbol. */
10922 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
10924 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10925 else if (h->unique_global && h->def_regular)
10926 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
10927 else if (h->root.type == bfd_link_hash_undefweak
10928 || h->root.type == bfd_link_hash_defweak)
10929 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
10930 else
10931 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
10932 sym.st_target_internal = h->target_internal;
10934 /* Give the processor backend a chance to tweak the symbol value,
10935 and also to finish up anything that needs to be done for this
10936 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
10937 forced local syms when non-shared is due to a historical quirk.
10938 STT_GNU_IFUNC symbol must go through PLT. */
10939 if ((h->type == STT_GNU_IFUNC
10940 && h->def_regular
10941 && !bfd_link_relocatable (flinfo->info))
10942 || ((h->dynindx != -1
10943 || h->forced_local)
10944 && ((bfd_link_pic (flinfo->info)
10945 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
10946 || h->root.type != bfd_link_hash_undefweak))
10947 || !h->forced_local)
10948 && elf_hash_table (flinfo->info)->dynamic_sections_created))
10950 if (! ((*bed->elf_backend_finish_dynamic_symbol)
10951 (flinfo->output_bfd, flinfo->info, h, &sym)))
10953 eoinfo->failed = true;
10954 return false;
10956 /* If a symbol is in the dynamic symbol table and isn't a
10957 should-strip symbol, also keep it in the symbol table. */
10958 if (!should_strip)
10959 strip = false;
10962 /* If we are marking the symbol as undefined, and there are no
10963 non-weak references to this symbol from a regular object, then
10964 mark the symbol as weak undefined; if there are non-weak
10965 references, mark the symbol as strong. We can't do this earlier,
10966 because it might not be marked as undefined until the
10967 finish_dynamic_symbol routine gets through with it. */
10968 if (sym.st_shndx == SHN_UNDEF
10969 && h->ref_regular
10970 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
10971 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
10973 int bindtype;
10974 type = ELF_ST_TYPE (sym.st_info);
10976 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10977 if (type == STT_GNU_IFUNC)
10978 type = STT_FUNC;
10980 if (h->ref_regular_nonweak)
10981 bindtype = STB_GLOBAL;
10982 else
10983 bindtype = STB_WEAK;
10984 sym.st_info = ELF_ST_INFO (bindtype, type);
10987 /* If this is a symbol defined in a dynamic library, don't use the
10988 symbol size from the dynamic library. Relinking an executable
10989 against a new library may introduce gratuitous changes in the
10990 executable's symbols if we keep the size. */
10991 if (sym.st_shndx == SHN_UNDEF
10992 && !h->def_regular
10993 && h->def_dynamic)
10994 sym.st_size = 0;
10996 /* If a non-weak symbol with non-default visibility is not defined
10997 locally, it is a fatal error. */
10998 if (!bfd_link_relocatable (flinfo->info)
10999 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
11000 && ELF_ST_BIND (sym.st_info) != STB_WEAK
11001 && h->root.type == bfd_link_hash_undefined
11002 && !h->def_regular)
11004 const char *msg;
11006 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
11007 /* xgettext:c-format */
11008 msg = _("%pB: protected symbol `%s' isn't defined");
11009 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
11010 /* xgettext:c-format */
11011 msg = _("%pB: internal symbol `%s' isn't defined");
11012 else
11013 /* xgettext:c-format */
11014 msg = _("%pB: hidden symbol `%s' isn't defined");
11015 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
11016 bfd_set_error (bfd_error_bad_value);
11017 eoinfo->failed = true;
11018 return false;
11021 /* If this symbol should be put in the .dynsym section, then put it
11022 there now. We already know the symbol index. We also fill in
11023 the entry in the .hash section. */
11024 if (h->dynindx != -1
11025 && elf_hash_table (flinfo->info)->dynamic_sections_created
11026 && elf_hash_table (flinfo->info)->dynsym != NULL
11027 && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
11029 bfd_byte *esym;
11031 /* Since there is no version information in the dynamic string,
11032 if there is no version info in symbol version section, we will
11033 have a run-time problem if not linking executable, referenced
11034 by shared library, or not bound locally. */
11035 if (h->verinfo.verdef == NULL
11036 && (!bfd_link_executable (flinfo->info)
11037 || h->ref_dynamic
11038 || !h->def_regular))
11040 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
11042 if (p && p [1] != '\0')
11044 _bfd_error_handler
11045 /* xgettext:c-format */
11046 (_("%pB: no symbol version section for versioned symbol `%s'"),
11047 flinfo->output_bfd, h->root.root.string);
11048 eoinfo->failed = true;
11049 return false;
11053 sym.st_name = h->dynstr_index;
11054 esym = (elf_hash_table (flinfo->info)->dynsym->contents
11055 + h->dynindx * bed->s->sizeof_sym);
11056 if (!check_dynsym (flinfo->output_bfd, &sym))
11058 eoinfo->failed = true;
11059 return false;
11062 /* Inform the linker of the addition of this symbol. */
11064 if (flinfo->info->callbacks->ctf_new_dynsym)
11065 flinfo->info->callbacks->ctf_new_dynsym (h->dynindx, &sym);
11067 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
11069 if (flinfo->hash_sec != NULL)
11071 size_t hash_entry_size;
11072 bfd_byte *bucketpos;
11073 bfd_vma chain;
11074 size_t bucketcount;
11075 size_t bucket;
11077 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
11078 bucket = h->u.elf_hash_value % bucketcount;
11080 hash_entry_size
11081 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
11082 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
11083 + (bucket + 2) * hash_entry_size);
11084 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
11085 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
11086 bucketpos);
11087 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
11088 ((bfd_byte *) flinfo->hash_sec->contents
11089 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
11092 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
11094 Elf_Internal_Versym iversym;
11095 Elf_External_Versym *eversym;
11097 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
11099 if (h->verinfo.verdef == NULL
11100 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
11101 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
11102 iversym.vs_vers = 1;
11103 else
11104 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
11106 else
11108 if (h->verinfo.vertree == NULL)
11109 iversym.vs_vers = 1;
11110 else
11111 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
11112 if (flinfo->info->create_default_symver)
11113 iversym.vs_vers++;
11116 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
11117 defined locally. */
11118 if (h->versioned == versioned_hidden && h->def_regular)
11119 iversym.vs_vers |= VERSYM_HIDDEN;
11121 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
11122 eversym += h->dynindx;
11123 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
11127 /* If the symbol is undefined, and we didn't output it to .dynsym,
11128 strip it from .symtab too. Obviously we can't do this for
11129 relocatable output or when needed for --emit-relocs. */
11130 else if (input_sec == bfd_und_section_ptr
11131 && h->indx != -2
11132 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
11133 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
11134 && !bfd_link_relocatable (flinfo->info))
11135 return true;
11137 /* Also strip others that we couldn't earlier due to dynamic symbol
11138 processing. */
11139 if (strip)
11140 return true;
11141 if ((input_sec->flags & SEC_EXCLUDE) != 0)
11142 return true;
11144 /* Output a FILE symbol so that following locals are not associated
11145 with the wrong input file. We need one for forced local symbols
11146 if we've seen more than one FILE symbol or when we have exactly
11147 one FILE symbol but global symbols are present in a file other
11148 than the one with the FILE symbol. We also need one if linker
11149 defined symbols are present. In practice these conditions are
11150 always met, so just emit the FILE symbol unconditionally. */
11151 if (eoinfo->localsyms
11152 && !eoinfo->file_sym_done
11153 && eoinfo->flinfo->filesym_count != 0)
11155 Elf_Internal_Sym fsym;
11157 memset (&fsym, 0, sizeof (fsym));
11158 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
11159 fsym.st_shndx = SHN_ABS;
11160 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
11161 bfd_und_section_ptr, NULL))
11162 return false;
11164 eoinfo->file_sym_done = true;
11167 indx = bfd_get_symcount (flinfo->output_bfd);
11168 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
11169 input_sec, h);
11170 if (ret == 0)
11172 eoinfo->failed = true;
11173 return false;
11175 else if (ret == 1)
11176 h->indx = indx;
11177 else if (h->indx == -2)
11178 abort();
11180 return true;
11183 /* Return TRUE if special handling is done for relocs in SEC against
11184 symbols defined in discarded sections. */
11186 static bool
11187 elf_section_ignore_discarded_relocs (asection *sec)
11189 const struct elf_backend_data *bed;
11191 switch (sec->sec_info_type)
11193 case SEC_INFO_TYPE_STABS:
11194 case SEC_INFO_TYPE_EH_FRAME:
11195 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11196 case SEC_INFO_TYPE_SFRAME:
11197 return true;
11198 default:
11199 break;
11202 bed = get_elf_backend_data (sec->owner);
11203 if (bed->elf_backend_ignore_discarded_relocs != NULL
11204 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
11205 return true;
11207 return false;
11210 /* Return a mask saying how ld should treat relocations in SEC against
11211 symbols defined in discarded sections. If this function returns
11212 COMPLAIN set, ld will issue a warning message. If this function
11213 returns PRETEND set, and the discarded section was link-once and the
11214 same size as the kept link-once section, ld will pretend that the
11215 symbol was actually defined in the kept section. Otherwise ld will
11216 zero the reloc (at least that is the intent, but some cooperation by
11217 the target dependent code is needed, particularly for REL targets). */
11219 unsigned int
11220 _bfd_elf_default_action_discarded (asection *sec)
11222 const struct elf_backend_data *bed;
11223 bed = get_elf_backend_data (sec->owner);
11225 if (sec->flags & SEC_DEBUGGING)
11226 return PRETEND;
11228 if (strcmp (".eh_frame", sec->name) == 0)
11229 return 0;
11231 if (bed->elf_backend_can_make_multiple_eh_frame
11232 && strncmp (sec->name, ".eh_frame.", 10) == 0)
11233 return 0;
11235 if (strcmp (".sframe", sec->name) == 0)
11236 return 0;
11238 if (strcmp (".gcc_except_table", sec->name) == 0)
11239 return 0;
11241 return COMPLAIN | PRETEND;
11244 /* Find a match between a section and a member of a section group. */
11246 static asection *
11247 match_group_member (asection *sec, asection *group,
11248 struct bfd_link_info *info)
11250 asection *first = elf_next_in_group (group);
11251 asection *s = first;
11253 while (s != NULL)
11255 if (bfd_elf_match_symbols_in_sections (s, sec, info))
11256 return s;
11258 s = elf_next_in_group (s);
11259 if (s == first)
11260 break;
11263 return NULL;
11266 /* Check if the kept section of a discarded section SEC can be used
11267 to replace it. Return the replacement if it is OK. Otherwise return
11268 NULL. */
11270 asection *
11271 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
11273 asection *kept;
11275 kept = sec->kept_section;
11276 if (kept != NULL)
11278 if ((kept->flags & SEC_GROUP) != 0)
11279 kept = match_group_member (sec, kept, info);
11280 if (kept != NULL)
11282 if ((sec->rawsize != 0 ? sec->rawsize : sec->size)
11283 != (kept->rawsize != 0 ? kept->rawsize : kept->size))
11284 kept = NULL;
11285 else
11287 /* Get the real kept section. */
11288 asection *next;
11289 for (next = kept->kept_section;
11290 next != NULL;
11291 next = next->kept_section)
11292 kept = next;
11295 sec->kept_section = kept;
11297 return kept;
11300 /* Link an input file into the linker output file. This function
11301 handles all the sections and relocations of the input file at once.
11302 This is so that we only have to read the local symbols once, and
11303 don't have to keep them in memory. */
11305 static bool
11306 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
11308 int (*relocate_section)
11309 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
11310 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
11311 bfd *output_bfd;
11312 Elf_Internal_Shdr *symtab_hdr;
11313 size_t locsymcount;
11314 size_t extsymoff;
11315 Elf_Internal_Sym *isymbuf;
11316 Elf_Internal_Sym *isym;
11317 Elf_Internal_Sym *isymend;
11318 long *pindex;
11319 asection **ppsection;
11320 asection *o;
11321 const struct elf_backend_data *bed;
11322 struct elf_link_hash_entry **sym_hashes;
11323 bfd_size_type address_size;
11324 bfd_vma r_type_mask;
11325 int r_sym_shift;
11326 bool have_file_sym = false;
11328 output_bfd = flinfo->output_bfd;
11329 bed = get_elf_backend_data (output_bfd);
11330 relocate_section = bed->elf_backend_relocate_section;
11332 /* If this is a dynamic object, we don't want to do anything here:
11333 we don't want the local symbols, and we don't want the section
11334 contents. */
11335 if ((input_bfd->flags & DYNAMIC) != 0)
11336 return true;
11338 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
11339 if (elf_bad_symtab (input_bfd))
11341 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
11342 extsymoff = 0;
11344 else
11346 locsymcount = symtab_hdr->sh_info;
11347 extsymoff = symtab_hdr->sh_info;
11350 /* Enable GNU OSABI features in the output BFD that are used in the input
11351 BFD. */
11352 if (bed->elf_osabi == ELFOSABI_NONE
11353 || bed->elf_osabi == ELFOSABI_GNU
11354 || bed->elf_osabi == ELFOSABI_FREEBSD)
11355 elf_tdata (output_bfd)->has_gnu_osabi
11356 |= (elf_tdata (input_bfd)->has_gnu_osabi
11357 & (bfd_link_relocatable (flinfo->info)
11358 ? -1 : ~elf_gnu_osabi_retain));
11360 /* Read the local symbols. */
11361 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
11362 if (isymbuf == NULL && locsymcount != 0)
11364 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
11365 flinfo->internal_syms,
11366 flinfo->external_syms,
11367 flinfo->locsym_shndx);
11368 if (isymbuf == NULL)
11369 return false;
11372 /* Find local symbol sections and adjust values of symbols in
11373 SEC_MERGE sections. Write out those local symbols we know are
11374 going into the output file. */
11375 isymend = PTR_ADD (isymbuf, locsymcount);
11376 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
11377 isym < isymend;
11378 isym++, pindex++, ppsection++)
11380 asection *isec;
11381 const char *name;
11382 Elf_Internal_Sym osym;
11383 long indx;
11384 int ret;
11386 *pindex = -1;
11388 if (elf_bad_symtab (input_bfd))
11390 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
11392 *ppsection = NULL;
11393 continue;
11397 if (isym->st_shndx == SHN_UNDEF)
11398 isec = bfd_und_section_ptr;
11399 else if (isym->st_shndx == SHN_ABS)
11400 isec = bfd_abs_section_ptr;
11401 else if (isym->st_shndx == SHN_COMMON)
11402 isec = bfd_com_section_ptr;
11403 else
11405 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
11406 if (isec == NULL)
11408 /* Don't attempt to output symbols with st_shnx in the
11409 reserved range other than SHN_ABS and SHN_COMMON. */
11410 isec = bfd_und_section_ptr;
11412 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
11413 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
11414 isym->st_value =
11415 _bfd_merged_section_offset (output_bfd, &isec,
11416 elf_section_data (isec)->sec_info,
11417 isym->st_value);
11420 *ppsection = isec;
11422 /* Don't output the first, undefined, symbol. In fact, don't
11423 output any undefined local symbol. */
11424 if (isec == bfd_und_section_ptr)
11425 continue;
11427 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
11429 /* We never output section symbols. Instead, we use the
11430 section symbol of the corresponding section in the output
11431 file. */
11432 continue;
11435 /* If we are stripping all symbols, we don't want to output this
11436 one. */
11437 if (flinfo->info->strip == strip_all)
11438 continue;
11440 /* If we are discarding all local symbols, we don't want to
11441 output this one. If we are generating a relocatable output
11442 file, then some of the local symbols may be required by
11443 relocs; we output them below as we discover that they are
11444 needed. */
11445 if (flinfo->info->discard == discard_all)
11446 continue;
11448 /* If this symbol is defined in a section which we are
11449 discarding, we don't need to keep it. */
11450 if (isym->st_shndx < SHN_LORESERVE
11451 && (isec->output_section == NULL
11452 || bfd_section_removed_from_list (output_bfd,
11453 isec->output_section)))
11454 continue;
11456 /* Get the name of the symbol. */
11457 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
11458 isym->st_name);
11459 if (name == NULL)
11460 return false;
11462 /* See if we are discarding symbols with this name. */
11463 if ((flinfo->info->strip == strip_some
11464 && (bfd_hash_lookup (flinfo->info->keep_hash, name, false, false)
11465 == NULL))
11466 || (((flinfo->info->discard == discard_sec_merge
11467 && (isec->flags & SEC_MERGE)
11468 && !bfd_link_relocatable (flinfo->info))
11469 || flinfo->info->discard == discard_l)
11470 && bfd_is_local_label_name (input_bfd, name)))
11471 continue;
11473 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
11475 if (input_bfd->lto_output)
11476 /* -flto puts a temp file name here. This means builds
11477 are not reproducible. Discard the symbol. */
11478 continue;
11479 have_file_sym = true;
11480 flinfo->filesym_count += 1;
11482 if (!have_file_sym)
11484 /* In the absence of debug info, bfd_find_nearest_line uses
11485 FILE symbols to determine the source file for local
11486 function symbols. Provide a FILE symbol here if input
11487 files lack such, so that their symbols won't be
11488 associated with a previous input file. It's not the
11489 source file, but the best we can do. */
11490 const char *filename;
11491 have_file_sym = true;
11492 flinfo->filesym_count += 1;
11493 memset (&osym, 0, sizeof (osym));
11494 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
11495 osym.st_shndx = SHN_ABS;
11496 if (input_bfd->lto_output)
11497 filename = NULL;
11498 else
11499 filename = lbasename (bfd_get_filename (input_bfd));
11500 if (!elf_link_output_symstrtab (flinfo, filename, &osym,
11501 bfd_abs_section_ptr, NULL))
11502 return false;
11505 osym = *isym;
11507 /* Adjust the section index for the output file. */
11508 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11509 isec->output_section);
11510 if (osym.st_shndx == SHN_BAD)
11511 return false;
11513 /* ELF symbols in relocatable files are section relative, but
11514 in executable files they are virtual addresses. Note that
11515 this code assumes that all ELF sections have an associated
11516 BFD section with a reasonable value for output_offset; below
11517 we assume that they also have a reasonable value for
11518 output_section. Any special sections must be set up to meet
11519 these requirements. */
11520 osym.st_value += isec->output_offset;
11521 if (!bfd_link_relocatable (flinfo->info))
11523 osym.st_value += isec->output_section->vma;
11524 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
11526 /* STT_TLS symbols are relative to PT_TLS segment base. */
11527 if (elf_hash_table (flinfo->info)->tls_sec != NULL)
11528 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
11529 else
11530 osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
11531 STT_NOTYPE);
11535 indx = bfd_get_symcount (output_bfd);
11536 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
11537 if (ret == 0)
11538 return false;
11539 else if (ret == 1)
11540 *pindex = indx;
11543 if (bed->s->arch_size == 32)
11545 r_type_mask = 0xff;
11546 r_sym_shift = 8;
11547 address_size = 4;
11549 else
11551 r_type_mask = 0xffffffff;
11552 r_sym_shift = 32;
11553 address_size = 8;
11556 /* Relocate the contents of each section. */
11557 sym_hashes = elf_sym_hashes (input_bfd);
11558 for (o = input_bfd->sections; o != NULL; o = o->next)
11560 bfd_byte *contents;
11562 if (! o->linker_mark)
11564 /* This section was omitted from the link. */
11565 continue;
11568 if (!flinfo->info->resolve_section_groups
11569 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
11571 /* Deal with the group signature symbol. */
11572 struct bfd_elf_section_data *sec_data = elf_section_data (o);
11573 unsigned long symndx = sec_data->this_hdr.sh_info;
11574 asection *osec = o->output_section;
11576 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
11577 if (symndx >= locsymcount
11578 || (elf_bad_symtab (input_bfd)
11579 && flinfo->sections[symndx] == NULL))
11581 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
11582 while (h->root.type == bfd_link_hash_indirect
11583 || h->root.type == bfd_link_hash_warning)
11584 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11585 /* Arrange for symbol to be output. */
11586 h->indx = -2;
11587 elf_section_data (osec)->this_hdr.sh_info = -2;
11589 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
11591 /* We'll use the output section target_index. */
11592 asection *sec = flinfo->sections[symndx]->output_section;
11593 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
11595 else
11597 if (flinfo->indices[symndx] == -1)
11599 /* Otherwise output the local symbol now. */
11600 Elf_Internal_Sym sym = isymbuf[symndx];
11601 asection *sec = flinfo->sections[symndx]->output_section;
11602 const char *name;
11603 long indx;
11604 int ret;
11606 name = bfd_elf_string_from_elf_section (input_bfd,
11607 symtab_hdr->sh_link,
11608 sym.st_name);
11609 if (name == NULL)
11610 return false;
11612 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11613 sec);
11614 if (sym.st_shndx == SHN_BAD)
11615 return false;
11617 sym.st_value += o->output_offset;
11619 indx = bfd_get_symcount (output_bfd);
11620 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
11621 NULL);
11622 if (ret == 0)
11623 return false;
11624 else if (ret == 1)
11625 flinfo->indices[symndx] = indx;
11626 else
11627 abort ();
11629 elf_section_data (osec)->this_hdr.sh_info
11630 = flinfo->indices[symndx];
11634 if ((o->flags & SEC_HAS_CONTENTS) == 0
11635 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
11636 continue;
11638 if ((o->flags & SEC_LINKER_CREATED) != 0)
11640 /* Section was created by _bfd_elf_link_create_dynamic_sections
11641 or somesuch. */
11642 continue;
11645 /* Get the contents of the section. They have been cached by a
11646 relaxation routine. Note that o is a section in an input
11647 file, so the contents field will not have been set by any of
11648 the routines which work on output files. */
11649 if (elf_section_data (o)->this_hdr.contents != NULL)
11651 contents = elf_section_data (o)->this_hdr.contents;
11652 if (bed->caches_rawsize
11653 && o->rawsize != 0
11654 && o->rawsize < o->size)
11656 memcpy (flinfo->contents, contents, o->rawsize);
11657 contents = flinfo->contents;
11660 else if (!(o->flags & SEC_RELOC)
11661 && !bed->elf_backend_write_section
11662 && o->sec_info_type == SEC_INFO_TYPE_MERGE)
11663 /* A MERGE section that has no relocations doesn't need the
11664 contents anymore, they have been recorded earlier. Except
11665 if the backend has special provisions for writing sections. */
11666 contents = NULL;
11667 else
11669 contents = flinfo->contents;
11670 if (! _bfd_elf_link_mmap_section_contents (input_bfd, o,
11671 &contents))
11672 return false;
11675 if ((o->flags & SEC_RELOC) != 0)
11677 Elf_Internal_Rela *internal_relocs;
11678 Elf_Internal_Rela *rel, *relend;
11679 int action_discarded;
11680 int ret;
11682 /* Get the swapped relocs. */
11683 internal_relocs
11684 = _bfd_elf_link_info_read_relocs (input_bfd, flinfo->info, o,
11685 flinfo->external_relocs,
11686 flinfo->internal_relocs,
11687 false);
11688 if (internal_relocs == NULL
11689 && o->reloc_count > 0)
11690 return false;
11692 action_discarded = -1;
11693 if (!elf_section_ignore_discarded_relocs (o))
11694 action_discarded = (*bed->action_discarded) (o);
11696 /* Run through the relocs evaluating complex reloc symbols and
11697 looking for relocs against symbols from discarded sections
11698 or section symbols from removed link-once sections.
11699 Complain about relocs against discarded sections. Zero
11700 relocs against removed link-once sections. */
11702 rel = internal_relocs;
11703 relend = rel + o->reloc_count;
11704 for ( ; rel < relend; rel++)
11706 unsigned long r_symndx = rel->r_info >> r_sym_shift;
11707 unsigned int s_type;
11708 asection **ps, *sec;
11709 struct elf_link_hash_entry *h = NULL;
11710 const char *sym_name;
11712 if (r_symndx == STN_UNDEF)
11713 continue;
11715 if (r_symndx >= locsymcount
11716 || (elf_bad_symtab (input_bfd)
11717 && flinfo->sections[r_symndx] == NULL))
11719 h = sym_hashes[r_symndx - extsymoff];
11721 /* Badly formatted input files can contain relocs that
11722 reference non-existant symbols. Check here so that
11723 we do not seg fault. */
11724 if (h == NULL)
11726 _bfd_error_handler
11727 /* xgettext:c-format */
11728 (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
11729 "that references a non-existent global symbol"),
11730 input_bfd, (uint64_t) rel->r_info, o);
11731 bfd_set_error (bfd_error_bad_value);
11732 return false;
11735 while (h->root.type == bfd_link_hash_indirect
11736 || h->root.type == bfd_link_hash_warning)
11737 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11739 s_type = h->type;
11741 /* If a plugin symbol is referenced from a non-IR file,
11742 mark the symbol as undefined. Note that the
11743 linker may attach linker created dynamic sections
11744 to the plugin bfd. Symbols defined in linker
11745 created sections are not plugin symbols. */
11746 if ((h->root.non_ir_ref_regular
11747 || h->root.non_ir_ref_dynamic)
11748 && (h->root.type == bfd_link_hash_defined
11749 || h->root.type == bfd_link_hash_defweak)
11750 && (h->root.u.def.section->flags
11751 & SEC_LINKER_CREATED) == 0
11752 && h->root.u.def.section->owner != NULL
11753 && (h->root.u.def.section->owner->flags
11754 & BFD_PLUGIN) != 0)
11756 h->root.type = bfd_link_hash_undefined;
11757 h->root.u.undef.abfd = h->root.u.def.section->owner;
11760 ps = NULL;
11761 if (h->root.type == bfd_link_hash_defined
11762 || h->root.type == bfd_link_hash_defweak)
11763 ps = &h->root.u.def.section;
11765 sym_name = h->root.root.string;
11767 else
11769 Elf_Internal_Sym *sym = isymbuf + r_symndx;
11771 s_type = ELF_ST_TYPE (sym->st_info);
11772 ps = &flinfo->sections[r_symndx];
11773 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
11774 sym, *ps);
11777 if ((s_type == STT_RELC || s_type == STT_SRELC)
11778 && !bfd_link_relocatable (flinfo->info))
11780 bfd_vma val;
11781 bfd_vma dot = (rel->r_offset
11782 + o->output_offset + o->output_section->vma);
11783 #ifdef DEBUG
11784 printf ("Encountered a complex symbol!");
11785 printf (" (input_bfd %s, section %s, reloc %ld\n",
11786 bfd_get_filename (input_bfd), o->name,
11787 (long) (rel - internal_relocs));
11788 printf (" symbol: idx %8.8lx, name %s\n",
11789 r_symndx, sym_name);
11790 printf (" reloc : info %8.8lx, addr %8.8lx\n",
11791 (unsigned long) rel->r_info,
11792 (unsigned long) rel->r_offset);
11793 #endif
11794 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
11795 isymbuf, locsymcount, s_type == STT_SRELC))
11796 return false;
11798 /* Symbol evaluated OK. Update to absolute value. */
11799 set_symbol_value (input_bfd, isymbuf, locsymcount,
11800 r_symndx, val);
11801 continue;
11804 if (action_discarded != -1 && ps != NULL)
11806 /* Complain if the definition comes from a
11807 discarded section. */
11808 if ((sec = *ps) != NULL && discarded_section (sec))
11810 BFD_ASSERT (r_symndx != STN_UNDEF);
11811 if (action_discarded & COMPLAIN)
11812 (*flinfo->info->callbacks->einfo)
11813 /* xgettext:c-format */
11814 (_("%X`%s' referenced in section `%pA' of %pB: "
11815 "defined in discarded section `%pA' of %pB\n"),
11816 sym_name, o, input_bfd, sec, sec->owner);
11818 /* Try to do the best we can to support buggy old
11819 versions of gcc. Pretend that the symbol is
11820 really defined in the kept linkonce section.
11821 FIXME: This is quite broken. Modifying the
11822 symbol here means we will be changing all later
11823 uses of the symbol, not just in this section. */
11824 if (action_discarded & PRETEND)
11826 asection *kept;
11828 kept = _bfd_elf_check_kept_section (sec,
11829 flinfo->info);
11830 if (kept != NULL)
11832 *ps = kept;
11833 continue;
11840 /* Relocate the section by invoking a back end routine.
11842 The back end routine is responsible for adjusting the
11843 section contents as necessary, and (if using Rela relocs
11844 and generating a relocatable output file) adjusting the
11845 reloc addend as necessary.
11847 The back end routine does not have to worry about setting
11848 the reloc address or the reloc symbol index.
11850 The back end routine is given a pointer to the swapped in
11851 internal symbols, and can access the hash table entries
11852 for the external symbols via elf_sym_hashes (input_bfd).
11854 When generating relocatable output, the back end routine
11855 must handle STB_LOCAL/STT_SECTION symbols specially. The
11856 output symbol is going to be a section symbol
11857 corresponding to the output section, which will require
11858 the addend to be adjusted. */
11860 ret = (*relocate_section) (output_bfd, flinfo->info,
11861 input_bfd, o, contents,
11862 internal_relocs,
11863 isymbuf,
11864 flinfo->sections);
11865 if (!ret)
11866 return false;
11868 if (ret == 2
11869 || bfd_link_relocatable (flinfo->info)
11870 || flinfo->info->emitrelocations)
11872 Elf_Internal_Rela *irela;
11873 Elf_Internal_Rela *irelaend, *irelamid;
11874 bfd_vma last_offset;
11875 struct elf_link_hash_entry **rel_hash;
11876 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
11877 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
11878 unsigned int next_erel;
11879 bool rela_normal;
11880 struct bfd_elf_section_data *esdi, *esdo;
11882 esdi = elf_section_data (o);
11883 esdo = elf_section_data (o->output_section);
11884 rela_normal = false;
11886 /* Adjust the reloc addresses and symbol indices. */
11888 irela = internal_relocs;
11889 irelaend = irela + o->reloc_count;
11890 rel_hash = PTR_ADD (esdo->rel.hashes, esdo->rel.count);
11891 /* We start processing the REL relocs, if any. When we reach
11892 IRELAMID in the loop, we switch to the RELA relocs. */
11893 irelamid = irela;
11894 if (esdi->rel.hdr != NULL)
11895 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
11896 * bed->s->int_rels_per_ext_rel);
11897 rel_hash_list = rel_hash;
11898 rela_hash_list = NULL;
11899 last_offset = o->output_offset;
11900 if (!bfd_link_relocatable (flinfo->info))
11901 last_offset += o->output_section->vma;
11902 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
11904 unsigned long r_symndx;
11905 asection *sec;
11906 Elf_Internal_Sym sym;
11908 if (next_erel == bed->s->int_rels_per_ext_rel)
11910 rel_hash++;
11911 next_erel = 0;
11914 if (irela == irelamid)
11916 rel_hash = PTR_ADD (esdo->rela.hashes, esdo->rela.count);
11917 rela_hash_list = rel_hash;
11918 rela_normal = bed->rela_normal;
11921 irela->r_offset = _bfd_elf_section_offset (output_bfd,
11922 flinfo->info, o,
11923 irela->r_offset);
11924 if (irela->r_offset >= (bfd_vma) -2)
11926 /* This is a reloc for a deleted entry or somesuch.
11927 Turn it into an R_*_NONE reloc, at the same
11928 offset as the last reloc. elf_eh_frame.c and
11929 bfd_elf_discard_info rely on reloc offsets
11930 being ordered. */
11931 irela->r_offset = last_offset;
11932 irela->r_info = 0;
11933 irela->r_addend = 0;
11934 continue;
11937 irela->r_offset += o->output_offset;
11939 /* Relocs in an executable have to be virtual addresses. */
11940 if (!bfd_link_relocatable (flinfo->info))
11941 irela->r_offset += o->output_section->vma;
11943 last_offset = irela->r_offset;
11945 r_symndx = irela->r_info >> r_sym_shift;
11946 if (r_symndx == STN_UNDEF)
11947 continue;
11949 if (r_symndx >= locsymcount
11950 || (elf_bad_symtab (input_bfd)
11951 && flinfo->sections[r_symndx] == NULL))
11953 struct elf_link_hash_entry *rh;
11954 unsigned long indx;
11956 /* This is a reloc against a global symbol. We
11957 have not yet output all the local symbols, so
11958 we do not know the symbol index of any global
11959 symbol. We set the rel_hash entry for this
11960 reloc to point to the global hash table entry
11961 for this symbol. The symbol index is then
11962 set at the end of bfd_elf_final_link. */
11963 indx = r_symndx - extsymoff;
11964 rh = elf_sym_hashes (input_bfd)[indx];
11965 while (rh->root.type == bfd_link_hash_indirect
11966 || rh->root.type == bfd_link_hash_warning)
11967 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
11969 /* Setting the index to -2 tells
11970 elf_link_output_extsym that this symbol is
11971 used by a reloc. */
11972 BFD_ASSERT (rh->indx < 0);
11973 rh->indx = -2;
11974 *rel_hash = rh;
11976 continue;
11979 /* This is a reloc against a local symbol. */
11981 *rel_hash = NULL;
11982 sym = isymbuf[r_symndx];
11983 sec = flinfo->sections[r_symndx];
11984 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
11986 /* I suppose the backend ought to fill in the
11987 section of any STT_SECTION symbol against a
11988 processor specific section. */
11989 r_symndx = STN_UNDEF;
11990 if (bfd_is_abs_section (sec))
11992 else if (sec == NULL || sec->owner == NULL)
11994 bfd_set_error (bfd_error_bad_value);
11995 return false;
11997 else
11999 asection *osec = sec->output_section;
12001 /* If we have discarded a section, the output
12002 section will be the absolute section. In
12003 case of discarded SEC_MERGE sections, use
12004 the kept section. relocate_section should
12005 have already handled discarded linkonce
12006 sections. */
12007 if (bfd_is_abs_section (osec)
12008 && sec->kept_section != NULL
12009 && sec->kept_section->output_section != NULL)
12011 osec = sec->kept_section->output_section;
12012 irela->r_addend -= osec->vma;
12015 if (!bfd_is_abs_section (osec))
12017 r_symndx = osec->target_index;
12018 if (r_symndx == STN_UNDEF)
12020 irela->r_addend += osec->vma;
12021 osec = _bfd_nearby_section (output_bfd, osec,
12022 osec->vma);
12023 irela->r_addend -= osec->vma;
12024 r_symndx = osec->target_index;
12029 /* Adjust the addend according to where the
12030 section winds up in the output section. */
12031 if (rela_normal)
12032 irela->r_addend += sec->output_offset;
12034 else
12036 if (flinfo->indices[r_symndx] == -1)
12038 unsigned long shlink;
12039 const char *name;
12040 asection *osec;
12041 long indx;
12043 if (flinfo->info->strip == strip_all)
12045 /* You can't do ld -r -s. */
12046 bfd_set_error (bfd_error_invalid_operation);
12047 return false;
12050 /* This symbol was skipped earlier, but
12051 since it is needed by a reloc, we
12052 must output it now. */
12053 shlink = symtab_hdr->sh_link;
12054 name = (bfd_elf_string_from_elf_section
12055 (input_bfd, shlink, sym.st_name));
12056 if (name == NULL)
12057 return false;
12059 osec = sec->output_section;
12060 sym.st_shndx =
12061 _bfd_elf_section_from_bfd_section (output_bfd,
12062 osec);
12063 if (sym.st_shndx == SHN_BAD)
12064 return false;
12066 sym.st_value += sec->output_offset;
12067 if (!bfd_link_relocatable (flinfo->info))
12069 sym.st_value += osec->vma;
12070 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
12072 struct elf_link_hash_table *htab
12073 = elf_hash_table (flinfo->info);
12075 /* STT_TLS symbols are relative to PT_TLS
12076 segment base. */
12077 if (htab->tls_sec != NULL)
12078 sym.st_value -= htab->tls_sec->vma;
12079 else
12080 sym.st_info
12081 = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
12082 STT_NOTYPE);
12086 indx = bfd_get_symcount (output_bfd);
12087 ret = elf_link_output_symstrtab (flinfo, name,
12088 &sym, sec,
12089 NULL);
12090 if (ret == 0)
12091 return false;
12092 else if (ret == 1)
12093 flinfo->indices[r_symndx] = indx;
12094 else
12095 abort ();
12098 r_symndx = flinfo->indices[r_symndx];
12101 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
12102 | (irela->r_info & r_type_mask));
12105 /* Swap out the relocs. */
12106 input_rel_hdr = esdi->rel.hdr;
12107 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
12109 if (!bed->elf_backend_emit_relocs (output_bfd, o,
12110 input_rel_hdr,
12111 internal_relocs,
12112 rel_hash_list))
12113 return false;
12114 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
12115 * bed->s->int_rels_per_ext_rel);
12116 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
12119 input_rela_hdr = esdi->rela.hdr;
12120 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
12122 if (!bed->elf_backend_emit_relocs (output_bfd, o,
12123 input_rela_hdr,
12124 internal_relocs,
12125 rela_hash_list))
12126 return false;
12131 /* Write out the modified section contents. */
12132 if (bed->elf_backend_write_section
12133 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
12134 contents))
12136 /* Section written out. */
12138 else switch (o->sec_info_type)
12140 case SEC_INFO_TYPE_STABS:
12141 if (! (_bfd_write_section_stabs
12142 (output_bfd,
12143 &elf_hash_table (flinfo->info)->stab_info,
12144 o, &elf_section_data (o)->sec_info, contents)))
12145 return false;
12146 break;
12147 case SEC_INFO_TYPE_MERGE:
12148 if (! _bfd_write_merged_section (output_bfd, o,
12149 elf_section_data (o)->sec_info))
12150 return false;
12151 break;
12152 case SEC_INFO_TYPE_EH_FRAME:
12154 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
12155 o, contents))
12156 return false;
12158 break;
12159 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
12161 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
12162 flinfo->info,
12163 o, contents))
12164 return false;
12166 break;
12167 case SEC_INFO_TYPE_SFRAME:
12169 /* Merge .sframe sections into the ctf frame encoder
12170 context of the output_bfd's section. The final .sframe
12171 output section will be written out later. */
12172 if (!_bfd_elf_merge_section_sframe (output_bfd, flinfo->info,
12173 o, contents))
12174 return false;
12176 break;
12177 default:
12179 if (! (o->flags & SEC_EXCLUDE))
12181 file_ptr offset = (file_ptr) o->output_offset;
12182 bfd_size_type todo = o->size;
12184 offset *= bfd_octets_per_byte (output_bfd, o);
12186 if ((o->flags & SEC_ELF_REVERSE_COPY)
12187 && o->size > address_size)
12189 /* Reverse-copy input section to output. */
12191 if ((o->size & (address_size - 1)) != 0
12192 || (o->reloc_count != 0
12193 && (o->size * bed->s->int_rels_per_ext_rel
12194 != o->reloc_count * address_size)))
12196 _bfd_error_handler
12197 /* xgettext:c-format */
12198 (_("error: %pB: size of section %pA is not "
12199 "multiple of address size"),
12200 input_bfd, o);
12201 bfd_set_error (bfd_error_bad_value);
12202 return false;
12207 todo -= address_size;
12208 if (! bfd_set_section_contents (output_bfd,
12209 o->output_section,
12210 contents + todo,
12211 offset,
12212 address_size))
12213 return false;
12214 if (todo == 0)
12215 break;
12216 offset += address_size;
12218 while (1);
12220 else if (! bfd_set_section_contents (output_bfd,
12221 o->output_section,
12222 contents,
12223 offset, todo))
12224 return false;
12227 break;
12230 /* Munmap the section contents for each input section. */
12231 _bfd_elf_link_munmap_section_contents (o);
12234 return true;
12237 /* Generate a reloc when linking an ELF file. This is a reloc
12238 requested by the linker, and does not come from any input file. This
12239 is used to build constructor and destructor tables when linking
12240 with -Ur. */
12242 static bool
12243 elf_reloc_link_order (bfd *output_bfd,
12244 struct bfd_link_info *info,
12245 asection *output_section,
12246 struct bfd_link_order *link_order)
12248 reloc_howto_type *howto;
12249 long indx;
12250 bfd_vma offset;
12251 bfd_vma addend;
12252 struct bfd_elf_section_reloc_data *reldata;
12253 struct elf_link_hash_entry **rel_hash_ptr;
12254 Elf_Internal_Shdr *rel_hdr;
12255 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
12256 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
12257 bfd_byte *erel;
12258 unsigned int i;
12259 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
12261 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
12262 if (howto == NULL)
12264 bfd_set_error (bfd_error_bad_value);
12265 return false;
12268 addend = link_order->u.reloc.p->addend;
12270 if (esdo->rel.hdr)
12271 reldata = &esdo->rel;
12272 else if (esdo->rela.hdr)
12273 reldata = &esdo->rela;
12274 else
12276 reldata = NULL;
12277 BFD_ASSERT (0);
12280 /* Figure out the symbol index. */
12281 rel_hash_ptr = reldata->hashes + reldata->count;
12282 if (link_order->type == bfd_section_reloc_link_order)
12284 indx = link_order->u.reloc.p->u.section->target_index;
12285 BFD_ASSERT (indx != 0);
12286 *rel_hash_ptr = NULL;
12288 else
12290 struct elf_link_hash_entry *h;
12292 /* Treat a reloc against a defined symbol as though it were
12293 actually against the section. */
12294 h = ((struct elf_link_hash_entry *)
12295 bfd_wrapped_link_hash_lookup (output_bfd, info,
12296 link_order->u.reloc.p->u.name,
12297 false, false, true));
12298 if (h != NULL
12299 && (h->root.type == bfd_link_hash_defined
12300 || h->root.type == bfd_link_hash_defweak))
12302 asection *section;
12304 section = h->root.u.def.section;
12305 indx = section->output_section->target_index;
12306 *rel_hash_ptr = NULL;
12307 /* It seems that we ought to add the symbol value to the
12308 addend here, but in practice it has already been added
12309 because it was passed to constructor_callback. */
12310 addend += section->output_section->vma + section->output_offset;
12312 else if (h != NULL)
12314 /* Setting the index to -2 tells elf_link_output_extsym that
12315 this symbol is used by a reloc. */
12316 h->indx = -2;
12317 *rel_hash_ptr = h;
12318 indx = 0;
12320 else
12322 (*info->callbacks->unattached_reloc)
12323 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
12324 indx = 0;
12328 /* If this is an inplace reloc, we must write the addend into the
12329 object file. */
12330 if (howto->partial_inplace && addend != 0)
12332 bfd_size_type size;
12333 bfd_reloc_status_type rstat;
12334 bfd_byte *buf;
12335 bool ok;
12336 const char *sym_name;
12337 bfd_size_type octets;
12339 size = (bfd_size_type) bfd_get_reloc_size (howto);
12340 buf = (bfd_byte *) bfd_zmalloc (size);
12341 if (buf == NULL && size != 0)
12342 return false;
12343 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
12344 switch (rstat)
12346 case bfd_reloc_ok:
12347 break;
12349 default:
12350 case bfd_reloc_outofrange:
12351 abort ();
12353 case bfd_reloc_overflow:
12354 if (link_order->type == bfd_section_reloc_link_order)
12355 sym_name = bfd_section_name (link_order->u.reloc.p->u.section);
12356 else
12357 sym_name = link_order->u.reloc.p->u.name;
12358 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
12359 howto->name, addend, NULL, NULL,
12360 (bfd_vma) 0);
12361 break;
12364 octets = link_order->offset * bfd_octets_per_byte (output_bfd,
12365 output_section);
12366 ok = bfd_set_section_contents (output_bfd, output_section, buf,
12367 octets, size);
12368 free (buf);
12369 if (! ok)
12370 return false;
12373 /* The address of a reloc is relative to the section in a
12374 relocatable file, and is a virtual address in an executable
12375 file. */
12376 offset = link_order->offset;
12377 if (! bfd_link_relocatable (info))
12378 offset += output_section->vma;
12380 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
12382 irel[i].r_offset = offset;
12383 irel[i].r_info = 0;
12384 irel[i].r_addend = 0;
12386 if (bed->s->arch_size == 32)
12387 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
12388 else
12389 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
12391 rel_hdr = reldata->hdr;
12392 erel = rel_hdr->contents;
12393 if (rel_hdr->sh_type == SHT_REL)
12395 erel += reldata->count * bed->s->sizeof_rel;
12396 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
12398 else
12400 irel[0].r_addend = addend;
12401 erel += reldata->count * bed->s->sizeof_rela;
12402 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
12405 ++reldata->count;
12407 return true;
12410 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
12411 Returns TRUE upon success, FALSE otherwise. */
12413 static bool
12414 elf_output_implib (bfd *abfd, struct bfd_link_info *info)
12416 bool ret = false;
12417 bfd *implib_bfd;
12418 const struct elf_backend_data *bed;
12419 flagword flags;
12420 enum bfd_architecture arch;
12421 unsigned int mach;
12422 asymbol **sympp = NULL;
12423 long symsize;
12424 long symcount;
12425 long src_count;
12426 elf_symbol_type *osymbuf;
12427 size_t amt;
12429 implib_bfd = info->out_implib_bfd;
12430 bed = get_elf_backend_data (abfd);
12432 if (!bfd_set_format (implib_bfd, bfd_object))
12433 return false;
12435 /* Use flag from executable but make it a relocatable object. */
12436 flags = bfd_get_file_flags (abfd);
12437 flags &= ~HAS_RELOC;
12438 if (!bfd_set_start_address (implib_bfd, 0)
12439 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
12440 return false;
12442 /* Copy architecture of output file to import library file. */
12443 arch = bfd_get_arch (abfd);
12444 mach = bfd_get_mach (abfd);
12445 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
12446 && (abfd->target_defaulted
12447 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
12448 return false;
12450 /* Get symbol table size. */
12451 symsize = bfd_get_symtab_upper_bound (abfd);
12452 if (symsize < 0)
12453 return false;
12455 /* Read in the symbol table. */
12456 sympp = (asymbol **) bfd_malloc (symsize);
12457 if (sympp == NULL)
12458 return false;
12460 symcount = bfd_canonicalize_symtab (abfd, sympp);
12461 if (symcount < 0)
12462 goto free_sym_buf;
12464 /* Allow the BFD backend to copy any private header data it
12465 understands from the output BFD to the import library BFD. */
12466 if (! bfd_copy_private_header_data (abfd, implib_bfd))
12467 goto free_sym_buf;
12469 /* Filter symbols to appear in the import library. */
12470 if (bed->elf_backend_filter_implib_symbols)
12471 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
12472 symcount);
12473 else
12474 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
12475 if (symcount == 0)
12477 bfd_set_error (bfd_error_no_symbols);
12478 _bfd_error_handler (_("%pB: no symbol found for import library"),
12479 implib_bfd);
12480 goto free_sym_buf;
12484 /* Make symbols absolute. */
12485 amt = symcount * sizeof (*osymbuf);
12486 osymbuf = (elf_symbol_type *) bfd_alloc (implib_bfd, amt);
12487 if (osymbuf == NULL)
12488 goto free_sym_buf;
12490 for (src_count = 0; src_count < symcount; src_count++)
12492 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
12493 sizeof (*osymbuf));
12494 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
12495 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
12496 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
12497 osymbuf[src_count].internal_elf_sym.st_value =
12498 osymbuf[src_count].symbol.value;
12499 sympp[src_count] = &osymbuf[src_count].symbol;
12502 bfd_set_symtab (implib_bfd, sympp, symcount);
12504 /* Allow the BFD backend to copy any private data it understands
12505 from the output BFD to the import library BFD. This is done last
12506 to permit the routine to look at the filtered symbol table. */
12507 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
12508 goto free_sym_buf;
12510 if (!bfd_close (implib_bfd))
12511 goto free_sym_buf;
12513 ret = true;
12515 free_sym_buf:
12516 free (sympp);
12517 return ret;
12520 static void
12521 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
12523 asection *o;
12525 if (flinfo->symstrtab != NULL)
12526 _bfd_elf_strtab_free (flinfo->symstrtab);
12527 free (flinfo->contents);
12528 free (flinfo->external_relocs);
12529 free (flinfo->internal_relocs);
12530 free (flinfo->external_syms);
12531 free (flinfo->locsym_shndx);
12532 free (flinfo->internal_syms);
12533 free (flinfo->indices);
12534 free (flinfo->sections);
12535 if (flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1)
12536 free (flinfo->symshndxbuf);
12537 for (o = obfd->sections; o != NULL; o = o->next)
12539 struct bfd_elf_section_data *esdo = elf_section_data (o);
12540 free (esdo->rel.hashes);
12541 free (esdo->rela.hashes);
12545 /* Do the final step of an ELF link. */
12547 bool
12548 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
12550 bool dynamic;
12551 bool emit_relocs;
12552 bfd *dynobj;
12553 struct elf_final_link_info flinfo;
12554 asection *o;
12555 struct bfd_link_order *p;
12556 bfd *sub;
12557 bfd_size_type max_contents_size;
12558 bfd_size_type max_external_reloc_size;
12559 bfd_size_type max_internal_reloc_count;
12560 bfd_size_type max_sym_count;
12561 bfd_size_type max_sym_shndx_count;
12562 Elf_Internal_Sym elfsym;
12563 unsigned int i;
12564 Elf_Internal_Shdr *symtab_hdr;
12565 Elf_Internal_Shdr *symtab_shndx_hdr;
12566 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12567 struct elf_outext_info eoinfo;
12568 bool merged;
12569 size_t relativecount;
12570 size_t relr_entsize;
12571 asection *reldyn = 0;
12572 bfd_size_type amt;
12573 asection *attr_section = NULL;
12574 bfd_vma attr_size = 0;
12575 const char *std_attrs_section;
12576 struct elf_link_hash_table *htab = elf_hash_table (info);
12577 bool sections_removed;
12579 if (!is_elf_hash_table (&htab->root))
12580 return false;
12582 if (bfd_link_pic (info))
12583 abfd->flags |= DYNAMIC;
12585 dynamic = htab->dynamic_sections_created;
12586 dynobj = htab->dynobj;
12588 emit_relocs = (bfd_link_relocatable (info)
12589 || info->emitrelocations);
12591 memset (&flinfo, 0, sizeof (flinfo));
12592 flinfo.info = info;
12593 flinfo.output_bfd = abfd;
12594 flinfo.symstrtab = _bfd_elf_strtab_init ();
12595 if (flinfo.symstrtab == NULL)
12596 return false;
12598 if (! dynamic)
12600 flinfo.hash_sec = NULL;
12601 flinfo.symver_sec = NULL;
12603 else
12605 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
12606 /* Note that dynsym_sec can be NULL (on VMS). */
12607 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
12608 /* Note that it is OK if symver_sec is NULL. */
12611 if (info->unique_symbol
12612 && !bfd_hash_table_init (&flinfo.local_hash_table,
12613 local_hash_newfunc,
12614 sizeof (struct local_hash_entry)))
12615 return false;
12617 /* The object attributes have been merged. Remove the input
12618 sections from the link, and set the contents of the output
12619 section. */
12620 sections_removed = false;
12621 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
12622 for (o = abfd->sections; o != NULL; o = o->next)
12624 bool remove_section = false;
12626 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
12627 || strcmp (o->name, ".gnu.attributes") == 0)
12629 for (p = o->map_head.link_order; p != NULL; p = p->next)
12631 asection *input_section;
12633 if (p->type != bfd_indirect_link_order)
12634 continue;
12635 input_section = p->u.indirect.section;
12636 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12637 elf_link_input_bfd ignores this section. */
12638 input_section->flags &= ~SEC_HAS_CONTENTS;
12641 attr_size = bfd_elf_obj_attr_size (abfd);
12642 bfd_set_section_size (o, attr_size);
12643 /* Skip this section later on. */
12644 o->map_head.link_order = NULL;
12645 if (attr_size)
12646 attr_section = o;
12647 else
12648 remove_section = true;
12650 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
12652 /* Remove empty group section from linker output. */
12653 remove_section = true;
12655 if (remove_section)
12657 o->flags |= SEC_EXCLUDE;
12658 bfd_section_list_remove (abfd, o);
12659 abfd->section_count--;
12660 sections_removed = true;
12663 if (sections_removed)
12664 _bfd_fix_excluded_sec_syms (abfd, info);
12666 /* Count up the number of relocations we will output for each output
12667 section, so that we know the sizes of the reloc sections. We
12668 also figure out some maximum sizes. */
12669 #ifdef USE_MMAP
12670 if (bed->use_mmap)
12672 /* Mmap is used only if section size >= the minimum mmap section
12673 size. The initial max_contents_size value covers all sections
12674 smaller than the minimum mmap section size. It may be increased
12675 for compressed or linker created sections or sections whose
12676 rawsize != size. max_external_reloc_size covers all relocation
12677 sections smaller than the minimum mmap section size. */
12678 max_contents_size = _bfd_minimum_mmap_size;
12679 max_external_reloc_size = _bfd_minimum_mmap_size;
12681 else
12682 #endif
12684 max_contents_size = 0;
12685 max_external_reloc_size = 0;
12687 max_internal_reloc_count = 0;
12688 max_sym_count = 0;
12689 max_sym_shndx_count = 0;
12690 merged = false;
12691 for (o = abfd->sections; o != NULL; o = o->next)
12693 struct bfd_elf_section_data *esdo = elf_section_data (o);
12694 o->reloc_count = 0;
12696 for (p = o->map_head.link_order; p != NULL; p = p->next)
12698 unsigned int reloc_count = 0;
12699 unsigned int additional_reloc_count = 0;
12700 struct bfd_elf_section_data *esdi = NULL;
12702 if (p->type == bfd_section_reloc_link_order
12703 || p->type == bfd_symbol_reloc_link_order)
12704 reloc_count = 1;
12705 else if (p->type == bfd_indirect_link_order)
12707 asection *sec;
12709 sec = p->u.indirect.section;
12711 /* Mark all sections which are to be included in the
12712 link. This will normally be every section. We need
12713 to do this so that we can identify any sections which
12714 the linker has decided to not include. */
12715 sec->linker_mark = true;
12717 if (sec->flags & SEC_MERGE)
12718 merged = true;
12720 #ifdef USE_MMAP
12721 /* Mmap is used only on non-compressed, non-linker created
12722 sections whose rawsize == size. */
12723 if (!bed->use_mmap
12724 || sec->compress_status != COMPRESS_SECTION_NONE
12725 || (sec->flags & SEC_LINKER_CREATED) != 0
12726 || sec->rawsize != sec->size)
12727 #endif
12729 if (sec->rawsize > max_contents_size)
12730 max_contents_size = sec->rawsize;
12731 if (sec->size > max_contents_size)
12732 max_contents_size = sec->size;
12735 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
12736 && (sec->owner->flags & DYNAMIC) == 0)
12738 size_t sym_count;
12740 /* We are interested in just local symbols, not all
12741 symbols. */
12742 if (elf_bad_symtab (sec->owner))
12743 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
12744 / bed->s->sizeof_sym);
12745 else
12746 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
12748 if (sym_count > max_sym_count)
12749 max_sym_count = sym_count;
12751 if (sym_count > max_sym_shndx_count
12752 && elf_symtab_shndx_list (sec->owner) != NULL)
12753 max_sym_shndx_count = sym_count;
12755 esdi = elf_section_data (sec);
12757 if (esdi->this_hdr.sh_type == SHT_REL
12758 || esdi->this_hdr.sh_type == SHT_RELA)
12759 /* Some backends use reloc_count in relocation sections
12760 to count particular types of relocs. Of course,
12761 reloc sections themselves can't have relocations. */
12763 else if (emit_relocs)
12765 reloc_count = sec->reloc_count;
12766 if (bed->elf_backend_count_additional_relocs)
12768 int c;
12769 c = (*bed->elf_backend_count_additional_relocs) (sec);
12770 additional_reloc_count += c;
12773 else if (bed->elf_backend_count_relocs)
12774 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
12776 if ((sec->flags & SEC_RELOC) != 0)
12778 #ifdef USE_MMAP
12779 if (!bed->use_mmap)
12780 #endif
12782 size_t ext_size = 0;
12784 if (esdi->rel.hdr != NULL)
12785 ext_size = esdi->rel.hdr->sh_size;
12786 if (esdi->rela.hdr != NULL)
12787 ext_size += esdi->rela.hdr->sh_size;
12789 if (ext_size > max_external_reloc_size)
12790 max_external_reloc_size = ext_size;
12792 if (sec->reloc_count > max_internal_reloc_count)
12793 max_internal_reloc_count = sec->reloc_count;
12798 if (reloc_count == 0)
12799 continue;
12801 reloc_count += additional_reloc_count;
12802 o->reloc_count += reloc_count;
12804 if (p->type == bfd_indirect_link_order && emit_relocs)
12806 if (esdi->rel.hdr)
12808 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
12809 esdo->rel.count += additional_reloc_count;
12811 if (esdi->rela.hdr)
12813 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
12814 esdo->rela.count += additional_reloc_count;
12817 else
12819 if (o->use_rela_p)
12820 esdo->rela.count += reloc_count;
12821 else
12822 esdo->rel.count += reloc_count;
12826 if (o->reloc_count > 0)
12827 o->flags |= SEC_RELOC;
12828 else
12830 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12831 set it (this is probably a bug) and if it is set
12832 assign_section_numbers will create a reloc section. */
12833 o->flags &=~ SEC_RELOC;
12836 /* If the SEC_ALLOC flag is not set, force the section VMA to
12837 zero. This is done in elf_fake_sections as well, but forcing
12838 the VMA to 0 here will ensure that relocs against these
12839 sections are handled correctly. */
12840 if ((o->flags & SEC_ALLOC) == 0
12841 && ! o->user_set_vma)
12842 o->vma = 0;
12845 if (! bfd_link_relocatable (info) && merged)
12846 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
12848 /* Figure out the file positions for everything but the symbol table
12849 and the relocs. We set symcount to force assign_section_numbers
12850 to create a symbol table. */
12851 abfd->symcount = info->strip != strip_all || emit_relocs;
12852 BFD_ASSERT (! abfd->output_has_begun);
12853 if (! _bfd_elf_compute_section_file_positions (abfd, info))
12854 goto error_return;
12856 /* Set sizes, and assign file positions for reloc sections. */
12857 for (o = abfd->sections; o != NULL; o = o->next)
12859 struct bfd_elf_section_data *esdo = elf_section_data (o);
12860 if ((o->flags & SEC_RELOC) != 0)
12862 if (esdo->rel.hdr
12863 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
12864 goto error_return;
12866 if (esdo->rela.hdr
12867 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
12868 goto error_return;
12871 /* _bfd_elf_compute_section_file_positions makes temporary use
12872 of target_index. Reset it. */
12873 o->target_index = 0;
12875 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12876 to count upwards while actually outputting the relocations. */
12877 esdo->rel.count = 0;
12878 esdo->rela.count = 0;
12880 if ((esdo->this_hdr.sh_offset == (file_ptr) -1)
12881 && !bfd_section_is_ctf (o))
12883 /* Cache the section contents so that they can be compressed
12884 later. Use bfd_malloc since it will be freed by
12885 bfd_compress_section_contents. */
12886 unsigned char *contents = esdo->this_hdr.contents;
12887 if (contents != NULL)
12888 abort ();
12889 contents
12890 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
12891 if (contents == NULL)
12892 goto error_return;
12893 esdo->this_hdr.contents = contents;
12897 /* We have now assigned file positions for all the sections except .symtab,
12898 .strtab, and non-loaded reloc and compressed debugging sections. We start
12899 the .symtab section at the current file position, and write directly to it.
12900 We build the .strtab section in memory. */
12901 abfd->symcount = 0;
12902 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12903 /* sh_name is set in prep_headers. */
12904 symtab_hdr->sh_type = SHT_SYMTAB;
12905 /* sh_flags, sh_addr and sh_size all start off zero. */
12906 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
12907 /* sh_link is set in assign_section_numbers. */
12908 /* sh_info is set below. */
12909 /* sh_offset is set just below. */
12910 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
12912 if (max_sym_count < 20)
12913 max_sym_count = 20;
12914 htab->strtabsize = max_sym_count;
12915 amt = max_sym_count * sizeof (struct elf_sym_strtab);
12916 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
12917 if (htab->strtab == NULL)
12918 goto error_return;
12919 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12920 flinfo.symshndxbuf
12921 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
12922 ? (Elf_External_Sym_Shndx *) -1 : NULL);
12924 if (info->strip != strip_all || emit_relocs)
12926 file_ptr off = elf_next_file_pos (abfd);
12928 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, true, 0);
12930 /* Note that at this point elf_next_file_pos (abfd) is
12931 incorrect. We do not yet know the size of the .symtab section.
12932 We correct next_file_pos below, after we do know the size. */
12934 /* Start writing out the symbol table. The first symbol is always a
12935 dummy symbol. */
12936 elfsym.st_value = 0;
12937 elfsym.st_size = 0;
12938 elfsym.st_info = 0;
12939 elfsym.st_other = 0;
12940 elfsym.st_shndx = SHN_UNDEF;
12941 elfsym.st_target_internal = 0;
12942 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
12943 bfd_und_section_ptr, NULL) != 1)
12944 goto error_return;
12946 /* Output a symbol for each section if asked or they are used for
12947 relocs. These symbols usually have no names. We store the
12948 index of each one in the index field of the section, so that
12949 we can find it again when outputting relocs. */
12951 if (bfd_keep_unused_section_symbols (abfd) || emit_relocs)
12953 bool name_local_sections
12954 = (bed->elf_backend_name_local_section_symbols
12955 && bed->elf_backend_name_local_section_symbols (abfd));
12956 const char *name = NULL;
12958 elfsym.st_size = 0;
12959 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12960 elfsym.st_other = 0;
12961 elfsym.st_value = 0;
12962 elfsym.st_target_internal = 0;
12963 for (i = 1; i < elf_numsections (abfd); i++)
12965 o = bfd_section_from_elf_index (abfd, i);
12966 if (o != NULL)
12968 o->target_index = bfd_get_symcount (abfd);
12969 elfsym.st_shndx = i;
12970 if (!bfd_link_relocatable (info))
12971 elfsym.st_value = o->vma;
12972 if (name_local_sections)
12973 name = o->name;
12974 if (elf_link_output_symstrtab (&flinfo, name, &elfsym, o,
12975 NULL) != 1)
12976 goto error_return;
12982 /* On some targets like Irix 5 the symbol split between local and global
12983 ones recorded in the sh_info field needs to be done between section
12984 and all other symbols. */
12985 if (bed->elf_backend_elfsym_local_is_section
12986 && bed->elf_backend_elfsym_local_is_section (abfd))
12987 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12989 /* Allocate some memory to hold information read in from the input
12990 files. */
12991 if (max_contents_size != 0)
12993 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12994 if (flinfo.contents == NULL)
12995 goto error_return;
12998 if (max_external_reloc_size != 0)
13000 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
13001 if (flinfo.external_relocs == NULL)
13002 goto error_return;
13005 if (max_internal_reloc_count != 0)
13007 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
13008 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
13009 if (flinfo.internal_relocs == NULL)
13010 goto error_return;
13013 if (max_sym_count != 0)
13015 amt = max_sym_count * bed->s->sizeof_sym;
13016 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
13017 if (flinfo.external_syms == NULL)
13018 goto error_return;
13020 amt = max_sym_count * sizeof (Elf_Internal_Sym);
13021 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
13022 if (flinfo.internal_syms == NULL)
13023 goto error_return;
13025 amt = max_sym_count * sizeof (long);
13026 flinfo.indices = (long int *) bfd_malloc (amt);
13027 if (flinfo.indices == NULL)
13028 goto error_return;
13030 amt = max_sym_count * sizeof (asection *);
13031 flinfo.sections = (asection **) bfd_malloc (amt);
13032 if (flinfo.sections == NULL)
13033 goto error_return;
13036 if (max_sym_shndx_count != 0)
13038 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
13039 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
13040 if (flinfo.locsym_shndx == NULL)
13041 goto error_return;
13044 if (htab->tls_sec)
13046 bfd_vma base, end = 0; /* Both bytes. */
13047 asection *sec;
13049 for (sec = htab->tls_sec;
13050 sec && (sec->flags & SEC_THREAD_LOCAL);
13051 sec = sec->next)
13053 bfd_size_type size = sec->size;
13054 unsigned int opb = bfd_octets_per_byte (abfd, sec);
13056 if (size == 0
13057 && (sec->flags & SEC_HAS_CONTENTS) == 0)
13059 struct bfd_link_order *ord = sec->map_tail.link_order;
13061 if (ord != NULL)
13062 size = ord->offset * opb + ord->size;
13064 end = sec->vma + size / opb;
13066 base = htab->tls_sec->vma;
13067 /* Only align end of TLS section if static TLS doesn't have special
13068 alignment requirements. */
13069 if (bed->static_tls_alignment == 1)
13070 end = align_power (end, htab->tls_sec->alignment_power);
13071 htab->tls_size = end - base;
13074 if (!_bfd_elf_fixup_eh_frame_hdr (info))
13075 return false;
13077 /* Finish relative relocations here after regular symbol processing
13078 is finished if DT_RELR is enabled. */
13079 if (info->enable_dt_relr
13080 && bed->finish_relative_relocs
13081 && !bed->finish_relative_relocs (info))
13082 info->callbacks->einfo
13083 (_("%F%P: %pB: failed to finish relative relocations\n"), abfd);
13085 /* Since ELF permits relocations to be against local symbols, we
13086 must have the local symbols available when we do the relocations.
13087 Since we would rather only read the local symbols once, and we
13088 would rather not keep them in memory, we handle all the
13089 relocations for a single input file at the same time.
13091 Unfortunately, there is no way to know the total number of local
13092 symbols until we have seen all of them, and the local symbol
13093 indices precede the global symbol indices. This means that when
13094 we are generating relocatable output, and we see a reloc against
13095 a global symbol, we can not know the symbol index until we have
13096 finished examining all the local symbols to see which ones we are
13097 going to output. To deal with this, we keep the relocations in
13098 memory, and don't output them until the end of the link. This is
13099 an unfortunate waste of memory, but I don't see a good way around
13100 it. Fortunately, it only happens when performing a relocatable
13101 link, which is not the common case. FIXME: If keep_memory is set
13102 we could write the relocs out and then read them again; I don't
13103 know how bad the memory loss will be. */
13105 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13106 sub->output_has_begun = false;
13107 for (o = abfd->sections; o != NULL; o = o->next)
13109 for (p = o->map_head.link_order; p != NULL; p = p->next)
13111 if (p->type == bfd_indirect_link_order
13112 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
13113 == bfd_target_elf_flavour)
13114 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
13116 if (! sub->output_has_begun)
13118 if (! elf_link_input_bfd (&flinfo, sub))
13119 goto error_return;
13120 sub->output_has_begun = true;
13123 else if (p->type == bfd_section_reloc_link_order
13124 || p->type == bfd_symbol_reloc_link_order)
13126 if (! elf_reloc_link_order (abfd, info, o, p))
13127 goto error_return;
13129 else
13131 if (! _bfd_default_link_order (abfd, info, o, p))
13133 if (p->type == bfd_indirect_link_order
13134 && (bfd_get_flavour (sub)
13135 == bfd_target_elf_flavour)
13136 && (elf_elfheader (sub)->e_ident[EI_CLASS]
13137 != bed->s->elfclass))
13139 const char *iclass, *oclass;
13141 switch (bed->s->elfclass)
13143 case ELFCLASS64: oclass = "ELFCLASS64"; break;
13144 case ELFCLASS32: oclass = "ELFCLASS32"; break;
13145 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
13146 default: abort ();
13149 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
13151 case ELFCLASS64: iclass = "ELFCLASS64"; break;
13152 case ELFCLASS32: iclass = "ELFCLASS32"; break;
13153 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
13154 default: abort ();
13157 bfd_set_error (bfd_error_wrong_format);
13158 _bfd_error_handler
13159 /* xgettext:c-format */
13160 (_("%pB: file class %s incompatible with %s"),
13161 sub, iclass, oclass);
13164 goto error_return;
13170 /* Free symbol buffer if needed. */
13171 if (!info->reduce_memory_overheads)
13173 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13174 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
13176 free (elf_tdata (sub)->symbuf);
13177 elf_tdata (sub)->symbuf = NULL;
13181 /* Output any global symbols that got converted to local in a
13182 version script or due to symbol visibility. We do this in a
13183 separate step since ELF requires all local symbols to appear
13184 prior to any global symbols. FIXME: We should only do this if
13185 some global symbols were, in fact, converted to become local.
13186 FIXME: Will this work correctly with the Irix 5 linker? */
13187 eoinfo.failed = false;
13188 eoinfo.flinfo = &flinfo;
13189 eoinfo.localsyms = true;
13190 eoinfo.file_sym_done = false;
13191 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
13192 if (eoinfo.failed)
13193 goto error_return;
13195 /* If backend needs to output some local symbols not present in the hash
13196 table, do it now. */
13197 if (bed->elf_backend_output_arch_local_syms)
13199 if (! ((*bed->elf_backend_output_arch_local_syms)
13200 (abfd, info, &flinfo, elf_link_output_symstrtab)))
13201 goto error_return;
13204 /* That wrote out all the local symbols. Finish up the symbol table
13205 with the global symbols. Even if we want to strip everything we
13206 can, we still need to deal with those global symbols that got
13207 converted to local in a version script. */
13209 /* The sh_info field records the index of the first non local symbol. */
13210 if (!symtab_hdr->sh_info)
13211 symtab_hdr->sh_info = bfd_get_symcount (abfd);
13213 if (dynamic
13214 && htab->dynsym != NULL
13215 && htab->dynsym->output_section != bfd_abs_section_ptr)
13217 Elf_Internal_Sym sym;
13218 bfd_byte *dynsym = htab->dynsym->contents;
13220 o = htab->dynsym->output_section;
13221 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
13223 /* Write out the section symbols for the output sections. */
13224 if (bfd_link_pic (info)
13225 || htab->is_relocatable_executable)
13227 asection *s;
13229 sym.st_size = 0;
13230 sym.st_name = 0;
13231 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
13232 sym.st_other = 0;
13233 sym.st_target_internal = 0;
13235 for (s = abfd->sections; s != NULL; s = s->next)
13237 int indx;
13238 bfd_byte *dest;
13239 long dynindx;
13241 dynindx = elf_section_data (s)->dynindx;
13242 if (dynindx <= 0)
13243 continue;
13244 indx = elf_section_data (s)->this_idx;
13245 BFD_ASSERT (indx > 0);
13246 sym.st_shndx = indx;
13247 if (! check_dynsym (abfd, &sym))
13248 goto error_return;
13249 sym.st_value = s->vma;
13250 dest = dynsym + dynindx * bed->s->sizeof_sym;
13252 /* Inform the linker of the addition of this symbol. */
13254 if (info->callbacks->ctf_new_dynsym)
13255 info->callbacks->ctf_new_dynsym (dynindx, &sym);
13257 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
13261 /* Write out the local dynsyms. */
13262 if (htab->dynlocal)
13264 struct elf_link_local_dynamic_entry *e;
13265 for (e = htab->dynlocal; e ; e = e->next)
13267 asection *s;
13268 bfd_byte *dest;
13270 /* Copy the internal symbol and turn off visibility.
13271 Note that we saved a word of storage and overwrote
13272 the original st_name with the dynstr_index. */
13273 sym = e->isym;
13274 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
13275 sym.st_shndx = SHN_UNDEF;
13277 s = bfd_section_from_elf_index (e->input_bfd,
13278 e->isym.st_shndx);
13279 if (s != NULL
13280 && s->output_section != NULL
13281 && elf_section_data (s->output_section) != NULL)
13283 sym.st_shndx =
13284 elf_section_data (s->output_section)->this_idx;
13285 if (! check_dynsym (abfd, &sym))
13286 goto error_return;
13287 sym.st_value = (s->output_section->vma
13288 + s->output_offset
13289 + e->isym.st_value);
13292 /* Inform the linker of the addition of this symbol. */
13294 if (info->callbacks->ctf_new_dynsym)
13295 info->callbacks->ctf_new_dynsym (e->dynindx, &sym);
13297 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
13298 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
13303 /* We get the global symbols from the hash table. */
13304 eoinfo.failed = false;
13305 eoinfo.localsyms = false;
13306 eoinfo.flinfo = &flinfo;
13307 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
13308 if (eoinfo.failed)
13309 goto error_return;
13311 /* If backend needs to output some symbols not present in the hash
13312 table, do it now. */
13313 if (bed->elf_backend_output_arch_syms
13314 && (info->strip != strip_all || emit_relocs))
13316 if (! ((*bed->elf_backend_output_arch_syms)
13317 (abfd, info, &flinfo, elf_link_output_symstrtab)))
13318 goto error_return;
13321 /* Finalize the .strtab section. */
13322 _bfd_elf_strtab_finalize (flinfo.symstrtab);
13324 /* Swap out the .strtab section. */
13325 if (!elf_link_swap_symbols_out (&flinfo))
13326 goto error_return;
13327 free (htab->strtab);
13328 htab->strtab = NULL;
13330 /* Now we know the size of the symtab section. */
13331 if (bfd_get_symcount (abfd) > 0)
13333 /* Finish up and write out the symbol string table (.strtab)
13334 section. */
13335 Elf_Internal_Shdr *symstrtab_hdr = NULL;
13336 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
13338 if (elf_symtab_shndx_list (abfd))
13340 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
13342 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
13344 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
13345 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
13346 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
13347 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
13348 symtab_shndx_hdr->sh_size = amt;
13350 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
13351 off, true, 0);
13353 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
13354 || (bfd_write (flinfo.symshndxbuf, amt, abfd) != amt))
13355 goto error_return;
13359 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
13360 /* sh_name was set in prep_headers. */
13361 symstrtab_hdr->sh_type = SHT_STRTAB;
13362 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
13363 symstrtab_hdr->sh_addr = 0;
13364 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
13365 symstrtab_hdr->sh_entsize = 0;
13366 symstrtab_hdr->sh_link = 0;
13367 symstrtab_hdr->sh_info = 0;
13368 /* sh_offset is set just below. */
13369 symstrtab_hdr->sh_addralign = 1;
13371 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
13372 off, true, 0);
13373 elf_next_file_pos (abfd) = off;
13375 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
13376 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
13377 goto error_return;
13380 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
13382 _bfd_error_handler (_("%pB: failed to generate import library"),
13383 info->out_implib_bfd);
13384 goto error_return;
13387 /* Adjust the relocs to have the correct symbol indices. */
13388 for (o = abfd->sections; o != NULL; o = o->next)
13390 struct bfd_elf_section_data *esdo = elf_section_data (o);
13391 bool sort;
13393 if ((o->flags & SEC_RELOC) == 0)
13394 continue;
13396 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
13397 if (esdo->rel.hdr != NULL
13398 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
13399 goto error_return;
13400 if (esdo->rela.hdr != NULL
13401 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
13402 goto error_return;
13404 /* Set the reloc_count field to 0 to prevent write_relocs from
13405 trying to swap the relocs out itself. */
13406 o->reloc_count = 0;
13409 relativecount = 0;
13410 if (dynamic && info->combreloc && dynobj != NULL)
13411 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
13413 relr_entsize = 0;
13414 if (htab->srelrdyn != NULL
13415 && htab->srelrdyn->output_section != NULL
13416 && htab->srelrdyn->size != 0)
13418 asection *s = htab->srelrdyn->output_section;
13419 relr_entsize = elf_section_data (s)->this_hdr.sh_entsize;
13420 if (relr_entsize == 0)
13422 relr_entsize = bed->s->arch_size / 8;
13423 elf_section_data (s)->this_hdr.sh_entsize = relr_entsize;
13427 /* If we are linking against a dynamic object, or generating a
13428 shared library, finish up the dynamic linking information. */
13429 if (dynamic)
13431 bfd_byte *dyncon, *dynconend;
13433 /* Fix up .dynamic entries. */
13434 o = htab->dynamic;
13435 BFD_ASSERT (o != NULL);
13437 dyncon = o->contents;
13438 dynconend = PTR_ADD (o->contents, o->size);
13439 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13441 Elf_Internal_Dyn dyn;
13442 const char *name;
13443 unsigned int type;
13444 bfd_size_type sh_size;
13445 bfd_vma sh_addr;
13447 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13449 switch (dyn.d_tag)
13451 default:
13452 continue;
13453 case DT_NULL:
13454 if (relativecount != 0)
13456 switch (elf_section_data (reldyn)->this_hdr.sh_type)
13458 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
13459 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
13461 if (dyn.d_tag != DT_NULL
13462 && dynconend - dyncon >= bed->s->sizeof_dyn)
13464 dyn.d_un.d_val = relativecount;
13465 relativecount = 0;
13466 break;
13468 relativecount = 0;
13470 if (relr_entsize != 0)
13472 if (dynconend - dyncon >= 3 * bed->s->sizeof_dyn)
13474 asection *s = htab->srelrdyn;
13475 dyn.d_tag = DT_RELR;
13476 dyn.d_un.d_ptr
13477 = s->output_section->vma + s->output_offset;
13478 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13479 dyncon += bed->s->sizeof_dyn;
13481 dyn.d_tag = DT_RELRSZ;
13482 dyn.d_un.d_val = s->size;
13483 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13484 dyncon += bed->s->sizeof_dyn;
13486 dyn.d_tag = DT_RELRENT;
13487 dyn.d_un.d_val = relr_entsize;
13488 relr_entsize = 0;
13489 break;
13491 relr_entsize = 0;
13493 continue;
13495 case DT_INIT:
13496 name = info->init_function;
13497 goto get_sym;
13498 case DT_FINI:
13499 name = info->fini_function;
13500 get_sym:
13502 struct elf_link_hash_entry *h;
13504 h = elf_link_hash_lookup (htab, name, false, false, true);
13505 if (h != NULL
13506 && (h->root.type == bfd_link_hash_defined
13507 || h->root.type == bfd_link_hash_defweak))
13509 dyn.d_un.d_ptr = h->root.u.def.value;
13510 o = h->root.u.def.section;
13511 if (o->output_section != NULL)
13512 dyn.d_un.d_ptr += (o->output_section->vma
13513 + o->output_offset);
13514 else
13516 /* The symbol is imported from another shared
13517 library and does not apply to this one. */
13518 dyn.d_un.d_ptr = 0;
13520 break;
13523 continue;
13525 case DT_PREINIT_ARRAYSZ:
13526 name = ".preinit_array";
13527 goto get_out_size;
13528 case DT_INIT_ARRAYSZ:
13529 name = ".init_array";
13530 goto get_out_size;
13531 case DT_FINI_ARRAYSZ:
13532 name = ".fini_array";
13533 get_out_size:
13534 o = bfd_get_section_by_name (abfd, name);
13535 if (o == NULL)
13537 _bfd_error_handler
13538 (_("could not find section %s"), name);
13539 goto error_return;
13541 if (o->size == 0)
13542 _bfd_error_handler
13543 (_("warning: %s section has zero size"), name);
13544 dyn.d_un.d_val = o->size;
13545 break;
13547 case DT_PREINIT_ARRAY:
13548 name = ".preinit_array";
13549 goto get_out_vma;
13550 case DT_INIT_ARRAY:
13551 name = ".init_array";
13552 goto get_out_vma;
13553 case DT_FINI_ARRAY:
13554 name = ".fini_array";
13555 get_out_vma:
13556 o = bfd_get_section_by_name (abfd, name);
13557 goto do_vma;
13559 case DT_HASH:
13560 name = ".hash";
13561 goto get_vma;
13562 case DT_GNU_HASH:
13563 name = ".gnu.hash";
13564 goto get_vma;
13565 case DT_STRTAB:
13566 name = ".dynstr";
13567 goto get_vma;
13568 case DT_SYMTAB:
13569 name = ".dynsym";
13570 goto get_vma;
13571 case DT_VERDEF:
13572 name = ".gnu.version_d";
13573 goto get_vma;
13574 case DT_VERNEED:
13575 name = ".gnu.version_r";
13576 goto get_vma;
13577 case DT_VERSYM:
13578 name = ".gnu.version";
13579 get_vma:
13580 o = bfd_get_linker_section (dynobj, name);
13581 do_vma:
13582 if (o == NULL || bfd_is_abs_section (o->output_section))
13584 _bfd_error_handler
13585 (_("could not find section %s"), name);
13586 goto error_return;
13588 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
13590 _bfd_error_handler
13591 (_("warning: section '%s' is being made into a note"), name);
13592 bfd_set_error (bfd_error_nonrepresentable_section);
13593 goto error_return;
13595 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
13596 break;
13598 case DT_REL:
13599 case DT_RELA:
13600 case DT_RELSZ:
13601 case DT_RELASZ:
13602 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
13603 type = SHT_REL;
13604 else
13605 type = SHT_RELA;
13606 sh_size = 0;
13607 sh_addr = 0;
13608 for (i = 1; i < elf_numsections (abfd); i++)
13610 Elf_Internal_Shdr *hdr;
13612 hdr = elf_elfsections (abfd)[i];
13613 if (hdr->sh_type == type
13614 && (hdr->sh_flags & SHF_ALLOC) != 0)
13616 sh_size += hdr->sh_size;
13617 if (sh_addr == 0
13618 || sh_addr > hdr->sh_addr)
13619 sh_addr = hdr->sh_addr;
13623 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
13625 unsigned int opb = bfd_octets_per_byte (abfd, o);
13627 /* Don't count procedure linkage table relocs in the
13628 overall reloc count. */
13629 sh_size -= htab->srelplt->size;
13630 if (sh_size == 0)
13631 /* If the size is zero, make the address zero too.
13632 This is to avoid a glibc bug. If the backend
13633 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
13634 zero, then we'll put DT_RELA at the end of
13635 DT_JMPREL. glibc will interpret the end of
13636 DT_RELA matching the end of DT_JMPREL as the
13637 case where DT_RELA includes DT_JMPREL, and for
13638 LD_BIND_NOW will decide that processing DT_RELA
13639 will process the PLT relocs too. Net result:
13640 No PLT relocs applied. */
13641 sh_addr = 0;
13643 /* If .rela.plt is the first .rela section, exclude
13644 it from DT_RELA. */
13645 else if (sh_addr == (htab->srelplt->output_section->vma
13646 + htab->srelplt->output_offset) * opb)
13647 sh_addr += htab->srelplt->size;
13650 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
13651 dyn.d_un.d_val = sh_size;
13652 else
13653 dyn.d_un.d_ptr = sh_addr;
13654 break;
13656 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13660 /* If we have created any dynamic sections, then output them. */
13661 if (dynobj != NULL)
13663 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
13664 goto error_return;
13666 /* Check for DT_TEXTREL (late, in case the backend removes it). */
13667 if (bfd_link_textrel_check (info)
13668 && (o = htab->dynamic) != NULL
13669 && o->size != 0)
13671 bfd_byte *dyncon, *dynconend;
13673 dyncon = o->contents;
13674 dynconend = o->contents + o->size;
13675 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13677 Elf_Internal_Dyn dyn;
13679 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13681 if (dyn.d_tag == DT_TEXTREL)
13683 if (info->textrel_check == textrel_check_error)
13684 info->callbacks->einfo
13685 (_("%P%X: read-only segment has dynamic relocations\n"));
13686 else if (bfd_link_dll (info))
13687 info->callbacks->einfo
13688 (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
13689 else if (bfd_link_pde (info))
13690 info->callbacks->einfo
13691 (_("%P: warning: creating DT_TEXTREL in a PDE\n"));
13692 else
13693 info->callbacks->einfo
13694 (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
13695 break;
13700 for (o = dynobj->sections; o != NULL; o = o->next)
13702 if ((o->flags & SEC_HAS_CONTENTS) == 0
13703 || o->size == 0
13704 || o->output_section == bfd_abs_section_ptr)
13705 continue;
13706 if ((o->flags & SEC_LINKER_CREATED) == 0)
13708 /* At this point, we are only interested in sections
13709 created by _bfd_elf_link_create_dynamic_sections. */
13710 continue;
13712 if (htab->stab_info.stabstr == o)
13713 continue;
13714 if (htab->eh_info.hdr_sec == o)
13715 continue;
13716 if (strcmp (o->name, ".dynstr") != 0)
13718 bfd_size_type octets = ((file_ptr) o->output_offset
13719 * bfd_octets_per_byte (abfd, o));
13720 if (!bfd_set_section_contents (abfd, o->output_section,
13721 o->contents, octets, o->size))
13722 goto error_return;
13724 else
13726 /* The contents of the .dynstr section are actually in a
13727 stringtab. */
13728 file_ptr off;
13730 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
13731 if (bfd_seek (abfd, off, SEEK_SET) != 0
13732 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
13733 goto error_return;
13738 if (!info->resolve_section_groups)
13740 bool failed = false;
13742 BFD_ASSERT (bfd_link_relocatable (info));
13743 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
13744 if (failed)
13745 goto error_return;
13748 /* If we have optimized stabs strings, output them. */
13749 if (htab->stab_info.stabstr != NULL)
13751 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
13752 goto error_return;
13755 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
13756 goto error_return;
13758 if (! _bfd_elf_write_section_sframe (abfd, info))
13759 goto error_return;
13761 if (info->callbacks->emit_ctf)
13762 info->callbacks->emit_ctf ();
13764 elf_final_link_free (abfd, &flinfo);
13766 if (attr_section)
13768 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
13769 if (contents == NULL)
13770 goto error_ret2;
13771 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
13772 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
13773 free (contents);
13776 if (info->unique_symbol)
13777 bfd_hash_table_free (&flinfo.local_hash_table);
13778 return true;
13780 error_return:
13781 free (htab->strtab);
13782 htab->strtab = NULL;
13783 elf_final_link_free (abfd, &flinfo);
13784 error_ret2:
13785 if (info->unique_symbol)
13786 bfd_hash_table_free (&flinfo.local_hash_table);
13787 return false;
13790 /* Initialize COOKIE for input bfd ABFD. */
13792 static bool
13793 init_reloc_cookie (struct elf_reloc_cookie *cookie,
13794 struct bfd_link_info *info, bfd *abfd,
13795 bool keep_memory)
13797 Elf_Internal_Shdr *symtab_hdr;
13798 const struct elf_backend_data *bed;
13800 bed = get_elf_backend_data (abfd);
13801 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13803 cookie->abfd = abfd;
13804 cookie->sym_hashes = elf_sym_hashes (abfd);
13805 cookie->bad_symtab = elf_bad_symtab (abfd);
13806 if (cookie->bad_symtab)
13808 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13809 cookie->extsymoff = 0;
13811 else
13813 cookie->locsymcount = symtab_hdr->sh_info;
13814 cookie->extsymoff = symtab_hdr->sh_info;
13817 if (bed->s->arch_size == 32)
13818 cookie->r_sym_shift = 8;
13819 else
13820 cookie->r_sym_shift = 32;
13822 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
13823 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
13825 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
13826 cookie->locsymcount, 0,
13827 NULL, NULL, NULL);
13828 if (cookie->locsyms == NULL)
13830 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
13831 return false;
13833 if (keep_memory || _bfd_elf_link_keep_memory (info))
13835 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
13836 info->cache_size += (cookie->locsymcount
13837 * sizeof (Elf_Internal_Sym));
13840 return true;
13843 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
13845 static void
13846 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
13848 Elf_Internal_Shdr *symtab_hdr;
13850 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13851 if (symtab_hdr->contents != (unsigned char *) cookie->locsyms)
13852 free (cookie->locsyms);
13855 /* Initialize the relocation information in COOKIE for input section SEC
13856 of input bfd ABFD. */
13858 static bool
13859 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13860 struct bfd_link_info *info, bfd *abfd,
13861 asection *sec, bool keep_memory)
13863 if (sec->reloc_count == 0)
13865 cookie->rels = NULL;
13866 cookie->relend = NULL;
13868 else
13870 cookie->rels = _bfd_elf_link_info_read_relocs
13871 (abfd, info, sec, NULL, NULL,
13872 keep_memory || _bfd_elf_link_keep_memory (info));
13873 if (cookie->rels == NULL)
13874 return false;
13875 cookie->rel = cookie->rels;
13876 cookie->relend = cookie->rels + sec->reloc_count;
13878 cookie->rel = cookie->rels;
13879 return true;
13882 /* Free the memory allocated by init_reloc_cookie_rels,
13883 if appropriate. */
13885 static void
13886 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13887 asection *sec)
13889 if (elf_section_data (sec)->relocs != cookie->rels)
13890 free (cookie->rels);
13893 /* Initialize the whole of COOKIE for input section SEC. */
13895 static bool
13896 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13897 struct bfd_link_info *info,
13898 asection *sec, bool keep_memory)
13900 if (!init_reloc_cookie (cookie, info, sec->owner, keep_memory))
13901 goto error1;
13902 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec,
13903 keep_memory))
13904 goto error2;
13905 return true;
13907 error2:
13908 fini_reloc_cookie (cookie, sec->owner);
13909 error1:
13910 return false;
13913 /* Free the memory allocated by init_reloc_cookie_for_section,
13914 if appropriate. */
13916 static void
13917 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13918 asection *sec)
13920 fini_reloc_cookie_rels (cookie, sec);
13921 fini_reloc_cookie (cookie, sec->owner);
13924 /* Garbage collect unused sections. */
13926 /* Default gc_mark_hook. */
13928 asection *
13929 _bfd_elf_gc_mark_hook (asection *sec,
13930 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13931 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13932 struct elf_link_hash_entry *h,
13933 Elf_Internal_Sym *sym)
13935 if (h != NULL)
13937 switch (h->root.type)
13939 case bfd_link_hash_defined:
13940 case bfd_link_hash_defweak:
13941 return h->root.u.def.section;
13943 case bfd_link_hash_common:
13944 return h->root.u.c.p->section;
13946 default:
13947 break;
13950 else
13951 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
13953 return NULL;
13956 /* Return the debug definition section. */
13958 static asection *
13959 elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
13960 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13961 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13962 struct elf_link_hash_entry *h,
13963 Elf_Internal_Sym *sym)
13965 if (h != NULL)
13967 /* Return the global debug definition section. */
13968 if ((h->root.type == bfd_link_hash_defined
13969 || h->root.type == bfd_link_hash_defweak)
13970 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
13971 return h->root.u.def.section;
13973 else
13975 /* Return the local debug definition section. */
13976 asection *isec = bfd_section_from_elf_index (sec->owner,
13977 sym->st_shndx);
13978 if (isec != NULL && (isec->flags & SEC_DEBUGGING) != 0)
13979 return isec;
13982 return NULL;
13985 /* COOKIE->rel describes a relocation against section SEC, which is
13986 a section we've decided to keep. Return the section that contains
13987 the relocation symbol, or NULL if no section contains it. */
13989 asection *
13990 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
13991 elf_gc_mark_hook_fn gc_mark_hook,
13992 struct elf_reloc_cookie *cookie,
13993 bool *start_stop)
13995 unsigned long r_symndx;
13996 struct elf_link_hash_entry *h, *hw;
13998 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
13999 if (r_symndx == STN_UNDEF)
14000 return NULL;
14002 if (r_symndx >= cookie->locsymcount
14003 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
14005 bool was_marked;
14007 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
14008 if (h == NULL)
14010 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
14011 sec->owner);
14012 return NULL;
14014 while (h->root.type == bfd_link_hash_indirect
14015 || h->root.type == bfd_link_hash_warning)
14016 h = (struct elf_link_hash_entry *) h->root.u.i.link;
14018 was_marked = h->mark;
14019 h->mark = 1;
14020 /* Keep all aliases of the symbol too. If an object symbol
14021 needs to be copied into .dynbss then all of its aliases
14022 should be present as dynamic symbols, not just the one used
14023 on the copy relocation. */
14024 hw = h;
14025 while (hw->is_weakalias)
14027 hw = hw->u.alias;
14028 hw->mark = 1;
14031 if (!was_marked && h->start_stop && !h->root.ldscript_def)
14033 if (info->start_stop_gc)
14034 return NULL;
14036 /* To work around a glibc bug, mark XXX input sections
14037 when there is a reference to __start_XXX or __stop_XXX
14038 symbols. */
14039 else if (start_stop != NULL)
14041 asection *s = h->u2.start_stop_section;
14042 *start_stop = true;
14043 return s;
14047 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
14050 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
14051 &cookie->locsyms[r_symndx]);
14054 /* COOKIE->rel describes a relocation against section SEC, which is
14055 a section we've decided to keep. Mark the section that contains
14056 the relocation symbol. */
14058 bool
14059 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
14060 asection *sec,
14061 elf_gc_mark_hook_fn gc_mark_hook,
14062 struct elf_reloc_cookie *cookie)
14064 asection *rsec;
14065 bool start_stop = false;
14067 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
14068 while (rsec != NULL)
14070 if (!rsec->gc_mark)
14072 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
14073 || (rsec->owner->flags & DYNAMIC) != 0)
14074 rsec->gc_mark = 1;
14075 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
14076 return false;
14078 if (!start_stop)
14079 break;
14080 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
14082 return true;
14085 /* The mark phase of garbage collection. For a given section, mark
14086 it and any sections in this section's group, and all the sections
14087 which define symbols to which it refers. */
14089 bool
14090 _bfd_elf_gc_mark (struct bfd_link_info *info,
14091 asection *sec,
14092 elf_gc_mark_hook_fn gc_mark_hook)
14094 bool ret;
14095 asection *group_sec, *eh_frame;
14097 sec->gc_mark = 1;
14099 /* Mark all the sections in the group. */
14100 group_sec = elf_section_data (sec)->next_in_group;
14101 if (group_sec && !group_sec->gc_mark)
14102 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
14103 return false;
14105 /* Look through the section relocs. */
14106 ret = true;
14107 eh_frame = elf_eh_frame_section (sec->owner);
14108 if ((sec->flags & SEC_RELOC) != 0
14109 && sec->reloc_count > 0
14110 && sec != eh_frame)
14112 struct elf_reloc_cookie cookie;
14114 if (!init_reloc_cookie_for_section (&cookie, info, sec, false))
14115 ret = false;
14116 else
14118 for (; cookie.rel < cookie.relend; cookie.rel++)
14119 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
14121 ret = false;
14122 break;
14124 fini_reloc_cookie_for_section (&cookie, sec);
14128 if (ret && eh_frame && elf_fde_list (sec))
14130 struct elf_reloc_cookie cookie;
14132 /* NB: When --no-keep-memory is used, the symbol table and
14133 relocation info for eh_frame are freed after they are retrieved
14134 for each text section in the input object. If an input object
14135 has many text sections, the same data is retrieved and freed
14136 many times which can take a very long time. Always keep the
14137 symbol table and relocation info for eh_frame to avoid it. */
14138 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame,
14139 true))
14140 ret = false;
14141 else
14143 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
14144 gc_mark_hook, &cookie))
14145 ret = false;
14146 fini_reloc_cookie_for_section (&cookie, eh_frame);
14150 eh_frame = elf_section_eh_frame_entry (sec);
14151 if (ret && eh_frame && !eh_frame->gc_mark)
14152 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
14153 ret = false;
14155 return ret;
14158 /* Scan and mark sections in a special or debug section group. */
14160 static void
14161 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
14163 /* Point to first section of section group. */
14164 asection *ssec;
14165 /* Used to iterate the section group. */
14166 asection *msec;
14168 bool is_special_grp = true;
14169 bool is_debug_grp = true;
14171 /* First scan to see if group contains any section other than debug
14172 and special section. */
14173 ssec = msec = elf_next_in_group (grp);
14176 if ((msec->flags & SEC_DEBUGGING) == 0)
14177 is_debug_grp = false;
14179 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
14180 is_special_grp = false;
14182 msec = elf_next_in_group (msec);
14184 while (msec != ssec);
14186 /* If this is a pure debug section group or pure special section group,
14187 keep all sections in this group. */
14188 if (is_debug_grp || is_special_grp)
14192 msec->gc_mark = 1;
14193 msec = elf_next_in_group (msec);
14195 while (msec != ssec);
14199 /* Keep debug and special sections. */
14201 bool
14202 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
14203 elf_gc_mark_hook_fn mark_hook)
14205 bfd *ibfd;
14207 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14209 asection *isec;
14210 bool some_kept;
14211 bool debug_frag_seen;
14212 bool has_kept_debug_info;
14214 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
14215 continue;
14216 isec = ibfd->sections;
14217 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14218 continue;
14220 /* Ensure all linker created sections are kept,
14221 see if any other section is already marked,
14222 and note if we have any fragmented debug sections. */
14223 debug_frag_seen = some_kept = has_kept_debug_info = false;
14224 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14226 if ((isec->flags & SEC_LINKER_CREATED) != 0)
14227 isec->gc_mark = 1;
14228 else if (isec->gc_mark
14229 && (isec->flags & SEC_ALLOC) != 0
14230 && elf_section_type (isec) != SHT_NOTE)
14231 some_kept = true;
14232 else
14234 /* Since all sections, except for backend specific ones,
14235 have been garbage collected, call mark_hook on this
14236 section if any of its linked-to sections is marked. */
14237 asection *linked_to_sec;
14238 for (linked_to_sec = elf_linked_to_section (isec);
14239 linked_to_sec != NULL && !linked_to_sec->linker_mark;
14240 linked_to_sec = elf_linked_to_section (linked_to_sec))
14242 if (linked_to_sec->gc_mark)
14244 if (!_bfd_elf_gc_mark (info, isec, mark_hook))
14245 return false;
14246 break;
14248 linked_to_sec->linker_mark = 1;
14250 for (linked_to_sec = elf_linked_to_section (isec);
14251 linked_to_sec != NULL && linked_to_sec->linker_mark;
14252 linked_to_sec = elf_linked_to_section (linked_to_sec))
14253 linked_to_sec->linker_mark = 0;
14256 if (!debug_frag_seen
14257 && (isec->flags & SEC_DEBUGGING)
14258 && startswith (isec->name, ".debug_line."))
14259 debug_frag_seen = true;
14260 else if (strcmp (bfd_section_name (isec),
14261 "__patchable_function_entries") == 0
14262 && elf_linked_to_section (isec) == NULL)
14263 info->callbacks->einfo (_("%F%P: %pB(%pA): error: "
14264 "need linked-to section "
14265 "for --gc-sections\n"),
14266 isec->owner, isec);
14269 /* If no non-note alloc section in this file will be kept, then
14270 we can toss out the debug and special sections. */
14271 if (!some_kept)
14272 continue;
14274 /* Keep debug and special sections like .comment when they are
14275 not part of a group. Also keep section groups that contain
14276 just debug sections or special sections. NB: Sections with
14277 linked-to section has been handled above. */
14278 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14280 if ((isec->flags & SEC_GROUP) != 0)
14281 _bfd_elf_gc_mark_debug_special_section_group (isec);
14282 else if (((isec->flags & SEC_DEBUGGING) != 0
14283 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
14284 && elf_next_in_group (isec) == NULL
14285 && elf_linked_to_section (isec) == NULL)
14286 isec->gc_mark = 1;
14287 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
14288 has_kept_debug_info = true;
14291 /* Look for CODE sections which are going to be discarded,
14292 and find and discard any fragmented debug sections which
14293 are associated with that code section. */
14294 if (debug_frag_seen)
14295 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14296 if ((isec->flags & SEC_CODE) != 0
14297 && isec->gc_mark == 0)
14299 unsigned int ilen;
14300 asection *dsec;
14302 ilen = strlen (isec->name);
14304 /* Association is determined by the name of the debug
14305 section containing the name of the code section as
14306 a suffix. For example .debug_line.text.foo is a
14307 debug section associated with .text.foo. */
14308 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
14310 unsigned int dlen;
14312 if (dsec->gc_mark == 0
14313 || (dsec->flags & SEC_DEBUGGING) == 0)
14314 continue;
14316 dlen = strlen (dsec->name);
14318 if (dlen > ilen
14319 && strncmp (dsec->name + (dlen - ilen),
14320 isec->name, ilen) == 0)
14321 dsec->gc_mark = 0;
14325 /* Mark debug sections referenced by kept debug sections. */
14326 if (has_kept_debug_info)
14327 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14328 if (isec->gc_mark
14329 && (isec->flags & SEC_DEBUGGING) != 0)
14330 if (!_bfd_elf_gc_mark (info, isec,
14331 elf_gc_mark_debug_section))
14332 return false;
14334 return true;
14337 static bool
14338 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
14340 bfd *sub;
14341 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14343 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
14345 asection *o;
14347 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
14348 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
14349 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
14350 continue;
14351 o = sub->sections;
14352 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14353 continue;
14355 for (o = sub->sections; o != NULL; o = o->next)
14357 /* When any section in a section group is kept, we keep all
14358 sections in the section group. If the first member of
14359 the section group is excluded, we will also exclude the
14360 group section. */
14361 if (o->flags & SEC_GROUP)
14363 asection *first = elf_next_in_group (o);
14364 o->gc_mark = first->gc_mark;
14367 if (o->gc_mark)
14368 continue;
14370 /* Skip sweeping sections already excluded. */
14371 if (o->flags & SEC_EXCLUDE)
14372 continue;
14374 /* Since this is early in the link process, it is simple
14375 to remove a section from the output. */
14376 o->flags |= SEC_EXCLUDE;
14378 if (info->print_gc_sections && o->size != 0)
14379 /* xgettext:c-format */
14380 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
14381 o, sub);
14385 return true;
14388 /* Propagate collected vtable information. This is called through
14389 elf_link_hash_traverse. */
14391 static bool
14392 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
14394 /* Those that are not vtables. */
14395 if (h->start_stop
14396 || h->u2.vtable == NULL
14397 || h->u2.vtable->parent == NULL)
14398 return true;
14400 /* Those vtables that do not have parents, we cannot merge. */
14401 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
14402 return true;
14404 /* If we've already been done, exit. */
14405 if (h->u2.vtable->used && h->u2.vtable->used[-1])
14406 return true;
14408 /* Make sure the parent's table is up to date. */
14409 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
14411 if (h->u2.vtable->used == NULL)
14413 /* None of this table's entries were referenced. Re-use the
14414 parent's table. */
14415 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
14416 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
14418 else
14420 size_t n;
14421 bool *cu, *pu;
14423 /* Or the parent's entries into ours. */
14424 cu = h->u2.vtable->used;
14425 cu[-1] = true;
14426 pu = h->u2.vtable->parent->u2.vtable->used;
14427 if (pu != NULL)
14429 const struct elf_backend_data *bed;
14430 unsigned int log_file_align;
14432 bed = get_elf_backend_data (h->root.u.def.section->owner);
14433 log_file_align = bed->s->log_file_align;
14434 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
14435 while (n--)
14437 if (*pu)
14438 *cu = true;
14439 pu++;
14440 cu++;
14445 return true;
14448 struct link_info_ok
14450 struct bfd_link_info *info;
14451 bool ok;
14454 static bool
14455 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h,
14456 void *ptr)
14458 asection *sec;
14459 bfd_vma hstart, hend;
14460 Elf_Internal_Rela *relstart, *relend, *rel;
14461 const struct elf_backend_data *bed;
14462 unsigned int log_file_align;
14463 struct link_info_ok *info = (struct link_info_ok *) ptr;
14465 /* Take care of both those symbols that do not describe vtables as
14466 well as those that are not loaded. */
14467 if (h->start_stop
14468 || h->u2.vtable == NULL
14469 || h->u2.vtable->parent == NULL)
14470 return true;
14472 BFD_ASSERT (h->root.type == bfd_link_hash_defined
14473 || h->root.type == bfd_link_hash_defweak);
14475 sec = h->root.u.def.section;
14476 hstart = h->root.u.def.value;
14477 hend = hstart + h->size;
14479 relstart = _bfd_elf_link_info_read_relocs (sec->owner, info->info,
14480 sec, NULL, NULL, true);
14481 if (!relstart)
14482 return info->ok = false;
14483 bed = get_elf_backend_data (sec->owner);
14484 log_file_align = bed->s->log_file_align;
14486 relend = relstart + sec->reloc_count;
14488 for (rel = relstart; rel < relend; ++rel)
14489 if (rel->r_offset >= hstart && rel->r_offset < hend)
14491 /* If the entry is in use, do nothing. */
14492 if (h->u2.vtable->used
14493 && (rel->r_offset - hstart) < h->u2.vtable->size)
14495 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
14496 if (h->u2.vtable->used[entry])
14497 continue;
14499 /* Otherwise, kill it. */
14500 rel->r_offset = rel->r_info = rel->r_addend = 0;
14503 return true;
14506 /* Mark sections containing dynamically referenced symbols. When
14507 building shared libraries, we must assume that any visible symbol is
14508 referenced. */
14510 bool
14511 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
14513 struct bfd_link_info *info = (struct bfd_link_info *) inf;
14514 struct bfd_elf_dynamic_list *d = info->dynamic_list;
14516 if ((h->root.type == bfd_link_hash_defined
14517 || h->root.type == bfd_link_hash_defweak)
14518 && (!h->start_stop
14519 || h->root.ldscript_def
14520 || !info->start_stop_gc)
14521 && ((h->ref_dynamic && !h->forced_local)
14522 || ((h->def_regular || ELF_COMMON_DEF_P (h))
14523 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
14524 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
14525 && (!bfd_link_executable (info)
14526 || info->gc_keep_exported
14527 || info->export_dynamic
14528 || (h->dynamic
14529 && d != NULL
14530 && (*d->match) (&d->head, NULL, h->root.root.string)))
14531 && (h->versioned >= versioned
14532 || !bfd_hide_sym_by_version (info->version_info,
14533 h->root.root.string)))))
14534 h->root.u.def.section->flags |= SEC_KEEP;
14536 return true;
14539 /* Keep all sections containing symbols undefined on the command-line,
14540 and the section containing the entry symbol. */
14542 void
14543 _bfd_elf_gc_keep (struct bfd_link_info *info)
14545 struct bfd_sym_chain *sym;
14547 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
14549 struct elf_link_hash_entry *h;
14551 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
14552 false, false, false);
14554 if (h != NULL
14555 && (h->root.type == bfd_link_hash_defined
14556 || h->root.type == bfd_link_hash_defweak)
14557 && !bfd_is_const_section (h->root.u.def.section))
14558 h->root.u.def.section->flags |= SEC_KEEP;
14562 bool
14563 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
14564 struct bfd_link_info *info)
14566 bfd *ibfd = info->input_bfds;
14568 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14570 asection *sec;
14571 struct elf_reloc_cookie cookie;
14573 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
14574 continue;
14575 sec = ibfd->sections;
14576 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14577 continue;
14579 if (!init_reloc_cookie (&cookie, info, ibfd, false))
14580 return false;
14582 for (sec = ibfd->sections; sec; sec = sec->next)
14584 if (startswith (bfd_section_name (sec), ".eh_frame_entry")
14585 && init_reloc_cookie_rels (&cookie, info, ibfd, sec,
14586 false))
14588 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
14589 fini_reloc_cookie_rels (&cookie, sec);
14593 return true;
14596 /* Do mark and sweep of unused sections. */
14598 bool
14599 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
14601 bool ok = true;
14602 bfd *sub;
14603 elf_gc_mark_hook_fn gc_mark_hook;
14604 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14605 struct elf_link_hash_table *htab;
14606 struct link_info_ok info_ok;
14608 if (!bed->can_gc_sections
14609 || !is_elf_hash_table (info->hash))
14611 _bfd_error_handler(_("warning: gc-sections option ignored"));
14612 return true;
14615 bed->gc_keep (info);
14616 htab = elf_hash_table (info);
14618 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
14619 at the .eh_frame section if we can mark the FDEs individually. */
14620 for (sub = info->input_bfds;
14621 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
14622 sub = sub->link.next)
14624 asection *sec;
14625 struct elf_reloc_cookie cookie;
14627 sec = sub->sections;
14628 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14629 continue;
14630 sec = bfd_get_section_by_name (sub, ".eh_frame");
14631 while (sec && init_reloc_cookie_for_section (&cookie, info, sec,
14632 false))
14634 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
14635 if (elf_section_data (sec)->sec_info
14636 && (sec->flags & SEC_LINKER_CREATED) == 0)
14637 elf_eh_frame_section (sub) = sec;
14638 fini_reloc_cookie_for_section (&cookie, sec);
14639 sec = bfd_get_next_section_by_name (NULL, sec);
14643 /* Apply transitive closure to the vtable entry usage info. */
14644 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
14645 if (!ok)
14646 return false;
14648 /* Kill the vtable relocations that were not used. */
14649 info_ok.info = info;
14650 info_ok.ok = true;
14651 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &info_ok);
14652 if (!info_ok.ok)
14653 return false;
14655 /* Mark dynamically referenced symbols. */
14656 if (htab->dynamic_sections_created || info->gc_keep_exported)
14657 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
14659 /* Grovel through relocs to find out who stays ... */
14660 gc_mark_hook = bed->gc_mark_hook;
14661 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
14663 asection *o;
14665 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
14666 || elf_object_id (sub) != elf_hash_table_id (htab)
14667 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
14668 continue;
14670 o = sub->sections;
14671 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14672 continue;
14674 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
14675 Also treat note sections as a root, if the section is not part
14676 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
14677 well as FINI_ARRAY sections for ld -r. */
14678 for (o = sub->sections; o != NULL; o = o->next)
14679 if (!o->gc_mark
14680 && (o->flags & SEC_EXCLUDE) == 0
14681 && ((o->flags & SEC_KEEP) != 0
14682 || (bfd_link_relocatable (info)
14683 && ((elf_section_data (o)->this_hdr.sh_type
14684 == SHT_PREINIT_ARRAY)
14685 || (elf_section_data (o)->this_hdr.sh_type
14686 == SHT_INIT_ARRAY)
14687 || (elf_section_data (o)->this_hdr.sh_type
14688 == SHT_FINI_ARRAY)))
14689 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
14690 && elf_next_in_group (o) == NULL
14691 && elf_linked_to_section (o) == NULL)
14692 || ((elf_tdata (sub)->has_gnu_osabi & elf_gnu_osabi_retain)
14693 && (elf_section_flags (o) & SHF_GNU_RETAIN))))
14695 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
14696 return false;
14700 /* Allow the backend to mark additional target specific sections. */
14701 bed->gc_mark_extra_sections (info, gc_mark_hook);
14703 /* ... and mark SEC_EXCLUDE for those that go. */
14704 return elf_gc_sweep (abfd, info);
14707 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
14709 bool
14710 bfd_elf_gc_record_vtinherit (bfd *abfd,
14711 asection *sec,
14712 struct elf_link_hash_entry *h,
14713 bfd_vma offset)
14715 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
14716 struct elf_link_hash_entry **search, *child;
14717 size_t extsymcount;
14718 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14720 /* The sh_info field of the symtab header tells us where the
14721 external symbols start. We don't care about the local symbols at
14722 this point. */
14723 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
14724 if (!elf_bad_symtab (abfd))
14725 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
14727 sym_hashes = elf_sym_hashes (abfd);
14728 sym_hashes_end = PTR_ADD (sym_hashes, extsymcount);
14730 /* Hunt down the child symbol, which is in this section at the same
14731 offset as the relocation. */
14732 for (search = sym_hashes; search != sym_hashes_end; ++search)
14734 if ((child = *search) != NULL
14735 && (child->root.type == bfd_link_hash_defined
14736 || child->root.type == bfd_link_hash_defweak)
14737 && child->root.u.def.section == sec
14738 && child->root.u.def.value == offset)
14739 goto win;
14742 /* xgettext:c-format */
14743 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
14744 abfd, sec, (uint64_t) offset);
14745 bfd_set_error (bfd_error_invalid_operation);
14746 return false;
14748 win:
14749 if (!child->u2.vtable)
14751 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
14752 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
14753 if (!child->u2.vtable)
14754 return false;
14756 if (!h)
14758 /* This *should* only be the absolute section. It could potentially
14759 be that someone has defined a non-global vtable though, which
14760 would be bad. It isn't worth paging in the local symbols to be
14761 sure though; that case should simply be handled by the assembler. */
14763 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
14765 else
14766 child->u2.vtable->parent = h;
14768 return true;
14771 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
14773 bool
14774 bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec,
14775 struct elf_link_hash_entry *h,
14776 bfd_vma addend)
14778 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14779 unsigned int log_file_align = bed->s->log_file_align;
14781 if (!h)
14783 /* xgettext:c-format */
14784 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
14785 abfd, sec);
14786 bfd_set_error (bfd_error_bad_value);
14787 return false;
14790 if (!h->u2.vtable)
14792 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
14793 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
14794 if (!h->u2.vtable)
14795 return false;
14798 if (addend >= h->u2.vtable->size)
14800 size_t size, bytes, file_align;
14801 bool *ptr = h->u2.vtable->used;
14803 /* While the symbol is undefined, we have to be prepared to handle
14804 a zero size. */
14805 file_align = 1 << log_file_align;
14806 if (h->root.type == bfd_link_hash_undefined)
14807 size = addend + file_align;
14808 else
14810 size = h->size;
14811 if (addend >= size)
14813 /* Oops! We've got a reference past the defined end of
14814 the table. This is probably a bug -- shall we warn? */
14815 size = addend + file_align;
14818 size = (size + file_align - 1) & -file_align;
14820 /* Allocate one extra entry for use as a "done" flag for the
14821 consolidation pass. */
14822 bytes = ((size >> log_file_align) + 1) * sizeof (bool);
14824 if (ptr)
14826 ptr = (bool *) bfd_realloc (ptr - 1, bytes);
14828 if (ptr != NULL)
14830 size_t oldbytes;
14832 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
14833 * sizeof (bool));
14834 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
14837 else
14838 ptr = (bool *) bfd_zmalloc (bytes);
14840 if (ptr == NULL)
14841 return false;
14843 /* And arrange for that done flag to be at index -1. */
14844 h->u2.vtable->used = ptr + 1;
14845 h->u2.vtable->size = size;
14848 h->u2.vtable->used[addend >> log_file_align] = true;
14850 return true;
14853 /* Map an ELF section header flag to its corresponding string. */
14854 typedef struct
14856 char *flag_name;
14857 flagword flag_value;
14858 } elf_flags_to_name_table;
14860 static const elf_flags_to_name_table elf_flags_to_names [] =
14862 { "SHF_WRITE", SHF_WRITE },
14863 { "SHF_ALLOC", SHF_ALLOC },
14864 { "SHF_EXECINSTR", SHF_EXECINSTR },
14865 { "SHF_MERGE", SHF_MERGE },
14866 { "SHF_STRINGS", SHF_STRINGS },
14867 { "SHF_INFO_LINK", SHF_INFO_LINK},
14868 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
14869 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
14870 { "SHF_GROUP", SHF_GROUP },
14871 { "SHF_TLS", SHF_TLS },
14872 { "SHF_MASKOS", SHF_MASKOS },
14873 { "SHF_EXCLUDE", SHF_EXCLUDE },
14876 /* Returns TRUE if the section is to be included, otherwise FALSE. */
14877 bool
14878 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
14879 struct flag_info *flaginfo,
14880 asection *section)
14882 const bfd_vma sh_flags = elf_section_flags (section);
14884 if (!flaginfo->flags_initialized)
14886 bfd *obfd = info->output_bfd;
14887 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14888 struct flag_info_list *tf = flaginfo->flag_list;
14889 int with_hex = 0;
14890 int without_hex = 0;
14892 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
14894 unsigned i;
14895 flagword (*lookup) (char *);
14897 lookup = bed->elf_backend_lookup_section_flags_hook;
14898 if (lookup != NULL)
14900 flagword hexval = (*lookup) ((char *) tf->name);
14902 if (hexval != 0)
14904 if (tf->with == with_flags)
14905 with_hex |= hexval;
14906 else if (tf->with == without_flags)
14907 without_hex |= hexval;
14908 tf->valid = true;
14909 continue;
14912 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
14914 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
14916 if (tf->with == with_flags)
14917 with_hex |= elf_flags_to_names[i].flag_value;
14918 else if (tf->with == without_flags)
14919 without_hex |= elf_flags_to_names[i].flag_value;
14920 tf->valid = true;
14921 break;
14924 if (!tf->valid)
14926 info->callbacks->einfo
14927 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
14928 return false;
14931 flaginfo->flags_initialized = true;
14932 flaginfo->only_with_flags |= with_hex;
14933 flaginfo->not_with_flags |= without_hex;
14936 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
14937 return false;
14939 if ((flaginfo->not_with_flags & sh_flags) != 0)
14940 return false;
14942 return true;
14945 struct alloc_got_off_arg {
14946 bfd_vma gotoff;
14947 struct bfd_link_info *info;
14950 /* We need a special top-level link routine to convert got reference counts
14951 to real got offsets. */
14953 static bool
14954 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
14956 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
14957 bfd *obfd = gofarg->info->output_bfd;
14958 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14960 if (h->got.refcount > 0)
14962 h->got.offset = gofarg->gotoff;
14963 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
14965 else
14966 h->got.offset = (bfd_vma) -1;
14968 return true;
14971 /* And an accompanying bit to work out final got entry offsets once
14972 we're done. Should be called from final_link. */
14974 bool
14975 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
14976 struct bfd_link_info *info)
14978 bfd *i;
14979 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14980 bfd_vma gotoff;
14981 struct alloc_got_off_arg gofarg;
14983 BFD_ASSERT (abfd == info->output_bfd);
14985 if (! is_elf_hash_table (info->hash))
14986 return false;
14988 /* The GOT offset is relative to the .got section, but the GOT header is
14989 put into the .got.plt section, if the backend uses it. */
14990 if (bed->want_got_plt)
14991 gotoff = 0;
14992 else
14993 gotoff = bed->got_header_size;
14995 /* Do the local .got entries first. */
14996 for (i = info->input_bfds; i; i = i->link.next)
14998 bfd_signed_vma *local_got;
14999 size_t j, locsymcount;
15000 Elf_Internal_Shdr *symtab_hdr;
15002 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
15003 continue;
15005 local_got = elf_local_got_refcounts (i);
15006 if (!local_got)
15007 continue;
15009 symtab_hdr = &elf_tdata (i)->symtab_hdr;
15010 if (elf_bad_symtab (i))
15011 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
15012 else
15013 locsymcount = symtab_hdr->sh_info;
15015 for (j = 0; j < locsymcount; ++j)
15017 if (local_got[j] > 0)
15019 local_got[j] = gotoff;
15020 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
15022 else
15023 local_got[j] = (bfd_vma) -1;
15027 /* Then the global .got entries. .plt refcounts are handled by
15028 adjust_dynamic_symbol */
15029 gofarg.gotoff = gotoff;
15030 gofarg.info = info;
15031 elf_link_hash_traverse (elf_hash_table (info),
15032 elf_gc_allocate_got_offsets,
15033 &gofarg);
15034 return true;
15037 /* Many folk need no more in the way of final link than this, once
15038 got entry reference counting is enabled. */
15040 bool
15041 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
15043 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
15044 return false;
15046 /* Invoke the regular ELF backend linker to do all the work. */
15047 return bfd_elf_final_link (abfd, info);
15050 bool
15051 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
15053 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
15055 if (rcookie->bad_symtab)
15056 rcookie->rel = rcookie->rels;
15058 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
15060 unsigned long r_symndx;
15062 if (! rcookie->bad_symtab)
15063 if (rcookie->rel->r_offset > offset)
15064 return false;
15065 if (rcookie->rel->r_offset != offset)
15066 continue;
15068 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
15069 if (r_symndx == STN_UNDEF)
15070 return true;
15072 if (r_symndx >= rcookie->locsymcount
15073 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
15075 struct elf_link_hash_entry *h;
15077 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
15079 while (h->root.type == bfd_link_hash_indirect
15080 || h->root.type == bfd_link_hash_warning)
15081 h = (struct elf_link_hash_entry *) h->root.u.i.link;
15083 if ((h->root.type == bfd_link_hash_defined
15084 || h->root.type == bfd_link_hash_defweak)
15085 && (h->root.u.def.section->owner != rcookie->abfd
15086 || h->root.u.def.section->kept_section != NULL
15087 || discarded_section (h->root.u.def.section)))
15088 return true;
15090 else
15092 /* It's not a relocation against a global symbol,
15093 but it could be a relocation against a local
15094 symbol for a discarded section. */
15095 asection *isec;
15096 Elf_Internal_Sym *isym;
15098 /* Need to: get the symbol; get the section. */
15099 isym = &rcookie->locsyms[r_symndx];
15100 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
15101 if (isec != NULL
15102 && (isec->kept_section != NULL
15103 || discarded_section (isec)))
15104 return true;
15106 return false;
15108 return false;
15111 /* Discard unneeded references to discarded sections.
15112 Returns -1 on error, 1 if any section's size was changed, 0 if
15113 nothing changed. This function assumes that the relocations are in
15114 sorted order, which is true for all known assemblers. */
15117 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
15119 struct elf_reloc_cookie cookie;
15120 asection *o;
15121 bfd *abfd;
15122 int changed = 0;
15124 if (info->traditional_format
15125 || !is_elf_hash_table (info->hash))
15126 return 0;
15128 o = bfd_get_section_by_name (output_bfd, ".stab");
15129 if (o != NULL)
15131 asection *i;
15133 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
15135 if (i->size == 0
15136 || i->reloc_count == 0
15137 || i->sec_info_type != SEC_INFO_TYPE_STABS)
15138 continue;
15140 abfd = i->owner;
15141 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
15142 continue;
15144 if (!init_reloc_cookie_for_section (&cookie, info, i, false))
15145 return -1;
15147 if (_bfd_discard_section_stabs (abfd, i,
15148 elf_section_data (i)->sec_info,
15149 bfd_elf_reloc_symbol_deleted_p,
15150 &cookie))
15151 changed = 1;
15153 fini_reloc_cookie_for_section (&cookie, i);
15157 o = NULL;
15158 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
15159 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
15160 if (o != NULL)
15162 asection *i;
15163 int eh_changed = 0;
15164 unsigned int eh_alignment; /* Octets. */
15166 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
15168 if (i->size == 0)
15169 continue;
15171 abfd = i->owner;
15172 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
15173 continue;
15175 if (!init_reloc_cookie_for_section (&cookie, info, i, false))
15176 return -1;
15178 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
15179 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
15180 bfd_elf_reloc_symbol_deleted_p,
15181 &cookie))
15183 eh_changed = 1;
15184 if (i->size != i->rawsize)
15185 changed = 1;
15188 fini_reloc_cookie_for_section (&cookie, i);
15191 eh_alignment = ((1 << o->alignment_power)
15192 * bfd_octets_per_byte (output_bfd, o));
15193 /* Skip over zero terminator, and prevent empty sections from
15194 adding alignment padding at the end. */
15195 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
15196 if (i->size == 0)
15197 i->flags |= SEC_EXCLUDE;
15198 else if (i->size > 4)
15199 break;
15200 /* The last non-empty eh_frame section doesn't need padding. */
15201 if (i != NULL)
15202 i = i->map_tail.s;
15203 /* Any prior sections must pad the last FDE out to the output
15204 section alignment. Otherwise we might have zero padding
15205 between sections, which would be seen as a terminator. */
15206 for (; i != NULL; i = i->map_tail.s)
15207 if (i->size == 4)
15208 /* All but the last zero terminator should have been removed. */
15209 BFD_FAIL ();
15210 else
15212 bfd_size_type size
15213 = (i->size + eh_alignment - 1) & -eh_alignment;
15214 if (i->size != size)
15216 i->size = size;
15217 changed = 1;
15218 eh_changed = 1;
15221 if (eh_changed)
15222 elf_link_hash_traverse (elf_hash_table (info),
15223 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
15226 o = bfd_get_section_by_name (output_bfd, ".sframe");
15227 if (o != NULL)
15229 asection *i;
15231 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
15233 if (i->size == 0)
15234 continue;
15236 abfd = i->owner;
15237 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
15238 continue;
15240 if (!init_reloc_cookie_for_section (&cookie, info, i, false))
15241 return -1;
15243 if (_bfd_elf_parse_sframe (abfd, info, i, &cookie))
15245 if (_bfd_elf_discard_section_sframe (i,
15246 bfd_elf_reloc_symbol_deleted_p,
15247 &cookie))
15249 if (i->size != i->rawsize)
15250 changed = 1;
15253 fini_reloc_cookie_for_section (&cookie, i);
15255 /* Update the reference to the output .sframe section. Used to
15256 determine later if PT_GNU_SFRAME segment is to be generated. */
15257 if (!_bfd_elf_set_section_sframe (output_bfd, info))
15258 return -1;
15261 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
15263 const struct elf_backend_data *bed;
15264 asection *s;
15266 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
15267 continue;
15268 s = abfd->sections;
15269 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
15270 continue;
15272 bed = get_elf_backend_data (abfd);
15274 if (bed->elf_backend_discard_info != NULL)
15276 if (!init_reloc_cookie (&cookie, info, abfd, false))
15277 return -1;
15279 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
15280 changed = 1;
15282 fini_reloc_cookie (&cookie, abfd);
15286 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
15287 _bfd_elf_end_eh_frame_parsing (info);
15289 if (_bfd_elf_discard_section_eh_frame_hdr (info))
15290 changed = 1;
15292 return changed;
15295 bool
15296 _bfd_elf_section_already_linked (bfd *abfd,
15297 asection *sec,
15298 struct bfd_link_info *info)
15300 flagword flags;
15301 const char *name, *key;
15302 struct bfd_section_already_linked *l;
15303 struct bfd_section_already_linked_hash_entry *already_linked_list;
15305 if (sec->output_section == bfd_abs_section_ptr)
15306 return false;
15308 flags = sec->flags;
15310 /* Return if it isn't a linkonce section. A comdat group section
15311 also has SEC_LINK_ONCE set. */
15312 if ((flags & SEC_LINK_ONCE) == 0)
15313 return false;
15315 /* Don't put group member sections on our list of already linked
15316 sections. They are handled as a group via their group section. */
15317 if (elf_sec_group (sec) != NULL)
15318 return false;
15320 /* For a SHT_GROUP section, use the group signature as the key. */
15321 name = sec->name;
15322 if ((flags & SEC_GROUP) != 0
15323 && elf_next_in_group (sec) != NULL
15324 && elf_group_name (elf_next_in_group (sec)) != NULL)
15325 key = elf_group_name (elf_next_in_group (sec));
15326 else
15328 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
15329 if (startswith (name, ".gnu.linkonce.")
15330 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
15331 key++;
15332 else
15333 /* Must be a user linkonce section that doesn't follow gcc's
15334 naming convention. In this case we won't be matching
15335 single member groups. */
15336 key = name;
15339 already_linked_list = bfd_section_already_linked_table_lookup (key);
15341 for (l = already_linked_list->entry; l != NULL; l = l->next)
15343 /* We may have 2 different types of sections on the list: group
15344 sections with a signature of <key> (<key> is some string),
15345 and linkonce sections named .gnu.linkonce.<type>.<key>.
15346 Match like sections. LTO plugin sections are an exception.
15347 They are always named .gnu.linkonce.t.<key> and match either
15348 type of section. */
15349 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
15350 && ((flags & SEC_GROUP) != 0
15351 || strcmp (name, l->sec->name) == 0))
15352 || (l->sec->owner->flags & BFD_PLUGIN) != 0
15353 || (sec->owner->flags & BFD_PLUGIN) != 0)
15355 /* The section has already been linked. See if we should
15356 issue a warning. */
15357 if (!_bfd_handle_already_linked (sec, l, info))
15358 return false;
15360 if (flags & SEC_GROUP)
15362 asection *first = elf_next_in_group (sec);
15363 asection *s = first;
15365 while (s != NULL)
15367 s->output_section = bfd_abs_section_ptr;
15368 /* Record which group discards it. */
15369 s->kept_section = l->sec;
15370 s = elf_next_in_group (s);
15371 /* These lists are circular. */
15372 if (s == first)
15373 break;
15377 return true;
15381 /* A single member comdat group section may be discarded by a
15382 linkonce section and vice versa. */
15383 if ((flags & SEC_GROUP) != 0)
15385 asection *first = elf_next_in_group (sec);
15387 if (first != NULL && elf_next_in_group (first) == first)
15388 /* Check this single member group against linkonce sections. */
15389 for (l = already_linked_list->entry; l != NULL; l = l->next)
15390 if ((l->sec->flags & SEC_GROUP) == 0
15391 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
15393 first->output_section = bfd_abs_section_ptr;
15394 first->kept_section = l->sec;
15395 sec->output_section = bfd_abs_section_ptr;
15396 break;
15399 else
15400 /* Check this linkonce section against single member groups. */
15401 for (l = already_linked_list->entry; l != NULL; l = l->next)
15402 if (l->sec->flags & SEC_GROUP)
15404 asection *first = elf_next_in_group (l->sec);
15406 if (first != NULL
15407 && elf_next_in_group (first) == first
15408 && bfd_elf_match_symbols_in_sections (first, sec, info))
15410 sec->output_section = bfd_abs_section_ptr;
15411 sec->kept_section = first;
15412 break;
15416 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
15417 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
15418 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
15419 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
15420 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
15421 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
15422 `.gnu.linkonce.t.F' section from a different bfd not requiring any
15423 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
15424 The reverse order cannot happen as there is never a bfd with only the
15425 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
15426 matter as here were are looking only for cross-bfd sections. */
15428 if ((flags & SEC_GROUP) == 0 && startswith (name, ".gnu.linkonce.r."))
15429 for (l = already_linked_list->entry; l != NULL; l = l->next)
15430 if ((l->sec->flags & SEC_GROUP) == 0
15431 && startswith (l->sec->name, ".gnu.linkonce.t."))
15433 if (abfd != l->sec->owner)
15434 sec->output_section = bfd_abs_section_ptr;
15435 break;
15438 /* This is the first section with this name. Record it. */
15439 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
15440 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
15441 return sec->output_section == bfd_abs_section_ptr;
15444 bool
15445 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
15447 return sym->st_shndx == SHN_COMMON;
15450 unsigned int
15451 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
15453 return SHN_COMMON;
15456 asection *
15457 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
15459 return bfd_com_section_ptr;
15462 bfd_vma
15463 _bfd_elf_default_got_elt_size (bfd *abfd,
15464 struct bfd_link_info *info ATTRIBUTE_UNUSED,
15465 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
15466 bfd *ibfd ATTRIBUTE_UNUSED,
15467 unsigned long symndx ATTRIBUTE_UNUSED)
15469 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15470 return bed->s->arch_size / 8;
15473 /* Routines to support the creation of dynamic relocs. */
15475 /* Returns the name of the dynamic reloc section associated with SEC. */
15477 static const char *
15478 get_dynamic_reloc_section_name (bfd * abfd,
15479 asection * sec,
15480 bool is_rela)
15482 char *name;
15483 const char *old_name = bfd_section_name (sec);
15484 const char *prefix = is_rela ? ".rela" : ".rel";
15486 if (old_name == NULL)
15487 return NULL;
15489 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
15490 sprintf (name, "%s%s", prefix, old_name);
15492 return name;
15495 /* Returns the dynamic reloc section associated with SEC.
15496 If necessary compute the name of the dynamic reloc section based
15497 on SEC's name (looked up in ABFD's string table) and the setting
15498 of IS_RELA. */
15500 asection *
15501 _bfd_elf_get_dynamic_reloc_section (bfd *abfd,
15502 asection *sec,
15503 bool is_rela)
15505 asection *reloc_sec = elf_section_data (sec)->sreloc;
15507 if (reloc_sec == NULL)
15509 const char *name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
15511 if (name != NULL)
15513 reloc_sec = bfd_get_linker_section (abfd, name);
15515 if (reloc_sec != NULL)
15516 elf_section_data (sec)->sreloc = reloc_sec;
15520 return reloc_sec;
15523 /* Returns the dynamic reloc section associated with SEC. If the
15524 section does not exist it is created and attached to the DYNOBJ
15525 bfd and stored in the SRELOC field of SEC's elf_section_data
15526 structure.
15528 ALIGNMENT is the alignment for the newly created section and
15529 IS_RELA defines whether the name should be .rela.<SEC's name>
15530 or .rel.<SEC's name>. The section name is looked up in the
15531 string table associated with ABFD. */
15533 asection *
15534 _bfd_elf_make_dynamic_reloc_section (asection *sec,
15535 bfd *dynobj,
15536 unsigned int alignment,
15537 bfd *abfd,
15538 bool is_rela)
15540 asection * reloc_sec = elf_section_data (sec)->sreloc;
15542 if (reloc_sec == NULL)
15544 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
15546 if (name == NULL)
15547 return NULL;
15549 reloc_sec = bfd_get_linker_section (dynobj, name);
15551 if (reloc_sec == NULL)
15553 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
15554 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
15555 if ((sec->flags & SEC_ALLOC) != 0)
15556 flags |= SEC_ALLOC | SEC_LOAD;
15558 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
15559 if (reloc_sec != NULL)
15561 /* _bfd_elf_get_sec_type_attr chooses a section type by
15562 name. Override as it may be wrong, eg. for a user
15563 section named "auto" we'll get ".relauto" which is
15564 seen to be a .rela section. */
15565 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
15566 if (!bfd_set_section_alignment (reloc_sec, alignment))
15567 reloc_sec = NULL;
15571 elf_section_data (sec)->sreloc = reloc_sec;
15574 return reloc_sec;
15577 /* Copy the ELF symbol type and other attributes for a linker script
15578 assignment from HSRC to HDEST. Generally this should be treated as
15579 if we found a strong non-dynamic definition for HDEST (except that
15580 ld ignores multiple definition errors). */
15581 void
15582 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
15583 struct bfd_link_hash_entry *hdest,
15584 struct bfd_link_hash_entry *hsrc)
15586 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
15587 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
15588 Elf_Internal_Sym isym;
15590 ehdest->type = ehsrc->type;
15591 ehdest->target_internal = ehsrc->target_internal;
15593 isym.st_other = ehsrc->other;
15594 elf_merge_st_other (abfd, ehdest, isym.st_other, NULL, true, false);
15597 /* Append a RELA relocation REL to section S in BFD. */
15599 void
15600 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15602 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15603 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
15604 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
15605 bed->s->swap_reloca_out (abfd, rel, loc);
15608 /* Append a REL relocation REL to section S in BFD. */
15610 void
15611 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15613 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15614 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
15615 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
15616 bed->s->swap_reloc_out (abfd, rel, loc);
15619 /* Define __start, __stop, .startof. or .sizeof. symbol. */
15621 struct bfd_link_hash_entry *
15622 bfd_elf_define_start_stop (struct bfd_link_info *info,
15623 const char *symbol, asection *sec)
15625 struct elf_link_hash_entry *h;
15627 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
15628 false, false, true);
15629 /* NB: Common symbols will be turned into definition later. */
15630 if (h != NULL
15631 && !h->root.ldscript_def
15632 && (h->root.type == bfd_link_hash_undefined
15633 || h->root.type == bfd_link_hash_undefweak
15634 || ((h->ref_regular || h->def_dynamic)
15635 && !h->def_regular
15636 && h->root.type != bfd_link_hash_common)))
15638 bool was_dynamic = h->ref_dynamic || h->def_dynamic;
15639 h->verinfo.verdef = NULL;
15640 h->root.type = bfd_link_hash_defined;
15641 h->root.u.def.section = sec;
15642 h->root.u.def.value = 0;
15643 h->def_regular = 1;
15644 h->def_dynamic = 0;
15645 h->start_stop = 1;
15646 h->u2.start_stop_section = sec;
15647 if (symbol[0] == '.')
15649 /* .startof. and .sizeof. symbols are local. */
15650 const struct elf_backend_data *bed;
15651 bed = get_elf_backend_data (info->output_bfd);
15652 (*bed->elf_backend_hide_symbol) (info, h, true);
15654 else
15656 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
15657 h->other = ((h->other & ~ELF_ST_VISIBILITY (-1))
15658 | info->start_stop_visibility);
15659 if (was_dynamic)
15660 bfd_elf_link_record_dynamic_symbol (info, h);
15662 return &h->root;
15664 return NULL;
15667 /* Find dynamic relocs for H that apply to read-only sections. */
15669 asection *
15670 _bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry *h)
15672 struct elf_dyn_relocs *p;
15674 for (p = h->dyn_relocs; p != NULL; p = p->next)
15676 asection *s = p->sec->output_section;
15678 if (s != NULL && (s->flags & SEC_READONLY) != 0)
15679 return p->sec;
15681 return NULL;
15684 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
15685 read-only sections. */
15687 bool
15688 _bfd_elf_maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
15690 asection *sec;
15692 if (h->root.type == bfd_link_hash_indirect)
15693 return true;
15695 sec = _bfd_elf_readonly_dynrelocs (h);
15696 if (sec != NULL)
15698 struct bfd_link_info *info = (struct bfd_link_info *) inf;
15700 info->flags |= DF_TEXTREL;
15701 /* xgettext:c-format */
15702 info->callbacks->minfo (_("%pB: dynamic relocation against `%pT' "
15703 "in read-only section `%pA'\n"),
15704 sec->owner, h->root.root.string, sec);
15706 if (bfd_link_textrel_check (info))
15707 /* xgettext:c-format */
15708 info->callbacks->einfo (_("%P: %pB: warning: relocation against `%s' "
15709 "in read-only section `%pA'\n"),
15710 sec->owner, h->root.root.string, sec);
15712 /* Not an error, just cut short the traversal. */
15713 return false;
15715 return true;
15718 /* Add dynamic tags. */
15720 bool
15721 _bfd_elf_add_dynamic_tags (bfd *output_bfd, struct bfd_link_info *info,
15722 bool need_dynamic_reloc)
15724 struct elf_link_hash_table *htab = elf_hash_table (info);
15726 if (htab->dynamic_sections_created)
15728 /* Add some entries to the .dynamic section. We fill in the
15729 values later, in finish_dynamic_sections, but we must add
15730 the entries now so that we get the correct size for the
15731 .dynamic section. The DT_DEBUG entry is filled in by the
15732 dynamic linker and used by the debugger. */
15733 #define add_dynamic_entry(TAG, VAL) \
15734 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
15736 const struct elf_backend_data *bed
15737 = get_elf_backend_data (output_bfd);
15739 if (bfd_link_executable (info))
15741 if (!add_dynamic_entry (DT_DEBUG, 0))
15742 return false;
15745 if (htab->dt_pltgot_required || htab->splt->size != 0)
15747 /* DT_PLTGOT is used by prelink even if there is no PLT
15748 relocation. */
15749 if (!add_dynamic_entry (DT_PLTGOT, 0))
15750 return false;
15753 if (htab->dt_jmprel_required || htab->srelplt->size != 0)
15755 if (!add_dynamic_entry (DT_PLTRELSZ, 0)
15756 || !add_dynamic_entry (DT_PLTREL,
15757 (bed->rela_plts_and_copies_p
15758 ? DT_RELA : DT_REL))
15759 || !add_dynamic_entry (DT_JMPREL, 0))
15760 return false;
15763 if (htab->tlsdesc_plt
15764 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
15765 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
15766 return false;
15768 if (need_dynamic_reloc)
15770 if (bed->rela_plts_and_copies_p)
15772 if (!add_dynamic_entry (DT_RELA, 0)
15773 || !add_dynamic_entry (DT_RELASZ, 0)
15774 || !add_dynamic_entry (DT_RELAENT,
15775 bed->s->sizeof_rela))
15776 return false;
15778 else
15780 if (!add_dynamic_entry (DT_REL, 0)
15781 || !add_dynamic_entry (DT_RELSZ, 0)
15782 || !add_dynamic_entry (DT_RELENT,
15783 bed->s->sizeof_rel))
15784 return false;
15787 /* If any dynamic relocs apply to a read-only section,
15788 then we need a DT_TEXTREL entry. */
15789 if ((info->flags & DF_TEXTREL) == 0)
15790 elf_link_hash_traverse (htab, _bfd_elf_maybe_set_textrel,
15791 info);
15793 if ((info->flags & DF_TEXTREL) != 0)
15795 if (htab->ifunc_resolvers)
15796 info->callbacks->einfo
15797 (_("%P: warning: GNU indirect functions with DT_TEXTREL "
15798 "may result in a segfault at runtime; recompile with %s\n"),
15799 bfd_link_dll (info) ? "-fPIC" : "-fPIE");
15801 if (!add_dynamic_entry (DT_TEXTREL, 0))
15802 return false;
15806 #undef add_dynamic_entry
15808 return true;