Regenerate AArch64 opcodes files
[binutils-gdb.git] / bfd / elflink.c
blob5a6cb07b2ceb16a702c74eb78d063c4ffd8aa917
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 asection *
53 _bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
54 unsigned long r_symndx,
55 bool discard)
57 if (r_symndx >= cookie->locsymcount
58 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
60 struct elf_link_hash_entry *h;
62 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
64 while (h->root.type == bfd_link_hash_indirect
65 || h->root.type == bfd_link_hash_warning)
66 h = (struct elf_link_hash_entry *) h->root.u.i.link;
68 if ((h->root.type == bfd_link_hash_defined
69 || h->root.type == bfd_link_hash_defweak)
70 && discarded_section (h->root.u.def.section))
71 return h->root.u.def.section;
72 else
73 return NULL;
75 else
77 /* It's not a relocation against a global symbol,
78 but it could be a relocation against a local
79 symbol for a discarded section. */
80 asection *isec;
81 Elf_Internal_Sym *isym;
83 /* Need to: get the symbol; get the section. */
84 isym = &cookie->locsyms[r_symndx];
85 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
86 if (isec != NULL
87 && discard ? discarded_section (isec) : 1)
88 return isec;
90 return NULL;
93 /* Define a symbol in a dynamic linkage section. */
95 struct elf_link_hash_entry *
96 _bfd_elf_define_linkage_sym (bfd *abfd,
97 struct bfd_link_info *info,
98 asection *sec,
99 const char *name)
101 struct elf_link_hash_entry *h;
102 struct bfd_link_hash_entry *bh;
103 const struct elf_backend_data *bed;
105 h = elf_link_hash_lookup (elf_hash_table (info), name, false, false, false);
106 if (h != NULL)
108 /* Zap symbol defined in an as-needed lib that wasn't linked.
109 This is a symptom of a larger problem: Absolute symbols
110 defined in shared libraries can't be overridden, because we
111 lose the link to the bfd which is via the symbol section. */
112 h->root.type = bfd_link_hash_new;
113 bh = &h->root;
115 else
116 bh = NULL;
118 bed = get_elf_backend_data (abfd);
119 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
120 sec, 0, NULL, false, bed->collect,
121 &bh))
122 return NULL;
123 h = (struct elf_link_hash_entry *) bh;
124 BFD_ASSERT (h != NULL);
125 h->def_regular = 1;
126 h->non_elf = 0;
127 h->root.linker_def = 1;
128 h->type = STT_OBJECT;
129 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
130 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
132 (*bed->elf_backend_hide_symbol) (info, h, true);
133 return h;
136 bool
137 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
139 flagword flags;
140 asection *s;
141 struct elf_link_hash_entry *h;
142 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
143 struct elf_link_hash_table *htab = elf_hash_table (info);
145 /* This function may be called more than once. */
146 if (htab->sgot != NULL)
147 return true;
149 flags = bed->dynamic_sec_flags;
151 s = bfd_make_section_anyway_with_flags (abfd,
152 (bed->rela_plts_and_copies_p
153 ? ".rela.got" : ".rel.got"),
154 (bed->dynamic_sec_flags
155 | SEC_READONLY));
156 if (s == NULL
157 || !bfd_set_section_alignment (s, bed->s->log_file_align))
158 return false;
159 htab->srelgot = s;
161 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
162 if (s == NULL
163 || !bfd_set_section_alignment (s, bed->s->log_file_align))
164 return false;
165 htab->sgot = s;
167 if (bed->want_got_plt)
169 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
170 if (s == NULL
171 || !bfd_set_section_alignment (s, bed->s->log_file_align))
172 return false;
173 htab->sgotplt = s;
176 /* The first bit of the global offset table is the header. */
177 s->size += bed->got_header_size;
179 if (bed->want_got_sym)
181 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
182 (or .got.plt) section. We don't do this in the linker script
183 because we don't want to define the symbol if we are not creating
184 a global offset table. */
185 h = _bfd_elf_define_linkage_sym (abfd, info, s,
186 "_GLOBAL_OFFSET_TABLE_");
187 elf_hash_table (info)->hgot = h;
188 if (h == NULL)
189 return false;
192 return true;
195 /* Create a strtab to hold the dynamic symbol names. */
196 static bool
197 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
199 struct elf_link_hash_table *hash_table;
201 hash_table = elf_hash_table (info);
202 if (hash_table->dynobj == NULL)
204 /* We may not set dynobj, an input file holding linker created
205 dynamic sections to abfd, which may be a dynamic object with
206 its own dynamic sections. We need to find a normal input file
207 to hold linker created sections if possible. */
208 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
210 bfd *ibfd;
211 asection *s;
212 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
213 if ((ibfd->flags
214 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
215 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
216 && elf_object_id (ibfd) == elf_hash_table_id (hash_table)
217 && !((s = ibfd->sections) != NULL
218 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS))
220 abfd = ibfd;
221 break;
224 hash_table->dynobj = abfd;
227 if (hash_table->dynstr == NULL)
229 hash_table->dynstr = _bfd_elf_strtab_init ();
230 if (hash_table->dynstr == NULL)
231 return false;
233 return true;
236 /* Create some sections which will be filled in with dynamic linking
237 information. ABFD is an input file which requires dynamic sections
238 to be created. The dynamic sections take up virtual memory space
239 when the final executable is run, so we need to create them before
240 addresses are assigned to the output sections. We work out the
241 actual contents and size of these sections later. */
243 bool
244 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
246 flagword flags;
247 asection *s;
248 const struct elf_backend_data *bed;
249 struct elf_link_hash_entry *h;
251 if (! is_elf_hash_table (info->hash))
252 return false;
254 if (elf_hash_table (info)->dynamic_sections_created)
255 return true;
257 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
258 return false;
260 abfd = elf_hash_table (info)->dynobj;
261 bed = get_elf_backend_data (abfd);
263 flags = bed->dynamic_sec_flags;
265 /* A dynamically linked executable has a .interp section, but a
266 shared library does not. */
267 if (bfd_link_executable (info) && !info->nointerp)
269 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
270 flags | SEC_READONLY);
271 if (s == NULL)
272 return false;
275 /* Create sections to hold version informations. These are removed
276 if they are not needed. */
277 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
278 flags | SEC_READONLY);
279 if (s == NULL
280 || !bfd_set_section_alignment (s, bed->s->log_file_align))
281 return false;
283 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
284 flags | SEC_READONLY);
285 if (s == NULL
286 || !bfd_set_section_alignment (s, 1))
287 return false;
289 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
290 flags | SEC_READONLY);
291 if (s == NULL
292 || !bfd_set_section_alignment (s, bed->s->log_file_align))
293 return false;
295 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
296 flags | SEC_READONLY);
297 if (s == NULL
298 || !bfd_set_section_alignment (s, bed->s->log_file_align))
299 return false;
300 elf_hash_table (info)->dynsym = s;
302 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
303 flags | SEC_READONLY);
304 if (s == NULL)
305 return false;
307 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
308 if (s == NULL
309 || !bfd_set_section_alignment (s, bed->s->log_file_align))
310 return false;
312 /* The special symbol _DYNAMIC is always set to the start of the
313 .dynamic section. We could set _DYNAMIC in a linker script, but we
314 only want to define it if we are, in fact, creating a .dynamic
315 section. We don't want to define it if there is no .dynamic
316 section, since on some ELF platforms the start up code examines it
317 to decide how to initialize the process. */
318 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
319 elf_hash_table (info)->hdynamic = h;
320 if (h == NULL)
321 return false;
323 if (info->emit_hash)
325 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
326 flags | SEC_READONLY);
327 if (s == NULL
328 || !bfd_set_section_alignment (s, bed->s->log_file_align))
329 return false;
330 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
333 if (info->emit_gnu_hash && bed->record_xhash_symbol == NULL)
335 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
336 flags | SEC_READONLY);
337 if (s == NULL
338 || !bfd_set_section_alignment (s, bed->s->log_file_align))
339 return false;
340 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
341 4 32-bit words followed by variable count of 64-bit words, then
342 variable count of 32-bit words. */
343 if (bed->s->arch_size == 64)
344 elf_section_data (s)->this_hdr.sh_entsize = 0;
345 else
346 elf_section_data (s)->this_hdr.sh_entsize = 4;
349 if (info->enable_dt_relr)
351 s = bfd_make_section_anyway_with_flags (abfd, ".relr.dyn",
352 (bed->dynamic_sec_flags
353 | SEC_READONLY));
354 if (s == NULL
355 || !bfd_set_section_alignment (s, bed->s->log_file_align))
356 return false;
357 elf_hash_table (info)->srelrdyn = s;
360 /* Let the backend create the rest of the sections. This lets the
361 backend set the right flags. The backend will normally create
362 the .got and .plt sections. */
363 if (bed->elf_backend_create_dynamic_sections == NULL
364 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
365 return false;
367 elf_hash_table (info)->dynamic_sections_created = true;
369 return true;
372 /* Create dynamic sections when linking against a dynamic object. */
374 bool
375 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
377 flagword flags, pltflags;
378 struct elf_link_hash_entry *h;
379 asection *s;
380 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
381 struct elf_link_hash_table *htab = elf_hash_table (info);
383 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
384 .rel[a].bss sections. */
385 flags = bed->dynamic_sec_flags;
387 pltflags = flags;
388 if (bed->plt_not_loaded)
389 /* We do not clear SEC_ALLOC here because we still want the OS to
390 allocate space for the section; it's just that there's nothing
391 to read in from the object file. */
392 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
393 else
394 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
395 if (bed->plt_readonly)
396 pltflags |= SEC_READONLY;
398 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
399 if (s == NULL
400 || !bfd_set_section_alignment (s, bed->plt_alignment))
401 return false;
402 htab->splt = s;
404 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
405 .plt section. */
406 if (bed->want_plt_sym)
408 h = _bfd_elf_define_linkage_sym (abfd, info, s,
409 "_PROCEDURE_LINKAGE_TABLE_");
410 elf_hash_table (info)->hplt = h;
411 if (h == NULL)
412 return false;
415 s = bfd_make_section_anyway_with_flags (abfd,
416 (bed->rela_plts_and_copies_p
417 ? ".rela.plt" : ".rel.plt"),
418 flags | SEC_READONLY);
419 if (s == NULL
420 || !bfd_set_section_alignment (s, bed->s->log_file_align))
421 return false;
422 htab->srelplt = s;
424 if (! _bfd_elf_create_got_section (abfd, info))
425 return false;
427 if (bed->want_dynbss)
429 /* The .dynbss section is a place to put symbols which are defined
430 by dynamic objects, are referenced by regular objects, and are
431 not functions. We must allocate space for them in the process
432 image and use a R_*_COPY reloc to tell the dynamic linker to
433 initialize them at run time. The linker script puts the .dynbss
434 section into the .bss section of the final image. */
435 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
436 SEC_ALLOC | SEC_LINKER_CREATED);
437 if (s == NULL)
438 return false;
439 htab->sdynbss = s;
441 if (bed->want_dynrelro)
443 /* Similarly, but for symbols that were originally in read-only
444 sections. This section doesn't really need to have contents,
445 but make it like other .data.rel.ro sections. */
446 s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
447 flags);
448 if (s == NULL)
449 return false;
450 htab->sdynrelro = s;
453 /* The .rel[a].bss section holds copy relocs. This section is not
454 normally needed. We need to create it here, though, so that the
455 linker will map it to an output section. We can't just create it
456 only if we need it, because we will not know whether we need it
457 until we have seen all the input files, and the first time the
458 main linker code calls BFD after examining all the input files
459 (size_dynamic_sections) the input sections have already been
460 mapped to the output sections. If the section turns out not to
461 be needed, we can discard it later. We will never need this
462 section when generating a shared object, since they do not use
463 copy relocs. */
464 if (bfd_link_executable (info))
466 s = bfd_make_section_anyway_with_flags (abfd,
467 (bed->rela_plts_and_copies_p
468 ? ".rela.bss" : ".rel.bss"),
469 flags | SEC_READONLY);
470 if (s == NULL
471 || !bfd_set_section_alignment (s, bed->s->log_file_align))
472 return false;
473 htab->srelbss = s;
475 if (bed->want_dynrelro)
477 s = (bfd_make_section_anyway_with_flags
478 (abfd, (bed->rela_plts_and_copies_p
479 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
480 flags | SEC_READONLY));
481 if (s == NULL
482 || !bfd_set_section_alignment (s, bed->s->log_file_align))
483 return false;
484 htab->sreldynrelro = s;
489 return true;
492 /* Record a new dynamic symbol. We record the dynamic symbols as we
493 read the input files, since we need to have a list of all of them
494 before we can determine the final sizes of the output sections.
495 Note that we may actually call this function even though we are not
496 going to output any dynamic symbols; in some cases we know that a
497 symbol should be in the dynamic symbol table, but only if there is
498 one. */
500 bool
501 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
502 struct elf_link_hash_entry *h)
504 if (h->dynindx == -1)
506 struct elf_strtab_hash *dynstr;
507 char *p;
508 const char *name;
509 size_t indx;
511 if (h->root.type == bfd_link_hash_defined
512 || h->root.type == bfd_link_hash_defweak)
514 /* An IR symbol should not be made dynamic. */
515 if (h->root.u.def.section != NULL
516 && h->root.u.def.section->owner != NULL
517 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)
518 return true;
521 /* XXX: The ABI draft says the linker must turn hidden and
522 internal symbols into STB_LOCAL symbols when producing the
523 DSO. However, if ld.so honors st_other in the dynamic table,
524 this would not be necessary. */
525 switch (ELF_ST_VISIBILITY (h->other))
527 case STV_INTERNAL:
528 case STV_HIDDEN:
529 if (h->root.type != bfd_link_hash_undefined
530 && h->root.type != bfd_link_hash_undefweak)
532 h->forced_local = 1;
533 return true;
536 default:
537 break;
540 h->dynindx = elf_hash_table (info)->dynsymcount;
541 ++elf_hash_table (info)->dynsymcount;
543 dynstr = elf_hash_table (info)->dynstr;
544 if (dynstr == NULL)
546 /* Create a strtab to hold the dynamic symbol names. */
547 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
548 if (dynstr == NULL)
549 return false;
552 /* We don't put any version information in the dynamic string
553 table. */
554 name = h->root.root.string;
555 p = strchr (name, ELF_VER_CHR);
556 if (p != NULL)
557 /* We know that the p points into writable memory. In fact,
558 there are only a few symbols that have read-only names, being
559 those like _GLOBAL_OFFSET_TABLE_ that are created specially
560 by the backends. Most symbols will have names pointing into
561 an ELF string table read from a file, or to objalloc memory. */
562 *p = 0;
564 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
566 if (p != NULL)
567 *p = ELF_VER_CHR;
569 if (indx == (size_t) -1)
570 return false;
571 h->dynstr_index = indx;
574 return true;
577 /* Mark a symbol dynamic. */
579 static void
580 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
581 struct elf_link_hash_entry *h,
582 Elf_Internal_Sym *sym)
584 struct bfd_elf_dynamic_list *d = info->dynamic_list;
586 /* It may be called more than once on the same H. */
587 if(h->dynamic || bfd_link_relocatable (info))
588 return;
590 if ((info->dynamic_data
591 && (h->type == STT_OBJECT
592 || h->type == STT_COMMON
593 || (sym != NULL
594 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
595 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
596 || (d != NULL
597 && h->non_elf
598 && (*d->match) (&d->head, NULL, h->root.root.string)))
600 h->dynamic = 1;
601 /* NB: If a symbol is made dynamic by --dynamic-list, it has
602 non-IR reference. */
603 h->root.non_ir_ref_dynamic = 1;
607 /* Record an assignment to a symbol made by a linker script. We need
608 this in case some dynamic object refers to this symbol. */
610 bool
611 bfd_elf_record_link_assignment (bfd *output_bfd,
612 struct bfd_link_info *info,
613 const char *name,
614 bool provide,
615 bool hidden)
617 struct elf_link_hash_entry *h, *hv;
618 struct elf_link_hash_table *htab;
619 const struct elf_backend_data *bed;
621 if (!is_elf_hash_table (info->hash))
622 return true;
624 htab = elf_hash_table (info);
625 h = elf_link_hash_lookup (htab, name, !provide, true, false);
626 if (h == NULL)
627 return provide;
629 if (h->root.type == bfd_link_hash_warning)
630 h = (struct elf_link_hash_entry *) h->root.u.i.link;
632 if (h->versioned == unknown)
634 /* Set versioned if symbol version is unknown. */
635 char *version = strrchr (name, ELF_VER_CHR);
636 if (version)
638 if (version > name && version[-1] != ELF_VER_CHR)
639 h->versioned = versioned_hidden;
640 else
641 h->versioned = versioned;
645 /* Symbols defined in a linker script but not referenced anywhere
646 else will have non_elf set. */
647 if (h->non_elf)
649 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
650 h->non_elf = 0;
653 switch (h->root.type)
655 case bfd_link_hash_defined:
656 case bfd_link_hash_defweak:
657 case bfd_link_hash_common:
658 break;
659 case bfd_link_hash_undefweak:
660 case bfd_link_hash_undefined:
661 /* Since we're defining the symbol, don't let it seem to have not
662 been defined. record_dynamic_symbol and size_dynamic_sections
663 may depend on this. */
664 h->root.type = bfd_link_hash_new;
665 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
666 bfd_link_repair_undef_list (&htab->root);
667 break;
668 case bfd_link_hash_new:
669 break;
670 case bfd_link_hash_indirect:
671 /* We had a versioned symbol in a dynamic library. We make the
672 the versioned symbol point to this one. */
673 bed = get_elf_backend_data (output_bfd);
674 hv = h;
675 while (hv->root.type == bfd_link_hash_indirect
676 || hv->root.type == bfd_link_hash_warning)
677 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
678 /* We don't need to update h->root.u since linker will set them
679 later. */
680 h->root.type = bfd_link_hash_undefined;
681 hv->root.type = bfd_link_hash_indirect;
682 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
683 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
684 break;
685 default:
686 BFD_FAIL ();
687 return false;
690 /* If this symbol is being provided by the linker script, and it is
691 currently defined by a dynamic object, but not by a regular
692 object, then mark it as undefined so that the generic linker will
693 force the correct value. */
694 if (provide
695 && h->def_dynamic
696 && !h->def_regular)
697 h->root.type = bfd_link_hash_undefined;
699 /* If this symbol is currently defined by a dynamic object, but not
700 by a regular object, then clear out any version information because
701 the symbol will not be associated with the dynamic object any
702 more. */
703 if (h->def_dynamic && !h->def_regular)
704 h->verinfo.verdef = NULL;
706 /* Make sure this symbol is not garbage collected. */
707 h->mark = 1;
709 h->def_regular = 1;
711 if (hidden)
713 bed = get_elf_backend_data (output_bfd);
714 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
715 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
716 (*bed->elf_backend_hide_symbol) (info, h, true);
719 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
720 and executables. */
721 if (!bfd_link_relocatable (info)
722 && h->dynindx != -1
723 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
724 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
725 h->forced_local = 1;
727 if ((h->def_dynamic
728 || h->ref_dynamic
729 || bfd_link_dll (info))
730 && !h->forced_local
731 && h->dynindx == -1)
733 if (! bfd_elf_link_record_dynamic_symbol (info, h))
734 return false;
736 /* If this is a weak defined symbol, and we know a corresponding
737 real symbol from the same dynamic object, make sure the real
738 symbol is also made into a dynamic symbol. */
739 if (h->is_weakalias)
741 struct elf_link_hash_entry *def = weakdef (h);
743 if (def->dynindx == -1
744 && !bfd_elf_link_record_dynamic_symbol (info, def))
745 return false;
749 return true;
752 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
753 success, and 2 on a failure caused by attempting to record a symbol
754 in a discarded section, eg. a discarded link-once section symbol. */
757 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
758 bfd *input_bfd,
759 long input_indx)
761 size_t amt;
762 struct elf_link_local_dynamic_entry *entry;
763 struct elf_link_hash_table *eht;
764 struct elf_strtab_hash *dynstr;
765 size_t dynstr_index;
766 char *name;
767 Elf_External_Sym_Shndx eshndx;
768 char esym[sizeof (Elf64_External_Sym)];
770 if (! is_elf_hash_table (info->hash))
771 return 0;
773 /* See if the entry exists already. */
774 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
775 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
776 return 1;
778 amt = sizeof (*entry);
779 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
780 if (entry == NULL)
781 return 0;
783 /* Go find the symbol, so that we can find it's name. */
784 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
785 1, input_indx, &entry->isym, esym, &eshndx))
787 bfd_release (input_bfd, entry);
788 return 0;
791 if (entry->isym.st_shndx != SHN_UNDEF
792 && entry->isym.st_shndx < SHN_LORESERVE)
794 asection *s;
796 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
797 if (s == NULL || bfd_is_abs_section (s->output_section))
799 /* We can still bfd_release here as nothing has done another
800 bfd_alloc. We can't do this later in this function. */
801 bfd_release (input_bfd, entry);
802 return 2;
806 name = (bfd_elf_string_from_elf_section
807 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
808 entry->isym.st_name));
810 dynstr = elf_hash_table (info)->dynstr;
811 if (dynstr == NULL)
813 /* Create a strtab to hold the dynamic symbol names. */
814 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
815 if (dynstr == NULL)
816 return 0;
819 dynstr_index = _bfd_elf_strtab_add (dynstr, name, false);
820 if (dynstr_index == (size_t) -1)
821 return 0;
822 entry->isym.st_name = dynstr_index;
824 eht = elf_hash_table (info);
826 entry->next = eht->dynlocal;
827 eht->dynlocal = entry;
828 entry->input_bfd = input_bfd;
829 entry->input_indx = input_indx;
830 eht->dynsymcount++;
832 /* Whatever binding the symbol had before, it's now local. */
833 entry->isym.st_info
834 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
836 /* The dynindx will be set at the end of size_dynamic_sections. */
838 return 1;
841 /* Return the dynindex of a local dynamic symbol. */
843 long
844 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
845 bfd *input_bfd,
846 long input_indx)
848 struct elf_link_local_dynamic_entry *e;
850 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
851 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
852 return e->dynindx;
853 return -1;
856 /* This function is used to renumber the dynamic symbols, if some of
857 them are removed because they are marked as local. This is called
858 via elf_link_hash_traverse. */
860 static bool
861 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
862 void *data)
864 size_t *count = (size_t *) data;
866 if (h->forced_local)
867 return true;
869 if (h->dynindx != -1)
870 h->dynindx = ++(*count);
872 return true;
876 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
877 STB_LOCAL binding. */
879 static bool
880 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
881 void *data)
883 size_t *count = (size_t *) data;
885 if (!h->forced_local)
886 return true;
888 if (h->dynindx != -1)
889 h->dynindx = ++(*count);
891 return true;
894 /* Return true if the dynamic symbol for a given section should be
895 omitted when creating a shared library. */
896 bool
897 _bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED,
898 struct bfd_link_info *info,
899 asection *p)
901 struct elf_link_hash_table *htab;
902 asection *ip;
904 switch (elf_section_data (p)->this_hdr.sh_type)
906 case SHT_PROGBITS:
907 case SHT_NOBITS:
908 /* If sh_type is yet undecided, assume it could be
909 SHT_PROGBITS/SHT_NOBITS. */
910 case SHT_NULL:
911 htab = elf_hash_table (info);
912 if (htab->text_index_section != NULL)
913 return p != htab->text_index_section && p != htab->data_index_section;
915 return (htab->dynobj != NULL
916 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
917 && ip->output_section == p);
919 /* There shouldn't be section relative relocations
920 against any other section. */
921 default:
922 return true;
926 bool
927 _bfd_elf_omit_section_dynsym_all
928 (bfd *output_bfd ATTRIBUTE_UNUSED,
929 struct bfd_link_info *info ATTRIBUTE_UNUSED,
930 asection *p ATTRIBUTE_UNUSED)
932 return true;
935 /* Assign dynsym indices. In a shared library we generate a section
936 symbol for each output section, which come first. Next come symbols
937 which have been forced to local binding. Then all of the back-end
938 allocated local dynamic syms, followed by the rest of the global
939 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
940 (This prevents the early call before elf_backend_init_index_section
941 and strip_excluded_output_sections setting dynindx for sections
942 that are stripped.) */
944 static unsigned long
945 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
946 struct bfd_link_info *info,
947 unsigned long *section_sym_count)
949 unsigned long dynsymcount = 0;
950 bool do_sec = section_sym_count != NULL;
952 if (bfd_link_pic (info)
953 || elf_hash_table (info)->is_relocatable_executable)
955 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
956 asection *p;
957 for (p = output_bfd->sections; p ; p = p->next)
958 if ((p->flags & SEC_EXCLUDE) == 0
959 && (p->flags & SEC_ALLOC) != 0
960 && elf_hash_table (info)->dynamic_relocs
961 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
963 ++dynsymcount;
964 if (do_sec)
965 elf_section_data (p)->dynindx = dynsymcount;
967 else if (do_sec)
968 elf_section_data (p)->dynindx = 0;
970 if (do_sec)
971 *section_sym_count = dynsymcount;
973 elf_link_hash_traverse (elf_hash_table (info),
974 elf_link_renumber_local_hash_table_dynsyms,
975 &dynsymcount);
977 if (elf_hash_table (info)->dynlocal)
979 struct elf_link_local_dynamic_entry *p;
980 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
981 p->dynindx = ++dynsymcount;
983 elf_hash_table (info)->local_dynsymcount = dynsymcount;
985 elf_link_hash_traverse (elf_hash_table (info),
986 elf_link_renumber_hash_table_dynsyms,
987 &dynsymcount);
989 /* There is an unused NULL entry at the head of the table which we
990 must account for in our count even if the table is empty since it
991 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
992 .dynamic section. */
993 dynsymcount++;
995 elf_hash_table (info)->dynsymcount = dynsymcount;
996 return dynsymcount;
999 /* Merge st_other field. */
1001 static void
1002 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
1003 unsigned int st_other, asection *sec,
1004 bool definition, bool dynamic)
1006 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1008 /* If st_other has a processor-specific meaning, specific
1009 code might be needed here. */
1010 if (bed->elf_backend_merge_symbol_attribute)
1011 (*bed->elf_backend_merge_symbol_attribute) (h, st_other, definition,
1012 dynamic);
1014 if (!dynamic)
1016 unsigned symvis = ELF_ST_VISIBILITY (st_other);
1017 unsigned hvis = ELF_ST_VISIBILITY (h->other);
1019 /* Keep the most constraining visibility. Leave the remainder
1020 of the st_other field to elf_backend_merge_symbol_attribute. */
1021 if (symvis - 1 < hvis - 1)
1022 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
1024 else if (definition
1025 && ELF_ST_VISIBILITY (st_other) != STV_DEFAULT
1026 && (sec->flags & SEC_READONLY) == 0)
1027 h->protected_def = 1;
1030 /* This function is called when we want to merge a new symbol with an
1031 existing symbol. It handles the various cases which arise when we
1032 find a definition in a dynamic object, or when there is already a
1033 definition in a dynamic object. The new symbol is described by
1034 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1035 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1036 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1037 of an old common symbol. We set OVERRIDE if the old symbol is
1038 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1039 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1040 to change. By OK to change, we mean that we shouldn't warn if the
1041 type or size does change. */
1043 static bool
1044 _bfd_elf_merge_symbol (bfd *abfd,
1045 struct bfd_link_info *info,
1046 const char *name,
1047 Elf_Internal_Sym *sym,
1048 asection **psec,
1049 bfd_vma *pvalue,
1050 struct elf_link_hash_entry **sym_hash,
1051 bfd **poldbfd,
1052 bool *pold_weak,
1053 unsigned int *pold_alignment,
1054 bool *skip,
1055 bfd **override,
1056 bool *type_change_ok,
1057 bool *size_change_ok,
1058 bool *matched)
1060 asection *sec, *oldsec;
1061 struct elf_link_hash_entry *h;
1062 struct elf_link_hash_entry *hi;
1063 struct elf_link_hash_entry *flip;
1064 int bind;
1065 bfd *oldbfd;
1066 bool newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
1067 bool newweak, oldweak, newfunc, oldfunc;
1068 const struct elf_backend_data *bed;
1069 char *new_version;
1070 bool default_sym = *matched;
1071 struct elf_link_hash_table *htab;
1073 *skip = false;
1074 *override = NULL;
1076 sec = *psec;
1077 bind = ELF_ST_BIND (sym->st_info);
1079 if (! bfd_is_und_section (sec))
1080 h = elf_link_hash_lookup (elf_hash_table (info), name, true, false, false);
1081 else
1082 h = ((struct elf_link_hash_entry *)
1083 bfd_wrapped_link_hash_lookup (abfd, info, name, true, false, false));
1084 if (h == NULL)
1085 return false;
1086 *sym_hash = h;
1088 bed = get_elf_backend_data (abfd);
1090 /* NEW_VERSION is the symbol version of the new symbol. */
1091 if (h->versioned != unversioned)
1093 /* Symbol version is unknown or versioned. */
1094 new_version = strrchr (name, ELF_VER_CHR);
1095 if (new_version)
1097 if (h->versioned == unknown)
1099 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1100 h->versioned = versioned_hidden;
1101 else
1102 h->versioned = versioned;
1104 new_version += 1;
1105 if (new_version[0] == '\0')
1106 new_version = NULL;
1108 else
1109 h->versioned = unversioned;
1111 else
1112 new_version = NULL;
1114 /* For merging, we only care about real symbols. But we need to make
1115 sure that indirect symbol dynamic flags are updated. */
1116 hi = h;
1117 while (h->root.type == bfd_link_hash_indirect
1118 || h->root.type == bfd_link_hash_warning)
1119 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1121 if (!*matched)
1123 if (hi == h || h->root.type == bfd_link_hash_new)
1124 *matched = true;
1125 else
1127 /* OLD_HIDDEN is true if the existing symbol is only visible
1128 to the symbol with the same symbol version. NEW_HIDDEN is
1129 true if the new symbol is only visible to the symbol with
1130 the same symbol version. */
1131 bool old_hidden = h->versioned == versioned_hidden;
1132 bool new_hidden = hi->versioned == versioned_hidden;
1133 if (!old_hidden && !new_hidden)
1134 /* The new symbol matches the existing symbol if both
1135 aren't hidden. */
1136 *matched = true;
1137 else
1139 /* OLD_VERSION is the symbol version of the existing
1140 symbol. */
1141 char *old_version;
1143 if (h->versioned >= versioned)
1144 old_version = strrchr (h->root.root.string,
1145 ELF_VER_CHR) + 1;
1146 else
1147 old_version = NULL;
1149 /* The new symbol matches the existing symbol if they
1150 have the same symbol version. */
1151 *matched = (old_version == new_version
1152 || (old_version != NULL
1153 && new_version != NULL
1154 && strcmp (old_version, new_version) == 0));
1159 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1160 existing symbol. */
1162 oldbfd = NULL;
1163 oldsec = NULL;
1164 switch (h->root.type)
1166 default:
1167 break;
1169 case bfd_link_hash_undefined:
1170 case bfd_link_hash_undefweak:
1171 oldbfd = h->root.u.undef.abfd;
1172 break;
1174 case bfd_link_hash_defined:
1175 case bfd_link_hash_defweak:
1176 oldbfd = h->root.u.def.section->owner;
1177 oldsec = h->root.u.def.section;
1178 break;
1180 case bfd_link_hash_common:
1181 oldbfd = h->root.u.c.p->section->owner;
1182 oldsec = h->root.u.c.p->section;
1183 if (pold_alignment)
1184 *pold_alignment = h->root.u.c.p->alignment_power;
1185 break;
1187 if (poldbfd && *poldbfd == NULL)
1188 *poldbfd = oldbfd;
1190 /* Differentiate strong and weak symbols. */
1191 newweak = bind == STB_WEAK;
1192 oldweak = (h->root.type == bfd_link_hash_defweak
1193 || h->root.type == bfd_link_hash_undefweak);
1194 if (pold_weak)
1195 *pold_weak = oldweak;
1197 /* We have to check it for every instance since the first few may be
1198 references and not all compilers emit symbol type for undefined
1199 symbols. */
1200 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1202 htab = elf_hash_table (info);
1204 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1205 respectively, is from a dynamic object. */
1207 newdyn = (abfd->flags & DYNAMIC) != 0;
1209 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1210 syms and defined syms in dynamic libraries respectively.
1211 ref_dynamic on the other hand can be set for a symbol defined in
1212 a dynamic library, and def_dynamic may not be set; When the
1213 definition in a dynamic lib is overridden by a definition in the
1214 executable use of the symbol in the dynamic lib becomes a
1215 reference to the executable symbol. */
1216 if (newdyn)
1218 if (bfd_is_und_section (sec))
1220 if (bind != STB_WEAK)
1222 h->ref_dynamic_nonweak = 1;
1223 hi->ref_dynamic_nonweak = 1;
1226 else
1228 /* Update the existing symbol only if they match. */
1229 if (*matched)
1230 h->dynamic_def = 1;
1231 hi->dynamic_def = 1;
1235 /* If we just created the symbol, mark it as being an ELF symbol.
1236 Other than that, there is nothing to do--there is no merge issue
1237 with a newly defined symbol--so we just return. */
1239 if (h->root.type == bfd_link_hash_new)
1241 h->non_elf = 0;
1242 return true;
1245 /* In cases involving weak versioned symbols, we may wind up trying
1246 to merge a symbol with itself. Catch that here, to avoid the
1247 confusion that results if we try to override a symbol with
1248 itself. The additional tests catch cases like
1249 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1250 dynamic object, which we do want to handle here. */
1251 if (abfd == oldbfd
1252 && (newweak || oldweak)
1253 && ((abfd->flags & DYNAMIC) == 0
1254 || !h->def_regular))
1255 return true;
1257 olddyn = false;
1258 if (oldbfd != NULL)
1259 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1260 else if (oldsec != NULL)
1262 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1263 indices used by MIPS ELF. */
1264 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1267 /* Set non_ir_ref_dynamic only when not handling DT_NEEDED entries. */
1268 if (!htab->handling_dt_needed
1269 && oldbfd != NULL
1270 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN))
1272 if (newdyn != olddyn)
1274 /* Handle a case where plugin_notice won't be called and thus
1275 won't set the non_ir_ref flags on the first pass over
1276 symbols. */
1277 h->root.non_ir_ref_dynamic = true;
1278 hi->root.non_ir_ref_dynamic = true;
1280 else if ((oldbfd->flags & BFD_PLUGIN) != 0
1281 && hi->root.type == bfd_link_hash_indirect)
1283 /* Change indirect symbol from IR to undefined. */
1284 hi->root.type = bfd_link_hash_undefined;
1285 hi->root.u.undef.abfd = oldbfd;
1289 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1290 respectively, appear to be a definition rather than reference. */
1292 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1294 olddef = (h->root.type != bfd_link_hash_undefined
1295 && h->root.type != bfd_link_hash_undefweak
1296 && h->root.type != bfd_link_hash_common);
1298 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1299 respectively, appear to be a function. */
1301 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1302 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1304 oldfunc = (h->type != STT_NOTYPE
1305 && bed->is_function_type (h->type));
1307 if (!(newfunc && oldfunc)
1308 && ELF_ST_TYPE (sym->st_info) != h->type
1309 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1310 && h->type != STT_NOTYPE
1311 && (newdef || bfd_is_com_section (sec))
1312 && (olddef || h->root.type == bfd_link_hash_common))
1314 /* If creating a default indirect symbol ("foo" or "foo@") from
1315 a dynamic versioned definition ("foo@@") skip doing so if
1316 there is an existing regular definition with a different
1317 type. We don't want, for example, a "time" variable in the
1318 executable overriding a "time" function in a shared library. */
1319 if (newdyn
1320 && !olddyn)
1322 *skip = true;
1323 return true;
1326 /* When adding a symbol from a regular object file after we have
1327 created indirect symbols, undo the indirection and any
1328 dynamic state. */
1329 if (hi != h
1330 && !newdyn
1331 && olddyn)
1333 h = hi;
1334 (*bed->elf_backend_hide_symbol) (info, h, true);
1335 h->forced_local = 0;
1336 h->ref_dynamic = 0;
1337 h->def_dynamic = 0;
1338 h->dynamic_def = 0;
1339 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1341 h->root.type = bfd_link_hash_undefined;
1342 h->root.u.undef.abfd = abfd;
1344 else
1346 h->root.type = bfd_link_hash_new;
1347 h->root.u.undef.abfd = NULL;
1349 return true;
1353 /* Check TLS symbols. We don't check undefined symbols introduced
1354 by "ld -u" which have no type (and oldbfd NULL), and we don't
1355 check symbols from plugins because they also have no type. */
1356 if (oldbfd != NULL
1357 && (oldbfd->flags & BFD_PLUGIN) == 0
1358 && (abfd->flags & BFD_PLUGIN) == 0
1359 && ELF_ST_TYPE (sym->st_info) != h->type
1360 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1362 bfd *ntbfd, *tbfd;
1363 bool ntdef, tdef;
1364 asection *ntsec, *tsec;
1366 if (h->type == STT_TLS)
1368 ntbfd = abfd;
1369 ntsec = sec;
1370 ntdef = newdef;
1371 tbfd = oldbfd;
1372 tsec = oldsec;
1373 tdef = olddef;
1375 else
1377 ntbfd = oldbfd;
1378 ntsec = oldsec;
1379 ntdef = olddef;
1380 tbfd = abfd;
1381 tsec = sec;
1382 tdef = newdef;
1385 if (tdef && ntdef)
1386 _bfd_error_handler
1387 /* xgettext:c-format */
1388 (_("%s: TLS definition in %pB section %pA "
1389 "mismatches non-TLS definition in %pB section %pA"),
1390 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
1391 else if (!tdef && !ntdef)
1392 _bfd_error_handler
1393 /* xgettext:c-format */
1394 (_("%s: TLS reference in %pB "
1395 "mismatches non-TLS reference in %pB"),
1396 h->root.root.string, tbfd, ntbfd);
1397 else if (tdef)
1398 _bfd_error_handler
1399 /* xgettext:c-format */
1400 (_("%s: TLS definition in %pB section %pA "
1401 "mismatches non-TLS reference in %pB"),
1402 h->root.root.string, tbfd, tsec, ntbfd);
1403 else
1404 _bfd_error_handler
1405 /* xgettext:c-format */
1406 (_("%s: TLS reference in %pB "
1407 "mismatches non-TLS definition in %pB section %pA"),
1408 h->root.root.string, tbfd, ntbfd, ntsec);
1410 bfd_set_error (bfd_error_bad_value);
1411 return false;
1414 /* If the old symbol has non-default visibility, we ignore the new
1415 definition from a dynamic object. */
1416 if (newdyn
1417 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1418 && !bfd_is_und_section (sec))
1420 *skip = true;
1421 /* Make sure this symbol is dynamic. */
1422 h->ref_dynamic = 1;
1423 hi->ref_dynamic = 1;
1424 /* A protected symbol has external availability. Make sure it is
1425 recorded as dynamic.
1427 FIXME: Should we check type and size for protected symbol? */
1428 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1429 return bfd_elf_link_record_dynamic_symbol (info, h);
1430 else
1431 return true;
1433 else if (!newdyn
1434 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1435 && h->def_dynamic)
1437 /* If the new symbol with non-default visibility comes from a
1438 relocatable file and the old definition comes from a dynamic
1439 object, we remove the old definition. */
1440 if (hi->root.type == bfd_link_hash_indirect)
1442 /* Handle the case where the old dynamic definition is
1443 default versioned. We need to copy the symbol info from
1444 the symbol with default version to the normal one if it
1445 was referenced before. */
1446 if (h->ref_regular)
1448 hi->root.type = h->root.type;
1449 h->root.type = bfd_link_hash_indirect;
1450 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1452 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1453 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1455 /* If the new symbol is hidden or internal, completely undo
1456 any dynamic link state. */
1457 (*bed->elf_backend_hide_symbol) (info, h, true);
1458 h->forced_local = 0;
1459 h->ref_dynamic = 0;
1461 else
1462 h->ref_dynamic = 1;
1464 h->def_dynamic = 0;
1465 /* FIXME: Should we check type and size for protected symbol? */
1466 h->size = 0;
1467 h->type = 0;
1469 h = hi;
1471 else
1472 h = hi;
1475 /* If the old symbol was undefined before, then it will still be
1476 on the undefs list. If the new symbol is undefined or
1477 common, we can't make it bfd_link_hash_new here, because new
1478 undefined or common symbols will be added to the undefs list
1479 by _bfd_generic_link_add_one_symbol. Symbols may not be
1480 added twice to the undefs list. Also, if the new symbol is
1481 undefweak then we don't want to lose the strong undef. */
1482 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1484 h->root.type = bfd_link_hash_undefined;
1485 h->root.u.undef.abfd = abfd;
1487 else
1489 h->root.type = bfd_link_hash_new;
1490 h->root.u.undef.abfd = NULL;
1493 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1495 /* If the new symbol is hidden or internal, completely undo
1496 any dynamic link state. */
1497 (*bed->elf_backend_hide_symbol) (info, h, true);
1498 h->forced_local = 0;
1499 h->ref_dynamic = 0;
1501 else
1502 h->ref_dynamic = 1;
1503 h->def_dynamic = 0;
1504 /* FIXME: Should we check type and size for protected symbol? */
1505 h->size = 0;
1506 h->type = 0;
1507 return true;
1510 /* If a new weak symbol definition comes from a regular file and the
1511 old symbol comes from a dynamic library, we treat the new one as
1512 strong. Similarly, an old weak symbol definition from a regular
1513 file is treated as strong when the new symbol comes from a dynamic
1514 library. Further, an old weak symbol from a dynamic library is
1515 treated as strong if the new symbol is from a dynamic library.
1516 This reflects the way glibc's ld.so works.
1518 Also allow a weak symbol to override a linker script symbol
1519 defined by an early pass over the script. This is done so the
1520 linker knows the symbol is defined in an object file, for the
1521 DEFINED script function.
1523 Do this before setting *type_change_ok or *size_change_ok so that
1524 we warn properly when dynamic library symbols are overridden. */
1526 if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
1527 newweak = false;
1528 if (olddef && newdyn)
1529 oldweak = false;
1531 /* Allow changes between different types of function symbol. */
1532 if (newfunc && oldfunc)
1533 *type_change_ok = true;
1535 /* It's OK to change the type if either the existing symbol or the
1536 new symbol is weak. A type change is also OK if the old symbol
1537 is undefined and the new symbol is defined. */
1539 if (oldweak
1540 || newweak
1541 || (newdef
1542 && h->root.type == bfd_link_hash_undefined))
1543 *type_change_ok = true;
1545 /* It's OK to change the size if either the existing symbol or the
1546 new symbol is weak, or if the old symbol is undefined. */
1548 if (*type_change_ok
1549 || h->root.type == bfd_link_hash_undefined)
1550 *size_change_ok = true;
1552 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1553 symbol, respectively, appears to be a common symbol in a dynamic
1554 object. If a symbol appears in an uninitialized section, and is
1555 not weak, and is not a function, then it may be a common symbol
1556 which was resolved when the dynamic object was created. We want
1557 to treat such symbols specially, because they raise special
1558 considerations when setting the symbol size: if the symbol
1559 appears as a common symbol in a regular object, and the size in
1560 the regular object is larger, we must make sure that we use the
1561 larger size. This problematic case can always be avoided in C,
1562 but it must be handled correctly when using Fortran shared
1563 libraries.
1565 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1566 likewise for OLDDYNCOMMON and OLDDEF.
1568 Note that this test is just a heuristic, and that it is quite
1569 possible to have an uninitialized symbol in a shared object which
1570 is really a definition, rather than a common symbol. This could
1571 lead to some minor confusion when the symbol really is a common
1572 symbol in some regular object. However, I think it will be
1573 harmless. */
1575 if (newdyn
1576 && newdef
1577 && !newweak
1578 && (sec->flags & SEC_ALLOC) != 0
1579 && (sec->flags & SEC_LOAD) == 0
1580 && sym->st_size > 0
1581 && !newfunc)
1582 newdyncommon = true;
1583 else
1584 newdyncommon = false;
1586 if (olddyn
1587 && olddef
1588 && h->root.type == bfd_link_hash_defined
1589 && h->def_dynamic
1590 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1591 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1592 && h->size > 0
1593 && !oldfunc)
1594 olddyncommon = true;
1595 else
1596 olddyncommon = false;
1598 /* We now know everything about the old and new symbols. We ask the
1599 backend to check if we can merge them. */
1600 if (bed->merge_symbol != NULL)
1602 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1603 return false;
1604 sec = *psec;
1607 /* There are multiple definitions of a normal symbol. Skip the
1608 default symbol as well as definition from an IR object. */
1609 if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
1610 && !default_sym && h->def_regular
1611 && !(oldbfd != NULL
1612 && (oldbfd->flags & BFD_PLUGIN) != 0
1613 && (abfd->flags & BFD_PLUGIN) == 0))
1615 /* Handle a multiple definition. */
1616 (*info->callbacks->multiple_definition) (info, &h->root,
1617 abfd, sec, *pvalue);
1618 *skip = true;
1619 return true;
1622 /* If both the old and the new symbols look like common symbols in a
1623 dynamic object, set the size of the symbol to the larger of the
1624 two. */
1626 if (olddyncommon
1627 && newdyncommon
1628 && sym->st_size != h->size)
1630 /* Since we think we have two common symbols, issue a multiple
1631 common warning if desired. Note that we only warn if the
1632 size is different. If the size is the same, we simply let
1633 the old symbol override the new one as normally happens with
1634 symbols defined in dynamic objects. */
1636 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1637 bfd_link_hash_common, sym->st_size);
1638 if (sym->st_size > h->size)
1639 h->size = sym->st_size;
1641 *size_change_ok = true;
1644 /* If we are looking at a dynamic object, and we have found a
1645 definition, we need to see if the symbol was already defined by
1646 some other object. If so, we want to use the existing
1647 definition, and we do not want to report a multiple symbol
1648 definition error; we do this by clobbering *PSEC to be
1649 bfd_und_section_ptr.
1651 We treat a common symbol as a definition if the symbol in the
1652 shared library is a function, since common symbols always
1653 represent variables; this can cause confusion in principle, but
1654 any such confusion would seem to indicate an erroneous program or
1655 shared library. We also permit a common symbol in a regular
1656 object to override a weak symbol in a shared object. */
1658 if (newdyn
1659 && newdef
1660 && (olddef
1661 || (h->root.type == bfd_link_hash_common
1662 && (newweak || newfunc))))
1664 *override = abfd;
1665 newdef = false;
1666 newdyncommon = false;
1668 *psec = sec = bfd_und_section_ptr;
1669 *size_change_ok = true;
1671 /* If we get here when the old symbol is a common symbol, then
1672 we are explicitly letting it override a weak symbol or
1673 function in a dynamic object, and we don't want to warn about
1674 a type change. If the old symbol is a defined symbol, a type
1675 change warning may still be appropriate. */
1677 if (h->root.type == bfd_link_hash_common)
1678 *type_change_ok = true;
1681 /* Handle the special case of an old common symbol merging with a
1682 new symbol which looks like a common symbol in a shared object.
1683 We change *PSEC and *PVALUE to make the new symbol look like a
1684 common symbol, and let _bfd_generic_link_add_one_symbol do the
1685 right thing. */
1687 if (newdyncommon
1688 && h->root.type == bfd_link_hash_common)
1690 *override = oldbfd;
1691 newdef = false;
1692 newdyncommon = false;
1693 *pvalue = sym->st_size;
1694 *psec = sec = bed->common_section (oldsec);
1695 *size_change_ok = true;
1698 /* Skip weak definitions of symbols that are already defined. */
1699 if (newdef && olddef && newweak)
1701 /* Don't skip new non-IR weak syms. */
1702 if (!(oldbfd != NULL
1703 && (oldbfd->flags & BFD_PLUGIN) != 0
1704 && (abfd->flags & BFD_PLUGIN) == 0))
1706 newdef = false;
1707 *skip = true;
1710 /* Merge st_other. If the symbol already has a dynamic index,
1711 but visibility says it should not be visible, turn it into a
1712 local symbol. */
1713 elf_merge_st_other (abfd, h, sym->st_other, sec, newdef, newdyn);
1714 if (h->dynindx != -1)
1715 switch (ELF_ST_VISIBILITY (h->other))
1717 case STV_INTERNAL:
1718 case STV_HIDDEN:
1719 (*bed->elf_backend_hide_symbol) (info, h, true);
1720 break;
1724 /* If the old symbol is from a dynamic object, and the new symbol is
1725 a definition which is not from a dynamic object, then the new
1726 symbol overrides the old symbol. Symbols from regular files
1727 always take precedence over symbols from dynamic objects, even if
1728 they are defined after the dynamic object in the link.
1730 As above, we again permit a common symbol in a regular object to
1731 override a definition in a shared object if the shared object
1732 symbol is a function or is weak. */
1734 flip = NULL;
1735 if (!newdyn
1736 && (newdef
1737 || (bfd_is_com_section (sec)
1738 && (oldweak || oldfunc)))
1739 && olddyn
1740 && olddef
1741 && h->def_dynamic)
1743 /* Change the hash table entry to undefined, and let
1744 _bfd_generic_link_add_one_symbol do the right thing with the
1745 new definition. */
1747 h->root.type = bfd_link_hash_undefined;
1748 h->root.u.undef.abfd = h->root.u.def.section->owner;
1749 *size_change_ok = true;
1751 olddef = false;
1752 olddyncommon = false;
1754 /* We again permit a type change when a common symbol may be
1755 overriding a function. */
1757 if (bfd_is_com_section (sec))
1759 if (oldfunc)
1761 /* If a common symbol overrides a function, make sure
1762 that it isn't defined dynamically nor has type
1763 function. */
1764 h->def_dynamic = 0;
1765 h->type = STT_NOTYPE;
1767 *type_change_ok = true;
1770 if (hi->root.type == bfd_link_hash_indirect)
1771 flip = hi;
1772 else
1773 /* This union may have been set to be non-NULL when this symbol
1774 was seen in a dynamic object. We must force the union to be
1775 NULL, so that it is correct for a regular symbol. */
1776 h->verinfo.vertree = NULL;
1779 /* Handle the special case of a new common symbol merging with an
1780 old symbol that looks like it might be a common symbol defined in
1781 a shared object. Note that we have already handled the case in
1782 which a new common symbol should simply override the definition
1783 in the shared library. */
1785 if (! newdyn
1786 && bfd_is_com_section (sec)
1787 && olddyncommon)
1789 /* It would be best if we could set the hash table entry to a
1790 common symbol, but we don't know what to use for the section
1791 or the alignment. */
1792 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1793 bfd_link_hash_common, sym->st_size);
1795 /* If the presumed common symbol in the dynamic object is
1796 larger, pretend that the new symbol has its size. */
1798 if (h->size > *pvalue)
1799 *pvalue = h->size;
1801 /* We need to remember the alignment required by the symbol
1802 in the dynamic object. */
1803 BFD_ASSERT (pold_alignment);
1804 *pold_alignment = h->root.u.def.section->alignment_power;
1806 olddef = false;
1807 olddyncommon = false;
1809 h->root.type = bfd_link_hash_undefined;
1810 h->root.u.undef.abfd = h->root.u.def.section->owner;
1812 *size_change_ok = true;
1813 *type_change_ok = true;
1815 if (hi->root.type == bfd_link_hash_indirect)
1816 flip = hi;
1817 else
1818 h->verinfo.vertree = NULL;
1821 if (flip != NULL)
1823 /* Handle the case where we had a versioned symbol in a dynamic
1824 library and now find a definition in a normal object. In this
1825 case, we make the versioned symbol point to the normal one. */
1826 flip->root.type = h->root.type;
1827 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1828 h->root.type = bfd_link_hash_indirect;
1829 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1830 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1831 if (h->def_dynamic)
1833 h->def_dynamic = 0;
1834 flip->ref_dynamic = 1;
1838 return true;
1841 /* This function is called to create an indirect symbol from the
1842 default for the symbol with the default version if needed. The
1843 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1844 set DYNSYM if the new indirect symbol is dynamic. */
1846 static bool
1847 _bfd_elf_add_default_symbol (bfd *abfd,
1848 struct bfd_link_info *info,
1849 struct elf_link_hash_entry *h,
1850 const char *name,
1851 Elf_Internal_Sym *sym,
1852 asection *sec,
1853 bfd_vma value,
1854 bfd **poldbfd,
1855 bool *dynsym)
1857 bool type_change_ok;
1858 bool size_change_ok;
1859 bool skip;
1860 char *shortname;
1861 struct elf_link_hash_entry *hi;
1862 struct bfd_link_hash_entry *bh;
1863 const struct elf_backend_data *bed;
1864 bool collect;
1865 bool dynamic;
1866 bfd *override;
1867 char *p;
1868 size_t len, shortlen;
1869 asection *tmp_sec;
1870 bool matched;
1872 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1873 return true;
1875 /* If this symbol has a version, and it is the default version, we
1876 create an indirect symbol from the default name to the fully
1877 decorated name. This will cause external references which do not
1878 specify a version to be bound to this version of the symbol. */
1879 p = strchr (name, ELF_VER_CHR);
1880 if (h->versioned == unknown)
1882 if (p == NULL)
1884 h->versioned = unversioned;
1885 return true;
1887 else
1889 if (p[1] != ELF_VER_CHR)
1891 h->versioned = versioned_hidden;
1892 return true;
1894 else
1895 h->versioned = versioned;
1898 else
1900 /* PR ld/19073: We may see an unversioned definition after the
1901 default version. */
1902 if (p == NULL)
1903 return true;
1906 bed = get_elf_backend_data (abfd);
1907 collect = bed->collect;
1908 dynamic = (abfd->flags & DYNAMIC) != 0;
1910 shortlen = p - name;
1911 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1912 if (shortname == NULL)
1913 return false;
1914 memcpy (shortname, name, shortlen);
1915 shortname[shortlen] = '\0';
1917 /* We are going to create a new symbol. Merge it with any existing
1918 symbol with this name. For the purposes of the merge, act as
1919 though we were defining the symbol we just defined, although we
1920 actually going to define an indirect symbol. */
1921 type_change_ok = false;
1922 size_change_ok = false;
1923 matched = true;
1924 tmp_sec = sec;
1925 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1926 &hi, poldbfd, NULL, NULL, &skip, &override,
1927 &type_change_ok, &size_change_ok, &matched))
1928 return false;
1930 if (skip)
1931 goto nondefault;
1933 if (hi->def_regular || ELF_COMMON_DEF_P (hi))
1935 /* If the undecorated symbol will have a version added by a
1936 script different to H, then don't indirect to/from the
1937 undecorated symbol. This isn't ideal because we may not yet
1938 have seen symbol versions, if given by a script on the
1939 command line rather than via --version-script. */
1940 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1942 bool hide;
1944 hi->verinfo.vertree
1945 = bfd_find_version_for_sym (info->version_info,
1946 hi->root.root.string, &hide);
1947 if (hi->verinfo.vertree != NULL && hide)
1949 (*bed->elf_backend_hide_symbol) (info, hi, true);
1950 goto nondefault;
1953 if (hi->verinfo.vertree != NULL
1954 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1955 goto nondefault;
1958 if (! override)
1960 /* Add the default symbol if not performing a relocatable link. */
1961 if (! bfd_link_relocatable (info))
1963 bh = &hi->root;
1964 if (bh->type == bfd_link_hash_defined
1965 && bh->u.def.section->owner != NULL
1966 && (bh->u.def.section->owner->flags & BFD_PLUGIN) != 0)
1968 /* Mark the previous definition from IR object as
1969 undefined so that the generic linker will override
1970 it. */
1971 bh->type = bfd_link_hash_undefined;
1972 bh->u.undef.abfd = bh->u.def.section->owner;
1974 if (! (_bfd_generic_link_add_one_symbol
1975 (info, abfd, shortname, BSF_INDIRECT,
1976 bfd_ind_section_ptr,
1977 0, name, false, collect, &bh)))
1978 return false;
1979 hi = (struct elf_link_hash_entry *) bh;
1982 else
1984 /* In this case the symbol named SHORTNAME is overriding the
1985 indirect symbol we want to add. We were planning on making
1986 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1987 is the name without a version. NAME is the fully versioned
1988 name, and it is the default version.
1990 Overriding means that we already saw a definition for the
1991 symbol SHORTNAME in a regular object, and it is overriding
1992 the symbol defined in the dynamic object.
1994 When this happens, we actually want to change NAME, the
1995 symbol we just added, to refer to SHORTNAME. This will cause
1996 references to NAME in the shared object to become references
1997 to SHORTNAME in the regular object. This is what we expect
1998 when we override a function in a shared object: that the
1999 references in the shared object will be mapped to the
2000 definition in the regular object. */
2002 while (hi->root.type == bfd_link_hash_indirect
2003 || hi->root.type == bfd_link_hash_warning)
2004 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2006 h->root.type = bfd_link_hash_indirect;
2007 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
2008 if (h->def_dynamic)
2010 h->def_dynamic = 0;
2011 hi->ref_dynamic = 1;
2012 if (hi->ref_regular
2013 || hi->def_regular)
2015 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
2016 return false;
2020 /* Now set HI to H, so that the following code will set the
2021 other fields correctly. */
2022 hi = h;
2025 /* Check if HI is a warning symbol. */
2026 if (hi->root.type == bfd_link_hash_warning)
2027 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2029 /* If there is a duplicate definition somewhere, then HI may not
2030 point to an indirect symbol. We will have reported an error to
2031 the user in that case. */
2033 if (hi->root.type == bfd_link_hash_indirect)
2035 struct elf_link_hash_entry *ht;
2037 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
2038 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
2040 /* If we first saw a reference to SHORTNAME with non-default
2041 visibility, merge that visibility to the @@VER symbol. */
2042 elf_merge_st_other (abfd, ht, hi->other, sec, true, dynamic);
2044 /* A reference to the SHORTNAME symbol from a dynamic library
2045 will be satisfied by the versioned symbol at runtime. In
2046 effect, we have a reference to the versioned symbol. */
2047 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2048 hi->dynamic_def |= ht->dynamic_def;
2050 /* See if the new flags lead us to realize that the symbol must
2051 be dynamic. */
2052 if (! *dynsym)
2054 if (! dynamic)
2056 if (! bfd_link_executable (info)
2057 || hi->def_dynamic
2058 || hi->ref_dynamic)
2059 *dynsym = true;
2061 else
2063 if (hi->ref_regular)
2064 *dynsym = true;
2069 /* We also need to define an indirection from the nondefault version
2070 of the symbol. */
2072 nondefault:
2073 len = strlen (name);
2074 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
2075 if (shortname == NULL)
2076 return false;
2077 memcpy (shortname, name, shortlen);
2078 memcpy (shortname + shortlen, p + 1, len - shortlen);
2080 /* Once again, merge with any existing symbol. */
2081 type_change_ok = false;
2082 size_change_ok = false;
2083 tmp_sec = sec;
2084 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
2085 &hi, poldbfd, NULL, NULL, &skip, &override,
2086 &type_change_ok, &size_change_ok, &matched))
2087 return false;
2089 if (skip)
2091 if (!dynamic
2092 && h->root.type == bfd_link_hash_defweak
2093 && hi->root.type == bfd_link_hash_defined)
2095 /* We are handling a weak sym@@ver and attempting to define
2096 a weak sym@ver, but _bfd_elf_merge_symbol said to skip the
2097 new weak sym@ver because there is already a strong sym@ver.
2098 However, sym@ver and sym@@ver are really the same symbol.
2099 The existing strong sym@ver ought to override sym@@ver. */
2100 h->root.type = bfd_link_hash_defined;
2101 h->root.u.def.section = hi->root.u.def.section;
2102 h->root.u.def.value = hi->root.u.def.value;
2103 hi->root.type = bfd_link_hash_indirect;
2104 hi->root.u.i.link = &h->root;
2106 else
2107 return true;
2109 else if (override)
2111 /* Here SHORTNAME is a versioned name, so we don't expect to see
2112 the type of override we do in the case above unless it is
2113 overridden by a versioned definition. */
2114 if (hi->root.type != bfd_link_hash_defined
2115 && hi->root.type != bfd_link_hash_defweak)
2116 _bfd_error_handler
2117 /* xgettext:c-format */
2118 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
2119 abfd, shortname);
2120 return true;
2122 else
2124 bh = &hi->root;
2125 if (! (_bfd_generic_link_add_one_symbol
2126 (info, abfd, shortname, BSF_INDIRECT,
2127 bfd_ind_section_ptr, 0, name, false, collect, &bh)))
2128 return false;
2129 hi = (struct elf_link_hash_entry *) bh;
2132 /* If there is a duplicate definition somewhere, then HI may not
2133 point to an indirect symbol. We will have reported an error
2134 to the user in that case. */
2135 if (hi->root.type == bfd_link_hash_indirect)
2137 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
2138 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2139 hi->dynamic_def |= h->dynamic_def;
2141 /* If we first saw a reference to @VER symbol with
2142 non-default visibility, merge that visibility to the
2143 @@VER symbol. */
2144 elf_merge_st_other (abfd, h, hi->other, sec, true, dynamic);
2146 /* See if the new flags lead us to realize that the symbol
2147 must be dynamic. */
2148 if (! *dynsym)
2150 if (! dynamic)
2152 if (! bfd_link_executable (info)
2153 || hi->ref_dynamic)
2154 *dynsym = true;
2156 else
2158 if (hi->ref_regular)
2159 *dynsym = true;
2164 return true;
2167 /* This routine is used to export all defined symbols into the dynamic
2168 symbol table. It is called via elf_link_hash_traverse. */
2170 static bool
2171 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
2173 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2175 /* Ignore indirect symbols. These are added by the versioning code. */
2176 if (h->root.type == bfd_link_hash_indirect)
2177 return true;
2179 /* Ignore this if we won't export it. */
2180 if (!eif->info->export_dynamic && !h->dynamic)
2181 return true;
2183 if (h->dynindx == -1
2184 && (h->def_regular || h->ref_regular)
2185 && ! bfd_hide_sym_by_version (eif->info->version_info,
2186 h->root.root.string))
2188 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2190 eif->failed = true;
2191 return false;
2195 return true;
2198 /* Return the glibc version reference if VERSION_DEP is added to the
2199 list of glibc version dependencies successfully. VERSION_DEP will
2200 be put into the .gnu.version_r section. */
2202 static Elf_Internal_Verneed *
2203 elf_link_add_glibc_verneed (struct elf_find_verdep_info *rinfo,
2204 Elf_Internal_Verneed *glibc_verref,
2205 const char *version_dep)
2207 Elf_Internal_Verneed *t;
2208 Elf_Internal_Vernaux *a;
2209 size_t amt;
2211 if (glibc_verref != NULL)
2213 t = glibc_verref;
2215 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2217 /* Return if VERSION_DEP dependency has been added. */
2218 if (a->vna_nodename == version_dep
2219 || strcmp (a->vna_nodename, version_dep) == 0)
2220 return t;
2223 else
2225 bool is_glibc;
2227 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2228 t != NULL;
2229 t = t->vn_nextref)
2231 const char *soname = bfd_elf_get_dt_soname (t->vn_bfd);
2232 if (soname != NULL && startswith (soname, "libc.so."))
2233 break;
2236 /* Skip the shared library if it isn't libc.so. */
2237 if (t == NULL)
2238 return t;
2240 is_glibc = false;
2241 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2243 /* Return if VERSION_DEP dependency has been added. */
2244 if (a->vna_nodename == version_dep
2245 || strcmp (a->vna_nodename, version_dep) == 0)
2246 return t;
2248 /* Check if libc.so provides GLIBC_2.XX version. */
2249 if (!is_glibc && startswith (a->vna_nodename, "GLIBC_2."))
2250 is_glibc = true;
2253 /* Skip if it isn't linked against glibc. */
2254 if (!is_glibc)
2255 return NULL;
2258 amt = sizeof *a;
2259 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2260 if (a == NULL)
2262 rinfo->failed = true;
2263 return NULL;
2266 a->vna_nodename = version_dep;
2267 a->vna_flags = 0;
2268 a->vna_nextptr = t->vn_auxptr;
2269 a->vna_other = rinfo->vers + 1;
2270 ++rinfo->vers;
2272 t->vn_auxptr = a;
2274 return t;
2277 /* Add VERSION_DEP to the list of version dependencies when linked
2278 against glibc. */
2280 void
2281 _bfd_elf_link_add_glibc_version_dependency
2282 (struct elf_find_verdep_info *rinfo,
2283 const char *version_dep[])
2285 Elf_Internal_Verneed *t = NULL;
2289 t = elf_link_add_glibc_verneed (rinfo, t, *version_dep);
2290 /* Return if there is no glibc version reference. */
2291 if (t == NULL)
2292 return;
2293 version_dep++;
2295 while (*version_dep != NULL);
2298 /* Add GLIBC_ABI_DT_RELR to the list of version dependencies when
2299 linked against glibc. */
2301 void
2302 _bfd_elf_link_add_dt_relr_dependency (struct elf_find_verdep_info *rinfo)
2304 if (rinfo->info->enable_dt_relr)
2306 const char *version[] =
2308 "GLIBC_ABI_DT_RELR",
2309 NULL
2311 _bfd_elf_link_add_glibc_version_dependency (rinfo, version);
2315 /* Look through the symbols which are defined in other shared
2316 libraries and referenced here. Update the list of version
2317 dependencies. This will be put into the .gnu.version_r section.
2318 This function is called via elf_link_hash_traverse. */
2320 static bool
2321 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2322 void *data)
2324 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2325 Elf_Internal_Verneed *t;
2326 Elf_Internal_Vernaux *a;
2327 size_t amt;
2329 /* We only care about symbols defined in shared objects with version
2330 information. */
2331 if (!h->def_dynamic
2332 || h->def_regular
2333 || h->dynindx == -1
2334 || h->verinfo.verdef == NULL
2335 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2336 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2337 return true;
2339 /* See if we already know about this version. */
2340 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2341 t != NULL;
2342 t = t->vn_nextref)
2344 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2345 continue;
2347 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2348 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2349 return true;
2351 break;
2354 /* This is a new version. Add it to tree we are building. */
2356 if (t == NULL)
2358 amt = sizeof *t;
2359 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2360 if (t == NULL)
2362 rinfo->failed = true;
2363 return false;
2366 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2367 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2368 elf_tdata (rinfo->info->output_bfd)->verref = t;
2371 amt = sizeof *a;
2372 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2373 if (a == NULL)
2375 rinfo->failed = true;
2376 return false;
2379 /* Note that we are copying a string pointer here, and testing it
2380 above. If bfd_elf_string_from_elf_section is ever changed to
2381 discard the string data when low in memory, this will have to be
2382 fixed. */
2383 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2385 a->vna_flags = h->verinfo.verdef->vd_flags;
2386 a->vna_nextptr = t->vn_auxptr;
2388 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2389 ++rinfo->vers;
2391 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2393 t->vn_auxptr = a;
2395 return true;
2398 /* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2399 hidden. Set *T_P to NULL if there is no match. */
2401 static bool
2402 _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
2403 struct elf_link_hash_entry *h,
2404 const char *version_p,
2405 struct bfd_elf_version_tree **t_p,
2406 bool *hide)
2408 struct bfd_elf_version_tree *t;
2410 /* Look for the version. If we find it, it is no longer weak. */
2411 for (t = info->version_info; t != NULL; t = t->next)
2413 if (strcmp (t->name, version_p) == 0)
2415 size_t len;
2416 char *alc;
2417 struct bfd_elf_version_expr *d;
2419 len = version_p - h->root.root.string;
2420 alc = (char *) bfd_malloc (len);
2421 if (alc == NULL)
2422 return false;
2423 memcpy (alc, h->root.root.string, len - 1);
2424 alc[len - 1] = '\0';
2425 if (alc[len - 2] == ELF_VER_CHR)
2426 alc[len - 2] = '\0';
2428 h->verinfo.vertree = t;
2429 t->used = true;
2430 d = NULL;
2432 if (t->globals.list != NULL)
2433 d = (*t->match) (&t->globals, NULL, alc);
2435 /* See if there is anything to force this symbol to
2436 local scope. */
2437 if (d == NULL && t->locals.list != NULL)
2439 d = (*t->match) (&t->locals, NULL, alc);
2440 if (d != NULL
2441 && h->dynindx != -1
2442 && ! info->export_dynamic)
2443 *hide = true;
2446 free (alc);
2447 break;
2451 *t_p = t;
2453 return true;
2456 /* Return TRUE if the symbol H is hidden by version script. */
2458 bool
2459 _bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
2460 struct elf_link_hash_entry *h)
2462 const char *p;
2463 bool hide = false;
2464 const struct elf_backend_data *bed
2465 = get_elf_backend_data (info->output_bfd);
2467 /* Version script only hides symbols defined in regular objects. */
2468 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2469 return true;
2471 p = strchr (h->root.root.string, ELF_VER_CHR);
2472 if (p != NULL && h->verinfo.vertree == NULL)
2474 struct bfd_elf_version_tree *t;
2476 ++p;
2477 if (*p == ELF_VER_CHR)
2478 ++p;
2480 if (*p != '\0'
2481 && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
2482 && hide)
2484 if (hide)
2485 (*bed->elf_backend_hide_symbol) (info, h, true);
2486 return true;
2490 /* If we don't have a version for this symbol, see if we can find
2491 something. */
2492 if (h->verinfo.vertree == NULL && info->version_info != NULL)
2494 h->verinfo.vertree
2495 = bfd_find_version_for_sym (info->version_info,
2496 h->root.root.string, &hide);
2497 if (h->verinfo.vertree != NULL && hide)
2499 (*bed->elf_backend_hide_symbol) (info, h, true);
2500 return true;
2504 return false;
2507 /* Figure out appropriate versions for all the symbols. We may not
2508 have the version number script until we have read all of the input
2509 files, so until that point we don't know which symbols should be
2510 local. This function is called via elf_link_hash_traverse. */
2512 static bool
2513 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2515 struct elf_info_failed *sinfo;
2516 struct bfd_link_info *info;
2517 const struct elf_backend_data *bed;
2518 struct elf_info_failed eif;
2519 char *p;
2520 bool hide;
2522 sinfo = (struct elf_info_failed *) data;
2523 info = sinfo->info;
2525 /* Fix the symbol flags. */
2526 eif.failed = false;
2527 eif.info = info;
2528 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2530 if (eif.failed)
2531 sinfo->failed = true;
2532 return false;
2535 bed = get_elf_backend_data (info->output_bfd);
2537 /* We only need version numbers for symbols defined in regular
2538 objects. */
2539 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2541 /* Hide symbols defined in discarded input sections. */
2542 if ((h->root.type == bfd_link_hash_defined
2543 || h->root.type == bfd_link_hash_defweak)
2544 && discarded_section (h->root.u.def.section))
2545 (*bed->elf_backend_hide_symbol) (info, h, true);
2546 return true;
2549 hide = false;
2550 p = strchr (h->root.root.string, ELF_VER_CHR);
2551 if (p != NULL && h->verinfo.vertree == NULL)
2553 struct bfd_elf_version_tree *t;
2555 ++p;
2556 if (*p == ELF_VER_CHR)
2557 ++p;
2559 /* If there is no version string, we can just return out. */
2560 if (*p == '\0')
2561 return true;
2563 if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
2565 sinfo->failed = true;
2566 return false;
2569 if (hide)
2570 (*bed->elf_backend_hide_symbol) (info, h, true);
2572 /* If we are building an application, we need to create a
2573 version node for this version. */
2574 if (t == NULL && bfd_link_executable (info))
2576 struct bfd_elf_version_tree **pp;
2577 int version_index;
2579 /* If we aren't going to export this symbol, we don't need
2580 to worry about it. */
2581 if (h->dynindx == -1)
2582 return true;
2584 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2585 sizeof *t);
2586 if (t == NULL)
2588 sinfo->failed = true;
2589 return false;
2592 t->name = p;
2593 t->name_indx = (unsigned int) -1;
2594 t->used = true;
2596 version_index = 1;
2597 /* Don't count anonymous version tag. */
2598 if (sinfo->info->version_info != NULL
2599 && sinfo->info->version_info->vernum == 0)
2600 version_index = 0;
2601 for (pp = &sinfo->info->version_info;
2602 *pp != NULL;
2603 pp = &(*pp)->next)
2604 ++version_index;
2605 t->vernum = version_index;
2607 *pp = t;
2609 h->verinfo.vertree = t;
2611 else if (t == NULL)
2613 /* We could not find the version for a symbol when
2614 generating a shared archive. Return an error. */
2615 _bfd_error_handler
2616 /* xgettext:c-format */
2617 (_("%pB: version node not found for symbol %s"),
2618 info->output_bfd, h->root.root.string);
2619 bfd_set_error (bfd_error_bad_value);
2620 sinfo->failed = true;
2621 return false;
2625 /* If we don't have a version for this symbol, see if we can find
2626 something. */
2627 if (!hide
2628 && h->verinfo.vertree == NULL
2629 && sinfo->info->version_info != NULL)
2631 h->verinfo.vertree
2632 = bfd_find_version_for_sym (sinfo->info->version_info,
2633 h->root.root.string, &hide);
2634 if (h->verinfo.vertree != NULL && hide)
2635 (*bed->elf_backend_hide_symbol) (info, h, true);
2638 return true;
2641 /* Read and swap the relocs from the section indicated by SHDR. This
2642 may be either a REL or a RELA section. The relocations are
2643 translated into RELA relocations and stored in INTERNAL_RELOCS,
2644 which should have already been allocated to contain enough space.
2645 The EXTERNAL_RELOCS are a buffer where the external form of the
2646 relocations should be stored.
2648 Returns FALSE if something goes wrong. */
2650 static bool
2651 elf_link_read_relocs_from_section (bfd *abfd,
2652 asection *sec,
2653 Elf_Internal_Shdr *shdr,
2654 void *external_relocs,
2655 Elf_Internal_Rela *internal_relocs)
2657 const struct elf_backend_data *bed;
2658 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2659 const bfd_byte *erela;
2660 const bfd_byte *erelaend;
2661 Elf_Internal_Rela *irela;
2662 Elf_Internal_Shdr *symtab_hdr;
2663 size_t nsyms;
2665 /* Position ourselves at the start of the section. */
2666 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2667 return false;
2669 /* Read the relocations. */
2670 if (bfd_read (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2671 return false;
2673 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2674 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2676 bed = get_elf_backend_data (abfd);
2678 /* Convert the external relocations to the internal format. */
2679 if (shdr->sh_entsize == bed->s->sizeof_rel)
2680 swap_in = bed->s->swap_reloc_in;
2681 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2682 swap_in = bed->s->swap_reloca_in;
2683 else
2685 bfd_set_error (bfd_error_wrong_format);
2686 return false;
2689 erela = (const bfd_byte *) external_relocs;
2690 /* Setting erelaend like this and comparing with <= handles case of
2691 a fuzzed object with sh_size not a multiple of sh_entsize. */
2692 erelaend = erela + shdr->sh_size - shdr->sh_entsize;
2693 irela = internal_relocs;
2694 while (erela <= erelaend)
2696 bfd_vma r_symndx;
2698 (*swap_in) (abfd, erela, irela);
2699 r_symndx = ELF32_R_SYM (irela->r_info);
2700 if (bed->s->arch_size == 64)
2701 r_symndx >>= 24;
2702 if (nsyms > 0)
2704 if ((size_t) r_symndx >= nsyms)
2706 _bfd_error_handler
2707 /* xgettext:c-format */
2708 (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
2709 " for offset %#" PRIx64 " in section `%pA'"),
2710 abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
2711 (uint64_t) irela->r_offset, sec);
2712 bfd_set_error (bfd_error_bad_value);
2713 return false;
2716 else if (r_symndx != STN_UNDEF)
2718 _bfd_error_handler
2719 /* xgettext:c-format */
2720 (_("%pB: non-zero symbol index (%#" PRIx64 ")"
2721 " for offset %#" PRIx64 " in section `%pA'"
2722 " when the object file has no symbol table"),
2723 abfd, (uint64_t) r_symndx,
2724 (uint64_t) irela->r_offset, sec);
2725 bfd_set_error (bfd_error_bad_value);
2726 return false;
2728 irela += bed->s->int_rels_per_ext_rel;
2729 erela += shdr->sh_entsize;
2732 return true;
2735 /* Read and swap the relocs for a section O. They may have been
2736 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2737 not NULL, they are used as buffers to read into. They are known to
2738 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2739 the return value is allocated using either malloc or bfd_alloc,
2740 according to the KEEP_MEMORY argument. If O has two relocation
2741 sections (both REL and RELA relocations), then the REL_HDR
2742 relocations will appear first in INTERNAL_RELOCS, followed by the
2743 RELA_HDR relocations. If INFO isn't NULL and KEEP_MEMORY is true,
2744 update cache_size. */
2746 Elf_Internal_Rela *
2747 _bfd_elf_link_info_read_relocs (bfd *abfd,
2748 struct bfd_link_info *info,
2749 asection *o,
2750 void *external_relocs,
2751 Elf_Internal_Rela *internal_relocs,
2752 bool keep_memory)
2754 void *alloc1 = NULL;
2755 Elf_Internal_Rela *alloc2 = NULL;
2756 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2757 struct bfd_elf_section_data *esdo = elf_section_data (o);
2758 Elf_Internal_Rela *internal_rela_relocs;
2760 if (esdo->relocs != NULL)
2761 return esdo->relocs;
2763 if (o->reloc_count == 0)
2764 return NULL;
2766 if (internal_relocs == NULL)
2768 bfd_size_type size;
2770 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
2771 if (keep_memory)
2773 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2774 if (info)
2775 info->cache_size += size;
2777 else
2778 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2779 if (internal_relocs == NULL)
2780 goto error_return;
2783 if (external_relocs == NULL)
2785 bfd_size_type size = 0;
2787 if (esdo->rel.hdr)
2788 size += esdo->rel.hdr->sh_size;
2789 if (esdo->rela.hdr)
2790 size += esdo->rela.hdr->sh_size;
2792 alloc1 = bfd_malloc (size);
2793 if (alloc1 == NULL)
2794 goto error_return;
2795 external_relocs = alloc1;
2798 internal_rela_relocs = internal_relocs;
2799 if (esdo->rel.hdr)
2801 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2802 external_relocs,
2803 internal_relocs))
2804 goto error_return;
2805 external_relocs = (((bfd_byte *) external_relocs)
2806 + esdo->rel.hdr->sh_size);
2807 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2808 * bed->s->int_rels_per_ext_rel);
2811 if (esdo->rela.hdr
2812 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2813 external_relocs,
2814 internal_rela_relocs)))
2815 goto error_return;
2817 /* Cache the results for next time, if we can. */
2818 if (keep_memory)
2819 esdo->relocs = internal_relocs;
2821 free (alloc1);
2823 /* Don't free alloc2, since if it was allocated we are passing it
2824 back (under the name of internal_relocs). */
2826 return internal_relocs;
2828 error_return:
2829 free (alloc1);
2830 if (alloc2 != NULL)
2832 if (keep_memory)
2833 bfd_release (abfd, alloc2);
2834 else
2835 free (alloc2);
2837 return NULL;
2840 /* This is similar to _bfd_elf_link_info_read_relocs, except for that
2841 NULL is passed to _bfd_elf_link_info_read_relocs for pointer to
2842 struct bfd_link_info. */
2844 Elf_Internal_Rela *
2845 _bfd_elf_link_read_relocs (bfd *abfd,
2846 asection *o,
2847 void *external_relocs,
2848 Elf_Internal_Rela *internal_relocs,
2849 bool keep_memory)
2851 return _bfd_elf_link_info_read_relocs (abfd, NULL, o, external_relocs,
2852 internal_relocs, keep_memory);
2856 /* Compute the size of, and allocate space for, REL_HDR which is the
2857 section header for a section containing relocations for O. */
2859 static bool
2860 _bfd_elf_link_size_reloc_section (bfd *abfd,
2861 struct bfd_elf_section_reloc_data *reldata)
2863 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2865 /* That allows us to calculate the size of the section. */
2866 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2868 /* The contents field must last into write_object_contents, so we
2869 allocate it with bfd_alloc rather than malloc. Also since we
2870 cannot be sure that the contents will actually be filled in,
2871 we zero the allocated space. */
2872 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2873 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2874 return false;
2876 if (reldata->hashes == NULL && reldata->count)
2878 struct elf_link_hash_entry **p;
2880 p = ((struct elf_link_hash_entry **)
2881 bfd_zmalloc (reldata->count * sizeof (*p)));
2882 if (p == NULL)
2883 return false;
2885 reldata->hashes = p;
2888 return true;
2891 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2892 originated from the section given by INPUT_REL_HDR) to the
2893 OUTPUT_BFD. */
2895 bool
2896 _bfd_elf_link_output_relocs (bfd *output_bfd,
2897 asection *input_section,
2898 Elf_Internal_Shdr *input_rel_hdr,
2899 Elf_Internal_Rela *internal_relocs,
2900 struct elf_link_hash_entry **rel_hash
2901 ATTRIBUTE_UNUSED)
2903 Elf_Internal_Rela *irela;
2904 Elf_Internal_Rela *irelaend;
2905 bfd_byte *erel;
2906 struct bfd_elf_section_reloc_data *output_reldata;
2907 asection *output_section;
2908 const struct elf_backend_data *bed;
2909 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2910 struct bfd_elf_section_data *esdo;
2912 output_section = input_section->output_section;
2914 bed = get_elf_backend_data (output_bfd);
2915 esdo = elf_section_data (output_section);
2916 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2918 output_reldata = &esdo->rel;
2919 swap_out = bed->s->swap_reloc_out;
2921 else if (esdo->rela.hdr
2922 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2924 output_reldata = &esdo->rela;
2925 swap_out = bed->s->swap_reloca_out;
2927 else
2929 _bfd_error_handler
2930 /* xgettext:c-format */
2931 (_("%pB: relocation size mismatch in %pB section %pA"),
2932 output_bfd, input_section->owner, input_section);
2933 bfd_set_error (bfd_error_wrong_format);
2934 return false;
2937 erel = output_reldata->hdr->contents;
2938 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2939 irela = internal_relocs;
2940 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2941 * bed->s->int_rels_per_ext_rel);
2942 while (irela < irelaend)
2944 (*swap_out) (output_bfd, irela, erel);
2945 irela += bed->s->int_rels_per_ext_rel;
2946 erel += input_rel_hdr->sh_entsize;
2949 /* Bump the counter, so that we know where to add the next set of
2950 relocations. */
2951 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2953 return true;
2956 /* Make weak undefined symbols in PIE dynamic. */
2958 bool
2959 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2960 struct elf_link_hash_entry *h)
2962 if (bfd_link_pie (info)
2963 && h->dynindx == -1
2964 && h->root.type == bfd_link_hash_undefweak)
2965 return bfd_elf_link_record_dynamic_symbol (info, h);
2967 return true;
2970 /* Fix up the flags for a symbol. This handles various cases which
2971 can only be fixed after all the input files are seen. This is
2972 currently called by both adjust_dynamic_symbol and
2973 assign_sym_version, which is unnecessary but perhaps more robust in
2974 the face of future changes. */
2976 static bool
2977 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2978 struct elf_info_failed *eif)
2980 const struct elf_backend_data *bed;
2982 /* If this symbol was mentioned in a non-ELF file, try to set
2983 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2984 permit a non-ELF file to correctly refer to a symbol defined in
2985 an ELF dynamic object. */
2986 if (h->non_elf)
2988 while (h->root.type == bfd_link_hash_indirect)
2989 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2991 if (h->root.type != bfd_link_hash_defined
2992 && h->root.type != bfd_link_hash_defweak)
2994 h->ref_regular = 1;
2995 h->ref_regular_nonweak = 1;
2997 else
2999 if (h->root.u.def.section->owner != NULL
3000 && (bfd_get_flavour (h->root.u.def.section->owner)
3001 == bfd_target_elf_flavour))
3003 h->ref_regular = 1;
3004 h->ref_regular_nonweak = 1;
3006 else
3007 h->def_regular = 1;
3010 if (h->dynindx == -1
3011 && (h->def_dynamic
3012 || h->ref_dynamic))
3014 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
3016 eif->failed = true;
3017 return false;
3021 else
3023 /* Unfortunately, NON_ELF is only correct if the symbol
3024 was first seen in a non-ELF file. Fortunately, if the symbol
3025 was first seen in an ELF file, we're probably OK unless the
3026 symbol was defined in a non-ELF file. Catch that case here.
3027 FIXME: We're still in trouble if the symbol was first seen in
3028 a dynamic object, and then later in a non-ELF regular object. */
3029 if ((h->root.type == bfd_link_hash_defined
3030 || h->root.type == bfd_link_hash_defweak)
3031 && !h->def_regular
3032 && (h->root.u.def.section->owner != NULL
3033 ? (bfd_get_flavour (h->root.u.def.section->owner)
3034 != bfd_target_elf_flavour)
3035 : (bfd_is_abs_section (h->root.u.def.section)
3036 && !h->def_dynamic)))
3037 h->def_regular = 1;
3040 /* Backend specific symbol fixup. */
3041 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
3042 if (bed->elf_backend_fixup_symbol
3043 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
3044 return false;
3046 /* If this is a final link, and the symbol was defined as a common
3047 symbol in a regular object file, and there was no definition in
3048 any dynamic object, then the linker will have allocated space for
3049 the symbol in a common section but the DEF_REGULAR
3050 flag will not have been set. */
3051 if (h->root.type == bfd_link_hash_defined
3052 && !h->def_regular
3053 && h->ref_regular
3054 && !h->def_dynamic
3055 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
3056 h->def_regular = 1;
3058 /* Symbols defined in discarded sections shouldn't be dynamic. */
3059 if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
3060 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3062 /* If a weak undefined symbol has non-default visibility, we also
3063 hide it from the dynamic linker. */
3064 else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3065 && h->root.type == bfd_link_hash_undefweak)
3066 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3068 /* A hidden versioned symbol in executable should be forced local if
3069 it is is locally defined, not referenced by shared library and not
3070 exported. */
3071 else if (bfd_link_executable (eif->info)
3072 && h->versioned == versioned_hidden
3073 && !eif->info->export_dynamic
3074 && !h->dynamic
3075 && !h->ref_dynamic
3076 && h->def_regular)
3077 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3079 /* If -Bsymbolic was used (which means to bind references to global
3080 symbols to the definition within the shared object), and this
3081 symbol was defined in a regular object, then it actually doesn't
3082 need a PLT entry. Likewise, if the symbol has non-default
3083 visibility. If the symbol has hidden or internal visibility, we
3084 will force it local. */
3085 else if (h->needs_plt
3086 && bfd_link_pic (eif->info)
3087 && is_elf_hash_table (eif->info->hash)
3088 && (SYMBOLIC_BIND (eif->info, h)
3089 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
3090 && h->def_regular)
3092 bool force_local;
3094 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
3095 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
3096 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
3099 /* If this is a weak defined symbol in a dynamic object, and we know
3100 the real definition in the dynamic object, copy interesting flags
3101 over to the real definition. */
3102 if (h->is_weakalias)
3104 struct elf_link_hash_entry *def = weakdef (h);
3106 /* If the real definition is defined by a regular object file,
3107 don't do anything special. See the longer description in
3108 _bfd_elf_adjust_dynamic_symbol, below. If the def is not
3109 bfd_link_hash_defined as it was when put on the alias list
3110 then it must have originally been a versioned symbol (for
3111 which a non-versioned indirect symbol is created) and later
3112 a definition for the non-versioned symbol is found. In that
3113 case the indirection is flipped with the versioned symbol
3114 becoming an indirect pointing at the non-versioned symbol.
3115 Thus, not an alias any more. */
3116 if (def->def_regular
3117 || def->root.type != bfd_link_hash_defined)
3119 h = def;
3120 while ((h = h->u.alias) != def)
3121 h->is_weakalias = 0;
3123 else
3125 while (h->root.type == bfd_link_hash_indirect)
3126 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3127 BFD_ASSERT (h->root.type == bfd_link_hash_defined
3128 || h->root.type == bfd_link_hash_defweak);
3129 BFD_ASSERT (def->def_dynamic);
3130 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
3134 return true;
3137 /* Make the backend pick a good value for a dynamic symbol. This is
3138 called via elf_link_hash_traverse, and also calls itself
3139 recursively. */
3141 static bool
3142 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
3144 struct elf_info_failed *eif = (struct elf_info_failed *) data;
3145 struct elf_link_hash_table *htab;
3146 const struct elf_backend_data *bed;
3148 if (! is_elf_hash_table (eif->info->hash))
3149 return false;
3151 /* Ignore indirect symbols. These are added by the versioning code. */
3152 if (h->root.type == bfd_link_hash_indirect)
3153 return true;
3155 /* Fix the symbol flags. */
3156 if (! _bfd_elf_fix_symbol_flags (h, eif))
3157 return false;
3159 htab = elf_hash_table (eif->info);
3160 bed = get_elf_backend_data (htab->dynobj);
3162 if (h->root.type == bfd_link_hash_undefweak)
3164 if (eif->info->dynamic_undefined_weak == 0)
3165 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3166 else if (eif->info->dynamic_undefined_weak > 0
3167 && h->ref_regular
3168 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3169 && !bfd_hide_sym_by_version (eif->info->version_info,
3170 h->root.root.string))
3172 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
3174 eif->failed = true;
3175 return false;
3180 /* If this symbol does not require a PLT entry, and it is not
3181 defined by a dynamic object, or is not referenced by a regular
3182 object, ignore it. We do have to handle a weak defined symbol,
3183 even if no regular object refers to it, if we decided to add it
3184 to the dynamic symbol table. FIXME: Do we normally need to worry
3185 about symbols which are defined by one dynamic object and
3186 referenced by another one? */
3187 if (!h->needs_plt
3188 && h->type != STT_GNU_IFUNC
3189 && (h->def_regular
3190 || !h->def_dynamic
3191 || (!h->ref_regular
3192 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
3194 h->plt = elf_hash_table (eif->info)->init_plt_offset;
3195 return true;
3198 /* If we've already adjusted this symbol, don't do it again. This
3199 can happen via a recursive call. */
3200 if (h->dynamic_adjusted)
3201 return true;
3203 /* Don't look at this symbol again. Note that we must set this
3204 after checking the above conditions, because we may look at a
3205 symbol once, decide not to do anything, and then get called
3206 recursively later after REF_REGULAR is set below. */
3207 h->dynamic_adjusted = 1;
3209 /* If this is a weak definition, and we know a real definition, and
3210 the real symbol is not itself defined by a regular object file,
3211 then get a good value for the real definition. We handle the
3212 real symbol first, for the convenience of the backend routine.
3214 Note that there is a confusing case here. If the real definition
3215 is defined by a regular object file, we don't get the real symbol
3216 from the dynamic object, but we do get the weak symbol. If the
3217 processor backend uses a COPY reloc, then if some routine in the
3218 dynamic object changes the real symbol, we will not see that
3219 change in the corresponding weak symbol. This is the way other
3220 ELF linkers work as well, and seems to be a result of the shared
3221 library model.
3223 I will clarify this issue. Most SVR4 shared libraries define the
3224 variable _timezone and define timezone as a weak synonym. The
3225 tzset call changes _timezone. If you write
3226 extern int timezone;
3227 int _timezone = 5;
3228 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3229 you might expect that, since timezone is a synonym for _timezone,
3230 the same number will print both times. However, if the processor
3231 backend uses a COPY reloc, then actually timezone will be copied
3232 into your process image, and, since you define _timezone
3233 yourself, _timezone will not. Thus timezone and _timezone will
3234 wind up at different memory locations. The tzset call will set
3235 _timezone, leaving timezone unchanged. */
3237 if (h->is_weakalias)
3239 struct elf_link_hash_entry *def = weakdef (h);
3241 /* If we get to this point, there is an implicit reference to
3242 the alias by a regular object file via the weak symbol H. */
3243 def->ref_regular = 1;
3245 /* Ensure that the backend adjust_dynamic_symbol function sees
3246 the strong alias before H by recursively calling ourselves. */
3247 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
3248 return false;
3251 /* If a symbol has no type and no size and does not require a PLT
3252 entry, then we are probably about to do the wrong thing here: we
3253 are probably going to create a COPY reloc for an empty object.
3254 This case can arise when a shared object is built with assembly
3255 code, and the assembly code fails to set the symbol type. */
3256 if (h->size == 0
3257 && h->type == STT_NOTYPE
3258 && !h->needs_plt)
3259 _bfd_error_handler
3260 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3261 h->root.root.string);
3263 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3265 eif->failed = true;
3266 return false;
3269 return true;
3272 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3273 DYNBSS. */
3275 bool
3276 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3277 struct elf_link_hash_entry *h,
3278 asection *dynbss)
3280 unsigned int power_of_two;
3281 bfd_vma mask;
3282 asection *sec = h->root.u.def.section;
3284 /* The section alignment of the definition is the maximum alignment
3285 requirement of symbols defined in the section. Since we don't
3286 know the symbol alignment requirement, we start with the
3287 maximum alignment and check low bits of the symbol address
3288 for the minimum alignment. */
3289 power_of_two = bfd_section_alignment (sec);
3290 mask = ((bfd_vma) 1 << power_of_two) - 1;
3291 while ((h->root.u.def.value & mask) != 0)
3293 mask >>= 1;
3294 --power_of_two;
3297 if (power_of_two > bfd_section_alignment (dynbss))
3299 /* Adjust the section alignment if needed. */
3300 if (!bfd_set_section_alignment (dynbss, power_of_two))
3301 return false;
3304 /* We make sure that the symbol will be aligned properly. */
3305 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3307 /* Define the symbol as being at this point in DYNBSS. */
3308 h->root.u.def.section = dynbss;
3309 h->root.u.def.value = dynbss->size;
3311 /* Increment the size of DYNBSS to make room for the symbol. */
3312 dynbss->size += h->size;
3314 /* No error if extern_protected_data is true. */
3315 if (h->protected_def
3316 && (!info->extern_protected_data
3317 || (info->extern_protected_data < 0
3318 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
3319 info->callbacks->einfo
3320 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3321 h->root.root.string);
3323 return true;
3326 /* Adjust all external symbols pointing into SEC_MERGE sections
3327 to reflect the object merging within the sections. */
3329 static bool
3330 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
3332 asection *sec;
3334 if ((h->root.type == bfd_link_hash_defined
3335 || h->root.type == bfd_link_hash_defweak)
3336 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
3337 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3339 bfd *output_bfd = (bfd *) data;
3341 h->root.u.def.value =
3342 _bfd_merged_section_offset (output_bfd,
3343 &h->root.u.def.section,
3344 elf_section_data (sec)->sec_info,
3345 h->root.u.def.value);
3348 return true;
3351 /* Returns false if the symbol referred to by H should be considered
3352 to resolve local to the current module, and true if it should be
3353 considered to bind dynamically. */
3355 bool
3356 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3357 struct bfd_link_info *info,
3358 bool not_local_protected)
3360 bool binding_stays_local_p;
3361 const struct elf_backend_data *bed;
3362 struct elf_link_hash_table *hash_table;
3364 if (h == NULL)
3365 return false;
3367 while (h->root.type == bfd_link_hash_indirect
3368 || h->root.type == bfd_link_hash_warning)
3369 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3371 /* If it was forced local, then clearly it's not dynamic. */
3372 if (h->dynindx == -1)
3373 return false;
3374 if (h->forced_local)
3375 return false;
3377 /* Identify the cases where name binding rules say that a
3378 visible symbol resolves locally. */
3379 binding_stays_local_p = (bfd_link_executable (info)
3380 || SYMBOLIC_BIND (info, h));
3382 switch (ELF_ST_VISIBILITY (h->other))
3384 case STV_INTERNAL:
3385 case STV_HIDDEN:
3386 return false;
3388 case STV_PROTECTED:
3389 hash_table = elf_hash_table (info);
3390 if (!is_elf_hash_table (&hash_table->root))
3391 return false;
3393 bed = get_elf_backend_data (hash_table->dynobj);
3395 /* Proper resolution for function pointer equality may require
3396 that these symbols perhaps be resolved dynamically, even though
3397 we should be resolving them to the current module. */
3398 if (!not_local_protected || !bed->is_function_type (h->type))
3399 binding_stays_local_p = true;
3400 break;
3402 default:
3403 break;
3406 /* If it isn't defined locally, then clearly it's dynamic. */
3407 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
3408 return true;
3410 /* Otherwise, the symbol is dynamic if binding rules don't tell
3411 us that it remains local. */
3412 return !binding_stays_local_p;
3415 /* Return true if the symbol referred to by H should be considered
3416 to resolve local to the current module, and false otherwise. Differs
3417 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3418 undefined symbols. The two functions are virtually identical except
3419 for the place where dynindx == -1 is tested. If that test is true,
3420 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3421 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3422 defined symbols.
3423 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3424 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3425 treatment of undefined weak symbols. For those that do not make
3426 undefined weak symbols dynamic, both functions may return false. */
3428 bool
3429 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3430 struct bfd_link_info *info,
3431 bool local_protected)
3433 const struct elf_backend_data *bed;
3434 struct elf_link_hash_table *hash_table;
3436 /* If it's a local sym, of course we resolve locally. */
3437 if (h == NULL)
3438 return true;
3440 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3441 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3442 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3443 return true;
3445 /* Forced local symbols resolve locally. */
3446 if (h->forced_local)
3447 return true;
3449 /* Common symbols that become definitions don't get the DEF_REGULAR
3450 flag set, so test it first, and don't bail out. */
3451 if (ELF_COMMON_DEF_P (h))
3452 /* Do nothing. */;
3453 /* If we don't have a definition in a regular file, then we can't
3454 resolve locally. The sym is either undefined or dynamic. */
3455 else if (!h->def_regular)
3456 return false;
3458 /* Non-dynamic symbols resolve locally. */
3459 if (h->dynindx == -1)
3460 return true;
3462 /* At this point, we know the symbol is defined and dynamic. In an
3463 executable it must resolve locally, likewise when building symbolic
3464 shared libraries. */
3465 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3466 return true;
3468 /* Now deal with defined dynamic symbols in shared libraries. Ones
3469 with default visibility might not resolve locally. */
3470 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3471 return false;
3473 hash_table = elf_hash_table (info);
3474 if (!is_elf_hash_table (&hash_table->root))
3475 return true;
3477 /* STV_PROTECTED symbols with indirect external access are local. */
3478 if (info->indirect_extern_access > 0)
3479 return true;
3481 bed = get_elf_backend_data (hash_table->dynobj);
3483 /* If extern_protected_data is false, STV_PROTECTED non-function
3484 symbols are local. */
3485 if ((!info->extern_protected_data
3486 || (info->extern_protected_data < 0
3487 && !bed->extern_protected_data))
3488 && !bed->is_function_type (h->type))
3489 return true;
3491 /* Function pointer equality tests may require that STV_PROTECTED
3492 symbols be treated as dynamic symbols. If the address of a
3493 function not defined in an executable is set to that function's
3494 plt entry in the executable, then the address of the function in
3495 a shared library must also be the plt entry in the executable. */
3496 return local_protected;
3499 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3500 aligned. Returns the first TLS output section. */
3502 struct bfd_section *
3503 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3505 struct bfd_section *sec, *tls;
3506 unsigned int align = 0;
3508 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3509 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3510 break;
3511 tls = sec;
3513 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3514 if (sec->alignment_power > align)
3515 align = sec->alignment_power;
3517 elf_hash_table (info)->tls_sec = tls;
3519 /* Ensure the alignment of the first section (usually .tdata) is the largest
3520 alignment, so that the tls segment starts aligned. */
3521 if (tls != NULL)
3522 tls->alignment_power = align;
3524 return tls;
3527 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3528 static bool
3529 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3530 Elf_Internal_Sym *sym)
3532 const struct elf_backend_data *bed;
3534 /* Local symbols do not count, but target specific ones might. */
3535 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3536 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3537 return false;
3539 bed = get_elf_backend_data (abfd);
3540 /* Function symbols do not count. */
3541 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3542 return false;
3544 /* If the section is undefined, then so is the symbol. */
3545 if (sym->st_shndx == SHN_UNDEF)
3546 return false;
3548 /* If the symbol is defined in the common section, then
3549 it is a common definition and so does not count. */
3550 if (bed->common_definition (sym))
3551 return false;
3553 /* If the symbol is in a target specific section then we
3554 must rely upon the backend to tell us what it is. */
3555 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3556 /* FIXME - this function is not coded yet:
3558 return _bfd_is_global_symbol_definition (abfd, sym);
3560 Instead for now assume that the definition is not global,
3561 Even if this is wrong, at least the linker will behave
3562 in the same way that it used to do. */
3563 return false;
3565 return true;
3568 /* Search the symbol table of the archive element of the archive ABFD
3569 whose archive map contains a mention of SYMDEF, and determine if
3570 the symbol is defined in this element. */
3571 static bool
3572 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3574 Elf_Internal_Shdr * hdr;
3575 size_t symcount;
3576 size_t extsymcount;
3577 size_t extsymoff;
3578 Elf_Internal_Sym *isymbuf;
3579 Elf_Internal_Sym *isym;
3580 Elf_Internal_Sym *isymend;
3581 bool result;
3583 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset, NULL);
3584 if (abfd == NULL)
3585 return false;
3587 if (! bfd_check_format (abfd, bfd_object))
3588 return false;
3590 /* Select the appropriate symbol table. If we don't know if the
3591 object file is an IR object, give linker LTO plugin a chance to
3592 get the correct symbol table. */
3593 if (abfd->plugin_format == bfd_plugin_yes
3594 #if BFD_SUPPORTS_PLUGINS
3595 || (abfd->plugin_format == bfd_plugin_unknown
3596 && bfd_link_plugin_object_p (abfd))
3597 #endif
3600 /* Use the IR symbol table if the object has been claimed by
3601 plugin. */
3602 abfd = abfd->plugin_dummy_bfd;
3603 hdr = &elf_tdata (abfd)->symtab_hdr;
3605 else
3607 if (elf_use_dt_symtab_p (abfd))
3609 bfd_set_error (bfd_error_wrong_format);
3610 return false;
3613 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3614 hdr = &elf_tdata (abfd)->symtab_hdr;
3615 else
3616 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3619 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3621 /* The sh_info field of the symtab header tells us where the
3622 external symbols start. We don't care about the local symbols. */
3623 if (elf_bad_symtab (abfd))
3625 extsymcount = symcount;
3626 extsymoff = 0;
3628 else
3630 extsymcount = symcount - hdr->sh_info;
3631 extsymoff = hdr->sh_info;
3634 if (extsymcount == 0)
3635 return false;
3637 /* Read in the symbol table. */
3638 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3639 NULL, NULL, NULL);
3640 if (isymbuf == NULL)
3641 return false;
3643 /* Scan the symbol table looking for SYMDEF. */
3644 result = false;
3645 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3647 const char *name;
3649 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3650 isym->st_name);
3651 if (name == NULL)
3652 break;
3654 if (strcmp (name, symdef->name) == 0)
3656 result = is_global_data_symbol_definition (abfd, isym);
3657 break;
3661 free (isymbuf);
3663 return result;
3666 /* Add an entry to the .dynamic table. */
3668 bool
3669 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3670 bfd_vma tag,
3671 bfd_vma val)
3673 struct elf_link_hash_table *hash_table;
3674 const struct elf_backend_data *bed;
3675 asection *s;
3676 bfd_size_type newsize;
3677 bfd_byte *newcontents;
3678 Elf_Internal_Dyn dyn;
3680 hash_table = elf_hash_table (info);
3681 if (! is_elf_hash_table (&hash_table->root))
3682 return false;
3684 if (tag == DT_RELA || tag == DT_REL)
3685 hash_table->dynamic_relocs = true;
3687 bed = get_elf_backend_data (hash_table->dynobj);
3688 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3689 BFD_ASSERT (s != NULL);
3691 newsize = s->size + bed->s->sizeof_dyn;
3692 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3693 if (newcontents == NULL)
3694 return false;
3696 dyn.d_tag = tag;
3697 dyn.d_un.d_val = val;
3698 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3700 s->size = newsize;
3701 s->contents = newcontents;
3703 return true;
3706 /* Strip zero-sized dynamic sections. */
3708 bool
3709 _bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info *info)
3711 struct elf_link_hash_table *hash_table;
3712 const struct elf_backend_data *bed;
3713 asection *s, *sdynamic, **pp;
3714 asection *rela_dyn, *rel_dyn;
3715 Elf_Internal_Dyn dyn;
3716 bfd_byte *extdyn, *next;
3717 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3718 bool strip_zero_sized;
3719 bool strip_zero_sized_plt;
3721 if (bfd_link_relocatable (info))
3722 return true;
3724 hash_table = elf_hash_table (info);
3725 if (!is_elf_hash_table (&hash_table->root))
3726 return false;
3728 if (!hash_table->dynobj)
3729 return true;
3731 sdynamic= bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3732 if (!sdynamic)
3733 return true;
3735 bed = get_elf_backend_data (hash_table->dynobj);
3736 swap_dyn_in = bed->s->swap_dyn_in;
3738 strip_zero_sized = false;
3739 strip_zero_sized_plt = false;
3741 /* Strip zero-sized dynamic sections. */
3742 rela_dyn = bfd_get_section_by_name (info->output_bfd, ".rela.dyn");
3743 rel_dyn = bfd_get_section_by_name (info->output_bfd, ".rel.dyn");
3744 for (pp = &info->output_bfd->sections; (s = *pp) != NULL;)
3745 if (s->size == 0
3746 && (s == rela_dyn
3747 || s == rel_dyn
3748 || s == hash_table->srelplt->output_section
3749 || s == hash_table->splt->output_section))
3751 *pp = s->next;
3752 info->output_bfd->section_count--;
3753 strip_zero_sized = true;
3754 if (s == rela_dyn)
3755 s = rela_dyn;
3756 if (s == rel_dyn)
3757 s = rel_dyn;
3758 else if (s == hash_table->splt->output_section)
3760 s = hash_table->splt;
3761 strip_zero_sized_plt = true;
3763 else
3764 s = hash_table->srelplt;
3765 s->flags |= SEC_EXCLUDE;
3766 s->output_section = bfd_abs_section_ptr;
3768 else
3769 pp = &s->next;
3771 if (strip_zero_sized_plt && sdynamic->size != 0)
3772 for (extdyn = sdynamic->contents;
3773 extdyn < sdynamic->contents + sdynamic->size;
3774 extdyn = next)
3776 next = extdyn + bed->s->sizeof_dyn;
3777 swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3778 switch (dyn.d_tag)
3780 default:
3781 break;
3782 case DT_JMPREL:
3783 case DT_PLTRELSZ:
3784 case DT_PLTREL:
3785 /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
3786 the procedure linkage table (the .plt section) has been
3787 removed. */
3788 memmove (extdyn, next,
3789 sdynamic->size - (next - sdynamic->contents));
3790 next = extdyn;
3794 if (strip_zero_sized)
3796 /* Regenerate program headers. */
3797 elf_seg_map (info->output_bfd) = NULL;
3798 return _bfd_elf_map_sections_to_segments (info->output_bfd, info,
3799 NULL);
3802 return true;
3805 /* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error,
3806 1 if a DT_NEEDED tag already exists, and 0 on success. */
3809 bfd_elf_add_dt_needed_tag (bfd *abfd, struct bfd_link_info *info)
3811 struct elf_link_hash_table *hash_table;
3812 size_t strindex;
3813 const char *soname;
3815 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3816 return -1;
3818 hash_table = elf_hash_table (info);
3819 soname = elf_dt_name (abfd);
3820 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, false);
3821 if (strindex == (size_t) -1)
3822 return -1;
3824 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3826 asection *sdyn;
3827 const struct elf_backend_data *bed;
3828 bfd_byte *extdyn;
3830 bed = get_elf_backend_data (hash_table->dynobj);
3831 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3832 if (sdyn != NULL && sdyn->size != 0)
3833 for (extdyn = sdyn->contents;
3834 extdyn < sdyn->contents + sdyn->size;
3835 extdyn += bed->s->sizeof_dyn)
3837 Elf_Internal_Dyn dyn;
3839 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3840 if (dyn.d_tag == DT_NEEDED
3841 && dyn.d_un.d_val == strindex)
3843 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3844 return 1;
3849 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3850 return -1;
3852 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3853 return -1;
3855 return 0;
3858 /* Return true if SONAME is on the needed list between NEEDED and STOP
3859 (or the end of list if STOP is NULL), and needed by a library that
3860 will be loaded. */
3862 static bool
3863 on_needed_list (const char *soname,
3864 struct bfd_link_needed_list *needed,
3865 struct bfd_link_needed_list *stop)
3867 struct bfd_link_needed_list *look;
3868 for (look = needed; look != stop; look = look->next)
3869 if (strcmp (soname, look->name) == 0
3870 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3871 /* If needed by a library that itself is not directly
3872 needed, recursively check whether that library is
3873 indirectly needed. Since we add DT_NEEDED entries to
3874 the end of the list, library dependencies appear after
3875 the library. Therefore search prior to the current
3876 LOOK, preventing possible infinite recursion. */
3877 || on_needed_list (elf_dt_name (look->by), needed, look)))
3878 return true;
3880 return false;
3883 /* Sort symbol by value, section, size, and type. */
3884 static int
3885 elf_sort_symbol (const void *arg1, const void *arg2)
3887 const struct elf_link_hash_entry *h1;
3888 const struct elf_link_hash_entry *h2;
3889 bfd_signed_vma vdiff;
3890 int sdiff;
3891 const char *n1;
3892 const char *n2;
3894 h1 = *(const struct elf_link_hash_entry **) arg1;
3895 h2 = *(const struct elf_link_hash_entry **) arg2;
3896 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3897 if (vdiff != 0)
3898 return vdiff > 0 ? 1 : -1;
3900 sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3901 if (sdiff != 0)
3902 return sdiff;
3904 /* Sort so that sized symbols are selected over zero size symbols. */
3905 vdiff = h1->size - h2->size;
3906 if (vdiff != 0)
3907 return vdiff > 0 ? 1 : -1;
3909 /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */
3910 if (h1->type != h2->type)
3911 return h1->type - h2->type;
3913 /* If symbols are properly sized and typed, and multiple strong
3914 aliases are not defined in a shared library by the user we
3915 shouldn't get here. Unfortunately linker script symbols like
3916 __bss_start sometimes match a user symbol defined at the start of
3917 .bss without proper size and type. We'd like to preference the
3918 user symbol over reserved system symbols. Sort on leading
3919 underscores. */
3920 n1 = h1->root.root.string;
3921 n2 = h2->root.root.string;
3922 while (*n1 == *n2)
3924 if (*n1 == 0)
3925 break;
3926 ++n1;
3927 ++n2;
3929 if (*n1 == '_')
3930 return -1;
3931 if (*n2 == '_')
3932 return 1;
3934 /* Final sort on name selects user symbols like '_u' over reserved
3935 system symbols like '_Z' and also will avoid qsort instability. */
3936 return *n1 - *n2;
3939 /* This function is used to adjust offsets into .dynstr for
3940 dynamic symbols. This is called via elf_link_hash_traverse. */
3942 static bool
3943 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3945 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3947 if (h->dynindx != -1)
3948 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3949 return true;
3952 /* Assign string offsets in .dynstr, update all structures referencing
3953 them. */
3955 static bool
3956 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3958 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3959 struct elf_link_local_dynamic_entry *entry;
3960 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3961 bfd *dynobj = hash_table->dynobj;
3962 asection *sdyn;
3963 bfd_size_type size;
3964 const struct elf_backend_data *bed;
3965 bfd_byte *extdyn;
3967 _bfd_elf_strtab_finalize (dynstr);
3968 size = _bfd_elf_strtab_size (dynstr);
3970 /* Allow the linker to examine the dynsymtab now it's fully populated. */
3972 if (info->callbacks->examine_strtab)
3973 info->callbacks->examine_strtab (dynstr);
3975 bed = get_elf_backend_data (dynobj);
3976 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3977 BFD_ASSERT (sdyn != NULL);
3979 /* Update all .dynamic entries referencing .dynstr strings. */
3980 for (extdyn = sdyn->contents;
3981 extdyn < PTR_ADD (sdyn->contents, sdyn->size);
3982 extdyn += bed->s->sizeof_dyn)
3984 Elf_Internal_Dyn dyn;
3986 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3987 switch (dyn.d_tag)
3989 case DT_STRSZ:
3990 dyn.d_un.d_val = size;
3991 break;
3992 case DT_NEEDED:
3993 case DT_SONAME:
3994 case DT_RPATH:
3995 case DT_RUNPATH:
3996 case DT_FILTER:
3997 case DT_AUXILIARY:
3998 case DT_AUDIT:
3999 case DT_DEPAUDIT:
4000 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
4001 break;
4002 default:
4003 continue;
4005 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
4008 /* Now update local dynamic symbols. */
4009 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
4010 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
4011 entry->isym.st_name);
4013 /* And the rest of dynamic symbols. */
4014 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
4016 /* Adjust version definitions. */
4017 if (elf_tdata (output_bfd)->cverdefs)
4019 asection *s;
4020 bfd_byte *p;
4021 size_t i;
4022 Elf_Internal_Verdef def;
4023 Elf_Internal_Verdaux defaux;
4025 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
4026 p = s->contents;
4029 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
4030 &def);
4031 p += sizeof (Elf_External_Verdef);
4032 if (def.vd_aux != sizeof (Elf_External_Verdef))
4033 continue;
4034 for (i = 0; i < def.vd_cnt; ++i)
4036 _bfd_elf_swap_verdaux_in (output_bfd,
4037 (Elf_External_Verdaux *) p, &defaux);
4038 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
4039 defaux.vda_name);
4040 _bfd_elf_swap_verdaux_out (output_bfd,
4041 &defaux, (Elf_External_Verdaux *) p);
4042 p += sizeof (Elf_External_Verdaux);
4045 while (def.vd_next);
4048 /* Adjust version references. */
4049 if (elf_tdata (output_bfd)->verref)
4051 asection *s;
4052 bfd_byte *p;
4053 size_t i;
4054 Elf_Internal_Verneed need;
4055 Elf_Internal_Vernaux needaux;
4057 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
4058 p = s->contents;
4061 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
4062 &need);
4063 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
4064 _bfd_elf_swap_verneed_out (output_bfd, &need,
4065 (Elf_External_Verneed *) p);
4066 p += sizeof (Elf_External_Verneed);
4067 for (i = 0; i < need.vn_cnt; ++i)
4069 _bfd_elf_swap_vernaux_in (output_bfd,
4070 (Elf_External_Vernaux *) p, &needaux);
4071 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
4072 needaux.vna_name);
4073 _bfd_elf_swap_vernaux_out (output_bfd,
4074 &needaux,
4075 (Elf_External_Vernaux *) p);
4076 p += sizeof (Elf_External_Vernaux);
4079 while (need.vn_next);
4082 return true;
4085 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4086 The default is to only match when the INPUT and OUTPUT are exactly
4087 the same target. */
4089 bool
4090 _bfd_elf_default_relocs_compatible (const bfd_target *input,
4091 const bfd_target *output)
4093 return input == output;
4096 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4097 This version is used when different targets for the same architecture
4098 are virtually identical. */
4100 bool
4101 _bfd_elf_relocs_compatible (const bfd_target *input,
4102 const bfd_target *output)
4104 const struct elf_backend_data *obed, *ibed;
4106 if (input == output)
4107 return true;
4109 ibed = xvec_get_elf_backend_data (input);
4110 obed = xvec_get_elf_backend_data (output);
4112 if (ibed->arch != obed->arch)
4113 return false;
4115 /* If both backends are using this function, deem them compatible. */
4116 return ibed->relocs_compatible == obed->relocs_compatible;
4119 /* Make a special call to the linker "notice" function to tell it that
4120 we are about to handle an as-needed lib, or have finished
4121 processing the lib. */
4123 bool
4124 _bfd_elf_notice_as_needed (bfd *ibfd,
4125 struct bfd_link_info *info,
4126 enum notice_asneeded_action act)
4128 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
4131 /* Call ACTION on each relocation in an ELF object file. */
4133 bool
4134 _bfd_elf_link_iterate_on_relocs
4135 (bfd *abfd, struct bfd_link_info *info,
4136 bool (*action) (bfd *, struct bfd_link_info *, asection *,
4137 const Elf_Internal_Rela *))
4139 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4140 struct elf_link_hash_table *htab = elf_hash_table (info);
4142 /* If this object is the same format as the output object, and it is
4143 not a shared library, then let the backend look through the
4144 relocs.
4146 This is required to build global offset table entries and to
4147 arrange for dynamic relocs. It is not required for the
4148 particular common case of linking non PIC code, even when linking
4149 against shared libraries, but unfortunately there is no way of
4150 knowing whether an object file has been compiled PIC or not.
4151 Looking through the relocs is not particularly time consuming.
4152 The problem is that we must either (1) keep the relocs in memory,
4153 which causes the linker to require additional runtime memory or
4154 (2) read the relocs twice from the input file, which wastes time.
4155 This would be a good case for using mmap.
4157 I have no idea how to handle linking PIC code into a file of a
4158 different format. It probably can't be done. */
4159 if ((abfd->flags & DYNAMIC) == 0
4160 && is_elf_hash_table (&htab->root)
4161 && elf_object_id (abfd) == elf_hash_table_id (htab)
4162 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4164 asection *o;
4166 for (o = abfd->sections; o != NULL; o = o->next)
4168 Elf_Internal_Rela *internal_relocs;
4169 bool ok;
4171 /* Don't check relocations in excluded sections. Don't do
4172 anything special with non-loaded, non-alloced sections.
4173 In particular, any relocs in such sections should not
4174 affect GOT and PLT reference counting (ie. we don't
4175 allow them to create GOT or PLT entries), there's no
4176 possibility or desire to optimize TLS relocs, and
4177 there's not much point in propagating relocs to shared
4178 libs that the dynamic linker won't relocate. */
4179 if ((o->flags & SEC_ALLOC) == 0
4180 || (o->flags & SEC_RELOC) == 0
4181 || (o->flags & SEC_EXCLUDE) != 0
4182 || o->reloc_count == 0
4183 || ((info->strip == strip_all || info->strip == strip_debugger)
4184 && (o->flags & SEC_DEBUGGING) != 0)
4185 || bfd_is_abs_section (o->output_section))
4186 continue;
4188 internal_relocs = _bfd_elf_link_info_read_relocs (abfd, info,
4189 o, NULL,
4190 NULL,
4191 _bfd_link_keep_memory (info));
4192 if (internal_relocs == NULL)
4193 return false;
4195 ok = action (abfd, info, o, internal_relocs);
4197 if (elf_section_data (o)->relocs != internal_relocs)
4198 free (internal_relocs);
4200 if (! ok)
4201 return false;
4205 return true;
4208 /* Check relocations in an ELF object file. This is called after
4209 all input files have been opened. */
4211 bool
4212 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
4214 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4215 if (bed->check_relocs != NULL)
4216 return _bfd_elf_link_iterate_on_relocs (abfd, info,
4217 bed->check_relocs);
4218 return true;
4221 /* Add symbols from an ELF object file to the linker hash table. */
4223 static bool
4224 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
4226 Elf_Internal_Ehdr *ehdr;
4227 Elf_Internal_Shdr *hdr;
4228 size_t symcount;
4229 size_t extsymcount;
4230 size_t extsymoff;
4231 struct elf_link_hash_entry **sym_hash;
4232 bool dynamic;
4233 Elf_External_Versym *extversym = NULL;
4234 Elf_External_Versym *extversym_end = NULL;
4235 Elf_External_Versym *ever;
4236 struct elf_link_hash_entry *weaks;
4237 struct elf_link_hash_entry **nondeflt_vers = NULL;
4238 size_t nondeflt_vers_cnt = 0;
4239 Elf_Internal_Sym *isymbuf = NULL;
4240 Elf_Internal_Sym *isym;
4241 Elf_Internal_Sym *isymend;
4242 const struct elf_backend_data *bed;
4243 bool add_needed;
4244 struct elf_link_hash_table *htab;
4245 void *alloc_mark = NULL;
4246 struct bfd_hash_entry **old_table = NULL;
4247 unsigned int old_size = 0;
4248 unsigned int old_count = 0;
4249 void *old_tab = NULL;
4250 void *old_ent;
4251 struct bfd_link_hash_entry *old_undefs = NULL;
4252 struct bfd_link_hash_entry *old_undefs_tail = NULL;
4253 void *old_strtab = NULL;
4254 size_t tabsize = 0;
4255 asection *s;
4256 bool just_syms;
4258 htab = elf_hash_table (info);
4259 bed = get_elf_backend_data (abfd);
4261 if (elf_use_dt_symtab_p (abfd))
4263 bfd_set_error (bfd_error_wrong_format);
4264 return false;
4267 if ((abfd->flags & DYNAMIC) == 0)
4268 dynamic = false;
4269 else
4271 dynamic = true;
4273 /* You can't use -r against a dynamic object. Also, there's no
4274 hope of using a dynamic object which does not exactly match
4275 the format of the output file. */
4276 if (bfd_link_relocatable (info)
4277 || !is_elf_hash_table (&htab->root)
4278 || info->output_bfd->xvec != abfd->xvec)
4280 if (bfd_link_relocatable (info))
4281 bfd_set_error (bfd_error_invalid_operation);
4282 else
4283 bfd_set_error (bfd_error_wrong_format);
4284 goto error_return;
4288 ehdr = elf_elfheader (abfd);
4289 if (info->warn_alternate_em
4290 && bed->elf_machine_code != ehdr->e_machine
4291 && ((bed->elf_machine_alt1 != 0
4292 && ehdr->e_machine == bed->elf_machine_alt1)
4293 || (bed->elf_machine_alt2 != 0
4294 && ehdr->e_machine == bed->elf_machine_alt2)))
4295 _bfd_error_handler
4296 /* xgettext:c-format */
4297 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
4298 ehdr->e_machine, abfd, bed->elf_machine_code);
4300 /* As a GNU extension, any input sections which are named
4301 .gnu.warning.SYMBOL are treated as warning symbols for the given
4302 symbol. This differs from .gnu.warning sections, which generate
4303 warnings when they are included in an output file. */
4304 /* PR 12761: Also generate this warning when building shared libraries. */
4305 for (s = abfd->sections; s != NULL; s = s->next)
4307 const char *name;
4309 name = bfd_section_name (s);
4310 if (startswith (name, ".gnu.warning."))
4312 char *msg;
4313 bfd_size_type sz;
4315 name += sizeof ".gnu.warning." - 1;
4317 /* If this is a shared object, then look up the symbol
4318 in the hash table. If it is there, and it is already
4319 been defined, then we will not be using the entry
4320 from this shared object, so we don't need to warn.
4321 FIXME: If we see the definition in a regular object
4322 later on, we will warn, but we shouldn't. The only
4323 fix is to keep track of what warnings we are supposed
4324 to emit, and then handle them all at the end of the
4325 link. */
4326 if (dynamic)
4328 struct elf_link_hash_entry *h;
4330 h = elf_link_hash_lookup (htab, name, false, false, true);
4332 /* FIXME: What about bfd_link_hash_common? */
4333 if (h != NULL
4334 && (h->root.type == bfd_link_hash_defined
4335 || h->root.type == bfd_link_hash_defweak))
4336 continue;
4339 sz = s->size;
4340 msg = (char *) bfd_alloc (abfd, sz + 1);
4341 if (msg == NULL)
4342 goto error_return;
4344 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
4345 goto error_return;
4347 msg[sz] = '\0';
4349 if (! (_bfd_generic_link_add_one_symbol
4350 (info, abfd, name, BSF_WARNING, s, 0, msg,
4351 false, bed->collect, NULL)))
4352 goto error_return;
4354 if (bfd_link_executable (info))
4356 /* Clobber the section size so that the warning does
4357 not get copied into the output file. */
4358 s->size = 0;
4360 /* Also set SEC_EXCLUDE, so that symbols defined in
4361 the warning section don't get copied to the output. */
4362 s->flags |= SEC_EXCLUDE;
4367 just_syms = ((s = abfd->sections) != NULL
4368 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
4370 add_needed = true;
4371 if (! dynamic)
4373 /* If we are creating a shared library, create all the dynamic
4374 sections immediately. We need to attach them to something,
4375 so we attach them to this BFD, provided it is the right
4376 format and is not from ld --just-symbols. Always create the
4377 dynamic sections for -E/--dynamic-list. FIXME: If there
4378 are no input BFD's of the same format as the output, we can't
4379 make a shared library. */
4380 if (!just_syms
4381 && (bfd_link_pic (info)
4382 || (!bfd_link_relocatable (info)
4383 && info->nointerp
4384 && (info->export_dynamic || info->dynamic)))
4385 && is_elf_hash_table (&htab->root)
4386 && info->output_bfd->xvec == abfd->xvec
4387 && !htab->dynamic_sections_created)
4389 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4390 goto error_return;
4393 else if (!is_elf_hash_table (&htab->root))
4394 goto error_return;
4395 else
4397 const char *soname = NULL;
4398 char *audit = NULL;
4399 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
4400 const Elf_Internal_Phdr *phdr;
4401 struct elf_link_loaded_list *loaded_lib;
4403 /* ld --just-symbols and dynamic objects don't mix very well.
4404 ld shouldn't allow it. */
4405 if (just_syms)
4406 abort ();
4408 /* If this dynamic lib was specified on the command line with
4409 --as-needed in effect, then we don't want to add a DT_NEEDED
4410 tag unless the lib is actually used. Similary for libs brought
4411 in by another lib's DT_NEEDED. When --no-add-needed is used
4412 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4413 any dynamic library in DT_NEEDED tags in the dynamic lib at
4414 all. */
4415 add_needed = (elf_dyn_lib_class (abfd)
4416 & (DYN_AS_NEEDED | DYN_DT_NEEDED
4417 | DYN_NO_NEEDED)) == 0;
4419 s = bfd_get_section_by_name (abfd, ".dynamic");
4420 if (s != NULL && s->size != 0 && (s->flags & SEC_HAS_CONTENTS) != 0)
4422 bfd_byte *dynbuf;
4423 bfd_byte *extdyn;
4424 unsigned int elfsec;
4425 unsigned long shlink;
4427 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
4429 error_free_dyn:
4430 free (dynbuf);
4431 goto error_return;
4434 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
4435 if (elfsec == SHN_BAD)
4436 goto error_free_dyn;
4437 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4439 for (extdyn = dynbuf;
4440 (size_t) (dynbuf + s->size - extdyn) >= bed->s->sizeof_dyn;
4441 extdyn += bed->s->sizeof_dyn)
4443 Elf_Internal_Dyn dyn;
4445 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4446 if (dyn.d_tag == DT_SONAME)
4448 unsigned int tagv = dyn.d_un.d_val;
4449 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4450 if (soname == NULL)
4451 goto error_free_dyn;
4453 if (dyn.d_tag == DT_NEEDED)
4455 struct bfd_link_needed_list *n, **pn;
4456 char *fnm, *anm;
4457 unsigned int tagv = dyn.d_un.d_val;
4458 size_t amt = sizeof (struct bfd_link_needed_list);
4460 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4461 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4462 if (n == NULL || fnm == NULL)
4463 goto error_free_dyn;
4464 amt = strlen (fnm) + 1;
4465 anm = (char *) bfd_alloc (abfd, amt);
4466 if (anm == NULL)
4467 goto error_free_dyn;
4468 memcpy (anm, fnm, amt);
4469 n->name = anm;
4470 n->by = abfd;
4471 n->next = NULL;
4472 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4474 *pn = n;
4476 if (dyn.d_tag == DT_RUNPATH)
4478 struct bfd_link_needed_list *n, **pn;
4479 char *fnm, *anm;
4480 unsigned int tagv = dyn.d_un.d_val;
4481 size_t amt = sizeof (struct bfd_link_needed_list);
4483 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4484 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4485 if (n == NULL || fnm == NULL)
4486 goto error_free_dyn;
4487 amt = strlen (fnm) + 1;
4488 anm = (char *) bfd_alloc (abfd, amt);
4489 if (anm == NULL)
4490 goto error_free_dyn;
4491 memcpy (anm, fnm, amt);
4492 n->name = anm;
4493 n->by = abfd;
4494 n->next = NULL;
4495 for (pn = & runpath;
4496 *pn != NULL;
4497 pn = &(*pn)->next)
4499 *pn = n;
4501 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4502 if (!runpath && dyn.d_tag == DT_RPATH)
4504 struct bfd_link_needed_list *n, **pn;
4505 char *fnm, *anm;
4506 unsigned int tagv = dyn.d_un.d_val;
4507 size_t amt = sizeof (struct bfd_link_needed_list);
4509 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4510 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4511 if (n == NULL || fnm == NULL)
4512 goto error_free_dyn;
4513 amt = strlen (fnm) + 1;
4514 anm = (char *) bfd_alloc (abfd, amt);
4515 if (anm == NULL)
4516 goto error_free_dyn;
4517 memcpy (anm, fnm, amt);
4518 n->name = anm;
4519 n->by = abfd;
4520 n->next = NULL;
4521 for (pn = & rpath;
4522 *pn != NULL;
4523 pn = &(*pn)->next)
4525 *pn = n;
4527 if (dyn.d_tag == DT_AUDIT)
4529 unsigned int tagv = dyn.d_un.d_val;
4530 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4532 if (dyn.d_tag == DT_FLAGS_1)
4533 elf_tdata (abfd)->is_pie = (dyn.d_un.d_val & DF_1_PIE) != 0;
4536 free (dynbuf);
4539 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4540 frees all more recently bfd_alloc'd blocks as well. */
4541 if (runpath)
4542 rpath = runpath;
4544 if (rpath)
4546 struct bfd_link_needed_list **pn;
4547 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4549 *pn = rpath;
4552 /* If we have a PT_GNU_RELRO program header, mark as read-only
4553 all sections contained fully therein. This makes relro
4554 shared library sections appear as they will at run-time. */
4555 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4556 while (phdr-- > elf_tdata (abfd)->phdr)
4557 if (phdr->p_type == PT_GNU_RELRO)
4559 for (s = abfd->sections; s != NULL; s = s->next)
4561 unsigned int opb = bfd_octets_per_byte (abfd, s);
4563 if ((s->flags & SEC_ALLOC) != 0
4564 && s->vma * opb >= phdr->p_vaddr
4565 && s->vma * opb + s->size <= phdr->p_vaddr + phdr->p_memsz)
4566 s->flags |= SEC_READONLY;
4568 break;
4571 /* We do not want to include any of the sections in a dynamic
4572 object in the output file. We hack by simply clobbering the
4573 list of sections in the BFD. This could be handled more
4574 cleanly by, say, a new section flag; the existing
4575 SEC_NEVER_LOAD flag is not the one we want, because that one
4576 still implies that the section takes up space in the output
4577 file. */
4578 bfd_section_list_clear (abfd);
4580 /* Find the name to use in a DT_NEEDED entry that refers to this
4581 object. If the object has a DT_SONAME entry, we use it.
4582 Otherwise, if the generic linker stuck something in
4583 elf_dt_name, we use that. Otherwise, we just use the file
4584 name. */
4585 if (soname == NULL || *soname == '\0')
4587 soname = elf_dt_name (abfd);
4588 if (soname == NULL || *soname == '\0')
4589 soname = bfd_get_filename (abfd);
4592 /* Save the SONAME because sometimes the linker emulation code
4593 will need to know it. */
4594 elf_dt_name (abfd) = soname;
4596 /* If we have already included this dynamic object in the
4597 link, just ignore it. There is no reason to include a
4598 particular dynamic object more than once. */
4599 for (loaded_lib = htab->dyn_loaded;
4600 loaded_lib != NULL;
4601 loaded_lib = loaded_lib->next)
4603 if (strcmp (elf_dt_name (loaded_lib->abfd), soname) == 0)
4604 return true;
4607 /* Create dynamic sections for backends that require that be done
4608 before setup_gnu_properties. */
4609 if (add_needed
4610 && !_bfd_elf_link_create_dynamic_sections (abfd, info))
4611 return false;
4613 /* Save the DT_AUDIT entry for the linker emulation code. */
4614 elf_dt_audit (abfd) = audit;
4617 /* If this is a dynamic object, we always link against the .dynsym
4618 symbol table, not the .symtab symbol table. The dynamic linker
4619 will only see the .dynsym symbol table, so there is no reason to
4620 look at .symtab for a dynamic object. */
4622 if (! dynamic || elf_dynsymtab (abfd) == 0)
4623 hdr = &elf_tdata (abfd)->symtab_hdr;
4624 else
4625 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4627 symcount = hdr->sh_size / bed->s->sizeof_sym;
4629 /* The sh_info field of the symtab header tells us where the
4630 external symbols start. We don't care about the local symbols at
4631 this point. */
4632 if (elf_bad_symtab (abfd))
4634 extsymcount = symcount;
4635 extsymoff = 0;
4637 else
4639 extsymcount = symcount - hdr->sh_info;
4640 extsymoff = hdr->sh_info;
4643 sym_hash = elf_sym_hashes (abfd);
4644 if (extsymcount != 0)
4646 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4647 NULL, NULL, NULL);
4648 if (isymbuf == NULL)
4649 goto error_return;
4651 if (sym_hash == NULL)
4653 /* We store a pointer to the hash table entry for each
4654 external symbol. */
4655 size_t amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4656 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4657 if (sym_hash == NULL)
4658 goto error_free_sym;
4659 elf_sym_hashes (abfd) = sym_hash;
4663 if (dynamic)
4665 /* Read in any version definitions. */
4666 if (!_bfd_elf_slurp_version_tables (abfd,
4667 info->default_imported_symver))
4668 goto error_free_sym;
4670 /* Read in the symbol versions, but don't bother to convert them
4671 to internal format. */
4672 if (elf_dynversym (abfd) != 0)
4674 Elf_Internal_Shdr *versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4675 bfd_size_type amt = versymhdr->sh_size;
4677 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0)
4678 goto error_free_sym;
4679 extversym = (Elf_External_Versym *)
4680 _bfd_malloc_and_read (abfd, amt, amt);
4681 if (extversym == NULL)
4682 goto error_free_sym;
4683 extversym_end = extversym + amt / sizeof (*extversym);
4687 /* If we are loading an as-needed shared lib, save the symbol table
4688 state before we start adding symbols. If the lib turns out
4689 to be unneeded, restore the state. */
4690 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4692 unsigned int i;
4693 size_t entsize;
4695 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4697 struct bfd_hash_entry *p;
4698 struct elf_link_hash_entry *h;
4700 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4702 h = (struct elf_link_hash_entry *) p;
4703 entsize += htab->root.table.entsize;
4704 if (h->root.type == bfd_link_hash_warning)
4706 entsize += htab->root.table.entsize;
4707 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4709 if (h->root.type == bfd_link_hash_common)
4710 entsize += sizeof (*h->root.u.c.p);
4714 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4715 old_tab = bfd_malloc (tabsize + entsize);
4716 if (old_tab == NULL)
4717 goto error_free_vers;
4719 /* Remember the current objalloc pointer, so that all mem for
4720 symbols added can later be reclaimed. */
4721 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4722 if (alloc_mark == NULL)
4723 goto error_free_vers;
4725 /* Make a special call to the linker "notice" function to
4726 tell it that we are about to handle an as-needed lib. */
4727 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4728 goto error_free_vers;
4730 /* Clone the symbol table. Remember some pointers into the
4731 symbol table, and dynamic symbol count. */
4732 old_ent = (char *) old_tab + tabsize;
4733 memcpy (old_tab, htab->root.table.table, tabsize);
4734 old_undefs = htab->root.undefs;
4735 old_undefs_tail = htab->root.undefs_tail;
4736 old_table = htab->root.table.table;
4737 old_size = htab->root.table.size;
4738 old_count = htab->root.table.count;
4739 old_strtab = NULL;
4740 if (htab->dynstr != NULL)
4742 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4743 if (old_strtab == NULL)
4744 goto error_free_vers;
4747 for (i = 0; i < htab->root.table.size; i++)
4749 struct bfd_hash_entry *p;
4750 struct elf_link_hash_entry *h;
4752 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4754 h = (struct elf_link_hash_entry *) p;
4755 memcpy (old_ent, h, htab->root.table.entsize);
4756 old_ent = (char *) old_ent + htab->root.table.entsize;
4757 if (h->root.type == bfd_link_hash_warning)
4759 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4760 memcpy (old_ent, h, htab->root.table.entsize);
4761 old_ent = (char *) old_ent + htab->root.table.entsize;
4763 if (h->root.type == bfd_link_hash_common)
4765 memcpy (old_ent, h->root.u.c.p, sizeof (*h->root.u.c.p));
4766 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
4772 weaks = NULL;
4773 if (extversym == NULL)
4774 ever = NULL;
4775 else if (extversym + extsymoff < extversym_end)
4776 ever = extversym + extsymoff;
4777 else
4779 /* xgettext:c-format */
4780 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4781 abfd, (long) extsymoff,
4782 (long) (extversym_end - extversym) / sizeof (* extversym));
4783 bfd_set_error (bfd_error_bad_value);
4784 goto error_free_vers;
4787 if (!bfd_link_relocatable (info)
4788 && abfd->lto_slim_object)
4790 _bfd_error_handler
4791 (_("%pB: plugin needed to handle lto object"), abfd);
4794 for (isym = isymbuf, isymend = PTR_ADD (isymbuf, extsymcount);
4795 isym < isymend;
4796 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4798 int bind;
4799 bfd_vma value;
4800 asection *sec, *new_sec;
4801 flagword flags;
4802 const char *name;
4803 struct elf_link_hash_entry *h;
4804 struct elf_link_hash_entry *hi;
4805 bool definition;
4806 bool size_change_ok;
4807 bool type_change_ok;
4808 bool new_weak;
4809 bool old_weak;
4810 bfd *override;
4811 bool common;
4812 bool discarded;
4813 unsigned int old_alignment;
4814 unsigned int shindex;
4815 bfd *old_bfd;
4816 bool matched;
4818 override = NULL;
4820 flags = BSF_NO_FLAGS;
4821 sec = NULL;
4822 value = isym->st_value;
4823 common = bed->common_definition (isym);
4824 if (common && info->inhibit_common_definition)
4826 /* Treat common symbol as undefined for --no-define-common. */
4827 isym->st_shndx = SHN_UNDEF;
4828 common = false;
4830 discarded = false;
4832 bind = ELF_ST_BIND (isym->st_info);
4833 switch (bind)
4835 case STB_LOCAL:
4836 /* This should be impossible, since ELF requires that all
4837 global symbols follow all local symbols, and that sh_info
4838 point to the first global symbol. Unfortunately, Irix 5
4839 screws this up. */
4840 if (elf_bad_symtab (abfd))
4841 continue;
4843 /* If we aren't prepared to handle locals within the globals
4844 then we'll likely segfault on a NULL symbol hash if the
4845 symbol is ever referenced in relocations. */
4846 shindex = elf_elfheader (abfd)->e_shstrndx;
4847 name = bfd_elf_string_from_elf_section (abfd, shindex, hdr->sh_name);
4848 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4849 " (>= sh_info of %lu)"),
4850 abfd, name, (long) (isym - isymbuf + extsymoff),
4851 (long) extsymoff);
4853 /* Dynamic object relocations are not processed by ld, so
4854 ld won't run into the problem mentioned above. */
4855 if (dynamic)
4856 continue;
4857 bfd_set_error (bfd_error_bad_value);
4858 goto error_free_vers;
4860 case STB_GLOBAL:
4861 if (isym->st_shndx != SHN_UNDEF && !common)
4862 flags = BSF_GLOBAL;
4863 break;
4865 case STB_WEAK:
4866 flags = BSF_WEAK;
4867 break;
4869 case STB_GNU_UNIQUE:
4870 flags = BSF_GNU_UNIQUE;
4871 break;
4873 default:
4874 /* Leave it up to the processor backend. */
4875 break;
4878 if (isym->st_shndx == SHN_UNDEF)
4879 sec = bfd_und_section_ptr;
4880 else if (isym->st_shndx == SHN_ABS)
4881 sec = bfd_abs_section_ptr;
4882 else if (isym->st_shndx == SHN_COMMON)
4884 sec = bfd_com_section_ptr;
4885 /* What ELF calls the size we call the value. What ELF
4886 calls the value we call the alignment. */
4887 value = isym->st_size;
4889 else
4891 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4892 if (sec == NULL)
4893 sec = bfd_abs_section_ptr;
4894 else if (discarded_section (sec))
4896 /* Symbols from discarded section are undefined. We keep
4897 its visibility. */
4898 sec = bfd_und_section_ptr;
4899 discarded = true;
4900 isym->st_shndx = SHN_UNDEF;
4902 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4903 value -= sec->vma;
4906 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4907 isym->st_name);
4908 if (name == NULL)
4909 goto error_free_vers;
4911 if (isym->st_shndx == SHN_COMMON
4912 && (abfd->flags & BFD_PLUGIN) != 0)
4914 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4916 if (xc == NULL)
4918 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4919 | SEC_EXCLUDE);
4920 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4921 if (xc == NULL)
4922 goto error_free_vers;
4924 sec = xc;
4926 else if (isym->st_shndx == SHN_COMMON
4927 && ELF_ST_TYPE (isym->st_info) == STT_TLS
4928 && !bfd_link_relocatable (info))
4930 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4932 if (tcomm == NULL)
4934 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4935 | SEC_LINKER_CREATED);
4936 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4937 if (tcomm == NULL)
4938 goto error_free_vers;
4940 sec = tcomm;
4942 else if (bed->elf_add_symbol_hook)
4944 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4945 &sec, &value))
4946 goto error_free_vers;
4948 /* The hook function sets the name to NULL if this symbol
4949 should be skipped for some reason. */
4950 if (name == NULL)
4951 continue;
4954 /* Sanity check that all possibilities were handled. */
4955 if (sec == NULL)
4956 abort ();
4958 /* Silently discard TLS symbols from --just-syms. There's
4959 no way to combine a static TLS block with a new TLS block
4960 for this executable. */
4961 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4962 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4963 continue;
4965 if (bfd_is_und_section (sec)
4966 || bfd_is_com_section (sec))
4967 definition = false;
4968 else
4969 definition = true;
4971 size_change_ok = false;
4972 type_change_ok = bed->type_change_ok;
4973 old_weak = false;
4974 matched = false;
4975 old_alignment = 0;
4976 old_bfd = NULL;
4977 new_sec = sec;
4979 if (is_elf_hash_table (&htab->root))
4981 Elf_Internal_Versym iver;
4982 unsigned int vernum = 0;
4983 bool skip;
4985 if (ever == NULL)
4987 if (info->default_imported_symver)
4988 /* Use the default symbol version created earlier. */
4989 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4990 else
4991 iver.vs_vers = 0;
4993 else if (ever >= extversym_end)
4995 /* xgettext:c-format */
4996 _bfd_error_handler (_("%pB: not enough version information"),
4997 abfd);
4998 bfd_set_error (bfd_error_bad_value);
4999 goto error_free_vers;
5001 else
5002 _bfd_elf_swap_versym_in (abfd, ever, &iver);
5004 vernum = iver.vs_vers & VERSYM_VERSION;
5006 /* If this is a hidden symbol, or if it is not version
5007 1, we append the version name to the symbol name.
5008 However, we do not modify a non-hidden absolute symbol
5009 if it is not a function, because it might be the version
5010 symbol itself. FIXME: What if it isn't? */
5011 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
5012 || (vernum > 1
5013 && (!bfd_is_abs_section (sec)
5014 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
5016 const char *verstr;
5017 size_t namelen, verlen, newlen;
5018 char *newname, *p;
5020 if (isym->st_shndx != SHN_UNDEF)
5022 if (vernum > elf_tdata (abfd)->cverdefs)
5023 verstr = NULL;
5024 else if (vernum > 1)
5025 verstr =
5026 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
5027 else
5028 verstr = "";
5030 if (verstr == NULL)
5032 _bfd_error_handler
5033 /* xgettext:c-format */
5034 (_("%pB: %s: invalid version %u (max %d)"),
5035 abfd, name, vernum,
5036 elf_tdata (abfd)->cverdefs);
5037 bfd_set_error (bfd_error_bad_value);
5038 goto error_free_vers;
5041 else
5043 /* We cannot simply test for the number of
5044 entries in the VERNEED section since the
5045 numbers for the needed versions do not start
5046 at 0. */
5047 Elf_Internal_Verneed *t;
5049 verstr = NULL;
5050 for (t = elf_tdata (abfd)->verref;
5051 t != NULL;
5052 t = t->vn_nextref)
5054 Elf_Internal_Vernaux *a;
5056 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5058 if (a->vna_other == vernum)
5060 verstr = a->vna_nodename;
5061 break;
5064 if (a != NULL)
5065 break;
5067 if (verstr == NULL)
5069 _bfd_error_handler
5070 /* xgettext:c-format */
5071 (_("%pB: %s: invalid needed version %d"),
5072 abfd, name, vernum);
5073 bfd_set_error (bfd_error_bad_value);
5074 goto error_free_vers;
5078 namelen = strlen (name);
5079 verlen = strlen (verstr);
5080 newlen = namelen + verlen + 2;
5081 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
5082 && isym->st_shndx != SHN_UNDEF)
5083 ++newlen;
5085 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
5086 if (newname == NULL)
5087 goto error_free_vers;
5088 memcpy (newname, name, namelen);
5089 p = newname + namelen;
5090 *p++ = ELF_VER_CHR;
5091 /* If this is a defined non-hidden version symbol,
5092 we add another @ to the name. This indicates the
5093 default version of the symbol. */
5094 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
5095 && isym->st_shndx != SHN_UNDEF)
5096 *p++ = ELF_VER_CHR;
5097 memcpy (p, verstr, verlen + 1);
5099 name = newname;
5102 /* If this symbol has default visibility and the user has
5103 requested we not re-export it, then mark it as hidden. */
5104 if (!bfd_is_und_section (sec)
5105 && !dynamic
5106 && abfd->no_export
5107 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
5108 isym->st_other = (STV_HIDDEN
5109 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
5111 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
5112 sym_hash, &old_bfd, &old_weak,
5113 &old_alignment, &skip, &override,
5114 &type_change_ok, &size_change_ok,
5115 &matched))
5116 goto error_free_vers;
5118 if (skip)
5119 continue;
5121 /* Override a definition only if the new symbol matches the
5122 existing one. */
5123 if (override && matched)
5124 definition = false;
5126 h = *sym_hash;
5127 while (h->root.type == bfd_link_hash_indirect
5128 || h->root.type == bfd_link_hash_warning)
5129 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5131 if (h->versioned != unversioned
5132 && elf_tdata (abfd)->verdef != NULL
5133 && vernum > 1
5134 && definition)
5135 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
5138 if (! (_bfd_generic_link_add_one_symbol
5139 (info, override ? override : abfd, name, flags, sec, value,
5140 NULL, false, bed->collect,
5141 (struct bfd_link_hash_entry **) sym_hash)))
5142 goto error_free_vers;
5144 h = *sym_hash;
5145 /* We need to make sure that indirect symbol dynamic flags are
5146 updated. */
5147 hi = h;
5148 while (h->root.type == bfd_link_hash_indirect
5149 || h->root.type == bfd_link_hash_warning)
5150 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5152 *sym_hash = h;
5154 /* Setting the index to -3 tells elf_link_output_extsym that
5155 this symbol is defined in a discarded section. */
5156 if (discarded && is_elf_hash_table (&htab->root))
5157 h->indx = -3;
5159 new_weak = (flags & BSF_WEAK) != 0;
5160 if (dynamic
5161 && definition
5162 && new_weak
5163 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
5164 && is_elf_hash_table (&htab->root)
5165 && h->u.alias == NULL)
5167 /* Keep a list of all weak defined non function symbols from
5168 a dynamic object, using the alias field. Later in this
5169 function we will set the alias field to the correct
5170 value. We only put non-function symbols from dynamic
5171 objects on this list, because that happens to be the only
5172 time we need to know the normal symbol corresponding to a
5173 weak symbol, and the information is time consuming to
5174 figure out. If the alias field is not already NULL,
5175 then this symbol was already defined by some previous
5176 dynamic object, and we will be using that previous
5177 definition anyhow. */
5179 h->u.alias = weaks;
5180 weaks = h;
5183 /* Set the alignment of a common symbol. */
5184 if ((common || bfd_is_com_section (sec))
5185 && h->root.type == bfd_link_hash_common)
5187 unsigned int align;
5189 if (common)
5190 align = bfd_log2 (isym->st_value);
5191 else
5193 /* The new symbol is a common symbol in a shared object.
5194 We need to get the alignment from the section. */
5195 align = new_sec->alignment_power;
5197 if (align > old_alignment)
5198 h->root.u.c.p->alignment_power = align;
5199 else
5200 h->root.u.c.p->alignment_power = old_alignment;
5203 if (is_elf_hash_table (&htab->root))
5205 /* Set a flag in the hash table entry indicating the type of
5206 reference or definition we just found. A dynamic symbol
5207 is one which is referenced or defined by both a regular
5208 object and a shared object. */
5209 bool dynsym = false;
5211 /* Plugin symbols aren't normal. Don't set def/ref flags. */
5212 if ((abfd->flags & BFD_PLUGIN) != 0)
5214 /* Except for this flag to track nonweak references. */
5215 if (!definition
5216 && bind != STB_WEAK)
5217 h->ref_ir_nonweak = 1;
5219 else if (!dynamic)
5221 if (! definition)
5223 h->ref_regular = 1;
5224 if (bind != STB_WEAK)
5225 h->ref_regular_nonweak = 1;
5227 else
5229 h->def_regular = 1;
5230 if (h->def_dynamic)
5232 h->def_dynamic = 0;
5233 h->ref_dynamic = 1;
5237 else
5239 if (! definition)
5241 h->ref_dynamic = 1;
5242 hi->ref_dynamic = 1;
5244 else
5246 h->def_dynamic = 1;
5247 hi->def_dynamic = 1;
5251 /* If an indirect symbol has been forced local, don't
5252 make the real symbol dynamic. */
5253 if (h != hi && hi->forced_local)
5255 else if (!dynamic)
5257 if (bfd_link_dll (info)
5258 || h->def_dynamic
5259 || h->ref_dynamic)
5260 dynsym = true;
5262 else
5264 if (h->def_regular
5265 || h->ref_regular
5266 || (h->is_weakalias
5267 && weakdef (h)->dynindx != -1))
5268 dynsym = true;
5271 /* Check to see if we need to add an indirect symbol for
5272 the default name. */
5273 if ((definition
5274 || (!override && h->root.type == bfd_link_hash_common))
5275 && !(hi != h
5276 && hi->versioned == versioned_hidden))
5277 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
5278 sec, value, &old_bfd, &dynsym))
5279 goto error_free_vers;
5281 /* Check the alignment when a common symbol is involved. This
5282 can change when a common symbol is overridden by a normal
5283 definition or a common symbol is ignored due to the old
5284 normal definition. We need to make sure the maximum
5285 alignment is maintained. */
5286 if ((old_alignment || common)
5287 && h->root.type != bfd_link_hash_common)
5289 unsigned int common_align;
5290 unsigned int normal_align;
5291 unsigned int symbol_align;
5292 bfd *normal_bfd;
5293 bfd *common_bfd;
5295 BFD_ASSERT (h->root.type == bfd_link_hash_defined
5296 || h->root.type == bfd_link_hash_defweak);
5298 symbol_align = ffs (h->root.u.def.value) - 1;
5299 if (h->root.u.def.section->owner != NULL
5300 && (h->root.u.def.section->owner->flags
5301 & (DYNAMIC | BFD_PLUGIN)) == 0)
5303 normal_align = h->root.u.def.section->alignment_power;
5304 if (normal_align > symbol_align)
5305 normal_align = symbol_align;
5307 else
5308 normal_align = symbol_align;
5310 if (old_alignment)
5312 common_align = old_alignment;
5313 common_bfd = old_bfd;
5314 normal_bfd = abfd;
5316 else
5318 common_align = bfd_log2 (isym->st_value);
5319 common_bfd = abfd;
5320 normal_bfd = old_bfd;
5323 if (normal_align < common_align)
5325 /* PR binutils/2735 */
5326 if (normal_bfd == NULL)
5327 _bfd_error_handler
5328 /* xgettext:c-format */
5329 (_("warning: alignment %u of common symbol `%s' in %pB is"
5330 " greater than the alignment (%u) of its section %pA"),
5331 1 << common_align, name, common_bfd,
5332 1 << normal_align, h->root.u.def.section);
5333 else
5334 _bfd_error_handler
5335 /* xgettext:c-format */
5336 (_("warning: alignment %u of normal symbol `%s' in %pB"
5337 " is smaller than %u used by the common definition in %pB"),
5338 1 << normal_align, name, normal_bfd,
5339 1 << common_align, common_bfd);
5341 /* PR 30499: make sure that users understand that this warning is serious. */
5342 _bfd_error_handler
5343 (_("warning: NOTE: alignment discrepancies can cause real problems. Investigation is advised."));
5347 /* Remember the symbol size if it isn't undefined. */
5348 if (isym->st_size != 0
5349 && isym->st_shndx != SHN_UNDEF
5350 && (definition || h->size == 0))
5352 if (h->size != 0
5353 && h->size != isym->st_size
5354 && ! size_change_ok)
5356 _bfd_error_handler
5357 /* xgettext:c-format */
5358 (_("warning: size of symbol `%s' changed"
5359 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
5360 name, (uint64_t) h->size, old_bfd,
5361 (uint64_t) isym->st_size, abfd);
5363 /* PR 30499: make sure that users understand that this warning is serious. */
5364 _bfd_error_handler
5365 (_("warning: NOTE: size discrepancies can cause real problems. Investigation is advised."));
5368 h->size = isym->st_size;
5371 /* If this is a common symbol, then we always want H->SIZE
5372 to be the size of the common symbol. The code just above
5373 won't fix the size if a common symbol becomes larger. We
5374 don't warn about a size change here, because that is
5375 covered by --warn-common. Allow changes between different
5376 function types. */
5377 if (h->root.type == bfd_link_hash_common)
5378 h->size = h->root.u.c.size;
5380 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
5381 && ((definition && !new_weak)
5382 || (old_weak && h->root.type == bfd_link_hash_common)
5383 || h->type == STT_NOTYPE))
5385 unsigned int type = ELF_ST_TYPE (isym->st_info);
5387 /* Turn an IFUNC symbol from a DSO into a normal FUNC
5388 symbol. */
5389 if (type == STT_GNU_IFUNC
5390 && (abfd->flags & DYNAMIC) != 0)
5391 type = STT_FUNC;
5393 if (h->type != type)
5395 if (h->type != STT_NOTYPE && ! type_change_ok)
5396 /* xgettext:c-format */
5397 _bfd_error_handler
5398 (_("warning: type of symbol `%s' changed"
5399 " from %d to %d in %pB"),
5400 name, h->type, type, abfd);
5402 h->type = type;
5406 /* Merge st_other field. */
5407 elf_merge_st_other (abfd, h, isym->st_other, sec,
5408 definition, dynamic);
5410 /* We don't want to make debug symbol dynamic. */
5411 if (definition
5412 && (sec->flags & SEC_DEBUGGING)
5413 && !bfd_link_relocatable (info))
5414 dynsym = false;
5416 /* Nor should we make plugin symbols dynamic. */
5417 if ((abfd->flags & BFD_PLUGIN) != 0)
5418 dynsym = false;
5420 if (definition)
5422 h->target_internal = isym->st_target_internal;
5423 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5426 /* Don't add indirect symbols for .symver x, x@FOO aliases
5427 in IR. Since all data or text symbols in IR have the
5428 same type, value and section, we can't tell if a symbol
5429 is an alias of another symbol by their types, values and
5430 sections. */
5431 if (definition
5432 && !dynamic
5433 && (abfd->flags & BFD_PLUGIN) == 0)
5435 char *p = strchr (name, ELF_VER_CHR);
5436 if (p != NULL && p[1] != ELF_VER_CHR)
5438 /* Queue non-default versions so that .symver x, x@FOO
5439 aliases can be checked. */
5440 if (!nondeflt_vers)
5442 size_t amt = ((isymend - isym + 1)
5443 * sizeof (struct elf_link_hash_entry *));
5444 nondeflt_vers
5445 = (struct elf_link_hash_entry **) bfd_malloc (amt);
5446 if (!nondeflt_vers)
5447 goto error_free_vers;
5449 nondeflt_vers[nondeflt_vers_cnt++] = h;
5453 if (dynsym && h->dynindx == -1)
5455 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5456 goto error_free_vers;
5457 if (h->is_weakalias
5458 && weakdef (h)->dynindx == -1)
5460 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
5461 goto error_free_vers;
5464 else if (h->dynindx != -1)
5465 /* If the symbol already has a dynamic index, but
5466 visibility says it should not be visible, turn it into
5467 a local symbol. */
5468 switch (ELF_ST_VISIBILITY (h->other))
5470 case STV_INTERNAL:
5471 case STV_HIDDEN:
5472 (*bed->elf_backend_hide_symbol) (info, h, true);
5473 dynsym = false;
5474 break;
5477 if (!add_needed
5478 && matched
5479 && definition
5480 && h->root.type != bfd_link_hash_indirect
5481 && ((dynsym
5482 && h->ref_regular_nonweak)
5483 || (old_bfd != NULL
5484 && (old_bfd->flags & BFD_PLUGIN) != 0
5485 && h->ref_ir_nonweak
5486 && !info->lto_all_symbols_read)
5487 || (h->ref_dynamic_nonweak
5488 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5489 && !on_needed_list (elf_dt_name (abfd),
5490 htab->needed, NULL))))
5492 const char *soname = elf_dt_name (abfd);
5494 info->callbacks->minfo ("%!", soname, old_bfd,
5495 h->root.root.string);
5497 /* A symbol from a library loaded via DT_NEEDED of some
5498 other library is referenced by a regular object.
5499 Add a DT_NEEDED entry for it. Issue an error if
5500 --no-add-needed is used and the reference was not
5501 a weak one. */
5502 if (old_bfd != NULL
5503 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
5505 _bfd_error_handler
5506 /* xgettext:c-format */
5507 (_("%pB: undefined reference to symbol '%s'"),
5508 old_bfd, name);
5509 bfd_set_error (bfd_error_missing_dso);
5510 goto error_free_vers;
5513 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
5514 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
5516 /* Create dynamic sections for backends that require
5517 that be done before setup_gnu_properties. */
5518 if (!_bfd_elf_link_create_dynamic_sections (abfd, info))
5519 return false;
5520 add_needed = true;
5525 if (info->lto_plugin_active
5526 && !bfd_link_relocatable (info)
5527 && (abfd->flags & BFD_PLUGIN) == 0
5528 && !just_syms
5529 && extsymcount)
5531 int r_sym_shift;
5533 if (bed->s->arch_size == 32)
5534 r_sym_shift = 8;
5535 else
5536 r_sym_shift = 32;
5538 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5539 referenced in regular objects so that linker plugin will get
5540 the correct symbol resolution. */
5542 sym_hash = elf_sym_hashes (abfd);
5543 for (s = abfd->sections; s != NULL; s = s->next)
5545 Elf_Internal_Rela *internal_relocs;
5546 Elf_Internal_Rela *rel, *relend;
5548 /* Don't check relocations in excluded sections. */
5549 if ((s->flags & SEC_RELOC) == 0
5550 || s->reloc_count == 0
5551 || (s->flags & SEC_EXCLUDE) != 0
5552 || ((info->strip == strip_all
5553 || info->strip == strip_debugger)
5554 && (s->flags & SEC_DEBUGGING) != 0))
5555 continue;
5557 internal_relocs = _bfd_elf_link_info_read_relocs (abfd, info,
5558 s, NULL,
5559 NULL,
5560 _bfd_link_keep_memory (info));
5561 if (internal_relocs == NULL)
5562 goto error_free_vers;
5564 rel = internal_relocs;
5565 relend = rel + s->reloc_count;
5566 for ( ; rel < relend; rel++)
5568 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5569 struct elf_link_hash_entry *h;
5571 /* Skip local symbols. */
5572 if (r_symndx < extsymoff)
5573 continue;
5575 h = sym_hash[r_symndx - extsymoff];
5576 if (h != NULL)
5577 h->root.non_ir_ref_regular = 1;
5580 if (elf_section_data (s)->relocs != internal_relocs)
5581 free (internal_relocs);
5585 free (extversym);
5586 extversym = NULL;
5587 free (isymbuf);
5588 isymbuf = NULL;
5590 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5592 unsigned int i;
5594 /* Restore the symbol table. */
5595 old_ent = (char *) old_tab + tabsize;
5596 memset (elf_sym_hashes (abfd), 0,
5597 extsymcount * sizeof (struct elf_link_hash_entry *));
5598 htab->root.table.table = old_table;
5599 htab->root.table.size = old_size;
5600 htab->root.table.count = old_count;
5601 memcpy (htab->root.table.table, old_tab, tabsize);
5602 htab->root.undefs = old_undefs;
5603 htab->root.undefs_tail = old_undefs_tail;
5604 if (htab->dynstr != NULL)
5605 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5606 free (old_strtab);
5607 old_strtab = NULL;
5608 for (i = 0; i < htab->root.table.size; i++)
5610 struct bfd_hash_entry *p;
5611 struct elf_link_hash_entry *h;
5612 unsigned int non_ir_ref_dynamic;
5614 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5616 /* Preserve non_ir_ref_dynamic so that this symbol
5617 will be exported when the dynamic lib becomes needed
5618 in the second pass. */
5619 h = (struct elf_link_hash_entry *) p;
5620 if (h->root.type == bfd_link_hash_warning)
5621 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5622 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
5624 h = (struct elf_link_hash_entry *) p;
5625 memcpy (h, old_ent, htab->root.table.entsize);
5626 old_ent = (char *) old_ent + htab->root.table.entsize;
5627 if (h->root.type == bfd_link_hash_warning)
5629 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5630 memcpy (h, old_ent, htab->root.table.entsize);
5631 old_ent = (char *) old_ent + htab->root.table.entsize;
5633 if (h->root.type == bfd_link_hash_common)
5635 memcpy (h->root.u.c.p, old_ent, sizeof (*h->root.u.c.p));
5636 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
5638 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
5642 /* Make a special call to the linker "notice" function to
5643 tell it that symbols added for crefs may need to be removed. */
5644 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
5645 goto error_free_vers;
5647 free (old_tab);
5648 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5649 alloc_mark);
5650 free (nondeflt_vers);
5651 return true;
5654 if (old_tab != NULL)
5656 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
5657 goto error_free_vers;
5658 free (old_tab);
5659 old_tab = NULL;
5662 /* Now that all the symbols from this input file are created, if
5663 not performing a relocatable link, handle .symver foo, foo@BAR
5664 such that any relocs against foo become foo@BAR. */
5665 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
5667 size_t cnt, symidx;
5669 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5671 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5672 char *shortname, *p;
5673 size_t amt;
5675 p = strchr (h->root.root.string, ELF_VER_CHR);
5676 if (p == NULL
5677 || (h->root.type != bfd_link_hash_defined
5678 && h->root.type != bfd_link_hash_defweak))
5679 continue;
5681 amt = p - h->root.root.string;
5682 shortname = (char *) bfd_malloc (amt + 1);
5683 if (!shortname)
5684 goto error_free_vers;
5685 memcpy (shortname, h->root.root.string, amt);
5686 shortname[amt] = '\0';
5688 hi = (struct elf_link_hash_entry *)
5689 bfd_link_hash_lookup (&htab->root, shortname,
5690 false, false, false);
5691 if (hi != NULL
5692 && hi->root.type == h->root.type
5693 && hi->root.u.def.value == h->root.u.def.value
5694 && hi->root.u.def.section == h->root.u.def.section)
5696 (*bed->elf_backend_hide_symbol) (info, hi, true);
5697 hi->root.type = bfd_link_hash_indirect;
5698 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
5699 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
5700 sym_hash = elf_sym_hashes (abfd);
5701 if (sym_hash)
5702 for (symidx = 0; symidx < extsymcount; ++symidx)
5703 if (sym_hash[symidx] == hi)
5705 sym_hash[symidx] = h;
5706 break;
5709 free (shortname);
5711 free (nondeflt_vers);
5712 nondeflt_vers = NULL;
5715 /* Now set the alias field correctly for all the weak defined
5716 symbols we found. The only way to do this is to search all the
5717 symbols. Since we only need the information for non functions in
5718 dynamic objects, that's the only time we actually put anything on
5719 the list WEAKS. We need this information so that if a regular
5720 object refers to a symbol defined weakly in a dynamic object, the
5721 real symbol in the dynamic object is also put in the dynamic
5722 symbols; we also must arrange for both symbols to point to the
5723 same memory location. We could handle the general case of symbol
5724 aliasing, but a general symbol alias can only be generated in
5725 assembler code, handling it correctly would be very time
5726 consuming, and other ELF linkers don't handle general aliasing
5727 either. */
5728 if (weaks != NULL)
5730 struct elf_link_hash_entry **hpp;
5731 struct elf_link_hash_entry **hppend;
5732 struct elf_link_hash_entry **sorted_sym_hash;
5733 struct elf_link_hash_entry *h;
5734 size_t sym_count, amt;
5736 /* Since we have to search the whole symbol list for each weak
5737 defined symbol, search time for N weak defined symbols will be
5738 O(N^2). Binary search will cut it down to O(NlogN). */
5739 amt = extsymcount * sizeof (*sorted_sym_hash);
5740 sorted_sym_hash = bfd_malloc (amt);
5741 if (sorted_sym_hash == NULL)
5742 goto error_return;
5743 sym_hash = sorted_sym_hash;
5744 hpp = elf_sym_hashes (abfd);
5745 hppend = hpp + extsymcount;
5746 sym_count = 0;
5747 for (; hpp < hppend; hpp++)
5749 h = *hpp;
5750 if (h != NULL
5751 && h->root.type == bfd_link_hash_defined
5752 && !bed->is_function_type (h->type))
5754 *sym_hash = h;
5755 sym_hash++;
5756 sym_count++;
5760 qsort (sorted_sym_hash, sym_count, sizeof (*sorted_sym_hash),
5761 elf_sort_symbol);
5763 while (weaks != NULL)
5765 struct elf_link_hash_entry *hlook;
5766 asection *slook;
5767 bfd_vma vlook;
5768 size_t i, j, idx = 0;
5770 hlook = weaks;
5771 weaks = hlook->u.alias;
5772 hlook->u.alias = NULL;
5774 if (hlook->root.type != bfd_link_hash_defined
5775 && hlook->root.type != bfd_link_hash_defweak)
5776 continue;
5778 slook = hlook->root.u.def.section;
5779 vlook = hlook->root.u.def.value;
5781 i = 0;
5782 j = sym_count;
5783 while (i != j)
5785 bfd_signed_vma vdiff;
5786 idx = (i + j) / 2;
5787 h = sorted_sym_hash[idx];
5788 vdiff = vlook - h->root.u.def.value;
5789 if (vdiff < 0)
5790 j = idx;
5791 else if (vdiff > 0)
5792 i = idx + 1;
5793 else
5795 int sdiff = slook->id - h->root.u.def.section->id;
5796 if (sdiff < 0)
5797 j = idx;
5798 else if (sdiff > 0)
5799 i = idx + 1;
5800 else
5801 break;
5805 /* We didn't find a value/section match. */
5806 if (i == j)
5807 continue;
5809 /* With multiple aliases, or when the weak symbol is already
5810 strongly defined, we have multiple matching symbols and
5811 the binary search above may land on any of them. Step
5812 one past the matching symbol(s). */
5813 while (++idx != j)
5815 h = sorted_sym_hash[idx];
5816 if (h->root.u.def.section != slook
5817 || h->root.u.def.value != vlook)
5818 break;
5821 /* Now look back over the aliases. Since we sorted by size
5822 as well as value and section, we'll choose the one with
5823 the largest size. */
5824 while (idx-- != i)
5826 h = sorted_sym_hash[idx];
5828 /* Stop if value or section doesn't match. */
5829 if (h->root.u.def.section != slook
5830 || h->root.u.def.value != vlook)
5831 break;
5832 else if (h != hlook)
5834 struct elf_link_hash_entry *t;
5836 hlook->u.alias = h;
5837 hlook->is_weakalias = 1;
5838 t = h;
5839 if (t->u.alias != NULL)
5840 while (t->u.alias != h)
5841 t = t->u.alias;
5842 t->u.alias = hlook;
5844 /* If the weak definition is in the list of dynamic
5845 symbols, make sure the real definition is put
5846 there as well. */
5847 if (hlook->dynindx != -1 && h->dynindx == -1)
5849 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5851 err_free_sym_hash:
5852 free (sorted_sym_hash);
5853 goto error_return;
5857 /* If the real definition is in the list of dynamic
5858 symbols, make sure the weak definition is put
5859 there as well. If we don't do this, then the
5860 dynamic loader might not merge the entries for the
5861 real definition and the weak definition. */
5862 if (h->dynindx != -1 && hlook->dynindx == -1)
5864 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
5865 goto err_free_sym_hash;
5867 break;
5872 free (sorted_sym_hash);
5875 if (bed->check_directives
5876 && !(*bed->check_directives) (abfd, info))
5877 return false;
5879 /* If this is a non-traditional link, try to optimize the handling
5880 of the .stab/.stabstr sections. */
5881 if (! dynamic
5882 && ! info->traditional_format
5883 && is_elf_hash_table (&htab->root)
5884 && (info->strip != strip_all && info->strip != strip_debugger))
5886 asection *stabstr;
5888 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5889 if (stabstr != NULL)
5891 bfd_size_type string_offset = 0;
5892 asection *stab;
5894 for (stab = abfd->sections; stab; stab = stab->next)
5895 if (startswith (stab->name, ".stab")
5896 && (!stab->name[5] ||
5897 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5898 && (stab->flags & SEC_MERGE) == 0
5899 && !bfd_is_abs_section (stab->output_section))
5901 struct bfd_elf_section_data *secdata;
5903 secdata = elf_section_data (stab);
5904 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5905 stabstr, &secdata->sec_info,
5906 &string_offset))
5907 goto error_return;
5908 if (secdata->sec_info)
5909 stab->sec_info_type = SEC_INFO_TYPE_STABS;
5914 if (dynamic && add_needed)
5916 /* Add this bfd to the loaded list. */
5917 struct elf_link_loaded_list *n;
5919 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5920 if (n == NULL)
5921 goto error_return;
5922 n->abfd = abfd;
5923 n->next = htab->dyn_loaded;
5924 htab->dyn_loaded = n;
5926 if (dynamic && !add_needed
5927 && (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) != 0)
5928 elf_dyn_lib_class (abfd) |= DYN_NO_NEEDED;
5930 return true;
5932 error_free_vers:
5933 free (old_tab);
5934 free (old_strtab);
5935 free (nondeflt_vers);
5936 free (extversym);
5937 error_free_sym:
5938 free (isymbuf);
5939 error_return:
5940 return false;
5943 /* Return the linker hash table entry of a symbol that might be
5944 satisfied by an archive symbol. Return -1 on error. */
5946 struct bfd_link_hash_entry *
5947 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5948 struct bfd_link_info *info,
5949 const char *name)
5951 struct bfd_link_hash_entry *h;
5952 char *p, *copy;
5953 size_t len, first;
5955 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
5956 if (h != NULL)
5957 return h;
5959 /* If this is a default version (the name contains @@), look up the
5960 symbol again with only one `@' as well as without the version.
5961 The effect is that references to the symbol with and without the
5962 version will be matched by the default symbol in the archive. */
5964 p = strchr (name, ELF_VER_CHR);
5965 if (p == NULL || p[1] != ELF_VER_CHR)
5966 return h;
5968 /* First check with only one `@'. */
5969 len = strlen (name);
5970 copy = (char *) bfd_alloc (abfd, len);
5971 if (copy == NULL)
5972 return (struct bfd_link_hash_entry *) -1;
5974 first = p - name + 1;
5975 memcpy (copy, name, first);
5976 memcpy (copy + first, name + first + 1, len - first);
5978 h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
5979 if (h == NULL)
5981 /* We also need to check references to the symbol without the
5982 version. */
5983 copy[first - 1] = '\0';
5984 h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
5987 bfd_release (abfd, copy);
5988 return h;
5991 /* Add symbols from an ELF archive file to the linker hash table. We
5992 don't use _bfd_generic_link_add_archive_symbols because we need to
5993 handle versioned symbols.
5995 Fortunately, ELF archive handling is simpler than that done by
5996 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5997 oddities. In ELF, if we find a symbol in the archive map, and the
5998 symbol is currently undefined, we know that we must pull in that
5999 object file.
6001 Unfortunately, we do have to make multiple passes over the symbol
6002 table until nothing further is resolved. */
6004 static bool
6005 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
6007 symindex c;
6008 unsigned char *included = NULL;
6009 carsym *symdefs;
6010 bool loop;
6011 size_t amt;
6012 const struct elf_backend_data *bed;
6013 struct bfd_link_hash_entry * (*archive_symbol_lookup)
6014 (bfd *, struct bfd_link_info *, const char *);
6016 if (! bfd_has_map (abfd))
6018 /* An empty archive is a special case. */
6019 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
6020 return true;
6021 bfd_set_error (bfd_error_no_armap);
6022 return false;
6025 /* Keep track of all symbols we know to be already defined, and all
6026 files we know to be already included. This is to speed up the
6027 second and subsequent passes. */
6028 c = bfd_ardata (abfd)->symdef_count;
6029 if (c == 0)
6030 return true;
6031 amt = c * sizeof (*included);
6032 included = (unsigned char *) bfd_zmalloc (amt);
6033 if (included == NULL)
6034 return false;
6036 symdefs = bfd_ardata (abfd)->symdefs;
6037 bed = get_elf_backend_data (abfd);
6038 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
6042 file_ptr last;
6043 symindex i;
6044 carsym *symdef;
6045 carsym *symdefend;
6047 loop = false;
6048 last = -1;
6050 symdef = symdefs;
6051 symdefend = symdef + c;
6052 for (i = 0; symdef < symdefend; symdef++, i++)
6054 struct bfd_link_hash_entry *h;
6055 bfd *element;
6056 struct bfd_link_hash_entry *undefs_tail;
6057 symindex mark;
6059 if (included[i])
6060 continue;
6061 if (symdef->file_offset == last)
6063 included[i] = true;
6064 continue;
6067 h = archive_symbol_lookup (abfd, info, symdef->name);
6068 if (h == (struct bfd_link_hash_entry *) -1)
6069 goto error_return;
6071 if (h == NULL)
6072 continue;
6074 if (h->type == bfd_link_hash_undefined)
6076 /* If the archive element has already been loaded then one
6077 of the symbols defined by that element might have been
6078 made undefined due to being in a discarded section. */
6079 if (is_elf_hash_table (info->hash)
6080 && ((struct elf_link_hash_entry *) h)->indx == -3)
6081 continue;
6083 else if (h->type == bfd_link_hash_common)
6085 /* We currently have a common symbol. The archive map contains
6086 a reference to this symbol, so we may want to include it. We
6087 only want to include it however, if this archive element
6088 contains a definition of the symbol, not just another common
6089 declaration of it.
6091 Unfortunately some archivers (including GNU ar) will put
6092 declarations of common symbols into their archive maps, as
6093 well as real definitions, so we cannot just go by the archive
6094 map alone. Instead we must read in the element's symbol
6095 table and check that to see what kind of symbol definition
6096 this is. */
6097 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
6098 continue;
6100 else
6102 if (h->type != bfd_link_hash_undefweak)
6103 /* Symbol must be defined. Don't check it again. */
6104 included[i] = true;
6105 continue;
6108 /* We need to include this archive member. */
6109 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset,
6110 info);
6111 if (element == NULL)
6112 goto error_return;
6114 if (! bfd_check_format (element, bfd_object))
6115 goto error_return;
6117 undefs_tail = info->hash->undefs_tail;
6119 if (!(*info->callbacks
6120 ->add_archive_element) (info, element, symdef->name, &element))
6121 continue;
6122 if (!bfd_link_add_symbols (element, info))
6123 goto error_return;
6125 /* If there are any new undefined symbols, we need to make
6126 another pass through the archive in order to see whether
6127 they can be defined. FIXME: This isn't perfect, because
6128 common symbols wind up on undefs_tail and because an
6129 undefined symbol which is defined later on in this pass
6130 does not require another pass. This isn't a bug, but it
6131 does make the code less efficient than it could be. */
6132 if (undefs_tail != info->hash->undefs_tail)
6133 loop = true;
6135 /* Look backward to mark all symbols from this object file
6136 which we have already seen in this pass. */
6137 mark = i;
6140 included[mark] = true;
6141 if (mark == 0)
6142 break;
6143 --mark;
6145 while (symdefs[mark].file_offset == symdef->file_offset);
6147 /* We mark subsequent symbols from this object file as we go
6148 on through the loop. */
6149 last = symdef->file_offset;
6152 while (loop);
6154 free (included);
6155 return true;
6157 error_return:
6158 free (included);
6159 return false;
6162 /* Given an ELF BFD, add symbols to the global hash table as
6163 appropriate. */
6165 bool
6166 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
6168 switch (bfd_get_format (abfd))
6170 case bfd_object:
6171 return elf_link_add_object_symbols (abfd, info);
6172 case bfd_archive:
6173 return elf_link_add_archive_symbols (abfd, info);
6174 default:
6175 bfd_set_error (bfd_error_wrong_format);
6176 return false;
6180 struct hash_codes_info
6182 unsigned long *hashcodes;
6183 bool error;
6186 /* This function will be called though elf_link_hash_traverse to store
6187 all hash value of the exported symbols in an array. */
6189 static bool
6190 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
6192 struct hash_codes_info *inf = (struct hash_codes_info *) data;
6193 const char *name;
6194 unsigned long ha;
6195 char *alc = NULL;
6197 /* Ignore indirect symbols. These are added by the versioning code. */
6198 if (h->dynindx == -1)
6199 return true;
6201 name = h->root.root.string;
6202 if (h->versioned >= versioned)
6204 char *p = strchr (name, ELF_VER_CHR);
6205 if (p != NULL)
6207 alc = (char *) bfd_malloc (p - name + 1);
6208 if (alc == NULL)
6210 inf->error = true;
6211 return false;
6213 memcpy (alc, name, p - name);
6214 alc[p - name] = '\0';
6215 name = alc;
6219 /* Compute the hash value. */
6220 ha = bfd_elf_hash (name);
6222 /* Store the found hash value in the array given as the argument. */
6223 *(inf->hashcodes)++ = ha;
6225 /* And store it in the struct so that we can put it in the hash table
6226 later. */
6227 h->u.elf_hash_value = ha;
6229 free (alc);
6230 return true;
6233 struct collect_gnu_hash_codes
6235 bfd *output_bfd;
6236 const struct elf_backend_data *bed;
6237 unsigned long int nsyms;
6238 unsigned long int maskbits;
6239 unsigned long int *hashcodes;
6240 unsigned long int *hashval;
6241 unsigned long int *indx;
6242 unsigned long int *counts;
6243 bfd_vma *bitmask;
6244 bfd_byte *contents;
6245 bfd_size_type xlat;
6246 long int min_dynindx;
6247 unsigned long int bucketcount;
6248 unsigned long int symindx;
6249 long int local_indx;
6250 long int shift1, shift2;
6251 unsigned long int mask;
6252 bool error;
6255 /* This function will be called though elf_link_hash_traverse to store
6256 all hash value of the exported symbols in an array. */
6258 static bool
6259 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
6261 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6262 const char *name;
6263 unsigned long ha;
6264 char *alc = NULL;
6266 /* Ignore indirect symbols. These are added by the versioning code. */
6267 if (h->dynindx == -1)
6268 return true;
6270 /* Ignore also local symbols and undefined symbols. */
6271 if (! (*s->bed->elf_hash_symbol) (h))
6272 return true;
6274 name = h->root.root.string;
6275 if (h->versioned >= versioned)
6277 char *p = strchr (name, ELF_VER_CHR);
6278 if (p != NULL)
6280 alc = (char *) bfd_malloc (p - name + 1);
6281 if (alc == NULL)
6283 s->error = true;
6284 return false;
6286 memcpy (alc, name, p - name);
6287 alc[p - name] = '\0';
6288 name = alc;
6292 /* Compute the hash value. */
6293 ha = bfd_elf_gnu_hash (name);
6295 /* Store the found hash value in the array for compute_bucket_count,
6296 and also for .dynsym reordering purposes. */
6297 s->hashcodes[s->nsyms] = ha;
6298 s->hashval[h->dynindx] = ha;
6299 ++s->nsyms;
6300 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
6301 s->min_dynindx = h->dynindx;
6303 free (alc);
6304 return true;
6307 /* This function will be called though elf_link_hash_traverse to do
6308 final dynamic symbol renumbering in case of .gnu.hash.
6309 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6310 to the translation table. */
6312 static bool
6313 elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data)
6315 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6316 unsigned long int bucket;
6317 unsigned long int val;
6319 /* Ignore indirect symbols. */
6320 if (h->dynindx == -1)
6321 return true;
6323 /* Ignore also local symbols and undefined symbols. */
6324 if (! (*s->bed->elf_hash_symbol) (h))
6326 if (h->dynindx >= s->min_dynindx)
6328 if (s->bed->record_xhash_symbol != NULL)
6330 (*s->bed->record_xhash_symbol) (h, 0);
6331 s->local_indx++;
6333 else
6334 h->dynindx = s->local_indx++;
6336 return true;
6339 bucket = s->hashval[h->dynindx] % s->bucketcount;
6340 val = (s->hashval[h->dynindx] >> s->shift1)
6341 & ((s->maskbits >> s->shift1) - 1);
6342 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
6343 s->bitmask[val]
6344 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
6345 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
6346 if (s->counts[bucket] == 1)
6347 /* Last element terminates the chain. */
6348 val |= 1;
6349 bfd_put_32 (s->output_bfd, val,
6350 s->contents + (s->indx[bucket] - s->symindx) * 4);
6351 --s->counts[bucket];
6352 if (s->bed->record_xhash_symbol != NULL)
6354 bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4;
6356 (*s->bed->record_xhash_symbol) (h, xlat_loc);
6358 else
6359 h->dynindx = s->indx[bucket]++;
6360 return true;
6363 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6365 bool
6366 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
6368 return !(h->forced_local
6369 || h->root.type == bfd_link_hash_undefined
6370 || h->root.type == bfd_link_hash_undefweak
6371 || ((h->root.type == bfd_link_hash_defined
6372 || h->root.type == bfd_link_hash_defweak)
6373 && h->root.u.def.section->output_section == NULL));
6376 /* Array used to determine the number of hash table buckets to use
6377 based on the number of symbols there are. If there are fewer than
6378 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6379 fewer than 37 we use 17 buckets, and so forth. We never use more
6380 than 32771 buckets. */
6382 static const size_t elf_buckets[] =
6384 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6385 16411, 32771, 0
6388 /* Compute bucket count for hashing table. We do not use a static set
6389 of possible tables sizes anymore. Instead we determine for all
6390 possible reasonable sizes of the table the outcome (i.e., the
6391 number of collisions etc) and choose the best solution. The
6392 weighting functions are not too simple to allow the table to grow
6393 without bounds. Instead one of the weighting factors is the size.
6394 Therefore the result is always a good payoff between few collisions
6395 (= short chain lengths) and table size. */
6396 static size_t
6397 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
6398 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
6399 unsigned long int nsyms,
6400 int gnu_hash)
6402 size_t best_size = 0;
6403 unsigned long int i;
6405 if (info->optimize)
6407 size_t minsize;
6408 size_t maxsize;
6409 uint64_t best_chlen = ~((uint64_t) 0);
6410 bfd *dynobj = elf_hash_table (info)->dynobj;
6411 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
6412 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
6413 unsigned long int *counts;
6414 bfd_size_type amt;
6415 unsigned int no_improvement_count = 0;
6417 /* Possible optimization parameters: if we have NSYMS symbols we say
6418 that the hashing table must at least have NSYMS/4 and at most
6419 2*NSYMS buckets. */
6420 minsize = nsyms / 4;
6421 if (minsize == 0)
6422 minsize = 1;
6423 best_size = maxsize = nsyms * 2;
6424 if (gnu_hash)
6426 if (minsize < 2)
6427 minsize = 2;
6428 if ((best_size & 31) == 0)
6429 ++best_size;
6432 /* Create array where we count the collisions in. We must use bfd_malloc
6433 since the size could be large. */
6434 amt = maxsize;
6435 amt *= sizeof (unsigned long int);
6436 counts = (unsigned long int *) bfd_malloc (amt);
6437 if (counts == NULL)
6438 return 0;
6440 /* Compute the "optimal" size for the hash table. The criteria is a
6441 minimal chain length. The minor criteria is (of course) the size
6442 of the table. */
6443 for (i = minsize; i < maxsize; ++i)
6445 /* Walk through the array of hashcodes and count the collisions. */
6446 uint64_t max;
6447 unsigned long int j;
6448 unsigned long int fact;
6450 if (gnu_hash && (i & 31) == 0)
6451 continue;
6453 memset (counts, '\0', i * sizeof (unsigned long int));
6455 /* Determine how often each hash bucket is used. */
6456 for (j = 0; j < nsyms; ++j)
6457 ++counts[hashcodes[j] % i];
6459 /* For the weight function we need some information about the
6460 pagesize on the target. This is information need not be 100%
6461 accurate. Since this information is not available (so far) we
6462 define it here to a reasonable default value. If it is crucial
6463 to have a better value some day simply define this value. */
6464 # ifndef BFD_TARGET_PAGESIZE
6465 # define BFD_TARGET_PAGESIZE (4096)
6466 # endif
6468 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6469 and the chains. */
6470 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
6472 # if 1
6473 /* Variant 1: optimize for short chains. We add the squares
6474 of all the chain lengths (which favors many small chain
6475 over a few long chains). */
6476 for (j = 0; j < i; ++j)
6477 max += counts[j] * counts[j];
6479 /* This adds penalties for the overall size of the table. */
6480 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6481 max *= fact * fact;
6482 # else
6483 /* Variant 2: Optimize a lot more for small table. Here we
6484 also add squares of the size but we also add penalties for
6485 empty slots (the +1 term). */
6486 for (j = 0; j < i; ++j)
6487 max += (1 + counts[j]) * (1 + counts[j]);
6489 /* The overall size of the table is considered, but not as
6490 strong as in variant 1, where it is squared. */
6491 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6492 max *= fact;
6493 # endif
6495 /* Compare with current best results. */
6496 if (max < best_chlen)
6498 best_chlen = max;
6499 best_size = i;
6500 no_improvement_count = 0;
6502 /* PR 11843: Avoid futile long searches for the best bucket size
6503 when there are a large number of symbols. */
6504 else if (++no_improvement_count == 100)
6505 break;
6508 free (counts);
6510 else
6512 for (i = 0; elf_buckets[i] != 0; i++)
6514 best_size = elf_buckets[i];
6515 if (nsyms < elf_buckets[i + 1])
6516 break;
6518 if (gnu_hash && best_size < 2)
6519 best_size = 2;
6522 return best_size;
6525 /* Size any SHT_GROUP section for ld -r. */
6527 bool
6528 _bfd_elf_size_group_sections (struct bfd_link_info *info)
6530 bfd *ibfd;
6531 asection *s;
6533 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6534 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6535 && (s = ibfd->sections) != NULL
6536 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
6537 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6538 return false;
6539 return true;
6542 /* Set a default stack segment size. The value in INFO wins. If it
6543 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6544 undefined it is initialized. */
6546 bool
6547 bfd_elf_stack_segment_size (bfd *output_bfd,
6548 struct bfd_link_info *info,
6549 const char *legacy_symbol,
6550 bfd_vma default_size)
6552 struct elf_link_hash_entry *h = NULL;
6554 /* Look for legacy symbol. */
6555 if (legacy_symbol)
6556 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6557 false, false, false);
6558 if (h && (h->root.type == bfd_link_hash_defined
6559 || h->root.type == bfd_link_hash_defweak)
6560 && h->def_regular
6561 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6563 /* The symbol has no type if specified on the command line. */
6564 h->type = STT_OBJECT;
6565 if (info->stacksize)
6566 /* xgettext:c-format */
6567 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6568 output_bfd, legacy_symbol);
6569 else if (h->root.u.def.section != bfd_abs_section_ptr)
6570 /* xgettext:c-format */
6571 _bfd_error_handler (_("%pB: %s not absolute"),
6572 output_bfd, legacy_symbol);
6573 else
6574 info->stacksize = h->root.u.def.value;
6577 if (!info->stacksize)
6578 /* If the user didn't set a size, or explicitly inhibit the
6579 size, set it now. */
6580 info->stacksize = default_size;
6582 /* Provide the legacy symbol, if it is referenced. */
6583 if (h && (h->root.type == bfd_link_hash_undefined
6584 || h->root.type == bfd_link_hash_undefweak))
6586 struct bfd_link_hash_entry *bh = NULL;
6588 if (!(_bfd_generic_link_add_one_symbol
6589 (info, output_bfd, legacy_symbol,
6590 BSF_GLOBAL, bfd_abs_section_ptr,
6591 info->stacksize >= 0 ? info->stacksize : 0,
6592 NULL, false, get_elf_backend_data (output_bfd)->collect, &bh)))
6593 return false;
6595 h = (struct elf_link_hash_entry *) bh;
6596 h->def_regular = 1;
6597 h->type = STT_OBJECT;
6600 return true;
6603 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6605 struct elf_gc_sweep_symbol_info
6607 struct bfd_link_info *info;
6608 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6609 bool);
6612 static bool
6613 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6615 if (!h->mark
6616 && (((h->root.type == bfd_link_hash_defined
6617 || h->root.type == bfd_link_hash_defweak)
6618 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6619 && h->root.u.def.section->gc_mark))
6620 || h->root.type == bfd_link_hash_undefined
6621 || h->root.type == bfd_link_hash_undefweak))
6623 struct elf_gc_sweep_symbol_info *inf;
6625 inf = (struct elf_gc_sweep_symbol_info *) data;
6626 (*inf->hide_symbol) (inf->info, h, true);
6627 h->def_regular = 0;
6628 h->ref_regular = 0;
6629 h->ref_regular_nonweak = 0;
6632 return true;
6635 /* Set up the sizes and contents of the ELF dynamic sections. This is
6636 called by the ELF linker emulation before_allocation routine. We
6637 must set the sizes of the sections before the linker sets the
6638 addresses of the various sections. */
6640 bool
6641 bfd_elf_size_dynamic_sections (bfd *output_bfd,
6642 const char *soname,
6643 const char *rpath,
6644 const char *filter_shlib,
6645 const char *audit,
6646 const char *depaudit,
6647 const char * const *auxiliary_filters,
6648 struct bfd_link_info *info,
6649 asection **sinterpptr)
6651 bfd *dynobj;
6652 const struct elf_backend_data *bed;
6654 *sinterpptr = NULL;
6656 if (!is_elf_hash_table (info->hash))
6657 return true;
6659 /* Any syms created from now on start with -1 in
6660 got.refcount/offset and plt.refcount/offset. */
6661 elf_hash_table (info)->init_got_refcount
6662 = elf_hash_table (info)->init_got_offset;
6663 elf_hash_table (info)->init_plt_refcount
6664 = elf_hash_table (info)->init_plt_offset;
6666 bed = get_elf_backend_data (output_bfd);
6668 /* The backend may have to create some sections regardless of whether
6669 we're dynamic or not. */
6670 if (bed->elf_backend_always_size_sections
6671 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6672 return false;
6674 dynobj = elf_hash_table (info)->dynobj;
6676 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6678 struct bfd_elf_version_tree *verdefs;
6679 struct elf_info_failed asvinfo;
6680 struct bfd_elf_version_tree *t;
6681 struct bfd_elf_version_expr *d;
6682 asection *s;
6683 size_t soname_indx;
6685 /* If we are supposed to export all symbols into the dynamic symbol
6686 table (this is not the normal case), then do so. */
6687 if (info->export_dynamic
6688 || (bfd_link_executable (info) && info->dynamic))
6690 struct elf_info_failed eif;
6692 eif.info = info;
6693 eif.failed = false;
6694 elf_link_hash_traverse (elf_hash_table (info),
6695 _bfd_elf_export_symbol,
6696 &eif);
6697 if (eif.failed)
6698 return false;
6701 if (soname != NULL)
6703 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6704 soname, true);
6705 if (soname_indx == (size_t) -1
6706 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6707 return false;
6709 else
6710 soname_indx = (size_t) -1;
6712 /* Make all global versions with definition. */
6713 for (t = info->version_info; t != NULL; t = t->next)
6714 for (d = t->globals.list; d != NULL; d = d->next)
6715 if (!d->symver && d->literal)
6717 const char *verstr, *name;
6718 size_t namelen, verlen, newlen;
6719 char *newname, *p, leading_char;
6720 struct elf_link_hash_entry *newh;
6722 leading_char = bfd_get_symbol_leading_char (output_bfd);
6723 name = d->pattern;
6724 namelen = strlen (name) + (leading_char != '\0');
6725 verstr = t->name;
6726 verlen = strlen (verstr);
6727 newlen = namelen + verlen + 3;
6729 newname = (char *) bfd_malloc (newlen);
6730 if (newname == NULL)
6731 return false;
6732 newname[0] = leading_char;
6733 memcpy (newname + (leading_char != '\0'), name, namelen);
6735 /* Check the hidden versioned definition. */
6736 p = newname + namelen;
6737 *p++ = ELF_VER_CHR;
6738 memcpy (p, verstr, verlen + 1);
6739 newh = elf_link_hash_lookup (elf_hash_table (info),
6740 newname, false, false,
6741 false);
6742 if (newh == NULL
6743 || (newh->root.type != bfd_link_hash_defined
6744 && newh->root.type != bfd_link_hash_defweak))
6746 /* Check the default versioned definition. */
6747 *p++ = ELF_VER_CHR;
6748 memcpy (p, verstr, verlen + 1);
6749 newh = elf_link_hash_lookup (elf_hash_table (info),
6750 newname, false, false,
6751 false);
6753 free (newname);
6755 /* Mark this version if there is a definition and it is
6756 not defined in a shared object. */
6757 if (newh != NULL
6758 && !newh->def_dynamic
6759 && (newh->root.type == bfd_link_hash_defined
6760 || newh->root.type == bfd_link_hash_defweak))
6761 d->symver = 1;
6764 /* Attach all the symbols to their version information. */
6765 asvinfo.info = info;
6766 asvinfo.failed = false;
6768 elf_link_hash_traverse (elf_hash_table (info),
6769 _bfd_elf_link_assign_sym_version,
6770 &asvinfo);
6771 if (asvinfo.failed)
6772 return false;
6774 if (!info->allow_undefined_version)
6776 /* Check if all global versions have a definition. */
6777 bool all_defined = true;
6778 for (t = info->version_info; t != NULL; t = t->next)
6779 for (d = t->globals.list; d != NULL; d = d->next)
6780 if (d->literal && !d->symver && !d->script)
6782 _bfd_error_handler
6783 (_("%s: undefined version: %s"),
6784 d->pattern, t->name);
6785 all_defined = false;
6788 if (!all_defined)
6790 bfd_set_error (bfd_error_bad_value);
6791 return false;
6795 /* Set up the version definition section. */
6796 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6797 BFD_ASSERT (s != NULL);
6799 /* We may have created additional version definitions if we are
6800 just linking a regular application. */
6801 verdefs = info->version_info;
6803 /* Skip anonymous version tag. */
6804 if (verdefs != NULL && verdefs->vernum == 0)
6805 verdefs = verdefs->next;
6807 if (verdefs == NULL && !info->create_default_symver)
6808 s->flags |= SEC_EXCLUDE;
6809 else
6811 unsigned int cdefs;
6812 bfd_size_type size;
6813 bfd_byte *p;
6814 Elf_Internal_Verdef def;
6815 Elf_Internal_Verdaux defaux;
6816 struct bfd_link_hash_entry *bh;
6817 struct elf_link_hash_entry *h;
6818 const char *name;
6820 cdefs = 0;
6821 size = 0;
6823 /* Make space for the base version. */
6824 size += sizeof (Elf_External_Verdef);
6825 size += sizeof (Elf_External_Verdaux);
6826 ++cdefs;
6828 /* Make space for the default version. */
6829 if (info->create_default_symver)
6831 size += sizeof (Elf_External_Verdef);
6832 ++cdefs;
6835 for (t = verdefs; t != NULL; t = t->next)
6837 struct bfd_elf_version_deps *n;
6839 /* Don't emit base version twice. */
6840 if (t->vernum == 0)
6841 continue;
6843 size += sizeof (Elf_External_Verdef);
6844 size += sizeof (Elf_External_Verdaux);
6845 ++cdefs;
6847 for (n = t->deps; n != NULL; n = n->next)
6848 size += sizeof (Elf_External_Verdaux);
6851 s->size = size;
6852 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6853 if (s->contents == NULL && s->size != 0)
6854 return false;
6856 /* Fill in the version definition section. */
6858 p = s->contents;
6860 def.vd_version = VER_DEF_CURRENT;
6861 def.vd_flags = VER_FLG_BASE;
6862 def.vd_ndx = 1;
6863 def.vd_cnt = 1;
6864 if (info->create_default_symver)
6866 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6867 def.vd_next = sizeof (Elf_External_Verdef);
6869 else
6871 def.vd_aux = sizeof (Elf_External_Verdef);
6872 def.vd_next = (sizeof (Elf_External_Verdef)
6873 + sizeof (Elf_External_Verdaux));
6876 if (soname_indx != (size_t) -1)
6878 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6879 soname_indx);
6880 def.vd_hash = bfd_elf_hash (soname);
6881 defaux.vda_name = soname_indx;
6882 name = soname;
6884 else
6886 size_t indx;
6888 name = lbasename (bfd_get_filename (output_bfd));
6889 def.vd_hash = bfd_elf_hash (name);
6890 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6891 name, false);
6892 if (indx == (size_t) -1)
6893 return false;
6894 defaux.vda_name = indx;
6896 defaux.vda_next = 0;
6898 _bfd_elf_swap_verdef_out (output_bfd, &def,
6899 (Elf_External_Verdef *) p);
6900 p += sizeof (Elf_External_Verdef);
6901 if (info->create_default_symver)
6903 /* Add a symbol representing this version. */
6904 bh = NULL;
6905 if (! (_bfd_generic_link_add_one_symbol
6906 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6907 0, NULL, false,
6908 get_elf_backend_data (dynobj)->collect, &bh)))
6909 return false;
6910 h = (struct elf_link_hash_entry *) bh;
6911 h->non_elf = 0;
6912 h->def_regular = 1;
6913 h->type = STT_OBJECT;
6914 h->verinfo.vertree = NULL;
6916 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6917 return false;
6919 /* Create a duplicate of the base version with the same
6920 aux block, but different flags. */
6921 def.vd_flags = 0;
6922 def.vd_ndx = 2;
6923 def.vd_aux = sizeof (Elf_External_Verdef);
6924 if (verdefs)
6925 def.vd_next = (sizeof (Elf_External_Verdef)
6926 + sizeof (Elf_External_Verdaux));
6927 else
6928 def.vd_next = 0;
6929 _bfd_elf_swap_verdef_out (output_bfd, &def,
6930 (Elf_External_Verdef *) p);
6931 p += sizeof (Elf_External_Verdef);
6933 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6934 (Elf_External_Verdaux *) p);
6935 p += sizeof (Elf_External_Verdaux);
6937 for (t = verdefs; t != NULL; t = t->next)
6939 unsigned int cdeps;
6940 struct bfd_elf_version_deps *n;
6942 /* Don't emit the base version twice. */
6943 if (t->vernum == 0)
6944 continue;
6946 cdeps = 0;
6947 for (n = t->deps; n != NULL; n = n->next)
6948 ++cdeps;
6950 /* Add a symbol representing this version. */
6951 bh = NULL;
6952 if (! (_bfd_generic_link_add_one_symbol
6953 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6954 0, NULL, false,
6955 get_elf_backend_data (dynobj)->collect, &bh)))
6956 return false;
6957 h = (struct elf_link_hash_entry *) bh;
6958 h->non_elf = 0;
6959 h->def_regular = 1;
6960 h->type = STT_OBJECT;
6961 h->verinfo.vertree = t;
6963 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6964 return false;
6966 def.vd_version = VER_DEF_CURRENT;
6967 def.vd_flags = 0;
6968 if (t->globals.list == NULL
6969 && t->locals.list == NULL
6970 && ! t->used)
6971 def.vd_flags |= VER_FLG_WEAK;
6972 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6973 def.vd_cnt = cdeps + 1;
6974 def.vd_hash = bfd_elf_hash (t->name);
6975 def.vd_aux = sizeof (Elf_External_Verdef);
6976 def.vd_next = 0;
6978 /* If a basever node is next, it *must* be the last node in
6979 the chain, otherwise Verdef construction breaks. */
6980 if (t->next != NULL && t->next->vernum == 0)
6981 BFD_ASSERT (t->next->next == NULL);
6983 if (t->next != NULL && t->next->vernum != 0)
6984 def.vd_next = (sizeof (Elf_External_Verdef)
6985 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6987 _bfd_elf_swap_verdef_out (output_bfd, &def,
6988 (Elf_External_Verdef *) p);
6989 p += sizeof (Elf_External_Verdef);
6991 defaux.vda_name = h->dynstr_index;
6992 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6993 h->dynstr_index);
6994 defaux.vda_next = 0;
6995 if (t->deps != NULL)
6996 defaux.vda_next = sizeof (Elf_External_Verdaux);
6997 t->name_indx = defaux.vda_name;
6999 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
7000 (Elf_External_Verdaux *) p);
7001 p += sizeof (Elf_External_Verdaux);
7003 for (n = t->deps; n != NULL; n = n->next)
7005 if (n->version_needed == NULL)
7007 /* This can happen if there was an error in the
7008 version script. */
7009 defaux.vda_name = 0;
7011 else
7013 defaux.vda_name = n->version_needed->name_indx;
7014 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
7015 defaux.vda_name);
7017 if (n->next == NULL)
7018 defaux.vda_next = 0;
7019 else
7020 defaux.vda_next = sizeof (Elf_External_Verdaux);
7022 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
7023 (Elf_External_Verdaux *) p);
7024 p += sizeof (Elf_External_Verdaux);
7028 elf_tdata (output_bfd)->cverdefs = cdefs;
7032 if (info->gc_sections && bed->can_gc_sections)
7034 struct elf_gc_sweep_symbol_info sweep_info;
7036 /* Remove the symbols that were in the swept sections from the
7037 dynamic symbol table. */
7038 sweep_info.info = info;
7039 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
7040 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
7041 &sweep_info);
7044 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7046 asection *s;
7047 struct elf_find_verdep_info sinfo;
7049 /* Work out the size of the version reference section. */
7051 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
7052 BFD_ASSERT (s != NULL);
7054 sinfo.info = info;
7055 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
7056 if (sinfo.vers == 0)
7057 sinfo.vers = 1;
7058 sinfo.failed = false;
7060 elf_link_hash_traverse (elf_hash_table (info),
7061 _bfd_elf_link_find_version_dependencies,
7062 &sinfo);
7063 if (sinfo.failed)
7064 return false;
7066 bed->elf_backend_add_glibc_version_dependency (&sinfo);
7067 if (sinfo.failed)
7068 return false;
7070 if (elf_tdata (output_bfd)->verref == NULL)
7071 s->flags |= SEC_EXCLUDE;
7072 else
7074 Elf_Internal_Verneed *vn;
7075 unsigned int size;
7076 unsigned int crefs;
7077 bfd_byte *p;
7079 /* Build the version dependency section. */
7080 size = 0;
7081 crefs = 0;
7082 for (vn = elf_tdata (output_bfd)->verref;
7083 vn != NULL;
7084 vn = vn->vn_nextref)
7086 Elf_Internal_Vernaux *a;
7088 size += sizeof (Elf_External_Verneed);
7089 ++crefs;
7090 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7091 size += sizeof (Elf_External_Vernaux);
7094 s->size = size;
7095 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7096 if (s->contents == NULL)
7097 return false;
7099 p = s->contents;
7100 for (vn = elf_tdata (output_bfd)->verref;
7101 vn != NULL;
7102 vn = vn->vn_nextref)
7104 unsigned int caux;
7105 Elf_Internal_Vernaux *a;
7106 size_t indx;
7108 caux = 0;
7109 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7110 ++caux;
7112 vn->vn_version = VER_NEED_CURRENT;
7113 vn->vn_cnt = caux;
7114 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7115 elf_dt_name (vn->vn_bfd) != NULL
7116 ? elf_dt_name (vn->vn_bfd)
7117 : lbasename (bfd_get_filename
7118 (vn->vn_bfd)),
7119 false);
7120 if (indx == (size_t) -1)
7121 return false;
7122 vn->vn_file = indx;
7123 vn->vn_aux = sizeof (Elf_External_Verneed);
7124 if (vn->vn_nextref == NULL)
7125 vn->vn_next = 0;
7126 else
7127 vn->vn_next = (sizeof (Elf_External_Verneed)
7128 + caux * sizeof (Elf_External_Vernaux));
7130 _bfd_elf_swap_verneed_out (output_bfd, vn,
7131 (Elf_External_Verneed *) p);
7132 p += sizeof (Elf_External_Verneed);
7134 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7136 a->vna_hash = bfd_elf_hash (a->vna_nodename);
7137 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7138 a->vna_nodename, false);
7139 if (indx == (size_t) -1)
7140 return false;
7141 a->vna_name = indx;
7142 if (a->vna_nextptr == NULL)
7143 a->vna_next = 0;
7144 else
7145 a->vna_next = sizeof (Elf_External_Vernaux);
7147 _bfd_elf_swap_vernaux_out (output_bfd, a,
7148 (Elf_External_Vernaux *) p);
7149 p += sizeof (Elf_External_Vernaux);
7153 elf_tdata (output_bfd)->cverrefs = crefs;
7157 if (bfd_link_relocatable (info)
7158 && !_bfd_elf_size_group_sections (info))
7159 return false;
7161 /* Determine any GNU_STACK segment requirements, after the backend
7162 has had a chance to set a default segment size. */
7163 if (info->execstack)
7165 /* If the user has explicitly requested warnings, then generate one even
7166 though the choice is the result of another command line option. */
7167 if (info->warn_execstack == 1)
7169 if (info->error_execstack)
7171 _bfd_error_handler
7172 (_("\
7173 error: creating an executable stack because of -z execstack command line option"));
7174 return false;
7177 _bfd_error_handler
7178 (_("\
7179 warning: enabling an executable stack because of -z execstack command line option"));
7182 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
7184 else if (info->noexecstack)
7185 elf_stack_flags (output_bfd) = PF_R | PF_W;
7186 else
7188 bfd *inputobj;
7189 asection *notesec = NULL;
7190 bfd *noteobj = NULL;
7191 bfd *emptyobj = NULL;
7192 int exec = 0;
7194 for (inputobj = info->input_bfds;
7195 inputobj;
7196 inputobj = inputobj->link.next)
7198 asection *s;
7200 if (inputobj->flags
7201 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
7202 continue;
7203 s = inputobj->sections;
7204 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7205 continue;
7207 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
7208 if (s)
7210 notesec = s;
7211 if (s->flags & SEC_CODE)
7213 noteobj = inputobj;
7214 exec = PF_X;
7215 /* There is no point in scanning the remaining bfds. */
7216 break;
7219 else if (bed->default_execstack && info->default_execstack)
7221 exec = PF_X;
7222 emptyobj = inputobj;
7226 if (notesec || info->stacksize > 0)
7228 if (exec)
7230 if (info->warn_execstack != 0)
7232 /* PR 29072: Because an executable stack is a serious
7233 security risk, make sure that the user knows that it is
7234 being enabled despite the fact that it was not requested
7235 on the command line. */
7236 if (noteobj)
7238 if (info->error_execstack)
7240 _bfd_error_handler (_("\
7241 error: %s: is triggering the generation of an executable stack (because it has an executable .note.GNU-stack section)"),
7242 bfd_get_filename (noteobj));
7243 return false;
7246 _bfd_error_handler (_("\
7247 warning: %s: requires executable stack (because the .note.GNU-stack section is executable)"),
7248 bfd_get_filename (noteobj));
7250 else if (emptyobj)
7252 if (info->error_execstack)
7254 _bfd_error_handler (_("\
7255 error: %s: is triggering the generation of an executable stack because it does not have a .note.GNU-stack section"),
7256 bfd_get_filename (emptyobj));
7257 return false;
7260 _bfd_error_handler (_("\
7261 warning: %s: missing .note.GNU-stack section implies executable stack"),
7262 bfd_get_filename (emptyobj));
7263 _bfd_error_handler (_("\
7264 NOTE: This behaviour is deprecated and will be removed in a future version of the linker"));
7268 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
7271 if (notesec && exec && bfd_link_relocatable (info)
7272 && notesec->output_section != bfd_abs_section_ptr)
7273 notesec->output_section->flags |= SEC_CODE;
7276 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7278 struct elf_info_failed eif;
7279 struct elf_link_hash_entry *h;
7280 asection *dynstr;
7281 asection *s;
7283 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
7284 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
7286 if (info->symbolic)
7288 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
7289 return false;
7290 info->flags |= DF_SYMBOLIC;
7293 if (rpath != NULL)
7295 size_t indx;
7296 bfd_vma tag;
7298 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
7299 true);
7300 if (indx == (size_t) -1)
7301 return false;
7303 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
7304 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
7305 return false;
7308 if (filter_shlib != NULL)
7310 size_t indx;
7312 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7313 filter_shlib, true);
7314 if (indx == (size_t) -1
7315 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
7316 return false;
7319 if (auxiliary_filters != NULL)
7321 const char * const *p;
7323 for (p = auxiliary_filters; *p != NULL; p++)
7325 size_t indx;
7327 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7328 *p, true);
7329 if (indx == (size_t) -1
7330 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
7331 return false;
7335 if (audit != NULL)
7337 size_t indx;
7339 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
7340 true);
7341 if (indx == (size_t) -1
7342 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
7343 return false;
7346 if (depaudit != NULL)
7348 size_t indx;
7350 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
7351 true);
7352 if (indx == (size_t) -1
7353 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
7354 return false;
7357 eif.info = info;
7358 eif.failed = false;
7360 /* Find all symbols which were defined in a dynamic object and make
7361 the backend pick a reasonable value for them. */
7362 elf_link_hash_traverse (elf_hash_table (info),
7363 _bfd_elf_adjust_dynamic_symbol,
7364 &eif);
7365 if (eif.failed)
7366 return false;
7368 /* Add some entries to the .dynamic section. We fill in some of the
7369 values later, in bfd_elf_final_link, but we must add the entries
7370 now so that we know the final size of the .dynamic section. */
7372 /* If there are initialization and/or finalization functions to
7373 call then add the corresponding DT_INIT/DT_FINI entries. */
7374 h = (info->init_function
7375 ? elf_link_hash_lookup (elf_hash_table (info),
7376 info->init_function, false,
7377 false, false)
7378 : NULL);
7379 if (h != NULL
7380 && (h->ref_regular
7381 || h->def_regular))
7383 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
7384 return false;
7386 h = (info->fini_function
7387 ? elf_link_hash_lookup (elf_hash_table (info),
7388 info->fini_function, false,
7389 false, false)
7390 : NULL);
7391 if (h != NULL
7392 && (h->ref_regular
7393 || h->def_regular))
7395 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
7396 return false;
7399 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
7400 if (s != NULL && s->linker_has_input)
7402 /* DT_PREINIT_ARRAY is not allowed in shared library. */
7403 if (! bfd_link_executable (info))
7405 bfd *sub;
7406 asection *o;
7408 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
7409 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
7410 && (o = sub->sections) != NULL
7411 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
7412 for (o = sub->sections; o != NULL; o = o->next)
7413 if (elf_section_data (o)->this_hdr.sh_type
7414 == SHT_PREINIT_ARRAY)
7416 _bfd_error_handler
7417 (_("%pB: .preinit_array section is not allowed in DSO"),
7418 sub);
7419 break;
7422 bfd_set_error (bfd_error_nonrepresentable_section);
7423 return false;
7426 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
7427 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
7428 return false;
7430 s = bfd_get_section_by_name (output_bfd, ".init_array");
7431 if (s != NULL && s->linker_has_input)
7433 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
7434 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
7435 return false;
7437 s = bfd_get_section_by_name (output_bfd, ".fini_array");
7438 if (s != NULL && s->linker_has_input)
7440 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
7441 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
7442 return false;
7445 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
7446 /* If .dynstr is excluded from the link, we don't want any of
7447 these tags. Strictly, we should be checking each section
7448 individually; This quick check covers for the case where
7449 someone does a /DISCARD/ : { *(*) }. */
7450 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
7452 bfd_size_type strsize;
7454 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7455 if ((info->emit_hash
7456 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
7457 || (info->emit_gnu_hash
7458 && (bed->record_xhash_symbol == NULL
7459 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)))
7460 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
7461 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
7462 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
7463 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
7464 bed->s->sizeof_sym)
7465 || (info->gnu_flags_1
7466 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_FLAGS_1,
7467 info->gnu_flags_1)))
7468 return false;
7472 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
7473 return false;
7475 /* The backend must work out the sizes of all the other dynamic
7476 sections. */
7477 if (dynobj != NULL
7478 && bed->elf_backend_size_dynamic_sections != NULL
7479 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
7480 return false;
7482 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7484 if (elf_tdata (output_bfd)->cverdefs)
7486 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
7488 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
7489 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
7490 return false;
7493 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
7495 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
7496 return false;
7498 else if (info->flags & DF_BIND_NOW)
7500 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
7501 return false;
7504 if (info->flags_1)
7506 if (bfd_link_executable (info))
7507 info->flags_1 &= ~ (DF_1_INITFIRST
7508 | DF_1_NODELETE
7509 | DF_1_NOOPEN);
7510 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
7511 return false;
7514 if (elf_tdata (output_bfd)->cverrefs)
7516 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
7518 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
7519 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
7520 return false;
7523 if ((elf_tdata (output_bfd)->cverrefs == 0
7524 && elf_tdata (output_bfd)->cverdefs == 0)
7525 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
7527 asection *s;
7529 s = bfd_get_linker_section (dynobj, ".gnu.version");
7530 s->flags |= SEC_EXCLUDE;
7533 return true;
7536 /* Find the first non-excluded output section. We'll use its
7537 section symbol for some emitted relocs. */
7538 void
7539 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7541 asection *s;
7542 asection *found = NULL;
7544 for (s = output_bfd->sections; s != NULL; s = s->next)
7545 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7546 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7548 found = s;
7549 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7550 break;
7552 elf_hash_table (info)->text_index_section = found;
7555 /* Find two non-excluded output sections, one for code, one for data.
7556 We'll use their section symbols for some emitted relocs. */
7557 void
7558 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7560 asection *s;
7561 asection *found = NULL;
7563 /* Data first, since setting text_index_section changes
7564 _bfd_elf_omit_section_dynsym_default. */
7565 for (s = output_bfd->sections; s != NULL; s = s->next)
7566 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7567 && !(s->flags & SEC_READONLY)
7568 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7570 found = s;
7571 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7572 break;
7574 elf_hash_table (info)->data_index_section = found;
7576 for (s = output_bfd->sections; s != NULL; s = s->next)
7577 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7578 && (s->flags & SEC_READONLY)
7579 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7581 found = s;
7582 break;
7584 elf_hash_table (info)->text_index_section = found;
7587 #define GNU_HASH_SECTION_NAME(bed) \
7588 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7590 bool
7591 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7593 const struct elf_backend_data *bed;
7594 unsigned long section_sym_count;
7595 bfd_size_type dynsymcount = 0;
7597 if (!is_elf_hash_table (info->hash))
7598 return true;
7600 bed = get_elf_backend_data (output_bfd);
7601 (*bed->elf_backend_init_index_section) (output_bfd, info);
7603 /* Assign dynsym indices. In a shared library we generate a section
7604 symbol for each output section, which come first. Next come all
7605 of the back-end allocated local dynamic syms, followed by the rest
7606 of the global symbols.
7608 This is usually not needed for static binaries, however backends
7609 can request to always do it, e.g. the MIPS backend uses dynamic
7610 symbol counts to lay out GOT, which will be produced in the
7611 presence of GOT relocations even in static binaries (holding fixed
7612 data in that case, to satisfy those relocations). */
7614 if (elf_hash_table (info)->dynamic_sections_created
7615 || bed->always_renumber_dynsyms)
7616 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7617 &section_sym_count);
7619 if (elf_hash_table (info)->dynamic_sections_created)
7621 bfd *dynobj;
7622 asection *s;
7623 unsigned int dtagcount;
7625 dynobj = elf_hash_table (info)->dynobj;
7627 /* Work out the size of the symbol version section. */
7628 s = bfd_get_linker_section (dynobj, ".gnu.version");
7629 BFD_ASSERT (s != NULL);
7630 if ((s->flags & SEC_EXCLUDE) == 0)
7632 s->size = dynsymcount * sizeof (Elf_External_Versym);
7633 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7634 if (s->contents == NULL)
7635 return false;
7637 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7638 return false;
7641 /* Set the size of the .dynsym and .hash sections. We counted
7642 the number of dynamic symbols in elf_link_add_object_symbols.
7643 We will build the contents of .dynsym and .hash when we build
7644 the final symbol table, because until then we do not know the
7645 correct value to give the symbols. We built the .dynstr
7646 section as we went along in elf_link_add_object_symbols. */
7647 s = elf_hash_table (info)->dynsym;
7648 BFD_ASSERT (s != NULL);
7649 s->size = dynsymcount * bed->s->sizeof_sym;
7651 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7652 if (s->contents == NULL)
7653 return false;
7655 /* The first entry in .dynsym is a dummy symbol. Clear all the
7656 section syms, in case we don't output them all. */
7657 ++section_sym_count;
7658 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
7660 elf_hash_table (info)->bucketcount = 0;
7662 /* Compute the size of the hashing table. As a side effect this
7663 computes the hash values for all the names we export. */
7664 if (info->emit_hash)
7666 unsigned long int *hashcodes;
7667 struct hash_codes_info hashinf;
7668 bfd_size_type amt;
7669 unsigned long int nsyms;
7670 size_t bucketcount;
7671 size_t hash_entry_size;
7673 /* Compute the hash values for all exported symbols. At the same
7674 time store the values in an array so that we could use them for
7675 optimizations. */
7676 amt = dynsymcount * sizeof (unsigned long int);
7677 hashcodes = (unsigned long int *) bfd_malloc (amt);
7678 if (hashcodes == NULL)
7679 return false;
7680 hashinf.hashcodes = hashcodes;
7681 hashinf.error = false;
7683 /* Put all hash values in HASHCODES. */
7684 elf_link_hash_traverse (elf_hash_table (info),
7685 elf_collect_hash_codes, &hashinf);
7686 if (hashinf.error)
7688 free (hashcodes);
7689 return false;
7692 nsyms = hashinf.hashcodes - hashcodes;
7693 bucketcount
7694 = compute_bucket_count (info, hashcodes, nsyms, 0);
7695 free (hashcodes);
7697 if (bucketcount == 0 && nsyms > 0)
7698 return false;
7700 elf_hash_table (info)->bucketcount = bucketcount;
7702 s = bfd_get_linker_section (dynobj, ".hash");
7703 BFD_ASSERT (s != NULL);
7704 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7705 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
7706 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7707 if (s->contents == NULL)
7708 return false;
7710 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7711 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7712 s->contents + hash_entry_size);
7715 if (info->emit_gnu_hash)
7717 size_t i, cnt;
7718 unsigned char *contents;
7719 struct collect_gnu_hash_codes cinfo;
7720 bfd_size_type amt;
7721 size_t bucketcount;
7723 memset (&cinfo, 0, sizeof (cinfo));
7725 /* Compute the hash values for all exported symbols. At the same
7726 time store the values in an array so that we could use them for
7727 optimizations. */
7728 amt = dynsymcount * 2 * sizeof (unsigned long int);
7729 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
7730 if (cinfo.hashcodes == NULL)
7731 return false;
7733 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7734 cinfo.min_dynindx = -1;
7735 cinfo.output_bfd = output_bfd;
7736 cinfo.bed = bed;
7738 /* Put all hash values in HASHCODES. */
7739 elf_link_hash_traverse (elf_hash_table (info),
7740 elf_collect_gnu_hash_codes, &cinfo);
7741 if (cinfo.error)
7743 free (cinfo.hashcodes);
7744 return false;
7747 bucketcount
7748 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7750 if (bucketcount == 0)
7752 free (cinfo.hashcodes);
7753 return false;
7756 s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (bed));
7757 BFD_ASSERT (s != NULL);
7759 if (cinfo.nsyms == 0)
7761 /* Empty .gnu.hash or .MIPS.xhash section is special. */
7762 BFD_ASSERT (cinfo.min_dynindx == -1);
7763 free (cinfo.hashcodes);
7764 s->size = 5 * 4 + bed->s->arch_size / 8;
7765 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7766 if (contents == NULL)
7767 return false;
7768 s->contents = contents;
7769 /* 1 empty bucket. */
7770 bfd_put_32 (output_bfd, 1, contents);
7771 /* SYMIDX above the special symbol 0. */
7772 bfd_put_32 (output_bfd, 1, contents + 4);
7773 /* Just one word for bitmask. */
7774 bfd_put_32 (output_bfd, 1, contents + 8);
7775 /* Only hash fn bloom filter. */
7776 bfd_put_32 (output_bfd, 0, contents + 12);
7777 /* No hashes are valid - empty bitmask. */
7778 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7779 /* No hashes in the only bucket. */
7780 bfd_put_32 (output_bfd, 0,
7781 contents + 16 + bed->s->arch_size / 8);
7783 else
7785 unsigned long int maskwords, maskbitslog2, x;
7786 BFD_ASSERT (cinfo.min_dynindx != -1);
7788 x = cinfo.nsyms;
7789 maskbitslog2 = 1;
7790 while ((x >>= 1) != 0)
7791 ++maskbitslog2;
7792 if (maskbitslog2 < 3)
7793 maskbitslog2 = 5;
7794 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7795 maskbitslog2 = maskbitslog2 + 3;
7796 else
7797 maskbitslog2 = maskbitslog2 + 2;
7798 if (bed->s->arch_size == 64)
7800 if (maskbitslog2 == 5)
7801 maskbitslog2 = 6;
7802 cinfo.shift1 = 6;
7804 else
7805 cinfo.shift1 = 5;
7806 cinfo.mask = (1 << cinfo.shift1) - 1;
7807 cinfo.shift2 = maskbitslog2;
7808 cinfo.maskbits = 1 << maskbitslog2;
7809 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7810 amt = bucketcount * sizeof (unsigned long int) * 2;
7811 amt += maskwords * sizeof (bfd_vma);
7812 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
7813 if (cinfo.bitmask == NULL)
7815 free (cinfo.hashcodes);
7816 return false;
7819 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
7820 cinfo.indx = cinfo.counts + bucketcount;
7821 cinfo.symindx = dynsymcount - cinfo.nsyms;
7822 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7824 /* Determine how often each hash bucket is used. */
7825 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7826 for (i = 0; i < cinfo.nsyms; ++i)
7827 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7829 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7830 if (cinfo.counts[i] != 0)
7832 cinfo.indx[i] = cnt;
7833 cnt += cinfo.counts[i];
7835 BFD_ASSERT (cnt == dynsymcount);
7836 cinfo.bucketcount = bucketcount;
7837 cinfo.local_indx = cinfo.min_dynindx;
7839 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7840 s->size += cinfo.maskbits / 8;
7841 if (bed->record_xhash_symbol != NULL)
7842 s->size += cinfo.nsyms * 4;
7843 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7844 if (contents == NULL)
7846 free (cinfo.bitmask);
7847 free (cinfo.hashcodes);
7848 return false;
7851 s->contents = contents;
7852 bfd_put_32 (output_bfd, bucketcount, contents);
7853 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7854 bfd_put_32 (output_bfd, maskwords, contents + 8);
7855 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7856 contents += 16 + cinfo.maskbits / 8;
7858 for (i = 0; i < bucketcount; ++i)
7860 if (cinfo.counts[i] == 0)
7861 bfd_put_32 (output_bfd, 0, contents);
7862 else
7863 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7864 contents += 4;
7867 cinfo.contents = contents;
7869 cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents;
7870 /* Renumber dynamic symbols, if populating .gnu.hash section.
7871 If using .MIPS.xhash, populate the translation table. */
7872 elf_link_hash_traverse (elf_hash_table (info),
7873 elf_gnu_hash_process_symidx, &cinfo);
7875 contents = s->contents + 16;
7876 for (i = 0; i < maskwords; ++i)
7878 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7879 contents);
7880 contents += bed->s->arch_size / 8;
7883 free (cinfo.bitmask);
7884 free (cinfo.hashcodes);
7888 s = bfd_get_linker_section (dynobj, ".dynstr");
7889 BFD_ASSERT (s != NULL);
7891 elf_finalize_dynstr (output_bfd, info);
7893 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7895 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7896 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7897 return false;
7900 return true;
7903 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7905 static void
7906 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7907 asection *sec)
7909 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7910 sec->sec_info_type = SEC_INFO_TYPE_NONE;
7913 /* Finish SHF_MERGE section merging. */
7915 bool
7916 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
7918 bfd *ibfd;
7919 asection *sec;
7921 if (!is_elf_hash_table (info->hash))
7922 return false;
7924 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7925 if ((ibfd->flags & DYNAMIC) == 0
7926 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
7927 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7928 == get_elf_backend_data (obfd)->s->elfclass))
7929 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7930 if ((sec->flags & SEC_MERGE) != 0
7931 && !bfd_is_abs_section (sec->output_section))
7933 struct bfd_elf_section_data *secdata;
7935 secdata = elf_section_data (sec);
7936 if (! _bfd_add_merge_section (obfd,
7937 &elf_hash_table (info)->merge_info,
7938 sec, &secdata->sec_info))
7939 return false;
7940 else if (secdata->sec_info)
7941 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
7944 if (elf_hash_table (info)->merge_info != NULL)
7945 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
7946 merge_sections_remove_hook);
7947 return true;
7950 /* Create an entry in an ELF linker hash table. */
7952 struct bfd_hash_entry *
7953 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7954 struct bfd_hash_table *table,
7955 const char *string)
7957 /* Allocate the structure if it has not already been allocated by a
7958 subclass. */
7959 if (entry == NULL)
7961 entry = (struct bfd_hash_entry *)
7962 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
7963 if (entry == NULL)
7964 return entry;
7967 /* Call the allocation method of the superclass. */
7968 entry = _bfd_link_hash_newfunc (entry, table, string);
7969 if (entry != NULL)
7971 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7972 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7974 /* Set local fields. */
7975 ret->indx = -1;
7976 ret->dynindx = -1;
7977 ret->got = htab->init_got_refcount;
7978 ret->plt = htab->init_plt_refcount;
7979 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7980 - offsetof (struct elf_link_hash_entry, size)));
7981 /* Assume that we have been called by a non-ELF symbol reader.
7982 This flag is then reset by the code which reads an ELF input
7983 file. This ensures that a symbol created by a non-ELF symbol
7984 reader will have the flag set correctly. */
7985 ret->non_elf = 1;
7988 return entry;
7991 /* Copy data from an indirect symbol to its direct symbol, hiding the
7992 old indirect symbol. Also used for copying flags to a weakdef. */
7994 void
7995 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7996 struct elf_link_hash_entry *dir,
7997 struct elf_link_hash_entry *ind)
7999 struct elf_link_hash_table *htab;
8001 if (ind->dyn_relocs != NULL)
8003 if (dir->dyn_relocs != NULL)
8005 struct elf_dyn_relocs **pp;
8006 struct elf_dyn_relocs *p;
8008 /* Add reloc counts against the indirect sym to the direct sym
8009 list. Merge any entries against the same section. */
8010 for (pp = &ind->dyn_relocs; (p = *pp) != NULL; )
8012 struct elf_dyn_relocs *q;
8014 for (q = dir->dyn_relocs; q != NULL; q = q->next)
8015 if (q->sec == p->sec)
8017 q->pc_count += p->pc_count;
8018 q->count += p->count;
8019 *pp = p->next;
8020 break;
8022 if (q == NULL)
8023 pp = &p->next;
8025 *pp = dir->dyn_relocs;
8028 dir->dyn_relocs = ind->dyn_relocs;
8029 ind->dyn_relocs = NULL;
8032 /* Copy down any references that we may have already seen to the
8033 symbol which just became indirect. */
8035 if (dir->versioned != versioned_hidden)
8036 dir->ref_dynamic |= ind->ref_dynamic;
8037 dir->ref_regular |= ind->ref_regular;
8038 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
8039 dir->non_got_ref |= ind->non_got_ref;
8040 dir->needs_plt |= ind->needs_plt;
8041 dir->pointer_equality_needed |= ind->pointer_equality_needed;
8043 if (ind->root.type != bfd_link_hash_indirect)
8044 return;
8046 /* Copy over the global and procedure linkage table refcount entries.
8047 These may have been already set up by a check_relocs routine. */
8048 htab = elf_hash_table (info);
8049 if (ind->got.refcount > htab->init_got_refcount.refcount)
8051 if (dir->got.refcount < 0)
8052 dir->got.refcount = 0;
8053 dir->got.refcount += ind->got.refcount;
8054 ind->got.refcount = htab->init_got_refcount.refcount;
8057 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
8059 if (dir->plt.refcount < 0)
8060 dir->plt.refcount = 0;
8061 dir->plt.refcount += ind->plt.refcount;
8062 ind->plt.refcount = htab->init_plt_refcount.refcount;
8065 if (ind->dynindx != -1)
8067 if (dir->dynindx != -1)
8068 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
8069 dir->dynindx = ind->dynindx;
8070 dir->dynstr_index = ind->dynstr_index;
8071 ind->dynindx = -1;
8072 ind->dynstr_index = 0;
8076 void
8077 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
8078 struct elf_link_hash_entry *h,
8079 bool force_local)
8081 /* STT_GNU_IFUNC symbol must go through PLT. */
8082 if (h->type != STT_GNU_IFUNC)
8084 h->plt = elf_hash_table (info)->init_plt_offset;
8085 h->needs_plt = 0;
8087 if (force_local)
8089 h->forced_local = 1;
8090 if (h->dynindx != -1)
8092 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8093 h->dynstr_index);
8094 h->dynindx = -1;
8095 h->dynstr_index = 0;
8100 /* Hide a symbol. */
8102 void
8103 _bfd_elf_link_hide_symbol (bfd *output_bfd,
8104 struct bfd_link_info *info,
8105 struct bfd_link_hash_entry *h)
8107 if (is_elf_hash_table (info->hash))
8109 const struct elf_backend_data *bed
8110 = get_elf_backend_data (output_bfd);
8111 struct elf_link_hash_entry *eh
8112 = (struct elf_link_hash_entry *) h;
8113 bed->elf_backend_hide_symbol (info, eh, true);
8114 eh->def_dynamic = 0;
8115 eh->ref_dynamic = 0;
8116 eh->dynamic_def = 0;
8120 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
8121 caller. */
8123 bool
8124 _bfd_elf_link_hash_table_init
8125 (struct elf_link_hash_table *table,
8126 bfd *abfd,
8127 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
8128 struct bfd_hash_table *,
8129 const char *),
8130 unsigned int entsize,
8131 enum elf_target_id target_id)
8133 bool ret;
8134 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
8136 table->init_got_refcount.refcount = can_refcount - 1;
8137 table->init_plt_refcount.refcount = can_refcount - 1;
8138 table->init_got_offset.offset = -(bfd_vma) 1;
8139 table->init_plt_offset.offset = -(bfd_vma) 1;
8140 /* The first dynamic symbol is a dummy. */
8141 table->dynsymcount = 1;
8143 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
8145 table->root.type = bfd_link_elf_hash_table;
8146 table->hash_table_id = target_id;
8147 table->target_os = get_elf_backend_data (abfd)->target_os;
8149 return ret;
8152 /* Create an ELF linker hash table. */
8154 struct bfd_link_hash_table *
8155 _bfd_elf_link_hash_table_create (bfd *abfd)
8157 struct elf_link_hash_table *ret;
8158 size_t amt = sizeof (struct elf_link_hash_table);
8160 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
8161 if (ret == NULL)
8162 return NULL;
8164 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
8165 sizeof (struct elf_link_hash_entry),
8166 GENERIC_ELF_DATA))
8168 free (ret);
8169 return NULL;
8171 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
8173 return &ret->root;
8176 /* Destroy an ELF linker hash table. */
8178 void
8179 _bfd_elf_link_hash_table_free (bfd *obfd)
8181 struct elf_link_hash_table *htab;
8183 htab = (struct elf_link_hash_table *) obfd->link.hash;
8184 if (htab->dynstr != NULL)
8185 _bfd_elf_strtab_free (htab->dynstr);
8186 _bfd_merge_sections_free (htab->merge_info);
8187 _bfd_generic_link_hash_table_free (obfd);
8190 /* This is a hook for the ELF emulation code in the generic linker to
8191 tell the backend linker what file name to use for the DT_NEEDED
8192 entry for a dynamic object. */
8194 void
8195 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
8197 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8198 && bfd_get_format (abfd) == bfd_object)
8199 elf_dt_name (abfd) = name;
8203 bfd_elf_get_dyn_lib_class (bfd *abfd)
8205 int lib_class;
8206 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8207 && bfd_get_format (abfd) == bfd_object)
8208 lib_class = elf_dyn_lib_class (abfd);
8209 else
8210 lib_class = 0;
8211 return lib_class;
8214 void
8215 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
8217 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8218 && bfd_get_format (abfd) == bfd_object)
8219 elf_dyn_lib_class (abfd) = lib_class;
8222 /* Get the list of DT_NEEDED entries for a link. This is a hook for
8223 the linker ELF emulation code. */
8225 struct bfd_link_needed_list *
8226 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
8227 struct bfd_link_info *info)
8229 if (! is_elf_hash_table (info->hash))
8230 return NULL;
8231 return elf_hash_table (info)->needed;
8234 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
8235 hook for the linker ELF emulation code. */
8237 struct bfd_link_needed_list *
8238 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
8239 struct bfd_link_info *info)
8241 if (! is_elf_hash_table (info->hash))
8242 return NULL;
8243 return elf_hash_table (info)->runpath;
8246 /* Get the name actually used for a dynamic object for a link. This
8247 is the SONAME entry if there is one. Otherwise, it is the string
8248 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
8250 const char *
8251 bfd_elf_get_dt_soname (bfd *abfd)
8253 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8254 && bfd_get_format (abfd) == bfd_object)
8255 return elf_dt_name (abfd);
8256 return NULL;
8259 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
8260 the ELF linker emulation code. */
8262 bool
8263 bfd_elf_get_bfd_needed_list (bfd *abfd,
8264 struct bfd_link_needed_list **pneeded)
8266 asection *s;
8267 bfd_byte *dynbuf = NULL;
8268 unsigned int elfsec;
8269 unsigned long shlink;
8270 bfd_byte *extdyn, *extdynend;
8271 size_t extdynsize;
8272 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
8274 *pneeded = NULL;
8276 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
8277 || bfd_get_format (abfd) != bfd_object)
8278 return true;
8280 s = bfd_get_section_by_name (abfd, ".dynamic");
8281 if (s == NULL || s->size == 0 || (s->flags & SEC_HAS_CONTENTS) == 0)
8282 return true;
8284 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
8285 goto error_return;
8287 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
8288 if (elfsec == SHN_BAD)
8289 goto error_return;
8291 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
8293 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
8294 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
8296 for (extdyn = dynbuf, extdynend = dynbuf + s->size;
8297 (size_t) (extdynend - extdyn) >= extdynsize;
8298 extdyn += extdynsize)
8300 Elf_Internal_Dyn dyn;
8302 (*swap_dyn_in) (abfd, extdyn, &dyn);
8304 if (dyn.d_tag == DT_NULL)
8305 break;
8307 if (dyn.d_tag == DT_NEEDED)
8309 const char *string;
8310 struct bfd_link_needed_list *l;
8311 unsigned int tagv = dyn.d_un.d_val;
8312 size_t amt;
8314 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
8315 if (string == NULL)
8316 goto error_return;
8318 amt = sizeof *l;
8319 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
8320 if (l == NULL)
8321 goto error_return;
8323 l->by = abfd;
8324 l->name = string;
8325 l->next = *pneeded;
8326 *pneeded = l;
8330 free (dynbuf);
8332 return true;
8334 error_return:
8335 free (dynbuf);
8336 return false;
8339 struct elf_symbuf_symbol
8341 unsigned long st_name; /* Symbol name, index in string tbl */
8342 unsigned char st_info; /* Type and binding attributes */
8343 unsigned char st_other; /* Visibilty, and target specific */
8346 struct elf_symbuf_head
8348 struct elf_symbuf_symbol *ssym;
8349 size_t count;
8350 unsigned int st_shndx;
8353 struct elf_symbol
8355 union
8357 Elf_Internal_Sym *isym;
8358 struct elf_symbuf_symbol *ssym;
8359 void *p;
8360 } u;
8361 const char *name;
8364 /* Sort references to symbols by ascending section number. */
8366 static int
8367 elf_sort_elf_symbol (const void *arg1, const void *arg2)
8369 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
8370 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
8372 if (s1->st_shndx != s2->st_shndx)
8373 return s1->st_shndx > s2->st_shndx ? 1 : -1;
8374 /* Final sort by the address of the sym in the symbuf ensures
8375 a stable sort. */
8376 if (s1 != s2)
8377 return s1 > s2 ? 1 : -1;
8378 return 0;
8381 static int
8382 elf_sym_name_compare (const void *arg1, const void *arg2)
8384 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
8385 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
8386 int ret = strcmp (s1->name, s2->name);
8387 if (ret != 0)
8388 return ret;
8389 if (s1->u.p != s2->u.p)
8390 return s1->u.p > s2->u.p ? 1 : -1;
8391 return 0;
8394 static struct elf_symbuf_head *
8395 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
8397 Elf_Internal_Sym **ind, **indbufend, **indbuf;
8398 struct elf_symbuf_symbol *ssym;
8399 struct elf_symbuf_head *ssymbuf, *ssymhead;
8400 size_t i, shndx_count, total_size, amt;
8402 amt = symcount * sizeof (*indbuf);
8403 indbuf = (Elf_Internal_Sym **) bfd_malloc (amt);
8404 if (indbuf == NULL)
8405 return NULL;
8407 for (ind = indbuf, i = 0; i < symcount; i++)
8408 if (isymbuf[i].st_shndx != SHN_UNDEF)
8409 *ind++ = &isymbuf[i];
8410 indbufend = ind;
8412 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
8413 elf_sort_elf_symbol);
8415 shndx_count = 0;
8416 if (indbufend > indbuf)
8417 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
8418 if (ind[0]->st_shndx != ind[1]->st_shndx)
8419 shndx_count++;
8421 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
8422 + (indbufend - indbuf) * sizeof (*ssym));
8423 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
8424 if (ssymbuf == NULL)
8426 free (indbuf);
8427 return NULL;
8430 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
8431 ssymbuf->ssym = NULL;
8432 ssymbuf->count = shndx_count;
8433 ssymbuf->st_shndx = 0;
8434 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
8436 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
8438 ssymhead++;
8439 ssymhead->ssym = ssym;
8440 ssymhead->count = 0;
8441 ssymhead->st_shndx = (*ind)->st_shndx;
8443 ssym->st_name = (*ind)->st_name;
8444 ssym->st_info = (*ind)->st_info;
8445 ssym->st_other = (*ind)->st_other;
8446 ssymhead->count++;
8448 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
8449 && (uintptr_t) ssym - (uintptr_t) ssymbuf == total_size);
8451 free (indbuf);
8452 return ssymbuf;
8455 /* Check if 2 sections define the same set of local and global
8456 symbols. */
8458 static bool
8459 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
8460 struct bfd_link_info *info)
8462 bfd *bfd1, *bfd2;
8463 const struct elf_backend_data *bed1, *bed2;
8464 Elf_Internal_Shdr *hdr1, *hdr2;
8465 size_t symcount1, symcount2;
8466 Elf_Internal_Sym *isymbuf1, *isymbuf2;
8467 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
8468 Elf_Internal_Sym *isym, *isymend;
8469 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
8470 size_t count1, count2, sec_count1, sec_count2, i;
8471 unsigned int shndx1, shndx2;
8472 bool result;
8473 bool ignore_section_symbol_p;
8475 bfd1 = sec1->owner;
8476 bfd2 = sec2->owner;
8478 /* Both sections have to be in ELF. */
8479 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
8480 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
8481 return false;
8483 if (elf_section_type (sec1) != elf_section_type (sec2))
8484 return false;
8486 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
8487 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
8488 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
8489 return false;
8491 bed1 = get_elf_backend_data (bfd1);
8492 bed2 = get_elf_backend_data (bfd2);
8493 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
8494 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
8495 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
8496 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
8498 if (symcount1 == 0 || symcount2 == 0)
8499 return false;
8501 result = false;
8502 isymbuf1 = NULL;
8503 isymbuf2 = NULL;
8504 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
8505 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
8507 /* Ignore section symbols only when matching non-debugging sections
8508 or linkonce section with comdat section. */
8509 ignore_section_symbol_p
8510 = ((sec1->flags & SEC_DEBUGGING) == 0
8511 || ((elf_section_flags (sec1) & SHF_GROUP)
8512 != (elf_section_flags (sec2) & SHF_GROUP)));
8514 if (ssymbuf1 == NULL)
8516 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
8517 NULL, NULL, NULL);
8518 if (isymbuf1 == NULL)
8519 goto done;
8521 if (info != NULL && !info->reduce_memory_overheads)
8523 ssymbuf1 = elf_create_symbuf (symcount1, isymbuf1);
8524 elf_tdata (bfd1)->symbuf = ssymbuf1;
8528 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
8530 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
8531 NULL, NULL, NULL);
8532 if (isymbuf2 == NULL)
8533 goto done;
8535 if (ssymbuf1 != NULL && info != NULL && !info->reduce_memory_overheads)
8537 ssymbuf2 = elf_create_symbuf (symcount2, isymbuf2);
8538 elf_tdata (bfd2)->symbuf = ssymbuf2;
8542 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
8544 /* Optimized faster version. */
8545 size_t lo, hi, mid;
8546 struct elf_symbol *symp;
8547 struct elf_symbuf_symbol *ssym, *ssymend;
8549 lo = 0;
8550 hi = ssymbuf1->count;
8551 ssymbuf1++;
8552 count1 = 0;
8553 sec_count1 = 0;
8554 while (lo < hi)
8556 mid = (lo + hi) / 2;
8557 if (shndx1 < ssymbuf1[mid].st_shndx)
8558 hi = mid;
8559 else if (shndx1 > ssymbuf1[mid].st_shndx)
8560 lo = mid + 1;
8561 else
8563 count1 = ssymbuf1[mid].count;
8564 ssymbuf1 += mid;
8565 break;
8568 if (ignore_section_symbol_p)
8570 for (i = 0; i < count1; i++)
8571 if (ELF_ST_TYPE (ssymbuf1->ssym[i].st_info) == STT_SECTION)
8572 sec_count1++;
8573 count1 -= sec_count1;
8576 lo = 0;
8577 hi = ssymbuf2->count;
8578 ssymbuf2++;
8579 count2 = 0;
8580 sec_count2 = 0;
8581 while (lo < hi)
8583 mid = (lo + hi) / 2;
8584 if (shndx2 < ssymbuf2[mid].st_shndx)
8585 hi = mid;
8586 else if (shndx2 > ssymbuf2[mid].st_shndx)
8587 lo = mid + 1;
8588 else
8590 count2 = ssymbuf2[mid].count;
8591 ssymbuf2 += mid;
8592 break;
8595 if (ignore_section_symbol_p)
8597 for (i = 0; i < count2; i++)
8598 if (ELF_ST_TYPE (ssymbuf2->ssym[i].st_info) == STT_SECTION)
8599 sec_count2++;
8600 count2 -= sec_count2;
8603 if (count1 == 0 || count2 == 0 || count1 != count2)
8604 goto done;
8606 symtable1
8607 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
8608 symtable2
8609 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
8610 if (symtable1 == NULL || symtable2 == NULL)
8611 goto done;
8613 symp = symtable1;
8614 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1 + sec_count1;
8615 ssym < ssymend; ssym++)
8616 if (sec_count1 == 0
8617 || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
8619 symp->u.ssym = ssym;
8620 symp->name = bfd_elf_string_from_elf_section (bfd1,
8621 hdr1->sh_link,
8622 ssym->st_name);
8623 symp++;
8626 symp = symtable2;
8627 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2 + sec_count2;
8628 ssym < ssymend; ssym++)
8629 if (sec_count2 == 0
8630 || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
8632 symp->u.ssym = ssym;
8633 symp->name = bfd_elf_string_from_elf_section (bfd2,
8634 hdr2->sh_link,
8635 ssym->st_name);
8636 symp++;
8639 /* Sort symbol by name. */
8640 qsort (symtable1, count1, sizeof (struct elf_symbol),
8641 elf_sym_name_compare);
8642 qsort (symtable2, count1, sizeof (struct elf_symbol),
8643 elf_sym_name_compare);
8645 for (i = 0; i < count1; i++)
8646 /* Two symbols must have the same binding, type and name. */
8647 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8648 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8649 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8650 goto done;
8652 result = true;
8653 goto done;
8656 symtable1 = (struct elf_symbol *)
8657 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8658 symtable2 = (struct elf_symbol *)
8659 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
8660 if (symtable1 == NULL || symtable2 == NULL)
8661 goto done;
8663 /* Count definitions in the section. */
8664 count1 = 0;
8665 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
8666 if (isym->st_shndx == shndx1
8667 && (!ignore_section_symbol_p
8668 || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
8669 symtable1[count1++].u.isym = isym;
8671 count2 = 0;
8672 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
8673 if (isym->st_shndx == shndx2
8674 && (!ignore_section_symbol_p
8675 || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
8676 symtable2[count2++].u.isym = isym;
8678 if (count1 == 0 || count2 == 0 || count1 != count2)
8679 goto done;
8681 for (i = 0; i < count1; i++)
8682 symtable1[i].name
8683 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8684 symtable1[i].u.isym->st_name);
8686 for (i = 0; i < count2; i++)
8687 symtable2[i].name
8688 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8689 symtable2[i].u.isym->st_name);
8691 /* Sort symbol by name. */
8692 qsort (symtable1, count1, sizeof (struct elf_symbol),
8693 elf_sym_name_compare);
8694 qsort (symtable2, count1, sizeof (struct elf_symbol),
8695 elf_sym_name_compare);
8697 for (i = 0; i < count1; i++)
8698 /* Two symbols must have the same binding, type and name. */
8699 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8700 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8701 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8702 goto done;
8704 result = true;
8706 done:
8707 free (symtable1);
8708 free (symtable2);
8709 free (isymbuf1);
8710 free (isymbuf2);
8712 return result;
8715 /* Return TRUE if 2 section types are compatible. */
8717 bool
8718 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8719 bfd *bbfd, const asection *bsec)
8721 if (asec == NULL
8722 || bsec == NULL
8723 || abfd->xvec->flavour != bfd_target_elf_flavour
8724 || bbfd->xvec->flavour != bfd_target_elf_flavour)
8725 return true;
8727 return elf_section_type (asec) == elf_section_type (bsec);
8730 /* Final phase of ELF linker. */
8732 /* A structure we use to avoid passing large numbers of arguments. */
8734 struct elf_final_link_info
8736 /* General link information. */
8737 struct bfd_link_info *info;
8738 /* Output BFD. */
8739 bfd *output_bfd;
8740 /* Symbol string table. */
8741 struct elf_strtab_hash *symstrtab;
8742 /* .hash section. */
8743 asection *hash_sec;
8744 /* symbol version section (.gnu.version). */
8745 asection *symver_sec;
8746 /* Buffer large enough to hold contents of any section. */
8747 bfd_byte *contents;
8748 /* Buffer large enough to hold external relocs of any section. */
8749 void *external_relocs;
8750 /* Buffer large enough to hold internal relocs of any section. */
8751 Elf_Internal_Rela *internal_relocs;
8752 /* Buffer large enough to hold external local symbols of any input
8753 BFD. */
8754 bfd_byte *external_syms;
8755 /* And a buffer for symbol section indices. */
8756 Elf_External_Sym_Shndx *locsym_shndx;
8757 /* Buffer large enough to hold internal local symbols of any input
8758 BFD. */
8759 Elf_Internal_Sym *internal_syms;
8760 /* Array large enough to hold a symbol index for each local symbol
8761 of any input BFD. */
8762 long *indices;
8763 /* Array large enough to hold a section pointer for each local
8764 symbol of any input BFD. */
8765 asection **sections;
8766 /* Buffer for SHT_SYMTAB_SHNDX section. */
8767 Elf_External_Sym_Shndx *symshndxbuf;
8768 /* Number of STT_FILE syms seen. */
8769 size_t filesym_count;
8770 /* Local symbol hash table. */
8771 struct bfd_hash_table local_hash_table;
8774 struct local_hash_entry
8776 /* Base hash table entry structure. */
8777 struct bfd_hash_entry root;
8778 /* Size of the local symbol name. */
8779 size_t size;
8780 /* Number of the duplicated local symbol names. */
8781 long count;
8784 /* Create an entry in the local symbol hash table. */
8786 static struct bfd_hash_entry *
8787 local_hash_newfunc (struct bfd_hash_entry *entry,
8788 struct bfd_hash_table *table,
8789 const char *string)
8792 /* Allocate the structure if it has not already been allocated by a
8793 subclass. */
8794 if (entry == NULL)
8796 entry = bfd_hash_allocate (table,
8797 sizeof (struct local_hash_entry));
8798 if (entry == NULL)
8799 return entry;
8802 /* Call the allocation method of the superclass. */
8803 entry = bfd_hash_newfunc (entry, table, string);
8804 if (entry != NULL)
8806 ((struct local_hash_entry *) entry)->count = 0;
8807 ((struct local_hash_entry *) entry)->size = 0;
8810 return entry;
8813 /* This struct is used to pass information to elf_link_output_extsym. */
8815 struct elf_outext_info
8817 bool failed;
8818 bool localsyms;
8819 bool file_sym_done;
8820 struct elf_final_link_info *flinfo;
8824 /* Support for evaluating a complex relocation.
8826 Complex relocations are generalized, self-describing relocations. The
8827 implementation of them consists of two parts: complex symbols, and the
8828 relocations themselves.
8830 The relocations use a reserved elf-wide relocation type code (R_RELC
8831 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8832 information (start bit, end bit, word width, etc) into the addend. This
8833 information is extracted from CGEN-generated operand tables within gas.
8835 Complex symbols are mangled symbols (STT_RELC external / BSF_RELC
8836 internal) representing prefix-notation expressions, including but not
8837 limited to those sorts of expressions normally encoded as addends in the
8838 addend field. The symbol mangling format is:
8840 <node> := <literal>
8841 | <unary-operator> ':' <node>
8842 | <binary-operator> ':' <node> ':' <node>
8845 <literal> := 's' <digits=N> ':' <N character symbol name>
8846 | 'S' <digits=N> ':' <N character section name>
8847 | '#' <hexdigits>
8850 <binary-operator> := as in C
8851 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8853 static void
8854 set_symbol_value (bfd *bfd_with_globals,
8855 Elf_Internal_Sym *isymbuf,
8856 size_t locsymcount,
8857 size_t symidx,
8858 bfd_vma val)
8860 struct elf_link_hash_entry **sym_hashes;
8861 struct elf_link_hash_entry *h;
8862 size_t extsymoff = locsymcount;
8864 if (symidx < locsymcount)
8866 Elf_Internal_Sym *sym;
8868 sym = isymbuf + symidx;
8869 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8871 /* It is a local symbol: move it to the
8872 "absolute" section and give it a value. */
8873 sym->st_shndx = SHN_ABS;
8874 sym->st_value = val;
8875 return;
8877 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8878 extsymoff = 0;
8881 /* It is a global symbol: set its link type
8882 to "defined" and give it a value. */
8884 sym_hashes = elf_sym_hashes (bfd_with_globals);
8885 h = sym_hashes [symidx - extsymoff];
8886 while (h->root.type == bfd_link_hash_indirect
8887 || h->root.type == bfd_link_hash_warning)
8888 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8889 h->root.type = bfd_link_hash_defined;
8890 h->root.u.def.value = val;
8891 h->root.u.def.section = bfd_abs_section_ptr;
8894 static bool
8895 resolve_symbol (const char *name,
8896 bfd *input_bfd,
8897 struct elf_final_link_info *flinfo,
8898 bfd_vma *result,
8899 Elf_Internal_Sym *isymbuf,
8900 size_t locsymcount)
8902 Elf_Internal_Sym *sym;
8903 struct bfd_link_hash_entry *global_entry;
8904 const char *candidate = NULL;
8905 Elf_Internal_Shdr *symtab_hdr;
8906 size_t i;
8908 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8910 for (i = 0; i < locsymcount; ++ i)
8912 sym = isymbuf + i;
8914 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8915 continue;
8917 candidate = bfd_elf_string_from_elf_section (input_bfd,
8918 symtab_hdr->sh_link,
8919 sym->st_name);
8920 #ifdef DEBUG
8921 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8922 name, candidate, (unsigned long) sym->st_value);
8923 #endif
8924 if (candidate && strcmp (candidate, name) == 0)
8926 asection *sec = flinfo->sections [i];
8928 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8929 *result += sec->output_offset + sec->output_section->vma;
8930 #ifdef DEBUG
8931 printf ("Found symbol with value %8.8lx\n",
8932 (unsigned long) *result);
8933 #endif
8934 return true;
8938 /* Hmm, haven't found it yet. perhaps it is a global. */
8939 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
8940 false, false, true);
8941 if (!global_entry)
8942 return false;
8944 if (global_entry->type == bfd_link_hash_defined
8945 || global_entry->type == bfd_link_hash_defweak)
8947 *result = (global_entry->u.def.value
8948 + global_entry->u.def.section->output_section->vma
8949 + global_entry->u.def.section->output_offset);
8950 #ifdef DEBUG
8951 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8952 global_entry->root.string, (unsigned long) *result);
8953 #endif
8954 return true;
8957 return false;
8960 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8961 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8962 names like "foo.end" which is the end address of section "foo". */
8964 static bool
8965 resolve_section (const char *name,
8966 asection *sections,
8967 bfd_vma *result,
8968 bfd * abfd)
8970 asection *curr;
8971 unsigned int len;
8973 for (curr = sections; curr; curr = curr->next)
8974 if (strcmp (curr->name, name) == 0)
8976 *result = curr->vma;
8977 return true;
8980 /* Hmm. still haven't found it. try pseudo-section names. */
8981 /* FIXME: This could be coded more efficiently... */
8982 for (curr = sections; curr; curr = curr->next)
8984 len = strlen (curr->name);
8985 if (len > strlen (name))
8986 continue;
8988 if (strncmp (curr->name, name, len) == 0)
8990 if (startswith (name + len, ".end"))
8992 *result = (curr->vma
8993 + curr->size / bfd_octets_per_byte (abfd, curr));
8994 return true;
8997 /* Insert more pseudo-section names here, if you like. */
9001 return false;
9004 static void
9005 undefined_reference (const char *reftype, const char *name)
9007 /* xgettext:c-format */
9008 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
9009 reftype, name);
9010 bfd_set_error (bfd_error_bad_value);
9013 static bool
9014 eval_symbol (bfd_vma *result,
9015 const char **symp,
9016 bfd *input_bfd,
9017 struct elf_final_link_info *flinfo,
9018 bfd_vma dot,
9019 Elf_Internal_Sym *isymbuf,
9020 size_t locsymcount,
9021 int signed_p)
9023 size_t len;
9024 size_t symlen;
9025 bfd_vma a;
9026 bfd_vma b;
9027 char symbuf[4096];
9028 const char *sym = *symp;
9029 const char *symend;
9030 bool symbol_is_section = false;
9032 len = strlen (sym);
9033 symend = sym + len;
9035 if (len < 1 || len > sizeof (symbuf))
9037 bfd_set_error (bfd_error_invalid_operation);
9038 return false;
9041 switch (* sym)
9043 case '.':
9044 *result = dot;
9045 *symp = sym + 1;
9046 return true;
9048 case '#':
9049 ++sym;
9050 *result = strtoul (sym, (char **) symp, 16);
9051 return true;
9053 case 'S':
9054 symbol_is_section = true;
9055 /* Fall through. */
9056 case 's':
9057 ++sym;
9058 symlen = strtol (sym, (char **) symp, 10);
9059 sym = *symp + 1; /* Skip the trailing ':'. */
9061 if (symend < sym || symlen + 1 > sizeof (symbuf))
9063 bfd_set_error (bfd_error_invalid_operation);
9064 return false;
9067 memcpy (symbuf, sym, symlen);
9068 symbuf[symlen] = '\0';
9069 *symp = sym + symlen;
9071 /* Is it always possible, with complex symbols, that gas "mis-guessed"
9072 the symbol as a section, or vice-versa. so we're pretty liberal in our
9073 interpretation here; section means "try section first", not "must be a
9074 section", and likewise with symbol. */
9076 if (symbol_is_section)
9078 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
9079 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
9080 isymbuf, locsymcount))
9082 undefined_reference ("section", symbuf);
9083 return false;
9086 else
9088 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
9089 isymbuf, locsymcount)
9090 && !resolve_section (symbuf, flinfo->output_bfd->sections,
9091 result, input_bfd))
9093 undefined_reference ("symbol", symbuf);
9094 return false;
9098 return true;
9100 /* All that remains are operators. */
9102 #define UNARY_OP(op) \
9103 if (startswith (sym, #op)) \
9105 sym += strlen (#op); \
9106 if (*sym == ':') \
9107 ++sym; \
9108 *symp = sym; \
9109 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
9110 isymbuf, locsymcount, signed_p)) \
9111 return false; \
9112 if (signed_p) \
9113 *result = op ((bfd_signed_vma) a); \
9114 else \
9115 *result = op a; \
9116 return true; \
9119 #define BINARY_OP_HEAD(op) \
9120 if (startswith (sym, #op)) \
9122 sym += strlen (#op); \
9123 if (*sym == ':') \
9124 ++sym; \
9125 *symp = sym; \
9126 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
9127 isymbuf, locsymcount, signed_p)) \
9128 return false; \
9129 ++*symp; \
9130 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
9131 isymbuf, locsymcount, signed_p)) \
9132 return false;
9133 #define BINARY_OP_TAIL(op) \
9134 if (signed_p) \
9135 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
9136 else \
9137 *result = a op b; \
9138 return true; \
9140 #define BINARY_OP(op) BINARY_OP_HEAD(op) BINARY_OP_TAIL(op)
9142 default:
9143 UNARY_OP (0-);
9144 BINARY_OP_HEAD (<<);
9145 if (b >= sizeof (a) * CHAR_BIT)
9147 *result = 0;
9148 return true;
9150 signed_p = 0;
9151 BINARY_OP_TAIL (<<);
9152 BINARY_OP_HEAD (>>);
9153 if (b >= sizeof (a) * CHAR_BIT)
9155 *result = signed_p && (bfd_signed_vma) a < 0 ? -1 : 0;
9156 return true;
9158 BINARY_OP_TAIL (>>);
9159 BINARY_OP (==);
9160 BINARY_OP (!=);
9161 BINARY_OP (<=);
9162 BINARY_OP (>=);
9163 BINARY_OP (&&);
9164 BINARY_OP (||);
9165 UNARY_OP (~);
9166 UNARY_OP (!);
9167 BINARY_OP (*);
9168 BINARY_OP_HEAD (/);
9169 if (b == 0)
9171 _bfd_error_handler (_("division by zero"));
9172 bfd_set_error (bfd_error_bad_value);
9173 return false;
9175 BINARY_OP_TAIL (/);
9176 BINARY_OP_HEAD (%);
9177 if (b == 0)
9179 _bfd_error_handler (_("division by zero"));
9180 bfd_set_error (bfd_error_bad_value);
9181 return false;
9183 BINARY_OP_TAIL (%);
9184 BINARY_OP (^);
9185 BINARY_OP (|);
9186 BINARY_OP (&);
9187 BINARY_OP (+);
9188 BINARY_OP (-);
9189 BINARY_OP (<);
9190 BINARY_OP (>);
9191 #undef UNARY_OP
9192 #undef BINARY_OP
9193 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
9194 bfd_set_error (bfd_error_invalid_operation);
9195 return false;
9199 static void
9200 put_value (bfd_vma size,
9201 unsigned long chunksz,
9202 bfd *input_bfd,
9203 bfd_vma x,
9204 bfd_byte *location)
9206 location += (size - chunksz);
9208 for (; size; size -= chunksz, location -= chunksz)
9210 switch (chunksz)
9212 case 1:
9213 bfd_put_8 (input_bfd, x, location);
9214 x >>= 8;
9215 break;
9216 case 2:
9217 bfd_put_16 (input_bfd, x, location);
9218 x >>= 16;
9219 break;
9220 case 4:
9221 bfd_put_32 (input_bfd, x, location);
9222 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
9223 x >>= 16;
9224 x >>= 16;
9225 break;
9226 #ifdef BFD64
9227 case 8:
9228 bfd_put_64 (input_bfd, x, location);
9229 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
9230 x >>= 32;
9231 x >>= 32;
9232 break;
9233 #endif
9234 default:
9235 abort ();
9236 break;
9241 static bfd_vma
9242 get_value (bfd_vma size,
9243 unsigned long chunksz,
9244 bfd *input_bfd,
9245 bfd_byte *location)
9247 int shift;
9248 bfd_vma x = 0;
9250 /* Sanity checks. */
9251 BFD_ASSERT (chunksz <= sizeof (x)
9252 && size >= chunksz
9253 && chunksz != 0
9254 && (size % chunksz) == 0
9255 && input_bfd != NULL
9256 && location != NULL);
9258 if (chunksz == sizeof (x))
9260 BFD_ASSERT (size == chunksz);
9262 /* Make sure that we do not perform an undefined shift operation.
9263 We know that size == chunksz so there will only be one iteration
9264 of the loop below. */
9265 shift = 0;
9267 else
9268 shift = 8 * chunksz;
9270 for (; size; size -= chunksz, location += chunksz)
9272 switch (chunksz)
9274 case 1:
9275 x = (x << shift) | bfd_get_8 (input_bfd, location);
9276 break;
9277 case 2:
9278 x = (x << shift) | bfd_get_16 (input_bfd, location);
9279 break;
9280 case 4:
9281 x = (x << shift) | bfd_get_32 (input_bfd, location);
9282 break;
9283 #ifdef BFD64
9284 case 8:
9285 x = (x << shift) | bfd_get_64 (input_bfd, location);
9286 break;
9287 #endif
9288 default:
9289 abort ();
9292 return x;
9295 static void
9296 decode_complex_addend (unsigned long *start, /* in bits */
9297 unsigned long *oplen, /* in bits */
9298 unsigned long *len, /* in bits */
9299 unsigned long *wordsz, /* in bytes */
9300 unsigned long *chunksz, /* in bytes */
9301 unsigned long *lsb0_p,
9302 unsigned long *signed_p,
9303 unsigned long *trunc_p,
9304 unsigned long encoded)
9306 * start = encoded & 0x3F;
9307 * len = (encoded >> 6) & 0x3F;
9308 * oplen = (encoded >> 12) & 0x3F;
9309 * wordsz = (encoded >> 18) & 0xF;
9310 * chunksz = (encoded >> 22) & 0xF;
9311 * lsb0_p = (encoded >> 27) & 1;
9312 * signed_p = (encoded >> 28) & 1;
9313 * trunc_p = (encoded >> 29) & 1;
9316 bfd_reloc_status_type
9317 bfd_elf_perform_complex_relocation (bfd *input_bfd,
9318 asection *input_section,
9319 bfd_byte *contents,
9320 Elf_Internal_Rela *rel,
9321 bfd_vma relocation)
9323 bfd_vma shift, x, mask;
9324 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
9325 bfd_reloc_status_type r;
9326 bfd_size_type octets;
9328 /* Perform this reloc, since it is complex.
9329 (this is not to say that it necessarily refers to a complex
9330 symbol; merely that it is a self-describing CGEN based reloc.
9331 i.e. the addend has the complete reloc information (bit start, end,
9332 word size, etc) encoded within it.). */
9334 decode_complex_addend (&start, &oplen, &len, &wordsz,
9335 &chunksz, &lsb0_p, &signed_p,
9336 &trunc_p, rel->r_addend);
9338 mask = (((1L << (len - 1)) - 1) << 1) | 1;
9340 if (lsb0_p)
9341 shift = (start + 1) - len;
9342 else
9343 shift = (8 * wordsz) - (start + len);
9345 octets = rel->r_offset * bfd_octets_per_byte (input_bfd, input_section);
9346 x = get_value (wordsz, chunksz, input_bfd, contents + octets);
9348 #ifdef DEBUG
9349 printf ("Doing complex reloc: "
9350 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
9351 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
9352 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
9353 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9354 oplen, (unsigned long) x, (unsigned long) mask,
9355 (unsigned long) relocation);
9356 #endif
9358 r = bfd_reloc_ok;
9359 if (! trunc_p)
9360 /* Now do an overflow check. */
9361 r = bfd_check_overflow ((signed_p
9362 ? complain_overflow_signed
9363 : complain_overflow_unsigned),
9364 len, 0, (8 * wordsz),
9365 relocation);
9367 /* Do the deed. */
9368 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
9370 #ifdef DEBUG
9371 printf (" relocation: %8.8lx\n"
9372 " shifted mask: %8.8lx\n"
9373 " shifted/masked reloc: %8.8lx\n"
9374 " result: %8.8lx\n",
9375 (unsigned long) relocation, (unsigned long) (mask << shift),
9376 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
9377 #endif
9378 put_value (wordsz, chunksz, input_bfd, x, contents + octets);
9379 return r;
9382 /* Functions to read r_offset from external (target order) reloc
9383 entry. Faster than bfd_getl32 et al, because we let the compiler
9384 know the value is aligned. */
9386 static bfd_vma
9387 ext32l_r_offset (const void *p)
9389 union aligned32
9391 uint32_t v;
9392 unsigned char c[4];
9394 const union aligned32 *a
9395 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9397 uint32_t aval = ( (uint32_t) a->c[0]
9398 | (uint32_t) a->c[1] << 8
9399 | (uint32_t) a->c[2] << 16
9400 | (uint32_t) a->c[3] << 24);
9401 return aval;
9404 static bfd_vma
9405 ext32b_r_offset (const void *p)
9407 union aligned32
9409 uint32_t v;
9410 unsigned char c[4];
9412 const union aligned32 *a
9413 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9415 uint32_t aval = ( (uint32_t) a->c[0] << 24
9416 | (uint32_t) a->c[1] << 16
9417 | (uint32_t) a->c[2] << 8
9418 | (uint32_t) a->c[3]);
9419 return aval;
9422 static bfd_vma
9423 ext64l_r_offset (const void *p)
9425 union aligned64
9427 uint64_t v;
9428 unsigned char c[8];
9430 const union aligned64 *a
9431 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9433 uint64_t aval = ( (uint64_t) a->c[0]
9434 | (uint64_t) a->c[1] << 8
9435 | (uint64_t) a->c[2] << 16
9436 | (uint64_t) a->c[3] << 24
9437 | (uint64_t) a->c[4] << 32
9438 | (uint64_t) a->c[5] << 40
9439 | (uint64_t) a->c[6] << 48
9440 | (uint64_t) a->c[7] << 56);
9441 return aval;
9444 static bfd_vma
9445 ext64b_r_offset (const void *p)
9447 union aligned64
9449 uint64_t v;
9450 unsigned char c[8];
9452 const union aligned64 *a
9453 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9455 uint64_t aval = ( (uint64_t) a->c[0] << 56
9456 | (uint64_t) a->c[1] << 48
9457 | (uint64_t) a->c[2] << 40
9458 | (uint64_t) a->c[3] << 32
9459 | (uint64_t) a->c[4] << 24
9460 | (uint64_t) a->c[5] << 16
9461 | (uint64_t) a->c[6] << 8
9462 | (uint64_t) a->c[7]);
9463 return aval;
9466 /* When performing a relocatable link, the input relocations are
9467 preserved. But, if they reference global symbols, the indices
9468 referenced must be updated. Update all the relocations found in
9469 RELDATA. */
9471 static bool
9472 elf_link_adjust_relocs (bfd *abfd,
9473 asection *sec,
9474 struct bfd_elf_section_reloc_data *reldata,
9475 bool sort,
9476 struct bfd_link_info *info)
9478 unsigned int i;
9479 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9480 bfd_byte *erela;
9481 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9482 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9483 bfd_vma r_type_mask;
9484 int r_sym_shift;
9485 unsigned int count = reldata->count;
9486 struct elf_link_hash_entry **rel_hash = reldata->hashes;
9488 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
9490 swap_in = bed->s->swap_reloc_in;
9491 swap_out = bed->s->swap_reloc_out;
9493 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
9495 swap_in = bed->s->swap_reloca_in;
9496 swap_out = bed->s->swap_reloca_out;
9498 else
9499 abort ();
9501 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
9502 abort ();
9504 if (bed->s->arch_size == 32)
9506 r_type_mask = 0xff;
9507 r_sym_shift = 8;
9509 else
9511 r_type_mask = 0xffffffff;
9512 r_sym_shift = 32;
9515 erela = reldata->hdr->contents;
9516 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
9518 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
9519 unsigned int j;
9521 if (*rel_hash == NULL)
9522 continue;
9524 if ((*rel_hash)->indx == -2
9525 && info->gc_sections
9526 && ! info->gc_keep_exported)
9528 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9529 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
9530 abfd, sec,
9531 (*rel_hash)->root.root.string);
9532 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
9533 abfd, sec);
9534 bfd_set_error (bfd_error_invalid_operation);
9535 return false;
9537 BFD_ASSERT ((*rel_hash)->indx >= 0);
9539 (*swap_in) (abfd, erela, irela);
9540 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
9541 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
9542 | (irela[j].r_info & r_type_mask));
9543 (*swap_out) (abfd, irela, erela);
9546 if (bed->elf_backend_update_relocs)
9547 (*bed->elf_backend_update_relocs) (sec, reldata);
9549 if (sort && count != 0)
9551 bfd_vma (*ext_r_off) (const void *);
9552 bfd_vma r_off;
9553 size_t elt_size;
9554 bfd_byte *base, *end, *p, *loc;
9555 bfd_byte *buf = NULL;
9557 if (bed->s->arch_size == 32)
9559 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9560 ext_r_off = ext32l_r_offset;
9561 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9562 ext_r_off = ext32b_r_offset;
9563 else
9564 abort ();
9566 else
9568 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9569 ext_r_off = ext64l_r_offset;
9570 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9571 ext_r_off = ext64b_r_offset;
9572 else
9573 abort ();
9576 /* Must use a stable sort here. A modified insertion sort,
9577 since the relocs are mostly sorted already. */
9578 elt_size = reldata->hdr->sh_entsize;
9579 base = reldata->hdr->contents;
9580 end = base + count * elt_size;
9581 if (elt_size > sizeof (Elf64_External_Rela))
9582 abort ();
9584 /* Ensure the first element is lowest. This acts as a sentinel,
9585 speeding the main loop below. */
9586 r_off = (*ext_r_off) (base);
9587 for (p = loc = base; (p += elt_size) < end; )
9589 bfd_vma r_off2 = (*ext_r_off) (p);
9590 if (r_off > r_off2)
9592 r_off = r_off2;
9593 loc = p;
9596 if (loc != base)
9598 /* Don't just swap *base and *loc as that changes the order
9599 of the original base[0] and base[1] if they happen to
9600 have the same r_offset. */
9601 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
9602 memcpy (onebuf, loc, elt_size);
9603 memmove (base + elt_size, base, loc - base);
9604 memcpy (base, onebuf, elt_size);
9607 for (p = base + elt_size; (p += elt_size) < end; )
9609 /* base to p is sorted, *p is next to insert. */
9610 r_off = (*ext_r_off) (p);
9611 /* Search the sorted region for location to insert. */
9612 loc = p - elt_size;
9613 while (r_off < (*ext_r_off) (loc))
9614 loc -= elt_size;
9615 loc += elt_size;
9616 if (loc != p)
9618 /* Chances are there is a run of relocs to insert here,
9619 from one of more input files. Files are not always
9620 linked in order due to the way elf_link_input_bfd is
9621 called. See pr17666. */
9622 size_t sortlen = p - loc;
9623 bfd_vma r_off2 = (*ext_r_off) (loc);
9624 size_t runlen = elt_size;
9625 bfd_vma r_off_runend = r_off;
9626 bfd_vma r_off_runend_next;
9627 size_t buf_size = 96 * 1024;
9628 while (p + runlen < end
9629 && (sortlen <= buf_size
9630 || runlen + elt_size <= buf_size)
9631 /* run must not break the ordering of base..loc+1 */
9632 && r_off2 > (r_off_runend_next = (*ext_r_off) (p + runlen))
9633 /* run must be already sorted */
9634 && r_off_runend_next >= r_off_runend)
9636 runlen += elt_size;
9637 r_off_runend = r_off_runend_next;
9639 if (buf == NULL)
9641 buf = bfd_malloc (buf_size);
9642 if (buf == NULL)
9643 return false;
9645 if (runlen < sortlen)
9647 memcpy (buf, p, runlen);
9648 memmove (loc + runlen, loc, sortlen);
9649 memcpy (loc, buf, runlen);
9651 else
9653 memcpy (buf, loc, sortlen);
9654 memmove (loc, p, runlen);
9655 memcpy (loc + runlen, buf, sortlen);
9657 p += runlen - elt_size;
9660 /* Hashes are no longer valid. */
9661 free (reldata->hashes);
9662 reldata->hashes = NULL;
9663 free (buf);
9665 return true;
9668 struct elf_link_sort_rela
9670 union {
9671 bfd_vma offset;
9672 bfd_vma sym_mask;
9673 } u;
9674 enum elf_reloc_type_class type;
9675 /* We use this as an array of size int_rels_per_ext_rel. */
9676 Elf_Internal_Rela rela[1];
9679 /* qsort stability here and for cmp2 is only an issue if multiple
9680 dynamic relocations are emitted at the same address. But targets
9681 that apply a series of dynamic relocations each operating on the
9682 result of the prior relocation can't use -z combreloc as
9683 implemented anyway. Such schemes tend to be broken by sorting on
9684 symbol index. That leaves dynamic NONE relocs as the only other
9685 case where ld might emit multiple relocs at the same address, and
9686 those are only emitted due to target bugs. */
9688 static int
9689 elf_link_sort_cmp1 (const void *A, const void *B)
9691 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9692 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9693 int relativea, relativeb;
9695 relativea = a->type == reloc_class_relative;
9696 relativeb = b->type == reloc_class_relative;
9698 if (relativea < relativeb)
9699 return 1;
9700 if (relativea > relativeb)
9701 return -1;
9702 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
9703 return -1;
9704 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
9705 return 1;
9706 if (a->rela->r_offset < b->rela->r_offset)
9707 return -1;
9708 if (a->rela->r_offset > b->rela->r_offset)
9709 return 1;
9710 return 0;
9713 static int
9714 elf_link_sort_cmp2 (const void *A, const void *B)
9716 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9717 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9719 if (a->type < b->type)
9720 return -1;
9721 if (a->type > b->type)
9722 return 1;
9723 if (a->u.offset < b->u.offset)
9724 return -1;
9725 if (a->u.offset > b->u.offset)
9726 return 1;
9727 if (a->rela->r_offset < b->rela->r_offset)
9728 return -1;
9729 if (a->rela->r_offset > b->rela->r_offset)
9730 return 1;
9731 return 0;
9734 static size_t
9735 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9737 asection *dynamic_relocs;
9738 asection *rela_dyn;
9739 asection *rel_dyn;
9740 bfd_size_type count, size;
9741 size_t i, ret, sort_elt, ext_size;
9742 bfd_byte *sort, *s_non_relative, *p;
9743 struct elf_link_sort_rela *sq;
9744 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9745 int i2e = bed->s->int_rels_per_ext_rel;
9746 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
9747 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9748 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9749 struct bfd_link_order *lo;
9750 bfd_vma r_sym_mask;
9751 bool use_rela;
9753 /* Find a dynamic reloc section. */
9754 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9755 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
9756 if (rela_dyn != NULL && rela_dyn->size > 0
9757 && rel_dyn != NULL && rel_dyn->size > 0)
9759 bool use_rela_initialised = false;
9761 /* This is just here to stop gcc from complaining.
9762 Its initialization checking code is not perfect. */
9763 use_rela = true;
9765 /* Both sections are present. Examine the sizes
9766 of the indirect sections to help us choose. */
9767 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9768 if (lo->type == bfd_indirect_link_order)
9770 asection *o = lo->u.indirect.section;
9772 if ((o->size % bed->s->sizeof_rela) == 0)
9774 if ((o->size % bed->s->sizeof_rel) == 0)
9775 /* Section size is divisible by both rel and rela sizes.
9776 It is of no help to us. */
9778 else
9780 /* Section size is only divisible by rela. */
9781 if (use_rela_initialised && !use_rela)
9783 _bfd_error_handler (_("%pB: unable to sort relocs - "
9784 "they are in more than one size"),
9785 abfd);
9786 bfd_set_error (bfd_error_invalid_operation);
9787 return 0;
9789 else
9791 use_rela = true;
9792 use_rela_initialised = true;
9796 else if ((o->size % bed->s->sizeof_rel) == 0)
9798 /* Section size is only divisible by rel. */
9799 if (use_rela_initialised && use_rela)
9801 _bfd_error_handler (_("%pB: unable to sort relocs - "
9802 "they are in more than one size"),
9803 abfd);
9804 bfd_set_error (bfd_error_invalid_operation);
9805 return 0;
9807 else
9809 use_rela = false;
9810 use_rela_initialised = true;
9813 else
9815 /* The section size is not divisible by either -
9816 something is wrong. */
9817 _bfd_error_handler (_("%pB: unable to sort relocs - "
9818 "they are of an unknown size"), abfd);
9819 bfd_set_error (bfd_error_invalid_operation);
9820 return 0;
9824 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9825 if (lo->type == bfd_indirect_link_order)
9827 asection *o = lo->u.indirect.section;
9829 if ((o->size % bed->s->sizeof_rela) == 0)
9831 if ((o->size % bed->s->sizeof_rel) == 0)
9832 /* Section size is divisible by both rel and rela sizes.
9833 It is of no help to us. */
9835 else
9837 /* Section size is only divisible by rela. */
9838 if (use_rela_initialised && !use_rela)
9840 _bfd_error_handler (_("%pB: unable to sort relocs - "
9841 "they are in more than one size"),
9842 abfd);
9843 bfd_set_error (bfd_error_invalid_operation);
9844 return 0;
9846 else
9848 use_rela = true;
9849 use_rela_initialised = true;
9853 else if ((o->size % bed->s->sizeof_rel) == 0)
9855 /* Section size is only divisible by rel. */
9856 if (use_rela_initialised && use_rela)
9858 _bfd_error_handler (_("%pB: unable to sort relocs - "
9859 "they are in more than one size"),
9860 abfd);
9861 bfd_set_error (bfd_error_invalid_operation);
9862 return 0;
9864 else
9866 use_rela = false;
9867 use_rela_initialised = true;
9870 else
9872 /* The section size is not divisible by either -
9873 something is wrong. */
9874 _bfd_error_handler (_("%pB: unable to sort relocs - "
9875 "they are of an unknown size"), abfd);
9876 bfd_set_error (bfd_error_invalid_operation);
9877 return 0;
9881 if (! use_rela_initialised)
9882 /* Make a guess. */
9883 use_rela = true;
9885 else if (rela_dyn != NULL && rela_dyn->size > 0)
9886 use_rela = true;
9887 else if (rel_dyn != NULL && rel_dyn->size > 0)
9888 use_rela = false;
9889 else
9890 return 0;
9892 if (use_rela)
9894 dynamic_relocs = rela_dyn;
9895 ext_size = bed->s->sizeof_rela;
9896 swap_in = bed->s->swap_reloca_in;
9897 swap_out = bed->s->swap_reloca_out;
9899 else
9901 dynamic_relocs = rel_dyn;
9902 ext_size = bed->s->sizeof_rel;
9903 swap_in = bed->s->swap_reloc_in;
9904 swap_out = bed->s->swap_reloc_out;
9907 size = 0;
9908 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9909 if (lo->type == bfd_indirect_link_order)
9910 size += lo->u.indirect.section->size;
9912 if (size != dynamic_relocs->size)
9913 return 0;
9915 sort_elt = (sizeof (struct elf_link_sort_rela)
9916 + (i2e - 1) * sizeof (Elf_Internal_Rela));
9918 count = dynamic_relocs->size / ext_size;
9919 if (count == 0)
9920 return 0;
9921 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
9923 if (sort == NULL)
9925 (*info->callbacks->warning)
9926 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
9927 return 0;
9930 if (bed->s->arch_size == 32)
9931 r_sym_mask = ~(bfd_vma) 0xff;
9932 else
9933 r_sym_mask = ~(bfd_vma) 0xffffffff;
9935 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9936 if (lo->type == bfd_indirect_link_order)
9938 bfd_byte *erel, *erelend;
9939 asection *o = lo->u.indirect.section;
9941 if (o->contents == NULL && o->size != 0)
9943 /* This is a reloc section that is being handled as a normal
9944 section. See bfd_section_from_shdr. We can't combine
9945 relocs in this case. */
9946 free (sort);
9947 return 0;
9949 erel = o->contents;
9950 erelend = o->contents + o->size;
9951 p = sort + o->output_offset * opb / ext_size * sort_elt;
9953 while (erel < erelend)
9955 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9957 (*swap_in) (abfd, erel, s->rela);
9958 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
9959 s->u.sym_mask = r_sym_mask;
9960 p += sort_elt;
9961 erel += ext_size;
9965 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9967 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9969 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9970 if (s->type != reloc_class_relative)
9971 break;
9973 ret = i;
9974 s_non_relative = p;
9976 sq = (struct elf_link_sort_rela *) s_non_relative;
9977 for (; i < count; i++, p += sort_elt)
9979 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9980 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9981 sq = sp;
9982 sp->u.offset = sq->rela->r_offset;
9985 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9987 struct elf_link_hash_table *htab = elf_hash_table (info);
9988 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9990 /* We have plt relocs in .rela.dyn. */
9991 sq = (struct elf_link_sort_rela *) sort;
9992 for (i = 0; i < count; i++)
9993 if (sq[count - i - 1].type != reloc_class_plt)
9994 break;
9995 if (i != 0 && htab->srelplt->size == i * ext_size)
9997 struct bfd_link_order **plo;
9998 /* Put srelplt link_order last. This is so the output_offset
9999 set in the next loop is correct for DT_JMPREL. */
10000 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
10001 if ((*plo)->type == bfd_indirect_link_order
10002 && (*plo)->u.indirect.section == htab->srelplt)
10004 lo = *plo;
10005 *plo = lo->next;
10007 else
10008 plo = &(*plo)->next;
10009 *plo = lo;
10010 lo->next = NULL;
10011 dynamic_relocs->map_tail.link_order = lo;
10015 p = sort;
10016 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
10017 if (lo->type == bfd_indirect_link_order)
10019 bfd_byte *erel, *erelend;
10020 asection *o = lo->u.indirect.section;
10022 erel = o->contents;
10023 erelend = o->contents + o->size;
10024 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
10025 while (erel < erelend)
10027 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
10028 (*swap_out) (abfd, s->rela, erel);
10029 p += sort_elt;
10030 erel += ext_size;
10034 free (sort);
10035 *psec = dynamic_relocs;
10036 return ret;
10039 /* Add a symbol to the output symbol string table. */
10041 static int
10042 elf_link_output_symstrtab (void *finf,
10043 const char *name,
10044 Elf_Internal_Sym *elfsym,
10045 asection *input_sec,
10046 struct elf_link_hash_entry *h)
10048 struct elf_final_link_info *flinfo = finf;
10049 int (*output_symbol_hook)
10050 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
10051 struct elf_link_hash_entry *);
10052 struct elf_link_hash_table *hash_table;
10053 const struct elf_backend_data *bed;
10054 bfd_size_type strtabsize;
10056 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
10058 bed = get_elf_backend_data (flinfo->output_bfd);
10059 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
10060 if (output_symbol_hook != NULL)
10062 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
10063 if (ret != 1)
10064 return ret;
10067 if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC)
10068 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
10069 if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE)
10070 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique;
10072 if (name == NULL || *name == '\0')
10073 elfsym->st_name = (unsigned long) -1;
10074 else
10076 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
10077 to get the final offset for st_name. */
10078 char *versioned_name = (char *) name;
10079 if (h != NULL)
10081 if (h->versioned == versioned && h->def_dynamic)
10083 /* Keep only one '@' for versioned symbols defined in
10084 shared objects. */
10085 char *version = strrchr (name, ELF_VER_CHR);
10086 char *base_end = strchr (name, ELF_VER_CHR);
10087 if (version != base_end)
10089 size_t base_len;
10090 size_t len = strlen (name);
10091 versioned_name = bfd_alloc (flinfo->output_bfd, len);
10092 if (versioned_name == NULL)
10093 return 0;
10094 base_len = base_end - name;
10095 memcpy (versioned_name, name, base_len);
10096 memcpy (versioned_name + base_len, version,
10097 len - base_len);
10101 else if (flinfo->info->unique_symbol
10102 && ELF_ST_BIND (elfsym->st_info) == STB_LOCAL)
10104 struct local_hash_entry *lh;
10105 size_t count_len;
10106 size_t base_len;
10107 char buf[30];
10108 switch (ELF_ST_TYPE (elfsym->st_info))
10110 case STT_FILE:
10111 case STT_SECTION:
10112 break;
10113 default:
10114 lh = (struct local_hash_entry *) bfd_hash_lookup
10115 (&flinfo->local_hash_table, name, true, false);
10116 if (lh == NULL)
10117 return 0;
10118 /* Always append ".COUNT" to local symbols to avoid
10119 potential conflicts with local symbol "XXX.COUNT". */
10120 sprintf (buf, "%lx", lh->count);
10121 base_len = lh->size;
10122 if (!base_len)
10124 base_len = strlen (name);
10125 lh->size = base_len;
10127 count_len = strlen (buf);
10128 versioned_name = bfd_alloc (flinfo->output_bfd,
10129 base_len + count_len + 2);
10130 if (versioned_name == NULL)
10131 return 0;
10132 memcpy (versioned_name, name, base_len);
10133 versioned_name[base_len] = '.';
10134 memcpy (versioned_name + base_len + 1, buf,
10135 count_len + 1);
10136 lh->count++;
10137 break;
10140 elfsym->st_name
10141 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
10142 versioned_name, false);
10143 if (elfsym->st_name == (unsigned long) -1)
10144 return 0;
10147 hash_table = elf_hash_table (flinfo->info);
10148 strtabsize = hash_table->strtabsize;
10149 if (strtabsize <= flinfo->output_bfd->symcount)
10151 strtabsize += strtabsize;
10152 hash_table->strtabsize = strtabsize;
10153 strtabsize *= sizeof (*hash_table->strtab);
10154 hash_table->strtab
10155 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
10156 strtabsize);
10157 if (hash_table->strtab == NULL)
10158 return 0;
10160 hash_table->strtab[flinfo->output_bfd->symcount].sym = *elfsym;
10161 hash_table->strtab[flinfo->output_bfd->symcount].dest_index
10162 = flinfo->output_bfd->symcount;
10163 flinfo->output_bfd->symcount += 1;
10165 return 1;
10168 /* Swap symbols out to the symbol table and flush the output symbols to
10169 the file. */
10171 static bool
10172 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
10174 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
10175 size_t amt;
10176 size_t i;
10177 const struct elf_backend_data *bed;
10178 bfd_byte *symbuf;
10179 Elf_Internal_Shdr *hdr;
10180 file_ptr pos;
10181 bool ret;
10183 if (flinfo->output_bfd->symcount == 0)
10184 return true;
10186 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
10188 bed = get_elf_backend_data (flinfo->output_bfd);
10190 amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount;
10191 symbuf = (bfd_byte *) bfd_malloc (amt);
10192 if (symbuf == NULL)
10193 return false;
10195 if (flinfo->symshndxbuf)
10197 amt = sizeof (Elf_External_Sym_Shndx);
10198 amt *= bfd_get_symcount (flinfo->output_bfd);
10199 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
10200 if (flinfo->symshndxbuf == NULL)
10202 free (symbuf);
10203 return false;
10207 /* Now swap out the symbols. */
10208 for (i = 0; i < flinfo->output_bfd->symcount; i++)
10210 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
10211 if (elfsym->sym.st_name == (unsigned long) -1)
10212 elfsym->sym.st_name = 0;
10213 else
10214 elfsym->sym.st_name
10215 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
10216 elfsym->sym.st_name);
10218 /* Inform the linker of the addition of this symbol. */
10220 if (flinfo->info->callbacks->ctf_new_symbol)
10221 flinfo->info->callbacks->ctf_new_symbol (elfsym->dest_index,
10222 &elfsym->sym);
10224 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
10225 ((bfd_byte *) symbuf
10226 + (elfsym->dest_index
10227 * bed->s->sizeof_sym)),
10228 NPTR_ADD (flinfo->symshndxbuf,
10229 elfsym->dest_index));
10232 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
10233 pos = hdr->sh_offset + hdr->sh_size;
10234 amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount;
10235 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
10236 && bfd_write (symbuf, amt, flinfo->output_bfd) == amt)
10238 hdr->sh_size += amt;
10239 ret = true;
10241 else
10242 ret = false;
10244 free (symbuf);
10246 free (hash_table->strtab);
10247 hash_table->strtab = NULL;
10249 return ret;
10252 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
10254 static bool
10255 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
10257 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
10258 && sym->st_shndx < SHN_LORESERVE)
10260 /* The gABI doesn't support dynamic symbols in output sections
10261 beyond 64k. */
10262 _bfd_error_handler
10263 /* xgettext:c-format */
10264 (_("%pB: too many sections: %d (>= %d)"),
10265 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
10266 bfd_set_error (bfd_error_nonrepresentable_section);
10267 return false;
10269 return true;
10272 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
10273 allowing an unsatisfied unversioned symbol in the DSO to match a
10274 versioned symbol that would normally require an explicit version.
10275 We also handle the case that a DSO references a hidden symbol
10276 which may be satisfied by a versioned symbol in another DSO. */
10278 static bool
10279 elf_link_check_versioned_symbol (struct bfd_link_info *info,
10280 const struct elf_backend_data *bed,
10281 struct elf_link_hash_entry *h)
10283 bfd *abfd;
10284 struct elf_link_loaded_list *loaded;
10286 if (!is_elf_hash_table (info->hash))
10287 return false;
10289 /* Check indirect symbol. */
10290 while (h->root.type == bfd_link_hash_indirect)
10291 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10293 switch (h->root.type)
10295 default:
10296 abfd = NULL;
10297 break;
10299 case bfd_link_hash_undefined:
10300 case bfd_link_hash_undefweak:
10301 abfd = h->root.u.undef.abfd;
10302 if (abfd == NULL
10303 || (abfd->flags & DYNAMIC) == 0
10304 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
10305 return false;
10306 break;
10308 case bfd_link_hash_defined:
10309 case bfd_link_hash_defweak:
10310 abfd = h->root.u.def.section->owner;
10311 break;
10313 case bfd_link_hash_common:
10314 abfd = h->root.u.c.p->section->owner;
10315 break;
10317 BFD_ASSERT (abfd != NULL);
10319 for (loaded = elf_hash_table (info)->dyn_loaded;
10320 loaded != NULL;
10321 loaded = loaded->next)
10323 bfd *input;
10324 Elf_Internal_Shdr *hdr;
10325 size_t symcount;
10326 size_t extsymcount;
10327 size_t extsymoff;
10328 Elf_Internal_Shdr *versymhdr;
10329 Elf_Internal_Sym *isym;
10330 Elf_Internal_Sym *isymend;
10331 Elf_Internal_Sym *isymbuf;
10332 Elf_External_Versym *ever;
10333 Elf_External_Versym *extversym;
10335 input = loaded->abfd;
10337 /* We check each DSO for a possible hidden versioned definition. */
10338 if (input == abfd
10339 || elf_dynversym (input) == 0)
10340 continue;
10342 hdr = &elf_tdata (input)->dynsymtab_hdr;
10344 symcount = hdr->sh_size / bed->s->sizeof_sym;
10345 if (elf_bad_symtab (input))
10347 extsymcount = symcount;
10348 extsymoff = 0;
10350 else
10352 extsymcount = symcount - hdr->sh_info;
10353 extsymoff = hdr->sh_info;
10356 if (extsymcount == 0)
10357 continue;
10359 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
10360 NULL, NULL, NULL);
10361 if (isymbuf == NULL)
10362 return false;
10364 /* Read in any version definitions. */
10365 versymhdr = &elf_tdata (input)->dynversym_hdr;
10366 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
10367 || (extversym = (Elf_External_Versym *)
10368 _bfd_malloc_and_read (input, versymhdr->sh_size,
10369 versymhdr->sh_size)) == NULL)
10371 free (isymbuf);
10372 return false;
10375 ever = extversym + extsymoff;
10376 isymend = isymbuf + extsymcount;
10377 for (isym = isymbuf; isym < isymend; isym++, ever++)
10379 const char *name;
10380 Elf_Internal_Versym iver;
10381 unsigned short version_index;
10383 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
10384 || isym->st_shndx == SHN_UNDEF)
10385 continue;
10387 name = bfd_elf_string_from_elf_section (input,
10388 hdr->sh_link,
10389 isym->st_name);
10390 if (strcmp (name, h->root.root.string) != 0)
10391 continue;
10393 _bfd_elf_swap_versym_in (input, ever, &iver);
10395 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
10396 && !(h->def_regular
10397 && h->forced_local))
10399 /* If we have a non-hidden versioned sym, then it should
10400 have provided a definition for the undefined sym unless
10401 it is defined in a non-shared object and forced local.
10403 abort ();
10406 version_index = iver.vs_vers & VERSYM_VERSION;
10407 if (version_index == 1 || version_index == 2)
10409 /* This is the base or first version. We can use it. */
10410 free (extversym);
10411 free (isymbuf);
10412 return true;
10416 free (extversym);
10417 free (isymbuf);
10420 return false;
10423 /* Convert ELF common symbol TYPE. */
10425 static int
10426 elf_link_convert_common_type (struct bfd_link_info *info, int type)
10428 /* Commom symbol can only appear in relocatable link. */
10429 if (!bfd_link_relocatable (info))
10430 abort ();
10431 switch (info->elf_stt_common)
10433 case unchanged:
10434 break;
10435 case elf_stt_common:
10436 type = STT_COMMON;
10437 break;
10438 case no_elf_stt_common:
10439 type = STT_OBJECT;
10440 break;
10442 return type;
10445 /* Add an external symbol to the symbol table. This is called from
10446 the hash table traversal routine. When generating a shared object,
10447 we go through the symbol table twice. The first time we output
10448 anything that might have been forced to local scope in a version
10449 script. The second time we output the symbols that are still
10450 global symbols. */
10452 static bool
10453 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
10455 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
10456 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
10457 struct elf_final_link_info *flinfo = eoinfo->flinfo;
10458 bool strip;
10459 Elf_Internal_Sym sym;
10460 asection *input_sec;
10461 const struct elf_backend_data *bed;
10462 long indx;
10463 int ret;
10464 unsigned int type;
10466 if (h->root.type == bfd_link_hash_warning)
10468 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10469 if (h->root.type == bfd_link_hash_new)
10470 return true;
10473 /* Decide whether to output this symbol in this pass. */
10474 if (eoinfo->localsyms)
10476 if (!h->forced_local)
10477 return true;
10479 else
10481 if (h->forced_local)
10482 return true;
10485 bed = get_elf_backend_data (flinfo->output_bfd);
10487 if (h->root.type == bfd_link_hash_undefined)
10489 /* If we have an undefined symbol reference here then it must have
10490 come from a shared library that is being linked in. (Undefined
10491 references in regular files have already been handled unless
10492 they are in unreferenced sections which are removed by garbage
10493 collection). */
10494 bool ignore_undef = false;
10496 /* Some symbols may be special in that the fact that they're
10497 undefined can be safely ignored - let backend determine that. */
10498 if (bed->elf_backend_ignore_undef_symbol)
10499 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
10501 /* If we are reporting errors for this situation then do so now. */
10502 if (!ignore_undef
10503 && h->ref_dynamic_nonweak
10504 && (!h->ref_regular || flinfo->info->gc_sections)
10505 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
10506 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
10508 flinfo->info->callbacks->undefined_symbol
10509 (flinfo->info, h->root.root.string,
10510 h->ref_regular ? NULL : h->root.u.undef.abfd, NULL, 0,
10511 flinfo->info->unresolved_syms_in_shared_libs == RM_DIAGNOSE
10512 && !flinfo->info->warn_unresolved_syms);
10515 /* Strip a global symbol defined in a discarded section. */
10516 if (h->indx == -3)
10517 return true;
10520 /* We should also warn if a forced local symbol is referenced from
10521 shared libraries. */
10522 if (bfd_link_executable (flinfo->info)
10523 && h->forced_local
10524 && h->ref_dynamic
10525 && h->def_regular
10526 && !h->dynamic_def
10527 && h->ref_dynamic_nonweak
10528 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
10530 bfd *def_bfd;
10531 const char *msg;
10532 struct elf_link_hash_entry *hi = h;
10534 /* Check indirect symbol. */
10535 while (hi->root.type == bfd_link_hash_indirect)
10536 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
10538 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
10539 /* xgettext:c-format */
10540 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
10541 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
10542 /* xgettext:c-format */
10543 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
10544 else
10545 /* xgettext:c-format */
10546 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
10547 def_bfd = flinfo->output_bfd;
10548 if (hi->root.u.def.section != bfd_abs_section_ptr)
10549 def_bfd = hi->root.u.def.section->owner;
10550 _bfd_error_handler (msg, flinfo->output_bfd,
10551 h->root.root.string, def_bfd);
10552 bfd_set_error (bfd_error_bad_value);
10553 eoinfo->failed = true;
10554 return false;
10557 /* We don't want to output symbols that have never been mentioned by
10558 a regular file, or that we have been told to strip. However, if
10559 h->indx is set to -2, the symbol is used by a reloc and we must
10560 output it. */
10561 strip = false;
10562 if (h->indx == -2)
10564 else if ((h->def_dynamic
10565 || h->ref_dynamic
10566 || h->root.type == bfd_link_hash_new)
10567 && !h->def_regular
10568 && !h->ref_regular)
10569 strip = true;
10570 else if (flinfo->info->strip == strip_all)
10571 strip = true;
10572 else if (flinfo->info->strip == strip_some
10573 && bfd_hash_lookup (flinfo->info->keep_hash,
10574 h->root.root.string, false, false) == NULL)
10575 strip = true;
10576 else if ((h->root.type == bfd_link_hash_defined
10577 || h->root.type == bfd_link_hash_defweak)
10578 && ((flinfo->info->strip_discarded
10579 && discarded_section (h->root.u.def.section))
10580 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
10581 && h->root.u.def.section->owner != NULL
10582 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
10583 strip = true;
10584 else if ((h->root.type == bfd_link_hash_undefined
10585 || h->root.type == bfd_link_hash_undefweak)
10586 && h->root.u.undef.abfd != NULL
10587 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
10588 strip = true;
10590 type = h->type;
10592 /* If we're stripping it, and it's not a dynamic symbol, there's
10593 nothing else to do. However, if it is a forced local symbol or
10594 an ifunc symbol we need to give the backend finish_dynamic_symbol
10595 function a chance to make it dynamic. */
10596 if (strip
10597 && h->dynindx == -1
10598 && type != STT_GNU_IFUNC
10599 && !h->forced_local)
10600 return true;
10602 sym.st_value = 0;
10603 sym.st_size = h->size;
10604 sym.st_other = h->other;
10605 switch (h->root.type)
10607 default:
10608 case bfd_link_hash_new:
10609 case bfd_link_hash_warning:
10610 abort ();
10611 return false;
10613 case bfd_link_hash_undefined:
10614 case bfd_link_hash_undefweak:
10615 input_sec = bfd_und_section_ptr;
10616 sym.st_shndx = SHN_UNDEF;
10617 break;
10619 case bfd_link_hash_defined:
10620 case bfd_link_hash_defweak:
10622 input_sec = h->root.u.def.section;
10623 if (input_sec->output_section != NULL)
10625 sym.st_shndx =
10626 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
10627 input_sec->output_section);
10628 if (sym.st_shndx == SHN_BAD)
10630 _bfd_error_handler
10631 /* xgettext:c-format */
10632 (_("%pB: could not find output section %pA for input section %pA"),
10633 flinfo->output_bfd, input_sec->output_section, input_sec);
10634 bfd_set_error (bfd_error_nonrepresentable_section);
10635 eoinfo->failed = true;
10636 return false;
10639 /* ELF symbols in relocatable files are section relative,
10640 but in nonrelocatable files they are virtual
10641 addresses. */
10642 sym.st_value = h->root.u.def.value + input_sec->output_offset;
10643 if (!bfd_link_relocatable (flinfo->info))
10645 sym.st_value += input_sec->output_section->vma;
10646 if (h->type == STT_TLS)
10648 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
10649 if (tls_sec != NULL)
10650 sym.st_value -= tls_sec->vma;
10654 else
10656 BFD_ASSERT (input_sec->owner == NULL
10657 || (input_sec->owner->flags & DYNAMIC) != 0);
10658 sym.st_shndx = SHN_UNDEF;
10659 input_sec = bfd_und_section_ptr;
10662 break;
10664 case bfd_link_hash_common:
10665 input_sec = h->root.u.c.p->section;
10666 sym.st_shndx = bed->common_section_index (input_sec);
10667 sym.st_value = 1 << h->root.u.c.p->alignment_power;
10668 break;
10670 case bfd_link_hash_indirect:
10671 /* These symbols are created by symbol versioning. They point
10672 to the decorated version of the name. For example, if the
10673 symbol foo@@GNU_1.2 is the default, which should be used when
10674 foo is used with no version, then we add an indirect symbol
10675 foo which points to foo@@GNU_1.2. We ignore these symbols,
10676 since the indirected symbol is already in the hash table. */
10677 return true;
10680 if (type == STT_COMMON || type == STT_OBJECT)
10681 switch (h->root.type)
10683 case bfd_link_hash_common:
10684 type = elf_link_convert_common_type (flinfo->info, type);
10685 break;
10686 case bfd_link_hash_defined:
10687 case bfd_link_hash_defweak:
10688 if (bed->common_definition (&sym))
10689 type = elf_link_convert_common_type (flinfo->info, type);
10690 else
10691 type = STT_OBJECT;
10692 break;
10693 case bfd_link_hash_undefined:
10694 case bfd_link_hash_undefweak:
10695 break;
10696 default:
10697 abort ();
10700 if (h->forced_local)
10702 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
10703 /* Turn off visibility on local symbol. */
10704 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
10706 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10707 else if (h->unique_global && h->def_regular)
10708 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
10709 else if (h->root.type == bfd_link_hash_undefweak
10710 || h->root.type == bfd_link_hash_defweak)
10711 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
10712 else
10713 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
10714 sym.st_target_internal = h->target_internal;
10716 /* Give the processor backend a chance to tweak the symbol value,
10717 and also to finish up anything that needs to be done for this
10718 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
10719 forced local syms when non-shared is due to a historical quirk.
10720 STT_GNU_IFUNC symbol must go through PLT. */
10721 if ((h->type == STT_GNU_IFUNC
10722 && h->def_regular
10723 && !bfd_link_relocatable (flinfo->info))
10724 || ((h->dynindx != -1
10725 || h->forced_local)
10726 && ((bfd_link_pic (flinfo->info)
10727 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
10728 || h->root.type != bfd_link_hash_undefweak))
10729 || !h->forced_local)
10730 && elf_hash_table (flinfo->info)->dynamic_sections_created))
10732 if (! ((*bed->elf_backend_finish_dynamic_symbol)
10733 (flinfo->output_bfd, flinfo->info, h, &sym)))
10735 eoinfo->failed = true;
10736 return false;
10740 /* If we are marking the symbol as undefined, and there are no
10741 non-weak references to this symbol from a regular object, then
10742 mark the symbol as weak undefined; if there are non-weak
10743 references, mark the symbol as strong. We can't do this earlier,
10744 because it might not be marked as undefined until the
10745 finish_dynamic_symbol routine gets through with it. */
10746 if (sym.st_shndx == SHN_UNDEF
10747 && h->ref_regular
10748 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
10749 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
10751 int bindtype;
10752 type = ELF_ST_TYPE (sym.st_info);
10754 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10755 if (type == STT_GNU_IFUNC)
10756 type = STT_FUNC;
10758 if (h->ref_regular_nonweak)
10759 bindtype = STB_GLOBAL;
10760 else
10761 bindtype = STB_WEAK;
10762 sym.st_info = ELF_ST_INFO (bindtype, type);
10765 /* If this is a symbol defined in a dynamic library, don't use the
10766 symbol size from the dynamic library. Relinking an executable
10767 against a new library may introduce gratuitous changes in the
10768 executable's symbols if we keep the size. */
10769 if (sym.st_shndx == SHN_UNDEF
10770 && !h->def_regular
10771 && h->def_dynamic)
10772 sym.st_size = 0;
10774 /* If a non-weak symbol with non-default visibility is not defined
10775 locally, it is a fatal error. */
10776 if (!bfd_link_relocatable (flinfo->info)
10777 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
10778 && ELF_ST_BIND (sym.st_info) != STB_WEAK
10779 && h->root.type == bfd_link_hash_undefined
10780 && !h->def_regular)
10782 const char *msg;
10784 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
10785 /* xgettext:c-format */
10786 msg = _("%pB: protected symbol `%s' isn't defined");
10787 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
10788 /* xgettext:c-format */
10789 msg = _("%pB: internal symbol `%s' isn't defined");
10790 else
10791 /* xgettext:c-format */
10792 msg = _("%pB: hidden symbol `%s' isn't defined");
10793 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
10794 bfd_set_error (bfd_error_bad_value);
10795 eoinfo->failed = true;
10796 return false;
10799 /* If this symbol should be put in the .dynsym section, then put it
10800 there now. We already know the symbol index. We also fill in
10801 the entry in the .hash section. */
10802 if (h->dynindx != -1
10803 && elf_hash_table (flinfo->info)->dynamic_sections_created
10804 && elf_hash_table (flinfo->info)->dynsym != NULL
10805 && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
10807 bfd_byte *esym;
10809 /* Since there is no version information in the dynamic string,
10810 if there is no version info in symbol version section, we will
10811 have a run-time problem if not linking executable, referenced
10812 by shared library, or not bound locally. */
10813 if (h->verinfo.verdef == NULL
10814 && (!bfd_link_executable (flinfo->info)
10815 || h->ref_dynamic
10816 || !h->def_regular))
10818 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
10820 if (p && p [1] != '\0')
10822 _bfd_error_handler
10823 /* xgettext:c-format */
10824 (_("%pB: no symbol version section for versioned symbol `%s'"),
10825 flinfo->output_bfd, h->root.root.string);
10826 eoinfo->failed = true;
10827 return false;
10831 sym.st_name = h->dynstr_index;
10832 esym = (elf_hash_table (flinfo->info)->dynsym->contents
10833 + h->dynindx * bed->s->sizeof_sym);
10834 if (!check_dynsym (flinfo->output_bfd, &sym))
10836 eoinfo->failed = true;
10837 return false;
10840 /* Inform the linker of the addition of this symbol. */
10842 if (flinfo->info->callbacks->ctf_new_dynsym)
10843 flinfo->info->callbacks->ctf_new_dynsym (h->dynindx, &sym);
10845 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
10847 if (flinfo->hash_sec != NULL)
10849 size_t hash_entry_size;
10850 bfd_byte *bucketpos;
10851 bfd_vma chain;
10852 size_t bucketcount;
10853 size_t bucket;
10855 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
10856 bucket = h->u.elf_hash_value % bucketcount;
10858 hash_entry_size
10859 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
10860 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
10861 + (bucket + 2) * hash_entry_size);
10862 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
10863 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
10864 bucketpos);
10865 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
10866 ((bfd_byte *) flinfo->hash_sec->contents
10867 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
10870 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
10872 Elf_Internal_Versym iversym;
10873 Elf_External_Versym *eversym;
10875 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
10877 if (h->verinfo.verdef == NULL
10878 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
10879 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
10880 iversym.vs_vers = 1;
10881 else
10882 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
10884 else
10886 if (h->verinfo.vertree == NULL)
10887 iversym.vs_vers = 1;
10888 else
10889 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
10890 if (flinfo->info->create_default_symver)
10891 iversym.vs_vers++;
10894 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
10895 defined locally. */
10896 if (h->versioned == versioned_hidden && h->def_regular)
10897 iversym.vs_vers |= VERSYM_HIDDEN;
10899 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
10900 eversym += h->dynindx;
10901 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
10905 /* If the symbol is undefined, and we didn't output it to .dynsym,
10906 strip it from .symtab too. Obviously we can't do this for
10907 relocatable output or when needed for --emit-relocs. */
10908 else if (input_sec == bfd_und_section_ptr
10909 && h->indx != -2
10910 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10911 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
10912 && !bfd_link_relocatable (flinfo->info))
10913 return true;
10915 /* Also strip others that we couldn't earlier due to dynamic symbol
10916 processing. */
10917 if (strip)
10918 return true;
10919 if ((input_sec->flags & SEC_EXCLUDE) != 0)
10920 return true;
10922 /* Output a FILE symbol so that following locals are not associated
10923 with the wrong input file. We need one for forced local symbols
10924 if we've seen more than one FILE symbol or when we have exactly
10925 one FILE symbol but global symbols are present in a file other
10926 than the one with the FILE symbol. We also need one if linker
10927 defined symbols are present. In practice these conditions are
10928 always met, so just emit the FILE symbol unconditionally. */
10929 if (eoinfo->localsyms
10930 && !eoinfo->file_sym_done
10931 && eoinfo->flinfo->filesym_count != 0)
10933 Elf_Internal_Sym fsym;
10935 memset (&fsym, 0, sizeof (fsym));
10936 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10937 fsym.st_shndx = SHN_ABS;
10938 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10939 bfd_und_section_ptr, NULL))
10940 return false;
10942 eoinfo->file_sym_done = true;
10945 indx = bfd_get_symcount (flinfo->output_bfd);
10946 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10947 input_sec, h);
10948 if (ret == 0)
10950 eoinfo->failed = true;
10951 return false;
10953 else if (ret == 1)
10954 h->indx = indx;
10955 else if (h->indx == -2)
10956 abort();
10958 return true;
10961 /* Return TRUE if special handling is done for relocs in SEC against
10962 symbols defined in discarded sections. */
10964 static bool
10965 elf_section_ignore_discarded_relocs (asection *sec)
10967 const struct elf_backend_data *bed;
10969 switch (sec->sec_info_type)
10971 case SEC_INFO_TYPE_STABS:
10972 case SEC_INFO_TYPE_EH_FRAME:
10973 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10974 case SEC_INFO_TYPE_SFRAME:
10975 return true;
10976 default:
10977 break;
10980 bed = get_elf_backend_data (sec->owner);
10981 if (bed->elf_backend_ignore_discarded_relocs != NULL
10982 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10983 return true;
10985 return false;
10988 /* Return a mask saying how ld should treat relocations in SEC against
10989 symbols defined in discarded sections. If this function returns
10990 COMPLAIN set, ld will issue a warning message. If this function
10991 returns PRETEND set, and the discarded section was link-once and the
10992 same size as the kept link-once section, ld will pretend that the
10993 symbol was actually defined in the kept section. Otherwise ld will
10994 zero the reloc (at least that is the intent, but some cooperation by
10995 the target dependent code is needed, particularly for REL targets). */
10997 unsigned int
10998 _bfd_elf_default_action_discarded (asection *sec)
11000 const struct elf_backend_data *bed;
11001 bed = get_elf_backend_data (sec->owner);
11003 if (sec->flags & SEC_DEBUGGING)
11004 return PRETEND;
11006 if (strcmp (".eh_frame", sec->name) == 0)
11007 return 0;
11009 if (bed->elf_backend_can_make_multiple_eh_frame
11010 && strncmp (sec->name, ".eh_frame.", 10) == 0)
11011 return 0;
11013 if (strcmp (".sframe", sec->name) == 0)
11014 return 0;
11016 if (strcmp (".gcc_except_table", sec->name) == 0)
11017 return 0;
11019 return COMPLAIN | PRETEND;
11022 /* Find a match between a section and a member of a section group. */
11024 static asection *
11025 match_group_member (asection *sec, asection *group,
11026 struct bfd_link_info *info)
11028 asection *first = elf_next_in_group (group);
11029 asection *s = first;
11031 while (s != NULL)
11033 if (bfd_elf_match_symbols_in_sections (s, sec, info))
11034 return s;
11036 s = elf_next_in_group (s);
11037 if (s == first)
11038 break;
11041 return NULL;
11044 /* Check if the kept section of a discarded section SEC can be used
11045 to replace it. Return the replacement if it is OK. Otherwise return
11046 NULL. */
11048 asection *
11049 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
11051 asection *kept;
11053 kept = sec->kept_section;
11054 if (kept != NULL)
11056 if ((kept->flags & SEC_GROUP) != 0)
11057 kept = match_group_member (sec, kept, info);
11058 if (kept != NULL)
11060 if ((sec->rawsize != 0 ? sec->rawsize : sec->size)
11061 != (kept->rawsize != 0 ? kept->rawsize : kept->size))
11062 kept = NULL;
11063 else
11065 /* Get the real kept section. */
11066 asection *next;
11067 for (next = kept->kept_section;
11068 next != NULL;
11069 next = next->kept_section)
11070 kept = next;
11073 sec->kept_section = kept;
11075 return kept;
11078 /* Link an input file into the linker output file. This function
11079 handles all the sections and relocations of the input file at once.
11080 This is so that we only have to read the local symbols once, and
11081 don't have to keep them in memory. */
11083 static bool
11084 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
11086 int (*relocate_section)
11087 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
11088 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
11089 bfd *output_bfd;
11090 Elf_Internal_Shdr *symtab_hdr;
11091 size_t locsymcount;
11092 size_t extsymoff;
11093 Elf_Internal_Sym *isymbuf;
11094 Elf_Internal_Sym *isym;
11095 Elf_Internal_Sym *isymend;
11096 long *pindex;
11097 asection **ppsection;
11098 asection *o;
11099 const struct elf_backend_data *bed;
11100 struct elf_link_hash_entry **sym_hashes;
11101 bfd_size_type address_size;
11102 bfd_vma r_type_mask;
11103 int r_sym_shift;
11104 bool have_file_sym = false;
11106 output_bfd = flinfo->output_bfd;
11107 bed = get_elf_backend_data (output_bfd);
11108 relocate_section = bed->elf_backend_relocate_section;
11110 /* If this is a dynamic object, we don't want to do anything here:
11111 we don't want the local symbols, and we don't want the section
11112 contents. */
11113 if ((input_bfd->flags & DYNAMIC) != 0)
11114 return true;
11116 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
11117 if (elf_bad_symtab (input_bfd))
11119 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
11120 extsymoff = 0;
11122 else
11124 locsymcount = symtab_hdr->sh_info;
11125 extsymoff = symtab_hdr->sh_info;
11128 /* Enable GNU OSABI features in the output BFD that are used in the input
11129 BFD. */
11130 if (bed->elf_osabi == ELFOSABI_NONE
11131 || bed->elf_osabi == ELFOSABI_GNU
11132 || bed->elf_osabi == ELFOSABI_FREEBSD)
11133 elf_tdata (output_bfd)->has_gnu_osabi
11134 |= (elf_tdata (input_bfd)->has_gnu_osabi
11135 & (bfd_link_relocatable (flinfo->info)
11136 ? -1 : ~elf_gnu_osabi_retain));
11138 /* Read the local symbols. */
11139 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
11140 if (isymbuf == NULL && locsymcount != 0)
11142 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
11143 flinfo->internal_syms,
11144 flinfo->external_syms,
11145 flinfo->locsym_shndx);
11146 if (isymbuf == NULL)
11147 return false;
11150 /* Find local symbol sections and adjust values of symbols in
11151 SEC_MERGE sections. Write out those local symbols we know are
11152 going into the output file. */
11153 isymend = PTR_ADD (isymbuf, locsymcount);
11154 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
11155 isym < isymend;
11156 isym++, pindex++, ppsection++)
11158 asection *isec;
11159 const char *name;
11160 Elf_Internal_Sym osym;
11161 long indx;
11162 int ret;
11164 *pindex = -1;
11166 if (elf_bad_symtab (input_bfd))
11168 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
11170 *ppsection = NULL;
11171 continue;
11175 if (isym->st_shndx == SHN_UNDEF)
11176 isec = bfd_und_section_ptr;
11177 else if (isym->st_shndx == SHN_ABS)
11178 isec = bfd_abs_section_ptr;
11179 else if (isym->st_shndx == SHN_COMMON)
11180 isec = bfd_com_section_ptr;
11181 else
11183 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
11184 if (isec == NULL)
11186 /* Don't attempt to output symbols with st_shnx in the
11187 reserved range other than SHN_ABS and SHN_COMMON. */
11188 isec = bfd_und_section_ptr;
11190 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
11191 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
11192 isym->st_value =
11193 _bfd_merged_section_offset (output_bfd, &isec,
11194 elf_section_data (isec)->sec_info,
11195 isym->st_value);
11198 *ppsection = isec;
11200 /* Don't output the first, undefined, symbol. In fact, don't
11201 output any undefined local symbol. */
11202 if (isec == bfd_und_section_ptr)
11203 continue;
11205 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
11207 /* We never output section symbols. Instead, we use the
11208 section symbol of the corresponding section in the output
11209 file. */
11210 continue;
11213 /* If we are stripping all symbols, we don't want to output this
11214 one. */
11215 if (flinfo->info->strip == strip_all)
11216 continue;
11218 /* If we are discarding all local symbols, we don't want to
11219 output this one. If we are generating a relocatable output
11220 file, then some of the local symbols may be required by
11221 relocs; we output them below as we discover that they are
11222 needed. */
11223 if (flinfo->info->discard == discard_all)
11224 continue;
11226 /* If this symbol is defined in a section which we are
11227 discarding, we don't need to keep it. */
11228 if (isym->st_shndx < SHN_LORESERVE
11229 && (isec->output_section == NULL
11230 || bfd_section_removed_from_list (output_bfd,
11231 isec->output_section)))
11232 continue;
11234 /* Get the name of the symbol. */
11235 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
11236 isym->st_name);
11237 if (name == NULL)
11238 return false;
11240 /* See if we are discarding symbols with this name. */
11241 if ((flinfo->info->strip == strip_some
11242 && (bfd_hash_lookup (flinfo->info->keep_hash, name, false, false)
11243 == NULL))
11244 || (((flinfo->info->discard == discard_sec_merge
11245 && (isec->flags & SEC_MERGE)
11246 && !bfd_link_relocatable (flinfo->info))
11247 || flinfo->info->discard == discard_l)
11248 && bfd_is_local_label_name (input_bfd, name)))
11249 continue;
11251 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
11253 if (input_bfd->lto_output)
11254 /* -flto puts a temp file name here. This means builds
11255 are not reproducible. Discard the symbol. */
11256 continue;
11257 have_file_sym = true;
11258 flinfo->filesym_count += 1;
11260 if (!have_file_sym)
11262 /* In the absence of debug info, bfd_find_nearest_line uses
11263 FILE symbols to determine the source file for local
11264 function symbols. Provide a FILE symbol here if input
11265 files lack such, so that their symbols won't be
11266 associated with a previous input file. It's not the
11267 source file, but the best we can do. */
11268 const char *filename;
11269 have_file_sym = true;
11270 flinfo->filesym_count += 1;
11271 memset (&osym, 0, sizeof (osym));
11272 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
11273 osym.st_shndx = SHN_ABS;
11274 if (input_bfd->lto_output)
11275 filename = NULL;
11276 else
11277 filename = lbasename (bfd_get_filename (input_bfd));
11278 if (!elf_link_output_symstrtab (flinfo, filename, &osym,
11279 bfd_abs_section_ptr, NULL))
11280 return false;
11283 osym = *isym;
11285 /* Adjust the section index for the output file. */
11286 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11287 isec->output_section);
11288 if (osym.st_shndx == SHN_BAD)
11289 return false;
11291 /* ELF symbols in relocatable files are section relative, but
11292 in executable files they are virtual addresses. Note that
11293 this code assumes that all ELF sections have an associated
11294 BFD section with a reasonable value for output_offset; below
11295 we assume that they also have a reasonable value for
11296 output_section. Any special sections must be set up to meet
11297 these requirements. */
11298 osym.st_value += isec->output_offset;
11299 if (!bfd_link_relocatable (flinfo->info))
11301 osym.st_value += isec->output_section->vma;
11302 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
11304 /* STT_TLS symbols are relative to PT_TLS segment base. */
11305 if (elf_hash_table (flinfo->info)->tls_sec != NULL)
11306 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
11307 else
11308 osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
11309 STT_NOTYPE);
11313 indx = bfd_get_symcount (output_bfd);
11314 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
11315 if (ret == 0)
11316 return false;
11317 else if (ret == 1)
11318 *pindex = indx;
11321 if (bed->s->arch_size == 32)
11323 r_type_mask = 0xff;
11324 r_sym_shift = 8;
11325 address_size = 4;
11327 else
11329 r_type_mask = 0xffffffff;
11330 r_sym_shift = 32;
11331 address_size = 8;
11334 /* Relocate the contents of each section. */
11335 sym_hashes = elf_sym_hashes (input_bfd);
11336 for (o = input_bfd->sections; o != NULL; o = o->next)
11338 bfd_byte *contents;
11340 if (! o->linker_mark)
11342 /* This section was omitted from the link. */
11343 continue;
11346 if (!flinfo->info->resolve_section_groups
11347 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
11349 /* Deal with the group signature symbol. */
11350 struct bfd_elf_section_data *sec_data = elf_section_data (o);
11351 unsigned long symndx = sec_data->this_hdr.sh_info;
11352 asection *osec = o->output_section;
11354 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
11355 if (symndx >= locsymcount
11356 || (elf_bad_symtab (input_bfd)
11357 && flinfo->sections[symndx] == NULL))
11359 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
11360 while (h->root.type == bfd_link_hash_indirect
11361 || h->root.type == bfd_link_hash_warning)
11362 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11363 /* Arrange for symbol to be output. */
11364 h->indx = -2;
11365 elf_section_data (osec)->this_hdr.sh_info = -2;
11367 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
11369 /* We'll use the output section target_index. */
11370 asection *sec = flinfo->sections[symndx]->output_section;
11371 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
11373 else
11375 if (flinfo->indices[symndx] == -1)
11377 /* Otherwise output the local symbol now. */
11378 Elf_Internal_Sym sym = isymbuf[symndx];
11379 asection *sec = flinfo->sections[symndx]->output_section;
11380 const char *name;
11381 long indx;
11382 int ret;
11384 name = bfd_elf_string_from_elf_section (input_bfd,
11385 symtab_hdr->sh_link,
11386 sym.st_name);
11387 if (name == NULL)
11388 return false;
11390 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11391 sec);
11392 if (sym.st_shndx == SHN_BAD)
11393 return false;
11395 sym.st_value += o->output_offset;
11397 indx = bfd_get_symcount (output_bfd);
11398 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
11399 NULL);
11400 if (ret == 0)
11401 return false;
11402 else if (ret == 1)
11403 flinfo->indices[symndx] = indx;
11404 else
11405 abort ();
11407 elf_section_data (osec)->this_hdr.sh_info
11408 = flinfo->indices[symndx];
11412 if ((o->flags & SEC_HAS_CONTENTS) == 0
11413 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
11414 continue;
11416 if ((o->flags & SEC_LINKER_CREATED) != 0)
11418 /* Section was created by _bfd_elf_link_create_dynamic_sections
11419 or somesuch. */
11420 continue;
11423 /* Get the contents of the section. They have been cached by a
11424 relaxation routine. Note that o is a section in an input
11425 file, so the contents field will not have been set by any of
11426 the routines which work on output files. */
11427 if (elf_section_data (o)->this_hdr.contents != NULL)
11429 contents = elf_section_data (o)->this_hdr.contents;
11430 if (bed->caches_rawsize
11431 && o->rawsize != 0
11432 && o->rawsize < o->size)
11434 memcpy (flinfo->contents, contents, o->rawsize);
11435 contents = flinfo->contents;
11438 else if (!(o->flags & SEC_RELOC)
11439 && !bed->elf_backend_write_section
11440 && o->sec_info_type == SEC_INFO_TYPE_MERGE)
11441 /* A MERGE section that has no relocations doesn't need the
11442 contents anymore, they have been recorded earlier. Except
11443 if the backend has special provisions for writing sections. */
11444 contents = NULL;
11445 else
11447 contents = flinfo->contents;
11448 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
11449 return false;
11452 if ((o->flags & SEC_RELOC) != 0)
11454 Elf_Internal_Rela *internal_relocs;
11455 Elf_Internal_Rela *rel, *relend;
11456 int action_discarded;
11457 int ret;
11459 /* Get the swapped relocs. */
11460 internal_relocs
11461 = _bfd_elf_link_info_read_relocs (input_bfd, flinfo->info, o,
11462 flinfo->external_relocs,
11463 flinfo->internal_relocs,
11464 false);
11465 if (internal_relocs == NULL
11466 && o->reloc_count > 0)
11467 return false;
11469 action_discarded = -1;
11470 if (!elf_section_ignore_discarded_relocs (o))
11471 action_discarded = (*bed->action_discarded) (o);
11473 /* Run through the relocs evaluating complex reloc symbols and
11474 looking for relocs against symbols from discarded sections
11475 or section symbols from removed link-once sections.
11476 Complain about relocs against discarded sections. Zero
11477 relocs against removed link-once sections. */
11479 rel = internal_relocs;
11480 relend = rel + o->reloc_count;
11481 for ( ; rel < relend; rel++)
11483 unsigned long r_symndx = rel->r_info >> r_sym_shift;
11484 unsigned int s_type;
11485 asection **ps, *sec;
11486 struct elf_link_hash_entry *h = NULL;
11487 const char *sym_name;
11489 if (r_symndx == STN_UNDEF)
11490 continue;
11492 if (r_symndx >= locsymcount
11493 || (elf_bad_symtab (input_bfd)
11494 && flinfo->sections[r_symndx] == NULL))
11496 h = sym_hashes[r_symndx - extsymoff];
11498 /* Badly formatted input files can contain relocs that
11499 reference non-existant symbols. Check here so that
11500 we do not seg fault. */
11501 if (h == NULL)
11503 _bfd_error_handler
11504 /* xgettext:c-format */
11505 (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
11506 "that references a non-existent global symbol"),
11507 input_bfd, (uint64_t) rel->r_info, o);
11508 bfd_set_error (bfd_error_bad_value);
11509 return false;
11512 while (h->root.type == bfd_link_hash_indirect
11513 || h->root.type == bfd_link_hash_warning)
11514 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11516 s_type = h->type;
11518 /* If a plugin symbol is referenced from a non-IR file,
11519 mark the symbol as undefined. Note that the
11520 linker may attach linker created dynamic sections
11521 to the plugin bfd. Symbols defined in linker
11522 created sections are not plugin symbols. */
11523 if ((h->root.non_ir_ref_regular
11524 || h->root.non_ir_ref_dynamic)
11525 && (h->root.type == bfd_link_hash_defined
11526 || h->root.type == bfd_link_hash_defweak)
11527 && (h->root.u.def.section->flags
11528 & SEC_LINKER_CREATED) == 0
11529 && h->root.u.def.section->owner != NULL
11530 && (h->root.u.def.section->owner->flags
11531 & BFD_PLUGIN) != 0)
11533 h->root.type = bfd_link_hash_undefined;
11534 h->root.u.undef.abfd = h->root.u.def.section->owner;
11537 ps = NULL;
11538 if (h->root.type == bfd_link_hash_defined
11539 || h->root.type == bfd_link_hash_defweak)
11540 ps = &h->root.u.def.section;
11542 sym_name = h->root.root.string;
11544 else
11546 Elf_Internal_Sym *sym = isymbuf + r_symndx;
11548 s_type = ELF_ST_TYPE (sym->st_info);
11549 ps = &flinfo->sections[r_symndx];
11550 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
11551 sym, *ps);
11554 if ((s_type == STT_RELC || s_type == STT_SRELC)
11555 && !bfd_link_relocatable (flinfo->info))
11557 bfd_vma val;
11558 bfd_vma dot = (rel->r_offset
11559 + o->output_offset + o->output_section->vma);
11560 #ifdef DEBUG
11561 printf ("Encountered a complex symbol!");
11562 printf (" (input_bfd %s, section %s, reloc %ld\n",
11563 bfd_get_filename (input_bfd), o->name,
11564 (long) (rel - internal_relocs));
11565 printf (" symbol: idx %8.8lx, name %s\n",
11566 r_symndx, sym_name);
11567 printf (" reloc : info %8.8lx, addr %8.8lx\n",
11568 (unsigned long) rel->r_info,
11569 (unsigned long) rel->r_offset);
11570 #endif
11571 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
11572 isymbuf, locsymcount, s_type == STT_SRELC))
11573 return false;
11575 /* Symbol evaluated OK. Update to absolute value. */
11576 set_symbol_value (input_bfd, isymbuf, locsymcount,
11577 r_symndx, val);
11578 continue;
11581 if (action_discarded != -1 && ps != NULL)
11583 /* Complain if the definition comes from a
11584 discarded section. */
11585 if ((sec = *ps) != NULL && discarded_section (sec))
11587 BFD_ASSERT (r_symndx != STN_UNDEF);
11588 if (action_discarded & COMPLAIN)
11589 (*flinfo->info->callbacks->einfo)
11590 /* xgettext:c-format */
11591 (_("%X`%s' referenced in section `%pA' of %pB: "
11592 "defined in discarded section `%pA' of %pB\n"),
11593 sym_name, o, input_bfd, sec, sec->owner);
11595 /* Try to do the best we can to support buggy old
11596 versions of gcc. Pretend that the symbol is
11597 really defined in the kept linkonce section.
11598 FIXME: This is quite broken. Modifying the
11599 symbol here means we will be changing all later
11600 uses of the symbol, not just in this section. */
11601 if (action_discarded & PRETEND)
11603 asection *kept;
11605 kept = _bfd_elf_check_kept_section (sec,
11606 flinfo->info);
11607 if (kept != NULL)
11609 *ps = kept;
11610 continue;
11617 /* Relocate the section by invoking a back end routine.
11619 The back end routine is responsible for adjusting the
11620 section contents as necessary, and (if using Rela relocs
11621 and generating a relocatable output file) adjusting the
11622 reloc addend as necessary.
11624 The back end routine does not have to worry about setting
11625 the reloc address or the reloc symbol index.
11627 The back end routine is given a pointer to the swapped in
11628 internal symbols, and can access the hash table entries
11629 for the external symbols via elf_sym_hashes (input_bfd).
11631 When generating relocatable output, the back end routine
11632 must handle STB_LOCAL/STT_SECTION symbols specially. The
11633 output symbol is going to be a section symbol
11634 corresponding to the output section, which will require
11635 the addend to be adjusted. */
11637 ret = (*relocate_section) (output_bfd, flinfo->info,
11638 input_bfd, o, contents,
11639 internal_relocs,
11640 isymbuf,
11641 flinfo->sections);
11642 if (!ret)
11643 return false;
11645 if (ret == 2
11646 || bfd_link_relocatable (flinfo->info)
11647 || flinfo->info->emitrelocations)
11649 Elf_Internal_Rela *irela;
11650 Elf_Internal_Rela *irelaend, *irelamid;
11651 bfd_vma last_offset;
11652 struct elf_link_hash_entry **rel_hash;
11653 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
11654 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
11655 unsigned int next_erel;
11656 bool rela_normal;
11657 struct bfd_elf_section_data *esdi, *esdo;
11659 esdi = elf_section_data (o);
11660 esdo = elf_section_data (o->output_section);
11661 rela_normal = false;
11663 /* Adjust the reloc addresses and symbol indices. */
11665 irela = internal_relocs;
11666 irelaend = irela + o->reloc_count;
11667 rel_hash = PTR_ADD (esdo->rel.hashes, esdo->rel.count);
11668 /* We start processing the REL relocs, if any. When we reach
11669 IRELAMID in the loop, we switch to the RELA relocs. */
11670 irelamid = irela;
11671 if (esdi->rel.hdr != NULL)
11672 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
11673 * bed->s->int_rels_per_ext_rel);
11674 rel_hash_list = rel_hash;
11675 rela_hash_list = NULL;
11676 last_offset = o->output_offset;
11677 if (!bfd_link_relocatable (flinfo->info))
11678 last_offset += o->output_section->vma;
11679 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
11681 unsigned long r_symndx;
11682 asection *sec;
11683 Elf_Internal_Sym sym;
11685 if (next_erel == bed->s->int_rels_per_ext_rel)
11687 rel_hash++;
11688 next_erel = 0;
11691 if (irela == irelamid)
11693 rel_hash = PTR_ADD (esdo->rela.hashes, esdo->rela.count);
11694 rela_hash_list = rel_hash;
11695 rela_normal = bed->rela_normal;
11698 irela->r_offset = _bfd_elf_section_offset (output_bfd,
11699 flinfo->info, o,
11700 irela->r_offset);
11701 if (irela->r_offset >= (bfd_vma) -2)
11703 /* This is a reloc for a deleted entry or somesuch.
11704 Turn it into an R_*_NONE reloc, at the same
11705 offset as the last reloc. elf_eh_frame.c and
11706 bfd_elf_discard_info rely on reloc offsets
11707 being ordered. */
11708 irela->r_offset = last_offset;
11709 irela->r_info = 0;
11710 irela->r_addend = 0;
11711 continue;
11714 irela->r_offset += o->output_offset;
11716 /* Relocs in an executable have to be virtual addresses. */
11717 if (!bfd_link_relocatable (flinfo->info))
11718 irela->r_offset += o->output_section->vma;
11720 last_offset = irela->r_offset;
11722 r_symndx = irela->r_info >> r_sym_shift;
11723 if (r_symndx == STN_UNDEF)
11724 continue;
11726 if (r_symndx >= locsymcount
11727 || (elf_bad_symtab (input_bfd)
11728 && flinfo->sections[r_symndx] == NULL))
11730 struct elf_link_hash_entry *rh;
11731 unsigned long indx;
11733 /* This is a reloc against a global symbol. We
11734 have not yet output all the local symbols, so
11735 we do not know the symbol index of any global
11736 symbol. We set the rel_hash entry for this
11737 reloc to point to the global hash table entry
11738 for this symbol. The symbol index is then
11739 set at the end of bfd_elf_final_link. */
11740 indx = r_symndx - extsymoff;
11741 rh = elf_sym_hashes (input_bfd)[indx];
11742 while (rh->root.type == bfd_link_hash_indirect
11743 || rh->root.type == bfd_link_hash_warning)
11744 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
11746 /* Setting the index to -2 tells
11747 elf_link_output_extsym that this symbol is
11748 used by a reloc. */
11749 BFD_ASSERT (rh->indx < 0);
11750 rh->indx = -2;
11751 *rel_hash = rh;
11753 continue;
11756 /* This is a reloc against a local symbol. */
11758 *rel_hash = NULL;
11759 sym = isymbuf[r_symndx];
11760 sec = flinfo->sections[r_symndx];
11761 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
11763 /* I suppose the backend ought to fill in the
11764 section of any STT_SECTION symbol against a
11765 processor specific section. */
11766 r_symndx = STN_UNDEF;
11767 if (bfd_is_abs_section (sec))
11769 else if (sec == NULL || sec->owner == NULL)
11771 bfd_set_error (bfd_error_bad_value);
11772 return false;
11774 else
11776 asection *osec = sec->output_section;
11778 /* If we have discarded a section, the output
11779 section will be the absolute section. In
11780 case of discarded SEC_MERGE sections, use
11781 the kept section. relocate_section should
11782 have already handled discarded linkonce
11783 sections. */
11784 if (bfd_is_abs_section (osec)
11785 && sec->kept_section != NULL
11786 && sec->kept_section->output_section != NULL)
11788 osec = sec->kept_section->output_section;
11789 irela->r_addend -= osec->vma;
11792 if (!bfd_is_abs_section (osec))
11794 r_symndx = osec->target_index;
11795 if (r_symndx == STN_UNDEF)
11797 irela->r_addend += osec->vma;
11798 osec = _bfd_nearby_section (output_bfd, osec,
11799 osec->vma);
11800 irela->r_addend -= osec->vma;
11801 r_symndx = osec->target_index;
11806 /* Adjust the addend according to where the
11807 section winds up in the output section. */
11808 if (rela_normal)
11809 irela->r_addend += sec->output_offset;
11811 else
11813 if (flinfo->indices[r_symndx] == -1)
11815 unsigned long shlink;
11816 const char *name;
11817 asection *osec;
11818 long indx;
11820 if (flinfo->info->strip == strip_all)
11822 /* You can't do ld -r -s. */
11823 bfd_set_error (bfd_error_invalid_operation);
11824 return false;
11827 /* This symbol was skipped earlier, but
11828 since it is needed by a reloc, we
11829 must output it now. */
11830 shlink = symtab_hdr->sh_link;
11831 name = (bfd_elf_string_from_elf_section
11832 (input_bfd, shlink, sym.st_name));
11833 if (name == NULL)
11834 return false;
11836 osec = sec->output_section;
11837 sym.st_shndx =
11838 _bfd_elf_section_from_bfd_section (output_bfd,
11839 osec);
11840 if (sym.st_shndx == SHN_BAD)
11841 return false;
11843 sym.st_value += sec->output_offset;
11844 if (!bfd_link_relocatable (flinfo->info))
11846 sym.st_value += osec->vma;
11847 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
11849 struct elf_link_hash_table *htab
11850 = elf_hash_table (flinfo->info);
11852 /* STT_TLS symbols are relative to PT_TLS
11853 segment base. */
11854 if (htab->tls_sec != NULL)
11855 sym.st_value -= htab->tls_sec->vma;
11856 else
11857 sym.st_info
11858 = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
11859 STT_NOTYPE);
11863 indx = bfd_get_symcount (output_bfd);
11864 ret = elf_link_output_symstrtab (flinfo, name,
11865 &sym, sec,
11866 NULL);
11867 if (ret == 0)
11868 return false;
11869 else if (ret == 1)
11870 flinfo->indices[r_symndx] = indx;
11871 else
11872 abort ();
11875 r_symndx = flinfo->indices[r_symndx];
11878 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
11879 | (irela->r_info & r_type_mask));
11882 /* Swap out the relocs. */
11883 input_rel_hdr = esdi->rel.hdr;
11884 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
11886 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11887 input_rel_hdr,
11888 internal_relocs,
11889 rel_hash_list))
11890 return false;
11891 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
11892 * bed->s->int_rels_per_ext_rel);
11893 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
11896 input_rela_hdr = esdi->rela.hdr;
11897 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
11899 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11900 input_rela_hdr,
11901 internal_relocs,
11902 rela_hash_list))
11903 return false;
11908 /* Write out the modified section contents. */
11909 if (bed->elf_backend_write_section
11910 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
11911 contents))
11913 /* Section written out. */
11915 else switch (o->sec_info_type)
11917 case SEC_INFO_TYPE_STABS:
11918 if (! (_bfd_write_section_stabs
11919 (output_bfd,
11920 &elf_hash_table (flinfo->info)->stab_info,
11921 o, &elf_section_data (o)->sec_info, contents)))
11922 return false;
11923 break;
11924 case SEC_INFO_TYPE_MERGE:
11925 if (! _bfd_write_merged_section (output_bfd, o,
11926 elf_section_data (o)->sec_info))
11927 return false;
11928 break;
11929 case SEC_INFO_TYPE_EH_FRAME:
11931 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
11932 o, contents))
11933 return false;
11935 break;
11936 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11938 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11939 flinfo->info,
11940 o, contents))
11941 return false;
11943 break;
11944 case SEC_INFO_TYPE_SFRAME:
11946 /* Merge .sframe sections into the ctf frame encoder
11947 context of the output_bfd's section. The final .sframe
11948 output section will be written out later. */
11949 if (!_bfd_elf_merge_section_sframe (output_bfd, flinfo->info,
11950 o, contents))
11951 return false;
11953 break;
11954 default:
11956 if (! (o->flags & SEC_EXCLUDE))
11958 file_ptr offset = (file_ptr) o->output_offset;
11959 bfd_size_type todo = o->size;
11961 offset *= bfd_octets_per_byte (output_bfd, o);
11963 if ((o->flags & SEC_ELF_REVERSE_COPY)
11964 && o->size > address_size)
11966 /* Reverse-copy input section to output. */
11968 if ((o->size & (address_size - 1)) != 0
11969 || (o->reloc_count != 0
11970 && (o->size * bed->s->int_rels_per_ext_rel
11971 != o->reloc_count * address_size)))
11973 _bfd_error_handler
11974 /* xgettext:c-format */
11975 (_("error: %pB: size of section %pA is not "
11976 "multiple of address size"),
11977 input_bfd, o);
11978 bfd_set_error (bfd_error_bad_value);
11979 return false;
11984 todo -= address_size;
11985 if (! bfd_set_section_contents (output_bfd,
11986 o->output_section,
11987 contents + todo,
11988 offset,
11989 address_size))
11990 return false;
11991 if (todo == 0)
11992 break;
11993 offset += address_size;
11995 while (1);
11997 else if (! bfd_set_section_contents (output_bfd,
11998 o->output_section,
11999 contents,
12000 offset, todo))
12001 return false;
12004 break;
12008 return true;
12011 /* Generate a reloc when linking an ELF file. This is a reloc
12012 requested by the linker, and does not come from any input file. This
12013 is used to build constructor and destructor tables when linking
12014 with -Ur. */
12016 static bool
12017 elf_reloc_link_order (bfd *output_bfd,
12018 struct bfd_link_info *info,
12019 asection *output_section,
12020 struct bfd_link_order *link_order)
12022 reloc_howto_type *howto;
12023 long indx;
12024 bfd_vma offset;
12025 bfd_vma addend;
12026 struct bfd_elf_section_reloc_data *reldata;
12027 struct elf_link_hash_entry **rel_hash_ptr;
12028 Elf_Internal_Shdr *rel_hdr;
12029 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
12030 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
12031 bfd_byte *erel;
12032 unsigned int i;
12033 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
12035 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
12036 if (howto == NULL)
12038 bfd_set_error (bfd_error_bad_value);
12039 return false;
12042 addend = link_order->u.reloc.p->addend;
12044 if (esdo->rel.hdr)
12045 reldata = &esdo->rel;
12046 else if (esdo->rela.hdr)
12047 reldata = &esdo->rela;
12048 else
12050 reldata = NULL;
12051 BFD_ASSERT (0);
12054 /* Figure out the symbol index. */
12055 rel_hash_ptr = reldata->hashes + reldata->count;
12056 if (link_order->type == bfd_section_reloc_link_order)
12058 indx = link_order->u.reloc.p->u.section->target_index;
12059 BFD_ASSERT (indx != 0);
12060 *rel_hash_ptr = NULL;
12062 else
12064 struct elf_link_hash_entry *h;
12066 /* Treat a reloc against a defined symbol as though it were
12067 actually against the section. */
12068 h = ((struct elf_link_hash_entry *)
12069 bfd_wrapped_link_hash_lookup (output_bfd, info,
12070 link_order->u.reloc.p->u.name,
12071 false, false, true));
12072 if (h != NULL
12073 && (h->root.type == bfd_link_hash_defined
12074 || h->root.type == bfd_link_hash_defweak))
12076 asection *section;
12078 section = h->root.u.def.section;
12079 indx = section->output_section->target_index;
12080 *rel_hash_ptr = NULL;
12081 /* It seems that we ought to add the symbol value to the
12082 addend here, but in practice it has already been added
12083 because it was passed to constructor_callback. */
12084 addend += section->output_section->vma + section->output_offset;
12086 else if (h != NULL)
12088 /* Setting the index to -2 tells elf_link_output_extsym that
12089 this symbol is used by a reloc. */
12090 h->indx = -2;
12091 *rel_hash_ptr = h;
12092 indx = 0;
12094 else
12096 (*info->callbacks->unattached_reloc)
12097 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
12098 indx = 0;
12102 /* If this is an inplace reloc, we must write the addend into the
12103 object file. */
12104 if (howto->partial_inplace && addend != 0)
12106 bfd_size_type size;
12107 bfd_reloc_status_type rstat;
12108 bfd_byte *buf;
12109 bool ok;
12110 const char *sym_name;
12111 bfd_size_type octets;
12113 size = (bfd_size_type) bfd_get_reloc_size (howto);
12114 buf = (bfd_byte *) bfd_zmalloc (size);
12115 if (buf == NULL && size != 0)
12116 return false;
12117 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
12118 switch (rstat)
12120 case bfd_reloc_ok:
12121 break;
12123 default:
12124 case bfd_reloc_outofrange:
12125 abort ();
12127 case bfd_reloc_overflow:
12128 if (link_order->type == bfd_section_reloc_link_order)
12129 sym_name = bfd_section_name (link_order->u.reloc.p->u.section);
12130 else
12131 sym_name = link_order->u.reloc.p->u.name;
12132 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
12133 howto->name, addend, NULL, NULL,
12134 (bfd_vma) 0);
12135 break;
12138 octets = link_order->offset * bfd_octets_per_byte (output_bfd,
12139 output_section);
12140 ok = bfd_set_section_contents (output_bfd, output_section, buf,
12141 octets, size);
12142 free (buf);
12143 if (! ok)
12144 return false;
12147 /* The address of a reloc is relative to the section in a
12148 relocatable file, and is a virtual address in an executable
12149 file. */
12150 offset = link_order->offset;
12151 if (! bfd_link_relocatable (info))
12152 offset += output_section->vma;
12154 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
12156 irel[i].r_offset = offset;
12157 irel[i].r_info = 0;
12158 irel[i].r_addend = 0;
12160 if (bed->s->arch_size == 32)
12161 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
12162 else
12163 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
12165 rel_hdr = reldata->hdr;
12166 erel = rel_hdr->contents;
12167 if (rel_hdr->sh_type == SHT_REL)
12169 erel += reldata->count * bed->s->sizeof_rel;
12170 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
12172 else
12174 irel[0].r_addend = addend;
12175 erel += reldata->count * bed->s->sizeof_rela;
12176 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
12179 ++reldata->count;
12181 return true;
12184 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
12185 Returns TRUE upon success, FALSE otherwise. */
12187 static bool
12188 elf_output_implib (bfd *abfd, struct bfd_link_info *info)
12190 bool ret = false;
12191 bfd *implib_bfd;
12192 const struct elf_backend_data *bed;
12193 flagword flags;
12194 enum bfd_architecture arch;
12195 unsigned int mach;
12196 asymbol **sympp = NULL;
12197 long symsize;
12198 long symcount;
12199 long src_count;
12200 elf_symbol_type *osymbuf;
12201 size_t amt;
12203 implib_bfd = info->out_implib_bfd;
12204 bed = get_elf_backend_data (abfd);
12206 if (!bfd_set_format (implib_bfd, bfd_object))
12207 return false;
12209 /* Use flag from executable but make it a relocatable object. */
12210 flags = bfd_get_file_flags (abfd);
12211 flags &= ~HAS_RELOC;
12212 if (!bfd_set_start_address (implib_bfd, 0)
12213 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
12214 return false;
12216 /* Copy architecture of output file to import library file. */
12217 arch = bfd_get_arch (abfd);
12218 mach = bfd_get_mach (abfd);
12219 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
12220 && (abfd->target_defaulted
12221 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
12222 return false;
12224 /* Get symbol table size. */
12225 symsize = bfd_get_symtab_upper_bound (abfd);
12226 if (symsize < 0)
12227 return false;
12229 /* Read in the symbol table. */
12230 sympp = (asymbol **) bfd_malloc (symsize);
12231 if (sympp == NULL)
12232 return false;
12234 symcount = bfd_canonicalize_symtab (abfd, sympp);
12235 if (symcount < 0)
12236 goto free_sym_buf;
12238 /* Allow the BFD backend to copy any private header data it
12239 understands from the output BFD to the import library BFD. */
12240 if (! bfd_copy_private_header_data (abfd, implib_bfd))
12241 goto free_sym_buf;
12243 /* Filter symbols to appear in the import library. */
12244 if (bed->elf_backend_filter_implib_symbols)
12245 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
12246 symcount);
12247 else
12248 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
12249 if (symcount == 0)
12251 bfd_set_error (bfd_error_no_symbols);
12252 _bfd_error_handler (_("%pB: no symbol found for import library"),
12253 implib_bfd);
12254 goto free_sym_buf;
12258 /* Make symbols absolute. */
12259 amt = symcount * sizeof (*osymbuf);
12260 osymbuf = (elf_symbol_type *) bfd_alloc (implib_bfd, amt);
12261 if (osymbuf == NULL)
12262 goto free_sym_buf;
12264 for (src_count = 0; src_count < symcount; src_count++)
12266 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
12267 sizeof (*osymbuf));
12268 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
12269 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
12270 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
12271 osymbuf[src_count].internal_elf_sym.st_value =
12272 osymbuf[src_count].symbol.value;
12273 sympp[src_count] = &osymbuf[src_count].symbol;
12276 bfd_set_symtab (implib_bfd, sympp, symcount);
12278 /* Allow the BFD backend to copy any private data it understands
12279 from the output BFD to the import library BFD. This is done last
12280 to permit the routine to look at the filtered symbol table. */
12281 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
12282 goto free_sym_buf;
12284 if (!bfd_close (implib_bfd))
12285 goto free_sym_buf;
12287 ret = true;
12289 free_sym_buf:
12290 free (sympp);
12291 return ret;
12294 static void
12295 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
12297 asection *o;
12299 if (flinfo->symstrtab != NULL)
12300 _bfd_elf_strtab_free (flinfo->symstrtab);
12301 free (flinfo->contents);
12302 free (flinfo->external_relocs);
12303 free (flinfo->internal_relocs);
12304 free (flinfo->external_syms);
12305 free (flinfo->locsym_shndx);
12306 free (flinfo->internal_syms);
12307 free (flinfo->indices);
12308 free (flinfo->sections);
12309 if (flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1)
12310 free (flinfo->symshndxbuf);
12311 for (o = obfd->sections; o != NULL; o = o->next)
12313 struct bfd_elf_section_data *esdo = elf_section_data (o);
12314 free (esdo->rel.hashes);
12315 free (esdo->rela.hashes);
12319 /* Do the final step of an ELF link. */
12321 bool
12322 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
12324 bool dynamic;
12325 bool emit_relocs;
12326 bfd *dynobj;
12327 struct elf_final_link_info flinfo;
12328 asection *o;
12329 struct bfd_link_order *p;
12330 bfd *sub;
12331 bfd_size_type max_contents_size;
12332 bfd_size_type max_external_reloc_size;
12333 bfd_size_type max_internal_reloc_count;
12334 bfd_size_type max_sym_count;
12335 bfd_size_type max_sym_shndx_count;
12336 Elf_Internal_Sym elfsym;
12337 unsigned int i;
12338 Elf_Internal_Shdr *symtab_hdr;
12339 Elf_Internal_Shdr *symtab_shndx_hdr;
12340 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12341 struct elf_outext_info eoinfo;
12342 bool merged;
12343 size_t relativecount;
12344 size_t relr_entsize;
12345 asection *reldyn = 0;
12346 bfd_size_type amt;
12347 asection *attr_section = NULL;
12348 bfd_vma attr_size = 0;
12349 const char *std_attrs_section;
12350 struct elf_link_hash_table *htab = elf_hash_table (info);
12351 bool sections_removed;
12352 bool ret;
12354 if (!is_elf_hash_table (&htab->root))
12355 return false;
12357 if (bfd_link_pic (info))
12358 abfd->flags |= DYNAMIC;
12360 dynamic = htab->dynamic_sections_created;
12361 dynobj = htab->dynobj;
12363 emit_relocs = (bfd_link_relocatable (info)
12364 || info->emitrelocations);
12366 memset (&flinfo, 0, sizeof (flinfo));
12367 flinfo.info = info;
12368 flinfo.output_bfd = abfd;
12369 flinfo.symstrtab = _bfd_elf_strtab_init ();
12370 if (flinfo.symstrtab == NULL)
12371 return false;
12373 if (! dynamic)
12375 flinfo.hash_sec = NULL;
12376 flinfo.symver_sec = NULL;
12378 else
12380 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
12381 /* Note that dynsym_sec can be NULL (on VMS). */
12382 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
12383 /* Note that it is OK if symver_sec is NULL. */
12386 if (info->unique_symbol
12387 && !bfd_hash_table_init (&flinfo.local_hash_table,
12388 local_hash_newfunc,
12389 sizeof (struct local_hash_entry)))
12390 return false;
12392 /* The object attributes have been merged. Remove the input
12393 sections from the link, and set the contents of the output
12394 section. */
12395 sections_removed = false;
12396 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
12397 for (o = abfd->sections; o != NULL; o = o->next)
12399 bool remove_section = false;
12401 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
12402 || strcmp (o->name, ".gnu.attributes") == 0)
12404 for (p = o->map_head.link_order; p != NULL; p = p->next)
12406 asection *input_section;
12408 if (p->type != bfd_indirect_link_order)
12409 continue;
12410 input_section = p->u.indirect.section;
12411 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12412 elf_link_input_bfd ignores this section. */
12413 input_section->flags &= ~SEC_HAS_CONTENTS;
12416 attr_size = bfd_elf_obj_attr_size (abfd);
12417 bfd_set_section_size (o, attr_size);
12418 /* Skip this section later on. */
12419 o->map_head.link_order = NULL;
12420 if (attr_size)
12421 attr_section = o;
12422 else
12423 remove_section = true;
12425 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
12427 /* Remove empty group section from linker output. */
12428 remove_section = true;
12430 if (remove_section)
12432 o->flags |= SEC_EXCLUDE;
12433 bfd_section_list_remove (abfd, o);
12434 abfd->section_count--;
12435 sections_removed = true;
12438 if (sections_removed)
12439 _bfd_fix_excluded_sec_syms (abfd, info);
12441 /* Count up the number of relocations we will output for each output
12442 section, so that we know the sizes of the reloc sections. We
12443 also figure out some maximum sizes. */
12444 max_contents_size = 0;
12445 max_external_reloc_size = 0;
12446 max_internal_reloc_count = 0;
12447 max_sym_count = 0;
12448 max_sym_shndx_count = 0;
12449 merged = false;
12450 for (o = abfd->sections; o != NULL; o = o->next)
12452 struct bfd_elf_section_data *esdo = elf_section_data (o);
12453 o->reloc_count = 0;
12455 for (p = o->map_head.link_order; p != NULL; p = p->next)
12457 unsigned int reloc_count = 0;
12458 unsigned int additional_reloc_count = 0;
12459 struct bfd_elf_section_data *esdi = NULL;
12461 if (p->type == bfd_section_reloc_link_order
12462 || p->type == bfd_symbol_reloc_link_order)
12463 reloc_count = 1;
12464 else if (p->type == bfd_indirect_link_order)
12466 asection *sec;
12468 sec = p->u.indirect.section;
12470 /* Mark all sections which are to be included in the
12471 link. This will normally be every section. We need
12472 to do this so that we can identify any sections which
12473 the linker has decided to not include. */
12474 sec->linker_mark = true;
12476 if (sec->flags & SEC_MERGE)
12477 merged = true;
12479 if (sec->rawsize > max_contents_size)
12480 max_contents_size = sec->rawsize;
12481 if (sec->size > max_contents_size)
12482 max_contents_size = sec->size;
12484 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
12485 && (sec->owner->flags & DYNAMIC) == 0)
12487 size_t sym_count;
12489 /* We are interested in just local symbols, not all
12490 symbols. */
12491 if (elf_bad_symtab (sec->owner))
12492 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
12493 / bed->s->sizeof_sym);
12494 else
12495 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
12497 if (sym_count > max_sym_count)
12498 max_sym_count = sym_count;
12500 if (sym_count > max_sym_shndx_count
12501 && elf_symtab_shndx_list (sec->owner) != NULL)
12502 max_sym_shndx_count = sym_count;
12504 esdi = elf_section_data (sec);
12506 if (esdi->this_hdr.sh_type == SHT_REL
12507 || esdi->this_hdr.sh_type == SHT_RELA)
12508 /* Some backends use reloc_count in relocation sections
12509 to count particular types of relocs. Of course,
12510 reloc sections themselves can't have relocations. */
12512 else if (emit_relocs)
12514 reloc_count = sec->reloc_count;
12515 if (bed->elf_backend_count_additional_relocs)
12517 int c;
12518 c = (*bed->elf_backend_count_additional_relocs) (sec);
12519 additional_reloc_count += c;
12522 else if (bed->elf_backend_count_relocs)
12523 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
12525 if ((sec->flags & SEC_RELOC) != 0)
12527 size_t ext_size = 0;
12529 if (esdi->rel.hdr != NULL)
12530 ext_size = esdi->rel.hdr->sh_size;
12531 if (esdi->rela.hdr != NULL)
12532 ext_size += esdi->rela.hdr->sh_size;
12534 if (ext_size > max_external_reloc_size)
12535 max_external_reloc_size = ext_size;
12536 if (sec->reloc_count > max_internal_reloc_count)
12537 max_internal_reloc_count = sec->reloc_count;
12542 if (reloc_count == 0)
12543 continue;
12545 reloc_count += additional_reloc_count;
12546 o->reloc_count += reloc_count;
12548 if (p->type == bfd_indirect_link_order && emit_relocs)
12550 if (esdi->rel.hdr)
12552 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
12553 esdo->rel.count += additional_reloc_count;
12555 if (esdi->rela.hdr)
12557 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
12558 esdo->rela.count += additional_reloc_count;
12561 else
12563 if (o->use_rela_p)
12564 esdo->rela.count += reloc_count;
12565 else
12566 esdo->rel.count += reloc_count;
12570 if (o->reloc_count > 0)
12571 o->flags |= SEC_RELOC;
12572 else
12574 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12575 set it (this is probably a bug) and if it is set
12576 assign_section_numbers will create a reloc section. */
12577 o->flags &=~ SEC_RELOC;
12580 /* If the SEC_ALLOC flag is not set, force the section VMA to
12581 zero. This is done in elf_fake_sections as well, but forcing
12582 the VMA to 0 here will ensure that relocs against these
12583 sections are handled correctly. */
12584 if ((o->flags & SEC_ALLOC) == 0
12585 && ! o->user_set_vma)
12586 o->vma = 0;
12589 if (! bfd_link_relocatable (info) && merged)
12590 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
12592 /* Figure out the file positions for everything but the symbol table
12593 and the relocs. We set symcount to force assign_section_numbers
12594 to create a symbol table. */
12595 abfd->symcount = info->strip != strip_all || emit_relocs;
12596 BFD_ASSERT (! abfd->output_has_begun);
12597 if (! _bfd_elf_compute_section_file_positions (abfd, info))
12598 goto error_return;
12600 /* Set sizes, and assign file positions for reloc sections. */
12601 for (o = abfd->sections; o != NULL; o = o->next)
12603 struct bfd_elf_section_data *esdo = elf_section_data (o);
12604 if ((o->flags & SEC_RELOC) != 0)
12606 if (esdo->rel.hdr
12607 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
12608 goto error_return;
12610 if (esdo->rela.hdr
12611 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
12612 goto error_return;
12615 /* _bfd_elf_compute_section_file_positions makes temporary use
12616 of target_index. Reset it. */
12617 o->target_index = 0;
12619 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12620 to count upwards while actually outputting the relocations. */
12621 esdo->rel.count = 0;
12622 esdo->rela.count = 0;
12624 if ((esdo->this_hdr.sh_offset == (file_ptr) -1)
12625 && !bfd_section_is_ctf (o))
12627 /* Cache the section contents so that they can be compressed
12628 later. Use bfd_malloc since it will be freed by
12629 bfd_compress_section_contents. */
12630 unsigned char *contents = esdo->this_hdr.contents;
12631 if (contents != NULL)
12632 abort ();
12633 contents
12634 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
12635 if (contents == NULL)
12636 goto error_return;
12637 esdo->this_hdr.contents = contents;
12641 /* We have now assigned file positions for all the sections except .symtab,
12642 .strtab, and non-loaded reloc and compressed debugging sections. We start
12643 the .symtab section at the current file position, and write directly to it.
12644 We build the .strtab section in memory. */
12645 abfd->symcount = 0;
12646 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12647 /* sh_name is set in prep_headers. */
12648 symtab_hdr->sh_type = SHT_SYMTAB;
12649 /* sh_flags, sh_addr and sh_size all start off zero. */
12650 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
12651 /* sh_link is set in assign_section_numbers. */
12652 /* sh_info is set below. */
12653 /* sh_offset is set just below. */
12654 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
12656 if (max_sym_count < 20)
12657 max_sym_count = 20;
12658 htab->strtabsize = max_sym_count;
12659 amt = max_sym_count * sizeof (struct elf_sym_strtab);
12660 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
12661 if (htab->strtab == NULL)
12662 goto error_return;
12663 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12664 flinfo.symshndxbuf
12665 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
12666 ? (Elf_External_Sym_Shndx *) -1 : NULL);
12668 if (info->strip != strip_all || emit_relocs)
12670 file_ptr off = elf_next_file_pos (abfd);
12672 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, true);
12674 /* Note that at this point elf_next_file_pos (abfd) is
12675 incorrect. We do not yet know the size of the .symtab section.
12676 We correct next_file_pos below, after we do know the size. */
12678 /* Start writing out the symbol table. The first symbol is always a
12679 dummy symbol. */
12680 elfsym.st_value = 0;
12681 elfsym.st_size = 0;
12682 elfsym.st_info = 0;
12683 elfsym.st_other = 0;
12684 elfsym.st_shndx = SHN_UNDEF;
12685 elfsym.st_target_internal = 0;
12686 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
12687 bfd_und_section_ptr, NULL) != 1)
12688 goto error_return;
12690 /* Output a symbol for each section if asked or they are used for
12691 relocs. These symbols usually have no names. We store the
12692 index of each one in the index field of the section, so that
12693 we can find it again when outputting relocs. */
12695 if (bfd_keep_unused_section_symbols (abfd) || emit_relocs)
12697 bool name_local_sections
12698 = (bed->elf_backend_name_local_section_symbols
12699 && bed->elf_backend_name_local_section_symbols (abfd));
12700 const char *name = NULL;
12702 elfsym.st_size = 0;
12703 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12704 elfsym.st_other = 0;
12705 elfsym.st_value = 0;
12706 elfsym.st_target_internal = 0;
12707 for (i = 1; i < elf_numsections (abfd); i++)
12709 o = bfd_section_from_elf_index (abfd, i);
12710 if (o != NULL)
12712 o->target_index = bfd_get_symcount (abfd);
12713 elfsym.st_shndx = i;
12714 if (!bfd_link_relocatable (info))
12715 elfsym.st_value = o->vma;
12716 if (name_local_sections)
12717 name = o->name;
12718 if (elf_link_output_symstrtab (&flinfo, name, &elfsym, o,
12719 NULL) != 1)
12720 goto error_return;
12726 /* On some targets like Irix 5 the symbol split between local and global
12727 ones recorded in the sh_info field needs to be done between section
12728 and all other symbols. */
12729 if (bed->elf_backend_elfsym_local_is_section
12730 && bed->elf_backend_elfsym_local_is_section (abfd))
12731 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12733 /* Allocate some memory to hold information read in from the input
12734 files. */
12735 if (max_contents_size != 0)
12737 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12738 if (flinfo.contents == NULL)
12739 goto error_return;
12742 if (max_external_reloc_size != 0)
12744 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
12745 if (flinfo.external_relocs == NULL)
12746 goto error_return;
12749 if (max_internal_reloc_count != 0)
12751 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
12752 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
12753 if (flinfo.internal_relocs == NULL)
12754 goto error_return;
12757 if (max_sym_count != 0)
12759 amt = max_sym_count * bed->s->sizeof_sym;
12760 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
12761 if (flinfo.external_syms == NULL)
12762 goto error_return;
12764 amt = max_sym_count * sizeof (Elf_Internal_Sym);
12765 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
12766 if (flinfo.internal_syms == NULL)
12767 goto error_return;
12769 amt = max_sym_count * sizeof (long);
12770 flinfo.indices = (long int *) bfd_malloc (amt);
12771 if (flinfo.indices == NULL)
12772 goto error_return;
12774 amt = max_sym_count * sizeof (asection *);
12775 flinfo.sections = (asection **) bfd_malloc (amt);
12776 if (flinfo.sections == NULL)
12777 goto error_return;
12780 if (max_sym_shndx_count != 0)
12782 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
12783 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
12784 if (flinfo.locsym_shndx == NULL)
12785 goto error_return;
12788 if (htab->tls_sec)
12790 bfd_vma base, end = 0; /* Both bytes. */
12791 asection *sec;
12793 for (sec = htab->tls_sec;
12794 sec && (sec->flags & SEC_THREAD_LOCAL);
12795 sec = sec->next)
12797 bfd_size_type size = sec->size;
12798 unsigned int opb = bfd_octets_per_byte (abfd, sec);
12800 if (size == 0
12801 && (sec->flags & SEC_HAS_CONTENTS) == 0)
12803 struct bfd_link_order *ord = sec->map_tail.link_order;
12805 if (ord != NULL)
12806 size = ord->offset * opb + ord->size;
12808 end = sec->vma + size / opb;
12810 base = htab->tls_sec->vma;
12811 /* Only align end of TLS section if static TLS doesn't have special
12812 alignment requirements. */
12813 if (bed->static_tls_alignment == 1)
12814 end = align_power (end, htab->tls_sec->alignment_power);
12815 htab->tls_size = end - base;
12818 if (!_bfd_elf_fixup_eh_frame_hdr (info))
12819 return false;
12821 /* Finish relative relocations here after regular symbol processing
12822 is finished if DT_RELR is enabled. */
12823 if (info->enable_dt_relr
12824 && bed->finish_relative_relocs
12825 && !bed->finish_relative_relocs (info))
12826 info->callbacks->einfo
12827 (_("%F%P: %pB: failed to finish relative relocations\n"), abfd);
12829 /* Since ELF permits relocations to be against local symbols, we
12830 must have the local symbols available when we do the relocations.
12831 Since we would rather only read the local symbols once, and we
12832 would rather not keep them in memory, we handle all the
12833 relocations for a single input file at the same time.
12835 Unfortunately, there is no way to know the total number of local
12836 symbols until we have seen all of them, and the local symbol
12837 indices precede the global symbol indices. This means that when
12838 we are generating relocatable output, and we see a reloc against
12839 a global symbol, we can not know the symbol index until we have
12840 finished examining all the local symbols to see which ones we are
12841 going to output. To deal with this, we keep the relocations in
12842 memory, and don't output them until the end of the link. This is
12843 an unfortunate waste of memory, but I don't see a good way around
12844 it. Fortunately, it only happens when performing a relocatable
12845 link, which is not the common case. FIXME: If keep_memory is set
12846 we could write the relocs out and then read them again; I don't
12847 know how bad the memory loss will be. */
12849 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12850 sub->output_has_begun = false;
12851 for (o = abfd->sections; o != NULL; o = o->next)
12853 for (p = o->map_head.link_order; p != NULL; p = p->next)
12855 if (p->type == bfd_indirect_link_order
12856 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12857 == bfd_target_elf_flavour)
12858 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12860 if (! sub->output_has_begun)
12862 if (! elf_link_input_bfd (&flinfo, sub))
12863 goto error_return;
12864 sub->output_has_begun = true;
12867 else if (p->type == bfd_section_reloc_link_order
12868 || p->type == bfd_symbol_reloc_link_order)
12870 if (! elf_reloc_link_order (abfd, info, o, p))
12871 goto error_return;
12873 else
12875 if (! _bfd_default_link_order (abfd, info, o, p))
12877 if (p->type == bfd_indirect_link_order
12878 && (bfd_get_flavour (sub)
12879 == bfd_target_elf_flavour)
12880 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12881 != bed->s->elfclass))
12883 const char *iclass, *oclass;
12885 switch (bed->s->elfclass)
12887 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12888 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12889 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12890 default: abort ();
12893 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
12895 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12896 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12897 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12898 default: abort ();
12901 bfd_set_error (bfd_error_wrong_format);
12902 _bfd_error_handler
12903 /* xgettext:c-format */
12904 (_("%pB: file class %s incompatible with %s"),
12905 sub, iclass, oclass);
12908 goto error_return;
12914 /* Free symbol buffer if needed. */
12915 if (!info->reduce_memory_overheads)
12917 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12918 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
12920 free (elf_tdata (sub)->symbuf);
12921 elf_tdata (sub)->symbuf = NULL;
12925 ret = true;
12927 /* Output any global symbols that got converted to local in a
12928 version script or due to symbol visibility. We do this in a
12929 separate step since ELF requires all local symbols to appear
12930 prior to any global symbols. FIXME: We should only do this if
12931 some global symbols were, in fact, converted to become local.
12932 FIXME: Will this work correctly with the Irix 5 linker? */
12933 eoinfo.failed = false;
12934 eoinfo.flinfo = &flinfo;
12935 eoinfo.localsyms = true;
12936 eoinfo.file_sym_done = false;
12937 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12938 if (eoinfo.failed)
12940 ret = false;
12941 goto return_local_hash_table;
12944 /* If backend needs to output some local symbols not present in the hash
12945 table, do it now. */
12946 if (bed->elf_backend_output_arch_local_syms)
12948 if (! ((*bed->elf_backend_output_arch_local_syms)
12949 (abfd, info, &flinfo, elf_link_output_symstrtab)))
12951 ret = false;
12952 goto return_local_hash_table;
12956 /* That wrote out all the local symbols. Finish up the symbol table
12957 with the global symbols. Even if we want to strip everything we
12958 can, we still need to deal with those global symbols that got
12959 converted to local in a version script. */
12961 /* The sh_info field records the index of the first non local symbol. */
12962 if (!symtab_hdr->sh_info)
12963 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12965 if (dynamic
12966 && htab->dynsym != NULL
12967 && htab->dynsym->output_section != bfd_abs_section_ptr)
12969 Elf_Internal_Sym sym;
12970 bfd_byte *dynsym = htab->dynsym->contents;
12972 o = htab->dynsym->output_section;
12973 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
12975 /* Write out the section symbols for the output sections. */
12976 if (bfd_link_pic (info)
12977 || htab->is_relocatable_executable)
12979 asection *s;
12981 sym.st_size = 0;
12982 sym.st_name = 0;
12983 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12984 sym.st_other = 0;
12985 sym.st_target_internal = 0;
12987 for (s = abfd->sections; s != NULL; s = s->next)
12989 int indx;
12990 bfd_byte *dest;
12991 long dynindx;
12993 dynindx = elf_section_data (s)->dynindx;
12994 if (dynindx <= 0)
12995 continue;
12996 indx = elf_section_data (s)->this_idx;
12997 BFD_ASSERT (indx > 0);
12998 sym.st_shndx = indx;
12999 if (! check_dynsym (abfd, &sym))
13001 ret = false;
13002 goto return_local_hash_table;
13004 sym.st_value = s->vma;
13005 dest = dynsym + dynindx * bed->s->sizeof_sym;
13007 /* Inform the linker of the addition of this symbol. */
13009 if (info->callbacks->ctf_new_dynsym)
13010 info->callbacks->ctf_new_dynsym (dynindx, &sym);
13012 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
13016 /* Write out the local dynsyms. */
13017 if (htab->dynlocal)
13019 struct elf_link_local_dynamic_entry *e;
13020 for (e = htab->dynlocal; e ; e = e->next)
13022 asection *s;
13023 bfd_byte *dest;
13025 /* Copy the internal symbol and turn off visibility.
13026 Note that we saved a word of storage and overwrote
13027 the original st_name with the dynstr_index. */
13028 sym = e->isym;
13029 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
13030 sym.st_shndx = SHN_UNDEF;
13032 s = bfd_section_from_elf_index (e->input_bfd,
13033 e->isym.st_shndx);
13034 if (s != NULL
13035 && s->output_section != NULL
13036 && elf_section_data (s->output_section) != NULL)
13038 sym.st_shndx =
13039 elf_section_data (s->output_section)->this_idx;
13040 if (! check_dynsym (abfd, &sym))
13042 ret = false;
13043 goto return_local_hash_table;
13045 sym.st_value = (s->output_section->vma
13046 + s->output_offset
13047 + e->isym.st_value);
13050 /* Inform the linker of the addition of this symbol. */
13052 if (info->callbacks->ctf_new_dynsym)
13053 info->callbacks->ctf_new_dynsym (e->dynindx, &sym);
13055 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
13056 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
13061 /* We get the global symbols from the hash table. */
13062 eoinfo.failed = false;
13063 eoinfo.localsyms = false;
13064 eoinfo.flinfo = &flinfo;
13065 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
13066 if (eoinfo.failed)
13068 ret = false;
13069 goto return_local_hash_table;
13072 /* If backend needs to output some symbols not present in the hash
13073 table, do it now. */
13074 if (bed->elf_backend_output_arch_syms
13075 && (info->strip != strip_all || emit_relocs))
13077 if (! ((*bed->elf_backend_output_arch_syms)
13078 (abfd, info, &flinfo, elf_link_output_symstrtab)))
13080 ret = false;
13081 goto return_local_hash_table;
13085 /* Finalize the .strtab section. */
13086 _bfd_elf_strtab_finalize (flinfo.symstrtab);
13088 /* Swap out the .strtab section. */
13089 if (!elf_link_swap_symbols_out (&flinfo))
13091 ret = false;
13092 goto return_local_hash_table;
13095 /* Now we know the size of the symtab section. */
13096 if (bfd_get_symcount (abfd) > 0)
13098 /* Finish up and write out the symbol string table (.strtab)
13099 section. */
13100 Elf_Internal_Shdr *symstrtab_hdr = NULL;
13101 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
13103 if (elf_symtab_shndx_list (abfd))
13105 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
13107 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
13109 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
13110 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
13111 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
13112 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
13113 symtab_shndx_hdr->sh_size = amt;
13115 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
13116 off, true);
13118 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
13119 || (bfd_write (flinfo.symshndxbuf, amt, abfd) != amt))
13121 ret = false;
13122 goto return_local_hash_table;
13127 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
13128 /* sh_name was set in prep_headers. */
13129 symstrtab_hdr->sh_type = SHT_STRTAB;
13130 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
13131 symstrtab_hdr->sh_addr = 0;
13132 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
13133 symstrtab_hdr->sh_entsize = 0;
13134 symstrtab_hdr->sh_link = 0;
13135 symstrtab_hdr->sh_info = 0;
13136 /* sh_offset is set just below. */
13137 symstrtab_hdr->sh_addralign = 1;
13139 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
13140 off, true);
13141 elf_next_file_pos (abfd) = off;
13143 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
13144 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
13146 ret = false;
13147 goto return_local_hash_table;
13151 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
13153 _bfd_error_handler (_("%pB: failed to generate import library"),
13154 info->out_implib_bfd);
13155 ret = false;
13156 goto return_local_hash_table;
13159 /* Adjust the relocs to have the correct symbol indices. */
13160 for (o = abfd->sections; o != NULL; o = o->next)
13162 struct bfd_elf_section_data *esdo = elf_section_data (o);
13163 bool sort;
13165 if ((o->flags & SEC_RELOC) == 0)
13166 continue;
13168 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
13169 if (esdo->rel.hdr != NULL
13170 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
13172 ret = false;
13173 goto return_local_hash_table;
13175 if (esdo->rela.hdr != NULL
13176 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
13178 ret = false;
13179 goto return_local_hash_table;
13182 /* Set the reloc_count field to 0 to prevent write_relocs from
13183 trying to swap the relocs out itself. */
13184 o->reloc_count = 0;
13187 relativecount = 0;
13188 if (dynamic && info->combreloc && dynobj != NULL)
13189 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
13191 relr_entsize = 0;
13192 if (htab->srelrdyn != NULL
13193 && htab->srelrdyn->output_section != NULL
13194 && htab->srelrdyn->size != 0)
13196 asection *s = htab->srelrdyn->output_section;
13197 relr_entsize = elf_section_data (s)->this_hdr.sh_entsize;
13198 if (relr_entsize == 0)
13200 relr_entsize = bed->s->arch_size / 8;
13201 elf_section_data (s)->this_hdr.sh_entsize = relr_entsize;
13205 /* If we are linking against a dynamic object, or generating a
13206 shared library, finish up the dynamic linking information. */
13207 if (dynamic)
13209 bfd_byte *dyncon, *dynconend;
13211 /* Fix up .dynamic entries. */
13212 o = bfd_get_linker_section (dynobj, ".dynamic");
13213 BFD_ASSERT (o != NULL);
13215 dyncon = o->contents;
13216 dynconend = PTR_ADD (o->contents, o->size);
13217 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13219 Elf_Internal_Dyn dyn;
13220 const char *name;
13221 unsigned int type;
13222 bfd_size_type sh_size;
13223 bfd_vma sh_addr;
13225 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13227 switch (dyn.d_tag)
13229 default:
13230 continue;
13231 case DT_NULL:
13232 if (relativecount != 0)
13234 switch (elf_section_data (reldyn)->this_hdr.sh_type)
13236 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
13237 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
13239 if (dyn.d_tag != DT_NULL
13240 && dynconend - dyncon >= bed->s->sizeof_dyn)
13242 dyn.d_un.d_val = relativecount;
13243 relativecount = 0;
13244 break;
13246 relativecount = 0;
13248 if (relr_entsize != 0)
13250 if (dynconend - dyncon >= 3 * bed->s->sizeof_dyn)
13252 asection *s = htab->srelrdyn;
13253 dyn.d_tag = DT_RELR;
13254 dyn.d_un.d_ptr
13255 = s->output_section->vma + s->output_offset;
13256 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13257 dyncon += bed->s->sizeof_dyn;
13259 dyn.d_tag = DT_RELRSZ;
13260 dyn.d_un.d_val = s->size;
13261 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13262 dyncon += bed->s->sizeof_dyn;
13264 dyn.d_tag = DT_RELRENT;
13265 dyn.d_un.d_val = relr_entsize;
13266 relr_entsize = 0;
13267 break;
13269 relr_entsize = 0;
13271 continue;
13273 case DT_INIT:
13274 name = info->init_function;
13275 goto get_sym;
13276 case DT_FINI:
13277 name = info->fini_function;
13278 get_sym:
13280 struct elf_link_hash_entry *h;
13282 h = elf_link_hash_lookup (htab, name, false, false, true);
13283 if (h != NULL
13284 && (h->root.type == bfd_link_hash_defined
13285 || h->root.type == bfd_link_hash_defweak))
13287 dyn.d_un.d_ptr = h->root.u.def.value;
13288 o = h->root.u.def.section;
13289 if (o->output_section != NULL)
13290 dyn.d_un.d_ptr += (o->output_section->vma
13291 + o->output_offset);
13292 else
13294 /* The symbol is imported from another shared
13295 library and does not apply to this one. */
13296 dyn.d_un.d_ptr = 0;
13298 break;
13301 continue;
13303 case DT_PREINIT_ARRAYSZ:
13304 name = ".preinit_array";
13305 goto get_out_size;
13306 case DT_INIT_ARRAYSZ:
13307 name = ".init_array";
13308 goto get_out_size;
13309 case DT_FINI_ARRAYSZ:
13310 name = ".fini_array";
13311 get_out_size:
13312 o = bfd_get_section_by_name (abfd, name);
13313 if (o == NULL)
13315 _bfd_error_handler
13316 (_("could not find section %s"), name);
13317 goto error_return;
13319 if (o->size == 0)
13320 _bfd_error_handler
13321 (_("warning: %s section has zero size"), name);
13322 dyn.d_un.d_val = o->size;
13323 break;
13325 case DT_PREINIT_ARRAY:
13326 name = ".preinit_array";
13327 goto get_out_vma;
13328 case DT_INIT_ARRAY:
13329 name = ".init_array";
13330 goto get_out_vma;
13331 case DT_FINI_ARRAY:
13332 name = ".fini_array";
13333 get_out_vma:
13334 o = bfd_get_section_by_name (abfd, name);
13335 goto do_vma;
13337 case DT_HASH:
13338 name = ".hash";
13339 goto get_vma;
13340 case DT_GNU_HASH:
13341 name = ".gnu.hash";
13342 goto get_vma;
13343 case DT_STRTAB:
13344 name = ".dynstr";
13345 goto get_vma;
13346 case DT_SYMTAB:
13347 name = ".dynsym";
13348 goto get_vma;
13349 case DT_VERDEF:
13350 name = ".gnu.version_d";
13351 goto get_vma;
13352 case DT_VERNEED:
13353 name = ".gnu.version_r";
13354 goto get_vma;
13355 case DT_VERSYM:
13356 name = ".gnu.version";
13357 get_vma:
13358 o = bfd_get_linker_section (dynobj, name);
13359 do_vma:
13360 if (o == NULL || bfd_is_abs_section (o->output_section))
13362 _bfd_error_handler
13363 (_("could not find section %s"), name);
13364 goto error_return;
13366 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
13368 _bfd_error_handler
13369 (_("warning: section '%s' is being made into a note"), name);
13370 bfd_set_error (bfd_error_nonrepresentable_section);
13371 goto error_return;
13373 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
13374 break;
13376 case DT_REL:
13377 case DT_RELA:
13378 case DT_RELSZ:
13379 case DT_RELASZ:
13380 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
13381 type = SHT_REL;
13382 else
13383 type = SHT_RELA;
13384 sh_size = 0;
13385 sh_addr = 0;
13386 for (i = 1; i < elf_numsections (abfd); i++)
13388 Elf_Internal_Shdr *hdr;
13390 hdr = elf_elfsections (abfd)[i];
13391 if (hdr->sh_type == type
13392 && (hdr->sh_flags & SHF_ALLOC) != 0)
13394 sh_size += hdr->sh_size;
13395 if (sh_addr == 0
13396 || sh_addr > hdr->sh_addr)
13397 sh_addr = hdr->sh_addr;
13401 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
13403 unsigned int opb = bfd_octets_per_byte (abfd, o);
13405 /* Don't count procedure linkage table relocs in the
13406 overall reloc count. */
13407 sh_size -= htab->srelplt->size;
13408 if (sh_size == 0)
13409 /* If the size is zero, make the address zero too.
13410 This is to avoid a glibc bug. If the backend
13411 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
13412 zero, then we'll put DT_RELA at the end of
13413 DT_JMPREL. glibc will interpret the end of
13414 DT_RELA matching the end of DT_JMPREL as the
13415 case where DT_RELA includes DT_JMPREL, and for
13416 LD_BIND_NOW will decide that processing DT_RELA
13417 will process the PLT relocs too. Net result:
13418 No PLT relocs applied. */
13419 sh_addr = 0;
13421 /* If .rela.plt is the first .rela section, exclude
13422 it from DT_RELA. */
13423 else if (sh_addr == (htab->srelplt->output_section->vma
13424 + htab->srelplt->output_offset) * opb)
13425 sh_addr += htab->srelplt->size;
13428 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
13429 dyn.d_un.d_val = sh_size;
13430 else
13431 dyn.d_un.d_ptr = sh_addr;
13432 break;
13434 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13438 /* If we have created any dynamic sections, then output them. */
13439 if (dynobj != NULL)
13441 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
13442 goto error_return;
13444 /* Check for DT_TEXTREL (late, in case the backend removes it). */
13445 if (bfd_link_textrel_check (info)
13446 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL
13447 && o->size != 0)
13449 bfd_byte *dyncon, *dynconend;
13451 dyncon = o->contents;
13452 dynconend = o->contents + o->size;
13453 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13455 Elf_Internal_Dyn dyn;
13457 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13459 if (dyn.d_tag == DT_TEXTREL)
13461 if (info->textrel_check == textrel_check_error)
13462 info->callbacks->einfo
13463 (_("%P%X: read-only segment has dynamic relocations\n"));
13464 else if (bfd_link_dll (info))
13465 info->callbacks->einfo
13466 (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
13467 else if (bfd_link_pde (info))
13468 info->callbacks->einfo
13469 (_("%P: warning: creating DT_TEXTREL in a PDE\n"));
13470 else
13471 info->callbacks->einfo
13472 (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
13473 break;
13478 for (o = dynobj->sections; o != NULL; o = o->next)
13480 if ((o->flags & SEC_HAS_CONTENTS) == 0
13481 || o->size == 0
13482 || o->output_section == bfd_abs_section_ptr)
13483 continue;
13484 if ((o->flags & SEC_LINKER_CREATED) == 0)
13486 /* At this point, we are only interested in sections
13487 created by _bfd_elf_link_create_dynamic_sections. */
13488 continue;
13490 if (htab->stab_info.stabstr == o)
13491 continue;
13492 if (htab->eh_info.hdr_sec == o)
13493 continue;
13494 if (strcmp (o->name, ".dynstr") != 0)
13496 bfd_size_type octets = ((file_ptr) o->output_offset
13497 * bfd_octets_per_byte (abfd, o));
13498 if (!bfd_set_section_contents (abfd, o->output_section,
13499 o->contents, octets, o->size))
13500 goto error_return;
13502 else
13504 /* The contents of the .dynstr section are actually in a
13505 stringtab. */
13506 file_ptr off;
13508 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
13509 if (bfd_seek (abfd, off, SEEK_SET) != 0
13510 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
13511 goto error_return;
13516 if (!info->resolve_section_groups)
13518 bool failed = false;
13520 BFD_ASSERT (bfd_link_relocatable (info));
13521 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
13522 if (failed)
13523 goto error_return;
13526 /* If we have optimized stabs strings, output them. */
13527 if (htab->stab_info.stabstr != NULL)
13529 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
13530 goto error_return;
13533 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
13534 goto error_return;
13536 if (! _bfd_elf_write_section_sframe (abfd, info))
13537 goto error_return;
13539 if (info->callbacks->emit_ctf)
13540 info->callbacks->emit_ctf ();
13542 elf_final_link_free (abfd, &flinfo);
13544 if (attr_section)
13546 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
13547 if (contents == NULL)
13549 /* Bail out and fail. */
13550 ret = false;
13551 goto return_local_hash_table;
13553 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
13554 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
13555 free (contents);
13558 return_local_hash_table:
13559 if (info->unique_symbol)
13560 bfd_hash_table_free (&flinfo.local_hash_table);
13561 return ret;
13563 error_return:
13564 elf_final_link_free (abfd, &flinfo);
13565 ret = false;
13566 goto return_local_hash_table;
13569 /* Initialize COOKIE for input bfd ABFD. */
13571 static bool
13572 init_reloc_cookie (struct elf_reloc_cookie *cookie,
13573 struct bfd_link_info *info, bfd *abfd)
13575 Elf_Internal_Shdr *symtab_hdr;
13576 const struct elf_backend_data *bed;
13578 bed = get_elf_backend_data (abfd);
13579 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13581 cookie->abfd = abfd;
13582 cookie->sym_hashes = elf_sym_hashes (abfd);
13583 cookie->bad_symtab = elf_bad_symtab (abfd);
13584 if (cookie->bad_symtab)
13586 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13587 cookie->extsymoff = 0;
13589 else
13591 cookie->locsymcount = symtab_hdr->sh_info;
13592 cookie->extsymoff = symtab_hdr->sh_info;
13595 if (bed->s->arch_size == 32)
13596 cookie->r_sym_shift = 8;
13597 else
13598 cookie->r_sym_shift = 32;
13600 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
13601 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
13603 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
13604 cookie->locsymcount, 0,
13605 NULL, NULL, NULL);
13606 if (cookie->locsyms == NULL)
13608 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
13609 return false;
13611 if (_bfd_link_keep_memory (info) )
13613 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
13614 info->cache_size += (cookie->locsymcount
13615 * sizeof (Elf_External_Sym_Shndx));
13618 return true;
13621 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
13623 static void
13624 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
13626 Elf_Internal_Shdr *symtab_hdr;
13628 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13629 if (symtab_hdr->contents != (unsigned char *) cookie->locsyms)
13630 free (cookie->locsyms);
13633 /* Initialize the relocation information in COOKIE for input section SEC
13634 of input bfd ABFD. */
13636 static bool
13637 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13638 struct bfd_link_info *info, bfd *abfd,
13639 asection *sec)
13641 if (sec->reloc_count == 0)
13643 cookie->rels = NULL;
13644 cookie->relend = NULL;
13646 else
13648 cookie->rels = _bfd_elf_link_info_read_relocs (abfd, info, sec,
13649 NULL, NULL,
13650 _bfd_link_keep_memory (info));
13651 if (cookie->rels == NULL)
13652 return false;
13653 cookie->rel = cookie->rels;
13654 cookie->relend = cookie->rels + sec->reloc_count;
13656 cookie->rel = cookie->rels;
13657 return true;
13660 /* Free the memory allocated by init_reloc_cookie_rels,
13661 if appropriate. */
13663 static void
13664 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13665 asection *sec)
13667 if (elf_section_data (sec)->relocs != cookie->rels)
13668 free (cookie->rels);
13671 /* Initialize the whole of COOKIE for input section SEC. */
13673 static bool
13674 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13675 struct bfd_link_info *info,
13676 asection *sec)
13678 if (!init_reloc_cookie (cookie, info, sec->owner))
13679 goto error1;
13680 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
13681 goto error2;
13682 return true;
13684 error2:
13685 fini_reloc_cookie (cookie, sec->owner);
13686 error1:
13687 return false;
13690 /* Free the memory allocated by init_reloc_cookie_for_section,
13691 if appropriate. */
13693 static void
13694 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13695 asection *sec)
13697 fini_reloc_cookie_rels (cookie, sec);
13698 fini_reloc_cookie (cookie, sec->owner);
13701 /* Garbage collect unused sections. */
13703 /* Default gc_mark_hook. */
13705 asection *
13706 _bfd_elf_gc_mark_hook (asection *sec,
13707 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13708 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13709 struct elf_link_hash_entry *h,
13710 Elf_Internal_Sym *sym)
13712 if (h != NULL)
13714 switch (h->root.type)
13716 case bfd_link_hash_defined:
13717 case bfd_link_hash_defweak:
13718 return h->root.u.def.section;
13720 case bfd_link_hash_common:
13721 return h->root.u.c.p->section;
13723 default:
13724 break;
13727 else
13728 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
13730 return NULL;
13733 /* Return the debug definition section. */
13735 static asection *
13736 elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
13737 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13738 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13739 struct elf_link_hash_entry *h,
13740 Elf_Internal_Sym *sym)
13742 if (h != NULL)
13744 /* Return the global debug definition section. */
13745 if ((h->root.type == bfd_link_hash_defined
13746 || h->root.type == bfd_link_hash_defweak)
13747 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
13748 return h->root.u.def.section;
13750 else
13752 /* Return the local debug definition section. */
13753 asection *isec = bfd_section_from_elf_index (sec->owner,
13754 sym->st_shndx);
13755 if (isec != NULL && (isec->flags & SEC_DEBUGGING) != 0)
13756 return isec;
13759 return NULL;
13762 /* COOKIE->rel describes a relocation against section SEC, which is
13763 a section we've decided to keep. Return the section that contains
13764 the relocation symbol, or NULL if no section contains it. */
13766 asection *
13767 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
13768 elf_gc_mark_hook_fn gc_mark_hook,
13769 struct elf_reloc_cookie *cookie,
13770 bool *start_stop)
13772 unsigned long r_symndx;
13773 struct elf_link_hash_entry *h, *hw;
13775 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
13776 if (r_symndx == STN_UNDEF)
13777 return NULL;
13779 if (r_symndx >= cookie->locsymcount
13780 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13782 bool was_marked;
13784 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
13785 if (h == NULL)
13787 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
13788 sec->owner);
13789 return NULL;
13791 while (h->root.type == bfd_link_hash_indirect
13792 || h->root.type == bfd_link_hash_warning)
13793 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13795 was_marked = h->mark;
13796 h->mark = 1;
13797 /* Keep all aliases of the symbol too. If an object symbol
13798 needs to be copied into .dynbss then all of its aliases
13799 should be present as dynamic symbols, not just the one used
13800 on the copy relocation. */
13801 hw = h;
13802 while (hw->is_weakalias)
13804 hw = hw->u.alias;
13805 hw->mark = 1;
13808 if (!was_marked && h->start_stop && !h->root.ldscript_def)
13810 if (info->start_stop_gc)
13811 return NULL;
13813 /* To work around a glibc bug, mark XXX input sections
13814 when there is a reference to __start_XXX or __stop_XXX
13815 symbols. */
13816 else if (start_stop != NULL)
13818 asection *s = h->u2.start_stop_section;
13819 *start_stop = true;
13820 return s;
13824 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
13827 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
13828 &cookie->locsyms[r_symndx]);
13831 /* COOKIE->rel describes a relocation against section SEC, which is
13832 a section we've decided to keep. Mark the section that contains
13833 the relocation symbol. */
13835 bool
13836 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
13837 asection *sec,
13838 elf_gc_mark_hook_fn gc_mark_hook,
13839 struct elf_reloc_cookie *cookie)
13841 asection *rsec;
13842 bool start_stop = false;
13844 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
13845 while (rsec != NULL)
13847 if (!rsec->gc_mark)
13849 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
13850 || (rsec->owner->flags & DYNAMIC) != 0)
13851 rsec->gc_mark = 1;
13852 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
13853 return false;
13855 if (!start_stop)
13856 break;
13857 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
13859 return true;
13862 /* The mark phase of garbage collection. For a given section, mark
13863 it and any sections in this section's group, and all the sections
13864 which define symbols to which it refers. */
13866 bool
13867 _bfd_elf_gc_mark (struct bfd_link_info *info,
13868 asection *sec,
13869 elf_gc_mark_hook_fn gc_mark_hook)
13871 bool ret;
13872 asection *group_sec, *eh_frame;
13874 sec->gc_mark = 1;
13876 /* Mark all the sections in the group. */
13877 group_sec = elf_section_data (sec)->next_in_group;
13878 if (group_sec && !group_sec->gc_mark)
13879 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
13880 return false;
13882 /* Look through the section relocs. */
13883 ret = true;
13884 eh_frame = elf_eh_frame_section (sec->owner);
13885 if ((sec->flags & SEC_RELOC) != 0
13886 && sec->reloc_count > 0
13887 && sec != eh_frame)
13889 struct elf_reloc_cookie cookie;
13891 if (!init_reloc_cookie_for_section (&cookie, info, sec))
13892 ret = false;
13893 else
13895 for (; cookie.rel < cookie.relend; cookie.rel++)
13896 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
13898 ret = false;
13899 break;
13901 fini_reloc_cookie_for_section (&cookie, sec);
13905 if (ret && eh_frame && elf_fde_list (sec))
13907 struct elf_reloc_cookie cookie;
13909 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
13910 ret = false;
13911 else
13913 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
13914 gc_mark_hook, &cookie))
13915 ret = false;
13916 fini_reloc_cookie_for_section (&cookie, eh_frame);
13920 eh_frame = elf_section_eh_frame_entry (sec);
13921 if (ret && eh_frame && !eh_frame->gc_mark)
13922 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
13923 ret = false;
13925 return ret;
13928 /* Scan and mark sections in a special or debug section group. */
13930 static void
13931 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
13933 /* Point to first section of section group. */
13934 asection *ssec;
13935 /* Used to iterate the section group. */
13936 asection *msec;
13938 bool is_special_grp = true;
13939 bool is_debug_grp = true;
13941 /* First scan to see if group contains any section other than debug
13942 and special section. */
13943 ssec = msec = elf_next_in_group (grp);
13946 if ((msec->flags & SEC_DEBUGGING) == 0)
13947 is_debug_grp = false;
13949 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
13950 is_special_grp = false;
13952 msec = elf_next_in_group (msec);
13954 while (msec != ssec);
13956 /* If this is a pure debug section group or pure special section group,
13957 keep all sections in this group. */
13958 if (is_debug_grp || is_special_grp)
13962 msec->gc_mark = 1;
13963 msec = elf_next_in_group (msec);
13965 while (msec != ssec);
13969 /* Keep debug and special sections. */
13971 bool
13972 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
13973 elf_gc_mark_hook_fn mark_hook)
13975 bfd *ibfd;
13977 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13979 asection *isec;
13980 bool some_kept;
13981 bool debug_frag_seen;
13982 bool has_kept_debug_info;
13984 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13985 continue;
13986 isec = ibfd->sections;
13987 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13988 continue;
13990 /* Ensure all linker created sections are kept,
13991 see if any other section is already marked,
13992 and note if we have any fragmented debug sections. */
13993 debug_frag_seen = some_kept = has_kept_debug_info = false;
13994 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13996 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13997 isec->gc_mark = 1;
13998 else if (isec->gc_mark
13999 && (isec->flags & SEC_ALLOC) != 0
14000 && elf_section_type (isec) != SHT_NOTE)
14001 some_kept = true;
14002 else
14004 /* Since all sections, except for backend specific ones,
14005 have been garbage collected, call mark_hook on this
14006 section if any of its linked-to sections is marked. */
14007 asection *linked_to_sec;
14008 for (linked_to_sec = elf_linked_to_section (isec);
14009 linked_to_sec != NULL && !linked_to_sec->linker_mark;
14010 linked_to_sec = elf_linked_to_section (linked_to_sec))
14012 if (linked_to_sec->gc_mark)
14014 if (!_bfd_elf_gc_mark (info, isec, mark_hook))
14015 return false;
14016 break;
14018 linked_to_sec->linker_mark = 1;
14020 for (linked_to_sec = elf_linked_to_section (isec);
14021 linked_to_sec != NULL && linked_to_sec->linker_mark;
14022 linked_to_sec = elf_linked_to_section (linked_to_sec))
14023 linked_to_sec->linker_mark = 0;
14026 if (!debug_frag_seen
14027 && (isec->flags & SEC_DEBUGGING)
14028 && startswith (isec->name, ".debug_line."))
14029 debug_frag_seen = true;
14030 else if (strcmp (bfd_section_name (isec),
14031 "__patchable_function_entries") == 0
14032 && elf_linked_to_section (isec) == NULL)
14033 info->callbacks->einfo (_("%F%P: %pB(%pA): error: "
14034 "need linked-to section "
14035 "for --gc-sections\n"),
14036 isec->owner, isec);
14039 /* If no non-note alloc section in this file will be kept, then
14040 we can toss out the debug and special sections. */
14041 if (!some_kept)
14042 continue;
14044 /* Keep debug and special sections like .comment when they are
14045 not part of a group. Also keep section groups that contain
14046 just debug sections or special sections. NB: Sections with
14047 linked-to section has been handled above. */
14048 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14050 if ((isec->flags & SEC_GROUP) != 0)
14051 _bfd_elf_gc_mark_debug_special_section_group (isec);
14052 else if (((isec->flags & SEC_DEBUGGING) != 0
14053 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
14054 && elf_next_in_group (isec) == NULL
14055 && elf_linked_to_section (isec) == NULL)
14056 isec->gc_mark = 1;
14057 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
14058 has_kept_debug_info = true;
14061 /* Look for CODE sections which are going to be discarded,
14062 and find and discard any fragmented debug sections which
14063 are associated with that code section. */
14064 if (debug_frag_seen)
14065 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14066 if ((isec->flags & SEC_CODE) != 0
14067 && isec->gc_mark == 0)
14069 unsigned int ilen;
14070 asection *dsec;
14072 ilen = strlen (isec->name);
14074 /* Association is determined by the name of the debug
14075 section containing the name of the code section as
14076 a suffix. For example .debug_line.text.foo is a
14077 debug section associated with .text.foo. */
14078 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
14080 unsigned int dlen;
14082 if (dsec->gc_mark == 0
14083 || (dsec->flags & SEC_DEBUGGING) == 0)
14084 continue;
14086 dlen = strlen (dsec->name);
14088 if (dlen > ilen
14089 && strncmp (dsec->name + (dlen - ilen),
14090 isec->name, ilen) == 0)
14091 dsec->gc_mark = 0;
14095 /* Mark debug sections referenced by kept debug sections. */
14096 if (has_kept_debug_info)
14097 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14098 if (isec->gc_mark
14099 && (isec->flags & SEC_DEBUGGING) != 0)
14100 if (!_bfd_elf_gc_mark (info, isec,
14101 elf_gc_mark_debug_section))
14102 return false;
14104 return true;
14107 static bool
14108 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
14110 bfd *sub;
14111 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14113 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
14115 asection *o;
14117 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
14118 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
14119 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
14120 continue;
14121 o = sub->sections;
14122 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14123 continue;
14125 for (o = sub->sections; o != NULL; o = o->next)
14127 /* When any section in a section group is kept, we keep all
14128 sections in the section group. If the first member of
14129 the section group is excluded, we will also exclude the
14130 group section. */
14131 if (o->flags & SEC_GROUP)
14133 asection *first = elf_next_in_group (o);
14134 o->gc_mark = first->gc_mark;
14137 if (o->gc_mark)
14138 continue;
14140 /* Skip sweeping sections already excluded. */
14141 if (o->flags & SEC_EXCLUDE)
14142 continue;
14144 /* Since this is early in the link process, it is simple
14145 to remove a section from the output. */
14146 o->flags |= SEC_EXCLUDE;
14148 if (info->print_gc_sections && o->size != 0)
14149 /* xgettext:c-format */
14150 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
14151 o, sub);
14155 return true;
14158 /* Propagate collected vtable information. This is called through
14159 elf_link_hash_traverse. */
14161 static bool
14162 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
14164 /* Those that are not vtables. */
14165 if (h->start_stop
14166 || h->u2.vtable == NULL
14167 || h->u2.vtable->parent == NULL)
14168 return true;
14170 /* Those vtables that do not have parents, we cannot merge. */
14171 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
14172 return true;
14174 /* If we've already been done, exit. */
14175 if (h->u2.vtable->used && h->u2.vtable->used[-1])
14176 return true;
14178 /* Make sure the parent's table is up to date. */
14179 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
14181 if (h->u2.vtable->used == NULL)
14183 /* None of this table's entries were referenced. Re-use the
14184 parent's table. */
14185 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
14186 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
14188 else
14190 size_t n;
14191 bool *cu, *pu;
14193 /* Or the parent's entries into ours. */
14194 cu = h->u2.vtable->used;
14195 cu[-1] = true;
14196 pu = h->u2.vtable->parent->u2.vtable->used;
14197 if (pu != NULL)
14199 const struct elf_backend_data *bed;
14200 unsigned int log_file_align;
14202 bed = get_elf_backend_data (h->root.u.def.section->owner);
14203 log_file_align = bed->s->log_file_align;
14204 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
14205 while (n--)
14207 if (*pu)
14208 *cu = true;
14209 pu++;
14210 cu++;
14215 return true;
14218 struct link_info_ok
14220 struct bfd_link_info *info;
14221 bool ok;
14224 static bool
14225 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h,
14226 void *ptr)
14228 asection *sec;
14229 bfd_vma hstart, hend;
14230 Elf_Internal_Rela *relstart, *relend, *rel;
14231 const struct elf_backend_data *bed;
14232 unsigned int log_file_align;
14233 struct link_info_ok *info = (struct link_info_ok *) ptr;
14235 /* Take care of both those symbols that do not describe vtables as
14236 well as those that are not loaded. */
14237 if (h->start_stop
14238 || h->u2.vtable == NULL
14239 || h->u2.vtable->parent == NULL)
14240 return true;
14242 BFD_ASSERT (h->root.type == bfd_link_hash_defined
14243 || h->root.type == bfd_link_hash_defweak);
14245 sec = h->root.u.def.section;
14246 hstart = h->root.u.def.value;
14247 hend = hstart + h->size;
14249 relstart = _bfd_elf_link_info_read_relocs (sec->owner, info->info,
14250 sec, NULL, NULL, true);
14251 if (!relstart)
14252 return info->ok = false;
14253 bed = get_elf_backend_data (sec->owner);
14254 log_file_align = bed->s->log_file_align;
14256 relend = relstart + sec->reloc_count;
14258 for (rel = relstart; rel < relend; ++rel)
14259 if (rel->r_offset >= hstart && rel->r_offset < hend)
14261 /* If the entry is in use, do nothing. */
14262 if (h->u2.vtable->used
14263 && (rel->r_offset - hstart) < h->u2.vtable->size)
14265 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
14266 if (h->u2.vtable->used[entry])
14267 continue;
14269 /* Otherwise, kill it. */
14270 rel->r_offset = rel->r_info = rel->r_addend = 0;
14273 return true;
14276 /* Mark sections containing dynamically referenced symbols. When
14277 building shared libraries, we must assume that any visible symbol is
14278 referenced. */
14280 bool
14281 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
14283 struct bfd_link_info *info = (struct bfd_link_info *) inf;
14284 struct bfd_elf_dynamic_list *d = info->dynamic_list;
14286 if ((h->root.type == bfd_link_hash_defined
14287 || h->root.type == bfd_link_hash_defweak)
14288 && (!h->start_stop
14289 || h->root.ldscript_def
14290 || !info->start_stop_gc)
14291 && ((h->ref_dynamic && !h->forced_local)
14292 || ((h->def_regular || ELF_COMMON_DEF_P (h))
14293 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
14294 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
14295 && (!bfd_link_executable (info)
14296 || info->gc_keep_exported
14297 || info->export_dynamic
14298 || (h->dynamic
14299 && d != NULL
14300 && (*d->match) (&d->head, NULL, h->root.root.string)))
14301 && (h->versioned >= versioned
14302 || !bfd_hide_sym_by_version (info->version_info,
14303 h->root.root.string)))))
14304 h->root.u.def.section->flags |= SEC_KEEP;
14306 return true;
14309 /* Keep all sections containing symbols undefined on the command-line,
14310 and the section containing the entry symbol. */
14312 void
14313 _bfd_elf_gc_keep (struct bfd_link_info *info)
14315 struct bfd_sym_chain *sym;
14317 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
14319 struct elf_link_hash_entry *h;
14321 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
14322 false, false, false);
14324 if (h != NULL
14325 && (h->root.type == bfd_link_hash_defined
14326 || h->root.type == bfd_link_hash_defweak)
14327 && !bfd_is_const_section (h->root.u.def.section))
14328 h->root.u.def.section->flags |= SEC_KEEP;
14332 bool
14333 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
14334 struct bfd_link_info *info)
14336 bfd *ibfd = info->input_bfds;
14338 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14340 asection *sec;
14341 struct elf_reloc_cookie cookie;
14343 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
14344 continue;
14345 sec = ibfd->sections;
14346 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14347 continue;
14349 if (!init_reloc_cookie (&cookie, info, ibfd))
14350 return false;
14352 for (sec = ibfd->sections; sec; sec = sec->next)
14354 if (startswith (bfd_section_name (sec), ".eh_frame_entry")
14355 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
14357 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
14358 fini_reloc_cookie_rels (&cookie, sec);
14362 return true;
14365 /* Do mark and sweep of unused sections. */
14367 bool
14368 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
14370 bool ok = true;
14371 bfd *sub;
14372 elf_gc_mark_hook_fn gc_mark_hook;
14373 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14374 struct elf_link_hash_table *htab;
14375 struct link_info_ok info_ok;
14377 if (!bed->can_gc_sections
14378 || !is_elf_hash_table (info->hash))
14380 _bfd_error_handler(_("warning: gc-sections option ignored"));
14381 return true;
14384 bed->gc_keep (info);
14385 htab = elf_hash_table (info);
14387 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
14388 at the .eh_frame section if we can mark the FDEs individually. */
14389 for (sub = info->input_bfds;
14390 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
14391 sub = sub->link.next)
14393 asection *sec;
14394 struct elf_reloc_cookie cookie;
14396 sec = sub->sections;
14397 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14398 continue;
14399 sec = bfd_get_section_by_name (sub, ".eh_frame");
14400 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
14402 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
14403 if (elf_section_data (sec)->sec_info
14404 && (sec->flags & SEC_LINKER_CREATED) == 0)
14405 elf_eh_frame_section (sub) = sec;
14406 fini_reloc_cookie_for_section (&cookie, sec);
14407 sec = bfd_get_next_section_by_name (NULL, sec);
14411 /* Apply transitive closure to the vtable entry usage info. */
14412 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
14413 if (!ok)
14414 return false;
14416 /* Kill the vtable relocations that were not used. */
14417 info_ok.info = info;
14418 info_ok.ok = true;
14419 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &info_ok);
14420 if (!info_ok.ok)
14421 return false;
14423 /* Mark dynamically referenced symbols. */
14424 if (htab->dynamic_sections_created || info->gc_keep_exported)
14425 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
14427 /* Grovel through relocs to find out who stays ... */
14428 gc_mark_hook = bed->gc_mark_hook;
14429 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
14431 asection *o;
14433 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
14434 || elf_object_id (sub) != elf_hash_table_id (htab)
14435 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
14436 continue;
14438 o = sub->sections;
14439 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14440 continue;
14442 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
14443 Also treat note sections as a root, if the section is not part
14444 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
14445 well as FINI_ARRAY sections for ld -r. */
14446 for (o = sub->sections; o != NULL; o = o->next)
14447 if (!o->gc_mark
14448 && (o->flags & SEC_EXCLUDE) == 0
14449 && ((o->flags & SEC_KEEP) != 0
14450 || (bfd_link_relocatable (info)
14451 && ((elf_section_data (o)->this_hdr.sh_type
14452 == SHT_PREINIT_ARRAY)
14453 || (elf_section_data (o)->this_hdr.sh_type
14454 == SHT_INIT_ARRAY)
14455 || (elf_section_data (o)->this_hdr.sh_type
14456 == SHT_FINI_ARRAY)))
14457 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
14458 && elf_next_in_group (o) == NULL
14459 && elf_linked_to_section (o) == NULL)
14460 || ((elf_tdata (sub)->has_gnu_osabi & elf_gnu_osabi_retain)
14461 && (elf_section_flags (o) & SHF_GNU_RETAIN))))
14463 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
14464 return false;
14468 /* Allow the backend to mark additional target specific sections. */
14469 bed->gc_mark_extra_sections (info, gc_mark_hook);
14471 /* ... and mark SEC_EXCLUDE for those that go. */
14472 return elf_gc_sweep (abfd, info);
14475 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
14477 bool
14478 bfd_elf_gc_record_vtinherit (bfd *abfd,
14479 asection *sec,
14480 struct elf_link_hash_entry *h,
14481 bfd_vma offset)
14483 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
14484 struct elf_link_hash_entry **search, *child;
14485 size_t extsymcount;
14486 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14488 /* The sh_info field of the symtab header tells us where the
14489 external symbols start. We don't care about the local symbols at
14490 this point. */
14491 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
14492 if (!elf_bad_symtab (abfd))
14493 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
14495 sym_hashes = elf_sym_hashes (abfd);
14496 sym_hashes_end = PTR_ADD (sym_hashes, extsymcount);
14498 /* Hunt down the child symbol, which is in this section at the same
14499 offset as the relocation. */
14500 for (search = sym_hashes; search != sym_hashes_end; ++search)
14502 if ((child = *search) != NULL
14503 && (child->root.type == bfd_link_hash_defined
14504 || child->root.type == bfd_link_hash_defweak)
14505 && child->root.u.def.section == sec
14506 && child->root.u.def.value == offset)
14507 goto win;
14510 /* xgettext:c-format */
14511 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
14512 abfd, sec, (uint64_t) offset);
14513 bfd_set_error (bfd_error_invalid_operation);
14514 return false;
14516 win:
14517 if (!child->u2.vtable)
14519 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
14520 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
14521 if (!child->u2.vtable)
14522 return false;
14524 if (!h)
14526 /* This *should* only be the absolute section. It could potentially
14527 be that someone has defined a non-global vtable though, which
14528 would be bad. It isn't worth paging in the local symbols to be
14529 sure though; that case should simply be handled by the assembler. */
14531 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
14533 else
14534 child->u2.vtable->parent = h;
14536 return true;
14539 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
14541 bool
14542 bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec,
14543 struct elf_link_hash_entry *h,
14544 bfd_vma addend)
14546 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14547 unsigned int log_file_align = bed->s->log_file_align;
14549 if (!h)
14551 /* xgettext:c-format */
14552 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
14553 abfd, sec);
14554 bfd_set_error (bfd_error_bad_value);
14555 return false;
14558 if (!h->u2.vtable)
14560 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
14561 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
14562 if (!h->u2.vtable)
14563 return false;
14566 if (addend >= h->u2.vtable->size)
14568 size_t size, bytes, file_align;
14569 bool *ptr = h->u2.vtable->used;
14571 /* While the symbol is undefined, we have to be prepared to handle
14572 a zero size. */
14573 file_align = 1 << log_file_align;
14574 if (h->root.type == bfd_link_hash_undefined)
14575 size = addend + file_align;
14576 else
14578 size = h->size;
14579 if (addend >= size)
14581 /* Oops! We've got a reference past the defined end of
14582 the table. This is probably a bug -- shall we warn? */
14583 size = addend + file_align;
14586 size = (size + file_align - 1) & -file_align;
14588 /* Allocate one extra entry for use as a "done" flag for the
14589 consolidation pass. */
14590 bytes = ((size >> log_file_align) + 1) * sizeof (bool);
14592 if (ptr)
14594 ptr = (bool *) bfd_realloc (ptr - 1, bytes);
14596 if (ptr != NULL)
14598 size_t oldbytes;
14600 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
14601 * sizeof (bool));
14602 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
14605 else
14606 ptr = (bool *) bfd_zmalloc (bytes);
14608 if (ptr == NULL)
14609 return false;
14611 /* And arrange for that done flag to be at index -1. */
14612 h->u2.vtable->used = ptr + 1;
14613 h->u2.vtable->size = size;
14616 h->u2.vtable->used[addend >> log_file_align] = true;
14618 return true;
14621 /* Map an ELF section header flag to its corresponding string. */
14622 typedef struct
14624 char *flag_name;
14625 flagword flag_value;
14626 } elf_flags_to_name_table;
14628 static const elf_flags_to_name_table elf_flags_to_names [] =
14630 { "SHF_WRITE", SHF_WRITE },
14631 { "SHF_ALLOC", SHF_ALLOC },
14632 { "SHF_EXECINSTR", SHF_EXECINSTR },
14633 { "SHF_MERGE", SHF_MERGE },
14634 { "SHF_STRINGS", SHF_STRINGS },
14635 { "SHF_INFO_LINK", SHF_INFO_LINK},
14636 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
14637 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
14638 { "SHF_GROUP", SHF_GROUP },
14639 { "SHF_TLS", SHF_TLS },
14640 { "SHF_MASKOS", SHF_MASKOS },
14641 { "SHF_EXCLUDE", SHF_EXCLUDE },
14644 /* Returns TRUE if the section is to be included, otherwise FALSE. */
14645 bool
14646 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
14647 struct flag_info *flaginfo,
14648 asection *section)
14650 const bfd_vma sh_flags = elf_section_flags (section);
14652 if (!flaginfo->flags_initialized)
14654 bfd *obfd = info->output_bfd;
14655 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14656 struct flag_info_list *tf = flaginfo->flag_list;
14657 int with_hex = 0;
14658 int without_hex = 0;
14660 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
14662 unsigned i;
14663 flagword (*lookup) (char *);
14665 lookup = bed->elf_backend_lookup_section_flags_hook;
14666 if (lookup != NULL)
14668 flagword hexval = (*lookup) ((char *) tf->name);
14670 if (hexval != 0)
14672 if (tf->with == with_flags)
14673 with_hex |= hexval;
14674 else if (tf->with == without_flags)
14675 without_hex |= hexval;
14676 tf->valid = true;
14677 continue;
14680 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
14682 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
14684 if (tf->with == with_flags)
14685 with_hex |= elf_flags_to_names[i].flag_value;
14686 else if (tf->with == without_flags)
14687 without_hex |= elf_flags_to_names[i].flag_value;
14688 tf->valid = true;
14689 break;
14692 if (!tf->valid)
14694 info->callbacks->einfo
14695 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
14696 return false;
14699 flaginfo->flags_initialized = true;
14700 flaginfo->only_with_flags |= with_hex;
14701 flaginfo->not_with_flags |= without_hex;
14704 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
14705 return false;
14707 if ((flaginfo->not_with_flags & sh_flags) != 0)
14708 return false;
14710 return true;
14713 struct alloc_got_off_arg {
14714 bfd_vma gotoff;
14715 struct bfd_link_info *info;
14718 /* We need a special top-level link routine to convert got reference counts
14719 to real got offsets. */
14721 static bool
14722 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
14724 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
14725 bfd *obfd = gofarg->info->output_bfd;
14726 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14728 if (h->got.refcount > 0)
14730 h->got.offset = gofarg->gotoff;
14731 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
14733 else
14734 h->got.offset = (bfd_vma) -1;
14736 return true;
14739 /* And an accompanying bit to work out final got entry offsets once
14740 we're done. Should be called from final_link. */
14742 bool
14743 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
14744 struct bfd_link_info *info)
14746 bfd *i;
14747 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14748 bfd_vma gotoff;
14749 struct alloc_got_off_arg gofarg;
14751 BFD_ASSERT (abfd == info->output_bfd);
14753 if (! is_elf_hash_table (info->hash))
14754 return false;
14756 /* The GOT offset is relative to the .got section, but the GOT header is
14757 put into the .got.plt section, if the backend uses it. */
14758 if (bed->want_got_plt)
14759 gotoff = 0;
14760 else
14761 gotoff = bed->got_header_size;
14763 /* Do the local .got entries first. */
14764 for (i = info->input_bfds; i; i = i->link.next)
14766 bfd_signed_vma *local_got;
14767 size_t j, locsymcount;
14768 Elf_Internal_Shdr *symtab_hdr;
14770 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
14771 continue;
14773 local_got = elf_local_got_refcounts (i);
14774 if (!local_got)
14775 continue;
14777 symtab_hdr = &elf_tdata (i)->symtab_hdr;
14778 if (elf_bad_symtab (i))
14779 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
14780 else
14781 locsymcount = symtab_hdr->sh_info;
14783 for (j = 0; j < locsymcount; ++j)
14785 if (local_got[j] > 0)
14787 local_got[j] = gotoff;
14788 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
14790 else
14791 local_got[j] = (bfd_vma) -1;
14795 /* Then the global .got entries. .plt refcounts are handled by
14796 adjust_dynamic_symbol */
14797 gofarg.gotoff = gotoff;
14798 gofarg.info = info;
14799 elf_link_hash_traverse (elf_hash_table (info),
14800 elf_gc_allocate_got_offsets,
14801 &gofarg);
14802 return true;
14805 /* Many folk need no more in the way of final link than this, once
14806 got entry reference counting is enabled. */
14808 bool
14809 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
14811 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
14812 return false;
14814 /* Invoke the regular ELF backend linker to do all the work. */
14815 return bfd_elf_final_link (abfd, info);
14818 bool
14819 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
14821 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
14823 if (rcookie->bad_symtab)
14824 rcookie->rel = rcookie->rels;
14826 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
14828 unsigned long r_symndx;
14830 if (! rcookie->bad_symtab)
14831 if (rcookie->rel->r_offset > offset)
14832 return false;
14833 if (rcookie->rel->r_offset != offset)
14834 continue;
14836 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
14837 if (r_symndx == STN_UNDEF)
14838 return true;
14840 if (r_symndx >= rcookie->locsymcount
14841 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
14843 struct elf_link_hash_entry *h;
14845 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
14847 while (h->root.type == bfd_link_hash_indirect
14848 || h->root.type == bfd_link_hash_warning)
14849 h = (struct elf_link_hash_entry *) h->root.u.i.link;
14851 if ((h->root.type == bfd_link_hash_defined
14852 || h->root.type == bfd_link_hash_defweak)
14853 && (h->root.u.def.section->owner != rcookie->abfd
14854 || h->root.u.def.section->kept_section != NULL
14855 || discarded_section (h->root.u.def.section)))
14856 return true;
14858 else
14860 /* It's not a relocation against a global symbol,
14861 but it could be a relocation against a local
14862 symbol for a discarded section. */
14863 asection *isec;
14864 Elf_Internal_Sym *isym;
14866 /* Need to: get the symbol; get the section. */
14867 isym = &rcookie->locsyms[r_symndx];
14868 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
14869 if (isec != NULL
14870 && (isec->kept_section != NULL
14871 || discarded_section (isec)))
14872 return true;
14874 return false;
14876 return false;
14879 /* Discard unneeded references to discarded sections.
14880 Returns -1 on error, 1 if any section's size was changed, 0 if
14881 nothing changed. This function assumes that the relocations are in
14882 sorted order, which is true for all known assemblers. */
14885 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
14887 struct elf_reloc_cookie cookie;
14888 asection *o;
14889 bfd *abfd;
14890 int changed = 0;
14892 if (info->traditional_format
14893 || !is_elf_hash_table (info->hash))
14894 return 0;
14896 o = bfd_get_section_by_name (output_bfd, ".stab");
14897 if (o != NULL)
14899 asection *i;
14901 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14903 if (i->size == 0
14904 || i->reloc_count == 0
14905 || i->sec_info_type != SEC_INFO_TYPE_STABS)
14906 continue;
14908 abfd = i->owner;
14909 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14910 continue;
14912 if (!init_reloc_cookie_for_section (&cookie, info, i))
14913 return -1;
14915 if (_bfd_discard_section_stabs (abfd, i,
14916 elf_section_data (i)->sec_info,
14917 bfd_elf_reloc_symbol_deleted_p,
14918 &cookie))
14919 changed = 1;
14921 fini_reloc_cookie_for_section (&cookie, i);
14925 o = NULL;
14926 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
14927 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
14928 if (o != NULL)
14930 asection *i;
14931 int eh_changed = 0;
14932 unsigned int eh_alignment; /* Octets. */
14934 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14936 if (i->size == 0)
14937 continue;
14939 abfd = i->owner;
14940 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14941 continue;
14943 if (!init_reloc_cookie_for_section (&cookie, info, i))
14944 return -1;
14946 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
14947 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
14948 bfd_elf_reloc_symbol_deleted_p,
14949 &cookie))
14951 eh_changed = 1;
14952 if (i->size != i->rawsize)
14953 changed = 1;
14956 fini_reloc_cookie_for_section (&cookie, i);
14959 eh_alignment = ((1 << o->alignment_power)
14960 * bfd_octets_per_byte (output_bfd, o));
14961 /* Skip over zero terminator, and prevent empty sections from
14962 adding alignment padding at the end. */
14963 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
14964 if (i->size == 0)
14965 i->flags |= SEC_EXCLUDE;
14966 else if (i->size > 4)
14967 break;
14968 /* The last non-empty eh_frame section doesn't need padding. */
14969 if (i != NULL)
14970 i = i->map_tail.s;
14971 /* Any prior sections must pad the last FDE out to the output
14972 section alignment. Otherwise we might have zero padding
14973 between sections, which would be seen as a terminator. */
14974 for (; i != NULL; i = i->map_tail.s)
14975 if (i->size == 4)
14976 /* All but the last zero terminator should have been removed. */
14977 BFD_FAIL ();
14978 else
14980 bfd_size_type size
14981 = (i->size + eh_alignment - 1) & -eh_alignment;
14982 if (i->size != size)
14984 i->size = size;
14985 changed = 1;
14986 eh_changed = 1;
14989 if (eh_changed)
14990 elf_link_hash_traverse (elf_hash_table (info),
14991 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
14994 o = bfd_get_section_by_name (output_bfd, ".sframe");
14995 if (o != NULL)
14997 asection *i;
14999 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
15001 if (i->size == 0)
15002 continue;
15004 abfd = i->owner;
15005 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
15006 continue;
15008 if (!init_reloc_cookie_for_section (&cookie, info, i))
15009 return -1;
15011 if (_bfd_elf_parse_sframe (abfd, info, i, &cookie))
15013 if (_bfd_elf_discard_section_sframe (i,
15014 bfd_elf_reloc_symbol_deleted_p,
15015 &cookie))
15017 if (i->size != i->rawsize)
15018 changed = 1;
15021 fini_reloc_cookie_for_section (&cookie, i);
15023 /* Update the reference to the output .sframe section. Used to
15024 determine later if PT_GNU_SFRAME segment is to be generated. */
15025 if (!_bfd_elf_set_section_sframe (output_bfd, info))
15026 return -1;
15029 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
15031 const struct elf_backend_data *bed;
15032 asection *s;
15034 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
15035 continue;
15036 s = abfd->sections;
15037 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
15038 continue;
15040 bed = get_elf_backend_data (abfd);
15042 if (bed->elf_backend_discard_info != NULL)
15044 if (!init_reloc_cookie (&cookie, info, abfd))
15045 return -1;
15047 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
15048 changed = 1;
15050 fini_reloc_cookie (&cookie, abfd);
15054 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
15055 _bfd_elf_end_eh_frame_parsing (info);
15057 if (info->eh_frame_hdr_type
15058 && !bfd_link_relocatable (info)
15059 && _bfd_elf_discard_section_eh_frame_hdr (info))
15060 changed = 1;
15062 return changed;
15065 bool
15066 _bfd_elf_section_already_linked (bfd *abfd,
15067 asection *sec,
15068 struct bfd_link_info *info)
15070 flagword flags;
15071 const char *name, *key;
15072 struct bfd_section_already_linked *l;
15073 struct bfd_section_already_linked_hash_entry *already_linked_list;
15075 if (sec->output_section == bfd_abs_section_ptr)
15076 return false;
15078 flags = sec->flags;
15080 /* Return if it isn't a linkonce section. A comdat group section
15081 also has SEC_LINK_ONCE set. */
15082 if ((flags & SEC_LINK_ONCE) == 0)
15083 return false;
15085 /* Don't put group member sections on our list of already linked
15086 sections. They are handled as a group via their group section. */
15087 if (elf_sec_group (sec) != NULL)
15088 return false;
15090 /* For a SHT_GROUP section, use the group signature as the key. */
15091 name = sec->name;
15092 if ((flags & SEC_GROUP) != 0
15093 && elf_next_in_group (sec) != NULL
15094 && elf_group_name (elf_next_in_group (sec)) != NULL)
15095 key = elf_group_name (elf_next_in_group (sec));
15096 else
15098 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
15099 if (startswith (name, ".gnu.linkonce.")
15100 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
15101 key++;
15102 else
15103 /* Must be a user linkonce section that doesn't follow gcc's
15104 naming convention. In this case we won't be matching
15105 single member groups. */
15106 key = name;
15109 already_linked_list = bfd_section_already_linked_table_lookup (key);
15111 for (l = already_linked_list->entry; l != NULL; l = l->next)
15113 /* We may have 2 different types of sections on the list: group
15114 sections with a signature of <key> (<key> is some string),
15115 and linkonce sections named .gnu.linkonce.<type>.<key>.
15116 Match like sections. LTO plugin sections are an exception.
15117 They are always named .gnu.linkonce.t.<key> and match either
15118 type of section. */
15119 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
15120 && ((flags & SEC_GROUP) != 0
15121 || strcmp (name, l->sec->name) == 0))
15122 || (l->sec->owner->flags & BFD_PLUGIN) != 0
15123 || (sec->owner->flags & BFD_PLUGIN) != 0)
15125 /* The section has already been linked. See if we should
15126 issue a warning. */
15127 if (!_bfd_handle_already_linked (sec, l, info))
15128 return false;
15130 if (flags & SEC_GROUP)
15132 asection *first = elf_next_in_group (sec);
15133 asection *s = first;
15135 while (s != NULL)
15137 s->output_section = bfd_abs_section_ptr;
15138 /* Record which group discards it. */
15139 s->kept_section = l->sec;
15140 s = elf_next_in_group (s);
15141 /* These lists are circular. */
15142 if (s == first)
15143 break;
15147 return true;
15151 /* A single member comdat group section may be discarded by a
15152 linkonce section and vice versa. */
15153 if ((flags & SEC_GROUP) != 0)
15155 asection *first = elf_next_in_group (sec);
15157 if (first != NULL && elf_next_in_group (first) == first)
15158 /* Check this single member group against linkonce sections. */
15159 for (l = already_linked_list->entry; l != NULL; l = l->next)
15160 if ((l->sec->flags & SEC_GROUP) == 0
15161 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
15163 first->output_section = bfd_abs_section_ptr;
15164 first->kept_section = l->sec;
15165 sec->output_section = bfd_abs_section_ptr;
15166 break;
15169 else
15170 /* Check this linkonce section against single member groups. */
15171 for (l = already_linked_list->entry; l != NULL; l = l->next)
15172 if (l->sec->flags & SEC_GROUP)
15174 asection *first = elf_next_in_group (l->sec);
15176 if (first != NULL
15177 && elf_next_in_group (first) == first
15178 && bfd_elf_match_symbols_in_sections (first, sec, info))
15180 sec->output_section = bfd_abs_section_ptr;
15181 sec->kept_section = first;
15182 break;
15186 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
15187 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
15188 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
15189 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
15190 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
15191 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
15192 `.gnu.linkonce.t.F' section from a different bfd not requiring any
15193 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
15194 The reverse order cannot happen as there is never a bfd with only the
15195 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
15196 matter as here were are looking only for cross-bfd sections. */
15198 if ((flags & SEC_GROUP) == 0 && startswith (name, ".gnu.linkonce.r."))
15199 for (l = already_linked_list->entry; l != NULL; l = l->next)
15200 if ((l->sec->flags & SEC_GROUP) == 0
15201 && startswith (l->sec->name, ".gnu.linkonce.t."))
15203 if (abfd != l->sec->owner)
15204 sec->output_section = bfd_abs_section_ptr;
15205 break;
15208 /* This is the first section with this name. Record it. */
15209 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
15210 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
15211 return sec->output_section == bfd_abs_section_ptr;
15214 bool
15215 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
15217 return sym->st_shndx == SHN_COMMON;
15220 unsigned int
15221 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
15223 return SHN_COMMON;
15226 asection *
15227 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
15229 return bfd_com_section_ptr;
15232 bfd_vma
15233 _bfd_elf_default_got_elt_size (bfd *abfd,
15234 struct bfd_link_info *info ATTRIBUTE_UNUSED,
15235 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
15236 bfd *ibfd ATTRIBUTE_UNUSED,
15237 unsigned long symndx ATTRIBUTE_UNUSED)
15239 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15240 return bed->s->arch_size / 8;
15243 /* Routines to support the creation of dynamic relocs. */
15245 /* Returns the name of the dynamic reloc section associated with SEC. */
15247 static const char *
15248 get_dynamic_reloc_section_name (bfd * abfd,
15249 asection * sec,
15250 bool is_rela)
15252 char *name;
15253 const char *old_name = bfd_section_name (sec);
15254 const char *prefix = is_rela ? ".rela" : ".rel";
15256 if (old_name == NULL)
15257 return NULL;
15259 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
15260 sprintf (name, "%s%s", prefix, old_name);
15262 return name;
15265 /* Returns the dynamic reloc section associated with SEC.
15266 If necessary compute the name of the dynamic reloc section based
15267 on SEC's name (looked up in ABFD's string table) and the setting
15268 of IS_RELA. */
15270 asection *
15271 _bfd_elf_get_dynamic_reloc_section (bfd *abfd,
15272 asection *sec,
15273 bool is_rela)
15275 asection *reloc_sec = elf_section_data (sec)->sreloc;
15277 if (reloc_sec == NULL)
15279 const char *name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
15281 if (name != NULL)
15283 reloc_sec = bfd_get_linker_section (abfd, name);
15285 if (reloc_sec != NULL)
15286 elf_section_data (sec)->sreloc = reloc_sec;
15290 return reloc_sec;
15293 /* Returns the dynamic reloc section associated with SEC. If the
15294 section does not exist it is created and attached to the DYNOBJ
15295 bfd and stored in the SRELOC field of SEC's elf_section_data
15296 structure.
15298 ALIGNMENT is the alignment for the newly created section and
15299 IS_RELA defines whether the name should be .rela.<SEC's name>
15300 or .rel.<SEC's name>. The section name is looked up in the
15301 string table associated with ABFD. */
15303 asection *
15304 _bfd_elf_make_dynamic_reloc_section (asection *sec,
15305 bfd *dynobj,
15306 unsigned int alignment,
15307 bfd *abfd,
15308 bool is_rela)
15310 asection * reloc_sec = elf_section_data (sec)->sreloc;
15312 if (reloc_sec == NULL)
15314 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
15316 if (name == NULL)
15317 return NULL;
15319 reloc_sec = bfd_get_linker_section (dynobj, name);
15321 if (reloc_sec == NULL)
15323 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
15324 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
15325 if ((sec->flags & SEC_ALLOC) != 0)
15326 flags |= SEC_ALLOC | SEC_LOAD;
15328 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
15329 if (reloc_sec != NULL)
15331 /* _bfd_elf_get_sec_type_attr chooses a section type by
15332 name. Override as it may be wrong, eg. for a user
15333 section named "auto" we'll get ".relauto" which is
15334 seen to be a .rela section. */
15335 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
15336 if (!bfd_set_section_alignment (reloc_sec, alignment))
15337 reloc_sec = NULL;
15341 elf_section_data (sec)->sreloc = reloc_sec;
15344 return reloc_sec;
15347 /* Copy the ELF symbol type and other attributes for a linker script
15348 assignment from HSRC to HDEST. Generally this should be treated as
15349 if we found a strong non-dynamic definition for HDEST (except that
15350 ld ignores multiple definition errors). */
15351 void
15352 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
15353 struct bfd_link_hash_entry *hdest,
15354 struct bfd_link_hash_entry *hsrc)
15356 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
15357 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
15358 Elf_Internal_Sym isym;
15360 ehdest->type = ehsrc->type;
15361 ehdest->target_internal = ehsrc->target_internal;
15363 isym.st_other = ehsrc->other;
15364 elf_merge_st_other (abfd, ehdest, isym.st_other, NULL, true, false);
15367 /* Append a RELA relocation REL to section S in BFD. */
15369 void
15370 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15372 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15373 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
15374 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
15375 bed->s->swap_reloca_out (abfd, rel, loc);
15378 /* Append a REL relocation REL to section S in BFD. */
15380 void
15381 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15383 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15384 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
15385 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
15386 bed->s->swap_reloc_out (abfd, rel, loc);
15389 /* Define __start, __stop, .startof. or .sizeof. symbol. */
15391 struct bfd_link_hash_entry *
15392 bfd_elf_define_start_stop (struct bfd_link_info *info,
15393 const char *symbol, asection *sec)
15395 struct elf_link_hash_entry *h;
15397 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
15398 false, false, true);
15399 /* NB: Common symbols will be turned into definition later. */
15400 if (h != NULL
15401 && !h->root.ldscript_def
15402 && (h->root.type == bfd_link_hash_undefined
15403 || h->root.type == bfd_link_hash_undefweak
15404 || ((h->ref_regular || h->def_dynamic)
15405 && !h->def_regular
15406 && h->root.type != bfd_link_hash_common)))
15408 bool was_dynamic = h->ref_dynamic || h->def_dynamic;
15409 h->verinfo.verdef = NULL;
15410 h->root.type = bfd_link_hash_defined;
15411 h->root.u.def.section = sec;
15412 h->root.u.def.value = 0;
15413 h->def_regular = 1;
15414 h->def_dynamic = 0;
15415 h->start_stop = 1;
15416 h->u2.start_stop_section = sec;
15417 if (symbol[0] == '.')
15419 /* .startof. and .sizeof. symbols are local. */
15420 const struct elf_backend_data *bed;
15421 bed = get_elf_backend_data (info->output_bfd);
15422 (*bed->elf_backend_hide_symbol) (info, h, true);
15424 else
15426 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
15427 h->other = ((h->other & ~ELF_ST_VISIBILITY (-1))
15428 | info->start_stop_visibility);
15429 if (was_dynamic)
15430 bfd_elf_link_record_dynamic_symbol (info, h);
15432 return &h->root;
15434 return NULL;
15437 /* Find dynamic relocs for H that apply to read-only sections. */
15439 asection *
15440 _bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry *h)
15442 struct elf_dyn_relocs *p;
15444 for (p = h->dyn_relocs; p != NULL; p = p->next)
15446 asection *s = p->sec->output_section;
15448 if (s != NULL && (s->flags & SEC_READONLY) != 0)
15449 return p->sec;
15451 return NULL;
15454 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
15455 read-only sections. */
15457 bool
15458 _bfd_elf_maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
15460 asection *sec;
15462 if (h->root.type == bfd_link_hash_indirect)
15463 return true;
15465 sec = _bfd_elf_readonly_dynrelocs (h);
15466 if (sec != NULL)
15468 struct bfd_link_info *info = (struct bfd_link_info *) inf;
15470 info->flags |= DF_TEXTREL;
15471 /* xgettext:c-format */
15472 info->callbacks->minfo (_("%pB: dynamic relocation against `%pT' "
15473 "in read-only section `%pA'\n"),
15474 sec->owner, h->root.root.string, sec);
15476 if (bfd_link_textrel_check (info))
15477 /* xgettext:c-format */
15478 info->callbacks->einfo (_("%P: %pB: warning: relocation against `%s' "
15479 "in read-only section `%pA'\n"),
15480 sec->owner, h->root.root.string, sec);
15482 /* Not an error, just cut short the traversal. */
15483 return false;
15485 return true;
15488 /* Add dynamic tags. */
15490 bool
15491 _bfd_elf_add_dynamic_tags (bfd *output_bfd, struct bfd_link_info *info,
15492 bool need_dynamic_reloc)
15494 struct elf_link_hash_table *htab = elf_hash_table (info);
15496 if (htab->dynamic_sections_created)
15498 /* Add some entries to the .dynamic section. We fill in the
15499 values later, in finish_dynamic_sections, but we must add
15500 the entries now so that we get the correct size for the
15501 .dynamic section. The DT_DEBUG entry is filled in by the
15502 dynamic linker and used by the debugger. */
15503 #define add_dynamic_entry(TAG, VAL) \
15504 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
15506 const struct elf_backend_data *bed
15507 = get_elf_backend_data (output_bfd);
15509 if (bfd_link_executable (info))
15511 if (!add_dynamic_entry (DT_DEBUG, 0))
15512 return false;
15515 if (htab->dt_pltgot_required || htab->splt->size != 0)
15517 /* DT_PLTGOT is used by prelink even if there is no PLT
15518 relocation. */
15519 if (!add_dynamic_entry (DT_PLTGOT, 0))
15520 return false;
15523 if (htab->dt_jmprel_required || htab->srelplt->size != 0)
15525 if (!add_dynamic_entry (DT_PLTRELSZ, 0)
15526 || !add_dynamic_entry (DT_PLTREL,
15527 (bed->rela_plts_and_copies_p
15528 ? DT_RELA : DT_REL))
15529 || !add_dynamic_entry (DT_JMPREL, 0))
15530 return false;
15533 if (htab->tlsdesc_plt
15534 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
15535 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
15536 return false;
15538 if (need_dynamic_reloc)
15540 if (bed->rela_plts_and_copies_p)
15542 if (!add_dynamic_entry (DT_RELA, 0)
15543 || !add_dynamic_entry (DT_RELASZ, 0)
15544 || !add_dynamic_entry (DT_RELAENT,
15545 bed->s->sizeof_rela))
15546 return false;
15548 else
15550 if (!add_dynamic_entry (DT_REL, 0)
15551 || !add_dynamic_entry (DT_RELSZ, 0)
15552 || !add_dynamic_entry (DT_RELENT,
15553 bed->s->sizeof_rel))
15554 return false;
15557 /* If any dynamic relocs apply to a read-only section,
15558 then we need a DT_TEXTREL entry. */
15559 if ((info->flags & DF_TEXTREL) == 0)
15560 elf_link_hash_traverse (htab, _bfd_elf_maybe_set_textrel,
15561 info);
15563 if ((info->flags & DF_TEXTREL) != 0)
15565 if (htab->ifunc_resolvers)
15566 info->callbacks->einfo
15567 (_("%P: warning: GNU indirect functions with DT_TEXTREL "
15568 "may result in a segfault at runtime; recompile with %s\n"),
15569 bfd_link_dll (info) ? "-fPIC" : "-fPIE");
15571 if (!add_dynamic_entry (DT_TEXTREL, 0))
15572 return false;
15576 #undef add_dynamic_entry
15578 return true;