LoongArch: Do not relax pcalau12i+ld.d when there is overflow
[binutils-gdb.git] / bfd / elflink.c
blobef159dafd55f8ff31746fe8f0f023142f34ad119
1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2024 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)
2846 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2847 if (info)
2848 info->cache_size += size;
2850 else
2851 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2852 if (internal_relocs == NULL)
2853 return NULL;
2856 alloc1 = external_relocs;
2857 internal_rela_relocs = internal_relocs;
2858 if (esdo->rel.hdr)
2860 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2861 &alloc1, &alloc1_size,
2862 internal_relocs))
2863 goto error_return;
2864 external_relocs = (((bfd_byte *) external_relocs)
2865 + esdo->rel.hdr->sh_size);
2866 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2867 * bed->s->int_rels_per_ext_rel);
2870 if (esdo->rela.hdr
2871 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2872 &alloc1, &alloc1_size,
2873 internal_rela_relocs)))
2874 goto error_return;
2876 /* Cache the results for next time, if we can. */
2877 if (keep_memory)
2878 esdo->relocs = internal_relocs;
2880 _bfd_munmap_temporary (alloc1, alloc1_size);
2882 /* Don't free alloc2, since if it was allocated we are passing it
2883 back (under the name of internal_relocs). */
2885 return internal_relocs;
2887 error_return:
2888 _bfd_munmap_temporary (alloc1, alloc1_size);
2889 if (alloc2 != NULL)
2891 if (keep_memory)
2892 bfd_release (abfd, alloc2);
2893 else
2894 free (alloc2);
2896 return NULL;
2899 /* This is similar to _bfd_elf_link_info_read_relocs, except for that
2900 NULL is passed to _bfd_elf_link_info_read_relocs for pointer to
2901 struct bfd_link_info. */
2903 Elf_Internal_Rela *
2904 _bfd_elf_link_read_relocs (bfd *abfd,
2905 const asection *o,
2906 void *external_relocs,
2907 Elf_Internal_Rela *internal_relocs,
2908 bool keep_memory)
2910 return _bfd_elf_link_info_read_relocs (abfd, NULL, o, external_relocs,
2911 internal_relocs, keep_memory);
2915 /* Compute the size of, and allocate space for, REL_HDR which is the
2916 section header for a section containing relocations for O. */
2918 static bool
2919 _bfd_elf_link_size_reloc_section (bfd *abfd,
2920 struct bfd_elf_section_reloc_data *reldata)
2922 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2924 /* That allows us to calculate the size of the section. */
2925 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2927 /* The contents field must last into write_object_contents, so we
2928 allocate it with bfd_alloc rather than malloc. Also since we
2929 cannot be sure that the contents will actually be filled in,
2930 we zero the allocated space. */
2931 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2932 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2933 return false;
2935 if (reldata->hashes == NULL && reldata->count)
2937 struct elf_link_hash_entry **p;
2939 p = ((struct elf_link_hash_entry **)
2940 bfd_zmalloc (reldata->count * sizeof (*p)));
2941 if (p == NULL)
2942 return false;
2944 reldata->hashes = p;
2947 return true;
2950 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2951 originated from the section given by INPUT_REL_HDR) to the
2952 OUTPUT_BFD. */
2954 bool
2955 _bfd_elf_link_output_relocs (bfd *output_bfd,
2956 asection *input_section,
2957 Elf_Internal_Shdr *input_rel_hdr,
2958 Elf_Internal_Rela *internal_relocs,
2959 struct elf_link_hash_entry **rel_hash)
2961 Elf_Internal_Rela *irela;
2962 Elf_Internal_Rela *irelaend;
2963 bfd_byte *erel;
2964 struct bfd_elf_section_reloc_data *output_reldata;
2965 asection *output_section;
2966 const struct elf_backend_data *bed;
2967 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2968 struct bfd_elf_section_data *esdo;
2970 output_section = input_section->output_section;
2972 bed = get_elf_backend_data (output_bfd);
2973 esdo = elf_section_data (output_section);
2974 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2976 output_reldata = &esdo->rel;
2977 swap_out = bed->s->swap_reloc_out;
2979 else if (esdo->rela.hdr
2980 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2982 output_reldata = &esdo->rela;
2983 swap_out = bed->s->swap_reloca_out;
2985 else
2987 _bfd_error_handler
2988 /* xgettext:c-format */
2989 (_("%pB: relocation size mismatch in %pB section %pA"),
2990 output_bfd, input_section->owner, input_section);
2991 bfd_set_error (bfd_error_wrong_format);
2992 return false;
2995 erel = output_reldata->hdr->contents;
2996 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2997 irela = internal_relocs;
2998 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2999 * bed->s->int_rels_per_ext_rel);
3000 while (irela < irelaend)
3002 if (rel_hash && *rel_hash)
3003 (*rel_hash)->has_reloc = 1;
3004 (*swap_out) (output_bfd, irela, erel);
3005 irela += bed->s->int_rels_per_ext_rel;
3006 erel += input_rel_hdr->sh_entsize;
3007 if (rel_hash)
3008 rel_hash++;
3011 /* Bump the counter, so that we know where to add the next set of
3012 relocations. */
3013 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
3015 return true;
3018 /* Make weak undefined symbols in PIE dynamic. */
3020 bool
3021 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
3022 struct elf_link_hash_entry *h)
3024 if (bfd_link_pie (info)
3025 && h->dynindx == -1
3026 && h->root.type == bfd_link_hash_undefweak)
3027 return bfd_elf_link_record_dynamic_symbol (info, h);
3029 return true;
3032 /* Fix up the flags for a symbol. This handles various cases which
3033 can only be fixed after all the input files are seen. This is
3034 currently called by both adjust_dynamic_symbol and
3035 assign_sym_version, which is unnecessary but perhaps more robust in
3036 the face of future changes. */
3038 static bool
3039 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
3040 struct elf_info_failed *eif)
3042 const struct elf_backend_data *bed;
3044 /* If this symbol was mentioned in a non-ELF file, try to set
3045 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
3046 permit a non-ELF file to correctly refer to a symbol defined in
3047 an ELF dynamic object. */
3048 if (h->non_elf)
3050 while (h->root.type == bfd_link_hash_indirect)
3051 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3053 if (h->root.type != bfd_link_hash_defined
3054 && h->root.type != bfd_link_hash_defweak)
3056 h->ref_regular = 1;
3057 h->ref_regular_nonweak = 1;
3059 else
3061 if (h->root.u.def.section->owner != NULL
3062 && (bfd_get_flavour (h->root.u.def.section->owner)
3063 == bfd_target_elf_flavour))
3065 h->ref_regular = 1;
3066 h->ref_regular_nonweak = 1;
3068 else
3069 h->def_regular = 1;
3072 if (h->dynindx == -1
3073 && (h->def_dynamic
3074 || h->ref_dynamic))
3076 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
3078 eif->failed = true;
3079 return false;
3083 else
3085 /* Unfortunately, NON_ELF is only correct if the symbol
3086 was first seen in a non-ELF file. Fortunately, if the symbol
3087 was first seen in an ELF file, we're probably OK unless the
3088 symbol was defined in a non-ELF file. Catch that case here.
3089 FIXME: We're still in trouble if the symbol was first seen in
3090 a dynamic object, and then later in a non-ELF regular object. */
3091 if ((h->root.type == bfd_link_hash_defined
3092 || h->root.type == bfd_link_hash_defweak)
3093 && !h->def_regular
3094 && (h->root.u.def.section->owner != NULL
3095 ? (bfd_get_flavour (h->root.u.def.section->owner)
3096 != bfd_target_elf_flavour)
3097 : (bfd_is_abs_section (h->root.u.def.section)
3098 && !h->def_dynamic)))
3099 h->def_regular = 1;
3102 /* Backend specific symbol fixup. */
3103 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
3104 if (bed->elf_backend_fixup_symbol
3105 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
3106 return false;
3108 /* If this is a final link, and the symbol was defined as a common
3109 symbol in a regular object file, and there was no definition in
3110 any dynamic object, then the linker will have allocated space for
3111 the symbol in a common section but the DEF_REGULAR
3112 flag will not have been set. */
3113 if (h->root.type == bfd_link_hash_defined
3114 && !h->def_regular
3115 && h->ref_regular
3116 && !h->def_dynamic
3117 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
3118 h->def_regular = 1;
3120 /* Symbols defined in discarded sections shouldn't be dynamic. */
3121 if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
3122 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3124 /* If a weak undefined symbol has non-default visibility, we also
3125 hide it from the dynamic linker. */
3126 else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3127 && h->root.type == bfd_link_hash_undefweak)
3128 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3130 /* A hidden versioned symbol in executable should be forced local if
3131 it is is locally defined, not referenced by shared library and not
3132 exported. */
3133 else if (bfd_link_executable (eif->info)
3134 && h->versioned == versioned_hidden
3135 && !eif->info->export_dynamic
3136 && !h->dynamic
3137 && !h->ref_dynamic
3138 && h->def_regular)
3139 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3141 /* If -Bsymbolic was used (which means to bind references to global
3142 symbols to the definition within the shared object), and this
3143 symbol was defined in a regular object, then it actually doesn't
3144 need a PLT entry. Likewise, if the symbol has non-default
3145 visibility. If the symbol has hidden or internal visibility, we
3146 will force it local. */
3147 else if (h->needs_plt
3148 && bfd_link_pic (eif->info)
3149 && is_elf_hash_table (eif->info->hash)
3150 && (SYMBOLIC_BIND (eif->info, h)
3151 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
3152 && h->def_regular)
3154 bool force_local;
3156 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
3157 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
3158 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
3161 /* If this is a weak defined symbol in a dynamic object, and we know
3162 the real definition in the dynamic object, copy interesting flags
3163 over to the real definition. */
3164 if (h->is_weakalias)
3166 struct elf_link_hash_entry *def = weakdef (h);
3168 /* If the real definition is defined by a regular object file,
3169 don't do anything special. See the longer description in
3170 _bfd_elf_adjust_dynamic_symbol, below. If the def is not
3171 bfd_link_hash_defined as it was when put on the alias list
3172 then it must have originally been a versioned symbol (for
3173 which a non-versioned indirect symbol is created) and later
3174 a definition for the non-versioned symbol is found. In that
3175 case the indirection is flipped with the versioned symbol
3176 becoming an indirect pointing at the non-versioned symbol.
3177 Thus, not an alias any more. */
3178 if (def->def_regular
3179 || def->root.type != bfd_link_hash_defined)
3181 h = def;
3182 while ((h = h->u.alias) != def)
3183 h->is_weakalias = 0;
3185 else
3187 while (h->root.type == bfd_link_hash_indirect)
3188 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3189 BFD_ASSERT (h->root.type == bfd_link_hash_defined
3190 || h->root.type == bfd_link_hash_defweak);
3191 BFD_ASSERT (def->def_dynamic);
3192 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
3196 return true;
3199 /* Make the backend pick a good value for a dynamic symbol. This is
3200 called via elf_link_hash_traverse, and also calls itself
3201 recursively. */
3203 static bool
3204 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
3206 struct elf_info_failed *eif = (struct elf_info_failed *) data;
3207 struct elf_link_hash_table *htab;
3208 const struct elf_backend_data *bed;
3210 if (! is_elf_hash_table (eif->info->hash))
3211 return false;
3213 /* Ignore indirect symbols. These are added by the versioning code. */
3214 if (h->root.type == bfd_link_hash_indirect)
3215 return true;
3217 /* Fix the symbol flags. */
3218 if (! _bfd_elf_fix_symbol_flags (h, eif))
3219 return false;
3221 htab = elf_hash_table (eif->info);
3222 bed = get_elf_backend_data (htab->dynobj);
3224 if (h->root.type == bfd_link_hash_undefweak)
3226 if (eif->info->dynamic_undefined_weak == 0)
3227 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3228 else if (eif->info->dynamic_undefined_weak > 0
3229 && h->ref_regular
3230 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3231 && !bfd_hide_sym_by_version (eif->info->version_info,
3232 h->root.root.string))
3234 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
3236 eif->failed = true;
3237 return false;
3242 /* If this symbol does not require a PLT entry, and it is not
3243 defined by a dynamic object, or is not referenced by a regular
3244 object, ignore it. We do have to handle a weak defined symbol,
3245 even if no regular object refers to it, if we decided to add it
3246 to the dynamic symbol table. FIXME: Do we normally need to worry
3247 about symbols which are defined by one dynamic object and
3248 referenced by another one? */
3249 if (!h->needs_plt
3250 && h->type != STT_GNU_IFUNC
3251 && (h->def_regular
3252 || !h->def_dynamic
3253 || (!h->ref_regular
3254 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
3256 h->plt = elf_hash_table (eif->info)->init_plt_offset;
3257 return true;
3260 /* If we've already adjusted this symbol, don't do it again. This
3261 can happen via a recursive call. */
3262 if (h->dynamic_adjusted)
3263 return true;
3265 /* Don't look at this symbol again. Note that we must set this
3266 after checking the above conditions, because we may look at a
3267 symbol once, decide not to do anything, and then get called
3268 recursively later after REF_REGULAR is set below. */
3269 h->dynamic_adjusted = 1;
3271 /* If this is a weak definition, and we know a real definition, and
3272 the real symbol is not itself defined by a regular object file,
3273 then get a good value for the real definition. We handle the
3274 real symbol first, for the convenience of the backend routine.
3276 Note that there is a confusing case here. If the real definition
3277 is defined by a regular object file, we don't get the real symbol
3278 from the dynamic object, but we do get the weak symbol. If the
3279 processor backend uses a COPY reloc, then if some routine in the
3280 dynamic object changes the real symbol, we will not see that
3281 change in the corresponding weak symbol. This is the way other
3282 ELF linkers work as well, and seems to be a result of the shared
3283 library model.
3285 I will clarify this issue. Most SVR4 shared libraries define the
3286 variable _timezone and define timezone as a weak synonym. The
3287 tzset call changes _timezone. If you write
3288 extern int timezone;
3289 int _timezone = 5;
3290 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3291 you might expect that, since timezone is a synonym for _timezone,
3292 the same number will print both times. However, if the processor
3293 backend uses a COPY reloc, then actually timezone will be copied
3294 into your process image, and, since you define _timezone
3295 yourself, _timezone will not. Thus timezone and _timezone will
3296 wind up at different memory locations. The tzset call will set
3297 _timezone, leaving timezone unchanged. */
3299 if (h->is_weakalias)
3301 struct elf_link_hash_entry *def = weakdef (h);
3303 /* If we get to this point, there is an implicit reference to
3304 the alias by a regular object file via the weak symbol H. */
3305 def->ref_regular = 1;
3307 /* Ensure that the backend adjust_dynamic_symbol function sees
3308 the strong alias before H by recursively calling ourselves. */
3309 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
3310 return false;
3313 /* If a symbol has no type and no size and does not require a PLT
3314 entry, then we are probably about to do the wrong thing here: we
3315 are probably going to create a COPY reloc for an empty object.
3316 This case can arise when a shared object is built with assembly
3317 code, and the assembly code fails to set the symbol type. */
3318 if (h->size == 0
3319 && h->type == STT_NOTYPE
3320 && !h->needs_plt)
3321 _bfd_error_handler
3322 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3323 h->root.root.string);
3325 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3327 eif->failed = true;
3328 return false;
3331 return true;
3334 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3335 DYNBSS. */
3337 bool
3338 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3339 struct elf_link_hash_entry *h,
3340 asection *dynbss)
3342 unsigned int power_of_two;
3343 bfd_vma mask;
3344 asection *sec = h->root.u.def.section;
3346 /* The section alignment of the definition is the maximum alignment
3347 requirement of symbols defined in the section. Since we don't
3348 know the symbol alignment requirement, we start with the
3349 maximum alignment and check low bits of the symbol address
3350 for the minimum alignment. */
3351 power_of_two = bfd_section_alignment (sec);
3352 mask = ((bfd_vma) 1 << power_of_two) - 1;
3353 while ((h->root.u.def.value & mask) != 0)
3355 mask >>= 1;
3356 --power_of_two;
3359 if (power_of_two > bfd_section_alignment (dynbss))
3361 /* Adjust the section alignment if needed. */
3362 if (!bfd_set_section_alignment (dynbss, power_of_two))
3363 return false;
3366 /* We make sure that the symbol will be aligned properly. */
3367 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3369 /* Define the symbol as being at this point in DYNBSS. */
3370 h->root.u.def.section = dynbss;
3371 h->root.u.def.value = dynbss->size;
3373 /* Increment the size of DYNBSS to make room for the symbol. */
3374 dynbss->size += h->size;
3376 /* No error if extern_protected_data is true. */
3377 if (h->protected_def
3378 && (!info->extern_protected_data
3379 || (info->extern_protected_data < 0
3380 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
3381 info->callbacks->einfo
3382 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3383 h->root.root.string);
3385 return true;
3388 /* Adjust all external symbols pointing into SEC_MERGE sections
3389 to reflect the object merging within the sections. */
3391 static bool
3392 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
3394 asection *sec;
3396 if ((h->root.type == bfd_link_hash_defined
3397 || h->root.type == bfd_link_hash_defweak)
3398 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
3399 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3401 bfd *output_bfd = (bfd *) data;
3403 h->root.u.def.value =
3404 _bfd_merged_section_offset (output_bfd,
3405 &h->root.u.def.section,
3406 elf_section_data (sec)->sec_info,
3407 h->root.u.def.value);
3410 return true;
3413 /* Returns false if the symbol referred to by H should be considered
3414 to resolve local to the current module, and true if it should be
3415 considered to bind dynamically. */
3417 bool
3418 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3419 struct bfd_link_info *info,
3420 bool not_local_protected)
3422 bool binding_stays_local_p;
3423 const struct elf_backend_data *bed;
3424 struct elf_link_hash_table *hash_table;
3426 if (h == NULL)
3427 return false;
3429 while (h->root.type == bfd_link_hash_indirect
3430 || h->root.type == bfd_link_hash_warning)
3431 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3433 /* If it was forced local, then clearly it's not dynamic. */
3434 if (h->dynindx == -1)
3435 return false;
3436 if (h->forced_local)
3437 return false;
3439 /* Identify the cases where name binding rules say that a
3440 visible symbol resolves locally. */
3441 binding_stays_local_p = (bfd_link_executable (info)
3442 || SYMBOLIC_BIND (info, h));
3444 switch (ELF_ST_VISIBILITY (h->other))
3446 case STV_INTERNAL:
3447 case STV_HIDDEN:
3448 return false;
3450 case STV_PROTECTED:
3451 hash_table = elf_hash_table (info);
3452 if (!is_elf_hash_table (&hash_table->root))
3453 return false;
3455 bed = get_elf_backend_data (hash_table->dynobj);
3457 /* Proper resolution for function pointer equality may require
3458 that these symbols perhaps be resolved dynamically, even though
3459 we should be resolving them to the current module. */
3460 if (!not_local_protected || !bed->is_function_type (h->type))
3461 binding_stays_local_p = true;
3462 break;
3464 default:
3465 break;
3468 /* If it isn't defined locally, then clearly it's dynamic. */
3469 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
3470 return true;
3472 /* Otherwise, the symbol is dynamic if binding rules don't tell
3473 us that it remains local. */
3474 return !binding_stays_local_p;
3477 /* Return true if the symbol referred to by H should be considered
3478 to resolve local to the current module, and false otherwise. Differs
3479 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3480 undefined symbols. The two functions are virtually identical except
3481 for the place where dynindx == -1 is tested. If that test is true,
3482 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3483 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3484 defined symbols.
3485 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3486 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3487 treatment of undefined weak symbols. For those that do not make
3488 undefined weak symbols dynamic, both functions may return false. */
3490 bool
3491 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3492 struct bfd_link_info *info,
3493 bool local_protected)
3495 const struct elf_backend_data *bed;
3496 struct elf_link_hash_table *hash_table;
3498 /* If it's a local sym, of course we resolve locally. */
3499 if (h == NULL)
3500 return true;
3502 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3503 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3504 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3505 return true;
3507 /* Forced local symbols resolve locally. */
3508 if (h->forced_local)
3509 return true;
3511 /* Common symbols that become definitions don't get the DEF_REGULAR
3512 flag set, so test it first, and don't bail out. */
3513 if (ELF_COMMON_DEF_P (h))
3514 /* Do nothing. */;
3515 /* If we don't have a definition in a regular file, then we can't
3516 resolve locally. The sym is either undefined or dynamic. */
3517 else if (!h->def_regular)
3518 return false;
3520 /* Non-dynamic symbols resolve locally. */
3521 if (h->dynindx == -1)
3522 return true;
3524 /* At this point, we know the symbol is defined and dynamic. In an
3525 executable it must resolve locally, likewise when building symbolic
3526 shared libraries. */
3527 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3528 return true;
3530 /* Now deal with defined dynamic symbols in shared libraries. Ones
3531 with default visibility might not resolve locally. */
3532 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3533 return false;
3535 hash_table = elf_hash_table (info);
3536 if (!is_elf_hash_table (&hash_table->root))
3537 return true;
3539 /* STV_PROTECTED symbols with indirect external access are local. */
3540 if (info->indirect_extern_access > 0)
3541 return true;
3543 bed = get_elf_backend_data (hash_table->dynobj);
3545 /* If extern_protected_data is false, STV_PROTECTED non-function
3546 symbols are local. */
3547 if ((!info->extern_protected_data
3548 || (info->extern_protected_data < 0
3549 && !bed->extern_protected_data))
3550 && !bed->is_function_type (h->type))
3551 return true;
3553 /* Function pointer equality tests may require that STV_PROTECTED
3554 symbols be treated as dynamic symbols. If the address of a
3555 function not defined in an executable is set to that function's
3556 plt entry in the executable, then the address of the function in
3557 a shared library must also be the plt entry in the executable. */
3558 return local_protected;
3561 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3562 aligned. Returns the first TLS output section. */
3564 struct bfd_section *
3565 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3567 struct bfd_section *sec, *tls;
3568 unsigned int align = 0;
3570 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3571 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3572 break;
3573 tls = sec;
3575 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3576 if (sec->alignment_power > align)
3577 align = sec->alignment_power;
3579 elf_hash_table (info)->tls_sec = tls;
3581 /* Ensure the alignment of the first section (usually .tdata) is the largest
3582 alignment, so that the tls segment starts aligned. */
3583 if (tls != NULL)
3584 tls->alignment_power = align;
3586 return tls;
3589 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3590 static bool
3591 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3592 Elf_Internal_Sym *sym)
3594 const struct elf_backend_data *bed;
3596 /* Local symbols do not count, but target specific ones might. */
3597 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3598 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3599 return false;
3601 bed = get_elf_backend_data (abfd);
3602 /* Function symbols do not count. */
3603 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3604 return false;
3606 /* If the section is undefined, then so is the symbol. */
3607 if (sym->st_shndx == SHN_UNDEF)
3608 return false;
3610 /* If the symbol is defined in the common section, then
3611 it is a common definition and so does not count. */
3612 if (bed->common_definition (sym))
3613 return false;
3615 /* If the symbol is in a target specific section then we
3616 must rely upon the backend to tell us what it is. */
3617 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3618 /* FIXME - this function is not coded yet:
3620 return _bfd_is_global_symbol_definition (abfd, sym);
3622 Instead for now assume that the definition is not global,
3623 Even if this is wrong, at least the linker will behave
3624 in the same way that it used to do. */
3625 return false;
3627 return true;
3630 /* Search the symbol table of the archive element of the archive ABFD
3631 whose archive map contains a mention of SYMDEF, and determine if
3632 the symbol is defined in this element. */
3633 static bool
3634 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3636 Elf_Internal_Shdr * hdr;
3637 size_t symcount;
3638 size_t extsymcount;
3639 size_t extsymoff;
3640 Elf_Internal_Sym *isymbuf;
3641 Elf_Internal_Sym *isym;
3642 Elf_Internal_Sym *isymend;
3643 bool result;
3645 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset, NULL);
3646 if (abfd == NULL)
3647 return false;
3649 if (! bfd_check_format (abfd, bfd_object))
3650 return false;
3652 /* Select the appropriate symbol table. If we don't know if the
3653 object file is an IR object, give linker LTO plugin a chance to
3654 get the correct symbol table. */
3655 if (abfd->plugin_format == bfd_plugin_yes
3656 || abfd->plugin_format == bfd_plugin_yes_unused
3657 #if BFD_SUPPORTS_PLUGINS
3658 || (abfd->plugin_format == bfd_plugin_unknown
3659 && bfd_link_plugin_object_p (abfd))
3660 #endif
3663 /* Use the IR symbol table if the object has been claimed by
3664 plugin. */
3665 abfd = abfd->plugin_dummy_bfd;
3666 hdr = &elf_tdata (abfd)->symtab_hdr;
3668 else
3670 if (elf_use_dt_symtab_p (abfd))
3672 bfd_set_error (bfd_error_wrong_format);
3673 return false;
3676 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3677 hdr = &elf_tdata (abfd)->symtab_hdr;
3678 else
3679 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3682 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3684 /* The sh_info field of the symtab header tells us where the
3685 external symbols start. We don't care about the local symbols. */
3686 if (elf_bad_symtab (abfd))
3688 extsymcount = symcount;
3689 extsymoff = 0;
3691 else
3693 extsymcount = symcount - hdr->sh_info;
3694 extsymoff = hdr->sh_info;
3697 if (extsymcount == 0)
3698 return false;
3700 /* Read in the symbol table. */
3701 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3702 NULL, NULL, NULL);
3703 if (isymbuf == NULL)
3704 return false;
3706 /* Scan the symbol table looking for SYMDEF. */
3707 result = false;
3708 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3710 const char *name;
3712 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3713 isym->st_name);
3714 if (name == NULL)
3715 break;
3717 if (strcmp (name, symdef->name) == 0)
3719 result = is_global_data_symbol_definition (abfd, isym);
3720 break;
3724 free (isymbuf);
3726 return result;
3729 /* Add an entry to the .dynamic table. */
3731 bool
3732 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3733 bfd_vma tag,
3734 bfd_vma val)
3736 struct elf_link_hash_table *hash_table;
3737 const struct elf_backend_data *bed;
3738 asection *s;
3739 bfd_size_type newsize;
3740 bfd_byte *newcontents;
3741 Elf_Internal_Dyn dyn;
3743 hash_table = elf_hash_table (info);
3744 if (! is_elf_hash_table (&hash_table->root))
3745 return false;
3747 if (tag == DT_RELA || tag == DT_REL)
3748 hash_table->dynamic_relocs = true;
3750 bed = get_elf_backend_data (hash_table->dynobj);
3751 s = hash_table->dynamic;
3752 BFD_ASSERT (s != NULL);
3754 newsize = s->size + bed->s->sizeof_dyn;
3755 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3756 if (newcontents == NULL)
3757 return false;
3759 dyn.d_tag = tag;
3760 dyn.d_un.d_val = val;
3761 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3763 s->size = newsize;
3764 s->contents = newcontents;
3766 return true;
3769 /* Strip zero-sized dynamic sections. */
3771 bool
3772 _bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info *info)
3774 struct elf_link_hash_table *hash_table;
3775 const struct elf_backend_data *bed;
3776 asection *s, *sdynamic, **pp;
3777 asection *rela_dyn, *rel_dyn;
3778 Elf_Internal_Dyn dyn;
3779 bfd_byte *extdyn, *next;
3780 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3781 bool strip_zero_sized;
3782 bool strip_zero_sized_plt;
3784 if (bfd_link_relocatable (info))
3785 return true;
3787 hash_table = elf_hash_table (info);
3788 if (!is_elf_hash_table (&hash_table->root))
3789 return false;
3791 if (!hash_table->dynobj)
3792 return true;
3794 sdynamic= hash_table->dynamic;
3795 if (!sdynamic)
3796 return true;
3798 bed = get_elf_backend_data (hash_table->dynobj);
3799 swap_dyn_in = bed->s->swap_dyn_in;
3801 strip_zero_sized = false;
3802 strip_zero_sized_plt = false;
3804 /* Strip zero-sized dynamic sections. */
3805 rela_dyn = bfd_get_section_by_name (info->output_bfd, ".rela.dyn");
3806 rel_dyn = bfd_get_section_by_name (info->output_bfd, ".rel.dyn");
3807 for (pp = &info->output_bfd->sections; (s = *pp) != NULL;)
3808 if (s->size == 0
3809 && (s == rela_dyn
3810 || s == rel_dyn
3811 || s == hash_table->srelplt->output_section
3812 || s == hash_table->splt->output_section))
3814 *pp = s->next;
3815 info->output_bfd->section_count--;
3816 strip_zero_sized = true;
3817 if (s == rela_dyn)
3818 s = rela_dyn;
3819 if (s == rel_dyn)
3820 s = rel_dyn;
3821 else if (s == hash_table->splt->output_section)
3823 s = hash_table->splt;
3824 strip_zero_sized_plt = true;
3826 else
3827 s = hash_table->srelplt;
3828 s->flags |= SEC_EXCLUDE;
3829 s->output_section = bfd_abs_section_ptr;
3831 else
3832 pp = &s->next;
3834 if (strip_zero_sized_plt && sdynamic->size != 0)
3835 for (extdyn = sdynamic->contents;
3836 extdyn < sdynamic->contents + sdynamic->size;
3837 extdyn = next)
3839 next = extdyn + bed->s->sizeof_dyn;
3840 swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3841 switch (dyn.d_tag)
3843 default:
3844 break;
3845 case DT_JMPREL:
3846 case DT_PLTRELSZ:
3847 case DT_PLTREL:
3848 /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
3849 the procedure linkage table (the .plt section) has been
3850 removed. */
3851 memmove (extdyn, next,
3852 sdynamic->size - (next - sdynamic->contents));
3853 next = extdyn;
3857 if (strip_zero_sized)
3859 /* Regenerate program headers. */
3860 elf_seg_map (info->output_bfd) = NULL;
3861 return _bfd_elf_map_sections_to_segments (info->output_bfd, info,
3862 NULL);
3865 return true;
3868 /* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error,
3869 1 if a DT_NEEDED tag already exists, and 0 on success. */
3872 bfd_elf_add_dt_needed_tag (bfd *abfd, struct bfd_link_info *info)
3874 struct elf_link_hash_table *hash_table;
3875 size_t strindex;
3876 const char *soname;
3878 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3879 return -1;
3881 hash_table = elf_hash_table (info);
3882 soname = elf_dt_name (abfd);
3883 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, false);
3884 if (strindex == (size_t) -1)
3885 return -1;
3887 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3889 asection *sdyn;
3890 const struct elf_backend_data *bed;
3891 bfd_byte *extdyn;
3893 bed = get_elf_backend_data (hash_table->dynobj);
3894 sdyn = hash_table->dynamic;
3895 if (sdyn != NULL && sdyn->size != 0)
3896 for (extdyn = sdyn->contents;
3897 extdyn < sdyn->contents + sdyn->size;
3898 extdyn += bed->s->sizeof_dyn)
3900 Elf_Internal_Dyn dyn;
3902 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3903 if (dyn.d_tag == DT_NEEDED
3904 && dyn.d_un.d_val == strindex)
3906 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3907 return 1;
3912 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3913 return -1;
3915 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3916 return -1;
3918 return 0;
3921 /* Return true if SONAME is on the needed list between NEEDED and STOP
3922 (or the end of list if STOP is NULL), and needed by a library that
3923 will be loaded. */
3925 static bool
3926 on_needed_list (const char *soname,
3927 struct bfd_link_needed_list *needed,
3928 struct bfd_link_needed_list *stop)
3930 struct bfd_link_needed_list *look;
3931 for (look = needed; look != stop; look = look->next)
3932 if (strcmp (soname, look->name) == 0
3933 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3934 /* If needed by a library that itself is not directly
3935 needed, recursively check whether that library is
3936 indirectly needed. Since we add DT_NEEDED entries to
3937 the end of the list, library dependencies appear after
3938 the library. Therefore search prior to the current
3939 LOOK, preventing possible infinite recursion. */
3940 || on_needed_list (elf_dt_name (look->by), needed, look)))
3941 return true;
3943 return false;
3946 /* Sort symbol by value, section, size, and type. */
3947 static int
3948 elf_sort_symbol (const void *arg1, const void *arg2)
3950 const struct elf_link_hash_entry *h1;
3951 const struct elf_link_hash_entry *h2;
3952 bfd_signed_vma vdiff;
3953 int sdiff;
3954 const char *n1;
3955 const char *n2;
3957 h1 = *(const struct elf_link_hash_entry **) arg1;
3958 h2 = *(const struct elf_link_hash_entry **) arg2;
3959 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3960 if (vdiff != 0)
3961 return vdiff > 0 ? 1 : -1;
3963 sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3964 if (sdiff != 0)
3965 return sdiff;
3967 /* Sort so that sized symbols are selected over zero size symbols. */
3968 vdiff = h1->size - h2->size;
3969 if (vdiff != 0)
3970 return vdiff > 0 ? 1 : -1;
3972 /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */
3973 if (h1->type != h2->type)
3974 return h1->type - h2->type;
3976 /* If symbols are properly sized and typed, and multiple strong
3977 aliases are not defined in a shared library by the user we
3978 shouldn't get here. Unfortunately linker script symbols like
3979 __bss_start sometimes match a user symbol defined at the start of
3980 .bss without proper size and type. We'd like to preference the
3981 user symbol over reserved system symbols. Sort on leading
3982 underscores. */
3983 n1 = h1->root.root.string;
3984 n2 = h2->root.root.string;
3985 while (*n1 == *n2)
3987 if (*n1 == 0)
3988 break;
3989 ++n1;
3990 ++n2;
3992 if (*n1 == '_')
3993 return -1;
3994 if (*n2 == '_')
3995 return 1;
3997 /* Final sort on name selects user symbols like '_u' over reserved
3998 system symbols like '_Z' and also will avoid qsort instability. */
3999 return *n1 - *n2;
4002 /* This function is used to adjust offsets into .dynstr for
4003 dynamic symbols. This is called via elf_link_hash_traverse. */
4005 static bool
4006 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
4008 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
4010 if (h->dynindx != -1)
4011 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
4012 return true;
4015 /* Assign string offsets in .dynstr, update all structures referencing
4016 them. */
4018 static bool
4019 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
4021 struct elf_link_hash_table *hash_table = elf_hash_table (info);
4022 struct elf_link_local_dynamic_entry *entry;
4023 struct elf_strtab_hash *dynstr = hash_table->dynstr;
4024 bfd *dynobj = hash_table->dynobj;
4025 asection *sdyn;
4026 bfd_size_type size;
4027 const struct elf_backend_data *bed;
4028 bfd_byte *extdyn;
4030 _bfd_elf_strtab_finalize (dynstr);
4031 size = _bfd_elf_strtab_size (dynstr);
4033 /* Allow the linker to examine the dynsymtab now it's fully populated. */
4035 if (info->callbacks->examine_strtab)
4036 info->callbacks->examine_strtab (dynstr);
4038 bed = get_elf_backend_data (dynobj);
4039 sdyn = hash_table->dynamic;
4040 BFD_ASSERT (sdyn != NULL);
4042 /* Update all .dynamic entries referencing .dynstr strings. */
4043 for (extdyn = sdyn->contents;
4044 extdyn < PTR_ADD (sdyn->contents, sdyn->size);
4045 extdyn += bed->s->sizeof_dyn)
4047 Elf_Internal_Dyn dyn;
4049 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
4050 switch (dyn.d_tag)
4052 case DT_STRSZ:
4053 dyn.d_un.d_val = size;
4054 break;
4055 case DT_NEEDED:
4056 case DT_SONAME:
4057 case DT_RPATH:
4058 case DT_RUNPATH:
4059 case DT_FILTER:
4060 case DT_AUXILIARY:
4061 case DT_AUDIT:
4062 case DT_DEPAUDIT:
4063 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
4064 break;
4065 default:
4066 continue;
4068 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
4071 /* Now update local dynamic symbols. */
4072 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
4073 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
4074 entry->isym.st_name);
4076 /* And the rest of dynamic symbols. */
4077 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
4079 /* Adjust version definitions. */
4080 if (elf_tdata (output_bfd)->cverdefs)
4082 asection *s;
4083 bfd_byte *p;
4084 size_t i;
4085 Elf_Internal_Verdef def;
4086 Elf_Internal_Verdaux defaux;
4088 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
4089 p = s->contents;
4092 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
4093 &def);
4094 p += sizeof (Elf_External_Verdef);
4095 if (def.vd_aux != sizeof (Elf_External_Verdef))
4096 continue;
4097 for (i = 0; i < def.vd_cnt; ++i)
4099 _bfd_elf_swap_verdaux_in (output_bfd,
4100 (Elf_External_Verdaux *) p, &defaux);
4101 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
4102 defaux.vda_name);
4103 _bfd_elf_swap_verdaux_out (output_bfd,
4104 &defaux, (Elf_External_Verdaux *) p);
4105 p += sizeof (Elf_External_Verdaux);
4108 while (def.vd_next);
4111 /* Adjust version references. */
4112 if (elf_tdata (output_bfd)->verref)
4114 asection *s;
4115 bfd_byte *p;
4116 size_t i;
4117 Elf_Internal_Verneed need;
4118 Elf_Internal_Vernaux needaux;
4120 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
4121 p = s->contents;
4124 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
4125 &need);
4126 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
4127 _bfd_elf_swap_verneed_out (output_bfd, &need,
4128 (Elf_External_Verneed *) p);
4129 p += sizeof (Elf_External_Verneed);
4130 for (i = 0; i < need.vn_cnt; ++i)
4132 _bfd_elf_swap_vernaux_in (output_bfd,
4133 (Elf_External_Vernaux *) p, &needaux);
4134 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
4135 needaux.vna_name);
4136 _bfd_elf_swap_vernaux_out (output_bfd,
4137 &needaux,
4138 (Elf_External_Vernaux *) p);
4139 p += sizeof (Elf_External_Vernaux);
4142 while (need.vn_next);
4145 return true;
4148 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4149 The default is to only match when the INPUT and OUTPUT are exactly
4150 the same target. */
4152 bool
4153 _bfd_elf_default_relocs_compatible (const bfd_target *input,
4154 const bfd_target *output)
4156 return input == output;
4159 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4160 This version is used when different targets for the same architecture
4161 are virtually identical. */
4163 bool
4164 _bfd_elf_relocs_compatible (const bfd_target *input,
4165 const bfd_target *output)
4167 const struct elf_backend_data *obed, *ibed;
4169 if (input == output)
4170 return true;
4172 ibed = xvec_get_elf_backend_data (input);
4173 obed = xvec_get_elf_backend_data (output);
4175 if (ibed->arch != obed->arch)
4176 return false;
4178 /* If both backends are using this function, deem them compatible. */
4179 return ibed->relocs_compatible == obed->relocs_compatible;
4182 /* Make a special call to the linker "notice" function to tell it that
4183 we are about to handle an as-needed lib, or have finished
4184 processing the lib. */
4186 bool
4187 _bfd_elf_notice_as_needed (bfd *ibfd,
4188 struct bfd_link_info *info,
4189 enum notice_asneeded_action act)
4191 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
4194 /* Call ACTION on each relocation in an ELF object file. */
4196 bool
4197 _bfd_elf_link_iterate_on_relocs
4198 (bfd *abfd, struct bfd_link_info *info,
4199 bool (*action) (bfd *, struct bfd_link_info *, asection *,
4200 const Elf_Internal_Rela *))
4202 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4203 struct elf_link_hash_table *htab = elf_hash_table (info);
4205 /* If this object is the same format as the output object, and it is
4206 not a shared library, then let the backend look through the
4207 relocs.
4209 This is required to build global offset table entries and to
4210 arrange for dynamic relocs. It is not required for the
4211 particular common case of linking non PIC code, even when linking
4212 against shared libraries, but unfortunately there is no way of
4213 knowing whether an object file has been compiled PIC or not.
4214 Looking through the relocs is not particularly time consuming.
4215 The problem is that we must either (1) keep the relocs in memory,
4216 which causes the linker to require additional runtime memory or
4217 (2) read the relocs twice from the input file, which wastes time.
4218 This would be a good case for using mmap.
4220 I have no idea how to handle linking PIC code into a file of a
4221 different format. It probably can't be done. */
4222 if ((abfd->flags & DYNAMIC) == 0
4223 && is_elf_hash_table (&htab->root)
4224 && elf_object_id (abfd) == elf_hash_table_id (htab)
4225 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4227 asection *o;
4229 for (o = abfd->sections; o != NULL; o = o->next)
4231 Elf_Internal_Rela *internal_relocs;
4232 bool ok;
4234 /* Don't check relocations in excluded sections. Don't do
4235 anything special with non-loaded, non-alloced sections.
4236 In particular, any relocs in such sections should not
4237 affect GOT and PLT reference counting (ie. we don't
4238 allow them to create GOT or PLT entries), there's no
4239 possibility or desire to optimize TLS relocs, and
4240 there's not much point in propagating relocs to shared
4241 libs that the dynamic linker won't relocate. */
4242 if ((o->flags & SEC_ALLOC) == 0
4243 || (o->flags & SEC_RELOC) == 0
4244 || (o->flags & SEC_EXCLUDE) != 0
4245 || o->reloc_count == 0
4246 || ((info->strip == strip_all || info->strip == strip_debugger)
4247 && (o->flags & SEC_DEBUGGING) != 0)
4248 || bfd_is_abs_section (o->output_section))
4249 continue;
4251 internal_relocs = _bfd_elf_link_info_read_relocs
4252 (abfd, info, o, NULL, NULL,
4253 _bfd_elf_link_keep_memory (info));
4254 if (internal_relocs == NULL)
4255 return false;
4257 ok = action (abfd, info, o, internal_relocs);
4259 if (elf_section_data (o)->relocs != internal_relocs)
4260 free (internal_relocs);
4262 if (! ok)
4263 return false;
4267 return true;
4270 /* Check relocations in an ELF object file. This is called after
4271 all input files have been opened. */
4273 bool
4274 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
4276 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4277 if (bed->check_relocs != NULL)
4278 return _bfd_elf_link_iterate_on_relocs (abfd, info,
4279 bed->check_relocs);
4280 return true;
4283 /* An entry in the first definition hash table. */
4285 struct elf_link_first_hash_entry
4287 struct bfd_hash_entry root;
4288 /* The object of the first definition. */
4289 bfd *abfd;
4292 /* The function to create a new entry in the first definition hash
4293 table. */
4295 static struct bfd_hash_entry *
4296 elf_link_first_hash_newfunc (struct bfd_hash_entry *entry,
4297 struct bfd_hash_table *table,
4298 const char *string)
4300 struct elf_link_first_hash_entry *ret =
4301 (struct elf_link_first_hash_entry *) entry;
4303 /* Allocate the structure if it has not already been allocated by a
4304 subclass. */
4305 if (ret == NULL)
4306 ret = (struct elf_link_first_hash_entry *)
4307 bfd_hash_allocate (table,
4308 sizeof (struct elf_link_first_hash_entry));
4309 if (ret == NULL)
4310 return NULL;
4312 /* Call the allocation method of the superclass. */
4313 ret = ((struct elf_link_first_hash_entry *)
4314 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table,
4315 string));
4316 if (ret != NULL)
4317 ret->abfd = NULL;
4319 return (struct bfd_hash_entry *) ret;
4322 /* Add the symbol NAME from ABFD to first hash. */
4324 static void
4325 elf_link_add_to_first_hash (bfd *abfd, struct bfd_link_info *info,
4326 const char *name, bool copy)
4328 struct elf_link_hash_table *htab = elf_hash_table (info);
4329 /* Skip if there is no first hash. */
4330 if (htab->first_hash == NULL)
4331 return;
4333 struct elf_link_first_hash_entry *e
4334 = ((struct elf_link_first_hash_entry *)
4335 bfd_hash_lookup (htab->first_hash, name, true, copy));
4336 if (e == NULL)
4337 info->callbacks->einfo
4338 (_("%F%P: %pB: failed to add %s to first hash\n"), abfd, name);
4340 if (e->abfd == NULL)
4341 /* Store ABFD in abfd. */
4342 e->abfd = abfd;
4345 /* Add symbols from an ELF object file to the linker hash table. */
4347 static bool
4348 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
4350 Elf_Internal_Ehdr *ehdr;
4351 Elf_Internal_Shdr *hdr;
4352 size_t symcount;
4353 size_t extsymcount;
4354 size_t extsymoff;
4355 struct elf_link_hash_entry **sym_hash;
4356 bool dynamic;
4357 Elf_External_Versym *extversym = NULL;
4358 Elf_External_Versym *extversym_end = NULL;
4359 Elf_External_Versym *ever;
4360 struct elf_link_hash_entry *weaks;
4361 struct elf_link_hash_entry **nondeflt_vers = NULL;
4362 size_t nondeflt_vers_cnt = 0;
4363 Elf_Internal_Sym *isymbuf = NULL;
4364 Elf_Internal_Sym *isym;
4365 Elf_Internal_Sym *isymend;
4366 const struct elf_backend_data *bed;
4367 bool add_needed;
4368 struct elf_link_hash_table *htab;
4369 void *alloc_mark = NULL;
4370 struct bfd_hash_entry **old_table = NULL;
4371 unsigned int old_size = 0;
4372 unsigned int old_count = 0;
4373 void *old_tab = NULL;
4374 void *old_ent;
4375 struct bfd_link_hash_entry *old_undefs = NULL;
4376 struct bfd_link_hash_entry *old_undefs_tail = NULL;
4377 void *old_strtab = NULL;
4378 size_t tabsize = 0;
4379 asection *s;
4380 bool just_syms;
4382 htab = elf_hash_table (info);
4383 bed = get_elf_backend_data (abfd);
4385 if (elf_use_dt_symtab_p (abfd))
4387 bfd_set_error (bfd_error_wrong_format);
4388 return false;
4391 if ((abfd->flags & DYNAMIC) == 0)
4393 dynamic = false;
4394 if ((abfd->flags & BFD_PLUGIN) != 0
4395 && is_elf_hash_table (&htab->root)
4396 && htab->first_hash == NULL)
4398 /* Initialize first_hash for an IR input. */
4399 htab->first_hash = (struct bfd_hash_table *)
4400 bfd_malloc (sizeof (struct bfd_hash_table));
4401 if (htab->first_hash == NULL
4402 || !bfd_hash_table_init
4403 (htab->first_hash, elf_link_first_hash_newfunc,
4404 sizeof (struct elf_link_first_hash_entry)))
4405 info->callbacks->einfo
4406 (_("%F%P: first_hash failed to create: %E\n"));
4409 else
4411 dynamic = true;
4413 /* You can't use -r against a dynamic object. Also, there's no
4414 hope of using a dynamic object which does not exactly match
4415 the format of the output file. */
4416 if (bfd_link_relocatable (info)
4417 || !is_elf_hash_table (&htab->root)
4418 || info->output_bfd->xvec != abfd->xvec)
4420 if (bfd_link_relocatable (info))
4421 bfd_set_error (bfd_error_invalid_operation);
4422 else
4423 bfd_set_error (bfd_error_wrong_format);
4424 goto error_return;
4428 ehdr = elf_elfheader (abfd);
4429 if (info->warn_alternate_em
4430 && bed->elf_machine_code != ehdr->e_machine
4431 && ((bed->elf_machine_alt1 != 0
4432 && ehdr->e_machine == bed->elf_machine_alt1)
4433 || (bed->elf_machine_alt2 != 0
4434 && ehdr->e_machine == bed->elf_machine_alt2)))
4435 _bfd_error_handler
4436 /* xgettext:c-format */
4437 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
4438 ehdr->e_machine, abfd, bed->elf_machine_code);
4440 /* As a GNU extension, any input sections which are named
4441 .gnu.warning.SYMBOL are treated as warning symbols for the given
4442 symbol. This differs from .gnu.warning sections, which generate
4443 warnings when they are included in an output file. */
4444 /* PR 12761: Also generate this warning when building shared libraries. */
4445 for (s = abfd->sections; s != NULL; s = s->next)
4447 const char *name;
4449 name = bfd_section_name (s);
4450 if (startswith (name, ".gnu.warning."))
4452 char *msg;
4453 bfd_size_type sz;
4455 name += sizeof ".gnu.warning." - 1;
4457 /* If this is a shared object, then look up the symbol
4458 in the hash table. If it is there, and it is already
4459 been defined, then we will not be using the entry
4460 from this shared object, so we don't need to warn.
4461 FIXME: If we see the definition in a regular object
4462 later on, we will warn, but we shouldn't. The only
4463 fix is to keep track of what warnings we are supposed
4464 to emit, and then handle them all at the end of the
4465 link. */
4466 if (dynamic)
4468 struct elf_link_hash_entry *h;
4470 h = elf_link_hash_lookup (htab, name, false, false, true);
4472 /* FIXME: What about bfd_link_hash_common? */
4473 if (h != NULL
4474 && (h->root.type == bfd_link_hash_defined
4475 || h->root.type == bfd_link_hash_defweak))
4476 continue;
4479 sz = s->size;
4480 msg = (char *) bfd_alloc (abfd, sz + 1);
4481 if (msg == NULL)
4482 goto error_return;
4484 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
4485 goto error_return;
4487 msg[sz] = '\0';
4489 if (! (_bfd_generic_link_add_one_symbol
4490 (info, abfd, name, BSF_WARNING, s, 0, msg,
4491 false, bed->collect, NULL)))
4492 goto error_return;
4494 if (bfd_link_executable (info))
4496 /* Clobber the section size so that the warning does
4497 not get copied into the output file. */
4498 s->size = 0;
4500 /* Also set SEC_EXCLUDE, so that symbols defined in
4501 the warning section don't get copied to the output. */
4502 s->flags |= SEC_EXCLUDE;
4507 just_syms = ((s = abfd->sections) != NULL
4508 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
4510 add_needed = true;
4511 if (! dynamic)
4513 /* If we are creating a shared library, create all the dynamic
4514 sections immediately. We need to attach them to something,
4515 so we attach them to this BFD, provided it is the right
4516 format and is not from ld --just-symbols. Always create the
4517 dynamic sections for -E/--dynamic-list. FIXME: If there
4518 are no input BFD's of the same format as the output, we can't
4519 make a shared library. */
4520 if (!just_syms
4521 && (bfd_link_pic (info)
4522 || (!bfd_link_relocatable (info)
4523 && info->nointerp
4524 && (info->export_dynamic || info->dynamic)))
4525 && is_elf_hash_table (&htab->root)
4526 && info->output_bfd->xvec == abfd->xvec
4527 && !htab->dynamic_sections_created)
4529 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4530 goto error_return;
4533 else if (!is_elf_hash_table (&htab->root))
4534 goto error_return;
4535 else
4537 const char *soname = NULL;
4538 char *audit = NULL;
4539 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
4540 const Elf_Internal_Phdr *phdr;
4541 struct elf_link_loaded_list *loaded_lib;
4543 /* ld --just-symbols and dynamic objects don't mix very well.
4544 ld shouldn't allow it. */
4545 if (just_syms)
4546 abort ();
4548 /* If this dynamic lib was specified on the command line with
4549 --as-needed in effect, then we don't want to add a DT_NEEDED
4550 tag unless the lib is actually used. Similary for libs brought
4551 in by another lib's DT_NEEDED. When --no-add-needed is used
4552 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4553 any dynamic library in DT_NEEDED tags in the dynamic lib at
4554 all. */
4555 add_needed = (elf_dyn_lib_class (abfd)
4556 & (DYN_AS_NEEDED | DYN_DT_NEEDED
4557 | DYN_NO_NEEDED)) == 0;
4559 s = bfd_get_section_by_name (abfd, ".dynamic");
4560 if (s != NULL && s->size != 0 && (s->flags & SEC_HAS_CONTENTS) != 0)
4562 bfd_byte *dynbuf;
4563 bfd_byte *extdyn;
4564 unsigned int elfsec;
4565 unsigned long shlink;
4567 if (!_bfd_elf_mmap_section_contents (abfd, s, &dynbuf))
4569 error_free_dyn:
4570 _bfd_elf_munmap_section_contents (s, dynbuf);
4571 goto error_return;
4574 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
4575 if (elfsec == SHN_BAD)
4576 goto error_free_dyn;
4577 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4579 for (extdyn = dynbuf;
4580 (size_t) (dynbuf + s->size - extdyn) >= bed->s->sizeof_dyn;
4581 extdyn += bed->s->sizeof_dyn)
4583 Elf_Internal_Dyn dyn;
4585 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4586 if (dyn.d_tag == DT_SONAME)
4588 unsigned int tagv = dyn.d_un.d_val;
4589 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4590 if (soname == NULL)
4591 goto error_free_dyn;
4593 if (dyn.d_tag == DT_NEEDED)
4595 struct bfd_link_needed_list *n, **pn;
4596 char *fnm, *anm;
4597 unsigned int tagv = dyn.d_un.d_val;
4598 size_t amt = sizeof (struct bfd_link_needed_list);
4600 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4601 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4602 if (n == NULL || fnm == NULL)
4603 goto error_free_dyn;
4604 amt = strlen (fnm) + 1;
4605 anm = (char *) bfd_alloc (abfd, amt);
4606 if (anm == NULL)
4607 goto error_free_dyn;
4608 memcpy (anm, fnm, amt);
4609 n->name = anm;
4610 n->by = abfd;
4611 n->next = NULL;
4612 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4614 *pn = n;
4616 if (dyn.d_tag == DT_RUNPATH)
4618 struct bfd_link_needed_list *n, **pn;
4619 char *fnm, *anm;
4620 unsigned int tagv = dyn.d_un.d_val;
4621 size_t amt = sizeof (struct bfd_link_needed_list);
4623 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4624 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4625 if (n == NULL || fnm == NULL)
4626 goto error_free_dyn;
4627 amt = strlen (fnm) + 1;
4628 anm = (char *) bfd_alloc (abfd, amt);
4629 if (anm == NULL)
4630 goto error_free_dyn;
4631 memcpy (anm, fnm, amt);
4632 n->name = anm;
4633 n->by = abfd;
4634 n->next = NULL;
4635 for (pn = & runpath;
4636 *pn != NULL;
4637 pn = &(*pn)->next)
4639 *pn = n;
4641 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4642 if (!runpath && dyn.d_tag == DT_RPATH)
4644 struct bfd_link_needed_list *n, **pn;
4645 char *fnm, *anm;
4646 unsigned int tagv = dyn.d_un.d_val;
4647 size_t amt = sizeof (struct bfd_link_needed_list);
4649 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4650 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4651 if (n == NULL || fnm == NULL)
4652 goto error_free_dyn;
4653 amt = strlen (fnm) + 1;
4654 anm = (char *) bfd_alloc (abfd, amt);
4655 if (anm == NULL)
4656 goto error_free_dyn;
4657 memcpy (anm, fnm, amt);
4658 n->name = anm;
4659 n->by = abfd;
4660 n->next = NULL;
4661 for (pn = & rpath;
4662 *pn != NULL;
4663 pn = &(*pn)->next)
4665 *pn = n;
4667 if (dyn.d_tag == DT_AUDIT)
4669 unsigned int tagv = dyn.d_un.d_val;
4670 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4672 if (dyn.d_tag == DT_FLAGS_1)
4673 elf_tdata (abfd)->is_pie = (dyn.d_un.d_val & DF_1_PIE) != 0;
4676 _bfd_elf_munmap_section_contents (s, dynbuf);
4679 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4680 frees all more recently bfd_alloc'd blocks as well. */
4681 if (runpath)
4682 rpath = runpath;
4684 if (rpath)
4686 struct bfd_link_needed_list **pn;
4687 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4689 *pn = rpath;
4692 /* If we have a PT_GNU_RELRO program header, mark as read-only
4693 all sections contained fully therein. This makes relro
4694 shared library sections appear as they will at run-time. */
4695 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4696 while (phdr-- > elf_tdata (abfd)->phdr)
4697 if (phdr->p_type == PT_GNU_RELRO)
4699 for (s = abfd->sections; s != NULL; s = s->next)
4701 unsigned int opb = bfd_octets_per_byte (abfd, s);
4703 if ((s->flags & SEC_ALLOC) != 0
4704 && s->vma * opb >= phdr->p_vaddr
4705 && s->vma * opb + s->size <= phdr->p_vaddr + phdr->p_memsz)
4706 s->flags |= SEC_READONLY;
4708 break;
4711 /* We do not want to include any of the sections in a dynamic
4712 object in the output file. We hack by simply clobbering the
4713 list of sections in the BFD. This could be handled more
4714 cleanly by, say, a new section flag; the existing
4715 SEC_NEVER_LOAD flag is not the one we want, because that one
4716 still implies that the section takes up space in the output
4717 file. */
4718 bfd_section_list_clear (abfd);
4720 /* Find the name to use in a DT_NEEDED entry that refers to this
4721 object. If the object has a DT_SONAME entry, we use it.
4722 Otherwise, if the generic linker stuck something in
4723 elf_dt_name, we use that. Otherwise, we just use the file
4724 name. */
4725 if (soname == NULL || *soname == '\0')
4727 soname = elf_dt_name (abfd);
4728 if (soname == NULL || *soname == '\0')
4729 soname = bfd_get_filename (abfd);
4732 /* Save the SONAME because sometimes the linker emulation code
4733 will need to know it. */
4734 elf_dt_name (abfd) = soname;
4736 /* If we have already included this dynamic object in the
4737 link, just ignore it. There is no reason to include a
4738 particular dynamic object more than once. */
4739 for (loaded_lib = htab->dyn_loaded;
4740 loaded_lib != NULL;
4741 loaded_lib = loaded_lib->next)
4743 if (strcmp (elf_dt_name (loaded_lib->abfd), soname) == 0)
4744 return true;
4747 /* Create dynamic sections for backends that require that be done
4748 before setup_gnu_properties. */
4749 if (add_needed
4750 && !_bfd_elf_link_create_dynamic_sections (abfd, info))
4751 return false;
4753 /* Save the DT_AUDIT entry for the linker emulation code. */
4754 elf_dt_audit (abfd) = audit;
4757 /* If this is a dynamic object, we always link against the .dynsym
4758 symbol table, not the .symtab symbol table. The dynamic linker
4759 will only see the .dynsym symbol table, so there is no reason to
4760 look at .symtab for a dynamic object. */
4762 if (! dynamic || elf_dynsymtab (abfd) == 0)
4763 hdr = &elf_tdata (abfd)->symtab_hdr;
4764 else
4765 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4767 symcount = hdr->sh_size / bed->s->sizeof_sym;
4769 /* The sh_info field of the symtab header tells us where the
4770 external symbols start. We don't care about the local symbols at
4771 this point. */
4772 if (elf_bad_symtab (abfd))
4774 extsymcount = symcount;
4775 extsymoff = 0;
4777 else
4779 extsymcount = symcount - hdr->sh_info;
4780 extsymoff = hdr->sh_info;
4783 sym_hash = elf_sym_hashes (abfd);
4784 if (extsymcount != 0)
4786 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4787 NULL, NULL, NULL);
4788 if (isymbuf == NULL)
4789 goto error_return;
4791 if (sym_hash == NULL)
4793 /* We store a pointer to the hash table entry for each
4794 external symbol. */
4795 size_t amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4796 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4797 if (sym_hash == NULL)
4798 goto error_free_sym;
4799 elf_sym_hashes (abfd) = sym_hash;
4803 if (dynamic)
4805 /* Read in any version definitions. */
4806 if (!_bfd_elf_slurp_version_tables (abfd,
4807 info->default_imported_symver))
4808 goto error_free_sym;
4810 /* Read in the symbol versions, but don't bother to convert them
4811 to internal format. */
4812 if (elf_dynversym (abfd) != 0)
4814 Elf_Internal_Shdr *versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4815 bfd_size_type amt = versymhdr->sh_size;
4817 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0)
4818 goto error_free_sym;
4819 extversym = (Elf_External_Versym *)
4820 _bfd_malloc_and_read (abfd, amt, amt);
4821 if (extversym == NULL)
4822 goto error_free_sym;
4823 extversym_end = extversym + amt / sizeof (*extversym);
4827 /* If we are loading an as-needed shared lib, save the symbol table
4828 state before we start adding symbols. If the lib turns out
4829 to be unneeded, restore the state. */
4830 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4832 unsigned int i;
4833 size_t entsize;
4835 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4837 struct bfd_hash_entry *p;
4838 struct elf_link_hash_entry *h;
4840 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4842 h = (struct elf_link_hash_entry *) p;
4843 entsize += htab->root.table.entsize;
4844 if (h->root.type == bfd_link_hash_warning)
4846 entsize += htab->root.table.entsize;
4847 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4849 if (h->root.type == bfd_link_hash_common)
4850 entsize += sizeof (*h->root.u.c.p);
4854 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4855 old_tab = bfd_malloc (tabsize + entsize);
4856 if (old_tab == NULL)
4857 goto error_free_vers;
4859 /* Remember the current objalloc pointer, so that all mem for
4860 symbols added can later be reclaimed. */
4861 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4862 if (alloc_mark == NULL)
4863 goto error_free_vers;
4865 /* Make a special call to the linker "notice" function to
4866 tell it that we are about to handle an as-needed lib. */
4867 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4868 goto error_free_vers;
4870 /* Clone the symbol table. Remember some pointers into the
4871 symbol table, and dynamic symbol count. */
4872 old_ent = (char *) old_tab + tabsize;
4873 memcpy (old_tab, htab->root.table.table, tabsize);
4874 old_undefs = htab->root.undefs;
4875 old_undefs_tail = htab->root.undefs_tail;
4876 old_table = htab->root.table.table;
4877 old_size = htab->root.table.size;
4878 old_count = htab->root.table.count;
4879 old_strtab = NULL;
4880 if (htab->dynstr != NULL)
4882 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4883 if (old_strtab == NULL)
4884 goto error_free_vers;
4887 for (i = 0; i < htab->root.table.size; i++)
4889 struct bfd_hash_entry *p;
4890 struct elf_link_hash_entry *h;
4892 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4894 h = (struct elf_link_hash_entry *) p;
4895 memcpy (old_ent, h, htab->root.table.entsize);
4896 old_ent = (char *) old_ent + htab->root.table.entsize;
4897 if (h->root.type == bfd_link_hash_warning)
4899 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4900 memcpy (old_ent, h, htab->root.table.entsize);
4901 old_ent = (char *) old_ent + htab->root.table.entsize;
4903 if (h->root.type == bfd_link_hash_common)
4905 memcpy (old_ent, h->root.u.c.p, sizeof (*h->root.u.c.p));
4906 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
4912 weaks = NULL;
4913 if (extversym == NULL)
4914 ever = NULL;
4915 else if (extversym + extsymoff < extversym_end)
4916 ever = extversym + extsymoff;
4917 else
4919 /* xgettext:c-format */
4920 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4921 abfd, (long) extsymoff,
4922 (long) (extversym_end - extversym) / sizeof (* extversym));
4923 bfd_set_error (bfd_error_bad_value);
4924 goto error_free_vers;
4927 if (!bfd_link_relocatable (info)
4928 && bfd_get_lto_type (abfd) == lto_slim_ir_object)
4930 _bfd_error_handler
4931 (_("%pB: plugin needed to handle lto object"), abfd);
4934 for (isym = isymbuf, isymend = PTR_ADD (isymbuf, extsymcount);
4935 isym < isymend;
4936 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4938 int bind;
4939 bfd_vma value;
4940 asection *sec, *new_sec;
4941 flagword flags;
4942 const char *name;
4943 bool must_copy_name = false;
4944 struct elf_link_hash_entry *h;
4945 struct elf_link_hash_entry *hi;
4946 bool definition;
4947 bool size_change_ok;
4948 bool type_change_ok;
4949 bool new_weak;
4950 bool old_weak;
4951 bfd *override;
4952 bool common;
4953 bool discarded;
4954 unsigned int old_alignment;
4955 unsigned int shindex;
4956 bfd *old_bfd;
4957 bool matched;
4959 override = NULL;
4961 flags = BSF_NO_FLAGS;
4962 sec = NULL;
4963 value = isym->st_value;
4964 common = bed->common_definition (isym);
4965 if (common && info->inhibit_common_definition)
4967 /* Treat common symbol as undefined for --no-define-common. */
4968 isym->st_shndx = SHN_UNDEF;
4969 common = false;
4971 discarded = false;
4973 bind = ELF_ST_BIND (isym->st_info);
4974 switch (bind)
4976 case STB_LOCAL:
4977 /* This should be impossible, since ELF requires that all
4978 global symbols follow all local symbols, and that sh_info
4979 point to the first global symbol. Unfortunately, Irix 5
4980 screws this up. */
4981 if (elf_bad_symtab (abfd))
4982 continue;
4984 /* If we aren't prepared to handle locals within the globals
4985 then we'll likely segfault on a NULL symbol hash if the
4986 symbol is ever referenced in relocations. */
4987 shindex = elf_elfheader (abfd)->e_shstrndx;
4988 name = bfd_elf_string_from_elf_section (abfd, shindex, hdr->sh_name);
4989 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4990 " (>= sh_info of %lu)"),
4991 abfd, name, (long) (isym - isymbuf + extsymoff),
4992 (long) extsymoff);
4994 /* Dynamic object relocations are not processed by ld, so
4995 ld won't run into the problem mentioned above. */
4996 if (dynamic)
4997 continue;
4998 bfd_set_error (bfd_error_bad_value);
4999 goto error_free_vers;
5001 case STB_GLOBAL:
5002 if (isym->st_shndx != SHN_UNDEF && !common)
5003 flags = BSF_GLOBAL;
5004 break;
5006 case STB_WEAK:
5007 flags = BSF_WEAK;
5008 break;
5010 case STB_GNU_UNIQUE:
5011 flags = BSF_GNU_UNIQUE;
5012 break;
5014 default:
5015 /* Leave it up to the processor backend. */
5016 break;
5019 if (isym->st_shndx == SHN_UNDEF)
5020 sec = bfd_und_section_ptr;
5021 else if (isym->st_shndx == SHN_ABS)
5022 sec = bfd_abs_section_ptr;
5023 else if (isym->st_shndx == SHN_COMMON)
5025 sec = bfd_com_section_ptr;
5026 /* What ELF calls the size we call the value. What ELF
5027 calls the value we call the alignment. */
5028 value = isym->st_size;
5030 else
5032 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
5033 if (sec == NULL)
5034 sec = bfd_abs_section_ptr;
5035 else if (discarded_section (sec))
5037 /* Symbols from discarded section are undefined. We keep
5038 its visibility. */
5039 sec = bfd_und_section_ptr;
5040 discarded = true;
5041 isym->st_shndx = SHN_UNDEF;
5043 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
5044 value -= sec->vma;
5047 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5048 isym->st_name);
5049 if (name == NULL)
5050 goto error_free_vers;
5052 if (isym->st_shndx == SHN_COMMON
5053 && (abfd->flags & BFD_PLUGIN) != 0)
5055 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
5057 if (xc == NULL)
5059 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
5060 | SEC_EXCLUDE);
5061 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
5062 if (xc == NULL)
5063 goto error_free_vers;
5065 sec = xc;
5067 else if (isym->st_shndx == SHN_COMMON
5068 && ELF_ST_TYPE (isym->st_info) == STT_TLS
5069 && !bfd_link_relocatable (info))
5071 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
5073 if (tcomm == NULL)
5075 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
5076 | SEC_LINKER_CREATED);
5077 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
5078 if (tcomm == NULL)
5079 goto error_free_vers;
5081 sec = tcomm;
5083 else if (bed->elf_add_symbol_hook)
5085 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
5086 &sec, &value))
5087 goto error_free_vers;
5089 /* The hook function sets the name to NULL if this symbol
5090 should be skipped for some reason. */
5091 if (name == NULL)
5092 continue;
5095 /* Sanity check that all possibilities were handled. */
5096 if (sec == NULL)
5097 abort ();
5099 /* Silently discard TLS symbols from --just-syms. There's
5100 no way to combine a static TLS block with a new TLS block
5101 for this executable. */
5102 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
5103 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
5104 continue;
5106 if (bfd_is_und_section (sec)
5107 || bfd_is_com_section (sec))
5108 definition = false;
5109 else
5110 definition = true;
5112 size_change_ok = false;
5113 type_change_ok = bed->type_change_ok;
5114 old_weak = false;
5115 matched = false;
5116 old_alignment = 0;
5117 old_bfd = NULL;
5118 new_sec = sec;
5120 if (is_elf_hash_table (&htab->root))
5122 Elf_Internal_Versym iver;
5123 unsigned int vernum = 0;
5124 bool skip;
5126 if (ever == NULL)
5128 if (info->default_imported_symver)
5129 /* Use the default symbol version created earlier. */
5130 iver.vs_vers = elf_tdata (abfd)->cverdefs;
5131 else
5132 iver.vs_vers = 0;
5134 else if (ever >= extversym_end)
5136 /* xgettext:c-format */
5137 _bfd_error_handler (_("%pB: not enough version information"),
5138 abfd);
5139 bfd_set_error (bfd_error_bad_value);
5140 goto error_free_vers;
5142 else
5143 _bfd_elf_swap_versym_in (abfd, ever, &iver);
5145 vernum = iver.vs_vers & VERSYM_VERSION;
5147 /* If this is a hidden symbol, or if it is not version
5148 1, we append the version name to the symbol name.
5149 However, we do not modify a non-hidden absolute symbol
5150 if it is not a function, because it might be the version
5151 symbol itself. FIXME: What if it isn't? */
5152 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
5153 || (vernum > 1
5154 && (!bfd_is_abs_section (sec)
5155 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
5157 const char *verstr;
5158 size_t namelen, verlen, newlen;
5159 char *newname, *p;
5161 if (isym->st_shndx != SHN_UNDEF)
5163 if (vernum > elf_tdata (abfd)->cverdefs)
5164 verstr = NULL;
5165 else if (vernum > 1)
5166 verstr =
5167 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
5168 else
5169 verstr = "";
5171 if (verstr == NULL)
5173 _bfd_error_handler
5174 /* xgettext:c-format */
5175 (_("%pB: %s: invalid version %u (max %d)"),
5176 abfd, name, vernum,
5177 elf_tdata (abfd)->cverdefs);
5178 bfd_set_error (bfd_error_bad_value);
5179 goto error_free_vers;
5182 else
5184 /* We cannot simply test for the number of
5185 entries in the VERNEED section since the
5186 numbers for the needed versions do not start
5187 at 0. */
5188 Elf_Internal_Verneed *t;
5190 verstr = NULL;
5191 for (t = elf_tdata (abfd)->verref;
5192 t != NULL;
5193 t = t->vn_nextref)
5195 Elf_Internal_Vernaux *a;
5197 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5199 if (a->vna_other == vernum)
5201 verstr = a->vna_nodename;
5202 break;
5205 if (a != NULL)
5206 break;
5208 if (verstr == NULL)
5210 _bfd_error_handler
5211 /* xgettext:c-format */
5212 (_("%pB: %s: invalid needed version %d"),
5213 abfd, name, vernum);
5214 bfd_set_error (bfd_error_bad_value);
5215 goto error_free_vers;
5219 namelen = strlen (name);
5220 verlen = strlen (verstr);
5221 newlen = namelen + verlen + 2;
5222 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
5223 && isym->st_shndx != SHN_UNDEF)
5224 ++newlen;
5226 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
5227 if (newname == NULL)
5228 goto error_free_vers;
5229 memcpy (newname, name, namelen);
5230 p = newname + namelen;
5231 *p++ = ELF_VER_CHR;
5232 /* If this is a defined non-hidden version symbol,
5233 we add another @ to the name. This indicates the
5234 default version of the symbol. */
5235 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
5236 && isym->st_shndx != SHN_UNDEF)
5237 *p++ = ELF_VER_CHR;
5238 memcpy (p, verstr, verlen + 1);
5240 name = newname;
5241 /* Since bfd_hash_alloc is used for "name", the string
5242 must be copied if added to first_hash. The string
5243 memory can be freed when an --as-needed library is
5244 not needed. */
5245 must_copy_name = true;
5248 /* If this symbol has default visibility and the user has
5249 requested we not re-export it, then mark it as hidden. */
5250 if (!bfd_is_und_section (sec)
5251 && !dynamic
5252 && abfd->no_export
5253 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
5254 isym->st_other = (STV_HIDDEN
5255 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
5257 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
5258 sym_hash, &old_bfd, &old_weak,
5259 &old_alignment, &skip, &override,
5260 &type_change_ok, &size_change_ok,
5261 &matched))
5262 goto error_free_vers;
5264 if (skip)
5265 continue;
5267 h = *sym_hash;
5268 while (h->root.type == bfd_link_hash_indirect
5269 || h->root.type == bfd_link_hash_warning)
5270 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5272 /* Override a definition only if the new symbol matches the
5273 existing one. */
5274 if (override && matched)
5276 definition = false;
5277 if (htab->first_hash != NULL
5278 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5279 && h->root.non_ir_ref_regular)
5281 /* When reloading --as-needed shared objects for new
5282 symbols added from IR inputs, if this shared object
5283 has the first definition, use it. */
5284 struct elf_link_first_hash_entry *e
5285 = ((struct elf_link_first_hash_entry *)
5286 bfd_hash_lookup (htab->first_hash, name, false,
5287 false));
5288 if (e != NULL && e->abfd == abfd)
5289 definition = true;
5293 if (h->versioned != unversioned
5294 && elf_tdata (abfd)->verdef != NULL
5295 && vernum > 1
5296 && definition)
5297 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
5300 if (! (_bfd_generic_link_add_one_symbol
5301 (info, override ? override : abfd, name, flags, sec, value,
5302 NULL, false, bed->collect,
5303 (struct bfd_link_hash_entry **) sym_hash)))
5304 goto error_free_vers;
5306 h = *sym_hash;
5307 /* We need to make sure that indirect symbol dynamic flags are
5308 updated. */
5309 hi = h;
5310 while (h->root.type == bfd_link_hash_indirect
5311 || h->root.type == bfd_link_hash_warning)
5312 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5314 *sym_hash = h;
5316 /* Setting the index to -3 tells elf_link_output_extsym that
5317 this symbol is defined in a discarded section. */
5318 if (discarded && is_elf_hash_table (&htab->root))
5319 h->indx = -3;
5321 new_weak = (flags & BSF_WEAK) != 0;
5322 if (dynamic
5323 && definition
5324 && new_weak
5325 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
5326 && is_elf_hash_table (&htab->root)
5327 && h->u.alias == NULL)
5329 /* Keep a list of all weak defined non function symbols from
5330 a dynamic object, using the alias field. Later in this
5331 function we will set the alias field to the correct
5332 value. We only put non-function symbols from dynamic
5333 objects on this list, because that happens to be the only
5334 time we need to know the normal symbol corresponding to a
5335 weak symbol, and the information is time consuming to
5336 figure out. If the alias field is not already NULL,
5337 then this symbol was already defined by some previous
5338 dynamic object, and we will be using that previous
5339 definition anyhow. */
5341 h->u.alias = weaks;
5342 weaks = h;
5345 /* Set the alignment of a common symbol. */
5346 if ((common || bfd_is_com_section (sec))
5347 && h->root.type == bfd_link_hash_common)
5349 unsigned int align;
5351 if (common)
5352 align = bfd_log2 (isym->st_value);
5353 else
5355 /* The new symbol is a common symbol in a shared object.
5356 We need to get the alignment from the section. */
5357 align = new_sec->alignment_power;
5359 if (align > old_alignment)
5360 h->root.u.c.p->alignment_power = align;
5361 else
5362 h->root.u.c.p->alignment_power = old_alignment;
5365 if (is_elf_hash_table (&htab->root))
5367 /* Set a flag in the hash table entry indicating the type of
5368 reference or definition we just found. A dynamic symbol
5369 is one which is referenced or defined by both a regular
5370 object and a shared object. */
5371 bool dynsym = false;
5373 /* Plugin symbols aren't normal. Don't set def/ref flags. */
5374 if ((abfd->flags & BFD_PLUGIN) != 0)
5376 /* Except for this flag to track nonweak references. */
5377 if (!definition
5378 && bind != STB_WEAK)
5379 h->ref_ir_nonweak = 1;
5381 else if (!dynamic)
5383 if (! definition)
5385 h->ref_regular = 1;
5386 if (bind != STB_WEAK)
5387 h->ref_regular_nonweak = 1;
5389 else
5391 h->def_regular = 1;
5392 if (h->def_dynamic)
5394 h->def_dynamic = 0;
5395 h->ref_dynamic = 1;
5399 else
5401 if (! definition)
5403 h->ref_dynamic = 1;
5404 hi->ref_dynamic = 1;
5406 else
5408 h->def_dynamic = 1;
5409 hi->def_dynamic = 1;
5413 /* If an indirect symbol has been forced local, don't
5414 make the real symbol dynamic. */
5415 if (h != hi && hi->forced_local)
5417 else if (!dynamic)
5419 if (bfd_link_dll (info)
5420 || h->def_dynamic
5421 || h->ref_dynamic)
5422 dynsym = true;
5424 else
5426 if (h->def_regular
5427 || h->ref_regular
5428 || (h->is_weakalias
5429 && weakdef (h)->dynindx != -1))
5430 dynsym = true;
5433 /* Check to see if we need to add an indirect symbol for
5434 the default name. */
5435 if ((definition
5436 || (!override && h->root.type == bfd_link_hash_common))
5437 && !(hi != h
5438 && hi->versioned == versioned_hidden))
5439 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
5440 sec, value, &old_bfd, &dynsym))
5441 goto error_free_vers;
5443 /* Check the alignment when a common symbol is involved. This
5444 can change when a common symbol is overridden by a normal
5445 definition or a common symbol is ignored due to the old
5446 normal definition. We need to make sure the maximum
5447 alignment is maintained. */
5448 if ((old_alignment || common)
5449 && h->root.type != bfd_link_hash_common)
5451 unsigned int common_align;
5452 unsigned int normal_align;
5453 unsigned int symbol_align;
5454 bfd *normal_bfd;
5455 bfd *common_bfd;
5457 BFD_ASSERT (h->root.type == bfd_link_hash_defined
5458 || h->root.type == bfd_link_hash_defweak);
5460 symbol_align = ffs (h->root.u.def.value) - 1;
5461 if (h->root.u.def.section->owner != NULL
5462 && (h->root.u.def.section->owner->flags
5463 & (DYNAMIC | BFD_PLUGIN)) == 0)
5465 normal_align = h->root.u.def.section->alignment_power;
5466 if (normal_align > symbol_align)
5467 normal_align = symbol_align;
5469 else
5470 normal_align = symbol_align;
5472 if (old_alignment)
5474 common_align = old_alignment;
5475 common_bfd = old_bfd;
5476 normal_bfd = abfd;
5478 else
5480 common_align = bfd_log2 (isym->st_value);
5481 common_bfd = abfd;
5482 normal_bfd = old_bfd;
5485 if (normal_align < common_align)
5487 /* PR binutils/2735 */
5488 if (normal_bfd == NULL)
5489 _bfd_error_handler
5490 /* xgettext:c-format */
5491 (_("warning: alignment %u of common symbol `%s' in %pB is"
5492 " greater than the alignment (%u) of its section %pA"),
5493 1 << common_align, name, common_bfd,
5494 1 << normal_align, h->root.u.def.section);
5495 else
5496 _bfd_error_handler
5497 /* xgettext:c-format */
5498 (_("warning: alignment %u of normal symbol `%s' in %pB"
5499 " is smaller than %u used by the common definition in %pB"),
5500 1 << normal_align, name, normal_bfd,
5501 1 << common_align, common_bfd);
5503 /* PR 30499: make sure that users understand that this warning is serious. */
5504 _bfd_error_handler
5505 (_("warning: NOTE: alignment discrepancies can cause real problems. Investigation is advised."));
5509 /* Remember the symbol size if it isn't undefined. */
5510 if (isym->st_size != 0
5511 && isym->st_shndx != SHN_UNDEF
5512 && (definition || h->size == 0))
5514 if (h->size != 0
5515 && h->size != isym->st_size
5516 && ! size_change_ok)
5518 _bfd_error_handler
5519 /* xgettext:c-format */
5520 (_("warning: size of symbol `%s' changed"
5521 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
5522 name, (uint64_t) h->size, old_bfd,
5523 (uint64_t) isym->st_size, abfd);
5525 /* PR 30499: make sure that users understand that this warning is serious. */
5526 _bfd_error_handler
5527 (_("warning: NOTE: size discrepancies can cause real problems. Investigation is advised."));
5530 h->size = isym->st_size;
5533 /* If this is a common symbol, then we always want H->SIZE
5534 to be the size of the common symbol. The code just above
5535 won't fix the size if a common symbol becomes larger. We
5536 don't warn about a size change here, because that is
5537 covered by --warn-common. Allow changes between different
5538 function types. */
5539 if (h->root.type == bfd_link_hash_common)
5540 h->size = h->root.u.c.size;
5542 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
5543 && ((definition && !new_weak)
5544 || (old_weak && h->root.type == bfd_link_hash_common)
5545 || h->type == STT_NOTYPE))
5547 unsigned int type = ELF_ST_TYPE (isym->st_info);
5549 /* Turn an IFUNC symbol from a DSO into a normal FUNC
5550 symbol. */
5551 if (type == STT_GNU_IFUNC
5552 && (abfd->flags & DYNAMIC) != 0)
5553 type = STT_FUNC;
5555 if (h->type != type)
5557 if (h->type != STT_NOTYPE && ! type_change_ok)
5558 /* xgettext:c-format */
5559 _bfd_error_handler
5560 (_("warning: type of symbol `%s' changed"
5561 " from %d to %d in %pB"),
5562 name, h->type, type, abfd);
5564 h->type = type;
5568 /* Merge st_other field. */
5569 elf_merge_st_other (abfd, h, isym->st_other, sec,
5570 definition, dynamic);
5572 /* We don't want to make debug symbol dynamic. */
5573 if (definition
5574 && (sec->flags & SEC_DEBUGGING)
5575 && !bfd_link_relocatable (info))
5576 dynsym = false;
5578 /* Nor should we make plugin symbols dynamic. */
5579 if ((abfd->flags & BFD_PLUGIN) != 0)
5580 dynsym = false;
5582 if (definition)
5584 h->target_internal = isym->st_target_internal;
5585 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5588 /* Don't add indirect symbols for .symver x, x@FOO aliases
5589 in IR. Since all data or text symbols in IR have the
5590 same type, value and section, we can't tell if a symbol
5591 is an alias of another symbol by their types, values and
5592 sections. */
5593 if (definition
5594 && !dynamic
5595 && (abfd->flags & BFD_PLUGIN) == 0)
5597 char *p = strchr (name, ELF_VER_CHR);
5598 if (p != NULL && p[1] != ELF_VER_CHR)
5600 /* Queue non-default versions so that .symver x, x@FOO
5601 aliases can be checked. */
5602 if (!nondeflt_vers)
5604 size_t amt = ((isymend - isym + 1)
5605 * sizeof (struct elf_link_hash_entry *));
5606 nondeflt_vers
5607 = (struct elf_link_hash_entry **) bfd_malloc (amt);
5608 if (!nondeflt_vers)
5609 goto error_free_vers;
5611 nondeflt_vers[nondeflt_vers_cnt++] = h;
5615 if (dynsym && h->dynindx == -1)
5617 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5618 goto error_free_vers;
5619 if (h->is_weakalias
5620 && weakdef (h)->dynindx == -1)
5622 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
5623 goto error_free_vers;
5626 else if (h->dynindx != -1)
5627 /* If the symbol already has a dynamic index, but
5628 visibility says it should not be visible, turn it into
5629 a local symbol. */
5630 switch (ELF_ST_VISIBILITY (h->other))
5632 case STV_INTERNAL:
5633 case STV_HIDDEN:
5634 (*bed->elf_backend_hide_symbol) (info, h, true);
5635 dynsym = false;
5636 break;
5639 if (!add_needed
5640 && matched
5641 && definition
5642 && h->root.type != bfd_link_hash_indirect)
5644 if ((dynsym
5645 && h->ref_regular_nonweak)
5646 || (old_bfd != NULL
5647 && (old_bfd->flags & BFD_PLUGIN) != 0
5648 && h->ref_ir_nonweak
5649 && !info->lto_all_symbols_read)
5650 || (h->ref_dynamic_nonweak
5651 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5652 && !on_needed_list (elf_dt_name (abfd),
5653 htab->needed, NULL)))
5655 const char *soname = elf_dt_name (abfd);
5657 info->callbacks->minfo ("%!", soname, old_bfd,
5658 h->root.root.string);
5660 /* A symbol from a library loaded via DT_NEEDED of some
5661 other library is referenced by a regular object.
5662 Add a DT_NEEDED entry for it. Issue an error if
5663 --no-add-needed is used and the reference was not
5664 a weak one. */
5665 if (old_bfd != NULL
5666 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
5668 _bfd_error_handler
5669 /* xgettext:c-format */
5670 (_("%pB: undefined reference to symbol '%s'"),
5671 old_bfd, name);
5672 bfd_set_error (bfd_error_missing_dso);
5673 goto error_free_vers;
5676 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
5677 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
5679 /* Create dynamic sections for backends that require
5680 that be done before setup_gnu_properties. */
5681 if (!_bfd_elf_link_create_dynamic_sections (abfd, info))
5682 return false;
5683 add_needed = true;
5685 else if (dynamic
5686 && h->root.u.def.section->owner == abfd)
5687 /* Add this symbol to first hash if this shared
5688 object has the first definition. */
5689 elf_link_add_to_first_hash (abfd, info, name, must_copy_name);
5694 if (info->lto_plugin_active
5695 && !bfd_link_relocatable (info)
5696 && (abfd->flags & BFD_PLUGIN) == 0
5697 && !just_syms
5698 && extsymcount != 0
5699 && is_elf_hash_table (&htab->root))
5701 int r_sym_shift;
5703 if (bed->s->arch_size == 32)
5704 r_sym_shift = 8;
5705 else
5706 r_sym_shift = 32;
5708 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5709 referenced in regular objects so that linker plugin will get
5710 the correct symbol resolution. */
5712 sym_hash = elf_sym_hashes (abfd);
5713 for (s = abfd->sections; s != NULL; s = s->next)
5715 Elf_Internal_Rela *internal_relocs;
5716 Elf_Internal_Rela *rel, *relend;
5718 /* Don't check relocations in excluded sections. */
5719 if ((s->flags & SEC_RELOC) == 0
5720 || s->reloc_count == 0
5721 || (s->flags & SEC_EXCLUDE) != 0
5722 || (s->flags & SEC_DEBUGGING) != 0)
5723 continue;
5725 internal_relocs = _bfd_elf_link_info_read_relocs
5726 (abfd, info, s, NULL, NULL,
5727 _bfd_elf_link_keep_memory (info));
5728 if (internal_relocs == NULL)
5729 goto error_free_vers;
5731 rel = internal_relocs;
5732 relend = rel + s->reloc_count;
5733 for ( ; rel < relend; rel++)
5735 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5736 struct elf_link_hash_entry *h;
5738 /* Skip local symbols. */
5739 if (r_symndx < extsymoff)
5740 continue;
5742 h = sym_hash[r_symndx - extsymoff];
5743 if (h != NULL)
5744 h->root.non_ir_ref_regular = 1;
5747 if (elf_section_data (s)->relocs != internal_relocs)
5748 free (internal_relocs);
5752 free (extversym);
5753 extversym = NULL;
5754 free (isymbuf);
5755 isymbuf = NULL;
5757 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5759 unsigned int i;
5761 /* Restore the symbol table. */
5762 old_ent = (char *) old_tab + tabsize;
5763 memset (elf_sym_hashes (abfd), 0,
5764 extsymcount * sizeof (struct elf_link_hash_entry *));
5765 htab->root.table.table = old_table;
5766 htab->root.table.size = old_size;
5767 htab->root.table.count = old_count;
5768 memcpy (htab->root.table.table, old_tab, tabsize);
5769 htab->root.undefs = old_undefs;
5770 htab->root.undefs_tail = old_undefs_tail;
5771 if (htab->dynstr != NULL)
5772 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5773 free (old_strtab);
5774 old_strtab = NULL;
5775 for (i = 0; i < htab->root.table.size; i++)
5777 struct bfd_hash_entry *p;
5778 struct elf_link_hash_entry *h;
5779 unsigned int non_ir_ref_dynamic;
5781 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5783 /* Preserve non_ir_ref_dynamic so that this symbol
5784 will be exported when the dynamic lib becomes needed
5785 in the second pass. */
5786 h = (struct elf_link_hash_entry *) p;
5787 if (h->root.type == bfd_link_hash_warning)
5788 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5789 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
5791 h = (struct elf_link_hash_entry *) p;
5792 memcpy (h, old_ent, htab->root.table.entsize);
5793 old_ent = (char *) old_ent + htab->root.table.entsize;
5794 if (h->root.type == bfd_link_hash_warning)
5796 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5797 memcpy (h, old_ent, htab->root.table.entsize);
5798 old_ent = (char *) old_ent + htab->root.table.entsize;
5800 if (h->root.type == bfd_link_hash_common)
5802 memcpy (h->root.u.c.p, old_ent, sizeof (*h->root.u.c.p));
5803 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
5805 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
5809 /* Make a special call to the linker "notice" function to
5810 tell it that symbols added for crefs may need to be removed. */
5811 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
5812 goto error_free_vers;
5814 free (old_tab);
5815 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5816 alloc_mark);
5817 free (nondeflt_vers);
5818 return true;
5821 if (old_tab != NULL)
5823 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
5824 goto error_free_vers;
5825 free (old_tab);
5826 old_tab = NULL;
5829 /* Now that all the symbols from this input file are created, if
5830 not performing a relocatable link, handle .symver foo, foo@BAR
5831 such that any relocs against foo become foo@BAR. */
5832 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
5834 size_t cnt, symidx;
5836 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5838 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5839 char *shortname, *p;
5840 size_t amt;
5842 p = strchr (h->root.root.string, ELF_VER_CHR);
5843 if (p == NULL
5844 || (h->root.type != bfd_link_hash_defined
5845 && h->root.type != bfd_link_hash_defweak))
5846 continue;
5848 amt = p - h->root.root.string;
5849 shortname = (char *) bfd_malloc (amt + 1);
5850 if (!shortname)
5851 goto error_free_vers;
5852 memcpy (shortname, h->root.root.string, amt);
5853 shortname[amt] = '\0';
5855 hi = (struct elf_link_hash_entry *)
5856 bfd_link_hash_lookup (&htab->root, shortname,
5857 false, false, false);
5858 if (hi != NULL
5859 && hi->root.type == h->root.type
5860 && hi->root.u.def.value == h->root.u.def.value
5861 && hi->root.u.def.section == h->root.u.def.section)
5863 (*bed->elf_backend_hide_symbol) (info, hi, true);
5864 hi->root.type = bfd_link_hash_indirect;
5865 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
5866 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
5867 sym_hash = elf_sym_hashes (abfd);
5868 if (sym_hash)
5869 for (symidx = 0; symidx < extsymcount; ++symidx)
5870 if (sym_hash[symidx] == hi)
5872 sym_hash[symidx] = h;
5873 break;
5876 free (shortname);
5878 free (nondeflt_vers);
5879 nondeflt_vers = NULL;
5882 /* Now set the alias field correctly for all the weak defined
5883 symbols we found. The only way to do this is to search all the
5884 symbols. Since we only need the information for non functions in
5885 dynamic objects, that's the only time we actually put anything on
5886 the list WEAKS. We need this information so that if a regular
5887 object refers to a symbol defined weakly in a dynamic object, the
5888 real symbol in the dynamic object is also put in the dynamic
5889 symbols; we also must arrange for both symbols to point to the
5890 same memory location. We could handle the general case of symbol
5891 aliasing, but a general symbol alias can only be generated in
5892 assembler code, handling it correctly would be very time
5893 consuming, and other ELF linkers don't handle general aliasing
5894 either. */
5895 if (weaks != NULL)
5897 struct elf_link_hash_entry **hpp;
5898 struct elf_link_hash_entry **hppend;
5899 struct elf_link_hash_entry **sorted_sym_hash;
5900 struct elf_link_hash_entry *h;
5901 size_t sym_count, amt;
5903 /* Since we have to search the whole symbol list for each weak
5904 defined symbol, search time for N weak defined symbols will be
5905 O(N^2). Binary search will cut it down to O(NlogN). */
5906 amt = extsymcount * sizeof (*sorted_sym_hash);
5907 sorted_sym_hash = bfd_malloc (amt);
5908 if (sorted_sym_hash == NULL)
5909 goto error_return;
5910 sym_hash = sorted_sym_hash;
5911 hpp = elf_sym_hashes (abfd);
5912 hppend = hpp + extsymcount;
5913 sym_count = 0;
5914 for (; hpp < hppend; hpp++)
5916 h = *hpp;
5917 if (h != NULL
5918 && h->root.type == bfd_link_hash_defined
5919 && !bed->is_function_type (h->type))
5921 *sym_hash = h;
5922 sym_hash++;
5923 sym_count++;
5927 qsort (sorted_sym_hash, sym_count, sizeof (*sorted_sym_hash),
5928 elf_sort_symbol);
5930 while (weaks != NULL)
5932 struct elf_link_hash_entry *hlook;
5933 asection *slook;
5934 bfd_vma vlook;
5935 size_t i, j, idx = 0;
5937 hlook = weaks;
5938 weaks = hlook->u.alias;
5939 hlook->u.alias = NULL;
5941 if (hlook->root.type != bfd_link_hash_defined
5942 && hlook->root.type != bfd_link_hash_defweak)
5943 continue;
5945 slook = hlook->root.u.def.section;
5946 vlook = hlook->root.u.def.value;
5948 i = 0;
5949 j = sym_count;
5950 while (i != j)
5952 bfd_signed_vma vdiff;
5953 idx = (i + j) / 2;
5954 h = sorted_sym_hash[idx];
5955 vdiff = vlook - h->root.u.def.value;
5956 if (vdiff < 0)
5957 j = idx;
5958 else if (vdiff > 0)
5959 i = idx + 1;
5960 else
5962 int sdiff = slook->id - h->root.u.def.section->id;
5963 if (sdiff < 0)
5964 j = idx;
5965 else if (sdiff > 0)
5966 i = idx + 1;
5967 else
5968 break;
5972 /* We didn't find a value/section match. */
5973 if (i == j)
5974 continue;
5976 /* With multiple aliases, or when the weak symbol is already
5977 strongly defined, we have multiple matching symbols and
5978 the binary search above may land on any of them. Step
5979 one past the matching symbol(s). */
5980 while (++idx != j)
5982 h = sorted_sym_hash[idx];
5983 if (h->root.u.def.section != slook
5984 || h->root.u.def.value != vlook)
5985 break;
5988 /* Now look back over the aliases. Since we sorted by size
5989 as well as value and section, we'll choose the one with
5990 the largest size. */
5991 while (idx-- != i)
5993 h = sorted_sym_hash[idx];
5995 /* Stop if value or section doesn't match. */
5996 if (h->root.u.def.section != slook
5997 || h->root.u.def.value != vlook)
5998 break;
5999 else if (h != hlook)
6001 struct elf_link_hash_entry *t;
6003 hlook->u.alias = h;
6004 hlook->is_weakalias = 1;
6005 t = h;
6006 if (t->u.alias != NULL)
6007 while (t->u.alias != h)
6008 t = t->u.alias;
6009 t->u.alias = hlook;
6011 /* If the weak definition is in the list of dynamic
6012 symbols, make sure the real definition is put
6013 there as well. */
6014 if (hlook->dynindx != -1 && h->dynindx == -1)
6016 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6018 err_free_sym_hash:
6019 free (sorted_sym_hash);
6020 goto error_return;
6024 /* If the real definition is in the list of dynamic
6025 symbols, make sure the weak definition is put
6026 there as well. If we don't do this, then the
6027 dynamic loader might not merge the entries for the
6028 real definition and the weak definition. */
6029 if (h->dynindx != -1 && hlook->dynindx == -1)
6031 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
6032 goto err_free_sym_hash;
6034 break;
6039 free (sorted_sym_hash);
6042 if (bed->check_directives
6043 && !(*bed->check_directives) (abfd, info))
6044 return false;
6046 /* If this is a non-traditional link, try to optimize the handling
6047 of the .stab/.stabstr sections. */
6048 if (! dynamic
6049 && ! info->traditional_format
6050 && is_elf_hash_table (&htab->root)
6051 && (info->strip != strip_all && info->strip != strip_debugger))
6053 asection *stabstr;
6055 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
6056 if (stabstr != NULL)
6058 bfd_size_type string_offset = 0;
6059 asection *stab;
6061 for (stab = abfd->sections; stab; stab = stab->next)
6062 if (startswith (stab->name, ".stab")
6063 && (!stab->name[5] ||
6064 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
6065 && (stab->flags & SEC_MERGE) == 0
6066 && !bfd_is_abs_section (stab->output_section))
6068 struct bfd_elf_section_data *secdata;
6070 secdata = elf_section_data (stab);
6071 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
6072 stabstr, &secdata->sec_info,
6073 &string_offset))
6074 goto error_return;
6075 if (secdata->sec_info)
6076 stab->sec_info_type = SEC_INFO_TYPE_STABS;
6081 if (dynamic && add_needed)
6083 /* Add this bfd to the loaded list. */
6084 struct elf_link_loaded_list *n;
6086 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
6087 if (n == NULL)
6088 goto error_return;
6089 n->abfd = abfd;
6090 n->next = htab->dyn_loaded;
6091 htab->dyn_loaded = n;
6093 if (dynamic && !add_needed
6094 && (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) != 0)
6095 elf_dyn_lib_class (abfd) |= DYN_NO_NEEDED;
6097 return true;
6099 error_free_vers:
6100 free (old_tab);
6101 free (old_strtab);
6102 free (nondeflt_vers);
6103 free (extversym);
6104 error_free_sym:
6105 free (isymbuf);
6106 error_return:
6107 return false;
6110 /* Return the linker hash table entry of a symbol that might be
6111 satisfied by an archive symbol. Return -1 on error. */
6113 struct bfd_link_hash_entry *
6114 _bfd_elf_archive_symbol_lookup (bfd *abfd,
6115 struct bfd_link_info *info,
6116 const char *name)
6118 struct bfd_link_hash_entry *h;
6119 char *p, *copy;
6120 size_t len, first;
6122 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
6123 if (h != NULL)
6124 return h;
6126 /* If this is a default version (the name contains @@), look up the
6127 symbol again with only one `@' as well as without the version.
6128 The effect is that references to the symbol with and without the
6129 version will be matched by the default symbol in the archive. */
6131 p = strchr (name, ELF_VER_CHR);
6132 if (p == NULL || p[1] != ELF_VER_CHR)
6134 /* Add this symbol to first hash if this archive has the first
6135 definition. */
6136 if (is_elf_hash_table (info->hash))
6137 elf_link_add_to_first_hash (abfd, info, name, false);
6138 return h;
6141 /* First check with only one `@'. */
6142 len = strlen (name);
6143 copy = (char *) bfd_alloc (abfd, len);
6144 if (copy == NULL)
6145 return (struct bfd_link_hash_entry *) -1;
6147 first = p - name + 1;
6148 memcpy (copy, name, first);
6149 memcpy (copy + first, name + first + 1, len - first);
6151 h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
6152 if (h == NULL)
6154 /* We also need to check references to the symbol without the
6155 version. */
6156 copy[first - 1] = '\0';
6157 h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
6160 bfd_release (abfd, copy);
6161 return h;
6164 /* Add symbols from an ELF archive file to the linker hash table. We
6165 don't use _bfd_generic_link_add_archive_symbols because we need to
6166 handle versioned symbols.
6168 Fortunately, ELF archive handling is simpler than that done by
6169 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
6170 oddities. In ELF, if we find a symbol in the archive map, and the
6171 symbol is currently undefined, we know that we must pull in that
6172 object file.
6174 Unfortunately, we do have to make multiple passes over the symbol
6175 table until nothing further is resolved. */
6177 static bool
6178 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
6180 symindex c;
6181 unsigned char *included = NULL;
6182 carsym *symdefs;
6183 bool loop;
6184 size_t amt;
6185 const struct elf_backend_data *bed;
6186 struct bfd_link_hash_entry * (*archive_symbol_lookup)
6187 (bfd *, struct bfd_link_info *, const char *);
6189 if (! bfd_has_map (abfd))
6191 /* An empty archive is a special case. */
6192 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
6193 return true;
6194 bfd_set_error (bfd_error_no_armap);
6195 return false;
6198 /* Keep track of all symbols we know to be already defined, and all
6199 files we know to be already included. This is to speed up the
6200 second and subsequent passes. */
6201 c = bfd_ardata (abfd)->symdef_count;
6202 if (c == 0)
6203 return true;
6204 amt = c * sizeof (*included);
6205 included = (unsigned char *) bfd_zmalloc (amt);
6206 if (included == NULL)
6207 return false;
6209 symdefs = bfd_ardata (abfd)->symdefs;
6210 bed = get_elf_backend_data (abfd);
6211 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
6215 file_ptr last;
6216 symindex i;
6217 carsym *symdef;
6218 carsym *symdefend;
6220 loop = false;
6221 last = -1;
6223 symdef = symdefs;
6224 symdefend = symdef + c;
6225 for (i = 0; symdef < symdefend; symdef++, i++)
6227 struct bfd_link_hash_entry *h;
6228 bfd *element;
6229 struct bfd_link_hash_entry *undefs_tail;
6230 symindex mark;
6232 if (included[i])
6233 continue;
6234 if (symdef->file_offset == last)
6236 included[i] = true;
6237 continue;
6240 h = archive_symbol_lookup (abfd, info, symdef->name);
6241 if (h == (struct bfd_link_hash_entry *) -1)
6242 goto error_return;
6244 if (h == NULL)
6245 continue;
6247 if (h->type == bfd_link_hash_undefined)
6249 /* If the archive element has already been loaded then one
6250 of the symbols defined by that element might have been
6251 made undefined due to being in a discarded section. */
6252 if (is_elf_hash_table (info->hash)
6253 && ((struct elf_link_hash_entry *) h)->indx == -3)
6254 continue;
6256 else if (h->type == bfd_link_hash_common)
6258 /* We currently have a common symbol. The archive map contains
6259 a reference to this symbol, so we may want to include it. We
6260 only want to include it however, if this archive element
6261 contains a definition of the symbol, not just another common
6262 declaration of it.
6264 Unfortunately some archivers (including GNU ar) will put
6265 declarations of common symbols into their archive maps, as
6266 well as real definitions, so we cannot just go by the archive
6267 map alone. Instead we must read in the element's symbol
6268 table and check that to see what kind of symbol definition
6269 this is. */
6270 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
6271 continue;
6273 else
6275 if (h->type != bfd_link_hash_undefweak)
6276 /* Symbol must be defined. Don't check it again. */
6277 included[i] = true;
6279 if (!is_elf_hash_table (info->hash))
6280 continue;
6281 struct elf_link_hash_entry *eh
6282 = (struct elf_link_hash_entry *) h;
6283 /* Ignore the archive if the symbol isn't referenced by a
6284 regular object or isn't defined in a shared object. */
6285 if (!eh->ref_regular || !eh->def_dynamic)
6286 continue;
6287 /* Ignore the dynamic definition if symbol is first
6288 defined in this archive. */
6289 struct elf_link_hash_table *htab = elf_hash_table (info);
6290 if (htab->first_hash == NULL)
6291 continue;
6292 struct elf_link_first_hash_entry *e
6293 = ((struct elf_link_first_hash_entry *)
6294 bfd_hash_lookup (htab->first_hash, symdef->name,
6295 false, false));
6296 if (e == NULL || e->abfd != abfd)
6297 continue;
6300 /* We need to include this archive member. */
6301 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset,
6302 info);
6303 if (element == NULL)
6304 goto error_return;
6306 if (! bfd_check_format (element, bfd_object))
6307 goto error_return;
6309 undefs_tail = info->hash->undefs_tail;
6311 if (!(*info->callbacks
6312 ->add_archive_element) (info, element, symdef->name, &element))
6313 continue;
6314 if (!bfd_link_add_symbols (element, info))
6315 goto error_return;
6317 /* If there are any new undefined symbols, we need to make
6318 another pass through the archive in order to see whether
6319 they can be defined. FIXME: This isn't perfect, because
6320 common symbols wind up on undefs_tail and because an
6321 undefined symbol which is defined later on in this pass
6322 does not require another pass. This isn't a bug, but it
6323 does make the code less efficient than it could be. */
6324 if (undefs_tail != info->hash->undefs_tail)
6325 loop = true;
6327 /* Look backward to mark all symbols from this object file
6328 which we have already seen in this pass. */
6329 mark = i;
6332 included[mark] = true;
6333 if (mark == 0)
6334 break;
6335 --mark;
6337 while (symdefs[mark].file_offset == symdef->file_offset);
6339 /* We mark subsequent symbols from this object file as we go
6340 on through the loop. */
6341 last = symdef->file_offset;
6344 while (loop);
6346 free (included);
6347 return true;
6349 error_return:
6350 free (included);
6351 return false;
6354 /* Given an ELF BFD, add symbols to the global hash table as
6355 appropriate. */
6357 bool
6358 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
6360 switch (bfd_get_format (abfd))
6362 case bfd_object:
6363 return elf_link_add_object_symbols (abfd, info);
6364 case bfd_archive:
6365 return elf_link_add_archive_symbols (abfd, info);
6366 default:
6367 bfd_set_error (bfd_error_wrong_format);
6368 return false;
6372 struct hash_codes_info
6374 unsigned long *hashcodes;
6375 bool error;
6378 /* This function will be called though elf_link_hash_traverse to store
6379 all hash value of the exported symbols in an array. */
6381 static bool
6382 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
6384 struct hash_codes_info *inf = (struct hash_codes_info *) data;
6385 const char *name;
6386 unsigned long ha;
6387 char *alc = NULL;
6389 /* Ignore indirect symbols. These are added by the versioning code. */
6390 if (h->dynindx == -1)
6391 return true;
6393 name = h->root.root.string;
6394 if (h->versioned >= versioned)
6396 char *p = strchr (name, ELF_VER_CHR);
6397 if (p != NULL)
6399 alc = (char *) bfd_malloc (p - name + 1);
6400 if (alc == NULL)
6402 inf->error = true;
6403 return false;
6405 memcpy (alc, name, p - name);
6406 alc[p - name] = '\0';
6407 name = alc;
6411 /* Compute the hash value. */
6412 ha = bfd_elf_hash (name);
6414 /* Store the found hash value in the array given as the argument. */
6415 *(inf->hashcodes)++ = ha;
6417 /* And store it in the struct so that we can put it in the hash table
6418 later. */
6419 h->u.elf_hash_value = ha;
6421 free (alc);
6422 return true;
6425 struct collect_gnu_hash_codes
6427 bfd *output_bfd;
6428 const struct elf_backend_data *bed;
6429 unsigned long int nsyms;
6430 unsigned long int maskbits;
6431 unsigned long int *hashcodes;
6432 unsigned long int *hashval;
6433 unsigned long int *indx;
6434 unsigned long int *counts;
6435 bfd_vma *bitmask;
6436 bfd_byte *contents;
6437 bfd_size_type xlat;
6438 long int min_dynindx;
6439 unsigned long int bucketcount;
6440 unsigned long int symindx;
6441 long int local_indx;
6442 long int shift1, shift2;
6443 unsigned long int mask;
6444 bool error;
6447 /* This function will be called though elf_link_hash_traverse to store
6448 all hash value of the exported symbols in an array. */
6450 static bool
6451 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
6453 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6454 const char *name;
6455 unsigned long ha;
6456 char *alc = NULL;
6458 /* Ignore indirect symbols. These are added by the versioning code. */
6459 if (h->dynindx == -1)
6460 return true;
6462 /* Ignore also local symbols and undefined symbols. */
6463 if (! (*s->bed->elf_hash_symbol) (h))
6464 return true;
6466 name = h->root.root.string;
6467 if (h->versioned >= versioned)
6469 char *p = strchr (name, ELF_VER_CHR);
6470 if (p != NULL)
6472 alc = (char *) bfd_malloc (p - name + 1);
6473 if (alc == NULL)
6475 s->error = true;
6476 return false;
6478 memcpy (alc, name, p - name);
6479 alc[p - name] = '\0';
6480 name = alc;
6484 /* Compute the hash value. */
6485 ha = bfd_elf_gnu_hash (name);
6487 /* Store the found hash value in the array for compute_bucket_count,
6488 and also for .dynsym reordering purposes. */
6489 s->hashcodes[s->nsyms] = ha;
6490 s->hashval[h->dynindx] = ha;
6491 ++s->nsyms;
6492 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
6493 s->min_dynindx = h->dynindx;
6495 free (alc);
6496 return true;
6499 /* This function will be called though elf_link_hash_traverse to do
6500 final dynamic symbol renumbering in case of .gnu.hash.
6501 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6502 to the translation table. */
6504 static bool
6505 elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data)
6507 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6508 unsigned long int bucket;
6509 unsigned long int val;
6511 /* Ignore indirect symbols. */
6512 if (h->dynindx == -1)
6513 return true;
6515 /* Ignore also local symbols and undefined symbols. */
6516 if (! (*s->bed->elf_hash_symbol) (h))
6518 if (h->dynindx >= s->min_dynindx)
6520 if (s->bed->record_xhash_symbol != NULL)
6522 (*s->bed->record_xhash_symbol) (h, 0);
6523 s->local_indx++;
6525 else
6526 h->dynindx = s->local_indx++;
6528 return true;
6531 bucket = s->hashval[h->dynindx] % s->bucketcount;
6532 val = (s->hashval[h->dynindx] >> s->shift1)
6533 & ((s->maskbits >> s->shift1) - 1);
6534 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
6535 s->bitmask[val]
6536 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
6537 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
6538 if (s->counts[bucket] == 1)
6539 /* Last element terminates the chain. */
6540 val |= 1;
6541 bfd_put_32 (s->output_bfd, val,
6542 s->contents + (s->indx[bucket] - s->symindx) * 4);
6543 --s->counts[bucket];
6544 if (s->bed->record_xhash_symbol != NULL)
6546 bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4;
6548 (*s->bed->record_xhash_symbol) (h, xlat_loc);
6550 else
6551 h->dynindx = s->indx[bucket]++;
6552 return true;
6555 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6557 bool
6558 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
6560 return !(h->forced_local
6561 || h->root.type == bfd_link_hash_undefined
6562 || h->root.type == bfd_link_hash_undefweak
6563 || ((h->root.type == bfd_link_hash_defined
6564 || h->root.type == bfd_link_hash_defweak)
6565 && h->root.u.def.section->output_section == NULL));
6568 /* Array used to determine the number of hash table buckets to use
6569 based on the number of symbols there are. If there are fewer than
6570 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6571 fewer than 37 we use 17 buckets, and so forth. We never use more
6572 than 32771 buckets. */
6574 static const size_t elf_buckets[] =
6576 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6577 16411, 32771, 0
6580 /* Compute bucket count for hashing table. We do not use a static set
6581 of possible tables sizes anymore. Instead we determine for all
6582 possible reasonable sizes of the table the outcome (i.e., the
6583 number of collisions etc) and choose the best solution. The
6584 weighting functions are not too simple to allow the table to grow
6585 without bounds. Instead one of the weighting factors is the size.
6586 Therefore the result is always a good payoff between few collisions
6587 (= short chain lengths) and table size. */
6588 static size_t
6589 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
6590 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
6591 unsigned long int nsyms,
6592 int gnu_hash)
6594 size_t best_size = 0;
6595 unsigned long int i;
6597 if (info->optimize)
6599 size_t minsize;
6600 size_t maxsize;
6601 uint64_t best_chlen = ~((uint64_t) 0);
6602 bfd *dynobj = elf_hash_table (info)->dynobj;
6603 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
6604 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
6605 unsigned long int *counts;
6606 bfd_size_type amt;
6607 unsigned int no_improvement_count = 0;
6609 /* Possible optimization parameters: if we have NSYMS symbols we say
6610 that the hashing table must at least have NSYMS/4 and at most
6611 2*NSYMS buckets. */
6612 minsize = nsyms / 4;
6613 if (minsize == 0)
6614 minsize = 1;
6615 best_size = maxsize = nsyms * 2;
6616 if (gnu_hash)
6618 if (minsize < 2)
6619 minsize = 2;
6620 if ((best_size & 31) == 0)
6621 ++best_size;
6624 /* Create array where we count the collisions in. We must use bfd_malloc
6625 since the size could be large. */
6626 amt = maxsize;
6627 amt *= sizeof (unsigned long int);
6628 counts = (unsigned long int *) bfd_malloc (amt);
6629 if (counts == NULL)
6630 return 0;
6632 /* Compute the "optimal" size for the hash table. The criteria is a
6633 minimal chain length. The minor criteria is (of course) the size
6634 of the table. */
6635 for (i = minsize; i < maxsize; ++i)
6637 /* Walk through the array of hashcodes and count the collisions. */
6638 uint64_t max;
6639 unsigned long int j;
6640 unsigned long int fact;
6642 if (gnu_hash && (i & 31) == 0)
6643 continue;
6645 memset (counts, '\0', i * sizeof (unsigned long int));
6647 /* Determine how often each hash bucket is used. */
6648 for (j = 0; j < nsyms; ++j)
6649 ++counts[hashcodes[j] % i];
6651 /* For the weight function we need some information about the
6652 pagesize on the target. This is information need not be 100%
6653 accurate. Since this information is not available (so far) we
6654 define it here to a reasonable default value. If it is crucial
6655 to have a better value some day simply define this value. */
6656 # ifndef BFD_TARGET_PAGESIZE
6657 # define BFD_TARGET_PAGESIZE (4096)
6658 # endif
6660 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6661 and the chains. */
6662 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
6664 # if 1
6665 /* Variant 1: optimize for short chains. We add the squares
6666 of all the chain lengths (which favors many small chain
6667 over a few long chains). */
6668 for (j = 0; j < i; ++j)
6669 max += counts[j] * counts[j];
6671 /* This adds penalties for the overall size of the table. */
6672 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6673 max *= fact * fact;
6674 # else
6675 /* Variant 2: Optimize a lot more for small table. Here we
6676 also add squares of the size but we also add penalties for
6677 empty slots (the +1 term). */
6678 for (j = 0; j < i; ++j)
6679 max += (1 + counts[j]) * (1 + counts[j]);
6681 /* The overall size of the table is considered, but not as
6682 strong as in variant 1, where it is squared. */
6683 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6684 max *= fact;
6685 # endif
6687 /* Compare with current best results. */
6688 if (max < best_chlen)
6690 best_chlen = max;
6691 best_size = i;
6692 no_improvement_count = 0;
6694 /* PR 11843: Avoid futile long searches for the best bucket size
6695 when there are a large number of symbols. */
6696 else if (++no_improvement_count == 100)
6697 break;
6700 free (counts);
6702 else
6704 for (i = 0; elf_buckets[i] != 0; i++)
6706 best_size = elf_buckets[i];
6707 if (nsyms < elf_buckets[i + 1])
6708 break;
6710 if (gnu_hash && best_size < 2)
6711 best_size = 2;
6714 return best_size;
6717 /* Size any SHT_GROUP section for ld -r. */
6719 bool
6720 _bfd_elf_size_group_sections (struct bfd_link_info *info)
6722 bfd *ibfd;
6723 asection *s;
6725 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6726 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6727 && (s = ibfd->sections) != NULL
6728 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
6729 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6730 return false;
6731 return true;
6734 /* Set a default stack segment size. The value in INFO wins. If it
6735 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6736 undefined it is initialized. */
6738 bool
6739 bfd_elf_stack_segment_size (bfd *output_bfd,
6740 struct bfd_link_info *info,
6741 const char *legacy_symbol,
6742 bfd_vma default_size)
6744 struct elf_link_hash_entry *h = NULL;
6746 /* Look for legacy symbol. */
6747 if (legacy_symbol)
6748 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6749 false, false, false);
6750 if (h && (h->root.type == bfd_link_hash_defined
6751 || h->root.type == bfd_link_hash_defweak)
6752 && h->def_regular
6753 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6755 /* The symbol has no type if specified on the command line. */
6756 h->type = STT_OBJECT;
6757 if (info->stacksize)
6758 /* xgettext:c-format */
6759 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6760 output_bfd, legacy_symbol);
6761 else if (h->root.u.def.section != bfd_abs_section_ptr)
6762 /* xgettext:c-format */
6763 _bfd_error_handler (_("%pB: %s not absolute"),
6764 output_bfd, legacy_symbol);
6765 else
6766 info->stacksize = h->root.u.def.value;
6769 if (!info->stacksize)
6770 /* If the user didn't set a size, or explicitly inhibit the
6771 size, set it now. */
6772 info->stacksize = default_size;
6774 /* Provide the legacy symbol, if it is referenced. */
6775 if (h && (h->root.type == bfd_link_hash_undefined
6776 || h->root.type == bfd_link_hash_undefweak))
6778 struct bfd_link_hash_entry *bh = NULL;
6780 if (!(_bfd_generic_link_add_one_symbol
6781 (info, output_bfd, legacy_symbol,
6782 BSF_GLOBAL, bfd_abs_section_ptr,
6783 info->stacksize >= 0 ? info->stacksize : 0,
6784 NULL, false, get_elf_backend_data (output_bfd)->collect, &bh)))
6785 return false;
6787 h = (struct elf_link_hash_entry *) bh;
6788 h->def_regular = 1;
6789 h->type = STT_OBJECT;
6792 return true;
6795 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6797 struct elf_gc_sweep_symbol_info
6799 struct bfd_link_info *info;
6800 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6801 bool);
6804 static bool
6805 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6807 if (!h->mark
6808 && (((h->root.type == bfd_link_hash_defined
6809 || h->root.type == bfd_link_hash_defweak)
6810 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6811 && h->root.u.def.section->gc_mark))
6812 || h->root.type == bfd_link_hash_undefined
6813 || h->root.type == bfd_link_hash_undefweak))
6815 struct elf_gc_sweep_symbol_info *inf;
6817 inf = (struct elf_gc_sweep_symbol_info *) data;
6818 (*inf->hide_symbol) (inf->info, h, true);
6819 h->def_regular = 0;
6820 h->ref_regular = 0;
6821 h->ref_regular_nonweak = 0;
6824 return true;
6827 /* Set up the sizes and contents of the ELF dynamic sections. This is
6828 called by the ELF linker emulation before_allocation routine. We
6829 must set the sizes of the sections before the linker sets the
6830 addresses of the various sections. */
6832 bool
6833 bfd_elf_size_dynamic_sections (bfd *output_bfd,
6834 const char *soname,
6835 const char *rpath,
6836 const char *filter_shlib,
6837 const char *audit,
6838 const char *depaudit,
6839 const char * const *auxiliary_filters,
6840 struct bfd_link_info *info,
6841 asection **sinterpptr)
6843 bfd *dynobj;
6844 const struct elf_backend_data *bed;
6846 *sinterpptr = NULL;
6848 if (!is_elf_hash_table (info->hash))
6849 return true;
6851 /* Any syms created from now on start with -1 in
6852 got.refcount/offset and plt.refcount/offset. */
6853 elf_hash_table (info)->init_got_refcount
6854 = elf_hash_table (info)->init_got_offset;
6855 elf_hash_table (info)->init_plt_refcount
6856 = elf_hash_table (info)->init_plt_offset;
6858 bed = get_elf_backend_data (output_bfd);
6860 /* The backend may have to create some sections regardless of whether
6861 we're dynamic or not. */
6862 if (bed->elf_backend_early_size_sections
6863 && !bed->elf_backend_early_size_sections (output_bfd, info))
6864 return false;
6866 dynobj = elf_hash_table (info)->dynobj;
6868 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6870 struct bfd_elf_version_tree *verdefs;
6871 struct elf_info_failed asvinfo;
6872 struct bfd_elf_version_tree *t;
6873 struct bfd_elf_version_expr *d;
6874 asection *s;
6875 size_t soname_indx;
6877 /* If we are supposed to export all symbols into the dynamic symbol
6878 table (this is not the normal case), then do so. */
6879 if (info->export_dynamic
6880 || (bfd_link_executable (info) && info->dynamic))
6882 struct elf_info_failed eif;
6884 eif.info = info;
6885 eif.failed = false;
6886 elf_link_hash_traverse (elf_hash_table (info),
6887 _bfd_elf_export_symbol,
6888 &eif);
6889 if (eif.failed)
6890 return false;
6893 if (soname != NULL)
6895 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6896 soname, true);
6897 if (soname_indx == (size_t) -1
6898 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6899 return false;
6901 else
6902 soname_indx = (size_t) -1;
6904 /* Make all global versions with definition. */
6905 for (t = info->version_info; t != NULL; t = t->next)
6906 for (d = t->globals.list; d != NULL; d = d->next)
6907 if (!d->symver && d->literal)
6909 const char *verstr, *name;
6910 size_t namelen, verlen, newlen;
6911 char *newname, *p, leading_char;
6912 struct elf_link_hash_entry *newh;
6914 leading_char = bfd_get_symbol_leading_char (output_bfd);
6915 name = d->pattern;
6916 namelen = strlen (name) + (leading_char != '\0');
6917 verstr = t->name;
6918 verlen = strlen (verstr);
6919 newlen = namelen + verlen + 3;
6921 newname = (char *) bfd_malloc (newlen);
6922 if (newname == NULL)
6923 return false;
6924 newname[0] = leading_char;
6925 memcpy (newname + (leading_char != '\0'), name, namelen);
6927 /* Check the hidden versioned definition. */
6928 p = newname + namelen;
6929 *p++ = ELF_VER_CHR;
6930 memcpy (p, verstr, verlen + 1);
6931 newh = elf_link_hash_lookup (elf_hash_table (info),
6932 newname, false, false,
6933 false);
6934 if (newh == NULL
6935 || (newh->root.type != bfd_link_hash_defined
6936 && newh->root.type != bfd_link_hash_defweak))
6938 /* Check the default versioned definition. */
6939 *p++ = ELF_VER_CHR;
6940 memcpy (p, verstr, verlen + 1);
6941 newh = elf_link_hash_lookup (elf_hash_table (info),
6942 newname, false, false,
6943 false);
6945 free (newname);
6947 /* Mark this version if there is a definition and it is
6948 not defined in a shared object. */
6949 if (newh != NULL
6950 && !newh->def_dynamic
6951 && (newh->root.type == bfd_link_hash_defined
6952 || newh->root.type == bfd_link_hash_defweak))
6953 d->symver = 1;
6956 /* Attach all the symbols to their version information. */
6957 asvinfo.info = info;
6958 asvinfo.failed = false;
6960 elf_link_hash_traverse (elf_hash_table (info),
6961 _bfd_elf_link_assign_sym_version,
6962 &asvinfo);
6963 if (asvinfo.failed)
6964 return false;
6966 if (!info->allow_undefined_version)
6968 /* Check if all global versions have a definition. */
6969 bool all_defined = true;
6970 for (t = info->version_info; t != NULL; t = t->next)
6971 for (d = t->globals.list; d != NULL; d = d->next)
6972 if (d->literal && !d->symver && !d->script)
6974 _bfd_error_handler
6975 (_("%s: undefined version: %s"),
6976 d->pattern, t->name);
6977 all_defined = false;
6980 if (!all_defined)
6982 bfd_set_error (bfd_error_bad_value);
6983 return false;
6987 /* Set up the version definition section. */
6988 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6989 BFD_ASSERT (s != NULL);
6991 /* We may have created additional version definitions if we are
6992 just linking a regular application. */
6993 verdefs = info->version_info;
6995 /* Skip anonymous version tag. */
6996 if (verdefs != NULL && verdefs->vernum == 0)
6997 verdefs = verdefs->next;
6999 if (verdefs == NULL && !info->create_default_symver)
7000 s->flags |= SEC_EXCLUDE;
7001 else
7003 unsigned int cdefs;
7004 bfd_size_type size;
7005 bfd_byte *p;
7006 Elf_Internal_Verdef def;
7007 Elf_Internal_Verdaux defaux;
7008 struct bfd_link_hash_entry *bh;
7009 struct elf_link_hash_entry *h;
7010 const char *name;
7012 cdefs = 0;
7013 size = 0;
7015 /* Make space for the base version. */
7016 size += sizeof (Elf_External_Verdef);
7017 size += sizeof (Elf_External_Verdaux);
7018 ++cdefs;
7020 /* Make space for the default version. */
7021 if (info->create_default_symver)
7023 size += sizeof (Elf_External_Verdef);
7024 ++cdefs;
7027 for (t = verdefs; t != NULL; t = t->next)
7029 struct bfd_elf_version_deps *n;
7031 /* Don't emit base version twice. */
7032 if (t->vernum == 0)
7033 continue;
7035 size += sizeof (Elf_External_Verdef);
7036 size += sizeof (Elf_External_Verdaux);
7037 ++cdefs;
7039 for (n = t->deps; n != NULL; n = n->next)
7040 size += sizeof (Elf_External_Verdaux);
7043 s->size = size;
7044 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7045 if (s->contents == NULL && s->size != 0)
7046 return false;
7048 /* Fill in the version definition section. */
7050 p = s->contents;
7052 def.vd_version = VER_DEF_CURRENT;
7053 def.vd_flags = VER_FLG_BASE;
7054 def.vd_ndx = 1;
7055 def.vd_cnt = 1;
7056 if (info->create_default_symver)
7058 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
7059 def.vd_next = sizeof (Elf_External_Verdef);
7061 else
7063 def.vd_aux = sizeof (Elf_External_Verdef);
7064 def.vd_next = (sizeof (Elf_External_Verdef)
7065 + sizeof (Elf_External_Verdaux));
7068 if (soname_indx != (size_t) -1)
7070 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
7071 soname_indx);
7072 def.vd_hash = bfd_elf_hash (soname);
7073 defaux.vda_name = soname_indx;
7074 name = soname;
7076 else
7078 size_t indx;
7080 name = lbasename (bfd_get_filename (output_bfd));
7081 def.vd_hash = bfd_elf_hash (name);
7082 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7083 name, false);
7084 if (indx == (size_t) -1)
7085 return false;
7086 defaux.vda_name = indx;
7088 defaux.vda_next = 0;
7090 _bfd_elf_swap_verdef_out (output_bfd, &def,
7091 (Elf_External_Verdef *) p);
7092 p += sizeof (Elf_External_Verdef);
7093 if (info->create_default_symver)
7095 /* Add a symbol representing this version. */
7096 bh = NULL;
7097 if (! (_bfd_generic_link_add_one_symbol
7098 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
7099 0, NULL, false,
7100 get_elf_backend_data (dynobj)->collect, &bh)))
7101 return false;
7102 h = (struct elf_link_hash_entry *) bh;
7103 h->non_elf = 0;
7104 h->def_regular = 1;
7105 h->type = STT_OBJECT;
7106 h->verinfo.vertree = NULL;
7108 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7109 return false;
7111 /* Create a duplicate of the base version with the same
7112 aux block, but different flags. */
7113 def.vd_flags = 0;
7114 def.vd_ndx = 2;
7115 def.vd_aux = sizeof (Elf_External_Verdef);
7116 if (verdefs)
7117 def.vd_next = (sizeof (Elf_External_Verdef)
7118 + sizeof (Elf_External_Verdaux));
7119 else
7120 def.vd_next = 0;
7121 _bfd_elf_swap_verdef_out (output_bfd, &def,
7122 (Elf_External_Verdef *) p);
7123 p += sizeof (Elf_External_Verdef);
7125 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
7126 (Elf_External_Verdaux *) p);
7127 p += sizeof (Elf_External_Verdaux);
7129 for (t = verdefs; t != NULL; t = t->next)
7131 unsigned int cdeps;
7132 struct bfd_elf_version_deps *n;
7134 /* Don't emit the base version twice. */
7135 if (t->vernum == 0)
7136 continue;
7138 cdeps = 0;
7139 for (n = t->deps; n != NULL; n = n->next)
7140 ++cdeps;
7142 /* Add a symbol representing this version. */
7143 bh = NULL;
7144 if (! (_bfd_generic_link_add_one_symbol
7145 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
7146 0, NULL, false,
7147 get_elf_backend_data (dynobj)->collect, &bh)))
7148 return false;
7149 h = (struct elf_link_hash_entry *) bh;
7150 h->non_elf = 0;
7151 h->def_regular = 1;
7152 h->type = STT_OBJECT;
7153 h->verinfo.vertree = t;
7155 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7156 return false;
7158 def.vd_version = VER_DEF_CURRENT;
7159 def.vd_flags = 0;
7160 if (t->globals.list == NULL
7161 && t->locals.list == NULL
7162 && ! t->used)
7163 def.vd_flags |= VER_FLG_WEAK;
7164 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
7165 def.vd_cnt = cdeps + 1;
7166 def.vd_hash = bfd_elf_hash (t->name);
7167 def.vd_aux = sizeof (Elf_External_Verdef);
7168 def.vd_next = 0;
7170 /* If a basever node is next, it *must* be the last node in
7171 the chain, otherwise Verdef construction breaks. */
7172 if (t->next != NULL && t->next->vernum == 0)
7173 BFD_ASSERT (t->next->next == NULL);
7175 if (t->next != NULL && t->next->vernum != 0)
7176 def.vd_next = (sizeof (Elf_External_Verdef)
7177 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
7179 _bfd_elf_swap_verdef_out (output_bfd, &def,
7180 (Elf_External_Verdef *) p);
7181 p += sizeof (Elf_External_Verdef);
7183 defaux.vda_name = h->dynstr_index;
7184 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
7185 h->dynstr_index);
7186 defaux.vda_next = 0;
7187 if (t->deps != NULL)
7188 defaux.vda_next = sizeof (Elf_External_Verdaux);
7189 t->name_indx = defaux.vda_name;
7191 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
7192 (Elf_External_Verdaux *) p);
7193 p += sizeof (Elf_External_Verdaux);
7195 for (n = t->deps; n != NULL; n = n->next)
7197 if (n->version_needed == NULL)
7199 /* This can happen if there was an error in the
7200 version script. */
7201 defaux.vda_name = 0;
7203 else
7205 defaux.vda_name = n->version_needed->name_indx;
7206 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
7207 defaux.vda_name);
7209 if (n->next == NULL)
7210 defaux.vda_next = 0;
7211 else
7212 defaux.vda_next = sizeof (Elf_External_Verdaux);
7214 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
7215 (Elf_External_Verdaux *) p);
7216 p += sizeof (Elf_External_Verdaux);
7220 elf_tdata (output_bfd)->cverdefs = cdefs;
7224 if (info->gc_sections && bed->can_gc_sections)
7226 struct elf_gc_sweep_symbol_info sweep_info;
7228 /* Remove the symbols that were in the swept sections from the
7229 dynamic symbol table. */
7230 sweep_info.info = info;
7231 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
7232 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
7233 &sweep_info);
7236 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7238 asection *s;
7239 struct elf_find_verdep_info sinfo;
7241 /* Work out the size of the version reference section. */
7243 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
7244 BFD_ASSERT (s != NULL);
7246 sinfo.info = info;
7247 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
7248 if (sinfo.vers == 0)
7249 sinfo.vers = 1;
7250 sinfo.failed = false;
7252 elf_link_hash_traverse (elf_hash_table (info),
7253 _bfd_elf_link_find_version_dependencies,
7254 &sinfo);
7255 if (sinfo.failed)
7256 return false;
7258 bed->elf_backend_add_glibc_version_dependency (&sinfo);
7259 if (sinfo.failed)
7260 return false;
7262 if (elf_tdata (output_bfd)->verref == NULL)
7263 s->flags |= SEC_EXCLUDE;
7264 else
7266 Elf_Internal_Verneed *vn;
7267 unsigned int size;
7268 unsigned int crefs;
7269 bfd_byte *p;
7271 /* Build the version dependency section. */
7272 size = 0;
7273 crefs = 0;
7274 for (vn = elf_tdata (output_bfd)->verref;
7275 vn != NULL;
7276 vn = vn->vn_nextref)
7278 Elf_Internal_Vernaux *a;
7280 size += sizeof (Elf_External_Verneed);
7281 ++crefs;
7282 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7283 size += sizeof (Elf_External_Vernaux);
7286 s->size = size;
7287 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7288 if (s->contents == NULL)
7289 return false;
7291 p = s->contents;
7292 for (vn = elf_tdata (output_bfd)->verref;
7293 vn != NULL;
7294 vn = vn->vn_nextref)
7296 unsigned int caux;
7297 Elf_Internal_Vernaux *a;
7298 size_t indx;
7300 caux = 0;
7301 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7302 ++caux;
7304 vn->vn_version = VER_NEED_CURRENT;
7305 vn->vn_cnt = caux;
7306 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7307 elf_dt_name (vn->vn_bfd) != NULL
7308 ? elf_dt_name (vn->vn_bfd)
7309 : lbasename (bfd_get_filename
7310 (vn->vn_bfd)),
7311 false);
7312 if (indx == (size_t) -1)
7313 return false;
7314 vn->vn_file = indx;
7315 vn->vn_aux = sizeof (Elf_External_Verneed);
7316 if (vn->vn_nextref == NULL)
7317 vn->vn_next = 0;
7318 else
7319 vn->vn_next = (sizeof (Elf_External_Verneed)
7320 + caux * sizeof (Elf_External_Vernaux));
7322 _bfd_elf_swap_verneed_out (output_bfd, vn,
7323 (Elf_External_Verneed *) p);
7324 p += sizeof (Elf_External_Verneed);
7326 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7328 a->vna_hash = bfd_elf_hash (a->vna_nodename);
7329 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7330 a->vna_nodename, false);
7331 if (indx == (size_t) -1)
7332 return false;
7333 a->vna_name = indx;
7334 if (a->vna_nextptr == NULL)
7335 a->vna_next = 0;
7336 else
7337 a->vna_next = sizeof (Elf_External_Vernaux);
7339 _bfd_elf_swap_vernaux_out (output_bfd, a,
7340 (Elf_External_Vernaux *) p);
7341 p += sizeof (Elf_External_Vernaux);
7345 elf_tdata (output_bfd)->cverrefs = crefs;
7349 if (bfd_link_relocatable (info)
7350 && !_bfd_elf_size_group_sections (info))
7351 return false;
7353 /* Determine any GNU_STACK segment requirements, after the backend
7354 has had a chance to set a default segment size. */
7355 if (info->execstack)
7357 /* If the user has explicitly requested warnings, then generate one even
7358 though the choice is the result of another command line option. */
7359 if (info->warn_execstack == 1)
7361 if (info->error_execstack)
7363 _bfd_error_handler
7364 (_("\
7365 error: creating an executable stack because of -z execstack command line option"));
7366 return false;
7369 _bfd_error_handler
7370 (_("\
7371 warning: enabling an executable stack because of -z execstack command line option"));
7374 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
7376 else if (info->noexecstack)
7377 elf_stack_flags (output_bfd) = PF_R | PF_W;
7378 else
7380 bfd *inputobj;
7381 asection *notesec = NULL;
7382 bfd *noteobj = NULL;
7383 bfd *emptyobj = NULL;
7384 int exec = 0;
7386 for (inputobj = info->input_bfds;
7387 inputobj;
7388 inputobj = inputobj->link.next)
7390 asection *s;
7392 if (inputobj->flags
7393 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
7394 continue;
7395 s = inputobj->sections;
7396 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7397 continue;
7399 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
7400 if (s)
7402 notesec = s;
7403 if (s->flags & SEC_CODE)
7405 noteobj = inputobj;
7406 exec = PF_X;
7407 /* There is no point in scanning the remaining bfds. */
7408 break;
7411 else if (bed->default_execstack && info->default_execstack)
7413 exec = PF_X;
7414 emptyobj = inputobj;
7418 if (notesec || info->stacksize > 0)
7420 if (exec)
7422 if (info->warn_execstack != 0)
7424 /* PR 29072: Because an executable stack is a serious
7425 security risk, make sure that the user knows that it is
7426 being enabled despite the fact that it was not requested
7427 on the command line. */
7428 if (noteobj)
7430 if (info->error_execstack)
7432 _bfd_error_handler (_("\
7433 error: %s: is triggering the generation of an executable stack (because it has an executable .note.GNU-stack section)"),
7434 bfd_get_filename (noteobj));
7435 return false;
7438 _bfd_error_handler (_("\
7439 warning: %s: requires executable stack (because the .note.GNU-stack section is executable)"),
7440 bfd_get_filename (noteobj));
7442 else if (emptyobj)
7444 if (info->error_execstack)
7446 _bfd_error_handler (_("\
7447 error: %s: is triggering the generation of an executable stack because it does not have a .note.GNU-stack section"),
7448 bfd_get_filename (emptyobj));
7449 return false;
7452 _bfd_error_handler (_("\
7453 warning: %s: missing .note.GNU-stack section implies executable stack"),
7454 bfd_get_filename (emptyobj));
7455 _bfd_error_handler (_("\
7456 NOTE: This behaviour is deprecated and will be removed in a future version of the linker"));
7460 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
7463 if (notesec && exec && bfd_link_relocatable (info)
7464 && notesec->output_section != bfd_abs_section_ptr)
7465 notesec->output_section->flags |= SEC_CODE;
7468 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7470 struct elf_info_failed eif;
7471 struct elf_link_hash_entry *h;
7472 asection *dynstr;
7473 asection *s;
7475 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
7476 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
7478 if (info->symbolic)
7480 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
7481 return false;
7482 info->flags |= DF_SYMBOLIC;
7485 if (rpath != NULL)
7487 size_t indx;
7488 bfd_vma tag;
7490 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
7491 true);
7492 if (indx == (size_t) -1)
7493 return false;
7495 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
7496 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
7497 return false;
7500 if (filter_shlib != NULL)
7502 size_t indx;
7504 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7505 filter_shlib, true);
7506 if (indx == (size_t) -1
7507 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
7508 return false;
7511 if (auxiliary_filters != NULL)
7513 const char * const *p;
7515 for (p = auxiliary_filters; *p != NULL; p++)
7517 size_t indx;
7519 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7520 *p, true);
7521 if (indx == (size_t) -1
7522 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
7523 return false;
7527 if (audit != NULL)
7529 size_t indx;
7531 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
7532 true);
7533 if (indx == (size_t) -1
7534 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
7535 return false;
7538 if (depaudit != NULL)
7540 size_t indx;
7542 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
7543 true);
7544 if (indx == (size_t) -1
7545 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
7546 return false;
7549 eif.info = info;
7550 eif.failed = false;
7552 /* Find all symbols which were defined in a dynamic object and make
7553 the backend pick a reasonable value for them. */
7554 elf_link_hash_traverse (elf_hash_table (info),
7555 _bfd_elf_adjust_dynamic_symbol,
7556 &eif);
7557 if (eif.failed)
7558 return false;
7560 /* Add some entries to the .dynamic section. We fill in some of the
7561 values later, in bfd_elf_final_link, but we must add the entries
7562 now so that we know the final size of the .dynamic section. */
7564 /* If there are initialization and/or finalization functions to
7565 call then add the corresponding DT_INIT/DT_FINI entries. */
7566 h = (info->init_function
7567 ? elf_link_hash_lookup (elf_hash_table (info),
7568 info->init_function, false,
7569 false, false)
7570 : NULL);
7571 if (h != NULL
7572 && (h->ref_regular
7573 || h->def_regular))
7575 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
7576 return false;
7578 h = (info->fini_function
7579 ? elf_link_hash_lookup (elf_hash_table (info),
7580 info->fini_function, false,
7581 false, false)
7582 : NULL);
7583 if (h != NULL
7584 && (h->ref_regular
7585 || h->def_regular))
7587 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
7588 return false;
7591 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
7592 if (s != NULL && s->linker_has_input)
7594 /* DT_PREINIT_ARRAY is not allowed in shared library. */
7595 if (! bfd_link_executable (info))
7597 bfd *sub;
7598 asection *o;
7600 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
7601 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
7602 && (o = sub->sections) != NULL
7603 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
7604 for (o = sub->sections; o != NULL; o = o->next)
7605 if (elf_section_data (o)->this_hdr.sh_type
7606 == SHT_PREINIT_ARRAY)
7608 _bfd_error_handler
7609 (_("%pB: .preinit_array section is not allowed in DSO"),
7610 sub);
7611 break;
7614 bfd_set_error (bfd_error_nonrepresentable_section);
7615 return false;
7618 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
7619 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
7620 return false;
7622 s = bfd_get_section_by_name (output_bfd, ".init_array");
7623 if (s != NULL && s->linker_has_input)
7625 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
7626 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
7627 return false;
7629 s = bfd_get_section_by_name (output_bfd, ".fini_array");
7630 if (s != NULL && s->linker_has_input)
7632 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
7633 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
7634 return false;
7637 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
7638 /* If .dynstr is excluded from the link, we don't want any of
7639 these tags. Strictly, we should be checking each section
7640 individually; This quick check covers for the case where
7641 someone does a /DISCARD/ : { *(*) }. */
7642 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
7644 bfd_size_type strsize;
7646 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7647 if ((info->emit_hash
7648 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
7649 || (info->emit_gnu_hash
7650 && (bed->record_xhash_symbol == NULL
7651 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)))
7652 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
7653 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
7654 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
7655 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
7656 bed->s->sizeof_sym)
7657 || (info->gnu_flags_1
7658 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_FLAGS_1,
7659 info->gnu_flags_1)))
7660 return false;
7664 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
7665 return false;
7667 /* The backend must work out the sizes of all the other dynamic
7668 sections. */
7669 if (bed->elf_backend_late_size_sections != NULL
7670 && !bed->elf_backend_late_size_sections (output_bfd, info))
7671 return false;
7673 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7675 if (elf_tdata (output_bfd)->cverdefs)
7677 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
7679 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
7680 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
7681 return false;
7684 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
7686 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
7687 return false;
7689 else if (info->flags & DF_BIND_NOW)
7691 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
7692 return false;
7695 if (info->flags_1)
7697 if (bfd_link_executable (info))
7698 info->flags_1 &= ~ (DF_1_INITFIRST
7699 | DF_1_NODELETE
7700 | DF_1_NOOPEN);
7701 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
7702 return false;
7705 if (elf_tdata (output_bfd)->cverrefs)
7707 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
7709 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
7710 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
7711 return false;
7714 if ((elf_tdata (output_bfd)->cverrefs == 0
7715 && elf_tdata (output_bfd)->cverdefs == 0)
7716 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
7718 asection *s;
7720 s = bfd_get_linker_section (dynobj, ".gnu.version");
7721 s->flags |= SEC_EXCLUDE;
7724 return true;
7727 /* Find the first non-excluded output section. We'll use its
7728 section symbol for some emitted relocs. */
7729 void
7730 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7732 asection *s;
7733 asection *found = NULL;
7735 for (s = output_bfd->sections; s != NULL; s = s->next)
7736 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7737 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7739 found = s;
7740 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7741 break;
7743 elf_hash_table (info)->text_index_section = found;
7746 /* Find two non-excluded output sections, one for code, one for data.
7747 We'll use their section symbols for some emitted relocs. */
7748 void
7749 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7751 asection *s;
7752 asection *found = NULL;
7754 /* Data first, since setting text_index_section changes
7755 _bfd_elf_omit_section_dynsym_default. */
7756 for (s = output_bfd->sections; s != NULL; s = s->next)
7757 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7758 && !(s->flags & SEC_READONLY)
7759 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7761 found = s;
7762 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7763 break;
7765 elf_hash_table (info)->data_index_section = found;
7767 for (s = output_bfd->sections; s != NULL; s = s->next)
7768 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7769 && (s->flags & SEC_READONLY)
7770 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7772 found = s;
7773 break;
7775 elf_hash_table (info)->text_index_section = found;
7778 #define GNU_HASH_SECTION_NAME(bed) \
7779 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7781 bool
7782 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7784 const struct elf_backend_data *bed;
7785 unsigned long section_sym_count;
7786 bfd_size_type dynsymcount = 0;
7788 if (!is_elf_hash_table (info->hash))
7789 return true;
7791 bed = get_elf_backend_data (output_bfd);
7792 (*bed->elf_backend_init_index_section) (output_bfd, info);
7794 /* Assign dynsym indices. In a shared library we generate a section
7795 symbol for each output section, which come first. Next come all
7796 of the back-end allocated local dynamic syms, followed by the rest
7797 of the global symbols.
7799 This is usually not needed for static binaries, however backends
7800 can request to always do it, e.g. the MIPS backend uses dynamic
7801 symbol counts to lay out GOT, which will be produced in the
7802 presence of GOT relocations even in static binaries (holding fixed
7803 data in that case, to satisfy those relocations). */
7805 if (elf_hash_table (info)->dynamic_sections_created
7806 || bed->always_renumber_dynsyms)
7807 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7808 &section_sym_count);
7810 if (elf_hash_table (info)->dynamic_sections_created)
7812 bfd *dynobj;
7813 asection *s;
7814 unsigned int dtagcount;
7816 dynobj = elf_hash_table (info)->dynobj;
7818 /* Work out the size of the symbol version section. */
7819 s = bfd_get_linker_section (dynobj, ".gnu.version");
7820 BFD_ASSERT (s != NULL);
7821 if ((s->flags & SEC_EXCLUDE) == 0)
7823 s->size = dynsymcount * sizeof (Elf_External_Versym);
7824 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7825 if (s->contents == NULL)
7826 return false;
7828 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7829 return false;
7832 /* Set the size of the .dynsym and .hash sections. We counted
7833 the number of dynamic symbols in elf_link_add_object_symbols.
7834 We will build the contents of .dynsym and .hash when we build
7835 the final symbol table, because until then we do not know the
7836 correct value to give the symbols. We built the .dynstr
7837 section as we went along in elf_link_add_object_symbols. */
7838 s = elf_hash_table (info)->dynsym;
7839 BFD_ASSERT (s != NULL);
7840 s->size = dynsymcount * bed->s->sizeof_sym;
7842 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7843 if (s->contents == NULL)
7844 return false;
7846 /* The first entry in .dynsym is a dummy symbol. Clear all the
7847 section syms, in case we don't output them all. */
7848 ++section_sym_count;
7849 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
7851 elf_hash_table (info)->bucketcount = 0;
7853 /* Compute the size of the hashing table. As a side effect this
7854 computes the hash values for all the names we export. */
7855 if (info->emit_hash)
7857 unsigned long int *hashcodes;
7858 struct hash_codes_info hashinf;
7859 bfd_size_type amt;
7860 unsigned long int nsyms;
7861 size_t bucketcount;
7862 size_t hash_entry_size;
7864 /* Compute the hash values for all exported symbols. At the same
7865 time store the values in an array so that we could use them for
7866 optimizations. */
7867 amt = dynsymcount * sizeof (unsigned long int);
7868 hashcodes = (unsigned long int *) bfd_malloc (amt);
7869 if (hashcodes == NULL)
7870 return false;
7871 hashinf.hashcodes = hashcodes;
7872 hashinf.error = false;
7874 /* Put all hash values in HASHCODES. */
7875 elf_link_hash_traverse (elf_hash_table (info),
7876 elf_collect_hash_codes, &hashinf);
7877 if (hashinf.error)
7879 free (hashcodes);
7880 return false;
7883 nsyms = hashinf.hashcodes - hashcodes;
7884 bucketcount
7885 = compute_bucket_count (info, hashcodes, nsyms, 0);
7886 free (hashcodes);
7888 if (bucketcount == 0 && nsyms > 0)
7889 return false;
7891 elf_hash_table (info)->bucketcount = bucketcount;
7893 s = bfd_get_linker_section (dynobj, ".hash");
7894 BFD_ASSERT (s != NULL);
7895 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7896 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
7897 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7898 if (s->contents == NULL)
7899 return false;
7901 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7902 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7903 s->contents + hash_entry_size);
7906 if (info->emit_gnu_hash)
7908 size_t i, cnt;
7909 unsigned char *contents;
7910 struct collect_gnu_hash_codes cinfo;
7911 bfd_size_type amt;
7912 size_t bucketcount;
7914 memset (&cinfo, 0, sizeof (cinfo));
7916 /* Compute the hash values for all exported symbols. At the same
7917 time store the values in an array so that we could use them for
7918 optimizations. */
7919 amt = dynsymcount * 2 * sizeof (unsigned long int);
7920 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
7921 if (cinfo.hashcodes == NULL)
7922 return false;
7924 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7925 cinfo.min_dynindx = -1;
7926 cinfo.output_bfd = output_bfd;
7927 cinfo.bed = bed;
7929 /* Put all hash values in HASHCODES. */
7930 elf_link_hash_traverse (elf_hash_table (info),
7931 elf_collect_gnu_hash_codes, &cinfo);
7932 if (cinfo.error)
7934 free (cinfo.hashcodes);
7935 return false;
7938 bucketcount
7939 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7941 if (bucketcount == 0)
7943 free (cinfo.hashcodes);
7944 return false;
7947 s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (bed));
7948 BFD_ASSERT (s != NULL);
7950 if (cinfo.nsyms == 0)
7952 /* Empty .gnu.hash or .MIPS.xhash section is special. */
7953 BFD_ASSERT (cinfo.min_dynindx == -1);
7954 free (cinfo.hashcodes);
7955 s->size = 5 * 4 + bed->s->arch_size / 8;
7956 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7957 if (contents == NULL)
7958 return false;
7959 s->contents = contents;
7960 /* 1 empty bucket. */
7961 bfd_put_32 (output_bfd, 1, contents);
7962 /* SYMIDX above the special symbol 0. */
7963 bfd_put_32 (output_bfd, 1, contents + 4);
7964 /* Just one word for bitmask. */
7965 bfd_put_32 (output_bfd, 1, contents + 8);
7966 /* Only hash fn bloom filter. */
7967 bfd_put_32 (output_bfd, 0, contents + 12);
7968 /* No hashes are valid - empty bitmask. */
7969 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7970 /* No hashes in the only bucket. */
7971 bfd_put_32 (output_bfd, 0,
7972 contents + 16 + bed->s->arch_size / 8);
7974 else
7976 unsigned long int maskwords, maskbitslog2, x;
7977 BFD_ASSERT (cinfo.min_dynindx != -1);
7979 x = cinfo.nsyms;
7980 maskbitslog2 = 1;
7981 while ((x >>= 1) != 0)
7982 ++maskbitslog2;
7983 if (maskbitslog2 < 3)
7984 maskbitslog2 = 5;
7985 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7986 maskbitslog2 = maskbitslog2 + 3;
7987 else
7988 maskbitslog2 = maskbitslog2 + 2;
7989 if (bed->s->arch_size == 64)
7991 if (maskbitslog2 == 5)
7992 maskbitslog2 = 6;
7993 cinfo.shift1 = 6;
7995 else
7996 cinfo.shift1 = 5;
7997 cinfo.mask = (1 << cinfo.shift1) - 1;
7998 cinfo.shift2 = maskbitslog2;
7999 cinfo.maskbits = 1 << maskbitslog2;
8000 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
8001 amt = bucketcount * sizeof (unsigned long int) * 2;
8002 amt += maskwords * sizeof (bfd_vma);
8003 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
8004 if (cinfo.bitmask == NULL)
8006 free (cinfo.hashcodes);
8007 return false;
8010 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
8011 cinfo.indx = cinfo.counts + bucketcount;
8012 cinfo.symindx = dynsymcount - cinfo.nsyms;
8013 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
8015 /* Determine how often each hash bucket is used. */
8016 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
8017 for (i = 0; i < cinfo.nsyms; ++i)
8018 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
8020 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
8021 if (cinfo.counts[i] != 0)
8023 cinfo.indx[i] = cnt;
8024 cnt += cinfo.counts[i];
8026 BFD_ASSERT (cnt == dynsymcount);
8027 cinfo.bucketcount = bucketcount;
8028 cinfo.local_indx = cinfo.min_dynindx;
8030 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
8031 s->size += cinfo.maskbits / 8;
8032 if (bed->record_xhash_symbol != NULL)
8033 s->size += cinfo.nsyms * 4;
8034 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
8035 if (contents == NULL)
8037 free (cinfo.bitmask);
8038 free (cinfo.hashcodes);
8039 return false;
8042 s->contents = contents;
8043 bfd_put_32 (output_bfd, bucketcount, contents);
8044 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
8045 bfd_put_32 (output_bfd, maskwords, contents + 8);
8046 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
8047 contents += 16 + cinfo.maskbits / 8;
8049 for (i = 0; i < bucketcount; ++i)
8051 if (cinfo.counts[i] == 0)
8052 bfd_put_32 (output_bfd, 0, contents);
8053 else
8054 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
8055 contents += 4;
8058 cinfo.contents = contents;
8060 cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents;
8061 /* Renumber dynamic symbols, if populating .gnu.hash section.
8062 If using .MIPS.xhash, populate the translation table. */
8063 elf_link_hash_traverse (elf_hash_table (info),
8064 elf_gnu_hash_process_symidx, &cinfo);
8066 contents = s->contents + 16;
8067 for (i = 0; i < maskwords; ++i)
8069 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
8070 contents);
8071 contents += bed->s->arch_size / 8;
8074 free (cinfo.bitmask);
8075 free (cinfo.hashcodes);
8079 s = bfd_get_linker_section (dynobj, ".dynstr");
8080 BFD_ASSERT (s != NULL);
8082 elf_finalize_dynstr (output_bfd, info);
8084 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
8086 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
8087 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
8088 return false;
8091 return true;
8094 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
8096 static void
8097 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
8098 asection *sec)
8100 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
8101 sec->sec_info_type = SEC_INFO_TYPE_NONE;
8104 /* Finish SHF_MERGE section merging. */
8106 bool
8107 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
8109 bfd *ibfd;
8110 asection *sec;
8112 if (ENABLE_CHECKING && !is_elf_hash_table (info->hash))
8113 abort ();
8115 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8116 if ((ibfd->flags & DYNAMIC) == 0
8117 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
8118 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
8119 == get_elf_backend_data (obfd)->s->elfclass))
8120 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8121 if ((sec->flags & SEC_MERGE) != 0
8122 && !bfd_is_abs_section (sec->output_section))
8124 struct bfd_elf_section_data *secdata;
8126 secdata = elf_section_data (sec);
8127 if (! _bfd_add_merge_section (obfd,
8128 &elf_hash_table (info)->merge_info,
8129 sec, &secdata->sec_info))
8130 return false;
8131 else if (secdata->sec_info)
8132 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
8135 if (elf_hash_table (info)->merge_info != NULL)
8136 return _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
8137 merge_sections_remove_hook);
8138 return true;
8141 /* Create an entry in an ELF linker hash table. */
8143 struct bfd_hash_entry *
8144 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
8145 struct bfd_hash_table *table,
8146 const char *string)
8148 /* Allocate the structure if it has not already been allocated by a
8149 subclass. */
8150 if (entry == NULL)
8152 entry = (struct bfd_hash_entry *)
8153 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
8154 if (entry == NULL)
8155 return entry;
8158 /* Call the allocation method of the superclass. */
8159 entry = _bfd_link_hash_newfunc (entry, table, string);
8160 if (entry != NULL)
8162 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
8163 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
8165 /* Set local fields. */
8166 ret->indx = -1;
8167 ret->dynindx = -1;
8168 ret->got = htab->init_got_refcount;
8169 ret->plt = htab->init_plt_refcount;
8170 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
8171 - offsetof (struct elf_link_hash_entry, size)));
8172 /* Assume that we have been called by a non-ELF symbol reader.
8173 This flag is then reset by the code which reads an ELF input
8174 file. This ensures that a symbol created by a non-ELF symbol
8175 reader will have the flag set correctly. */
8176 ret->non_elf = 1;
8179 return entry;
8182 /* Copy data from an indirect symbol to its direct symbol, hiding the
8183 old indirect symbol. Also used for copying flags to a weakdef. */
8185 void
8186 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
8187 struct elf_link_hash_entry *dir,
8188 struct elf_link_hash_entry *ind)
8190 struct elf_link_hash_table *htab;
8192 if (ind->dyn_relocs != NULL)
8194 if (dir->dyn_relocs != NULL)
8196 struct elf_dyn_relocs **pp;
8197 struct elf_dyn_relocs *p;
8199 /* Add reloc counts against the indirect sym to the direct sym
8200 list. Merge any entries against the same section. */
8201 for (pp = &ind->dyn_relocs; (p = *pp) != NULL; )
8203 struct elf_dyn_relocs *q;
8205 for (q = dir->dyn_relocs; q != NULL; q = q->next)
8206 if (q->sec == p->sec)
8208 q->pc_count += p->pc_count;
8209 q->count += p->count;
8210 *pp = p->next;
8211 break;
8213 if (q == NULL)
8214 pp = &p->next;
8216 *pp = dir->dyn_relocs;
8219 dir->dyn_relocs = ind->dyn_relocs;
8220 ind->dyn_relocs = NULL;
8223 /* Copy down any references that we may have already seen to the
8224 symbol which just became indirect. */
8226 if (dir->versioned != versioned_hidden)
8227 dir->ref_dynamic |= ind->ref_dynamic;
8228 dir->ref_regular |= ind->ref_regular;
8229 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
8230 dir->non_got_ref |= ind->non_got_ref;
8231 dir->needs_plt |= ind->needs_plt;
8232 dir->pointer_equality_needed |= ind->pointer_equality_needed;
8234 if (ind->root.type != bfd_link_hash_indirect)
8235 return;
8237 /* Copy over the global and procedure linkage table refcount entries.
8238 These may have been already set up by a check_relocs routine. */
8239 htab = elf_hash_table (info);
8240 if (ind->got.refcount > htab->init_got_refcount.refcount)
8242 if (dir->got.refcount < 0)
8243 dir->got.refcount = 0;
8244 dir->got.refcount += ind->got.refcount;
8245 ind->got.refcount = htab->init_got_refcount.refcount;
8248 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
8250 if (dir->plt.refcount < 0)
8251 dir->plt.refcount = 0;
8252 dir->plt.refcount += ind->plt.refcount;
8253 ind->plt.refcount = htab->init_plt_refcount.refcount;
8256 if (ind->dynindx != -1)
8258 if (dir->dynindx != -1)
8259 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
8260 dir->dynindx = ind->dynindx;
8261 dir->dynstr_index = ind->dynstr_index;
8262 ind->dynindx = -1;
8263 ind->dynstr_index = 0;
8267 void
8268 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
8269 struct elf_link_hash_entry *h,
8270 bool force_local)
8272 /* STT_GNU_IFUNC symbol must go through PLT. */
8273 if (h->type != STT_GNU_IFUNC)
8275 h->plt = elf_hash_table (info)->init_plt_offset;
8276 h->needs_plt = 0;
8278 if (force_local)
8280 h->forced_local = 1;
8281 if (h->dynindx != -1)
8283 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8284 h->dynstr_index);
8285 h->dynindx = -1;
8286 h->dynstr_index = 0;
8291 /* Hide a symbol. */
8293 void
8294 _bfd_elf_link_hide_symbol (bfd *output_bfd,
8295 struct bfd_link_info *info,
8296 struct bfd_link_hash_entry *h)
8298 if (is_elf_hash_table (info->hash))
8300 const struct elf_backend_data *bed
8301 = get_elf_backend_data (output_bfd);
8302 struct elf_link_hash_entry *eh
8303 = (struct elf_link_hash_entry *) h;
8304 bed->elf_backend_hide_symbol (info, eh, true);
8305 eh->def_dynamic = 0;
8306 eh->ref_dynamic = 0;
8307 eh->dynamic_def = 0;
8311 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
8312 caller. */
8314 bool
8315 _bfd_elf_link_hash_table_init
8316 (struct elf_link_hash_table *table,
8317 bfd *abfd,
8318 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
8319 struct bfd_hash_table *,
8320 const char *),
8321 unsigned int entsize,
8322 enum elf_target_id target_id)
8324 bool ret;
8325 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
8327 table->init_got_refcount.refcount = can_refcount - 1;
8328 table->init_plt_refcount.refcount = can_refcount - 1;
8329 table->init_got_offset.offset = -(bfd_vma) 1;
8330 table->init_plt_offset.offset = -(bfd_vma) 1;
8331 /* The first dynamic symbol is a dummy. */
8332 table->dynsymcount = 1;
8334 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
8336 table->root.type = bfd_link_elf_hash_table;
8337 table->hash_table_id = target_id;
8338 table->target_os = get_elf_backend_data (abfd)->target_os;
8340 return ret;
8343 /* Create an ELF linker hash table. */
8345 struct bfd_link_hash_table *
8346 _bfd_elf_link_hash_table_create (bfd *abfd)
8348 struct elf_link_hash_table *ret;
8349 size_t amt = sizeof (struct elf_link_hash_table);
8351 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
8352 if (ret == NULL)
8353 return NULL;
8355 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
8356 sizeof (struct elf_link_hash_entry),
8357 GENERIC_ELF_DATA))
8359 free (ret);
8360 return NULL;
8362 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
8364 return &ret->root;
8367 /* Destroy an ELF linker hash table. */
8369 void
8370 _bfd_elf_link_hash_table_free (bfd *obfd)
8372 struct elf_link_hash_table *htab;
8374 htab = (struct elf_link_hash_table *) obfd->link.hash;
8375 if (htab->dynstr != NULL)
8376 _bfd_elf_strtab_free (htab->dynstr);
8377 _bfd_merge_sections_free (htab->merge_info);
8378 /* NB: htab->dynamic->contents is always allocated by bfd_realloc. */
8379 if (htab->dynamic != NULL)
8380 free (htab->dynamic->contents);
8381 if (htab->first_hash != NULL)
8383 bfd_hash_table_free (htab->first_hash);
8384 free (htab->first_hash);
8386 _bfd_generic_link_hash_table_free (obfd);
8389 /* This is a hook for the ELF emulation code in the generic linker to
8390 tell the backend linker what file name to use for the DT_NEEDED
8391 entry for a dynamic object. */
8393 void
8394 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
8396 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8397 && bfd_get_format (abfd) == bfd_object)
8398 elf_dt_name (abfd) = name;
8402 bfd_elf_get_dyn_lib_class (bfd *abfd)
8404 int lib_class;
8405 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8406 && bfd_get_format (abfd) == bfd_object)
8407 lib_class = elf_dyn_lib_class (abfd);
8408 else
8409 lib_class = 0;
8410 return lib_class;
8413 void
8414 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
8416 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8417 && bfd_get_format (abfd) == bfd_object)
8418 elf_dyn_lib_class (abfd) = lib_class;
8421 /* Get the list of DT_NEEDED entries for a link. This is a hook for
8422 the linker ELF emulation code. */
8424 struct bfd_link_needed_list *
8425 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
8426 struct bfd_link_info *info)
8428 if (! is_elf_hash_table (info->hash))
8429 return NULL;
8430 return elf_hash_table (info)->needed;
8433 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
8434 hook for the linker ELF emulation code. */
8436 struct bfd_link_needed_list *
8437 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
8438 struct bfd_link_info *info)
8440 if (! is_elf_hash_table (info->hash))
8441 return NULL;
8442 return elf_hash_table (info)->runpath;
8445 /* Get the name actually used for a dynamic object for a link. This
8446 is the SONAME entry if there is one. Otherwise, it is the string
8447 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
8449 const char *
8450 bfd_elf_get_dt_soname (bfd *abfd)
8452 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8453 && bfd_get_format (abfd) == bfd_object)
8454 return elf_dt_name (abfd);
8455 return NULL;
8458 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
8459 the ELF linker emulation code. */
8461 bool
8462 bfd_elf_get_bfd_needed_list (bfd *abfd,
8463 struct bfd_link_needed_list **pneeded)
8465 asection *s;
8466 bfd_byte *dynbuf = NULL;
8467 unsigned int elfsec;
8468 unsigned long shlink;
8469 bfd_byte *extdyn, *extdynend;
8470 size_t extdynsize;
8471 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
8473 *pneeded = NULL;
8475 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
8476 || bfd_get_format (abfd) != bfd_object)
8477 return true;
8479 s = bfd_get_section_by_name (abfd, ".dynamic");
8480 if (s == NULL || s->size == 0 || (s->flags & SEC_HAS_CONTENTS) == 0)
8481 return true;
8483 if (!_bfd_elf_mmap_section_contents (abfd, s, &dynbuf))
8484 goto error_return;
8486 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
8487 if (elfsec == SHN_BAD)
8488 goto error_return;
8490 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
8492 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
8493 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
8495 for (extdyn = dynbuf, extdynend = dynbuf + s->size;
8496 (size_t) (extdynend - extdyn) >= extdynsize;
8497 extdyn += extdynsize)
8499 Elf_Internal_Dyn dyn;
8501 (*swap_dyn_in) (abfd, extdyn, &dyn);
8503 if (dyn.d_tag == DT_NULL)
8504 break;
8506 if (dyn.d_tag == DT_NEEDED)
8508 const char *string;
8509 struct bfd_link_needed_list *l;
8510 unsigned int tagv = dyn.d_un.d_val;
8511 size_t amt;
8513 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
8514 if (string == NULL)
8515 goto error_return;
8517 amt = sizeof *l;
8518 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
8519 if (l == NULL)
8520 goto error_return;
8522 l->by = abfd;
8523 l->name = string;
8524 l->next = *pneeded;
8525 *pneeded = l;
8529 _bfd_elf_munmap_section_contents (s, dynbuf);
8531 return true;
8533 error_return:
8534 _bfd_elf_munmap_section_contents (s, dynbuf);
8535 return false;
8538 struct elf_symbuf_symbol
8540 unsigned long st_name; /* Symbol name, index in string tbl */
8541 unsigned char st_info; /* Type and binding attributes */
8542 unsigned char st_other; /* Visibilty, and target specific */
8545 struct elf_symbuf_head
8547 struct elf_symbuf_symbol *ssym;
8548 size_t count;
8549 unsigned int st_shndx;
8552 struct elf_symbol
8554 union
8556 Elf_Internal_Sym *isym;
8557 struct elf_symbuf_symbol *ssym;
8558 void *p;
8559 } u;
8560 const char *name;
8563 /* Sort references to symbols by ascending section number. */
8565 static int
8566 elf_sort_elf_symbol (const void *arg1, const void *arg2)
8568 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
8569 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
8571 if (s1->st_shndx != s2->st_shndx)
8572 return s1->st_shndx > s2->st_shndx ? 1 : -1;
8573 /* Final sort by the address of the sym in the symbuf ensures
8574 a stable sort. */
8575 if (s1 != s2)
8576 return s1 > s2 ? 1 : -1;
8577 return 0;
8580 static int
8581 elf_sym_name_compare (const void *arg1, const void *arg2)
8583 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
8584 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
8585 int ret = strcmp (s1->name, s2->name);
8586 if (ret != 0)
8587 return ret;
8588 if (s1->u.p != s2->u.p)
8589 return s1->u.p > s2->u.p ? 1 : -1;
8590 return 0;
8593 static struct elf_symbuf_head *
8594 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
8596 Elf_Internal_Sym **ind, **indbufend, **indbuf;
8597 struct elf_symbuf_symbol *ssym;
8598 struct elf_symbuf_head *ssymbuf, *ssymhead;
8599 size_t i, shndx_count, total_size, amt;
8601 amt = symcount * sizeof (*indbuf);
8602 indbuf = (Elf_Internal_Sym **) bfd_malloc (amt);
8603 if (indbuf == NULL)
8604 return NULL;
8606 for (ind = indbuf, i = 0; i < symcount; i++)
8607 if (isymbuf[i].st_shndx != SHN_UNDEF)
8608 *ind++ = &isymbuf[i];
8609 indbufend = ind;
8611 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
8612 elf_sort_elf_symbol);
8614 shndx_count = 0;
8615 if (indbufend > indbuf)
8616 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
8617 if (ind[0]->st_shndx != ind[1]->st_shndx)
8618 shndx_count++;
8620 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
8621 + (indbufend - indbuf) * sizeof (*ssym));
8622 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
8623 if (ssymbuf == NULL)
8625 free (indbuf);
8626 return NULL;
8629 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
8630 ssymbuf->ssym = NULL;
8631 ssymbuf->count = shndx_count;
8632 ssymbuf->st_shndx = 0;
8633 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
8635 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
8637 ssymhead++;
8638 ssymhead->ssym = ssym;
8639 ssymhead->count = 0;
8640 ssymhead->st_shndx = (*ind)->st_shndx;
8642 ssym->st_name = (*ind)->st_name;
8643 ssym->st_info = (*ind)->st_info;
8644 ssym->st_other = (*ind)->st_other;
8645 ssymhead->count++;
8647 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
8648 && (uintptr_t) ssym - (uintptr_t) ssymbuf == total_size);
8650 free (indbuf);
8651 return ssymbuf;
8654 /* Check if 2 sections define the same set of local and global
8655 symbols. */
8657 static bool
8658 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
8659 struct bfd_link_info *info)
8661 bfd *bfd1, *bfd2;
8662 const struct elf_backend_data *bed1, *bed2;
8663 Elf_Internal_Shdr *hdr1, *hdr2;
8664 size_t symcount1, symcount2;
8665 Elf_Internal_Sym *isymbuf1, *isymbuf2;
8666 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
8667 Elf_Internal_Sym *isym, *isymend;
8668 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
8669 size_t count1, count2, sec_count1, sec_count2, i;
8670 unsigned int shndx1, shndx2;
8671 bool result;
8672 bool ignore_section_symbol_p;
8674 bfd1 = sec1->owner;
8675 bfd2 = sec2->owner;
8677 /* Both sections have to be in ELF. */
8678 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
8679 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
8680 return false;
8682 if (elf_section_type (sec1) != elf_section_type (sec2))
8683 return false;
8685 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
8686 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
8687 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
8688 return false;
8690 bed1 = get_elf_backend_data (bfd1);
8691 bed2 = get_elf_backend_data (bfd2);
8692 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
8693 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
8694 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
8695 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
8697 if (symcount1 == 0 || symcount2 == 0)
8698 return false;
8700 result = false;
8701 isymbuf1 = NULL;
8702 isymbuf2 = NULL;
8703 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
8704 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
8706 /* Ignore section symbols only when matching non-debugging sections
8707 or linkonce section with comdat section. */
8708 ignore_section_symbol_p
8709 = ((sec1->flags & SEC_DEBUGGING) == 0
8710 || ((elf_section_flags (sec1) & SHF_GROUP)
8711 != (elf_section_flags (sec2) & SHF_GROUP)));
8713 if (ssymbuf1 == NULL)
8715 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
8716 NULL, NULL, NULL);
8717 if (isymbuf1 == NULL)
8718 goto done;
8720 if (info != NULL && !info->reduce_memory_overheads)
8722 ssymbuf1 = elf_create_symbuf (symcount1, isymbuf1);
8723 elf_tdata (bfd1)->symbuf = ssymbuf1;
8727 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
8729 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
8730 NULL, NULL, NULL);
8731 if (isymbuf2 == NULL)
8732 goto done;
8734 if (ssymbuf1 != NULL && info != NULL && !info->reduce_memory_overheads)
8736 ssymbuf2 = elf_create_symbuf (symcount2, isymbuf2);
8737 elf_tdata (bfd2)->symbuf = ssymbuf2;
8741 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
8743 /* Optimized faster version. */
8744 size_t lo, hi, mid;
8745 struct elf_symbol *symp;
8746 struct elf_symbuf_symbol *ssym, *ssymend;
8748 lo = 0;
8749 hi = ssymbuf1->count;
8750 ssymbuf1++;
8751 count1 = 0;
8752 sec_count1 = 0;
8753 while (lo < hi)
8755 mid = (lo + hi) / 2;
8756 if (shndx1 < ssymbuf1[mid].st_shndx)
8757 hi = mid;
8758 else if (shndx1 > ssymbuf1[mid].st_shndx)
8759 lo = mid + 1;
8760 else
8762 count1 = ssymbuf1[mid].count;
8763 ssymbuf1 += mid;
8764 break;
8767 if (ignore_section_symbol_p)
8769 for (i = 0; i < count1; i++)
8770 if (ELF_ST_TYPE (ssymbuf1->ssym[i].st_info) == STT_SECTION)
8771 sec_count1++;
8772 count1 -= sec_count1;
8775 lo = 0;
8776 hi = ssymbuf2->count;
8777 ssymbuf2++;
8778 count2 = 0;
8779 sec_count2 = 0;
8780 while (lo < hi)
8782 mid = (lo + hi) / 2;
8783 if (shndx2 < ssymbuf2[mid].st_shndx)
8784 hi = mid;
8785 else if (shndx2 > ssymbuf2[mid].st_shndx)
8786 lo = mid + 1;
8787 else
8789 count2 = ssymbuf2[mid].count;
8790 ssymbuf2 += mid;
8791 break;
8794 if (ignore_section_symbol_p)
8796 for (i = 0; i < count2; i++)
8797 if (ELF_ST_TYPE (ssymbuf2->ssym[i].st_info) == STT_SECTION)
8798 sec_count2++;
8799 count2 -= sec_count2;
8802 if (count1 == 0 || count2 == 0 || count1 != count2)
8803 goto done;
8805 symtable1
8806 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
8807 symtable2
8808 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
8809 if (symtable1 == NULL || symtable2 == NULL)
8810 goto done;
8812 symp = symtable1;
8813 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1 + sec_count1;
8814 ssym < ssymend; ssym++)
8815 if (sec_count1 == 0
8816 || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
8818 symp->u.ssym = ssym;
8819 symp->name = bfd_elf_string_from_elf_section (bfd1,
8820 hdr1->sh_link,
8821 ssym->st_name);
8822 if (symp->name == NULL)
8823 goto done;
8824 symp++;
8827 symp = symtable2;
8828 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2 + sec_count2;
8829 ssym < ssymend; ssym++)
8830 if (sec_count2 == 0
8831 || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
8833 symp->u.ssym = ssym;
8834 symp->name = bfd_elf_string_from_elf_section (bfd2,
8835 hdr2->sh_link,
8836 ssym->st_name);
8837 if (symp->name == NULL)
8838 goto done;
8839 symp++;
8842 /* Sort symbol by name. */
8843 qsort (symtable1, count1, sizeof (struct elf_symbol),
8844 elf_sym_name_compare);
8845 qsort (symtable2, count1, sizeof (struct elf_symbol),
8846 elf_sym_name_compare);
8848 for (i = 0; i < count1; i++)
8849 /* Two symbols must have the same binding, type and name. */
8850 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8851 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8852 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8853 goto done;
8855 result = true;
8856 goto done;
8859 symtable1 = (struct elf_symbol *)
8860 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8861 symtable2 = (struct elf_symbol *)
8862 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
8863 if (symtable1 == NULL || symtable2 == NULL)
8864 goto done;
8866 /* Count definitions in the section. */
8867 count1 = 0;
8868 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
8869 if (isym->st_shndx == shndx1
8870 && (!ignore_section_symbol_p
8871 || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
8872 symtable1[count1++].u.isym = isym;
8874 count2 = 0;
8875 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
8876 if (isym->st_shndx == shndx2
8877 && (!ignore_section_symbol_p
8878 || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
8879 symtable2[count2++].u.isym = isym;
8881 if (count1 == 0 || count2 == 0 || count1 != count2)
8882 goto done;
8884 for (i = 0; i < count1; i++)
8886 symtable1[i].name
8887 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8888 symtable1[i].u.isym->st_name);
8889 if (symtable1[i].name == NULL)
8890 goto done;
8893 for (i = 0; i < count2; i++)
8895 symtable2[i].name
8896 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8897 symtable2[i].u.isym->st_name);
8898 if (symtable2[i].name == NULL)
8899 goto done;
8902 /* Sort symbol by name. */
8903 qsort (symtable1, count1, sizeof (struct elf_symbol),
8904 elf_sym_name_compare);
8905 qsort (symtable2, count1, sizeof (struct elf_symbol),
8906 elf_sym_name_compare);
8908 for (i = 0; i < count1; i++)
8909 /* Two symbols must have the same binding, type and name. */
8910 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8911 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8912 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8913 goto done;
8915 result = true;
8917 done:
8918 free (symtable1);
8919 free (symtable2);
8920 free (isymbuf1);
8921 free (isymbuf2);
8923 return result;
8926 /* Return TRUE if 2 section types are compatible. */
8928 bool
8929 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8930 bfd *bbfd, const asection *bsec)
8932 if (asec == NULL
8933 || bsec == NULL
8934 || abfd->xvec->flavour != bfd_target_elf_flavour
8935 || bbfd->xvec->flavour != bfd_target_elf_flavour)
8936 return true;
8938 return elf_section_type (asec) == elf_section_type (bsec);
8941 /* Final phase of ELF linker. */
8943 /* A structure we use to avoid passing large numbers of arguments. */
8945 struct elf_final_link_info
8947 /* General link information. */
8948 struct bfd_link_info *info;
8949 /* Output BFD. */
8950 bfd *output_bfd;
8951 /* Symbol string table. */
8952 struct elf_strtab_hash *symstrtab;
8953 /* .hash section. */
8954 asection *hash_sec;
8955 /* symbol version section (.gnu.version). */
8956 asection *symver_sec;
8957 /* Buffer large enough to hold contents of any section. */
8958 bfd_byte *contents;
8959 /* Buffer large enough to hold external relocs of any section. */
8960 void *external_relocs;
8961 /* Buffer large enough to hold internal relocs of any section. */
8962 Elf_Internal_Rela *internal_relocs;
8963 /* Buffer large enough to hold external local symbols of any input
8964 BFD. */
8965 bfd_byte *external_syms;
8966 /* And a buffer for symbol section indices. */
8967 Elf_External_Sym_Shndx *locsym_shndx;
8968 /* Buffer large enough to hold internal local symbols of any input
8969 BFD. */
8970 Elf_Internal_Sym *internal_syms;
8971 /* Array large enough to hold a symbol index for each local symbol
8972 of any input BFD. */
8973 long *indices;
8974 /* Array large enough to hold a section pointer for each local
8975 symbol of any input BFD. */
8976 asection **sections;
8977 /* Buffer for SHT_SYMTAB_SHNDX section. */
8978 Elf_External_Sym_Shndx *symshndxbuf;
8979 /* Number of STT_FILE syms seen. */
8980 size_t filesym_count;
8981 /* Local symbol hash table. */
8982 struct bfd_hash_table local_hash_table;
8985 struct local_hash_entry
8987 /* Base hash table entry structure. */
8988 struct bfd_hash_entry root;
8989 /* Size of the local symbol name. */
8990 size_t size;
8991 /* Number of the duplicated local symbol names. */
8992 long count;
8995 /* Create an entry in the local symbol hash table. */
8997 static struct bfd_hash_entry *
8998 local_hash_newfunc (struct bfd_hash_entry *entry,
8999 struct bfd_hash_table *table,
9000 const char *string)
9003 /* Allocate the structure if it has not already been allocated by a
9004 subclass. */
9005 if (entry == NULL)
9007 entry = bfd_hash_allocate (table,
9008 sizeof (struct local_hash_entry));
9009 if (entry == NULL)
9010 return entry;
9013 /* Call the allocation method of the superclass. */
9014 entry = bfd_hash_newfunc (entry, table, string);
9015 if (entry != NULL)
9017 ((struct local_hash_entry *) entry)->count = 0;
9018 ((struct local_hash_entry *) entry)->size = 0;
9021 return entry;
9024 /* This struct is used to pass information to elf_link_output_extsym. */
9026 struct elf_outext_info
9028 bool failed;
9029 bool localsyms;
9030 bool file_sym_done;
9031 struct elf_final_link_info *flinfo;
9035 /* Support for evaluating a complex relocation.
9037 Complex relocations are generalized, self-describing relocations. The
9038 implementation of them consists of two parts: complex symbols, and the
9039 relocations themselves.
9041 The relocations use a reserved elf-wide relocation type code (R_RELC
9042 external / BFD_RELOC_RELC internal) and an encoding of relocation field
9043 information (start bit, end bit, word width, etc) into the addend. This
9044 information is extracted from CGEN-generated operand tables within gas.
9046 Complex symbols are mangled symbols (STT_RELC external / BSF_RELC
9047 internal) representing prefix-notation expressions, including but not
9048 limited to those sorts of expressions normally encoded as addends in the
9049 addend field. The symbol mangling format is:
9051 <node> := <literal>
9052 | <unary-operator> ':' <node>
9053 | <binary-operator> ':' <node> ':' <node>
9056 <literal> := 's' <digits=N> ':' <N character symbol name>
9057 | 'S' <digits=N> ':' <N character section name>
9058 | '#' <hexdigits>
9061 <binary-operator> := as in C
9062 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
9064 static void
9065 set_symbol_value (bfd *bfd_with_globals,
9066 Elf_Internal_Sym *isymbuf,
9067 size_t locsymcount,
9068 size_t symidx,
9069 bfd_vma val)
9071 struct elf_link_hash_entry **sym_hashes;
9072 struct elf_link_hash_entry *h;
9073 size_t extsymoff = locsymcount;
9075 if (symidx < locsymcount)
9077 Elf_Internal_Sym *sym;
9079 sym = isymbuf + symidx;
9080 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
9082 /* It is a local symbol: move it to the
9083 "absolute" section and give it a value. */
9084 sym->st_shndx = SHN_ABS;
9085 sym->st_value = val;
9086 return;
9088 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
9089 extsymoff = 0;
9092 /* It is a global symbol: set its link type
9093 to "defined" and give it a value. */
9095 sym_hashes = elf_sym_hashes (bfd_with_globals);
9096 h = sym_hashes [symidx - extsymoff];
9097 while (h->root.type == bfd_link_hash_indirect
9098 || h->root.type == bfd_link_hash_warning)
9099 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9100 h->root.type = bfd_link_hash_defined;
9101 h->root.u.def.value = val;
9102 h->root.u.def.section = bfd_abs_section_ptr;
9105 static bool
9106 resolve_symbol (const char *name,
9107 bfd *input_bfd,
9108 struct elf_final_link_info *flinfo,
9109 bfd_vma *result,
9110 Elf_Internal_Sym *isymbuf,
9111 size_t locsymcount)
9113 Elf_Internal_Sym *sym;
9114 struct bfd_link_hash_entry *global_entry;
9115 const char *candidate = NULL;
9116 Elf_Internal_Shdr *symtab_hdr;
9117 size_t i;
9119 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
9121 for (i = 0; i < locsymcount; ++ i)
9123 sym = isymbuf + i;
9125 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
9126 continue;
9128 candidate = bfd_elf_string_from_elf_section (input_bfd,
9129 symtab_hdr->sh_link,
9130 sym->st_name);
9131 #ifdef DEBUG
9132 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
9133 name, candidate, (unsigned long) sym->st_value);
9134 #endif
9135 if (candidate && strcmp (candidate, name) == 0)
9137 asection *sec = flinfo->sections [i];
9139 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
9140 *result += sec->output_offset + sec->output_section->vma;
9141 #ifdef DEBUG
9142 printf ("Found symbol with value %8.8lx\n",
9143 (unsigned long) *result);
9144 #endif
9145 return true;
9149 /* Hmm, haven't found it yet. perhaps it is a global. */
9150 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
9151 false, false, true);
9152 if (!global_entry)
9153 return false;
9155 if (global_entry->type == bfd_link_hash_defined
9156 || global_entry->type == bfd_link_hash_defweak)
9158 *result = (global_entry->u.def.value
9159 + global_entry->u.def.section->output_section->vma
9160 + global_entry->u.def.section->output_offset);
9161 #ifdef DEBUG
9162 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
9163 global_entry->root.string, (unsigned long) *result);
9164 #endif
9165 return true;
9168 return false;
9171 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
9172 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
9173 names like "foo.end" which is the end address of section "foo". */
9175 static bool
9176 resolve_section (const char *name,
9177 asection *sections,
9178 bfd_vma *result,
9179 bfd * abfd)
9181 asection *curr;
9182 unsigned int len;
9184 for (curr = sections; curr; curr = curr->next)
9185 if (strcmp (curr->name, name) == 0)
9187 *result = curr->vma;
9188 return true;
9191 /* Hmm. still haven't found it. try pseudo-section names. */
9192 /* FIXME: This could be coded more efficiently... */
9193 for (curr = sections; curr; curr = curr->next)
9195 len = strlen (curr->name);
9196 if (len > strlen (name))
9197 continue;
9199 if (strncmp (curr->name, name, len) == 0)
9201 if (startswith (name + len, ".end"))
9203 *result = (curr->vma
9204 + curr->size / bfd_octets_per_byte (abfd, curr));
9205 return true;
9208 /* Insert more pseudo-section names here, if you like. */
9212 return false;
9215 static void
9216 undefined_reference (const char *reftype, const char *name)
9218 /* xgettext:c-format */
9219 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
9220 reftype, name);
9221 bfd_set_error (bfd_error_bad_value);
9224 static bool
9225 eval_symbol (bfd_vma *result,
9226 const char **symp,
9227 bfd *input_bfd,
9228 struct elf_final_link_info *flinfo,
9229 bfd_vma dot,
9230 Elf_Internal_Sym *isymbuf,
9231 size_t locsymcount,
9232 int signed_p)
9234 size_t len;
9235 size_t symlen;
9236 bfd_vma a;
9237 bfd_vma b;
9238 char symbuf[4096];
9239 const char *sym = *symp;
9240 const char *symend;
9241 bool symbol_is_section = false;
9243 len = strlen (sym);
9244 symend = sym + len;
9246 if (len < 1 || len > sizeof (symbuf))
9248 bfd_set_error (bfd_error_invalid_operation);
9249 return false;
9252 switch (* sym)
9254 case '.':
9255 *result = dot;
9256 *symp = sym + 1;
9257 return true;
9259 case '#':
9260 ++sym;
9261 *result = strtoul (sym, (char **) symp, 16);
9262 return true;
9264 case 'S':
9265 symbol_is_section = true;
9266 /* Fall through. */
9267 case 's':
9268 ++sym;
9269 symlen = strtol (sym, (char **) symp, 10);
9270 sym = *symp + 1; /* Skip the trailing ':'. */
9272 if (symend < sym || symlen + 1 > sizeof (symbuf))
9274 bfd_set_error (bfd_error_invalid_operation);
9275 return false;
9278 memcpy (symbuf, sym, symlen);
9279 symbuf[symlen] = '\0';
9280 *symp = sym + symlen;
9282 /* Is it always possible, with complex symbols, that gas "mis-guessed"
9283 the symbol as a section, or vice-versa. so we're pretty liberal in our
9284 interpretation here; section means "try section first", not "must be a
9285 section", and likewise with symbol. */
9287 if (symbol_is_section)
9289 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
9290 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
9291 isymbuf, locsymcount))
9293 undefined_reference ("section", symbuf);
9294 return false;
9297 else
9299 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
9300 isymbuf, locsymcount)
9301 && !resolve_section (symbuf, flinfo->output_bfd->sections,
9302 result, input_bfd))
9304 undefined_reference ("symbol", symbuf);
9305 return false;
9309 return true;
9311 /* All that remains are operators. */
9313 #define UNARY_OP(op) \
9314 if (startswith (sym, #op)) \
9316 sym += strlen (#op); \
9317 if (*sym == ':') \
9318 ++sym; \
9319 *symp = sym; \
9320 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
9321 isymbuf, locsymcount, signed_p)) \
9322 return false; \
9323 if (signed_p) \
9324 *result = op ((bfd_signed_vma) a); \
9325 else \
9326 *result = op a; \
9327 return true; \
9330 #define BINARY_OP_HEAD(op) \
9331 if (startswith (sym, #op)) \
9333 sym += strlen (#op); \
9334 if (*sym == ':') \
9335 ++sym; \
9336 *symp = sym; \
9337 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
9338 isymbuf, locsymcount, signed_p)) \
9339 return false; \
9340 ++*symp; \
9341 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
9342 isymbuf, locsymcount, signed_p)) \
9343 return false;
9344 #define BINARY_OP_TAIL(op) \
9345 if (signed_p) \
9346 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
9347 else \
9348 *result = a op b; \
9349 return true; \
9351 #define BINARY_OP(op) BINARY_OP_HEAD(op) BINARY_OP_TAIL(op)
9353 default:
9354 UNARY_OP (0-);
9355 BINARY_OP_HEAD (<<);
9356 if (b >= sizeof (a) * CHAR_BIT)
9358 *result = 0;
9359 return true;
9361 signed_p = 0;
9362 BINARY_OP_TAIL (<<);
9363 BINARY_OP_HEAD (>>);
9364 if (b >= sizeof (a) * CHAR_BIT)
9366 *result = signed_p && (bfd_signed_vma) a < 0 ? -1 : 0;
9367 return true;
9369 BINARY_OP_TAIL (>>);
9370 BINARY_OP (==);
9371 BINARY_OP (!=);
9372 BINARY_OP (<=);
9373 BINARY_OP (>=);
9374 BINARY_OP (&&);
9375 BINARY_OP (||);
9376 UNARY_OP (~);
9377 UNARY_OP (!);
9378 BINARY_OP (*);
9379 BINARY_OP_HEAD (/);
9380 if (b == 0)
9382 _bfd_error_handler (_("division by zero"));
9383 bfd_set_error (bfd_error_bad_value);
9384 return false;
9386 BINARY_OP_TAIL (/);
9387 BINARY_OP_HEAD (%);
9388 if (b == 0)
9390 _bfd_error_handler (_("division by zero"));
9391 bfd_set_error (bfd_error_bad_value);
9392 return false;
9394 BINARY_OP_TAIL (%);
9395 BINARY_OP (^);
9396 BINARY_OP (|);
9397 BINARY_OP (&);
9398 BINARY_OP (+);
9399 BINARY_OP (-);
9400 BINARY_OP (<);
9401 BINARY_OP (>);
9402 #undef UNARY_OP
9403 #undef BINARY_OP
9404 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
9405 bfd_set_error (bfd_error_invalid_operation);
9406 return false;
9410 static void
9411 put_value (bfd_vma size,
9412 unsigned long chunksz,
9413 bfd *input_bfd,
9414 bfd_vma x,
9415 bfd_byte *location)
9417 location += (size - chunksz);
9419 for (; size; size -= chunksz, location -= chunksz)
9421 switch (chunksz)
9423 case 1:
9424 bfd_put_8 (input_bfd, x, location);
9425 x >>= 8;
9426 break;
9427 case 2:
9428 bfd_put_16 (input_bfd, x, location);
9429 x >>= 16;
9430 break;
9431 case 4:
9432 bfd_put_32 (input_bfd, x, location);
9433 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
9434 x >>= 16;
9435 x >>= 16;
9436 break;
9437 #ifdef BFD64
9438 case 8:
9439 bfd_put_64 (input_bfd, x, location);
9440 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
9441 x >>= 32;
9442 x >>= 32;
9443 break;
9444 #endif
9445 default:
9446 abort ();
9447 break;
9452 static bfd_vma
9453 get_value (bfd_vma size,
9454 unsigned long chunksz,
9455 bfd *input_bfd,
9456 bfd_byte *location)
9458 int shift;
9459 bfd_vma x = 0;
9461 /* Sanity checks. */
9462 BFD_ASSERT (chunksz <= sizeof (x)
9463 && size >= chunksz
9464 && chunksz != 0
9465 && (size % chunksz) == 0
9466 && input_bfd != NULL
9467 && location != NULL);
9469 if (chunksz == sizeof (x))
9471 BFD_ASSERT (size == chunksz);
9473 /* Make sure that we do not perform an undefined shift operation.
9474 We know that size == chunksz so there will only be one iteration
9475 of the loop below. */
9476 shift = 0;
9478 else
9479 shift = 8 * chunksz;
9481 for (; size; size -= chunksz, location += chunksz)
9483 switch (chunksz)
9485 case 1:
9486 x = (x << shift) | bfd_get_8 (input_bfd, location);
9487 break;
9488 case 2:
9489 x = (x << shift) | bfd_get_16 (input_bfd, location);
9490 break;
9491 case 4:
9492 x = (x << shift) | bfd_get_32 (input_bfd, location);
9493 break;
9494 #ifdef BFD64
9495 case 8:
9496 x = (x << shift) | bfd_get_64 (input_bfd, location);
9497 break;
9498 #endif
9499 default:
9500 abort ();
9503 return x;
9506 static void
9507 decode_complex_addend (unsigned long *start, /* in bits */
9508 unsigned long *oplen, /* in bits */
9509 unsigned long *len, /* in bits */
9510 unsigned long *wordsz, /* in bytes */
9511 unsigned long *chunksz, /* in bytes */
9512 unsigned long *lsb0_p,
9513 unsigned long *signed_p,
9514 unsigned long *trunc_p,
9515 unsigned long encoded)
9517 * start = encoded & 0x3F;
9518 * len = (encoded >> 6) & 0x3F;
9519 * oplen = (encoded >> 12) & 0x3F;
9520 * wordsz = (encoded >> 18) & 0xF;
9521 * chunksz = (encoded >> 22) & 0xF;
9522 * lsb0_p = (encoded >> 27) & 1;
9523 * signed_p = (encoded >> 28) & 1;
9524 * trunc_p = (encoded >> 29) & 1;
9527 bfd_reloc_status_type
9528 bfd_elf_perform_complex_relocation (bfd *input_bfd,
9529 asection *input_section,
9530 bfd_byte *contents,
9531 Elf_Internal_Rela *rel,
9532 bfd_vma relocation)
9534 bfd_vma shift, x, mask;
9535 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
9536 bfd_reloc_status_type r;
9537 bfd_size_type octets;
9539 /* Perform this reloc, since it is complex.
9540 (this is not to say that it necessarily refers to a complex
9541 symbol; merely that it is a self-describing CGEN based reloc.
9542 i.e. the addend has the complete reloc information (bit start, end,
9543 word size, etc) encoded within it.). */
9545 decode_complex_addend (&start, &oplen, &len, &wordsz,
9546 &chunksz, &lsb0_p, &signed_p,
9547 &trunc_p, rel->r_addend);
9549 mask = (((1L << (len - 1)) - 1) << 1) | 1;
9551 if (lsb0_p)
9552 shift = (start + 1) - len;
9553 else
9554 shift = (8 * wordsz) - (start + len);
9556 octets = rel->r_offset * bfd_octets_per_byte (input_bfd, input_section);
9557 x = get_value (wordsz, chunksz, input_bfd, contents + octets);
9559 #ifdef DEBUG
9560 printf ("Doing complex reloc: "
9561 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
9562 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
9563 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
9564 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9565 oplen, (unsigned long) x, (unsigned long) mask,
9566 (unsigned long) relocation);
9567 #endif
9569 r = bfd_reloc_ok;
9570 if (! trunc_p)
9571 /* Now do an overflow check. */
9572 r = bfd_check_overflow ((signed_p
9573 ? complain_overflow_signed
9574 : complain_overflow_unsigned),
9575 len, 0, (8 * wordsz),
9576 relocation);
9578 /* Do the deed. */
9579 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
9581 #ifdef DEBUG
9582 printf (" relocation: %8.8lx\n"
9583 " shifted mask: %8.8lx\n"
9584 " shifted/masked reloc: %8.8lx\n"
9585 " result: %8.8lx\n",
9586 (unsigned long) relocation, (unsigned long) (mask << shift),
9587 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
9588 #endif
9589 put_value (wordsz, chunksz, input_bfd, x, contents + octets);
9590 return r;
9593 /* Functions to read r_offset from external (target order) reloc
9594 entry. Faster than bfd_getl32 et al, because we let the compiler
9595 know the value is aligned. */
9597 static bfd_vma
9598 ext32l_r_offset (const void *p)
9600 union aligned32
9602 uint32_t v;
9603 unsigned char c[4];
9605 const union aligned32 *a
9606 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9608 uint32_t aval = ( (uint32_t) a->c[0]
9609 | (uint32_t) a->c[1] << 8
9610 | (uint32_t) a->c[2] << 16
9611 | (uint32_t) a->c[3] << 24);
9612 return aval;
9615 static bfd_vma
9616 ext32b_r_offset (const void *p)
9618 union aligned32
9620 uint32_t v;
9621 unsigned char c[4];
9623 const union aligned32 *a
9624 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9626 uint32_t aval = ( (uint32_t) a->c[0] << 24
9627 | (uint32_t) a->c[1] << 16
9628 | (uint32_t) a->c[2] << 8
9629 | (uint32_t) a->c[3]);
9630 return aval;
9633 static bfd_vma
9634 ext64l_r_offset (const void *p)
9636 union aligned64
9638 uint64_t v;
9639 unsigned char c[8];
9641 const union aligned64 *a
9642 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9644 uint64_t aval = ( (uint64_t) a->c[0]
9645 | (uint64_t) a->c[1] << 8
9646 | (uint64_t) a->c[2] << 16
9647 | (uint64_t) a->c[3] << 24
9648 | (uint64_t) a->c[4] << 32
9649 | (uint64_t) a->c[5] << 40
9650 | (uint64_t) a->c[6] << 48
9651 | (uint64_t) a->c[7] << 56);
9652 return aval;
9655 static bfd_vma
9656 ext64b_r_offset (const void *p)
9658 union aligned64
9660 uint64_t v;
9661 unsigned char c[8];
9663 const union aligned64 *a
9664 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9666 uint64_t aval = ( (uint64_t) a->c[0] << 56
9667 | (uint64_t) a->c[1] << 48
9668 | (uint64_t) a->c[2] << 40
9669 | (uint64_t) a->c[3] << 32
9670 | (uint64_t) a->c[4] << 24
9671 | (uint64_t) a->c[5] << 16
9672 | (uint64_t) a->c[6] << 8
9673 | (uint64_t) a->c[7]);
9674 return aval;
9677 /* When performing a relocatable link, the input relocations are
9678 preserved. But, if they reference global symbols, the indices
9679 referenced must be updated. Update all the relocations found in
9680 RELDATA. */
9682 static bool
9683 elf_link_adjust_relocs (bfd *abfd,
9684 asection *sec,
9685 struct bfd_elf_section_reloc_data *reldata,
9686 bool sort,
9687 struct bfd_link_info *info)
9689 unsigned int i;
9690 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9691 bfd_byte *erela;
9692 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9693 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9694 bfd_vma r_type_mask;
9695 int r_sym_shift;
9696 unsigned int count = reldata->count;
9697 struct elf_link_hash_entry **rel_hash = reldata->hashes;
9699 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
9701 swap_in = bed->s->swap_reloc_in;
9702 swap_out = bed->s->swap_reloc_out;
9704 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
9706 swap_in = bed->s->swap_reloca_in;
9707 swap_out = bed->s->swap_reloca_out;
9709 else
9710 abort ();
9712 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
9713 abort ();
9715 if (bed->s->arch_size == 32)
9717 r_type_mask = 0xff;
9718 r_sym_shift = 8;
9720 else
9722 r_type_mask = 0xffffffff;
9723 r_sym_shift = 32;
9726 erela = reldata->hdr->contents;
9727 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
9729 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
9730 unsigned int j;
9732 if (*rel_hash == NULL)
9733 continue;
9735 if ((*rel_hash)->indx == -2
9736 && info->gc_sections
9737 && ! info->gc_keep_exported)
9739 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9740 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
9741 abfd, sec,
9742 (*rel_hash)->root.root.string);
9743 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
9744 abfd, sec);
9745 bfd_set_error (bfd_error_invalid_operation);
9746 return false;
9748 BFD_ASSERT ((*rel_hash)->indx >= 0);
9750 (*swap_in) (abfd, erela, irela);
9751 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
9752 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
9753 | (irela[j].r_info & r_type_mask));
9754 (*swap_out) (abfd, irela, erela);
9757 if (bed->elf_backend_update_relocs)
9758 (*bed->elf_backend_update_relocs) (sec, reldata);
9760 if (sort && count != 0)
9762 bfd_vma (*ext_r_off) (const void *);
9763 bfd_vma r_off;
9764 size_t elt_size;
9765 bfd_byte *base, *end, *p, *loc;
9766 bfd_byte *buf = NULL;
9768 if (bed->s->arch_size == 32)
9770 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9771 ext_r_off = ext32l_r_offset;
9772 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9773 ext_r_off = ext32b_r_offset;
9774 else
9775 abort ();
9777 else
9779 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9780 ext_r_off = ext64l_r_offset;
9781 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9782 ext_r_off = ext64b_r_offset;
9783 else
9784 abort ();
9787 /* Must use a stable sort here. A modified insertion sort,
9788 since the relocs are mostly sorted already. */
9789 elt_size = reldata->hdr->sh_entsize;
9790 base = reldata->hdr->contents;
9791 end = base + count * elt_size;
9792 if (elt_size > sizeof (Elf64_External_Rela))
9793 abort ();
9795 /* Ensure the first element is lowest. This acts as a sentinel,
9796 speeding the main loop below. */
9797 r_off = (*ext_r_off) (base);
9798 for (p = loc = base; (p += elt_size) < end; )
9800 bfd_vma r_off2 = (*ext_r_off) (p);
9801 if (r_off > r_off2)
9803 r_off = r_off2;
9804 loc = p;
9807 if (loc != base)
9809 /* Don't just swap *base and *loc as that changes the order
9810 of the original base[0] and base[1] if they happen to
9811 have the same r_offset. */
9812 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
9813 memcpy (onebuf, loc, elt_size);
9814 memmove (base + elt_size, base, loc - base);
9815 memcpy (base, onebuf, elt_size);
9818 for (p = base + elt_size; (p += elt_size) < end; )
9820 /* base to p is sorted, *p is next to insert. */
9821 r_off = (*ext_r_off) (p);
9822 /* Search the sorted region for location to insert. */
9823 loc = p - elt_size;
9824 while (r_off < (*ext_r_off) (loc))
9825 loc -= elt_size;
9826 loc += elt_size;
9827 if (loc != p)
9829 /* Chances are there is a run of relocs to insert here,
9830 from one of more input files. Files are not always
9831 linked in order due to the way elf_link_input_bfd is
9832 called. See pr17666. */
9833 size_t sortlen = p - loc;
9834 bfd_vma r_off2 = (*ext_r_off) (loc);
9835 size_t runlen = elt_size;
9836 bfd_vma r_off_runend = r_off;
9837 bfd_vma r_off_runend_next;
9838 size_t buf_size = 96 * 1024;
9839 while (p + runlen < end
9840 && (sortlen <= buf_size
9841 || runlen + elt_size <= buf_size)
9842 /* run must not break the ordering of base..loc+1 */
9843 && r_off2 > (r_off_runend_next = (*ext_r_off) (p + runlen))
9844 /* run must be already sorted */
9845 && r_off_runend_next >= r_off_runend)
9847 runlen += elt_size;
9848 r_off_runend = r_off_runend_next;
9850 if (buf == NULL)
9852 buf = bfd_malloc (buf_size);
9853 if (buf == NULL)
9854 return false;
9856 if (runlen < sortlen)
9858 memcpy (buf, p, runlen);
9859 memmove (loc + runlen, loc, sortlen);
9860 memcpy (loc, buf, runlen);
9862 else
9864 memcpy (buf, loc, sortlen);
9865 memmove (loc, p, runlen);
9866 memcpy (loc + runlen, buf, sortlen);
9868 p += runlen - elt_size;
9871 /* Hashes are no longer valid. */
9872 free (reldata->hashes);
9873 reldata->hashes = NULL;
9874 free (buf);
9876 return true;
9879 struct elf_link_sort_rela
9881 union {
9882 bfd_vma offset;
9883 bfd_vma sym_mask;
9884 } u;
9885 enum elf_reloc_type_class type;
9886 /* We use this as an array of size int_rels_per_ext_rel. */
9887 Elf_Internal_Rela rela[1];
9890 /* qsort stability here and for cmp2 is only an issue if multiple
9891 dynamic relocations are emitted at the same address. But targets
9892 that apply a series of dynamic relocations each operating on the
9893 result of the prior relocation can't use -z combreloc as
9894 implemented anyway. Such schemes tend to be broken by sorting on
9895 symbol index. That leaves dynamic NONE relocs as the only other
9896 case where ld might emit multiple relocs at the same address, and
9897 those are only emitted due to target bugs. */
9899 static int
9900 elf_link_sort_cmp1 (const void *A, const void *B)
9902 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9903 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9904 int relativea, relativeb;
9906 relativea = a->type == reloc_class_relative;
9907 relativeb = b->type == reloc_class_relative;
9909 if (relativea < relativeb)
9910 return 1;
9911 if (relativea > relativeb)
9912 return -1;
9913 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
9914 return -1;
9915 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
9916 return 1;
9917 if (a->rela->r_offset < b->rela->r_offset)
9918 return -1;
9919 if (a->rela->r_offset > b->rela->r_offset)
9920 return 1;
9921 return 0;
9924 static int
9925 elf_link_sort_cmp2 (const void *A, const void *B)
9927 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9928 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9930 if (a->type < b->type)
9931 return -1;
9932 if (a->type > b->type)
9933 return 1;
9934 if (a->u.offset < b->u.offset)
9935 return -1;
9936 if (a->u.offset > b->u.offset)
9937 return 1;
9938 if (a->rela->r_offset < b->rela->r_offset)
9939 return -1;
9940 if (a->rela->r_offset > b->rela->r_offset)
9941 return 1;
9942 return 0;
9945 static size_t
9946 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9948 asection *dynamic_relocs;
9949 asection *rela_dyn;
9950 asection *rel_dyn;
9951 bfd_size_type count, size;
9952 size_t i, ret, sort_elt, ext_size;
9953 bfd_byte *sort, *s_non_relative, *p;
9954 struct elf_link_sort_rela *sq;
9955 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9956 int i2e = bed->s->int_rels_per_ext_rel;
9957 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
9958 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9959 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9960 struct bfd_link_order *lo;
9961 bfd_vma r_sym_mask;
9962 bool use_rela;
9964 /* Find a dynamic reloc section. */
9965 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9966 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
9967 if (rela_dyn != NULL && rela_dyn->size > 0
9968 && rel_dyn != NULL && rel_dyn->size > 0)
9970 bool use_rela_initialised = false;
9972 /* This is just here to stop gcc from complaining.
9973 Its initialization checking code is not perfect. */
9974 use_rela = true;
9976 /* Both sections are present. Examine the sizes
9977 of the indirect sections to help us choose. */
9978 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9979 if (lo->type == bfd_indirect_link_order)
9981 asection *o = lo->u.indirect.section;
9983 if ((o->size % bed->s->sizeof_rela) == 0)
9985 if ((o->size % bed->s->sizeof_rel) == 0)
9986 /* Section size is divisible by both rel and rela sizes.
9987 It is of no help to us. */
9989 else
9991 /* Section size is only divisible by rela. */
9992 if (use_rela_initialised && !use_rela)
9994 _bfd_error_handler (_("%pB: unable to sort relocs - "
9995 "they are in more than one size"),
9996 abfd);
9997 bfd_set_error (bfd_error_invalid_operation);
9998 return 0;
10000 else
10002 use_rela = true;
10003 use_rela_initialised = true;
10007 else if ((o->size % bed->s->sizeof_rel) == 0)
10009 /* Section size is only divisible by rel. */
10010 if (use_rela_initialised && use_rela)
10012 _bfd_error_handler (_("%pB: unable to sort relocs - "
10013 "they are in more than one size"),
10014 abfd);
10015 bfd_set_error (bfd_error_invalid_operation);
10016 return 0;
10018 else
10020 use_rela = false;
10021 use_rela_initialised = true;
10024 else
10026 /* The section size is not divisible by either -
10027 something is wrong. */
10028 _bfd_error_handler (_("%pB: unable to sort relocs - "
10029 "they are of an unknown size"), abfd);
10030 bfd_set_error (bfd_error_invalid_operation);
10031 return 0;
10035 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
10036 if (lo->type == bfd_indirect_link_order)
10038 asection *o = lo->u.indirect.section;
10040 if ((o->size % bed->s->sizeof_rela) == 0)
10042 if ((o->size % bed->s->sizeof_rel) == 0)
10043 /* Section size is divisible by both rel and rela sizes.
10044 It is of no help to us. */
10046 else
10048 /* Section size is only divisible by rela. */
10049 if (use_rela_initialised && !use_rela)
10051 _bfd_error_handler (_("%pB: unable to sort relocs - "
10052 "they are in more than one size"),
10053 abfd);
10054 bfd_set_error (bfd_error_invalid_operation);
10055 return 0;
10057 else
10059 use_rela = true;
10060 use_rela_initialised = true;
10064 else if ((o->size % bed->s->sizeof_rel) == 0)
10066 /* Section size is only divisible by rel. */
10067 if (use_rela_initialised && use_rela)
10069 _bfd_error_handler (_("%pB: unable to sort relocs - "
10070 "they are in more than one size"),
10071 abfd);
10072 bfd_set_error (bfd_error_invalid_operation);
10073 return 0;
10075 else
10077 use_rela = false;
10078 use_rela_initialised = true;
10081 else
10083 /* The section size is not divisible by either -
10084 something is wrong. */
10085 _bfd_error_handler (_("%pB: unable to sort relocs - "
10086 "they are of an unknown size"), abfd);
10087 bfd_set_error (bfd_error_invalid_operation);
10088 return 0;
10092 if (! use_rela_initialised)
10093 /* Make a guess. */
10094 use_rela = true;
10096 else if (rela_dyn != NULL && rela_dyn->size > 0)
10097 use_rela = true;
10098 else if (rel_dyn != NULL && rel_dyn->size > 0)
10099 use_rela = false;
10100 else
10101 return 0;
10103 if (use_rela)
10105 dynamic_relocs = rela_dyn;
10106 ext_size = bed->s->sizeof_rela;
10107 swap_in = bed->s->swap_reloca_in;
10108 swap_out = bed->s->swap_reloca_out;
10110 else
10112 dynamic_relocs = rel_dyn;
10113 ext_size = bed->s->sizeof_rel;
10114 swap_in = bed->s->swap_reloc_in;
10115 swap_out = bed->s->swap_reloc_out;
10118 size = 0;
10119 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
10120 if (lo->type == bfd_indirect_link_order)
10121 size += lo->u.indirect.section->size;
10123 if (size != dynamic_relocs->size)
10124 return 0;
10126 sort_elt = (sizeof (struct elf_link_sort_rela)
10127 + (i2e - 1) * sizeof (Elf_Internal_Rela));
10129 count = dynamic_relocs->size / ext_size;
10130 if (count == 0)
10131 return 0;
10132 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
10134 if (sort == NULL)
10136 (*info->callbacks->warning)
10137 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
10138 return 0;
10141 if (bed->s->arch_size == 32)
10142 r_sym_mask = ~(bfd_vma) 0xff;
10143 else
10144 r_sym_mask = ~(bfd_vma) 0xffffffff;
10146 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
10147 if (lo->type == bfd_indirect_link_order)
10149 bfd_byte *erel, *erelend;
10150 asection *o = lo->u.indirect.section;
10152 if (o->contents == NULL && o->size != 0)
10154 /* This is a reloc section that is being handled as a normal
10155 section. See bfd_section_from_shdr. We can't combine
10156 relocs in this case. */
10157 free (sort);
10158 return 0;
10160 erel = o->contents;
10161 erelend = o->contents + o->size;
10162 p = sort + o->output_offset * opb / ext_size * sort_elt;
10164 while (erel < erelend)
10166 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
10168 (*swap_in) (abfd, erel, s->rela);
10169 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
10170 s->u.sym_mask = r_sym_mask;
10171 p += sort_elt;
10172 erel += ext_size;
10176 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
10178 for (i = 0, p = sort; i < count; i++, p += sort_elt)
10180 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
10181 if (s->type != reloc_class_relative)
10182 break;
10184 ret = i;
10185 s_non_relative = p;
10187 sq = (struct elf_link_sort_rela *) s_non_relative;
10188 for (; i < count; i++, p += sort_elt)
10190 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
10191 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
10192 sq = sp;
10193 sp->u.offset = sq->rela->r_offset;
10196 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
10198 struct elf_link_hash_table *htab = elf_hash_table (info);
10199 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
10201 /* We have plt relocs in .rela.dyn. */
10202 sq = (struct elf_link_sort_rela *) sort;
10203 for (i = 0; i < count; i++)
10204 if (sq[count - i - 1].type != reloc_class_plt)
10205 break;
10206 if (i != 0 && htab->srelplt->size == i * ext_size)
10208 struct bfd_link_order **plo;
10209 /* Put srelplt link_order last. This is so the output_offset
10210 set in the next loop is correct for DT_JMPREL. */
10211 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
10212 if ((*plo)->type == bfd_indirect_link_order
10213 && (*plo)->u.indirect.section == htab->srelplt)
10215 lo = *plo;
10216 *plo = lo->next;
10218 else
10219 plo = &(*plo)->next;
10220 *plo = lo;
10221 lo->next = NULL;
10222 dynamic_relocs->map_tail.link_order = lo;
10226 p = sort;
10227 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
10228 if (lo->type == bfd_indirect_link_order)
10230 bfd_byte *erel, *erelend;
10231 asection *o = lo->u.indirect.section;
10233 erel = o->contents;
10234 erelend = o->contents + o->size;
10235 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
10236 while (erel < erelend)
10238 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
10239 (*swap_out) (abfd, s->rela, erel);
10240 p += sort_elt;
10241 erel += ext_size;
10245 free (sort);
10246 *psec = dynamic_relocs;
10247 return ret;
10250 /* Add a symbol to the output symbol string table. */
10252 static int
10253 elf_link_output_symstrtab (void *finf,
10254 const char *name,
10255 Elf_Internal_Sym *elfsym,
10256 asection *input_sec,
10257 struct elf_link_hash_entry *h)
10259 struct elf_final_link_info *flinfo = finf;
10260 int (*output_symbol_hook)
10261 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
10262 struct elf_link_hash_entry *);
10263 struct elf_link_hash_table *hash_table;
10264 const struct elf_backend_data *bed;
10265 bfd_size_type strtabsize;
10267 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
10269 bed = get_elf_backend_data (flinfo->output_bfd);
10270 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
10271 if (output_symbol_hook != NULL)
10273 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
10274 if (ret != 1)
10275 return ret;
10278 if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC)
10279 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
10280 if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE)
10281 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique;
10283 if (name == NULL || *name == '\0')
10284 elfsym->st_name = (unsigned long) -1;
10285 else
10287 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
10288 to get the final offset for st_name. */
10289 char *versioned_name = (char *) name;
10290 if (h != NULL)
10292 if (h->versioned == versioned && h->def_dynamic)
10294 /* Keep only one '@' for versioned symbols defined in
10295 shared objects. */
10296 char *version = strrchr (name, ELF_VER_CHR);
10297 char *base_end = strchr (name, ELF_VER_CHR);
10298 if (version != base_end)
10300 size_t base_len;
10301 size_t len = strlen (name);
10302 versioned_name = bfd_alloc (flinfo->output_bfd, len);
10303 if (versioned_name == NULL)
10304 return 0;
10305 base_len = base_end - name;
10306 memcpy (versioned_name, name, base_len);
10307 memcpy (versioned_name + base_len, version,
10308 len - base_len);
10312 else if (flinfo->info->unique_symbol
10313 && ELF_ST_BIND (elfsym->st_info) == STB_LOCAL)
10315 struct local_hash_entry *lh;
10316 size_t count_len;
10317 size_t base_len;
10318 char buf[30];
10319 switch (ELF_ST_TYPE (elfsym->st_info))
10321 case STT_FILE:
10322 case STT_SECTION:
10323 break;
10324 default:
10325 lh = (struct local_hash_entry *) bfd_hash_lookup
10326 (&flinfo->local_hash_table, name, true, false);
10327 if (lh == NULL)
10328 return 0;
10329 /* Always append ".COUNT" to local symbols to avoid
10330 potential conflicts with local symbol "XXX.COUNT". */
10331 sprintf (buf, "%lx", lh->count);
10332 base_len = lh->size;
10333 if (!base_len)
10335 base_len = strlen (name);
10336 lh->size = base_len;
10338 count_len = strlen (buf);
10339 versioned_name = bfd_alloc (flinfo->output_bfd,
10340 base_len + count_len + 2);
10341 if (versioned_name == NULL)
10342 return 0;
10343 memcpy (versioned_name, name, base_len);
10344 versioned_name[base_len] = '.';
10345 memcpy (versioned_name + base_len + 1, buf,
10346 count_len + 1);
10347 lh->count++;
10348 break;
10351 elfsym->st_name
10352 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
10353 versioned_name, false);
10354 if (elfsym->st_name == (unsigned long) -1)
10355 return 0;
10358 hash_table = elf_hash_table (flinfo->info);
10359 strtabsize = hash_table->strtabsize;
10360 if (strtabsize <= flinfo->output_bfd->symcount)
10362 strtabsize += strtabsize;
10363 hash_table->strtabsize = strtabsize;
10364 strtabsize *= sizeof (*hash_table->strtab);
10365 hash_table->strtab
10366 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
10367 strtabsize);
10368 if (hash_table->strtab == NULL)
10369 return 0;
10371 hash_table->strtab[flinfo->output_bfd->symcount].sym = *elfsym;
10372 hash_table->strtab[flinfo->output_bfd->symcount].dest_index
10373 = flinfo->output_bfd->symcount;
10374 flinfo->output_bfd->symcount += 1;
10376 return 1;
10379 /* Swap symbols out to the symbol table and flush the output symbols to
10380 the file. */
10382 static bool
10383 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
10385 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
10386 size_t amt;
10387 size_t i;
10388 const struct elf_backend_data *bed;
10389 bfd_byte *symbuf;
10390 Elf_Internal_Shdr *hdr;
10391 file_ptr pos;
10392 bool ret;
10394 if (flinfo->output_bfd->symcount == 0)
10395 return true;
10397 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
10399 bed = get_elf_backend_data (flinfo->output_bfd);
10401 amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount;
10402 symbuf = (bfd_byte *) bfd_malloc (amt);
10403 if (symbuf == NULL)
10404 return false;
10406 if (flinfo->symshndxbuf)
10408 amt = sizeof (Elf_External_Sym_Shndx);
10409 amt *= bfd_get_symcount (flinfo->output_bfd);
10410 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
10411 if (flinfo->symshndxbuf == NULL)
10413 free (symbuf);
10414 return false;
10418 /* Now swap out the symbols. */
10419 for (i = 0; i < flinfo->output_bfd->symcount; i++)
10421 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
10422 if (elfsym->sym.st_name == (unsigned long) -1)
10423 elfsym->sym.st_name = 0;
10424 else
10425 elfsym->sym.st_name
10426 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
10427 elfsym->sym.st_name);
10429 /* Inform the linker of the addition of this symbol. */
10431 if (flinfo->info->callbacks->ctf_new_symbol)
10432 flinfo->info->callbacks->ctf_new_symbol (elfsym->dest_index,
10433 &elfsym->sym);
10435 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
10436 ((bfd_byte *) symbuf
10437 + (elfsym->dest_index
10438 * bed->s->sizeof_sym)),
10439 NPTR_ADD (flinfo->symshndxbuf,
10440 elfsym->dest_index));
10443 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
10444 pos = hdr->sh_offset + hdr->sh_size;
10445 amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount;
10446 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
10447 && bfd_write (symbuf, amt, flinfo->output_bfd) == amt)
10449 hdr->sh_size += amt;
10450 ret = true;
10452 else
10453 ret = false;
10455 free (symbuf);
10457 free (hash_table->strtab);
10458 hash_table->strtab = NULL;
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;
12578 bool ret;
12580 if (!is_elf_hash_table (&htab->root))
12581 return false;
12583 if (bfd_link_pic (info))
12584 abfd->flags |= DYNAMIC;
12586 dynamic = htab->dynamic_sections_created;
12587 dynobj = htab->dynobj;
12589 emit_relocs = (bfd_link_relocatable (info)
12590 || info->emitrelocations);
12592 memset (&flinfo, 0, sizeof (flinfo));
12593 flinfo.info = info;
12594 flinfo.output_bfd = abfd;
12595 flinfo.symstrtab = _bfd_elf_strtab_init ();
12596 if (flinfo.symstrtab == NULL)
12597 return false;
12599 if (! dynamic)
12601 flinfo.hash_sec = NULL;
12602 flinfo.symver_sec = NULL;
12604 else
12606 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
12607 /* Note that dynsym_sec can be NULL (on VMS). */
12608 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
12609 /* Note that it is OK if symver_sec is NULL. */
12612 if (info->unique_symbol
12613 && !bfd_hash_table_init (&flinfo.local_hash_table,
12614 local_hash_newfunc,
12615 sizeof (struct local_hash_entry)))
12616 return false;
12618 /* The object attributes have been merged. Remove the input
12619 sections from the link, and set the contents of the output
12620 section. */
12621 sections_removed = false;
12622 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
12623 for (o = abfd->sections; o != NULL; o = o->next)
12625 bool remove_section = false;
12627 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
12628 || strcmp (o->name, ".gnu.attributes") == 0)
12630 for (p = o->map_head.link_order; p != NULL; p = p->next)
12632 asection *input_section;
12634 if (p->type != bfd_indirect_link_order)
12635 continue;
12636 input_section = p->u.indirect.section;
12637 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12638 elf_link_input_bfd ignores this section. */
12639 input_section->flags &= ~SEC_HAS_CONTENTS;
12642 attr_size = bfd_elf_obj_attr_size (abfd);
12643 bfd_set_section_size (o, attr_size);
12644 /* Skip this section later on. */
12645 o->map_head.link_order = NULL;
12646 if (attr_size)
12647 attr_section = o;
12648 else
12649 remove_section = true;
12651 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
12653 /* Remove empty group section from linker output. */
12654 remove_section = true;
12656 if (remove_section)
12658 o->flags |= SEC_EXCLUDE;
12659 bfd_section_list_remove (abfd, o);
12660 abfd->section_count--;
12661 sections_removed = true;
12664 if (sections_removed)
12665 _bfd_fix_excluded_sec_syms (abfd, info);
12667 /* Count up the number of relocations we will output for each output
12668 section, so that we know the sizes of the reloc sections. We
12669 also figure out some maximum sizes. */
12670 #ifdef USE_MMAP
12671 if (bed->use_mmap)
12673 /* Mmap is used only if section size >= the minimum mmap section
12674 size. The initial max_contents_size value covers all sections
12675 smaller than the minimum mmap section size. It may be increased
12676 for compressed or linker created sections or sections whose
12677 rawsize != size. max_external_reloc_size covers all relocation
12678 sections smaller than the minimum mmap section size. */
12679 max_contents_size = _bfd_minimum_mmap_size;
12680 max_external_reloc_size = _bfd_minimum_mmap_size;
12682 else
12683 #endif
12685 max_contents_size = 0;
12686 max_external_reloc_size = 0;
12688 max_internal_reloc_count = 0;
12689 max_sym_count = 0;
12690 max_sym_shndx_count = 0;
12691 merged = false;
12692 for (o = abfd->sections; o != NULL; o = o->next)
12694 struct bfd_elf_section_data *esdo = elf_section_data (o);
12695 o->reloc_count = 0;
12697 for (p = o->map_head.link_order; p != NULL; p = p->next)
12699 unsigned int reloc_count = 0;
12700 unsigned int additional_reloc_count = 0;
12701 struct bfd_elf_section_data *esdi = NULL;
12703 if (p->type == bfd_section_reloc_link_order
12704 || p->type == bfd_symbol_reloc_link_order)
12705 reloc_count = 1;
12706 else if (p->type == bfd_indirect_link_order)
12708 asection *sec;
12710 sec = p->u.indirect.section;
12712 /* Mark all sections which are to be included in the
12713 link. This will normally be every section. We need
12714 to do this so that we can identify any sections which
12715 the linker has decided to not include. */
12716 sec->linker_mark = true;
12718 if (sec->flags & SEC_MERGE)
12719 merged = true;
12721 #ifdef USE_MMAP
12722 /* Mmap is used only on non-compressed, non-linker created
12723 sections whose rawsize == size. */
12724 if (!bed->use_mmap
12725 || sec->compress_status != COMPRESS_SECTION_NONE
12726 || (sec->flags & SEC_LINKER_CREATED) != 0
12727 || sec->rawsize != sec->size)
12728 #endif
12730 if (sec->rawsize > max_contents_size)
12731 max_contents_size = sec->rawsize;
12732 if (sec->size > max_contents_size)
12733 max_contents_size = sec->size;
12736 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
12737 && (sec->owner->flags & DYNAMIC) == 0)
12739 size_t sym_count;
12741 /* We are interested in just local symbols, not all
12742 symbols. */
12743 if (elf_bad_symtab (sec->owner))
12744 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
12745 / bed->s->sizeof_sym);
12746 else
12747 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
12749 if (sym_count > max_sym_count)
12750 max_sym_count = sym_count;
12752 if (sym_count > max_sym_shndx_count
12753 && elf_symtab_shndx_list (sec->owner) != NULL)
12754 max_sym_shndx_count = sym_count;
12756 esdi = elf_section_data (sec);
12758 if (esdi->this_hdr.sh_type == SHT_REL
12759 || esdi->this_hdr.sh_type == SHT_RELA)
12760 /* Some backends use reloc_count in relocation sections
12761 to count particular types of relocs. Of course,
12762 reloc sections themselves can't have relocations. */
12764 else if (emit_relocs)
12766 reloc_count = sec->reloc_count;
12767 if (bed->elf_backend_count_additional_relocs)
12769 int c;
12770 c = (*bed->elf_backend_count_additional_relocs) (sec);
12771 additional_reloc_count += c;
12774 else if (bed->elf_backend_count_relocs)
12775 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
12777 if ((sec->flags & SEC_RELOC) != 0)
12779 #ifdef USE_MMAP
12780 if (!bed->use_mmap)
12781 #endif
12783 size_t ext_size = 0;
12785 if (esdi->rel.hdr != NULL)
12786 ext_size = esdi->rel.hdr->sh_size;
12787 if (esdi->rela.hdr != NULL)
12788 ext_size += esdi->rela.hdr->sh_size;
12790 if (ext_size > max_external_reloc_size)
12791 max_external_reloc_size = ext_size;
12793 if (sec->reloc_count > max_internal_reloc_count)
12794 max_internal_reloc_count = sec->reloc_count;
12799 if (reloc_count == 0)
12800 continue;
12802 reloc_count += additional_reloc_count;
12803 o->reloc_count += reloc_count;
12805 if (p->type == bfd_indirect_link_order && emit_relocs)
12807 if (esdi->rel.hdr)
12809 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
12810 esdo->rel.count += additional_reloc_count;
12812 if (esdi->rela.hdr)
12814 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
12815 esdo->rela.count += additional_reloc_count;
12818 else
12820 if (o->use_rela_p)
12821 esdo->rela.count += reloc_count;
12822 else
12823 esdo->rel.count += reloc_count;
12827 if (o->reloc_count > 0)
12828 o->flags |= SEC_RELOC;
12829 else
12831 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12832 set it (this is probably a bug) and if it is set
12833 assign_section_numbers will create a reloc section. */
12834 o->flags &=~ SEC_RELOC;
12837 /* If the SEC_ALLOC flag is not set, force the section VMA to
12838 zero. This is done in elf_fake_sections as well, but forcing
12839 the VMA to 0 here will ensure that relocs against these
12840 sections are handled correctly. */
12841 if ((o->flags & SEC_ALLOC) == 0
12842 && ! o->user_set_vma)
12843 o->vma = 0;
12846 if (! bfd_link_relocatable (info) && merged)
12847 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
12849 /* Figure out the file positions for everything but the symbol table
12850 and the relocs. We set symcount to force assign_section_numbers
12851 to create a symbol table. */
12852 abfd->symcount = info->strip != strip_all || emit_relocs;
12853 BFD_ASSERT (! abfd->output_has_begun);
12854 if (! _bfd_elf_compute_section_file_positions (abfd, info))
12855 goto error_return;
12857 /* Set sizes, and assign file positions for reloc sections. */
12858 for (o = abfd->sections; o != NULL; o = o->next)
12860 struct bfd_elf_section_data *esdo = elf_section_data (o);
12861 if ((o->flags & SEC_RELOC) != 0)
12863 if (esdo->rel.hdr
12864 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
12865 goto error_return;
12867 if (esdo->rela.hdr
12868 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
12869 goto error_return;
12872 /* _bfd_elf_compute_section_file_positions makes temporary use
12873 of target_index. Reset it. */
12874 o->target_index = 0;
12876 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12877 to count upwards while actually outputting the relocations. */
12878 esdo->rel.count = 0;
12879 esdo->rela.count = 0;
12881 if ((esdo->this_hdr.sh_offset == (file_ptr) -1)
12882 && !bfd_section_is_ctf (o))
12884 /* Cache the section contents so that they can be compressed
12885 later. Use bfd_malloc since it will be freed by
12886 bfd_compress_section_contents. */
12887 unsigned char *contents = esdo->this_hdr.contents;
12888 if (contents != NULL)
12889 abort ();
12890 contents
12891 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
12892 if (contents == NULL)
12893 goto error_return;
12894 esdo->this_hdr.contents = contents;
12898 /* We have now assigned file positions for all the sections except .symtab,
12899 .strtab, and non-loaded reloc and compressed debugging sections. We start
12900 the .symtab section at the current file position, and write directly to it.
12901 We build the .strtab section in memory. */
12902 abfd->symcount = 0;
12903 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12904 /* sh_name is set in prep_headers. */
12905 symtab_hdr->sh_type = SHT_SYMTAB;
12906 /* sh_flags, sh_addr and sh_size all start off zero. */
12907 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
12908 /* sh_link is set in assign_section_numbers. */
12909 /* sh_info is set below. */
12910 /* sh_offset is set just below. */
12911 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
12913 if (max_sym_count < 20)
12914 max_sym_count = 20;
12915 htab->strtabsize = max_sym_count;
12916 amt = max_sym_count * sizeof (struct elf_sym_strtab);
12917 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
12918 if (htab->strtab == NULL)
12919 goto error_return;
12920 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12921 flinfo.symshndxbuf
12922 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
12923 ? (Elf_External_Sym_Shndx *) -1 : NULL);
12925 if (info->strip != strip_all || emit_relocs)
12927 file_ptr off = elf_next_file_pos (abfd);
12929 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, true, 0);
12931 /* Note that at this point elf_next_file_pos (abfd) is
12932 incorrect. We do not yet know the size of the .symtab section.
12933 We correct next_file_pos below, after we do know the size. */
12935 /* Start writing out the symbol table. The first symbol is always a
12936 dummy symbol. */
12937 elfsym.st_value = 0;
12938 elfsym.st_size = 0;
12939 elfsym.st_info = 0;
12940 elfsym.st_other = 0;
12941 elfsym.st_shndx = SHN_UNDEF;
12942 elfsym.st_target_internal = 0;
12943 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
12944 bfd_und_section_ptr, NULL) != 1)
12945 goto error_return;
12947 /* Output a symbol for each section if asked or they are used for
12948 relocs. These symbols usually have no names. We store the
12949 index of each one in the index field of the section, so that
12950 we can find it again when outputting relocs. */
12952 if (bfd_keep_unused_section_symbols (abfd) || emit_relocs)
12954 bool name_local_sections
12955 = (bed->elf_backend_name_local_section_symbols
12956 && bed->elf_backend_name_local_section_symbols (abfd));
12957 const char *name = NULL;
12959 elfsym.st_size = 0;
12960 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12961 elfsym.st_other = 0;
12962 elfsym.st_value = 0;
12963 elfsym.st_target_internal = 0;
12964 for (i = 1; i < elf_numsections (abfd); i++)
12966 o = bfd_section_from_elf_index (abfd, i);
12967 if (o != NULL)
12969 o->target_index = bfd_get_symcount (abfd);
12970 elfsym.st_shndx = i;
12971 if (!bfd_link_relocatable (info))
12972 elfsym.st_value = o->vma;
12973 if (name_local_sections)
12974 name = o->name;
12975 if (elf_link_output_symstrtab (&flinfo, name, &elfsym, o,
12976 NULL) != 1)
12977 goto error_return;
12983 /* On some targets like Irix 5 the symbol split between local and global
12984 ones recorded in the sh_info field needs to be done between section
12985 and all other symbols. */
12986 if (bed->elf_backend_elfsym_local_is_section
12987 && bed->elf_backend_elfsym_local_is_section (abfd))
12988 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12990 /* Allocate some memory to hold information read in from the input
12991 files. */
12992 if (max_contents_size != 0)
12994 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12995 if (flinfo.contents == NULL)
12996 goto error_return;
12999 if (max_external_reloc_size != 0)
13001 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
13002 if (flinfo.external_relocs == NULL)
13003 goto error_return;
13006 if (max_internal_reloc_count != 0)
13008 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
13009 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
13010 if (flinfo.internal_relocs == NULL)
13011 goto error_return;
13014 if (max_sym_count != 0)
13016 amt = max_sym_count * bed->s->sizeof_sym;
13017 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
13018 if (flinfo.external_syms == NULL)
13019 goto error_return;
13021 amt = max_sym_count * sizeof (Elf_Internal_Sym);
13022 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
13023 if (flinfo.internal_syms == NULL)
13024 goto error_return;
13026 amt = max_sym_count * sizeof (long);
13027 flinfo.indices = (long int *) bfd_malloc (amt);
13028 if (flinfo.indices == NULL)
13029 goto error_return;
13031 amt = max_sym_count * sizeof (asection *);
13032 flinfo.sections = (asection **) bfd_malloc (amt);
13033 if (flinfo.sections == NULL)
13034 goto error_return;
13037 if (max_sym_shndx_count != 0)
13039 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
13040 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
13041 if (flinfo.locsym_shndx == NULL)
13042 goto error_return;
13045 if (htab->tls_sec)
13047 bfd_vma base, end = 0; /* Both bytes. */
13048 asection *sec;
13050 for (sec = htab->tls_sec;
13051 sec && (sec->flags & SEC_THREAD_LOCAL);
13052 sec = sec->next)
13054 bfd_size_type size = sec->size;
13055 unsigned int opb = bfd_octets_per_byte (abfd, sec);
13057 if (size == 0
13058 && (sec->flags & SEC_HAS_CONTENTS) == 0)
13060 struct bfd_link_order *ord = sec->map_tail.link_order;
13062 if (ord != NULL)
13063 size = ord->offset * opb + ord->size;
13065 end = sec->vma + size / opb;
13067 base = htab->tls_sec->vma;
13068 /* Only align end of TLS section if static TLS doesn't have special
13069 alignment requirements. */
13070 if (bed->static_tls_alignment == 1)
13071 end = align_power (end, htab->tls_sec->alignment_power);
13072 htab->tls_size = end - base;
13075 if (!_bfd_elf_fixup_eh_frame_hdr (info))
13076 return false;
13078 /* Finish relative relocations here after regular symbol processing
13079 is finished if DT_RELR is enabled. */
13080 if (info->enable_dt_relr
13081 && bed->finish_relative_relocs
13082 && !bed->finish_relative_relocs (info))
13083 info->callbacks->einfo
13084 (_("%F%P: %pB: failed to finish relative relocations\n"), abfd);
13086 /* Since ELF permits relocations to be against local symbols, we
13087 must have the local symbols available when we do the relocations.
13088 Since we would rather only read the local symbols once, and we
13089 would rather not keep them in memory, we handle all the
13090 relocations for a single input file at the same time.
13092 Unfortunately, there is no way to know the total number of local
13093 symbols until we have seen all of them, and the local symbol
13094 indices precede the global symbol indices. This means that when
13095 we are generating relocatable output, and we see a reloc against
13096 a global symbol, we can not know the symbol index until we have
13097 finished examining all the local symbols to see which ones we are
13098 going to output. To deal with this, we keep the relocations in
13099 memory, and don't output them until the end of the link. This is
13100 an unfortunate waste of memory, but I don't see a good way around
13101 it. Fortunately, it only happens when performing a relocatable
13102 link, which is not the common case. FIXME: If keep_memory is set
13103 we could write the relocs out and then read them again; I don't
13104 know how bad the memory loss will be. */
13106 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13107 sub->output_has_begun = false;
13108 for (o = abfd->sections; o != NULL; o = o->next)
13110 for (p = o->map_head.link_order; p != NULL; p = p->next)
13112 if (p->type == bfd_indirect_link_order
13113 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
13114 == bfd_target_elf_flavour)
13115 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
13117 if (! sub->output_has_begun)
13119 if (! elf_link_input_bfd (&flinfo, sub))
13120 goto error_return;
13121 sub->output_has_begun = true;
13124 else if (p->type == bfd_section_reloc_link_order
13125 || p->type == bfd_symbol_reloc_link_order)
13127 if (! elf_reloc_link_order (abfd, info, o, p))
13128 goto error_return;
13130 else
13132 if (! _bfd_default_link_order (abfd, info, o, p))
13134 if (p->type == bfd_indirect_link_order
13135 && (bfd_get_flavour (sub)
13136 == bfd_target_elf_flavour)
13137 && (elf_elfheader (sub)->e_ident[EI_CLASS]
13138 != bed->s->elfclass))
13140 const char *iclass, *oclass;
13142 switch (bed->s->elfclass)
13144 case ELFCLASS64: oclass = "ELFCLASS64"; break;
13145 case ELFCLASS32: oclass = "ELFCLASS32"; break;
13146 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
13147 default: abort ();
13150 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
13152 case ELFCLASS64: iclass = "ELFCLASS64"; break;
13153 case ELFCLASS32: iclass = "ELFCLASS32"; break;
13154 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
13155 default: abort ();
13158 bfd_set_error (bfd_error_wrong_format);
13159 _bfd_error_handler
13160 /* xgettext:c-format */
13161 (_("%pB: file class %s incompatible with %s"),
13162 sub, iclass, oclass);
13165 goto error_return;
13171 /* Free symbol buffer if needed. */
13172 if (!info->reduce_memory_overheads)
13174 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13175 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
13177 free (elf_tdata (sub)->symbuf);
13178 elf_tdata (sub)->symbuf = NULL;
13182 ret = true;
13184 /* Output any global symbols that got converted to local in a
13185 version script or due to symbol visibility. We do this in a
13186 separate step since ELF requires all local symbols to appear
13187 prior to any global symbols. FIXME: We should only do this if
13188 some global symbols were, in fact, converted to become local.
13189 FIXME: Will this work correctly with the Irix 5 linker? */
13190 eoinfo.failed = false;
13191 eoinfo.flinfo = &flinfo;
13192 eoinfo.localsyms = true;
13193 eoinfo.file_sym_done = false;
13194 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
13195 if (eoinfo.failed)
13197 ret = false;
13198 goto return_local_hash_table;
13201 /* If backend needs to output some local symbols not present in the hash
13202 table, do it now. */
13203 if (bed->elf_backend_output_arch_local_syms)
13205 if (! ((*bed->elf_backend_output_arch_local_syms)
13206 (abfd, info, &flinfo, elf_link_output_symstrtab)))
13208 ret = false;
13209 goto return_local_hash_table;
13213 /* That wrote out all the local symbols. Finish up the symbol table
13214 with the global symbols. Even if we want to strip everything we
13215 can, we still need to deal with those global symbols that got
13216 converted to local in a version script. */
13218 /* The sh_info field records the index of the first non local symbol. */
13219 if (!symtab_hdr->sh_info)
13220 symtab_hdr->sh_info = bfd_get_symcount (abfd);
13222 if (dynamic
13223 && htab->dynsym != NULL
13224 && htab->dynsym->output_section != bfd_abs_section_ptr)
13226 Elf_Internal_Sym sym;
13227 bfd_byte *dynsym = htab->dynsym->contents;
13229 o = htab->dynsym->output_section;
13230 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
13232 /* Write out the section symbols for the output sections. */
13233 if (bfd_link_pic (info)
13234 || htab->is_relocatable_executable)
13236 asection *s;
13238 sym.st_size = 0;
13239 sym.st_name = 0;
13240 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
13241 sym.st_other = 0;
13242 sym.st_target_internal = 0;
13244 for (s = abfd->sections; s != NULL; s = s->next)
13246 int indx;
13247 bfd_byte *dest;
13248 long dynindx;
13250 dynindx = elf_section_data (s)->dynindx;
13251 if (dynindx <= 0)
13252 continue;
13253 indx = elf_section_data (s)->this_idx;
13254 BFD_ASSERT (indx > 0);
13255 sym.st_shndx = indx;
13256 if (! check_dynsym (abfd, &sym))
13258 ret = false;
13259 goto return_local_hash_table;
13261 sym.st_value = s->vma;
13262 dest = dynsym + dynindx * bed->s->sizeof_sym;
13264 /* Inform the linker of the addition of this symbol. */
13266 if (info->callbacks->ctf_new_dynsym)
13267 info->callbacks->ctf_new_dynsym (dynindx, &sym);
13269 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
13273 /* Write out the local dynsyms. */
13274 if (htab->dynlocal)
13276 struct elf_link_local_dynamic_entry *e;
13277 for (e = htab->dynlocal; e ; e = e->next)
13279 asection *s;
13280 bfd_byte *dest;
13282 /* Copy the internal symbol and turn off visibility.
13283 Note that we saved a word of storage and overwrote
13284 the original st_name with the dynstr_index. */
13285 sym = e->isym;
13286 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
13287 sym.st_shndx = SHN_UNDEF;
13289 s = bfd_section_from_elf_index (e->input_bfd,
13290 e->isym.st_shndx);
13291 if (s != NULL
13292 && s->output_section != NULL
13293 && elf_section_data (s->output_section) != NULL)
13295 sym.st_shndx =
13296 elf_section_data (s->output_section)->this_idx;
13297 if (! check_dynsym (abfd, &sym))
13299 ret = false;
13300 goto return_local_hash_table;
13302 sym.st_value = (s->output_section->vma
13303 + s->output_offset
13304 + e->isym.st_value);
13307 /* Inform the linker of the addition of this symbol. */
13309 if (info->callbacks->ctf_new_dynsym)
13310 info->callbacks->ctf_new_dynsym (e->dynindx, &sym);
13312 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
13313 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
13318 /* We get the global symbols from the hash table. */
13319 eoinfo.failed = false;
13320 eoinfo.localsyms = false;
13321 eoinfo.flinfo = &flinfo;
13322 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
13323 if (eoinfo.failed)
13325 ret = false;
13326 goto return_local_hash_table;
13329 /* If backend needs to output some symbols not present in the hash
13330 table, do it now. */
13331 if (bed->elf_backend_output_arch_syms
13332 && (info->strip != strip_all || emit_relocs))
13334 if (! ((*bed->elf_backend_output_arch_syms)
13335 (abfd, info, &flinfo, elf_link_output_symstrtab)))
13337 ret = false;
13338 goto return_local_hash_table;
13342 /* Finalize the .strtab section. */
13343 _bfd_elf_strtab_finalize (flinfo.symstrtab);
13345 /* Swap out the .strtab section. */
13346 if (!elf_link_swap_symbols_out (&flinfo))
13348 ret = false;
13349 goto return_local_hash_table;
13352 /* Now we know the size of the symtab section. */
13353 if (bfd_get_symcount (abfd) > 0)
13355 /* Finish up and write out the symbol string table (.strtab)
13356 section. */
13357 Elf_Internal_Shdr *symstrtab_hdr = NULL;
13358 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
13360 if (elf_symtab_shndx_list (abfd))
13362 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
13364 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
13366 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
13367 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
13368 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
13369 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
13370 symtab_shndx_hdr->sh_size = amt;
13372 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
13373 off, true, 0);
13375 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
13376 || (bfd_write (flinfo.symshndxbuf, amt, abfd) != amt))
13378 ret = false;
13379 goto return_local_hash_table;
13384 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
13385 /* sh_name was set in prep_headers. */
13386 symstrtab_hdr->sh_type = SHT_STRTAB;
13387 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
13388 symstrtab_hdr->sh_addr = 0;
13389 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
13390 symstrtab_hdr->sh_entsize = 0;
13391 symstrtab_hdr->sh_link = 0;
13392 symstrtab_hdr->sh_info = 0;
13393 /* sh_offset is set just below. */
13394 symstrtab_hdr->sh_addralign = 1;
13396 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
13397 off, true, 0);
13398 elf_next_file_pos (abfd) = off;
13400 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
13401 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
13403 ret = false;
13404 goto return_local_hash_table;
13408 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
13410 _bfd_error_handler (_("%pB: failed to generate import library"),
13411 info->out_implib_bfd);
13412 ret = false;
13413 goto return_local_hash_table;
13416 /* Adjust the relocs to have the correct symbol indices. */
13417 for (o = abfd->sections; o != NULL; o = o->next)
13419 struct bfd_elf_section_data *esdo = elf_section_data (o);
13420 bool sort;
13422 if ((o->flags & SEC_RELOC) == 0)
13423 continue;
13425 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
13426 if (esdo->rel.hdr != NULL
13427 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
13429 ret = false;
13430 goto return_local_hash_table;
13432 if (esdo->rela.hdr != NULL
13433 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
13435 ret = false;
13436 goto return_local_hash_table;
13439 /* Set the reloc_count field to 0 to prevent write_relocs from
13440 trying to swap the relocs out itself. */
13441 o->reloc_count = 0;
13444 relativecount = 0;
13445 if (dynamic && info->combreloc && dynobj != NULL)
13446 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
13448 relr_entsize = 0;
13449 if (htab->srelrdyn != NULL
13450 && htab->srelrdyn->output_section != NULL
13451 && htab->srelrdyn->size != 0)
13453 asection *s = htab->srelrdyn->output_section;
13454 relr_entsize = elf_section_data (s)->this_hdr.sh_entsize;
13455 if (relr_entsize == 0)
13457 relr_entsize = bed->s->arch_size / 8;
13458 elf_section_data (s)->this_hdr.sh_entsize = relr_entsize;
13462 /* If we are linking against a dynamic object, or generating a
13463 shared library, finish up the dynamic linking information. */
13464 if (dynamic)
13466 bfd_byte *dyncon, *dynconend;
13468 /* Fix up .dynamic entries. */
13469 o = htab->dynamic;
13470 BFD_ASSERT (o != NULL);
13472 dyncon = o->contents;
13473 dynconend = PTR_ADD (o->contents, o->size);
13474 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13476 Elf_Internal_Dyn dyn;
13477 const char *name;
13478 unsigned int type;
13479 bfd_size_type sh_size;
13480 bfd_vma sh_addr;
13482 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13484 switch (dyn.d_tag)
13486 default:
13487 continue;
13488 case DT_NULL:
13489 if (relativecount != 0)
13491 switch (elf_section_data (reldyn)->this_hdr.sh_type)
13493 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
13494 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
13496 if (dyn.d_tag != DT_NULL
13497 && dynconend - dyncon >= bed->s->sizeof_dyn)
13499 dyn.d_un.d_val = relativecount;
13500 relativecount = 0;
13501 break;
13503 relativecount = 0;
13505 if (relr_entsize != 0)
13507 if (dynconend - dyncon >= 3 * bed->s->sizeof_dyn)
13509 asection *s = htab->srelrdyn;
13510 dyn.d_tag = DT_RELR;
13511 dyn.d_un.d_ptr
13512 = s->output_section->vma + s->output_offset;
13513 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13514 dyncon += bed->s->sizeof_dyn;
13516 dyn.d_tag = DT_RELRSZ;
13517 dyn.d_un.d_val = s->size;
13518 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13519 dyncon += bed->s->sizeof_dyn;
13521 dyn.d_tag = DT_RELRENT;
13522 dyn.d_un.d_val = relr_entsize;
13523 relr_entsize = 0;
13524 break;
13526 relr_entsize = 0;
13528 continue;
13530 case DT_INIT:
13531 name = info->init_function;
13532 goto get_sym;
13533 case DT_FINI:
13534 name = info->fini_function;
13535 get_sym:
13537 struct elf_link_hash_entry *h;
13539 h = elf_link_hash_lookup (htab, name, false, false, true);
13540 if (h != NULL
13541 && (h->root.type == bfd_link_hash_defined
13542 || h->root.type == bfd_link_hash_defweak))
13544 dyn.d_un.d_ptr = h->root.u.def.value;
13545 o = h->root.u.def.section;
13546 if (o->output_section != NULL)
13547 dyn.d_un.d_ptr += (o->output_section->vma
13548 + o->output_offset);
13549 else
13551 /* The symbol is imported from another shared
13552 library and does not apply to this one. */
13553 dyn.d_un.d_ptr = 0;
13555 break;
13558 continue;
13560 case DT_PREINIT_ARRAYSZ:
13561 name = ".preinit_array";
13562 goto get_out_size;
13563 case DT_INIT_ARRAYSZ:
13564 name = ".init_array";
13565 goto get_out_size;
13566 case DT_FINI_ARRAYSZ:
13567 name = ".fini_array";
13568 get_out_size:
13569 o = bfd_get_section_by_name (abfd, name);
13570 if (o == NULL)
13572 _bfd_error_handler
13573 (_("could not find section %s"), name);
13574 goto error_return;
13576 if (o->size == 0)
13577 _bfd_error_handler
13578 (_("warning: %s section has zero size"), name);
13579 dyn.d_un.d_val = o->size;
13580 break;
13582 case DT_PREINIT_ARRAY:
13583 name = ".preinit_array";
13584 goto get_out_vma;
13585 case DT_INIT_ARRAY:
13586 name = ".init_array";
13587 goto get_out_vma;
13588 case DT_FINI_ARRAY:
13589 name = ".fini_array";
13590 get_out_vma:
13591 o = bfd_get_section_by_name (abfd, name);
13592 goto do_vma;
13594 case DT_HASH:
13595 name = ".hash";
13596 goto get_vma;
13597 case DT_GNU_HASH:
13598 name = ".gnu.hash";
13599 goto get_vma;
13600 case DT_STRTAB:
13601 name = ".dynstr";
13602 goto get_vma;
13603 case DT_SYMTAB:
13604 name = ".dynsym";
13605 goto get_vma;
13606 case DT_VERDEF:
13607 name = ".gnu.version_d";
13608 goto get_vma;
13609 case DT_VERNEED:
13610 name = ".gnu.version_r";
13611 goto get_vma;
13612 case DT_VERSYM:
13613 name = ".gnu.version";
13614 get_vma:
13615 o = bfd_get_linker_section (dynobj, name);
13616 do_vma:
13617 if (o == NULL || bfd_is_abs_section (o->output_section))
13619 _bfd_error_handler
13620 (_("could not find section %s"), name);
13621 goto error_return;
13623 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
13625 _bfd_error_handler
13626 (_("warning: section '%s' is being made into a note"), name);
13627 bfd_set_error (bfd_error_nonrepresentable_section);
13628 goto error_return;
13630 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
13631 break;
13633 case DT_REL:
13634 case DT_RELA:
13635 case DT_RELSZ:
13636 case DT_RELASZ:
13637 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
13638 type = SHT_REL;
13639 else
13640 type = SHT_RELA;
13641 sh_size = 0;
13642 sh_addr = 0;
13643 for (i = 1; i < elf_numsections (abfd); i++)
13645 Elf_Internal_Shdr *hdr;
13647 hdr = elf_elfsections (abfd)[i];
13648 if (hdr->sh_type == type
13649 && (hdr->sh_flags & SHF_ALLOC) != 0)
13651 sh_size += hdr->sh_size;
13652 if (sh_addr == 0
13653 || sh_addr > hdr->sh_addr)
13654 sh_addr = hdr->sh_addr;
13658 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
13660 unsigned int opb = bfd_octets_per_byte (abfd, o);
13662 /* Don't count procedure linkage table relocs in the
13663 overall reloc count. */
13664 sh_size -= htab->srelplt->size;
13665 if (sh_size == 0)
13666 /* If the size is zero, make the address zero too.
13667 This is to avoid a glibc bug. If the backend
13668 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
13669 zero, then we'll put DT_RELA at the end of
13670 DT_JMPREL. glibc will interpret the end of
13671 DT_RELA matching the end of DT_JMPREL as the
13672 case where DT_RELA includes DT_JMPREL, and for
13673 LD_BIND_NOW will decide that processing DT_RELA
13674 will process the PLT relocs too. Net result:
13675 No PLT relocs applied. */
13676 sh_addr = 0;
13678 /* If .rela.plt is the first .rela section, exclude
13679 it from DT_RELA. */
13680 else if (sh_addr == (htab->srelplt->output_section->vma
13681 + htab->srelplt->output_offset) * opb)
13682 sh_addr += htab->srelplt->size;
13685 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
13686 dyn.d_un.d_val = sh_size;
13687 else
13688 dyn.d_un.d_ptr = sh_addr;
13689 break;
13691 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13695 /* If we have created any dynamic sections, then output them. */
13696 if (dynobj != NULL)
13698 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
13699 goto error_return;
13701 /* Check for DT_TEXTREL (late, in case the backend removes it). */
13702 if (bfd_link_textrel_check (info)
13703 && (o = htab->dynamic) != NULL
13704 && o->size != 0)
13706 bfd_byte *dyncon, *dynconend;
13708 dyncon = o->contents;
13709 dynconend = o->contents + o->size;
13710 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13712 Elf_Internal_Dyn dyn;
13714 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13716 if (dyn.d_tag == DT_TEXTREL)
13718 if (info->textrel_check == textrel_check_error)
13719 info->callbacks->einfo
13720 (_("%P%X: read-only segment has dynamic relocations\n"));
13721 else if (bfd_link_dll (info))
13722 info->callbacks->einfo
13723 (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
13724 else if (bfd_link_pde (info))
13725 info->callbacks->einfo
13726 (_("%P: warning: creating DT_TEXTREL in a PDE\n"));
13727 else
13728 info->callbacks->einfo
13729 (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
13730 break;
13735 for (o = dynobj->sections; o != NULL; o = o->next)
13737 if ((o->flags & SEC_HAS_CONTENTS) == 0
13738 || o->size == 0
13739 || o->output_section == bfd_abs_section_ptr)
13740 continue;
13741 if ((o->flags & SEC_LINKER_CREATED) == 0)
13743 /* At this point, we are only interested in sections
13744 created by _bfd_elf_link_create_dynamic_sections. */
13745 continue;
13747 if (htab->stab_info.stabstr == o)
13748 continue;
13749 if (htab->eh_info.hdr_sec == o)
13750 continue;
13751 if (strcmp (o->name, ".dynstr") != 0)
13753 bfd_size_type octets = ((file_ptr) o->output_offset
13754 * bfd_octets_per_byte (abfd, o));
13755 if (!bfd_set_section_contents (abfd, o->output_section,
13756 o->contents, octets, o->size))
13757 goto error_return;
13759 else
13761 /* The contents of the .dynstr section are actually in a
13762 stringtab. */
13763 file_ptr off;
13765 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
13766 if (bfd_seek (abfd, off, SEEK_SET) != 0
13767 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
13768 goto error_return;
13773 if (!info->resolve_section_groups)
13775 bool failed = false;
13777 BFD_ASSERT (bfd_link_relocatable (info));
13778 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
13779 if (failed)
13780 goto error_return;
13783 /* If we have optimized stabs strings, output them. */
13784 if (htab->stab_info.stabstr != NULL)
13786 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
13787 goto error_return;
13790 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
13791 goto error_return;
13793 if (! _bfd_elf_write_section_sframe (abfd, info))
13794 goto error_return;
13796 if (info->callbacks->emit_ctf)
13797 info->callbacks->emit_ctf ();
13799 elf_final_link_free (abfd, &flinfo);
13801 if (attr_section)
13803 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
13804 if (contents == NULL)
13806 /* Bail out and fail. */
13807 ret = false;
13808 goto return_local_hash_table;
13810 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
13811 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
13812 free (contents);
13815 return_local_hash_table:
13816 if (info->unique_symbol)
13817 bfd_hash_table_free (&flinfo.local_hash_table);
13818 return ret;
13820 error_return:
13821 elf_final_link_free (abfd, &flinfo);
13822 ret = false;
13823 goto return_local_hash_table;
13826 /* Initialize COOKIE for input bfd ABFD. */
13828 static bool
13829 init_reloc_cookie (struct elf_reloc_cookie *cookie,
13830 struct bfd_link_info *info, bfd *abfd,
13831 bool keep_memory)
13833 Elf_Internal_Shdr *symtab_hdr;
13834 const struct elf_backend_data *bed;
13836 bed = get_elf_backend_data (abfd);
13837 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13839 cookie->abfd = abfd;
13840 cookie->sym_hashes = elf_sym_hashes (abfd);
13841 cookie->bad_symtab = elf_bad_symtab (abfd);
13842 if (cookie->bad_symtab)
13844 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13845 cookie->extsymoff = 0;
13847 else
13849 cookie->locsymcount = symtab_hdr->sh_info;
13850 cookie->extsymoff = symtab_hdr->sh_info;
13853 if (bed->s->arch_size == 32)
13854 cookie->r_sym_shift = 8;
13855 else
13856 cookie->r_sym_shift = 32;
13858 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
13859 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
13861 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
13862 cookie->locsymcount, 0,
13863 NULL, NULL, NULL);
13864 if (cookie->locsyms == NULL)
13866 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
13867 return false;
13869 if (keep_memory || _bfd_elf_link_keep_memory (info))
13871 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
13872 info->cache_size += (cookie->locsymcount
13873 * sizeof (Elf_External_Sym_Shndx));
13876 return true;
13879 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
13881 static void
13882 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
13884 Elf_Internal_Shdr *symtab_hdr;
13886 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13887 if (symtab_hdr->contents != (unsigned char *) cookie->locsyms)
13888 free (cookie->locsyms);
13891 /* Initialize the relocation information in COOKIE for input section SEC
13892 of input bfd ABFD. */
13894 static bool
13895 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13896 struct bfd_link_info *info, bfd *abfd,
13897 asection *sec, bool keep_memory)
13899 if (sec->reloc_count == 0)
13901 cookie->rels = NULL;
13902 cookie->relend = NULL;
13904 else
13906 cookie->rels = _bfd_elf_link_info_read_relocs
13907 (abfd, info, sec, NULL, NULL,
13908 keep_memory || _bfd_elf_link_keep_memory (info));
13909 if (cookie->rels == NULL)
13910 return false;
13911 cookie->rel = cookie->rels;
13912 cookie->relend = cookie->rels + sec->reloc_count;
13914 cookie->rel = cookie->rels;
13915 return true;
13918 /* Free the memory allocated by init_reloc_cookie_rels,
13919 if appropriate. */
13921 static void
13922 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13923 asection *sec)
13925 if (elf_section_data (sec)->relocs != cookie->rels)
13926 free (cookie->rels);
13929 /* Initialize the whole of COOKIE for input section SEC. */
13931 static bool
13932 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13933 struct bfd_link_info *info,
13934 asection *sec, bool keep_memory)
13936 if (!init_reloc_cookie (cookie, info, sec->owner, keep_memory))
13937 goto error1;
13938 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec,
13939 keep_memory))
13940 goto error2;
13941 return true;
13943 error2:
13944 fini_reloc_cookie (cookie, sec->owner);
13945 error1:
13946 return false;
13949 /* Free the memory allocated by init_reloc_cookie_for_section,
13950 if appropriate. */
13952 static void
13953 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13954 asection *sec)
13956 fini_reloc_cookie_rels (cookie, sec);
13957 fini_reloc_cookie (cookie, sec->owner);
13960 /* Garbage collect unused sections. */
13962 /* Default gc_mark_hook. */
13964 asection *
13965 _bfd_elf_gc_mark_hook (asection *sec,
13966 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13967 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13968 struct elf_link_hash_entry *h,
13969 Elf_Internal_Sym *sym)
13971 if (h != NULL)
13973 switch (h->root.type)
13975 case bfd_link_hash_defined:
13976 case bfd_link_hash_defweak:
13977 return h->root.u.def.section;
13979 case bfd_link_hash_common:
13980 return h->root.u.c.p->section;
13982 default:
13983 break;
13986 else
13987 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
13989 return NULL;
13992 /* Return the debug definition section. */
13994 static asection *
13995 elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
13996 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13997 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13998 struct elf_link_hash_entry *h,
13999 Elf_Internal_Sym *sym)
14001 if (h != NULL)
14003 /* Return the global debug definition section. */
14004 if ((h->root.type == bfd_link_hash_defined
14005 || h->root.type == bfd_link_hash_defweak)
14006 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
14007 return h->root.u.def.section;
14009 else
14011 /* Return the local debug definition section. */
14012 asection *isec = bfd_section_from_elf_index (sec->owner,
14013 sym->st_shndx);
14014 if (isec != NULL && (isec->flags & SEC_DEBUGGING) != 0)
14015 return isec;
14018 return NULL;
14021 /* COOKIE->rel describes a relocation against section SEC, which is
14022 a section we've decided to keep. Return the section that contains
14023 the relocation symbol, or NULL if no section contains it. */
14025 asection *
14026 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
14027 elf_gc_mark_hook_fn gc_mark_hook,
14028 struct elf_reloc_cookie *cookie,
14029 bool *start_stop)
14031 unsigned long r_symndx;
14032 struct elf_link_hash_entry *h, *hw;
14034 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
14035 if (r_symndx == STN_UNDEF)
14036 return NULL;
14038 if (r_symndx >= cookie->locsymcount
14039 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
14041 bool was_marked;
14043 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
14044 if (h == NULL)
14046 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
14047 sec->owner);
14048 return NULL;
14050 while (h->root.type == bfd_link_hash_indirect
14051 || h->root.type == bfd_link_hash_warning)
14052 h = (struct elf_link_hash_entry *) h->root.u.i.link;
14054 was_marked = h->mark;
14055 h->mark = 1;
14056 /* Keep all aliases of the symbol too. If an object symbol
14057 needs to be copied into .dynbss then all of its aliases
14058 should be present as dynamic symbols, not just the one used
14059 on the copy relocation. */
14060 hw = h;
14061 while (hw->is_weakalias)
14063 hw = hw->u.alias;
14064 hw->mark = 1;
14067 if (!was_marked && h->start_stop && !h->root.ldscript_def)
14069 if (info->start_stop_gc)
14070 return NULL;
14072 /* To work around a glibc bug, mark XXX input sections
14073 when there is a reference to __start_XXX or __stop_XXX
14074 symbols. */
14075 else if (start_stop != NULL)
14077 asection *s = h->u2.start_stop_section;
14078 *start_stop = true;
14079 return s;
14083 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
14086 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
14087 &cookie->locsyms[r_symndx]);
14090 /* COOKIE->rel describes a relocation against section SEC, which is
14091 a section we've decided to keep. Mark the section that contains
14092 the relocation symbol. */
14094 bool
14095 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
14096 asection *sec,
14097 elf_gc_mark_hook_fn gc_mark_hook,
14098 struct elf_reloc_cookie *cookie)
14100 asection *rsec;
14101 bool start_stop = false;
14103 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
14104 while (rsec != NULL)
14106 if (!rsec->gc_mark)
14108 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
14109 || (rsec->owner->flags & DYNAMIC) != 0)
14110 rsec->gc_mark = 1;
14111 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
14112 return false;
14114 if (!start_stop)
14115 break;
14116 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
14118 return true;
14121 /* The mark phase of garbage collection. For a given section, mark
14122 it and any sections in this section's group, and all the sections
14123 which define symbols to which it refers. */
14125 bool
14126 _bfd_elf_gc_mark (struct bfd_link_info *info,
14127 asection *sec,
14128 elf_gc_mark_hook_fn gc_mark_hook)
14130 bool ret;
14131 asection *group_sec, *eh_frame;
14133 sec->gc_mark = 1;
14135 /* Mark all the sections in the group. */
14136 group_sec = elf_section_data (sec)->next_in_group;
14137 if (group_sec && !group_sec->gc_mark)
14138 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
14139 return false;
14141 /* Look through the section relocs. */
14142 ret = true;
14143 eh_frame = elf_eh_frame_section (sec->owner);
14144 if ((sec->flags & SEC_RELOC) != 0
14145 && sec->reloc_count > 0
14146 && sec != eh_frame)
14148 struct elf_reloc_cookie cookie;
14150 if (!init_reloc_cookie_for_section (&cookie, info, sec, false))
14151 ret = false;
14152 else
14154 for (; cookie.rel < cookie.relend; cookie.rel++)
14155 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
14157 ret = false;
14158 break;
14160 fini_reloc_cookie_for_section (&cookie, sec);
14164 if (ret && eh_frame && elf_fde_list (sec))
14166 struct elf_reloc_cookie cookie;
14168 /* NB: When --no-keep-memory is used, the symbol table and
14169 relocation info for eh_frame are freed after they are retrieved
14170 for each text section in the input object. If an input object
14171 has many text sections, the same data is retrieved and freed
14172 many times which can take a very long time. Always keep the
14173 symbol table and relocation info for eh_frame to avoid it. */
14174 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame,
14175 true))
14176 ret = false;
14177 else
14179 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
14180 gc_mark_hook, &cookie))
14181 ret = false;
14182 fini_reloc_cookie_for_section (&cookie, eh_frame);
14186 eh_frame = elf_section_eh_frame_entry (sec);
14187 if (ret && eh_frame && !eh_frame->gc_mark)
14188 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
14189 ret = false;
14191 return ret;
14194 /* Scan and mark sections in a special or debug section group. */
14196 static void
14197 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
14199 /* Point to first section of section group. */
14200 asection *ssec;
14201 /* Used to iterate the section group. */
14202 asection *msec;
14204 bool is_special_grp = true;
14205 bool is_debug_grp = true;
14207 /* First scan to see if group contains any section other than debug
14208 and special section. */
14209 ssec = msec = elf_next_in_group (grp);
14212 if ((msec->flags & SEC_DEBUGGING) == 0)
14213 is_debug_grp = false;
14215 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
14216 is_special_grp = false;
14218 msec = elf_next_in_group (msec);
14220 while (msec != ssec);
14222 /* If this is a pure debug section group or pure special section group,
14223 keep all sections in this group. */
14224 if (is_debug_grp || is_special_grp)
14228 msec->gc_mark = 1;
14229 msec = elf_next_in_group (msec);
14231 while (msec != ssec);
14235 /* Keep debug and special sections. */
14237 bool
14238 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
14239 elf_gc_mark_hook_fn mark_hook)
14241 bfd *ibfd;
14243 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14245 asection *isec;
14246 bool some_kept;
14247 bool debug_frag_seen;
14248 bool has_kept_debug_info;
14250 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
14251 continue;
14252 isec = ibfd->sections;
14253 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14254 continue;
14256 /* Ensure all linker created sections are kept,
14257 see if any other section is already marked,
14258 and note if we have any fragmented debug sections. */
14259 debug_frag_seen = some_kept = has_kept_debug_info = false;
14260 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14262 if ((isec->flags & SEC_LINKER_CREATED) != 0)
14263 isec->gc_mark = 1;
14264 else if (isec->gc_mark
14265 && (isec->flags & SEC_ALLOC) != 0
14266 && elf_section_type (isec) != SHT_NOTE)
14267 some_kept = true;
14268 else
14270 /* Since all sections, except for backend specific ones,
14271 have been garbage collected, call mark_hook on this
14272 section if any of its linked-to sections is marked. */
14273 asection *linked_to_sec;
14274 for (linked_to_sec = elf_linked_to_section (isec);
14275 linked_to_sec != NULL && !linked_to_sec->linker_mark;
14276 linked_to_sec = elf_linked_to_section (linked_to_sec))
14278 if (linked_to_sec->gc_mark)
14280 if (!_bfd_elf_gc_mark (info, isec, mark_hook))
14281 return false;
14282 break;
14284 linked_to_sec->linker_mark = 1;
14286 for (linked_to_sec = elf_linked_to_section (isec);
14287 linked_to_sec != NULL && linked_to_sec->linker_mark;
14288 linked_to_sec = elf_linked_to_section (linked_to_sec))
14289 linked_to_sec->linker_mark = 0;
14292 if (!debug_frag_seen
14293 && (isec->flags & SEC_DEBUGGING)
14294 && startswith (isec->name, ".debug_line."))
14295 debug_frag_seen = true;
14296 else if (strcmp (bfd_section_name (isec),
14297 "__patchable_function_entries") == 0
14298 && elf_linked_to_section (isec) == NULL)
14299 info->callbacks->einfo (_("%F%P: %pB(%pA): error: "
14300 "need linked-to section "
14301 "for --gc-sections\n"),
14302 isec->owner, isec);
14305 /* If no non-note alloc section in this file will be kept, then
14306 we can toss out the debug and special sections. */
14307 if (!some_kept)
14308 continue;
14310 /* Keep debug and special sections like .comment when they are
14311 not part of a group. Also keep section groups that contain
14312 just debug sections or special sections. NB: Sections with
14313 linked-to section has been handled above. */
14314 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14316 if ((isec->flags & SEC_GROUP) != 0)
14317 _bfd_elf_gc_mark_debug_special_section_group (isec);
14318 else if (((isec->flags & SEC_DEBUGGING) != 0
14319 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
14320 && elf_next_in_group (isec) == NULL
14321 && elf_linked_to_section (isec) == NULL)
14322 isec->gc_mark = 1;
14323 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
14324 has_kept_debug_info = true;
14327 /* Look for CODE sections which are going to be discarded,
14328 and find and discard any fragmented debug sections which
14329 are associated with that code section. */
14330 if (debug_frag_seen)
14331 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14332 if ((isec->flags & SEC_CODE) != 0
14333 && isec->gc_mark == 0)
14335 unsigned int ilen;
14336 asection *dsec;
14338 ilen = strlen (isec->name);
14340 /* Association is determined by the name of the debug
14341 section containing the name of the code section as
14342 a suffix. For example .debug_line.text.foo is a
14343 debug section associated with .text.foo. */
14344 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
14346 unsigned int dlen;
14348 if (dsec->gc_mark == 0
14349 || (dsec->flags & SEC_DEBUGGING) == 0)
14350 continue;
14352 dlen = strlen (dsec->name);
14354 if (dlen > ilen
14355 && strncmp (dsec->name + (dlen - ilen),
14356 isec->name, ilen) == 0)
14357 dsec->gc_mark = 0;
14361 /* Mark debug sections referenced by kept debug sections. */
14362 if (has_kept_debug_info)
14363 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14364 if (isec->gc_mark
14365 && (isec->flags & SEC_DEBUGGING) != 0)
14366 if (!_bfd_elf_gc_mark (info, isec,
14367 elf_gc_mark_debug_section))
14368 return false;
14370 return true;
14373 static bool
14374 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
14376 bfd *sub;
14377 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14379 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
14381 asection *o;
14383 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
14384 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
14385 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
14386 continue;
14387 o = sub->sections;
14388 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14389 continue;
14391 for (o = sub->sections; o != NULL; o = o->next)
14393 /* When any section in a section group is kept, we keep all
14394 sections in the section group. If the first member of
14395 the section group is excluded, we will also exclude the
14396 group section. */
14397 if (o->flags & SEC_GROUP)
14399 asection *first = elf_next_in_group (o);
14400 o->gc_mark = first->gc_mark;
14403 if (o->gc_mark)
14404 continue;
14406 /* Skip sweeping sections already excluded. */
14407 if (o->flags & SEC_EXCLUDE)
14408 continue;
14410 /* Since this is early in the link process, it is simple
14411 to remove a section from the output. */
14412 o->flags |= SEC_EXCLUDE;
14414 if (info->print_gc_sections && o->size != 0)
14415 /* xgettext:c-format */
14416 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
14417 o, sub);
14421 return true;
14424 /* Propagate collected vtable information. This is called through
14425 elf_link_hash_traverse. */
14427 static bool
14428 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
14430 /* Those that are not vtables. */
14431 if (h->start_stop
14432 || h->u2.vtable == NULL
14433 || h->u2.vtable->parent == NULL)
14434 return true;
14436 /* Those vtables that do not have parents, we cannot merge. */
14437 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
14438 return true;
14440 /* If we've already been done, exit. */
14441 if (h->u2.vtable->used && h->u2.vtable->used[-1])
14442 return true;
14444 /* Make sure the parent's table is up to date. */
14445 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
14447 if (h->u2.vtable->used == NULL)
14449 /* None of this table's entries were referenced. Re-use the
14450 parent's table. */
14451 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
14452 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
14454 else
14456 size_t n;
14457 bool *cu, *pu;
14459 /* Or the parent's entries into ours. */
14460 cu = h->u2.vtable->used;
14461 cu[-1] = true;
14462 pu = h->u2.vtable->parent->u2.vtable->used;
14463 if (pu != NULL)
14465 const struct elf_backend_data *bed;
14466 unsigned int log_file_align;
14468 bed = get_elf_backend_data (h->root.u.def.section->owner);
14469 log_file_align = bed->s->log_file_align;
14470 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
14471 while (n--)
14473 if (*pu)
14474 *cu = true;
14475 pu++;
14476 cu++;
14481 return true;
14484 struct link_info_ok
14486 struct bfd_link_info *info;
14487 bool ok;
14490 static bool
14491 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h,
14492 void *ptr)
14494 asection *sec;
14495 bfd_vma hstart, hend;
14496 Elf_Internal_Rela *relstart, *relend, *rel;
14497 const struct elf_backend_data *bed;
14498 unsigned int log_file_align;
14499 struct link_info_ok *info = (struct link_info_ok *) ptr;
14501 /* Take care of both those symbols that do not describe vtables as
14502 well as those that are not loaded. */
14503 if (h->start_stop
14504 || h->u2.vtable == NULL
14505 || h->u2.vtable->parent == NULL)
14506 return true;
14508 BFD_ASSERT (h->root.type == bfd_link_hash_defined
14509 || h->root.type == bfd_link_hash_defweak);
14511 sec = h->root.u.def.section;
14512 hstart = h->root.u.def.value;
14513 hend = hstart + h->size;
14515 relstart = _bfd_elf_link_info_read_relocs (sec->owner, info->info,
14516 sec, NULL, NULL, true);
14517 if (!relstart)
14518 return info->ok = false;
14519 bed = get_elf_backend_data (sec->owner);
14520 log_file_align = bed->s->log_file_align;
14522 relend = relstart + sec->reloc_count;
14524 for (rel = relstart; rel < relend; ++rel)
14525 if (rel->r_offset >= hstart && rel->r_offset < hend)
14527 /* If the entry is in use, do nothing. */
14528 if (h->u2.vtable->used
14529 && (rel->r_offset - hstart) < h->u2.vtable->size)
14531 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
14532 if (h->u2.vtable->used[entry])
14533 continue;
14535 /* Otherwise, kill it. */
14536 rel->r_offset = rel->r_info = rel->r_addend = 0;
14539 return true;
14542 /* Mark sections containing dynamically referenced symbols. When
14543 building shared libraries, we must assume that any visible symbol is
14544 referenced. */
14546 bool
14547 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
14549 struct bfd_link_info *info = (struct bfd_link_info *) inf;
14550 struct bfd_elf_dynamic_list *d = info->dynamic_list;
14552 if ((h->root.type == bfd_link_hash_defined
14553 || h->root.type == bfd_link_hash_defweak)
14554 && (!h->start_stop
14555 || h->root.ldscript_def
14556 || !info->start_stop_gc)
14557 && ((h->ref_dynamic && !h->forced_local)
14558 || ((h->def_regular || ELF_COMMON_DEF_P (h))
14559 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
14560 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
14561 && (!bfd_link_executable (info)
14562 || info->gc_keep_exported
14563 || info->export_dynamic
14564 || (h->dynamic
14565 && d != NULL
14566 && (*d->match) (&d->head, NULL, h->root.root.string)))
14567 && (h->versioned >= versioned
14568 || !bfd_hide_sym_by_version (info->version_info,
14569 h->root.root.string)))))
14570 h->root.u.def.section->flags |= SEC_KEEP;
14572 return true;
14575 /* Keep all sections containing symbols undefined on the command-line,
14576 and the section containing the entry symbol. */
14578 void
14579 _bfd_elf_gc_keep (struct bfd_link_info *info)
14581 struct bfd_sym_chain *sym;
14583 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
14585 struct elf_link_hash_entry *h;
14587 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
14588 false, false, false);
14590 if (h != NULL
14591 && (h->root.type == bfd_link_hash_defined
14592 || h->root.type == bfd_link_hash_defweak)
14593 && !bfd_is_const_section (h->root.u.def.section))
14594 h->root.u.def.section->flags |= SEC_KEEP;
14598 bool
14599 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
14600 struct bfd_link_info *info)
14602 bfd *ibfd = info->input_bfds;
14604 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14606 asection *sec;
14607 struct elf_reloc_cookie cookie;
14609 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
14610 continue;
14611 sec = ibfd->sections;
14612 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14613 continue;
14615 if (!init_reloc_cookie (&cookie, info, ibfd, false))
14616 return false;
14618 for (sec = ibfd->sections; sec; sec = sec->next)
14620 if (startswith (bfd_section_name (sec), ".eh_frame_entry")
14621 && init_reloc_cookie_rels (&cookie, info, ibfd, sec,
14622 false))
14624 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
14625 fini_reloc_cookie_rels (&cookie, sec);
14629 return true;
14632 /* Do mark and sweep of unused sections. */
14634 bool
14635 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
14637 bool ok = true;
14638 bfd *sub;
14639 elf_gc_mark_hook_fn gc_mark_hook;
14640 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14641 struct elf_link_hash_table *htab;
14642 struct link_info_ok info_ok;
14644 if (!bed->can_gc_sections
14645 || !is_elf_hash_table (info->hash))
14647 _bfd_error_handler(_("warning: gc-sections option ignored"));
14648 return true;
14651 bed->gc_keep (info);
14652 htab = elf_hash_table (info);
14654 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
14655 at the .eh_frame section if we can mark the FDEs individually. */
14656 for (sub = info->input_bfds;
14657 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
14658 sub = sub->link.next)
14660 asection *sec;
14661 struct elf_reloc_cookie cookie;
14663 sec = sub->sections;
14664 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14665 continue;
14666 sec = bfd_get_section_by_name (sub, ".eh_frame");
14667 while (sec && init_reloc_cookie_for_section (&cookie, info, sec,
14668 false))
14670 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
14671 if (elf_section_data (sec)->sec_info
14672 && (sec->flags & SEC_LINKER_CREATED) == 0)
14673 elf_eh_frame_section (sub) = sec;
14674 fini_reloc_cookie_for_section (&cookie, sec);
14675 sec = bfd_get_next_section_by_name (NULL, sec);
14679 /* Apply transitive closure to the vtable entry usage info. */
14680 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
14681 if (!ok)
14682 return false;
14684 /* Kill the vtable relocations that were not used. */
14685 info_ok.info = info;
14686 info_ok.ok = true;
14687 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &info_ok);
14688 if (!info_ok.ok)
14689 return false;
14691 /* Mark dynamically referenced symbols. */
14692 if (htab->dynamic_sections_created || info->gc_keep_exported)
14693 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
14695 /* Grovel through relocs to find out who stays ... */
14696 gc_mark_hook = bed->gc_mark_hook;
14697 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
14699 asection *o;
14701 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
14702 || elf_object_id (sub) != elf_hash_table_id (htab)
14703 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
14704 continue;
14706 o = sub->sections;
14707 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14708 continue;
14710 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
14711 Also treat note sections as a root, if the section is not part
14712 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
14713 well as FINI_ARRAY sections for ld -r. */
14714 for (o = sub->sections; o != NULL; o = o->next)
14715 if (!o->gc_mark
14716 && (o->flags & SEC_EXCLUDE) == 0
14717 && ((o->flags & SEC_KEEP) != 0
14718 || (bfd_link_relocatable (info)
14719 && ((elf_section_data (o)->this_hdr.sh_type
14720 == SHT_PREINIT_ARRAY)
14721 || (elf_section_data (o)->this_hdr.sh_type
14722 == SHT_INIT_ARRAY)
14723 || (elf_section_data (o)->this_hdr.sh_type
14724 == SHT_FINI_ARRAY)))
14725 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
14726 && elf_next_in_group (o) == NULL
14727 && elf_linked_to_section (o) == NULL)
14728 || ((elf_tdata (sub)->has_gnu_osabi & elf_gnu_osabi_retain)
14729 && (elf_section_flags (o) & SHF_GNU_RETAIN))))
14731 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
14732 return false;
14736 /* Allow the backend to mark additional target specific sections. */
14737 bed->gc_mark_extra_sections (info, gc_mark_hook);
14739 /* ... and mark SEC_EXCLUDE for those that go. */
14740 return elf_gc_sweep (abfd, info);
14743 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
14745 bool
14746 bfd_elf_gc_record_vtinherit (bfd *abfd,
14747 asection *sec,
14748 struct elf_link_hash_entry *h,
14749 bfd_vma offset)
14751 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
14752 struct elf_link_hash_entry **search, *child;
14753 size_t extsymcount;
14754 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14756 /* The sh_info field of the symtab header tells us where the
14757 external symbols start. We don't care about the local symbols at
14758 this point. */
14759 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
14760 if (!elf_bad_symtab (abfd))
14761 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
14763 sym_hashes = elf_sym_hashes (abfd);
14764 sym_hashes_end = PTR_ADD (sym_hashes, extsymcount);
14766 /* Hunt down the child symbol, which is in this section at the same
14767 offset as the relocation. */
14768 for (search = sym_hashes; search != sym_hashes_end; ++search)
14770 if ((child = *search) != NULL
14771 && (child->root.type == bfd_link_hash_defined
14772 || child->root.type == bfd_link_hash_defweak)
14773 && child->root.u.def.section == sec
14774 && child->root.u.def.value == offset)
14775 goto win;
14778 /* xgettext:c-format */
14779 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
14780 abfd, sec, (uint64_t) offset);
14781 bfd_set_error (bfd_error_invalid_operation);
14782 return false;
14784 win:
14785 if (!child->u2.vtable)
14787 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
14788 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
14789 if (!child->u2.vtable)
14790 return false;
14792 if (!h)
14794 /* This *should* only be the absolute section. It could potentially
14795 be that someone has defined a non-global vtable though, which
14796 would be bad. It isn't worth paging in the local symbols to be
14797 sure though; that case should simply be handled by the assembler. */
14799 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
14801 else
14802 child->u2.vtable->parent = h;
14804 return true;
14807 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
14809 bool
14810 bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec,
14811 struct elf_link_hash_entry *h,
14812 bfd_vma addend)
14814 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14815 unsigned int log_file_align = bed->s->log_file_align;
14817 if (!h)
14819 /* xgettext:c-format */
14820 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
14821 abfd, sec);
14822 bfd_set_error (bfd_error_bad_value);
14823 return false;
14826 if (!h->u2.vtable)
14828 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
14829 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
14830 if (!h->u2.vtable)
14831 return false;
14834 if (addend >= h->u2.vtable->size)
14836 size_t size, bytes, file_align;
14837 bool *ptr = h->u2.vtable->used;
14839 /* While the symbol is undefined, we have to be prepared to handle
14840 a zero size. */
14841 file_align = 1 << log_file_align;
14842 if (h->root.type == bfd_link_hash_undefined)
14843 size = addend + file_align;
14844 else
14846 size = h->size;
14847 if (addend >= size)
14849 /* Oops! We've got a reference past the defined end of
14850 the table. This is probably a bug -- shall we warn? */
14851 size = addend + file_align;
14854 size = (size + file_align - 1) & -file_align;
14856 /* Allocate one extra entry for use as a "done" flag for the
14857 consolidation pass. */
14858 bytes = ((size >> log_file_align) + 1) * sizeof (bool);
14860 if (ptr)
14862 ptr = (bool *) bfd_realloc (ptr - 1, bytes);
14864 if (ptr != NULL)
14866 size_t oldbytes;
14868 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
14869 * sizeof (bool));
14870 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
14873 else
14874 ptr = (bool *) bfd_zmalloc (bytes);
14876 if (ptr == NULL)
14877 return false;
14879 /* And arrange for that done flag to be at index -1. */
14880 h->u2.vtable->used = ptr + 1;
14881 h->u2.vtable->size = size;
14884 h->u2.vtable->used[addend >> log_file_align] = true;
14886 return true;
14889 /* Map an ELF section header flag to its corresponding string. */
14890 typedef struct
14892 char *flag_name;
14893 flagword flag_value;
14894 } elf_flags_to_name_table;
14896 static const elf_flags_to_name_table elf_flags_to_names [] =
14898 { "SHF_WRITE", SHF_WRITE },
14899 { "SHF_ALLOC", SHF_ALLOC },
14900 { "SHF_EXECINSTR", SHF_EXECINSTR },
14901 { "SHF_MERGE", SHF_MERGE },
14902 { "SHF_STRINGS", SHF_STRINGS },
14903 { "SHF_INFO_LINK", SHF_INFO_LINK},
14904 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
14905 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
14906 { "SHF_GROUP", SHF_GROUP },
14907 { "SHF_TLS", SHF_TLS },
14908 { "SHF_MASKOS", SHF_MASKOS },
14909 { "SHF_EXCLUDE", SHF_EXCLUDE },
14912 /* Returns TRUE if the section is to be included, otherwise FALSE. */
14913 bool
14914 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
14915 struct flag_info *flaginfo,
14916 asection *section)
14918 const bfd_vma sh_flags = elf_section_flags (section);
14920 if (!flaginfo->flags_initialized)
14922 bfd *obfd = info->output_bfd;
14923 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14924 struct flag_info_list *tf = flaginfo->flag_list;
14925 int with_hex = 0;
14926 int without_hex = 0;
14928 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
14930 unsigned i;
14931 flagword (*lookup) (char *);
14933 lookup = bed->elf_backend_lookup_section_flags_hook;
14934 if (lookup != NULL)
14936 flagword hexval = (*lookup) ((char *) tf->name);
14938 if (hexval != 0)
14940 if (tf->with == with_flags)
14941 with_hex |= hexval;
14942 else if (tf->with == without_flags)
14943 without_hex |= hexval;
14944 tf->valid = true;
14945 continue;
14948 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
14950 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
14952 if (tf->with == with_flags)
14953 with_hex |= elf_flags_to_names[i].flag_value;
14954 else if (tf->with == without_flags)
14955 without_hex |= elf_flags_to_names[i].flag_value;
14956 tf->valid = true;
14957 break;
14960 if (!tf->valid)
14962 info->callbacks->einfo
14963 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
14964 return false;
14967 flaginfo->flags_initialized = true;
14968 flaginfo->only_with_flags |= with_hex;
14969 flaginfo->not_with_flags |= without_hex;
14972 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
14973 return false;
14975 if ((flaginfo->not_with_flags & sh_flags) != 0)
14976 return false;
14978 return true;
14981 struct alloc_got_off_arg {
14982 bfd_vma gotoff;
14983 struct bfd_link_info *info;
14986 /* We need a special top-level link routine to convert got reference counts
14987 to real got offsets. */
14989 static bool
14990 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
14992 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
14993 bfd *obfd = gofarg->info->output_bfd;
14994 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14996 if (h->got.refcount > 0)
14998 h->got.offset = gofarg->gotoff;
14999 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
15001 else
15002 h->got.offset = (bfd_vma) -1;
15004 return true;
15007 /* And an accompanying bit to work out final got entry offsets once
15008 we're done. Should be called from final_link. */
15010 bool
15011 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
15012 struct bfd_link_info *info)
15014 bfd *i;
15015 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15016 bfd_vma gotoff;
15017 struct alloc_got_off_arg gofarg;
15019 BFD_ASSERT (abfd == info->output_bfd);
15021 if (! is_elf_hash_table (info->hash))
15022 return false;
15024 /* The GOT offset is relative to the .got section, but the GOT header is
15025 put into the .got.plt section, if the backend uses it. */
15026 if (bed->want_got_plt)
15027 gotoff = 0;
15028 else
15029 gotoff = bed->got_header_size;
15031 /* Do the local .got entries first. */
15032 for (i = info->input_bfds; i; i = i->link.next)
15034 bfd_signed_vma *local_got;
15035 size_t j, locsymcount;
15036 Elf_Internal_Shdr *symtab_hdr;
15038 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
15039 continue;
15041 local_got = elf_local_got_refcounts (i);
15042 if (!local_got)
15043 continue;
15045 symtab_hdr = &elf_tdata (i)->symtab_hdr;
15046 if (elf_bad_symtab (i))
15047 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
15048 else
15049 locsymcount = symtab_hdr->sh_info;
15051 for (j = 0; j < locsymcount; ++j)
15053 if (local_got[j] > 0)
15055 local_got[j] = gotoff;
15056 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
15058 else
15059 local_got[j] = (bfd_vma) -1;
15063 /* Then the global .got entries. .plt refcounts are handled by
15064 adjust_dynamic_symbol */
15065 gofarg.gotoff = gotoff;
15066 gofarg.info = info;
15067 elf_link_hash_traverse (elf_hash_table (info),
15068 elf_gc_allocate_got_offsets,
15069 &gofarg);
15070 return true;
15073 /* Many folk need no more in the way of final link than this, once
15074 got entry reference counting is enabled. */
15076 bool
15077 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
15079 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
15080 return false;
15082 /* Invoke the regular ELF backend linker to do all the work. */
15083 return bfd_elf_final_link (abfd, info);
15086 bool
15087 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
15089 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
15091 if (rcookie->bad_symtab)
15092 rcookie->rel = rcookie->rels;
15094 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
15096 unsigned long r_symndx;
15098 if (! rcookie->bad_symtab)
15099 if (rcookie->rel->r_offset > offset)
15100 return false;
15101 if (rcookie->rel->r_offset != offset)
15102 continue;
15104 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
15105 if (r_symndx == STN_UNDEF)
15106 return true;
15108 if (r_symndx >= rcookie->locsymcount
15109 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
15111 struct elf_link_hash_entry *h;
15113 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
15115 while (h->root.type == bfd_link_hash_indirect
15116 || h->root.type == bfd_link_hash_warning)
15117 h = (struct elf_link_hash_entry *) h->root.u.i.link;
15119 if ((h->root.type == bfd_link_hash_defined
15120 || h->root.type == bfd_link_hash_defweak)
15121 && (h->root.u.def.section->owner != rcookie->abfd
15122 || h->root.u.def.section->kept_section != NULL
15123 || discarded_section (h->root.u.def.section)))
15124 return true;
15126 else
15128 /* It's not a relocation against a global symbol,
15129 but it could be a relocation against a local
15130 symbol for a discarded section. */
15131 asection *isec;
15132 Elf_Internal_Sym *isym;
15134 /* Need to: get the symbol; get the section. */
15135 isym = &rcookie->locsyms[r_symndx];
15136 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
15137 if (isec != NULL
15138 && (isec->kept_section != NULL
15139 || discarded_section (isec)))
15140 return true;
15142 return false;
15144 return false;
15147 /* Discard unneeded references to discarded sections.
15148 Returns -1 on error, 1 if any section's size was changed, 0 if
15149 nothing changed. This function assumes that the relocations are in
15150 sorted order, which is true for all known assemblers. */
15153 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
15155 struct elf_reloc_cookie cookie;
15156 asection *o;
15157 bfd *abfd;
15158 int changed = 0;
15160 if (info->traditional_format
15161 || !is_elf_hash_table (info->hash))
15162 return 0;
15164 o = bfd_get_section_by_name (output_bfd, ".stab");
15165 if (o != NULL)
15167 asection *i;
15169 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
15171 if (i->size == 0
15172 || i->reloc_count == 0
15173 || i->sec_info_type != SEC_INFO_TYPE_STABS)
15174 continue;
15176 abfd = i->owner;
15177 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
15178 continue;
15180 if (!init_reloc_cookie_for_section (&cookie, info, i, false))
15181 return -1;
15183 if (_bfd_discard_section_stabs (abfd, i,
15184 elf_section_data (i)->sec_info,
15185 bfd_elf_reloc_symbol_deleted_p,
15186 &cookie))
15187 changed = 1;
15189 fini_reloc_cookie_for_section (&cookie, i);
15193 o = NULL;
15194 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
15195 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
15196 if (o != NULL)
15198 asection *i;
15199 int eh_changed = 0;
15200 unsigned int eh_alignment; /* Octets. */
15202 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
15204 if (i->size == 0)
15205 continue;
15207 abfd = i->owner;
15208 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
15209 continue;
15211 if (!init_reloc_cookie_for_section (&cookie, info, i, false))
15212 return -1;
15214 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
15215 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
15216 bfd_elf_reloc_symbol_deleted_p,
15217 &cookie))
15219 eh_changed = 1;
15220 if (i->size != i->rawsize)
15221 changed = 1;
15224 fini_reloc_cookie_for_section (&cookie, i);
15227 eh_alignment = ((1 << o->alignment_power)
15228 * bfd_octets_per_byte (output_bfd, o));
15229 /* Skip over zero terminator, and prevent empty sections from
15230 adding alignment padding at the end. */
15231 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
15232 if (i->size == 0)
15233 i->flags |= SEC_EXCLUDE;
15234 else if (i->size > 4)
15235 break;
15236 /* The last non-empty eh_frame section doesn't need padding. */
15237 if (i != NULL)
15238 i = i->map_tail.s;
15239 /* Any prior sections must pad the last FDE out to the output
15240 section alignment. Otherwise we might have zero padding
15241 between sections, which would be seen as a terminator. */
15242 for (; i != NULL; i = i->map_tail.s)
15243 if (i->size == 4)
15244 /* All but the last zero terminator should have been removed. */
15245 BFD_FAIL ();
15246 else
15248 bfd_size_type size
15249 = (i->size + eh_alignment - 1) & -eh_alignment;
15250 if (i->size != size)
15252 i->size = size;
15253 changed = 1;
15254 eh_changed = 1;
15257 if (eh_changed)
15258 elf_link_hash_traverse (elf_hash_table (info),
15259 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
15262 o = bfd_get_section_by_name (output_bfd, ".sframe");
15263 if (o != NULL)
15265 asection *i;
15267 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
15269 if (i->size == 0)
15270 continue;
15272 abfd = i->owner;
15273 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
15274 continue;
15276 if (!init_reloc_cookie_for_section (&cookie, info, i, false))
15277 return -1;
15279 if (_bfd_elf_parse_sframe (abfd, info, i, &cookie))
15281 if (_bfd_elf_discard_section_sframe (i,
15282 bfd_elf_reloc_symbol_deleted_p,
15283 &cookie))
15285 if (i->size != i->rawsize)
15286 changed = 1;
15289 fini_reloc_cookie_for_section (&cookie, i);
15291 /* Update the reference to the output .sframe section. Used to
15292 determine later if PT_GNU_SFRAME segment is to be generated. */
15293 if (!_bfd_elf_set_section_sframe (output_bfd, info))
15294 return -1;
15297 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
15299 const struct elf_backend_data *bed;
15300 asection *s;
15302 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
15303 continue;
15304 s = abfd->sections;
15305 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
15306 continue;
15308 bed = get_elf_backend_data (abfd);
15310 if (bed->elf_backend_discard_info != NULL)
15312 if (!init_reloc_cookie (&cookie, info, abfd, false))
15313 return -1;
15315 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
15316 changed = 1;
15318 fini_reloc_cookie (&cookie, abfd);
15322 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
15323 _bfd_elf_end_eh_frame_parsing (info);
15325 if (info->eh_frame_hdr_type
15326 && !bfd_link_relocatable (info)
15327 && _bfd_elf_discard_section_eh_frame_hdr (info))
15328 changed = 1;
15330 return changed;
15333 bool
15334 _bfd_elf_section_already_linked (bfd *abfd,
15335 asection *sec,
15336 struct bfd_link_info *info)
15338 flagword flags;
15339 const char *name, *key;
15340 struct bfd_section_already_linked *l;
15341 struct bfd_section_already_linked_hash_entry *already_linked_list;
15343 if (sec->output_section == bfd_abs_section_ptr)
15344 return false;
15346 flags = sec->flags;
15348 /* Return if it isn't a linkonce section. A comdat group section
15349 also has SEC_LINK_ONCE set. */
15350 if ((flags & SEC_LINK_ONCE) == 0)
15351 return false;
15353 /* Don't put group member sections on our list of already linked
15354 sections. They are handled as a group via their group section. */
15355 if (elf_sec_group (sec) != NULL)
15356 return false;
15358 /* For a SHT_GROUP section, use the group signature as the key. */
15359 name = sec->name;
15360 if ((flags & SEC_GROUP) != 0
15361 && elf_next_in_group (sec) != NULL
15362 && elf_group_name (elf_next_in_group (sec)) != NULL)
15363 key = elf_group_name (elf_next_in_group (sec));
15364 else
15366 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
15367 if (startswith (name, ".gnu.linkonce.")
15368 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
15369 key++;
15370 else
15371 /* Must be a user linkonce section that doesn't follow gcc's
15372 naming convention. In this case we won't be matching
15373 single member groups. */
15374 key = name;
15377 already_linked_list = bfd_section_already_linked_table_lookup (key);
15379 for (l = already_linked_list->entry; l != NULL; l = l->next)
15381 /* We may have 2 different types of sections on the list: group
15382 sections with a signature of <key> (<key> is some string),
15383 and linkonce sections named .gnu.linkonce.<type>.<key>.
15384 Match like sections. LTO plugin sections are an exception.
15385 They are always named .gnu.linkonce.t.<key> and match either
15386 type of section. */
15387 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
15388 && ((flags & SEC_GROUP) != 0
15389 || strcmp (name, l->sec->name) == 0))
15390 || (l->sec->owner->flags & BFD_PLUGIN) != 0
15391 || (sec->owner->flags & BFD_PLUGIN) != 0)
15393 /* The section has already been linked. See if we should
15394 issue a warning. */
15395 if (!_bfd_handle_already_linked (sec, l, info))
15396 return false;
15398 if (flags & SEC_GROUP)
15400 asection *first = elf_next_in_group (sec);
15401 asection *s = first;
15403 while (s != NULL)
15405 s->output_section = bfd_abs_section_ptr;
15406 /* Record which group discards it. */
15407 s->kept_section = l->sec;
15408 s = elf_next_in_group (s);
15409 /* These lists are circular. */
15410 if (s == first)
15411 break;
15415 return true;
15419 /* A single member comdat group section may be discarded by a
15420 linkonce section and vice versa. */
15421 if ((flags & SEC_GROUP) != 0)
15423 asection *first = elf_next_in_group (sec);
15425 if (first != NULL && elf_next_in_group (first) == first)
15426 /* Check this single member group against linkonce sections. */
15427 for (l = already_linked_list->entry; l != NULL; l = l->next)
15428 if ((l->sec->flags & SEC_GROUP) == 0
15429 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
15431 first->output_section = bfd_abs_section_ptr;
15432 first->kept_section = l->sec;
15433 sec->output_section = bfd_abs_section_ptr;
15434 break;
15437 else
15438 /* Check this linkonce section against single member groups. */
15439 for (l = already_linked_list->entry; l != NULL; l = l->next)
15440 if (l->sec->flags & SEC_GROUP)
15442 asection *first = elf_next_in_group (l->sec);
15444 if (first != NULL
15445 && elf_next_in_group (first) == first
15446 && bfd_elf_match_symbols_in_sections (first, sec, info))
15448 sec->output_section = bfd_abs_section_ptr;
15449 sec->kept_section = first;
15450 break;
15454 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
15455 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
15456 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
15457 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
15458 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
15459 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
15460 `.gnu.linkonce.t.F' section from a different bfd not requiring any
15461 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
15462 The reverse order cannot happen as there is never a bfd with only the
15463 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
15464 matter as here were are looking only for cross-bfd sections. */
15466 if ((flags & SEC_GROUP) == 0 && startswith (name, ".gnu.linkonce.r."))
15467 for (l = already_linked_list->entry; l != NULL; l = l->next)
15468 if ((l->sec->flags & SEC_GROUP) == 0
15469 && startswith (l->sec->name, ".gnu.linkonce.t."))
15471 if (abfd != l->sec->owner)
15472 sec->output_section = bfd_abs_section_ptr;
15473 break;
15476 /* This is the first section with this name. Record it. */
15477 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
15478 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
15479 return sec->output_section == bfd_abs_section_ptr;
15482 bool
15483 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
15485 return sym->st_shndx == SHN_COMMON;
15488 unsigned int
15489 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
15491 return SHN_COMMON;
15494 asection *
15495 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
15497 return bfd_com_section_ptr;
15500 bfd_vma
15501 _bfd_elf_default_got_elt_size (bfd *abfd,
15502 struct bfd_link_info *info ATTRIBUTE_UNUSED,
15503 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
15504 bfd *ibfd ATTRIBUTE_UNUSED,
15505 unsigned long symndx ATTRIBUTE_UNUSED)
15507 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15508 return bed->s->arch_size / 8;
15511 /* Routines to support the creation of dynamic relocs. */
15513 /* Returns the name of the dynamic reloc section associated with SEC. */
15515 static const char *
15516 get_dynamic_reloc_section_name (bfd * abfd,
15517 asection * sec,
15518 bool is_rela)
15520 char *name;
15521 const char *old_name = bfd_section_name (sec);
15522 const char *prefix = is_rela ? ".rela" : ".rel";
15524 if (old_name == NULL)
15525 return NULL;
15527 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
15528 sprintf (name, "%s%s", prefix, old_name);
15530 return name;
15533 /* Returns the dynamic reloc section associated with SEC.
15534 If necessary compute the name of the dynamic reloc section based
15535 on SEC's name (looked up in ABFD's string table) and the setting
15536 of IS_RELA. */
15538 asection *
15539 _bfd_elf_get_dynamic_reloc_section (bfd *abfd,
15540 asection *sec,
15541 bool is_rela)
15543 asection *reloc_sec = elf_section_data (sec)->sreloc;
15545 if (reloc_sec == NULL)
15547 const char *name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
15549 if (name != NULL)
15551 reloc_sec = bfd_get_linker_section (abfd, name);
15553 if (reloc_sec != NULL)
15554 elf_section_data (sec)->sreloc = reloc_sec;
15558 return reloc_sec;
15561 /* Returns the dynamic reloc section associated with SEC. If the
15562 section does not exist it is created and attached to the DYNOBJ
15563 bfd and stored in the SRELOC field of SEC's elf_section_data
15564 structure.
15566 ALIGNMENT is the alignment for the newly created section and
15567 IS_RELA defines whether the name should be .rela.<SEC's name>
15568 or .rel.<SEC's name>. The section name is looked up in the
15569 string table associated with ABFD. */
15571 asection *
15572 _bfd_elf_make_dynamic_reloc_section (asection *sec,
15573 bfd *dynobj,
15574 unsigned int alignment,
15575 bfd *abfd,
15576 bool is_rela)
15578 asection * reloc_sec = elf_section_data (sec)->sreloc;
15580 if (reloc_sec == NULL)
15582 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
15584 if (name == NULL)
15585 return NULL;
15587 reloc_sec = bfd_get_linker_section (dynobj, name);
15589 if (reloc_sec == NULL)
15591 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
15592 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
15593 if ((sec->flags & SEC_ALLOC) != 0)
15594 flags |= SEC_ALLOC | SEC_LOAD;
15596 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
15597 if (reloc_sec != NULL)
15599 /* _bfd_elf_get_sec_type_attr chooses a section type by
15600 name. Override as it may be wrong, eg. for a user
15601 section named "auto" we'll get ".relauto" which is
15602 seen to be a .rela section. */
15603 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
15604 if (!bfd_set_section_alignment (reloc_sec, alignment))
15605 reloc_sec = NULL;
15609 elf_section_data (sec)->sreloc = reloc_sec;
15612 return reloc_sec;
15615 /* Copy the ELF symbol type and other attributes for a linker script
15616 assignment from HSRC to HDEST. Generally this should be treated as
15617 if we found a strong non-dynamic definition for HDEST (except that
15618 ld ignores multiple definition errors). */
15619 void
15620 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
15621 struct bfd_link_hash_entry *hdest,
15622 struct bfd_link_hash_entry *hsrc)
15624 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
15625 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
15626 Elf_Internal_Sym isym;
15628 ehdest->type = ehsrc->type;
15629 ehdest->target_internal = ehsrc->target_internal;
15631 isym.st_other = ehsrc->other;
15632 elf_merge_st_other (abfd, ehdest, isym.st_other, NULL, true, false);
15635 /* Append a RELA relocation REL to section S in BFD. */
15637 void
15638 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15640 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15641 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
15642 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
15643 bed->s->swap_reloca_out (abfd, rel, loc);
15646 /* Append a REL relocation REL to section S in BFD. */
15648 void
15649 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15651 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15652 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
15653 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
15654 bed->s->swap_reloc_out (abfd, rel, loc);
15657 /* Define __start, __stop, .startof. or .sizeof. symbol. */
15659 struct bfd_link_hash_entry *
15660 bfd_elf_define_start_stop (struct bfd_link_info *info,
15661 const char *symbol, asection *sec)
15663 struct elf_link_hash_entry *h;
15665 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
15666 false, false, true);
15667 /* NB: Common symbols will be turned into definition later. */
15668 if (h != NULL
15669 && !h->root.ldscript_def
15670 && (h->root.type == bfd_link_hash_undefined
15671 || h->root.type == bfd_link_hash_undefweak
15672 || ((h->ref_regular || h->def_dynamic)
15673 && !h->def_regular
15674 && h->root.type != bfd_link_hash_common)))
15676 bool was_dynamic = h->ref_dynamic || h->def_dynamic;
15677 h->verinfo.verdef = NULL;
15678 h->root.type = bfd_link_hash_defined;
15679 h->root.u.def.section = sec;
15680 h->root.u.def.value = 0;
15681 h->def_regular = 1;
15682 h->def_dynamic = 0;
15683 h->start_stop = 1;
15684 h->u2.start_stop_section = sec;
15685 if (symbol[0] == '.')
15687 /* .startof. and .sizeof. symbols are local. */
15688 const struct elf_backend_data *bed;
15689 bed = get_elf_backend_data (info->output_bfd);
15690 (*bed->elf_backend_hide_symbol) (info, h, true);
15692 else
15694 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
15695 h->other = ((h->other & ~ELF_ST_VISIBILITY (-1))
15696 | info->start_stop_visibility);
15697 if (was_dynamic)
15698 bfd_elf_link_record_dynamic_symbol (info, h);
15700 return &h->root;
15702 return NULL;
15705 /* Find dynamic relocs for H that apply to read-only sections. */
15707 asection *
15708 _bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry *h)
15710 struct elf_dyn_relocs *p;
15712 for (p = h->dyn_relocs; p != NULL; p = p->next)
15714 asection *s = p->sec->output_section;
15716 if (s != NULL && (s->flags & SEC_READONLY) != 0)
15717 return p->sec;
15719 return NULL;
15722 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
15723 read-only sections. */
15725 bool
15726 _bfd_elf_maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
15728 asection *sec;
15730 if (h->root.type == bfd_link_hash_indirect)
15731 return true;
15733 sec = _bfd_elf_readonly_dynrelocs (h);
15734 if (sec != NULL)
15736 struct bfd_link_info *info = (struct bfd_link_info *) inf;
15738 info->flags |= DF_TEXTREL;
15739 /* xgettext:c-format */
15740 info->callbacks->minfo (_("%pB: dynamic relocation against `%pT' "
15741 "in read-only section `%pA'\n"),
15742 sec->owner, h->root.root.string, sec);
15744 if (bfd_link_textrel_check (info))
15745 /* xgettext:c-format */
15746 info->callbacks->einfo (_("%P: %pB: warning: relocation against `%s' "
15747 "in read-only section `%pA'\n"),
15748 sec->owner, h->root.root.string, sec);
15750 /* Not an error, just cut short the traversal. */
15751 return false;
15753 return true;
15756 /* Add dynamic tags. */
15758 bool
15759 _bfd_elf_add_dynamic_tags (bfd *output_bfd, struct bfd_link_info *info,
15760 bool need_dynamic_reloc)
15762 struct elf_link_hash_table *htab = elf_hash_table (info);
15764 if (htab->dynamic_sections_created)
15766 /* Add some entries to the .dynamic section. We fill in the
15767 values later, in finish_dynamic_sections, but we must add
15768 the entries now so that we get the correct size for the
15769 .dynamic section. The DT_DEBUG entry is filled in by the
15770 dynamic linker and used by the debugger. */
15771 #define add_dynamic_entry(TAG, VAL) \
15772 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
15774 const struct elf_backend_data *bed
15775 = get_elf_backend_data (output_bfd);
15777 if (bfd_link_executable (info))
15779 if (!add_dynamic_entry (DT_DEBUG, 0))
15780 return false;
15783 if (htab->dt_pltgot_required || htab->splt->size != 0)
15785 /* DT_PLTGOT is used by prelink even if there is no PLT
15786 relocation. */
15787 if (!add_dynamic_entry (DT_PLTGOT, 0))
15788 return false;
15791 if (htab->dt_jmprel_required || htab->srelplt->size != 0)
15793 if (!add_dynamic_entry (DT_PLTRELSZ, 0)
15794 || !add_dynamic_entry (DT_PLTREL,
15795 (bed->rela_plts_and_copies_p
15796 ? DT_RELA : DT_REL))
15797 || !add_dynamic_entry (DT_JMPREL, 0))
15798 return false;
15801 if (htab->tlsdesc_plt
15802 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
15803 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
15804 return false;
15806 if (need_dynamic_reloc)
15808 if (bed->rela_plts_and_copies_p)
15810 if (!add_dynamic_entry (DT_RELA, 0)
15811 || !add_dynamic_entry (DT_RELASZ, 0)
15812 || !add_dynamic_entry (DT_RELAENT,
15813 bed->s->sizeof_rela))
15814 return false;
15816 else
15818 if (!add_dynamic_entry (DT_REL, 0)
15819 || !add_dynamic_entry (DT_RELSZ, 0)
15820 || !add_dynamic_entry (DT_RELENT,
15821 bed->s->sizeof_rel))
15822 return false;
15825 /* If any dynamic relocs apply to a read-only section,
15826 then we need a DT_TEXTREL entry. */
15827 if ((info->flags & DF_TEXTREL) == 0)
15828 elf_link_hash_traverse (htab, _bfd_elf_maybe_set_textrel,
15829 info);
15831 if ((info->flags & DF_TEXTREL) != 0)
15833 if (htab->ifunc_resolvers)
15834 info->callbacks->einfo
15835 (_("%P: warning: GNU indirect functions with DT_TEXTREL "
15836 "may result in a segfault at runtime; recompile with %s\n"),
15837 bfd_link_dll (info) ? "-fPIC" : "-fPIE");
15839 if (!add_dynamic_entry (DT_TEXTREL, 0))
15840 return false;
15844 #undef add_dynamic_entry
15846 return true;