Automatic date update in version.in
[binutils-gdb.git] / bfd / elflink.c
blob6db6a9c0b4702c66d73edba87294e2a59ffafcf5
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->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. */
2252 static Elf_Internal_Verneed *
2253 elf_link_add_glibc_verneed (struct elf_find_verdep_info *rinfo,
2254 Elf_Internal_Verneed *glibc_verref,
2255 const char *version_dep)
2257 Elf_Internal_Verneed *t;
2258 Elf_Internal_Vernaux *a;
2259 size_t amt;
2261 if (glibc_verref != NULL)
2263 t = glibc_verref;
2265 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2267 /* Return if VERSION_DEP dependency has been added. */
2268 if (a->vna_nodename == version_dep
2269 || strcmp (a->vna_nodename, version_dep) == 0)
2270 return t;
2273 else
2275 bool is_glibc;
2277 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2278 t != NULL;
2279 t = t->vn_nextref)
2281 const char *soname = bfd_elf_get_dt_soname (t->vn_bfd);
2282 if (soname != NULL && startswith (soname, "libc.so."))
2283 break;
2286 /* Skip the shared library if it isn't libc.so. */
2287 if (t == NULL)
2288 return t;
2290 is_glibc = false;
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 (!is_glibc && startswith (a->vna_nodename, "GLIBC_2."))
2300 is_glibc = true;
2303 /* Skip if it isn't linked against glibc. */
2304 if (!is_glibc)
2305 return NULL;
2308 amt = sizeof *a;
2309 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2310 if (a == NULL)
2312 rinfo->failed = true;
2313 return NULL;
2316 a->vna_nodename = version_dep;
2317 a->vna_flags = 0;
2318 a->vna_nextptr = t->vn_auxptr;
2319 a->vna_other = rinfo->vers + 1;
2320 ++rinfo->vers;
2322 t->vn_auxptr = a;
2324 return t;
2327 /* Add VERSION_DEP to the list of version dependencies when linked
2328 against glibc. */
2330 void
2331 _bfd_elf_link_add_glibc_version_dependency
2332 (struct elf_find_verdep_info *rinfo,
2333 const char *version_dep[])
2335 Elf_Internal_Verneed *t = NULL;
2339 t = elf_link_add_glibc_verneed (rinfo, t, *version_dep);
2340 /* Return if there is no glibc version reference. */
2341 if (t == NULL)
2342 return;
2343 version_dep++;
2345 while (*version_dep != NULL);
2348 /* Add GLIBC_ABI_DT_RELR to the list of version dependencies when
2349 linked against glibc. */
2351 void
2352 _bfd_elf_link_add_dt_relr_dependency (struct elf_find_verdep_info *rinfo)
2354 if (rinfo->info->enable_dt_relr)
2356 const char *version[] =
2358 "GLIBC_ABI_DT_RELR",
2359 NULL
2361 _bfd_elf_link_add_glibc_version_dependency (rinfo, version);
2365 /* Look through the symbols which are defined in other shared
2366 libraries and referenced here. Update the list of version
2367 dependencies. This will be put into the .gnu.version_r section.
2368 This function is called via elf_link_hash_traverse. */
2370 static bool
2371 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2372 void *data)
2374 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2375 Elf_Internal_Verneed *t;
2376 Elf_Internal_Vernaux *a;
2377 size_t amt;
2379 /* We only care about symbols defined in shared objects with version
2380 information. */
2381 if (!h->def_dynamic
2382 || h->def_regular
2383 || h->dynindx == -1
2384 || h->verinfo.verdef == NULL
2385 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2386 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2387 return true;
2389 /* See if we already know about this version. */
2390 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2391 t != NULL;
2392 t = t->vn_nextref)
2394 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2395 continue;
2397 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2398 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2399 return true;
2401 break;
2404 /* This is a new version. Add it to tree we are building. */
2406 if (t == NULL)
2408 amt = sizeof *t;
2409 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2410 if (t == NULL)
2412 rinfo->failed = true;
2413 return false;
2416 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2417 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2418 elf_tdata (rinfo->info->output_bfd)->verref = t;
2421 amt = sizeof *a;
2422 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2423 if (a == NULL)
2425 rinfo->failed = true;
2426 return false;
2429 /* Note that we are copying a string pointer here, and testing it
2430 above. If bfd_elf_string_from_elf_section is ever changed to
2431 discard the string data when low in memory, this will have to be
2432 fixed. */
2433 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2435 a->vna_flags = h->verinfo.verdef->vd_flags;
2436 a->vna_nextptr = t->vn_auxptr;
2438 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2439 ++rinfo->vers;
2441 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2443 t->vn_auxptr = a;
2445 return true;
2448 /* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2449 hidden. Set *T_P to NULL if there is no match. */
2451 static bool
2452 _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
2453 struct elf_link_hash_entry *h,
2454 const char *version_p,
2455 struct bfd_elf_version_tree **t_p,
2456 bool *hide)
2458 struct bfd_elf_version_tree *t;
2460 /* Look for the version. If we find it, it is no longer weak. */
2461 for (t = info->version_info; t != NULL; t = t->next)
2463 if (strcmp (t->name, version_p) == 0)
2465 size_t len;
2466 char *alc;
2467 struct bfd_elf_version_expr *d;
2469 len = version_p - h->root.root.string;
2470 alc = (char *) bfd_malloc (len);
2471 if (alc == NULL)
2472 return false;
2473 memcpy (alc, h->root.root.string, len - 1);
2474 alc[len - 1] = '\0';
2475 if (alc[len - 2] == ELF_VER_CHR)
2476 alc[len - 2] = '\0';
2478 h->verinfo.vertree = t;
2479 t->used = true;
2480 d = NULL;
2482 if (t->globals.list != NULL)
2483 d = (*t->match) (&t->globals, NULL, alc);
2485 /* See if there is anything to force this symbol to
2486 local scope. */
2487 if (d == NULL && t->locals.list != NULL)
2489 d = (*t->match) (&t->locals, NULL, alc);
2490 if (d != NULL
2491 && h->dynindx != -1
2492 && ! info->export_dynamic)
2493 *hide = true;
2496 free (alc);
2497 break;
2501 *t_p = t;
2503 return true;
2506 /* Return TRUE if the symbol H is hidden by version script. */
2508 bool
2509 _bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
2510 struct elf_link_hash_entry *h)
2512 const char *p;
2513 bool hide = false;
2514 const struct elf_backend_data *bed
2515 = get_elf_backend_data (info->output_bfd);
2517 /* Version script only hides symbols defined in regular objects. */
2518 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2519 return true;
2521 p = strchr (h->root.root.string, ELF_VER_CHR);
2522 if (p != NULL && h->verinfo.vertree == NULL)
2524 struct bfd_elf_version_tree *t;
2526 ++p;
2527 if (*p == ELF_VER_CHR)
2528 ++p;
2530 if (*p != '\0'
2531 && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
2532 && hide)
2534 if (hide)
2535 (*bed->elf_backend_hide_symbol) (info, h, true);
2536 return true;
2540 /* If we don't have a version for this symbol, see if we can find
2541 something. */
2542 if (h->verinfo.vertree == NULL && info->version_info != NULL)
2544 h->verinfo.vertree
2545 = bfd_find_version_for_sym (info->version_info,
2546 h->root.root.string, &hide);
2547 if (h->verinfo.vertree != NULL && hide)
2549 (*bed->elf_backend_hide_symbol) (info, h, true);
2550 return true;
2554 return false;
2557 /* Figure out appropriate versions for all the symbols. We may not
2558 have the version number script until we have read all of the input
2559 files, so until that point we don't know which symbols should be
2560 local. This function is called via elf_link_hash_traverse. */
2562 static bool
2563 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2565 struct elf_info_failed *sinfo;
2566 struct bfd_link_info *info;
2567 const struct elf_backend_data *bed;
2568 struct elf_info_failed eif;
2569 char *p;
2570 bool hide;
2572 sinfo = (struct elf_info_failed *) data;
2573 info = sinfo->info;
2575 /* Fix the symbol flags. */
2576 eif.failed = false;
2577 eif.info = info;
2578 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2580 if (eif.failed)
2581 sinfo->failed = true;
2582 return false;
2585 bed = get_elf_backend_data (info->output_bfd);
2587 /* We only need version numbers for symbols defined in regular
2588 objects. */
2589 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2591 /* Hide symbols defined in discarded input sections. */
2592 if ((h->root.type == bfd_link_hash_defined
2593 || h->root.type == bfd_link_hash_defweak)
2594 && discarded_section (h->root.u.def.section))
2595 (*bed->elf_backend_hide_symbol) (info, h, true);
2596 return true;
2599 hide = false;
2600 p = strchr (h->root.root.string, ELF_VER_CHR);
2601 if (p != NULL && h->verinfo.vertree == NULL)
2603 struct bfd_elf_version_tree *t;
2605 ++p;
2606 if (*p == ELF_VER_CHR)
2607 ++p;
2609 /* If there is no version string, we can just return out. */
2610 if (*p == '\0')
2611 return true;
2613 if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
2615 sinfo->failed = true;
2616 return false;
2619 if (hide)
2620 (*bed->elf_backend_hide_symbol) (info, h, true);
2622 /* If we are building an application, we need to create a
2623 version node for this version. */
2624 if (t == NULL && bfd_link_executable (info))
2626 struct bfd_elf_version_tree **pp;
2627 int version_index;
2629 /* If we aren't going to export this symbol, we don't need
2630 to worry about it. */
2631 if (h->dynindx == -1)
2632 return true;
2634 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2635 sizeof *t);
2636 if (t == NULL)
2638 sinfo->failed = true;
2639 return false;
2642 t->name = p;
2643 t->name_indx = (unsigned int) -1;
2644 t->used = true;
2646 version_index = 1;
2647 /* Don't count anonymous version tag. */
2648 if (sinfo->info->version_info != NULL
2649 && sinfo->info->version_info->vernum == 0)
2650 version_index = 0;
2651 for (pp = &sinfo->info->version_info;
2652 *pp != NULL;
2653 pp = &(*pp)->next)
2654 ++version_index;
2655 t->vernum = version_index;
2657 *pp = t;
2659 h->verinfo.vertree = t;
2661 else if (t == NULL)
2663 /* We could not find the version for a symbol when
2664 generating a shared archive. Return an error. */
2665 _bfd_error_handler
2666 /* xgettext:c-format */
2667 (_("%pB: version node not found for symbol %s"),
2668 info->output_bfd, h->root.root.string);
2669 bfd_set_error (bfd_error_bad_value);
2670 sinfo->failed = true;
2671 return false;
2675 /* If we don't have a version for this symbol, see if we can find
2676 something. */
2677 if (!hide
2678 && h->verinfo.vertree == NULL
2679 && sinfo->info->version_info != NULL)
2681 h->verinfo.vertree
2682 = bfd_find_version_for_sym (sinfo->info->version_info,
2683 h->root.root.string, &hide);
2684 if (h->verinfo.vertree != NULL && hide)
2685 (*bed->elf_backend_hide_symbol) (info, h, true);
2688 return true;
2691 /* Read and swap the relocs from the section indicated by SHDR. This
2692 may be either a REL or a RELA section. The relocations are
2693 translated into RELA relocations and stored in INTERNAL_RELOCS,
2694 which should have already been allocated to contain enough space.
2695 The *EXTERNAL_RELOCS_P are a buffer where the external form of the
2696 relocations should be stored. If *EXTERNAL_RELOCS_ADDR is NULL,
2697 *EXTERNAL_RELOCS_ADDR and *EXTERNAL_RELOCS_SIZE returns the mmap
2698 memory address and size. Otherwise, *EXTERNAL_RELOCS_ADDR is
2699 unchanged and *EXTERNAL_RELOCS_SIZE returns 0.
2701 Returns FALSE if something goes wrong. */
2703 static bool
2704 elf_link_read_relocs_from_section (bfd *abfd,
2705 const asection *sec,
2706 Elf_Internal_Shdr *shdr,
2707 void **external_relocs_addr,
2708 size_t *external_relocs_size,
2709 Elf_Internal_Rela *internal_relocs)
2711 const struct elf_backend_data *bed;
2712 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2713 const bfd_byte *erela;
2714 const bfd_byte *erelaend;
2715 Elf_Internal_Rela *irela;
2716 Elf_Internal_Shdr *symtab_hdr;
2717 size_t nsyms;
2718 void *external_relocs = *external_relocs_addr;
2720 /* Position ourselves at the start of the section. */
2721 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2722 return false;
2724 /* Read the relocations. */
2725 *external_relocs_size = shdr->sh_size;
2726 if (!_bfd_mmap_read_temporary (&external_relocs,
2727 external_relocs_size,
2728 external_relocs_addr, abfd, true))
2729 return false;
2731 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2732 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2734 bed = get_elf_backend_data (abfd);
2736 /* Convert the external relocations to the internal format. */
2737 if (shdr->sh_entsize == bed->s->sizeof_rel)
2738 swap_in = bed->s->swap_reloc_in;
2739 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2740 swap_in = bed->s->swap_reloca_in;
2741 else
2743 bfd_set_error (bfd_error_wrong_format);
2744 return false;
2747 erela = (const bfd_byte *) external_relocs;
2748 /* Setting erelaend like this and comparing with <= handles case of
2749 a fuzzed object with sh_size not a multiple of sh_entsize. */
2750 erelaend = erela + shdr->sh_size - shdr->sh_entsize;
2751 irela = internal_relocs;
2752 while (erela <= erelaend)
2754 bfd_vma r_symndx;
2756 (*swap_in) (abfd, erela, irela);
2757 r_symndx = ELF32_R_SYM (irela->r_info);
2758 if (bed->s->arch_size == 64)
2759 r_symndx >>= 24;
2760 if (nsyms > 0)
2762 if ((size_t) r_symndx >= nsyms)
2764 _bfd_error_handler
2765 /* xgettext:c-format */
2766 (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
2767 " for offset %#" PRIx64 " in section `%pA'"),
2768 abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
2769 (uint64_t) irela->r_offset, sec);
2770 bfd_set_error (bfd_error_bad_value);
2771 return false;
2774 else if (r_symndx != STN_UNDEF)
2776 _bfd_error_handler
2777 /* xgettext:c-format */
2778 (_("%pB: non-zero symbol index (%#" PRIx64 ")"
2779 " for offset %#" PRIx64 " in section `%pA'"
2780 " when the object file has no symbol table"),
2781 abfd, (uint64_t) r_symndx,
2782 (uint64_t) irela->r_offset, sec);
2783 bfd_set_error (bfd_error_bad_value);
2784 return false;
2786 irela += bed->s->int_rels_per_ext_rel;
2787 erela += shdr->sh_entsize;
2790 return true;
2793 /* Read and swap the relocs for a section O. They may have been
2794 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2795 not NULL, they are used as buffers to read into. They are known to
2796 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2797 the return value is allocated using either malloc or bfd_alloc,
2798 according to the KEEP_MEMORY argument. If O has two relocation
2799 sections (both REL and RELA relocations), then the REL_HDR
2800 relocations will appear first in INTERNAL_RELOCS, followed by the
2801 RELA_HDR relocations. If INFO isn't NULL and KEEP_MEMORY is true,
2802 update cache_size. */
2804 Elf_Internal_Rela *
2805 _bfd_elf_link_info_read_relocs (bfd *abfd,
2806 struct bfd_link_info *info,
2807 const asection *o,
2808 void *external_relocs,
2809 Elf_Internal_Rela *internal_relocs,
2810 bool keep_memory)
2812 void *alloc1 = NULL;
2813 size_t alloc1_size;
2814 Elf_Internal_Rela *alloc2 = NULL;
2815 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2816 struct bfd_elf_section_data *esdo = elf_section_data (o);
2817 Elf_Internal_Rela *internal_rela_relocs;
2819 if (esdo->relocs != NULL)
2820 return esdo->relocs;
2822 if (o->reloc_count == 0)
2823 return NULL;
2825 if (internal_relocs == NULL)
2827 bfd_size_type size;
2829 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
2830 if (keep_memory)
2832 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2833 if (info)
2834 info->cache_size += size;
2836 else
2837 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2838 if (internal_relocs == NULL)
2839 return NULL;
2842 alloc1 = external_relocs;
2843 internal_rela_relocs = internal_relocs;
2844 if (esdo->rel.hdr)
2846 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2847 &alloc1, &alloc1_size,
2848 internal_relocs))
2849 goto error_return;
2850 external_relocs = (((bfd_byte *) external_relocs)
2851 + esdo->rel.hdr->sh_size);
2852 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2853 * bed->s->int_rels_per_ext_rel);
2856 if (esdo->rela.hdr
2857 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2858 &alloc1, &alloc1_size,
2859 internal_rela_relocs)))
2860 goto error_return;
2862 /* Cache the results for next time, if we can. */
2863 if (keep_memory)
2864 esdo->relocs = internal_relocs;
2866 _bfd_munmap_readonly_temporary (alloc1, alloc1_size);
2868 /* Don't free alloc2, since if it was allocated we are passing it
2869 back (under the name of internal_relocs). */
2871 return internal_relocs;
2873 error_return:
2874 _bfd_munmap_readonly_temporary (alloc1, alloc1_size);
2875 if (alloc2 != NULL)
2877 if (keep_memory)
2878 bfd_release (abfd, alloc2);
2879 else
2880 free (alloc2);
2882 return NULL;
2885 /* This is similar to _bfd_elf_link_info_read_relocs, except for that
2886 NULL is passed to _bfd_elf_link_info_read_relocs for pointer to
2887 struct bfd_link_info. */
2889 Elf_Internal_Rela *
2890 _bfd_elf_link_read_relocs (bfd *abfd,
2891 const asection *o,
2892 void *external_relocs,
2893 Elf_Internal_Rela *internal_relocs,
2894 bool keep_memory)
2896 return _bfd_elf_link_info_read_relocs (abfd, NULL, o, external_relocs,
2897 internal_relocs, keep_memory);
2901 /* Compute the size of, and allocate space for, REL_HDR which is the
2902 section header for a section containing relocations for O. */
2904 static bool
2905 _bfd_elf_link_size_reloc_section (bfd *abfd,
2906 struct bfd_elf_section_reloc_data *reldata)
2908 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2910 /* That allows us to calculate the size of the section. */
2911 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2913 /* The contents field must last into write_object_contents, so we
2914 allocate it with bfd_alloc rather than malloc. Also since we
2915 cannot be sure that the contents will actually be filled in,
2916 we zero the allocated space. */
2917 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2918 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2919 return false;
2921 if (reldata->hashes == NULL && reldata->count)
2923 struct elf_link_hash_entry **p;
2925 p = ((struct elf_link_hash_entry **)
2926 bfd_zmalloc (reldata->count * sizeof (*p)));
2927 if (p == NULL)
2928 return false;
2930 reldata->hashes = p;
2933 return true;
2936 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2937 originated from the section given by INPUT_REL_HDR) to the
2938 OUTPUT_BFD. */
2940 bool
2941 _bfd_elf_link_output_relocs (bfd *output_bfd,
2942 asection *input_section,
2943 Elf_Internal_Shdr *input_rel_hdr,
2944 Elf_Internal_Rela *internal_relocs,
2945 struct elf_link_hash_entry **rel_hash)
2947 Elf_Internal_Rela *irela;
2948 Elf_Internal_Rela *irelaend;
2949 bfd_byte *erel;
2950 struct bfd_elf_section_reloc_data *output_reldata;
2951 asection *output_section;
2952 const struct elf_backend_data *bed;
2953 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2954 struct bfd_elf_section_data *esdo;
2956 output_section = input_section->output_section;
2958 bed = get_elf_backend_data (output_bfd);
2959 esdo = elf_section_data (output_section);
2960 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2962 output_reldata = &esdo->rel;
2963 swap_out = bed->s->swap_reloc_out;
2965 else if (esdo->rela.hdr
2966 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2968 output_reldata = &esdo->rela;
2969 swap_out = bed->s->swap_reloca_out;
2971 else
2973 _bfd_error_handler
2974 /* xgettext:c-format */
2975 (_("%pB: relocation size mismatch in %pB section %pA"),
2976 output_bfd, input_section->owner, input_section);
2977 bfd_set_error (bfd_error_wrong_format);
2978 return false;
2981 erel = output_reldata->hdr->contents;
2982 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2983 irela = internal_relocs;
2984 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2985 * bed->s->int_rels_per_ext_rel);
2986 while (irela < irelaend)
2988 if (rel_hash && *rel_hash)
2989 (*rel_hash)->has_reloc = 1;
2990 (*swap_out) (output_bfd, irela, erel);
2991 irela += bed->s->int_rels_per_ext_rel;
2992 erel += input_rel_hdr->sh_entsize;
2993 if (rel_hash)
2994 rel_hash++;
2997 /* Bump the counter, so that we know where to add the next set of
2998 relocations. */
2999 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
3001 return true;
3004 /* Make weak undefined symbols in PIE dynamic. */
3006 bool
3007 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
3008 struct elf_link_hash_entry *h)
3010 if (bfd_link_pie (info)
3011 && h->dynindx == -1
3012 && h->root.type == bfd_link_hash_undefweak)
3013 return bfd_elf_link_record_dynamic_symbol (info, h);
3015 return true;
3018 /* Fix up the flags for a symbol. This handles various cases which
3019 can only be fixed after all the input files are seen. This is
3020 currently called by both adjust_dynamic_symbol and
3021 assign_sym_version, which is unnecessary but perhaps more robust in
3022 the face of future changes. */
3024 static bool
3025 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
3026 struct elf_info_failed *eif)
3028 const struct elf_backend_data *bed;
3030 /* If this symbol was mentioned in a non-ELF file, try to set
3031 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
3032 permit a non-ELF file to correctly refer to a symbol defined in
3033 an ELF dynamic object. */
3034 if (h->non_elf)
3036 while (h->root.type == bfd_link_hash_indirect)
3037 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3039 if (h->root.type != bfd_link_hash_defined
3040 && h->root.type != bfd_link_hash_defweak)
3042 h->ref_regular = 1;
3043 h->ref_regular_nonweak = 1;
3045 else
3047 if (h->root.u.def.section->owner != NULL
3048 && (bfd_get_flavour (h->root.u.def.section->owner)
3049 == bfd_target_elf_flavour))
3051 h->ref_regular = 1;
3052 h->ref_regular_nonweak = 1;
3054 else
3055 h->def_regular = 1;
3058 if (h->dynindx == -1
3059 && (h->def_dynamic
3060 || h->ref_dynamic))
3062 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
3064 eif->failed = true;
3065 return false;
3069 else
3071 /* Unfortunately, NON_ELF is only correct if the symbol
3072 was first seen in a non-ELF file. Fortunately, if the symbol
3073 was first seen in an ELF file, we're probably OK unless the
3074 symbol was defined in a non-ELF file. Catch that case here.
3075 FIXME: We're still in trouble if the symbol was first seen in
3076 a dynamic object, and then later in a non-ELF regular object. */
3077 if ((h->root.type == bfd_link_hash_defined
3078 || h->root.type == bfd_link_hash_defweak)
3079 && !h->def_regular
3080 && (h->root.u.def.section->owner != NULL
3081 ? (bfd_get_flavour (h->root.u.def.section->owner)
3082 != bfd_target_elf_flavour)
3083 : (bfd_is_abs_section (h->root.u.def.section)
3084 && !h->def_dynamic)))
3085 h->def_regular = 1;
3088 /* Backend specific symbol fixup. */
3089 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
3090 if (bed->elf_backend_fixup_symbol
3091 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
3092 return false;
3094 /* If this is a final link, and the symbol was defined as a common
3095 symbol in a regular object file, and there was no definition in
3096 any dynamic object, then the linker will have allocated space for
3097 the symbol in a common section but the DEF_REGULAR
3098 flag will not have been set. */
3099 if (h->root.type == bfd_link_hash_defined
3100 && !h->def_regular
3101 && h->ref_regular
3102 && !h->def_dynamic
3103 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
3104 h->def_regular = 1;
3106 /* Symbols defined in discarded sections shouldn't be dynamic. */
3107 if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
3108 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3110 /* If a weak undefined symbol has non-default visibility, we also
3111 hide it from the dynamic linker. */
3112 else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3113 && h->root.type == bfd_link_hash_undefweak)
3114 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3116 /* A hidden versioned symbol in executable should be forced local if
3117 it is is locally defined, not referenced by shared library and not
3118 exported. */
3119 else if (bfd_link_executable (eif->info)
3120 && h->versioned == versioned_hidden
3121 && !eif->info->export_dynamic
3122 && !h->dynamic
3123 && !h->ref_dynamic
3124 && h->def_regular)
3125 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3127 /* If -Bsymbolic was used (which means to bind references to global
3128 symbols to the definition within the shared object), and this
3129 symbol was defined in a regular object, then it actually doesn't
3130 need a PLT entry. Likewise, if the symbol has non-default
3131 visibility. If the symbol has hidden or internal visibility, we
3132 will force it local. */
3133 else if (h->needs_plt
3134 && bfd_link_pic (eif->info)
3135 && is_elf_hash_table (eif->info->hash)
3136 && (SYMBOLIC_BIND (eif->info, h)
3137 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
3138 && h->def_regular)
3140 bool force_local;
3142 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
3143 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
3144 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
3147 /* If this is a weak defined symbol in a dynamic object, and we know
3148 the real definition in the dynamic object, copy interesting flags
3149 over to the real definition. */
3150 if (h->is_weakalias)
3152 struct elf_link_hash_entry *def = weakdef (h);
3154 /* If the real definition is defined by a regular object file,
3155 don't do anything special. See the longer description in
3156 _bfd_elf_adjust_dynamic_symbol, below. If the def is not
3157 bfd_link_hash_defined as it was when put on the alias list
3158 then it must have originally been a versioned symbol (for
3159 which a non-versioned indirect symbol is created) and later
3160 a definition for the non-versioned symbol is found. In that
3161 case the indirection is flipped with the versioned symbol
3162 becoming an indirect pointing at the non-versioned symbol.
3163 Thus, not an alias any more. */
3164 if (def->def_regular
3165 || def->root.type != bfd_link_hash_defined)
3167 h = def;
3168 while ((h = h->u.alias) != def)
3169 h->is_weakalias = 0;
3171 else
3173 while (h->root.type == bfd_link_hash_indirect)
3174 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3175 BFD_ASSERT (h->root.type == bfd_link_hash_defined
3176 || h->root.type == bfd_link_hash_defweak);
3177 BFD_ASSERT (def->def_dynamic);
3178 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
3182 return true;
3185 /* Make the backend pick a good value for a dynamic symbol. This is
3186 called via elf_link_hash_traverse, and also calls itself
3187 recursively. */
3189 static bool
3190 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
3192 struct elf_info_failed *eif = (struct elf_info_failed *) data;
3193 struct elf_link_hash_table *htab;
3194 const struct elf_backend_data *bed;
3196 if (! is_elf_hash_table (eif->info->hash))
3197 return false;
3199 /* Ignore indirect symbols. These are added by the versioning code. */
3200 if (h->root.type == bfd_link_hash_indirect)
3201 return true;
3203 /* Fix the symbol flags. */
3204 if (! _bfd_elf_fix_symbol_flags (h, eif))
3205 return false;
3207 htab = elf_hash_table (eif->info);
3208 bed = get_elf_backend_data (htab->dynobj);
3210 if (h->root.type == bfd_link_hash_undefweak)
3212 if (eif->info->dynamic_undefined_weak == 0)
3213 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3214 else if (eif->info->dynamic_undefined_weak > 0
3215 && h->ref_regular
3216 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3217 && !bfd_hide_sym_by_version (eif->info->version_info,
3218 h->root.root.string))
3220 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
3222 eif->failed = true;
3223 return false;
3228 /* If this symbol does not require a PLT entry, and it is not
3229 defined by a dynamic object, or is not referenced by a regular
3230 object, ignore it. We do have to handle a weak defined symbol,
3231 even if no regular object refers to it, if we decided to add it
3232 to the dynamic symbol table. FIXME: Do we normally need to worry
3233 about symbols which are defined by one dynamic object and
3234 referenced by another one? */
3235 if (!h->needs_plt
3236 && h->type != STT_GNU_IFUNC
3237 && (h->def_regular
3238 || !h->def_dynamic
3239 || (!h->ref_regular
3240 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
3242 h->plt = elf_hash_table (eif->info)->init_plt_offset;
3243 return true;
3246 /* If we've already adjusted this symbol, don't do it again. This
3247 can happen via a recursive call. */
3248 if (h->dynamic_adjusted)
3249 return true;
3251 /* Don't look at this symbol again. Note that we must set this
3252 after checking the above conditions, because we may look at a
3253 symbol once, decide not to do anything, and then get called
3254 recursively later after REF_REGULAR is set below. */
3255 h->dynamic_adjusted = 1;
3257 /* If this is a weak definition, and we know a real definition, and
3258 the real symbol is not itself defined by a regular object file,
3259 then get a good value for the real definition. We handle the
3260 real symbol first, for the convenience of the backend routine.
3262 Note that there is a confusing case here. If the real definition
3263 is defined by a regular object file, we don't get the real symbol
3264 from the dynamic object, but we do get the weak symbol. If the
3265 processor backend uses a COPY reloc, then if some routine in the
3266 dynamic object changes the real symbol, we will not see that
3267 change in the corresponding weak symbol. This is the way other
3268 ELF linkers work as well, and seems to be a result of the shared
3269 library model.
3271 I will clarify this issue. Most SVR4 shared libraries define the
3272 variable _timezone and define timezone as a weak synonym. The
3273 tzset call changes _timezone. If you write
3274 extern int timezone;
3275 int _timezone = 5;
3276 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3277 you might expect that, since timezone is a synonym for _timezone,
3278 the same number will print both times. However, if the processor
3279 backend uses a COPY reloc, then actually timezone will be copied
3280 into your process image, and, since you define _timezone
3281 yourself, _timezone will not. Thus timezone and _timezone will
3282 wind up at different memory locations. The tzset call will set
3283 _timezone, leaving timezone unchanged. */
3285 if (h->is_weakalias)
3287 struct elf_link_hash_entry *def = weakdef (h);
3289 /* If we get to this point, there is an implicit reference to
3290 the alias by a regular object file via the weak symbol H. */
3291 def->ref_regular = 1;
3293 /* Ensure that the backend adjust_dynamic_symbol function sees
3294 the strong alias before H by recursively calling ourselves. */
3295 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
3296 return false;
3299 /* If a symbol has no type and no size and does not require a PLT
3300 entry, then we are probably about to do the wrong thing here: we
3301 are probably going to create a COPY reloc for an empty object.
3302 This case can arise when a shared object is built with assembly
3303 code, and the assembly code fails to set the symbol type. */
3304 if (h->size == 0
3305 && h->type == STT_NOTYPE
3306 && !h->needs_plt)
3307 _bfd_error_handler
3308 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3309 h->root.root.string);
3311 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3313 eif->failed = true;
3314 return false;
3317 return true;
3320 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3321 DYNBSS. */
3323 bool
3324 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3325 struct elf_link_hash_entry *h,
3326 asection *dynbss)
3328 unsigned int power_of_two;
3329 bfd_vma mask;
3330 asection *sec = h->root.u.def.section;
3332 /* The section alignment of the definition is the maximum alignment
3333 requirement of symbols defined in the section. Since we don't
3334 know the symbol alignment requirement, we start with the
3335 maximum alignment and check low bits of the symbol address
3336 for the minimum alignment. */
3337 power_of_two = bfd_section_alignment (sec);
3338 mask = ((bfd_vma) 1 << power_of_two) - 1;
3339 while ((h->root.u.def.value & mask) != 0)
3341 mask >>= 1;
3342 --power_of_two;
3345 if (power_of_two > bfd_section_alignment (dynbss))
3347 /* Adjust the section alignment if needed. */
3348 if (!bfd_set_section_alignment (dynbss, power_of_two))
3349 return false;
3352 /* We make sure that the symbol will be aligned properly. */
3353 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3355 /* Define the symbol as being at this point in DYNBSS. */
3356 h->root.u.def.section = dynbss;
3357 h->root.u.def.value = dynbss->size;
3359 /* Increment the size of DYNBSS to make room for the symbol. */
3360 dynbss->size += h->size;
3362 /* No error if extern_protected_data is true. */
3363 if (h->protected_def
3364 && (!info->extern_protected_data
3365 || (info->extern_protected_data < 0
3366 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
3367 info->callbacks->einfo
3368 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3369 h->root.root.string);
3371 return true;
3374 /* Adjust all external symbols pointing into SEC_MERGE sections
3375 to reflect the object merging within the sections. */
3377 static bool
3378 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
3380 asection *sec;
3382 if ((h->root.type == bfd_link_hash_defined
3383 || h->root.type == bfd_link_hash_defweak)
3384 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
3385 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3387 bfd *output_bfd = (bfd *) data;
3389 h->root.u.def.value =
3390 _bfd_merged_section_offset (output_bfd,
3391 &h->root.u.def.section,
3392 elf_section_data (sec)->sec_info,
3393 h->root.u.def.value);
3396 return true;
3399 /* Returns false if the symbol referred to by H should be considered
3400 to resolve local to the current module, and true if it should be
3401 considered to bind dynamically. */
3403 bool
3404 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3405 struct bfd_link_info *info,
3406 bool not_local_protected)
3408 bool binding_stays_local_p;
3409 const struct elf_backend_data *bed;
3410 struct elf_link_hash_table *hash_table;
3412 if (h == NULL)
3413 return false;
3415 while (h->root.type == bfd_link_hash_indirect
3416 || h->root.type == bfd_link_hash_warning)
3417 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3419 /* If it was forced local, then clearly it's not dynamic. */
3420 if (h->dynindx == -1)
3421 return false;
3422 if (h->forced_local)
3423 return false;
3425 /* Identify the cases where name binding rules say that a
3426 visible symbol resolves locally. */
3427 binding_stays_local_p = (bfd_link_executable (info)
3428 || SYMBOLIC_BIND (info, h));
3430 switch (ELF_ST_VISIBILITY (h->other))
3432 case STV_INTERNAL:
3433 case STV_HIDDEN:
3434 return false;
3436 case STV_PROTECTED:
3437 hash_table = elf_hash_table (info);
3438 if (!is_elf_hash_table (&hash_table->root))
3439 return false;
3441 bed = get_elf_backend_data (hash_table->dynobj);
3443 /* Proper resolution for function pointer equality may require
3444 that these symbols perhaps be resolved dynamically, even though
3445 we should be resolving them to the current module. */
3446 if (!not_local_protected || !bed->is_function_type (h->type))
3447 binding_stays_local_p = true;
3448 break;
3450 default:
3451 break;
3454 /* If it isn't defined locally, then clearly it's dynamic. */
3455 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
3456 return true;
3458 /* Otherwise, the symbol is dynamic if binding rules don't tell
3459 us that it remains local. */
3460 return !binding_stays_local_p;
3463 /* Return true if the symbol referred to by H should be considered
3464 to resolve local to the current module, and false otherwise. Differs
3465 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3466 undefined symbols. The two functions are virtually identical except
3467 for the place where dynindx == -1 is tested. If that test is true,
3468 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3469 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3470 defined symbols.
3471 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3472 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3473 treatment of undefined weak symbols. For those that do not make
3474 undefined weak symbols dynamic, both functions may return false. */
3476 bool
3477 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3478 struct bfd_link_info *info,
3479 bool local_protected)
3481 const struct elf_backend_data *bed;
3482 struct elf_link_hash_table *hash_table;
3484 /* If it's a local sym, of course we resolve locally. */
3485 if (h == NULL)
3486 return true;
3488 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3489 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3490 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3491 return true;
3493 /* Forced local symbols resolve locally. */
3494 if (h->forced_local)
3495 return true;
3497 /* Common symbols that become definitions don't get the DEF_REGULAR
3498 flag set, so test it first, and don't bail out. */
3499 if (ELF_COMMON_DEF_P (h))
3500 /* Do nothing. */;
3501 /* If we don't have a definition in a regular file, then we can't
3502 resolve locally. The sym is either undefined or dynamic. */
3503 else if (!h->def_regular)
3504 return false;
3506 /* Non-dynamic symbols resolve locally. */
3507 if (h->dynindx == -1)
3508 return true;
3510 /* At this point, we know the symbol is defined and dynamic. In an
3511 executable it must resolve locally, likewise when building symbolic
3512 shared libraries. */
3513 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3514 return true;
3516 /* Now deal with defined dynamic symbols in shared libraries. Ones
3517 with default visibility might not resolve locally. */
3518 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3519 return false;
3521 hash_table = elf_hash_table (info);
3522 if (!is_elf_hash_table (&hash_table->root))
3523 return true;
3525 /* STV_PROTECTED symbols with indirect external access are local. */
3526 if (info->indirect_extern_access > 0)
3527 return true;
3529 bed = get_elf_backend_data (hash_table->dynobj);
3531 /* If extern_protected_data is false, STV_PROTECTED non-function
3532 symbols are local. */
3533 if ((!info->extern_protected_data
3534 || (info->extern_protected_data < 0
3535 && !bed->extern_protected_data))
3536 && !bed->is_function_type (h->type))
3537 return true;
3539 /* Function pointer equality tests may require that STV_PROTECTED
3540 symbols be treated as dynamic symbols. If the address of a
3541 function not defined in an executable is set to that function's
3542 plt entry in the executable, then the address of the function in
3543 a shared library must also be the plt entry in the executable. */
3544 return local_protected;
3547 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3548 aligned. Returns the first TLS output section. */
3550 struct bfd_section *
3551 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3553 struct bfd_section *sec, *tls;
3554 unsigned int align = 0;
3556 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3557 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3558 break;
3559 tls = sec;
3561 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3562 if (sec->alignment_power > align)
3563 align = sec->alignment_power;
3565 elf_hash_table (info)->tls_sec = tls;
3567 /* Ensure the alignment of the first section (usually .tdata) is the largest
3568 alignment, so that the tls segment starts aligned. */
3569 if (tls != NULL)
3570 tls->alignment_power = align;
3572 return tls;
3575 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3576 static bool
3577 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3578 Elf_Internal_Sym *sym)
3580 const struct elf_backend_data *bed;
3582 /* Local symbols do not count, but target specific ones might. */
3583 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3584 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3585 return false;
3587 bed = get_elf_backend_data (abfd);
3588 /* Function symbols do not count. */
3589 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3590 return false;
3592 /* If the section is undefined, then so is the symbol. */
3593 if (sym->st_shndx == SHN_UNDEF)
3594 return false;
3596 /* If the symbol is defined in the common section, then
3597 it is a common definition and so does not count. */
3598 if (bed->common_definition (sym))
3599 return false;
3601 /* If the symbol is in a target specific section then we
3602 must rely upon the backend to tell us what it is. */
3603 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3604 /* FIXME - this function is not coded yet:
3606 return _bfd_is_global_symbol_definition (abfd, sym);
3608 Instead for now assume that the definition is not global,
3609 Even if this is wrong, at least the linker will behave
3610 in the same way that it used to do. */
3611 return false;
3613 return true;
3616 /* Search the symbol table of the archive element of the archive ABFD
3617 whose archive map contains a mention of SYMDEF, and determine if
3618 the symbol is defined in this element. */
3619 static bool
3620 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3622 Elf_Internal_Shdr * hdr;
3623 size_t symcount;
3624 size_t extsymcount;
3625 size_t extsymoff;
3626 Elf_Internal_Sym *isymbuf;
3627 Elf_Internal_Sym *isym;
3628 Elf_Internal_Sym *isymend;
3629 bool result;
3631 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset, NULL);
3632 if (abfd == NULL)
3633 return false;
3635 if (! bfd_check_format (abfd, bfd_object))
3636 return false;
3638 /* Select the appropriate symbol table. If we don't know if the
3639 object file is an IR object, give linker LTO plugin a chance to
3640 get the correct symbol table. */
3641 if (abfd->plugin_format == bfd_plugin_yes
3642 #if BFD_SUPPORTS_PLUGINS
3643 || (abfd->plugin_format == bfd_plugin_unknown
3644 && bfd_link_plugin_object_p (abfd))
3645 #endif
3648 /* Use the IR symbol table if the object has been claimed by
3649 plugin. */
3650 abfd = abfd->plugin_dummy_bfd;
3651 hdr = &elf_tdata (abfd)->symtab_hdr;
3653 else
3655 if (elf_use_dt_symtab_p (abfd))
3657 bfd_set_error (bfd_error_wrong_format);
3658 return false;
3661 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3662 hdr = &elf_tdata (abfd)->symtab_hdr;
3663 else
3664 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3667 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3669 /* The sh_info field of the symtab header tells us where the
3670 external symbols start. We don't care about the local symbols. */
3671 if (elf_bad_symtab (abfd))
3673 extsymcount = symcount;
3674 extsymoff = 0;
3676 else
3678 extsymcount = symcount - hdr->sh_info;
3679 extsymoff = hdr->sh_info;
3682 if (extsymcount == 0)
3683 return false;
3685 /* Read in the symbol table. */
3686 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3687 NULL, NULL, NULL);
3688 if (isymbuf == NULL)
3689 return false;
3691 /* Scan the symbol table looking for SYMDEF. */
3692 result = false;
3693 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3695 const char *name;
3697 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3698 isym->st_name);
3699 if (name == NULL)
3700 break;
3702 if (strcmp (name, symdef->name) == 0)
3704 result = is_global_data_symbol_definition (abfd, isym);
3705 break;
3709 free (isymbuf);
3711 return result;
3714 /* Add an entry to the .dynamic table. */
3716 bool
3717 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3718 bfd_vma tag,
3719 bfd_vma val)
3721 struct elf_link_hash_table *hash_table;
3722 const struct elf_backend_data *bed;
3723 asection *s;
3724 bfd_size_type newsize;
3725 bfd_byte *newcontents;
3726 Elf_Internal_Dyn dyn;
3728 hash_table = elf_hash_table (info);
3729 if (! is_elf_hash_table (&hash_table->root))
3730 return false;
3732 if (tag == DT_RELA || tag == DT_REL)
3733 hash_table->dynamic_relocs = true;
3735 bed = get_elf_backend_data (hash_table->dynobj);
3736 s = hash_table->dynamic;
3737 BFD_ASSERT (s != NULL);
3739 newsize = s->size + bed->s->sizeof_dyn;
3740 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3741 if (newcontents == NULL)
3742 return false;
3744 dyn.d_tag = tag;
3745 dyn.d_un.d_val = val;
3746 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3748 s->size = newsize;
3749 s->contents = newcontents;
3751 return true;
3754 /* Strip zero-sized dynamic sections. */
3756 bool
3757 _bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info *info)
3759 struct elf_link_hash_table *hash_table;
3760 const struct elf_backend_data *bed;
3761 asection *s, *sdynamic, **pp;
3762 asection *rela_dyn, *rel_dyn;
3763 Elf_Internal_Dyn dyn;
3764 bfd_byte *extdyn, *next;
3765 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3766 bool strip_zero_sized;
3767 bool strip_zero_sized_plt;
3769 if (bfd_link_relocatable (info))
3770 return true;
3772 hash_table = elf_hash_table (info);
3773 if (!is_elf_hash_table (&hash_table->root))
3774 return false;
3776 if (!hash_table->dynobj)
3777 return true;
3779 sdynamic= hash_table->dynamic;
3780 if (!sdynamic)
3781 return true;
3783 bed = get_elf_backend_data (hash_table->dynobj);
3784 swap_dyn_in = bed->s->swap_dyn_in;
3786 strip_zero_sized = false;
3787 strip_zero_sized_plt = false;
3789 /* Strip zero-sized dynamic sections. */
3790 rela_dyn = bfd_get_section_by_name (info->output_bfd, ".rela.dyn");
3791 rel_dyn = bfd_get_section_by_name (info->output_bfd, ".rel.dyn");
3792 for (pp = &info->output_bfd->sections; (s = *pp) != NULL;)
3793 if (s->size == 0
3794 && (s == rela_dyn
3795 || s == rel_dyn
3796 || s == hash_table->srelplt->output_section
3797 || s == hash_table->splt->output_section))
3799 *pp = s->next;
3800 info->output_bfd->section_count--;
3801 strip_zero_sized = true;
3802 if (s == rela_dyn)
3803 s = rela_dyn;
3804 if (s == rel_dyn)
3805 s = rel_dyn;
3806 else if (s == hash_table->splt->output_section)
3808 s = hash_table->splt;
3809 strip_zero_sized_plt = true;
3811 else
3812 s = hash_table->srelplt;
3813 s->flags |= SEC_EXCLUDE;
3814 s->output_section = bfd_abs_section_ptr;
3816 else
3817 pp = &s->next;
3819 if (strip_zero_sized_plt && sdynamic->size != 0)
3820 for (extdyn = sdynamic->contents;
3821 extdyn < sdynamic->contents + sdynamic->size;
3822 extdyn = next)
3824 next = extdyn + bed->s->sizeof_dyn;
3825 swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3826 switch (dyn.d_tag)
3828 default:
3829 break;
3830 case DT_JMPREL:
3831 case DT_PLTRELSZ:
3832 case DT_PLTREL:
3833 /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
3834 the procedure linkage table (the .plt section) has been
3835 removed. */
3836 memmove (extdyn, next,
3837 sdynamic->size - (next - sdynamic->contents));
3838 next = extdyn;
3842 if (strip_zero_sized)
3844 /* Regenerate program headers. */
3845 elf_seg_map (info->output_bfd) = NULL;
3846 return _bfd_elf_map_sections_to_segments (info->output_bfd, info,
3847 NULL);
3850 return true;
3853 /* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error,
3854 1 if a DT_NEEDED tag already exists, and 0 on success. */
3857 bfd_elf_add_dt_needed_tag (bfd *abfd, struct bfd_link_info *info)
3859 struct elf_link_hash_table *hash_table;
3860 size_t strindex;
3861 const char *soname;
3863 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3864 return -1;
3866 hash_table = elf_hash_table (info);
3867 soname = elf_dt_name (abfd);
3868 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, false);
3869 if (strindex == (size_t) -1)
3870 return -1;
3872 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3874 asection *sdyn;
3875 const struct elf_backend_data *bed;
3876 bfd_byte *extdyn;
3878 bed = get_elf_backend_data (hash_table->dynobj);
3879 sdyn = hash_table->dynamic;
3880 if (sdyn != NULL && sdyn->size != 0)
3881 for (extdyn = sdyn->contents;
3882 extdyn < sdyn->contents + sdyn->size;
3883 extdyn += bed->s->sizeof_dyn)
3885 Elf_Internal_Dyn dyn;
3887 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3888 if (dyn.d_tag == DT_NEEDED
3889 && dyn.d_un.d_val == strindex)
3891 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3892 return 1;
3897 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3898 return -1;
3900 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3901 return -1;
3903 return 0;
3906 /* Return true if SONAME is on the needed list between NEEDED and STOP
3907 (or the end of list if STOP is NULL), and needed by a library that
3908 will be loaded. */
3910 static bool
3911 on_needed_list (const char *soname,
3912 struct bfd_link_needed_list *needed,
3913 struct bfd_link_needed_list *stop)
3915 struct bfd_link_needed_list *look;
3916 for (look = needed; look != stop; look = look->next)
3917 if (strcmp (soname, look->name) == 0
3918 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3919 /* If needed by a library that itself is not directly
3920 needed, recursively check whether that library is
3921 indirectly needed. Since we add DT_NEEDED entries to
3922 the end of the list, library dependencies appear after
3923 the library. Therefore search prior to the current
3924 LOOK, preventing possible infinite recursion. */
3925 || on_needed_list (elf_dt_name (look->by), needed, look)))
3926 return true;
3928 return false;
3931 /* Sort symbol by value, section, size, and type. */
3932 static int
3933 elf_sort_symbol (const void *arg1, const void *arg2)
3935 const struct elf_link_hash_entry *h1;
3936 const struct elf_link_hash_entry *h2;
3937 bfd_signed_vma vdiff;
3938 int sdiff;
3939 const char *n1;
3940 const char *n2;
3942 h1 = *(const struct elf_link_hash_entry **) arg1;
3943 h2 = *(const struct elf_link_hash_entry **) arg2;
3944 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3945 if (vdiff != 0)
3946 return vdiff > 0 ? 1 : -1;
3948 sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3949 if (sdiff != 0)
3950 return sdiff;
3952 /* Sort so that sized symbols are selected over zero size symbols. */
3953 vdiff = h1->size - h2->size;
3954 if (vdiff != 0)
3955 return vdiff > 0 ? 1 : -1;
3957 /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */
3958 if (h1->type != h2->type)
3959 return h1->type - h2->type;
3961 /* If symbols are properly sized and typed, and multiple strong
3962 aliases are not defined in a shared library by the user we
3963 shouldn't get here. Unfortunately linker script symbols like
3964 __bss_start sometimes match a user symbol defined at the start of
3965 .bss without proper size and type. We'd like to preference the
3966 user symbol over reserved system symbols. Sort on leading
3967 underscores. */
3968 n1 = h1->root.root.string;
3969 n2 = h2->root.root.string;
3970 while (*n1 == *n2)
3972 if (*n1 == 0)
3973 break;
3974 ++n1;
3975 ++n2;
3977 if (*n1 == '_')
3978 return -1;
3979 if (*n2 == '_')
3980 return 1;
3982 /* Final sort on name selects user symbols like '_u' over reserved
3983 system symbols like '_Z' and also will avoid qsort instability. */
3984 return *n1 - *n2;
3987 /* This function is used to adjust offsets into .dynstr for
3988 dynamic symbols. This is called via elf_link_hash_traverse. */
3990 static bool
3991 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3993 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3995 if (h->dynindx != -1)
3996 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3997 return true;
4000 /* Assign string offsets in .dynstr, update all structures referencing
4001 them. */
4003 static bool
4004 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
4006 struct elf_link_hash_table *hash_table = elf_hash_table (info);
4007 struct elf_link_local_dynamic_entry *entry;
4008 struct elf_strtab_hash *dynstr = hash_table->dynstr;
4009 bfd *dynobj = hash_table->dynobj;
4010 asection *sdyn;
4011 bfd_size_type size;
4012 const struct elf_backend_data *bed;
4013 bfd_byte *extdyn;
4015 _bfd_elf_strtab_finalize (dynstr);
4016 size = _bfd_elf_strtab_size (dynstr);
4018 /* Allow the linker to examine the dynsymtab now it's fully populated. */
4020 if (info->callbacks->examine_strtab)
4021 info->callbacks->examine_strtab (dynstr);
4023 bed = get_elf_backend_data (dynobj);
4024 sdyn = hash_table->dynamic;
4025 BFD_ASSERT (sdyn != NULL);
4027 /* Update all .dynamic entries referencing .dynstr strings. */
4028 for (extdyn = sdyn->contents;
4029 extdyn < PTR_ADD (sdyn->contents, sdyn->size);
4030 extdyn += bed->s->sizeof_dyn)
4032 Elf_Internal_Dyn dyn;
4034 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
4035 switch (dyn.d_tag)
4037 case DT_STRSZ:
4038 dyn.d_un.d_val = size;
4039 break;
4040 case DT_NEEDED:
4041 case DT_SONAME:
4042 case DT_RPATH:
4043 case DT_RUNPATH:
4044 case DT_FILTER:
4045 case DT_AUXILIARY:
4046 case DT_AUDIT:
4047 case DT_DEPAUDIT:
4048 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
4049 break;
4050 default:
4051 continue;
4053 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
4056 /* Now update local dynamic symbols. */
4057 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
4058 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
4059 entry->isym.st_name);
4061 /* And the rest of dynamic symbols. */
4062 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
4064 /* Adjust version definitions. */
4065 if (elf_tdata (output_bfd)->cverdefs)
4067 asection *s;
4068 bfd_byte *p;
4069 size_t i;
4070 Elf_Internal_Verdef def;
4071 Elf_Internal_Verdaux defaux;
4073 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
4074 p = s->contents;
4077 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
4078 &def);
4079 p += sizeof (Elf_External_Verdef);
4080 if (def.vd_aux != sizeof (Elf_External_Verdef))
4081 continue;
4082 for (i = 0; i < def.vd_cnt; ++i)
4084 _bfd_elf_swap_verdaux_in (output_bfd,
4085 (Elf_External_Verdaux *) p, &defaux);
4086 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
4087 defaux.vda_name);
4088 _bfd_elf_swap_verdaux_out (output_bfd,
4089 &defaux, (Elf_External_Verdaux *) p);
4090 p += sizeof (Elf_External_Verdaux);
4093 while (def.vd_next);
4096 /* Adjust version references. */
4097 if (elf_tdata (output_bfd)->verref)
4099 asection *s;
4100 bfd_byte *p;
4101 size_t i;
4102 Elf_Internal_Verneed need;
4103 Elf_Internal_Vernaux needaux;
4105 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
4106 p = s->contents;
4109 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
4110 &need);
4111 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
4112 _bfd_elf_swap_verneed_out (output_bfd, &need,
4113 (Elf_External_Verneed *) p);
4114 p += sizeof (Elf_External_Verneed);
4115 for (i = 0; i < need.vn_cnt; ++i)
4117 _bfd_elf_swap_vernaux_in (output_bfd,
4118 (Elf_External_Vernaux *) p, &needaux);
4119 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
4120 needaux.vna_name);
4121 _bfd_elf_swap_vernaux_out (output_bfd,
4122 &needaux,
4123 (Elf_External_Vernaux *) p);
4124 p += sizeof (Elf_External_Vernaux);
4127 while (need.vn_next);
4130 return true;
4133 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4134 The default is to only match when the INPUT and OUTPUT are exactly
4135 the same target. */
4137 bool
4138 _bfd_elf_default_relocs_compatible (const bfd_target *input,
4139 const bfd_target *output)
4141 return input == output;
4144 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4145 This version is used when different targets for the same architecture
4146 are virtually identical. */
4148 bool
4149 _bfd_elf_relocs_compatible (const bfd_target *input,
4150 const bfd_target *output)
4152 const struct elf_backend_data *obed, *ibed;
4154 if (input == output)
4155 return true;
4157 ibed = xvec_get_elf_backend_data (input);
4158 obed = xvec_get_elf_backend_data (output);
4160 if (ibed->arch != obed->arch)
4161 return false;
4163 /* If both backends are using this function, deem them compatible. */
4164 return ibed->relocs_compatible == obed->relocs_compatible;
4167 /* Make a special call to the linker "notice" function to tell it that
4168 we are about to handle an as-needed lib, or have finished
4169 processing the lib. */
4171 bool
4172 _bfd_elf_notice_as_needed (bfd *ibfd,
4173 struct bfd_link_info *info,
4174 enum notice_asneeded_action act)
4176 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
4179 /* Call ACTION on each relocation in an ELF object file. */
4181 bool
4182 _bfd_elf_link_iterate_on_relocs
4183 (bfd *abfd, struct bfd_link_info *info,
4184 bool (*action) (bfd *, struct bfd_link_info *, asection *,
4185 const Elf_Internal_Rela *))
4187 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4188 struct elf_link_hash_table *htab = elf_hash_table (info);
4190 /* If this object is the same format as the output object, and it is
4191 not a shared library, then let the backend look through the
4192 relocs.
4194 This is required to build global offset table entries and to
4195 arrange for dynamic relocs. It is not required for the
4196 particular common case of linking non PIC code, even when linking
4197 against shared libraries, but unfortunately there is no way of
4198 knowing whether an object file has been compiled PIC or not.
4199 Looking through the relocs is not particularly time consuming.
4200 The problem is that we must either (1) keep the relocs in memory,
4201 which causes the linker to require additional runtime memory or
4202 (2) read the relocs twice from the input file, which wastes time.
4203 This would be a good case for using mmap.
4205 I have no idea how to handle linking PIC code into a file of a
4206 different format. It probably can't be done. */
4207 if ((abfd->flags & DYNAMIC) == 0
4208 && is_elf_hash_table (&htab->root)
4209 && elf_object_id (abfd) == elf_hash_table_id (htab)
4210 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4212 asection *o;
4214 for (o = abfd->sections; o != NULL; o = o->next)
4216 Elf_Internal_Rela *internal_relocs;
4217 bool ok;
4219 /* Don't check relocations in excluded sections. Don't do
4220 anything special with non-loaded, non-alloced sections.
4221 In particular, any relocs in such sections should not
4222 affect GOT and PLT reference counting (ie. we don't
4223 allow them to create GOT or PLT entries), there's no
4224 possibility or desire to optimize TLS relocs, and
4225 there's not much point in propagating relocs to shared
4226 libs that the dynamic linker won't relocate. */
4227 if ((o->flags & SEC_ALLOC) == 0
4228 || (o->flags & SEC_RELOC) == 0
4229 || (o->flags & SEC_EXCLUDE) != 0
4230 || o->reloc_count == 0
4231 || ((info->strip == strip_all || info->strip == strip_debugger)
4232 && (o->flags & SEC_DEBUGGING) != 0)
4233 || bfd_is_abs_section (o->output_section))
4234 continue;
4236 internal_relocs = _bfd_elf_link_info_read_relocs
4237 (abfd, info, o, NULL, NULL,
4238 _bfd_elf_link_keep_memory (info));
4239 if (internal_relocs == NULL)
4240 return false;
4242 ok = action (abfd, info, o, internal_relocs);
4244 if (elf_section_data (o)->relocs != internal_relocs)
4245 free (internal_relocs);
4247 if (! ok)
4248 return false;
4252 return true;
4255 /* Check relocations in an ELF object file. This is called after
4256 all input files have been opened. */
4258 bool
4259 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
4261 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4262 if (bed->check_relocs != NULL)
4263 return _bfd_elf_link_iterate_on_relocs (abfd, info,
4264 bed->check_relocs);
4265 return true;
4268 /* An entry in the first definition hash table. */
4270 struct elf_link_first_hash_entry
4272 struct bfd_hash_entry root;
4273 /* The object of the first definition. */
4274 bfd *abfd;
4277 /* The function to create a new entry in the first definition hash
4278 table. */
4280 static struct bfd_hash_entry *
4281 elf_link_first_hash_newfunc (struct bfd_hash_entry *entry,
4282 struct bfd_hash_table *table,
4283 const char *string)
4285 struct elf_link_first_hash_entry *ret =
4286 (struct elf_link_first_hash_entry *) entry;
4288 /* Allocate the structure if it has not already been allocated by a
4289 subclass. */
4290 if (ret == NULL)
4291 ret = (struct elf_link_first_hash_entry *)
4292 bfd_hash_allocate (table,
4293 sizeof (struct elf_link_first_hash_entry));
4294 if (ret == NULL)
4295 return NULL;
4297 /* Call the allocation method of the superclass. */
4298 ret = ((struct elf_link_first_hash_entry *)
4299 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table,
4300 string));
4301 if (ret != NULL)
4302 ret->abfd = NULL;
4304 return (struct bfd_hash_entry *) ret;
4307 /* Add the symbol NAME from ABFD to first hash. */
4309 static void
4310 elf_link_add_to_first_hash (bfd *abfd, struct bfd_link_info *info,
4311 const char *name, bool copy)
4313 struct elf_link_hash_table *htab = elf_hash_table (info);
4314 /* Skip if there is no first hash. */
4315 if (htab->first_hash == NULL)
4316 return;
4318 struct elf_link_first_hash_entry *e
4319 = ((struct elf_link_first_hash_entry *)
4320 bfd_hash_lookup (htab->first_hash, name, true, copy));
4321 if (e == NULL)
4322 info->callbacks->einfo
4323 (_("%F%P: %pB: failed to add %s to first hash\n"), abfd, name);
4325 if (e->abfd == NULL)
4326 /* Store ABFD in abfd. */
4327 e->abfd = abfd;
4330 /* Add symbols from an ELF object file to the linker hash table. */
4332 static bool
4333 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
4335 Elf_Internal_Ehdr *ehdr;
4336 Elf_Internal_Shdr *hdr;
4337 size_t symcount;
4338 size_t extsymcount;
4339 size_t extsymoff;
4340 struct elf_link_hash_entry **sym_hash;
4341 bool dynamic;
4342 Elf_External_Versym *extversym = NULL;
4343 Elf_External_Versym *extversym_end = NULL;
4344 Elf_External_Versym *ever;
4345 struct elf_link_hash_entry *weaks;
4346 struct elf_link_hash_entry **nondeflt_vers = NULL;
4347 size_t nondeflt_vers_cnt = 0;
4348 Elf_Internal_Sym *isymbuf = NULL;
4349 Elf_Internal_Sym *isym;
4350 Elf_Internal_Sym *isymend;
4351 const struct elf_backend_data *bed;
4352 bool add_needed;
4353 struct elf_link_hash_table *htab;
4354 void *alloc_mark = NULL;
4355 struct bfd_hash_entry **old_table = NULL;
4356 unsigned int old_size = 0;
4357 unsigned int old_count = 0;
4358 void *old_tab = NULL;
4359 void *old_ent;
4360 struct bfd_link_hash_entry *old_undefs = NULL;
4361 struct bfd_link_hash_entry *old_undefs_tail = NULL;
4362 void *old_strtab = NULL;
4363 size_t tabsize = 0;
4364 asection *s;
4365 bool just_syms;
4367 htab = elf_hash_table (info);
4368 bed = get_elf_backend_data (abfd);
4370 if (elf_use_dt_symtab_p (abfd))
4372 bfd_set_error (bfd_error_wrong_format);
4373 return false;
4376 if ((abfd->flags & DYNAMIC) == 0)
4378 dynamic = false;
4379 if ((abfd->flags & BFD_PLUGIN) != 0
4380 && is_elf_hash_table (&htab->root)
4381 && htab->first_hash == NULL)
4383 /* Initialize first_hash for an IR input. */
4384 htab->first_hash = (struct bfd_hash_table *)
4385 bfd_malloc (sizeof (struct bfd_hash_table));
4386 if (htab->first_hash == NULL
4387 || !bfd_hash_table_init
4388 (htab->first_hash, elf_link_first_hash_newfunc,
4389 sizeof (struct elf_link_first_hash_entry)))
4390 info->callbacks->einfo
4391 (_("%F%P: first_hash failed to create: %E\n"));
4394 else
4396 dynamic = true;
4398 /* You can't use -r against a dynamic object. Also, there's no
4399 hope of using a dynamic object which does not exactly match
4400 the format of the output file. */
4401 if (bfd_link_relocatable (info)
4402 || !is_elf_hash_table (&htab->root)
4403 || info->output_bfd->xvec != abfd->xvec)
4405 if (bfd_link_relocatable (info))
4406 bfd_set_error (bfd_error_invalid_operation);
4407 else
4408 bfd_set_error (bfd_error_wrong_format);
4409 goto error_return;
4413 ehdr = elf_elfheader (abfd);
4414 if (info->warn_alternate_em
4415 && bed->elf_machine_code != ehdr->e_machine
4416 && ((bed->elf_machine_alt1 != 0
4417 && ehdr->e_machine == bed->elf_machine_alt1)
4418 || (bed->elf_machine_alt2 != 0
4419 && ehdr->e_machine == bed->elf_machine_alt2)))
4420 _bfd_error_handler
4421 /* xgettext:c-format */
4422 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
4423 ehdr->e_machine, abfd, bed->elf_machine_code);
4425 /* As a GNU extension, any input sections which are named
4426 .gnu.warning.SYMBOL are treated as warning symbols for the given
4427 symbol. This differs from .gnu.warning sections, which generate
4428 warnings when they are included in an output file. */
4429 /* PR 12761: Also generate this warning when building shared libraries. */
4430 for (s = abfd->sections; s != NULL; s = s->next)
4432 const char *name;
4434 name = bfd_section_name (s);
4435 if (startswith (name, ".gnu.warning."))
4437 char *msg;
4438 bfd_size_type sz;
4440 name += sizeof ".gnu.warning." - 1;
4442 /* If this is a shared object, then look up the symbol
4443 in the hash table. If it is there, and it is already
4444 been defined, then we will not be using the entry
4445 from this shared object, so we don't need to warn.
4446 FIXME: If we see the definition in a regular object
4447 later on, we will warn, but we shouldn't. The only
4448 fix is to keep track of what warnings we are supposed
4449 to emit, and then handle them all at the end of the
4450 link. */
4451 if (dynamic)
4453 struct elf_link_hash_entry *h;
4455 h = elf_link_hash_lookup (htab, name, false, false, true);
4457 /* FIXME: What about bfd_link_hash_common? */
4458 if (h != NULL
4459 && (h->root.type == bfd_link_hash_defined
4460 || h->root.type == bfd_link_hash_defweak))
4461 continue;
4464 sz = s->size;
4465 msg = (char *) bfd_alloc (abfd, sz + 1);
4466 if (msg == NULL)
4467 goto error_return;
4469 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
4470 goto error_return;
4472 msg[sz] = '\0';
4474 if (! (_bfd_generic_link_add_one_symbol
4475 (info, abfd, name, BSF_WARNING, s, 0, msg,
4476 false, bed->collect, NULL)))
4477 goto error_return;
4479 if (bfd_link_executable (info))
4481 /* Clobber the section size so that the warning does
4482 not get copied into the output file. */
4483 s->size = 0;
4485 /* Also set SEC_EXCLUDE, so that symbols defined in
4486 the warning section don't get copied to the output. */
4487 s->flags |= SEC_EXCLUDE;
4492 just_syms = ((s = abfd->sections) != NULL
4493 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
4495 add_needed = true;
4496 if (! dynamic)
4498 /* If we are creating a shared library, create all the dynamic
4499 sections immediately. We need to attach them to something,
4500 so we attach them to this BFD, provided it is the right
4501 format and is not from ld --just-symbols. Always create the
4502 dynamic sections for -E/--dynamic-list. FIXME: If there
4503 are no input BFD's of the same format as the output, we can't
4504 make a shared library. */
4505 if (!just_syms
4506 && (bfd_link_pic (info)
4507 || (!bfd_link_relocatable (info)
4508 && info->nointerp
4509 && (info->export_dynamic || info->dynamic)))
4510 && is_elf_hash_table (&htab->root)
4511 && info->output_bfd->xvec == abfd->xvec
4512 && !htab->dynamic_sections_created)
4514 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4515 goto error_return;
4518 else if (!is_elf_hash_table (&htab->root))
4519 goto error_return;
4520 else
4522 const char *soname = NULL;
4523 char *audit = NULL;
4524 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
4525 const Elf_Internal_Phdr *phdr;
4526 struct elf_link_loaded_list *loaded_lib;
4528 /* ld --just-symbols and dynamic objects don't mix very well.
4529 ld shouldn't allow it. */
4530 if (just_syms)
4531 abort ();
4533 /* If this dynamic lib was specified on the command line with
4534 --as-needed in effect, then we don't want to add a DT_NEEDED
4535 tag unless the lib is actually used. Similary for libs brought
4536 in by another lib's DT_NEEDED. When --no-add-needed is used
4537 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4538 any dynamic library in DT_NEEDED tags in the dynamic lib at
4539 all. */
4540 add_needed = (elf_dyn_lib_class (abfd)
4541 & (DYN_AS_NEEDED | DYN_DT_NEEDED
4542 | DYN_NO_NEEDED)) == 0;
4544 s = bfd_get_section_by_name (abfd, ".dynamic");
4545 if (s != NULL && s->size != 0 && (s->flags & SEC_HAS_CONTENTS) != 0)
4547 bfd_byte *dynbuf;
4548 bfd_byte *extdyn;
4549 unsigned int elfsec;
4550 unsigned long shlink;
4552 if (!_bfd_elf_mmap_section_contents (abfd, s, &dynbuf))
4554 error_free_dyn:
4555 _bfd_elf_munmap_section_contents (s, dynbuf);
4556 goto error_return;
4559 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
4560 if (elfsec == SHN_BAD)
4561 goto error_free_dyn;
4562 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4564 for (extdyn = dynbuf;
4565 (size_t) (dynbuf + s->size - extdyn) >= bed->s->sizeof_dyn;
4566 extdyn += bed->s->sizeof_dyn)
4568 Elf_Internal_Dyn dyn;
4570 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4571 if (dyn.d_tag == DT_SONAME)
4573 unsigned int tagv = dyn.d_un.d_val;
4574 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4575 if (soname == NULL)
4576 goto error_free_dyn;
4578 if (dyn.d_tag == DT_NEEDED)
4580 struct bfd_link_needed_list *n, **pn;
4581 char *fnm, *anm;
4582 unsigned int tagv = dyn.d_un.d_val;
4583 size_t amt = sizeof (struct bfd_link_needed_list);
4585 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4586 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4587 if (n == NULL || fnm == NULL)
4588 goto error_free_dyn;
4589 amt = strlen (fnm) + 1;
4590 anm = (char *) bfd_alloc (abfd, amt);
4591 if (anm == NULL)
4592 goto error_free_dyn;
4593 memcpy (anm, fnm, amt);
4594 n->name = anm;
4595 n->by = abfd;
4596 n->next = NULL;
4597 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4599 *pn = n;
4601 if (dyn.d_tag == DT_RUNPATH)
4603 struct bfd_link_needed_list *n, **pn;
4604 char *fnm, *anm;
4605 unsigned int tagv = dyn.d_un.d_val;
4606 size_t amt = sizeof (struct bfd_link_needed_list);
4608 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4609 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4610 if (n == NULL || fnm == NULL)
4611 goto error_free_dyn;
4612 amt = strlen (fnm) + 1;
4613 anm = (char *) bfd_alloc (abfd, amt);
4614 if (anm == NULL)
4615 goto error_free_dyn;
4616 memcpy (anm, fnm, amt);
4617 n->name = anm;
4618 n->by = abfd;
4619 n->next = NULL;
4620 for (pn = & runpath;
4621 *pn != NULL;
4622 pn = &(*pn)->next)
4624 *pn = n;
4626 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4627 if (!runpath && dyn.d_tag == DT_RPATH)
4629 struct bfd_link_needed_list *n, **pn;
4630 char *fnm, *anm;
4631 unsigned int tagv = dyn.d_un.d_val;
4632 size_t amt = sizeof (struct bfd_link_needed_list);
4634 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4635 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4636 if (n == NULL || fnm == NULL)
4637 goto error_free_dyn;
4638 amt = strlen (fnm) + 1;
4639 anm = (char *) bfd_alloc (abfd, amt);
4640 if (anm == NULL)
4641 goto error_free_dyn;
4642 memcpy (anm, fnm, amt);
4643 n->name = anm;
4644 n->by = abfd;
4645 n->next = NULL;
4646 for (pn = & rpath;
4647 *pn != NULL;
4648 pn = &(*pn)->next)
4650 *pn = n;
4652 if (dyn.d_tag == DT_AUDIT)
4654 unsigned int tagv = dyn.d_un.d_val;
4655 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4657 if (dyn.d_tag == DT_FLAGS_1)
4658 elf_tdata (abfd)->is_pie = (dyn.d_un.d_val & DF_1_PIE) != 0;
4661 _bfd_elf_munmap_section_contents (s, dynbuf);
4664 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4665 frees all more recently bfd_alloc'd blocks as well. */
4666 if (runpath)
4667 rpath = runpath;
4669 if (rpath)
4671 struct bfd_link_needed_list **pn;
4672 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4674 *pn = rpath;
4677 /* If we have a PT_GNU_RELRO program header, mark as read-only
4678 all sections contained fully therein. This makes relro
4679 shared library sections appear as they will at run-time. */
4680 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4681 while (phdr-- > elf_tdata (abfd)->phdr)
4682 if (phdr->p_type == PT_GNU_RELRO)
4684 for (s = abfd->sections; s != NULL; s = s->next)
4686 unsigned int opb = bfd_octets_per_byte (abfd, s);
4688 if ((s->flags & SEC_ALLOC) != 0
4689 && s->vma * opb >= phdr->p_vaddr
4690 && s->vma * opb + s->size <= phdr->p_vaddr + phdr->p_memsz)
4691 s->flags |= SEC_READONLY;
4693 break;
4696 /* We do not want to include any of the sections in a dynamic
4697 object in the output file. We hack by simply clobbering the
4698 list of sections in the BFD. This could be handled more
4699 cleanly by, say, a new section flag; the existing
4700 SEC_NEVER_LOAD flag is not the one we want, because that one
4701 still implies that the section takes up space in the output
4702 file. */
4703 bfd_section_list_clear (abfd);
4705 /* Find the name to use in a DT_NEEDED entry that refers to this
4706 object. If the object has a DT_SONAME entry, we use it.
4707 Otherwise, if the generic linker stuck something in
4708 elf_dt_name, we use that. Otherwise, we just use the file
4709 name. */
4710 if (soname == NULL || *soname == '\0')
4712 soname = elf_dt_name (abfd);
4713 if (soname == NULL || *soname == '\0')
4714 soname = bfd_get_filename (abfd);
4717 /* Save the SONAME because sometimes the linker emulation code
4718 will need to know it. */
4719 elf_dt_name (abfd) = soname;
4721 /* If we have already included this dynamic object in the
4722 link, just ignore it. There is no reason to include a
4723 particular dynamic object more than once. */
4724 for (loaded_lib = htab->dyn_loaded;
4725 loaded_lib != NULL;
4726 loaded_lib = loaded_lib->next)
4728 if (strcmp (elf_dt_name (loaded_lib->abfd), soname) == 0)
4729 return true;
4732 /* Create dynamic sections for backends that require that be done
4733 before setup_gnu_properties. */
4734 if (add_needed
4735 && !_bfd_elf_link_create_dynamic_sections (abfd, info))
4736 return false;
4738 /* Save the DT_AUDIT entry for the linker emulation code. */
4739 elf_dt_audit (abfd) = audit;
4742 /* If this is a dynamic object, we always link against the .dynsym
4743 symbol table, not the .symtab symbol table. The dynamic linker
4744 will only see the .dynsym symbol table, so there is no reason to
4745 look at .symtab for a dynamic object. */
4747 if (! dynamic || elf_dynsymtab (abfd) == 0)
4748 hdr = &elf_tdata (abfd)->symtab_hdr;
4749 else
4750 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4752 symcount = hdr->sh_size / bed->s->sizeof_sym;
4754 /* The sh_info field of the symtab header tells us where the
4755 external symbols start. We don't care about the local symbols at
4756 this point. */
4757 if (elf_bad_symtab (abfd))
4759 extsymcount = symcount;
4760 extsymoff = 0;
4762 else
4764 extsymcount = symcount - hdr->sh_info;
4765 extsymoff = hdr->sh_info;
4768 sym_hash = elf_sym_hashes (abfd);
4769 if (extsymcount != 0)
4771 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4772 NULL, NULL, NULL);
4773 if (isymbuf == NULL)
4774 goto error_return;
4776 if (sym_hash == NULL)
4778 /* We store a pointer to the hash table entry for each
4779 external symbol. */
4780 size_t amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4781 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4782 if (sym_hash == NULL)
4783 goto error_free_sym;
4784 elf_sym_hashes (abfd) = sym_hash;
4788 if (dynamic)
4790 /* Read in any version definitions. */
4791 if (!_bfd_elf_slurp_version_tables (abfd,
4792 info->default_imported_symver))
4793 goto error_free_sym;
4795 /* Read in the symbol versions, but don't bother to convert them
4796 to internal format. */
4797 if (elf_dynversym (abfd) != 0)
4799 Elf_Internal_Shdr *versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4800 bfd_size_type amt = versymhdr->sh_size;
4802 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0)
4803 goto error_free_sym;
4804 extversym = (Elf_External_Versym *)
4805 _bfd_malloc_and_read (abfd, amt, amt);
4806 if (extversym == NULL)
4807 goto error_free_sym;
4808 extversym_end = extversym + amt / sizeof (*extversym);
4812 /* If we are loading an as-needed shared lib, save the symbol table
4813 state before we start adding symbols. If the lib turns out
4814 to be unneeded, restore the state. */
4815 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4817 unsigned int i;
4818 size_t entsize;
4820 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4822 struct bfd_hash_entry *p;
4823 struct elf_link_hash_entry *h;
4825 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4827 h = (struct elf_link_hash_entry *) p;
4828 entsize += htab->root.table.entsize;
4829 if (h->root.type == bfd_link_hash_warning)
4831 entsize += htab->root.table.entsize;
4832 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4834 if (h->root.type == bfd_link_hash_common)
4835 entsize += sizeof (*h->root.u.c.p);
4839 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4840 old_tab = bfd_malloc (tabsize + entsize);
4841 if (old_tab == NULL)
4842 goto error_free_vers;
4844 /* Remember the current objalloc pointer, so that all mem for
4845 symbols added can later be reclaimed. */
4846 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4847 if (alloc_mark == NULL)
4848 goto error_free_vers;
4850 /* Make a special call to the linker "notice" function to
4851 tell it that we are about to handle an as-needed lib. */
4852 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4853 goto error_free_vers;
4855 /* Clone the symbol table. Remember some pointers into the
4856 symbol table, and dynamic symbol count. */
4857 old_ent = (char *) old_tab + tabsize;
4858 memcpy (old_tab, htab->root.table.table, tabsize);
4859 old_undefs = htab->root.undefs;
4860 old_undefs_tail = htab->root.undefs_tail;
4861 old_table = htab->root.table.table;
4862 old_size = htab->root.table.size;
4863 old_count = htab->root.table.count;
4864 old_strtab = NULL;
4865 if (htab->dynstr != NULL)
4867 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4868 if (old_strtab == NULL)
4869 goto error_free_vers;
4872 for (i = 0; i < htab->root.table.size; i++)
4874 struct bfd_hash_entry *p;
4875 struct elf_link_hash_entry *h;
4877 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4879 h = (struct elf_link_hash_entry *) p;
4880 memcpy (old_ent, h, htab->root.table.entsize);
4881 old_ent = (char *) old_ent + htab->root.table.entsize;
4882 if (h->root.type == bfd_link_hash_warning)
4884 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4885 memcpy (old_ent, h, htab->root.table.entsize);
4886 old_ent = (char *) old_ent + htab->root.table.entsize;
4888 if (h->root.type == bfd_link_hash_common)
4890 memcpy (old_ent, h->root.u.c.p, sizeof (*h->root.u.c.p));
4891 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
4897 weaks = NULL;
4898 if (extversym == NULL)
4899 ever = NULL;
4900 else if (extversym + extsymoff < extversym_end)
4901 ever = extversym + extsymoff;
4902 else
4904 /* xgettext:c-format */
4905 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4906 abfd, (long) extsymoff,
4907 (long) (extversym_end - extversym) / sizeof (* extversym));
4908 bfd_set_error (bfd_error_bad_value);
4909 goto error_free_vers;
4912 if (!bfd_link_relocatable (info)
4913 && bfd_get_lto_type (abfd) == lto_slim_ir_object)
4915 _bfd_error_handler
4916 (_("%pB: plugin needed to handle lto object"), abfd);
4919 for (isym = isymbuf, isymend = PTR_ADD (isymbuf, extsymcount);
4920 isym < isymend;
4921 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4923 int bind;
4924 bfd_vma value;
4925 asection *sec, *new_sec;
4926 flagword flags;
4927 const char *name;
4928 bool must_copy_name = false;
4929 struct elf_link_hash_entry *h;
4930 struct elf_link_hash_entry *hi;
4931 bool definition;
4932 bool size_change_ok;
4933 bool type_change_ok;
4934 bool new_weak;
4935 bool old_weak;
4936 bfd *override;
4937 bool common;
4938 bool discarded;
4939 unsigned int old_alignment;
4940 unsigned int shindex;
4941 bfd *old_bfd;
4942 bool matched;
4944 override = NULL;
4946 flags = BSF_NO_FLAGS;
4947 sec = NULL;
4948 value = isym->st_value;
4949 common = bed->common_definition (isym);
4950 if (common && info->inhibit_common_definition)
4952 /* Treat common symbol as undefined for --no-define-common. */
4953 isym->st_shndx = SHN_UNDEF;
4954 common = false;
4956 discarded = false;
4958 bind = ELF_ST_BIND (isym->st_info);
4959 switch (bind)
4961 case STB_LOCAL:
4962 /* This should be impossible, since ELF requires that all
4963 global symbols follow all local symbols, and that sh_info
4964 point to the first global symbol. Unfortunately, Irix 5
4965 screws this up. */
4966 if (elf_bad_symtab (abfd))
4967 continue;
4969 /* If we aren't prepared to handle locals within the globals
4970 then we'll likely segfault on a NULL symbol hash if the
4971 symbol is ever referenced in relocations. */
4972 shindex = elf_elfheader (abfd)->e_shstrndx;
4973 name = bfd_elf_string_from_elf_section (abfd, shindex, hdr->sh_name);
4974 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4975 " (>= sh_info of %lu)"),
4976 abfd, name, (long) (isym - isymbuf + extsymoff),
4977 (long) extsymoff);
4979 /* Dynamic object relocations are not processed by ld, so
4980 ld won't run into the problem mentioned above. */
4981 if (dynamic)
4982 continue;
4983 bfd_set_error (bfd_error_bad_value);
4984 goto error_free_vers;
4986 case STB_GLOBAL:
4987 if (isym->st_shndx != SHN_UNDEF && !common)
4988 flags = BSF_GLOBAL;
4989 break;
4991 case STB_WEAK:
4992 flags = BSF_WEAK;
4993 break;
4995 case STB_GNU_UNIQUE:
4996 flags = BSF_GNU_UNIQUE;
4997 break;
4999 default:
5000 /* Leave it up to the processor backend. */
5001 break;
5004 if (isym->st_shndx == SHN_UNDEF)
5005 sec = bfd_und_section_ptr;
5006 else if (isym->st_shndx == SHN_ABS)
5007 sec = bfd_abs_section_ptr;
5008 else if (isym->st_shndx == SHN_COMMON)
5010 sec = bfd_com_section_ptr;
5011 /* What ELF calls the size we call the value. What ELF
5012 calls the value we call the alignment. */
5013 value = isym->st_size;
5015 else
5017 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
5018 if (sec == NULL)
5019 sec = bfd_abs_section_ptr;
5020 else if (discarded_section (sec))
5022 /* Symbols from discarded section are undefined. We keep
5023 its visibility. */
5024 sec = bfd_und_section_ptr;
5025 discarded = true;
5026 isym->st_shndx = SHN_UNDEF;
5028 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
5029 value -= sec->vma;
5032 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5033 isym->st_name);
5034 if (name == NULL)
5035 goto error_free_vers;
5037 if (isym->st_shndx == SHN_COMMON
5038 && (abfd->flags & BFD_PLUGIN) != 0)
5040 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
5042 if (xc == NULL)
5044 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
5045 | SEC_EXCLUDE);
5046 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
5047 if (xc == NULL)
5048 goto error_free_vers;
5050 sec = xc;
5052 else if (isym->st_shndx == SHN_COMMON
5053 && ELF_ST_TYPE (isym->st_info) == STT_TLS
5054 && !bfd_link_relocatable (info))
5056 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
5058 if (tcomm == NULL)
5060 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
5061 | SEC_LINKER_CREATED);
5062 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
5063 if (tcomm == NULL)
5064 goto error_free_vers;
5066 sec = tcomm;
5068 else if (bed->elf_add_symbol_hook)
5070 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
5071 &sec, &value))
5072 goto error_free_vers;
5074 /* The hook function sets the name to NULL if this symbol
5075 should be skipped for some reason. */
5076 if (name == NULL)
5077 continue;
5080 /* Sanity check that all possibilities were handled. */
5081 if (sec == NULL)
5082 abort ();
5084 /* Silently discard TLS symbols from --just-syms. There's
5085 no way to combine a static TLS block with a new TLS block
5086 for this executable. */
5087 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
5088 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
5089 continue;
5091 if (bfd_is_und_section (sec)
5092 || bfd_is_com_section (sec))
5093 definition = false;
5094 else
5095 definition = true;
5097 size_change_ok = false;
5098 type_change_ok = bed->type_change_ok;
5099 old_weak = false;
5100 matched = false;
5101 old_alignment = 0;
5102 old_bfd = NULL;
5103 new_sec = sec;
5105 if (is_elf_hash_table (&htab->root))
5107 Elf_Internal_Versym iver;
5108 unsigned int vernum = 0;
5109 bool skip;
5111 if (ever == NULL)
5113 if (info->default_imported_symver)
5114 /* Use the default symbol version created earlier. */
5115 iver.vs_vers = elf_tdata (abfd)->cverdefs;
5116 else
5117 iver.vs_vers = 0;
5119 else if (ever >= extversym_end)
5121 /* xgettext:c-format */
5122 _bfd_error_handler (_("%pB: not enough version information"),
5123 abfd);
5124 bfd_set_error (bfd_error_bad_value);
5125 goto error_free_vers;
5127 else
5128 _bfd_elf_swap_versym_in (abfd, ever, &iver);
5130 vernum = iver.vs_vers & VERSYM_VERSION;
5132 /* If this is a hidden symbol, or if it is not version
5133 1, we append the version name to the symbol name.
5134 However, we do not modify a non-hidden absolute symbol
5135 if it is not a function, because it might be the version
5136 symbol itself. FIXME: What if it isn't? */
5137 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
5138 || (vernum > 1
5139 && (!bfd_is_abs_section (sec)
5140 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
5142 const char *verstr;
5143 size_t namelen, verlen, newlen;
5144 char *newname, *p;
5146 if (isym->st_shndx != SHN_UNDEF)
5148 if (vernum > elf_tdata (abfd)->cverdefs)
5149 verstr = NULL;
5150 else if (vernum > 1)
5151 verstr =
5152 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
5153 else
5154 verstr = "";
5156 if (verstr == NULL)
5158 _bfd_error_handler
5159 /* xgettext:c-format */
5160 (_("%pB: %s: invalid version %u (max %d)"),
5161 abfd, name, vernum,
5162 elf_tdata (abfd)->cverdefs);
5163 bfd_set_error (bfd_error_bad_value);
5164 goto error_free_vers;
5167 else
5169 /* We cannot simply test for the number of
5170 entries in the VERNEED section since the
5171 numbers for the needed versions do not start
5172 at 0. */
5173 Elf_Internal_Verneed *t;
5175 verstr = NULL;
5176 for (t = elf_tdata (abfd)->verref;
5177 t != NULL;
5178 t = t->vn_nextref)
5180 Elf_Internal_Vernaux *a;
5182 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5184 if (a->vna_other == vernum)
5186 verstr = a->vna_nodename;
5187 break;
5190 if (a != NULL)
5191 break;
5193 if (verstr == NULL)
5195 _bfd_error_handler
5196 /* xgettext:c-format */
5197 (_("%pB: %s: invalid needed version %d"),
5198 abfd, name, vernum);
5199 bfd_set_error (bfd_error_bad_value);
5200 goto error_free_vers;
5204 namelen = strlen (name);
5205 verlen = strlen (verstr);
5206 newlen = namelen + verlen + 2;
5207 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
5208 && isym->st_shndx != SHN_UNDEF)
5209 ++newlen;
5211 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
5212 if (newname == NULL)
5213 goto error_free_vers;
5214 memcpy (newname, name, namelen);
5215 p = newname + namelen;
5216 *p++ = ELF_VER_CHR;
5217 /* If this is a defined non-hidden version symbol,
5218 we add another @ to the name. This indicates the
5219 default version of the symbol. */
5220 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
5221 && isym->st_shndx != SHN_UNDEF)
5222 *p++ = ELF_VER_CHR;
5223 memcpy (p, verstr, verlen + 1);
5225 name = newname;
5226 /* Since bfd_hash_alloc is used for "name", the string
5227 must be copied if added to first_hash. The string
5228 memory can be freed when an --as-needed library is
5229 not needed. */
5230 must_copy_name = true;
5233 /* If this symbol has default visibility and the user has
5234 requested we not re-export it, then mark it as hidden. */
5235 if (!bfd_is_und_section (sec)
5236 && !dynamic
5237 && abfd->no_export
5238 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
5239 isym->st_other = (STV_HIDDEN
5240 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
5242 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
5243 sym_hash, &old_bfd, &old_weak,
5244 &old_alignment, &skip, &override,
5245 &type_change_ok, &size_change_ok,
5246 &matched))
5247 goto error_free_vers;
5249 if (skip)
5250 continue;
5252 h = *sym_hash;
5253 while (h->root.type == bfd_link_hash_indirect
5254 || h->root.type == bfd_link_hash_warning)
5255 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5257 /* Override a definition only if the new symbol matches the
5258 existing one. */
5259 if (override && matched)
5261 definition = false;
5262 if (htab->first_hash != NULL
5263 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5264 && h->root.non_ir_ref_regular)
5266 /* When reloading --as-needed shared objects for new
5267 symbols added from IR inputs, if this shared object
5268 has the first definition, use it. */
5269 struct elf_link_first_hash_entry *e
5270 = ((struct elf_link_first_hash_entry *)
5271 bfd_hash_lookup (htab->first_hash, name, false,
5272 false));
5273 if (e != NULL && e->abfd == abfd)
5274 definition = true;
5278 if (h->versioned != unversioned
5279 && elf_tdata (abfd)->verdef != NULL
5280 && vernum > 1
5281 && definition)
5282 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
5285 if (! (_bfd_generic_link_add_one_symbol
5286 (info, override ? override : abfd, name, flags, sec, value,
5287 NULL, false, bed->collect,
5288 (struct bfd_link_hash_entry **) sym_hash)))
5289 goto error_free_vers;
5291 h = *sym_hash;
5292 /* We need to make sure that indirect symbol dynamic flags are
5293 updated. */
5294 hi = h;
5295 while (h->root.type == bfd_link_hash_indirect
5296 || h->root.type == bfd_link_hash_warning)
5297 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5299 *sym_hash = h;
5301 /* Setting the index to -3 tells elf_link_output_extsym that
5302 this symbol is defined in a discarded section. */
5303 if (discarded && is_elf_hash_table (&htab->root))
5304 h->indx = -3;
5306 new_weak = (flags & BSF_WEAK) != 0;
5307 if (dynamic
5308 && definition
5309 && new_weak
5310 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
5311 && is_elf_hash_table (&htab->root)
5312 && h->u.alias == NULL)
5314 /* Keep a list of all weak defined non function symbols from
5315 a dynamic object, using the alias field. Later in this
5316 function we will set the alias field to the correct
5317 value. We only put non-function symbols from dynamic
5318 objects on this list, because that happens to be the only
5319 time we need to know the normal symbol corresponding to a
5320 weak symbol, and the information is time consuming to
5321 figure out. If the alias field is not already NULL,
5322 then this symbol was already defined by some previous
5323 dynamic object, and we will be using that previous
5324 definition anyhow. */
5326 h->u.alias = weaks;
5327 weaks = h;
5330 /* Set the alignment of a common symbol. */
5331 if ((common || bfd_is_com_section (sec))
5332 && h->root.type == bfd_link_hash_common)
5334 unsigned int align;
5336 if (common)
5337 align = bfd_log2 (isym->st_value);
5338 else
5340 /* The new symbol is a common symbol in a shared object.
5341 We need to get the alignment from the section. */
5342 align = new_sec->alignment_power;
5344 if (align > old_alignment)
5345 h->root.u.c.p->alignment_power = align;
5346 else
5347 h->root.u.c.p->alignment_power = old_alignment;
5350 if (is_elf_hash_table (&htab->root))
5352 /* Set a flag in the hash table entry indicating the type of
5353 reference or definition we just found. A dynamic symbol
5354 is one which is referenced or defined by both a regular
5355 object and a shared object. */
5356 bool dynsym = false;
5358 /* Plugin symbols aren't normal. Don't set def/ref flags. */
5359 if ((abfd->flags & BFD_PLUGIN) != 0)
5361 /* Except for this flag to track nonweak references. */
5362 if (!definition
5363 && bind != STB_WEAK)
5364 h->ref_ir_nonweak = 1;
5366 else if (!dynamic)
5368 if (! definition)
5370 h->ref_regular = 1;
5371 if (bind != STB_WEAK)
5372 h->ref_regular_nonweak = 1;
5374 else
5376 h->def_regular = 1;
5377 if (h->def_dynamic)
5379 h->def_dynamic = 0;
5380 h->ref_dynamic = 1;
5384 else
5386 if (! definition)
5388 h->ref_dynamic = 1;
5389 hi->ref_dynamic = 1;
5391 else
5393 h->def_dynamic = 1;
5394 hi->def_dynamic = 1;
5398 /* If an indirect symbol has been forced local, don't
5399 make the real symbol dynamic. */
5400 if (h != hi && hi->forced_local)
5402 else if (!dynamic)
5404 if (bfd_link_dll (info)
5405 || h->def_dynamic
5406 || h->ref_dynamic)
5407 dynsym = true;
5409 else
5411 if (h->def_regular
5412 || h->ref_regular
5413 || (h->is_weakalias
5414 && weakdef (h)->dynindx != -1))
5415 dynsym = true;
5418 /* Check to see if we need to add an indirect symbol for
5419 the default name. */
5420 if ((definition
5421 || (!override && h->root.type == bfd_link_hash_common))
5422 && !(hi != h
5423 && hi->versioned == versioned_hidden))
5424 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
5425 sec, value, &old_bfd, &dynsym))
5426 goto error_free_vers;
5428 /* Check the alignment when a common symbol is involved. This
5429 can change when a common symbol is overridden by a normal
5430 definition or a common symbol is ignored due to the old
5431 normal definition. We need to make sure the maximum
5432 alignment is maintained. */
5433 if ((old_alignment || common)
5434 && h->root.type != bfd_link_hash_common)
5436 unsigned int common_align;
5437 unsigned int normal_align;
5438 unsigned int symbol_align;
5439 bfd *normal_bfd;
5440 bfd *common_bfd;
5442 BFD_ASSERT (h->root.type == bfd_link_hash_defined
5443 || h->root.type == bfd_link_hash_defweak);
5445 symbol_align = ffs (h->root.u.def.value) - 1;
5446 if (h->root.u.def.section->owner != NULL
5447 && (h->root.u.def.section->owner->flags
5448 & (DYNAMIC | BFD_PLUGIN)) == 0)
5450 normal_align = h->root.u.def.section->alignment_power;
5451 if (normal_align > symbol_align)
5452 normal_align = symbol_align;
5454 else
5455 normal_align = symbol_align;
5457 if (old_alignment)
5459 common_align = old_alignment;
5460 common_bfd = old_bfd;
5461 normal_bfd = abfd;
5463 else
5465 common_align = bfd_log2 (isym->st_value);
5466 common_bfd = abfd;
5467 normal_bfd = old_bfd;
5470 if (normal_align < common_align)
5472 /* PR binutils/2735 */
5473 if (normal_bfd == NULL)
5474 _bfd_error_handler
5475 /* xgettext:c-format */
5476 (_("warning: alignment %u of common symbol `%s' in %pB is"
5477 " greater than the alignment (%u) of its section %pA"),
5478 1 << common_align, name, common_bfd,
5479 1 << normal_align, h->root.u.def.section);
5480 else
5481 _bfd_error_handler
5482 /* xgettext:c-format */
5483 (_("warning: alignment %u of normal symbol `%s' in %pB"
5484 " is smaller than %u used by the common definition in %pB"),
5485 1 << normal_align, name, normal_bfd,
5486 1 << common_align, common_bfd);
5488 /* PR 30499: make sure that users understand that this warning is serious. */
5489 _bfd_error_handler
5490 (_("warning: NOTE: alignment discrepancies can cause real problems. Investigation is advised."));
5494 /* Remember the symbol size if it isn't undefined. */
5495 if (isym->st_size != 0
5496 && isym->st_shndx != SHN_UNDEF
5497 && (definition || h->size == 0))
5499 if (h->size != 0
5500 && h->size != isym->st_size
5501 && ! size_change_ok)
5503 _bfd_error_handler
5504 /* xgettext:c-format */
5505 (_("warning: size of symbol `%s' changed"
5506 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
5507 name, (uint64_t) h->size, old_bfd,
5508 (uint64_t) isym->st_size, abfd);
5510 /* PR 30499: make sure that users understand that this warning is serious. */
5511 _bfd_error_handler
5512 (_("warning: NOTE: size discrepancies can cause real problems. Investigation is advised."));
5515 h->size = isym->st_size;
5518 /* If this is a common symbol, then we always want H->SIZE
5519 to be the size of the common symbol. The code just above
5520 won't fix the size if a common symbol becomes larger. We
5521 don't warn about a size change here, because that is
5522 covered by --warn-common. Allow changes between different
5523 function types. */
5524 if (h->root.type == bfd_link_hash_common)
5525 h->size = h->root.u.c.size;
5527 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
5528 && ((definition && !new_weak)
5529 || (old_weak && h->root.type == bfd_link_hash_common)
5530 || h->type == STT_NOTYPE))
5532 unsigned int type = ELF_ST_TYPE (isym->st_info);
5534 /* Turn an IFUNC symbol from a DSO into a normal FUNC
5535 symbol. */
5536 if (type == STT_GNU_IFUNC
5537 && (abfd->flags & DYNAMIC) != 0)
5538 type = STT_FUNC;
5540 if (h->type != type)
5542 if (h->type != STT_NOTYPE && ! type_change_ok)
5543 /* xgettext:c-format */
5544 _bfd_error_handler
5545 (_("warning: type of symbol `%s' changed"
5546 " from %d to %d in %pB"),
5547 name, h->type, type, abfd);
5549 h->type = type;
5553 /* Merge st_other field. */
5554 elf_merge_st_other (abfd, h, isym->st_other, sec,
5555 definition, dynamic);
5557 /* We don't want to make debug symbol dynamic. */
5558 if (definition
5559 && (sec->flags & SEC_DEBUGGING)
5560 && !bfd_link_relocatable (info))
5561 dynsym = false;
5563 /* Nor should we make plugin symbols dynamic. */
5564 if ((abfd->flags & BFD_PLUGIN) != 0)
5565 dynsym = false;
5567 if (definition)
5569 h->target_internal = isym->st_target_internal;
5570 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5573 /* Don't add indirect symbols for .symver x, x@FOO aliases
5574 in IR. Since all data or text symbols in IR have the
5575 same type, value and section, we can't tell if a symbol
5576 is an alias of another symbol by their types, values and
5577 sections. */
5578 if (definition
5579 && !dynamic
5580 && (abfd->flags & BFD_PLUGIN) == 0)
5582 char *p = strchr (name, ELF_VER_CHR);
5583 if (p != NULL && p[1] != ELF_VER_CHR)
5585 /* Queue non-default versions so that .symver x, x@FOO
5586 aliases can be checked. */
5587 if (!nondeflt_vers)
5589 size_t amt = ((isymend - isym + 1)
5590 * sizeof (struct elf_link_hash_entry *));
5591 nondeflt_vers
5592 = (struct elf_link_hash_entry **) bfd_malloc (amt);
5593 if (!nondeflt_vers)
5594 goto error_free_vers;
5596 nondeflt_vers[nondeflt_vers_cnt++] = h;
5600 if (dynsym && h->dynindx == -1)
5602 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5603 goto error_free_vers;
5604 if (h->is_weakalias
5605 && weakdef (h)->dynindx == -1)
5607 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
5608 goto error_free_vers;
5611 else if (h->dynindx != -1)
5612 /* If the symbol already has a dynamic index, but
5613 visibility says it should not be visible, turn it into
5614 a local symbol. */
5615 switch (ELF_ST_VISIBILITY (h->other))
5617 case STV_INTERNAL:
5618 case STV_HIDDEN:
5619 (*bed->elf_backend_hide_symbol) (info, h, true);
5620 dynsym = false;
5621 break;
5624 if (!add_needed
5625 && matched
5626 && definition
5627 && h->root.type != bfd_link_hash_indirect)
5629 if ((dynsym
5630 && h->ref_regular_nonweak)
5631 || (old_bfd != NULL
5632 && (old_bfd->flags & BFD_PLUGIN) != 0
5633 && h->ref_ir_nonweak
5634 && !info->lto_all_symbols_read)
5635 || (h->ref_dynamic_nonweak
5636 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5637 && !on_needed_list (elf_dt_name (abfd),
5638 htab->needed, NULL)))
5640 const char *soname = elf_dt_name (abfd);
5642 info->callbacks->minfo ("%!", soname, old_bfd,
5643 h->root.root.string);
5645 /* A symbol from a library loaded via DT_NEEDED of some
5646 other library is referenced by a regular object.
5647 Add a DT_NEEDED entry for it. Issue an error if
5648 --no-add-needed is used and the reference was not
5649 a weak one. */
5650 if (old_bfd != NULL
5651 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
5653 _bfd_error_handler
5654 /* xgettext:c-format */
5655 (_("%pB: undefined reference to symbol '%s'"),
5656 old_bfd, name);
5657 bfd_set_error (bfd_error_missing_dso);
5658 goto error_free_vers;
5661 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
5662 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
5664 /* Create dynamic sections for backends that require
5665 that be done before setup_gnu_properties. */
5666 if (!_bfd_elf_link_create_dynamic_sections (abfd, info))
5667 return false;
5668 add_needed = true;
5670 else if (dynamic
5671 && h->root.u.def.section->owner == abfd)
5672 /* Add this symbol to first hash if this shared
5673 object has the first definition. */
5674 elf_link_add_to_first_hash (abfd, info, name, must_copy_name);
5679 if (info->lto_plugin_active
5680 && !bfd_link_relocatable (info)
5681 && (abfd->flags & BFD_PLUGIN) == 0
5682 && !just_syms
5683 && extsymcount)
5685 int r_sym_shift;
5687 if (bed->s->arch_size == 32)
5688 r_sym_shift = 8;
5689 else
5690 r_sym_shift = 32;
5692 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5693 referenced in regular objects so that linker plugin will get
5694 the correct symbol resolution. */
5696 sym_hash = elf_sym_hashes (abfd);
5697 for (s = abfd->sections; s != NULL; s = s->next)
5699 Elf_Internal_Rela *internal_relocs;
5700 Elf_Internal_Rela *rel, *relend;
5702 /* Don't check relocations in excluded sections. */
5703 if ((s->flags & SEC_RELOC) == 0
5704 || s->reloc_count == 0
5705 || (s->flags & SEC_EXCLUDE) != 0
5706 || ((info->strip == strip_all
5707 || info->strip == strip_debugger)
5708 && (s->flags & SEC_DEBUGGING) != 0))
5709 continue;
5711 internal_relocs = _bfd_elf_link_info_read_relocs
5712 (abfd, info, s, NULL, NULL,
5713 _bfd_elf_link_keep_memory (info));
5714 if (internal_relocs == NULL)
5715 goto error_free_vers;
5717 rel = internal_relocs;
5718 relend = rel + s->reloc_count;
5719 for ( ; rel < relend; rel++)
5721 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5722 struct elf_link_hash_entry *h;
5724 /* Skip local symbols. */
5725 if (r_symndx < extsymoff)
5726 continue;
5728 h = sym_hash[r_symndx - extsymoff];
5729 if (h != NULL)
5730 h->root.non_ir_ref_regular = 1;
5733 if (elf_section_data (s)->relocs != internal_relocs)
5734 free (internal_relocs);
5738 free (extversym);
5739 extversym = NULL;
5740 free (isymbuf);
5741 isymbuf = NULL;
5743 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5745 unsigned int i;
5747 /* Restore the symbol table. */
5748 old_ent = (char *) old_tab + tabsize;
5749 memset (elf_sym_hashes (abfd), 0,
5750 extsymcount * sizeof (struct elf_link_hash_entry *));
5751 htab->root.table.table = old_table;
5752 htab->root.table.size = old_size;
5753 htab->root.table.count = old_count;
5754 memcpy (htab->root.table.table, old_tab, tabsize);
5755 htab->root.undefs = old_undefs;
5756 htab->root.undefs_tail = old_undefs_tail;
5757 if (htab->dynstr != NULL)
5758 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5759 free (old_strtab);
5760 old_strtab = NULL;
5761 for (i = 0; i < htab->root.table.size; i++)
5763 struct bfd_hash_entry *p;
5764 struct elf_link_hash_entry *h;
5765 unsigned int non_ir_ref_dynamic;
5767 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5769 /* Preserve non_ir_ref_dynamic so that this symbol
5770 will be exported when the dynamic lib becomes needed
5771 in the second pass. */
5772 h = (struct elf_link_hash_entry *) p;
5773 if (h->root.type == bfd_link_hash_warning)
5774 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5775 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
5777 h = (struct elf_link_hash_entry *) p;
5778 memcpy (h, old_ent, htab->root.table.entsize);
5779 old_ent = (char *) old_ent + htab->root.table.entsize;
5780 if (h->root.type == bfd_link_hash_warning)
5782 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5783 memcpy (h, old_ent, htab->root.table.entsize);
5784 old_ent = (char *) old_ent + htab->root.table.entsize;
5786 if (h->root.type == bfd_link_hash_common)
5788 memcpy (h->root.u.c.p, old_ent, sizeof (*h->root.u.c.p));
5789 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
5791 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
5795 /* Make a special call to the linker "notice" function to
5796 tell it that symbols added for crefs may need to be removed. */
5797 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
5798 goto error_free_vers;
5800 free (old_tab);
5801 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5802 alloc_mark);
5803 free (nondeflt_vers);
5804 return true;
5807 if (old_tab != NULL)
5809 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
5810 goto error_free_vers;
5811 free (old_tab);
5812 old_tab = NULL;
5815 /* Now that all the symbols from this input file are created, if
5816 not performing a relocatable link, handle .symver foo, foo@BAR
5817 such that any relocs against foo become foo@BAR. */
5818 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
5820 size_t cnt, symidx;
5822 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5824 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5825 char *shortname, *p;
5826 size_t amt;
5828 p = strchr (h->root.root.string, ELF_VER_CHR);
5829 if (p == NULL
5830 || (h->root.type != bfd_link_hash_defined
5831 && h->root.type != bfd_link_hash_defweak))
5832 continue;
5834 amt = p - h->root.root.string;
5835 shortname = (char *) bfd_malloc (amt + 1);
5836 if (!shortname)
5837 goto error_free_vers;
5838 memcpy (shortname, h->root.root.string, amt);
5839 shortname[amt] = '\0';
5841 hi = (struct elf_link_hash_entry *)
5842 bfd_link_hash_lookup (&htab->root, shortname,
5843 false, false, false);
5844 if (hi != NULL
5845 && hi->root.type == h->root.type
5846 && hi->root.u.def.value == h->root.u.def.value
5847 && hi->root.u.def.section == h->root.u.def.section)
5849 (*bed->elf_backend_hide_symbol) (info, hi, true);
5850 hi->root.type = bfd_link_hash_indirect;
5851 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
5852 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
5853 sym_hash = elf_sym_hashes (abfd);
5854 if (sym_hash)
5855 for (symidx = 0; symidx < extsymcount; ++symidx)
5856 if (sym_hash[symidx] == hi)
5858 sym_hash[symidx] = h;
5859 break;
5862 free (shortname);
5864 free (nondeflt_vers);
5865 nondeflt_vers = NULL;
5868 /* Now set the alias field correctly for all the weak defined
5869 symbols we found. The only way to do this is to search all the
5870 symbols. Since we only need the information for non functions in
5871 dynamic objects, that's the only time we actually put anything on
5872 the list WEAKS. We need this information so that if a regular
5873 object refers to a symbol defined weakly in a dynamic object, the
5874 real symbol in the dynamic object is also put in the dynamic
5875 symbols; we also must arrange for both symbols to point to the
5876 same memory location. We could handle the general case of symbol
5877 aliasing, but a general symbol alias can only be generated in
5878 assembler code, handling it correctly would be very time
5879 consuming, and other ELF linkers don't handle general aliasing
5880 either. */
5881 if (weaks != NULL)
5883 struct elf_link_hash_entry **hpp;
5884 struct elf_link_hash_entry **hppend;
5885 struct elf_link_hash_entry **sorted_sym_hash;
5886 struct elf_link_hash_entry *h;
5887 size_t sym_count, amt;
5889 /* Since we have to search the whole symbol list for each weak
5890 defined symbol, search time for N weak defined symbols will be
5891 O(N^2). Binary search will cut it down to O(NlogN). */
5892 amt = extsymcount * sizeof (*sorted_sym_hash);
5893 sorted_sym_hash = bfd_malloc (amt);
5894 if (sorted_sym_hash == NULL)
5895 goto error_return;
5896 sym_hash = sorted_sym_hash;
5897 hpp = elf_sym_hashes (abfd);
5898 hppend = hpp + extsymcount;
5899 sym_count = 0;
5900 for (; hpp < hppend; hpp++)
5902 h = *hpp;
5903 if (h != NULL
5904 && h->root.type == bfd_link_hash_defined
5905 && !bed->is_function_type (h->type))
5907 *sym_hash = h;
5908 sym_hash++;
5909 sym_count++;
5913 qsort (sorted_sym_hash, sym_count, sizeof (*sorted_sym_hash),
5914 elf_sort_symbol);
5916 while (weaks != NULL)
5918 struct elf_link_hash_entry *hlook;
5919 asection *slook;
5920 bfd_vma vlook;
5921 size_t i, j, idx = 0;
5923 hlook = weaks;
5924 weaks = hlook->u.alias;
5925 hlook->u.alias = NULL;
5927 if (hlook->root.type != bfd_link_hash_defined
5928 && hlook->root.type != bfd_link_hash_defweak)
5929 continue;
5931 slook = hlook->root.u.def.section;
5932 vlook = hlook->root.u.def.value;
5934 i = 0;
5935 j = sym_count;
5936 while (i != j)
5938 bfd_signed_vma vdiff;
5939 idx = (i + j) / 2;
5940 h = sorted_sym_hash[idx];
5941 vdiff = vlook - h->root.u.def.value;
5942 if (vdiff < 0)
5943 j = idx;
5944 else if (vdiff > 0)
5945 i = idx + 1;
5946 else
5948 int sdiff = slook->id - h->root.u.def.section->id;
5949 if (sdiff < 0)
5950 j = idx;
5951 else if (sdiff > 0)
5952 i = idx + 1;
5953 else
5954 break;
5958 /* We didn't find a value/section match. */
5959 if (i == j)
5960 continue;
5962 /* With multiple aliases, or when the weak symbol is already
5963 strongly defined, we have multiple matching symbols and
5964 the binary search above may land on any of them. Step
5965 one past the matching symbol(s). */
5966 while (++idx != j)
5968 h = sorted_sym_hash[idx];
5969 if (h->root.u.def.section != slook
5970 || h->root.u.def.value != vlook)
5971 break;
5974 /* Now look back over the aliases. Since we sorted by size
5975 as well as value and section, we'll choose the one with
5976 the largest size. */
5977 while (idx-- != i)
5979 h = sorted_sym_hash[idx];
5981 /* Stop if value or section doesn't match. */
5982 if (h->root.u.def.section != slook
5983 || h->root.u.def.value != vlook)
5984 break;
5985 else if (h != hlook)
5987 struct elf_link_hash_entry *t;
5989 hlook->u.alias = h;
5990 hlook->is_weakalias = 1;
5991 t = h;
5992 if (t->u.alias != NULL)
5993 while (t->u.alias != h)
5994 t = t->u.alias;
5995 t->u.alias = hlook;
5997 /* If the weak definition is in the list of dynamic
5998 symbols, make sure the real definition is put
5999 there as well. */
6000 if (hlook->dynindx != -1 && h->dynindx == -1)
6002 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6004 err_free_sym_hash:
6005 free (sorted_sym_hash);
6006 goto error_return;
6010 /* If the real definition is in the list of dynamic
6011 symbols, make sure the weak definition is put
6012 there as well. If we don't do this, then the
6013 dynamic loader might not merge the entries for the
6014 real definition and the weak definition. */
6015 if (h->dynindx != -1 && hlook->dynindx == -1)
6017 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
6018 goto err_free_sym_hash;
6020 break;
6025 free (sorted_sym_hash);
6028 if (bed->check_directives
6029 && !(*bed->check_directives) (abfd, info))
6030 return false;
6032 /* If this is a non-traditional link, try to optimize the handling
6033 of the .stab/.stabstr sections. */
6034 if (! dynamic
6035 && ! info->traditional_format
6036 && is_elf_hash_table (&htab->root)
6037 && (info->strip != strip_all && info->strip != strip_debugger))
6039 asection *stabstr;
6041 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
6042 if (stabstr != NULL)
6044 bfd_size_type string_offset = 0;
6045 asection *stab;
6047 for (stab = abfd->sections; stab; stab = stab->next)
6048 if (startswith (stab->name, ".stab")
6049 && (!stab->name[5] ||
6050 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
6051 && (stab->flags & SEC_MERGE) == 0
6052 && !bfd_is_abs_section (stab->output_section))
6054 struct bfd_elf_section_data *secdata;
6056 secdata = elf_section_data (stab);
6057 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
6058 stabstr, &secdata->sec_info,
6059 &string_offset))
6060 goto error_return;
6061 if (secdata->sec_info)
6062 stab->sec_info_type = SEC_INFO_TYPE_STABS;
6067 if (dynamic && add_needed)
6069 /* Add this bfd to the loaded list. */
6070 struct elf_link_loaded_list *n;
6072 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
6073 if (n == NULL)
6074 goto error_return;
6075 n->abfd = abfd;
6076 n->next = htab->dyn_loaded;
6077 htab->dyn_loaded = n;
6079 if (dynamic && !add_needed
6080 && (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) != 0)
6081 elf_dyn_lib_class (abfd) |= DYN_NO_NEEDED;
6083 return true;
6085 error_free_vers:
6086 free (old_tab);
6087 free (old_strtab);
6088 free (nondeflt_vers);
6089 free (extversym);
6090 error_free_sym:
6091 free (isymbuf);
6092 error_return:
6093 return false;
6096 /* Return the linker hash table entry of a symbol that might be
6097 satisfied by an archive symbol. Return -1 on error. */
6099 struct bfd_link_hash_entry *
6100 _bfd_elf_archive_symbol_lookup (bfd *abfd,
6101 struct bfd_link_info *info,
6102 const char *name)
6104 struct bfd_link_hash_entry *h;
6105 char *p, *copy;
6106 size_t len, first;
6108 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
6109 if (h != NULL)
6110 return h;
6112 /* If this is a default version (the name contains @@), look up the
6113 symbol again with only one `@' as well as without the version.
6114 The effect is that references to the symbol with and without the
6115 version will be matched by the default symbol in the archive. */
6117 p = strchr (name, ELF_VER_CHR);
6118 if (p == NULL || p[1] != ELF_VER_CHR)
6120 /* Add this symbol to first hash if this archive has the first
6121 definition. */
6122 if (is_elf_hash_table (info->hash))
6123 elf_link_add_to_first_hash (abfd, info, name, false);
6124 return h;
6127 /* First check with only one `@'. */
6128 len = strlen (name);
6129 copy = (char *) bfd_alloc (abfd, len);
6130 if (copy == NULL)
6131 return (struct bfd_link_hash_entry *) -1;
6133 first = p - name + 1;
6134 memcpy (copy, name, first);
6135 memcpy (copy + first, name + first + 1, len - first);
6137 h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
6138 if (h == NULL)
6140 /* We also need to check references to the symbol without the
6141 version. */
6142 copy[first - 1] = '\0';
6143 h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
6146 bfd_release (abfd, copy);
6147 return h;
6150 /* Add symbols from an ELF archive file to the linker hash table. We
6151 don't use _bfd_generic_link_add_archive_symbols because we need to
6152 handle versioned symbols.
6154 Fortunately, ELF archive handling is simpler than that done by
6155 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
6156 oddities. In ELF, if we find a symbol in the archive map, and the
6157 symbol is currently undefined, we know that we must pull in that
6158 object file.
6160 Unfortunately, we do have to make multiple passes over the symbol
6161 table until nothing further is resolved. */
6163 static bool
6164 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
6166 symindex c;
6167 unsigned char *included = NULL;
6168 carsym *symdefs;
6169 bool loop;
6170 size_t amt;
6171 const struct elf_backend_data *bed;
6172 struct bfd_link_hash_entry * (*archive_symbol_lookup)
6173 (bfd *, struct bfd_link_info *, const char *);
6175 if (! bfd_has_map (abfd))
6177 /* An empty archive is a special case. */
6178 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
6179 return true;
6180 bfd_set_error (bfd_error_no_armap);
6181 return false;
6184 /* Keep track of all symbols we know to be already defined, and all
6185 files we know to be already included. This is to speed up the
6186 second and subsequent passes. */
6187 c = bfd_ardata (abfd)->symdef_count;
6188 if (c == 0)
6189 return true;
6190 amt = c * sizeof (*included);
6191 included = (unsigned char *) bfd_zmalloc (amt);
6192 if (included == NULL)
6193 return false;
6195 symdefs = bfd_ardata (abfd)->symdefs;
6196 bed = get_elf_backend_data (abfd);
6197 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
6201 file_ptr last;
6202 symindex i;
6203 carsym *symdef;
6204 carsym *symdefend;
6206 loop = false;
6207 last = -1;
6209 symdef = symdefs;
6210 symdefend = symdef + c;
6211 for (i = 0; symdef < symdefend; symdef++, i++)
6213 struct bfd_link_hash_entry *h;
6214 bfd *element;
6215 struct bfd_link_hash_entry *undefs_tail;
6216 symindex mark;
6218 if (included[i])
6219 continue;
6220 if (symdef->file_offset == last)
6222 included[i] = true;
6223 continue;
6226 h = archive_symbol_lookup (abfd, info, symdef->name);
6227 if (h == (struct bfd_link_hash_entry *) -1)
6228 goto error_return;
6230 if (h == NULL)
6231 continue;
6233 if (h->type == bfd_link_hash_undefined)
6235 /* If the archive element has already been loaded then one
6236 of the symbols defined by that element might have been
6237 made undefined due to being in a discarded section. */
6238 if (is_elf_hash_table (info->hash)
6239 && ((struct elf_link_hash_entry *) h)->indx == -3)
6240 continue;
6242 else if (h->type == bfd_link_hash_common)
6244 /* We currently have a common symbol. The archive map contains
6245 a reference to this symbol, so we may want to include it. We
6246 only want to include it however, if this archive element
6247 contains a definition of the symbol, not just another common
6248 declaration of it.
6250 Unfortunately some archivers (including GNU ar) will put
6251 declarations of common symbols into their archive maps, as
6252 well as real definitions, so we cannot just go by the archive
6253 map alone. Instead we must read in the element's symbol
6254 table and check that to see what kind of symbol definition
6255 this is. */
6256 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
6257 continue;
6259 else
6261 if (h->type != bfd_link_hash_undefweak)
6262 /* Symbol must be defined. Don't check it again. */
6263 included[i] = true;
6265 if (!is_elf_hash_table (info->hash))
6266 continue;
6267 struct elf_link_hash_entry *eh
6268 = (struct elf_link_hash_entry *) h;
6269 /* Ignore the archive if the symbol isn't referenced by a
6270 regular object or isn't defined in a shared object. */
6271 if (!eh->ref_regular || !eh->def_dynamic)
6272 continue;
6273 /* Ignore the dynamic definition if symbol is first
6274 defined in this archive. */
6275 struct elf_link_hash_table *htab = elf_hash_table (info);
6276 if (htab->first_hash == NULL)
6277 continue;
6278 struct elf_link_first_hash_entry *e
6279 = ((struct elf_link_first_hash_entry *)
6280 bfd_hash_lookup (htab->first_hash, symdef->name,
6281 false, false));
6282 if (e == NULL || e->abfd != abfd)
6283 continue;
6286 /* We need to include this archive member. */
6287 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset,
6288 info);
6289 if (element == NULL)
6290 goto error_return;
6292 if (! bfd_check_format (element, bfd_object))
6293 goto error_return;
6295 undefs_tail = info->hash->undefs_tail;
6297 if (!(*info->callbacks
6298 ->add_archive_element) (info, element, symdef->name, &element))
6299 continue;
6300 if (!bfd_link_add_symbols (element, info))
6301 goto error_return;
6303 /* If there are any new undefined symbols, we need to make
6304 another pass through the archive in order to see whether
6305 they can be defined. FIXME: This isn't perfect, because
6306 common symbols wind up on undefs_tail and because an
6307 undefined symbol which is defined later on in this pass
6308 does not require another pass. This isn't a bug, but it
6309 does make the code less efficient than it could be. */
6310 if (undefs_tail != info->hash->undefs_tail)
6311 loop = true;
6313 /* Look backward to mark all symbols from this object file
6314 which we have already seen in this pass. */
6315 mark = i;
6318 included[mark] = true;
6319 if (mark == 0)
6320 break;
6321 --mark;
6323 while (symdefs[mark].file_offset == symdef->file_offset);
6325 /* We mark subsequent symbols from this object file as we go
6326 on through the loop. */
6327 last = symdef->file_offset;
6330 while (loop);
6332 free (included);
6333 return true;
6335 error_return:
6336 free (included);
6337 return false;
6340 /* Given an ELF BFD, add symbols to the global hash table as
6341 appropriate. */
6343 bool
6344 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
6346 switch (bfd_get_format (abfd))
6348 case bfd_object:
6349 return elf_link_add_object_symbols (abfd, info);
6350 case bfd_archive:
6351 return elf_link_add_archive_symbols (abfd, info);
6352 default:
6353 bfd_set_error (bfd_error_wrong_format);
6354 return false;
6358 struct hash_codes_info
6360 unsigned long *hashcodes;
6361 bool error;
6364 /* This function will be called though elf_link_hash_traverse to store
6365 all hash value of the exported symbols in an array. */
6367 static bool
6368 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
6370 struct hash_codes_info *inf = (struct hash_codes_info *) data;
6371 const char *name;
6372 unsigned long ha;
6373 char *alc = NULL;
6375 /* Ignore indirect symbols. These are added by the versioning code. */
6376 if (h->dynindx == -1)
6377 return true;
6379 name = h->root.root.string;
6380 if (h->versioned >= versioned)
6382 char *p = strchr (name, ELF_VER_CHR);
6383 if (p != NULL)
6385 alc = (char *) bfd_malloc (p - name + 1);
6386 if (alc == NULL)
6388 inf->error = true;
6389 return false;
6391 memcpy (alc, name, p - name);
6392 alc[p - name] = '\0';
6393 name = alc;
6397 /* Compute the hash value. */
6398 ha = bfd_elf_hash (name);
6400 /* Store the found hash value in the array given as the argument. */
6401 *(inf->hashcodes)++ = ha;
6403 /* And store it in the struct so that we can put it in the hash table
6404 later. */
6405 h->u.elf_hash_value = ha;
6407 free (alc);
6408 return true;
6411 struct collect_gnu_hash_codes
6413 bfd *output_bfd;
6414 const struct elf_backend_data *bed;
6415 unsigned long int nsyms;
6416 unsigned long int maskbits;
6417 unsigned long int *hashcodes;
6418 unsigned long int *hashval;
6419 unsigned long int *indx;
6420 unsigned long int *counts;
6421 bfd_vma *bitmask;
6422 bfd_byte *contents;
6423 bfd_size_type xlat;
6424 long int min_dynindx;
6425 unsigned long int bucketcount;
6426 unsigned long int symindx;
6427 long int local_indx;
6428 long int shift1, shift2;
6429 unsigned long int mask;
6430 bool error;
6433 /* This function will be called though elf_link_hash_traverse to store
6434 all hash value of the exported symbols in an array. */
6436 static bool
6437 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
6439 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6440 const char *name;
6441 unsigned long ha;
6442 char *alc = NULL;
6444 /* Ignore indirect symbols. These are added by the versioning code. */
6445 if (h->dynindx == -1)
6446 return true;
6448 /* Ignore also local symbols and undefined symbols. */
6449 if (! (*s->bed->elf_hash_symbol) (h))
6450 return true;
6452 name = h->root.root.string;
6453 if (h->versioned >= versioned)
6455 char *p = strchr (name, ELF_VER_CHR);
6456 if (p != NULL)
6458 alc = (char *) bfd_malloc (p - name + 1);
6459 if (alc == NULL)
6461 s->error = true;
6462 return false;
6464 memcpy (alc, name, p - name);
6465 alc[p - name] = '\0';
6466 name = alc;
6470 /* Compute the hash value. */
6471 ha = bfd_elf_gnu_hash (name);
6473 /* Store the found hash value in the array for compute_bucket_count,
6474 and also for .dynsym reordering purposes. */
6475 s->hashcodes[s->nsyms] = ha;
6476 s->hashval[h->dynindx] = ha;
6477 ++s->nsyms;
6478 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
6479 s->min_dynindx = h->dynindx;
6481 free (alc);
6482 return true;
6485 /* This function will be called though elf_link_hash_traverse to do
6486 final dynamic symbol renumbering in case of .gnu.hash.
6487 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6488 to the translation table. */
6490 static bool
6491 elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data)
6493 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6494 unsigned long int bucket;
6495 unsigned long int val;
6497 /* Ignore indirect symbols. */
6498 if (h->dynindx == -1)
6499 return true;
6501 /* Ignore also local symbols and undefined symbols. */
6502 if (! (*s->bed->elf_hash_symbol) (h))
6504 if (h->dynindx >= s->min_dynindx)
6506 if (s->bed->record_xhash_symbol != NULL)
6508 (*s->bed->record_xhash_symbol) (h, 0);
6509 s->local_indx++;
6511 else
6512 h->dynindx = s->local_indx++;
6514 return true;
6517 bucket = s->hashval[h->dynindx] % s->bucketcount;
6518 val = (s->hashval[h->dynindx] >> s->shift1)
6519 & ((s->maskbits >> s->shift1) - 1);
6520 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
6521 s->bitmask[val]
6522 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
6523 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
6524 if (s->counts[bucket] == 1)
6525 /* Last element terminates the chain. */
6526 val |= 1;
6527 bfd_put_32 (s->output_bfd, val,
6528 s->contents + (s->indx[bucket] - s->symindx) * 4);
6529 --s->counts[bucket];
6530 if (s->bed->record_xhash_symbol != NULL)
6532 bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4;
6534 (*s->bed->record_xhash_symbol) (h, xlat_loc);
6536 else
6537 h->dynindx = s->indx[bucket]++;
6538 return true;
6541 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6543 bool
6544 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
6546 return !(h->forced_local
6547 || h->root.type == bfd_link_hash_undefined
6548 || h->root.type == bfd_link_hash_undefweak
6549 || ((h->root.type == bfd_link_hash_defined
6550 || h->root.type == bfd_link_hash_defweak)
6551 && h->root.u.def.section->output_section == NULL));
6554 /* Array used to determine the number of hash table buckets to use
6555 based on the number of symbols there are. If there are fewer than
6556 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6557 fewer than 37 we use 17 buckets, and so forth. We never use more
6558 than 32771 buckets. */
6560 static const size_t elf_buckets[] =
6562 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6563 16411, 32771, 0
6566 /* Compute bucket count for hashing table. We do not use a static set
6567 of possible tables sizes anymore. Instead we determine for all
6568 possible reasonable sizes of the table the outcome (i.e., the
6569 number of collisions etc) and choose the best solution. The
6570 weighting functions are not too simple to allow the table to grow
6571 without bounds. Instead one of the weighting factors is the size.
6572 Therefore the result is always a good payoff between few collisions
6573 (= short chain lengths) and table size. */
6574 static size_t
6575 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
6576 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
6577 unsigned long int nsyms,
6578 int gnu_hash)
6580 size_t best_size = 0;
6581 unsigned long int i;
6583 if (info->optimize)
6585 size_t minsize;
6586 size_t maxsize;
6587 uint64_t best_chlen = ~((uint64_t) 0);
6588 bfd *dynobj = elf_hash_table (info)->dynobj;
6589 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
6590 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
6591 unsigned long int *counts;
6592 bfd_size_type amt;
6593 unsigned int no_improvement_count = 0;
6595 /* Possible optimization parameters: if we have NSYMS symbols we say
6596 that the hashing table must at least have NSYMS/4 and at most
6597 2*NSYMS buckets. */
6598 minsize = nsyms / 4;
6599 if (minsize == 0)
6600 minsize = 1;
6601 best_size = maxsize = nsyms * 2;
6602 if (gnu_hash)
6604 if (minsize < 2)
6605 minsize = 2;
6606 if ((best_size & 31) == 0)
6607 ++best_size;
6610 /* Create array where we count the collisions in. We must use bfd_malloc
6611 since the size could be large. */
6612 amt = maxsize;
6613 amt *= sizeof (unsigned long int);
6614 counts = (unsigned long int *) bfd_malloc (amt);
6615 if (counts == NULL)
6616 return 0;
6618 /* Compute the "optimal" size for the hash table. The criteria is a
6619 minimal chain length. The minor criteria is (of course) the size
6620 of the table. */
6621 for (i = minsize; i < maxsize; ++i)
6623 /* Walk through the array of hashcodes and count the collisions. */
6624 uint64_t max;
6625 unsigned long int j;
6626 unsigned long int fact;
6628 if (gnu_hash && (i & 31) == 0)
6629 continue;
6631 memset (counts, '\0', i * sizeof (unsigned long int));
6633 /* Determine how often each hash bucket is used. */
6634 for (j = 0; j < nsyms; ++j)
6635 ++counts[hashcodes[j] % i];
6637 /* For the weight function we need some information about the
6638 pagesize on the target. This is information need not be 100%
6639 accurate. Since this information is not available (so far) we
6640 define it here to a reasonable default value. If it is crucial
6641 to have a better value some day simply define this value. */
6642 # ifndef BFD_TARGET_PAGESIZE
6643 # define BFD_TARGET_PAGESIZE (4096)
6644 # endif
6646 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6647 and the chains. */
6648 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
6650 # if 1
6651 /* Variant 1: optimize for short chains. We add the squares
6652 of all the chain lengths (which favors many small chain
6653 over a few long chains). */
6654 for (j = 0; j < i; ++j)
6655 max += counts[j] * counts[j];
6657 /* This adds penalties for the overall size of the table. */
6658 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6659 max *= fact * fact;
6660 # else
6661 /* Variant 2: Optimize a lot more for small table. Here we
6662 also add squares of the size but we also add penalties for
6663 empty slots (the +1 term). */
6664 for (j = 0; j < i; ++j)
6665 max += (1 + counts[j]) * (1 + counts[j]);
6667 /* The overall size of the table is considered, but not as
6668 strong as in variant 1, where it is squared. */
6669 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6670 max *= fact;
6671 # endif
6673 /* Compare with current best results. */
6674 if (max < best_chlen)
6676 best_chlen = max;
6677 best_size = i;
6678 no_improvement_count = 0;
6680 /* PR 11843: Avoid futile long searches for the best bucket size
6681 when there are a large number of symbols. */
6682 else if (++no_improvement_count == 100)
6683 break;
6686 free (counts);
6688 else
6690 for (i = 0; elf_buckets[i] != 0; i++)
6692 best_size = elf_buckets[i];
6693 if (nsyms < elf_buckets[i + 1])
6694 break;
6696 if (gnu_hash && best_size < 2)
6697 best_size = 2;
6700 return best_size;
6703 /* Size any SHT_GROUP section for ld -r. */
6705 bool
6706 _bfd_elf_size_group_sections (struct bfd_link_info *info)
6708 bfd *ibfd;
6709 asection *s;
6711 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6712 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6713 && (s = ibfd->sections) != NULL
6714 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
6715 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6716 return false;
6717 return true;
6720 /* Set a default stack segment size. The value in INFO wins. If it
6721 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6722 undefined it is initialized. */
6724 bool
6725 bfd_elf_stack_segment_size (bfd *output_bfd,
6726 struct bfd_link_info *info,
6727 const char *legacy_symbol,
6728 bfd_vma default_size)
6730 struct elf_link_hash_entry *h = NULL;
6732 /* Look for legacy symbol. */
6733 if (legacy_symbol)
6734 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6735 false, false, false);
6736 if (h && (h->root.type == bfd_link_hash_defined
6737 || h->root.type == bfd_link_hash_defweak)
6738 && h->def_regular
6739 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6741 /* The symbol has no type if specified on the command line. */
6742 h->type = STT_OBJECT;
6743 if (info->stacksize)
6744 /* xgettext:c-format */
6745 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6746 output_bfd, legacy_symbol);
6747 else if (h->root.u.def.section != bfd_abs_section_ptr)
6748 /* xgettext:c-format */
6749 _bfd_error_handler (_("%pB: %s not absolute"),
6750 output_bfd, legacy_symbol);
6751 else
6752 info->stacksize = h->root.u.def.value;
6755 if (!info->stacksize)
6756 /* If the user didn't set a size, or explicitly inhibit the
6757 size, set it now. */
6758 info->stacksize = default_size;
6760 /* Provide the legacy symbol, if it is referenced. */
6761 if (h && (h->root.type == bfd_link_hash_undefined
6762 || h->root.type == bfd_link_hash_undefweak))
6764 struct bfd_link_hash_entry *bh = NULL;
6766 if (!(_bfd_generic_link_add_one_symbol
6767 (info, output_bfd, legacy_symbol,
6768 BSF_GLOBAL, bfd_abs_section_ptr,
6769 info->stacksize >= 0 ? info->stacksize : 0,
6770 NULL, false, get_elf_backend_data (output_bfd)->collect, &bh)))
6771 return false;
6773 h = (struct elf_link_hash_entry *) bh;
6774 h->def_regular = 1;
6775 h->type = STT_OBJECT;
6778 return true;
6781 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6783 struct elf_gc_sweep_symbol_info
6785 struct bfd_link_info *info;
6786 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6787 bool);
6790 static bool
6791 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6793 if (!h->mark
6794 && (((h->root.type == bfd_link_hash_defined
6795 || h->root.type == bfd_link_hash_defweak)
6796 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6797 && h->root.u.def.section->gc_mark))
6798 || h->root.type == bfd_link_hash_undefined
6799 || h->root.type == bfd_link_hash_undefweak))
6801 struct elf_gc_sweep_symbol_info *inf;
6803 inf = (struct elf_gc_sweep_symbol_info *) data;
6804 (*inf->hide_symbol) (inf->info, h, true);
6805 h->def_regular = 0;
6806 h->ref_regular = 0;
6807 h->ref_regular_nonweak = 0;
6810 return true;
6813 /* Set up the sizes and contents of the ELF dynamic sections. This is
6814 called by the ELF linker emulation before_allocation routine. We
6815 must set the sizes of the sections before the linker sets the
6816 addresses of the various sections. */
6818 bool
6819 bfd_elf_size_dynamic_sections (bfd *output_bfd,
6820 const char *soname,
6821 const char *rpath,
6822 const char *filter_shlib,
6823 const char *audit,
6824 const char *depaudit,
6825 const char * const *auxiliary_filters,
6826 struct bfd_link_info *info,
6827 asection **sinterpptr)
6829 bfd *dynobj;
6830 const struct elf_backend_data *bed;
6832 *sinterpptr = NULL;
6834 if (!is_elf_hash_table (info->hash))
6835 return true;
6837 /* Any syms created from now on start with -1 in
6838 got.refcount/offset and plt.refcount/offset. */
6839 elf_hash_table (info)->init_got_refcount
6840 = elf_hash_table (info)->init_got_offset;
6841 elf_hash_table (info)->init_plt_refcount
6842 = elf_hash_table (info)->init_plt_offset;
6844 bed = get_elf_backend_data (output_bfd);
6846 /* The backend may have to create some sections regardless of whether
6847 we're dynamic or not. */
6848 if (bed->elf_backend_early_size_sections
6849 && !bed->elf_backend_early_size_sections (output_bfd, info))
6850 return false;
6852 dynobj = elf_hash_table (info)->dynobj;
6854 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6856 struct bfd_elf_version_tree *verdefs;
6857 struct elf_info_failed asvinfo;
6858 struct bfd_elf_version_tree *t;
6859 struct bfd_elf_version_expr *d;
6860 asection *s;
6861 size_t soname_indx;
6863 /* If we are supposed to export all symbols into the dynamic symbol
6864 table (this is not the normal case), then do so. */
6865 if (info->export_dynamic
6866 || (bfd_link_executable (info) && info->dynamic))
6868 struct elf_info_failed eif;
6870 eif.info = info;
6871 eif.failed = false;
6872 elf_link_hash_traverse (elf_hash_table (info),
6873 _bfd_elf_export_symbol,
6874 &eif);
6875 if (eif.failed)
6876 return false;
6879 if (soname != NULL)
6881 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6882 soname, true);
6883 if (soname_indx == (size_t) -1
6884 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6885 return false;
6887 else
6888 soname_indx = (size_t) -1;
6890 /* Make all global versions with definition. */
6891 for (t = info->version_info; t != NULL; t = t->next)
6892 for (d = t->globals.list; d != NULL; d = d->next)
6893 if (!d->symver && d->literal)
6895 const char *verstr, *name;
6896 size_t namelen, verlen, newlen;
6897 char *newname, *p, leading_char;
6898 struct elf_link_hash_entry *newh;
6900 leading_char = bfd_get_symbol_leading_char (output_bfd);
6901 name = d->pattern;
6902 namelen = strlen (name) + (leading_char != '\0');
6903 verstr = t->name;
6904 verlen = strlen (verstr);
6905 newlen = namelen + verlen + 3;
6907 newname = (char *) bfd_malloc (newlen);
6908 if (newname == NULL)
6909 return false;
6910 newname[0] = leading_char;
6911 memcpy (newname + (leading_char != '\0'), name, namelen);
6913 /* Check the hidden versioned definition. */
6914 p = newname + namelen;
6915 *p++ = ELF_VER_CHR;
6916 memcpy (p, verstr, verlen + 1);
6917 newh = elf_link_hash_lookup (elf_hash_table (info),
6918 newname, false, false,
6919 false);
6920 if (newh == NULL
6921 || (newh->root.type != bfd_link_hash_defined
6922 && newh->root.type != bfd_link_hash_defweak))
6924 /* Check the default versioned definition. */
6925 *p++ = ELF_VER_CHR;
6926 memcpy (p, verstr, verlen + 1);
6927 newh = elf_link_hash_lookup (elf_hash_table (info),
6928 newname, false, false,
6929 false);
6931 free (newname);
6933 /* Mark this version if there is a definition and it is
6934 not defined in a shared object. */
6935 if (newh != NULL
6936 && !newh->def_dynamic
6937 && (newh->root.type == bfd_link_hash_defined
6938 || newh->root.type == bfd_link_hash_defweak))
6939 d->symver = 1;
6942 /* Attach all the symbols to their version information. */
6943 asvinfo.info = info;
6944 asvinfo.failed = false;
6946 elf_link_hash_traverse (elf_hash_table (info),
6947 _bfd_elf_link_assign_sym_version,
6948 &asvinfo);
6949 if (asvinfo.failed)
6950 return false;
6952 if (!info->allow_undefined_version)
6954 /* Check if all global versions have a definition. */
6955 bool all_defined = true;
6956 for (t = info->version_info; t != NULL; t = t->next)
6957 for (d = t->globals.list; d != NULL; d = d->next)
6958 if (d->literal && !d->symver && !d->script)
6960 _bfd_error_handler
6961 (_("%s: undefined version: %s"),
6962 d->pattern, t->name);
6963 all_defined = false;
6966 if (!all_defined)
6968 bfd_set_error (bfd_error_bad_value);
6969 return false;
6973 /* Set up the version definition section. */
6974 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6975 BFD_ASSERT (s != NULL);
6977 /* We may have created additional version definitions if we are
6978 just linking a regular application. */
6979 verdefs = info->version_info;
6981 /* Skip anonymous version tag. */
6982 if (verdefs != NULL && verdefs->vernum == 0)
6983 verdefs = verdefs->next;
6985 if (verdefs == NULL && !info->create_default_symver)
6986 s->flags |= SEC_EXCLUDE;
6987 else
6989 unsigned int cdefs;
6990 bfd_size_type size;
6991 bfd_byte *p;
6992 Elf_Internal_Verdef def;
6993 Elf_Internal_Verdaux defaux;
6994 struct bfd_link_hash_entry *bh;
6995 struct elf_link_hash_entry *h;
6996 const char *name;
6998 cdefs = 0;
6999 size = 0;
7001 /* Make space for the base version. */
7002 size += sizeof (Elf_External_Verdef);
7003 size += sizeof (Elf_External_Verdaux);
7004 ++cdefs;
7006 /* Make space for the default version. */
7007 if (info->create_default_symver)
7009 size += sizeof (Elf_External_Verdef);
7010 ++cdefs;
7013 for (t = verdefs; t != NULL; t = t->next)
7015 struct bfd_elf_version_deps *n;
7017 /* Don't emit base version twice. */
7018 if (t->vernum == 0)
7019 continue;
7021 size += sizeof (Elf_External_Verdef);
7022 size += sizeof (Elf_External_Verdaux);
7023 ++cdefs;
7025 for (n = t->deps; n != NULL; n = n->next)
7026 size += sizeof (Elf_External_Verdaux);
7029 s->size = size;
7030 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7031 if (s->contents == NULL && s->size != 0)
7032 return false;
7034 /* Fill in the version definition section. */
7036 p = s->contents;
7038 def.vd_version = VER_DEF_CURRENT;
7039 def.vd_flags = VER_FLG_BASE;
7040 def.vd_ndx = 1;
7041 def.vd_cnt = 1;
7042 if (info->create_default_symver)
7044 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
7045 def.vd_next = sizeof (Elf_External_Verdef);
7047 else
7049 def.vd_aux = sizeof (Elf_External_Verdef);
7050 def.vd_next = (sizeof (Elf_External_Verdef)
7051 + sizeof (Elf_External_Verdaux));
7054 if (soname_indx != (size_t) -1)
7056 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
7057 soname_indx);
7058 def.vd_hash = bfd_elf_hash (soname);
7059 defaux.vda_name = soname_indx;
7060 name = soname;
7062 else
7064 size_t indx;
7066 name = lbasename (bfd_get_filename (output_bfd));
7067 def.vd_hash = bfd_elf_hash (name);
7068 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7069 name, false);
7070 if (indx == (size_t) -1)
7071 return false;
7072 defaux.vda_name = indx;
7074 defaux.vda_next = 0;
7076 _bfd_elf_swap_verdef_out (output_bfd, &def,
7077 (Elf_External_Verdef *) p);
7078 p += sizeof (Elf_External_Verdef);
7079 if (info->create_default_symver)
7081 /* Add a symbol representing this version. */
7082 bh = NULL;
7083 if (! (_bfd_generic_link_add_one_symbol
7084 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
7085 0, NULL, false,
7086 get_elf_backend_data (dynobj)->collect, &bh)))
7087 return false;
7088 h = (struct elf_link_hash_entry *) bh;
7089 h->non_elf = 0;
7090 h->def_regular = 1;
7091 h->type = STT_OBJECT;
7092 h->verinfo.vertree = NULL;
7094 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7095 return false;
7097 /* Create a duplicate of the base version with the same
7098 aux block, but different flags. */
7099 def.vd_flags = 0;
7100 def.vd_ndx = 2;
7101 def.vd_aux = sizeof (Elf_External_Verdef);
7102 if (verdefs)
7103 def.vd_next = (sizeof (Elf_External_Verdef)
7104 + sizeof (Elf_External_Verdaux));
7105 else
7106 def.vd_next = 0;
7107 _bfd_elf_swap_verdef_out (output_bfd, &def,
7108 (Elf_External_Verdef *) p);
7109 p += sizeof (Elf_External_Verdef);
7111 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
7112 (Elf_External_Verdaux *) p);
7113 p += sizeof (Elf_External_Verdaux);
7115 for (t = verdefs; t != NULL; t = t->next)
7117 unsigned int cdeps;
7118 struct bfd_elf_version_deps *n;
7120 /* Don't emit the base version twice. */
7121 if (t->vernum == 0)
7122 continue;
7124 cdeps = 0;
7125 for (n = t->deps; n != NULL; n = n->next)
7126 ++cdeps;
7128 /* Add a symbol representing this version. */
7129 bh = NULL;
7130 if (! (_bfd_generic_link_add_one_symbol
7131 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
7132 0, NULL, false,
7133 get_elf_backend_data (dynobj)->collect, &bh)))
7134 return false;
7135 h = (struct elf_link_hash_entry *) bh;
7136 h->non_elf = 0;
7137 h->def_regular = 1;
7138 h->type = STT_OBJECT;
7139 h->verinfo.vertree = t;
7141 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7142 return false;
7144 def.vd_version = VER_DEF_CURRENT;
7145 def.vd_flags = 0;
7146 if (t->globals.list == NULL
7147 && t->locals.list == NULL
7148 && ! t->used)
7149 def.vd_flags |= VER_FLG_WEAK;
7150 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
7151 def.vd_cnt = cdeps + 1;
7152 def.vd_hash = bfd_elf_hash (t->name);
7153 def.vd_aux = sizeof (Elf_External_Verdef);
7154 def.vd_next = 0;
7156 /* If a basever node is next, it *must* be the last node in
7157 the chain, otherwise Verdef construction breaks. */
7158 if (t->next != NULL && t->next->vernum == 0)
7159 BFD_ASSERT (t->next->next == NULL);
7161 if (t->next != NULL && t->next->vernum != 0)
7162 def.vd_next = (sizeof (Elf_External_Verdef)
7163 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
7165 _bfd_elf_swap_verdef_out (output_bfd, &def,
7166 (Elf_External_Verdef *) p);
7167 p += sizeof (Elf_External_Verdef);
7169 defaux.vda_name = h->dynstr_index;
7170 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
7171 h->dynstr_index);
7172 defaux.vda_next = 0;
7173 if (t->deps != NULL)
7174 defaux.vda_next = sizeof (Elf_External_Verdaux);
7175 t->name_indx = defaux.vda_name;
7177 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
7178 (Elf_External_Verdaux *) p);
7179 p += sizeof (Elf_External_Verdaux);
7181 for (n = t->deps; n != NULL; n = n->next)
7183 if (n->version_needed == NULL)
7185 /* This can happen if there was an error in the
7186 version script. */
7187 defaux.vda_name = 0;
7189 else
7191 defaux.vda_name = n->version_needed->name_indx;
7192 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
7193 defaux.vda_name);
7195 if (n->next == NULL)
7196 defaux.vda_next = 0;
7197 else
7198 defaux.vda_next = sizeof (Elf_External_Verdaux);
7200 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
7201 (Elf_External_Verdaux *) p);
7202 p += sizeof (Elf_External_Verdaux);
7206 elf_tdata (output_bfd)->cverdefs = cdefs;
7210 if (info->gc_sections && bed->can_gc_sections)
7212 struct elf_gc_sweep_symbol_info sweep_info;
7214 /* Remove the symbols that were in the swept sections from the
7215 dynamic symbol table. */
7216 sweep_info.info = info;
7217 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
7218 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
7219 &sweep_info);
7222 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7224 asection *s;
7225 struct elf_find_verdep_info sinfo;
7227 /* Work out the size of the version reference section. */
7229 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
7230 BFD_ASSERT (s != NULL);
7232 sinfo.info = info;
7233 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
7234 if (sinfo.vers == 0)
7235 sinfo.vers = 1;
7236 sinfo.failed = false;
7238 elf_link_hash_traverse (elf_hash_table (info),
7239 _bfd_elf_link_find_version_dependencies,
7240 &sinfo);
7241 if (sinfo.failed)
7242 return false;
7244 bed->elf_backend_add_glibc_version_dependency (&sinfo);
7245 if (sinfo.failed)
7246 return false;
7248 if (elf_tdata (output_bfd)->verref == NULL)
7249 s->flags |= SEC_EXCLUDE;
7250 else
7252 Elf_Internal_Verneed *vn;
7253 unsigned int size;
7254 unsigned int crefs;
7255 bfd_byte *p;
7257 /* Build the version dependency section. */
7258 size = 0;
7259 crefs = 0;
7260 for (vn = elf_tdata (output_bfd)->verref;
7261 vn != NULL;
7262 vn = vn->vn_nextref)
7264 Elf_Internal_Vernaux *a;
7266 size += sizeof (Elf_External_Verneed);
7267 ++crefs;
7268 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7269 size += sizeof (Elf_External_Vernaux);
7272 s->size = size;
7273 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7274 if (s->contents == NULL)
7275 return false;
7277 p = s->contents;
7278 for (vn = elf_tdata (output_bfd)->verref;
7279 vn != NULL;
7280 vn = vn->vn_nextref)
7282 unsigned int caux;
7283 Elf_Internal_Vernaux *a;
7284 size_t indx;
7286 caux = 0;
7287 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7288 ++caux;
7290 vn->vn_version = VER_NEED_CURRENT;
7291 vn->vn_cnt = caux;
7292 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7293 elf_dt_name (vn->vn_bfd) != NULL
7294 ? elf_dt_name (vn->vn_bfd)
7295 : lbasename (bfd_get_filename
7296 (vn->vn_bfd)),
7297 false);
7298 if (indx == (size_t) -1)
7299 return false;
7300 vn->vn_file = indx;
7301 vn->vn_aux = sizeof (Elf_External_Verneed);
7302 if (vn->vn_nextref == NULL)
7303 vn->vn_next = 0;
7304 else
7305 vn->vn_next = (sizeof (Elf_External_Verneed)
7306 + caux * sizeof (Elf_External_Vernaux));
7308 _bfd_elf_swap_verneed_out (output_bfd, vn,
7309 (Elf_External_Verneed *) p);
7310 p += sizeof (Elf_External_Verneed);
7312 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7314 a->vna_hash = bfd_elf_hash (a->vna_nodename);
7315 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7316 a->vna_nodename, false);
7317 if (indx == (size_t) -1)
7318 return false;
7319 a->vna_name = indx;
7320 if (a->vna_nextptr == NULL)
7321 a->vna_next = 0;
7322 else
7323 a->vna_next = sizeof (Elf_External_Vernaux);
7325 _bfd_elf_swap_vernaux_out (output_bfd, a,
7326 (Elf_External_Vernaux *) p);
7327 p += sizeof (Elf_External_Vernaux);
7331 elf_tdata (output_bfd)->cverrefs = crefs;
7335 if (bfd_link_relocatable (info)
7336 && !_bfd_elf_size_group_sections (info))
7337 return false;
7339 /* Determine any GNU_STACK segment requirements, after the backend
7340 has had a chance to set a default segment size. */
7341 if (info->execstack)
7343 /* If the user has explicitly requested warnings, then generate one even
7344 though the choice is the result of another command line option. */
7345 if (info->warn_execstack == 1)
7347 if (info->error_execstack)
7349 _bfd_error_handler
7350 (_("\
7351 error: creating an executable stack because of -z execstack command line option"));
7352 return false;
7355 _bfd_error_handler
7356 (_("\
7357 warning: enabling an executable stack because of -z execstack command line option"));
7360 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
7362 else if (info->noexecstack)
7363 elf_stack_flags (output_bfd) = PF_R | PF_W;
7364 else
7366 bfd *inputobj;
7367 asection *notesec = NULL;
7368 bfd *noteobj = NULL;
7369 bfd *emptyobj = NULL;
7370 int exec = 0;
7372 for (inputobj = info->input_bfds;
7373 inputobj;
7374 inputobj = inputobj->link.next)
7376 asection *s;
7378 if (inputobj->flags
7379 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
7380 continue;
7381 s = inputobj->sections;
7382 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7383 continue;
7385 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
7386 if (s)
7388 notesec = s;
7389 if (s->flags & SEC_CODE)
7391 noteobj = inputobj;
7392 exec = PF_X;
7393 /* There is no point in scanning the remaining bfds. */
7394 break;
7397 else if (bed->default_execstack && info->default_execstack)
7399 exec = PF_X;
7400 emptyobj = inputobj;
7404 if (notesec || info->stacksize > 0)
7406 if (exec)
7408 if (info->warn_execstack != 0)
7410 /* PR 29072: Because an executable stack is a serious
7411 security risk, make sure that the user knows that it is
7412 being enabled despite the fact that it was not requested
7413 on the command line. */
7414 if (noteobj)
7416 if (info->error_execstack)
7418 _bfd_error_handler (_("\
7419 error: %s: is triggering the generation of an executable stack (because it has an executable .note.GNU-stack section)"),
7420 bfd_get_filename (noteobj));
7421 return false;
7424 _bfd_error_handler (_("\
7425 warning: %s: requires executable stack (because the .note.GNU-stack section is executable)"),
7426 bfd_get_filename (noteobj));
7428 else if (emptyobj)
7430 if (info->error_execstack)
7432 _bfd_error_handler (_("\
7433 error: %s: is triggering the generation of an executable stack because it does not have a .note.GNU-stack section"),
7434 bfd_get_filename (emptyobj));
7435 return false;
7438 _bfd_error_handler (_("\
7439 warning: %s: missing .note.GNU-stack section implies executable stack"),
7440 bfd_get_filename (emptyobj));
7441 _bfd_error_handler (_("\
7442 NOTE: This behaviour is deprecated and will be removed in a future version of the linker"));
7446 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
7449 if (notesec && exec && bfd_link_relocatable (info)
7450 && notesec->output_section != bfd_abs_section_ptr)
7451 notesec->output_section->flags |= SEC_CODE;
7454 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7456 struct elf_info_failed eif;
7457 struct elf_link_hash_entry *h;
7458 asection *dynstr;
7459 asection *s;
7461 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
7462 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
7464 if (info->symbolic)
7466 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
7467 return false;
7468 info->flags |= DF_SYMBOLIC;
7471 if (rpath != NULL)
7473 size_t indx;
7474 bfd_vma tag;
7476 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
7477 true);
7478 if (indx == (size_t) -1)
7479 return false;
7481 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
7482 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
7483 return false;
7486 if (filter_shlib != NULL)
7488 size_t indx;
7490 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7491 filter_shlib, true);
7492 if (indx == (size_t) -1
7493 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
7494 return false;
7497 if (auxiliary_filters != NULL)
7499 const char * const *p;
7501 for (p = auxiliary_filters; *p != NULL; p++)
7503 size_t indx;
7505 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7506 *p, true);
7507 if (indx == (size_t) -1
7508 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
7509 return false;
7513 if (audit != NULL)
7515 size_t indx;
7517 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
7518 true);
7519 if (indx == (size_t) -1
7520 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
7521 return false;
7524 if (depaudit != NULL)
7526 size_t indx;
7528 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
7529 true);
7530 if (indx == (size_t) -1
7531 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
7532 return false;
7535 eif.info = info;
7536 eif.failed = false;
7538 /* Find all symbols which were defined in a dynamic object and make
7539 the backend pick a reasonable value for them. */
7540 elf_link_hash_traverse (elf_hash_table (info),
7541 _bfd_elf_adjust_dynamic_symbol,
7542 &eif);
7543 if (eif.failed)
7544 return false;
7546 /* Add some entries to the .dynamic section. We fill in some of the
7547 values later, in bfd_elf_final_link, but we must add the entries
7548 now so that we know the final size of the .dynamic section. */
7550 /* If there are initialization and/or finalization functions to
7551 call then add the corresponding DT_INIT/DT_FINI entries. */
7552 h = (info->init_function
7553 ? elf_link_hash_lookup (elf_hash_table (info),
7554 info->init_function, false,
7555 false, false)
7556 : NULL);
7557 if (h != NULL
7558 && (h->ref_regular
7559 || h->def_regular))
7561 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
7562 return false;
7564 h = (info->fini_function
7565 ? elf_link_hash_lookup (elf_hash_table (info),
7566 info->fini_function, false,
7567 false, false)
7568 : NULL);
7569 if (h != NULL
7570 && (h->ref_regular
7571 || h->def_regular))
7573 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
7574 return false;
7577 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
7578 if (s != NULL && s->linker_has_input)
7580 /* DT_PREINIT_ARRAY is not allowed in shared library. */
7581 if (! bfd_link_executable (info))
7583 bfd *sub;
7584 asection *o;
7586 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
7587 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
7588 && (o = sub->sections) != NULL
7589 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
7590 for (o = sub->sections; o != NULL; o = o->next)
7591 if (elf_section_data (o)->this_hdr.sh_type
7592 == SHT_PREINIT_ARRAY)
7594 _bfd_error_handler
7595 (_("%pB: .preinit_array section is not allowed in DSO"),
7596 sub);
7597 break;
7600 bfd_set_error (bfd_error_nonrepresentable_section);
7601 return false;
7604 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
7605 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
7606 return false;
7608 s = bfd_get_section_by_name (output_bfd, ".init_array");
7609 if (s != NULL && s->linker_has_input)
7611 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
7612 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
7613 return false;
7615 s = bfd_get_section_by_name (output_bfd, ".fini_array");
7616 if (s != NULL && s->linker_has_input)
7618 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
7619 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
7620 return false;
7623 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
7624 /* If .dynstr is excluded from the link, we don't want any of
7625 these tags. Strictly, we should be checking each section
7626 individually; This quick check covers for the case where
7627 someone does a /DISCARD/ : { *(*) }. */
7628 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
7630 bfd_size_type strsize;
7632 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7633 if ((info->emit_hash
7634 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
7635 || (info->emit_gnu_hash
7636 && (bed->record_xhash_symbol == NULL
7637 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)))
7638 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
7639 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
7640 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
7641 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
7642 bed->s->sizeof_sym)
7643 || (info->gnu_flags_1
7644 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_FLAGS_1,
7645 info->gnu_flags_1)))
7646 return false;
7650 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
7651 return false;
7653 /* The backend must work out the sizes of all the other dynamic
7654 sections. */
7655 if (bed->elf_backend_late_size_sections != NULL
7656 && !bed->elf_backend_late_size_sections (output_bfd, info))
7657 return false;
7659 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7661 if (elf_tdata (output_bfd)->cverdefs)
7663 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
7665 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
7666 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
7667 return false;
7670 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
7672 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
7673 return false;
7675 else if (info->flags & DF_BIND_NOW)
7677 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
7678 return false;
7681 if (info->flags_1)
7683 if (bfd_link_executable (info))
7684 info->flags_1 &= ~ (DF_1_INITFIRST
7685 | DF_1_NODELETE
7686 | DF_1_NOOPEN);
7687 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
7688 return false;
7691 if (elf_tdata (output_bfd)->cverrefs)
7693 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
7695 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
7696 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
7697 return false;
7700 if ((elf_tdata (output_bfd)->cverrefs == 0
7701 && elf_tdata (output_bfd)->cverdefs == 0)
7702 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
7704 asection *s;
7706 s = bfd_get_linker_section (dynobj, ".gnu.version");
7707 s->flags |= SEC_EXCLUDE;
7710 return true;
7713 /* Find the first non-excluded output section. We'll use its
7714 section symbol for some emitted relocs. */
7715 void
7716 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7718 asection *s;
7719 asection *found = NULL;
7721 for (s = output_bfd->sections; s != NULL; s = s->next)
7722 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7723 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7725 found = s;
7726 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7727 break;
7729 elf_hash_table (info)->text_index_section = found;
7732 /* Find two non-excluded output sections, one for code, one for data.
7733 We'll use their section symbols for some emitted relocs. */
7734 void
7735 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7737 asection *s;
7738 asection *found = NULL;
7740 /* Data first, since setting text_index_section changes
7741 _bfd_elf_omit_section_dynsym_default. */
7742 for (s = output_bfd->sections; s != NULL; s = s->next)
7743 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7744 && !(s->flags & SEC_READONLY)
7745 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7747 found = s;
7748 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7749 break;
7751 elf_hash_table (info)->data_index_section = found;
7753 for (s = output_bfd->sections; s != NULL; s = s->next)
7754 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7755 && (s->flags & SEC_READONLY)
7756 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7758 found = s;
7759 break;
7761 elf_hash_table (info)->text_index_section = found;
7764 #define GNU_HASH_SECTION_NAME(bed) \
7765 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7767 bool
7768 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7770 const struct elf_backend_data *bed;
7771 unsigned long section_sym_count;
7772 bfd_size_type dynsymcount = 0;
7774 if (!is_elf_hash_table (info->hash))
7775 return true;
7777 bed = get_elf_backend_data (output_bfd);
7778 (*bed->elf_backend_init_index_section) (output_bfd, info);
7780 /* Assign dynsym indices. In a shared library we generate a section
7781 symbol for each output section, which come first. Next come all
7782 of the back-end allocated local dynamic syms, followed by the rest
7783 of the global symbols.
7785 This is usually not needed for static binaries, however backends
7786 can request to always do it, e.g. the MIPS backend uses dynamic
7787 symbol counts to lay out GOT, which will be produced in the
7788 presence of GOT relocations even in static binaries (holding fixed
7789 data in that case, to satisfy those relocations). */
7791 if (elf_hash_table (info)->dynamic_sections_created
7792 || bed->always_renumber_dynsyms)
7793 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7794 &section_sym_count);
7796 if (elf_hash_table (info)->dynamic_sections_created)
7798 bfd *dynobj;
7799 asection *s;
7800 unsigned int dtagcount;
7802 dynobj = elf_hash_table (info)->dynobj;
7804 /* Work out the size of the symbol version section. */
7805 s = bfd_get_linker_section (dynobj, ".gnu.version");
7806 BFD_ASSERT (s != NULL);
7807 if ((s->flags & SEC_EXCLUDE) == 0)
7809 s->size = dynsymcount * sizeof (Elf_External_Versym);
7810 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7811 if (s->contents == NULL)
7812 return false;
7814 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7815 return false;
7818 /* Set the size of the .dynsym and .hash sections. We counted
7819 the number of dynamic symbols in elf_link_add_object_symbols.
7820 We will build the contents of .dynsym and .hash when we build
7821 the final symbol table, because until then we do not know the
7822 correct value to give the symbols. We built the .dynstr
7823 section as we went along in elf_link_add_object_symbols. */
7824 s = elf_hash_table (info)->dynsym;
7825 BFD_ASSERT (s != NULL);
7826 s->size = dynsymcount * bed->s->sizeof_sym;
7828 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7829 if (s->contents == NULL)
7830 return false;
7832 /* The first entry in .dynsym is a dummy symbol. Clear all the
7833 section syms, in case we don't output them all. */
7834 ++section_sym_count;
7835 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
7837 elf_hash_table (info)->bucketcount = 0;
7839 /* Compute the size of the hashing table. As a side effect this
7840 computes the hash values for all the names we export. */
7841 if (info->emit_hash)
7843 unsigned long int *hashcodes;
7844 struct hash_codes_info hashinf;
7845 bfd_size_type amt;
7846 unsigned long int nsyms;
7847 size_t bucketcount;
7848 size_t hash_entry_size;
7850 /* Compute the hash values for all exported symbols. At the same
7851 time store the values in an array so that we could use them for
7852 optimizations. */
7853 amt = dynsymcount * sizeof (unsigned long int);
7854 hashcodes = (unsigned long int *) bfd_malloc (amt);
7855 if (hashcodes == NULL)
7856 return false;
7857 hashinf.hashcodes = hashcodes;
7858 hashinf.error = false;
7860 /* Put all hash values in HASHCODES. */
7861 elf_link_hash_traverse (elf_hash_table (info),
7862 elf_collect_hash_codes, &hashinf);
7863 if (hashinf.error)
7865 free (hashcodes);
7866 return false;
7869 nsyms = hashinf.hashcodes - hashcodes;
7870 bucketcount
7871 = compute_bucket_count (info, hashcodes, nsyms, 0);
7872 free (hashcodes);
7874 if (bucketcount == 0 && nsyms > 0)
7875 return false;
7877 elf_hash_table (info)->bucketcount = bucketcount;
7879 s = bfd_get_linker_section (dynobj, ".hash");
7880 BFD_ASSERT (s != NULL);
7881 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7882 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
7883 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7884 if (s->contents == NULL)
7885 return false;
7887 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7888 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7889 s->contents + hash_entry_size);
7892 if (info->emit_gnu_hash)
7894 size_t i, cnt;
7895 unsigned char *contents;
7896 struct collect_gnu_hash_codes cinfo;
7897 bfd_size_type amt;
7898 size_t bucketcount;
7900 memset (&cinfo, 0, sizeof (cinfo));
7902 /* Compute the hash values for all exported symbols. At the same
7903 time store the values in an array so that we could use them for
7904 optimizations. */
7905 amt = dynsymcount * 2 * sizeof (unsigned long int);
7906 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
7907 if (cinfo.hashcodes == NULL)
7908 return false;
7910 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7911 cinfo.min_dynindx = -1;
7912 cinfo.output_bfd = output_bfd;
7913 cinfo.bed = bed;
7915 /* Put all hash values in HASHCODES. */
7916 elf_link_hash_traverse (elf_hash_table (info),
7917 elf_collect_gnu_hash_codes, &cinfo);
7918 if (cinfo.error)
7920 free (cinfo.hashcodes);
7921 return false;
7924 bucketcount
7925 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7927 if (bucketcount == 0)
7929 free (cinfo.hashcodes);
7930 return false;
7933 s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (bed));
7934 BFD_ASSERT (s != NULL);
7936 if (cinfo.nsyms == 0)
7938 /* Empty .gnu.hash or .MIPS.xhash section is special. */
7939 BFD_ASSERT (cinfo.min_dynindx == -1);
7940 free (cinfo.hashcodes);
7941 s->size = 5 * 4 + bed->s->arch_size / 8;
7942 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7943 if (contents == NULL)
7944 return false;
7945 s->contents = contents;
7946 /* 1 empty bucket. */
7947 bfd_put_32 (output_bfd, 1, contents);
7948 /* SYMIDX above the special symbol 0. */
7949 bfd_put_32 (output_bfd, 1, contents + 4);
7950 /* Just one word for bitmask. */
7951 bfd_put_32 (output_bfd, 1, contents + 8);
7952 /* Only hash fn bloom filter. */
7953 bfd_put_32 (output_bfd, 0, contents + 12);
7954 /* No hashes are valid - empty bitmask. */
7955 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7956 /* No hashes in the only bucket. */
7957 bfd_put_32 (output_bfd, 0,
7958 contents + 16 + bed->s->arch_size / 8);
7960 else
7962 unsigned long int maskwords, maskbitslog2, x;
7963 BFD_ASSERT (cinfo.min_dynindx != -1);
7965 x = cinfo.nsyms;
7966 maskbitslog2 = 1;
7967 while ((x >>= 1) != 0)
7968 ++maskbitslog2;
7969 if (maskbitslog2 < 3)
7970 maskbitslog2 = 5;
7971 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7972 maskbitslog2 = maskbitslog2 + 3;
7973 else
7974 maskbitslog2 = maskbitslog2 + 2;
7975 if (bed->s->arch_size == 64)
7977 if (maskbitslog2 == 5)
7978 maskbitslog2 = 6;
7979 cinfo.shift1 = 6;
7981 else
7982 cinfo.shift1 = 5;
7983 cinfo.mask = (1 << cinfo.shift1) - 1;
7984 cinfo.shift2 = maskbitslog2;
7985 cinfo.maskbits = 1 << maskbitslog2;
7986 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7987 amt = bucketcount * sizeof (unsigned long int) * 2;
7988 amt += maskwords * sizeof (bfd_vma);
7989 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
7990 if (cinfo.bitmask == NULL)
7992 free (cinfo.hashcodes);
7993 return false;
7996 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
7997 cinfo.indx = cinfo.counts + bucketcount;
7998 cinfo.symindx = dynsymcount - cinfo.nsyms;
7999 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
8001 /* Determine how often each hash bucket is used. */
8002 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
8003 for (i = 0; i < cinfo.nsyms; ++i)
8004 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
8006 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
8007 if (cinfo.counts[i] != 0)
8009 cinfo.indx[i] = cnt;
8010 cnt += cinfo.counts[i];
8012 BFD_ASSERT (cnt == dynsymcount);
8013 cinfo.bucketcount = bucketcount;
8014 cinfo.local_indx = cinfo.min_dynindx;
8016 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
8017 s->size += cinfo.maskbits / 8;
8018 if (bed->record_xhash_symbol != NULL)
8019 s->size += cinfo.nsyms * 4;
8020 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
8021 if (contents == NULL)
8023 free (cinfo.bitmask);
8024 free (cinfo.hashcodes);
8025 return false;
8028 s->contents = contents;
8029 bfd_put_32 (output_bfd, bucketcount, contents);
8030 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
8031 bfd_put_32 (output_bfd, maskwords, contents + 8);
8032 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
8033 contents += 16 + cinfo.maskbits / 8;
8035 for (i = 0; i < bucketcount; ++i)
8037 if (cinfo.counts[i] == 0)
8038 bfd_put_32 (output_bfd, 0, contents);
8039 else
8040 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
8041 contents += 4;
8044 cinfo.contents = contents;
8046 cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents;
8047 /* Renumber dynamic symbols, if populating .gnu.hash section.
8048 If using .MIPS.xhash, populate the translation table. */
8049 elf_link_hash_traverse (elf_hash_table (info),
8050 elf_gnu_hash_process_symidx, &cinfo);
8052 contents = s->contents + 16;
8053 for (i = 0; i < maskwords; ++i)
8055 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
8056 contents);
8057 contents += bed->s->arch_size / 8;
8060 free (cinfo.bitmask);
8061 free (cinfo.hashcodes);
8065 s = bfd_get_linker_section (dynobj, ".dynstr");
8066 BFD_ASSERT (s != NULL);
8068 elf_finalize_dynstr (output_bfd, info);
8070 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
8072 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
8073 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
8074 return false;
8077 return true;
8080 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
8082 static void
8083 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
8084 asection *sec)
8086 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
8087 sec->sec_info_type = SEC_INFO_TYPE_NONE;
8090 /* Finish SHF_MERGE section merging. */
8092 bool
8093 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
8095 bfd *ibfd;
8096 asection *sec;
8098 if (!is_elf_hash_table (info->hash))
8099 return false;
8101 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8102 if ((ibfd->flags & DYNAMIC) == 0
8103 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
8104 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
8105 == get_elf_backend_data (obfd)->s->elfclass))
8106 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8107 if ((sec->flags & SEC_MERGE) != 0
8108 && !bfd_is_abs_section (sec->output_section))
8110 struct bfd_elf_section_data *secdata;
8112 secdata = elf_section_data (sec);
8113 if (! _bfd_add_merge_section (obfd,
8114 &elf_hash_table (info)->merge_info,
8115 sec, &secdata->sec_info))
8116 return false;
8117 else if (secdata->sec_info)
8118 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
8121 if (elf_hash_table (info)->merge_info != NULL)
8122 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
8123 merge_sections_remove_hook);
8124 return true;
8127 /* Create an entry in an ELF linker hash table. */
8129 struct bfd_hash_entry *
8130 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
8131 struct bfd_hash_table *table,
8132 const char *string)
8134 /* Allocate the structure if it has not already been allocated by a
8135 subclass. */
8136 if (entry == NULL)
8138 entry = (struct bfd_hash_entry *)
8139 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
8140 if (entry == NULL)
8141 return entry;
8144 /* Call the allocation method of the superclass. */
8145 entry = _bfd_link_hash_newfunc (entry, table, string);
8146 if (entry != NULL)
8148 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
8149 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
8151 /* Set local fields. */
8152 ret->indx = -1;
8153 ret->dynindx = -1;
8154 ret->got = htab->init_got_refcount;
8155 ret->plt = htab->init_plt_refcount;
8156 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
8157 - offsetof (struct elf_link_hash_entry, size)));
8158 /* Assume that we have been called by a non-ELF symbol reader.
8159 This flag is then reset by the code which reads an ELF input
8160 file. This ensures that a symbol created by a non-ELF symbol
8161 reader will have the flag set correctly. */
8162 ret->non_elf = 1;
8165 return entry;
8168 /* Copy data from an indirect symbol to its direct symbol, hiding the
8169 old indirect symbol. Also used for copying flags to a weakdef. */
8171 void
8172 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
8173 struct elf_link_hash_entry *dir,
8174 struct elf_link_hash_entry *ind)
8176 struct elf_link_hash_table *htab;
8178 if (ind->dyn_relocs != NULL)
8180 if (dir->dyn_relocs != NULL)
8182 struct elf_dyn_relocs **pp;
8183 struct elf_dyn_relocs *p;
8185 /* Add reloc counts against the indirect sym to the direct sym
8186 list. Merge any entries against the same section. */
8187 for (pp = &ind->dyn_relocs; (p = *pp) != NULL; )
8189 struct elf_dyn_relocs *q;
8191 for (q = dir->dyn_relocs; q != NULL; q = q->next)
8192 if (q->sec == p->sec)
8194 q->pc_count += p->pc_count;
8195 q->count += p->count;
8196 *pp = p->next;
8197 break;
8199 if (q == NULL)
8200 pp = &p->next;
8202 *pp = dir->dyn_relocs;
8205 dir->dyn_relocs = ind->dyn_relocs;
8206 ind->dyn_relocs = NULL;
8209 /* Copy down any references that we may have already seen to the
8210 symbol which just became indirect. */
8212 if (dir->versioned != versioned_hidden)
8213 dir->ref_dynamic |= ind->ref_dynamic;
8214 dir->ref_regular |= ind->ref_regular;
8215 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
8216 dir->non_got_ref |= ind->non_got_ref;
8217 dir->needs_plt |= ind->needs_plt;
8218 dir->pointer_equality_needed |= ind->pointer_equality_needed;
8220 if (ind->root.type != bfd_link_hash_indirect)
8221 return;
8223 /* Copy over the global and procedure linkage table refcount entries.
8224 These may have been already set up by a check_relocs routine. */
8225 htab = elf_hash_table (info);
8226 if (ind->got.refcount > htab->init_got_refcount.refcount)
8228 if (dir->got.refcount < 0)
8229 dir->got.refcount = 0;
8230 dir->got.refcount += ind->got.refcount;
8231 ind->got.refcount = htab->init_got_refcount.refcount;
8234 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
8236 if (dir->plt.refcount < 0)
8237 dir->plt.refcount = 0;
8238 dir->plt.refcount += ind->plt.refcount;
8239 ind->plt.refcount = htab->init_plt_refcount.refcount;
8242 if (ind->dynindx != -1)
8244 if (dir->dynindx != -1)
8245 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
8246 dir->dynindx = ind->dynindx;
8247 dir->dynstr_index = ind->dynstr_index;
8248 ind->dynindx = -1;
8249 ind->dynstr_index = 0;
8253 void
8254 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
8255 struct elf_link_hash_entry *h,
8256 bool force_local)
8258 /* STT_GNU_IFUNC symbol must go through PLT. */
8259 if (h->type != STT_GNU_IFUNC)
8261 h->plt = elf_hash_table (info)->init_plt_offset;
8262 h->needs_plt = 0;
8264 if (force_local)
8266 h->forced_local = 1;
8267 if (h->dynindx != -1)
8269 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8270 h->dynstr_index);
8271 h->dynindx = -1;
8272 h->dynstr_index = 0;
8277 /* Hide a symbol. */
8279 void
8280 _bfd_elf_link_hide_symbol (bfd *output_bfd,
8281 struct bfd_link_info *info,
8282 struct bfd_link_hash_entry *h)
8284 if (is_elf_hash_table (info->hash))
8286 const struct elf_backend_data *bed
8287 = get_elf_backend_data (output_bfd);
8288 struct elf_link_hash_entry *eh
8289 = (struct elf_link_hash_entry *) h;
8290 bed->elf_backend_hide_symbol (info, eh, true);
8291 eh->def_dynamic = 0;
8292 eh->ref_dynamic = 0;
8293 eh->dynamic_def = 0;
8297 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
8298 caller. */
8300 bool
8301 _bfd_elf_link_hash_table_init
8302 (struct elf_link_hash_table *table,
8303 bfd *abfd,
8304 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
8305 struct bfd_hash_table *,
8306 const char *),
8307 unsigned int entsize,
8308 enum elf_target_id target_id)
8310 bool ret;
8311 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
8313 table->init_got_refcount.refcount = can_refcount - 1;
8314 table->init_plt_refcount.refcount = can_refcount - 1;
8315 table->init_got_offset.offset = -(bfd_vma) 1;
8316 table->init_plt_offset.offset = -(bfd_vma) 1;
8317 /* The first dynamic symbol is a dummy. */
8318 table->dynsymcount = 1;
8320 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
8322 table->root.type = bfd_link_elf_hash_table;
8323 table->hash_table_id = target_id;
8324 table->target_os = get_elf_backend_data (abfd)->target_os;
8326 return ret;
8329 /* Create an ELF linker hash table. */
8331 struct bfd_link_hash_table *
8332 _bfd_elf_link_hash_table_create (bfd *abfd)
8334 struct elf_link_hash_table *ret;
8335 size_t amt = sizeof (struct elf_link_hash_table);
8337 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
8338 if (ret == NULL)
8339 return NULL;
8341 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
8342 sizeof (struct elf_link_hash_entry),
8343 GENERIC_ELF_DATA))
8345 free (ret);
8346 return NULL;
8348 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
8350 return &ret->root;
8353 /* Destroy an ELF linker hash table. */
8355 void
8356 _bfd_elf_link_hash_table_free (bfd *obfd)
8358 struct elf_link_hash_table *htab;
8360 htab = (struct elf_link_hash_table *) obfd->link.hash;
8361 if (htab->dynstr != NULL)
8362 _bfd_elf_strtab_free (htab->dynstr);
8363 _bfd_merge_sections_free (htab->merge_info);
8364 /* NB: htab->dynamic->contents is always allocated by bfd_realloc. */
8365 if (htab->dynamic != NULL)
8366 free (htab->dynamic->contents);
8367 if (htab->first_hash != NULL)
8369 bfd_hash_table_free (htab->first_hash);
8370 free (htab->first_hash);
8372 _bfd_generic_link_hash_table_free (obfd);
8375 /* This is a hook for the ELF emulation code in the generic linker to
8376 tell the backend linker what file name to use for the DT_NEEDED
8377 entry for a dynamic object. */
8379 void
8380 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
8382 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8383 && bfd_get_format (abfd) == bfd_object)
8384 elf_dt_name (abfd) = name;
8388 bfd_elf_get_dyn_lib_class (bfd *abfd)
8390 int lib_class;
8391 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8392 && bfd_get_format (abfd) == bfd_object)
8393 lib_class = elf_dyn_lib_class (abfd);
8394 else
8395 lib_class = 0;
8396 return lib_class;
8399 void
8400 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
8402 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8403 && bfd_get_format (abfd) == bfd_object)
8404 elf_dyn_lib_class (abfd) = lib_class;
8407 /* Get the list of DT_NEEDED entries for a link. This is a hook for
8408 the linker ELF emulation code. */
8410 struct bfd_link_needed_list *
8411 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
8412 struct bfd_link_info *info)
8414 if (! is_elf_hash_table (info->hash))
8415 return NULL;
8416 return elf_hash_table (info)->needed;
8419 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
8420 hook for the linker ELF emulation code. */
8422 struct bfd_link_needed_list *
8423 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
8424 struct bfd_link_info *info)
8426 if (! is_elf_hash_table (info->hash))
8427 return NULL;
8428 return elf_hash_table (info)->runpath;
8431 /* Get the name actually used for a dynamic object for a link. This
8432 is the SONAME entry if there is one. Otherwise, it is the string
8433 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
8435 const char *
8436 bfd_elf_get_dt_soname (bfd *abfd)
8438 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8439 && bfd_get_format (abfd) == bfd_object)
8440 return elf_dt_name (abfd);
8441 return NULL;
8444 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
8445 the ELF linker emulation code. */
8447 bool
8448 bfd_elf_get_bfd_needed_list (bfd *abfd,
8449 struct bfd_link_needed_list **pneeded)
8451 asection *s;
8452 bfd_byte *dynbuf = NULL;
8453 unsigned int elfsec;
8454 unsigned long shlink;
8455 bfd_byte *extdyn, *extdynend;
8456 size_t extdynsize;
8457 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
8459 *pneeded = NULL;
8461 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
8462 || bfd_get_format (abfd) != bfd_object)
8463 return true;
8465 s = bfd_get_section_by_name (abfd, ".dynamic");
8466 if (s == NULL || s->size == 0 || (s->flags & SEC_HAS_CONTENTS) == 0)
8467 return true;
8469 if (!_bfd_elf_mmap_section_contents (abfd, s, &dynbuf))
8470 goto error_return;
8472 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
8473 if (elfsec == SHN_BAD)
8474 goto error_return;
8476 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
8478 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
8479 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
8481 for (extdyn = dynbuf, extdynend = dynbuf + s->size;
8482 (size_t) (extdynend - extdyn) >= extdynsize;
8483 extdyn += extdynsize)
8485 Elf_Internal_Dyn dyn;
8487 (*swap_dyn_in) (abfd, extdyn, &dyn);
8489 if (dyn.d_tag == DT_NULL)
8490 break;
8492 if (dyn.d_tag == DT_NEEDED)
8494 const char *string;
8495 struct bfd_link_needed_list *l;
8496 unsigned int tagv = dyn.d_un.d_val;
8497 size_t amt;
8499 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
8500 if (string == NULL)
8501 goto error_return;
8503 amt = sizeof *l;
8504 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
8505 if (l == NULL)
8506 goto error_return;
8508 l->by = abfd;
8509 l->name = string;
8510 l->next = *pneeded;
8511 *pneeded = l;
8515 _bfd_elf_munmap_section_contents (s, dynbuf);
8517 return true;
8519 error_return:
8520 _bfd_elf_munmap_section_contents (s, dynbuf);
8521 return false;
8524 struct elf_symbuf_symbol
8526 unsigned long st_name; /* Symbol name, index in string tbl */
8527 unsigned char st_info; /* Type and binding attributes */
8528 unsigned char st_other; /* Visibilty, and target specific */
8531 struct elf_symbuf_head
8533 struct elf_symbuf_symbol *ssym;
8534 size_t count;
8535 unsigned int st_shndx;
8538 struct elf_symbol
8540 union
8542 Elf_Internal_Sym *isym;
8543 struct elf_symbuf_symbol *ssym;
8544 void *p;
8545 } u;
8546 const char *name;
8549 /* Sort references to symbols by ascending section number. */
8551 static int
8552 elf_sort_elf_symbol (const void *arg1, const void *arg2)
8554 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
8555 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
8557 if (s1->st_shndx != s2->st_shndx)
8558 return s1->st_shndx > s2->st_shndx ? 1 : -1;
8559 /* Final sort by the address of the sym in the symbuf ensures
8560 a stable sort. */
8561 if (s1 != s2)
8562 return s1 > s2 ? 1 : -1;
8563 return 0;
8566 static int
8567 elf_sym_name_compare (const void *arg1, const void *arg2)
8569 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
8570 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
8571 int ret = strcmp (s1->name, s2->name);
8572 if (ret != 0)
8573 return ret;
8574 if (s1->u.p != s2->u.p)
8575 return s1->u.p > s2->u.p ? 1 : -1;
8576 return 0;
8579 static struct elf_symbuf_head *
8580 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
8582 Elf_Internal_Sym **ind, **indbufend, **indbuf;
8583 struct elf_symbuf_symbol *ssym;
8584 struct elf_symbuf_head *ssymbuf, *ssymhead;
8585 size_t i, shndx_count, total_size, amt;
8587 amt = symcount * sizeof (*indbuf);
8588 indbuf = (Elf_Internal_Sym **) bfd_malloc (amt);
8589 if (indbuf == NULL)
8590 return NULL;
8592 for (ind = indbuf, i = 0; i < symcount; i++)
8593 if (isymbuf[i].st_shndx != SHN_UNDEF)
8594 *ind++ = &isymbuf[i];
8595 indbufend = ind;
8597 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
8598 elf_sort_elf_symbol);
8600 shndx_count = 0;
8601 if (indbufend > indbuf)
8602 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
8603 if (ind[0]->st_shndx != ind[1]->st_shndx)
8604 shndx_count++;
8606 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
8607 + (indbufend - indbuf) * sizeof (*ssym));
8608 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
8609 if (ssymbuf == NULL)
8611 free (indbuf);
8612 return NULL;
8615 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
8616 ssymbuf->ssym = NULL;
8617 ssymbuf->count = shndx_count;
8618 ssymbuf->st_shndx = 0;
8619 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
8621 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
8623 ssymhead++;
8624 ssymhead->ssym = ssym;
8625 ssymhead->count = 0;
8626 ssymhead->st_shndx = (*ind)->st_shndx;
8628 ssym->st_name = (*ind)->st_name;
8629 ssym->st_info = (*ind)->st_info;
8630 ssym->st_other = (*ind)->st_other;
8631 ssymhead->count++;
8633 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
8634 && (uintptr_t) ssym - (uintptr_t) ssymbuf == total_size);
8636 free (indbuf);
8637 return ssymbuf;
8640 /* Check if 2 sections define the same set of local and global
8641 symbols. */
8643 static bool
8644 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
8645 struct bfd_link_info *info)
8647 bfd *bfd1, *bfd2;
8648 const struct elf_backend_data *bed1, *bed2;
8649 Elf_Internal_Shdr *hdr1, *hdr2;
8650 size_t symcount1, symcount2;
8651 Elf_Internal_Sym *isymbuf1, *isymbuf2;
8652 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
8653 Elf_Internal_Sym *isym, *isymend;
8654 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
8655 size_t count1, count2, sec_count1, sec_count2, i;
8656 unsigned int shndx1, shndx2;
8657 bool result;
8658 bool ignore_section_symbol_p;
8660 bfd1 = sec1->owner;
8661 bfd2 = sec2->owner;
8663 /* Both sections have to be in ELF. */
8664 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
8665 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
8666 return false;
8668 if (elf_section_type (sec1) != elf_section_type (sec2))
8669 return false;
8671 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
8672 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
8673 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
8674 return false;
8676 bed1 = get_elf_backend_data (bfd1);
8677 bed2 = get_elf_backend_data (bfd2);
8678 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
8679 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
8680 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
8681 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
8683 if (symcount1 == 0 || symcount2 == 0)
8684 return false;
8686 result = false;
8687 isymbuf1 = NULL;
8688 isymbuf2 = NULL;
8689 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
8690 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
8692 /* Ignore section symbols only when matching non-debugging sections
8693 or linkonce section with comdat section. */
8694 ignore_section_symbol_p
8695 = ((sec1->flags & SEC_DEBUGGING) == 0
8696 || ((elf_section_flags (sec1) & SHF_GROUP)
8697 != (elf_section_flags (sec2) & SHF_GROUP)));
8699 if (ssymbuf1 == NULL)
8701 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
8702 NULL, NULL, NULL);
8703 if (isymbuf1 == NULL)
8704 goto done;
8706 if (info != NULL && !info->reduce_memory_overheads)
8708 ssymbuf1 = elf_create_symbuf (symcount1, isymbuf1);
8709 elf_tdata (bfd1)->symbuf = ssymbuf1;
8713 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
8715 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
8716 NULL, NULL, NULL);
8717 if (isymbuf2 == NULL)
8718 goto done;
8720 if (ssymbuf1 != NULL && info != NULL && !info->reduce_memory_overheads)
8722 ssymbuf2 = elf_create_symbuf (symcount2, isymbuf2);
8723 elf_tdata (bfd2)->symbuf = ssymbuf2;
8727 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
8729 /* Optimized faster version. */
8730 size_t lo, hi, mid;
8731 struct elf_symbol *symp;
8732 struct elf_symbuf_symbol *ssym, *ssymend;
8734 lo = 0;
8735 hi = ssymbuf1->count;
8736 ssymbuf1++;
8737 count1 = 0;
8738 sec_count1 = 0;
8739 while (lo < hi)
8741 mid = (lo + hi) / 2;
8742 if (shndx1 < ssymbuf1[mid].st_shndx)
8743 hi = mid;
8744 else if (shndx1 > ssymbuf1[mid].st_shndx)
8745 lo = mid + 1;
8746 else
8748 count1 = ssymbuf1[mid].count;
8749 ssymbuf1 += mid;
8750 break;
8753 if (ignore_section_symbol_p)
8755 for (i = 0; i < count1; i++)
8756 if (ELF_ST_TYPE (ssymbuf1->ssym[i].st_info) == STT_SECTION)
8757 sec_count1++;
8758 count1 -= sec_count1;
8761 lo = 0;
8762 hi = ssymbuf2->count;
8763 ssymbuf2++;
8764 count2 = 0;
8765 sec_count2 = 0;
8766 while (lo < hi)
8768 mid = (lo + hi) / 2;
8769 if (shndx2 < ssymbuf2[mid].st_shndx)
8770 hi = mid;
8771 else if (shndx2 > ssymbuf2[mid].st_shndx)
8772 lo = mid + 1;
8773 else
8775 count2 = ssymbuf2[mid].count;
8776 ssymbuf2 += mid;
8777 break;
8780 if (ignore_section_symbol_p)
8782 for (i = 0; i < count2; i++)
8783 if (ELF_ST_TYPE (ssymbuf2->ssym[i].st_info) == STT_SECTION)
8784 sec_count2++;
8785 count2 -= sec_count2;
8788 if (count1 == 0 || count2 == 0 || count1 != count2)
8789 goto done;
8791 symtable1
8792 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
8793 symtable2
8794 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
8795 if (symtable1 == NULL || symtable2 == NULL)
8796 goto done;
8798 symp = symtable1;
8799 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1 + sec_count1;
8800 ssym < ssymend; ssym++)
8801 if (sec_count1 == 0
8802 || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
8804 symp->u.ssym = ssym;
8805 symp->name = bfd_elf_string_from_elf_section (bfd1,
8806 hdr1->sh_link,
8807 ssym->st_name);
8808 symp++;
8811 symp = symtable2;
8812 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2 + sec_count2;
8813 ssym < ssymend; ssym++)
8814 if (sec_count2 == 0
8815 || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
8817 symp->u.ssym = ssym;
8818 symp->name = bfd_elf_string_from_elf_section (bfd2,
8819 hdr2->sh_link,
8820 ssym->st_name);
8821 symp++;
8824 /* Sort symbol by name. */
8825 qsort (symtable1, count1, sizeof (struct elf_symbol),
8826 elf_sym_name_compare);
8827 qsort (symtable2, count1, sizeof (struct elf_symbol),
8828 elf_sym_name_compare);
8830 for (i = 0; i < count1; i++)
8831 /* Two symbols must have the same binding, type and name. */
8832 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8833 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8834 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8835 goto done;
8837 result = true;
8838 goto done;
8841 symtable1 = (struct elf_symbol *)
8842 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8843 symtable2 = (struct elf_symbol *)
8844 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
8845 if (symtable1 == NULL || symtable2 == NULL)
8846 goto done;
8848 /* Count definitions in the section. */
8849 count1 = 0;
8850 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
8851 if (isym->st_shndx == shndx1
8852 && (!ignore_section_symbol_p
8853 || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
8854 symtable1[count1++].u.isym = isym;
8856 count2 = 0;
8857 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
8858 if (isym->st_shndx == shndx2
8859 && (!ignore_section_symbol_p
8860 || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
8861 symtable2[count2++].u.isym = isym;
8863 if (count1 == 0 || count2 == 0 || count1 != count2)
8864 goto done;
8866 for (i = 0; i < count1; i++)
8867 symtable1[i].name
8868 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8869 symtable1[i].u.isym->st_name);
8871 for (i = 0; i < count2; i++)
8872 symtable2[i].name
8873 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8874 symtable2[i].u.isym->st_name);
8876 /* Sort symbol by name. */
8877 qsort (symtable1, count1, sizeof (struct elf_symbol),
8878 elf_sym_name_compare);
8879 qsort (symtable2, count1, sizeof (struct elf_symbol),
8880 elf_sym_name_compare);
8882 for (i = 0; i < count1; i++)
8883 /* Two symbols must have the same binding, type and name. */
8884 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8885 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8886 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8887 goto done;
8889 result = true;
8891 done:
8892 free (symtable1);
8893 free (symtable2);
8894 free (isymbuf1);
8895 free (isymbuf2);
8897 return result;
8900 /* Return TRUE if 2 section types are compatible. */
8902 bool
8903 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8904 bfd *bbfd, const asection *bsec)
8906 if (asec == NULL
8907 || bsec == NULL
8908 || abfd->xvec->flavour != bfd_target_elf_flavour
8909 || bbfd->xvec->flavour != bfd_target_elf_flavour)
8910 return true;
8912 return elf_section_type (asec) == elf_section_type (bsec);
8915 /* Final phase of ELF linker. */
8917 /* A structure we use to avoid passing large numbers of arguments. */
8919 struct elf_final_link_info
8921 /* General link information. */
8922 struct bfd_link_info *info;
8923 /* Output BFD. */
8924 bfd *output_bfd;
8925 /* Symbol string table. */
8926 struct elf_strtab_hash *symstrtab;
8927 /* .hash section. */
8928 asection *hash_sec;
8929 /* symbol version section (.gnu.version). */
8930 asection *symver_sec;
8931 /* Buffer large enough to hold contents of any section. */
8932 bfd_byte *contents;
8933 /* Buffer large enough to hold external relocs of any section. */
8934 void *external_relocs;
8935 /* Buffer large enough to hold internal relocs of any section. */
8936 Elf_Internal_Rela *internal_relocs;
8937 /* Buffer large enough to hold external local symbols of any input
8938 BFD. */
8939 bfd_byte *external_syms;
8940 /* And a buffer for symbol section indices. */
8941 Elf_External_Sym_Shndx *locsym_shndx;
8942 /* Buffer large enough to hold internal local symbols of any input
8943 BFD. */
8944 Elf_Internal_Sym *internal_syms;
8945 /* Array large enough to hold a symbol index for each local symbol
8946 of any input BFD. */
8947 long *indices;
8948 /* Array large enough to hold a section pointer for each local
8949 symbol of any input BFD. */
8950 asection **sections;
8951 /* Buffer for SHT_SYMTAB_SHNDX section. */
8952 Elf_External_Sym_Shndx *symshndxbuf;
8953 /* Number of STT_FILE syms seen. */
8954 size_t filesym_count;
8955 /* Local symbol hash table. */
8956 struct bfd_hash_table local_hash_table;
8959 struct local_hash_entry
8961 /* Base hash table entry structure. */
8962 struct bfd_hash_entry root;
8963 /* Size of the local symbol name. */
8964 size_t size;
8965 /* Number of the duplicated local symbol names. */
8966 long count;
8969 /* Create an entry in the local symbol hash table. */
8971 static struct bfd_hash_entry *
8972 local_hash_newfunc (struct bfd_hash_entry *entry,
8973 struct bfd_hash_table *table,
8974 const char *string)
8977 /* Allocate the structure if it has not already been allocated by a
8978 subclass. */
8979 if (entry == NULL)
8981 entry = bfd_hash_allocate (table,
8982 sizeof (struct local_hash_entry));
8983 if (entry == NULL)
8984 return entry;
8987 /* Call the allocation method of the superclass. */
8988 entry = bfd_hash_newfunc (entry, table, string);
8989 if (entry != NULL)
8991 ((struct local_hash_entry *) entry)->count = 0;
8992 ((struct local_hash_entry *) entry)->size = 0;
8995 return entry;
8998 /* This struct is used to pass information to elf_link_output_extsym. */
9000 struct elf_outext_info
9002 bool failed;
9003 bool localsyms;
9004 bool file_sym_done;
9005 struct elf_final_link_info *flinfo;
9009 /* Support for evaluating a complex relocation.
9011 Complex relocations are generalized, self-describing relocations. The
9012 implementation of them consists of two parts: complex symbols, and the
9013 relocations themselves.
9015 The relocations use a reserved elf-wide relocation type code (R_RELC
9016 external / BFD_RELOC_RELC internal) and an encoding of relocation field
9017 information (start bit, end bit, word width, etc) into the addend. This
9018 information is extracted from CGEN-generated operand tables within gas.
9020 Complex symbols are mangled symbols (STT_RELC external / BSF_RELC
9021 internal) representing prefix-notation expressions, including but not
9022 limited to those sorts of expressions normally encoded as addends in the
9023 addend field. The symbol mangling format is:
9025 <node> := <literal>
9026 | <unary-operator> ':' <node>
9027 | <binary-operator> ':' <node> ':' <node>
9030 <literal> := 's' <digits=N> ':' <N character symbol name>
9031 | 'S' <digits=N> ':' <N character section name>
9032 | '#' <hexdigits>
9035 <binary-operator> := as in C
9036 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
9038 static void
9039 set_symbol_value (bfd *bfd_with_globals,
9040 Elf_Internal_Sym *isymbuf,
9041 size_t locsymcount,
9042 size_t symidx,
9043 bfd_vma val)
9045 struct elf_link_hash_entry **sym_hashes;
9046 struct elf_link_hash_entry *h;
9047 size_t extsymoff = locsymcount;
9049 if (symidx < locsymcount)
9051 Elf_Internal_Sym *sym;
9053 sym = isymbuf + symidx;
9054 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
9056 /* It is a local symbol: move it to the
9057 "absolute" section and give it a value. */
9058 sym->st_shndx = SHN_ABS;
9059 sym->st_value = val;
9060 return;
9062 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
9063 extsymoff = 0;
9066 /* It is a global symbol: set its link type
9067 to "defined" and give it a value. */
9069 sym_hashes = elf_sym_hashes (bfd_with_globals);
9070 h = sym_hashes [symidx - extsymoff];
9071 while (h->root.type == bfd_link_hash_indirect
9072 || h->root.type == bfd_link_hash_warning)
9073 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9074 h->root.type = bfd_link_hash_defined;
9075 h->root.u.def.value = val;
9076 h->root.u.def.section = bfd_abs_section_ptr;
9079 static bool
9080 resolve_symbol (const char *name,
9081 bfd *input_bfd,
9082 struct elf_final_link_info *flinfo,
9083 bfd_vma *result,
9084 Elf_Internal_Sym *isymbuf,
9085 size_t locsymcount)
9087 Elf_Internal_Sym *sym;
9088 struct bfd_link_hash_entry *global_entry;
9089 const char *candidate = NULL;
9090 Elf_Internal_Shdr *symtab_hdr;
9091 size_t i;
9093 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
9095 for (i = 0; i < locsymcount; ++ i)
9097 sym = isymbuf + i;
9099 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
9100 continue;
9102 candidate = bfd_elf_string_from_elf_section (input_bfd,
9103 symtab_hdr->sh_link,
9104 sym->st_name);
9105 #ifdef DEBUG
9106 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
9107 name, candidate, (unsigned long) sym->st_value);
9108 #endif
9109 if (candidate && strcmp (candidate, name) == 0)
9111 asection *sec = flinfo->sections [i];
9113 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
9114 *result += sec->output_offset + sec->output_section->vma;
9115 #ifdef DEBUG
9116 printf ("Found symbol with value %8.8lx\n",
9117 (unsigned long) *result);
9118 #endif
9119 return true;
9123 /* Hmm, haven't found it yet. perhaps it is a global. */
9124 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
9125 false, false, true);
9126 if (!global_entry)
9127 return false;
9129 if (global_entry->type == bfd_link_hash_defined
9130 || global_entry->type == bfd_link_hash_defweak)
9132 *result = (global_entry->u.def.value
9133 + global_entry->u.def.section->output_section->vma
9134 + global_entry->u.def.section->output_offset);
9135 #ifdef DEBUG
9136 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
9137 global_entry->root.string, (unsigned long) *result);
9138 #endif
9139 return true;
9142 return false;
9145 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
9146 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
9147 names like "foo.end" which is the end address of section "foo". */
9149 static bool
9150 resolve_section (const char *name,
9151 asection *sections,
9152 bfd_vma *result,
9153 bfd * abfd)
9155 asection *curr;
9156 unsigned int len;
9158 for (curr = sections; curr; curr = curr->next)
9159 if (strcmp (curr->name, name) == 0)
9161 *result = curr->vma;
9162 return true;
9165 /* Hmm. still haven't found it. try pseudo-section names. */
9166 /* FIXME: This could be coded more efficiently... */
9167 for (curr = sections; curr; curr = curr->next)
9169 len = strlen (curr->name);
9170 if (len > strlen (name))
9171 continue;
9173 if (strncmp (curr->name, name, len) == 0)
9175 if (startswith (name + len, ".end"))
9177 *result = (curr->vma
9178 + curr->size / bfd_octets_per_byte (abfd, curr));
9179 return true;
9182 /* Insert more pseudo-section names here, if you like. */
9186 return false;
9189 static void
9190 undefined_reference (const char *reftype, const char *name)
9192 /* xgettext:c-format */
9193 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
9194 reftype, name);
9195 bfd_set_error (bfd_error_bad_value);
9198 static bool
9199 eval_symbol (bfd_vma *result,
9200 const char **symp,
9201 bfd *input_bfd,
9202 struct elf_final_link_info *flinfo,
9203 bfd_vma dot,
9204 Elf_Internal_Sym *isymbuf,
9205 size_t locsymcount,
9206 int signed_p)
9208 size_t len;
9209 size_t symlen;
9210 bfd_vma a;
9211 bfd_vma b;
9212 char symbuf[4096];
9213 const char *sym = *symp;
9214 const char *symend;
9215 bool symbol_is_section = false;
9217 len = strlen (sym);
9218 symend = sym + len;
9220 if (len < 1 || len > sizeof (symbuf))
9222 bfd_set_error (bfd_error_invalid_operation);
9223 return false;
9226 switch (* sym)
9228 case '.':
9229 *result = dot;
9230 *symp = sym + 1;
9231 return true;
9233 case '#':
9234 ++sym;
9235 *result = strtoul (sym, (char **) symp, 16);
9236 return true;
9238 case 'S':
9239 symbol_is_section = true;
9240 /* Fall through. */
9241 case 's':
9242 ++sym;
9243 symlen = strtol (sym, (char **) symp, 10);
9244 sym = *symp + 1; /* Skip the trailing ':'. */
9246 if (symend < sym || symlen + 1 > sizeof (symbuf))
9248 bfd_set_error (bfd_error_invalid_operation);
9249 return false;
9252 memcpy (symbuf, sym, symlen);
9253 symbuf[symlen] = '\0';
9254 *symp = sym + symlen;
9256 /* Is it always possible, with complex symbols, that gas "mis-guessed"
9257 the symbol as a section, or vice-versa. so we're pretty liberal in our
9258 interpretation here; section means "try section first", not "must be a
9259 section", and likewise with symbol. */
9261 if (symbol_is_section)
9263 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
9264 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
9265 isymbuf, locsymcount))
9267 undefined_reference ("section", symbuf);
9268 return false;
9271 else
9273 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
9274 isymbuf, locsymcount)
9275 && !resolve_section (symbuf, flinfo->output_bfd->sections,
9276 result, input_bfd))
9278 undefined_reference ("symbol", symbuf);
9279 return false;
9283 return true;
9285 /* All that remains are operators. */
9287 #define UNARY_OP(op) \
9288 if (startswith (sym, #op)) \
9290 sym += strlen (#op); \
9291 if (*sym == ':') \
9292 ++sym; \
9293 *symp = sym; \
9294 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
9295 isymbuf, locsymcount, signed_p)) \
9296 return false; \
9297 if (signed_p) \
9298 *result = op ((bfd_signed_vma) a); \
9299 else \
9300 *result = op a; \
9301 return true; \
9304 #define BINARY_OP_HEAD(op) \
9305 if (startswith (sym, #op)) \
9307 sym += strlen (#op); \
9308 if (*sym == ':') \
9309 ++sym; \
9310 *symp = sym; \
9311 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
9312 isymbuf, locsymcount, signed_p)) \
9313 return false; \
9314 ++*symp; \
9315 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
9316 isymbuf, locsymcount, signed_p)) \
9317 return false;
9318 #define BINARY_OP_TAIL(op) \
9319 if (signed_p) \
9320 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
9321 else \
9322 *result = a op b; \
9323 return true; \
9325 #define BINARY_OP(op) BINARY_OP_HEAD(op) BINARY_OP_TAIL(op)
9327 default:
9328 UNARY_OP (0-);
9329 BINARY_OP_HEAD (<<);
9330 if (b >= sizeof (a) * CHAR_BIT)
9332 *result = 0;
9333 return true;
9335 signed_p = 0;
9336 BINARY_OP_TAIL (<<);
9337 BINARY_OP_HEAD (>>);
9338 if (b >= sizeof (a) * CHAR_BIT)
9340 *result = signed_p && (bfd_signed_vma) a < 0 ? -1 : 0;
9341 return true;
9343 BINARY_OP_TAIL (>>);
9344 BINARY_OP (==);
9345 BINARY_OP (!=);
9346 BINARY_OP (<=);
9347 BINARY_OP (>=);
9348 BINARY_OP (&&);
9349 BINARY_OP (||);
9350 UNARY_OP (~);
9351 UNARY_OP (!);
9352 BINARY_OP (*);
9353 BINARY_OP_HEAD (/);
9354 if (b == 0)
9356 _bfd_error_handler (_("division by zero"));
9357 bfd_set_error (bfd_error_bad_value);
9358 return false;
9360 BINARY_OP_TAIL (/);
9361 BINARY_OP_HEAD (%);
9362 if (b == 0)
9364 _bfd_error_handler (_("division by zero"));
9365 bfd_set_error (bfd_error_bad_value);
9366 return false;
9368 BINARY_OP_TAIL (%);
9369 BINARY_OP (^);
9370 BINARY_OP (|);
9371 BINARY_OP (&);
9372 BINARY_OP (+);
9373 BINARY_OP (-);
9374 BINARY_OP (<);
9375 BINARY_OP (>);
9376 #undef UNARY_OP
9377 #undef BINARY_OP
9378 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
9379 bfd_set_error (bfd_error_invalid_operation);
9380 return false;
9384 static void
9385 put_value (bfd_vma size,
9386 unsigned long chunksz,
9387 bfd *input_bfd,
9388 bfd_vma x,
9389 bfd_byte *location)
9391 location += (size - chunksz);
9393 for (; size; size -= chunksz, location -= chunksz)
9395 switch (chunksz)
9397 case 1:
9398 bfd_put_8 (input_bfd, x, location);
9399 x >>= 8;
9400 break;
9401 case 2:
9402 bfd_put_16 (input_bfd, x, location);
9403 x >>= 16;
9404 break;
9405 case 4:
9406 bfd_put_32 (input_bfd, x, location);
9407 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
9408 x >>= 16;
9409 x >>= 16;
9410 break;
9411 #ifdef BFD64
9412 case 8:
9413 bfd_put_64 (input_bfd, x, location);
9414 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
9415 x >>= 32;
9416 x >>= 32;
9417 break;
9418 #endif
9419 default:
9420 abort ();
9421 break;
9426 static bfd_vma
9427 get_value (bfd_vma size,
9428 unsigned long chunksz,
9429 bfd *input_bfd,
9430 bfd_byte *location)
9432 int shift;
9433 bfd_vma x = 0;
9435 /* Sanity checks. */
9436 BFD_ASSERT (chunksz <= sizeof (x)
9437 && size >= chunksz
9438 && chunksz != 0
9439 && (size % chunksz) == 0
9440 && input_bfd != NULL
9441 && location != NULL);
9443 if (chunksz == sizeof (x))
9445 BFD_ASSERT (size == chunksz);
9447 /* Make sure that we do not perform an undefined shift operation.
9448 We know that size == chunksz so there will only be one iteration
9449 of the loop below. */
9450 shift = 0;
9452 else
9453 shift = 8 * chunksz;
9455 for (; size; size -= chunksz, location += chunksz)
9457 switch (chunksz)
9459 case 1:
9460 x = (x << shift) | bfd_get_8 (input_bfd, location);
9461 break;
9462 case 2:
9463 x = (x << shift) | bfd_get_16 (input_bfd, location);
9464 break;
9465 case 4:
9466 x = (x << shift) | bfd_get_32 (input_bfd, location);
9467 break;
9468 #ifdef BFD64
9469 case 8:
9470 x = (x << shift) | bfd_get_64 (input_bfd, location);
9471 break;
9472 #endif
9473 default:
9474 abort ();
9477 return x;
9480 static void
9481 decode_complex_addend (unsigned long *start, /* in bits */
9482 unsigned long *oplen, /* in bits */
9483 unsigned long *len, /* in bits */
9484 unsigned long *wordsz, /* in bytes */
9485 unsigned long *chunksz, /* in bytes */
9486 unsigned long *lsb0_p,
9487 unsigned long *signed_p,
9488 unsigned long *trunc_p,
9489 unsigned long encoded)
9491 * start = encoded & 0x3F;
9492 * len = (encoded >> 6) & 0x3F;
9493 * oplen = (encoded >> 12) & 0x3F;
9494 * wordsz = (encoded >> 18) & 0xF;
9495 * chunksz = (encoded >> 22) & 0xF;
9496 * lsb0_p = (encoded >> 27) & 1;
9497 * signed_p = (encoded >> 28) & 1;
9498 * trunc_p = (encoded >> 29) & 1;
9501 bfd_reloc_status_type
9502 bfd_elf_perform_complex_relocation (bfd *input_bfd,
9503 asection *input_section,
9504 bfd_byte *contents,
9505 Elf_Internal_Rela *rel,
9506 bfd_vma relocation)
9508 bfd_vma shift, x, mask;
9509 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
9510 bfd_reloc_status_type r;
9511 bfd_size_type octets;
9513 /* Perform this reloc, since it is complex.
9514 (this is not to say that it necessarily refers to a complex
9515 symbol; merely that it is a self-describing CGEN based reloc.
9516 i.e. the addend has the complete reloc information (bit start, end,
9517 word size, etc) encoded within it.). */
9519 decode_complex_addend (&start, &oplen, &len, &wordsz,
9520 &chunksz, &lsb0_p, &signed_p,
9521 &trunc_p, rel->r_addend);
9523 mask = (((1L << (len - 1)) - 1) << 1) | 1;
9525 if (lsb0_p)
9526 shift = (start + 1) - len;
9527 else
9528 shift = (8 * wordsz) - (start + len);
9530 octets = rel->r_offset * bfd_octets_per_byte (input_bfd, input_section);
9531 x = get_value (wordsz, chunksz, input_bfd, contents + octets);
9533 #ifdef DEBUG
9534 printf ("Doing complex reloc: "
9535 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
9536 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
9537 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
9538 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9539 oplen, (unsigned long) x, (unsigned long) mask,
9540 (unsigned long) relocation);
9541 #endif
9543 r = bfd_reloc_ok;
9544 if (! trunc_p)
9545 /* Now do an overflow check. */
9546 r = bfd_check_overflow ((signed_p
9547 ? complain_overflow_signed
9548 : complain_overflow_unsigned),
9549 len, 0, (8 * wordsz),
9550 relocation);
9552 /* Do the deed. */
9553 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
9555 #ifdef DEBUG
9556 printf (" relocation: %8.8lx\n"
9557 " shifted mask: %8.8lx\n"
9558 " shifted/masked reloc: %8.8lx\n"
9559 " result: %8.8lx\n",
9560 (unsigned long) relocation, (unsigned long) (mask << shift),
9561 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
9562 #endif
9563 put_value (wordsz, chunksz, input_bfd, x, contents + octets);
9564 return r;
9567 /* Functions to read r_offset from external (target order) reloc
9568 entry. Faster than bfd_getl32 et al, because we let the compiler
9569 know the value is aligned. */
9571 static bfd_vma
9572 ext32l_r_offset (const void *p)
9574 union aligned32
9576 uint32_t v;
9577 unsigned char c[4];
9579 const union aligned32 *a
9580 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9582 uint32_t aval = ( (uint32_t) a->c[0]
9583 | (uint32_t) a->c[1] << 8
9584 | (uint32_t) a->c[2] << 16
9585 | (uint32_t) a->c[3] << 24);
9586 return aval;
9589 static bfd_vma
9590 ext32b_r_offset (const void *p)
9592 union aligned32
9594 uint32_t v;
9595 unsigned char c[4];
9597 const union aligned32 *a
9598 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9600 uint32_t aval = ( (uint32_t) a->c[0] << 24
9601 | (uint32_t) a->c[1] << 16
9602 | (uint32_t) a->c[2] << 8
9603 | (uint32_t) a->c[3]);
9604 return aval;
9607 static bfd_vma
9608 ext64l_r_offset (const void *p)
9610 union aligned64
9612 uint64_t v;
9613 unsigned char c[8];
9615 const union aligned64 *a
9616 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9618 uint64_t aval = ( (uint64_t) a->c[0]
9619 | (uint64_t) a->c[1] << 8
9620 | (uint64_t) a->c[2] << 16
9621 | (uint64_t) a->c[3] << 24
9622 | (uint64_t) a->c[4] << 32
9623 | (uint64_t) a->c[5] << 40
9624 | (uint64_t) a->c[6] << 48
9625 | (uint64_t) a->c[7] << 56);
9626 return aval;
9629 static bfd_vma
9630 ext64b_r_offset (const void *p)
9632 union aligned64
9634 uint64_t v;
9635 unsigned char c[8];
9637 const union aligned64 *a
9638 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9640 uint64_t aval = ( (uint64_t) a->c[0] << 56
9641 | (uint64_t) a->c[1] << 48
9642 | (uint64_t) a->c[2] << 40
9643 | (uint64_t) a->c[3] << 32
9644 | (uint64_t) a->c[4] << 24
9645 | (uint64_t) a->c[5] << 16
9646 | (uint64_t) a->c[6] << 8
9647 | (uint64_t) a->c[7]);
9648 return aval;
9651 /* When performing a relocatable link, the input relocations are
9652 preserved. But, if they reference global symbols, the indices
9653 referenced must be updated. Update all the relocations found in
9654 RELDATA. */
9656 static bool
9657 elf_link_adjust_relocs (bfd *abfd,
9658 asection *sec,
9659 struct bfd_elf_section_reloc_data *reldata,
9660 bool sort,
9661 struct bfd_link_info *info)
9663 unsigned int i;
9664 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9665 bfd_byte *erela;
9666 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9667 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9668 bfd_vma r_type_mask;
9669 int r_sym_shift;
9670 unsigned int count = reldata->count;
9671 struct elf_link_hash_entry **rel_hash = reldata->hashes;
9673 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
9675 swap_in = bed->s->swap_reloc_in;
9676 swap_out = bed->s->swap_reloc_out;
9678 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
9680 swap_in = bed->s->swap_reloca_in;
9681 swap_out = bed->s->swap_reloca_out;
9683 else
9684 abort ();
9686 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
9687 abort ();
9689 if (bed->s->arch_size == 32)
9691 r_type_mask = 0xff;
9692 r_sym_shift = 8;
9694 else
9696 r_type_mask = 0xffffffff;
9697 r_sym_shift = 32;
9700 erela = reldata->hdr->contents;
9701 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
9703 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
9704 unsigned int j;
9706 if (*rel_hash == NULL)
9707 continue;
9709 if ((*rel_hash)->indx == -2
9710 && info->gc_sections
9711 && ! info->gc_keep_exported)
9713 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9714 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
9715 abfd, sec,
9716 (*rel_hash)->root.root.string);
9717 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
9718 abfd, sec);
9719 bfd_set_error (bfd_error_invalid_operation);
9720 return false;
9722 BFD_ASSERT ((*rel_hash)->indx >= 0);
9724 (*swap_in) (abfd, erela, irela);
9725 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
9726 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
9727 | (irela[j].r_info & r_type_mask));
9728 (*swap_out) (abfd, irela, erela);
9731 if (bed->elf_backend_update_relocs)
9732 (*bed->elf_backend_update_relocs) (sec, reldata);
9734 if (sort && count != 0)
9736 bfd_vma (*ext_r_off) (const void *);
9737 bfd_vma r_off;
9738 size_t elt_size;
9739 bfd_byte *base, *end, *p, *loc;
9740 bfd_byte *buf = NULL;
9742 if (bed->s->arch_size == 32)
9744 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9745 ext_r_off = ext32l_r_offset;
9746 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9747 ext_r_off = ext32b_r_offset;
9748 else
9749 abort ();
9751 else
9753 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9754 ext_r_off = ext64l_r_offset;
9755 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9756 ext_r_off = ext64b_r_offset;
9757 else
9758 abort ();
9761 /* Must use a stable sort here. A modified insertion sort,
9762 since the relocs are mostly sorted already. */
9763 elt_size = reldata->hdr->sh_entsize;
9764 base = reldata->hdr->contents;
9765 end = base + count * elt_size;
9766 if (elt_size > sizeof (Elf64_External_Rela))
9767 abort ();
9769 /* Ensure the first element is lowest. This acts as a sentinel,
9770 speeding the main loop below. */
9771 r_off = (*ext_r_off) (base);
9772 for (p = loc = base; (p += elt_size) < end; )
9774 bfd_vma r_off2 = (*ext_r_off) (p);
9775 if (r_off > r_off2)
9777 r_off = r_off2;
9778 loc = p;
9781 if (loc != base)
9783 /* Don't just swap *base and *loc as that changes the order
9784 of the original base[0] and base[1] if they happen to
9785 have the same r_offset. */
9786 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
9787 memcpy (onebuf, loc, elt_size);
9788 memmove (base + elt_size, base, loc - base);
9789 memcpy (base, onebuf, elt_size);
9792 for (p = base + elt_size; (p += elt_size) < end; )
9794 /* base to p is sorted, *p is next to insert. */
9795 r_off = (*ext_r_off) (p);
9796 /* Search the sorted region for location to insert. */
9797 loc = p - elt_size;
9798 while (r_off < (*ext_r_off) (loc))
9799 loc -= elt_size;
9800 loc += elt_size;
9801 if (loc != p)
9803 /* Chances are there is a run of relocs to insert here,
9804 from one of more input files. Files are not always
9805 linked in order due to the way elf_link_input_bfd is
9806 called. See pr17666. */
9807 size_t sortlen = p - loc;
9808 bfd_vma r_off2 = (*ext_r_off) (loc);
9809 size_t runlen = elt_size;
9810 bfd_vma r_off_runend = r_off;
9811 bfd_vma r_off_runend_next;
9812 size_t buf_size = 96 * 1024;
9813 while (p + runlen < end
9814 && (sortlen <= buf_size
9815 || runlen + elt_size <= buf_size)
9816 /* run must not break the ordering of base..loc+1 */
9817 && r_off2 > (r_off_runend_next = (*ext_r_off) (p + runlen))
9818 /* run must be already sorted */
9819 && r_off_runend_next >= r_off_runend)
9821 runlen += elt_size;
9822 r_off_runend = r_off_runend_next;
9824 if (buf == NULL)
9826 buf = bfd_malloc (buf_size);
9827 if (buf == NULL)
9828 return false;
9830 if (runlen < sortlen)
9832 memcpy (buf, p, runlen);
9833 memmove (loc + runlen, loc, sortlen);
9834 memcpy (loc, buf, runlen);
9836 else
9838 memcpy (buf, loc, sortlen);
9839 memmove (loc, p, runlen);
9840 memcpy (loc + runlen, buf, sortlen);
9842 p += runlen - elt_size;
9845 /* Hashes are no longer valid. */
9846 free (reldata->hashes);
9847 reldata->hashes = NULL;
9848 free (buf);
9850 return true;
9853 struct elf_link_sort_rela
9855 union {
9856 bfd_vma offset;
9857 bfd_vma sym_mask;
9858 } u;
9859 enum elf_reloc_type_class type;
9860 /* We use this as an array of size int_rels_per_ext_rel. */
9861 Elf_Internal_Rela rela[1];
9864 /* qsort stability here and for cmp2 is only an issue if multiple
9865 dynamic relocations are emitted at the same address. But targets
9866 that apply a series of dynamic relocations each operating on the
9867 result of the prior relocation can't use -z combreloc as
9868 implemented anyway. Such schemes tend to be broken by sorting on
9869 symbol index. That leaves dynamic NONE relocs as the only other
9870 case where ld might emit multiple relocs at the same address, and
9871 those are only emitted due to target bugs. */
9873 static int
9874 elf_link_sort_cmp1 (const void *A, const void *B)
9876 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9877 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9878 int relativea, relativeb;
9880 relativea = a->type == reloc_class_relative;
9881 relativeb = b->type == reloc_class_relative;
9883 if (relativea < relativeb)
9884 return 1;
9885 if (relativea > relativeb)
9886 return -1;
9887 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
9888 return -1;
9889 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
9890 return 1;
9891 if (a->rela->r_offset < b->rela->r_offset)
9892 return -1;
9893 if (a->rela->r_offset > b->rela->r_offset)
9894 return 1;
9895 return 0;
9898 static int
9899 elf_link_sort_cmp2 (const void *A, const void *B)
9901 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9902 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9904 if (a->type < b->type)
9905 return -1;
9906 if (a->type > b->type)
9907 return 1;
9908 if (a->u.offset < b->u.offset)
9909 return -1;
9910 if (a->u.offset > b->u.offset)
9911 return 1;
9912 if (a->rela->r_offset < b->rela->r_offset)
9913 return -1;
9914 if (a->rela->r_offset > b->rela->r_offset)
9915 return 1;
9916 return 0;
9919 static size_t
9920 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9922 asection *dynamic_relocs;
9923 asection *rela_dyn;
9924 asection *rel_dyn;
9925 bfd_size_type count, size;
9926 size_t i, ret, sort_elt, ext_size;
9927 bfd_byte *sort, *s_non_relative, *p;
9928 struct elf_link_sort_rela *sq;
9929 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9930 int i2e = bed->s->int_rels_per_ext_rel;
9931 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
9932 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9933 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9934 struct bfd_link_order *lo;
9935 bfd_vma r_sym_mask;
9936 bool use_rela;
9938 /* Find a dynamic reloc section. */
9939 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9940 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
9941 if (rela_dyn != NULL && rela_dyn->size > 0
9942 && rel_dyn != NULL && rel_dyn->size > 0)
9944 bool use_rela_initialised = false;
9946 /* This is just here to stop gcc from complaining.
9947 Its initialization checking code is not perfect. */
9948 use_rela = true;
9950 /* Both sections are present. Examine the sizes
9951 of the indirect sections to help us choose. */
9952 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9953 if (lo->type == bfd_indirect_link_order)
9955 asection *o = lo->u.indirect.section;
9957 if ((o->size % bed->s->sizeof_rela) == 0)
9959 if ((o->size % bed->s->sizeof_rel) == 0)
9960 /* Section size is divisible by both rel and rela sizes.
9961 It is of no help to us. */
9963 else
9965 /* Section size is only divisible by rela. */
9966 if (use_rela_initialised && !use_rela)
9968 _bfd_error_handler (_("%pB: unable to sort relocs - "
9969 "they are in more than one size"),
9970 abfd);
9971 bfd_set_error (bfd_error_invalid_operation);
9972 return 0;
9974 else
9976 use_rela = true;
9977 use_rela_initialised = true;
9981 else if ((o->size % bed->s->sizeof_rel) == 0)
9983 /* Section size is only divisible by rel. */
9984 if (use_rela_initialised && use_rela)
9986 _bfd_error_handler (_("%pB: unable to sort relocs - "
9987 "they are in more than one size"),
9988 abfd);
9989 bfd_set_error (bfd_error_invalid_operation);
9990 return 0;
9992 else
9994 use_rela = false;
9995 use_rela_initialised = true;
9998 else
10000 /* The section size is not divisible by either -
10001 something is wrong. */
10002 _bfd_error_handler (_("%pB: unable to sort relocs - "
10003 "they are of an unknown size"), abfd);
10004 bfd_set_error (bfd_error_invalid_operation);
10005 return 0;
10009 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
10010 if (lo->type == bfd_indirect_link_order)
10012 asection *o = lo->u.indirect.section;
10014 if ((o->size % bed->s->sizeof_rela) == 0)
10016 if ((o->size % bed->s->sizeof_rel) == 0)
10017 /* Section size is divisible by both rel and rela sizes.
10018 It is of no help to us. */
10020 else
10022 /* Section size is only divisible by rela. */
10023 if (use_rela_initialised && !use_rela)
10025 _bfd_error_handler (_("%pB: unable to sort relocs - "
10026 "they are in more than one size"),
10027 abfd);
10028 bfd_set_error (bfd_error_invalid_operation);
10029 return 0;
10031 else
10033 use_rela = true;
10034 use_rela_initialised = true;
10038 else if ((o->size % bed->s->sizeof_rel) == 0)
10040 /* Section size is only divisible by rel. */
10041 if (use_rela_initialised && use_rela)
10043 _bfd_error_handler (_("%pB: unable to sort relocs - "
10044 "they are in more than one size"),
10045 abfd);
10046 bfd_set_error (bfd_error_invalid_operation);
10047 return 0;
10049 else
10051 use_rela = false;
10052 use_rela_initialised = true;
10055 else
10057 /* The section size is not divisible by either -
10058 something is wrong. */
10059 _bfd_error_handler (_("%pB: unable to sort relocs - "
10060 "they are of an unknown size"), abfd);
10061 bfd_set_error (bfd_error_invalid_operation);
10062 return 0;
10066 if (! use_rela_initialised)
10067 /* Make a guess. */
10068 use_rela = true;
10070 else if (rela_dyn != NULL && rela_dyn->size > 0)
10071 use_rela = true;
10072 else if (rel_dyn != NULL && rel_dyn->size > 0)
10073 use_rela = false;
10074 else
10075 return 0;
10077 if (use_rela)
10079 dynamic_relocs = rela_dyn;
10080 ext_size = bed->s->sizeof_rela;
10081 swap_in = bed->s->swap_reloca_in;
10082 swap_out = bed->s->swap_reloca_out;
10084 else
10086 dynamic_relocs = rel_dyn;
10087 ext_size = bed->s->sizeof_rel;
10088 swap_in = bed->s->swap_reloc_in;
10089 swap_out = bed->s->swap_reloc_out;
10092 size = 0;
10093 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
10094 if (lo->type == bfd_indirect_link_order)
10095 size += lo->u.indirect.section->size;
10097 if (size != dynamic_relocs->size)
10098 return 0;
10100 sort_elt = (sizeof (struct elf_link_sort_rela)
10101 + (i2e - 1) * sizeof (Elf_Internal_Rela));
10103 count = dynamic_relocs->size / ext_size;
10104 if (count == 0)
10105 return 0;
10106 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
10108 if (sort == NULL)
10110 (*info->callbacks->warning)
10111 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
10112 return 0;
10115 if (bed->s->arch_size == 32)
10116 r_sym_mask = ~(bfd_vma) 0xff;
10117 else
10118 r_sym_mask = ~(bfd_vma) 0xffffffff;
10120 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
10121 if (lo->type == bfd_indirect_link_order)
10123 bfd_byte *erel, *erelend;
10124 asection *o = lo->u.indirect.section;
10126 if (o->contents == NULL && o->size != 0)
10128 /* This is a reloc section that is being handled as a normal
10129 section. See bfd_section_from_shdr. We can't combine
10130 relocs in this case. */
10131 free (sort);
10132 return 0;
10134 erel = o->contents;
10135 erelend = o->contents + o->size;
10136 p = sort + o->output_offset * opb / ext_size * sort_elt;
10138 while (erel < erelend)
10140 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
10142 (*swap_in) (abfd, erel, s->rela);
10143 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
10144 s->u.sym_mask = r_sym_mask;
10145 p += sort_elt;
10146 erel += ext_size;
10150 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
10152 for (i = 0, p = sort; i < count; i++, p += sort_elt)
10154 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
10155 if (s->type != reloc_class_relative)
10156 break;
10158 ret = i;
10159 s_non_relative = p;
10161 sq = (struct elf_link_sort_rela *) s_non_relative;
10162 for (; i < count; i++, p += sort_elt)
10164 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
10165 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
10166 sq = sp;
10167 sp->u.offset = sq->rela->r_offset;
10170 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
10172 struct elf_link_hash_table *htab = elf_hash_table (info);
10173 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
10175 /* We have plt relocs in .rela.dyn. */
10176 sq = (struct elf_link_sort_rela *) sort;
10177 for (i = 0; i < count; i++)
10178 if (sq[count - i - 1].type != reloc_class_plt)
10179 break;
10180 if (i != 0 && htab->srelplt->size == i * ext_size)
10182 struct bfd_link_order **plo;
10183 /* Put srelplt link_order last. This is so the output_offset
10184 set in the next loop is correct for DT_JMPREL. */
10185 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
10186 if ((*plo)->type == bfd_indirect_link_order
10187 && (*plo)->u.indirect.section == htab->srelplt)
10189 lo = *plo;
10190 *plo = lo->next;
10192 else
10193 plo = &(*plo)->next;
10194 *plo = lo;
10195 lo->next = NULL;
10196 dynamic_relocs->map_tail.link_order = lo;
10200 p = sort;
10201 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
10202 if (lo->type == bfd_indirect_link_order)
10204 bfd_byte *erel, *erelend;
10205 asection *o = lo->u.indirect.section;
10207 erel = o->contents;
10208 erelend = o->contents + o->size;
10209 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
10210 while (erel < erelend)
10212 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
10213 (*swap_out) (abfd, s->rela, erel);
10214 p += sort_elt;
10215 erel += ext_size;
10219 free (sort);
10220 *psec = dynamic_relocs;
10221 return ret;
10224 /* Add a symbol to the output symbol string table. */
10226 static int
10227 elf_link_output_symstrtab (void *finf,
10228 const char *name,
10229 Elf_Internal_Sym *elfsym,
10230 asection *input_sec,
10231 struct elf_link_hash_entry *h)
10233 struct elf_final_link_info *flinfo = finf;
10234 int (*output_symbol_hook)
10235 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
10236 struct elf_link_hash_entry *);
10237 struct elf_link_hash_table *hash_table;
10238 const struct elf_backend_data *bed;
10239 bfd_size_type strtabsize;
10241 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
10243 bed = get_elf_backend_data (flinfo->output_bfd);
10244 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
10245 if (output_symbol_hook != NULL)
10247 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
10248 if (ret != 1)
10249 return ret;
10252 if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC)
10253 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
10254 if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE)
10255 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique;
10257 if (name == NULL || *name == '\0')
10258 elfsym->st_name = (unsigned long) -1;
10259 else
10261 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
10262 to get the final offset for st_name. */
10263 char *versioned_name = (char *) name;
10264 if (h != NULL)
10266 if (h->versioned == versioned && h->def_dynamic)
10268 /* Keep only one '@' for versioned symbols defined in
10269 shared objects. */
10270 char *version = strrchr (name, ELF_VER_CHR);
10271 char *base_end = strchr (name, ELF_VER_CHR);
10272 if (version != base_end)
10274 size_t base_len;
10275 size_t len = strlen (name);
10276 versioned_name = bfd_alloc (flinfo->output_bfd, len);
10277 if (versioned_name == NULL)
10278 return 0;
10279 base_len = base_end - name;
10280 memcpy (versioned_name, name, base_len);
10281 memcpy (versioned_name + base_len, version,
10282 len - base_len);
10286 else if (flinfo->info->unique_symbol
10287 && ELF_ST_BIND (elfsym->st_info) == STB_LOCAL)
10289 struct local_hash_entry *lh;
10290 size_t count_len;
10291 size_t base_len;
10292 char buf[30];
10293 switch (ELF_ST_TYPE (elfsym->st_info))
10295 case STT_FILE:
10296 case STT_SECTION:
10297 break;
10298 default:
10299 lh = (struct local_hash_entry *) bfd_hash_lookup
10300 (&flinfo->local_hash_table, name, true, false);
10301 if (lh == NULL)
10302 return 0;
10303 /* Always append ".COUNT" to local symbols to avoid
10304 potential conflicts with local symbol "XXX.COUNT". */
10305 sprintf (buf, "%lx", lh->count);
10306 base_len = lh->size;
10307 if (!base_len)
10309 base_len = strlen (name);
10310 lh->size = base_len;
10312 count_len = strlen (buf);
10313 versioned_name = bfd_alloc (flinfo->output_bfd,
10314 base_len + count_len + 2);
10315 if (versioned_name == NULL)
10316 return 0;
10317 memcpy (versioned_name, name, base_len);
10318 versioned_name[base_len] = '.';
10319 memcpy (versioned_name + base_len + 1, buf,
10320 count_len + 1);
10321 lh->count++;
10322 break;
10325 elfsym->st_name
10326 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
10327 versioned_name, false);
10328 if (elfsym->st_name == (unsigned long) -1)
10329 return 0;
10332 hash_table = elf_hash_table (flinfo->info);
10333 strtabsize = hash_table->strtabsize;
10334 if (strtabsize <= flinfo->output_bfd->symcount)
10336 strtabsize += strtabsize;
10337 hash_table->strtabsize = strtabsize;
10338 strtabsize *= sizeof (*hash_table->strtab);
10339 hash_table->strtab
10340 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
10341 strtabsize);
10342 if (hash_table->strtab == NULL)
10343 return 0;
10345 hash_table->strtab[flinfo->output_bfd->symcount].sym = *elfsym;
10346 hash_table->strtab[flinfo->output_bfd->symcount].dest_index
10347 = flinfo->output_bfd->symcount;
10348 flinfo->output_bfd->symcount += 1;
10350 return 1;
10353 /* Swap symbols out to the symbol table and flush the output symbols to
10354 the file. */
10356 static bool
10357 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
10359 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
10360 size_t amt;
10361 size_t i;
10362 const struct elf_backend_data *bed;
10363 bfd_byte *symbuf;
10364 Elf_Internal_Shdr *hdr;
10365 file_ptr pos;
10366 bool ret;
10368 if (flinfo->output_bfd->symcount == 0)
10369 return true;
10371 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
10373 bed = get_elf_backend_data (flinfo->output_bfd);
10375 amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount;
10376 symbuf = (bfd_byte *) bfd_malloc (amt);
10377 if (symbuf == NULL)
10378 return false;
10380 if (flinfo->symshndxbuf)
10382 amt = sizeof (Elf_External_Sym_Shndx);
10383 amt *= bfd_get_symcount (flinfo->output_bfd);
10384 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
10385 if (flinfo->symshndxbuf == NULL)
10387 free (symbuf);
10388 return false;
10392 /* Now swap out the symbols. */
10393 for (i = 0; i < flinfo->output_bfd->symcount; i++)
10395 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
10396 if (elfsym->sym.st_name == (unsigned long) -1)
10397 elfsym->sym.st_name = 0;
10398 else
10399 elfsym->sym.st_name
10400 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
10401 elfsym->sym.st_name);
10403 /* Inform the linker of the addition of this symbol. */
10405 if (flinfo->info->callbacks->ctf_new_symbol)
10406 flinfo->info->callbacks->ctf_new_symbol (elfsym->dest_index,
10407 &elfsym->sym);
10409 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
10410 ((bfd_byte *) symbuf
10411 + (elfsym->dest_index
10412 * bed->s->sizeof_sym)),
10413 NPTR_ADD (flinfo->symshndxbuf,
10414 elfsym->dest_index));
10417 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
10418 pos = hdr->sh_offset + hdr->sh_size;
10419 amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount;
10420 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
10421 && bfd_write (symbuf, amt, flinfo->output_bfd) == amt)
10423 hdr->sh_size += amt;
10424 ret = true;
10426 else
10427 ret = false;
10429 free (symbuf);
10431 free (hash_table->strtab);
10432 hash_table->strtab = NULL;
10434 return ret;
10437 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
10439 static bool
10440 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
10442 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
10443 && sym->st_shndx < SHN_LORESERVE)
10445 /* The gABI doesn't support dynamic symbols in output sections
10446 beyond 64k. */
10447 _bfd_error_handler
10448 /* xgettext:c-format */
10449 (_("%pB: too many sections: %d (>= %d)"),
10450 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
10451 bfd_set_error (bfd_error_nonrepresentable_section);
10452 return false;
10454 return true;
10457 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
10458 allowing an unsatisfied unversioned symbol in the DSO to match a
10459 versioned symbol that would normally require an explicit version.
10460 We also handle the case that a DSO references a hidden symbol
10461 which may be satisfied by a versioned symbol in another DSO. */
10463 static bool
10464 elf_link_check_versioned_symbol (struct bfd_link_info *info,
10465 const struct elf_backend_data *bed,
10466 struct elf_link_hash_entry *h)
10468 bfd *abfd;
10469 struct elf_link_loaded_list *loaded;
10471 if (!is_elf_hash_table (info->hash))
10472 return false;
10474 /* Check indirect symbol. */
10475 while (h->root.type == bfd_link_hash_indirect)
10476 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10478 switch (h->root.type)
10480 default:
10481 abfd = NULL;
10482 break;
10484 case bfd_link_hash_undefined:
10485 case bfd_link_hash_undefweak:
10486 abfd = h->root.u.undef.abfd;
10487 if (abfd == NULL
10488 || (abfd->flags & DYNAMIC) == 0
10489 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
10490 return false;
10491 break;
10493 case bfd_link_hash_defined:
10494 case bfd_link_hash_defweak:
10495 abfd = h->root.u.def.section->owner;
10496 break;
10498 case bfd_link_hash_common:
10499 abfd = h->root.u.c.p->section->owner;
10500 break;
10502 BFD_ASSERT (abfd != NULL);
10504 for (loaded = elf_hash_table (info)->dyn_loaded;
10505 loaded != NULL;
10506 loaded = loaded->next)
10508 bfd *input;
10509 Elf_Internal_Shdr *hdr;
10510 size_t symcount;
10511 size_t extsymcount;
10512 size_t extsymoff;
10513 Elf_Internal_Shdr *versymhdr;
10514 Elf_Internal_Sym *isym;
10515 Elf_Internal_Sym *isymend;
10516 Elf_Internal_Sym *isymbuf;
10517 Elf_External_Versym *ever;
10518 Elf_External_Versym *extversym;
10520 input = loaded->abfd;
10522 /* We check each DSO for a possible hidden versioned definition. */
10523 if (input == abfd
10524 || elf_dynversym (input) == 0)
10525 continue;
10527 hdr = &elf_tdata (input)->dynsymtab_hdr;
10529 symcount = hdr->sh_size / bed->s->sizeof_sym;
10530 if (elf_bad_symtab (input))
10532 extsymcount = symcount;
10533 extsymoff = 0;
10535 else
10537 extsymcount = symcount - hdr->sh_info;
10538 extsymoff = hdr->sh_info;
10541 if (extsymcount == 0)
10542 continue;
10544 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
10545 NULL, NULL, NULL);
10546 if (isymbuf == NULL)
10547 return false;
10549 /* Read in any version definitions. */
10550 versymhdr = &elf_tdata (input)->dynversym_hdr;
10551 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
10552 || (extversym = (Elf_External_Versym *)
10553 _bfd_malloc_and_read (input, versymhdr->sh_size,
10554 versymhdr->sh_size)) == NULL)
10556 free (isymbuf);
10557 return false;
10560 ever = extversym + extsymoff;
10561 isymend = isymbuf + extsymcount;
10562 for (isym = isymbuf; isym < isymend; isym++, ever++)
10564 const char *name;
10565 Elf_Internal_Versym iver;
10566 unsigned short version_index;
10568 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
10569 || isym->st_shndx == SHN_UNDEF)
10570 continue;
10572 name = bfd_elf_string_from_elf_section (input,
10573 hdr->sh_link,
10574 isym->st_name);
10575 if (strcmp (name, h->root.root.string) != 0)
10576 continue;
10578 _bfd_elf_swap_versym_in (input, ever, &iver);
10580 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
10581 && !(h->def_regular
10582 && h->forced_local))
10584 /* If we have a non-hidden versioned sym, then it should
10585 have provided a definition for the undefined sym unless
10586 it is defined in a non-shared object and forced local.
10588 abort ();
10591 version_index = iver.vs_vers & VERSYM_VERSION;
10592 if (version_index == 1 || version_index == 2)
10594 /* This is the base or first version. We can use it. */
10595 free (extversym);
10596 free (isymbuf);
10597 return true;
10601 free (extversym);
10602 free (isymbuf);
10605 return false;
10608 /* Convert ELF common symbol TYPE. */
10610 static int
10611 elf_link_convert_common_type (struct bfd_link_info *info, int type)
10613 /* Commom symbol can only appear in relocatable link. */
10614 if (!bfd_link_relocatable (info))
10615 abort ();
10616 switch (info->elf_stt_common)
10618 case unchanged:
10619 break;
10620 case elf_stt_common:
10621 type = STT_COMMON;
10622 break;
10623 case no_elf_stt_common:
10624 type = STT_OBJECT;
10625 break;
10627 return type;
10630 /* Add an external symbol to the symbol table. This is called from
10631 the hash table traversal routine. When generating a shared object,
10632 we go through the symbol table twice. The first time we output
10633 anything that might have been forced to local scope in a version
10634 script. The second time we output the symbols that are still
10635 global symbols. */
10637 static bool
10638 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
10640 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
10641 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
10642 struct elf_final_link_info *flinfo = eoinfo->flinfo;
10643 bool strip;
10644 Elf_Internal_Sym sym;
10645 asection *input_sec;
10646 const struct elf_backend_data *bed;
10647 long indx;
10648 int ret;
10649 unsigned int type;
10651 if (h->root.type == bfd_link_hash_warning)
10653 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10654 if (h->root.type == bfd_link_hash_new)
10655 return true;
10658 /* Decide whether to output this symbol in this pass. */
10659 if (eoinfo->localsyms)
10661 if (!h->forced_local)
10662 return true;
10664 else
10666 if (h->forced_local)
10667 return true;
10670 bed = get_elf_backend_data (flinfo->output_bfd);
10672 if (h->root.type == bfd_link_hash_undefined)
10674 /* If we have an undefined symbol reference here then it must have
10675 come from a shared library that is being linked in. (Undefined
10676 references in regular files have already been handled unless
10677 they are in unreferenced sections which are removed by garbage
10678 collection). */
10679 bool ignore_undef = false;
10681 /* Some symbols may be special in that the fact that they're
10682 undefined can be safely ignored - let backend determine that. */
10683 if (bed->elf_backend_ignore_undef_symbol)
10684 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
10686 /* If we are reporting errors for this situation then do so now. */
10687 if (!ignore_undef
10688 && h->ref_dynamic_nonweak
10689 && (!h->ref_regular || flinfo->info->gc_sections)
10690 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
10691 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
10693 flinfo->info->callbacks->undefined_symbol
10694 (flinfo->info, h->root.root.string,
10695 h->ref_regular ? NULL : h->root.u.undef.abfd, NULL, 0,
10696 flinfo->info->unresolved_syms_in_shared_libs == RM_DIAGNOSE
10697 && !flinfo->info->warn_unresolved_syms);
10700 /* Strip a global symbol defined in a discarded section. */
10701 if (h->indx == -3)
10702 return true;
10705 /* We should also warn if a forced local symbol is referenced from
10706 shared libraries. */
10707 if (bfd_link_executable (flinfo->info)
10708 && h->forced_local
10709 && h->ref_dynamic
10710 && h->def_regular
10711 && !h->dynamic_def
10712 && h->ref_dynamic_nonweak
10713 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
10715 bfd *def_bfd;
10716 const char *msg;
10717 struct elf_link_hash_entry *hi = h;
10719 /* Check indirect symbol. */
10720 while (hi->root.type == bfd_link_hash_indirect)
10721 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
10723 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
10724 /* xgettext:c-format */
10725 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
10726 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
10727 /* xgettext:c-format */
10728 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
10729 else
10730 /* xgettext:c-format */
10731 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
10732 def_bfd = flinfo->output_bfd;
10733 if (hi->root.u.def.section != bfd_abs_section_ptr)
10734 def_bfd = hi->root.u.def.section->owner;
10735 _bfd_error_handler (msg, flinfo->output_bfd,
10736 h->root.root.string, def_bfd);
10737 bfd_set_error (bfd_error_bad_value);
10738 eoinfo->failed = true;
10739 return false;
10742 /* We don't want to output symbols that have never been mentioned by
10743 a regular file, or that we have been told to strip. However, if
10744 h->indx is set to -2, the symbol is used by a reloc and we must
10745 output it. */
10746 strip = false;
10747 if (h->indx == -2)
10749 else if ((h->def_dynamic
10750 || h->ref_dynamic
10751 || h->root.type == bfd_link_hash_new)
10752 && !h->def_regular
10753 && !h->ref_regular)
10754 strip = true;
10755 else if (flinfo->info->strip == strip_all)
10756 strip = true;
10757 else if (flinfo->info->strip == strip_some
10758 && bfd_hash_lookup (flinfo->info->keep_hash,
10759 h->root.root.string, false, false) == NULL)
10760 strip = true;
10761 else if ((h->root.type == bfd_link_hash_defined
10762 || h->root.type == bfd_link_hash_defweak)
10763 && ((flinfo->info->strip_discarded
10764 && discarded_section (h->root.u.def.section))
10765 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
10766 && h->root.u.def.section->owner != NULL
10767 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
10768 strip = true;
10769 else if ((h->root.type == bfd_link_hash_undefined
10770 || h->root.type == bfd_link_hash_undefweak)
10771 && h->root.u.undef.abfd != NULL
10772 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
10773 strip = true;
10775 /* Remember if this symbol should be stripped. */
10776 bool should_strip = strip;
10778 /* Strip undefined weak symbols link if they don't have relocation. */
10779 if (!strip)
10780 strip = !h->has_reloc && h->root.type == bfd_link_hash_undefweak;
10782 type = h->type;
10784 /* If we're stripping it, and it's not a dynamic symbol, there's
10785 nothing else to do. However, if it is a forced local symbol or
10786 an ifunc symbol we need to give the backend finish_dynamic_symbol
10787 function a chance to make it dynamic. */
10788 if (strip
10789 && h->dynindx == -1
10790 && type != STT_GNU_IFUNC
10791 && !h->forced_local)
10792 return true;
10794 sym.st_value = 0;
10795 sym.st_size = h->size;
10796 sym.st_other = h->other;
10797 switch (h->root.type)
10799 default:
10800 case bfd_link_hash_new:
10801 case bfd_link_hash_warning:
10802 abort ();
10803 return false;
10805 case bfd_link_hash_undefined:
10806 case bfd_link_hash_undefweak:
10807 input_sec = bfd_und_section_ptr;
10808 sym.st_shndx = SHN_UNDEF;
10809 break;
10811 case bfd_link_hash_defined:
10812 case bfd_link_hash_defweak:
10814 input_sec = h->root.u.def.section;
10815 if (input_sec->output_section != NULL)
10817 sym.st_shndx =
10818 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
10819 input_sec->output_section);
10820 if (sym.st_shndx == SHN_BAD)
10822 _bfd_error_handler
10823 /* xgettext:c-format */
10824 (_("%pB: could not find output section %pA for input section %pA"),
10825 flinfo->output_bfd, input_sec->output_section, input_sec);
10826 bfd_set_error (bfd_error_nonrepresentable_section);
10827 eoinfo->failed = true;
10828 return false;
10831 /* ELF symbols in relocatable files are section relative,
10832 but in nonrelocatable files they are virtual
10833 addresses. */
10834 sym.st_value = h->root.u.def.value + input_sec->output_offset;
10835 if (!bfd_link_relocatable (flinfo->info))
10837 sym.st_value += input_sec->output_section->vma;
10838 if (h->type == STT_TLS)
10840 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
10841 if (tls_sec != NULL)
10842 sym.st_value -= tls_sec->vma;
10846 else
10848 BFD_ASSERT (input_sec->owner == NULL
10849 || (input_sec->owner->flags & DYNAMIC) != 0);
10850 sym.st_shndx = SHN_UNDEF;
10851 input_sec = bfd_und_section_ptr;
10854 break;
10856 case bfd_link_hash_common:
10857 input_sec = h->root.u.c.p->section;
10858 sym.st_shndx = bed->common_section_index (input_sec);
10859 sym.st_value = 1 << h->root.u.c.p->alignment_power;
10860 break;
10862 case bfd_link_hash_indirect:
10863 /* These symbols are created by symbol versioning. They point
10864 to the decorated version of the name. For example, if the
10865 symbol foo@@GNU_1.2 is the default, which should be used when
10866 foo is used with no version, then we add an indirect symbol
10867 foo which points to foo@@GNU_1.2. We ignore these symbols,
10868 since the indirected symbol is already in the hash table. */
10869 return true;
10872 if (type == STT_COMMON || type == STT_OBJECT)
10873 switch (h->root.type)
10875 case bfd_link_hash_common:
10876 type = elf_link_convert_common_type (flinfo->info, type);
10877 break;
10878 case bfd_link_hash_defined:
10879 case bfd_link_hash_defweak:
10880 if (bed->common_definition (&sym))
10881 type = elf_link_convert_common_type (flinfo->info, type);
10882 else
10883 type = STT_OBJECT;
10884 break;
10885 case bfd_link_hash_undefined:
10886 case bfd_link_hash_undefweak:
10887 break;
10888 default:
10889 abort ();
10892 if (h->forced_local)
10894 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
10895 /* Turn off visibility on local symbol. */
10896 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
10898 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10899 else if (h->unique_global && h->def_regular)
10900 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
10901 else if (h->root.type == bfd_link_hash_undefweak
10902 || h->root.type == bfd_link_hash_defweak)
10903 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
10904 else
10905 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
10906 sym.st_target_internal = h->target_internal;
10908 /* Give the processor backend a chance to tweak the symbol value,
10909 and also to finish up anything that needs to be done for this
10910 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
10911 forced local syms when non-shared is due to a historical quirk.
10912 STT_GNU_IFUNC symbol must go through PLT. */
10913 if ((h->type == STT_GNU_IFUNC
10914 && h->def_regular
10915 && !bfd_link_relocatable (flinfo->info))
10916 || ((h->dynindx != -1
10917 || h->forced_local)
10918 && ((bfd_link_pic (flinfo->info)
10919 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
10920 || h->root.type != bfd_link_hash_undefweak))
10921 || !h->forced_local)
10922 && elf_hash_table (flinfo->info)->dynamic_sections_created))
10924 if (! ((*bed->elf_backend_finish_dynamic_symbol)
10925 (flinfo->output_bfd, flinfo->info, h, &sym)))
10927 eoinfo->failed = true;
10928 return false;
10930 /* If a symbol is in the dynamic symbol table and isn't a
10931 should-strip symbol, also keep it in the symbol table. */
10932 if (!should_strip)
10933 strip = false;
10936 /* If we are marking the symbol as undefined, and there are no
10937 non-weak references to this symbol from a regular object, then
10938 mark the symbol as weak undefined; if there are non-weak
10939 references, mark the symbol as strong. We can't do this earlier,
10940 because it might not be marked as undefined until the
10941 finish_dynamic_symbol routine gets through with it. */
10942 if (sym.st_shndx == SHN_UNDEF
10943 && h->ref_regular
10944 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
10945 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
10947 int bindtype;
10948 type = ELF_ST_TYPE (sym.st_info);
10950 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10951 if (type == STT_GNU_IFUNC)
10952 type = STT_FUNC;
10954 if (h->ref_regular_nonweak)
10955 bindtype = STB_GLOBAL;
10956 else
10957 bindtype = STB_WEAK;
10958 sym.st_info = ELF_ST_INFO (bindtype, type);
10961 /* If this is a symbol defined in a dynamic library, don't use the
10962 symbol size from the dynamic library. Relinking an executable
10963 against a new library may introduce gratuitous changes in the
10964 executable's symbols if we keep the size. */
10965 if (sym.st_shndx == SHN_UNDEF
10966 && !h->def_regular
10967 && h->def_dynamic)
10968 sym.st_size = 0;
10970 /* If a non-weak symbol with non-default visibility is not defined
10971 locally, it is a fatal error. */
10972 if (!bfd_link_relocatable (flinfo->info)
10973 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
10974 && ELF_ST_BIND (sym.st_info) != STB_WEAK
10975 && h->root.type == bfd_link_hash_undefined
10976 && !h->def_regular)
10978 const char *msg;
10980 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
10981 /* xgettext:c-format */
10982 msg = _("%pB: protected symbol `%s' isn't defined");
10983 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
10984 /* xgettext:c-format */
10985 msg = _("%pB: internal symbol `%s' isn't defined");
10986 else
10987 /* xgettext:c-format */
10988 msg = _("%pB: hidden symbol `%s' isn't defined");
10989 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
10990 bfd_set_error (bfd_error_bad_value);
10991 eoinfo->failed = true;
10992 return false;
10995 /* If this symbol should be put in the .dynsym section, then put it
10996 there now. We already know the symbol index. We also fill in
10997 the entry in the .hash section. */
10998 if (h->dynindx != -1
10999 && elf_hash_table (flinfo->info)->dynamic_sections_created
11000 && elf_hash_table (flinfo->info)->dynsym != NULL
11001 && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
11003 bfd_byte *esym;
11005 /* Since there is no version information in the dynamic string,
11006 if there is no version info in symbol version section, we will
11007 have a run-time problem if not linking executable, referenced
11008 by shared library, or not bound locally. */
11009 if (h->verinfo.verdef == NULL
11010 && (!bfd_link_executable (flinfo->info)
11011 || h->ref_dynamic
11012 || !h->def_regular))
11014 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
11016 if (p && p [1] != '\0')
11018 _bfd_error_handler
11019 /* xgettext:c-format */
11020 (_("%pB: no symbol version section for versioned symbol `%s'"),
11021 flinfo->output_bfd, h->root.root.string);
11022 eoinfo->failed = true;
11023 return false;
11027 sym.st_name = h->dynstr_index;
11028 esym = (elf_hash_table (flinfo->info)->dynsym->contents
11029 + h->dynindx * bed->s->sizeof_sym);
11030 if (!check_dynsym (flinfo->output_bfd, &sym))
11032 eoinfo->failed = true;
11033 return false;
11036 /* Inform the linker of the addition of this symbol. */
11038 if (flinfo->info->callbacks->ctf_new_dynsym)
11039 flinfo->info->callbacks->ctf_new_dynsym (h->dynindx, &sym);
11041 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
11043 if (flinfo->hash_sec != NULL)
11045 size_t hash_entry_size;
11046 bfd_byte *bucketpos;
11047 bfd_vma chain;
11048 size_t bucketcount;
11049 size_t bucket;
11051 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
11052 bucket = h->u.elf_hash_value % bucketcount;
11054 hash_entry_size
11055 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
11056 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
11057 + (bucket + 2) * hash_entry_size);
11058 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
11059 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
11060 bucketpos);
11061 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
11062 ((bfd_byte *) flinfo->hash_sec->contents
11063 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
11066 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
11068 Elf_Internal_Versym iversym;
11069 Elf_External_Versym *eversym;
11071 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
11073 if (h->verinfo.verdef == NULL
11074 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
11075 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
11076 iversym.vs_vers = 1;
11077 else
11078 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
11080 else
11082 if (h->verinfo.vertree == NULL)
11083 iversym.vs_vers = 1;
11084 else
11085 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
11086 if (flinfo->info->create_default_symver)
11087 iversym.vs_vers++;
11090 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
11091 defined locally. */
11092 if (h->versioned == versioned_hidden && h->def_regular)
11093 iversym.vs_vers |= VERSYM_HIDDEN;
11095 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
11096 eversym += h->dynindx;
11097 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
11101 /* If the symbol is undefined, and we didn't output it to .dynsym,
11102 strip it from .symtab too. Obviously we can't do this for
11103 relocatable output or when needed for --emit-relocs. */
11104 else if (input_sec == bfd_und_section_ptr
11105 && h->indx != -2
11106 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
11107 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
11108 && !bfd_link_relocatable (flinfo->info))
11109 return true;
11111 /* Also strip others that we couldn't earlier due to dynamic symbol
11112 processing. */
11113 if (strip)
11114 return true;
11115 if ((input_sec->flags & SEC_EXCLUDE) != 0)
11116 return true;
11118 /* Output a FILE symbol so that following locals are not associated
11119 with the wrong input file. We need one for forced local symbols
11120 if we've seen more than one FILE symbol or when we have exactly
11121 one FILE symbol but global symbols are present in a file other
11122 than the one with the FILE symbol. We also need one if linker
11123 defined symbols are present. In practice these conditions are
11124 always met, so just emit the FILE symbol unconditionally. */
11125 if (eoinfo->localsyms
11126 && !eoinfo->file_sym_done
11127 && eoinfo->flinfo->filesym_count != 0)
11129 Elf_Internal_Sym fsym;
11131 memset (&fsym, 0, sizeof (fsym));
11132 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
11133 fsym.st_shndx = SHN_ABS;
11134 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
11135 bfd_und_section_ptr, NULL))
11136 return false;
11138 eoinfo->file_sym_done = true;
11141 indx = bfd_get_symcount (flinfo->output_bfd);
11142 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
11143 input_sec, h);
11144 if (ret == 0)
11146 eoinfo->failed = true;
11147 return false;
11149 else if (ret == 1)
11150 h->indx = indx;
11151 else if (h->indx == -2)
11152 abort();
11154 return true;
11157 /* Return TRUE if special handling is done for relocs in SEC against
11158 symbols defined in discarded sections. */
11160 static bool
11161 elf_section_ignore_discarded_relocs (asection *sec)
11163 const struct elf_backend_data *bed;
11165 switch (sec->sec_info_type)
11167 case SEC_INFO_TYPE_STABS:
11168 case SEC_INFO_TYPE_EH_FRAME:
11169 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11170 case SEC_INFO_TYPE_SFRAME:
11171 return true;
11172 default:
11173 break;
11176 bed = get_elf_backend_data (sec->owner);
11177 if (bed->elf_backend_ignore_discarded_relocs != NULL
11178 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
11179 return true;
11181 return false;
11184 /* Return a mask saying how ld should treat relocations in SEC against
11185 symbols defined in discarded sections. If this function returns
11186 COMPLAIN set, ld will issue a warning message. If this function
11187 returns PRETEND set, and the discarded section was link-once and the
11188 same size as the kept link-once section, ld will pretend that the
11189 symbol was actually defined in the kept section. Otherwise ld will
11190 zero the reloc (at least that is the intent, but some cooperation by
11191 the target dependent code is needed, particularly for REL targets). */
11193 unsigned int
11194 _bfd_elf_default_action_discarded (asection *sec)
11196 const struct elf_backend_data *bed;
11197 bed = get_elf_backend_data (sec->owner);
11199 if (sec->flags & SEC_DEBUGGING)
11200 return PRETEND;
11202 if (strcmp (".eh_frame", sec->name) == 0)
11203 return 0;
11205 if (bed->elf_backend_can_make_multiple_eh_frame
11206 && strncmp (sec->name, ".eh_frame.", 10) == 0)
11207 return 0;
11209 if (strcmp (".sframe", sec->name) == 0)
11210 return 0;
11212 if (strcmp (".gcc_except_table", sec->name) == 0)
11213 return 0;
11215 return COMPLAIN | PRETEND;
11218 /* Find a match between a section and a member of a section group. */
11220 static asection *
11221 match_group_member (asection *sec, asection *group,
11222 struct bfd_link_info *info)
11224 asection *first = elf_next_in_group (group);
11225 asection *s = first;
11227 while (s != NULL)
11229 if (bfd_elf_match_symbols_in_sections (s, sec, info))
11230 return s;
11232 s = elf_next_in_group (s);
11233 if (s == first)
11234 break;
11237 return NULL;
11240 /* Check if the kept section of a discarded section SEC can be used
11241 to replace it. Return the replacement if it is OK. Otherwise return
11242 NULL. */
11244 asection *
11245 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
11247 asection *kept;
11249 kept = sec->kept_section;
11250 if (kept != NULL)
11252 if ((kept->flags & SEC_GROUP) != 0)
11253 kept = match_group_member (sec, kept, info);
11254 if (kept != NULL)
11256 if ((sec->rawsize != 0 ? sec->rawsize : sec->size)
11257 != (kept->rawsize != 0 ? kept->rawsize : kept->size))
11258 kept = NULL;
11259 else
11261 /* Get the real kept section. */
11262 asection *next;
11263 for (next = kept->kept_section;
11264 next != NULL;
11265 next = next->kept_section)
11266 kept = next;
11269 sec->kept_section = kept;
11271 return kept;
11274 /* Link an input file into the linker output file. This function
11275 handles all the sections and relocations of the input file at once.
11276 This is so that we only have to read the local symbols once, and
11277 don't have to keep them in memory. */
11279 static bool
11280 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
11282 int (*relocate_section)
11283 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
11284 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
11285 bfd *output_bfd;
11286 Elf_Internal_Shdr *symtab_hdr;
11287 size_t locsymcount;
11288 size_t extsymoff;
11289 Elf_Internal_Sym *isymbuf;
11290 Elf_Internal_Sym *isym;
11291 Elf_Internal_Sym *isymend;
11292 long *pindex;
11293 asection **ppsection;
11294 asection *o;
11295 const struct elf_backend_data *bed;
11296 struct elf_link_hash_entry **sym_hashes;
11297 bfd_size_type address_size;
11298 bfd_vma r_type_mask;
11299 int r_sym_shift;
11300 bool have_file_sym = false;
11302 output_bfd = flinfo->output_bfd;
11303 bed = get_elf_backend_data (output_bfd);
11304 relocate_section = bed->elf_backend_relocate_section;
11306 /* If this is a dynamic object, we don't want to do anything here:
11307 we don't want the local symbols, and we don't want the section
11308 contents. */
11309 if ((input_bfd->flags & DYNAMIC) != 0)
11310 return true;
11312 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
11313 if (elf_bad_symtab (input_bfd))
11315 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
11316 extsymoff = 0;
11318 else
11320 locsymcount = symtab_hdr->sh_info;
11321 extsymoff = symtab_hdr->sh_info;
11324 /* Enable GNU OSABI features in the output BFD that are used in the input
11325 BFD. */
11326 if (bed->elf_osabi == ELFOSABI_NONE
11327 || bed->elf_osabi == ELFOSABI_GNU
11328 || bed->elf_osabi == ELFOSABI_FREEBSD)
11329 elf_tdata (output_bfd)->has_gnu_osabi
11330 |= (elf_tdata (input_bfd)->has_gnu_osabi
11331 & (bfd_link_relocatable (flinfo->info)
11332 ? -1 : ~elf_gnu_osabi_retain));
11334 /* Read the local symbols. */
11335 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
11336 if (isymbuf == NULL && locsymcount != 0)
11338 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
11339 flinfo->internal_syms,
11340 flinfo->external_syms,
11341 flinfo->locsym_shndx);
11342 if (isymbuf == NULL)
11343 return false;
11346 /* Find local symbol sections and adjust values of symbols in
11347 SEC_MERGE sections. Write out those local symbols we know are
11348 going into the output file. */
11349 isymend = PTR_ADD (isymbuf, locsymcount);
11350 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
11351 isym < isymend;
11352 isym++, pindex++, ppsection++)
11354 asection *isec;
11355 const char *name;
11356 Elf_Internal_Sym osym;
11357 long indx;
11358 int ret;
11360 *pindex = -1;
11362 if (elf_bad_symtab (input_bfd))
11364 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
11366 *ppsection = NULL;
11367 continue;
11371 if (isym->st_shndx == SHN_UNDEF)
11372 isec = bfd_und_section_ptr;
11373 else if (isym->st_shndx == SHN_ABS)
11374 isec = bfd_abs_section_ptr;
11375 else if (isym->st_shndx == SHN_COMMON)
11376 isec = bfd_com_section_ptr;
11377 else
11379 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
11380 if (isec == NULL)
11382 /* Don't attempt to output symbols with st_shnx in the
11383 reserved range other than SHN_ABS and SHN_COMMON. */
11384 isec = bfd_und_section_ptr;
11386 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
11387 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
11388 isym->st_value =
11389 _bfd_merged_section_offset (output_bfd, &isec,
11390 elf_section_data (isec)->sec_info,
11391 isym->st_value);
11394 *ppsection = isec;
11396 /* Don't output the first, undefined, symbol. In fact, don't
11397 output any undefined local symbol. */
11398 if (isec == bfd_und_section_ptr)
11399 continue;
11401 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
11403 /* We never output section symbols. Instead, we use the
11404 section symbol of the corresponding section in the output
11405 file. */
11406 continue;
11409 /* If we are stripping all symbols, we don't want to output this
11410 one. */
11411 if (flinfo->info->strip == strip_all)
11412 continue;
11414 /* If we are discarding all local symbols, we don't want to
11415 output this one. If we are generating a relocatable output
11416 file, then some of the local symbols may be required by
11417 relocs; we output them below as we discover that they are
11418 needed. */
11419 if (flinfo->info->discard == discard_all)
11420 continue;
11422 /* If this symbol is defined in a section which we are
11423 discarding, we don't need to keep it. */
11424 if (isym->st_shndx < SHN_LORESERVE
11425 && (isec->output_section == NULL
11426 || bfd_section_removed_from_list (output_bfd,
11427 isec->output_section)))
11428 continue;
11430 /* Get the name of the symbol. */
11431 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
11432 isym->st_name);
11433 if (name == NULL)
11434 return false;
11436 /* See if we are discarding symbols with this name. */
11437 if ((flinfo->info->strip == strip_some
11438 && (bfd_hash_lookup (flinfo->info->keep_hash, name, false, false)
11439 == NULL))
11440 || (((flinfo->info->discard == discard_sec_merge
11441 && (isec->flags & SEC_MERGE)
11442 && !bfd_link_relocatable (flinfo->info))
11443 || flinfo->info->discard == discard_l)
11444 && bfd_is_local_label_name (input_bfd, name)))
11445 continue;
11447 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
11449 if (input_bfd->lto_output)
11450 /* -flto puts a temp file name here. This means builds
11451 are not reproducible. Discard the symbol. */
11452 continue;
11453 have_file_sym = true;
11454 flinfo->filesym_count += 1;
11456 if (!have_file_sym)
11458 /* In the absence of debug info, bfd_find_nearest_line uses
11459 FILE symbols to determine the source file for local
11460 function symbols. Provide a FILE symbol here if input
11461 files lack such, so that their symbols won't be
11462 associated with a previous input file. It's not the
11463 source file, but the best we can do. */
11464 const char *filename;
11465 have_file_sym = true;
11466 flinfo->filesym_count += 1;
11467 memset (&osym, 0, sizeof (osym));
11468 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
11469 osym.st_shndx = SHN_ABS;
11470 if (input_bfd->lto_output)
11471 filename = NULL;
11472 else
11473 filename = lbasename (bfd_get_filename (input_bfd));
11474 if (!elf_link_output_symstrtab (flinfo, filename, &osym,
11475 bfd_abs_section_ptr, NULL))
11476 return false;
11479 osym = *isym;
11481 /* Adjust the section index for the output file. */
11482 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11483 isec->output_section);
11484 if (osym.st_shndx == SHN_BAD)
11485 return false;
11487 /* ELF symbols in relocatable files are section relative, but
11488 in executable files they are virtual addresses. Note that
11489 this code assumes that all ELF sections have an associated
11490 BFD section with a reasonable value for output_offset; below
11491 we assume that they also have a reasonable value for
11492 output_section. Any special sections must be set up to meet
11493 these requirements. */
11494 osym.st_value += isec->output_offset;
11495 if (!bfd_link_relocatable (flinfo->info))
11497 osym.st_value += isec->output_section->vma;
11498 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
11500 /* STT_TLS symbols are relative to PT_TLS segment base. */
11501 if (elf_hash_table (flinfo->info)->tls_sec != NULL)
11502 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
11503 else
11504 osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
11505 STT_NOTYPE);
11509 indx = bfd_get_symcount (output_bfd);
11510 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
11511 if (ret == 0)
11512 return false;
11513 else if (ret == 1)
11514 *pindex = indx;
11517 if (bed->s->arch_size == 32)
11519 r_type_mask = 0xff;
11520 r_sym_shift = 8;
11521 address_size = 4;
11523 else
11525 r_type_mask = 0xffffffff;
11526 r_sym_shift = 32;
11527 address_size = 8;
11530 /* Relocate the contents of each section. */
11531 sym_hashes = elf_sym_hashes (input_bfd);
11532 for (o = input_bfd->sections; o != NULL; o = o->next)
11534 bfd_byte *contents;
11536 if (! o->linker_mark)
11538 /* This section was omitted from the link. */
11539 continue;
11542 if (!flinfo->info->resolve_section_groups
11543 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
11545 /* Deal with the group signature symbol. */
11546 struct bfd_elf_section_data *sec_data = elf_section_data (o);
11547 unsigned long symndx = sec_data->this_hdr.sh_info;
11548 asection *osec = o->output_section;
11550 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
11551 if (symndx >= locsymcount
11552 || (elf_bad_symtab (input_bfd)
11553 && flinfo->sections[symndx] == NULL))
11555 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
11556 while (h->root.type == bfd_link_hash_indirect
11557 || h->root.type == bfd_link_hash_warning)
11558 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11559 /* Arrange for symbol to be output. */
11560 h->indx = -2;
11561 elf_section_data (osec)->this_hdr.sh_info = -2;
11563 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
11565 /* We'll use the output section target_index. */
11566 asection *sec = flinfo->sections[symndx]->output_section;
11567 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
11569 else
11571 if (flinfo->indices[symndx] == -1)
11573 /* Otherwise output the local symbol now. */
11574 Elf_Internal_Sym sym = isymbuf[symndx];
11575 asection *sec = flinfo->sections[symndx]->output_section;
11576 const char *name;
11577 long indx;
11578 int ret;
11580 name = bfd_elf_string_from_elf_section (input_bfd,
11581 symtab_hdr->sh_link,
11582 sym.st_name);
11583 if (name == NULL)
11584 return false;
11586 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11587 sec);
11588 if (sym.st_shndx == SHN_BAD)
11589 return false;
11591 sym.st_value += o->output_offset;
11593 indx = bfd_get_symcount (output_bfd);
11594 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
11595 NULL);
11596 if (ret == 0)
11597 return false;
11598 else if (ret == 1)
11599 flinfo->indices[symndx] = indx;
11600 else
11601 abort ();
11603 elf_section_data (osec)->this_hdr.sh_info
11604 = flinfo->indices[symndx];
11608 if ((o->flags & SEC_HAS_CONTENTS) == 0
11609 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
11610 continue;
11612 if ((o->flags & SEC_LINKER_CREATED) != 0)
11614 /* Section was created by _bfd_elf_link_create_dynamic_sections
11615 or somesuch. */
11616 continue;
11619 /* Get the contents of the section. They have been cached by a
11620 relaxation routine. Note that o is a section in an input
11621 file, so the contents field will not have been set by any of
11622 the routines which work on output files. */
11623 if (elf_section_data (o)->this_hdr.contents != NULL)
11625 contents = elf_section_data (o)->this_hdr.contents;
11626 if (bed->caches_rawsize
11627 && o->rawsize != 0
11628 && o->rawsize < o->size)
11630 memcpy (flinfo->contents, contents, o->rawsize);
11631 contents = flinfo->contents;
11634 else if (!(o->flags & SEC_RELOC)
11635 && !bed->elf_backend_write_section
11636 && o->sec_info_type == SEC_INFO_TYPE_MERGE)
11637 /* A MERGE section that has no relocations doesn't need the
11638 contents anymore, they have been recorded earlier. Except
11639 if the backend has special provisions for writing sections. */
11640 contents = NULL;
11641 else
11643 contents = flinfo->contents;
11644 if (! _bfd_elf_link_mmap_section_contents (input_bfd, o,
11645 &contents))
11646 return false;
11649 if ((o->flags & SEC_RELOC) != 0)
11651 Elf_Internal_Rela *internal_relocs;
11652 Elf_Internal_Rela *rel, *relend;
11653 int action_discarded;
11654 int ret;
11656 /* Get the swapped relocs. */
11657 internal_relocs
11658 = _bfd_elf_link_info_read_relocs (input_bfd, flinfo->info, o,
11659 flinfo->external_relocs,
11660 flinfo->internal_relocs,
11661 false);
11662 if (internal_relocs == NULL
11663 && o->reloc_count > 0)
11664 return false;
11666 action_discarded = -1;
11667 if (!elf_section_ignore_discarded_relocs (o))
11668 action_discarded = (*bed->action_discarded) (o);
11670 /* Run through the relocs evaluating complex reloc symbols and
11671 looking for relocs against symbols from discarded sections
11672 or section symbols from removed link-once sections.
11673 Complain about relocs against discarded sections. Zero
11674 relocs against removed link-once sections. */
11676 rel = internal_relocs;
11677 relend = rel + o->reloc_count;
11678 for ( ; rel < relend; rel++)
11680 unsigned long r_symndx = rel->r_info >> r_sym_shift;
11681 unsigned int s_type;
11682 asection **ps, *sec;
11683 struct elf_link_hash_entry *h = NULL;
11684 const char *sym_name;
11686 if (r_symndx == STN_UNDEF)
11687 continue;
11689 if (r_symndx >= locsymcount
11690 || (elf_bad_symtab (input_bfd)
11691 && flinfo->sections[r_symndx] == NULL))
11693 h = sym_hashes[r_symndx - extsymoff];
11695 /* Badly formatted input files can contain relocs that
11696 reference non-existant symbols. Check here so that
11697 we do not seg fault. */
11698 if (h == NULL)
11700 _bfd_error_handler
11701 /* xgettext:c-format */
11702 (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
11703 "that references a non-existent global symbol"),
11704 input_bfd, (uint64_t) rel->r_info, o);
11705 bfd_set_error (bfd_error_bad_value);
11706 return false;
11709 while (h->root.type == bfd_link_hash_indirect
11710 || h->root.type == bfd_link_hash_warning)
11711 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11713 s_type = h->type;
11715 /* If a plugin symbol is referenced from a non-IR file,
11716 mark the symbol as undefined. Note that the
11717 linker may attach linker created dynamic sections
11718 to the plugin bfd. Symbols defined in linker
11719 created sections are not plugin symbols. */
11720 if ((h->root.non_ir_ref_regular
11721 || h->root.non_ir_ref_dynamic)
11722 && (h->root.type == bfd_link_hash_defined
11723 || h->root.type == bfd_link_hash_defweak)
11724 && (h->root.u.def.section->flags
11725 & SEC_LINKER_CREATED) == 0
11726 && h->root.u.def.section->owner != NULL
11727 && (h->root.u.def.section->owner->flags
11728 & BFD_PLUGIN) != 0)
11730 h->root.type = bfd_link_hash_undefined;
11731 h->root.u.undef.abfd = h->root.u.def.section->owner;
11734 ps = NULL;
11735 if (h->root.type == bfd_link_hash_defined
11736 || h->root.type == bfd_link_hash_defweak)
11737 ps = &h->root.u.def.section;
11739 sym_name = h->root.root.string;
11741 else
11743 Elf_Internal_Sym *sym = isymbuf + r_symndx;
11745 s_type = ELF_ST_TYPE (sym->st_info);
11746 ps = &flinfo->sections[r_symndx];
11747 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
11748 sym, *ps);
11751 if ((s_type == STT_RELC || s_type == STT_SRELC)
11752 && !bfd_link_relocatable (flinfo->info))
11754 bfd_vma val;
11755 bfd_vma dot = (rel->r_offset
11756 + o->output_offset + o->output_section->vma);
11757 #ifdef DEBUG
11758 printf ("Encountered a complex symbol!");
11759 printf (" (input_bfd %s, section %s, reloc %ld\n",
11760 bfd_get_filename (input_bfd), o->name,
11761 (long) (rel - internal_relocs));
11762 printf (" symbol: idx %8.8lx, name %s\n",
11763 r_symndx, sym_name);
11764 printf (" reloc : info %8.8lx, addr %8.8lx\n",
11765 (unsigned long) rel->r_info,
11766 (unsigned long) rel->r_offset);
11767 #endif
11768 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
11769 isymbuf, locsymcount, s_type == STT_SRELC))
11770 return false;
11772 /* Symbol evaluated OK. Update to absolute value. */
11773 set_symbol_value (input_bfd, isymbuf, locsymcount,
11774 r_symndx, val);
11775 continue;
11778 if (action_discarded != -1 && ps != NULL)
11780 /* Complain if the definition comes from a
11781 discarded section. */
11782 if ((sec = *ps) != NULL && discarded_section (sec))
11784 BFD_ASSERT (r_symndx != STN_UNDEF);
11785 if (action_discarded & COMPLAIN)
11786 (*flinfo->info->callbacks->einfo)
11787 /* xgettext:c-format */
11788 (_("%X`%s' referenced in section `%pA' of %pB: "
11789 "defined in discarded section `%pA' of %pB\n"),
11790 sym_name, o, input_bfd, sec, sec->owner);
11792 /* Try to do the best we can to support buggy old
11793 versions of gcc. Pretend that the symbol is
11794 really defined in the kept linkonce section.
11795 FIXME: This is quite broken. Modifying the
11796 symbol here means we will be changing all later
11797 uses of the symbol, not just in this section. */
11798 if (action_discarded & PRETEND)
11800 asection *kept;
11802 kept = _bfd_elf_check_kept_section (sec,
11803 flinfo->info);
11804 if (kept != NULL)
11806 *ps = kept;
11807 continue;
11814 /* Relocate the section by invoking a back end routine.
11816 The back end routine is responsible for adjusting the
11817 section contents as necessary, and (if using Rela relocs
11818 and generating a relocatable output file) adjusting the
11819 reloc addend as necessary.
11821 The back end routine does not have to worry about setting
11822 the reloc address or the reloc symbol index.
11824 The back end routine is given a pointer to the swapped in
11825 internal symbols, and can access the hash table entries
11826 for the external symbols via elf_sym_hashes (input_bfd).
11828 When generating relocatable output, the back end routine
11829 must handle STB_LOCAL/STT_SECTION symbols specially. The
11830 output symbol is going to be a section symbol
11831 corresponding to the output section, which will require
11832 the addend to be adjusted. */
11834 ret = (*relocate_section) (output_bfd, flinfo->info,
11835 input_bfd, o, contents,
11836 internal_relocs,
11837 isymbuf,
11838 flinfo->sections);
11839 if (!ret)
11840 return false;
11842 if (ret == 2
11843 || bfd_link_relocatable (flinfo->info)
11844 || flinfo->info->emitrelocations)
11846 Elf_Internal_Rela *irela;
11847 Elf_Internal_Rela *irelaend, *irelamid;
11848 bfd_vma last_offset;
11849 struct elf_link_hash_entry **rel_hash;
11850 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
11851 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
11852 unsigned int next_erel;
11853 bool rela_normal;
11854 struct bfd_elf_section_data *esdi, *esdo;
11856 esdi = elf_section_data (o);
11857 esdo = elf_section_data (o->output_section);
11858 rela_normal = false;
11860 /* Adjust the reloc addresses and symbol indices. */
11862 irela = internal_relocs;
11863 irelaend = irela + o->reloc_count;
11864 rel_hash = PTR_ADD (esdo->rel.hashes, esdo->rel.count);
11865 /* We start processing the REL relocs, if any. When we reach
11866 IRELAMID in the loop, we switch to the RELA relocs. */
11867 irelamid = irela;
11868 if (esdi->rel.hdr != NULL)
11869 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
11870 * bed->s->int_rels_per_ext_rel);
11871 rel_hash_list = rel_hash;
11872 rela_hash_list = NULL;
11873 last_offset = o->output_offset;
11874 if (!bfd_link_relocatable (flinfo->info))
11875 last_offset += o->output_section->vma;
11876 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
11878 unsigned long r_symndx;
11879 asection *sec;
11880 Elf_Internal_Sym sym;
11882 if (next_erel == bed->s->int_rels_per_ext_rel)
11884 rel_hash++;
11885 next_erel = 0;
11888 if (irela == irelamid)
11890 rel_hash = PTR_ADD (esdo->rela.hashes, esdo->rela.count);
11891 rela_hash_list = rel_hash;
11892 if (bed->is_rela_normal != NULL)
11893 rela_normal = bed->is_rela_normal (irela);
11894 else
11895 rela_normal = bed->rela_normal;
11898 irela->r_offset = _bfd_elf_section_offset (output_bfd,
11899 flinfo->info, o,
11900 irela->r_offset);
11901 if (irela->r_offset >= (bfd_vma) -2)
11903 /* This is a reloc for a deleted entry or somesuch.
11904 Turn it into an R_*_NONE reloc, at the same
11905 offset as the last reloc. elf_eh_frame.c and
11906 bfd_elf_discard_info rely on reloc offsets
11907 being ordered. */
11908 irela->r_offset = last_offset;
11909 irela->r_info = 0;
11910 irela->r_addend = 0;
11911 continue;
11914 irela->r_offset += o->output_offset;
11916 /* Relocs in an executable have to be virtual addresses. */
11917 if (!bfd_link_relocatable (flinfo->info))
11918 irela->r_offset += o->output_section->vma;
11920 last_offset = irela->r_offset;
11922 r_symndx = irela->r_info >> r_sym_shift;
11923 if (r_symndx == STN_UNDEF)
11924 continue;
11926 if (r_symndx >= locsymcount
11927 || (elf_bad_symtab (input_bfd)
11928 && flinfo->sections[r_symndx] == NULL))
11930 struct elf_link_hash_entry *rh;
11931 unsigned long indx;
11933 /* This is a reloc against a global symbol. We
11934 have not yet output all the local symbols, so
11935 we do not know the symbol index of any global
11936 symbol. We set the rel_hash entry for this
11937 reloc to point to the global hash table entry
11938 for this symbol. The symbol index is then
11939 set at the end of bfd_elf_final_link. */
11940 indx = r_symndx - extsymoff;
11941 rh = elf_sym_hashes (input_bfd)[indx];
11942 while (rh->root.type == bfd_link_hash_indirect
11943 || rh->root.type == bfd_link_hash_warning)
11944 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
11946 /* Setting the index to -2 tells
11947 elf_link_output_extsym that this symbol is
11948 used by a reloc. */
11949 BFD_ASSERT (rh->indx < 0);
11950 rh->indx = -2;
11951 *rel_hash = rh;
11953 continue;
11956 /* This is a reloc against a local symbol. */
11958 *rel_hash = NULL;
11959 sym = isymbuf[r_symndx];
11960 sec = flinfo->sections[r_symndx];
11961 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
11963 /* I suppose the backend ought to fill in the
11964 section of any STT_SECTION symbol against a
11965 processor specific section. */
11966 r_symndx = STN_UNDEF;
11967 if (bfd_is_abs_section (sec))
11969 else if (sec == NULL || sec->owner == NULL)
11971 bfd_set_error (bfd_error_bad_value);
11972 return false;
11974 else
11976 asection *osec = sec->output_section;
11978 /* If we have discarded a section, the output
11979 section will be the absolute section. In
11980 case of discarded SEC_MERGE sections, use
11981 the kept section. relocate_section should
11982 have already handled discarded linkonce
11983 sections. */
11984 if (bfd_is_abs_section (osec)
11985 && sec->kept_section != NULL
11986 && sec->kept_section->output_section != NULL)
11988 osec = sec->kept_section->output_section;
11989 irela->r_addend -= osec->vma;
11992 if (!bfd_is_abs_section (osec))
11994 r_symndx = osec->target_index;
11995 if (r_symndx == STN_UNDEF)
11997 irela->r_addend += osec->vma;
11998 osec = _bfd_nearby_section (output_bfd, osec,
11999 osec->vma);
12000 irela->r_addend -= osec->vma;
12001 r_symndx = osec->target_index;
12006 /* Adjust the addend according to where the
12007 section winds up in the output section. */
12008 if (rela_normal)
12009 irela->r_addend += sec->output_offset;
12011 else
12013 if (flinfo->indices[r_symndx] == -1)
12015 unsigned long shlink;
12016 const char *name;
12017 asection *osec;
12018 long indx;
12020 if (flinfo->info->strip == strip_all)
12022 /* You can't do ld -r -s. */
12023 bfd_set_error (bfd_error_invalid_operation);
12024 return false;
12027 /* This symbol was skipped earlier, but
12028 since it is needed by a reloc, we
12029 must output it now. */
12030 shlink = symtab_hdr->sh_link;
12031 name = (bfd_elf_string_from_elf_section
12032 (input_bfd, shlink, sym.st_name));
12033 if (name == NULL)
12034 return false;
12036 osec = sec->output_section;
12037 sym.st_shndx =
12038 _bfd_elf_section_from_bfd_section (output_bfd,
12039 osec);
12040 if (sym.st_shndx == SHN_BAD)
12041 return false;
12043 sym.st_value += sec->output_offset;
12044 if (!bfd_link_relocatable (flinfo->info))
12046 sym.st_value += osec->vma;
12047 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
12049 struct elf_link_hash_table *htab
12050 = elf_hash_table (flinfo->info);
12052 /* STT_TLS symbols are relative to PT_TLS
12053 segment base. */
12054 if (htab->tls_sec != NULL)
12055 sym.st_value -= htab->tls_sec->vma;
12056 else
12057 sym.st_info
12058 = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
12059 STT_NOTYPE);
12063 indx = bfd_get_symcount (output_bfd);
12064 ret = elf_link_output_symstrtab (flinfo, name,
12065 &sym, sec,
12066 NULL);
12067 if (ret == 0)
12068 return false;
12069 else if (ret == 1)
12070 flinfo->indices[r_symndx] = indx;
12071 else
12072 abort ();
12075 r_symndx = flinfo->indices[r_symndx];
12078 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
12079 | (irela->r_info & r_type_mask));
12082 /* Swap out the relocs. */
12083 input_rel_hdr = esdi->rel.hdr;
12084 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
12086 if (!bed->elf_backend_emit_relocs (output_bfd, o,
12087 input_rel_hdr,
12088 internal_relocs,
12089 rel_hash_list))
12090 return false;
12091 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
12092 * bed->s->int_rels_per_ext_rel);
12093 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
12096 input_rela_hdr = esdi->rela.hdr;
12097 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
12099 if (!bed->elf_backend_emit_relocs (output_bfd, o,
12100 input_rela_hdr,
12101 internal_relocs,
12102 rela_hash_list))
12103 return false;
12108 /* Write out the modified section contents. */
12109 if (bed->elf_backend_write_section
12110 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
12111 contents))
12113 /* Section written out. */
12115 else switch (o->sec_info_type)
12117 case SEC_INFO_TYPE_STABS:
12118 if (! (_bfd_write_section_stabs
12119 (output_bfd,
12120 &elf_hash_table (flinfo->info)->stab_info,
12121 o, &elf_section_data (o)->sec_info, contents)))
12122 return false;
12123 break;
12124 case SEC_INFO_TYPE_MERGE:
12125 if (! _bfd_write_merged_section (output_bfd, o,
12126 elf_section_data (o)->sec_info))
12127 return false;
12128 break;
12129 case SEC_INFO_TYPE_EH_FRAME:
12131 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
12132 o, contents))
12133 return false;
12135 break;
12136 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
12138 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
12139 flinfo->info,
12140 o, contents))
12141 return false;
12143 break;
12144 case SEC_INFO_TYPE_SFRAME:
12146 /* Merge .sframe sections into the ctf frame encoder
12147 context of the output_bfd's section. The final .sframe
12148 output section will be written out later. */
12149 if (!_bfd_elf_merge_section_sframe (output_bfd, flinfo->info,
12150 o, contents))
12151 return false;
12153 break;
12154 default:
12156 if (! (o->flags & SEC_EXCLUDE))
12158 file_ptr offset = (file_ptr) o->output_offset;
12159 bfd_size_type todo = o->size;
12161 offset *= bfd_octets_per_byte (output_bfd, o);
12163 if ((o->flags & SEC_ELF_REVERSE_COPY)
12164 && o->size > address_size)
12166 /* Reverse-copy input section to output. */
12168 if ((o->size & (address_size - 1)) != 0
12169 || (o->reloc_count != 0
12170 && (o->size * bed->s->int_rels_per_ext_rel
12171 != o->reloc_count * address_size)))
12173 _bfd_error_handler
12174 /* xgettext:c-format */
12175 (_("error: %pB: size of section %pA is not "
12176 "multiple of address size"),
12177 input_bfd, o);
12178 bfd_set_error (bfd_error_bad_value);
12179 return false;
12184 todo -= address_size;
12185 if (! bfd_set_section_contents (output_bfd,
12186 o->output_section,
12187 contents + todo,
12188 offset,
12189 address_size))
12190 return false;
12191 if (todo == 0)
12192 break;
12193 offset += address_size;
12195 while (1);
12197 else if (! bfd_set_section_contents (output_bfd,
12198 o->output_section,
12199 contents,
12200 offset, todo))
12201 return false;
12204 break;
12207 /* Munmap the section contents for each input section. */
12208 _bfd_elf_link_munmap_section_contents (o);
12211 return true;
12214 /* Generate a reloc when linking an ELF file. This is a reloc
12215 requested by the linker, and does not come from any input file. This
12216 is used to build constructor and destructor tables when linking
12217 with -Ur. */
12219 static bool
12220 elf_reloc_link_order (bfd *output_bfd,
12221 struct bfd_link_info *info,
12222 asection *output_section,
12223 struct bfd_link_order *link_order)
12225 reloc_howto_type *howto;
12226 long indx;
12227 bfd_vma offset;
12228 bfd_vma addend;
12229 struct bfd_elf_section_reloc_data *reldata;
12230 struct elf_link_hash_entry **rel_hash_ptr;
12231 Elf_Internal_Shdr *rel_hdr;
12232 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
12233 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
12234 bfd_byte *erel;
12235 unsigned int i;
12236 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
12238 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
12239 if (howto == NULL)
12241 bfd_set_error (bfd_error_bad_value);
12242 return false;
12245 addend = link_order->u.reloc.p->addend;
12247 if (esdo->rel.hdr)
12248 reldata = &esdo->rel;
12249 else if (esdo->rela.hdr)
12250 reldata = &esdo->rela;
12251 else
12253 reldata = NULL;
12254 BFD_ASSERT (0);
12257 /* Figure out the symbol index. */
12258 rel_hash_ptr = reldata->hashes + reldata->count;
12259 if (link_order->type == bfd_section_reloc_link_order)
12261 indx = link_order->u.reloc.p->u.section->target_index;
12262 BFD_ASSERT (indx != 0);
12263 *rel_hash_ptr = NULL;
12265 else
12267 struct elf_link_hash_entry *h;
12269 /* Treat a reloc against a defined symbol as though it were
12270 actually against the section. */
12271 h = ((struct elf_link_hash_entry *)
12272 bfd_wrapped_link_hash_lookup (output_bfd, info,
12273 link_order->u.reloc.p->u.name,
12274 false, false, true));
12275 if (h != NULL
12276 && (h->root.type == bfd_link_hash_defined
12277 || h->root.type == bfd_link_hash_defweak))
12279 asection *section;
12281 section = h->root.u.def.section;
12282 indx = section->output_section->target_index;
12283 *rel_hash_ptr = NULL;
12284 /* It seems that we ought to add the symbol value to the
12285 addend here, but in practice it has already been added
12286 because it was passed to constructor_callback. */
12287 addend += section->output_section->vma + section->output_offset;
12289 else if (h != NULL)
12291 /* Setting the index to -2 tells elf_link_output_extsym that
12292 this symbol is used by a reloc. */
12293 h->indx = -2;
12294 *rel_hash_ptr = h;
12295 indx = 0;
12297 else
12299 (*info->callbacks->unattached_reloc)
12300 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
12301 indx = 0;
12305 /* If this is an inplace reloc, we must write the addend into the
12306 object file. */
12307 if (howto->partial_inplace && addend != 0)
12309 bfd_size_type size;
12310 bfd_reloc_status_type rstat;
12311 bfd_byte *buf;
12312 bool ok;
12313 const char *sym_name;
12314 bfd_size_type octets;
12316 size = (bfd_size_type) bfd_get_reloc_size (howto);
12317 buf = (bfd_byte *) bfd_zmalloc (size);
12318 if (buf == NULL && size != 0)
12319 return false;
12320 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
12321 switch (rstat)
12323 case bfd_reloc_ok:
12324 break;
12326 default:
12327 case bfd_reloc_outofrange:
12328 abort ();
12330 case bfd_reloc_overflow:
12331 if (link_order->type == bfd_section_reloc_link_order)
12332 sym_name = bfd_section_name (link_order->u.reloc.p->u.section);
12333 else
12334 sym_name = link_order->u.reloc.p->u.name;
12335 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
12336 howto->name, addend, NULL, NULL,
12337 (bfd_vma) 0);
12338 break;
12341 octets = link_order->offset * bfd_octets_per_byte (output_bfd,
12342 output_section);
12343 ok = bfd_set_section_contents (output_bfd, output_section, buf,
12344 octets, size);
12345 free (buf);
12346 if (! ok)
12347 return false;
12350 /* The address of a reloc is relative to the section in a
12351 relocatable file, and is a virtual address in an executable
12352 file. */
12353 offset = link_order->offset;
12354 if (! bfd_link_relocatable (info))
12355 offset += output_section->vma;
12357 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
12359 irel[i].r_offset = offset;
12360 irel[i].r_info = 0;
12361 irel[i].r_addend = 0;
12363 if (bed->s->arch_size == 32)
12364 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
12365 else
12366 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
12368 rel_hdr = reldata->hdr;
12369 erel = rel_hdr->contents;
12370 if (rel_hdr->sh_type == SHT_REL)
12372 erel += reldata->count * bed->s->sizeof_rel;
12373 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
12375 else
12377 irel[0].r_addend = addend;
12378 erel += reldata->count * bed->s->sizeof_rela;
12379 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
12382 ++reldata->count;
12384 return true;
12387 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
12388 Returns TRUE upon success, FALSE otherwise. */
12390 static bool
12391 elf_output_implib (bfd *abfd, struct bfd_link_info *info)
12393 bool ret = false;
12394 bfd *implib_bfd;
12395 const struct elf_backend_data *bed;
12396 flagword flags;
12397 enum bfd_architecture arch;
12398 unsigned int mach;
12399 asymbol **sympp = NULL;
12400 long symsize;
12401 long symcount;
12402 long src_count;
12403 elf_symbol_type *osymbuf;
12404 size_t amt;
12406 implib_bfd = info->out_implib_bfd;
12407 bed = get_elf_backend_data (abfd);
12409 if (!bfd_set_format (implib_bfd, bfd_object))
12410 return false;
12412 /* Use flag from executable but make it a relocatable object. */
12413 flags = bfd_get_file_flags (abfd);
12414 flags &= ~HAS_RELOC;
12415 if (!bfd_set_start_address (implib_bfd, 0)
12416 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
12417 return false;
12419 /* Copy architecture of output file to import library file. */
12420 arch = bfd_get_arch (abfd);
12421 mach = bfd_get_mach (abfd);
12422 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
12423 && (abfd->target_defaulted
12424 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
12425 return false;
12427 /* Get symbol table size. */
12428 symsize = bfd_get_symtab_upper_bound (abfd);
12429 if (symsize < 0)
12430 return false;
12432 /* Read in the symbol table. */
12433 sympp = (asymbol **) bfd_malloc (symsize);
12434 if (sympp == NULL)
12435 return false;
12437 symcount = bfd_canonicalize_symtab (abfd, sympp);
12438 if (symcount < 0)
12439 goto free_sym_buf;
12441 /* Allow the BFD backend to copy any private header data it
12442 understands from the output BFD to the import library BFD. */
12443 if (! bfd_copy_private_header_data (abfd, implib_bfd))
12444 goto free_sym_buf;
12446 /* Filter symbols to appear in the import library. */
12447 if (bed->elf_backend_filter_implib_symbols)
12448 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
12449 symcount);
12450 else
12451 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
12452 if (symcount == 0)
12454 bfd_set_error (bfd_error_no_symbols);
12455 _bfd_error_handler (_("%pB: no symbol found for import library"),
12456 implib_bfd);
12457 goto free_sym_buf;
12461 /* Make symbols absolute. */
12462 amt = symcount * sizeof (*osymbuf);
12463 osymbuf = (elf_symbol_type *) bfd_alloc (implib_bfd, amt);
12464 if (osymbuf == NULL)
12465 goto free_sym_buf;
12467 for (src_count = 0; src_count < symcount; src_count++)
12469 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
12470 sizeof (*osymbuf));
12471 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
12472 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
12473 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
12474 osymbuf[src_count].internal_elf_sym.st_value =
12475 osymbuf[src_count].symbol.value;
12476 sympp[src_count] = &osymbuf[src_count].symbol;
12479 bfd_set_symtab (implib_bfd, sympp, symcount);
12481 /* Allow the BFD backend to copy any private data it understands
12482 from the output BFD to the import library BFD. This is done last
12483 to permit the routine to look at the filtered symbol table. */
12484 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
12485 goto free_sym_buf;
12487 if (!bfd_close (implib_bfd))
12488 goto free_sym_buf;
12490 ret = true;
12492 free_sym_buf:
12493 free (sympp);
12494 return ret;
12497 static void
12498 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
12500 asection *o;
12502 if (flinfo->symstrtab != NULL)
12503 _bfd_elf_strtab_free (flinfo->symstrtab);
12504 free (flinfo->contents);
12505 free (flinfo->external_relocs);
12506 free (flinfo->internal_relocs);
12507 free (flinfo->external_syms);
12508 free (flinfo->locsym_shndx);
12509 free (flinfo->internal_syms);
12510 free (flinfo->indices);
12511 free (flinfo->sections);
12512 if (flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1)
12513 free (flinfo->symshndxbuf);
12514 for (o = obfd->sections; o != NULL; o = o->next)
12516 struct bfd_elf_section_data *esdo = elf_section_data (o);
12517 free (esdo->rel.hashes);
12518 free (esdo->rela.hashes);
12522 /* Do the final step of an ELF link. */
12524 bool
12525 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
12527 bool dynamic;
12528 bool emit_relocs;
12529 bfd *dynobj;
12530 struct elf_final_link_info flinfo;
12531 asection *o;
12532 struct bfd_link_order *p;
12533 bfd *sub;
12534 bfd_size_type max_contents_size;
12535 bfd_size_type max_external_reloc_size;
12536 bfd_size_type max_internal_reloc_count;
12537 bfd_size_type max_sym_count;
12538 bfd_size_type max_sym_shndx_count;
12539 Elf_Internal_Sym elfsym;
12540 unsigned int i;
12541 Elf_Internal_Shdr *symtab_hdr;
12542 Elf_Internal_Shdr *symtab_shndx_hdr;
12543 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12544 struct elf_outext_info eoinfo;
12545 bool merged;
12546 size_t relativecount;
12547 size_t relr_entsize;
12548 asection *reldyn = 0;
12549 bfd_size_type amt;
12550 asection *attr_section = NULL;
12551 bfd_vma attr_size = 0;
12552 const char *std_attrs_section;
12553 struct elf_link_hash_table *htab = elf_hash_table (info);
12554 bool sections_removed;
12555 bool ret;
12557 if (!is_elf_hash_table (&htab->root))
12558 return false;
12560 if (bfd_link_pic (info))
12561 abfd->flags |= DYNAMIC;
12563 dynamic = htab->dynamic_sections_created;
12564 dynobj = htab->dynobj;
12566 emit_relocs = (bfd_link_relocatable (info)
12567 || info->emitrelocations);
12569 memset (&flinfo, 0, sizeof (flinfo));
12570 flinfo.info = info;
12571 flinfo.output_bfd = abfd;
12572 flinfo.symstrtab = _bfd_elf_strtab_init ();
12573 if (flinfo.symstrtab == NULL)
12574 return false;
12576 if (! dynamic)
12578 flinfo.hash_sec = NULL;
12579 flinfo.symver_sec = NULL;
12581 else
12583 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
12584 /* Note that dynsym_sec can be NULL (on VMS). */
12585 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
12586 /* Note that it is OK if symver_sec is NULL. */
12589 if (info->unique_symbol
12590 && !bfd_hash_table_init (&flinfo.local_hash_table,
12591 local_hash_newfunc,
12592 sizeof (struct local_hash_entry)))
12593 return false;
12595 /* The object attributes have been merged. Remove the input
12596 sections from the link, and set the contents of the output
12597 section. */
12598 sections_removed = false;
12599 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
12600 for (o = abfd->sections; o != NULL; o = o->next)
12602 bool remove_section = false;
12604 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
12605 || strcmp (o->name, ".gnu.attributes") == 0)
12607 for (p = o->map_head.link_order; p != NULL; p = p->next)
12609 asection *input_section;
12611 if (p->type != bfd_indirect_link_order)
12612 continue;
12613 input_section = p->u.indirect.section;
12614 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12615 elf_link_input_bfd ignores this section. */
12616 input_section->flags &= ~SEC_HAS_CONTENTS;
12619 attr_size = bfd_elf_obj_attr_size (abfd);
12620 bfd_set_section_size (o, attr_size);
12621 /* Skip this section later on. */
12622 o->map_head.link_order = NULL;
12623 if (attr_size)
12624 attr_section = o;
12625 else
12626 remove_section = true;
12628 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
12630 /* Remove empty group section from linker output. */
12631 remove_section = true;
12633 if (remove_section)
12635 o->flags |= SEC_EXCLUDE;
12636 bfd_section_list_remove (abfd, o);
12637 abfd->section_count--;
12638 sections_removed = true;
12641 if (sections_removed)
12642 _bfd_fix_excluded_sec_syms (abfd, info);
12644 /* Count up the number of relocations we will output for each output
12645 section, so that we know the sizes of the reloc sections. We
12646 also figure out some maximum sizes. */
12647 #ifdef USE_MMAP
12648 if (bed->use_mmap)
12650 /* Mmap is used only if section size >= the minimum mmap section
12651 size. The initial max_contents_size value covers all sections
12652 smaller than the minimum mmap section size. It may be increased
12653 for compressed or linker created sections or sections whose
12654 rawsize != size. max_external_reloc_size covers all relocation
12655 sections smaller than the minimum mmap section size. */
12656 max_contents_size = _bfd_minimum_mmap_size;
12657 max_external_reloc_size = _bfd_minimum_mmap_size;
12659 else
12660 #endif
12662 max_contents_size = 0;
12663 max_external_reloc_size = 0;
12665 max_internal_reloc_count = 0;
12666 max_sym_count = 0;
12667 max_sym_shndx_count = 0;
12668 merged = false;
12669 for (o = abfd->sections; o != NULL; o = o->next)
12671 struct bfd_elf_section_data *esdo = elf_section_data (o);
12672 o->reloc_count = 0;
12674 for (p = o->map_head.link_order; p != NULL; p = p->next)
12676 unsigned int reloc_count = 0;
12677 unsigned int additional_reloc_count = 0;
12678 struct bfd_elf_section_data *esdi = NULL;
12680 if (p->type == bfd_section_reloc_link_order
12681 || p->type == bfd_symbol_reloc_link_order)
12682 reloc_count = 1;
12683 else if (p->type == bfd_indirect_link_order)
12685 asection *sec;
12687 sec = p->u.indirect.section;
12689 /* Mark all sections which are to be included in the
12690 link. This will normally be every section. We need
12691 to do this so that we can identify any sections which
12692 the linker has decided to not include. */
12693 sec->linker_mark = true;
12695 if (sec->flags & SEC_MERGE)
12696 merged = true;
12698 #ifdef USE_MMAP
12699 /* Mmap is used only on non-compressed, non-linker created
12700 sections whose rawsize == size. */
12701 if (!bed->use_mmap
12702 || sec->compress_status != COMPRESS_SECTION_NONE
12703 || (sec->flags & SEC_LINKER_CREATED) != 0
12704 || sec->rawsize != sec->size)
12705 #endif
12707 if (sec->rawsize > max_contents_size)
12708 max_contents_size = sec->rawsize;
12709 if (sec->size > max_contents_size)
12710 max_contents_size = sec->size;
12713 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
12714 && (sec->owner->flags & DYNAMIC) == 0)
12716 size_t sym_count;
12718 /* We are interested in just local symbols, not all
12719 symbols. */
12720 if (elf_bad_symtab (sec->owner))
12721 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
12722 / bed->s->sizeof_sym);
12723 else
12724 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
12726 if (sym_count > max_sym_count)
12727 max_sym_count = sym_count;
12729 if (sym_count > max_sym_shndx_count
12730 && elf_symtab_shndx_list (sec->owner) != NULL)
12731 max_sym_shndx_count = sym_count;
12733 esdi = elf_section_data (sec);
12735 if (esdi->this_hdr.sh_type == SHT_REL
12736 || esdi->this_hdr.sh_type == SHT_RELA)
12737 /* Some backends use reloc_count in relocation sections
12738 to count particular types of relocs. Of course,
12739 reloc sections themselves can't have relocations. */
12741 else if (emit_relocs)
12743 reloc_count = sec->reloc_count;
12744 if (bed->elf_backend_count_additional_relocs)
12746 int c;
12747 c = (*bed->elf_backend_count_additional_relocs) (sec);
12748 additional_reloc_count += c;
12751 else if (bed->elf_backend_count_relocs)
12752 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
12754 if ((sec->flags & SEC_RELOC) != 0)
12756 #ifdef USE_MMAP
12757 if (!bed->use_mmap)
12758 #endif
12760 size_t ext_size = 0;
12762 if (esdi->rel.hdr != NULL)
12763 ext_size = esdi->rel.hdr->sh_size;
12764 if (esdi->rela.hdr != NULL)
12765 ext_size += esdi->rela.hdr->sh_size;
12767 if (ext_size > max_external_reloc_size)
12768 max_external_reloc_size = ext_size;
12770 if (sec->reloc_count > max_internal_reloc_count)
12771 max_internal_reloc_count = sec->reloc_count;
12776 if (reloc_count == 0)
12777 continue;
12779 reloc_count += additional_reloc_count;
12780 o->reloc_count += reloc_count;
12782 if (p->type == bfd_indirect_link_order && emit_relocs)
12784 if (esdi->rel.hdr)
12786 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
12787 esdo->rel.count += additional_reloc_count;
12789 if (esdi->rela.hdr)
12791 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
12792 esdo->rela.count += additional_reloc_count;
12795 else
12797 if (o->use_rela_p)
12798 esdo->rela.count += reloc_count;
12799 else
12800 esdo->rel.count += reloc_count;
12804 if (o->reloc_count > 0)
12805 o->flags |= SEC_RELOC;
12806 else
12808 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12809 set it (this is probably a bug) and if it is set
12810 assign_section_numbers will create a reloc section. */
12811 o->flags &=~ SEC_RELOC;
12814 /* If the SEC_ALLOC flag is not set, force the section VMA to
12815 zero. This is done in elf_fake_sections as well, but forcing
12816 the VMA to 0 here will ensure that relocs against these
12817 sections are handled correctly. */
12818 if ((o->flags & SEC_ALLOC) == 0
12819 && ! o->user_set_vma)
12820 o->vma = 0;
12823 if (! bfd_link_relocatable (info) && merged)
12824 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
12826 /* Figure out the file positions for everything but the symbol table
12827 and the relocs. We set symcount to force assign_section_numbers
12828 to create a symbol table. */
12829 abfd->symcount = info->strip != strip_all || emit_relocs;
12830 BFD_ASSERT (! abfd->output_has_begun);
12831 if (! _bfd_elf_compute_section_file_positions (abfd, info))
12832 goto error_return;
12834 /* Set sizes, and assign file positions for reloc sections. */
12835 for (o = abfd->sections; o != NULL; o = o->next)
12837 struct bfd_elf_section_data *esdo = elf_section_data (o);
12838 if ((o->flags & SEC_RELOC) != 0)
12840 if (esdo->rel.hdr
12841 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
12842 goto error_return;
12844 if (esdo->rela.hdr
12845 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
12846 goto error_return;
12849 /* _bfd_elf_compute_section_file_positions makes temporary use
12850 of target_index. Reset it. */
12851 o->target_index = 0;
12853 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12854 to count upwards while actually outputting the relocations. */
12855 esdo->rel.count = 0;
12856 esdo->rela.count = 0;
12858 if ((esdo->this_hdr.sh_offset == (file_ptr) -1)
12859 && !bfd_section_is_ctf (o))
12861 /* Cache the section contents so that they can be compressed
12862 later. Use bfd_malloc since it will be freed by
12863 bfd_compress_section_contents. */
12864 unsigned char *contents = esdo->this_hdr.contents;
12865 if (contents != NULL)
12866 abort ();
12867 contents
12868 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
12869 if (contents == NULL)
12870 goto error_return;
12871 esdo->this_hdr.contents = contents;
12875 /* We have now assigned file positions for all the sections except .symtab,
12876 .strtab, and non-loaded reloc and compressed debugging sections. We start
12877 the .symtab section at the current file position, and write directly to it.
12878 We build the .strtab section in memory. */
12879 abfd->symcount = 0;
12880 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12881 /* sh_name is set in prep_headers. */
12882 symtab_hdr->sh_type = SHT_SYMTAB;
12883 /* sh_flags, sh_addr and sh_size all start off zero. */
12884 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
12885 /* sh_link is set in assign_section_numbers. */
12886 /* sh_info is set below. */
12887 /* sh_offset is set just below. */
12888 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
12890 if (max_sym_count < 20)
12891 max_sym_count = 20;
12892 htab->strtabsize = max_sym_count;
12893 amt = max_sym_count * sizeof (struct elf_sym_strtab);
12894 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
12895 if (htab->strtab == NULL)
12896 goto error_return;
12897 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12898 flinfo.symshndxbuf
12899 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
12900 ? (Elf_External_Sym_Shndx *) -1 : NULL);
12902 if (info->strip != strip_all || emit_relocs)
12904 file_ptr off = elf_next_file_pos (abfd);
12906 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, true);
12908 /* Note that at this point elf_next_file_pos (abfd) is
12909 incorrect. We do not yet know the size of the .symtab section.
12910 We correct next_file_pos below, after we do know the size. */
12912 /* Start writing out the symbol table. The first symbol is always a
12913 dummy symbol. */
12914 elfsym.st_value = 0;
12915 elfsym.st_size = 0;
12916 elfsym.st_info = 0;
12917 elfsym.st_other = 0;
12918 elfsym.st_shndx = SHN_UNDEF;
12919 elfsym.st_target_internal = 0;
12920 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
12921 bfd_und_section_ptr, NULL) != 1)
12922 goto error_return;
12924 /* Output a symbol for each section if asked or they are used for
12925 relocs. These symbols usually have no names. We store the
12926 index of each one in the index field of the section, so that
12927 we can find it again when outputting relocs. */
12929 if (bfd_keep_unused_section_symbols (abfd) || emit_relocs)
12931 bool name_local_sections
12932 = (bed->elf_backend_name_local_section_symbols
12933 && bed->elf_backend_name_local_section_symbols (abfd));
12934 const char *name = NULL;
12936 elfsym.st_size = 0;
12937 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12938 elfsym.st_other = 0;
12939 elfsym.st_value = 0;
12940 elfsym.st_target_internal = 0;
12941 for (i = 1; i < elf_numsections (abfd); i++)
12943 o = bfd_section_from_elf_index (abfd, i);
12944 if (o != NULL)
12946 o->target_index = bfd_get_symcount (abfd);
12947 elfsym.st_shndx = i;
12948 if (!bfd_link_relocatable (info))
12949 elfsym.st_value = o->vma;
12950 if (name_local_sections)
12951 name = o->name;
12952 if (elf_link_output_symstrtab (&flinfo, name, &elfsym, o,
12953 NULL) != 1)
12954 goto error_return;
12960 /* On some targets like Irix 5 the symbol split between local and global
12961 ones recorded in the sh_info field needs to be done between section
12962 and all other symbols. */
12963 if (bed->elf_backend_elfsym_local_is_section
12964 && bed->elf_backend_elfsym_local_is_section (abfd))
12965 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12967 /* Allocate some memory to hold information read in from the input
12968 files. */
12969 if (max_contents_size != 0)
12971 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12972 if (flinfo.contents == NULL)
12973 goto error_return;
12976 if (max_external_reloc_size != 0)
12978 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
12979 if (flinfo.external_relocs == NULL)
12980 goto error_return;
12983 if (max_internal_reloc_count != 0)
12985 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
12986 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
12987 if (flinfo.internal_relocs == NULL)
12988 goto error_return;
12991 if (max_sym_count != 0)
12993 amt = max_sym_count * bed->s->sizeof_sym;
12994 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
12995 if (flinfo.external_syms == NULL)
12996 goto error_return;
12998 amt = max_sym_count * sizeof (Elf_Internal_Sym);
12999 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
13000 if (flinfo.internal_syms == NULL)
13001 goto error_return;
13003 amt = max_sym_count * sizeof (long);
13004 flinfo.indices = (long int *) bfd_malloc (amt);
13005 if (flinfo.indices == NULL)
13006 goto error_return;
13008 amt = max_sym_count * sizeof (asection *);
13009 flinfo.sections = (asection **) bfd_malloc (amt);
13010 if (flinfo.sections == NULL)
13011 goto error_return;
13014 if (max_sym_shndx_count != 0)
13016 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
13017 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
13018 if (flinfo.locsym_shndx == NULL)
13019 goto error_return;
13022 if (htab->tls_sec)
13024 bfd_vma base, end = 0; /* Both bytes. */
13025 asection *sec;
13027 for (sec = htab->tls_sec;
13028 sec && (sec->flags & SEC_THREAD_LOCAL);
13029 sec = sec->next)
13031 bfd_size_type size = sec->size;
13032 unsigned int opb = bfd_octets_per_byte (abfd, sec);
13034 if (size == 0
13035 && (sec->flags & SEC_HAS_CONTENTS) == 0)
13037 struct bfd_link_order *ord = sec->map_tail.link_order;
13039 if (ord != NULL)
13040 size = ord->offset * opb + ord->size;
13042 end = sec->vma + size / opb;
13044 base = htab->tls_sec->vma;
13045 /* Only align end of TLS section if static TLS doesn't have special
13046 alignment requirements. */
13047 if (bed->static_tls_alignment == 1)
13048 end = align_power (end, htab->tls_sec->alignment_power);
13049 htab->tls_size = end - base;
13052 if (!_bfd_elf_fixup_eh_frame_hdr (info))
13053 return false;
13055 /* Finish relative relocations here after regular symbol processing
13056 is finished if DT_RELR is enabled. */
13057 if (info->enable_dt_relr
13058 && bed->finish_relative_relocs
13059 && !bed->finish_relative_relocs (info))
13060 info->callbacks->einfo
13061 (_("%F%P: %pB: failed to finish relative relocations\n"), abfd);
13063 /* Since ELF permits relocations to be against local symbols, we
13064 must have the local symbols available when we do the relocations.
13065 Since we would rather only read the local symbols once, and we
13066 would rather not keep them in memory, we handle all the
13067 relocations for a single input file at the same time.
13069 Unfortunately, there is no way to know the total number of local
13070 symbols until we have seen all of them, and the local symbol
13071 indices precede the global symbol indices. This means that when
13072 we are generating relocatable output, and we see a reloc against
13073 a global symbol, we can not know the symbol index until we have
13074 finished examining all the local symbols to see which ones we are
13075 going to output. To deal with this, we keep the relocations in
13076 memory, and don't output them until the end of the link. This is
13077 an unfortunate waste of memory, but I don't see a good way around
13078 it. Fortunately, it only happens when performing a relocatable
13079 link, which is not the common case. FIXME: If keep_memory is set
13080 we could write the relocs out and then read them again; I don't
13081 know how bad the memory loss will be. */
13083 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13084 sub->output_has_begun = false;
13085 for (o = abfd->sections; o != NULL; o = o->next)
13087 for (p = o->map_head.link_order; p != NULL; p = p->next)
13089 if (p->type == bfd_indirect_link_order
13090 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
13091 == bfd_target_elf_flavour)
13092 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
13094 if (! sub->output_has_begun)
13096 if (! elf_link_input_bfd (&flinfo, sub))
13097 goto error_return;
13098 sub->output_has_begun = true;
13101 else if (p->type == bfd_section_reloc_link_order
13102 || p->type == bfd_symbol_reloc_link_order)
13104 if (! elf_reloc_link_order (abfd, info, o, p))
13105 goto error_return;
13107 else
13109 if (! _bfd_default_link_order (abfd, info, o, p))
13111 if (p->type == bfd_indirect_link_order
13112 && (bfd_get_flavour (sub)
13113 == bfd_target_elf_flavour)
13114 && (elf_elfheader (sub)->e_ident[EI_CLASS]
13115 != bed->s->elfclass))
13117 const char *iclass, *oclass;
13119 switch (bed->s->elfclass)
13121 case ELFCLASS64: oclass = "ELFCLASS64"; break;
13122 case ELFCLASS32: oclass = "ELFCLASS32"; break;
13123 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
13124 default: abort ();
13127 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
13129 case ELFCLASS64: iclass = "ELFCLASS64"; break;
13130 case ELFCLASS32: iclass = "ELFCLASS32"; break;
13131 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
13132 default: abort ();
13135 bfd_set_error (bfd_error_wrong_format);
13136 _bfd_error_handler
13137 /* xgettext:c-format */
13138 (_("%pB: file class %s incompatible with %s"),
13139 sub, iclass, oclass);
13142 goto error_return;
13148 /* Free symbol buffer if needed. */
13149 if (!info->reduce_memory_overheads)
13151 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13152 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
13154 free (elf_tdata (sub)->symbuf);
13155 elf_tdata (sub)->symbuf = NULL;
13159 ret = true;
13161 /* Output any global symbols that got converted to local in a
13162 version script or due to symbol visibility. We do this in a
13163 separate step since ELF requires all local symbols to appear
13164 prior to any global symbols. FIXME: We should only do this if
13165 some global symbols were, in fact, converted to become local.
13166 FIXME: Will this work correctly with the Irix 5 linker? */
13167 eoinfo.failed = false;
13168 eoinfo.flinfo = &flinfo;
13169 eoinfo.localsyms = true;
13170 eoinfo.file_sym_done = false;
13171 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
13172 if (eoinfo.failed)
13174 ret = false;
13175 goto return_local_hash_table;
13178 /* If backend needs to output some local symbols not present in the hash
13179 table, do it now. */
13180 if (bed->elf_backend_output_arch_local_syms)
13182 if (! ((*bed->elf_backend_output_arch_local_syms)
13183 (abfd, info, &flinfo, elf_link_output_symstrtab)))
13185 ret = false;
13186 goto return_local_hash_table;
13190 /* That wrote out all the local symbols. Finish up the symbol table
13191 with the global symbols. Even if we want to strip everything we
13192 can, we still need to deal with those global symbols that got
13193 converted to local in a version script. */
13195 /* The sh_info field records the index of the first non local symbol. */
13196 if (!symtab_hdr->sh_info)
13197 symtab_hdr->sh_info = bfd_get_symcount (abfd);
13199 if (dynamic
13200 && htab->dynsym != NULL
13201 && htab->dynsym->output_section != bfd_abs_section_ptr)
13203 Elf_Internal_Sym sym;
13204 bfd_byte *dynsym = htab->dynsym->contents;
13206 o = htab->dynsym->output_section;
13207 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
13209 /* Write out the section symbols for the output sections. */
13210 if (bfd_link_pic (info)
13211 || htab->is_relocatable_executable)
13213 asection *s;
13215 sym.st_size = 0;
13216 sym.st_name = 0;
13217 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
13218 sym.st_other = 0;
13219 sym.st_target_internal = 0;
13221 for (s = abfd->sections; s != NULL; s = s->next)
13223 int indx;
13224 bfd_byte *dest;
13225 long dynindx;
13227 dynindx = elf_section_data (s)->dynindx;
13228 if (dynindx <= 0)
13229 continue;
13230 indx = elf_section_data (s)->this_idx;
13231 BFD_ASSERT (indx > 0);
13232 sym.st_shndx = indx;
13233 if (! check_dynsym (abfd, &sym))
13235 ret = false;
13236 goto return_local_hash_table;
13238 sym.st_value = s->vma;
13239 dest = dynsym + dynindx * bed->s->sizeof_sym;
13241 /* Inform the linker of the addition of this symbol. */
13243 if (info->callbacks->ctf_new_dynsym)
13244 info->callbacks->ctf_new_dynsym (dynindx, &sym);
13246 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
13250 /* Write out the local dynsyms. */
13251 if (htab->dynlocal)
13253 struct elf_link_local_dynamic_entry *e;
13254 for (e = htab->dynlocal; e ; e = e->next)
13256 asection *s;
13257 bfd_byte *dest;
13259 /* Copy the internal symbol and turn off visibility.
13260 Note that we saved a word of storage and overwrote
13261 the original st_name with the dynstr_index. */
13262 sym = e->isym;
13263 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
13264 sym.st_shndx = SHN_UNDEF;
13266 s = bfd_section_from_elf_index (e->input_bfd,
13267 e->isym.st_shndx);
13268 if (s != NULL
13269 && s->output_section != NULL
13270 && elf_section_data (s->output_section) != NULL)
13272 sym.st_shndx =
13273 elf_section_data (s->output_section)->this_idx;
13274 if (! check_dynsym (abfd, &sym))
13276 ret = false;
13277 goto return_local_hash_table;
13279 sym.st_value = (s->output_section->vma
13280 + s->output_offset
13281 + e->isym.st_value);
13284 /* Inform the linker of the addition of this symbol. */
13286 if (info->callbacks->ctf_new_dynsym)
13287 info->callbacks->ctf_new_dynsym (e->dynindx, &sym);
13289 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
13290 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
13295 /* We get the global symbols from the hash table. */
13296 eoinfo.failed = false;
13297 eoinfo.localsyms = false;
13298 eoinfo.flinfo = &flinfo;
13299 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
13300 if (eoinfo.failed)
13302 ret = false;
13303 goto return_local_hash_table;
13306 /* If backend needs to output some symbols not present in the hash
13307 table, do it now. */
13308 if (bed->elf_backend_output_arch_syms
13309 && (info->strip != strip_all || emit_relocs))
13311 if (! ((*bed->elf_backend_output_arch_syms)
13312 (abfd, info, &flinfo, elf_link_output_symstrtab)))
13314 ret = false;
13315 goto return_local_hash_table;
13319 /* Finalize the .strtab section. */
13320 _bfd_elf_strtab_finalize (flinfo.symstrtab);
13322 /* Swap out the .strtab section. */
13323 if (!elf_link_swap_symbols_out (&flinfo))
13325 ret = false;
13326 goto return_local_hash_table;
13329 /* Now we know the size of the symtab section. */
13330 if (bfd_get_symcount (abfd) > 0)
13332 /* Finish up and write out the symbol string table (.strtab)
13333 section. */
13334 Elf_Internal_Shdr *symstrtab_hdr = NULL;
13335 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
13337 if (elf_symtab_shndx_list (abfd))
13339 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
13341 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
13343 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
13344 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
13345 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
13346 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
13347 symtab_shndx_hdr->sh_size = amt;
13349 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
13350 off, true);
13352 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
13353 || (bfd_write (flinfo.symshndxbuf, amt, abfd) != amt))
13355 ret = false;
13356 goto return_local_hash_table;
13361 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
13362 /* sh_name was set in prep_headers. */
13363 symstrtab_hdr->sh_type = SHT_STRTAB;
13364 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
13365 symstrtab_hdr->sh_addr = 0;
13366 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
13367 symstrtab_hdr->sh_entsize = 0;
13368 symstrtab_hdr->sh_link = 0;
13369 symstrtab_hdr->sh_info = 0;
13370 /* sh_offset is set just below. */
13371 symstrtab_hdr->sh_addralign = 1;
13373 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
13374 off, true);
13375 elf_next_file_pos (abfd) = off;
13377 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
13378 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
13380 ret = false;
13381 goto return_local_hash_table;
13385 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
13387 _bfd_error_handler (_("%pB: failed to generate import library"),
13388 info->out_implib_bfd);
13389 ret = false;
13390 goto return_local_hash_table;
13393 /* Adjust the relocs to have the correct symbol indices. */
13394 for (o = abfd->sections; o != NULL; o = o->next)
13396 struct bfd_elf_section_data *esdo = elf_section_data (o);
13397 bool sort;
13399 if ((o->flags & SEC_RELOC) == 0)
13400 continue;
13402 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
13403 if (esdo->rel.hdr != NULL
13404 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
13406 ret = false;
13407 goto return_local_hash_table;
13409 if (esdo->rela.hdr != NULL
13410 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
13412 ret = false;
13413 goto return_local_hash_table;
13416 /* Set the reloc_count field to 0 to prevent write_relocs from
13417 trying to swap the relocs out itself. */
13418 o->reloc_count = 0;
13421 relativecount = 0;
13422 if (dynamic && info->combreloc && dynobj != NULL)
13423 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
13425 relr_entsize = 0;
13426 if (htab->srelrdyn != NULL
13427 && htab->srelrdyn->output_section != NULL
13428 && htab->srelrdyn->size != 0)
13430 asection *s = htab->srelrdyn->output_section;
13431 relr_entsize = elf_section_data (s)->this_hdr.sh_entsize;
13432 if (relr_entsize == 0)
13434 relr_entsize = bed->s->arch_size / 8;
13435 elf_section_data (s)->this_hdr.sh_entsize = relr_entsize;
13439 /* If we are linking against a dynamic object, or generating a
13440 shared library, finish up the dynamic linking information. */
13441 if (dynamic)
13443 bfd_byte *dyncon, *dynconend;
13445 /* Fix up .dynamic entries. */
13446 o = htab->dynamic;
13447 BFD_ASSERT (o != NULL);
13449 dyncon = o->contents;
13450 dynconend = PTR_ADD (o->contents, o->size);
13451 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13453 Elf_Internal_Dyn dyn;
13454 const char *name;
13455 unsigned int type;
13456 bfd_size_type sh_size;
13457 bfd_vma sh_addr;
13459 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13461 switch (dyn.d_tag)
13463 default:
13464 continue;
13465 case DT_NULL:
13466 if (relativecount != 0)
13468 switch (elf_section_data (reldyn)->this_hdr.sh_type)
13470 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
13471 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
13473 if (dyn.d_tag != DT_NULL
13474 && dynconend - dyncon >= bed->s->sizeof_dyn)
13476 dyn.d_un.d_val = relativecount;
13477 relativecount = 0;
13478 break;
13480 relativecount = 0;
13482 if (relr_entsize != 0)
13484 if (dynconend - dyncon >= 3 * bed->s->sizeof_dyn)
13486 asection *s = htab->srelrdyn;
13487 dyn.d_tag = DT_RELR;
13488 dyn.d_un.d_ptr
13489 = s->output_section->vma + s->output_offset;
13490 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13491 dyncon += bed->s->sizeof_dyn;
13493 dyn.d_tag = DT_RELRSZ;
13494 dyn.d_un.d_val = s->size;
13495 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13496 dyncon += bed->s->sizeof_dyn;
13498 dyn.d_tag = DT_RELRENT;
13499 dyn.d_un.d_val = relr_entsize;
13500 relr_entsize = 0;
13501 break;
13503 relr_entsize = 0;
13505 continue;
13507 case DT_INIT:
13508 name = info->init_function;
13509 goto get_sym;
13510 case DT_FINI:
13511 name = info->fini_function;
13512 get_sym:
13514 struct elf_link_hash_entry *h;
13516 h = elf_link_hash_lookup (htab, name, false, false, true);
13517 if (h != NULL
13518 && (h->root.type == bfd_link_hash_defined
13519 || h->root.type == bfd_link_hash_defweak))
13521 dyn.d_un.d_ptr = h->root.u.def.value;
13522 o = h->root.u.def.section;
13523 if (o->output_section != NULL)
13524 dyn.d_un.d_ptr += (o->output_section->vma
13525 + o->output_offset);
13526 else
13528 /* The symbol is imported from another shared
13529 library and does not apply to this one. */
13530 dyn.d_un.d_ptr = 0;
13532 break;
13535 continue;
13537 case DT_PREINIT_ARRAYSZ:
13538 name = ".preinit_array";
13539 goto get_out_size;
13540 case DT_INIT_ARRAYSZ:
13541 name = ".init_array";
13542 goto get_out_size;
13543 case DT_FINI_ARRAYSZ:
13544 name = ".fini_array";
13545 get_out_size:
13546 o = bfd_get_section_by_name (abfd, name);
13547 if (o == NULL)
13549 _bfd_error_handler
13550 (_("could not find section %s"), name);
13551 goto error_return;
13553 if (o->size == 0)
13554 _bfd_error_handler
13555 (_("warning: %s section has zero size"), name);
13556 dyn.d_un.d_val = o->size;
13557 break;
13559 case DT_PREINIT_ARRAY:
13560 name = ".preinit_array";
13561 goto get_out_vma;
13562 case DT_INIT_ARRAY:
13563 name = ".init_array";
13564 goto get_out_vma;
13565 case DT_FINI_ARRAY:
13566 name = ".fini_array";
13567 get_out_vma:
13568 o = bfd_get_section_by_name (abfd, name);
13569 goto do_vma;
13571 case DT_HASH:
13572 name = ".hash";
13573 goto get_vma;
13574 case DT_GNU_HASH:
13575 name = ".gnu.hash";
13576 goto get_vma;
13577 case DT_STRTAB:
13578 name = ".dynstr";
13579 goto get_vma;
13580 case DT_SYMTAB:
13581 name = ".dynsym";
13582 goto get_vma;
13583 case DT_VERDEF:
13584 name = ".gnu.version_d";
13585 goto get_vma;
13586 case DT_VERNEED:
13587 name = ".gnu.version_r";
13588 goto get_vma;
13589 case DT_VERSYM:
13590 name = ".gnu.version";
13591 get_vma:
13592 o = bfd_get_linker_section (dynobj, name);
13593 do_vma:
13594 if (o == NULL || bfd_is_abs_section (o->output_section))
13596 _bfd_error_handler
13597 (_("could not find section %s"), name);
13598 goto error_return;
13600 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
13602 _bfd_error_handler
13603 (_("warning: section '%s' is being made into a note"), name);
13604 bfd_set_error (bfd_error_nonrepresentable_section);
13605 goto error_return;
13607 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
13608 break;
13610 case DT_REL:
13611 case DT_RELA:
13612 case DT_RELSZ:
13613 case DT_RELASZ:
13614 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
13615 type = SHT_REL;
13616 else
13617 type = SHT_RELA;
13618 sh_size = 0;
13619 sh_addr = 0;
13620 for (i = 1; i < elf_numsections (abfd); i++)
13622 Elf_Internal_Shdr *hdr;
13624 hdr = elf_elfsections (abfd)[i];
13625 if (hdr->sh_type == type
13626 && (hdr->sh_flags & SHF_ALLOC) != 0)
13628 sh_size += hdr->sh_size;
13629 if (sh_addr == 0
13630 || sh_addr > hdr->sh_addr)
13631 sh_addr = hdr->sh_addr;
13635 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
13637 unsigned int opb = bfd_octets_per_byte (abfd, o);
13639 /* Don't count procedure linkage table relocs in the
13640 overall reloc count. */
13641 sh_size -= htab->srelplt->size;
13642 if (sh_size == 0)
13643 /* If the size is zero, make the address zero too.
13644 This is to avoid a glibc bug. If the backend
13645 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
13646 zero, then we'll put DT_RELA at the end of
13647 DT_JMPREL. glibc will interpret the end of
13648 DT_RELA matching the end of DT_JMPREL as the
13649 case where DT_RELA includes DT_JMPREL, and for
13650 LD_BIND_NOW will decide that processing DT_RELA
13651 will process the PLT relocs too. Net result:
13652 No PLT relocs applied. */
13653 sh_addr = 0;
13655 /* If .rela.plt is the first .rela section, exclude
13656 it from DT_RELA. */
13657 else if (sh_addr == (htab->srelplt->output_section->vma
13658 + htab->srelplt->output_offset) * opb)
13659 sh_addr += htab->srelplt->size;
13662 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
13663 dyn.d_un.d_val = sh_size;
13664 else
13665 dyn.d_un.d_ptr = sh_addr;
13666 break;
13668 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13672 /* If we have created any dynamic sections, then output them. */
13673 if (dynobj != NULL)
13675 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
13676 goto error_return;
13678 /* Check for DT_TEXTREL (late, in case the backend removes it). */
13679 if (bfd_link_textrel_check (info)
13680 && (o = htab->dynamic) != NULL
13681 && o->size != 0)
13683 bfd_byte *dyncon, *dynconend;
13685 dyncon = o->contents;
13686 dynconend = o->contents + o->size;
13687 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13689 Elf_Internal_Dyn dyn;
13691 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13693 if (dyn.d_tag == DT_TEXTREL)
13695 if (info->textrel_check == textrel_check_error)
13696 info->callbacks->einfo
13697 (_("%P%X: read-only segment has dynamic relocations\n"));
13698 else if (bfd_link_dll (info))
13699 info->callbacks->einfo
13700 (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
13701 else if (bfd_link_pde (info))
13702 info->callbacks->einfo
13703 (_("%P: warning: creating DT_TEXTREL in a PDE\n"));
13704 else
13705 info->callbacks->einfo
13706 (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
13707 break;
13712 for (o = dynobj->sections; o != NULL; o = o->next)
13714 if ((o->flags & SEC_HAS_CONTENTS) == 0
13715 || o->size == 0
13716 || o->output_section == bfd_abs_section_ptr)
13717 continue;
13718 if ((o->flags & SEC_LINKER_CREATED) == 0)
13720 /* At this point, we are only interested in sections
13721 created by _bfd_elf_link_create_dynamic_sections. */
13722 continue;
13724 if (htab->stab_info.stabstr == o)
13725 continue;
13726 if (htab->eh_info.hdr_sec == o)
13727 continue;
13728 if (strcmp (o->name, ".dynstr") != 0)
13730 bfd_size_type octets = ((file_ptr) o->output_offset
13731 * bfd_octets_per_byte (abfd, o));
13732 if (!bfd_set_section_contents (abfd, o->output_section,
13733 o->contents, octets, o->size))
13734 goto error_return;
13736 else
13738 /* The contents of the .dynstr section are actually in a
13739 stringtab. */
13740 file_ptr off;
13742 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
13743 if (bfd_seek (abfd, off, SEEK_SET) != 0
13744 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
13745 goto error_return;
13750 if (!info->resolve_section_groups)
13752 bool failed = false;
13754 BFD_ASSERT (bfd_link_relocatable (info));
13755 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
13756 if (failed)
13757 goto error_return;
13760 /* If we have optimized stabs strings, output them. */
13761 if (htab->stab_info.stabstr != NULL)
13763 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
13764 goto error_return;
13767 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
13768 goto error_return;
13770 if (! _bfd_elf_write_section_sframe (abfd, info))
13771 goto error_return;
13773 if (info->callbacks->emit_ctf)
13774 info->callbacks->emit_ctf ();
13776 elf_final_link_free (abfd, &flinfo);
13778 if (attr_section)
13780 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
13781 if (contents == NULL)
13783 /* Bail out and fail. */
13784 ret = false;
13785 goto return_local_hash_table;
13787 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
13788 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
13789 free (contents);
13792 return_local_hash_table:
13793 if (info->unique_symbol)
13794 bfd_hash_table_free (&flinfo.local_hash_table);
13795 return ret;
13797 error_return:
13798 elf_final_link_free (abfd, &flinfo);
13799 ret = false;
13800 goto return_local_hash_table;
13803 /* Initialize COOKIE for input bfd ABFD. */
13805 static bool
13806 init_reloc_cookie (struct elf_reloc_cookie *cookie,
13807 struct bfd_link_info *info, bfd *abfd,
13808 bool keep_memory)
13810 Elf_Internal_Shdr *symtab_hdr;
13811 const struct elf_backend_data *bed;
13813 bed = get_elf_backend_data (abfd);
13814 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13816 cookie->abfd = abfd;
13817 cookie->sym_hashes = elf_sym_hashes (abfd);
13818 cookie->bad_symtab = elf_bad_symtab (abfd);
13819 if (cookie->bad_symtab)
13821 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13822 cookie->extsymoff = 0;
13824 else
13826 cookie->locsymcount = symtab_hdr->sh_info;
13827 cookie->extsymoff = symtab_hdr->sh_info;
13830 if (bed->s->arch_size == 32)
13831 cookie->r_sym_shift = 8;
13832 else
13833 cookie->r_sym_shift = 32;
13835 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
13836 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
13838 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
13839 cookie->locsymcount, 0,
13840 NULL, NULL, NULL);
13841 if (cookie->locsyms == NULL)
13843 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
13844 return false;
13846 if (keep_memory || _bfd_elf_link_keep_memory (info))
13848 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
13849 info->cache_size += (cookie->locsymcount
13850 * sizeof (Elf_External_Sym_Shndx));
13853 return true;
13856 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
13858 static void
13859 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
13861 Elf_Internal_Shdr *symtab_hdr;
13863 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13864 if (symtab_hdr->contents != (unsigned char *) cookie->locsyms)
13865 free (cookie->locsyms);
13868 /* Initialize the relocation information in COOKIE for input section SEC
13869 of input bfd ABFD. */
13871 static bool
13872 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13873 struct bfd_link_info *info, bfd *abfd,
13874 asection *sec, bool keep_memory)
13876 if (sec->reloc_count == 0)
13878 cookie->rels = NULL;
13879 cookie->relend = NULL;
13881 else
13883 cookie->rels = _bfd_elf_link_info_read_relocs
13884 (abfd, info, sec, NULL, NULL,
13885 keep_memory || _bfd_elf_link_keep_memory (info));
13886 if (cookie->rels == NULL)
13887 return false;
13888 cookie->rel = cookie->rels;
13889 cookie->relend = cookie->rels + sec->reloc_count;
13891 cookie->rel = cookie->rels;
13892 return true;
13895 /* Free the memory allocated by init_reloc_cookie_rels,
13896 if appropriate. */
13898 static void
13899 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13900 asection *sec)
13902 if (elf_section_data (sec)->relocs != cookie->rels)
13903 free (cookie->rels);
13906 /* Initialize the whole of COOKIE for input section SEC. */
13908 static bool
13909 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13910 struct bfd_link_info *info,
13911 asection *sec, bool keep_memory)
13913 if (!init_reloc_cookie (cookie, info, sec->owner, keep_memory))
13914 goto error1;
13915 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec,
13916 keep_memory))
13917 goto error2;
13918 return true;
13920 error2:
13921 fini_reloc_cookie (cookie, sec->owner);
13922 error1:
13923 return false;
13926 /* Free the memory allocated by init_reloc_cookie_for_section,
13927 if appropriate. */
13929 static void
13930 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13931 asection *sec)
13933 fini_reloc_cookie_rels (cookie, sec);
13934 fini_reloc_cookie (cookie, sec->owner);
13937 /* Garbage collect unused sections. */
13939 /* Default gc_mark_hook. */
13941 asection *
13942 _bfd_elf_gc_mark_hook (asection *sec,
13943 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13944 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13945 struct elf_link_hash_entry *h,
13946 Elf_Internal_Sym *sym)
13948 if (h != NULL)
13950 switch (h->root.type)
13952 case bfd_link_hash_defined:
13953 case bfd_link_hash_defweak:
13954 return h->root.u.def.section;
13956 case bfd_link_hash_common:
13957 return h->root.u.c.p->section;
13959 default:
13960 break;
13963 else
13964 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
13966 return NULL;
13969 /* Return the debug definition section. */
13971 static asection *
13972 elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
13973 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13974 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13975 struct elf_link_hash_entry *h,
13976 Elf_Internal_Sym *sym)
13978 if (h != NULL)
13980 /* Return the global debug definition section. */
13981 if ((h->root.type == bfd_link_hash_defined
13982 || h->root.type == bfd_link_hash_defweak)
13983 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
13984 return h->root.u.def.section;
13986 else
13988 /* Return the local debug definition section. */
13989 asection *isec = bfd_section_from_elf_index (sec->owner,
13990 sym->st_shndx);
13991 if (isec != NULL && (isec->flags & SEC_DEBUGGING) != 0)
13992 return isec;
13995 return NULL;
13998 /* COOKIE->rel describes a relocation against section SEC, which is
13999 a section we've decided to keep. Return the section that contains
14000 the relocation symbol, or NULL if no section contains it. */
14002 asection *
14003 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
14004 elf_gc_mark_hook_fn gc_mark_hook,
14005 struct elf_reloc_cookie *cookie,
14006 bool *start_stop)
14008 unsigned long r_symndx;
14009 struct elf_link_hash_entry *h, *hw;
14011 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
14012 if (r_symndx == STN_UNDEF)
14013 return NULL;
14015 if (r_symndx >= cookie->locsymcount
14016 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
14018 bool was_marked;
14020 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
14021 if (h == NULL)
14023 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
14024 sec->owner);
14025 return NULL;
14027 while (h->root.type == bfd_link_hash_indirect
14028 || h->root.type == bfd_link_hash_warning)
14029 h = (struct elf_link_hash_entry *) h->root.u.i.link;
14031 was_marked = h->mark;
14032 h->mark = 1;
14033 /* Keep all aliases of the symbol too. If an object symbol
14034 needs to be copied into .dynbss then all of its aliases
14035 should be present as dynamic symbols, not just the one used
14036 on the copy relocation. */
14037 hw = h;
14038 while (hw->is_weakalias)
14040 hw = hw->u.alias;
14041 hw->mark = 1;
14044 if (!was_marked && h->start_stop && !h->root.ldscript_def)
14046 if (info->start_stop_gc)
14047 return NULL;
14049 /* To work around a glibc bug, mark XXX input sections
14050 when there is a reference to __start_XXX or __stop_XXX
14051 symbols. */
14052 else if (start_stop != NULL)
14054 asection *s = h->u2.start_stop_section;
14055 *start_stop = true;
14056 return s;
14060 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
14063 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
14064 &cookie->locsyms[r_symndx]);
14067 /* COOKIE->rel describes a relocation against section SEC, which is
14068 a section we've decided to keep. Mark the section that contains
14069 the relocation symbol. */
14071 bool
14072 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
14073 asection *sec,
14074 elf_gc_mark_hook_fn gc_mark_hook,
14075 struct elf_reloc_cookie *cookie)
14077 asection *rsec;
14078 bool start_stop = false;
14080 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
14081 while (rsec != NULL)
14083 if (!rsec->gc_mark)
14085 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
14086 || (rsec->owner->flags & DYNAMIC) != 0)
14087 rsec->gc_mark = 1;
14088 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
14089 return false;
14091 if (!start_stop)
14092 break;
14093 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
14095 return true;
14098 /* The mark phase of garbage collection. For a given section, mark
14099 it and any sections in this section's group, and all the sections
14100 which define symbols to which it refers. */
14102 bool
14103 _bfd_elf_gc_mark (struct bfd_link_info *info,
14104 asection *sec,
14105 elf_gc_mark_hook_fn gc_mark_hook)
14107 bool ret;
14108 asection *group_sec, *eh_frame;
14110 sec->gc_mark = 1;
14112 /* Mark all the sections in the group. */
14113 group_sec = elf_section_data (sec)->next_in_group;
14114 if (group_sec && !group_sec->gc_mark)
14115 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
14116 return false;
14118 /* Look through the section relocs. */
14119 ret = true;
14120 eh_frame = elf_eh_frame_section (sec->owner);
14121 if ((sec->flags & SEC_RELOC) != 0
14122 && sec->reloc_count > 0
14123 && sec != eh_frame)
14125 struct elf_reloc_cookie cookie;
14127 if (!init_reloc_cookie_for_section (&cookie, info, sec, false))
14128 ret = false;
14129 else
14131 for (; cookie.rel < cookie.relend; cookie.rel++)
14132 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
14134 ret = false;
14135 break;
14137 fini_reloc_cookie_for_section (&cookie, sec);
14141 if (ret && eh_frame && elf_fde_list (sec))
14143 struct elf_reloc_cookie cookie;
14145 /* NB: When --no-keep-memory is used, the symbol table and
14146 relocation info for eh_frame are freed after they are retrieved
14147 for each text section in the input object. If an input object
14148 has many text sections, the same data is retrieved and freed
14149 many times which can take a very long time. Always keep the
14150 symbol table and relocation info for eh_frame to avoid it. */
14151 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame,
14152 true))
14153 ret = false;
14154 else
14156 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
14157 gc_mark_hook, &cookie))
14158 ret = false;
14159 fini_reloc_cookie_for_section (&cookie, eh_frame);
14163 eh_frame = elf_section_eh_frame_entry (sec);
14164 if (ret && eh_frame && !eh_frame->gc_mark)
14165 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
14166 ret = false;
14168 return ret;
14171 /* Scan and mark sections in a special or debug section group. */
14173 static void
14174 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
14176 /* Point to first section of section group. */
14177 asection *ssec;
14178 /* Used to iterate the section group. */
14179 asection *msec;
14181 bool is_special_grp = true;
14182 bool is_debug_grp = true;
14184 /* First scan to see if group contains any section other than debug
14185 and special section. */
14186 ssec = msec = elf_next_in_group (grp);
14189 if ((msec->flags & SEC_DEBUGGING) == 0)
14190 is_debug_grp = false;
14192 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
14193 is_special_grp = false;
14195 msec = elf_next_in_group (msec);
14197 while (msec != ssec);
14199 /* If this is a pure debug section group or pure special section group,
14200 keep all sections in this group. */
14201 if (is_debug_grp || is_special_grp)
14205 msec->gc_mark = 1;
14206 msec = elf_next_in_group (msec);
14208 while (msec != ssec);
14212 /* Keep debug and special sections. */
14214 bool
14215 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
14216 elf_gc_mark_hook_fn mark_hook)
14218 bfd *ibfd;
14220 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14222 asection *isec;
14223 bool some_kept;
14224 bool debug_frag_seen;
14225 bool has_kept_debug_info;
14227 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
14228 continue;
14229 isec = ibfd->sections;
14230 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14231 continue;
14233 /* Ensure all linker created sections are kept,
14234 see if any other section is already marked,
14235 and note if we have any fragmented debug sections. */
14236 debug_frag_seen = some_kept = has_kept_debug_info = false;
14237 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14239 if ((isec->flags & SEC_LINKER_CREATED) != 0)
14240 isec->gc_mark = 1;
14241 else if (isec->gc_mark
14242 && (isec->flags & SEC_ALLOC) != 0
14243 && elf_section_type (isec) != SHT_NOTE)
14244 some_kept = true;
14245 else
14247 /* Since all sections, except for backend specific ones,
14248 have been garbage collected, call mark_hook on this
14249 section if any of its linked-to sections is marked. */
14250 asection *linked_to_sec;
14251 for (linked_to_sec = elf_linked_to_section (isec);
14252 linked_to_sec != NULL && !linked_to_sec->linker_mark;
14253 linked_to_sec = elf_linked_to_section (linked_to_sec))
14255 if (linked_to_sec->gc_mark)
14257 if (!_bfd_elf_gc_mark (info, isec, mark_hook))
14258 return false;
14259 break;
14261 linked_to_sec->linker_mark = 1;
14263 for (linked_to_sec = elf_linked_to_section (isec);
14264 linked_to_sec != NULL && linked_to_sec->linker_mark;
14265 linked_to_sec = elf_linked_to_section (linked_to_sec))
14266 linked_to_sec->linker_mark = 0;
14269 if (!debug_frag_seen
14270 && (isec->flags & SEC_DEBUGGING)
14271 && startswith (isec->name, ".debug_line."))
14272 debug_frag_seen = true;
14273 else if (strcmp (bfd_section_name (isec),
14274 "__patchable_function_entries") == 0
14275 && elf_linked_to_section (isec) == NULL)
14276 info->callbacks->einfo (_("%F%P: %pB(%pA): error: "
14277 "need linked-to section "
14278 "for --gc-sections\n"),
14279 isec->owner, isec);
14282 /* If no non-note alloc section in this file will be kept, then
14283 we can toss out the debug and special sections. */
14284 if (!some_kept)
14285 continue;
14287 /* Keep debug and special sections like .comment when they are
14288 not part of a group. Also keep section groups that contain
14289 just debug sections or special sections. NB: Sections with
14290 linked-to section has been handled above. */
14291 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14293 if ((isec->flags & SEC_GROUP) != 0)
14294 _bfd_elf_gc_mark_debug_special_section_group (isec);
14295 else if (((isec->flags & SEC_DEBUGGING) != 0
14296 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
14297 && elf_next_in_group (isec) == NULL
14298 && elf_linked_to_section (isec) == NULL)
14299 isec->gc_mark = 1;
14300 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
14301 has_kept_debug_info = true;
14304 /* Look for CODE sections which are going to be discarded,
14305 and find and discard any fragmented debug sections which
14306 are associated with that code section. */
14307 if (debug_frag_seen)
14308 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14309 if ((isec->flags & SEC_CODE) != 0
14310 && isec->gc_mark == 0)
14312 unsigned int ilen;
14313 asection *dsec;
14315 ilen = strlen (isec->name);
14317 /* Association is determined by the name of the debug
14318 section containing the name of the code section as
14319 a suffix. For example .debug_line.text.foo is a
14320 debug section associated with .text.foo. */
14321 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
14323 unsigned int dlen;
14325 if (dsec->gc_mark == 0
14326 || (dsec->flags & SEC_DEBUGGING) == 0)
14327 continue;
14329 dlen = strlen (dsec->name);
14331 if (dlen > ilen
14332 && strncmp (dsec->name + (dlen - ilen),
14333 isec->name, ilen) == 0)
14334 dsec->gc_mark = 0;
14338 /* Mark debug sections referenced by kept debug sections. */
14339 if (has_kept_debug_info)
14340 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14341 if (isec->gc_mark
14342 && (isec->flags & SEC_DEBUGGING) != 0)
14343 if (!_bfd_elf_gc_mark (info, isec,
14344 elf_gc_mark_debug_section))
14345 return false;
14347 return true;
14350 static bool
14351 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
14353 bfd *sub;
14354 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14356 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
14358 asection *o;
14360 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
14361 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
14362 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
14363 continue;
14364 o = sub->sections;
14365 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14366 continue;
14368 for (o = sub->sections; o != NULL; o = o->next)
14370 /* When any section in a section group is kept, we keep all
14371 sections in the section group. If the first member of
14372 the section group is excluded, we will also exclude the
14373 group section. */
14374 if (o->flags & SEC_GROUP)
14376 asection *first = elf_next_in_group (o);
14377 o->gc_mark = first->gc_mark;
14380 if (o->gc_mark)
14381 continue;
14383 /* Skip sweeping sections already excluded. */
14384 if (o->flags & SEC_EXCLUDE)
14385 continue;
14387 /* Since this is early in the link process, it is simple
14388 to remove a section from the output. */
14389 o->flags |= SEC_EXCLUDE;
14391 if (info->print_gc_sections && o->size != 0)
14392 /* xgettext:c-format */
14393 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
14394 o, sub);
14398 return true;
14401 /* Propagate collected vtable information. This is called through
14402 elf_link_hash_traverse. */
14404 static bool
14405 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
14407 /* Those that are not vtables. */
14408 if (h->start_stop
14409 || h->u2.vtable == NULL
14410 || h->u2.vtable->parent == NULL)
14411 return true;
14413 /* Those vtables that do not have parents, we cannot merge. */
14414 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
14415 return true;
14417 /* If we've already been done, exit. */
14418 if (h->u2.vtable->used && h->u2.vtable->used[-1])
14419 return true;
14421 /* Make sure the parent's table is up to date. */
14422 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
14424 if (h->u2.vtable->used == NULL)
14426 /* None of this table's entries were referenced. Re-use the
14427 parent's table. */
14428 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
14429 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
14431 else
14433 size_t n;
14434 bool *cu, *pu;
14436 /* Or the parent's entries into ours. */
14437 cu = h->u2.vtable->used;
14438 cu[-1] = true;
14439 pu = h->u2.vtable->parent->u2.vtable->used;
14440 if (pu != NULL)
14442 const struct elf_backend_data *bed;
14443 unsigned int log_file_align;
14445 bed = get_elf_backend_data (h->root.u.def.section->owner);
14446 log_file_align = bed->s->log_file_align;
14447 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
14448 while (n--)
14450 if (*pu)
14451 *cu = true;
14452 pu++;
14453 cu++;
14458 return true;
14461 struct link_info_ok
14463 struct bfd_link_info *info;
14464 bool ok;
14467 static bool
14468 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h,
14469 void *ptr)
14471 asection *sec;
14472 bfd_vma hstart, hend;
14473 Elf_Internal_Rela *relstart, *relend, *rel;
14474 const struct elf_backend_data *bed;
14475 unsigned int log_file_align;
14476 struct link_info_ok *info = (struct link_info_ok *) ptr;
14478 /* Take care of both those symbols that do not describe vtables as
14479 well as those that are not loaded. */
14480 if (h->start_stop
14481 || h->u2.vtable == NULL
14482 || h->u2.vtable->parent == NULL)
14483 return true;
14485 BFD_ASSERT (h->root.type == bfd_link_hash_defined
14486 || h->root.type == bfd_link_hash_defweak);
14488 sec = h->root.u.def.section;
14489 hstart = h->root.u.def.value;
14490 hend = hstart + h->size;
14492 relstart = _bfd_elf_link_info_read_relocs (sec->owner, info->info,
14493 sec, NULL, NULL, true);
14494 if (!relstart)
14495 return info->ok = false;
14496 bed = get_elf_backend_data (sec->owner);
14497 log_file_align = bed->s->log_file_align;
14499 relend = relstart + sec->reloc_count;
14501 for (rel = relstart; rel < relend; ++rel)
14502 if (rel->r_offset >= hstart && rel->r_offset < hend)
14504 /* If the entry is in use, do nothing. */
14505 if (h->u2.vtable->used
14506 && (rel->r_offset - hstart) < h->u2.vtable->size)
14508 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
14509 if (h->u2.vtable->used[entry])
14510 continue;
14512 /* Otherwise, kill it. */
14513 rel->r_offset = rel->r_info = rel->r_addend = 0;
14516 return true;
14519 /* Mark sections containing dynamically referenced symbols. When
14520 building shared libraries, we must assume that any visible symbol is
14521 referenced. */
14523 bool
14524 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
14526 struct bfd_link_info *info = (struct bfd_link_info *) inf;
14527 struct bfd_elf_dynamic_list *d = info->dynamic_list;
14529 if ((h->root.type == bfd_link_hash_defined
14530 || h->root.type == bfd_link_hash_defweak)
14531 && (!h->start_stop
14532 || h->root.ldscript_def
14533 || !info->start_stop_gc)
14534 && ((h->ref_dynamic && !h->forced_local)
14535 || ((h->def_regular || ELF_COMMON_DEF_P (h))
14536 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
14537 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
14538 && (!bfd_link_executable (info)
14539 || info->gc_keep_exported
14540 || info->export_dynamic
14541 || (h->dynamic
14542 && d != NULL
14543 && (*d->match) (&d->head, NULL, h->root.root.string)))
14544 && (h->versioned >= versioned
14545 || !bfd_hide_sym_by_version (info->version_info,
14546 h->root.root.string)))))
14547 h->root.u.def.section->flags |= SEC_KEEP;
14549 return true;
14552 /* Keep all sections containing symbols undefined on the command-line,
14553 and the section containing the entry symbol. */
14555 void
14556 _bfd_elf_gc_keep (struct bfd_link_info *info)
14558 struct bfd_sym_chain *sym;
14560 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
14562 struct elf_link_hash_entry *h;
14564 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
14565 false, false, false);
14567 if (h != NULL
14568 && (h->root.type == bfd_link_hash_defined
14569 || h->root.type == bfd_link_hash_defweak)
14570 && !bfd_is_const_section (h->root.u.def.section))
14571 h->root.u.def.section->flags |= SEC_KEEP;
14575 bool
14576 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
14577 struct bfd_link_info *info)
14579 bfd *ibfd = info->input_bfds;
14581 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14583 asection *sec;
14584 struct elf_reloc_cookie cookie;
14586 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
14587 continue;
14588 sec = ibfd->sections;
14589 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14590 continue;
14592 if (!init_reloc_cookie (&cookie, info, ibfd, false))
14593 return false;
14595 for (sec = ibfd->sections; sec; sec = sec->next)
14597 if (startswith (bfd_section_name (sec), ".eh_frame_entry")
14598 && init_reloc_cookie_rels (&cookie, info, ibfd, sec,
14599 false))
14601 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
14602 fini_reloc_cookie_rels (&cookie, sec);
14606 return true;
14609 /* Do mark and sweep of unused sections. */
14611 bool
14612 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
14614 bool ok = true;
14615 bfd *sub;
14616 elf_gc_mark_hook_fn gc_mark_hook;
14617 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14618 struct elf_link_hash_table *htab;
14619 struct link_info_ok info_ok;
14621 if (!bed->can_gc_sections
14622 || !is_elf_hash_table (info->hash))
14624 _bfd_error_handler(_("warning: gc-sections option ignored"));
14625 return true;
14628 bed->gc_keep (info);
14629 htab = elf_hash_table (info);
14631 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
14632 at the .eh_frame section if we can mark the FDEs individually. */
14633 for (sub = info->input_bfds;
14634 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
14635 sub = sub->link.next)
14637 asection *sec;
14638 struct elf_reloc_cookie cookie;
14640 sec = sub->sections;
14641 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14642 continue;
14643 sec = bfd_get_section_by_name (sub, ".eh_frame");
14644 while (sec && init_reloc_cookie_for_section (&cookie, info, sec,
14645 false))
14647 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
14648 if (elf_section_data (sec)->sec_info
14649 && (sec->flags & SEC_LINKER_CREATED) == 0)
14650 elf_eh_frame_section (sub) = sec;
14651 fini_reloc_cookie_for_section (&cookie, sec);
14652 sec = bfd_get_next_section_by_name (NULL, sec);
14656 /* Apply transitive closure to the vtable entry usage info. */
14657 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
14658 if (!ok)
14659 return false;
14661 /* Kill the vtable relocations that were not used. */
14662 info_ok.info = info;
14663 info_ok.ok = true;
14664 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &info_ok);
14665 if (!info_ok.ok)
14666 return false;
14668 /* Mark dynamically referenced symbols. */
14669 if (htab->dynamic_sections_created || info->gc_keep_exported)
14670 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
14672 /* Grovel through relocs to find out who stays ... */
14673 gc_mark_hook = bed->gc_mark_hook;
14674 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
14676 asection *o;
14678 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
14679 || elf_object_id (sub) != elf_hash_table_id (htab)
14680 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
14681 continue;
14683 o = sub->sections;
14684 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14685 continue;
14687 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
14688 Also treat note sections as a root, if the section is not part
14689 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
14690 well as FINI_ARRAY sections for ld -r. */
14691 for (o = sub->sections; o != NULL; o = o->next)
14692 if (!o->gc_mark
14693 && (o->flags & SEC_EXCLUDE) == 0
14694 && ((o->flags & SEC_KEEP) != 0
14695 || (bfd_link_relocatable (info)
14696 && ((elf_section_data (o)->this_hdr.sh_type
14697 == SHT_PREINIT_ARRAY)
14698 || (elf_section_data (o)->this_hdr.sh_type
14699 == SHT_INIT_ARRAY)
14700 || (elf_section_data (o)->this_hdr.sh_type
14701 == SHT_FINI_ARRAY)))
14702 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
14703 && elf_next_in_group (o) == NULL
14704 && elf_linked_to_section (o) == NULL)
14705 || ((elf_tdata (sub)->has_gnu_osabi & elf_gnu_osabi_retain)
14706 && (elf_section_flags (o) & SHF_GNU_RETAIN))))
14708 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
14709 return false;
14713 /* Allow the backend to mark additional target specific sections. */
14714 bed->gc_mark_extra_sections (info, gc_mark_hook);
14716 /* ... and mark SEC_EXCLUDE for those that go. */
14717 return elf_gc_sweep (abfd, info);
14720 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
14722 bool
14723 bfd_elf_gc_record_vtinherit (bfd *abfd,
14724 asection *sec,
14725 struct elf_link_hash_entry *h,
14726 bfd_vma offset)
14728 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
14729 struct elf_link_hash_entry **search, *child;
14730 size_t extsymcount;
14731 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14733 /* The sh_info field of the symtab header tells us where the
14734 external symbols start. We don't care about the local symbols at
14735 this point. */
14736 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
14737 if (!elf_bad_symtab (abfd))
14738 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
14740 sym_hashes = elf_sym_hashes (abfd);
14741 sym_hashes_end = PTR_ADD (sym_hashes, extsymcount);
14743 /* Hunt down the child symbol, which is in this section at the same
14744 offset as the relocation. */
14745 for (search = sym_hashes; search != sym_hashes_end; ++search)
14747 if ((child = *search) != NULL
14748 && (child->root.type == bfd_link_hash_defined
14749 || child->root.type == bfd_link_hash_defweak)
14750 && child->root.u.def.section == sec
14751 && child->root.u.def.value == offset)
14752 goto win;
14755 /* xgettext:c-format */
14756 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
14757 abfd, sec, (uint64_t) offset);
14758 bfd_set_error (bfd_error_invalid_operation);
14759 return false;
14761 win:
14762 if (!child->u2.vtable)
14764 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
14765 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
14766 if (!child->u2.vtable)
14767 return false;
14769 if (!h)
14771 /* This *should* only be the absolute section. It could potentially
14772 be that someone has defined a non-global vtable though, which
14773 would be bad. It isn't worth paging in the local symbols to be
14774 sure though; that case should simply be handled by the assembler. */
14776 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
14778 else
14779 child->u2.vtable->parent = h;
14781 return true;
14784 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
14786 bool
14787 bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec,
14788 struct elf_link_hash_entry *h,
14789 bfd_vma addend)
14791 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14792 unsigned int log_file_align = bed->s->log_file_align;
14794 if (!h)
14796 /* xgettext:c-format */
14797 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
14798 abfd, sec);
14799 bfd_set_error (bfd_error_bad_value);
14800 return false;
14803 if (!h->u2.vtable)
14805 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
14806 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
14807 if (!h->u2.vtable)
14808 return false;
14811 if (addend >= h->u2.vtable->size)
14813 size_t size, bytes, file_align;
14814 bool *ptr = h->u2.vtable->used;
14816 /* While the symbol is undefined, we have to be prepared to handle
14817 a zero size. */
14818 file_align = 1 << log_file_align;
14819 if (h->root.type == bfd_link_hash_undefined)
14820 size = addend + file_align;
14821 else
14823 size = h->size;
14824 if (addend >= size)
14826 /* Oops! We've got a reference past the defined end of
14827 the table. This is probably a bug -- shall we warn? */
14828 size = addend + file_align;
14831 size = (size + file_align - 1) & -file_align;
14833 /* Allocate one extra entry for use as a "done" flag for the
14834 consolidation pass. */
14835 bytes = ((size >> log_file_align) + 1) * sizeof (bool);
14837 if (ptr)
14839 ptr = (bool *) bfd_realloc (ptr - 1, bytes);
14841 if (ptr != NULL)
14843 size_t oldbytes;
14845 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
14846 * sizeof (bool));
14847 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
14850 else
14851 ptr = (bool *) bfd_zmalloc (bytes);
14853 if (ptr == NULL)
14854 return false;
14856 /* And arrange for that done flag to be at index -1. */
14857 h->u2.vtable->used = ptr + 1;
14858 h->u2.vtable->size = size;
14861 h->u2.vtable->used[addend >> log_file_align] = true;
14863 return true;
14866 /* Map an ELF section header flag to its corresponding string. */
14867 typedef struct
14869 char *flag_name;
14870 flagword flag_value;
14871 } elf_flags_to_name_table;
14873 static const elf_flags_to_name_table elf_flags_to_names [] =
14875 { "SHF_WRITE", SHF_WRITE },
14876 { "SHF_ALLOC", SHF_ALLOC },
14877 { "SHF_EXECINSTR", SHF_EXECINSTR },
14878 { "SHF_MERGE", SHF_MERGE },
14879 { "SHF_STRINGS", SHF_STRINGS },
14880 { "SHF_INFO_LINK", SHF_INFO_LINK},
14881 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
14882 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
14883 { "SHF_GROUP", SHF_GROUP },
14884 { "SHF_TLS", SHF_TLS },
14885 { "SHF_MASKOS", SHF_MASKOS },
14886 { "SHF_EXCLUDE", SHF_EXCLUDE },
14889 /* Returns TRUE if the section is to be included, otherwise FALSE. */
14890 bool
14891 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
14892 struct flag_info *flaginfo,
14893 asection *section)
14895 const bfd_vma sh_flags = elf_section_flags (section);
14897 if (!flaginfo->flags_initialized)
14899 bfd *obfd = info->output_bfd;
14900 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14901 struct flag_info_list *tf = flaginfo->flag_list;
14902 int with_hex = 0;
14903 int without_hex = 0;
14905 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
14907 unsigned i;
14908 flagword (*lookup) (char *);
14910 lookup = bed->elf_backend_lookup_section_flags_hook;
14911 if (lookup != NULL)
14913 flagword hexval = (*lookup) ((char *) tf->name);
14915 if (hexval != 0)
14917 if (tf->with == with_flags)
14918 with_hex |= hexval;
14919 else if (tf->with == without_flags)
14920 without_hex |= hexval;
14921 tf->valid = true;
14922 continue;
14925 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
14927 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
14929 if (tf->with == with_flags)
14930 with_hex |= elf_flags_to_names[i].flag_value;
14931 else if (tf->with == without_flags)
14932 without_hex |= elf_flags_to_names[i].flag_value;
14933 tf->valid = true;
14934 break;
14937 if (!tf->valid)
14939 info->callbacks->einfo
14940 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
14941 return false;
14944 flaginfo->flags_initialized = true;
14945 flaginfo->only_with_flags |= with_hex;
14946 flaginfo->not_with_flags |= without_hex;
14949 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
14950 return false;
14952 if ((flaginfo->not_with_flags & sh_flags) != 0)
14953 return false;
14955 return true;
14958 struct alloc_got_off_arg {
14959 bfd_vma gotoff;
14960 struct bfd_link_info *info;
14963 /* We need a special top-level link routine to convert got reference counts
14964 to real got offsets. */
14966 static bool
14967 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
14969 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
14970 bfd *obfd = gofarg->info->output_bfd;
14971 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14973 if (h->got.refcount > 0)
14975 h->got.offset = gofarg->gotoff;
14976 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
14978 else
14979 h->got.offset = (bfd_vma) -1;
14981 return true;
14984 /* And an accompanying bit to work out final got entry offsets once
14985 we're done. Should be called from final_link. */
14987 bool
14988 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
14989 struct bfd_link_info *info)
14991 bfd *i;
14992 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14993 bfd_vma gotoff;
14994 struct alloc_got_off_arg gofarg;
14996 BFD_ASSERT (abfd == info->output_bfd);
14998 if (! is_elf_hash_table (info->hash))
14999 return false;
15001 /* The GOT offset is relative to the .got section, but the GOT header is
15002 put into the .got.plt section, if the backend uses it. */
15003 if (bed->want_got_plt)
15004 gotoff = 0;
15005 else
15006 gotoff = bed->got_header_size;
15008 /* Do the local .got entries first. */
15009 for (i = info->input_bfds; i; i = i->link.next)
15011 bfd_signed_vma *local_got;
15012 size_t j, locsymcount;
15013 Elf_Internal_Shdr *symtab_hdr;
15015 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
15016 continue;
15018 local_got = elf_local_got_refcounts (i);
15019 if (!local_got)
15020 continue;
15022 symtab_hdr = &elf_tdata (i)->symtab_hdr;
15023 if (elf_bad_symtab (i))
15024 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
15025 else
15026 locsymcount = symtab_hdr->sh_info;
15028 for (j = 0; j < locsymcount; ++j)
15030 if (local_got[j] > 0)
15032 local_got[j] = gotoff;
15033 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
15035 else
15036 local_got[j] = (bfd_vma) -1;
15040 /* Then the global .got entries. .plt refcounts are handled by
15041 adjust_dynamic_symbol */
15042 gofarg.gotoff = gotoff;
15043 gofarg.info = info;
15044 elf_link_hash_traverse (elf_hash_table (info),
15045 elf_gc_allocate_got_offsets,
15046 &gofarg);
15047 return true;
15050 /* Many folk need no more in the way of final link than this, once
15051 got entry reference counting is enabled. */
15053 bool
15054 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
15056 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
15057 return false;
15059 /* Invoke the regular ELF backend linker to do all the work. */
15060 return bfd_elf_final_link (abfd, info);
15063 bool
15064 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
15066 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
15068 if (rcookie->bad_symtab)
15069 rcookie->rel = rcookie->rels;
15071 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
15073 unsigned long r_symndx;
15075 if (! rcookie->bad_symtab)
15076 if (rcookie->rel->r_offset > offset)
15077 return false;
15078 if (rcookie->rel->r_offset != offset)
15079 continue;
15081 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
15082 if (r_symndx == STN_UNDEF)
15083 return true;
15085 if (r_symndx >= rcookie->locsymcount
15086 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
15088 struct elf_link_hash_entry *h;
15090 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
15092 while (h->root.type == bfd_link_hash_indirect
15093 || h->root.type == bfd_link_hash_warning)
15094 h = (struct elf_link_hash_entry *) h->root.u.i.link;
15096 if ((h->root.type == bfd_link_hash_defined
15097 || h->root.type == bfd_link_hash_defweak)
15098 && (h->root.u.def.section->owner != rcookie->abfd
15099 || h->root.u.def.section->kept_section != NULL
15100 || discarded_section (h->root.u.def.section)))
15101 return true;
15103 else
15105 /* It's not a relocation against a global symbol,
15106 but it could be a relocation against a local
15107 symbol for a discarded section. */
15108 asection *isec;
15109 Elf_Internal_Sym *isym;
15111 /* Need to: get the symbol; get the section. */
15112 isym = &rcookie->locsyms[r_symndx];
15113 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
15114 if (isec != NULL
15115 && (isec->kept_section != NULL
15116 || discarded_section (isec)))
15117 return true;
15119 return false;
15121 return false;
15124 /* Discard unneeded references to discarded sections.
15125 Returns -1 on error, 1 if any section's size was changed, 0 if
15126 nothing changed. This function assumes that the relocations are in
15127 sorted order, which is true for all known assemblers. */
15130 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
15132 struct elf_reloc_cookie cookie;
15133 asection *o;
15134 bfd *abfd;
15135 int changed = 0;
15137 if (info->traditional_format
15138 || !is_elf_hash_table (info->hash))
15139 return 0;
15141 o = bfd_get_section_by_name (output_bfd, ".stab");
15142 if (o != NULL)
15144 asection *i;
15146 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
15148 if (i->size == 0
15149 || i->reloc_count == 0
15150 || i->sec_info_type != SEC_INFO_TYPE_STABS)
15151 continue;
15153 abfd = i->owner;
15154 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
15155 continue;
15157 if (!init_reloc_cookie_for_section (&cookie, info, i, false))
15158 return -1;
15160 if (_bfd_discard_section_stabs (abfd, i,
15161 elf_section_data (i)->sec_info,
15162 bfd_elf_reloc_symbol_deleted_p,
15163 &cookie))
15164 changed = 1;
15166 fini_reloc_cookie_for_section (&cookie, i);
15170 o = NULL;
15171 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
15172 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
15173 if (o != NULL)
15175 asection *i;
15176 int eh_changed = 0;
15177 unsigned int eh_alignment; /* Octets. */
15179 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
15181 if (i->size == 0)
15182 continue;
15184 abfd = i->owner;
15185 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
15186 continue;
15188 if (!init_reloc_cookie_for_section (&cookie, info, i, false))
15189 return -1;
15191 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
15192 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
15193 bfd_elf_reloc_symbol_deleted_p,
15194 &cookie))
15196 eh_changed = 1;
15197 if (i->size != i->rawsize)
15198 changed = 1;
15201 fini_reloc_cookie_for_section (&cookie, i);
15204 eh_alignment = ((1 << o->alignment_power)
15205 * bfd_octets_per_byte (output_bfd, o));
15206 /* Skip over zero terminator, and prevent empty sections from
15207 adding alignment padding at the end. */
15208 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
15209 if (i->size == 0)
15210 i->flags |= SEC_EXCLUDE;
15211 else if (i->size > 4)
15212 break;
15213 /* The last non-empty eh_frame section doesn't need padding. */
15214 if (i != NULL)
15215 i = i->map_tail.s;
15216 /* Any prior sections must pad the last FDE out to the output
15217 section alignment. Otherwise we might have zero padding
15218 between sections, which would be seen as a terminator. */
15219 for (; i != NULL; i = i->map_tail.s)
15220 if (i->size == 4)
15221 /* All but the last zero terminator should have been removed. */
15222 BFD_FAIL ();
15223 else
15225 bfd_size_type size
15226 = (i->size + eh_alignment - 1) & -eh_alignment;
15227 if (i->size != size)
15229 i->size = size;
15230 changed = 1;
15231 eh_changed = 1;
15234 if (eh_changed)
15235 elf_link_hash_traverse (elf_hash_table (info),
15236 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
15239 o = bfd_get_section_by_name (output_bfd, ".sframe");
15240 if (o != NULL)
15242 asection *i;
15244 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
15246 if (i->size == 0)
15247 continue;
15249 abfd = i->owner;
15250 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
15251 continue;
15253 if (!init_reloc_cookie_for_section (&cookie, info, i, false))
15254 return -1;
15256 if (_bfd_elf_parse_sframe (abfd, info, i, &cookie))
15258 if (_bfd_elf_discard_section_sframe (i,
15259 bfd_elf_reloc_symbol_deleted_p,
15260 &cookie))
15262 if (i->size != i->rawsize)
15263 changed = 1;
15266 fini_reloc_cookie_for_section (&cookie, i);
15268 /* Update the reference to the output .sframe section. Used to
15269 determine later if PT_GNU_SFRAME segment is to be generated. */
15270 if (!_bfd_elf_set_section_sframe (output_bfd, info))
15271 return -1;
15274 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
15276 const struct elf_backend_data *bed;
15277 asection *s;
15279 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
15280 continue;
15281 s = abfd->sections;
15282 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
15283 continue;
15285 bed = get_elf_backend_data (abfd);
15287 if (bed->elf_backend_discard_info != NULL)
15289 if (!init_reloc_cookie (&cookie, info, abfd, false))
15290 return -1;
15292 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
15293 changed = 1;
15295 fini_reloc_cookie (&cookie, abfd);
15299 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
15300 _bfd_elf_end_eh_frame_parsing (info);
15302 if (info->eh_frame_hdr_type
15303 && !bfd_link_relocatable (info)
15304 && _bfd_elf_discard_section_eh_frame_hdr (info))
15305 changed = 1;
15307 return changed;
15310 bool
15311 _bfd_elf_section_already_linked (bfd *abfd,
15312 asection *sec,
15313 struct bfd_link_info *info)
15315 flagword flags;
15316 const char *name, *key;
15317 struct bfd_section_already_linked *l;
15318 struct bfd_section_already_linked_hash_entry *already_linked_list;
15320 if (sec->output_section == bfd_abs_section_ptr)
15321 return false;
15323 flags = sec->flags;
15325 /* Return if it isn't a linkonce section. A comdat group section
15326 also has SEC_LINK_ONCE set. */
15327 if ((flags & SEC_LINK_ONCE) == 0)
15328 return false;
15330 /* Don't put group member sections on our list of already linked
15331 sections. They are handled as a group via their group section. */
15332 if (elf_sec_group (sec) != NULL)
15333 return false;
15335 /* For a SHT_GROUP section, use the group signature as the key. */
15336 name = sec->name;
15337 if ((flags & SEC_GROUP) != 0
15338 && elf_next_in_group (sec) != NULL
15339 && elf_group_name (elf_next_in_group (sec)) != NULL)
15340 key = elf_group_name (elf_next_in_group (sec));
15341 else
15343 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
15344 if (startswith (name, ".gnu.linkonce.")
15345 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
15346 key++;
15347 else
15348 /* Must be a user linkonce section that doesn't follow gcc's
15349 naming convention. In this case we won't be matching
15350 single member groups. */
15351 key = name;
15354 already_linked_list = bfd_section_already_linked_table_lookup (key);
15356 for (l = already_linked_list->entry; l != NULL; l = l->next)
15358 /* We may have 2 different types of sections on the list: group
15359 sections with a signature of <key> (<key> is some string),
15360 and linkonce sections named .gnu.linkonce.<type>.<key>.
15361 Match like sections. LTO plugin sections are an exception.
15362 They are always named .gnu.linkonce.t.<key> and match either
15363 type of section. */
15364 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
15365 && ((flags & SEC_GROUP) != 0
15366 || strcmp (name, l->sec->name) == 0))
15367 || (l->sec->owner->flags & BFD_PLUGIN) != 0
15368 || (sec->owner->flags & BFD_PLUGIN) != 0)
15370 /* The section has already been linked. See if we should
15371 issue a warning. */
15372 if (!_bfd_handle_already_linked (sec, l, info))
15373 return false;
15375 if (flags & SEC_GROUP)
15377 asection *first = elf_next_in_group (sec);
15378 asection *s = first;
15380 while (s != NULL)
15382 s->output_section = bfd_abs_section_ptr;
15383 /* Record which group discards it. */
15384 s->kept_section = l->sec;
15385 s = elf_next_in_group (s);
15386 /* These lists are circular. */
15387 if (s == first)
15388 break;
15392 return true;
15396 /* A single member comdat group section may be discarded by a
15397 linkonce section and vice versa. */
15398 if ((flags & SEC_GROUP) != 0)
15400 asection *first = elf_next_in_group (sec);
15402 if (first != NULL && elf_next_in_group (first) == first)
15403 /* Check this single member group against linkonce sections. */
15404 for (l = already_linked_list->entry; l != NULL; l = l->next)
15405 if ((l->sec->flags & SEC_GROUP) == 0
15406 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
15408 first->output_section = bfd_abs_section_ptr;
15409 first->kept_section = l->sec;
15410 sec->output_section = bfd_abs_section_ptr;
15411 break;
15414 else
15415 /* Check this linkonce section against single member groups. */
15416 for (l = already_linked_list->entry; l != NULL; l = l->next)
15417 if (l->sec->flags & SEC_GROUP)
15419 asection *first = elf_next_in_group (l->sec);
15421 if (first != NULL
15422 && elf_next_in_group (first) == first
15423 && bfd_elf_match_symbols_in_sections (first, sec, info))
15425 sec->output_section = bfd_abs_section_ptr;
15426 sec->kept_section = first;
15427 break;
15431 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
15432 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
15433 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
15434 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
15435 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
15436 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
15437 `.gnu.linkonce.t.F' section from a different bfd not requiring any
15438 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
15439 The reverse order cannot happen as there is never a bfd with only the
15440 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
15441 matter as here were are looking only for cross-bfd sections. */
15443 if ((flags & SEC_GROUP) == 0 && startswith (name, ".gnu.linkonce.r."))
15444 for (l = already_linked_list->entry; l != NULL; l = l->next)
15445 if ((l->sec->flags & SEC_GROUP) == 0
15446 && startswith (l->sec->name, ".gnu.linkonce.t."))
15448 if (abfd != l->sec->owner)
15449 sec->output_section = bfd_abs_section_ptr;
15450 break;
15453 /* This is the first section with this name. Record it. */
15454 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
15455 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
15456 return sec->output_section == bfd_abs_section_ptr;
15459 bool
15460 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
15462 return sym->st_shndx == SHN_COMMON;
15465 unsigned int
15466 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
15468 return SHN_COMMON;
15471 asection *
15472 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
15474 return bfd_com_section_ptr;
15477 bfd_vma
15478 _bfd_elf_default_got_elt_size (bfd *abfd,
15479 struct bfd_link_info *info ATTRIBUTE_UNUSED,
15480 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
15481 bfd *ibfd ATTRIBUTE_UNUSED,
15482 unsigned long symndx ATTRIBUTE_UNUSED)
15484 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15485 return bed->s->arch_size / 8;
15488 /* Routines to support the creation of dynamic relocs. */
15490 /* Returns the name of the dynamic reloc section associated with SEC. */
15492 static const char *
15493 get_dynamic_reloc_section_name (bfd * abfd,
15494 asection * sec,
15495 bool is_rela)
15497 char *name;
15498 const char *old_name = bfd_section_name (sec);
15499 const char *prefix = is_rela ? ".rela" : ".rel";
15501 if (old_name == NULL)
15502 return NULL;
15504 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
15505 sprintf (name, "%s%s", prefix, old_name);
15507 return name;
15510 /* Returns the dynamic reloc section associated with SEC.
15511 If necessary compute the name of the dynamic reloc section based
15512 on SEC's name (looked up in ABFD's string table) and the setting
15513 of IS_RELA. */
15515 asection *
15516 _bfd_elf_get_dynamic_reloc_section (bfd *abfd,
15517 asection *sec,
15518 bool is_rela)
15520 asection *reloc_sec = elf_section_data (sec)->sreloc;
15522 if (reloc_sec == NULL)
15524 const char *name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
15526 if (name != NULL)
15528 reloc_sec = bfd_get_linker_section (abfd, name);
15530 if (reloc_sec != NULL)
15531 elf_section_data (sec)->sreloc = reloc_sec;
15535 return reloc_sec;
15538 /* Returns the dynamic reloc section associated with SEC. If the
15539 section does not exist it is created and attached to the DYNOBJ
15540 bfd and stored in the SRELOC field of SEC's elf_section_data
15541 structure.
15543 ALIGNMENT is the alignment for the newly created section and
15544 IS_RELA defines whether the name should be .rela.<SEC's name>
15545 or .rel.<SEC's name>. The section name is looked up in the
15546 string table associated with ABFD. */
15548 asection *
15549 _bfd_elf_make_dynamic_reloc_section (asection *sec,
15550 bfd *dynobj,
15551 unsigned int alignment,
15552 bfd *abfd,
15553 bool is_rela)
15555 asection * reloc_sec = elf_section_data (sec)->sreloc;
15557 if (reloc_sec == NULL)
15559 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
15561 if (name == NULL)
15562 return NULL;
15564 reloc_sec = bfd_get_linker_section (dynobj, name);
15566 if (reloc_sec == NULL)
15568 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
15569 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
15570 if ((sec->flags & SEC_ALLOC) != 0)
15571 flags |= SEC_ALLOC | SEC_LOAD;
15573 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
15574 if (reloc_sec != NULL)
15576 /* _bfd_elf_get_sec_type_attr chooses a section type by
15577 name. Override as it may be wrong, eg. for a user
15578 section named "auto" we'll get ".relauto" which is
15579 seen to be a .rela section. */
15580 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
15581 if (!bfd_set_section_alignment (reloc_sec, alignment))
15582 reloc_sec = NULL;
15586 elf_section_data (sec)->sreloc = reloc_sec;
15589 return reloc_sec;
15592 /* Copy the ELF symbol type and other attributes for a linker script
15593 assignment from HSRC to HDEST. Generally this should be treated as
15594 if we found a strong non-dynamic definition for HDEST (except that
15595 ld ignores multiple definition errors). */
15596 void
15597 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
15598 struct bfd_link_hash_entry *hdest,
15599 struct bfd_link_hash_entry *hsrc)
15601 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
15602 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
15603 Elf_Internal_Sym isym;
15605 ehdest->type = ehsrc->type;
15606 ehdest->target_internal = ehsrc->target_internal;
15608 isym.st_other = ehsrc->other;
15609 elf_merge_st_other (abfd, ehdest, isym.st_other, NULL, true, false);
15612 /* Append a RELA relocation REL to section S in BFD. */
15614 void
15615 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15617 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15618 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
15619 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
15620 bed->s->swap_reloca_out (abfd, rel, loc);
15623 /* Append a REL relocation REL to section S in BFD. */
15625 void
15626 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15628 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15629 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
15630 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
15631 bed->s->swap_reloc_out (abfd, rel, loc);
15634 /* Define __start, __stop, .startof. or .sizeof. symbol. */
15636 struct bfd_link_hash_entry *
15637 bfd_elf_define_start_stop (struct bfd_link_info *info,
15638 const char *symbol, asection *sec)
15640 struct elf_link_hash_entry *h;
15642 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
15643 false, false, true);
15644 /* NB: Common symbols will be turned into definition later. */
15645 if (h != NULL
15646 && !h->root.ldscript_def
15647 && (h->root.type == bfd_link_hash_undefined
15648 || h->root.type == bfd_link_hash_undefweak
15649 || ((h->ref_regular || h->def_dynamic)
15650 && !h->def_regular
15651 && h->root.type != bfd_link_hash_common)))
15653 bool was_dynamic = h->ref_dynamic || h->def_dynamic;
15654 h->verinfo.verdef = NULL;
15655 h->root.type = bfd_link_hash_defined;
15656 h->root.u.def.section = sec;
15657 h->root.u.def.value = 0;
15658 h->def_regular = 1;
15659 h->def_dynamic = 0;
15660 h->start_stop = 1;
15661 h->u2.start_stop_section = sec;
15662 if (symbol[0] == '.')
15664 /* .startof. and .sizeof. symbols are local. */
15665 const struct elf_backend_data *bed;
15666 bed = get_elf_backend_data (info->output_bfd);
15667 (*bed->elf_backend_hide_symbol) (info, h, true);
15669 else
15671 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
15672 h->other = ((h->other & ~ELF_ST_VISIBILITY (-1))
15673 | info->start_stop_visibility);
15674 if (was_dynamic)
15675 bfd_elf_link_record_dynamic_symbol (info, h);
15677 return &h->root;
15679 return NULL;
15682 /* Find dynamic relocs for H that apply to read-only sections. */
15684 asection *
15685 _bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry *h)
15687 struct elf_dyn_relocs *p;
15689 for (p = h->dyn_relocs; p != NULL; p = p->next)
15691 asection *s = p->sec->output_section;
15693 if (s != NULL && (s->flags & SEC_READONLY) != 0)
15694 return p->sec;
15696 return NULL;
15699 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
15700 read-only sections. */
15702 bool
15703 _bfd_elf_maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
15705 asection *sec;
15707 if (h->root.type == bfd_link_hash_indirect)
15708 return true;
15710 sec = _bfd_elf_readonly_dynrelocs (h);
15711 if (sec != NULL)
15713 struct bfd_link_info *info = (struct bfd_link_info *) inf;
15715 info->flags |= DF_TEXTREL;
15716 /* xgettext:c-format */
15717 info->callbacks->minfo (_("%pB: dynamic relocation against `%pT' "
15718 "in read-only section `%pA'\n"),
15719 sec->owner, h->root.root.string, sec);
15721 if (bfd_link_textrel_check (info))
15722 /* xgettext:c-format */
15723 info->callbacks->einfo (_("%P: %pB: warning: relocation against `%s' "
15724 "in read-only section `%pA'\n"),
15725 sec->owner, h->root.root.string, sec);
15727 /* Not an error, just cut short the traversal. */
15728 return false;
15730 return true;
15733 /* Add dynamic tags. */
15735 bool
15736 _bfd_elf_add_dynamic_tags (bfd *output_bfd, struct bfd_link_info *info,
15737 bool need_dynamic_reloc)
15739 struct elf_link_hash_table *htab = elf_hash_table (info);
15741 if (htab->dynamic_sections_created)
15743 /* Add some entries to the .dynamic section. We fill in the
15744 values later, in finish_dynamic_sections, but we must add
15745 the entries now so that we get the correct size for the
15746 .dynamic section. The DT_DEBUG entry is filled in by the
15747 dynamic linker and used by the debugger. */
15748 #define add_dynamic_entry(TAG, VAL) \
15749 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
15751 const struct elf_backend_data *bed
15752 = get_elf_backend_data (output_bfd);
15754 if (bfd_link_executable (info))
15756 if (!add_dynamic_entry (DT_DEBUG, 0))
15757 return false;
15760 if (htab->dt_pltgot_required || htab->splt->size != 0)
15762 /* DT_PLTGOT is used by prelink even if there is no PLT
15763 relocation. */
15764 if (!add_dynamic_entry (DT_PLTGOT, 0))
15765 return false;
15768 if (htab->dt_jmprel_required || htab->srelplt->size != 0)
15770 if (!add_dynamic_entry (DT_PLTRELSZ, 0)
15771 || !add_dynamic_entry (DT_PLTREL,
15772 (bed->rela_plts_and_copies_p
15773 ? DT_RELA : DT_REL))
15774 || !add_dynamic_entry (DT_JMPREL, 0))
15775 return false;
15778 if (htab->tlsdesc_plt
15779 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
15780 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
15781 return false;
15783 if (need_dynamic_reloc)
15785 if (bed->rela_plts_and_copies_p)
15787 if (!add_dynamic_entry (DT_RELA, 0)
15788 || !add_dynamic_entry (DT_RELASZ, 0)
15789 || !add_dynamic_entry (DT_RELAENT,
15790 bed->s->sizeof_rela))
15791 return false;
15793 else
15795 if (!add_dynamic_entry (DT_REL, 0)
15796 || !add_dynamic_entry (DT_RELSZ, 0)
15797 || !add_dynamic_entry (DT_RELENT,
15798 bed->s->sizeof_rel))
15799 return false;
15802 /* If any dynamic relocs apply to a read-only section,
15803 then we need a DT_TEXTREL entry. */
15804 if ((info->flags & DF_TEXTREL) == 0)
15805 elf_link_hash_traverse (htab, _bfd_elf_maybe_set_textrel,
15806 info);
15808 if ((info->flags & DF_TEXTREL) != 0)
15810 if (htab->ifunc_resolvers)
15811 info->callbacks->einfo
15812 (_("%P: warning: GNU indirect functions with DT_TEXTREL "
15813 "may result in a segfault at runtime; recompile with %s\n"),
15814 bfd_link_dll (info) ? "-fPIC" : "-fPIE");
15816 if (!add_dynamic_entry (DT_TEXTREL, 0))
15817 return false;
15821 #undef add_dynamic_entry
15823 return true;