Automatic date update in version.in
[binutils-gdb.git] / bfd / elflink.c
1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2021 Free Software Foundation, Inc.
3
4 This file is part of BFD, the Binary File Descriptor library.
5
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.
10
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.
15
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. */
20
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
34
35 #include <limits.h>
36 #ifndef CHAR_BIT
37 #define CHAR_BIT 8
38 #endif
39
40 /* This struct is used to pass information to routines called via
41 elf_link_hash_traverse which must return failure. */
42
43 struct elf_info_failed
44 {
45 struct bfd_link_info *info;
46 bool failed;
47 };
48
49 /* This structure is used to pass information to
50 _bfd_elf_link_find_version_dependencies. */
51
52 struct elf_find_verdep_info
53 {
54 /* General link information. */
55 struct bfd_link_info *info;
56 /* The number of dependencies. */
57 unsigned int vers;
58 /* Whether we had a failure. */
59 bool failed;
60 };
61
62 static bool _bfd_elf_fix_symbol_flags
63 (struct elf_link_hash_entry *, struct elf_info_failed *);
64
65 asection *
66 _bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
67 unsigned long r_symndx,
68 bool discard)
69 {
70 if (r_symndx >= cookie->locsymcount
71 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
72 {
73 struct elf_link_hash_entry *h;
74
75 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
76
77 while (h->root.type == bfd_link_hash_indirect
78 || h->root.type == bfd_link_hash_warning)
79 h = (struct elf_link_hash_entry *) h->root.u.i.link;
80
81 if ((h->root.type == bfd_link_hash_defined
82 || h->root.type == bfd_link_hash_defweak)
83 && discarded_section (h->root.u.def.section))
84 return h->root.u.def.section;
85 else
86 return NULL;
87 }
88 else
89 {
90 /* It's not a relocation against a global symbol,
91 but it could be a relocation against a local
92 symbol for a discarded section. */
93 asection *isec;
94 Elf_Internal_Sym *isym;
95
96 /* Need to: get the symbol; get the section. */
97 isym = &cookie->locsyms[r_symndx];
98 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
99 if (isec != NULL
100 && discard ? discarded_section (isec) : 1)
101 return isec;
102 }
103 return NULL;
104 }
105
106 /* Define a symbol in a dynamic linkage section. */
107
108 struct elf_link_hash_entry *
109 _bfd_elf_define_linkage_sym (bfd *abfd,
110 struct bfd_link_info *info,
111 asection *sec,
112 const char *name)
113 {
114 struct elf_link_hash_entry *h;
115 struct bfd_link_hash_entry *bh;
116 const struct elf_backend_data *bed;
117
118 h = elf_link_hash_lookup (elf_hash_table (info), name, false, false, false);
119 if (h != NULL)
120 {
121 /* Zap symbol defined in an as-needed lib that wasn't linked.
122 This is a symptom of a larger problem: Absolute symbols
123 defined in shared libraries can't be overridden, because we
124 lose the link to the bfd which is via the symbol section. */
125 h->root.type = bfd_link_hash_new;
126 bh = &h->root;
127 }
128 else
129 bh = NULL;
130
131 bed = get_elf_backend_data (abfd);
132 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
133 sec, 0, NULL, false, bed->collect,
134 &bh))
135 return NULL;
136 h = (struct elf_link_hash_entry *) bh;
137 BFD_ASSERT (h != NULL);
138 h->def_regular = 1;
139 h->non_elf = 0;
140 h->root.linker_def = 1;
141 h->type = STT_OBJECT;
142 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
143 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
144
145 (*bed->elf_backend_hide_symbol) (info, h, true);
146 return h;
147 }
148
149 bool
150 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
151 {
152 flagword flags;
153 asection *s;
154 struct elf_link_hash_entry *h;
155 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
156 struct elf_link_hash_table *htab = elf_hash_table (info);
157
158 /* This function may be called more than once. */
159 if (htab->sgot != NULL)
160 return true;
161
162 flags = bed->dynamic_sec_flags;
163
164 s = bfd_make_section_anyway_with_flags (abfd,
165 (bed->rela_plts_and_copies_p
166 ? ".rela.got" : ".rel.got"),
167 (bed->dynamic_sec_flags
168 | SEC_READONLY));
169 if (s == NULL
170 || !bfd_set_section_alignment (s, bed->s->log_file_align))
171 return false;
172 htab->srelgot = s;
173
174 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
175 if (s == NULL
176 || !bfd_set_section_alignment (s, bed->s->log_file_align))
177 return false;
178 htab->sgot = s;
179
180 if (bed->want_got_plt)
181 {
182 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
183 if (s == NULL
184 || !bfd_set_section_alignment (s, bed->s->log_file_align))
185 return false;
186 htab->sgotplt = s;
187 }
188
189 /* The first bit of the global offset table is the header. */
190 s->size += bed->got_header_size;
191
192 if (bed->want_got_sym)
193 {
194 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
195 (or .got.plt) section. We don't do this in the linker script
196 because we don't want to define the symbol if we are not creating
197 a global offset table. */
198 h = _bfd_elf_define_linkage_sym (abfd, info, s,
199 "_GLOBAL_OFFSET_TABLE_");
200 elf_hash_table (info)->hgot = h;
201 if (h == NULL)
202 return false;
203 }
204
205 return true;
206 }
207 \f
208 /* Create a strtab to hold the dynamic symbol names. */
209 static bool
210 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
211 {
212 struct elf_link_hash_table *hash_table;
213
214 hash_table = elf_hash_table (info);
215 if (hash_table->dynobj == NULL)
216 {
217 /* We may not set dynobj, an input file holding linker created
218 dynamic sections to abfd, which may be a dynamic object with
219 its own dynamic sections. We need to find a normal input file
220 to hold linker created sections if possible. */
221 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
222 {
223 bfd *ibfd;
224 asection *s;
225 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
226 if ((ibfd->flags
227 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
228 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
229 && elf_object_id (ibfd) == elf_hash_table_id (hash_table)
230 && !((s = ibfd->sections) != NULL
231 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS))
232 {
233 abfd = ibfd;
234 break;
235 }
236 }
237 hash_table->dynobj = abfd;
238 }
239
240 if (hash_table->dynstr == NULL)
241 {
242 hash_table->dynstr = _bfd_elf_strtab_init ();
243 if (hash_table->dynstr == NULL)
244 return false;
245 }
246 return true;
247 }
248
249 /* Create some sections which will be filled in with dynamic linking
250 information. ABFD is an input file which requires dynamic sections
251 to be created. The dynamic sections take up virtual memory space
252 when the final executable is run, so we need to create them before
253 addresses are assigned to the output sections. We work out the
254 actual contents and size of these sections later. */
255
256 bool
257 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
258 {
259 flagword flags;
260 asection *s;
261 const struct elf_backend_data *bed;
262 struct elf_link_hash_entry *h;
263
264 if (! is_elf_hash_table (info->hash))
265 return false;
266
267 if (elf_hash_table (info)->dynamic_sections_created)
268 return true;
269
270 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
271 return false;
272
273 abfd = elf_hash_table (info)->dynobj;
274 bed = get_elf_backend_data (abfd);
275
276 flags = bed->dynamic_sec_flags;
277
278 /* A dynamically linked executable has a .interp section, but a
279 shared library does not. */
280 if (bfd_link_executable (info) && !info->nointerp)
281 {
282 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
283 flags | SEC_READONLY);
284 if (s == NULL)
285 return false;
286 }
287
288 /* Create sections to hold version informations. These are removed
289 if they are not needed. */
290 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
291 flags | SEC_READONLY);
292 if (s == NULL
293 || !bfd_set_section_alignment (s, bed->s->log_file_align))
294 return false;
295
296 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
297 flags | SEC_READONLY);
298 if (s == NULL
299 || !bfd_set_section_alignment (s, 1))
300 return false;
301
302 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
303 flags | SEC_READONLY);
304 if (s == NULL
305 || !bfd_set_section_alignment (s, bed->s->log_file_align))
306 return false;
307
308 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
309 flags | SEC_READONLY);
310 if (s == NULL
311 || !bfd_set_section_alignment (s, bed->s->log_file_align))
312 return false;
313 elf_hash_table (info)->dynsym = s;
314
315 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
316 flags | SEC_READONLY);
317 if (s == NULL)
318 return false;
319
320 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
321 if (s == NULL
322 || !bfd_set_section_alignment (s, bed->s->log_file_align))
323 return false;
324
325 /* The special symbol _DYNAMIC is always set to the start of the
326 .dynamic section. We could set _DYNAMIC in a linker script, but we
327 only want to define it if we are, in fact, creating a .dynamic
328 section. We don't want to define it if there is no .dynamic
329 section, since on some ELF platforms the start up code examines it
330 to decide how to initialize the process. */
331 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
332 elf_hash_table (info)->hdynamic = h;
333 if (h == NULL)
334 return false;
335
336 if (info->emit_hash)
337 {
338 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
339 flags | SEC_READONLY);
340 if (s == NULL
341 || !bfd_set_section_alignment (s, bed->s->log_file_align))
342 return false;
343 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
344 }
345
346 if (info->emit_gnu_hash && bed->record_xhash_symbol == NULL)
347 {
348 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
349 flags | SEC_READONLY);
350 if (s == NULL
351 || !bfd_set_section_alignment (s, bed->s->log_file_align))
352 return false;
353 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
354 4 32-bit words followed by variable count of 64-bit words, then
355 variable count of 32-bit words. */
356 if (bed->s->arch_size == 64)
357 elf_section_data (s)->this_hdr.sh_entsize = 0;
358 else
359 elf_section_data (s)->this_hdr.sh_entsize = 4;
360 }
361
362 /* Let the backend create the rest of the sections. This lets the
363 backend set the right flags. The backend will normally create
364 the .got and .plt sections. */
365 if (bed->elf_backend_create_dynamic_sections == NULL
366 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
367 return false;
368
369 elf_hash_table (info)->dynamic_sections_created = true;
370
371 return true;
372 }
373
374 /* Create dynamic sections when linking against a dynamic object. */
375
376 bool
377 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
378 {
379 flagword flags, pltflags;
380 struct elf_link_hash_entry *h;
381 asection *s;
382 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
383 struct elf_link_hash_table *htab = elf_hash_table (info);
384
385 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
386 .rel[a].bss sections. */
387 flags = bed->dynamic_sec_flags;
388
389 pltflags = flags;
390 if (bed->plt_not_loaded)
391 /* We do not clear SEC_ALLOC here because we still want the OS to
392 allocate space for the section; it's just that there's nothing
393 to read in from the object file. */
394 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
395 else
396 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
397 if (bed->plt_readonly)
398 pltflags |= SEC_READONLY;
399
400 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
401 if (s == NULL
402 || !bfd_set_section_alignment (s, bed->plt_alignment))
403 return false;
404 htab->splt = s;
405
406 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
407 .plt section. */
408 if (bed->want_plt_sym)
409 {
410 h = _bfd_elf_define_linkage_sym (abfd, info, s,
411 "_PROCEDURE_LINKAGE_TABLE_");
412 elf_hash_table (info)->hplt = h;
413 if (h == NULL)
414 return false;
415 }
416
417 s = bfd_make_section_anyway_with_flags (abfd,
418 (bed->rela_plts_and_copies_p
419 ? ".rela.plt" : ".rel.plt"),
420 flags | SEC_READONLY);
421 if (s == NULL
422 || !bfd_set_section_alignment (s, bed->s->log_file_align))
423 return false;
424 htab->srelplt = s;
425
426 if (! _bfd_elf_create_got_section (abfd, info))
427 return false;
428
429 if (bed->want_dynbss)
430 {
431 /* The .dynbss section is a place to put symbols which are defined
432 by dynamic objects, are referenced by regular objects, and are
433 not functions. We must allocate space for them in the process
434 image and use a R_*_COPY reloc to tell the dynamic linker to
435 initialize them at run time. The linker script puts the .dynbss
436 section into the .bss section of the final image. */
437 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
438 SEC_ALLOC | SEC_LINKER_CREATED);
439 if (s == NULL)
440 return false;
441 htab->sdynbss = s;
442
443 if (bed->want_dynrelro)
444 {
445 /* Similarly, but for symbols that were originally in read-only
446 sections. This section doesn't really need to have contents,
447 but make it like other .data.rel.ro sections. */
448 s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
449 flags);
450 if (s == NULL)
451 return false;
452 htab->sdynrelro = s;
453 }
454
455 /* The .rel[a].bss section holds copy relocs. This section is not
456 normally needed. We need to create it here, though, so that the
457 linker will map it to an output section. We can't just create it
458 only if we need it, because we will not know whether we need it
459 until we have seen all the input files, and the first time the
460 main linker code calls BFD after examining all the input files
461 (size_dynamic_sections) the input sections have already been
462 mapped to the output sections. If the section turns out not to
463 be needed, we can discard it later. We will never need this
464 section when generating a shared object, since they do not use
465 copy relocs. */
466 if (bfd_link_executable (info))
467 {
468 s = bfd_make_section_anyway_with_flags (abfd,
469 (bed->rela_plts_and_copies_p
470 ? ".rela.bss" : ".rel.bss"),
471 flags | SEC_READONLY);
472 if (s == NULL
473 || !bfd_set_section_alignment (s, bed->s->log_file_align))
474 return false;
475 htab->srelbss = s;
476
477 if (bed->want_dynrelro)
478 {
479 s = (bfd_make_section_anyway_with_flags
480 (abfd, (bed->rela_plts_and_copies_p
481 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
482 flags | SEC_READONLY));
483 if (s == NULL
484 || !bfd_set_section_alignment (s, bed->s->log_file_align))
485 return false;
486 htab->sreldynrelro = s;
487 }
488 }
489 }
490
491 return true;
492 }
493 \f
494 /* Record a new dynamic symbol. We record the dynamic symbols as we
495 read the input files, since we need to have a list of all of them
496 before we can determine the final sizes of the output sections.
497 Note that we may actually call this function even though we are not
498 going to output any dynamic symbols; in some cases we know that a
499 symbol should be in the dynamic symbol table, but only if there is
500 one. */
501
502 bool
503 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
504 struct elf_link_hash_entry *h)
505 {
506 if (h->dynindx == -1)
507 {
508 struct elf_strtab_hash *dynstr;
509 char *p;
510 const char *name;
511 size_t indx;
512
513 if (h->root.type == bfd_link_hash_defined
514 || h->root.type == bfd_link_hash_defweak)
515 {
516 /* An IR symbol should not be made dynamic. */
517 if (h->root.u.def.section != NULL
518 && h->root.u.def.section->owner != NULL
519 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)
520 return true;
521 }
522
523 /* XXX: The ABI draft says the linker must turn hidden and
524 internal symbols into STB_LOCAL symbols when producing the
525 DSO. However, if ld.so honors st_other in the dynamic table,
526 this would not be necessary. */
527 switch (ELF_ST_VISIBILITY (h->other))
528 {
529 case STV_INTERNAL:
530 case STV_HIDDEN:
531 if (h->root.type != bfd_link_hash_undefined
532 && h->root.type != bfd_link_hash_undefweak)
533 {
534 h->forced_local = 1;
535 if (!elf_hash_table (info)->is_relocatable_executable
536 || ((h->root.type == bfd_link_hash_defined
537 || h->root.type == bfd_link_hash_defweak)
538 && h->root.u.def.section->owner != NULL
539 && h->root.u.def.section->owner->no_export)
540 || (h->root.type == bfd_link_hash_common
541 && h->root.u.c.p->section->owner != NULL
542 && h->root.u.c.p->section->owner->no_export))
543 return true;
544 }
545
546 default:
547 break;
548 }
549
550 h->dynindx = elf_hash_table (info)->dynsymcount;
551 ++elf_hash_table (info)->dynsymcount;
552
553 dynstr = elf_hash_table (info)->dynstr;
554 if (dynstr == NULL)
555 {
556 /* Create a strtab to hold the dynamic symbol names. */
557 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
558 if (dynstr == NULL)
559 return false;
560 }
561
562 /* We don't put any version information in the dynamic string
563 table. */
564 name = h->root.root.string;
565 p = strchr (name, ELF_VER_CHR);
566 if (p != NULL)
567 /* We know that the p points into writable memory. In fact,
568 there are only a few symbols that have read-only names, being
569 those like _GLOBAL_OFFSET_TABLE_ that are created specially
570 by the backends. Most symbols will have names pointing into
571 an ELF string table read from a file, or to objalloc memory. */
572 *p = 0;
573
574 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
575
576 if (p != NULL)
577 *p = ELF_VER_CHR;
578
579 if (indx == (size_t) -1)
580 return false;
581 h->dynstr_index = indx;
582 }
583
584 return true;
585 }
586 \f
587 /* Mark a symbol dynamic. */
588
589 static void
590 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
591 struct elf_link_hash_entry *h,
592 Elf_Internal_Sym *sym)
593 {
594 struct bfd_elf_dynamic_list *d = info->dynamic_list;
595
596 /* It may be called more than once on the same H. */
597 if(h->dynamic || bfd_link_relocatable (info))
598 return;
599
600 if ((info->dynamic_data
601 && (h->type == STT_OBJECT
602 || h->type == STT_COMMON
603 || (sym != NULL
604 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
605 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
606 || (d != NULL
607 && h->non_elf
608 && (*d->match) (&d->head, NULL, h->root.root.string)))
609 {
610 h->dynamic = 1;
611 /* NB: If a symbol is made dynamic by --dynamic-list, it has
612 non-IR reference. */
613 h->root.non_ir_ref_dynamic = 1;
614 }
615 }
616
617 /* Record an assignment to a symbol made by a linker script. We need
618 this in case some dynamic object refers to this symbol. */
619
620 bool
621 bfd_elf_record_link_assignment (bfd *output_bfd,
622 struct bfd_link_info *info,
623 const char *name,
624 bool provide,
625 bool hidden)
626 {
627 struct elf_link_hash_entry *h, *hv;
628 struct elf_link_hash_table *htab;
629 const struct elf_backend_data *bed;
630
631 if (!is_elf_hash_table (info->hash))
632 return true;
633
634 htab = elf_hash_table (info);
635 h = elf_link_hash_lookup (htab, name, !provide, true, false);
636 if (h == NULL)
637 return provide;
638
639 if (h->root.type == bfd_link_hash_warning)
640 h = (struct elf_link_hash_entry *) h->root.u.i.link;
641
642 if (h->versioned == unknown)
643 {
644 /* Set versioned if symbol version is unknown. */
645 char *version = strrchr (name, ELF_VER_CHR);
646 if (version)
647 {
648 if (version > name && version[-1] != ELF_VER_CHR)
649 h->versioned = versioned_hidden;
650 else
651 h->versioned = versioned;
652 }
653 }
654
655 /* Symbols defined in a linker script but not referenced anywhere
656 else will have non_elf set. */
657 if (h->non_elf)
658 {
659 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
660 h->non_elf = 0;
661 }
662
663 switch (h->root.type)
664 {
665 case bfd_link_hash_defined:
666 case bfd_link_hash_defweak:
667 case bfd_link_hash_common:
668 break;
669 case bfd_link_hash_undefweak:
670 case bfd_link_hash_undefined:
671 /* Since we're defining the symbol, don't let it seem to have not
672 been defined. record_dynamic_symbol and size_dynamic_sections
673 may depend on this. */
674 h->root.type = bfd_link_hash_new;
675 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
676 bfd_link_repair_undef_list (&htab->root);
677 break;
678 case bfd_link_hash_new:
679 break;
680 case bfd_link_hash_indirect:
681 /* We had a versioned symbol in a dynamic library. We make the
682 the versioned symbol point to this one. */
683 bed = get_elf_backend_data (output_bfd);
684 hv = h;
685 while (hv->root.type == bfd_link_hash_indirect
686 || hv->root.type == bfd_link_hash_warning)
687 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
688 /* We don't need to update h->root.u since linker will set them
689 later. */
690 h->root.type = bfd_link_hash_undefined;
691 hv->root.type = bfd_link_hash_indirect;
692 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
693 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
694 break;
695 default:
696 BFD_FAIL ();
697 return false;
698 }
699
700 /* If this symbol is being provided by the linker script, and it is
701 currently defined by a dynamic object, but not by a regular
702 object, then mark it as undefined so that the generic linker will
703 force the correct value. */
704 if (provide
705 && h->def_dynamic
706 && !h->def_regular)
707 h->root.type = bfd_link_hash_undefined;
708
709 /* If this symbol is currently defined by a dynamic object, but not
710 by a regular object, then clear out any version information because
711 the symbol will not be associated with the dynamic object any
712 more. */
713 if (h->def_dynamic && !h->def_regular)
714 h->verinfo.verdef = NULL;
715
716 /* Make sure this symbol is not garbage collected. */
717 h->mark = 1;
718
719 h->def_regular = 1;
720
721 if (hidden)
722 {
723 bed = get_elf_backend_data (output_bfd);
724 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
725 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
726 (*bed->elf_backend_hide_symbol) (info, h, true);
727 }
728
729 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
730 and executables. */
731 if (!bfd_link_relocatable (info)
732 && h->dynindx != -1
733 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
734 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
735 h->forced_local = 1;
736
737 if ((h->def_dynamic
738 || h->ref_dynamic
739 || bfd_link_dll (info)
740 || elf_hash_table (info)->is_relocatable_executable)
741 && !h->forced_local
742 && h->dynindx == -1)
743 {
744 if (! bfd_elf_link_record_dynamic_symbol (info, h))
745 return false;
746
747 /* If this is a weak defined symbol, and we know a corresponding
748 real symbol from the same dynamic object, make sure the real
749 symbol is also made into a dynamic symbol. */
750 if (h->is_weakalias)
751 {
752 struct elf_link_hash_entry *def = weakdef (h);
753
754 if (def->dynindx == -1
755 && !bfd_elf_link_record_dynamic_symbol (info, def))
756 return false;
757 }
758 }
759
760 return true;
761 }
762
763 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
764 success, and 2 on a failure caused by attempting to record a symbol
765 in a discarded section, eg. a discarded link-once section symbol. */
766
767 int
768 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
769 bfd *input_bfd,
770 long input_indx)
771 {
772 size_t amt;
773 struct elf_link_local_dynamic_entry *entry;
774 struct elf_link_hash_table *eht;
775 struct elf_strtab_hash *dynstr;
776 size_t dynstr_index;
777 char *name;
778 Elf_External_Sym_Shndx eshndx;
779 char esym[sizeof (Elf64_External_Sym)];
780
781 if (! is_elf_hash_table (info->hash))
782 return 0;
783
784 /* See if the entry exists already. */
785 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
786 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
787 return 1;
788
789 amt = sizeof (*entry);
790 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
791 if (entry == NULL)
792 return 0;
793
794 /* Go find the symbol, so that we can find it's name. */
795 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
796 1, input_indx, &entry->isym, esym, &eshndx))
797 {
798 bfd_release (input_bfd, entry);
799 return 0;
800 }
801
802 if (entry->isym.st_shndx != SHN_UNDEF
803 && entry->isym.st_shndx < SHN_LORESERVE)
804 {
805 asection *s;
806
807 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
808 if (s == NULL || bfd_is_abs_section (s->output_section))
809 {
810 /* We can still bfd_release here as nothing has done another
811 bfd_alloc. We can't do this later in this function. */
812 bfd_release (input_bfd, entry);
813 return 2;
814 }
815 }
816
817 name = (bfd_elf_string_from_elf_section
818 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
819 entry->isym.st_name));
820
821 dynstr = elf_hash_table (info)->dynstr;
822 if (dynstr == NULL)
823 {
824 /* Create a strtab to hold the dynamic symbol names. */
825 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
826 if (dynstr == NULL)
827 return 0;
828 }
829
830 dynstr_index = _bfd_elf_strtab_add (dynstr, name, false);
831 if (dynstr_index == (size_t) -1)
832 return 0;
833 entry->isym.st_name = dynstr_index;
834
835 eht = elf_hash_table (info);
836
837 entry->next = eht->dynlocal;
838 eht->dynlocal = entry;
839 entry->input_bfd = input_bfd;
840 entry->input_indx = input_indx;
841 eht->dynsymcount++;
842
843 /* Whatever binding the symbol had before, it's now local. */
844 entry->isym.st_info
845 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
846
847 /* The dynindx will be set at the end of size_dynamic_sections. */
848
849 return 1;
850 }
851
852 /* Return the dynindex of a local dynamic symbol. */
853
854 long
855 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
856 bfd *input_bfd,
857 long input_indx)
858 {
859 struct elf_link_local_dynamic_entry *e;
860
861 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
862 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
863 return e->dynindx;
864 return -1;
865 }
866
867 /* This function is used to renumber the dynamic symbols, if some of
868 them are removed because they are marked as local. This is called
869 via elf_link_hash_traverse. */
870
871 static bool
872 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
873 void *data)
874 {
875 size_t *count = (size_t *) data;
876
877 if (h->forced_local)
878 return true;
879
880 if (h->dynindx != -1)
881 h->dynindx = ++(*count);
882
883 return true;
884 }
885
886
887 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
888 STB_LOCAL binding. */
889
890 static bool
891 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
892 void *data)
893 {
894 size_t *count = (size_t *) data;
895
896 if (!h->forced_local)
897 return true;
898
899 if (h->dynindx != -1)
900 h->dynindx = ++(*count);
901
902 return true;
903 }
904
905 /* Return true if the dynamic symbol for a given section should be
906 omitted when creating a shared library. */
907 bool
908 _bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED,
909 struct bfd_link_info *info,
910 asection *p)
911 {
912 struct elf_link_hash_table *htab;
913 asection *ip;
914
915 switch (elf_section_data (p)->this_hdr.sh_type)
916 {
917 case SHT_PROGBITS:
918 case SHT_NOBITS:
919 /* If sh_type is yet undecided, assume it could be
920 SHT_PROGBITS/SHT_NOBITS. */
921 case SHT_NULL:
922 htab = elf_hash_table (info);
923 if (htab->text_index_section != NULL)
924 return p != htab->text_index_section && p != htab->data_index_section;
925
926 return (htab->dynobj != NULL
927 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
928 && ip->output_section == p);
929
930 /* There shouldn't be section relative relocations
931 against any other section. */
932 default:
933 return true;
934 }
935 }
936
937 bool
938 _bfd_elf_omit_section_dynsym_all
939 (bfd *output_bfd ATTRIBUTE_UNUSED,
940 struct bfd_link_info *info ATTRIBUTE_UNUSED,
941 asection *p ATTRIBUTE_UNUSED)
942 {
943 return true;
944 }
945
946 /* Assign dynsym indices. In a shared library we generate a section
947 symbol for each output section, which come first. Next come symbols
948 which have been forced to local binding. Then all of the back-end
949 allocated local dynamic syms, followed by the rest of the global
950 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
951 (This prevents the early call before elf_backend_init_index_section
952 and strip_excluded_output_sections setting dynindx for sections
953 that are stripped.) */
954
955 static unsigned long
956 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
957 struct bfd_link_info *info,
958 unsigned long *section_sym_count)
959 {
960 unsigned long dynsymcount = 0;
961 bool do_sec = section_sym_count != NULL;
962
963 if (bfd_link_pic (info)
964 || elf_hash_table (info)->is_relocatable_executable)
965 {
966 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
967 asection *p;
968 for (p = output_bfd->sections; p ; p = p->next)
969 if ((p->flags & SEC_EXCLUDE) == 0
970 && (p->flags & SEC_ALLOC) != 0
971 && elf_hash_table (info)->dynamic_relocs
972 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
973 {
974 ++dynsymcount;
975 if (do_sec)
976 elf_section_data (p)->dynindx = dynsymcount;
977 }
978 else if (do_sec)
979 elf_section_data (p)->dynindx = 0;
980 }
981 if (do_sec)
982 *section_sym_count = dynsymcount;
983
984 elf_link_hash_traverse (elf_hash_table (info),
985 elf_link_renumber_local_hash_table_dynsyms,
986 &dynsymcount);
987
988 if (elf_hash_table (info)->dynlocal)
989 {
990 struct elf_link_local_dynamic_entry *p;
991 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
992 p->dynindx = ++dynsymcount;
993 }
994 elf_hash_table (info)->local_dynsymcount = dynsymcount;
995
996 elf_link_hash_traverse (elf_hash_table (info),
997 elf_link_renumber_hash_table_dynsyms,
998 &dynsymcount);
999
1000 /* There is an unused NULL entry at the head of the table which we
1001 must account for in our count even if the table is empty since it
1002 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
1003 .dynamic section. */
1004 dynsymcount++;
1005
1006 elf_hash_table (info)->dynsymcount = dynsymcount;
1007 return dynsymcount;
1008 }
1009
1010 /* Merge st_other field. */
1011
1012 static void
1013 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
1014 unsigned int st_other, asection *sec,
1015 bool definition, bool dynamic)
1016 {
1017 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1018
1019 /* If st_other has a processor-specific meaning, specific
1020 code might be needed here. */
1021 if (bed->elf_backend_merge_symbol_attribute)
1022 (*bed->elf_backend_merge_symbol_attribute) (h, st_other, definition,
1023 dynamic);
1024
1025 if (!dynamic)
1026 {
1027 unsigned symvis = ELF_ST_VISIBILITY (st_other);
1028 unsigned hvis = ELF_ST_VISIBILITY (h->other);
1029
1030 /* Keep the most constraining visibility. Leave the remainder
1031 of the st_other field to elf_backend_merge_symbol_attribute. */
1032 if (symvis - 1 < hvis - 1)
1033 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
1034 }
1035 else if (definition
1036 && ELF_ST_VISIBILITY (st_other) != STV_DEFAULT
1037 && (sec->flags & SEC_READONLY) == 0)
1038 h->protected_def = 1;
1039 }
1040
1041 /* This function is called when we want to merge a new symbol with an
1042 existing symbol. It handles the various cases which arise when we
1043 find a definition in a dynamic object, or when there is already a
1044 definition in a dynamic object. The new symbol is described by
1045 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1046 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1047 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1048 of an old common symbol. We set OVERRIDE if the old symbol is
1049 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1050 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1051 to change. By OK to change, we mean that we shouldn't warn if the
1052 type or size does change. */
1053
1054 static bool
1055 _bfd_elf_merge_symbol (bfd *abfd,
1056 struct bfd_link_info *info,
1057 const char *name,
1058 Elf_Internal_Sym *sym,
1059 asection **psec,
1060 bfd_vma *pvalue,
1061 struct elf_link_hash_entry **sym_hash,
1062 bfd **poldbfd,
1063 bool *pold_weak,
1064 unsigned int *pold_alignment,
1065 bool *skip,
1066 bfd **override,
1067 bool *type_change_ok,
1068 bool *size_change_ok,
1069 bool *matched)
1070 {
1071 asection *sec, *oldsec;
1072 struct elf_link_hash_entry *h;
1073 struct elf_link_hash_entry *hi;
1074 struct elf_link_hash_entry *flip;
1075 int bind;
1076 bfd *oldbfd;
1077 bool newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
1078 bool newweak, oldweak, newfunc, oldfunc;
1079 const struct elf_backend_data *bed;
1080 char *new_version;
1081 bool default_sym = *matched;
1082
1083 *skip = false;
1084 *override = NULL;
1085
1086 sec = *psec;
1087 bind = ELF_ST_BIND (sym->st_info);
1088
1089 if (! bfd_is_und_section (sec))
1090 h = elf_link_hash_lookup (elf_hash_table (info), name, true, false, false);
1091 else
1092 h = ((struct elf_link_hash_entry *)
1093 bfd_wrapped_link_hash_lookup (abfd, info, name, true, false, false));
1094 if (h == NULL)
1095 return false;
1096 *sym_hash = h;
1097
1098 bed = get_elf_backend_data (abfd);
1099
1100 /* NEW_VERSION is the symbol version of the new symbol. */
1101 if (h->versioned != unversioned)
1102 {
1103 /* Symbol version is unknown or versioned. */
1104 new_version = strrchr (name, ELF_VER_CHR);
1105 if (new_version)
1106 {
1107 if (h->versioned == unknown)
1108 {
1109 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1110 h->versioned = versioned_hidden;
1111 else
1112 h->versioned = versioned;
1113 }
1114 new_version += 1;
1115 if (new_version[0] == '\0')
1116 new_version = NULL;
1117 }
1118 else
1119 h->versioned = unversioned;
1120 }
1121 else
1122 new_version = NULL;
1123
1124 /* For merging, we only care about real symbols. But we need to make
1125 sure that indirect symbol dynamic flags are updated. */
1126 hi = h;
1127 while (h->root.type == bfd_link_hash_indirect
1128 || h->root.type == bfd_link_hash_warning)
1129 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1130
1131 if (!*matched)
1132 {
1133 if (hi == h || h->root.type == bfd_link_hash_new)
1134 *matched = true;
1135 else
1136 {
1137 /* OLD_HIDDEN is true if the existing symbol is only visible
1138 to the symbol with the same symbol version. NEW_HIDDEN is
1139 true if the new symbol is only visible to the symbol with
1140 the same symbol version. */
1141 bool old_hidden = h->versioned == versioned_hidden;
1142 bool new_hidden = hi->versioned == versioned_hidden;
1143 if (!old_hidden && !new_hidden)
1144 /* The new symbol matches the existing symbol if both
1145 aren't hidden. */
1146 *matched = true;
1147 else
1148 {
1149 /* OLD_VERSION is the symbol version of the existing
1150 symbol. */
1151 char *old_version;
1152
1153 if (h->versioned >= versioned)
1154 old_version = strrchr (h->root.root.string,
1155 ELF_VER_CHR) + 1;
1156 else
1157 old_version = NULL;
1158
1159 /* The new symbol matches the existing symbol if they
1160 have the same symbol version. */
1161 *matched = (old_version == new_version
1162 || (old_version != NULL
1163 && new_version != NULL
1164 && strcmp (old_version, new_version) == 0));
1165 }
1166 }
1167 }
1168
1169 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1170 existing symbol. */
1171
1172 oldbfd = NULL;
1173 oldsec = NULL;
1174 switch (h->root.type)
1175 {
1176 default:
1177 break;
1178
1179 case bfd_link_hash_undefined:
1180 case bfd_link_hash_undefweak:
1181 oldbfd = h->root.u.undef.abfd;
1182 break;
1183
1184 case bfd_link_hash_defined:
1185 case bfd_link_hash_defweak:
1186 oldbfd = h->root.u.def.section->owner;
1187 oldsec = h->root.u.def.section;
1188 break;
1189
1190 case bfd_link_hash_common:
1191 oldbfd = h->root.u.c.p->section->owner;
1192 oldsec = h->root.u.c.p->section;
1193 if (pold_alignment)
1194 *pold_alignment = h->root.u.c.p->alignment_power;
1195 break;
1196 }
1197 if (poldbfd && *poldbfd == NULL)
1198 *poldbfd = oldbfd;
1199
1200 /* Differentiate strong and weak symbols. */
1201 newweak = bind == STB_WEAK;
1202 oldweak = (h->root.type == bfd_link_hash_defweak
1203 || h->root.type == bfd_link_hash_undefweak);
1204 if (pold_weak)
1205 *pold_weak = oldweak;
1206
1207 /* We have to check it for every instance since the first few may be
1208 references and not all compilers emit symbol type for undefined
1209 symbols. */
1210 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1211
1212 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1213 respectively, is from a dynamic object. */
1214
1215 newdyn = (abfd->flags & DYNAMIC) != 0;
1216
1217 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1218 syms and defined syms in dynamic libraries respectively.
1219 ref_dynamic on the other hand can be set for a symbol defined in
1220 a dynamic library, and def_dynamic may not be set; When the
1221 definition in a dynamic lib is overridden by a definition in the
1222 executable use of the symbol in the dynamic lib becomes a
1223 reference to the executable symbol. */
1224 if (newdyn)
1225 {
1226 if (bfd_is_und_section (sec))
1227 {
1228 if (bind != STB_WEAK)
1229 {
1230 h->ref_dynamic_nonweak = 1;
1231 hi->ref_dynamic_nonweak = 1;
1232 }
1233 }
1234 else
1235 {
1236 /* Update the existing symbol only if they match. */
1237 if (*matched)
1238 h->dynamic_def = 1;
1239 hi->dynamic_def = 1;
1240 }
1241 }
1242
1243 /* If we just created the symbol, mark it as being an ELF symbol.
1244 Other than that, there is nothing to do--there is no merge issue
1245 with a newly defined symbol--so we just return. */
1246
1247 if (h->root.type == bfd_link_hash_new)
1248 {
1249 h->non_elf = 0;
1250 return true;
1251 }
1252
1253 /* In cases involving weak versioned symbols, we may wind up trying
1254 to merge a symbol with itself. Catch that here, to avoid the
1255 confusion that results if we try to override a symbol with
1256 itself. The additional tests catch cases like
1257 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1258 dynamic object, which we do want to handle here. */
1259 if (abfd == oldbfd
1260 && (newweak || oldweak)
1261 && ((abfd->flags & DYNAMIC) == 0
1262 || !h->def_regular))
1263 return true;
1264
1265 olddyn = false;
1266 if (oldbfd != NULL)
1267 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1268 else if (oldsec != NULL)
1269 {
1270 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1271 indices used by MIPS ELF. */
1272 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1273 }
1274
1275 if (oldbfd != NULL
1276 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN))
1277 {
1278 if (newdyn != olddyn)
1279 {
1280 /* Handle a case where plugin_notice won't be called and thus
1281 won't set the non_ir_ref flags on the first pass over
1282 symbols. */
1283 h->root.non_ir_ref_dynamic = true;
1284 hi->root.non_ir_ref_dynamic = true;
1285 }
1286
1287 if ((oldbfd->flags & BFD_PLUGIN) != 0
1288 && hi->root.type == bfd_link_hash_indirect)
1289 {
1290 /* Change indirect symbol from IR to undefined. */
1291 hi->root.type = bfd_link_hash_undefined;
1292 hi->root.u.undef.abfd = oldbfd;
1293 }
1294 }
1295
1296 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1297 respectively, appear to be a definition rather than reference. */
1298
1299 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1300
1301 olddef = (h->root.type != bfd_link_hash_undefined
1302 && h->root.type != bfd_link_hash_undefweak
1303 && h->root.type != bfd_link_hash_common);
1304
1305 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1306 respectively, appear to be a function. */
1307
1308 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1309 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1310
1311 oldfunc = (h->type != STT_NOTYPE
1312 && bed->is_function_type (h->type));
1313
1314 if (!(newfunc && oldfunc)
1315 && ELF_ST_TYPE (sym->st_info) != h->type
1316 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1317 && h->type != STT_NOTYPE
1318 && (newdef || bfd_is_com_section (sec))
1319 && (olddef || h->root.type == bfd_link_hash_common))
1320 {
1321 /* If creating a default indirect symbol ("foo" or "foo@") from
1322 a dynamic versioned definition ("foo@@") skip doing so if
1323 there is an existing regular definition with a different
1324 type. We don't want, for example, a "time" variable in the
1325 executable overriding a "time" function in a shared library. */
1326 if (newdyn
1327 && !olddyn)
1328 {
1329 *skip = true;
1330 return true;
1331 }
1332
1333 /* When adding a symbol from a regular object file after we have
1334 created indirect symbols, undo the indirection and any
1335 dynamic state. */
1336 if (hi != h
1337 && !newdyn
1338 && olddyn)
1339 {
1340 h = hi;
1341 (*bed->elf_backend_hide_symbol) (info, h, true);
1342 h->forced_local = 0;
1343 h->ref_dynamic = 0;
1344 h->def_dynamic = 0;
1345 h->dynamic_def = 0;
1346 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1347 {
1348 h->root.type = bfd_link_hash_undefined;
1349 h->root.u.undef.abfd = abfd;
1350 }
1351 else
1352 {
1353 h->root.type = bfd_link_hash_new;
1354 h->root.u.undef.abfd = NULL;
1355 }
1356 return true;
1357 }
1358 }
1359
1360 /* Check TLS symbols. We don't check undefined symbols introduced
1361 by "ld -u" which have no type (and oldbfd NULL), and we don't
1362 check symbols from plugins because they also have no type. */
1363 if (oldbfd != NULL
1364 && (oldbfd->flags & BFD_PLUGIN) == 0
1365 && (abfd->flags & BFD_PLUGIN) == 0
1366 && ELF_ST_TYPE (sym->st_info) != h->type
1367 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1368 {
1369 bfd *ntbfd, *tbfd;
1370 bool ntdef, tdef;
1371 asection *ntsec, *tsec;
1372
1373 if (h->type == STT_TLS)
1374 {
1375 ntbfd = abfd;
1376 ntsec = sec;
1377 ntdef = newdef;
1378 tbfd = oldbfd;
1379 tsec = oldsec;
1380 tdef = olddef;
1381 }
1382 else
1383 {
1384 ntbfd = oldbfd;
1385 ntsec = oldsec;
1386 ntdef = olddef;
1387 tbfd = abfd;
1388 tsec = sec;
1389 tdef = newdef;
1390 }
1391
1392 if (tdef && ntdef)
1393 _bfd_error_handler
1394 /* xgettext:c-format */
1395 (_("%s: TLS definition in %pB section %pA "
1396 "mismatches non-TLS definition in %pB section %pA"),
1397 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
1398 else if (!tdef && !ntdef)
1399 _bfd_error_handler
1400 /* xgettext:c-format */
1401 (_("%s: TLS reference in %pB "
1402 "mismatches non-TLS reference in %pB"),
1403 h->root.root.string, tbfd, ntbfd);
1404 else if (tdef)
1405 _bfd_error_handler
1406 /* xgettext:c-format */
1407 (_("%s: TLS definition in %pB section %pA "
1408 "mismatches non-TLS reference in %pB"),
1409 h->root.root.string, tbfd, tsec, ntbfd);
1410 else
1411 _bfd_error_handler
1412 /* xgettext:c-format */
1413 (_("%s: TLS reference in %pB "
1414 "mismatches non-TLS definition in %pB section %pA"),
1415 h->root.root.string, tbfd, ntbfd, ntsec);
1416
1417 bfd_set_error (bfd_error_bad_value);
1418 return false;
1419 }
1420
1421 /* If the old symbol has non-default visibility, we ignore the new
1422 definition from a dynamic object. */
1423 if (newdyn
1424 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1425 && !bfd_is_und_section (sec))
1426 {
1427 *skip = true;
1428 /* Make sure this symbol is dynamic. */
1429 h->ref_dynamic = 1;
1430 hi->ref_dynamic = 1;
1431 /* A protected symbol has external availability. Make sure it is
1432 recorded as dynamic.
1433
1434 FIXME: Should we check type and size for protected symbol? */
1435 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1436 return bfd_elf_link_record_dynamic_symbol (info, h);
1437 else
1438 return true;
1439 }
1440 else if (!newdyn
1441 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1442 && h->def_dynamic)
1443 {
1444 /* If the new symbol with non-default visibility comes from a
1445 relocatable file and the old definition comes from a dynamic
1446 object, we remove the old definition. */
1447 if (hi->root.type == bfd_link_hash_indirect)
1448 {
1449 /* Handle the case where the old dynamic definition is
1450 default versioned. We need to copy the symbol info from
1451 the symbol with default version to the normal one if it
1452 was referenced before. */
1453 if (h->ref_regular)
1454 {
1455 hi->root.type = h->root.type;
1456 h->root.type = bfd_link_hash_indirect;
1457 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1458
1459 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1460 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1461 {
1462 /* If the new symbol is hidden or internal, completely undo
1463 any dynamic link state. */
1464 (*bed->elf_backend_hide_symbol) (info, h, true);
1465 h->forced_local = 0;
1466 h->ref_dynamic = 0;
1467 }
1468 else
1469 h->ref_dynamic = 1;
1470
1471 h->def_dynamic = 0;
1472 /* FIXME: Should we check type and size for protected symbol? */
1473 h->size = 0;
1474 h->type = 0;
1475
1476 h = hi;
1477 }
1478 else
1479 h = hi;
1480 }
1481
1482 /* If the old symbol was undefined before, then it will still be
1483 on the undefs list. If the new symbol is undefined or
1484 common, we can't make it bfd_link_hash_new here, because new
1485 undefined or common symbols will be added to the undefs list
1486 by _bfd_generic_link_add_one_symbol. Symbols may not be
1487 added twice to the undefs list. Also, if the new symbol is
1488 undefweak then we don't want to lose the strong undef. */
1489 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1490 {
1491 h->root.type = bfd_link_hash_undefined;
1492 h->root.u.undef.abfd = abfd;
1493 }
1494 else
1495 {
1496 h->root.type = bfd_link_hash_new;
1497 h->root.u.undef.abfd = NULL;
1498 }
1499
1500 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1501 {
1502 /* If the new symbol is hidden or internal, completely undo
1503 any dynamic link state. */
1504 (*bed->elf_backend_hide_symbol) (info, h, true);
1505 h->forced_local = 0;
1506 h->ref_dynamic = 0;
1507 }
1508 else
1509 h->ref_dynamic = 1;
1510 h->def_dynamic = 0;
1511 /* FIXME: Should we check type and size for protected symbol? */
1512 h->size = 0;
1513 h->type = 0;
1514 return true;
1515 }
1516
1517 /* If a new weak symbol definition comes from a regular file and the
1518 old symbol comes from a dynamic library, we treat the new one as
1519 strong. Similarly, an old weak symbol definition from a regular
1520 file is treated as strong when the new symbol comes from a dynamic
1521 library. Further, an old weak symbol from a dynamic library is
1522 treated as strong if the new symbol is from a dynamic library.
1523 This reflects the way glibc's ld.so works.
1524
1525 Also allow a weak symbol to override a linker script symbol
1526 defined by an early pass over the script. This is done so the
1527 linker knows the symbol is defined in an object file, for the
1528 DEFINED script function.
1529
1530 Do this before setting *type_change_ok or *size_change_ok so that
1531 we warn properly when dynamic library symbols are overridden. */
1532
1533 if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
1534 newweak = false;
1535 if (olddef && newdyn)
1536 oldweak = false;
1537
1538 /* Allow changes between different types of function symbol. */
1539 if (newfunc && oldfunc)
1540 *type_change_ok = true;
1541
1542 /* It's OK to change the type if either the existing symbol or the
1543 new symbol is weak. A type change is also OK if the old symbol
1544 is undefined and the new symbol is defined. */
1545
1546 if (oldweak
1547 || newweak
1548 || (newdef
1549 && h->root.type == bfd_link_hash_undefined))
1550 *type_change_ok = true;
1551
1552 /* It's OK to change the size if either the existing symbol or the
1553 new symbol is weak, or if the old symbol is undefined. */
1554
1555 if (*type_change_ok
1556 || h->root.type == bfd_link_hash_undefined)
1557 *size_change_ok = true;
1558
1559 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1560 symbol, respectively, appears to be a common symbol in a dynamic
1561 object. If a symbol appears in an uninitialized section, and is
1562 not weak, and is not a function, then it may be a common symbol
1563 which was resolved when the dynamic object was created. We want
1564 to treat such symbols specially, because they raise special
1565 considerations when setting the symbol size: if the symbol
1566 appears as a common symbol in a regular object, and the size in
1567 the regular object is larger, we must make sure that we use the
1568 larger size. This problematic case can always be avoided in C,
1569 but it must be handled correctly when using Fortran shared
1570 libraries.
1571
1572 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1573 likewise for OLDDYNCOMMON and OLDDEF.
1574
1575 Note that this test is just a heuristic, and that it is quite
1576 possible to have an uninitialized symbol in a shared object which
1577 is really a definition, rather than a common symbol. This could
1578 lead to some minor confusion when the symbol really is a common
1579 symbol in some regular object. However, I think it will be
1580 harmless. */
1581
1582 if (newdyn
1583 && newdef
1584 && !newweak
1585 && (sec->flags & SEC_ALLOC) != 0
1586 && (sec->flags & SEC_LOAD) == 0
1587 && sym->st_size > 0
1588 && !newfunc)
1589 newdyncommon = true;
1590 else
1591 newdyncommon = false;
1592
1593 if (olddyn
1594 && olddef
1595 && h->root.type == bfd_link_hash_defined
1596 && h->def_dynamic
1597 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1598 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1599 && h->size > 0
1600 && !oldfunc)
1601 olddyncommon = true;
1602 else
1603 olddyncommon = false;
1604
1605 /* We now know everything about the old and new symbols. We ask the
1606 backend to check if we can merge them. */
1607 if (bed->merge_symbol != NULL)
1608 {
1609 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1610 return false;
1611 sec = *psec;
1612 }
1613
1614 /* There are multiple definitions of a normal symbol. Skip the
1615 default symbol as well as definition from an IR object. */
1616 if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
1617 && !default_sym && h->def_regular
1618 && !(oldbfd != NULL
1619 && (oldbfd->flags & BFD_PLUGIN) != 0
1620 && (abfd->flags & BFD_PLUGIN) == 0))
1621 {
1622 /* Handle a multiple definition. */
1623 (*info->callbacks->multiple_definition) (info, &h->root,
1624 abfd, sec, *pvalue);
1625 *skip = true;
1626 return true;
1627 }
1628
1629 /* If both the old and the new symbols look like common symbols in a
1630 dynamic object, set the size of the symbol to the larger of the
1631 two. */
1632
1633 if (olddyncommon
1634 && newdyncommon
1635 && sym->st_size != h->size)
1636 {
1637 /* Since we think we have two common symbols, issue a multiple
1638 common warning if desired. Note that we only warn if the
1639 size is different. If the size is the same, we simply let
1640 the old symbol override the new one as normally happens with
1641 symbols defined in dynamic objects. */
1642
1643 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1644 bfd_link_hash_common, sym->st_size);
1645 if (sym->st_size > h->size)
1646 h->size = sym->st_size;
1647
1648 *size_change_ok = true;
1649 }
1650
1651 /* If we are looking at a dynamic object, and we have found a
1652 definition, we need to see if the symbol was already defined by
1653 some other object. If so, we want to use the existing
1654 definition, and we do not want to report a multiple symbol
1655 definition error; we do this by clobbering *PSEC to be
1656 bfd_und_section_ptr.
1657
1658 We treat a common symbol as a definition if the symbol in the
1659 shared library is a function, since common symbols always
1660 represent variables; this can cause confusion in principle, but
1661 any such confusion would seem to indicate an erroneous program or
1662 shared library. We also permit a common symbol in a regular
1663 object to override a weak symbol in a shared object. */
1664
1665 if (newdyn
1666 && newdef
1667 && (olddef
1668 || (h->root.type == bfd_link_hash_common
1669 && (newweak || newfunc))))
1670 {
1671 *override = abfd;
1672 newdef = false;
1673 newdyncommon = false;
1674
1675 *psec = sec = bfd_und_section_ptr;
1676 *size_change_ok = true;
1677
1678 /* If we get here when the old symbol is a common symbol, then
1679 we are explicitly letting it override a weak symbol or
1680 function in a dynamic object, and we don't want to warn about
1681 a type change. If the old symbol is a defined symbol, a type
1682 change warning may still be appropriate. */
1683
1684 if (h->root.type == bfd_link_hash_common)
1685 *type_change_ok = true;
1686 }
1687
1688 /* Handle the special case of an old common symbol merging with a
1689 new symbol which looks like a common symbol in a shared object.
1690 We change *PSEC and *PVALUE to make the new symbol look like a
1691 common symbol, and let _bfd_generic_link_add_one_symbol do the
1692 right thing. */
1693
1694 if (newdyncommon
1695 && h->root.type == bfd_link_hash_common)
1696 {
1697 *override = oldbfd;
1698 newdef = false;
1699 newdyncommon = false;
1700 *pvalue = sym->st_size;
1701 *psec = sec = bed->common_section (oldsec);
1702 *size_change_ok = true;
1703 }
1704
1705 /* Skip weak definitions of symbols that are already defined. */
1706 if (newdef && olddef && newweak)
1707 {
1708 /* Don't skip new non-IR weak syms. */
1709 if (!(oldbfd != NULL
1710 && (oldbfd->flags & BFD_PLUGIN) != 0
1711 && (abfd->flags & BFD_PLUGIN) == 0))
1712 {
1713 newdef = false;
1714 *skip = true;
1715 }
1716
1717 /* Merge st_other. If the symbol already has a dynamic index,
1718 but visibility says it should not be visible, turn it into a
1719 local symbol. */
1720 elf_merge_st_other (abfd, h, sym->st_other, sec, newdef, newdyn);
1721 if (h->dynindx != -1)
1722 switch (ELF_ST_VISIBILITY (h->other))
1723 {
1724 case STV_INTERNAL:
1725 case STV_HIDDEN:
1726 (*bed->elf_backend_hide_symbol) (info, h, true);
1727 break;
1728 }
1729 }
1730
1731 /* If the old symbol is from a dynamic object, and the new symbol is
1732 a definition which is not from a dynamic object, then the new
1733 symbol overrides the old symbol. Symbols from regular files
1734 always take precedence over symbols from dynamic objects, even if
1735 they are defined after the dynamic object in the link.
1736
1737 As above, we again permit a common symbol in a regular object to
1738 override a definition in a shared object if the shared object
1739 symbol is a function or is weak. */
1740
1741 flip = NULL;
1742 if (!newdyn
1743 && (newdef
1744 || (bfd_is_com_section (sec)
1745 && (oldweak || oldfunc)))
1746 && olddyn
1747 && olddef
1748 && h->def_dynamic)
1749 {
1750 /* Change the hash table entry to undefined, and let
1751 _bfd_generic_link_add_one_symbol do the right thing with the
1752 new definition. */
1753
1754 h->root.type = bfd_link_hash_undefined;
1755 h->root.u.undef.abfd = h->root.u.def.section->owner;
1756 *size_change_ok = true;
1757
1758 olddef = false;
1759 olddyncommon = false;
1760
1761 /* We again permit a type change when a common symbol may be
1762 overriding a function. */
1763
1764 if (bfd_is_com_section (sec))
1765 {
1766 if (oldfunc)
1767 {
1768 /* If a common symbol overrides a function, make sure
1769 that it isn't defined dynamically nor has type
1770 function. */
1771 h->def_dynamic = 0;
1772 h->type = STT_NOTYPE;
1773 }
1774 *type_change_ok = true;
1775 }
1776
1777 if (hi->root.type == bfd_link_hash_indirect)
1778 flip = hi;
1779 else
1780 /* This union may have been set to be non-NULL when this symbol
1781 was seen in a dynamic object. We must force the union to be
1782 NULL, so that it is correct for a regular symbol. */
1783 h->verinfo.vertree = NULL;
1784 }
1785
1786 /* Handle the special case of a new common symbol merging with an
1787 old symbol that looks like it might be a common symbol defined in
1788 a shared object. Note that we have already handled the case in
1789 which a new common symbol should simply override the definition
1790 in the shared library. */
1791
1792 if (! newdyn
1793 && bfd_is_com_section (sec)
1794 && olddyncommon)
1795 {
1796 /* It would be best if we could set the hash table entry to a
1797 common symbol, but we don't know what to use for the section
1798 or the alignment. */
1799 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1800 bfd_link_hash_common, sym->st_size);
1801
1802 /* If the presumed common symbol in the dynamic object is
1803 larger, pretend that the new symbol has its size. */
1804
1805 if (h->size > *pvalue)
1806 *pvalue = h->size;
1807
1808 /* We need to remember the alignment required by the symbol
1809 in the dynamic object. */
1810 BFD_ASSERT (pold_alignment);
1811 *pold_alignment = h->root.u.def.section->alignment_power;
1812
1813 olddef = false;
1814 olddyncommon = false;
1815
1816 h->root.type = bfd_link_hash_undefined;
1817 h->root.u.undef.abfd = h->root.u.def.section->owner;
1818
1819 *size_change_ok = true;
1820 *type_change_ok = true;
1821
1822 if (hi->root.type == bfd_link_hash_indirect)
1823 flip = hi;
1824 else
1825 h->verinfo.vertree = NULL;
1826 }
1827
1828 if (flip != NULL)
1829 {
1830 /* Handle the case where we had a versioned symbol in a dynamic
1831 library and now find a definition in a normal object. In this
1832 case, we make the versioned symbol point to the normal one. */
1833 flip->root.type = h->root.type;
1834 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1835 h->root.type = bfd_link_hash_indirect;
1836 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1837 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1838 if (h->def_dynamic)
1839 {
1840 h->def_dynamic = 0;
1841 flip->ref_dynamic = 1;
1842 }
1843 }
1844
1845 return true;
1846 }
1847
1848 /* This function is called to create an indirect symbol from the
1849 default for the symbol with the default version if needed. The
1850 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1851 set DYNSYM if the new indirect symbol is dynamic. */
1852
1853 static bool
1854 _bfd_elf_add_default_symbol (bfd *abfd,
1855 struct bfd_link_info *info,
1856 struct elf_link_hash_entry *h,
1857 const char *name,
1858 Elf_Internal_Sym *sym,
1859 asection *sec,
1860 bfd_vma value,
1861 bfd **poldbfd,
1862 bool *dynsym)
1863 {
1864 bool type_change_ok;
1865 bool size_change_ok;
1866 bool skip;
1867 char *shortname;
1868 struct elf_link_hash_entry *hi;
1869 struct bfd_link_hash_entry *bh;
1870 const struct elf_backend_data *bed;
1871 bool collect;
1872 bool dynamic;
1873 bfd *override;
1874 char *p;
1875 size_t len, shortlen;
1876 asection *tmp_sec;
1877 bool matched;
1878
1879 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1880 return true;
1881
1882 /* If this symbol has a version, and it is the default version, we
1883 create an indirect symbol from the default name to the fully
1884 decorated name. This will cause external references which do not
1885 specify a version to be bound to this version of the symbol. */
1886 p = strchr (name, ELF_VER_CHR);
1887 if (h->versioned == unknown)
1888 {
1889 if (p == NULL)
1890 {
1891 h->versioned = unversioned;
1892 return true;
1893 }
1894 else
1895 {
1896 if (p[1] != ELF_VER_CHR)
1897 {
1898 h->versioned = versioned_hidden;
1899 return true;
1900 }
1901 else
1902 h->versioned = versioned;
1903 }
1904 }
1905 else
1906 {
1907 /* PR ld/19073: We may see an unversioned definition after the
1908 default version. */
1909 if (p == NULL)
1910 return true;
1911 }
1912
1913 bed = get_elf_backend_data (abfd);
1914 collect = bed->collect;
1915 dynamic = (abfd->flags & DYNAMIC) != 0;
1916
1917 shortlen = p - name;
1918 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1919 if (shortname == NULL)
1920 return false;
1921 memcpy (shortname, name, shortlen);
1922 shortname[shortlen] = '\0';
1923
1924 /* We are going to create a new symbol. Merge it with any existing
1925 symbol with this name. For the purposes of the merge, act as
1926 though we were defining the symbol we just defined, although we
1927 actually going to define an indirect symbol. */
1928 type_change_ok = false;
1929 size_change_ok = false;
1930 matched = true;
1931 tmp_sec = sec;
1932 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1933 &hi, poldbfd, NULL, NULL, &skip, &override,
1934 &type_change_ok, &size_change_ok, &matched))
1935 return false;
1936
1937 if (skip)
1938 goto nondefault;
1939
1940 if (hi->def_regular || ELF_COMMON_DEF_P (hi))
1941 {
1942 /* If the undecorated symbol will have a version added by a
1943 script different to H, then don't indirect to/from the
1944 undecorated symbol. This isn't ideal because we may not yet
1945 have seen symbol versions, if given by a script on the
1946 command line rather than via --version-script. */
1947 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1948 {
1949 bool hide;
1950
1951 hi->verinfo.vertree
1952 = bfd_find_version_for_sym (info->version_info,
1953 hi->root.root.string, &hide);
1954 if (hi->verinfo.vertree != NULL && hide)
1955 {
1956 (*bed->elf_backend_hide_symbol) (info, hi, true);
1957 goto nondefault;
1958 }
1959 }
1960 if (hi->verinfo.vertree != NULL
1961 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1962 goto nondefault;
1963 }
1964
1965 if (! override)
1966 {
1967 /* Add the default symbol if not performing a relocatable link. */
1968 if (! bfd_link_relocatable (info))
1969 {
1970 bh = &hi->root;
1971 if (bh->type == bfd_link_hash_defined
1972 && bh->u.def.section->owner != NULL
1973 && (bh->u.def.section->owner->flags & BFD_PLUGIN) != 0)
1974 {
1975 /* Mark the previous definition from IR object as
1976 undefined so that the generic linker will override
1977 it. */
1978 bh->type = bfd_link_hash_undefined;
1979 bh->u.undef.abfd = bh->u.def.section->owner;
1980 }
1981 if (! (_bfd_generic_link_add_one_symbol
1982 (info, abfd, shortname, BSF_INDIRECT,
1983 bfd_ind_section_ptr,
1984 0, name, false, collect, &bh)))
1985 return false;
1986 hi = (struct elf_link_hash_entry *) bh;
1987 }
1988 }
1989 else
1990 {
1991 /* In this case the symbol named SHORTNAME is overriding the
1992 indirect symbol we want to add. We were planning on making
1993 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1994 is the name without a version. NAME is the fully versioned
1995 name, and it is the default version.
1996
1997 Overriding means that we already saw a definition for the
1998 symbol SHORTNAME in a regular object, and it is overriding
1999 the symbol defined in the dynamic object.
2000
2001 When this happens, we actually want to change NAME, the
2002 symbol we just added, to refer to SHORTNAME. This will cause
2003 references to NAME in the shared object to become references
2004 to SHORTNAME in the regular object. This is what we expect
2005 when we override a function in a shared object: that the
2006 references in the shared object will be mapped to the
2007 definition in the regular object. */
2008
2009 while (hi->root.type == bfd_link_hash_indirect
2010 || hi->root.type == bfd_link_hash_warning)
2011 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2012
2013 h->root.type = bfd_link_hash_indirect;
2014 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
2015 if (h->def_dynamic)
2016 {
2017 h->def_dynamic = 0;
2018 hi->ref_dynamic = 1;
2019 if (hi->ref_regular
2020 || hi->def_regular)
2021 {
2022 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
2023 return false;
2024 }
2025 }
2026
2027 /* Now set HI to H, so that the following code will set the
2028 other fields correctly. */
2029 hi = h;
2030 }
2031
2032 /* Check if HI is a warning symbol. */
2033 if (hi->root.type == bfd_link_hash_warning)
2034 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2035
2036 /* If there is a duplicate definition somewhere, then HI may not
2037 point to an indirect symbol. We will have reported an error to
2038 the user in that case. */
2039
2040 if (hi->root.type == bfd_link_hash_indirect)
2041 {
2042 struct elf_link_hash_entry *ht;
2043
2044 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
2045 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
2046
2047 /* If we first saw a reference to SHORTNAME with non-default
2048 visibility, merge that visibility to the @@VER symbol. */
2049 elf_merge_st_other (abfd, ht, hi->other, sec, true, dynamic);
2050
2051 /* A reference to the SHORTNAME symbol from a dynamic library
2052 will be satisfied by the versioned symbol at runtime. In
2053 effect, we have a reference to the versioned symbol. */
2054 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2055 hi->dynamic_def |= ht->dynamic_def;
2056
2057 /* See if the new flags lead us to realize that the symbol must
2058 be dynamic. */
2059 if (! *dynsym)
2060 {
2061 if (! dynamic)
2062 {
2063 if (! bfd_link_executable (info)
2064 || hi->def_dynamic
2065 || hi->ref_dynamic)
2066 *dynsym = true;
2067 }
2068 else
2069 {
2070 if (hi->ref_regular)
2071 *dynsym = true;
2072 }
2073 }
2074 }
2075
2076 /* We also need to define an indirection from the nondefault version
2077 of the symbol. */
2078
2079 nondefault:
2080 len = strlen (name);
2081 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
2082 if (shortname == NULL)
2083 return false;
2084 memcpy (shortname, name, shortlen);
2085 memcpy (shortname + shortlen, p + 1, len - shortlen);
2086
2087 /* Once again, merge with any existing symbol. */
2088 type_change_ok = false;
2089 size_change_ok = false;
2090 tmp_sec = sec;
2091 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
2092 &hi, poldbfd, NULL, NULL, &skip, &override,
2093 &type_change_ok, &size_change_ok, &matched))
2094 return false;
2095
2096 if (skip)
2097 {
2098 if (!dynamic
2099 && h->root.type == bfd_link_hash_defweak
2100 && hi->root.type == bfd_link_hash_defined)
2101 {
2102 /* We are handling a weak sym@@ver and attempting to define
2103 a weak sym@ver, but _bfd_elf_merge_symbol said to skip the
2104 new weak sym@ver because there is already a strong sym@ver.
2105 However, sym@ver and sym@@ver are really the same symbol.
2106 The existing strong sym@ver ought to override sym@@ver. */
2107 h->root.type = bfd_link_hash_defined;
2108 h->root.u.def.section = hi->root.u.def.section;
2109 h->root.u.def.value = hi->root.u.def.value;
2110 hi->root.type = bfd_link_hash_indirect;
2111 hi->root.u.i.link = &h->root;
2112 }
2113 else
2114 return true;
2115 }
2116 else if (override)
2117 {
2118 /* Here SHORTNAME is a versioned name, so we don't expect to see
2119 the type of override we do in the case above unless it is
2120 overridden by a versioned definition. */
2121 if (hi->root.type != bfd_link_hash_defined
2122 && hi->root.type != bfd_link_hash_defweak)
2123 _bfd_error_handler
2124 /* xgettext:c-format */
2125 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
2126 abfd, shortname);
2127 return true;
2128 }
2129 else
2130 {
2131 bh = &hi->root;
2132 if (! (_bfd_generic_link_add_one_symbol
2133 (info, abfd, shortname, BSF_INDIRECT,
2134 bfd_ind_section_ptr, 0, name, false, collect, &bh)))
2135 return false;
2136 hi = (struct elf_link_hash_entry *) bh;
2137 }
2138
2139 /* If there is a duplicate definition somewhere, then HI may not
2140 point to an indirect symbol. We will have reported an error
2141 to the user in that case. */
2142 if (hi->root.type == bfd_link_hash_indirect)
2143 {
2144 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
2145 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2146 hi->dynamic_def |= h->dynamic_def;
2147
2148 /* If we first saw a reference to @VER symbol with
2149 non-default visibility, merge that visibility to the
2150 @@VER symbol. */
2151 elf_merge_st_other (abfd, h, hi->other, sec, true, dynamic);
2152
2153 /* See if the new flags lead us to realize that the symbol
2154 must be dynamic. */
2155 if (! *dynsym)
2156 {
2157 if (! dynamic)
2158 {
2159 if (! bfd_link_executable (info)
2160 || hi->ref_dynamic)
2161 *dynsym = true;
2162 }
2163 else
2164 {
2165 if (hi->ref_regular)
2166 *dynsym = true;
2167 }
2168 }
2169 }
2170
2171 return true;
2172 }
2173 \f
2174 /* This routine is used to export all defined symbols into the dynamic
2175 symbol table. It is called via elf_link_hash_traverse. */
2176
2177 static bool
2178 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
2179 {
2180 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2181
2182 /* Ignore indirect symbols. These are added by the versioning code. */
2183 if (h->root.type == bfd_link_hash_indirect)
2184 return true;
2185
2186 /* Ignore this if we won't export it. */
2187 if (!eif->info->export_dynamic && !h->dynamic)
2188 return true;
2189
2190 if (h->dynindx == -1
2191 && (h->def_regular || h->ref_regular)
2192 && ! bfd_hide_sym_by_version (eif->info->version_info,
2193 h->root.root.string))
2194 {
2195 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2196 {
2197 eif->failed = true;
2198 return false;
2199 }
2200 }
2201
2202 return true;
2203 }
2204 \f
2205 /* Look through the symbols which are defined in other shared
2206 libraries and referenced here. Update the list of version
2207 dependencies. This will be put into the .gnu.version_r section.
2208 This function is called via elf_link_hash_traverse. */
2209
2210 static bool
2211 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2212 void *data)
2213 {
2214 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2215 Elf_Internal_Verneed *t;
2216 Elf_Internal_Vernaux *a;
2217 size_t amt;
2218
2219 /* We only care about symbols defined in shared objects with version
2220 information. */
2221 if (!h->def_dynamic
2222 || h->def_regular
2223 || h->dynindx == -1
2224 || h->verinfo.verdef == NULL
2225 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2226 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2227 return true;
2228
2229 /* See if we already know about this version. */
2230 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2231 t != NULL;
2232 t = t->vn_nextref)
2233 {
2234 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2235 continue;
2236
2237 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2238 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2239 return true;
2240
2241 break;
2242 }
2243
2244 /* This is a new version. Add it to tree we are building. */
2245
2246 if (t == NULL)
2247 {
2248 amt = sizeof *t;
2249 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2250 if (t == NULL)
2251 {
2252 rinfo->failed = true;
2253 return false;
2254 }
2255
2256 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2257 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2258 elf_tdata (rinfo->info->output_bfd)->verref = t;
2259 }
2260
2261 amt = sizeof *a;
2262 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2263 if (a == NULL)
2264 {
2265 rinfo->failed = true;
2266 return false;
2267 }
2268
2269 /* Note that we are copying a string pointer here, and testing it
2270 above. If bfd_elf_string_from_elf_section is ever changed to
2271 discard the string data when low in memory, this will have to be
2272 fixed. */
2273 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2274
2275 a->vna_flags = h->verinfo.verdef->vd_flags;
2276 a->vna_nextptr = t->vn_auxptr;
2277
2278 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2279 ++rinfo->vers;
2280
2281 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2282
2283 t->vn_auxptr = a;
2284
2285 return true;
2286 }
2287
2288 /* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2289 hidden. Set *T_P to NULL if there is no match. */
2290
2291 static bool
2292 _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
2293 struct elf_link_hash_entry *h,
2294 const char *version_p,
2295 struct bfd_elf_version_tree **t_p,
2296 bool *hide)
2297 {
2298 struct bfd_elf_version_tree *t;
2299
2300 /* Look for the version. If we find it, it is no longer weak. */
2301 for (t = info->version_info; t != NULL; t = t->next)
2302 {
2303 if (strcmp (t->name, version_p) == 0)
2304 {
2305 size_t len;
2306 char *alc;
2307 struct bfd_elf_version_expr *d;
2308
2309 len = version_p - h->root.root.string;
2310 alc = (char *) bfd_malloc (len);
2311 if (alc == NULL)
2312 return false;
2313 memcpy (alc, h->root.root.string, len - 1);
2314 alc[len - 1] = '\0';
2315 if (alc[len - 2] == ELF_VER_CHR)
2316 alc[len - 2] = '\0';
2317
2318 h->verinfo.vertree = t;
2319 t->used = true;
2320 d = NULL;
2321
2322 if (t->globals.list != NULL)
2323 d = (*t->match) (&t->globals, NULL, alc);
2324
2325 /* See if there is anything to force this symbol to
2326 local scope. */
2327 if (d == NULL && t->locals.list != NULL)
2328 {
2329 d = (*t->match) (&t->locals, NULL, alc);
2330 if (d != NULL
2331 && h->dynindx != -1
2332 && ! info->export_dynamic)
2333 *hide = true;
2334 }
2335
2336 free (alc);
2337 break;
2338 }
2339 }
2340
2341 *t_p = t;
2342
2343 return true;
2344 }
2345
2346 /* Return TRUE if the symbol H is hidden by version script. */
2347
2348 bool
2349 _bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
2350 struct elf_link_hash_entry *h)
2351 {
2352 const char *p;
2353 bool hide = false;
2354 const struct elf_backend_data *bed
2355 = get_elf_backend_data (info->output_bfd);
2356
2357 /* Version script only hides symbols defined in regular objects. */
2358 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2359 return true;
2360
2361 p = strchr (h->root.root.string, ELF_VER_CHR);
2362 if (p != NULL && h->verinfo.vertree == NULL)
2363 {
2364 struct bfd_elf_version_tree *t;
2365
2366 ++p;
2367 if (*p == ELF_VER_CHR)
2368 ++p;
2369
2370 if (*p != '\0'
2371 && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
2372 && hide)
2373 {
2374 if (hide)
2375 (*bed->elf_backend_hide_symbol) (info, h, true);
2376 return true;
2377 }
2378 }
2379
2380 /* If we don't have a version for this symbol, see if we can find
2381 something. */
2382 if (h->verinfo.vertree == NULL && info->version_info != NULL)
2383 {
2384 h->verinfo.vertree
2385 = bfd_find_version_for_sym (info->version_info,
2386 h->root.root.string, &hide);
2387 if (h->verinfo.vertree != NULL && hide)
2388 {
2389 (*bed->elf_backend_hide_symbol) (info, h, true);
2390 return true;
2391 }
2392 }
2393
2394 return false;
2395 }
2396
2397 /* Figure out appropriate versions for all the symbols. We may not
2398 have the version number script until we have read all of the input
2399 files, so until that point we don't know which symbols should be
2400 local. This function is called via elf_link_hash_traverse. */
2401
2402 static bool
2403 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2404 {
2405 struct elf_info_failed *sinfo;
2406 struct bfd_link_info *info;
2407 const struct elf_backend_data *bed;
2408 struct elf_info_failed eif;
2409 char *p;
2410 bool hide;
2411
2412 sinfo = (struct elf_info_failed *) data;
2413 info = sinfo->info;
2414
2415 /* Fix the symbol flags. */
2416 eif.failed = false;
2417 eif.info = info;
2418 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2419 {
2420 if (eif.failed)
2421 sinfo->failed = true;
2422 return false;
2423 }
2424
2425 bed = get_elf_backend_data (info->output_bfd);
2426
2427 /* We only need version numbers for symbols defined in regular
2428 objects. */
2429 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2430 {
2431 /* Hide symbols defined in discarded input sections. */
2432 if ((h->root.type == bfd_link_hash_defined
2433 || h->root.type == bfd_link_hash_defweak)
2434 && discarded_section (h->root.u.def.section))
2435 (*bed->elf_backend_hide_symbol) (info, h, true);
2436 return true;
2437 }
2438
2439 hide = false;
2440 p = strchr (h->root.root.string, ELF_VER_CHR);
2441 if (p != NULL && h->verinfo.vertree == NULL)
2442 {
2443 struct bfd_elf_version_tree *t;
2444
2445 ++p;
2446 if (*p == ELF_VER_CHR)
2447 ++p;
2448
2449 /* If there is no version string, we can just return out. */
2450 if (*p == '\0')
2451 return true;
2452
2453 if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
2454 {
2455 sinfo->failed = true;
2456 return false;
2457 }
2458
2459 if (hide)
2460 (*bed->elf_backend_hide_symbol) (info, h, true);
2461
2462 /* If we are building an application, we need to create a
2463 version node for this version. */
2464 if (t == NULL && bfd_link_executable (info))
2465 {
2466 struct bfd_elf_version_tree **pp;
2467 int version_index;
2468
2469 /* If we aren't going to export this symbol, we don't need
2470 to worry about it. */
2471 if (h->dynindx == -1)
2472 return true;
2473
2474 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2475 sizeof *t);
2476 if (t == NULL)
2477 {
2478 sinfo->failed = true;
2479 return false;
2480 }
2481
2482 t->name = p;
2483 t->name_indx = (unsigned int) -1;
2484 t->used = true;
2485
2486 version_index = 1;
2487 /* Don't count anonymous version tag. */
2488 if (sinfo->info->version_info != NULL
2489 && sinfo->info->version_info->vernum == 0)
2490 version_index = 0;
2491 for (pp = &sinfo->info->version_info;
2492 *pp != NULL;
2493 pp = &(*pp)->next)
2494 ++version_index;
2495 t->vernum = version_index;
2496
2497 *pp = t;
2498
2499 h->verinfo.vertree = t;
2500 }
2501 else if (t == NULL)
2502 {
2503 /* We could not find the version for a symbol when
2504 generating a shared archive. Return an error. */
2505 _bfd_error_handler
2506 /* xgettext:c-format */
2507 (_("%pB: version node not found for symbol %s"),
2508 info->output_bfd, h->root.root.string);
2509 bfd_set_error (bfd_error_bad_value);
2510 sinfo->failed = true;
2511 return false;
2512 }
2513 }
2514
2515 /* If we don't have a version for this symbol, see if we can find
2516 something. */
2517 if (!hide
2518 && h->verinfo.vertree == NULL
2519 && sinfo->info->version_info != NULL)
2520 {
2521 h->verinfo.vertree
2522 = bfd_find_version_for_sym (sinfo->info->version_info,
2523 h->root.root.string, &hide);
2524 if (h->verinfo.vertree != NULL && hide)
2525 (*bed->elf_backend_hide_symbol) (info, h, true);
2526 }
2527
2528 return true;
2529 }
2530 \f
2531 /* Read and swap the relocs from the section indicated by SHDR. This
2532 may be either a REL or a RELA section. The relocations are
2533 translated into RELA relocations and stored in INTERNAL_RELOCS,
2534 which should have already been allocated to contain enough space.
2535 The EXTERNAL_RELOCS are a buffer where the external form of the
2536 relocations should be stored.
2537
2538 Returns FALSE if something goes wrong. */
2539
2540 static bool
2541 elf_link_read_relocs_from_section (bfd *abfd,
2542 asection *sec,
2543 Elf_Internal_Shdr *shdr,
2544 void *external_relocs,
2545 Elf_Internal_Rela *internal_relocs)
2546 {
2547 const struct elf_backend_data *bed;
2548 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2549 const bfd_byte *erela;
2550 const bfd_byte *erelaend;
2551 Elf_Internal_Rela *irela;
2552 Elf_Internal_Shdr *symtab_hdr;
2553 size_t nsyms;
2554
2555 /* Position ourselves at the start of the section. */
2556 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2557 return false;
2558
2559 /* Read the relocations. */
2560 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2561 return false;
2562
2563 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2564 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2565
2566 bed = get_elf_backend_data (abfd);
2567
2568 /* Convert the external relocations to the internal format. */
2569 if (shdr->sh_entsize == bed->s->sizeof_rel)
2570 swap_in = bed->s->swap_reloc_in;
2571 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2572 swap_in = bed->s->swap_reloca_in;
2573 else
2574 {
2575 bfd_set_error (bfd_error_wrong_format);
2576 return false;
2577 }
2578
2579 erela = (const bfd_byte *) external_relocs;
2580 /* Setting erelaend like this and comparing with <= handles case of
2581 a fuzzed object with sh_size not a multiple of sh_entsize. */
2582 erelaend = erela + shdr->sh_size - shdr->sh_entsize;
2583 irela = internal_relocs;
2584 while (erela <= erelaend)
2585 {
2586 bfd_vma r_symndx;
2587
2588 (*swap_in) (abfd, erela, irela);
2589 r_symndx = ELF32_R_SYM (irela->r_info);
2590 if (bed->s->arch_size == 64)
2591 r_symndx >>= 24;
2592 if (nsyms > 0)
2593 {
2594 if ((size_t) r_symndx >= nsyms)
2595 {
2596 _bfd_error_handler
2597 /* xgettext:c-format */
2598 (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
2599 " for offset %#" PRIx64 " in section `%pA'"),
2600 abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
2601 (uint64_t) irela->r_offset, sec);
2602 bfd_set_error (bfd_error_bad_value);
2603 return false;
2604 }
2605 }
2606 else if (r_symndx != STN_UNDEF)
2607 {
2608 _bfd_error_handler
2609 /* xgettext:c-format */
2610 (_("%pB: non-zero symbol index (%#" PRIx64 ")"
2611 " for offset %#" PRIx64 " in section `%pA'"
2612 " when the object file has no symbol table"),
2613 abfd, (uint64_t) r_symndx,
2614 (uint64_t) irela->r_offset, sec);
2615 bfd_set_error (bfd_error_bad_value);
2616 return false;
2617 }
2618 irela += bed->s->int_rels_per_ext_rel;
2619 erela += shdr->sh_entsize;
2620 }
2621
2622 return true;
2623 }
2624
2625 /* Read and swap the relocs for a section O. They may have been
2626 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2627 not NULL, they are used as buffers to read into. They are known to
2628 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2629 the return value is allocated using either malloc or bfd_alloc,
2630 according to the KEEP_MEMORY argument. If O has two relocation
2631 sections (both REL and RELA relocations), then the REL_HDR
2632 relocations will appear first in INTERNAL_RELOCS, followed by the
2633 RELA_HDR relocations. If INFO isn't NULL and KEEP_MEMORY is true,
2634 update cache_size. */
2635
2636 Elf_Internal_Rela *
2637 _bfd_elf_link_info_read_relocs (bfd *abfd,
2638 struct bfd_link_info *info,
2639 asection *o,
2640 void *external_relocs,
2641 Elf_Internal_Rela *internal_relocs,
2642 bool keep_memory)
2643 {
2644 void *alloc1 = NULL;
2645 Elf_Internal_Rela *alloc2 = NULL;
2646 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2647 struct bfd_elf_section_data *esdo = elf_section_data (o);
2648 Elf_Internal_Rela *internal_rela_relocs;
2649
2650 if (esdo->relocs != NULL)
2651 return esdo->relocs;
2652
2653 if (o->reloc_count == 0)
2654 return NULL;
2655
2656 if (internal_relocs == NULL)
2657 {
2658 bfd_size_type size;
2659
2660 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
2661 if (keep_memory)
2662 {
2663 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2664 if (info)
2665 info->cache_size += size;
2666 }
2667 else
2668 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2669 if (internal_relocs == NULL)
2670 goto error_return;
2671 }
2672
2673 if (external_relocs == NULL)
2674 {
2675 bfd_size_type size = 0;
2676
2677 if (esdo->rel.hdr)
2678 size += esdo->rel.hdr->sh_size;
2679 if (esdo->rela.hdr)
2680 size += esdo->rela.hdr->sh_size;
2681
2682 alloc1 = bfd_malloc (size);
2683 if (alloc1 == NULL)
2684 goto error_return;
2685 external_relocs = alloc1;
2686 }
2687
2688 internal_rela_relocs = internal_relocs;
2689 if (esdo->rel.hdr)
2690 {
2691 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2692 external_relocs,
2693 internal_relocs))
2694 goto error_return;
2695 external_relocs = (((bfd_byte *) external_relocs)
2696 + esdo->rel.hdr->sh_size);
2697 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2698 * bed->s->int_rels_per_ext_rel);
2699 }
2700
2701 if (esdo->rela.hdr
2702 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2703 external_relocs,
2704 internal_rela_relocs)))
2705 goto error_return;
2706
2707 /* Cache the results for next time, if we can. */
2708 if (keep_memory)
2709 esdo->relocs = internal_relocs;
2710
2711 free (alloc1);
2712
2713 /* Don't free alloc2, since if it was allocated we are passing it
2714 back (under the name of internal_relocs). */
2715
2716 return internal_relocs;
2717
2718 error_return:
2719 free (alloc1);
2720 if (alloc2 != NULL)
2721 {
2722 if (keep_memory)
2723 bfd_release (abfd, alloc2);
2724 else
2725 free (alloc2);
2726 }
2727 return NULL;
2728 }
2729
2730 /* This is similar to _bfd_elf_link_info_read_relocs, except for that
2731 NULL is passed to _bfd_elf_link_info_read_relocs for pointer to
2732 struct bfd_link_info. */
2733
2734 Elf_Internal_Rela *
2735 _bfd_elf_link_read_relocs (bfd *abfd,
2736 asection *o,
2737 void *external_relocs,
2738 Elf_Internal_Rela *internal_relocs,
2739 bool keep_memory)
2740 {
2741 return _bfd_elf_link_info_read_relocs (abfd, NULL, o, external_relocs,
2742 internal_relocs, keep_memory);
2743
2744 }
2745
2746 /* Compute the size of, and allocate space for, REL_HDR which is the
2747 section header for a section containing relocations for O. */
2748
2749 static bool
2750 _bfd_elf_link_size_reloc_section (bfd *abfd,
2751 struct bfd_elf_section_reloc_data *reldata)
2752 {
2753 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2754
2755 /* That allows us to calculate the size of the section. */
2756 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2757
2758 /* The contents field must last into write_object_contents, so we
2759 allocate it with bfd_alloc rather than malloc. Also since we
2760 cannot be sure that the contents will actually be filled in,
2761 we zero the allocated space. */
2762 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2763 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2764 return false;
2765
2766 if (reldata->hashes == NULL && reldata->count)
2767 {
2768 struct elf_link_hash_entry **p;
2769
2770 p = ((struct elf_link_hash_entry **)
2771 bfd_zmalloc (reldata->count * sizeof (*p)));
2772 if (p == NULL)
2773 return false;
2774
2775 reldata->hashes = p;
2776 }
2777
2778 return true;
2779 }
2780
2781 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2782 originated from the section given by INPUT_REL_HDR) to the
2783 OUTPUT_BFD. */
2784
2785 bool
2786 _bfd_elf_link_output_relocs (bfd *output_bfd,
2787 asection *input_section,
2788 Elf_Internal_Shdr *input_rel_hdr,
2789 Elf_Internal_Rela *internal_relocs,
2790 struct elf_link_hash_entry **rel_hash
2791 ATTRIBUTE_UNUSED)
2792 {
2793 Elf_Internal_Rela *irela;
2794 Elf_Internal_Rela *irelaend;
2795 bfd_byte *erel;
2796 struct bfd_elf_section_reloc_data *output_reldata;
2797 asection *output_section;
2798 const struct elf_backend_data *bed;
2799 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2800 struct bfd_elf_section_data *esdo;
2801
2802 output_section = input_section->output_section;
2803
2804 bed = get_elf_backend_data (output_bfd);
2805 esdo = elf_section_data (output_section);
2806 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2807 {
2808 output_reldata = &esdo->rel;
2809 swap_out = bed->s->swap_reloc_out;
2810 }
2811 else if (esdo->rela.hdr
2812 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2813 {
2814 output_reldata = &esdo->rela;
2815 swap_out = bed->s->swap_reloca_out;
2816 }
2817 else
2818 {
2819 _bfd_error_handler
2820 /* xgettext:c-format */
2821 (_("%pB: relocation size mismatch in %pB section %pA"),
2822 output_bfd, input_section->owner, input_section);
2823 bfd_set_error (bfd_error_wrong_format);
2824 return false;
2825 }
2826
2827 erel = output_reldata->hdr->contents;
2828 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2829 irela = internal_relocs;
2830 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2831 * bed->s->int_rels_per_ext_rel);
2832 while (irela < irelaend)
2833 {
2834 (*swap_out) (output_bfd, irela, erel);
2835 irela += bed->s->int_rels_per_ext_rel;
2836 erel += input_rel_hdr->sh_entsize;
2837 }
2838
2839 /* Bump the counter, so that we know where to add the next set of
2840 relocations. */
2841 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2842
2843 return true;
2844 }
2845 \f
2846 /* Make weak undefined symbols in PIE dynamic. */
2847
2848 bool
2849 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2850 struct elf_link_hash_entry *h)
2851 {
2852 if (bfd_link_pie (info)
2853 && h->dynindx == -1
2854 && h->root.type == bfd_link_hash_undefweak)
2855 return bfd_elf_link_record_dynamic_symbol (info, h);
2856
2857 return true;
2858 }
2859
2860 /* Fix up the flags for a symbol. This handles various cases which
2861 can only be fixed after all the input files are seen. This is
2862 currently called by both adjust_dynamic_symbol and
2863 assign_sym_version, which is unnecessary but perhaps more robust in
2864 the face of future changes. */
2865
2866 static bool
2867 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2868 struct elf_info_failed *eif)
2869 {
2870 const struct elf_backend_data *bed;
2871
2872 /* If this symbol was mentioned in a non-ELF file, try to set
2873 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2874 permit a non-ELF file to correctly refer to a symbol defined in
2875 an ELF dynamic object. */
2876 if (h->non_elf)
2877 {
2878 while (h->root.type == bfd_link_hash_indirect)
2879 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2880
2881 if (h->root.type != bfd_link_hash_defined
2882 && h->root.type != bfd_link_hash_defweak)
2883 {
2884 h->ref_regular = 1;
2885 h->ref_regular_nonweak = 1;
2886 }
2887 else
2888 {
2889 if (h->root.u.def.section->owner != NULL
2890 && (bfd_get_flavour (h->root.u.def.section->owner)
2891 == bfd_target_elf_flavour))
2892 {
2893 h->ref_regular = 1;
2894 h->ref_regular_nonweak = 1;
2895 }
2896 else
2897 h->def_regular = 1;
2898 }
2899
2900 if (h->dynindx == -1
2901 && (h->def_dynamic
2902 || h->ref_dynamic))
2903 {
2904 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2905 {
2906 eif->failed = true;
2907 return false;
2908 }
2909 }
2910 }
2911 else
2912 {
2913 /* Unfortunately, NON_ELF is only correct if the symbol
2914 was first seen in a non-ELF file. Fortunately, if the symbol
2915 was first seen in an ELF file, we're probably OK unless the
2916 symbol was defined in a non-ELF file. Catch that case here.
2917 FIXME: We're still in trouble if the symbol was first seen in
2918 a dynamic object, and then later in a non-ELF regular object. */
2919 if ((h->root.type == bfd_link_hash_defined
2920 || h->root.type == bfd_link_hash_defweak)
2921 && !h->def_regular
2922 && (h->root.u.def.section->owner != NULL
2923 ? (bfd_get_flavour (h->root.u.def.section->owner)
2924 != bfd_target_elf_flavour)
2925 : (bfd_is_abs_section (h->root.u.def.section)
2926 && !h->def_dynamic)))
2927 h->def_regular = 1;
2928 }
2929
2930 /* Backend specific symbol fixup. */
2931 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2932 if (bed->elf_backend_fixup_symbol
2933 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2934 return false;
2935
2936 /* If this is a final link, and the symbol was defined as a common
2937 symbol in a regular object file, and there was no definition in
2938 any dynamic object, then the linker will have allocated space for
2939 the symbol in a common section but the DEF_REGULAR
2940 flag will not have been set. */
2941 if (h->root.type == bfd_link_hash_defined
2942 && !h->def_regular
2943 && h->ref_regular
2944 && !h->def_dynamic
2945 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
2946 h->def_regular = 1;
2947
2948 /* Symbols defined in discarded sections shouldn't be dynamic. */
2949 if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
2950 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
2951
2952 /* If a weak undefined symbol has non-default visibility, we also
2953 hide it from the dynamic linker. */
2954 else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2955 && h->root.type == bfd_link_hash_undefweak)
2956 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
2957
2958 /* A hidden versioned symbol in executable should be forced local if
2959 it is is locally defined, not referenced by shared library and not
2960 exported. */
2961 else if (bfd_link_executable (eif->info)
2962 && h->versioned == versioned_hidden
2963 && !eif->info->export_dynamic
2964 && !h->dynamic
2965 && !h->ref_dynamic
2966 && h->def_regular)
2967 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
2968
2969 /* If -Bsymbolic was used (which means to bind references to global
2970 symbols to the definition within the shared object), and this
2971 symbol was defined in a regular object, then it actually doesn't
2972 need a PLT entry. Likewise, if the symbol has non-default
2973 visibility. If the symbol has hidden or internal visibility, we
2974 will force it local. */
2975 else if (h->needs_plt
2976 && bfd_link_pic (eif->info)
2977 && is_elf_hash_table (eif->info->hash)
2978 && (SYMBOLIC_BIND (eif->info, h)
2979 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2980 && h->def_regular)
2981 {
2982 bool force_local;
2983
2984 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2985 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2986 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2987 }
2988
2989 /* If this is a weak defined symbol in a dynamic object, and we know
2990 the real definition in the dynamic object, copy interesting flags
2991 over to the real definition. */
2992 if (h->is_weakalias)
2993 {
2994 struct elf_link_hash_entry *def = weakdef (h);
2995
2996 /* If the real definition is defined by a regular object file,
2997 don't do anything special. See the longer description in
2998 _bfd_elf_adjust_dynamic_symbol, below. If the def is not
2999 bfd_link_hash_defined as it was when put on the alias list
3000 then it must have originally been a versioned symbol (for
3001 which a non-versioned indirect symbol is created) and later
3002 a definition for the non-versioned symbol is found. In that
3003 case the indirection is flipped with the versioned symbol
3004 becoming an indirect pointing at the non-versioned symbol.
3005 Thus, not an alias any more. */
3006 if (def->def_regular
3007 || def->root.type != bfd_link_hash_defined)
3008 {
3009 h = def;
3010 while ((h = h->u.alias) != def)
3011 h->is_weakalias = 0;
3012 }
3013 else
3014 {
3015 while (h->root.type == bfd_link_hash_indirect)
3016 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3017 BFD_ASSERT (h->root.type == bfd_link_hash_defined
3018 || h->root.type == bfd_link_hash_defweak);
3019 BFD_ASSERT (def->def_dynamic);
3020 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
3021 }
3022 }
3023
3024 return true;
3025 }
3026
3027 /* Make the backend pick a good value for a dynamic symbol. This is
3028 called via elf_link_hash_traverse, and also calls itself
3029 recursively. */
3030
3031 static bool
3032 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
3033 {
3034 struct elf_info_failed *eif = (struct elf_info_failed *) data;
3035 struct elf_link_hash_table *htab;
3036 const struct elf_backend_data *bed;
3037
3038 if (! is_elf_hash_table (eif->info->hash))
3039 return false;
3040
3041 /* Ignore indirect symbols. These are added by the versioning code. */
3042 if (h->root.type == bfd_link_hash_indirect)
3043 return true;
3044
3045 /* Fix the symbol flags. */
3046 if (! _bfd_elf_fix_symbol_flags (h, eif))
3047 return false;
3048
3049 htab = elf_hash_table (eif->info);
3050 bed = get_elf_backend_data (htab->dynobj);
3051
3052 if (h->root.type == bfd_link_hash_undefweak)
3053 {
3054 if (eif->info->dynamic_undefined_weak == 0)
3055 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3056 else if (eif->info->dynamic_undefined_weak > 0
3057 && h->ref_regular
3058 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3059 && !bfd_hide_sym_by_version (eif->info->version_info,
3060 h->root.root.string))
3061 {
3062 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
3063 {
3064 eif->failed = true;
3065 return false;
3066 }
3067 }
3068 }
3069
3070 /* If this symbol does not require a PLT entry, and it is not
3071 defined by a dynamic object, or is not referenced by a regular
3072 object, ignore it. We do have to handle a weak defined symbol,
3073 even if no regular object refers to it, if we decided to add it
3074 to the dynamic symbol table. FIXME: Do we normally need to worry
3075 about symbols which are defined by one dynamic object and
3076 referenced by another one? */
3077 if (!h->needs_plt
3078 && h->type != STT_GNU_IFUNC
3079 && (h->def_regular
3080 || !h->def_dynamic
3081 || (!h->ref_regular
3082 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
3083 {
3084 h->plt = elf_hash_table (eif->info)->init_plt_offset;
3085 return true;
3086 }
3087
3088 /* If we've already adjusted this symbol, don't do it again. This
3089 can happen via a recursive call. */
3090 if (h->dynamic_adjusted)
3091 return true;
3092
3093 /* Don't look at this symbol again. Note that we must set this
3094 after checking the above conditions, because we may look at a
3095 symbol once, decide not to do anything, and then get called
3096 recursively later after REF_REGULAR is set below. */
3097 h->dynamic_adjusted = 1;
3098
3099 /* If this is a weak definition, and we know a real definition, and
3100 the real symbol is not itself defined by a regular object file,
3101 then get a good value for the real definition. We handle the
3102 real symbol first, for the convenience of the backend routine.
3103
3104 Note that there is a confusing case here. If the real definition
3105 is defined by a regular object file, we don't get the real symbol
3106 from the dynamic object, but we do get the weak symbol. If the
3107 processor backend uses a COPY reloc, then if some routine in the
3108 dynamic object changes the real symbol, we will not see that
3109 change in the corresponding weak symbol. This is the way other
3110 ELF linkers work as well, and seems to be a result of the shared
3111 library model.
3112
3113 I will clarify this issue. Most SVR4 shared libraries define the
3114 variable _timezone and define timezone as a weak synonym. The
3115 tzset call changes _timezone. If you write
3116 extern int timezone;
3117 int _timezone = 5;
3118 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3119 you might expect that, since timezone is a synonym for _timezone,
3120 the same number will print both times. However, if the processor
3121 backend uses a COPY reloc, then actually timezone will be copied
3122 into your process image, and, since you define _timezone
3123 yourself, _timezone will not. Thus timezone and _timezone will
3124 wind up at different memory locations. The tzset call will set
3125 _timezone, leaving timezone unchanged. */
3126
3127 if (h->is_weakalias)
3128 {
3129 struct elf_link_hash_entry *def = weakdef (h);
3130
3131 /* If we get to this point, there is an implicit reference to
3132 the alias by a regular object file via the weak symbol H. */
3133 def->ref_regular = 1;
3134
3135 /* Ensure that the backend adjust_dynamic_symbol function sees
3136 the strong alias before H by recursively calling ourselves. */
3137 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
3138 return false;
3139 }
3140
3141 /* If a symbol has no type and no size and does not require a PLT
3142 entry, then we are probably about to do the wrong thing here: we
3143 are probably going to create a COPY reloc for an empty object.
3144 This case can arise when a shared object is built with assembly
3145 code, and the assembly code fails to set the symbol type. */
3146 if (h->size == 0
3147 && h->type == STT_NOTYPE
3148 && !h->needs_plt)
3149 _bfd_error_handler
3150 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3151 h->root.root.string);
3152
3153 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3154 {
3155 eif->failed = true;
3156 return false;
3157 }
3158
3159 return true;
3160 }
3161
3162 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3163 DYNBSS. */
3164
3165 bool
3166 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3167 struct elf_link_hash_entry *h,
3168 asection *dynbss)
3169 {
3170 unsigned int power_of_two;
3171 bfd_vma mask;
3172 asection *sec = h->root.u.def.section;
3173
3174 /* The section alignment of the definition is the maximum alignment
3175 requirement of symbols defined in the section. Since we don't
3176 know the symbol alignment requirement, we start with the
3177 maximum alignment and check low bits of the symbol address
3178 for the minimum alignment. */
3179 power_of_two = bfd_section_alignment (sec);
3180 mask = ((bfd_vma) 1 << power_of_two) - 1;
3181 while ((h->root.u.def.value & mask) != 0)
3182 {
3183 mask >>= 1;
3184 --power_of_two;
3185 }
3186
3187 if (power_of_two > bfd_section_alignment (dynbss))
3188 {
3189 /* Adjust the section alignment if needed. */
3190 if (!bfd_set_section_alignment (dynbss, power_of_two))
3191 return false;
3192 }
3193
3194 /* We make sure that the symbol will be aligned properly. */
3195 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3196
3197 /* Define the symbol as being at this point in DYNBSS. */
3198 h->root.u.def.section = dynbss;
3199 h->root.u.def.value = dynbss->size;
3200
3201 /* Increment the size of DYNBSS to make room for the symbol. */
3202 dynbss->size += h->size;
3203
3204 /* No error if extern_protected_data is true. */
3205 if (h->protected_def
3206 && (!info->extern_protected_data
3207 || (info->extern_protected_data < 0
3208 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
3209 info->callbacks->einfo
3210 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3211 h->root.root.string);
3212
3213 return true;
3214 }
3215
3216 /* Adjust all external symbols pointing into SEC_MERGE sections
3217 to reflect the object merging within the sections. */
3218
3219 static bool
3220 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
3221 {
3222 asection *sec;
3223
3224 if ((h->root.type == bfd_link_hash_defined
3225 || h->root.type == bfd_link_hash_defweak)
3226 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
3227 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3228 {
3229 bfd *output_bfd = (bfd *) data;
3230
3231 h->root.u.def.value =
3232 _bfd_merged_section_offset (output_bfd,
3233 &h->root.u.def.section,
3234 elf_section_data (sec)->sec_info,
3235 h->root.u.def.value);
3236 }
3237
3238 return true;
3239 }
3240
3241 /* Returns false if the symbol referred to by H should be considered
3242 to resolve local to the current module, and true if it should be
3243 considered to bind dynamically. */
3244
3245 bool
3246 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3247 struct bfd_link_info *info,
3248 bool not_local_protected)
3249 {
3250 bool binding_stays_local_p;
3251 const struct elf_backend_data *bed;
3252 struct elf_link_hash_table *hash_table;
3253
3254 if (h == NULL)
3255 return false;
3256
3257 while (h->root.type == bfd_link_hash_indirect
3258 || h->root.type == bfd_link_hash_warning)
3259 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3260
3261 /* If it was forced local, then clearly it's not dynamic. */
3262 if (h->dynindx == -1)
3263 return false;
3264 if (h->forced_local)
3265 return false;
3266
3267 /* Identify the cases where name binding rules say that a
3268 visible symbol resolves locally. */
3269 binding_stays_local_p = (bfd_link_executable (info)
3270 || SYMBOLIC_BIND (info, h));
3271
3272 switch (ELF_ST_VISIBILITY (h->other))
3273 {
3274 case STV_INTERNAL:
3275 case STV_HIDDEN:
3276 return false;
3277
3278 case STV_PROTECTED:
3279 hash_table = elf_hash_table (info);
3280 if (!is_elf_hash_table (&hash_table->root))
3281 return false;
3282
3283 bed = get_elf_backend_data (hash_table->dynobj);
3284
3285 /* Proper resolution for function pointer equality may require
3286 that these symbols perhaps be resolved dynamically, even though
3287 we should be resolving them to the current module. */
3288 if (!not_local_protected || !bed->is_function_type (h->type))
3289 binding_stays_local_p = true;
3290 break;
3291
3292 default:
3293 break;
3294 }
3295
3296 /* If it isn't defined locally, then clearly it's dynamic. */
3297 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
3298 return true;
3299
3300 /* Otherwise, the symbol is dynamic if binding rules don't tell
3301 us that it remains local. */
3302 return !binding_stays_local_p;
3303 }
3304
3305 /* Return true if the symbol referred to by H should be considered
3306 to resolve local to the current module, and false otherwise. Differs
3307 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3308 undefined symbols. The two functions are virtually identical except
3309 for the place where dynindx == -1 is tested. If that test is true,
3310 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3311 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3312 defined symbols.
3313 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3314 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3315 treatment of undefined weak symbols. For those that do not make
3316 undefined weak symbols dynamic, both functions may return false. */
3317
3318 bool
3319 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3320 struct bfd_link_info *info,
3321 bool local_protected)
3322 {
3323 const struct elf_backend_data *bed;
3324 struct elf_link_hash_table *hash_table;
3325
3326 /* If it's a local sym, of course we resolve locally. */
3327 if (h == NULL)
3328 return true;
3329
3330 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3331 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3332 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3333 return true;
3334
3335 /* Forced local symbols resolve locally. */
3336 if (h->forced_local)
3337 return true;
3338
3339 /* Common symbols that become definitions don't get the DEF_REGULAR
3340 flag set, so test it first, and don't bail out. */
3341 if (ELF_COMMON_DEF_P (h))
3342 /* Do nothing. */;
3343 /* If we don't have a definition in a regular file, then we can't
3344 resolve locally. The sym is either undefined or dynamic. */
3345 else if (!h->def_regular)
3346 return false;
3347
3348 /* Non-dynamic symbols resolve locally. */
3349 if (h->dynindx == -1)
3350 return true;
3351
3352 /* At this point, we know the symbol is defined and dynamic. In an
3353 executable it must resolve locally, likewise when building symbolic
3354 shared libraries. */
3355 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3356 return true;
3357
3358 /* Now deal with defined dynamic symbols in shared libraries. Ones
3359 with default visibility might not resolve locally. */
3360 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3361 return false;
3362
3363 hash_table = elf_hash_table (info);
3364 if (!is_elf_hash_table (&hash_table->root))
3365 return true;
3366
3367 /* STV_PROTECTED symbols with indirect external access are local. */
3368 if (info->indirect_extern_access > 0)
3369 return true;
3370
3371 bed = get_elf_backend_data (hash_table->dynobj);
3372
3373 /* If extern_protected_data is false, STV_PROTECTED non-function
3374 symbols are local. */
3375 if ((!info->extern_protected_data
3376 || (info->extern_protected_data < 0
3377 && !bed->extern_protected_data))
3378 && !bed->is_function_type (h->type))
3379 return true;
3380
3381 /* Function pointer equality tests may require that STV_PROTECTED
3382 symbols be treated as dynamic symbols. If the address of a
3383 function not defined in an executable is set to that function's
3384 plt entry in the executable, then the address of the function in
3385 a shared library must also be the plt entry in the executable. */
3386 return local_protected;
3387 }
3388
3389 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3390 aligned. Returns the first TLS output section. */
3391
3392 struct bfd_section *
3393 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3394 {
3395 struct bfd_section *sec, *tls;
3396 unsigned int align = 0;
3397
3398 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3399 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3400 break;
3401 tls = sec;
3402
3403 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3404 if (sec->alignment_power > align)
3405 align = sec->alignment_power;
3406
3407 elf_hash_table (info)->tls_sec = tls;
3408
3409 /* Ensure the alignment of the first section (usually .tdata) is the largest
3410 alignment, so that the tls segment starts aligned. */
3411 if (tls != NULL)
3412 tls->alignment_power = align;
3413
3414 return tls;
3415 }
3416
3417 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3418 static bool
3419 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3420 Elf_Internal_Sym *sym)
3421 {
3422 const struct elf_backend_data *bed;
3423
3424 /* Local symbols do not count, but target specific ones might. */
3425 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3426 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3427 return false;
3428
3429 bed = get_elf_backend_data (abfd);
3430 /* Function symbols do not count. */
3431 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3432 return false;
3433
3434 /* If the section is undefined, then so is the symbol. */
3435 if (sym->st_shndx == SHN_UNDEF)
3436 return false;
3437
3438 /* If the symbol is defined in the common section, then
3439 it is a common definition and so does not count. */
3440 if (bed->common_definition (sym))
3441 return false;
3442
3443 /* If the symbol is in a target specific section then we
3444 must rely upon the backend to tell us what it is. */
3445 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3446 /* FIXME - this function is not coded yet:
3447
3448 return _bfd_is_global_symbol_definition (abfd, sym);
3449
3450 Instead for now assume that the definition is not global,
3451 Even if this is wrong, at least the linker will behave
3452 in the same way that it used to do. */
3453 return false;
3454
3455 return true;
3456 }
3457
3458 /* Search the symbol table of the archive element of the archive ABFD
3459 whose archive map contains a mention of SYMDEF, and determine if
3460 the symbol is defined in this element. */
3461 static bool
3462 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3463 {
3464 Elf_Internal_Shdr * hdr;
3465 size_t symcount;
3466 size_t extsymcount;
3467 size_t extsymoff;
3468 Elf_Internal_Sym *isymbuf;
3469 Elf_Internal_Sym *isym;
3470 Elf_Internal_Sym *isymend;
3471 bool result;
3472
3473 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3474 if (abfd == NULL)
3475 return false;
3476
3477 if (! bfd_check_format (abfd, bfd_object))
3478 return false;
3479
3480 /* Select the appropriate symbol table. If we don't know if the
3481 object file is an IR object, give linker LTO plugin a chance to
3482 get the correct symbol table. */
3483 if (abfd->plugin_format == bfd_plugin_yes
3484 #if BFD_SUPPORTS_PLUGINS
3485 || (abfd->plugin_format == bfd_plugin_unknown
3486 && bfd_link_plugin_object_p (abfd))
3487 #endif
3488 )
3489 {
3490 /* Use the IR symbol table if the object has been claimed by
3491 plugin. */
3492 abfd = abfd->plugin_dummy_bfd;
3493 hdr = &elf_tdata (abfd)->symtab_hdr;
3494 }
3495 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3496 hdr = &elf_tdata (abfd)->symtab_hdr;
3497 else
3498 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3499
3500 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3501
3502 /* The sh_info field of the symtab header tells us where the
3503 external symbols start. We don't care about the local symbols. */
3504 if (elf_bad_symtab (abfd))
3505 {
3506 extsymcount = symcount;
3507 extsymoff = 0;
3508 }
3509 else
3510 {
3511 extsymcount = symcount - hdr->sh_info;
3512 extsymoff = hdr->sh_info;
3513 }
3514
3515 if (extsymcount == 0)
3516 return false;
3517
3518 /* Read in the symbol table. */
3519 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3520 NULL, NULL, NULL);
3521 if (isymbuf == NULL)
3522 return false;
3523
3524 /* Scan the symbol table looking for SYMDEF. */
3525 result = false;
3526 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3527 {
3528 const char *name;
3529
3530 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3531 isym->st_name);
3532 if (name == NULL)
3533 break;
3534
3535 if (strcmp (name, symdef->name) == 0)
3536 {
3537 result = is_global_data_symbol_definition (abfd, isym);
3538 break;
3539 }
3540 }
3541
3542 free (isymbuf);
3543
3544 return result;
3545 }
3546 \f
3547 /* Add an entry to the .dynamic table. */
3548
3549 bool
3550 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3551 bfd_vma tag,
3552 bfd_vma val)
3553 {
3554 struct elf_link_hash_table *hash_table;
3555 const struct elf_backend_data *bed;
3556 asection *s;
3557 bfd_size_type newsize;
3558 bfd_byte *newcontents;
3559 Elf_Internal_Dyn dyn;
3560
3561 hash_table = elf_hash_table (info);
3562 if (! is_elf_hash_table (&hash_table->root))
3563 return false;
3564
3565 if (tag == DT_RELA || tag == DT_REL)
3566 hash_table->dynamic_relocs = true;
3567
3568 bed = get_elf_backend_data (hash_table->dynobj);
3569 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3570 BFD_ASSERT (s != NULL);
3571
3572 newsize = s->size + bed->s->sizeof_dyn;
3573 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3574 if (newcontents == NULL)
3575 return false;
3576
3577 dyn.d_tag = tag;
3578 dyn.d_un.d_val = val;
3579 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3580
3581 s->size = newsize;
3582 s->contents = newcontents;
3583
3584 return true;
3585 }
3586
3587 /* Strip zero-sized dynamic sections. */
3588
3589 bool
3590 _bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info *info)
3591 {
3592 struct elf_link_hash_table *hash_table;
3593 const struct elf_backend_data *bed;
3594 asection *s, *sdynamic, **pp;
3595 asection *rela_dyn, *rel_dyn;
3596 Elf_Internal_Dyn dyn;
3597 bfd_byte *extdyn, *next;
3598 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3599 bool strip_zero_sized;
3600 bool strip_zero_sized_plt;
3601
3602 if (bfd_link_relocatable (info))
3603 return true;
3604
3605 hash_table = elf_hash_table (info);
3606 if (!is_elf_hash_table (&hash_table->root))
3607 return false;
3608
3609 if (!hash_table->dynobj)
3610 return true;
3611
3612 sdynamic= bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3613 if (!sdynamic)
3614 return true;
3615
3616 bed = get_elf_backend_data (hash_table->dynobj);
3617 swap_dyn_in = bed->s->swap_dyn_in;
3618
3619 strip_zero_sized = false;
3620 strip_zero_sized_plt = false;
3621
3622 /* Strip zero-sized dynamic sections. */
3623 rela_dyn = bfd_get_section_by_name (info->output_bfd, ".rela.dyn");
3624 rel_dyn = bfd_get_section_by_name (info->output_bfd, ".rel.dyn");
3625 for (pp = &info->output_bfd->sections; (s = *pp) != NULL;)
3626 if (s->size == 0
3627 && (s == rela_dyn
3628 || s == rel_dyn
3629 || s == hash_table->srelplt->output_section
3630 || s == hash_table->splt->output_section))
3631 {
3632 *pp = s->next;
3633 info->output_bfd->section_count--;
3634 strip_zero_sized = true;
3635 if (s == rela_dyn)
3636 s = rela_dyn;
3637 if (s == rel_dyn)
3638 s = rel_dyn;
3639 else if (s == hash_table->splt->output_section)
3640 {
3641 s = hash_table->splt;
3642 strip_zero_sized_plt = true;
3643 }
3644 else
3645 s = hash_table->srelplt;
3646 s->flags |= SEC_EXCLUDE;
3647 s->output_section = bfd_abs_section_ptr;
3648 }
3649 else
3650 pp = &s->next;
3651
3652 if (strip_zero_sized_plt && sdynamic->size != 0)
3653 for (extdyn = sdynamic->contents;
3654 extdyn < sdynamic->contents + sdynamic->size;
3655 extdyn = next)
3656 {
3657 next = extdyn + bed->s->sizeof_dyn;
3658 swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3659 switch (dyn.d_tag)
3660 {
3661 default:
3662 break;
3663 case DT_JMPREL:
3664 case DT_PLTRELSZ:
3665 case DT_PLTREL:
3666 /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
3667 the procedure linkage table (the .plt section) has been
3668 removed. */
3669 memmove (extdyn, next,
3670 sdynamic->size - (next - sdynamic->contents));
3671 next = extdyn;
3672 }
3673 }
3674
3675 if (strip_zero_sized)
3676 {
3677 /* Regenerate program headers. */
3678 elf_seg_map (info->output_bfd) = NULL;
3679 return _bfd_elf_map_sections_to_segments (info->output_bfd, info);
3680 }
3681
3682 return true;
3683 }
3684
3685 /* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error,
3686 1 if a DT_NEEDED tag already exists, and 0 on success. */
3687
3688 int
3689 bfd_elf_add_dt_needed_tag (bfd *abfd, struct bfd_link_info *info)
3690 {
3691 struct elf_link_hash_table *hash_table;
3692 size_t strindex;
3693 const char *soname;
3694
3695 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3696 return -1;
3697
3698 hash_table = elf_hash_table (info);
3699 soname = elf_dt_name (abfd);
3700 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, false);
3701 if (strindex == (size_t) -1)
3702 return -1;
3703
3704 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3705 {
3706 asection *sdyn;
3707 const struct elf_backend_data *bed;
3708 bfd_byte *extdyn;
3709
3710 bed = get_elf_backend_data (hash_table->dynobj);
3711 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3712 if (sdyn != NULL && sdyn->size != 0)
3713 for (extdyn = sdyn->contents;
3714 extdyn < sdyn->contents + sdyn->size;
3715 extdyn += bed->s->sizeof_dyn)
3716 {
3717 Elf_Internal_Dyn dyn;
3718
3719 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3720 if (dyn.d_tag == DT_NEEDED
3721 && dyn.d_un.d_val == strindex)
3722 {
3723 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3724 return 1;
3725 }
3726 }
3727 }
3728
3729 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3730 return -1;
3731
3732 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3733 return -1;
3734
3735 return 0;
3736 }
3737
3738 /* Return true if SONAME is on the needed list between NEEDED and STOP
3739 (or the end of list if STOP is NULL), and needed by a library that
3740 will be loaded. */
3741
3742 static bool
3743 on_needed_list (const char *soname,
3744 struct bfd_link_needed_list *needed,
3745 struct bfd_link_needed_list *stop)
3746 {
3747 struct bfd_link_needed_list *look;
3748 for (look = needed; look != stop; look = look->next)
3749 if (strcmp (soname, look->name) == 0
3750 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3751 /* If needed by a library that itself is not directly
3752 needed, recursively check whether that library is
3753 indirectly needed. Since we add DT_NEEDED entries to
3754 the end of the list, library dependencies appear after
3755 the library. Therefore search prior to the current
3756 LOOK, preventing possible infinite recursion. */
3757 || on_needed_list (elf_dt_name (look->by), needed, look)))
3758 return true;
3759
3760 return false;
3761 }
3762
3763 /* Sort symbol by value, section, size, and type. */
3764 static int
3765 elf_sort_symbol (const void *arg1, const void *arg2)
3766 {
3767 const struct elf_link_hash_entry *h1;
3768 const struct elf_link_hash_entry *h2;
3769 bfd_signed_vma vdiff;
3770 int sdiff;
3771 const char *n1;
3772 const char *n2;
3773
3774 h1 = *(const struct elf_link_hash_entry **) arg1;
3775 h2 = *(const struct elf_link_hash_entry **) arg2;
3776 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3777 if (vdiff != 0)
3778 return vdiff > 0 ? 1 : -1;
3779
3780 sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3781 if (sdiff != 0)
3782 return sdiff;
3783
3784 /* Sort so that sized symbols are selected over zero size symbols. */
3785 vdiff = h1->size - h2->size;
3786 if (vdiff != 0)
3787 return vdiff > 0 ? 1 : -1;
3788
3789 /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */
3790 if (h1->type != h2->type)
3791 return h1->type - h2->type;
3792
3793 /* If symbols are properly sized and typed, and multiple strong
3794 aliases are not defined in a shared library by the user we
3795 shouldn't get here. Unfortunately linker script symbols like
3796 __bss_start sometimes match a user symbol defined at the start of
3797 .bss without proper size and type. We'd like to preference the
3798 user symbol over reserved system symbols. Sort on leading
3799 underscores. */
3800 n1 = h1->root.root.string;
3801 n2 = h2->root.root.string;
3802 while (*n1 == *n2)
3803 {
3804 if (*n1 == 0)
3805 break;
3806 ++n1;
3807 ++n2;
3808 }
3809 if (*n1 == '_')
3810 return -1;
3811 if (*n2 == '_')
3812 return 1;
3813
3814 /* Final sort on name selects user symbols like '_u' over reserved
3815 system symbols like '_Z' and also will avoid qsort instability. */
3816 return *n1 - *n2;
3817 }
3818
3819 /* This function is used to adjust offsets into .dynstr for
3820 dynamic symbols. This is called via elf_link_hash_traverse. */
3821
3822 static bool
3823 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3824 {
3825 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3826
3827 if (h->dynindx != -1)
3828 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3829 return true;
3830 }
3831
3832 /* Assign string offsets in .dynstr, update all structures referencing
3833 them. */
3834
3835 static bool
3836 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3837 {
3838 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3839 struct elf_link_local_dynamic_entry *entry;
3840 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3841 bfd *dynobj = hash_table->dynobj;
3842 asection *sdyn;
3843 bfd_size_type size;
3844 const struct elf_backend_data *bed;
3845 bfd_byte *extdyn;
3846
3847 _bfd_elf_strtab_finalize (dynstr);
3848 size = _bfd_elf_strtab_size (dynstr);
3849
3850 /* Allow the linker to examine the dynsymtab now it's fully populated. */
3851
3852 if (info->callbacks->examine_strtab)
3853 info->callbacks->examine_strtab (dynstr);
3854
3855 bed = get_elf_backend_data (dynobj);
3856 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3857 BFD_ASSERT (sdyn != NULL);
3858
3859 /* Update all .dynamic entries referencing .dynstr strings. */
3860 for (extdyn = sdyn->contents;
3861 extdyn < PTR_ADD (sdyn->contents, sdyn->size);
3862 extdyn += bed->s->sizeof_dyn)
3863 {
3864 Elf_Internal_Dyn dyn;
3865
3866 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3867 switch (dyn.d_tag)
3868 {
3869 case DT_STRSZ:
3870 dyn.d_un.d_val = size;
3871 break;
3872 case DT_NEEDED:
3873 case DT_SONAME:
3874 case DT_RPATH:
3875 case DT_RUNPATH:
3876 case DT_FILTER:
3877 case DT_AUXILIARY:
3878 case DT_AUDIT:
3879 case DT_DEPAUDIT:
3880 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3881 break;
3882 default:
3883 continue;
3884 }
3885 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3886 }
3887
3888 /* Now update local dynamic symbols. */
3889 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3890 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3891 entry->isym.st_name);
3892
3893 /* And the rest of dynamic symbols. */
3894 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3895
3896 /* Adjust version definitions. */
3897 if (elf_tdata (output_bfd)->cverdefs)
3898 {
3899 asection *s;
3900 bfd_byte *p;
3901 size_t i;
3902 Elf_Internal_Verdef def;
3903 Elf_Internal_Verdaux defaux;
3904
3905 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3906 p = s->contents;
3907 do
3908 {
3909 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3910 &def);
3911 p += sizeof (Elf_External_Verdef);
3912 if (def.vd_aux != sizeof (Elf_External_Verdef))
3913 continue;
3914 for (i = 0; i < def.vd_cnt; ++i)
3915 {
3916 _bfd_elf_swap_verdaux_in (output_bfd,
3917 (Elf_External_Verdaux *) p, &defaux);
3918 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3919 defaux.vda_name);
3920 _bfd_elf_swap_verdaux_out (output_bfd,
3921 &defaux, (Elf_External_Verdaux *) p);
3922 p += sizeof (Elf_External_Verdaux);
3923 }
3924 }
3925 while (def.vd_next);
3926 }
3927
3928 /* Adjust version references. */
3929 if (elf_tdata (output_bfd)->verref)
3930 {
3931 asection *s;
3932 bfd_byte *p;
3933 size_t i;
3934 Elf_Internal_Verneed need;
3935 Elf_Internal_Vernaux needaux;
3936
3937 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3938 p = s->contents;
3939 do
3940 {
3941 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3942 &need);
3943 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3944 _bfd_elf_swap_verneed_out (output_bfd, &need,
3945 (Elf_External_Verneed *) p);
3946 p += sizeof (Elf_External_Verneed);
3947 for (i = 0; i < need.vn_cnt; ++i)
3948 {
3949 _bfd_elf_swap_vernaux_in (output_bfd,
3950 (Elf_External_Vernaux *) p, &needaux);
3951 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3952 needaux.vna_name);
3953 _bfd_elf_swap_vernaux_out (output_bfd,
3954 &needaux,
3955 (Elf_External_Vernaux *) p);
3956 p += sizeof (Elf_External_Vernaux);
3957 }
3958 }
3959 while (need.vn_next);
3960 }
3961
3962 return true;
3963 }
3964 \f
3965 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3966 The default is to only match when the INPUT and OUTPUT are exactly
3967 the same target. */
3968
3969 bool
3970 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3971 const bfd_target *output)
3972 {
3973 return input == output;
3974 }
3975
3976 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3977 This version is used when different targets for the same architecture
3978 are virtually identical. */
3979
3980 bool
3981 _bfd_elf_relocs_compatible (const bfd_target *input,
3982 const bfd_target *output)
3983 {
3984 const struct elf_backend_data *obed, *ibed;
3985
3986 if (input == output)
3987 return true;
3988
3989 ibed = xvec_get_elf_backend_data (input);
3990 obed = xvec_get_elf_backend_data (output);
3991
3992 if (ibed->arch != obed->arch)
3993 return false;
3994
3995 /* If both backends are using this function, deem them compatible. */
3996 return ibed->relocs_compatible == obed->relocs_compatible;
3997 }
3998
3999 /* Make a special call to the linker "notice" function to tell it that
4000 we are about to handle an as-needed lib, or have finished
4001 processing the lib. */
4002
4003 bool
4004 _bfd_elf_notice_as_needed (bfd *ibfd,
4005 struct bfd_link_info *info,
4006 enum notice_asneeded_action act)
4007 {
4008 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
4009 }
4010
4011 /* Check relocations an ELF object file. */
4012
4013 bool
4014 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
4015 {
4016 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4017 struct elf_link_hash_table *htab = elf_hash_table (info);
4018
4019 /* If this object is the same format as the output object, and it is
4020 not a shared library, then let the backend look through the
4021 relocs.
4022
4023 This is required to build global offset table entries and to
4024 arrange for dynamic relocs. It is not required for the
4025 particular common case of linking non PIC code, even when linking
4026 against shared libraries, but unfortunately there is no way of
4027 knowing whether an object file has been compiled PIC or not.
4028 Looking through the relocs is not particularly time consuming.
4029 The problem is that we must either (1) keep the relocs in memory,
4030 which causes the linker to require additional runtime memory or
4031 (2) read the relocs twice from the input file, which wastes time.
4032 This would be a good case for using mmap.
4033
4034 I have no idea how to handle linking PIC code into a file of a
4035 different format. It probably can't be done. */
4036 if ((abfd->flags & DYNAMIC) == 0
4037 && is_elf_hash_table (&htab->root)
4038 && bed->check_relocs != NULL
4039 && elf_object_id (abfd) == elf_hash_table_id (htab)
4040 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4041 {
4042 asection *o;
4043
4044 for (o = abfd->sections; o != NULL; o = o->next)
4045 {
4046 Elf_Internal_Rela *internal_relocs;
4047 bool ok;
4048
4049 /* Don't check relocations in excluded sections. Don't do
4050 anything special with non-loaded, non-alloced sections.
4051 In particular, any relocs in such sections should not
4052 affect GOT and PLT reference counting (ie. we don't
4053 allow them to create GOT or PLT entries), there's no
4054 possibility or desire to optimize TLS relocs, and
4055 there's not much point in propagating relocs to shared
4056 libs that the dynamic linker won't relocate. */
4057 if ((o->flags & SEC_ALLOC) == 0
4058 || (o->flags & SEC_RELOC) == 0
4059 || (o->flags & SEC_EXCLUDE) != 0
4060 || o->reloc_count == 0
4061 || ((info->strip == strip_all || info->strip == strip_debugger)
4062 && (o->flags & SEC_DEBUGGING) != 0)
4063 || bfd_is_abs_section (o->output_section))
4064 continue;
4065
4066 internal_relocs = _bfd_elf_link_info_read_relocs (abfd, info,
4067 o, NULL,
4068 NULL,
4069 _bfd_link_keep_memory (info));
4070 if (internal_relocs == NULL)
4071 return false;
4072
4073 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
4074
4075 if (elf_section_data (o)->relocs != internal_relocs)
4076 free (internal_relocs);
4077
4078 if (! ok)
4079 return false;
4080 }
4081 }
4082
4083 return true;
4084 }
4085
4086 /* Add symbols from an ELF object file to the linker hash table. */
4087
4088 static bool
4089 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
4090 {
4091 Elf_Internal_Ehdr *ehdr;
4092 Elf_Internal_Shdr *hdr;
4093 size_t symcount;
4094 size_t extsymcount;
4095 size_t extsymoff;
4096 struct elf_link_hash_entry **sym_hash;
4097 bool dynamic;
4098 Elf_External_Versym *extversym = NULL;
4099 Elf_External_Versym *extversym_end = NULL;
4100 Elf_External_Versym *ever;
4101 struct elf_link_hash_entry *weaks;
4102 struct elf_link_hash_entry **nondeflt_vers = NULL;
4103 size_t nondeflt_vers_cnt = 0;
4104 Elf_Internal_Sym *isymbuf = NULL;
4105 Elf_Internal_Sym *isym;
4106 Elf_Internal_Sym *isymend;
4107 const struct elf_backend_data *bed;
4108 bool add_needed;
4109 struct elf_link_hash_table *htab;
4110 void *alloc_mark = NULL;
4111 struct bfd_hash_entry **old_table = NULL;
4112 unsigned int old_size = 0;
4113 unsigned int old_count = 0;
4114 void *old_tab = NULL;
4115 void *old_ent;
4116 struct bfd_link_hash_entry *old_undefs = NULL;
4117 struct bfd_link_hash_entry *old_undefs_tail = NULL;
4118 void *old_strtab = NULL;
4119 size_t tabsize = 0;
4120 asection *s;
4121 bool just_syms;
4122
4123 htab = elf_hash_table (info);
4124 bed = get_elf_backend_data (abfd);
4125
4126 if ((abfd->flags & DYNAMIC) == 0)
4127 dynamic = false;
4128 else
4129 {
4130 dynamic = true;
4131
4132 /* You can't use -r against a dynamic object. Also, there's no
4133 hope of using a dynamic object which does not exactly match
4134 the format of the output file. */
4135 if (bfd_link_relocatable (info)
4136 || !is_elf_hash_table (&htab->root)
4137 || info->output_bfd->xvec != abfd->xvec)
4138 {
4139 if (bfd_link_relocatable (info))
4140 bfd_set_error (bfd_error_invalid_operation);
4141 else
4142 bfd_set_error (bfd_error_wrong_format);
4143 goto error_return;
4144 }
4145 }
4146
4147 ehdr = elf_elfheader (abfd);
4148 if (info->warn_alternate_em
4149 && bed->elf_machine_code != ehdr->e_machine
4150 && ((bed->elf_machine_alt1 != 0
4151 && ehdr->e_machine == bed->elf_machine_alt1)
4152 || (bed->elf_machine_alt2 != 0
4153 && ehdr->e_machine == bed->elf_machine_alt2)))
4154 _bfd_error_handler
4155 /* xgettext:c-format */
4156 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
4157 ehdr->e_machine, abfd, bed->elf_machine_code);
4158
4159 /* As a GNU extension, any input sections which are named
4160 .gnu.warning.SYMBOL are treated as warning symbols for the given
4161 symbol. This differs from .gnu.warning sections, which generate
4162 warnings when they are included in an output file. */
4163 /* PR 12761: Also generate this warning when building shared libraries. */
4164 for (s = abfd->sections; s != NULL; s = s->next)
4165 {
4166 const char *name;
4167
4168 name = bfd_section_name (s);
4169 if (startswith (name, ".gnu.warning."))
4170 {
4171 char *msg;
4172 bfd_size_type sz;
4173
4174 name += sizeof ".gnu.warning." - 1;
4175
4176 /* If this is a shared object, then look up the symbol
4177 in the hash table. If it is there, and it is already
4178 been defined, then we will not be using the entry
4179 from this shared object, so we don't need to warn.
4180 FIXME: If we see the definition in a regular object
4181 later on, we will warn, but we shouldn't. The only
4182 fix is to keep track of what warnings we are supposed
4183 to emit, and then handle them all at the end of the
4184 link. */
4185 if (dynamic)
4186 {
4187 struct elf_link_hash_entry *h;
4188
4189 h = elf_link_hash_lookup (htab, name, false, false, true);
4190
4191 /* FIXME: What about bfd_link_hash_common? */
4192 if (h != NULL
4193 && (h->root.type == bfd_link_hash_defined
4194 || h->root.type == bfd_link_hash_defweak))
4195 continue;
4196 }
4197
4198 sz = s->size;
4199 msg = (char *) bfd_alloc (abfd, sz + 1);
4200 if (msg == NULL)
4201 goto error_return;
4202
4203 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
4204 goto error_return;
4205
4206 msg[sz] = '\0';
4207
4208 if (! (_bfd_generic_link_add_one_symbol
4209 (info, abfd, name, BSF_WARNING, s, 0, msg,
4210 false, bed->collect, NULL)))
4211 goto error_return;
4212
4213 if (bfd_link_executable (info))
4214 {
4215 /* Clobber the section size so that the warning does
4216 not get copied into the output file. */
4217 s->size = 0;
4218
4219 /* Also set SEC_EXCLUDE, so that symbols defined in
4220 the warning section don't get copied to the output. */
4221 s->flags |= SEC_EXCLUDE;
4222 }
4223 }
4224 }
4225
4226 just_syms = ((s = abfd->sections) != NULL
4227 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
4228
4229 add_needed = true;
4230 if (! dynamic)
4231 {
4232 /* If we are creating a shared library, create all the dynamic
4233 sections immediately. We need to attach them to something,
4234 so we attach them to this BFD, provided it is the right
4235 format and is not from ld --just-symbols. Always create the
4236 dynamic sections for -E/--dynamic-list. FIXME: If there
4237 are no input BFD's of the same format as the output, we can't
4238 make a shared library. */
4239 if (!just_syms
4240 && (bfd_link_pic (info)
4241 || (!bfd_link_relocatable (info)
4242 && info->nointerp
4243 && (info->export_dynamic || info->dynamic)))
4244 && is_elf_hash_table (&htab->root)
4245 && info->output_bfd->xvec == abfd->xvec
4246 && !htab->dynamic_sections_created)
4247 {
4248 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4249 goto error_return;
4250 }
4251 }
4252 else if (!is_elf_hash_table (&htab->root))
4253 goto error_return;
4254 else
4255 {
4256 const char *soname = NULL;
4257 char *audit = NULL;
4258 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
4259 const Elf_Internal_Phdr *phdr;
4260 struct elf_link_loaded_list *loaded_lib;
4261
4262 /* ld --just-symbols and dynamic objects don't mix very well.
4263 ld shouldn't allow it. */
4264 if (just_syms)
4265 abort ();
4266
4267 /* If this dynamic lib was specified on the command line with
4268 --as-needed in effect, then we don't want to add a DT_NEEDED
4269 tag unless the lib is actually used. Similary for libs brought
4270 in by another lib's DT_NEEDED. When --no-add-needed is used
4271 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4272 any dynamic library in DT_NEEDED tags in the dynamic lib at
4273 all. */
4274 add_needed = (elf_dyn_lib_class (abfd)
4275 & (DYN_AS_NEEDED | DYN_DT_NEEDED
4276 | DYN_NO_NEEDED)) == 0;
4277
4278 s = bfd_get_section_by_name (abfd, ".dynamic");
4279 if (s != NULL && s->size != 0)
4280 {
4281 bfd_byte *dynbuf;
4282 bfd_byte *extdyn;
4283 unsigned int elfsec;
4284 unsigned long shlink;
4285
4286 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
4287 {
4288 error_free_dyn:
4289 free (dynbuf);
4290 goto error_return;
4291 }
4292
4293 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
4294 if (elfsec == SHN_BAD)
4295 goto error_free_dyn;
4296 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4297
4298 for (extdyn = dynbuf;
4299 extdyn <= dynbuf + s->size - bed->s->sizeof_dyn;
4300 extdyn += bed->s->sizeof_dyn)
4301 {
4302 Elf_Internal_Dyn dyn;
4303
4304 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4305 if (dyn.d_tag == DT_SONAME)
4306 {
4307 unsigned int tagv = dyn.d_un.d_val;
4308 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4309 if (soname == NULL)
4310 goto error_free_dyn;
4311 }
4312 if (dyn.d_tag == DT_NEEDED)
4313 {
4314 struct bfd_link_needed_list *n, **pn;
4315 char *fnm, *anm;
4316 unsigned int tagv = dyn.d_un.d_val;
4317 size_t amt = sizeof (struct bfd_link_needed_list);
4318
4319 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4320 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4321 if (n == NULL || fnm == NULL)
4322 goto error_free_dyn;
4323 amt = strlen (fnm) + 1;
4324 anm = (char *) bfd_alloc (abfd, amt);
4325 if (anm == NULL)
4326 goto error_free_dyn;
4327 memcpy (anm, fnm, amt);
4328 n->name = anm;
4329 n->by = abfd;
4330 n->next = NULL;
4331 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4332 ;
4333 *pn = n;
4334 }
4335 if (dyn.d_tag == DT_RUNPATH)
4336 {
4337 struct bfd_link_needed_list *n, **pn;
4338 char *fnm, *anm;
4339 unsigned int tagv = dyn.d_un.d_val;
4340 size_t amt = sizeof (struct bfd_link_needed_list);
4341
4342 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4343 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4344 if (n == NULL || fnm == NULL)
4345 goto error_free_dyn;
4346 amt = strlen (fnm) + 1;
4347 anm = (char *) bfd_alloc (abfd, amt);
4348 if (anm == NULL)
4349 goto error_free_dyn;
4350 memcpy (anm, fnm, amt);
4351 n->name = anm;
4352 n->by = abfd;
4353 n->next = NULL;
4354 for (pn = & runpath;
4355 *pn != NULL;
4356 pn = &(*pn)->next)
4357 ;
4358 *pn = n;
4359 }
4360 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4361 if (!runpath && dyn.d_tag == DT_RPATH)
4362 {
4363 struct bfd_link_needed_list *n, **pn;
4364 char *fnm, *anm;
4365 unsigned int tagv = dyn.d_un.d_val;
4366 size_t amt = sizeof (struct bfd_link_needed_list);
4367
4368 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4369 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4370 if (n == NULL || fnm == NULL)
4371 goto error_free_dyn;
4372 amt = strlen (fnm) + 1;
4373 anm = (char *) bfd_alloc (abfd, amt);
4374 if (anm == NULL)
4375 goto error_free_dyn;
4376 memcpy (anm, fnm, amt);
4377 n->name = anm;
4378 n->by = abfd;
4379 n->next = NULL;
4380 for (pn = & rpath;
4381 *pn != NULL;
4382 pn = &(*pn)->next)
4383 ;
4384 *pn = n;
4385 }
4386 if (dyn.d_tag == DT_AUDIT)
4387 {
4388 unsigned int tagv = dyn.d_un.d_val;
4389 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4390 }
4391 if (dyn.d_tag == DT_FLAGS_1)
4392 elf_tdata (abfd)->is_pie = (dyn.d_un.d_val & DF_1_PIE) != 0;
4393 }
4394
4395 free (dynbuf);
4396 }
4397
4398 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4399 frees all more recently bfd_alloc'd blocks as well. */
4400 if (runpath)
4401 rpath = runpath;
4402
4403 if (rpath)
4404 {
4405 struct bfd_link_needed_list **pn;
4406 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4407 ;
4408 *pn = rpath;
4409 }
4410
4411 /* If we have a PT_GNU_RELRO program header, mark as read-only
4412 all sections contained fully therein. This makes relro
4413 shared library sections appear as they will at run-time. */
4414 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4415 while (phdr-- > elf_tdata (abfd)->phdr)
4416 if (phdr->p_type == PT_GNU_RELRO)
4417 {
4418 for (s = abfd->sections; s != NULL; s = s->next)
4419 {
4420 unsigned int opb = bfd_octets_per_byte (abfd, s);
4421
4422 if ((s->flags & SEC_ALLOC) != 0
4423 && s->vma * opb >= phdr->p_vaddr
4424 && s->vma * opb + s->size <= phdr->p_vaddr + phdr->p_memsz)
4425 s->flags |= SEC_READONLY;
4426 }
4427 break;
4428 }
4429
4430 /* We do not want to include any of the sections in a dynamic
4431 object in the output file. We hack by simply clobbering the
4432 list of sections in the BFD. This could be handled more
4433 cleanly by, say, a new section flag; the existing
4434 SEC_NEVER_LOAD flag is not the one we want, because that one
4435 still implies that the section takes up space in the output
4436 file. */
4437 bfd_section_list_clear (abfd);
4438
4439 /* Find the name to use in a DT_NEEDED entry that refers to this
4440 object. If the object has a DT_SONAME entry, we use it.
4441 Otherwise, if the generic linker stuck something in
4442 elf_dt_name, we use that. Otherwise, we just use the file
4443 name. */
4444 if (soname == NULL || *soname == '\0')
4445 {
4446 soname = elf_dt_name (abfd);
4447 if (soname == NULL || *soname == '\0')
4448 soname = bfd_get_filename (abfd);
4449 }
4450
4451 /* Save the SONAME because sometimes the linker emulation code
4452 will need to know it. */
4453 elf_dt_name (abfd) = soname;
4454
4455 /* If we have already included this dynamic object in the
4456 link, just ignore it. There is no reason to include a
4457 particular dynamic object more than once. */
4458 for (loaded_lib = htab->dyn_loaded;
4459 loaded_lib != NULL;
4460 loaded_lib = loaded_lib->next)
4461 {
4462 if (strcmp (elf_dt_name (loaded_lib->abfd), soname) == 0)
4463 return true;
4464 }
4465
4466 /* Create dynamic sections for backends that require that be done
4467 before setup_gnu_properties. */
4468 if (add_needed
4469 && !_bfd_elf_link_create_dynamic_sections (abfd, info))
4470 return false;
4471
4472 /* Save the DT_AUDIT entry for the linker emulation code. */
4473 elf_dt_audit (abfd) = audit;
4474 }
4475
4476 /* If this is a dynamic object, we always link against the .dynsym
4477 symbol table, not the .symtab symbol table. The dynamic linker
4478 will only see the .dynsym symbol table, so there is no reason to
4479 look at .symtab for a dynamic object. */
4480
4481 if (! dynamic || elf_dynsymtab (abfd) == 0)
4482 hdr = &elf_tdata (abfd)->symtab_hdr;
4483 else
4484 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4485
4486 symcount = hdr->sh_size / bed->s->sizeof_sym;
4487
4488 /* The sh_info field of the symtab header tells us where the
4489 external symbols start. We don't care about the local symbols at
4490 this point. */
4491 if (elf_bad_symtab (abfd))
4492 {
4493 extsymcount = symcount;
4494 extsymoff = 0;
4495 }
4496 else
4497 {
4498 extsymcount = symcount - hdr->sh_info;
4499 extsymoff = hdr->sh_info;
4500 }
4501
4502 sym_hash = elf_sym_hashes (abfd);
4503 if (extsymcount != 0)
4504 {
4505 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4506 NULL, NULL, NULL);
4507 if (isymbuf == NULL)
4508 goto error_return;
4509
4510 if (sym_hash == NULL)
4511 {
4512 /* We store a pointer to the hash table entry for each
4513 external symbol. */
4514 size_t amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4515 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4516 if (sym_hash == NULL)
4517 goto error_free_sym;
4518 elf_sym_hashes (abfd) = sym_hash;
4519 }
4520 }
4521
4522 if (dynamic)
4523 {
4524 /* Read in any version definitions. */
4525 if (!_bfd_elf_slurp_version_tables (abfd,
4526 info->default_imported_symver))
4527 goto error_free_sym;
4528
4529 /* Read in the symbol versions, but don't bother to convert them
4530 to internal format. */
4531 if (elf_dynversym (abfd) != 0)
4532 {
4533 Elf_Internal_Shdr *versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4534 bfd_size_type amt = versymhdr->sh_size;
4535
4536 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0)
4537 goto error_free_sym;
4538 extversym = (Elf_External_Versym *)
4539 _bfd_malloc_and_read (abfd, amt, amt);
4540 if (extversym == NULL)
4541 goto error_free_sym;
4542 extversym_end = extversym + amt / sizeof (*extversym);
4543 }
4544 }
4545
4546 /* If we are loading an as-needed shared lib, save the symbol table
4547 state before we start adding symbols. If the lib turns out
4548 to be unneeded, restore the state. */
4549 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4550 {
4551 unsigned int i;
4552 size_t entsize;
4553
4554 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4555 {
4556 struct bfd_hash_entry *p;
4557 struct elf_link_hash_entry *h;
4558
4559 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4560 {
4561 h = (struct elf_link_hash_entry *) p;
4562 entsize += htab->root.table.entsize;
4563 if (h->root.type == bfd_link_hash_warning)
4564 {
4565 entsize += htab->root.table.entsize;
4566 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4567 }
4568 if (h->root.type == bfd_link_hash_common)
4569 entsize += sizeof (*h->root.u.c.p);
4570 }
4571 }
4572
4573 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4574 old_tab = bfd_malloc (tabsize + entsize);
4575 if (old_tab == NULL)
4576 goto error_free_vers;
4577
4578 /* Remember the current objalloc pointer, so that all mem for
4579 symbols added can later be reclaimed. */
4580 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4581 if (alloc_mark == NULL)
4582 goto error_free_vers;
4583
4584 /* Make a special call to the linker "notice" function to
4585 tell it that we are about to handle an as-needed lib. */
4586 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4587 goto error_free_vers;
4588
4589 /* Clone the symbol table. Remember some pointers into the
4590 symbol table, and dynamic symbol count. */
4591 old_ent = (char *) old_tab + tabsize;
4592 memcpy (old_tab, htab->root.table.table, tabsize);
4593 old_undefs = htab->root.undefs;
4594 old_undefs_tail = htab->root.undefs_tail;
4595 old_table = htab->root.table.table;
4596 old_size = htab->root.table.size;
4597 old_count = htab->root.table.count;
4598 old_strtab = NULL;
4599 if (htab->dynstr != NULL)
4600 {
4601 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4602 if (old_strtab == NULL)
4603 goto error_free_vers;
4604 }
4605
4606 for (i = 0; i < htab->root.table.size; i++)
4607 {
4608 struct bfd_hash_entry *p;
4609 struct elf_link_hash_entry *h;
4610
4611 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4612 {
4613 h = (struct elf_link_hash_entry *) p;
4614 memcpy (old_ent, h, htab->root.table.entsize);
4615 old_ent = (char *) old_ent + htab->root.table.entsize;
4616 if (h->root.type == bfd_link_hash_warning)
4617 {
4618 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4619 memcpy (old_ent, h, htab->root.table.entsize);
4620 old_ent = (char *) old_ent + htab->root.table.entsize;
4621 }
4622 if (h->root.type == bfd_link_hash_common)
4623 {
4624 memcpy (old_ent, h->root.u.c.p, sizeof (*h->root.u.c.p));
4625 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
4626 }
4627 }
4628 }
4629 }
4630
4631 weaks = NULL;
4632 if (extversym == NULL)
4633 ever = NULL;
4634 else if (extversym + extsymoff < extversym_end)
4635 ever = extversym + extsymoff;
4636 else
4637 {
4638 /* xgettext:c-format */
4639 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4640 abfd, (long) extsymoff,
4641 (long) (extversym_end - extversym) / sizeof (* extversym));
4642 bfd_set_error (bfd_error_bad_value);
4643 goto error_free_vers;
4644 }
4645
4646 if (!bfd_link_relocatable (info)
4647 && abfd->lto_slim_object)
4648 {
4649 _bfd_error_handler
4650 (_("%pB: plugin needed to handle lto object"), abfd);
4651 }
4652
4653 for (isym = isymbuf, isymend = PTR_ADD (isymbuf, extsymcount);
4654 isym < isymend;
4655 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4656 {
4657 int bind;
4658 bfd_vma value;
4659 asection *sec, *new_sec;
4660 flagword flags;
4661 const char *name;
4662 struct elf_link_hash_entry *h;
4663 struct elf_link_hash_entry *hi;
4664 bool definition;
4665 bool size_change_ok;
4666 bool type_change_ok;
4667 bool new_weak;
4668 bool old_weak;
4669 bfd *override;
4670 bool common;
4671 bool discarded;
4672 unsigned int old_alignment;
4673 unsigned int shindex;
4674 bfd *old_bfd;
4675 bool matched;
4676
4677 override = NULL;
4678
4679 flags = BSF_NO_FLAGS;
4680 sec = NULL;
4681 value = isym->st_value;
4682 common = bed->common_definition (isym);
4683 if (common && info->inhibit_common_definition)
4684 {
4685 /* Treat common symbol as undefined for --no-define-common. */
4686 isym->st_shndx = SHN_UNDEF;
4687 common = false;
4688 }
4689 discarded = false;
4690
4691 bind = ELF_ST_BIND (isym->st_info);
4692 switch (bind)
4693 {
4694 case STB_LOCAL:
4695 /* This should be impossible, since ELF requires that all
4696 global symbols follow all local symbols, and that sh_info
4697 point to the first global symbol. Unfortunately, Irix 5
4698 screws this up. */
4699 if (elf_bad_symtab (abfd))
4700 continue;
4701
4702 /* If we aren't prepared to handle locals within the globals
4703 then we'll likely segfault on a NULL symbol hash if the
4704 symbol is ever referenced in relocations. */
4705 shindex = elf_elfheader (abfd)->e_shstrndx;
4706 name = bfd_elf_string_from_elf_section (abfd, shindex, hdr->sh_name);
4707 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4708 " (>= sh_info of %lu)"),
4709 abfd, name, (long) (isym - isymbuf + extsymoff),
4710 (long) extsymoff);
4711
4712 /* Dynamic object relocations are not processed by ld, so
4713 ld won't run into the problem mentioned above. */
4714 if (dynamic)
4715 continue;
4716 bfd_set_error (bfd_error_bad_value);
4717 goto error_free_vers;
4718
4719 case STB_GLOBAL:
4720 if (isym->st_shndx != SHN_UNDEF && !common)
4721 flags = BSF_GLOBAL;
4722 break;
4723
4724 case STB_WEAK:
4725 flags = BSF_WEAK;
4726 break;
4727
4728 case STB_GNU_UNIQUE:
4729 flags = BSF_GNU_UNIQUE;
4730 break;
4731
4732 default:
4733 /* Leave it up to the processor backend. */
4734 break;
4735 }
4736
4737 if (isym->st_shndx == SHN_UNDEF)
4738 sec = bfd_und_section_ptr;
4739 else if (isym->st_shndx == SHN_ABS)
4740 sec = bfd_abs_section_ptr;
4741 else if (isym->st_shndx == SHN_COMMON)
4742 {
4743 sec = bfd_com_section_ptr;
4744 /* What ELF calls the size we call the value. What ELF
4745 calls the value we call the alignment. */
4746 value = isym->st_size;
4747 }
4748 else
4749 {
4750 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4751 if (sec == NULL)
4752 sec = bfd_abs_section_ptr;
4753 else if (discarded_section (sec))
4754 {
4755 /* Symbols from discarded section are undefined. We keep
4756 its visibility. */
4757 sec = bfd_und_section_ptr;
4758 discarded = true;
4759 isym->st_shndx = SHN_UNDEF;
4760 }
4761 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4762 value -= sec->vma;
4763 }
4764
4765 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4766 isym->st_name);
4767 if (name == NULL)
4768 goto error_free_vers;
4769
4770 if (isym->st_shndx == SHN_COMMON
4771 && (abfd->flags & BFD_PLUGIN) != 0)
4772 {
4773 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4774
4775 if (xc == NULL)
4776 {
4777 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4778 | SEC_EXCLUDE);
4779 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4780 if (xc == NULL)
4781 goto error_free_vers;
4782 }
4783 sec = xc;
4784 }
4785 else if (isym->st_shndx == SHN_COMMON
4786 && ELF_ST_TYPE (isym->st_info) == STT_TLS
4787 && !bfd_link_relocatable (info))
4788 {
4789 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4790
4791 if (tcomm == NULL)
4792 {
4793 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4794 | SEC_LINKER_CREATED);
4795 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4796 if (tcomm == NULL)
4797 goto error_free_vers;
4798 }
4799 sec = tcomm;
4800 }
4801 else if (bed->elf_add_symbol_hook)
4802 {
4803 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4804 &sec, &value))
4805 goto error_free_vers;
4806
4807 /* The hook function sets the name to NULL if this symbol
4808 should be skipped for some reason. */
4809 if (name == NULL)
4810 continue;
4811 }
4812
4813 /* Sanity check that all possibilities were handled. */
4814 if (sec == NULL)
4815 abort ();
4816
4817 /* Silently discard TLS symbols from --just-syms. There's
4818 no way to combine a static TLS block with a new TLS block
4819 for this executable. */
4820 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4821 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4822 continue;
4823
4824 if (bfd_is_und_section (sec)
4825 || bfd_is_com_section (sec))
4826 definition = false;
4827 else
4828 definition = true;
4829
4830 size_change_ok = false;
4831 type_change_ok = bed->type_change_ok;
4832 old_weak = false;
4833 matched = false;
4834 old_alignment = 0;
4835 old_bfd = NULL;
4836 new_sec = sec;
4837
4838 if (is_elf_hash_table (&htab->root))
4839 {
4840 Elf_Internal_Versym iver;
4841 unsigned int vernum = 0;
4842 bool skip;
4843
4844 if (ever == NULL)
4845 {
4846 if (info->default_imported_symver)
4847 /* Use the default symbol version created earlier. */
4848 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4849 else
4850 iver.vs_vers = 0;
4851 }
4852 else if (ever >= extversym_end)
4853 {
4854 /* xgettext:c-format */
4855 _bfd_error_handler (_("%pB: not enough version information"),
4856 abfd);
4857 bfd_set_error (bfd_error_bad_value);
4858 goto error_free_vers;
4859 }
4860 else
4861 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4862
4863 vernum = iver.vs_vers & VERSYM_VERSION;
4864
4865 /* If this is a hidden symbol, or if it is not version
4866 1, we append the version name to the symbol name.
4867 However, we do not modify a non-hidden absolute symbol
4868 if it is not a function, because it might be the version
4869 symbol itself. FIXME: What if it isn't? */
4870 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4871 || (vernum > 1
4872 && (!bfd_is_abs_section (sec)
4873 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4874 {
4875 const char *verstr;
4876 size_t namelen, verlen, newlen;
4877 char *newname, *p;
4878
4879 if (isym->st_shndx != SHN_UNDEF)
4880 {
4881 if (vernum > elf_tdata (abfd)->cverdefs)
4882 verstr = NULL;
4883 else if (vernum > 1)
4884 verstr =
4885 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4886 else
4887 verstr = "";
4888
4889 if (verstr == NULL)
4890 {
4891 _bfd_error_handler
4892 /* xgettext:c-format */
4893 (_("%pB: %s: invalid version %u (max %d)"),
4894 abfd, name, vernum,
4895 elf_tdata (abfd)->cverdefs);
4896 bfd_set_error (bfd_error_bad_value);
4897 goto error_free_vers;
4898 }
4899 }
4900 else
4901 {
4902 /* We cannot simply test for the number of
4903 entries in the VERNEED section since the
4904 numbers for the needed versions do not start
4905 at 0. */
4906 Elf_Internal_Verneed *t;
4907
4908 verstr = NULL;
4909 for (t = elf_tdata (abfd)->verref;
4910 t != NULL;
4911 t = t->vn_nextref)
4912 {
4913 Elf_Internal_Vernaux *a;
4914
4915 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4916 {
4917 if (a->vna_other == vernum)
4918 {
4919 verstr = a->vna_nodename;
4920 break;
4921 }
4922 }
4923 if (a != NULL)
4924 break;
4925 }
4926 if (verstr == NULL)
4927 {
4928 _bfd_error_handler
4929 /* xgettext:c-format */
4930 (_("%pB: %s: invalid needed version %d"),
4931 abfd, name, vernum);
4932 bfd_set_error (bfd_error_bad_value);
4933 goto error_free_vers;
4934 }
4935 }
4936
4937 namelen = strlen (name);
4938 verlen = strlen (verstr);
4939 newlen = namelen + verlen + 2;
4940 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4941 && isym->st_shndx != SHN_UNDEF)
4942 ++newlen;
4943
4944 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4945 if (newname == NULL)
4946 goto error_free_vers;
4947 memcpy (newname, name, namelen);
4948 p = newname + namelen;
4949 *p++ = ELF_VER_CHR;
4950 /* If this is a defined non-hidden version symbol,
4951 we add another @ to the name. This indicates the
4952 default version of the symbol. */
4953 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4954 && isym->st_shndx != SHN_UNDEF)
4955 *p++ = ELF_VER_CHR;
4956 memcpy (p, verstr, verlen + 1);
4957
4958 name = newname;
4959 }
4960
4961 /* If this symbol has default visibility and the user has
4962 requested we not re-export it, then mark it as hidden. */
4963 if (!bfd_is_und_section (sec)
4964 && !dynamic
4965 && abfd->no_export
4966 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4967 isym->st_other = (STV_HIDDEN
4968 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4969
4970 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4971 sym_hash, &old_bfd, &old_weak,
4972 &old_alignment, &skip, &override,
4973 &type_change_ok, &size_change_ok,
4974 &matched))
4975 goto error_free_vers;
4976
4977 if (skip)
4978 continue;
4979
4980 /* Override a definition only if the new symbol matches the
4981 existing one. */
4982 if (override && matched)
4983 definition = false;
4984
4985 h = *sym_hash;
4986 while (h->root.type == bfd_link_hash_indirect
4987 || h->root.type == bfd_link_hash_warning)
4988 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4989
4990 if (h->versioned != unversioned
4991 && elf_tdata (abfd)->verdef != NULL
4992 && vernum > 1
4993 && definition)
4994 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4995 }
4996
4997 if (! (_bfd_generic_link_add_one_symbol
4998 (info, override ? override : abfd, name, flags, sec, value,
4999 NULL, false, bed->collect,
5000 (struct bfd_link_hash_entry **) sym_hash)))
5001 goto error_free_vers;
5002
5003 h = *sym_hash;
5004 /* We need to make sure that indirect symbol dynamic flags are
5005 updated. */
5006 hi = h;
5007 while (h->root.type == bfd_link_hash_indirect
5008 || h->root.type == bfd_link_hash_warning)
5009 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5010
5011 *sym_hash = h;
5012
5013 /* Setting the index to -3 tells elf_link_output_extsym that
5014 this symbol is defined in a discarded section. */
5015 if (discarded && is_elf_hash_table (&htab->root))
5016 h->indx = -3;
5017
5018 new_weak = (flags & BSF_WEAK) != 0;
5019 if (dynamic
5020 && definition
5021 && new_weak
5022 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
5023 && is_elf_hash_table (&htab->root)
5024 && h->u.alias == NULL)
5025 {
5026 /* Keep a list of all weak defined non function symbols from
5027 a dynamic object, using the alias field. Later in this
5028 function we will set the alias field to the correct
5029 value. We only put non-function symbols from dynamic
5030 objects on this list, because that happens to be the only
5031 time we need to know the normal symbol corresponding to a
5032 weak symbol, and the information is time consuming to
5033 figure out. If the alias field is not already NULL,
5034 then this symbol was already defined by some previous
5035 dynamic object, and we will be using that previous
5036 definition anyhow. */
5037
5038 h->u.alias = weaks;
5039 weaks = h;
5040 }
5041
5042 /* Set the alignment of a common symbol. */
5043 if ((common || bfd_is_com_section (sec))
5044 && h->root.type == bfd_link_hash_common)
5045 {
5046 unsigned int align;
5047
5048 if (common)
5049 align = bfd_log2 (isym->st_value);
5050 else
5051 {
5052 /* The new symbol is a common symbol in a shared object.
5053 We need to get the alignment from the section. */
5054 align = new_sec->alignment_power;
5055 }
5056 if (align > old_alignment)
5057 h->root.u.c.p->alignment_power = align;
5058 else
5059 h->root.u.c.p->alignment_power = old_alignment;
5060 }
5061
5062 if (is_elf_hash_table (&htab->root))
5063 {
5064 /* Set a flag in the hash table entry indicating the type of
5065 reference or definition we just found. A dynamic symbol
5066 is one which is referenced or defined by both a regular
5067 object and a shared object. */
5068 bool dynsym = false;
5069
5070 /* Plugin symbols aren't normal. Don't set def/ref flags. */
5071 if ((abfd->flags & BFD_PLUGIN) != 0)
5072 {
5073 /* Except for this flag to track nonweak references. */
5074 if (!definition
5075 && bind != STB_WEAK)
5076 h->ref_ir_nonweak = 1;
5077 }
5078 else if (!dynamic)
5079 {
5080 if (! definition)
5081 {
5082 h->ref_regular = 1;
5083 if (bind != STB_WEAK)
5084 h->ref_regular_nonweak = 1;
5085 }
5086 else
5087 {
5088 h->def_regular = 1;
5089 if (h->def_dynamic)
5090 {
5091 h->def_dynamic = 0;
5092 h->ref_dynamic = 1;
5093 }
5094 }
5095 }
5096 else
5097 {
5098 if (! definition)
5099 {
5100 h->ref_dynamic = 1;
5101 hi->ref_dynamic = 1;
5102 }
5103 else
5104 {
5105 h->def_dynamic = 1;
5106 hi->def_dynamic = 1;
5107 }
5108 }
5109
5110 /* If an indirect symbol has been forced local, don't
5111 make the real symbol dynamic. */
5112 if (h != hi && hi->forced_local)
5113 ;
5114 else if (!dynamic)
5115 {
5116 if (bfd_link_dll (info)
5117 || h->def_dynamic
5118 || h->ref_dynamic)
5119 dynsym = true;
5120 }
5121 else
5122 {
5123 if (h->def_regular
5124 || h->ref_regular
5125 || (h->is_weakalias
5126 && weakdef (h)->dynindx != -1))
5127 dynsym = true;
5128 }
5129
5130 /* Check to see if we need to add an indirect symbol for
5131 the default name. */
5132 if ((definition
5133 || (!override && h->root.type == bfd_link_hash_common))
5134 && !(hi != h
5135 && hi->versioned == versioned_hidden))
5136 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
5137 sec, value, &old_bfd, &dynsym))
5138 goto error_free_vers;
5139
5140 /* Check the alignment when a common symbol is involved. This
5141 can change when a common symbol is overridden by a normal
5142 definition or a common symbol is ignored due to the old
5143 normal definition. We need to make sure the maximum
5144 alignment is maintained. */
5145 if ((old_alignment || common)
5146 && h->root.type != bfd_link_hash_common)
5147 {
5148 unsigned int common_align;
5149 unsigned int normal_align;
5150 unsigned int symbol_align;
5151 bfd *normal_bfd;
5152 bfd *common_bfd;
5153
5154 BFD_ASSERT (h->root.type == bfd_link_hash_defined
5155 || h->root.type == bfd_link_hash_defweak);
5156
5157 symbol_align = ffs (h->root.u.def.value) - 1;
5158 if (h->root.u.def.section->owner != NULL
5159 && (h->root.u.def.section->owner->flags
5160 & (DYNAMIC | BFD_PLUGIN)) == 0)
5161 {
5162 normal_align = h->root.u.def.section->alignment_power;
5163 if (normal_align > symbol_align)
5164 normal_align = symbol_align;
5165 }
5166 else
5167 normal_align = symbol_align;
5168
5169 if (old_alignment)
5170 {
5171 common_align = old_alignment;
5172 common_bfd = old_bfd;
5173 normal_bfd = abfd;
5174 }
5175 else
5176 {
5177 common_align = bfd_log2 (isym->st_value);
5178 common_bfd = abfd;
5179 normal_bfd = old_bfd;
5180 }
5181
5182 if (normal_align < common_align)
5183 {
5184 /* PR binutils/2735 */
5185 if (normal_bfd == NULL)
5186 _bfd_error_handler
5187 /* xgettext:c-format */
5188 (_("warning: alignment %u of common symbol `%s' in %pB is"
5189 " greater than the alignment (%u) of its section %pA"),
5190 1 << common_align, name, common_bfd,
5191 1 << normal_align, h->root.u.def.section);
5192 else
5193 _bfd_error_handler
5194 /* xgettext:c-format */
5195 (_("warning: alignment %u of symbol `%s' in %pB"
5196 " is smaller than %u in %pB"),
5197 1 << normal_align, name, normal_bfd,
5198 1 << common_align, common_bfd);
5199 }
5200 }
5201
5202 /* Remember the symbol size if it isn't undefined. */
5203 if (isym->st_size != 0
5204 && isym->st_shndx != SHN_UNDEF
5205 && (definition || h->size == 0))
5206 {
5207 if (h->size != 0
5208 && h->size != isym->st_size
5209 && ! size_change_ok)
5210 _bfd_error_handler
5211 /* xgettext:c-format */
5212 (_("warning: size of symbol `%s' changed"
5213 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
5214 name, (uint64_t) h->size, old_bfd,
5215 (uint64_t) isym->st_size, abfd);
5216
5217 h->size = isym->st_size;
5218 }
5219
5220 /* If this is a common symbol, then we always want H->SIZE
5221 to be the size of the common symbol. The code just above
5222 won't fix the size if a common symbol becomes larger. We
5223 don't warn about a size change here, because that is
5224 covered by --warn-common. Allow changes between different
5225 function types. */
5226 if (h->root.type == bfd_link_hash_common)
5227 h->size = h->root.u.c.size;
5228
5229 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
5230 && ((definition && !new_weak)
5231 || (old_weak && h->root.type == bfd_link_hash_common)
5232 || h->type == STT_NOTYPE))
5233 {
5234 unsigned int type = ELF_ST_TYPE (isym->st_info);
5235
5236 /* Turn an IFUNC symbol from a DSO into a normal FUNC
5237 symbol. */
5238 if (type == STT_GNU_IFUNC
5239 && (abfd->flags & DYNAMIC) != 0)
5240 type = STT_FUNC;
5241
5242 if (h->type != type)
5243 {
5244 if (h->type != STT_NOTYPE && ! type_change_ok)
5245 /* xgettext:c-format */
5246 _bfd_error_handler
5247 (_("warning: type of symbol `%s' changed"
5248 " from %d to %d in %pB"),
5249 name, h->type, type, abfd);
5250
5251 h->type = type;
5252 }
5253 }
5254
5255 /* Merge st_other field. */
5256 elf_merge_st_other (abfd, h, isym->st_other, sec,
5257 definition, dynamic);
5258
5259 /* We don't want to make debug symbol dynamic. */
5260 if (definition
5261 && (sec->flags & SEC_DEBUGGING)
5262 && !bfd_link_relocatable (info))
5263 dynsym = false;
5264
5265 /* Nor should we make plugin symbols dynamic. */
5266 if ((abfd->flags & BFD_PLUGIN) != 0)
5267 dynsym = false;
5268
5269 if (definition)
5270 {
5271 h->target_internal = isym->st_target_internal;
5272 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5273 }
5274
5275 if (definition && !dynamic)
5276 {
5277 char *p = strchr (name, ELF_VER_CHR);
5278 if (p != NULL && p[1] != ELF_VER_CHR)
5279 {
5280 /* Queue non-default versions so that .symver x, x@FOO
5281 aliases can be checked. */
5282 if (!nondeflt_vers)
5283 {
5284 size_t amt = ((isymend - isym + 1)
5285 * sizeof (struct elf_link_hash_entry *));
5286 nondeflt_vers
5287 = (struct elf_link_hash_entry **) bfd_malloc (amt);
5288 if (!nondeflt_vers)
5289 goto error_free_vers;
5290 }
5291 nondeflt_vers[nondeflt_vers_cnt++] = h;
5292 }
5293 }
5294
5295 if (dynsym && h->dynindx == -1)
5296 {
5297 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5298 goto error_free_vers;
5299 if (h->is_weakalias
5300 && weakdef (h)->dynindx == -1)
5301 {
5302 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
5303 goto error_free_vers;
5304 }
5305 }
5306 else if (h->dynindx != -1)
5307 /* If the symbol already has a dynamic index, but
5308 visibility says it should not be visible, turn it into
5309 a local symbol. */
5310 switch (ELF_ST_VISIBILITY (h->other))
5311 {
5312 case STV_INTERNAL:
5313 case STV_HIDDEN:
5314 (*bed->elf_backend_hide_symbol) (info, h, true);
5315 dynsym = false;
5316 break;
5317 }
5318
5319 if (!add_needed
5320 && matched
5321 && definition
5322 && h->root.type != bfd_link_hash_indirect
5323 && ((dynsym
5324 && h->ref_regular_nonweak)
5325 || (old_bfd != NULL
5326 && (old_bfd->flags & BFD_PLUGIN) != 0
5327 && h->ref_ir_nonweak
5328 && !info->lto_all_symbols_read)
5329 || (h->ref_dynamic_nonweak
5330 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5331 && !on_needed_list (elf_dt_name (abfd),
5332 htab->needed, NULL))))
5333 {
5334 const char *soname = elf_dt_name (abfd);
5335
5336 info->callbacks->minfo ("%!", soname, old_bfd,
5337 h->root.root.string);
5338
5339 /* A symbol from a library loaded via DT_NEEDED of some
5340 other library is referenced by a regular object.
5341 Add a DT_NEEDED entry for it. Issue an error if
5342 --no-add-needed is used and the reference was not
5343 a weak one. */
5344 if (old_bfd != NULL
5345 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
5346 {
5347 _bfd_error_handler
5348 /* xgettext:c-format */
5349 (_("%pB: undefined reference to symbol '%s'"),
5350 old_bfd, name);
5351 bfd_set_error (bfd_error_missing_dso);
5352 goto error_free_vers;
5353 }
5354
5355 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
5356 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
5357
5358 /* Create dynamic sections for backends that require
5359 that be done before setup_gnu_properties. */
5360 if (!_bfd_elf_link_create_dynamic_sections (abfd, info))
5361 return false;
5362 add_needed = true;
5363 }
5364 }
5365 }
5366
5367 if (info->lto_plugin_active
5368 && !bfd_link_relocatable (info)
5369 && (abfd->flags & BFD_PLUGIN) == 0
5370 && !just_syms
5371 && extsymcount)
5372 {
5373 int r_sym_shift;
5374
5375 if (bed->s->arch_size == 32)
5376 r_sym_shift = 8;
5377 else
5378 r_sym_shift = 32;
5379
5380 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5381 referenced in regular objects so that linker plugin will get
5382 the correct symbol resolution. */
5383
5384 sym_hash = elf_sym_hashes (abfd);
5385 for (s = abfd->sections; s != NULL; s = s->next)
5386 {
5387 Elf_Internal_Rela *internal_relocs;
5388 Elf_Internal_Rela *rel, *relend;
5389
5390 /* Don't check relocations in excluded sections. */
5391 if ((s->flags & SEC_RELOC) == 0
5392 || s->reloc_count == 0
5393 || (s->flags & SEC_EXCLUDE) != 0
5394 || ((info->strip == strip_all
5395 || info->strip == strip_debugger)
5396 && (s->flags & SEC_DEBUGGING) != 0))
5397 continue;
5398
5399 internal_relocs = _bfd_elf_link_info_read_relocs (abfd, info,
5400 s, NULL,
5401 NULL,
5402 _bfd_link_keep_memory (info));
5403 if (internal_relocs == NULL)
5404 goto error_free_vers;
5405
5406 rel = internal_relocs;
5407 relend = rel + s->reloc_count;
5408 for ( ; rel < relend; rel++)
5409 {
5410 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5411 struct elf_link_hash_entry *h;
5412
5413 /* Skip local symbols. */
5414 if (r_symndx < extsymoff)
5415 continue;
5416
5417 h = sym_hash[r_symndx - extsymoff];
5418 if (h != NULL)
5419 h->root.non_ir_ref_regular = 1;
5420 }
5421
5422 if (elf_section_data (s)->relocs != internal_relocs)
5423 free (internal_relocs);
5424 }
5425 }
5426
5427 free (extversym);
5428 extversym = NULL;
5429 free (isymbuf);
5430 isymbuf = NULL;
5431
5432 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5433 {
5434 unsigned int i;
5435
5436 /* Restore the symbol table. */
5437 old_ent = (char *) old_tab + tabsize;
5438 memset (elf_sym_hashes (abfd), 0,
5439 extsymcount * sizeof (struct elf_link_hash_entry *));
5440 htab->root.table.table = old_table;
5441 htab->root.table.size = old_size;
5442 htab->root.table.count = old_count;
5443 memcpy (htab->root.table.table, old_tab, tabsize);
5444 htab->root.undefs = old_undefs;
5445 htab->root.undefs_tail = old_undefs_tail;
5446 if (htab->dynstr != NULL)
5447 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5448 free (old_strtab);
5449 old_strtab = NULL;
5450 for (i = 0; i < htab->root.table.size; i++)
5451 {
5452 struct bfd_hash_entry *p;
5453 struct elf_link_hash_entry *h;
5454 unsigned int non_ir_ref_dynamic;
5455
5456 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5457 {
5458 /* Preserve non_ir_ref_dynamic so that this symbol
5459 will be exported when the dynamic lib becomes needed
5460 in the second pass. */
5461 h = (struct elf_link_hash_entry *) p;
5462 if (h->root.type == bfd_link_hash_warning)
5463 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5464 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
5465
5466 h = (struct elf_link_hash_entry *) p;
5467 memcpy (h, old_ent, htab->root.table.entsize);
5468 old_ent = (char *) old_ent + htab->root.table.entsize;
5469 if (h->root.type == bfd_link_hash_warning)
5470 {
5471 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5472 memcpy (h, old_ent, htab->root.table.entsize);
5473 old_ent = (char *) old_ent + htab->root.table.entsize;
5474 }
5475 if (h->root.type == bfd_link_hash_common)
5476 {
5477 memcpy (h->root.u.c.p, old_ent, sizeof (*h->root.u.c.p));
5478 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
5479 }
5480 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
5481 }
5482 }
5483
5484 /* Make a special call to the linker "notice" function to
5485 tell it that symbols added for crefs may need to be removed. */
5486 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
5487 goto error_free_vers;
5488
5489 free (old_tab);
5490 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5491 alloc_mark);
5492 free (nondeflt_vers);
5493 return true;
5494 }
5495
5496 if (old_tab != NULL)
5497 {
5498 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
5499 goto error_free_vers;
5500 free (old_tab);
5501 old_tab = NULL;
5502 }
5503
5504 /* Now that all the symbols from this input file are created, if
5505 not performing a relocatable link, handle .symver foo, foo@BAR
5506 such that any relocs against foo become foo@BAR. */
5507 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
5508 {
5509 size_t cnt, symidx;
5510
5511 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5512 {
5513 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5514 char *shortname, *p;
5515 size_t amt;
5516
5517 p = strchr (h->root.root.string, ELF_VER_CHR);
5518 if (p == NULL
5519 || (h->root.type != bfd_link_hash_defined
5520 && h->root.type != bfd_link_hash_defweak))
5521 continue;
5522
5523 amt = p - h->root.root.string;
5524 shortname = (char *) bfd_malloc (amt + 1);
5525 if (!shortname)
5526 goto error_free_vers;
5527 memcpy (shortname, h->root.root.string, amt);
5528 shortname[amt] = '\0';
5529
5530 hi = (struct elf_link_hash_entry *)
5531 bfd_link_hash_lookup (&htab->root, shortname,
5532 false, false, false);
5533 if (hi != NULL
5534 && hi->root.type == h->root.type
5535 && hi->root.u.def.value == h->root.u.def.value
5536 && hi->root.u.def.section == h->root.u.def.section)
5537 {
5538 (*bed->elf_backend_hide_symbol) (info, hi, true);
5539 hi->root.type = bfd_link_hash_indirect;
5540 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
5541 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
5542 sym_hash = elf_sym_hashes (abfd);
5543 if (sym_hash)
5544 for (symidx = 0; symidx < extsymcount; ++symidx)
5545 if (sym_hash[symidx] == hi)
5546 {
5547 sym_hash[symidx] = h;
5548 break;
5549 }
5550 }
5551 free (shortname);
5552 }
5553 free (nondeflt_vers);
5554 nondeflt_vers = NULL;
5555 }
5556
5557 /* Now set the alias field correctly for all the weak defined
5558 symbols we found. The only way to do this is to search all the
5559 symbols. Since we only need the information for non functions in
5560 dynamic objects, that's the only time we actually put anything on
5561 the list WEAKS. We need this information so that if a regular
5562 object refers to a symbol defined weakly in a dynamic object, the
5563 real symbol in the dynamic object is also put in the dynamic
5564 symbols; we also must arrange for both symbols to point to the
5565 same memory location. We could handle the general case of symbol
5566 aliasing, but a general symbol alias can only be generated in
5567 assembler code, handling it correctly would be very time
5568 consuming, and other ELF linkers don't handle general aliasing
5569 either. */
5570 if (weaks != NULL)
5571 {
5572 struct elf_link_hash_entry **hpp;
5573 struct elf_link_hash_entry **hppend;
5574 struct elf_link_hash_entry **sorted_sym_hash;
5575 struct elf_link_hash_entry *h;
5576 size_t sym_count, amt;
5577
5578 /* Since we have to search the whole symbol list for each weak
5579 defined symbol, search time for N weak defined symbols will be
5580 O(N^2). Binary search will cut it down to O(NlogN). */
5581 amt = extsymcount * sizeof (*sorted_sym_hash);
5582 sorted_sym_hash = bfd_malloc (amt);
5583 if (sorted_sym_hash == NULL)
5584 goto error_return;
5585 sym_hash = sorted_sym_hash;
5586 hpp = elf_sym_hashes (abfd);
5587 hppend = hpp + extsymcount;
5588 sym_count = 0;
5589 for (; hpp < hppend; hpp++)
5590 {
5591 h = *hpp;
5592 if (h != NULL
5593 && h->root.type == bfd_link_hash_defined
5594 && !bed->is_function_type (h->type))
5595 {
5596 *sym_hash = h;
5597 sym_hash++;
5598 sym_count++;
5599 }
5600 }
5601
5602 qsort (sorted_sym_hash, sym_count, sizeof (*sorted_sym_hash),
5603 elf_sort_symbol);
5604
5605 while (weaks != NULL)
5606 {
5607 struct elf_link_hash_entry *hlook;
5608 asection *slook;
5609 bfd_vma vlook;
5610 size_t i, j, idx = 0;
5611
5612 hlook = weaks;
5613 weaks = hlook->u.alias;
5614 hlook->u.alias = NULL;
5615
5616 if (hlook->root.type != bfd_link_hash_defined
5617 && hlook->root.type != bfd_link_hash_defweak)
5618 continue;
5619
5620 slook = hlook->root.u.def.section;
5621 vlook = hlook->root.u.def.value;
5622
5623 i = 0;
5624 j = sym_count;
5625 while (i != j)
5626 {
5627 bfd_signed_vma vdiff;
5628 idx = (i + j) / 2;
5629 h = sorted_sym_hash[idx];
5630 vdiff = vlook - h->root.u.def.value;
5631 if (vdiff < 0)
5632 j = idx;
5633 else if (vdiff > 0)
5634 i = idx + 1;
5635 else
5636 {
5637 int sdiff = slook->id - h->root.u.def.section->id;
5638 if (sdiff < 0)
5639 j = idx;
5640 else if (sdiff > 0)
5641 i = idx + 1;
5642 else
5643 break;
5644 }
5645 }
5646
5647 /* We didn't find a value/section match. */
5648 if (i == j)
5649 continue;
5650
5651 /* With multiple aliases, or when the weak symbol is already
5652 strongly defined, we have multiple matching symbols and
5653 the binary search above may land on any of them. Step
5654 one past the matching symbol(s). */
5655 while (++idx != j)
5656 {
5657 h = sorted_sym_hash[idx];
5658 if (h->root.u.def.section != slook
5659 || h->root.u.def.value != vlook)
5660 break;
5661 }
5662
5663 /* Now look back over the aliases. Since we sorted by size
5664 as well as value and section, we'll choose the one with
5665 the largest size. */
5666 while (idx-- != i)
5667 {
5668 h = sorted_sym_hash[idx];
5669
5670 /* Stop if value or section doesn't match. */
5671 if (h->root.u.def.section != slook
5672 || h->root.u.def.value != vlook)
5673 break;
5674 else if (h != hlook)
5675 {
5676 struct elf_link_hash_entry *t;
5677
5678 hlook->u.alias = h;
5679 hlook->is_weakalias = 1;
5680 t = h;
5681 if (t->u.alias != NULL)
5682 while (t->u.alias != h)
5683 t = t->u.alias;
5684 t->u.alias = hlook;
5685
5686 /* If the weak definition is in the list of dynamic
5687 symbols, make sure the real definition is put
5688 there as well. */
5689 if (hlook->dynindx != -1 && h->dynindx == -1)
5690 {
5691 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5692 {
5693 err_free_sym_hash:
5694 free (sorted_sym_hash);
5695 goto error_return;
5696 }
5697 }
5698
5699 /* If the real definition is in the list of dynamic
5700 symbols, make sure the weak definition is put
5701 there as well. If we don't do this, then the
5702 dynamic loader might not merge the entries for the
5703 real definition and the weak definition. */
5704 if (h->dynindx != -1 && hlook->dynindx == -1)
5705 {
5706 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
5707 goto err_free_sym_hash;
5708 }
5709 break;
5710 }
5711 }
5712 }
5713
5714 free (sorted_sym_hash);
5715 }
5716
5717 if (bed->check_directives
5718 && !(*bed->check_directives) (abfd, info))
5719 return false;
5720
5721 /* If this is a non-traditional link, try to optimize the handling
5722 of the .stab/.stabstr sections. */
5723 if (! dynamic
5724 && ! info->traditional_format
5725 && is_elf_hash_table (&htab->root)
5726 && (info->strip != strip_all && info->strip != strip_debugger))
5727 {
5728 asection *stabstr;
5729
5730 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5731 if (stabstr != NULL)
5732 {
5733 bfd_size_type string_offset = 0;
5734 asection *stab;
5735
5736 for (stab = abfd->sections; stab; stab = stab->next)
5737 if (startswith (stab->name, ".stab")
5738 && (!stab->name[5] ||
5739 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5740 && (stab->flags & SEC_MERGE) == 0
5741 && !bfd_is_abs_section (stab->output_section))
5742 {
5743 struct bfd_elf_section_data *secdata;
5744
5745 secdata = elf_section_data (stab);
5746 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5747 stabstr, &secdata->sec_info,
5748 &string_offset))
5749 goto error_return;
5750 if (secdata->sec_info)
5751 stab->sec_info_type = SEC_INFO_TYPE_STABS;
5752 }
5753 }
5754 }
5755
5756 if (dynamic && add_needed)
5757 {
5758 /* Add this bfd to the loaded list. */
5759 struct elf_link_loaded_list *n;
5760
5761 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5762 if (n == NULL)
5763 goto error_return;
5764 n->abfd = abfd;
5765 n->next = htab->dyn_loaded;
5766 htab->dyn_loaded = n;
5767 }
5768 if (dynamic && !add_needed
5769 && (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) != 0)
5770 elf_dyn_lib_class (abfd) |= DYN_NO_NEEDED;
5771
5772 return true;
5773
5774 error_free_vers:
5775 free (old_tab);
5776 free (old_strtab);
5777 free (nondeflt_vers);
5778 free (extversym);
5779 error_free_sym:
5780 free (isymbuf);
5781 error_return:
5782 return false;
5783 }
5784
5785 /* Return the linker hash table entry of a symbol that might be
5786 satisfied by an archive symbol. Return -1 on error. */
5787
5788 struct bfd_link_hash_entry *
5789 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5790 struct bfd_link_info *info,
5791 const char *name)
5792 {
5793 struct bfd_link_hash_entry *h;
5794 char *p, *copy;
5795 size_t len, first;
5796
5797 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
5798 if (h != NULL)
5799 return h;
5800
5801 /* If this is a default version (the name contains @@), look up the
5802 symbol again with only one `@' as well as without the version.
5803 The effect is that references to the symbol with and without the
5804 version will be matched by the default symbol in the archive. */
5805
5806 p = strchr (name, ELF_VER_CHR);
5807 if (p == NULL || p[1] != ELF_VER_CHR)
5808 return h;
5809
5810 /* First check with only one `@'. */
5811 len = strlen (name);
5812 copy = (char *) bfd_alloc (abfd, len);
5813 if (copy == NULL)
5814 return (struct bfd_link_hash_entry *) -1;
5815
5816 first = p - name + 1;
5817 memcpy (copy, name, first);
5818 memcpy (copy + first, name + first + 1, len - first);
5819
5820 h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
5821 if (h == NULL)
5822 {
5823 /* We also need to check references to the symbol without the
5824 version. */
5825 copy[first - 1] = '\0';
5826 h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
5827 }
5828
5829 bfd_release (abfd, copy);
5830 return h;
5831 }
5832
5833 /* Add symbols from an ELF archive file to the linker hash table. We
5834 don't use _bfd_generic_link_add_archive_symbols because we need to
5835 handle versioned symbols.
5836
5837 Fortunately, ELF archive handling is simpler than that done by
5838 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5839 oddities. In ELF, if we find a symbol in the archive map, and the
5840 symbol is currently undefined, we know that we must pull in that
5841 object file.
5842
5843 Unfortunately, we do have to make multiple passes over the symbol
5844 table until nothing further is resolved. */
5845
5846 static bool
5847 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5848 {
5849 symindex c;
5850 unsigned char *included = NULL;
5851 carsym *symdefs;
5852 bool loop;
5853 size_t amt;
5854 const struct elf_backend_data *bed;
5855 struct bfd_link_hash_entry * (*archive_symbol_lookup)
5856 (bfd *, struct bfd_link_info *, const char *);
5857
5858 if (! bfd_has_map (abfd))
5859 {
5860 /* An empty archive is a special case. */
5861 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5862 return true;
5863 bfd_set_error (bfd_error_no_armap);
5864 return false;
5865 }
5866
5867 /* Keep track of all symbols we know to be already defined, and all
5868 files we know to be already included. This is to speed up the
5869 second and subsequent passes. */
5870 c = bfd_ardata (abfd)->symdef_count;
5871 if (c == 0)
5872 return true;
5873 amt = c * sizeof (*included);
5874 included = (unsigned char *) bfd_zmalloc (amt);
5875 if (included == NULL)
5876 return false;
5877
5878 symdefs = bfd_ardata (abfd)->symdefs;
5879 bed = get_elf_backend_data (abfd);
5880 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5881
5882 do
5883 {
5884 file_ptr last;
5885 symindex i;
5886 carsym *symdef;
5887 carsym *symdefend;
5888
5889 loop = false;
5890 last = -1;
5891
5892 symdef = symdefs;
5893 symdefend = symdef + c;
5894 for (i = 0; symdef < symdefend; symdef++, i++)
5895 {
5896 struct bfd_link_hash_entry *h;
5897 bfd *element;
5898 struct bfd_link_hash_entry *undefs_tail;
5899 symindex mark;
5900
5901 if (included[i])
5902 continue;
5903 if (symdef->file_offset == last)
5904 {
5905 included[i] = true;
5906 continue;
5907 }
5908
5909 h = archive_symbol_lookup (abfd, info, symdef->name);
5910 if (h == (struct bfd_link_hash_entry *) -1)
5911 goto error_return;
5912
5913 if (h == NULL)
5914 continue;
5915
5916 if (h->type == bfd_link_hash_undefined)
5917 {
5918 /* If the archive element has already been loaded then one
5919 of the symbols defined by that element might have been
5920 made undefined due to being in a discarded section. */
5921 if (is_elf_hash_table (info->hash)
5922 && ((struct elf_link_hash_entry *) h)->indx == -3)
5923 continue;
5924 }
5925 else if (h->type == bfd_link_hash_common)
5926 {
5927 /* We currently have a common symbol. The archive map contains
5928 a reference to this symbol, so we may want to include it. We
5929 only want to include it however, if this archive element
5930 contains a definition of the symbol, not just another common
5931 declaration of it.
5932
5933 Unfortunately some archivers (including GNU ar) will put
5934 declarations of common symbols into their archive maps, as
5935 well as real definitions, so we cannot just go by the archive
5936 map alone. Instead we must read in the element's symbol
5937 table and check that to see what kind of symbol definition
5938 this is. */
5939 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5940 continue;
5941 }
5942 else
5943 {
5944 if (h->type != bfd_link_hash_undefweak)
5945 /* Symbol must be defined. Don't check it again. */
5946 included[i] = true;
5947 continue;
5948 }
5949
5950 /* We need to include this archive member. */
5951 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5952 if (element == NULL)
5953 goto error_return;
5954
5955 if (! bfd_check_format (element, bfd_object))
5956 goto error_return;
5957
5958 undefs_tail = info->hash->undefs_tail;
5959
5960 if (!(*info->callbacks
5961 ->add_archive_element) (info, element, symdef->name, &element))
5962 continue;
5963 if (!bfd_link_add_symbols (element, info))
5964 goto error_return;
5965
5966 /* If there are any new undefined symbols, we need to make
5967 another pass through the archive in order to see whether
5968 they can be defined. FIXME: This isn't perfect, because
5969 common symbols wind up on undefs_tail and because an
5970 undefined symbol which is defined later on in this pass
5971 does not require another pass. This isn't a bug, but it
5972 does make the code less efficient than it could be. */
5973 if (undefs_tail != info->hash->undefs_tail)
5974 loop = true;
5975
5976 /* Look backward to mark all symbols from this object file
5977 which we have already seen in this pass. */
5978 mark = i;
5979 do
5980 {
5981 included[mark] = true;
5982 if (mark == 0)
5983 break;
5984 --mark;
5985 }
5986 while (symdefs[mark].file_offset == symdef->file_offset);
5987
5988 /* We mark subsequent symbols from this object file as we go
5989 on through the loop. */
5990 last = symdef->file_offset;
5991 }
5992 }
5993 while (loop);
5994
5995 free (included);
5996 return true;
5997
5998 error_return:
5999 free (included);
6000 return false;
6001 }
6002
6003 /* Given an ELF BFD, add symbols to the global hash table as
6004 appropriate. */
6005
6006 bool
6007 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
6008 {
6009 switch (bfd_get_format (abfd))
6010 {
6011 case bfd_object:
6012 return elf_link_add_object_symbols (abfd, info);
6013 case bfd_archive:
6014 return elf_link_add_archive_symbols (abfd, info);
6015 default:
6016 bfd_set_error (bfd_error_wrong_format);
6017 return false;
6018 }
6019 }
6020 \f
6021 struct hash_codes_info
6022 {
6023 unsigned long *hashcodes;
6024 bool error;
6025 };
6026
6027 /* This function will be called though elf_link_hash_traverse to store
6028 all hash value of the exported symbols in an array. */
6029
6030 static bool
6031 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
6032 {
6033 struct hash_codes_info *inf = (struct hash_codes_info *) data;
6034 const char *name;
6035 unsigned long ha;
6036 char *alc = NULL;
6037
6038 /* Ignore indirect symbols. These are added by the versioning code. */
6039 if (h->dynindx == -1)
6040 return true;
6041
6042 name = h->root.root.string;
6043 if (h->versioned >= versioned)
6044 {
6045 char *p = strchr (name, ELF_VER_CHR);
6046 if (p != NULL)
6047 {
6048 alc = (char *) bfd_malloc (p - name + 1);
6049 if (alc == NULL)
6050 {
6051 inf->error = true;
6052 return false;
6053 }
6054 memcpy (alc, name, p - name);
6055 alc[p - name] = '\0';
6056 name = alc;
6057 }
6058 }
6059
6060 /* Compute the hash value. */
6061 ha = bfd_elf_hash (name);
6062
6063 /* Store the found hash value in the array given as the argument. */
6064 *(inf->hashcodes)++ = ha;
6065
6066 /* And store it in the struct so that we can put it in the hash table
6067 later. */
6068 h->u.elf_hash_value = ha;
6069
6070 free (alc);
6071 return true;
6072 }
6073
6074 struct collect_gnu_hash_codes
6075 {
6076 bfd *output_bfd;
6077 const struct elf_backend_data *bed;
6078 unsigned long int nsyms;
6079 unsigned long int maskbits;
6080 unsigned long int *hashcodes;
6081 unsigned long int *hashval;
6082 unsigned long int *indx;
6083 unsigned long int *counts;
6084 bfd_vma *bitmask;
6085 bfd_byte *contents;
6086 bfd_size_type xlat;
6087 long int min_dynindx;
6088 unsigned long int bucketcount;
6089 unsigned long int symindx;
6090 long int local_indx;
6091 long int shift1, shift2;
6092 unsigned long int mask;
6093 bool error;
6094 };
6095
6096 /* This function will be called though elf_link_hash_traverse to store
6097 all hash value of the exported symbols in an array. */
6098
6099 static bool
6100 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
6101 {
6102 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6103 const char *name;
6104 unsigned long ha;
6105 char *alc = NULL;
6106
6107 /* Ignore indirect symbols. These are added by the versioning code. */
6108 if (h->dynindx == -1)
6109 return true;
6110
6111 /* Ignore also local symbols and undefined symbols. */
6112 if (! (*s->bed->elf_hash_symbol) (h))
6113 return true;
6114
6115 name = h->root.root.string;
6116 if (h->versioned >= versioned)
6117 {
6118 char *p = strchr (name, ELF_VER_CHR);
6119 if (p != NULL)
6120 {
6121 alc = (char *) bfd_malloc (p - name + 1);
6122 if (alc == NULL)
6123 {
6124 s->error = true;
6125 return false;
6126 }
6127 memcpy (alc, name, p - name);
6128 alc[p - name] = '\0';
6129 name = alc;
6130 }
6131 }
6132
6133 /* Compute the hash value. */
6134 ha = bfd_elf_gnu_hash (name);
6135
6136 /* Store the found hash value in the array for compute_bucket_count,
6137 and also for .dynsym reordering purposes. */
6138 s->hashcodes[s->nsyms] = ha;
6139 s->hashval[h->dynindx] = ha;
6140 ++s->nsyms;
6141 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
6142 s->min_dynindx = h->dynindx;
6143
6144 free (alc);
6145 return true;
6146 }
6147
6148 /* This function will be called though elf_link_hash_traverse to do
6149 final dynamic symbol renumbering in case of .gnu.hash.
6150 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6151 to the translation table. */
6152
6153 static bool
6154 elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data)
6155 {
6156 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6157 unsigned long int bucket;
6158 unsigned long int val;
6159
6160 /* Ignore indirect symbols. */
6161 if (h->dynindx == -1)
6162 return true;
6163
6164 /* Ignore also local symbols and undefined symbols. */
6165 if (! (*s->bed->elf_hash_symbol) (h))
6166 {
6167 if (h->dynindx >= s->min_dynindx)
6168 {
6169 if (s->bed->record_xhash_symbol != NULL)
6170 {
6171 (*s->bed->record_xhash_symbol) (h, 0);
6172 s->local_indx++;
6173 }
6174 else
6175 h->dynindx = s->local_indx++;
6176 }
6177 return true;
6178 }
6179
6180 bucket = s->hashval[h->dynindx] % s->bucketcount;
6181 val = (s->hashval[h->dynindx] >> s->shift1)
6182 & ((s->maskbits >> s->shift1) - 1);
6183 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
6184 s->bitmask[val]
6185 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
6186 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
6187 if (s->counts[bucket] == 1)
6188 /* Last element terminates the chain. */
6189 val |= 1;
6190 bfd_put_32 (s->output_bfd, val,
6191 s->contents + (s->indx[bucket] - s->symindx) * 4);
6192 --s->counts[bucket];
6193 if (s->bed->record_xhash_symbol != NULL)
6194 {
6195 bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4;
6196
6197 (*s->bed->record_xhash_symbol) (h, xlat_loc);
6198 }
6199 else
6200 h->dynindx = s->indx[bucket]++;
6201 return true;
6202 }
6203
6204 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6205
6206 bool
6207 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
6208 {
6209 return !(h->forced_local
6210 || h->root.type == bfd_link_hash_undefined
6211 || h->root.type == bfd_link_hash_undefweak
6212 || ((h->root.type == bfd_link_hash_defined
6213 || h->root.type == bfd_link_hash_defweak)
6214 && h->root.u.def.section->output_section == NULL));
6215 }
6216
6217 /* Array used to determine the number of hash table buckets to use
6218 based on the number of symbols there are. If there are fewer than
6219 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6220 fewer than 37 we use 17 buckets, and so forth. We never use more
6221 than 32771 buckets. */
6222
6223 static const size_t elf_buckets[] =
6224 {
6225 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6226 16411, 32771, 0
6227 };
6228
6229 /* Compute bucket count for hashing table. We do not use a static set
6230 of possible tables sizes anymore. Instead we determine for all
6231 possible reasonable sizes of the table the outcome (i.e., the
6232 number of collisions etc) and choose the best solution. The
6233 weighting functions are not too simple to allow the table to grow
6234 without bounds. Instead one of the weighting factors is the size.
6235 Therefore the result is always a good payoff between few collisions
6236 (= short chain lengths) and table size. */
6237 static size_t
6238 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
6239 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
6240 unsigned long int nsyms,
6241 int gnu_hash)
6242 {
6243 size_t best_size = 0;
6244 unsigned long int i;
6245
6246 /* We have a problem here. The following code to optimize the table
6247 size requires an integer type with more the 32 bits. If
6248 BFD_HOST_U_64_BIT is set we know about such a type. */
6249 #ifdef BFD_HOST_U_64_BIT
6250 if (info->optimize)
6251 {
6252 size_t minsize;
6253 size_t maxsize;
6254 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
6255 bfd *dynobj = elf_hash_table (info)->dynobj;
6256 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
6257 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
6258 unsigned long int *counts;
6259 bfd_size_type amt;
6260 unsigned int no_improvement_count = 0;
6261
6262 /* Possible optimization parameters: if we have NSYMS symbols we say
6263 that the hashing table must at least have NSYMS/4 and at most
6264 2*NSYMS buckets. */
6265 minsize = nsyms / 4;
6266 if (minsize == 0)
6267 minsize = 1;
6268 best_size = maxsize = nsyms * 2;
6269 if (gnu_hash)
6270 {
6271 if (minsize < 2)
6272 minsize = 2;
6273 if ((best_size & 31) == 0)
6274 ++best_size;
6275 }
6276
6277 /* Create array where we count the collisions in. We must use bfd_malloc
6278 since the size could be large. */
6279 amt = maxsize;
6280 amt *= sizeof (unsigned long int);
6281 counts = (unsigned long int *) bfd_malloc (amt);
6282 if (counts == NULL)
6283 return 0;
6284
6285 /* Compute the "optimal" size for the hash table. The criteria is a
6286 minimal chain length. The minor criteria is (of course) the size
6287 of the table. */
6288 for (i = minsize; i < maxsize; ++i)
6289 {
6290 /* Walk through the array of hashcodes and count the collisions. */
6291 BFD_HOST_U_64_BIT max;
6292 unsigned long int j;
6293 unsigned long int fact;
6294
6295 if (gnu_hash && (i & 31) == 0)
6296 continue;
6297
6298 memset (counts, '\0', i * sizeof (unsigned long int));
6299
6300 /* Determine how often each hash bucket is used. */
6301 for (j = 0; j < nsyms; ++j)
6302 ++counts[hashcodes[j] % i];
6303
6304 /* For the weight function we need some information about the
6305 pagesize on the target. This is information need not be 100%
6306 accurate. Since this information is not available (so far) we
6307 define it here to a reasonable default value. If it is crucial
6308 to have a better value some day simply define this value. */
6309 # ifndef BFD_TARGET_PAGESIZE
6310 # define BFD_TARGET_PAGESIZE (4096)
6311 # endif
6312
6313 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6314 and the chains. */
6315 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
6316
6317 # if 1
6318 /* Variant 1: optimize for short chains. We add the squares
6319 of all the chain lengths (which favors many small chain
6320 over a few long chains). */
6321 for (j = 0; j < i; ++j)
6322 max += counts[j] * counts[j];
6323
6324 /* This adds penalties for the overall size of the table. */
6325 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6326 max *= fact * fact;
6327 # else
6328 /* Variant 2: Optimize a lot more for small table. Here we
6329 also add squares of the size but we also add penalties for
6330 empty slots (the +1 term). */
6331 for (j = 0; j < i; ++j)
6332 max += (1 + counts[j]) * (1 + counts[j]);
6333
6334 /* The overall size of the table is considered, but not as
6335 strong as in variant 1, where it is squared. */
6336 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6337 max *= fact;
6338 # endif
6339
6340 /* Compare with current best results. */
6341 if (max < best_chlen)
6342 {
6343 best_chlen = max;
6344 best_size = i;
6345 no_improvement_count = 0;
6346 }
6347 /* PR 11843: Avoid futile long searches for the best bucket size
6348 when there are a large number of symbols. */
6349 else if (++no_improvement_count == 100)
6350 break;
6351 }
6352
6353 free (counts);
6354 }
6355 else
6356 #endif /* defined (BFD_HOST_U_64_BIT) */
6357 {
6358 /* This is the fallback solution if no 64bit type is available or if we
6359 are not supposed to spend much time on optimizations. We select the
6360 bucket count using a fixed set of numbers. */
6361 for (i = 0; elf_buckets[i] != 0; i++)
6362 {
6363 best_size = elf_buckets[i];
6364 if (nsyms < elf_buckets[i + 1])
6365 break;
6366 }
6367 if (gnu_hash && best_size < 2)
6368 best_size = 2;
6369 }
6370
6371 return best_size;
6372 }
6373
6374 /* Size any SHT_GROUP section for ld -r. */
6375
6376 bool
6377 _bfd_elf_size_group_sections (struct bfd_link_info *info)
6378 {
6379 bfd *ibfd;
6380 asection *s;
6381
6382 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6383 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6384 && (s = ibfd->sections) != NULL
6385 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
6386 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6387 return false;
6388 return true;
6389 }
6390
6391 /* Set a default stack segment size. The value in INFO wins. If it
6392 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6393 undefined it is initialized. */
6394
6395 bool
6396 bfd_elf_stack_segment_size (bfd *output_bfd,
6397 struct bfd_link_info *info,
6398 const char *legacy_symbol,
6399 bfd_vma default_size)
6400 {
6401 struct elf_link_hash_entry *h = NULL;
6402
6403 /* Look for legacy symbol. */
6404 if (legacy_symbol)
6405 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6406 false, false, false);
6407 if (h && (h->root.type == bfd_link_hash_defined
6408 || h->root.type == bfd_link_hash_defweak)
6409 && h->def_regular
6410 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6411 {
6412 /* The symbol has no type if specified on the command line. */
6413 h->type = STT_OBJECT;
6414 if (info->stacksize)
6415 /* xgettext:c-format */
6416 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6417 output_bfd, legacy_symbol);
6418 else if (h->root.u.def.section != bfd_abs_section_ptr)
6419 /* xgettext:c-format */
6420 _bfd_error_handler (_("%pB: %s not absolute"),
6421 output_bfd, legacy_symbol);
6422 else
6423 info->stacksize = h->root.u.def.value;
6424 }
6425
6426 if (!info->stacksize)
6427 /* If the user didn't set a size, or explicitly inhibit the
6428 size, set it now. */
6429 info->stacksize = default_size;
6430
6431 /* Provide the legacy symbol, if it is referenced. */
6432 if (h && (h->root.type == bfd_link_hash_undefined
6433 || h->root.type == bfd_link_hash_undefweak))
6434 {
6435 struct bfd_link_hash_entry *bh = NULL;
6436
6437 if (!(_bfd_generic_link_add_one_symbol
6438 (info, output_bfd, legacy_symbol,
6439 BSF_GLOBAL, bfd_abs_section_ptr,
6440 info->stacksize >= 0 ? info->stacksize : 0,
6441 NULL, false, get_elf_backend_data (output_bfd)->collect, &bh)))
6442 return false;
6443
6444 h = (struct elf_link_hash_entry *) bh;
6445 h->def_regular = 1;
6446 h->type = STT_OBJECT;
6447 }
6448
6449 return true;
6450 }
6451
6452 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6453
6454 struct elf_gc_sweep_symbol_info
6455 {
6456 struct bfd_link_info *info;
6457 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6458 bool);
6459 };
6460
6461 static bool
6462 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6463 {
6464 if (!h->mark
6465 && (((h->root.type == bfd_link_hash_defined
6466 || h->root.type == bfd_link_hash_defweak)
6467 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6468 && h->root.u.def.section->gc_mark))
6469 || h->root.type == bfd_link_hash_undefined
6470 || h->root.type == bfd_link_hash_undefweak))
6471 {
6472 struct elf_gc_sweep_symbol_info *inf;
6473
6474 inf = (struct elf_gc_sweep_symbol_info *) data;
6475 (*inf->hide_symbol) (inf->info, h, true);
6476 h->def_regular = 0;
6477 h->ref_regular = 0;
6478 h->ref_regular_nonweak = 0;
6479 }
6480
6481 return true;
6482 }
6483
6484 /* Set up the sizes and contents of the ELF dynamic sections. This is
6485 called by the ELF linker emulation before_allocation routine. We
6486 must set the sizes of the sections before the linker sets the
6487 addresses of the various sections. */
6488
6489 bool
6490 bfd_elf_size_dynamic_sections (bfd *output_bfd,
6491 const char *soname,
6492 const char *rpath,
6493 const char *filter_shlib,
6494 const char *audit,
6495 const char *depaudit,
6496 const char * const *auxiliary_filters,
6497 struct bfd_link_info *info,
6498 asection **sinterpptr)
6499 {
6500 bfd *dynobj;
6501 const struct elf_backend_data *bed;
6502
6503 *sinterpptr = NULL;
6504
6505 if (!is_elf_hash_table (info->hash))
6506 return true;
6507
6508 dynobj = elf_hash_table (info)->dynobj;
6509
6510 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6511 {
6512 struct bfd_elf_version_tree *verdefs;
6513 struct elf_info_failed asvinfo;
6514 struct bfd_elf_version_tree *t;
6515 struct bfd_elf_version_expr *d;
6516 asection *s;
6517 size_t soname_indx;
6518
6519 /* If we are supposed to export all symbols into the dynamic symbol
6520 table (this is not the normal case), then do so. */
6521 if (info->export_dynamic
6522 || (bfd_link_executable (info) && info->dynamic))
6523 {
6524 struct elf_info_failed eif;
6525
6526 eif.info = info;
6527 eif.failed = false;
6528 elf_link_hash_traverse (elf_hash_table (info),
6529 _bfd_elf_export_symbol,
6530 &eif);
6531 if (eif.failed)
6532 return false;
6533 }
6534
6535 if (soname != NULL)
6536 {
6537 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6538 soname, true);
6539 if (soname_indx == (size_t) -1
6540 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6541 return false;
6542 }
6543 else
6544 soname_indx = (size_t) -1;
6545
6546 /* Make all global versions with definition. */
6547 for (t = info->version_info; t != NULL; t = t->next)
6548 for (d = t->globals.list; d != NULL; d = d->next)
6549 if (!d->symver && d->literal)
6550 {
6551 const char *verstr, *name;
6552 size_t namelen, verlen, newlen;
6553 char *newname, *p, leading_char;
6554 struct elf_link_hash_entry *newh;
6555
6556 leading_char = bfd_get_symbol_leading_char (output_bfd);
6557 name = d->pattern;
6558 namelen = strlen (name) + (leading_char != '\0');
6559 verstr = t->name;
6560 verlen = strlen (verstr);
6561 newlen = namelen + verlen + 3;
6562
6563 newname = (char *) bfd_malloc (newlen);
6564 if (newname == NULL)
6565 return false;
6566 newname[0] = leading_char;
6567 memcpy (newname + (leading_char != '\0'), name, namelen);
6568
6569 /* Check the hidden versioned definition. */
6570 p = newname + namelen;
6571 *p++ = ELF_VER_CHR;
6572 memcpy (p, verstr, verlen + 1);
6573 newh = elf_link_hash_lookup (elf_hash_table (info),
6574 newname, false, false,
6575 false);
6576 if (newh == NULL
6577 || (newh->root.type != bfd_link_hash_defined
6578 && newh->root.type != bfd_link_hash_defweak))
6579 {
6580 /* Check the default versioned definition. */
6581 *p++ = ELF_VER_CHR;
6582 memcpy (p, verstr, verlen + 1);
6583 newh = elf_link_hash_lookup (elf_hash_table (info),
6584 newname, false, false,
6585 false);
6586 }
6587 free (newname);
6588
6589 /* Mark this version if there is a definition and it is
6590 not defined in a shared object. */
6591 if (newh != NULL
6592 && !newh->def_dynamic
6593 && (newh->root.type == bfd_link_hash_defined
6594 || newh->root.type == bfd_link_hash_defweak))
6595 d->symver = 1;
6596 }
6597
6598 /* Attach all the symbols to their version information. */
6599 asvinfo.info = info;
6600 asvinfo.failed = false;
6601
6602 elf_link_hash_traverse (elf_hash_table (info),
6603 _bfd_elf_link_assign_sym_version,
6604 &asvinfo);
6605 if (asvinfo.failed)
6606 return false;
6607
6608 if (!info->allow_undefined_version)
6609 {
6610 /* Check if all global versions have a definition. */
6611 bool all_defined = true;
6612 for (t = info->version_info; t != NULL; t = t->next)
6613 for (d = t->globals.list; d != NULL; d = d->next)
6614 if (d->literal && !d->symver && !d->script)
6615 {
6616 _bfd_error_handler
6617 (_("%s: undefined version: %s"),
6618 d->pattern, t->name);
6619 all_defined = false;
6620 }
6621
6622 if (!all_defined)
6623 {
6624 bfd_set_error (bfd_error_bad_value);
6625 return false;
6626 }
6627 }
6628
6629 /* Set up the version definition section. */
6630 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6631 BFD_ASSERT (s != NULL);
6632
6633 /* We may have created additional version definitions if we are
6634 just linking a regular application. */
6635 verdefs = info->version_info;
6636
6637 /* Skip anonymous version tag. */
6638 if (verdefs != NULL && verdefs->vernum == 0)
6639 verdefs = verdefs->next;
6640
6641 if (verdefs == NULL && !info->create_default_symver)
6642 s->flags |= SEC_EXCLUDE;
6643 else
6644 {
6645 unsigned int cdefs;
6646 bfd_size_type size;
6647 bfd_byte *p;
6648 Elf_Internal_Verdef def;
6649 Elf_Internal_Verdaux defaux;
6650 struct bfd_link_hash_entry *bh;
6651 struct elf_link_hash_entry *h;
6652 const char *name;
6653
6654 cdefs = 0;
6655 size = 0;
6656
6657 /* Make space for the base version. */
6658 size += sizeof (Elf_External_Verdef);
6659 size += sizeof (Elf_External_Verdaux);
6660 ++cdefs;
6661
6662 /* Make space for the default version. */
6663 if (info->create_default_symver)
6664 {
6665 size += sizeof (Elf_External_Verdef);
6666 ++cdefs;
6667 }
6668
6669 for (t = verdefs; t != NULL; t = t->next)
6670 {
6671 struct bfd_elf_version_deps *n;
6672
6673 /* Don't emit base version twice. */
6674 if (t->vernum == 0)
6675 continue;
6676
6677 size += sizeof (Elf_External_Verdef);
6678 size += sizeof (Elf_External_Verdaux);
6679 ++cdefs;
6680
6681 for (n = t->deps; n != NULL; n = n->next)
6682 size += sizeof (Elf_External_Verdaux);
6683 }
6684
6685 s->size = size;
6686 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6687 if (s->contents == NULL && s->size != 0)
6688 return false;
6689
6690 /* Fill in the version definition section. */
6691
6692 p = s->contents;
6693
6694 def.vd_version = VER_DEF_CURRENT;
6695 def.vd_flags = VER_FLG_BASE;
6696 def.vd_ndx = 1;
6697 def.vd_cnt = 1;
6698 if (info->create_default_symver)
6699 {
6700 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6701 def.vd_next = sizeof (Elf_External_Verdef);
6702 }
6703 else
6704 {
6705 def.vd_aux = sizeof (Elf_External_Verdef);
6706 def.vd_next = (sizeof (Elf_External_Verdef)
6707 + sizeof (Elf_External_Verdaux));
6708 }
6709
6710 if (soname_indx != (size_t) -1)
6711 {
6712 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6713 soname_indx);
6714 def.vd_hash = bfd_elf_hash (soname);
6715 defaux.vda_name = soname_indx;
6716 name = soname;
6717 }
6718 else
6719 {
6720 size_t indx;
6721
6722 name = lbasename (bfd_get_filename (output_bfd));
6723 def.vd_hash = bfd_elf_hash (name);
6724 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6725 name, false);
6726 if (indx == (size_t) -1)
6727 return false;
6728 defaux.vda_name = indx;
6729 }
6730 defaux.vda_next = 0;
6731
6732 _bfd_elf_swap_verdef_out (output_bfd, &def,
6733 (Elf_External_Verdef *) p);
6734 p += sizeof (Elf_External_Verdef);
6735 if (info->create_default_symver)
6736 {
6737 /* Add a symbol representing this version. */
6738 bh = NULL;
6739 if (! (_bfd_generic_link_add_one_symbol
6740 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6741 0, NULL, false,
6742 get_elf_backend_data (dynobj)->collect, &bh)))
6743 return false;
6744 h = (struct elf_link_hash_entry *) bh;
6745 h->non_elf = 0;
6746 h->def_regular = 1;
6747 h->type = STT_OBJECT;
6748 h->verinfo.vertree = NULL;
6749
6750 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6751 return false;
6752
6753 /* Create a duplicate of the base version with the same
6754 aux block, but different flags. */
6755 def.vd_flags = 0;
6756 def.vd_ndx = 2;
6757 def.vd_aux = sizeof (Elf_External_Verdef);
6758 if (verdefs)
6759 def.vd_next = (sizeof (Elf_External_Verdef)
6760 + sizeof (Elf_External_Verdaux));
6761 else
6762 def.vd_next = 0;
6763 _bfd_elf_swap_verdef_out (output_bfd, &def,
6764 (Elf_External_Verdef *) p);
6765 p += sizeof (Elf_External_Verdef);
6766 }
6767 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6768 (Elf_External_Verdaux *) p);
6769 p += sizeof (Elf_External_Verdaux);
6770
6771 for (t = verdefs; t != NULL; t = t->next)
6772 {
6773 unsigned int cdeps;
6774 struct bfd_elf_version_deps *n;
6775
6776 /* Don't emit the base version twice. */
6777 if (t->vernum == 0)
6778 continue;
6779
6780 cdeps = 0;
6781 for (n = t->deps; n != NULL; n = n->next)
6782 ++cdeps;
6783
6784 /* Add a symbol representing this version. */
6785 bh = NULL;
6786 if (! (_bfd_generic_link_add_one_symbol
6787 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6788 0, NULL, false,
6789 get_elf_backend_data (dynobj)->collect, &bh)))
6790 return false;
6791 h = (struct elf_link_hash_entry *) bh;
6792 h->non_elf = 0;
6793 h->def_regular = 1;
6794 h->type = STT_OBJECT;
6795 h->verinfo.vertree = t;
6796
6797 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6798 return false;
6799
6800 def.vd_version = VER_DEF_CURRENT;
6801 def.vd_flags = 0;
6802 if (t->globals.list == NULL
6803 && t->locals.list == NULL
6804 && ! t->used)
6805 def.vd_flags |= VER_FLG_WEAK;
6806 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6807 def.vd_cnt = cdeps + 1;
6808 def.vd_hash = bfd_elf_hash (t->name);
6809 def.vd_aux = sizeof (Elf_External_Verdef);
6810 def.vd_next = 0;
6811
6812 /* If a basever node is next, it *must* be the last node in
6813 the chain, otherwise Verdef construction breaks. */
6814 if (t->next != NULL && t->next->vernum == 0)
6815 BFD_ASSERT (t->next->next == NULL);
6816
6817 if (t->next != NULL && t->next->vernum != 0)
6818 def.vd_next = (sizeof (Elf_External_Verdef)
6819 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6820
6821 _bfd_elf_swap_verdef_out (output_bfd, &def,
6822 (Elf_External_Verdef *) p);
6823 p += sizeof (Elf_External_Verdef);
6824
6825 defaux.vda_name = h->dynstr_index;
6826 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6827 h->dynstr_index);
6828 defaux.vda_next = 0;
6829 if (t->deps != NULL)
6830 defaux.vda_next = sizeof (Elf_External_Verdaux);
6831 t->name_indx = defaux.vda_name;
6832
6833 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6834 (Elf_External_Verdaux *) p);
6835 p += sizeof (Elf_External_Verdaux);
6836
6837 for (n = t->deps; n != NULL; n = n->next)
6838 {
6839 if (n->version_needed == NULL)
6840 {
6841 /* This can happen if there was an error in the
6842 version script. */
6843 defaux.vda_name = 0;
6844 }
6845 else
6846 {
6847 defaux.vda_name = n->version_needed->name_indx;
6848 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6849 defaux.vda_name);
6850 }
6851 if (n->next == NULL)
6852 defaux.vda_next = 0;
6853 else
6854 defaux.vda_next = sizeof (Elf_External_Verdaux);
6855
6856 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6857 (Elf_External_Verdaux *) p);
6858 p += sizeof (Elf_External_Verdaux);
6859 }
6860 }
6861
6862 elf_tdata (output_bfd)->cverdefs = cdefs;
6863 }
6864 }
6865
6866 bed = get_elf_backend_data (output_bfd);
6867
6868 if (info->gc_sections && bed->can_gc_sections)
6869 {
6870 struct elf_gc_sweep_symbol_info sweep_info;
6871
6872 /* Remove the symbols that were in the swept sections from the
6873 dynamic symbol table. */
6874 sweep_info.info = info;
6875 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6876 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6877 &sweep_info);
6878 }
6879
6880 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6881 {
6882 asection *s;
6883 struct elf_find_verdep_info sinfo;
6884
6885 /* Work out the size of the version reference section. */
6886
6887 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6888 BFD_ASSERT (s != NULL);
6889
6890 sinfo.info = info;
6891 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6892 if (sinfo.vers == 0)
6893 sinfo.vers = 1;
6894 sinfo.failed = false;
6895
6896 elf_link_hash_traverse (elf_hash_table (info),
6897 _bfd_elf_link_find_version_dependencies,
6898 &sinfo);
6899 if (sinfo.failed)
6900 return false;
6901
6902 if (elf_tdata (output_bfd)->verref == NULL)
6903 s->flags |= SEC_EXCLUDE;
6904 else
6905 {
6906 Elf_Internal_Verneed *vn;
6907 unsigned int size;
6908 unsigned int crefs;
6909 bfd_byte *p;
6910
6911 /* Build the version dependency section. */
6912 size = 0;
6913 crefs = 0;
6914 for (vn = elf_tdata (output_bfd)->verref;
6915 vn != NULL;
6916 vn = vn->vn_nextref)
6917 {
6918 Elf_Internal_Vernaux *a;
6919
6920 size += sizeof (Elf_External_Verneed);
6921 ++crefs;
6922 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6923 size += sizeof (Elf_External_Vernaux);
6924 }
6925
6926 s->size = size;
6927 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6928 if (s->contents == NULL)
6929 return false;
6930
6931 p = s->contents;
6932 for (vn = elf_tdata (output_bfd)->verref;
6933 vn != NULL;
6934 vn = vn->vn_nextref)
6935 {
6936 unsigned int caux;
6937 Elf_Internal_Vernaux *a;
6938 size_t indx;
6939
6940 caux = 0;
6941 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6942 ++caux;
6943
6944 vn->vn_version = VER_NEED_CURRENT;
6945 vn->vn_cnt = caux;
6946 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6947 elf_dt_name (vn->vn_bfd) != NULL
6948 ? elf_dt_name (vn->vn_bfd)
6949 : lbasename (bfd_get_filename
6950 (vn->vn_bfd)),
6951 false);
6952 if (indx == (size_t) -1)
6953 return false;
6954 vn->vn_file = indx;
6955 vn->vn_aux = sizeof (Elf_External_Verneed);
6956 if (vn->vn_nextref == NULL)
6957 vn->vn_next = 0;
6958 else
6959 vn->vn_next = (sizeof (Elf_External_Verneed)
6960 + caux * sizeof (Elf_External_Vernaux));
6961
6962 _bfd_elf_swap_verneed_out (output_bfd, vn,
6963 (Elf_External_Verneed *) p);
6964 p += sizeof (Elf_External_Verneed);
6965
6966 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6967 {
6968 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6969 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6970 a->vna_nodename, false);
6971 if (indx == (size_t) -1)
6972 return false;
6973 a->vna_name = indx;
6974 if (a->vna_nextptr == NULL)
6975 a->vna_next = 0;
6976 else
6977 a->vna_next = sizeof (Elf_External_Vernaux);
6978
6979 _bfd_elf_swap_vernaux_out (output_bfd, a,
6980 (Elf_External_Vernaux *) p);
6981 p += sizeof (Elf_External_Vernaux);
6982 }
6983 }
6984
6985 elf_tdata (output_bfd)->cverrefs = crefs;
6986 }
6987 }
6988
6989 /* Any syms created from now on start with -1 in
6990 got.refcount/offset and plt.refcount/offset. */
6991 elf_hash_table (info)->init_got_refcount
6992 = elf_hash_table (info)->init_got_offset;
6993 elf_hash_table (info)->init_plt_refcount
6994 = elf_hash_table (info)->init_plt_offset;
6995
6996 if (bfd_link_relocatable (info)
6997 && !_bfd_elf_size_group_sections (info))
6998 return false;
6999
7000 /* The backend may have to create some sections regardless of whether
7001 we're dynamic or not. */
7002 if (bed->elf_backend_always_size_sections
7003 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
7004 return false;
7005
7006 /* Determine any GNU_STACK segment requirements, after the backend
7007 has had a chance to set a default segment size. */
7008 if (info->execstack)
7009 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
7010 else if (info->noexecstack)
7011 elf_stack_flags (output_bfd) = PF_R | PF_W;
7012 else
7013 {
7014 bfd *inputobj;
7015 asection *notesec = NULL;
7016 int exec = 0;
7017
7018 for (inputobj = info->input_bfds;
7019 inputobj;
7020 inputobj = inputobj->link.next)
7021 {
7022 asection *s;
7023
7024 if (inputobj->flags
7025 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
7026 continue;
7027 s = inputobj->sections;
7028 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7029 continue;
7030
7031 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
7032 if (s)
7033 {
7034 if (s->flags & SEC_CODE)
7035 exec = PF_X;
7036 notesec = s;
7037 }
7038 else if (bed->default_execstack)
7039 exec = PF_X;
7040 }
7041 if (notesec || info->stacksize > 0)
7042 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
7043 if (notesec && exec && bfd_link_relocatable (info)
7044 && notesec->output_section != bfd_abs_section_ptr)
7045 notesec->output_section->flags |= SEC_CODE;
7046 }
7047
7048 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7049 {
7050 struct elf_info_failed eif;
7051 struct elf_link_hash_entry *h;
7052 asection *dynstr;
7053 asection *s;
7054
7055 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
7056 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
7057
7058 if (info->symbolic)
7059 {
7060 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
7061 return false;
7062 info->flags |= DF_SYMBOLIC;
7063 }
7064
7065 if (rpath != NULL)
7066 {
7067 size_t indx;
7068 bfd_vma tag;
7069
7070 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
7071 true);
7072 if (indx == (size_t) -1)
7073 return false;
7074
7075 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
7076 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
7077 return false;
7078 }
7079
7080 if (filter_shlib != NULL)
7081 {
7082 size_t indx;
7083
7084 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7085 filter_shlib, true);
7086 if (indx == (size_t) -1
7087 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
7088 return false;
7089 }
7090
7091 if (auxiliary_filters != NULL)
7092 {
7093 const char * const *p;
7094
7095 for (p = auxiliary_filters; *p != NULL; p++)
7096 {
7097 size_t indx;
7098
7099 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7100 *p, true);
7101 if (indx == (size_t) -1
7102 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
7103 return false;
7104 }
7105 }
7106
7107 if (audit != NULL)
7108 {
7109 size_t indx;
7110
7111 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
7112 true);
7113 if (indx == (size_t) -1
7114 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
7115 return false;
7116 }
7117
7118 if (depaudit != NULL)
7119 {
7120 size_t indx;
7121
7122 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
7123 true);
7124 if (indx == (size_t) -1
7125 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
7126 return false;
7127 }
7128
7129 eif.info = info;
7130 eif.failed = false;
7131
7132 /* Find all symbols which were defined in a dynamic object and make
7133 the backend pick a reasonable value for them. */
7134 elf_link_hash_traverse (elf_hash_table (info),
7135 _bfd_elf_adjust_dynamic_symbol,
7136 &eif);
7137 if (eif.failed)
7138 return false;
7139
7140 /* Add some entries to the .dynamic section. We fill in some of the
7141 values later, in bfd_elf_final_link, but we must add the entries
7142 now so that we know the final size of the .dynamic section. */
7143
7144 /* If there are initialization and/or finalization functions to
7145 call then add the corresponding DT_INIT/DT_FINI entries. */
7146 h = (info->init_function
7147 ? elf_link_hash_lookup (elf_hash_table (info),
7148 info->init_function, false,
7149 false, false)
7150 : NULL);
7151 if (h != NULL
7152 && (h->ref_regular
7153 || h->def_regular))
7154 {
7155 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
7156 return false;
7157 }
7158 h = (info->fini_function
7159 ? elf_link_hash_lookup (elf_hash_table (info),
7160 info->fini_function, false,
7161 false, false)
7162 : NULL);
7163 if (h != NULL
7164 && (h->ref_regular
7165 || h->def_regular))
7166 {
7167 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
7168 return false;
7169 }
7170
7171 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
7172 if (s != NULL && s->linker_has_input)
7173 {
7174 /* DT_PREINIT_ARRAY is not allowed in shared library. */
7175 if (! bfd_link_executable (info))
7176 {
7177 bfd *sub;
7178 asection *o;
7179
7180 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
7181 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
7182 && (o = sub->sections) != NULL
7183 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
7184 for (o = sub->sections; o != NULL; o = o->next)
7185 if (elf_section_data (o)->this_hdr.sh_type
7186 == SHT_PREINIT_ARRAY)
7187 {
7188 _bfd_error_handler
7189 (_("%pB: .preinit_array section is not allowed in DSO"),
7190 sub);
7191 break;
7192 }
7193
7194 bfd_set_error (bfd_error_nonrepresentable_section);
7195 return false;
7196 }
7197
7198 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
7199 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
7200 return false;
7201 }
7202 s = bfd_get_section_by_name (output_bfd, ".init_array");
7203 if (s != NULL && s->linker_has_input)
7204 {
7205 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
7206 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
7207 return false;
7208 }
7209 s = bfd_get_section_by_name (output_bfd, ".fini_array");
7210 if (s != NULL && s->linker_has_input)
7211 {
7212 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
7213 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
7214 return false;
7215 }
7216
7217 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
7218 /* If .dynstr is excluded from the link, we don't want any of
7219 these tags. Strictly, we should be checking each section
7220 individually; This quick check covers for the case where
7221 someone does a /DISCARD/ : { *(*) }. */
7222 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
7223 {
7224 bfd_size_type strsize;
7225
7226 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7227 if ((info->emit_hash
7228 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
7229 || (info->emit_gnu_hash
7230 && (bed->record_xhash_symbol == NULL
7231 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)))
7232 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
7233 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
7234 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
7235 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
7236 bed->s->sizeof_sym)
7237 || (info->gnu_flags_1
7238 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_FLAGS_1,
7239 info->gnu_flags_1)))
7240 return false;
7241 }
7242 }
7243
7244 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
7245 return false;
7246
7247 /* The backend must work out the sizes of all the other dynamic
7248 sections. */
7249 if (dynobj != NULL
7250 && bed->elf_backend_size_dynamic_sections != NULL
7251 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
7252 return false;
7253
7254 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7255 {
7256 if (elf_tdata (output_bfd)->cverdefs)
7257 {
7258 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
7259
7260 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
7261 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
7262 return false;
7263 }
7264
7265 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
7266 {
7267 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
7268 return false;
7269 }
7270 else if (info->flags & DF_BIND_NOW)
7271 {
7272 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
7273 return false;
7274 }
7275
7276 if (info->flags_1)
7277 {
7278 if (bfd_link_executable (info))
7279 info->flags_1 &= ~ (DF_1_INITFIRST
7280 | DF_1_NODELETE
7281 | DF_1_NOOPEN);
7282 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
7283 return false;
7284 }
7285
7286 if (elf_tdata (output_bfd)->cverrefs)
7287 {
7288 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
7289
7290 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
7291 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
7292 return false;
7293 }
7294
7295 if ((elf_tdata (output_bfd)->cverrefs == 0
7296 && elf_tdata (output_bfd)->cverdefs == 0)
7297 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
7298 {
7299 asection *s;
7300
7301 s = bfd_get_linker_section (dynobj, ".gnu.version");
7302 s->flags |= SEC_EXCLUDE;
7303 }
7304 }
7305 return true;
7306 }
7307
7308 /* Find the first non-excluded output section. We'll use its
7309 section symbol for some emitted relocs. */
7310 void
7311 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7312 {
7313 asection *s;
7314 asection *found = NULL;
7315
7316 for (s = output_bfd->sections; s != NULL; s = s->next)
7317 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7318 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7319 {
7320 found = s;
7321 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7322 break;
7323 }
7324 elf_hash_table (info)->text_index_section = found;
7325 }
7326
7327 /* Find two non-excluded output sections, one for code, one for data.
7328 We'll use their section symbols for some emitted relocs. */
7329 void
7330 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7331 {
7332 asection *s;
7333 asection *found = NULL;
7334
7335 /* Data first, since setting text_index_section changes
7336 _bfd_elf_omit_section_dynsym_default. */
7337 for (s = output_bfd->sections; s != NULL; s = s->next)
7338 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7339 && !(s->flags & SEC_READONLY)
7340 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7341 {
7342 found = s;
7343 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7344 break;
7345 }
7346 elf_hash_table (info)->data_index_section = found;
7347
7348 for (s = output_bfd->sections; s != NULL; s = s->next)
7349 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7350 && (s->flags & SEC_READONLY)
7351 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7352 {
7353 found = s;
7354 break;
7355 }
7356 elf_hash_table (info)->text_index_section = found;
7357 }
7358
7359 #define GNU_HASH_SECTION_NAME(bed) \
7360 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7361
7362 bool
7363 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7364 {
7365 const struct elf_backend_data *bed;
7366 unsigned long section_sym_count;
7367 bfd_size_type dynsymcount = 0;
7368
7369 if (!is_elf_hash_table (info->hash))
7370 return true;
7371
7372 bed = get_elf_backend_data (output_bfd);
7373 (*bed->elf_backend_init_index_section) (output_bfd, info);
7374
7375 /* Assign dynsym indices. In a shared library we generate a section
7376 symbol for each output section, which come first. Next come all
7377 of the back-end allocated local dynamic syms, followed by the rest
7378 of the global symbols.
7379
7380 This is usually not needed for static binaries, however backends
7381 can request to always do it, e.g. the MIPS backend uses dynamic
7382 symbol counts to lay out GOT, which will be produced in the
7383 presence of GOT relocations even in static binaries (holding fixed
7384 data in that case, to satisfy those relocations). */
7385
7386 if (elf_hash_table (info)->dynamic_sections_created
7387 || bed->always_renumber_dynsyms)
7388 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7389 &section_sym_count);
7390
7391 if (elf_hash_table (info)->dynamic_sections_created)
7392 {
7393 bfd *dynobj;
7394 asection *s;
7395 unsigned int dtagcount;
7396
7397 dynobj = elf_hash_table (info)->dynobj;
7398
7399 /* Work out the size of the symbol version section. */
7400 s = bfd_get_linker_section (dynobj, ".gnu.version");
7401 BFD_ASSERT (s != NULL);
7402 if ((s->flags & SEC_EXCLUDE) == 0)
7403 {
7404 s->size = dynsymcount * sizeof (Elf_External_Versym);
7405 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7406 if (s->contents == NULL)
7407 return false;
7408
7409 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7410 return false;
7411 }
7412
7413 /* Set the size of the .dynsym and .hash sections. We counted
7414 the number of dynamic symbols in elf_link_add_object_symbols.
7415 We will build the contents of .dynsym and .hash when we build
7416 the final symbol table, because until then we do not know the
7417 correct value to give the symbols. We built the .dynstr
7418 section as we went along in elf_link_add_object_symbols. */
7419 s = elf_hash_table (info)->dynsym;
7420 BFD_ASSERT (s != NULL);
7421 s->size = dynsymcount * bed->s->sizeof_sym;
7422
7423 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7424 if (s->contents == NULL)
7425 return false;
7426
7427 /* The first entry in .dynsym is a dummy symbol. Clear all the
7428 section syms, in case we don't output them all. */
7429 ++section_sym_count;
7430 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
7431
7432 elf_hash_table (info)->bucketcount = 0;
7433
7434 /* Compute the size of the hashing table. As a side effect this
7435 computes the hash values for all the names we export. */
7436 if (info->emit_hash)
7437 {
7438 unsigned long int *hashcodes;
7439 struct hash_codes_info hashinf;
7440 bfd_size_type amt;
7441 unsigned long int nsyms;
7442 size_t bucketcount;
7443 size_t hash_entry_size;
7444
7445 /* Compute the hash values for all exported symbols. At the same
7446 time store the values in an array so that we could use them for
7447 optimizations. */
7448 amt = dynsymcount * sizeof (unsigned long int);
7449 hashcodes = (unsigned long int *) bfd_malloc (amt);
7450 if (hashcodes == NULL)
7451 return false;
7452 hashinf.hashcodes = hashcodes;
7453 hashinf.error = false;
7454
7455 /* Put all hash values in HASHCODES. */
7456 elf_link_hash_traverse (elf_hash_table (info),
7457 elf_collect_hash_codes, &hashinf);
7458 if (hashinf.error)
7459 {
7460 free (hashcodes);
7461 return false;
7462 }
7463
7464 nsyms = hashinf.hashcodes - hashcodes;
7465 bucketcount
7466 = compute_bucket_count (info, hashcodes, nsyms, 0);
7467 free (hashcodes);
7468
7469 if (bucketcount == 0 && nsyms > 0)
7470 return false;
7471
7472 elf_hash_table (info)->bucketcount = bucketcount;
7473
7474 s = bfd_get_linker_section (dynobj, ".hash");
7475 BFD_ASSERT (s != NULL);
7476 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7477 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
7478 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7479 if (s->contents == NULL)
7480 return false;
7481
7482 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7483 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7484 s->contents + hash_entry_size);
7485 }
7486
7487 if (info->emit_gnu_hash)
7488 {
7489 size_t i, cnt;
7490 unsigned char *contents;
7491 struct collect_gnu_hash_codes cinfo;
7492 bfd_size_type amt;
7493 size_t bucketcount;
7494
7495 memset (&cinfo, 0, sizeof (cinfo));
7496
7497 /* Compute the hash values for all exported symbols. At the same
7498 time store the values in an array so that we could use them for
7499 optimizations. */
7500 amt = dynsymcount * 2 * sizeof (unsigned long int);
7501 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
7502 if (cinfo.hashcodes == NULL)
7503 return false;
7504
7505 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7506 cinfo.min_dynindx = -1;
7507 cinfo.output_bfd = output_bfd;
7508 cinfo.bed = bed;
7509
7510 /* Put all hash values in HASHCODES. */
7511 elf_link_hash_traverse (elf_hash_table (info),
7512 elf_collect_gnu_hash_codes, &cinfo);
7513 if (cinfo.error)
7514 {
7515 free (cinfo.hashcodes);
7516 return false;
7517 }
7518
7519 bucketcount
7520 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7521
7522 if (bucketcount == 0)
7523 {
7524 free (cinfo.hashcodes);
7525 return false;
7526 }
7527
7528 s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (bed));
7529 BFD_ASSERT (s != NULL);
7530
7531 if (cinfo.nsyms == 0)
7532 {
7533 /* Empty .gnu.hash or .MIPS.xhash section is special. */
7534 BFD_ASSERT (cinfo.min_dynindx == -1);
7535 free (cinfo.hashcodes);
7536 s->size = 5 * 4 + bed->s->arch_size / 8;
7537 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7538 if (contents == NULL)
7539 return false;
7540 s->contents = contents;
7541 /* 1 empty bucket. */
7542 bfd_put_32 (output_bfd, 1, contents);
7543 /* SYMIDX above the special symbol 0. */
7544 bfd_put_32 (output_bfd, 1, contents + 4);
7545 /* Just one word for bitmask. */
7546 bfd_put_32 (output_bfd, 1, contents + 8);
7547 /* Only hash fn bloom filter. */
7548 bfd_put_32 (output_bfd, 0, contents + 12);
7549 /* No hashes are valid - empty bitmask. */
7550 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7551 /* No hashes in the only bucket. */
7552 bfd_put_32 (output_bfd, 0,
7553 contents + 16 + bed->s->arch_size / 8);
7554 }
7555 else
7556 {
7557 unsigned long int maskwords, maskbitslog2, x;
7558 BFD_ASSERT (cinfo.min_dynindx != -1);
7559
7560 x = cinfo.nsyms;
7561 maskbitslog2 = 1;
7562 while ((x >>= 1) != 0)
7563 ++maskbitslog2;
7564 if (maskbitslog2 < 3)
7565 maskbitslog2 = 5;
7566 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7567 maskbitslog2 = maskbitslog2 + 3;
7568 else
7569 maskbitslog2 = maskbitslog2 + 2;
7570 if (bed->s->arch_size == 64)
7571 {
7572 if (maskbitslog2 == 5)
7573 maskbitslog2 = 6;
7574 cinfo.shift1 = 6;
7575 }
7576 else
7577 cinfo.shift1 = 5;
7578 cinfo.mask = (1 << cinfo.shift1) - 1;
7579 cinfo.shift2 = maskbitslog2;
7580 cinfo.maskbits = 1 << maskbitslog2;
7581 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7582 amt = bucketcount * sizeof (unsigned long int) * 2;
7583 amt += maskwords * sizeof (bfd_vma);
7584 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
7585 if (cinfo.bitmask == NULL)
7586 {
7587 free (cinfo.hashcodes);
7588 return false;
7589 }
7590
7591 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
7592 cinfo.indx = cinfo.counts + bucketcount;
7593 cinfo.symindx = dynsymcount - cinfo.nsyms;
7594 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7595
7596 /* Determine how often each hash bucket is used. */
7597 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7598 for (i = 0; i < cinfo.nsyms; ++i)
7599 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7600
7601 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7602 if (cinfo.counts[i] != 0)
7603 {
7604 cinfo.indx[i] = cnt;
7605 cnt += cinfo.counts[i];
7606 }
7607 BFD_ASSERT (cnt == dynsymcount);
7608 cinfo.bucketcount = bucketcount;
7609 cinfo.local_indx = cinfo.min_dynindx;
7610
7611 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7612 s->size += cinfo.maskbits / 8;
7613 if (bed->record_xhash_symbol != NULL)
7614 s->size += cinfo.nsyms * 4;
7615 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7616 if (contents == NULL)
7617 {
7618 free (cinfo.bitmask);
7619 free (cinfo.hashcodes);
7620 return false;
7621 }
7622
7623 s->contents = contents;
7624 bfd_put_32 (output_bfd, bucketcount, contents);
7625 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7626 bfd_put_32 (output_bfd, maskwords, contents + 8);
7627 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7628 contents += 16 + cinfo.maskbits / 8;
7629
7630 for (i = 0; i < bucketcount; ++i)
7631 {
7632 if (cinfo.counts[i] == 0)
7633 bfd_put_32 (output_bfd, 0, contents);
7634 else
7635 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7636 contents += 4;
7637 }
7638
7639 cinfo.contents = contents;
7640
7641 cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents;
7642 /* Renumber dynamic symbols, if populating .gnu.hash section.
7643 If using .MIPS.xhash, populate the translation table. */
7644 elf_link_hash_traverse (elf_hash_table (info),
7645 elf_gnu_hash_process_symidx, &cinfo);
7646
7647 contents = s->contents + 16;
7648 for (i = 0; i < maskwords; ++i)
7649 {
7650 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7651 contents);
7652 contents += bed->s->arch_size / 8;
7653 }
7654
7655 free (cinfo.bitmask);
7656 free (cinfo.hashcodes);
7657 }
7658 }
7659
7660 s = bfd_get_linker_section (dynobj, ".dynstr");
7661 BFD_ASSERT (s != NULL);
7662
7663 elf_finalize_dynstr (output_bfd, info);
7664
7665 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7666
7667 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7668 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7669 return false;
7670 }
7671
7672 return true;
7673 }
7674 \f
7675 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7676
7677 static void
7678 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7679 asection *sec)
7680 {
7681 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7682 sec->sec_info_type = SEC_INFO_TYPE_NONE;
7683 }
7684
7685 /* Finish SHF_MERGE section merging. */
7686
7687 bool
7688 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
7689 {
7690 bfd *ibfd;
7691 asection *sec;
7692
7693 if (!is_elf_hash_table (info->hash))
7694 return false;
7695
7696 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7697 if ((ibfd->flags & DYNAMIC) == 0
7698 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
7699 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7700 == get_elf_backend_data (obfd)->s->elfclass))
7701 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7702 if ((sec->flags & SEC_MERGE) != 0
7703 && !bfd_is_abs_section (sec->output_section))
7704 {
7705 struct bfd_elf_section_data *secdata;
7706
7707 secdata = elf_section_data (sec);
7708 if (! _bfd_add_merge_section (obfd,
7709 &elf_hash_table (info)->merge_info,
7710 sec, &secdata->sec_info))
7711 return false;
7712 else if (secdata->sec_info)
7713 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
7714 }
7715
7716 if (elf_hash_table (info)->merge_info != NULL)
7717 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
7718 merge_sections_remove_hook);
7719 return true;
7720 }
7721
7722 /* Create an entry in an ELF linker hash table. */
7723
7724 struct bfd_hash_entry *
7725 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7726 struct bfd_hash_table *table,
7727 const char *string)
7728 {
7729 /* Allocate the structure if it has not already been allocated by a
7730 subclass. */
7731 if (entry == NULL)
7732 {
7733 entry = (struct bfd_hash_entry *)
7734 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
7735 if (entry == NULL)
7736 return entry;
7737 }
7738
7739 /* Call the allocation method of the superclass. */
7740 entry = _bfd_link_hash_newfunc (entry, table, string);
7741 if (entry != NULL)
7742 {
7743 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7744 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7745
7746 /* Set local fields. */
7747 ret->indx = -1;
7748 ret->dynindx = -1;
7749 ret->got = htab->init_got_refcount;
7750 ret->plt = htab->init_plt_refcount;
7751 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7752 - offsetof (struct elf_link_hash_entry, size)));
7753 /* Assume that we have been called by a non-ELF symbol reader.
7754 This flag is then reset by the code which reads an ELF input
7755 file. This ensures that a symbol created by a non-ELF symbol
7756 reader will have the flag set correctly. */
7757 ret->non_elf = 1;
7758 }
7759
7760 return entry;
7761 }
7762
7763 /* Copy data from an indirect symbol to its direct symbol, hiding the
7764 old indirect symbol. Also used for copying flags to a weakdef. */
7765
7766 void
7767 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7768 struct elf_link_hash_entry *dir,
7769 struct elf_link_hash_entry *ind)
7770 {
7771 struct elf_link_hash_table *htab;
7772
7773 if (ind->dyn_relocs != NULL)
7774 {
7775 if (dir->dyn_relocs != NULL)
7776 {
7777 struct elf_dyn_relocs **pp;
7778 struct elf_dyn_relocs *p;
7779
7780 /* Add reloc counts against the indirect sym to the direct sym
7781 list. Merge any entries against the same section. */
7782 for (pp = &ind->dyn_relocs; (p = *pp) != NULL; )
7783 {
7784 struct elf_dyn_relocs *q;
7785
7786 for (q = dir->dyn_relocs; q != NULL; q = q->next)
7787 if (q->sec == p->sec)
7788 {
7789 q->pc_count += p->pc_count;
7790 q->count += p->count;
7791 *pp = p->next;
7792 break;
7793 }
7794 if (q == NULL)
7795 pp = &p->next;
7796 }
7797 *pp = dir->dyn_relocs;
7798 }
7799
7800 dir->dyn_relocs = ind->dyn_relocs;
7801 ind->dyn_relocs = NULL;
7802 }
7803
7804 /* Copy down any references that we may have already seen to the
7805 symbol which just became indirect. */
7806
7807 if (dir->versioned != versioned_hidden)
7808 dir->ref_dynamic |= ind->ref_dynamic;
7809 dir->ref_regular |= ind->ref_regular;
7810 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7811 dir->non_got_ref |= ind->non_got_ref;
7812 dir->needs_plt |= ind->needs_plt;
7813 dir->pointer_equality_needed |= ind->pointer_equality_needed;
7814
7815 if (ind->root.type != bfd_link_hash_indirect)
7816 return;
7817
7818 /* Copy over the global and procedure linkage table refcount entries.
7819 These may have been already set up by a check_relocs routine. */
7820 htab = elf_hash_table (info);
7821 if (ind->got.refcount > htab->init_got_refcount.refcount)
7822 {
7823 if (dir->got.refcount < 0)
7824 dir->got.refcount = 0;
7825 dir->got.refcount += ind->got.refcount;
7826 ind->got.refcount = htab->init_got_refcount.refcount;
7827 }
7828
7829 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7830 {
7831 if (dir->plt.refcount < 0)
7832 dir->plt.refcount = 0;
7833 dir->plt.refcount += ind->plt.refcount;
7834 ind->plt.refcount = htab->init_plt_refcount.refcount;
7835 }
7836
7837 if (ind->dynindx != -1)
7838 {
7839 if (dir->dynindx != -1)
7840 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7841 dir->dynindx = ind->dynindx;
7842 dir->dynstr_index = ind->dynstr_index;
7843 ind->dynindx = -1;
7844 ind->dynstr_index = 0;
7845 }
7846 }
7847
7848 void
7849 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7850 struct elf_link_hash_entry *h,
7851 bool force_local)
7852 {
7853 /* STT_GNU_IFUNC symbol must go through PLT. */
7854 if (h->type != STT_GNU_IFUNC)
7855 {
7856 h->plt = elf_hash_table (info)->init_plt_offset;
7857 h->needs_plt = 0;
7858 }
7859 if (force_local)
7860 {
7861 h->forced_local = 1;
7862 if (h->dynindx != -1)
7863 {
7864 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7865 h->dynstr_index);
7866 h->dynindx = -1;
7867 h->dynstr_index = 0;
7868 }
7869 }
7870 }
7871
7872 /* Hide a symbol. */
7873
7874 void
7875 _bfd_elf_link_hide_symbol (bfd *output_bfd,
7876 struct bfd_link_info *info,
7877 struct bfd_link_hash_entry *h)
7878 {
7879 if (is_elf_hash_table (info->hash))
7880 {
7881 const struct elf_backend_data *bed
7882 = get_elf_backend_data (output_bfd);
7883 struct elf_link_hash_entry *eh
7884 = (struct elf_link_hash_entry *) h;
7885 bed->elf_backend_hide_symbol (info, eh, true);
7886 eh->def_dynamic = 0;
7887 eh->ref_dynamic = 0;
7888 eh->dynamic_def = 0;
7889 }
7890 }
7891
7892 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7893 caller. */
7894
7895 bool
7896 _bfd_elf_link_hash_table_init
7897 (struct elf_link_hash_table *table,
7898 bfd *abfd,
7899 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7900 struct bfd_hash_table *,
7901 const char *),
7902 unsigned int entsize,
7903 enum elf_target_id target_id)
7904 {
7905 bool ret;
7906 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7907
7908 table->init_got_refcount.refcount = can_refcount - 1;
7909 table->init_plt_refcount.refcount = can_refcount - 1;
7910 table->init_got_offset.offset = -(bfd_vma) 1;
7911 table->init_plt_offset.offset = -(bfd_vma) 1;
7912 /* The first dynamic symbol is a dummy. */
7913 table->dynsymcount = 1;
7914
7915 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
7916
7917 table->root.type = bfd_link_elf_hash_table;
7918 table->hash_table_id = target_id;
7919 table->target_os = get_elf_backend_data (abfd)->target_os;
7920
7921 return ret;
7922 }
7923
7924 /* Create an ELF linker hash table. */
7925
7926 struct bfd_link_hash_table *
7927 _bfd_elf_link_hash_table_create (bfd *abfd)
7928 {
7929 struct elf_link_hash_table *ret;
7930 size_t amt = sizeof (struct elf_link_hash_table);
7931
7932 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
7933 if (ret == NULL)
7934 return NULL;
7935
7936 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
7937 sizeof (struct elf_link_hash_entry),
7938 GENERIC_ELF_DATA))
7939 {
7940 free (ret);
7941 return NULL;
7942 }
7943 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
7944
7945 return &ret->root;
7946 }
7947
7948 /* Destroy an ELF linker hash table. */
7949
7950 void
7951 _bfd_elf_link_hash_table_free (bfd *obfd)
7952 {
7953 struct elf_link_hash_table *htab;
7954
7955 htab = (struct elf_link_hash_table *) obfd->link.hash;
7956 if (htab->dynstr != NULL)
7957 _bfd_elf_strtab_free (htab->dynstr);
7958 _bfd_merge_sections_free (htab->merge_info);
7959 _bfd_generic_link_hash_table_free (obfd);
7960 }
7961
7962 /* This is a hook for the ELF emulation code in the generic linker to
7963 tell the backend linker what file name to use for the DT_NEEDED
7964 entry for a dynamic object. */
7965
7966 void
7967 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7968 {
7969 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7970 && bfd_get_format (abfd) == bfd_object)
7971 elf_dt_name (abfd) = name;
7972 }
7973
7974 int
7975 bfd_elf_get_dyn_lib_class (bfd *abfd)
7976 {
7977 int lib_class;
7978 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7979 && bfd_get_format (abfd) == bfd_object)
7980 lib_class = elf_dyn_lib_class (abfd);
7981 else
7982 lib_class = 0;
7983 return lib_class;
7984 }
7985
7986 void
7987 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7988 {
7989 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7990 && bfd_get_format (abfd) == bfd_object)
7991 elf_dyn_lib_class (abfd) = lib_class;
7992 }
7993
7994 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7995 the linker ELF emulation code. */
7996
7997 struct bfd_link_needed_list *
7998 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7999 struct bfd_link_info *info)
8000 {
8001 if (! is_elf_hash_table (info->hash))
8002 return NULL;
8003 return elf_hash_table (info)->needed;
8004 }
8005
8006 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
8007 hook for the linker ELF emulation code. */
8008
8009 struct bfd_link_needed_list *
8010 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
8011 struct bfd_link_info *info)
8012 {
8013 if (! is_elf_hash_table (info->hash))
8014 return NULL;
8015 return elf_hash_table (info)->runpath;
8016 }
8017
8018 /* Get the name actually used for a dynamic object for a link. This
8019 is the SONAME entry if there is one. Otherwise, it is the string
8020 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
8021
8022 const char *
8023 bfd_elf_get_dt_soname (bfd *abfd)
8024 {
8025 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8026 && bfd_get_format (abfd) == bfd_object)
8027 return elf_dt_name (abfd);
8028 return NULL;
8029 }
8030
8031 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
8032 the ELF linker emulation code. */
8033
8034 bool
8035 bfd_elf_get_bfd_needed_list (bfd *abfd,
8036 struct bfd_link_needed_list **pneeded)
8037 {
8038 asection *s;
8039 bfd_byte *dynbuf = NULL;
8040 unsigned int elfsec;
8041 unsigned long shlink;
8042 bfd_byte *extdyn, *extdynend;
8043 size_t extdynsize;
8044 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
8045
8046 *pneeded = NULL;
8047
8048 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
8049 || bfd_get_format (abfd) != bfd_object)
8050 return true;
8051
8052 s = bfd_get_section_by_name (abfd, ".dynamic");
8053 if (s == NULL || s->size == 0)
8054 return true;
8055
8056 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
8057 goto error_return;
8058
8059 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
8060 if (elfsec == SHN_BAD)
8061 goto error_return;
8062
8063 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
8064
8065 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
8066 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
8067
8068 extdyn = dynbuf;
8069 extdynend = extdyn + s->size;
8070 for (; extdyn < extdynend; extdyn += extdynsize)
8071 {
8072 Elf_Internal_Dyn dyn;
8073
8074 (*swap_dyn_in) (abfd, extdyn, &dyn);
8075
8076 if (dyn.d_tag == DT_NULL)
8077 break;
8078
8079 if (dyn.d_tag == DT_NEEDED)
8080 {
8081 const char *string;
8082 struct bfd_link_needed_list *l;
8083 unsigned int tagv = dyn.d_un.d_val;
8084 size_t amt;
8085
8086 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
8087 if (string == NULL)
8088 goto error_return;
8089
8090 amt = sizeof *l;
8091 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
8092 if (l == NULL)
8093 goto error_return;
8094
8095 l->by = abfd;
8096 l->name = string;
8097 l->next = *pneeded;
8098 *pneeded = l;
8099 }
8100 }
8101
8102 free (dynbuf);
8103
8104 return true;
8105
8106 error_return:
8107 free (dynbuf);
8108 return false;
8109 }
8110
8111 struct elf_symbuf_symbol
8112 {
8113 unsigned long st_name; /* Symbol name, index in string tbl */
8114 unsigned char st_info; /* Type and binding attributes */
8115 unsigned char st_other; /* Visibilty, and target specific */
8116 };
8117
8118 struct elf_symbuf_head
8119 {
8120 struct elf_symbuf_symbol *ssym;
8121 size_t count;
8122 unsigned int st_shndx;
8123 };
8124
8125 struct elf_symbol
8126 {
8127 union
8128 {
8129 Elf_Internal_Sym *isym;
8130 struct elf_symbuf_symbol *ssym;
8131 void *p;
8132 } u;
8133 const char *name;
8134 };
8135
8136 /* Sort references to symbols by ascending section number. */
8137
8138 static int
8139 elf_sort_elf_symbol (const void *arg1, const void *arg2)
8140 {
8141 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
8142 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
8143
8144 if (s1->st_shndx != s2->st_shndx)
8145 return s1->st_shndx > s2->st_shndx ? 1 : -1;
8146 /* Final sort by the address of the sym in the symbuf ensures
8147 a stable sort. */
8148 if (s1 != s2)
8149 return s1 > s2 ? 1 : -1;
8150 return 0;
8151 }
8152
8153 static int
8154 elf_sym_name_compare (const void *arg1, const void *arg2)
8155 {
8156 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
8157 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
8158 int ret = strcmp (s1->name, s2->name);
8159 if (ret != 0)
8160 return ret;
8161 if (s1->u.p != s2->u.p)
8162 return s1->u.p > s2->u.p ? 1 : -1;
8163 return 0;
8164 }
8165
8166 static struct elf_symbuf_head *
8167 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
8168 {
8169 Elf_Internal_Sym **ind, **indbufend, **indbuf;
8170 struct elf_symbuf_symbol *ssym;
8171 struct elf_symbuf_head *ssymbuf, *ssymhead;
8172 size_t i, shndx_count, total_size, amt;
8173
8174 amt = symcount * sizeof (*indbuf);
8175 indbuf = (Elf_Internal_Sym **) bfd_malloc (amt);
8176 if (indbuf == NULL)
8177 return NULL;
8178
8179 for (ind = indbuf, i = 0; i < symcount; i++)
8180 if (isymbuf[i].st_shndx != SHN_UNDEF)
8181 *ind++ = &isymbuf[i];
8182 indbufend = ind;
8183
8184 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
8185 elf_sort_elf_symbol);
8186
8187 shndx_count = 0;
8188 if (indbufend > indbuf)
8189 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
8190 if (ind[0]->st_shndx != ind[1]->st_shndx)
8191 shndx_count++;
8192
8193 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
8194 + (indbufend - indbuf) * sizeof (*ssym));
8195 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
8196 if (ssymbuf == NULL)
8197 {
8198 free (indbuf);
8199 return NULL;
8200 }
8201
8202 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
8203 ssymbuf->ssym = NULL;
8204 ssymbuf->count = shndx_count;
8205 ssymbuf->st_shndx = 0;
8206 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
8207 {
8208 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
8209 {
8210 ssymhead++;
8211 ssymhead->ssym = ssym;
8212 ssymhead->count = 0;
8213 ssymhead->st_shndx = (*ind)->st_shndx;
8214 }
8215 ssym->st_name = (*ind)->st_name;
8216 ssym->st_info = (*ind)->st_info;
8217 ssym->st_other = (*ind)->st_other;
8218 ssymhead->count++;
8219 }
8220 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
8221 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
8222 == total_size));
8223
8224 free (indbuf);
8225 return ssymbuf;
8226 }
8227
8228 /* Check if 2 sections define the same set of local and global
8229 symbols. */
8230
8231 static bool
8232 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
8233 struct bfd_link_info *info)
8234 {
8235 bfd *bfd1, *bfd2;
8236 const struct elf_backend_data *bed1, *bed2;
8237 Elf_Internal_Shdr *hdr1, *hdr2;
8238 size_t symcount1, symcount2;
8239 Elf_Internal_Sym *isymbuf1, *isymbuf2;
8240 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
8241 Elf_Internal_Sym *isym, *isymend;
8242 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
8243 size_t count1, count2, sec_count1, sec_count2, i;
8244 unsigned int shndx1, shndx2;
8245 bool result;
8246 bool ignore_section_symbol_p;
8247
8248 bfd1 = sec1->owner;
8249 bfd2 = sec2->owner;
8250
8251 /* Both sections have to be in ELF. */
8252 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
8253 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
8254 return false;
8255
8256 if (elf_section_type (sec1) != elf_section_type (sec2))
8257 return false;
8258
8259 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
8260 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
8261 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
8262 return false;
8263
8264 bed1 = get_elf_backend_data (bfd1);
8265 bed2 = get_elf_backend_data (bfd2);
8266 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
8267 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
8268 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
8269 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
8270
8271 if (symcount1 == 0 || symcount2 == 0)
8272 return false;
8273
8274 result = false;
8275 isymbuf1 = NULL;
8276 isymbuf2 = NULL;
8277 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
8278 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
8279
8280 /* Ignore section symbols only when matching non-debugging sections
8281 or linkonce section with comdat section. */
8282 ignore_section_symbol_p
8283 = ((sec1->flags & SEC_DEBUGGING) == 0
8284 || ((elf_section_flags (sec1) & SHF_GROUP)
8285 != (elf_section_flags (sec2) & SHF_GROUP)));
8286
8287 if (ssymbuf1 == NULL)
8288 {
8289 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
8290 NULL, NULL, NULL);
8291 if (isymbuf1 == NULL)
8292 goto done;
8293
8294 if (info != NULL && !info->reduce_memory_overheads)
8295 {
8296 ssymbuf1 = elf_create_symbuf (symcount1, isymbuf1);
8297 elf_tdata (bfd1)->symbuf = ssymbuf1;
8298 }
8299 }
8300
8301 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
8302 {
8303 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
8304 NULL, NULL, NULL);
8305 if (isymbuf2 == NULL)
8306 goto done;
8307
8308 if (ssymbuf1 != NULL && info != NULL && !info->reduce_memory_overheads)
8309 {
8310 ssymbuf2 = elf_create_symbuf (symcount2, isymbuf2);
8311 elf_tdata (bfd2)->symbuf = ssymbuf2;
8312 }
8313 }
8314
8315 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
8316 {
8317 /* Optimized faster version. */
8318 size_t lo, hi, mid;
8319 struct elf_symbol *symp;
8320 struct elf_symbuf_symbol *ssym, *ssymend;
8321
8322 lo = 0;
8323 hi = ssymbuf1->count;
8324 ssymbuf1++;
8325 count1 = 0;
8326 sec_count1 = 0;
8327 while (lo < hi)
8328 {
8329 mid = (lo + hi) / 2;
8330 if (shndx1 < ssymbuf1[mid].st_shndx)
8331 hi = mid;
8332 else if (shndx1 > ssymbuf1[mid].st_shndx)
8333 lo = mid + 1;
8334 else
8335 {
8336 count1 = ssymbuf1[mid].count;
8337 ssymbuf1 += mid;
8338 break;
8339 }
8340 }
8341 if (ignore_section_symbol_p)
8342 {
8343 for (i = 0; i < count1; i++)
8344 if (ELF_ST_TYPE (ssymbuf1->ssym[i].st_info) == STT_SECTION)
8345 sec_count1++;
8346 count1 -= sec_count1;
8347 }
8348
8349 lo = 0;
8350 hi = ssymbuf2->count;
8351 ssymbuf2++;
8352 count2 = 0;
8353 sec_count2 = 0;
8354 while (lo < hi)
8355 {
8356 mid = (lo + hi) / 2;
8357 if (shndx2 < ssymbuf2[mid].st_shndx)
8358 hi = mid;
8359 else if (shndx2 > ssymbuf2[mid].st_shndx)
8360 lo = mid + 1;
8361 else
8362 {
8363 count2 = ssymbuf2[mid].count;
8364 ssymbuf2 += mid;
8365 break;
8366 }
8367 }
8368 if (ignore_section_symbol_p)
8369 {
8370 for (i = 0; i < count2; i++)
8371 if (ELF_ST_TYPE (ssymbuf2->ssym[i].st_info) == STT_SECTION)
8372 sec_count2++;
8373 count2 -= sec_count2;
8374 }
8375
8376 if (count1 == 0 || count2 == 0 || count1 != count2)
8377 goto done;
8378
8379 symtable1
8380 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
8381 symtable2
8382 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
8383 if (symtable1 == NULL || symtable2 == NULL)
8384 goto done;
8385
8386 symp = symtable1;
8387 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1 + sec_count1;
8388 ssym < ssymend; ssym++)
8389 if (sec_count1 == 0
8390 || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
8391 {
8392 symp->u.ssym = ssym;
8393 symp->name = bfd_elf_string_from_elf_section (bfd1,
8394 hdr1->sh_link,
8395 ssym->st_name);
8396 symp++;
8397 }
8398
8399 symp = symtable2;
8400 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2 + sec_count2;
8401 ssym < ssymend; ssym++)
8402 if (sec_count2 == 0
8403 || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
8404 {
8405 symp->u.ssym = ssym;
8406 symp->name = bfd_elf_string_from_elf_section (bfd2,
8407 hdr2->sh_link,
8408 ssym->st_name);
8409 symp++;
8410 }
8411
8412 /* Sort symbol by name. */
8413 qsort (symtable1, count1, sizeof (struct elf_symbol),
8414 elf_sym_name_compare);
8415 qsort (symtable2, count1, sizeof (struct elf_symbol),
8416 elf_sym_name_compare);
8417
8418 for (i = 0; i < count1; i++)
8419 /* Two symbols must have the same binding, type and name. */
8420 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8421 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8422 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8423 goto done;
8424
8425 result = true;
8426 goto done;
8427 }
8428
8429 symtable1 = (struct elf_symbol *)
8430 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8431 symtable2 = (struct elf_symbol *)
8432 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
8433 if (symtable1 == NULL || symtable2 == NULL)
8434 goto done;
8435
8436 /* Count definitions in the section. */
8437 count1 = 0;
8438 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
8439 if (isym->st_shndx == shndx1
8440 && (!ignore_section_symbol_p
8441 || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
8442 symtable1[count1++].u.isym = isym;
8443
8444 count2 = 0;
8445 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
8446 if (isym->st_shndx == shndx2
8447 && (!ignore_section_symbol_p
8448 || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
8449 symtable2[count2++].u.isym = isym;
8450
8451 if (count1 == 0 || count2 == 0 || count1 != count2)
8452 goto done;
8453
8454 for (i = 0; i < count1; i++)
8455 symtable1[i].name
8456 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8457 symtable1[i].u.isym->st_name);
8458
8459 for (i = 0; i < count2; i++)
8460 symtable2[i].name
8461 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8462 symtable2[i].u.isym->st_name);
8463
8464 /* Sort symbol by name. */
8465 qsort (symtable1, count1, sizeof (struct elf_symbol),
8466 elf_sym_name_compare);
8467 qsort (symtable2, count1, sizeof (struct elf_symbol),
8468 elf_sym_name_compare);
8469
8470 for (i = 0; i < count1; i++)
8471 /* Two symbols must have the same binding, type and name. */
8472 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8473 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8474 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8475 goto done;
8476
8477 result = true;
8478
8479 done:
8480 free (symtable1);
8481 free (symtable2);
8482 free (isymbuf1);
8483 free (isymbuf2);
8484
8485 return result;
8486 }
8487
8488 /* Return TRUE if 2 section types are compatible. */
8489
8490 bool
8491 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8492 bfd *bbfd, const asection *bsec)
8493 {
8494 if (asec == NULL
8495 || bsec == NULL
8496 || abfd->xvec->flavour != bfd_target_elf_flavour
8497 || bbfd->xvec->flavour != bfd_target_elf_flavour)
8498 return true;
8499
8500 return elf_section_type (asec) == elf_section_type (bsec);
8501 }
8502 \f
8503 /* Final phase of ELF linker. */
8504
8505 /* A structure we use to avoid passing large numbers of arguments. */
8506
8507 struct elf_final_link_info
8508 {
8509 /* General link information. */
8510 struct bfd_link_info *info;
8511 /* Output BFD. */
8512 bfd *output_bfd;
8513 /* Symbol string table. */
8514 struct elf_strtab_hash *symstrtab;
8515 /* .hash section. */
8516 asection *hash_sec;
8517 /* symbol version section (.gnu.version). */
8518 asection *symver_sec;
8519 /* Buffer large enough to hold contents of any section. */
8520 bfd_byte *contents;
8521 /* Buffer large enough to hold external relocs of any section. */
8522 void *external_relocs;
8523 /* Buffer large enough to hold internal relocs of any section. */
8524 Elf_Internal_Rela *internal_relocs;
8525 /* Buffer large enough to hold external local symbols of any input
8526 BFD. */
8527 bfd_byte *external_syms;
8528 /* And a buffer for symbol section indices. */
8529 Elf_External_Sym_Shndx *locsym_shndx;
8530 /* Buffer large enough to hold internal local symbols of any input
8531 BFD. */
8532 Elf_Internal_Sym *internal_syms;
8533 /* Array large enough to hold a symbol index for each local symbol
8534 of any input BFD. */
8535 long *indices;
8536 /* Array large enough to hold a section pointer for each local
8537 symbol of any input BFD. */
8538 asection **sections;
8539 /* Buffer for SHT_SYMTAB_SHNDX section. */
8540 Elf_External_Sym_Shndx *symshndxbuf;
8541 /* Number of STT_FILE syms seen. */
8542 size_t filesym_count;
8543 /* Local symbol hash table. */
8544 struct bfd_hash_table local_hash_table;
8545 };
8546
8547 struct local_hash_entry
8548 {
8549 /* Base hash table entry structure. */
8550 struct bfd_hash_entry root;
8551 /* Size of the local symbol name. */
8552 size_t size;
8553 /* Number of the duplicated local symbol names. */
8554 long count;
8555 };
8556
8557 /* Create an entry in the local symbol hash table. */
8558
8559 static struct bfd_hash_entry *
8560 local_hash_newfunc (struct bfd_hash_entry *entry,
8561 struct bfd_hash_table *table,
8562 const char *string)
8563 {
8564
8565 /* Allocate the structure if it has not already been allocated by a
8566 subclass. */
8567 if (entry == NULL)
8568 {
8569 entry = bfd_hash_allocate (table,
8570 sizeof (struct local_hash_entry));
8571 if (entry == NULL)
8572 return entry;
8573 }
8574
8575 /* Call the allocation method of the superclass. */
8576 entry = bfd_hash_newfunc (entry, table, string);
8577 if (entry != NULL)
8578 {
8579 ((struct local_hash_entry *) entry)->count = 0;
8580 ((struct local_hash_entry *) entry)->size = 0;
8581 }
8582
8583 return entry;
8584 }
8585
8586 /* This struct is used to pass information to elf_link_output_extsym. */
8587
8588 struct elf_outext_info
8589 {
8590 bool failed;
8591 bool localsyms;
8592 bool file_sym_done;
8593 struct elf_final_link_info *flinfo;
8594 };
8595
8596
8597 /* Support for evaluating a complex relocation.
8598
8599 Complex relocations are generalized, self-describing relocations. The
8600 implementation of them consists of two parts: complex symbols, and the
8601 relocations themselves.
8602
8603 The relocations use a reserved elf-wide relocation type code (R_RELC
8604 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8605 information (start bit, end bit, word width, etc) into the addend. This
8606 information is extracted from CGEN-generated operand tables within gas.
8607
8608 Complex symbols are mangled symbols (STT_RELC external / BSF_RELC
8609 internal) representing prefix-notation expressions, including but not
8610 limited to those sorts of expressions normally encoded as addends in the
8611 addend field. The symbol mangling format is:
8612
8613 <node> := <literal>
8614 | <unary-operator> ':' <node>
8615 | <binary-operator> ':' <node> ':' <node>
8616 ;
8617
8618 <literal> := 's' <digits=N> ':' <N character symbol name>
8619 | 'S' <digits=N> ':' <N character section name>
8620 | '#' <hexdigits>
8621 ;
8622
8623 <binary-operator> := as in C
8624 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8625
8626 static void
8627 set_symbol_value (bfd *bfd_with_globals,
8628 Elf_Internal_Sym *isymbuf,
8629 size_t locsymcount,
8630 size_t symidx,
8631 bfd_vma val)
8632 {
8633 struct elf_link_hash_entry **sym_hashes;
8634 struct elf_link_hash_entry *h;
8635 size_t extsymoff = locsymcount;
8636
8637 if (symidx < locsymcount)
8638 {
8639 Elf_Internal_Sym *sym;
8640
8641 sym = isymbuf + symidx;
8642 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8643 {
8644 /* It is a local symbol: move it to the
8645 "absolute" section and give it a value. */
8646 sym->st_shndx = SHN_ABS;
8647 sym->st_value = val;
8648 return;
8649 }
8650 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8651 extsymoff = 0;
8652 }
8653
8654 /* It is a global symbol: set its link type
8655 to "defined" and give it a value. */
8656
8657 sym_hashes = elf_sym_hashes (bfd_with_globals);
8658 h = sym_hashes [symidx - extsymoff];
8659 while (h->root.type == bfd_link_hash_indirect
8660 || h->root.type == bfd_link_hash_warning)
8661 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8662 h->root.type = bfd_link_hash_defined;
8663 h->root.u.def.value = val;
8664 h->root.u.def.section = bfd_abs_section_ptr;
8665 }
8666
8667 static bool
8668 resolve_symbol (const char *name,
8669 bfd *input_bfd,
8670 struct elf_final_link_info *flinfo,
8671 bfd_vma *result,
8672 Elf_Internal_Sym *isymbuf,
8673 size_t locsymcount)
8674 {
8675 Elf_Internal_Sym *sym;
8676 struct bfd_link_hash_entry *global_entry;
8677 const char *candidate = NULL;
8678 Elf_Internal_Shdr *symtab_hdr;
8679 size_t i;
8680
8681 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8682
8683 for (i = 0; i < locsymcount; ++ i)
8684 {
8685 sym = isymbuf + i;
8686
8687 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8688 continue;
8689
8690 candidate = bfd_elf_string_from_elf_section (input_bfd,
8691 symtab_hdr->sh_link,
8692 sym->st_name);
8693 #ifdef DEBUG
8694 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8695 name, candidate, (unsigned long) sym->st_value);
8696 #endif
8697 if (candidate && strcmp (candidate, name) == 0)
8698 {
8699 asection *sec = flinfo->sections [i];
8700
8701 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8702 *result += sec->output_offset + sec->output_section->vma;
8703 #ifdef DEBUG
8704 printf ("Found symbol with value %8.8lx\n",
8705 (unsigned long) *result);
8706 #endif
8707 return true;
8708 }
8709 }
8710
8711 /* Hmm, haven't found it yet. perhaps it is a global. */
8712 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
8713 false, false, true);
8714 if (!global_entry)
8715 return false;
8716
8717 if (global_entry->type == bfd_link_hash_defined
8718 || global_entry->type == bfd_link_hash_defweak)
8719 {
8720 *result = (global_entry->u.def.value
8721 + global_entry->u.def.section->output_section->vma
8722 + global_entry->u.def.section->output_offset);
8723 #ifdef DEBUG
8724 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8725 global_entry->root.string, (unsigned long) *result);
8726 #endif
8727 return true;
8728 }
8729
8730 return false;
8731 }
8732
8733 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8734 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8735 names like "foo.end" which is the end address of section "foo". */
8736
8737 static bool
8738 resolve_section (const char *name,
8739 asection *sections,
8740 bfd_vma *result,
8741 bfd * abfd)
8742 {
8743 asection *curr;
8744 unsigned int len;
8745
8746 for (curr = sections; curr; curr = curr->next)
8747 if (strcmp (curr->name, name) == 0)
8748 {
8749 *result = curr->vma;
8750 return true;
8751 }
8752
8753 /* Hmm. still haven't found it. try pseudo-section names. */
8754 /* FIXME: This could be coded more efficiently... */
8755 for (curr = sections; curr; curr = curr->next)
8756 {
8757 len = strlen (curr->name);
8758 if (len > strlen (name))
8759 continue;
8760
8761 if (strncmp (curr->name, name, len) == 0)
8762 {
8763 if (startswith (name + len, ".end"))
8764 {
8765 *result = (curr->vma
8766 + curr->size / bfd_octets_per_byte (abfd, curr));
8767 return true;
8768 }
8769
8770 /* Insert more pseudo-section names here, if you like. */
8771 }
8772 }
8773
8774 return false;
8775 }
8776
8777 static void
8778 undefined_reference (const char *reftype, const char *name)
8779 {
8780 /* xgettext:c-format */
8781 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8782 reftype, name);
8783 bfd_set_error (bfd_error_bad_value);
8784 }
8785
8786 static bool
8787 eval_symbol (bfd_vma *result,
8788 const char **symp,
8789 bfd *input_bfd,
8790 struct elf_final_link_info *flinfo,
8791 bfd_vma dot,
8792 Elf_Internal_Sym *isymbuf,
8793 size_t locsymcount,
8794 int signed_p)
8795 {
8796 size_t len;
8797 size_t symlen;
8798 bfd_vma a;
8799 bfd_vma b;
8800 char symbuf[4096];
8801 const char *sym = *symp;
8802 const char *symend;
8803 bool symbol_is_section = false;
8804
8805 len = strlen (sym);
8806 symend = sym + len;
8807
8808 if (len < 1 || len > sizeof (symbuf))
8809 {
8810 bfd_set_error (bfd_error_invalid_operation);
8811 return false;
8812 }
8813
8814 switch (* sym)
8815 {
8816 case '.':
8817 *result = dot;
8818 *symp = sym + 1;
8819 return true;
8820
8821 case '#':
8822 ++sym;
8823 *result = strtoul (sym, (char **) symp, 16);
8824 return true;
8825
8826 case 'S':
8827 symbol_is_section = true;
8828 /* Fall through. */
8829 case 's':
8830 ++sym;
8831 symlen = strtol (sym, (char **) symp, 10);
8832 sym = *symp + 1; /* Skip the trailing ':'. */
8833
8834 if (symend < sym || symlen + 1 > sizeof (symbuf))
8835 {
8836 bfd_set_error (bfd_error_invalid_operation);
8837 return false;
8838 }
8839
8840 memcpy (symbuf, sym, symlen);
8841 symbuf[symlen] = '\0';
8842 *symp = sym + symlen;
8843
8844 /* Is it always possible, with complex symbols, that gas "mis-guessed"
8845 the symbol as a section, or vice-versa. so we're pretty liberal in our
8846 interpretation here; section means "try section first", not "must be a
8847 section", and likewise with symbol. */
8848
8849 if (symbol_is_section)
8850 {
8851 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8852 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8853 isymbuf, locsymcount))
8854 {
8855 undefined_reference ("section", symbuf);
8856 return false;
8857 }
8858 }
8859 else
8860 {
8861 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8862 isymbuf, locsymcount)
8863 && !resolve_section (symbuf, flinfo->output_bfd->sections,
8864 result, input_bfd))
8865 {
8866 undefined_reference ("symbol", symbuf);
8867 return false;
8868 }
8869 }
8870
8871 return true;
8872
8873 /* All that remains are operators. */
8874
8875 #define UNARY_OP(op) \
8876 if (startswith (sym, #op)) \
8877 { \
8878 sym += strlen (#op); \
8879 if (*sym == ':') \
8880 ++sym; \
8881 *symp = sym; \
8882 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8883 isymbuf, locsymcount, signed_p)) \
8884 return false; \
8885 if (signed_p) \
8886 *result = op ((bfd_signed_vma) a); \
8887 else \
8888 *result = op a; \
8889 return true; \
8890 }
8891
8892 #define BINARY_OP_HEAD(op) \
8893 if (startswith (sym, #op)) \
8894 { \
8895 sym += strlen (#op); \
8896 if (*sym == ':') \
8897 ++sym; \
8898 *symp = sym; \
8899 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8900 isymbuf, locsymcount, signed_p)) \
8901 return false; \
8902 ++*symp; \
8903 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8904 isymbuf, locsymcount, signed_p)) \
8905 return false;
8906 #define BINARY_OP_TAIL(op) \
8907 if (signed_p) \
8908 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8909 else \
8910 *result = a op b; \
8911 return true; \
8912 }
8913 #define BINARY_OP(op) BINARY_OP_HEAD(op) BINARY_OP_TAIL(op)
8914
8915 default:
8916 UNARY_OP (0-);
8917 BINARY_OP_HEAD (<<);
8918 if (b >= sizeof (a) * CHAR_BIT)
8919 {
8920 *result = 0;
8921 return true;
8922 }
8923 signed_p = 0;
8924 BINARY_OP_TAIL (<<);
8925 BINARY_OP_HEAD (>>);
8926 if (b >= sizeof (a) * CHAR_BIT)
8927 {
8928 *result = signed_p && (bfd_signed_vma) a < 0 ? -1 : 0;
8929 return true;
8930 }
8931 BINARY_OP_TAIL (>>);
8932 BINARY_OP (==);
8933 BINARY_OP (!=);
8934 BINARY_OP (<=);
8935 BINARY_OP (>=);
8936 BINARY_OP (&&);
8937 BINARY_OP (||);
8938 UNARY_OP (~);
8939 UNARY_OP (!);
8940 BINARY_OP (*);
8941 BINARY_OP_HEAD (/);
8942 if (b == 0)
8943 {
8944 _bfd_error_handler (_("division by zero"));
8945 bfd_set_error (bfd_error_bad_value);
8946 return false;
8947 }
8948 BINARY_OP_TAIL (/);
8949 BINARY_OP_HEAD (%);
8950 if (b == 0)
8951 {
8952 _bfd_error_handler (_("division by zero"));
8953 bfd_set_error (bfd_error_bad_value);
8954 return false;
8955 }
8956 BINARY_OP_TAIL (%);
8957 BINARY_OP (^);
8958 BINARY_OP (|);
8959 BINARY_OP (&);
8960 BINARY_OP (+);
8961 BINARY_OP (-);
8962 BINARY_OP (<);
8963 BINARY_OP (>);
8964 #undef UNARY_OP
8965 #undef BINARY_OP
8966 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8967 bfd_set_error (bfd_error_invalid_operation);
8968 return false;
8969 }
8970 }
8971
8972 static void
8973 put_value (bfd_vma size,
8974 unsigned long chunksz,
8975 bfd *input_bfd,
8976 bfd_vma x,
8977 bfd_byte *location)
8978 {
8979 location += (size - chunksz);
8980
8981 for (; size; size -= chunksz, location -= chunksz)
8982 {
8983 switch (chunksz)
8984 {
8985 case 1:
8986 bfd_put_8 (input_bfd, x, location);
8987 x >>= 8;
8988 break;
8989 case 2:
8990 bfd_put_16 (input_bfd, x, location);
8991 x >>= 16;
8992 break;
8993 case 4:
8994 bfd_put_32 (input_bfd, x, location);
8995 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8996 x >>= 16;
8997 x >>= 16;
8998 break;
8999 #ifdef BFD64
9000 case 8:
9001 bfd_put_64 (input_bfd, x, location);
9002 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
9003 x >>= 32;
9004 x >>= 32;
9005 break;
9006 #endif
9007 default:
9008 abort ();
9009 break;
9010 }
9011 }
9012 }
9013
9014 static bfd_vma
9015 get_value (bfd_vma size,
9016 unsigned long chunksz,
9017 bfd *input_bfd,
9018 bfd_byte *location)
9019 {
9020 int shift;
9021 bfd_vma x = 0;
9022
9023 /* Sanity checks. */
9024 BFD_ASSERT (chunksz <= sizeof (x)
9025 && size >= chunksz
9026 && chunksz != 0
9027 && (size % chunksz) == 0
9028 && input_bfd != NULL
9029 && location != NULL);
9030
9031 if (chunksz == sizeof (x))
9032 {
9033 BFD_ASSERT (size == chunksz);
9034
9035 /* Make sure that we do not perform an undefined shift operation.
9036 We know that size == chunksz so there will only be one iteration
9037 of the loop below. */
9038 shift = 0;
9039 }
9040 else
9041 shift = 8 * chunksz;
9042
9043 for (; size; size -= chunksz, location += chunksz)
9044 {
9045 switch (chunksz)
9046 {
9047 case 1:
9048 x = (x << shift) | bfd_get_8 (input_bfd, location);
9049 break;
9050 case 2:
9051 x = (x << shift) | bfd_get_16 (input_bfd, location);
9052 break;
9053 case 4:
9054 x = (x << shift) | bfd_get_32 (input_bfd, location);
9055 break;
9056 #ifdef BFD64
9057 case 8:
9058 x = (x << shift) | bfd_get_64 (input_bfd, location);
9059 break;
9060 #endif
9061 default:
9062 abort ();
9063 }
9064 }
9065 return x;
9066 }
9067
9068 static void
9069 decode_complex_addend (unsigned long *start, /* in bits */
9070 unsigned long *oplen, /* in bits */
9071 unsigned long *len, /* in bits */
9072 unsigned long *wordsz, /* in bytes */
9073 unsigned long *chunksz, /* in bytes */
9074 unsigned long *lsb0_p,
9075 unsigned long *signed_p,
9076 unsigned long *trunc_p,
9077 unsigned long encoded)
9078 {
9079 * start = encoded & 0x3F;
9080 * len = (encoded >> 6) & 0x3F;
9081 * oplen = (encoded >> 12) & 0x3F;
9082 * wordsz = (encoded >> 18) & 0xF;
9083 * chunksz = (encoded >> 22) & 0xF;
9084 * lsb0_p = (encoded >> 27) & 1;
9085 * signed_p = (encoded >> 28) & 1;
9086 * trunc_p = (encoded >> 29) & 1;
9087 }
9088
9089 bfd_reloc_status_type
9090 bfd_elf_perform_complex_relocation (bfd *input_bfd,
9091 asection *input_section,
9092 bfd_byte *contents,
9093 Elf_Internal_Rela *rel,
9094 bfd_vma relocation)
9095 {
9096 bfd_vma shift, x, mask;
9097 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
9098 bfd_reloc_status_type r;
9099 bfd_size_type octets;
9100
9101 /* Perform this reloc, since it is complex.
9102 (this is not to say that it necessarily refers to a complex
9103 symbol; merely that it is a self-describing CGEN based reloc.
9104 i.e. the addend has the complete reloc information (bit start, end,
9105 word size, etc) encoded within it.). */
9106
9107 decode_complex_addend (&start, &oplen, &len, &wordsz,
9108 &chunksz, &lsb0_p, &signed_p,
9109 &trunc_p, rel->r_addend);
9110
9111 mask = (((1L << (len - 1)) - 1) << 1) | 1;
9112
9113 if (lsb0_p)
9114 shift = (start + 1) - len;
9115 else
9116 shift = (8 * wordsz) - (start + len);
9117
9118 octets = rel->r_offset * bfd_octets_per_byte (input_bfd, input_section);
9119 x = get_value (wordsz, chunksz, input_bfd, contents + octets);
9120
9121 #ifdef DEBUG
9122 printf ("Doing complex reloc: "
9123 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
9124 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
9125 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
9126 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9127 oplen, (unsigned long) x, (unsigned long) mask,
9128 (unsigned long) relocation);
9129 #endif
9130
9131 r = bfd_reloc_ok;
9132 if (! trunc_p)
9133 /* Now do an overflow check. */
9134 r = bfd_check_overflow ((signed_p
9135 ? complain_overflow_signed
9136 : complain_overflow_unsigned),
9137 len, 0, (8 * wordsz),
9138 relocation);
9139
9140 /* Do the deed. */
9141 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
9142
9143 #ifdef DEBUG
9144 printf (" relocation: %8.8lx\n"
9145 " shifted mask: %8.8lx\n"
9146 " shifted/masked reloc: %8.8lx\n"
9147 " result: %8.8lx\n",
9148 (unsigned long) relocation, (unsigned long) (mask << shift),
9149 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
9150 #endif
9151 put_value (wordsz, chunksz, input_bfd, x, contents + octets);
9152 return r;
9153 }
9154
9155 /* Functions to read r_offset from external (target order) reloc
9156 entry. Faster than bfd_getl32 et al, because we let the compiler
9157 know the value is aligned. */
9158
9159 static bfd_vma
9160 ext32l_r_offset (const void *p)
9161 {
9162 union aligned32
9163 {
9164 uint32_t v;
9165 unsigned char c[4];
9166 };
9167 const union aligned32 *a
9168 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9169
9170 uint32_t aval = ( (uint32_t) a->c[0]
9171 | (uint32_t) a->c[1] << 8
9172 | (uint32_t) a->c[2] << 16
9173 | (uint32_t) a->c[3] << 24);
9174 return aval;
9175 }
9176
9177 static bfd_vma
9178 ext32b_r_offset (const void *p)
9179 {
9180 union aligned32
9181 {
9182 uint32_t v;
9183 unsigned char c[4];
9184 };
9185 const union aligned32 *a
9186 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9187
9188 uint32_t aval = ( (uint32_t) a->c[0] << 24
9189 | (uint32_t) a->c[1] << 16
9190 | (uint32_t) a->c[2] << 8
9191 | (uint32_t) a->c[3]);
9192 return aval;
9193 }
9194
9195 #ifdef BFD_HOST_64_BIT
9196 static bfd_vma
9197 ext64l_r_offset (const void *p)
9198 {
9199 union aligned64
9200 {
9201 uint64_t v;
9202 unsigned char c[8];
9203 };
9204 const union aligned64 *a
9205 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9206
9207 uint64_t aval = ( (uint64_t) a->c[0]
9208 | (uint64_t) a->c[1] << 8
9209 | (uint64_t) a->c[2] << 16
9210 | (uint64_t) a->c[3] << 24
9211 | (uint64_t) a->c[4] << 32
9212 | (uint64_t) a->c[5] << 40
9213 | (uint64_t) a->c[6] << 48
9214 | (uint64_t) a->c[7] << 56);
9215 return aval;
9216 }
9217
9218 static bfd_vma
9219 ext64b_r_offset (const void *p)
9220 {
9221 union aligned64
9222 {
9223 uint64_t v;
9224 unsigned char c[8];
9225 };
9226 const union aligned64 *a
9227 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9228
9229 uint64_t aval = ( (uint64_t) a->c[0] << 56
9230 | (uint64_t) a->c[1] << 48
9231 | (uint64_t) a->c[2] << 40
9232 | (uint64_t) a->c[3] << 32
9233 | (uint64_t) a->c[4] << 24
9234 | (uint64_t) a->c[5] << 16
9235 | (uint64_t) a->c[6] << 8
9236 | (uint64_t) a->c[7]);
9237 return aval;
9238 }
9239 #endif
9240
9241 /* When performing a relocatable link, the input relocations are
9242 preserved. But, if they reference global symbols, the indices
9243 referenced must be updated. Update all the relocations found in
9244 RELDATA. */
9245
9246 static bool
9247 elf_link_adjust_relocs (bfd *abfd,
9248 asection *sec,
9249 struct bfd_elf_section_reloc_data *reldata,
9250 bool sort,
9251 struct bfd_link_info *info)
9252 {
9253 unsigned int i;
9254 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9255 bfd_byte *erela;
9256 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9257 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9258 bfd_vma r_type_mask;
9259 int r_sym_shift;
9260 unsigned int count = reldata->count;
9261 struct elf_link_hash_entry **rel_hash = reldata->hashes;
9262
9263 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
9264 {
9265 swap_in = bed->s->swap_reloc_in;
9266 swap_out = bed->s->swap_reloc_out;
9267 }
9268 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
9269 {
9270 swap_in = bed->s->swap_reloca_in;
9271 swap_out = bed->s->swap_reloca_out;
9272 }
9273 else
9274 abort ();
9275
9276 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
9277 abort ();
9278
9279 if (bed->s->arch_size == 32)
9280 {
9281 r_type_mask = 0xff;
9282 r_sym_shift = 8;
9283 }
9284 else
9285 {
9286 r_type_mask = 0xffffffff;
9287 r_sym_shift = 32;
9288 }
9289
9290 erela = reldata->hdr->contents;
9291 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
9292 {
9293 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
9294 unsigned int j;
9295
9296 if (*rel_hash == NULL)
9297 continue;
9298
9299 if ((*rel_hash)->indx == -2
9300 && info->gc_sections
9301 && ! info->gc_keep_exported)
9302 {
9303 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9304 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
9305 abfd, sec,
9306 (*rel_hash)->root.root.string);
9307 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
9308 abfd, sec);
9309 bfd_set_error (bfd_error_invalid_operation);
9310 return false;
9311 }
9312 BFD_ASSERT ((*rel_hash)->indx >= 0);
9313
9314 (*swap_in) (abfd, erela, irela);
9315 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
9316 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
9317 | (irela[j].r_info & r_type_mask));
9318 (*swap_out) (abfd, irela, erela);
9319 }
9320
9321 if (bed->elf_backend_update_relocs)
9322 (*bed->elf_backend_update_relocs) (sec, reldata);
9323
9324 if (sort && count != 0)
9325 {
9326 bfd_vma (*ext_r_off) (const void *);
9327 bfd_vma r_off;
9328 size_t elt_size;
9329 bfd_byte *base, *end, *p, *loc;
9330 bfd_byte *buf = NULL;
9331
9332 if (bed->s->arch_size == 32)
9333 {
9334 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9335 ext_r_off = ext32l_r_offset;
9336 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9337 ext_r_off = ext32b_r_offset;
9338 else
9339 abort ();
9340 }
9341 else
9342 {
9343 #ifdef BFD_HOST_64_BIT
9344 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9345 ext_r_off = ext64l_r_offset;
9346 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9347 ext_r_off = ext64b_r_offset;
9348 else
9349 #endif
9350 abort ();
9351 }
9352
9353 /* Must use a stable sort here. A modified insertion sort,
9354 since the relocs are mostly sorted already. */
9355 elt_size = reldata->hdr->sh_entsize;
9356 base = reldata->hdr->contents;
9357 end = base + count * elt_size;
9358 if (elt_size > sizeof (Elf64_External_Rela))
9359 abort ();
9360
9361 /* Ensure the first element is lowest. This acts as a sentinel,
9362 speeding the main loop below. */
9363 r_off = (*ext_r_off) (base);
9364 for (p = loc = base; (p += elt_size) < end; )
9365 {
9366 bfd_vma r_off2 = (*ext_r_off) (p);
9367 if (r_off > r_off2)
9368 {
9369 r_off = r_off2;
9370 loc = p;
9371 }
9372 }
9373 if (loc != base)
9374 {
9375 /* Don't just swap *base and *loc as that changes the order
9376 of the original base[0] and base[1] if they happen to
9377 have the same r_offset. */
9378 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
9379 memcpy (onebuf, loc, elt_size);
9380 memmove (base + elt_size, base, loc - base);
9381 memcpy (base, onebuf, elt_size);
9382 }
9383
9384 for (p = base + elt_size; (p += elt_size) < end; )
9385 {
9386 /* base to p is sorted, *p is next to insert. */
9387 r_off = (*ext_r_off) (p);
9388 /* Search the sorted region for location to insert. */
9389 loc = p - elt_size;
9390 while (r_off < (*ext_r_off) (loc))
9391 loc -= elt_size;
9392 loc += elt_size;
9393 if (loc != p)
9394 {
9395 /* Chances are there is a run of relocs to insert here,
9396 from one of more input files. Files are not always
9397 linked in order due to the way elf_link_input_bfd is
9398 called. See pr17666. */
9399 size_t sortlen = p - loc;
9400 bfd_vma r_off2 = (*ext_r_off) (loc);
9401 size_t runlen = elt_size;
9402 size_t buf_size = 96 * 1024;
9403 while (p + runlen < end
9404 && (sortlen <= buf_size
9405 || runlen + elt_size <= buf_size)
9406 && r_off2 > (*ext_r_off) (p + runlen))
9407 runlen += elt_size;
9408 if (buf == NULL)
9409 {
9410 buf = bfd_malloc (buf_size);
9411 if (buf == NULL)
9412 return false;
9413 }
9414 if (runlen < sortlen)
9415 {
9416 memcpy (buf, p, runlen);
9417 memmove (loc + runlen, loc, sortlen);
9418 memcpy (loc, buf, runlen);
9419 }
9420 else
9421 {
9422 memcpy (buf, loc, sortlen);
9423 memmove (loc, p, runlen);
9424 memcpy (loc + runlen, buf, sortlen);
9425 }
9426 p += runlen - elt_size;
9427 }
9428 }
9429 /* Hashes are no longer valid. */
9430 free (reldata->hashes);
9431 reldata->hashes = NULL;
9432 free (buf);
9433 }
9434 return true;
9435 }
9436
9437 struct elf_link_sort_rela
9438 {
9439 union {
9440 bfd_vma offset;
9441 bfd_vma sym_mask;
9442 } u;
9443 enum elf_reloc_type_class type;
9444 /* We use this as an array of size int_rels_per_ext_rel. */
9445 Elf_Internal_Rela rela[1];
9446 };
9447
9448 /* qsort stability here and for cmp2 is only an issue if multiple
9449 dynamic relocations are emitted at the same address. But targets
9450 that apply a series of dynamic relocations each operating on the
9451 result of the prior relocation can't use -z combreloc as
9452 implemented anyway. Such schemes tend to be broken by sorting on
9453 symbol index. That leaves dynamic NONE relocs as the only other
9454 case where ld might emit multiple relocs at the same address, and
9455 those are only emitted due to target bugs. */
9456
9457 static int
9458 elf_link_sort_cmp1 (const void *A, const void *B)
9459 {
9460 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9461 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9462 int relativea, relativeb;
9463
9464 relativea = a->type == reloc_class_relative;
9465 relativeb = b->type == reloc_class_relative;
9466
9467 if (relativea < relativeb)
9468 return 1;
9469 if (relativea > relativeb)
9470 return -1;
9471 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
9472 return -1;
9473 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
9474 return 1;
9475 if (a->rela->r_offset < b->rela->r_offset)
9476 return -1;
9477 if (a->rela->r_offset > b->rela->r_offset)
9478 return 1;
9479 return 0;
9480 }
9481
9482 static int
9483 elf_link_sort_cmp2 (const void *A, const void *B)
9484 {
9485 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9486 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9487
9488 if (a->type < b->type)
9489 return -1;
9490 if (a->type > b->type)
9491 return 1;
9492 if (a->u.offset < b->u.offset)
9493 return -1;
9494 if (a->u.offset > b->u.offset)
9495 return 1;
9496 if (a->rela->r_offset < b->rela->r_offset)
9497 return -1;
9498 if (a->rela->r_offset > b->rela->r_offset)
9499 return 1;
9500 return 0;
9501 }
9502
9503 static size_t
9504 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9505 {
9506 asection *dynamic_relocs;
9507 asection *rela_dyn;
9508 asection *rel_dyn;
9509 bfd_size_type count, size;
9510 size_t i, ret, sort_elt, ext_size;
9511 bfd_byte *sort, *s_non_relative, *p;
9512 struct elf_link_sort_rela *sq;
9513 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9514 int i2e = bed->s->int_rels_per_ext_rel;
9515 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
9516 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9517 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9518 struct bfd_link_order *lo;
9519 bfd_vma r_sym_mask;
9520 bool use_rela;
9521
9522 /* Find a dynamic reloc section. */
9523 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9524 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
9525 if (rela_dyn != NULL && rela_dyn->size > 0
9526 && rel_dyn != NULL && rel_dyn->size > 0)
9527 {
9528 bool use_rela_initialised = false;
9529
9530 /* This is just here to stop gcc from complaining.
9531 Its initialization checking code is not perfect. */
9532 use_rela = true;
9533
9534 /* Both sections are present. Examine the sizes
9535 of the indirect sections to help us choose. */
9536 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9537 if (lo->type == bfd_indirect_link_order)
9538 {
9539 asection *o = lo->u.indirect.section;
9540
9541 if ((o->size % bed->s->sizeof_rela) == 0)
9542 {
9543 if ((o->size % bed->s->sizeof_rel) == 0)
9544 /* Section size is divisible by both rel and rela sizes.
9545 It is of no help to us. */
9546 ;
9547 else
9548 {
9549 /* Section size is only divisible by rela. */
9550 if (use_rela_initialised && !use_rela)
9551 {
9552 _bfd_error_handler (_("%pB: unable to sort relocs - "
9553 "they are in more than one size"),
9554 abfd);
9555 bfd_set_error (bfd_error_invalid_operation);
9556 return 0;
9557 }
9558 else
9559 {
9560 use_rela = true;
9561 use_rela_initialised = true;
9562 }
9563 }
9564 }
9565 else if ((o->size % bed->s->sizeof_rel) == 0)
9566 {
9567 /* Section size is only divisible by rel. */
9568 if (use_rela_initialised && use_rela)
9569 {
9570 _bfd_error_handler (_("%pB: unable to sort relocs - "
9571 "they are in more than one size"),
9572 abfd);
9573 bfd_set_error (bfd_error_invalid_operation);
9574 return 0;
9575 }
9576 else
9577 {
9578 use_rela = false;
9579 use_rela_initialised = true;
9580 }
9581 }
9582 else
9583 {
9584 /* The section size is not divisible by either -
9585 something is wrong. */
9586 _bfd_error_handler (_("%pB: unable to sort relocs - "
9587 "they are of an unknown size"), abfd);
9588 bfd_set_error (bfd_error_invalid_operation);
9589 return 0;
9590 }
9591 }
9592
9593 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9594 if (lo->type == bfd_indirect_link_order)
9595 {
9596 asection *o = lo->u.indirect.section;
9597
9598 if ((o->size % bed->s->sizeof_rela) == 0)
9599 {
9600 if ((o->size % bed->s->sizeof_rel) == 0)
9601 /* Section size is divisible by both rel and rela sizes.
9602 It is of no help to us. */
9603 ;
9604 else
9605 {
9606 /* Section size is only divisible by rela. */
9607 if (use_rela_initialised && !use_rela)
9608 {
9609 _bfd_error_handler (_("%pB: unable to sort relocs - "
9610 "they are in more than one size"),
9611 abfd);
9612 bfd_set_error (bfd_error_invalid_operation);
9613 return 0;
9614 }
9615 else
9616 {
9617 use_rela = true;
9618 use_rela_initialised = true;
9619 }
9620 }
9621 }
9622 else if ((o->size % bed->s->sizeof_rel) == 0)
9623 {
9624 /* Section size is only divisible by rel. */
9625 if (use_rela_initialised && use_rela)
9626 {
9627 _bfd_error_handler (_("%pB: unable to sort relocs - "
9628 "they are in more than one size"),
9629 abfd);
9630 bfd_set_error (bfd_error_invalid_operation);
9631 return 0;
9632 }
9633 else
9634 {
9635 use_rela = false;
9636 use_rela_initialised = true;
9637 }
9638 }
9639 else
9640 {
9641 /* The section size is not divisible by either -
9642 something is wrong. */
9643 _bfd_error_handler (_("%pB: unable to sort relocs - "
9644 "they are of an unknown size"), abfd);
9645 bfd_set_error (bfd_error_invalid_operation);
9646 return 0;
9647 }
9648 }
9649
9650 if (! use_rela_initialised)
9651 /* Make a guess. */
9652 use_rela = true;
9653 }
9654 else if (rela_dyn != NULL && rela_dyn->size > 0)
9655 use_rela = true;
9656 else if (rel_dyn != NULL && rel_dyn->size > 0)
9657 use_rela = false;
9658 else
9659 return 0;
9660
9661 if (use_rela)
9662 {
9663 dynamic_relocs = rela_dyn;
9664 ext_size = bed->s->sizeof_rela;
9665 swap_in = bed->s->swap_reloca_in;
9666 swap_out = bed->s->swap_reloca_out;
9667 }
9668 else
9669 {
9670 dynamic_relocs = rel_dyn;
9671 ext_size = bed->s->sizeof_rel;
9672 swap_in = bed->s->swap_reloc_in;
9673 swap_out = bed->s->swap_reloc_out;
9674 }
9675
9676 size = 0;
9677 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9678 if (lo->type == bfd_indirect_link_order)
9679 size += lo->u.indirect.section->size;
9680
9681 if (size != dynamic_relocs->size)
9682 return 0;
9683
9684 sort_elt = (sizeof (struct elf_link_sort_rela)
9685 + (i2e - 1) * sizeof (Elf_Internal_Rela));
9686
9687 count = dynamic_relocs->size / ext_size;
9688 if (count == 0)
9689 return 0;
9690 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
9691
9692 if (sort == NULL)
9693 {
9694 (*info->callbacks->warning)
9695 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
9696 return 0;
9697 }
9698
9699 if (bed->s->arch_size == 32)
9700 r_sym_mask = ~(bfd_vma) 0xff;
9701 else
9702 r_sym_mask = ~(bfd_vma) 0xffffffff;
9703
9704 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9705 if (lo->type == bfd_indirect_link_order)
9706 {
9707 bfd_byte *erel, *erelend;
9708 asection *o = lo->u.indirect.section;
9709
9710 if (o->contents == NULL && o->size != 0)
9711 {
9712 /* This is a reloc section that is being handled as a normal
9713 section. See bfd_section_from_shdr. We can't combine
9714 relocs in this case. */
9715 free (sort);
9716 return 0;
9717 }
9718 erel = o->contents;
9719 erelend = o->contents + o->size;
9720 p = sort + o->output_offset * opb / ext_size * sort_elt;
9721
9722 while (erel < erelend)
9723 {
9724 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9725
9726 (*swap_in) (abfd, erel, s->rela);
9727 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
9728 s->u.sym_mask = r_sym_mask;
9729 p += sort_elt;
9730 erel += ext_size;
9731 }
9732 }
9733
9734 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9735
9736 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9737 {
9738 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9739 if (s->type != reloc_class_relative)
9740 break;
9741 }
9742 ret = i;
9743 s_non_relative = p;
9744
9745 sq = (struct elf_link_sort_rela *) s_non_relative;
9746 for (; i < count; i++, p += sort_elt)
9747 {
9748 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9749 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9750 sq = sp;
9751 sp->u.offset = sq->rela->r_offset;
9752 }
9753
9754 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9755
9756 struct elf_link_hash_table *htab = elf_hash_table (info);
9757 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9758 {
9759 /* We have plt relocs in .rela.dyn. */
9760 sq = (struct elf_link_sort_rela *) sort;
9761 for (i = 0; i < count; i++)
9762 if (sq[count - i - 1].type != reloc_class_plt)
9763 break;
9764 if (i != 0 && htab->srelplt->size == i * ext_size)
9765 {
9766 struct bfd_link_order **plo;
9767 /* Put srelplt link_order last. This is so the output_offset
9768 set in the next loop is correct for DT_JMPREL. */
9769 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9770 if ((*plo)->type == bfd_indirect_link_order
9771 && (*plo)->u.indirect.section == htab->srelplt)
9772 {
9773 lo = *plo;
9774 *plo = lo->next;
9775 }
9776 else
9777 plo = &(*plo)->next;
9778 *plo = lo;
9779 lo->next = NULL;
9780 dynamic_relocs->map_tail.link_order = lo;
9781 }
9782 }
9783
9784 p = sort;
9785 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9786 if (lo->type == bfd_indirect_link_order)
9787 {
9788 bfd_byte *erel, *erelend;
9789 asection *o = lo->u.indirect.section;
9790
9791 erel = o->contents;
9792 erelend = o->contents + o->size;
9793 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
9794 while (erel < erelend)
9795 {
9796 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9797 (*swap_out) (abfd, s->rela, erel);
9798 p += sort_elt;
9799 erel += ext_size;
9800 }
9801 }
9802
9803 free (sort);
9804 *psec = dynamic_relocs;
9805 return ret;
9806 }
9807
9808 /* Add a symbol to the output symbol string table. */
9809
9810 static int
9811 elf_link_output_symstrtab (void *finf,
9812 const char *name,
9813 Elf_Internal_Sym *elfsym,
9814 asection *input_sec,
9815 struct elf_link_hash_entry *h)
9816 {
9817 struct elf_final_link_info *flinfo = finf;
9818 int (*output_symbol_hook)
9819 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9820 struct elf_link_hash_entry *);
9821 struct elf_link_hash_table *hash_table;
9822 const struct elf_backend_data *bed;
9823 bfd_size_type strtabsize;
9824
9825 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9826
9827 bed = get_elf_backend_data (flinfo->output_bfd);
9828 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9829 if (output_symbol_hook != NULL)
9830 {
9831 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
9832 if (ret != 1)
9833 return ret;
9834 }
9835
9836 if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC)
9837 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
9838 if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE)
9839 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique;
9840
9841 if (name == NULL
9842 || *name == '\0'
9843 || (input_sec->flags & SEC_EXCLUDE))
9844 elfsym->st_name = (unsigned long) -1;
9845 else
9846 {
9847 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9848 to get the final offset for st_name. */
9849 char *versioned_name = (char *) name;
9850 if (h != NULL)
9851 {
9852 if (h->versioned == versioned && h->def_dynamic)
9853 {
9854 /* Keep only one '@' for versioned symbols defined in
9855 shared objects. */
9856 char *version = strrchr (name, ELF_VER_CHR);
9857 char *base_end = strchr (name, ELF_VER_CHR);
9858 if (version != base_end)
9859 {
9860 size_t base_len;
9861 size_t len = strlen (name);
9862 versioned_name = bfd_alloc (flinfo->output_bfd, len);
9863 if (versioned_name == NULL)
9864 return 0;
9865 base_len = base_end - name;
9866 memcpy (versioned_name, name, base_len);
9867 memcpy (versioned_name + base_len, version,
9868 len - base_len);
9869 }
9870 }
9871 }
9872 else if (flinfo->info->unique_symbol
9873 && ELF_ST_BIND (elfsym->st_info) == STB_LOCAL)
9874 {
9875 struct local_hash_entry *lh;
9876 size_t count_len;
9877 size_t base_len;
9878 char buf[30];
9879 switch (ELF_ST_TYPE (elfsym->st_info))
9880 {
9881 case STT_FILE:
9882 case STT_SECTION:
9883 break;
9884 default:
9885 lh = (struct local_hash_entry *) bfd_hash_lookup
9886 (&flinfo->local_hash_table, name, true, false);
9887 if (lh == NULL)
9888 return 0;
9889 /* Always append ".COUNT" to local symbols to avoid
9890 potential conflicts with local symbol "XXX.COUNT". */
9891 sprintf (buf, "%lx", lh->count);
9892 base_len = lh->size;
9893 if (!base_len)
9894 {
9895 base_len = strlen (name);
9896 lh->size = base_len;
9897 }
9898 count_len = strlen (buf);
9899 versioned_name = bfd_alloc (flinfo->output_bfd,
9900 base_len + count_len + 2);
9901 if (versioned_name == NULL)
9902 return 0;
9903 memcpy (versioned_name, name, base_len);
9904 versioned_name[base_len] = '.';
9905 memcpy (versioned_name + base_len + 1, buf,
9906 count_len + 1);
9907 lh->count++;
9908 break;
9909 }
9910 }
9911 elfsym->st_name
9912 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
9913 versioned_name, false);
9914 if (elfsym->st_name == (unsigned long) -1)
9915 return 0;
9916 }
9917
9918 hash_table = elf_hash_table (flinfo->info);
9919 strtabsize = hash_table->strtabsize;
9920 if (strtabsize <= flinfo->output_bfd->symcount)
9921 {
9922 strtabsize += strtabsize;
9923 hash_table->strtabsize = strtabsize;
9924 strtabsize *= sizeof (*hash_table->strtab);
9925 hash_table->strtab
9926 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9927 strtabsize);
9928 if (hash_table->strtab == NULL)
9929 return 0;
9930 }
9931 hash_table->strtab[flinfo->output_bfd->symcount].sym = *elfsym;
9932 hash_table->strtab[flinfo->output_bfd->symcount].dest_index
9933 = flinfo->output_bfd->symcount;
9934 flinfo->output_bfd->symcount += 1;
9935
9936 return 1;
9937 }
9938
9939 /* Swap symbols out to the symbol table and flush the output symbols to
9940 the file. */
9941
9942 static bool
9943 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9944 {
9945 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
9946 size_t amt;
9947 size_t i;
9948 const struct elf_backend_data *bed;
9949 bfd_byte *symbuf;
9950 Elf_Internal_Shdr *hdr;
9951 file_ptr pos;
9952 bool ret;
9953
9954 if (flinfo->output_bfd->symcount == 0)
9955 return true;
9956
9957 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9958
9959 bed = get_elf_backend_data (flinfo->output_bfd);
9960
9961 amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount;
9962 symbuf = (bfd_byte *) bfd_malloc (amt);
9963 if (symbuf == NULL)
9964 return false;
9965
9966 if (flinfo->symshndxbuf)
9967 {
9968 amt = sizeof (Elf_External_Sym_Shndx);
9969 amt *= bfd_get_symcount (flinfo->output_bfd);
9970 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9971 if (flinfo->symshndxbuf == NULL)
9972 {
9973 free (symbuf);
9974 return false;
9975 }
9976 }
9977
9978 /* Now swap out the symbols. */
9979 for (i = 0; i < flinfo->output_bfd->symcount; i++)
9980 {
9981 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9982 if (elfsym->sym.st_name == (unsigned long) -1)
9983 elfsym->sym.st_name = 0;
9984 else
9985 elfsym->sym.st_name
9986 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9987 elfsym->sym.st_name);
9988
9989 /* Inform the linker of the addition of this symbol. */
9990
9991 if (flinfo->info->callbacks->ctf_new_symbol)
9992 flinfo->info->callbacks->ctf_new_symbol (elfsym->dest_index,
9993 &elfsym->sym);
9994
9995 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9996 ((bfd_byte *) symbuf
9997 + (elfsym->dest_index
9998 * bed->s->sizeof_sym)),
9999 NPTR_ADD (flinfo->symshndxbuf,
10000 elfsym->dest_index));
10001 }
10002
10003 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
10004 pos = hdr->sh_offset + hdr->sh_size;
10005 amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount;
10006 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
10007 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
10008 {
10009 hdr->sh_size += amt;
10010 ret = true;
10011 }
10012 else
10013 ret = false;
10014
10015 free (symbuf);
10016
10017 free (hash_table->strtab);
10018 hash_table->strtab = NULL;
10019
10020 return ret;
10021 }
10022
10023 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
10024
10025 static bool
10026 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
10027 {
10028 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
10029 && sym->st_shndx < SHN_LORESERVE)
10030 {
10031 /* The gABI doesn't support dynamic symbols in output sections
10032 beyond 64k. */
10033 _bfd_error_handler
10034 /* xgettext:c-format */
10035 (_("%pB: too many sections: %d (>= %d)"),
10036 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
10037 bfd_set_error (bfd_error_nonrepresentable_section);
10038 return false;
10039 }
10040 return true;
10041 }
10042
10043 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
10044 allowing an unsatisfied unversioned symbol in the DSO to match a
10045 versioned symbol that would normally require an explicit version.
10046 We also handle the case that a DSO references a hidden symbol
10047 which may be satisfied by a versioned symbol in another DSO. */
10048
10049 static bool
10050 elf_link_check_versioned_symbol (struct bfd_link_info *info,
10051 const struct elf_backend_data *bed,
10052 struct elf_link_hash_entry *h)
10053 {
10054 bfd *abfd;
10055 struct elf_link_loaded_list *loaded;
10056
10057 if (!is_elf_hash_table (info->hash))
10058 return false;
10059
10060 /* Check indirect symbol. */
10061 while (h->root.type == bfd_link_hash_indirect)
10062 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10063
10064 switch (h->root.type)
10065 {
10066 default:
10067 abfd = NULL;
10068 break;
10069
10070 case bfd_link_hash_undefined:
10071 case bfd_link_hash_undefweak:
10072 abfd = h->root.u.undef.abfd;
10073 if (abfd == NULL
10074 || (abfd->flags & DYNAMIC) == 0
10075 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
10076 return false;
10077 break;
10078
10079 case bfd_link_hash_defined:
10080 case bfd_link_hash_defweak:
10081 abfd = h->root.u.def.section->owner;
10082 break;
10083
10084 case bfd_link_hash_common:
10085 abfd = h->root.u.c.p->section->owner;
10086 break;
10087 }
10088 BFD_ASSERT (abfd != NULL);
10089
10090 for (loaded = elf_hash_table (info)->dyn_loaded;
10091 loaded != NULL;
10092 loaded = loaded->next)
10093 {
10094 bfd *input;
10095 Elf_Internal_Shdr *hdr;
10096 size_t symcount;
10097 size_t extsymcount;
10098 size_t extsymoff;
10099 Elf_Internal_Shdr *versymhdr;
10100 Elf_Internal_Sym *isym;
10101 Elf_Internal_Sym *isymend;
10102 Elf_Internal_Sym *isymbuf;
10103 Elf_External_Versym *ever;
10104 Elf_External_Versym *extversym;
10105
10106 input = loaded->abfd;
10107
10108 /* We check each DSO for a possible hidden versioned definition. */
10109 if (input == abfd
10110 || elf_dynversym (input) == 0)
10111 continue;
10112
10113 hdr = &elf_tdata (input)->dynsymtab_hdr;
10114
10115 symcount = hdr->sh_size / bed->s->sizeof_sym;
10116 if (elf_bad_symtab (input))
10117 {
10118 extsymcount = symcount;
10119 extsymoff = 0;
10120 }
10121 else
10122 {
10123 extsymcount = symcount - hdr->sh_info;
10124 extsymoff = hdr->sh_info;
10125 }
10126
10127 if (extsymcount == 0)
10128 continue;
10129
10130 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
10131 NULL, NULL, NULL);
10132 if (isymbuf == NULL)
10133 return false;
10134
10135 /* Read in any version definitions. */
10136 versymhdr = &elf_tdata (input)->dynversym_hdr;
10137 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
10138 || (extversym = (Elf_External_Versym *)
10139 _bfd_malloc_and_read (input, versymhdr->sh_size,
10140 versymhdr->sh_size)) == NULL)
10141 {
10142 free (isymbuf);
10143 return false;
10144 }
10145
10146 ever = extversym + extsymoff;
10147 isymend = isymbuf + extsymcount;
10148 for (isym = isymbuf; isym < isymend; isym++, ever++)
10149 {
10150 const char *name;
10151 Elf_Internal_Versym iver;
10152 unsigned short version_index;
10153
10154 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
10155 || isym->st_shndx == SHN_UNDEF)
10156 continue;
10157
10158 name = bfd_elf_string_from_elf_section (input,
10159 hdr->sh_link,
10160 isym->st_name);
10161 if (strcmp (name, h->root.root.string) != 0)
10162 continue;
10163
10164 _bfd_elf_swap_versym_in (input, ever, &iver);
10165
10166 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
10167 && !(h->def_regular
10168 && h->forced_local))
10169 {
10170 /* If we have a non-hidden versioned sym, then it should
10171 have provided a definition for the undefined sym unless
10172 it is defined in a non-shared object and forced local.
10173 */
10174 abort ();
10175 }
10176
10177 version_index = iver.vs_vers & VERSYM_VERSION;
10178 if (version_index == 1 || version_index == 2)
10179 {
10180 /* This is the base or first version. We can use it. */
10181 free (extversym);
10182 free (isymbuf);
10183 return true;
10184 }
10185 }
10186
10187 free (extversym);
10188 free (isymbuf);
10189 }
10190
10191 return false;
10192 }
10193
10194 /* Convert ELF common symbol TYPE. */
10195
10196 static int
10197 elf_link_convert_common_type (struct bfd_link_info *info, int type)
10198 {
10199 /* Commom symbol can only appear in relocatable link. */
10200 if (!bfd_link_relocatable (info))
10201 abort ();
10202 switch (info->elf_stt_common)
10203 {
10204 case unchanged:
10205 break;
10206 case elf_stt_common:
10207 type = STT_COMMON;
10208 break;
10209 case no_elf_stt_common:
10210 type = STT_OBJECT;
10211 break;
10212 }
10213 return type;
10214 }
10215
10216 /* Add an external symbol to the symbol table. This is called from
10217 the hash table traversal routine. When generating a shared object,
10218 we go through the symbol table twice. The first time we output
10219 anything that might have been forced to local scope in a version
10220 script. The second time we output the symbols that are still
10221 global symbols. */
10222
10223 static bool
10224 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
10225 {
10226 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
10227 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
10228 struct elf_final_link_info *flinfo = eoinfo->flinfo;
10229 bool strip;
10230 Elf_Internal_Sym sym;
10231 asection *input_sec;
10232 const struct elf_backend_data *bed;
10233 long indx;
10234 int ret;
10235 unsigned int type;
10236
10237 if (h->root.type == bfd_link_hash_warning)
10238 {
10239 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10240 if (h->root.type == bfd_link_hash_new)
10241 return true;
10242 }
10243
10244 /* Decide whether to output this symbol in this pass. */
10245 if (eoinfo->localsyms)
10246 {
10247 if (!h->forced_local)
10248 return true;
10249 }
10250 else
10251 {
10252 if (h->forced_local)
10253 return true;
10254 }
10255
10256 bed = get_elf_backend_data (flinfo->output_bfd);
10257
10258 if (h->root.type == bfd_link_hash_undefined)
10259 {
10260 /* If we have an undefined symbol reference here then it must have
10261 come from a shared library that is being linked in. (Undefined
10262 references in regular files have already been handled unless
10263 they are in unreferenced sections which are removed by garbage
10264 collection). */
10265 bool ignore_undef = false;
10266
10267 /* Some symbols may be special in that the fact that they're
10268 undefined can be safely ignored - let backend determine that. */
10269 if (bed->elf_backend_ignore_undef_symbol)
10270 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
10271
10272 /* If we are reporting errors for this situation then do so now. */
10273 if (!ignore_undef
10274 && h->ref_dynamic_nonweak
10275 && (!h->ref_regular || flinfo->info->gc_sections)
10276 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
10277 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
10278 {
10279 flinfo->info->callbacks->undefined_symbol
10280 (flinfo->info, h->root.root.string,
10281 h->ref_regular ? NULL : h->root.u.undef.abfd, NULL, 0,
10282 flinfo->info->unresolved_syms_in_shared_libs == RM_DIAGNOSE
10283 && !flinfo->info->warn_unresolved_syms);
10284 }
10285
10286 /* Strip a global symbol defined in a discarded section. */
10287 if (h->indx == -3)
10288 return true;
10289 }
10290
10291 /* We should also warn if a forced local symbol is referenced from
10292 shared libraries. */
10293 if (bfd_link_executable (flinfo->info)
10294 && h->forced_local
10295 && h->ref_dynamic
10296 && h->def_regular
10297 && !h->dynamic_def
10298 && h->ref_dynamic_nonweak
10299 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
10300 {
10301 bfd *def_bfd;
10302 const char *msg;
10303 struct elf_link_hash_entry *hi = h;
10304
10305 /* Check indirect symbol. */
10306 while (hi->root.type == bfd_link_hash_indirect)
10307 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
10308
10309 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
10310 /* xgettext:c-format */
10311 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
10312 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
10313 /* xgettext:c-format */
10314 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
10315 else
10316 /* xgettext:c-format */
10317 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
10318 def_bfd = flinfo->output_bfd;
10319 if (hi->root.u.def.section != bfd_abs_section_ptr)
10320 def_bfd = hi->root.u.def.section->owner;
10321 _bfd_error_handler (msg, flinfo->output_bfd,
10322 h->root.root.string, def_bfd);
10323 bfd_set_error (bfd_error_bad_value);
10324 eoinfo->failed = true;
10325 return false;
10326 }
10327
10328 /* We don't want to output symbols that have never been mentioned by
10329 a regular file, or that we have been told to strip. However, if
10330 h->indx is set to -2, the symbol is used by a reloc and we must
10331 output it. */
10332 strip = false;
10333 if (h->indx == -2)
10334 ;
10335 else if ((h->def_dynamic
10336 || h->ref_dynamic
10337 || h->root.type == bfd_link_hash_new)
10338 && !h->def_regular
10339 && !h->ref_regular)
10340 strip = true;
10341 else if (flinfo->info->strip == strip_all)
10342 strip = true;
10343 else if (flinfo->info->strip == strip_some
10344 && bfd_hash_lookup (flinfo->info->keep_hash,
10345 h->root.root.string, false, false) == NULL)
10346 strip = true;
10347 else if ((h->root.type == bfd_link_hash_defined
10348 || h->root.type == bfd_link_hash_defweak)
10349 && ((flinfo->info->strip_discarded
10350 && discarded_section (h->root.u.def.section))
10351 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
10352 && h->root.u.def.section->owner != NULL
10353 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
10354 strip = true;
10355 else if ((h->root.type == bfd_link_hash_undefined
10356 || h->root.type == bfd_link_hash_undefweak)
10357 && h->root.u.undef.abfd != NULL
10358 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
10359 strip = true;
10360
10361 type = h->type;
10362
10363 /* If we're stripping it, and it's not a dynamic symbol, there's
10364 nothing else to do. However, if it is a forced local symbol or
10365 an ifunc symbol we need to give the backend finish_dynamic_symbol
10366 function a chance to make it dynamic. */
10367 if (strip
10368 && h->dynindx == -1
10369 && type != STT_GNU_IFUNC
10370 && !h->forced_local)
10371 return true;
10372
10373 sym.st_value = 0;
10374 sym.st_size = h->size;
10375 sym.st_other = h->other;
10376 switch (h->root.type)
10377 {
10378 default:
10379 case bfd_link_hash_new:
10380 case bfd_link_hash_warning:
10381 abort ();
10382 return false;
10383
10384 case bfd_link_hash_undefined:
10385 case bfd_link_hash_undefweak:
10386 input_sec = bfd_und_section_ptr;
10387 sym.st_shndx = SHN_UNDEF;
10388 break;
10389
10390 case bfd_link_hash_defined:
10391 case bfd_link_hash_defweak:
10392 {
10393 input_sec = h->root.u.def.section;
10394 if (input_sec->output_section != NULL)
10395 {
10396 sym.st_shndx =
10397 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
10398 input_sec->output_section);
10399 if (sym.st_shndx == SHN_BAD)
10400 {
10401 _bfd_error_handler
10402 /* xgettext:c-format */
10403 (_("%pB: could not find output section %pA for input section %pA"),
10404 flinfo->output_bfd, input_sec->output_section, input_sec);
10405 bfd_set_error (bfd_error_nonrepresentable_section);
10406 eoinfo->failed = true;
10407 return false;
10408 }
10409
10410 /* ELF symbols in relocatable files are section relative,
10411 but in nonrelocatable files they are virtual
10412 addresses. */
10413 sym.st_value = h->root.u.def.value + input_sec->output_offset;
10414 if (!bfd_link_relocatable (flinfo->info))
10415 {
10416 sym.st_value += input_sec->output_section->vma;
10417 if (h->type == STT_TLS)
10418 {
10419 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
10420 if (tls_sec != NULL)
10421 sym.st_value -= tls_sec->vma;
10422 }
10423 }
10424 }
10425 else
10426 {
10427 BFD_ASSERT (input_sec->owner == NULL
10428 || (input_sec->owner->flags & DYNAMIC) != 0);
10429 sym.st_shndx = SHN_UNDEF;
10430 input_sec = bfd_und_section_ptr;
10431 }
10432 }
10433 break;
10434
10435 case bfd_link_hash_common:
10436 input_sec = h->root.u.c.p->section;
10437 sym.st_shndx = bed->common_section_index (input_sec);
10438 sym.st_value = 1 << h->root.u.c.p->alignment_power;
10439 break;
10440
10441 case bfd_link_hash_indirect:
10442 /* These symbols are created by symbol versioning. They point
10443 to the decorated version of the name. For example, if the
10444 symbol foo@@GNU_1.2 is the default, which should be used when
10445 foo is used with no version, then we add an indirect symbol
10446 foo which points to foo@@GNU_1.2. We ignore these symbols,
10447 since the indirected symbol is already in the hash table. */
10448 return true;
10449 }
10450
10451 if (type == STT_COMMON || type == STT_OBJECT)
10452 switch (h->root.type)
10453 {
10454 case bfd_link_hash_common:
10455 type = elf_link_convert_common_type (flinfo->info, type);
10456 break;
10457 case bfd_link_hash_defined:
10458 case bfd_link_hash_defweak:
10459 if (bed->common_definition (&sym))
10460 type = elf_link_convert_common_type (flinfo->info, type);
10461 else
10462 type = STT_OBJECT;
10463 break;
10464 case bfd_link_hash_undefined:
10465 case bfd_link_hash_undefweak:
10466 break;
10467 default:
10468 abort ();
10469 }
10470
10471 if (h->forced_local)
10472 {
10473 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
10474 /* Turn off visibility on local symbol. */
10475 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
10476 }
10477 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10478 else if (h->unique_global && h->def_regular)
10479 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
10480 else if (h->root.type == bfd_link_hash_undefweak
10481 || h->root.type == bfd_link_hash_defweak)
10482 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
10483 else
10484 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
10485 sym.st_target_internal = h->target_internal;
10486
10487 /* Give the processor backend a chance to tweak the symbol value,
10488 and also to finish up anything that needs to be done for this
10489 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
10490 forced local syms when non-shared is due to a historical quirk.
10491 STT_GNU_IFUNC symbol must go through PLT. */
10492 if ((h->type == STT_GNU_IFUNC
10493 && h->def_regular
10494 && !bfd_link_relocatable (flinfo->info))
10495 || ((h->dynindx != -1
10496 || h->forced_local)
10497 && ((bfd_link_pic (flinfo->info)
10498 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
10499 || h->root.type != bfd_link_hash_undefweak))
10500 || !h->forced_local)
10501 && elf_hash_table (flinfo->info)->dynamic_sections_created))
10502 {
10503 if (! ((*bed->elf_backend_finish_dynamic_symbol)
10504 (flinfo->output_bfd, flinfo->info, h, &sym)))
10505 {
10506 eoinfo->failed = true;
10507 return false;
10508 }
10509 }
10510
10511 /* If we are marking the symbol as undefined, and there are no
10512 non-weak references to this symbol from a regular object, then
10513 mark the symbol as weak undefined; if there are non-weak
10514 references, mark the symbol as strong. We can't do this earlier,
10515 because it might not be marked as undefined until the
10516 finish_dynamic_symbol routine gets through with it. */
10517 if (sym.st_shndx == SHN_UNDEF
10518 && h->ref_regular
10519 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
10520 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
10521 {
10522 int bindtype;
10523 type = ELF_ST_TYPE (sym.st_info);
10524
10525 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10526 if (type == STT_GNU_IFUNC)
10527 type = STT_FUNC;
10528
10529 if (h->ref_regular_nonweak)
10530 bindtype = STB_GLOBAL;
10531 else
10532 bindtype = STB_WEAK;
10533 sym.st_info = ELF_ST_INFO (bindtype, type);
10534 }
10535
10536 /* If this is a symbol defined in a dynamic library, don't use the
10537 symbol size from the dynamic library. Relinking an executable
10538 against a new library may introduce gratuitous changes in the
10539 executable's symbols if we keep the size. */
10540 if (sym.st_shndx == SHN_UNDEF
10541 && !h->def_regular
10542 && h->def_dynamic)
10543 sym.st_size = 0;
10544
10545 /* If a non-weak symbol with non-default visibility is not defined
10546 locally, it is a fatal error. */
10547 if (!bfd_link_relocatable (flinfo->info)
10548 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
10549 && ELF_ST_BIND (sym.st_info) != STB_WEAK
10550 && h->root.type == bfd_link_hash_undefined
10551 && !h->def_regular)
10552 {
10553 const char *msg;
10554
10555 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
10556 /* xgettext:c-format */
10557 msg = _("%pB: protected symbol `%s' isn't defined");
10558 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
10559 /* xgettext:c-format */
10560 msg = _("%pB: internal symbol `%s' isn't defined");
10561 else
10562 /* xgettext:c-format */
10563 msg = _("%pB: hidden symbol `%s' isn't defined");
10564 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
10565 bfd_set_error (bfd_error_bad_value);
10566 eoinfo->failed = true;
10567 return false;
10568 }
10569
10570 /* If this symbol should be put in the .dynsym section, then put it
10571 there now. We already know the symbol index. We also fill in
10572 the entry in the .hash section. */
10573 if (h->dynindx != -1
10574 && elf_hash_table (flinfo->info)->dynamic_sections_created
10575 && elf_hash_table (flinfo->info)->dynsym != NULL
10576 && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
10577 {
10578 bfd_byte *esym;
10579
10580 /* Since there is no version information in the dynamic string,
10581 if there is no version info in symbol version section, we will
10582 have a run-time problem if not linking executable, referenced
10583 by shared library, or not bound locally. */
10584 if (h->verinfo.verdef == NULL
10585 && (!bfd_link_executable (flinfo->info)
10586 || h->ref_dynamic
10587 || !h->def_regular))
10588 {
10589 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
10590
10591 if (p && p [1] != '\0')
10592 {
10593 _bfd_error_handler
10594 /* xgettext:c-format */
10595 (_("%pB: no symbol version section for versioned symbol `%s'"),
10596 flinfo->output_bfd, h->root.root.string);
10597 eoinfo->failed = true;
10598 return false;
10599 }
10600 }
10601
10602 sym.st_name = h->dynstr_index;
10603 esym = (elf_hash_table (flinfo->info)->dynsym->contents
10604 + h->dynindx * bed->s->sizeof_sym);
10605 if (!check_dynsym (flinfo->output_bfd, &sym))
10606 {
10607 eoinfo->failed = true;
10608 return false;
10609 }
10610
10611 /* Inform the linker of the addition of this symbol. */
10612
10613 if (flinfo->info->callbacks->ctf_new_dynsym)
10614 flinfo->info->callbacks->ctf_new_dynsym (h->dynindx, &sym);
10615
10616 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
10617
10618 if (flinfo->hash_sec != NULL)
10619 {
10620 size_t hash_entry_size;
10621 bfd_byte *bucketpos;
10622 bfd_vma chain;
10623 size_t bucketcount;
10624 size_t bucket;
10625
10626 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
10627 bucket = h->u.elf_hash_value % bucketcount;
10628
10629 hash_entry_size
10630 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
10631 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
10632 + (bucket + 2) * hash_entry_size);
10633 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
10634 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
10635 bucketpos);
10636 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
10637 ((bfd_byte *) flinfo->hash_sec->contents
10638 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
10639 }
10640
10641 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
10642 {
10643 Elf_Internal_Versym iversym;
10644 Elf_External_Versym *eversym;
10645
10646 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
10647 {
10648 if (h->verinfo.verdef == NULL
10649 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
10650 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
10651 iversym.vs_vers = 1;
10652 else
10653 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
10654 }
10655 else
10656 {
10657 if (h->verinfo.vertree == NULL)
10658 iversym.vs_vers = 1;
10659 else
10660 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
10661 if (flinfo->info->create_default_symver)
10662 iversym.vs_vers++;
10663 }
10664
10665 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
10666 defined locally. */
10667 if (h->versioned == versioned_hidden && h->def_regular)
10668 iversym.vs_vers |= VERSYM_HIDDEN;
10669
10670 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
10671 eversym += h->dynindx;
10672 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
10673 }
10674 }
10675
10676 /* If the symbol is undefined, and we didn't output it to .dynsym,
10677 strip it from .symtab too. Obviously we can't do this for
10678 relocatable output or when needed for --emit-relocs. */
10679 else if (input_sec == bfd_und_section_ptr
10680 && h->indx != -2
10681 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10682 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
10683 && !bfd_link_relocatable (flinfo->info))
10684 return true;
10685
10686 /* Also strip others that we couldn't earlier due to dynamic symbol
10687 processing. */
10688 if (strip)
10689 return true;
10690 if ((input_sec->flags & SEC_EXCLUDE) != 0)
10691 return true;
10692
10693 /* Output a FILE symbol so that following locals are not associated
10694 with the wrong input file. We need one for forced local symbols
10695 if we've seen more than one FILE symbol or when we have exactly
10696 one FILE symbol but global symbols are present in a file other
10697 than the one with the FILE symbol. We also need one if linker
10698 defined symbols are present. In practice these conditions are
10699 always met, so just emit the FILE symbol unconditionally. */
10700 if (eoinfo->localsyms
10701 && !eoinfo->file_sym_done
10702 && eoinfo->flinfo->filesym_count != 0)
10703 {
10704 Elf_Internal_Sym fsym;
10705
10706 memset (&fsym, 0, sizeof (fsym));
10707 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10708 fsym.st_shndx = SHN_ABS;
10709 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10710 bfd_und_section_ptr, NULL))
10711 return false;
10712
10713 eoinfo->file_sym_done = true;
10714 }
10715
10716 indx = bfd_get_symcount (flinfo->output_bfd);
10717 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10718 input_sec, h);
10719 if (ret == 0)
10720 {
10721 eoinfo->failed = true;
10722 return false;
10723 }
10724 else if (ret == 1)
10725 h->indx = indx;
10726 else if (h->indx == -2)
10727 abort();
10728
10729 return true;
10730 }
10731
10732 /* Return TRUE if special handling is done for relocs in SEC against
10733 symbols defined in discarded sections. */
10734
10735 static bool
10736 elf_section_ignore_discarded_relocs (asection *sec)
10737 {
10738 const struct elf_backend_data *bed;
10739
10740 switch (sec->sec_info_type)
10741 {
10742 case SEC_INFO_TYPE_STABS:
10743 case SEC_INFO_TYPE_EH_FRAME:
10744 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10745 return true;
10746 default:
10747 break;
10748 }
10749
10750 bed = get_elf_backend_data (sec->owner);
10751 if (bed->elf_backend_ignore_discarded_relocs != NULL
10752 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10753 return true;
10754
10755 return false;
10756 }
10757
10758 /* Return a mask saying how ld should treat relocations in SEC against
10759 symbols defined in discarded sections. If this function returns
10760 COMPLAIN set, ld will issue a warning message. If this function
10761 returns PRETEND set, and the discarded section was link-once and the
10762 same size as the kept link-once section, ld will pretend that the
10763 symbol was actually defined in the kept section. Otherwise ld will
10764 zero the reloc (at least that is the intent, but some cooperation by
10765 the target dependent code is needed, particularly for REL targets). */
10766
10767 unsigned int
10768 _bfd_elf_default_action_discarded (asection *sec)
10769 {
10770 if (sec->flags & SEC_DEBUGGING)
10771 return PRETEND;
10772
10773 if (strcmp (".eh_frame", sec->name) == 0)
10774 return 0;
10775
10776 if (strcmp (".gcc_except_table", sec->name) == 0)
10777 return 0;
10778
10779 return COMPLAIN | PRETEND;
10780 }
10781
10782 /* Find a match between a section and a member of a section group. */
10783
10784 static asection *
10785 match_group_member (asection *sec, asection *group,
10786 struct bfd_link_info *info)
10787 {
10788 asection *first = elf_next_in_group (group);
10789 asection *s = first;
10790
10791 while (s != NULL)
10792 {
10793 if (bfd_elf_match_symbols_in_sections (s, sec, info))
10794 return s;
10795
10796 s = elf_next_in_group (s);
10797 if (s == first)
10798 break;
10799 }
10800
10801 return NULL;
10802 }
10803
10804 /* Check if the kept section of a discarded section SEC can be used
10805 to replace it. Return the replacement if it is OK. Otherwise return
10806 NULL. */
10807
10808 asection *
10809 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
10810 {
10811 asection *kept;
10812
10813 kept = sec->kept_section;
10814 if (kept != NULL)
10815 {
10816 if ((kept->flags & SEC_GROUP) != 0)
10817 kept = match_group_member (sec, kept, info);
10818 if (kept != NULL)
10819 {
10820 if ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10821 != (kept->rawsize != 0 ? kept->rawsize : kept->size))
10822 kept = NULL;
10823 else
10824 {
10825 /* Get the real kept section. */
10826 asection *next;
10827 for (next = kept->kept_section;
10828 next != NULL;
10829 next = next->kept_section)
10830 kept = next;
10831 }
10832 }
10833 sec->kept_section = kept;
10834 }
10835 return kept;
10836 }
10837
10838 /* Link an input file into the linker output file. This function
10839 handles all the sections and relocations of the input file at once.
10840 This is so that we only have to read the local symbols once, and
10841 don't have to keep them in memory. */
10842
10843 static bool
10844 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
10845 {
10846 int (*relocate_section)
10847 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10848 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10849 bfd *output_bfd;
10850 Elf_Internal_Shdr *symtab_hdr;
10851 size_t locsymcount;
10852 size_t extsymoff;
10853 Elf_Internal_Sym *isymbuf;
10854 Elf_Internal_Sym *isym;
10855 Elf_Internal_Sym *isymend;
10856 long *pindex;
10857 asection **ppsection;
10858 asection *o;
10859 const struct elf_backend_data *bed;
10860 struct elf_link_hash_entry **sym_hashes;
10861 bfd_size_type address_size;
10862 bfd_vma r_type_mask;
10863 int r_sym_shift;
10864 bool have_file_sym = false;
10865
10866 output_bfd = flinfo->output_bfd;
10867 bed = get_elf_backend_data (output_bfd);
10868 relocate_section = bed->elf_backend_relocate_section;
10869
10870 /* If this is a dynamic object, we don't want to do anything here:
10871 we don't want the local symbols, and we don't want the section
10872 contents. */
10873 if ((input_bfd->flags & DYNAMIC) != 0)
10874 return true;
10875
10876 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10877 if (elf_bad_symtab (input_bfd))
10878 {
10879 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10880 extsymoff = 0;
10881 }
10882 else
10883 {
10884 locsymcount = symtab_hdr->sh_info;
10885 extsymoff = symtab_hdr->sh_info;
10886 }
10887
10888 /* Enable GNU OSABI features in the output BFD that are used in the input
10889 BFD. */
10890 if (bed->elf_osabi == ELFOSABI_NONE
10891 || bed->elf_osabi == ELFOSABI_GNU
10892 || bed->elf_osabi == ELFOSABI_FREEBSD)
10893 elf_tdata (output_bfd)->has_gnu_osabi
10894 |= (elf_tdata (input_bfd)->has_gnu_osabi
10895 & (bfd_link_relocatable (flinfo->info)
10896 ? -1 : ~elf_gnu_osabi_retain));
10897
10898 /* Read the local symbols. */
10899 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10900 if (isymbuf == NULL && locsymcount != 0)
10901 {
10902 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
10903 flinfo->internal_syms,
10904 flinfo->external_syms,
10905 flinfo->locsym_shndx);
10906 if (isymbuf == NULL)
10907 return false;
10908 }
10909
10910 /* Find local symbol sections and adjust values of symbols in
10911 SEC_MERGE sections. Write out those local symbols we know are
10912 going into the output file. */
10913 isymend = PTR_ADD (isymbuf, locsymcount);
10914 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
10915 isym < isymend;
10916 isym++, pindex++, ppsection++)
10917 {
10918 asection *isec;
10919 const char *name;
10920 Elf_Internal_Sym osym;
10921 long indx;
10922 int ret;
10923
10924 *pindex = -1;
10925
10926 if (elf_bad_symtab (input_bfd))
10927 {
10928 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10929 {
10930 *ppsection = NULL;
10931 continue;
10932 }
10933 }
10934
10935 if (isym->st_shndx == SHN_UNDEF)
10936 isec = bfd_und_section_ptr;
10937 else if (isym->st_shndx == SHN_ABS)
10938 isec = bfd_abs_section_ptr;
10939 else if (isym->st_shndx == SHN_COMMON)
10940 isec = bfd_com_section_ptr;
10941 else
10942 {
10943 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10944 if (isec == NULL)
10945 {
10946 /* Don't attempt to output symbols with st_shnx in the
10947 reserved range other than SHN_ABS and SHN_COMMON. */
10948 isec = bfd_und_section_ptr;
10949 }
10950 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
10951 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10952 isym->st_value =
10953 _bfd_merged_section_offset (output_bfd, &isec,
10954 elf_section_data (isec)->sec_info,
10955 isym->st_value);
10956 }
10957
10958 *ppsection = isec;
10959
10960 /* Don't output the first, undefined, symbol. In fact, don't
10961 output any undefined local symbol. */
10962 if (isec == bfd_und_section_ptr)
10963 continue;
10964
10965 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10966 {
10967 /* We never output section symbols. Instead, we use the
10968 section symbol of the corresponding section in the output
10969 file. */
10970 continue;
10971 }
10972
10973 /* If we are stripping all symbols, we don't want to output this
10974 one. */
10975 if (flinfo->info->strip == strip_all)
10976 continue;
10977
10978 /* If we are discarding all local symbols, we don't want to
10979 output this one. If we are generating a relocatable output
10980 file, then some of the local symbols may be required by
10981 relocs; we output them below as we discover that they are
10982 needed. */
10983 if (flinfo->info->discard == discard_all)
10984 continue;
10985
10986 /* If this symbol is defined in a section which we are
10987 discarding, we don't need to keep it. */
10988 if (isym->st_shndx != SHN_UNDEF
10989 && isym->st_shndx < SHN_LORESERVE
10990 && isec->output_section == NULL
10991 && flinfo->info->non_contiguous_regions
10992 && flinfo->info->non_contiguous_regions_warnings)
10993 {
10994 _bfd_error_handler (_("warning: --enable-non-contiguous-regions "
10995 "discards section `%s' from '%s'\n"),
10996 isec->name, bfd_get_filename (isec->owner));
10997 continue;
10998 }
10999
11000 if (isym->st_shndx != SHN_UNDEF
11001 && isym->st_shndx < SHN_LORESERVE
11002 && bfd_section_removed_from_list (output_bfd,
11003 isec->output_section))
11004 continue;
11005
11006 /* Get the name of the symbol. */
11007 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
11008 isym->st_name);
11009 if (name == NULL)
11010 return false;
11011
11012 /* See if we are discarding symbols with this name. */
11013 if ((flinfo->info->strip == strip_some
11014 && (bfd_hash_lookup (flinfo->info->keep_hash, name, false, false)
11015 == NULL))
11016 || (((flinfo->info->discard == discard_sec_merge
11017 && (isec->flags & SEC_MERGE)
11018 && !bfd_link_relocatable (flinfo->info))
11019 || flinfo->info->discard == discard_l)
11020 && bfd_is_local_label_name (input_bfd, name)))
11021 continue;
11022
11023 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
11024 {
11025 if (input_bfd->lto_output)
11026 /* -flto puts a temp file name here. This means builds
11027 are not reproducible. Discard the symbol. */
11028 continue;
11029 have_file_sym = true;
11030 flinfo->filesym_count += 1;
11031 }
11032 if (!have_file_sym)
11033 {
11034 /* In the absence of debug info, bfd_find_nearest_line uses
11035 FILE symbols to determine the source file for local
11036 function symbols. Provide a FILE symbol here if input
11037 files lack such, so that their symbols won't be
11038 associated with a previous input file. It's not the
11039 source file, but the best we can do. */
11040 const char *filename;
11041 have_file_sym = true;
11042 flinfo->filesym_count += 1;
11043 memset (&osym, 0, sizeof (osym));
11044 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
11045 osym.st_shndx = SHN_ABS;
11046 if (input_bfd->lto_output)
11047 filename = NULL;
11048 else
11049 filename = lbasename (bfd_get_filename (input_bfd));
11050 if (!elf_link_output_symstrtab (flinfo, filename, &osym,
11051 bfd_abs_section_ptr, NULL))
11052 return false;
11053 }
11054
11055 osym = *isym;
11056
11057 /* Adjust the section index for the output file. */
11058 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11059 isec->output_section);
11060 if (osym.st_shndx == SHN_BAD)
11061 return false;
11062
11063 /* ELF symbols in relocatable files are section relative, but
11064 in executable files they are virtual addresses. Note that
11065 this code assumes that all ELF sections have an associated
11066 BFD section with a reasonable value for output_offset; below
11067 we assume that they also have a reasonable value for
11068 output_section. Any special sections must be set up to meet
11069 these requirements. */
11070 osym.st_value += isec->output_offset;
11071 if (!bfd_link_relocatable (flinfo->info))
11072 {
11073 osym.st_value += isec->output_section->vma;
11074 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
11075 {
11076 /* STT_TLS symbols are relative to PT_TLS segment base. */
11077 if (elf_hash_table (flinfo->info)->tls_sec != NULL)
11078 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
11079 else
11080 osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
11081 STT_NOTYPE);
11082 }
11083 }
11084
11085 indx = bfd_get_symcount (output_bfd);
11086 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
11087 if (ret == 0)
11088 return false;
11089 else if (ret == 1)
11090 *pindex = indx;
11091 }
11092
11093 if (bed->s->arch_size == 32)
11094 {
11095 r_type_mask = 0xff;
11096 r_sym_shift = 8;
11097 address_size = 4;
11098 }
11099 else
11100 {
11101 r_type_mask = 0xffffffff;
11102 r_sym_shift = 32;
11103 address_size = 8;
11104 }
11105
11106 /* Relocate the contents of each section. */
11107 sym_hashes = elf_sym_hashes (input_bfd);
11108 for (o = input_bfd->sections; o != NULL; o = o->next)
11109 {
11110 bfd_byte *contents;
11111
11112 if (! o->linker_mark)
11113 {
11114 /* This section was omitted from the link. */
11115 continue;
11116 }
11117
11118 if (!flinfo->info->resolve_section_groups
11119 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
11120 {
11121 /* Deal with the group signature symbol. */
11122 struct bfd_elf_section_data *sec_data = elf_section_data (o);
11123 unsigned long symndx = sec_data->this_hdr.sh_info;
11124 asection *osec = o->output_section;
11125
11126 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
11127 if (symndx >= locsymcount
11128 || (elf_bad_symtab (input_bfd)
11129 && flinfo->sections[symndx] == NULL))
11130 {
11131 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
11132 while (h->root.type == bfd_link_hash_indirect
11133 || h->root.type == bfd_link_hash_warning)
11134 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11135 /* Arrange for symbol to be output. */
11136 h->indx = -2;
11137 elf_section_data (osec)->this_hdr.sh_info = -2;
11138 }
11139 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
11140 {
11141 /* We'll use the output section target_index. */
11142 asection *sec = flinfo->sections[symndx]->output_section;
11143 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
11144 }
11145 else
11146 {
11147 if (flinfo->indices[symndx] == -1)
11148 {
11149 /* Otherwise output the local symbol now. */
11150 Elf_Internal_Sym sym = isymbuf[symndx];
11151 asection *sec = flinfo->sections[symndx]->output_section;
11152 const char *name;
11153 long indx;
11154 int ret;
11155
11156 name = bfd_elf_string_from_elf_section (input_bfd,
11157 symtab_hdr->sh_link,
11158 sym.st_name);
11159 if (name == NULL)
11160 return false;
11161
11162 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11163 sec);
11164 if (sym.st_shndx == SHN_BAD)
11165 return false;
11166
11167 sym.st_value += o->output_offset;
11168
11169 indx = bfd_get_symcount (output_bfd);
11170 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
11171 NULL);
11172 if (ret == 0)
11173 return false;
11174 else if (ret == 1)
11175 flinfo->indices[symndx] = indx;
11176 else
11177 abort ();
11178 }
11179 elf_section_data (osec)->this_hdr.sh_info
11180 = flinfo->indices[symndx];
11181 }
11182 }
11183
11184 if ((o->flags & SEC_HAS_CONTENTS) == 0
11185 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
11186 continue;
11187
11188 if ((o->flags & SEC_LINKER_CREATED) != 0)
11189 {
11190 /* Section was created by _bfd_elf_link_create_dynamic_sections
11191 or somesuch. */
11192 continue;
11193 }
11194
11195 /* Get the contents of the section. They have been cached by a
11196 relaxation routine. Note that o is a section in an input
11197 file, so the contents field will not have been set by any of
11198 the routines which work on output files. */
11199 if (elf_section_data (o)->this_hdr.contents != NULL)
11200 {
11201 contents = elf_section_data (o)->this_hdr.contents;
11202 if (bed->caches_rawsize
11203 && o->rawsize != 0
11204 && o->rawsize < o->size)
11205 {
11206 memcpy (flinfo->contents, contents, o->rawsize);
11207 contents = flinfo->contents;
11208 }
11209 }
11210 else
11211 {
11212 contents = flinfo->contents;
11213 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
11214 return false;
11215 }
11216
11217 if ((o->flags & SEC_RELOC) != 0)
11218 {
11219 Elf_Internal_Rela *internal_relocs;
11220 Elf_Internal_Rela *rel, *relend;
11221 int action_discarded;
11222 int ret;
11223
11224 /* Get the swapped relocs. */
11225 internal_relocs
11226 = _bfd_elf_link_info_read_relocs (input_bfd, flinfo->info, o,
11227 flinfo->external_relocs,
11228 flinfo->internal_relocs,
11229 false);
11230 if (internal_relocs == NULL
11231 && o->reloc_count > 0)
11232 return false;
11233
11234 /* We need to reverse-copy input .ctors/.dtors sections if
11235 they are placed in .init_array/.finit_array for output. */
11236 if (o->size > address_size
11237 && ((startswith (o->name, ".ctors")
11238 && strcmp (o->output_section->name,
11239 ".init_array") == 0)
11240 || (startswith (o->name, ".dtors")
11241 && strcmp (o->output_section->name,
11242 ".fini_array") == 0))
11243 && (o->name[6] == 0 || o->name[6] == '.'))
11244 {
11245 if (o->size * bed->s->int_rels_per_ext_rel
11246 != o->reloc_count * address_size)
11247 {
11248 _bfd_error_handler
11249 /* xgettext:c-format */
11250 (_("error: %pB: size of section %pA is not "
11251 "multiple of address size"),
11252 input_bfd, o);
11253 bfd_set_error (bfd_error_bad_value);
11254 return false;
11255 }
11256 o->flags |= SEC_ELF_REVERSE_COPY;
11257 }
11258
11259 action_discarded = -1;
11260 if (!elf_section_ignore_discarded_relocs (o))
11261 action_discarded = (*bed->action_discarded) (o);
11262
11263 /* Run through the relocs evaluating complex reloc symbols and
11264 looking for relocs against symbols from discarded sections
11265 or section symbols from removed link-once sections.
11266 Complain about relocs against discarded sections. Zero
11267 relocs against removed link-once sections. */
11268
11269 rel = internal_relocs;
11270 relend = rel + o->reloc_count;
11271 for ( ; rel < relend; rel++)
11272 {
11273 unsigned long r_symndx = rel->r_info >> r_sym_shift;
11274 unsigned int s_type;
11275 asection **ps, *sec;
11276 struct elf_link_hash_entry *h = NULL;
11277 const char *sym_name;
11278
11279 if (r_symndx == STN_UNDEF)
11280 continue;
11281
11282 if (r_symndx >= locsymcount
11283 || (elf_bad_symtab (input_bfd)
11284 && flinfo->sections[r_symndx] == NULL))
11285 {
11286 h = sym_hashes[r_symndx - extsymoff];
11287
11288 /* Badly formatted input files can contain relocs that
11289 reference non-existant symbols. Check here so that
11290 we do not seg fault. */
11291 if (h == NULL)
11292 {
11293 _bfd_error_handler
11294 /* xgettext:c-format */
11295 (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
11296 "that references a non-existent global symbol"),
11297 input_bfd, (uint64_t) rel->r_info, o);
11298 bfd_set_error (bfd_error_bad_value);
11299 return false;
11300 }
11301
11302 while (h->root.type == bfd_link_hash_indirect
11303 || h->root.type == bfd_link_hash_warning)
11304 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11305
11306 s_type = h->type;
11307
11308 /* If a plugin symbol is referenced from a non-IR file,
11309 mark the symbol as undefined. Note that the
11310 linker may attach linker created dynamic sections
11311 to the plugin bfd. Symbols defined in linker
11312 created sections are not plugin symbols. */
11313 if ((h->root.non_ir_ref_regular
11314 || h->root.non_ir_ref_dynamic)
11315 && (h->root.type == bfd_link_hash_defined
11316 || h->root.type == bfd_link_hash_defweak)
11317 && (h->root.u.def.section->flags
11318 & SEC_LINKER_CREATED) == 0
11319 && h->root.u.def.section->owner != NULL
11320 && (h->root.u.def.section->owner->flags
11321 & BFD_PLUGIN) != 0)
11322 {
11323 h->root.type = bfd_link_hash_undefined;
11324 h->root.u.undef.abfd = h->root.u.def.section->owner;
11325 }
11326
11327 ps = NULL;
11328 if (h->root.type == bfd_link_hash_defined
11329 || h->root.type == bfd_link_hash_defweak)
11330 ps = &h->root.u.def.section;
11331
11332 sym_name = h->root.root.string;
11333 }
11334 else
11335 {
11336 Elf_Internal_Sym *sym = isymbuf + r_symndx;
11337
11338 s_type = ELF_ST_TYPE (sym->st_info);
11339 ps = &flinfo->sections[r_symndx];
11340 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
11341 sym, *ps);
11342 }
11343
11344 if ((s_type == STT_RELC || s_type == STT_SRELC)
11345 && !bfd_link_relocatable (flinfo->info))
11346 {
11347 bfd_vma val;
11348 bfd_vma dot = (rel->r_offset
11349 + o->output_offset + o->output_section->vma);
11350 #ifdef DEBUG
11351 printf ("Encountered a complex symbol!");
11352 printf (" (input_bfd %s, section %s, reloc %ld\n",
11353 bfd_get_filename (input_bfd), o->name,
11354 (long) (rel - internal_relocs));
11355 printf (" symbol: idx %8.8lx, name %s\n",
11356 r_symndx, sym_name);
11357 printf (" reloc : info %8.8lx, addr %8.8lx\n",
11358 (unsigned long) rel->r_info,
11359 (unsigned long) rel->r_offset);
11360 #endif
11361 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
11362 isymbuf, locsymcount, s_type == STT_SRELC))
11363 return false;
11364
11365 /* Symbol evaluated OK. Update to absolute value. */
11366 set_symbol_value (input_bfd, isymbuf, locsymcount,
11367 r_symndx, val);
11368 continue;
11369 }
11370
11371 if (action_discarded != -1 && ps != NULL)
11372 {
11373 /* Complain if the definition comes from a
11374 discarded section. */
11375 if ((sec = *ps) != NULL && discarded_section (sec))
11376 {
11377 BFD_ASSERT (r_symndx != STN_UNDEF);
11378 if (action_discarded & COMPLAIN)
11379 (*flinfo->info->callbacks->einfo)
11380 /* xgettext:c-format */
11381 (_("%X`%s' referenced in section `%pA' of %pB: "
11382 "defined in discarded section `%pA' of %pB\n"),
11383 sym_name, o, input_bfd, sec, sec->owner);
11384
11385 /* Try to do the best we can to support buggy old
11386 versions of gcc. Pretend that the symbol is
11387 really defined in the kept linkonce section.
11388 FIXME: This is quite broken. Modifying the
11389 symbol here means we will be changing all later
11390 uses of the symbol, not just in this section. */
11391 if (action_discarded & PRETEND)
11392 {
11393 asection *kept;
11394
11395 kept = _bfd_elf_check_kept_section (sec,
11396 flinfo->info);
11397 if (kept != NULL)
11398 {
11399 *ps = kept;
11400 continue;
11401 }
11402 }
11403 }
11404 }
11405 }
11406
11407 /* Relocate the section by invoking a back end routine.
11408
11409 The back end routine is responsible for adjusting the
11410 section contents as necessary, and (if using Rela relocs
11411 and generating a relocatable output file) adjusting the
11412 reloc addend as necessary.
11413
11414 The back end routine does not have to worry about setting
11415 the reloc address or the reloc symbol index.
11416
11417 The back end routine is given a pointer to the swapped in
11418 internal symbols, and can access the hash table entries
11419 for the external symbols via elf_sym_hashes (input_bfd).
11420
11421 When generating relocatable output, the back end routine
11422 must handle STB_LOCAL/STT_SECTION symbols specially. The
11423 output symbol is going to be a section symbol
11424 corresponding to the output section, which will require
11425 the addend to be adjusted. */
11426
11427 ret = (*relocate_section) (output_bfd, flinfo->info,
11428 input_bfd, o, contents,
11429 internal_relocs,
11430 isymbuf,
11431 flinfo->sections);
11432 if (!ret)
11433 return false;
11434
11435 if (ret == 2
11436 || bfd_link_relocatable (flinfo->info)
11437 || flinfo->info->emitrelocations)
11438 {
11439 Elf_Internal_Rela *irela;
11440 Elf_Internal_Rela *irelaend, *irelamid;
11441 bfd_vma last_offset;
11442 struct elf_link_hash_entry **rel_hash;
11443 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
11444 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
11445 unsigned int next_erel;
11446 bool rela_normal;
11447 struct bfd_elf_section_data *esdi, *esdo;
11448
11449 esdi = elf_section_data (o);
11450 esdo = elf_section_data (o->output_section);
11451 rela_normal = false;
11452
11453 /* Adjust the reloc addresses and symbol indices. */
11454
11455 irela = internal_relocs;
11456 irelaend = irela + o->reloc_count;
11457 rel_hash = PTR_ADD (esdo->rel.hashes, esdo->rel.count);
11458 /* We start processing the REL relocs, if any. When we reach
11459 IRELAMID in the loop, we switch to the RELA relocs. */
11460 irelamid = irela;
11461 if (esdi->rel.hdr != NULL)
11462 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
11463 * bed->s->int_rels_per_ext_rel);
11464 rel_hash_list = rel_hash;
11465 rela_hash_list = NULL;
11466 last_offset = o->output_offset;
11467 if (!bfd_link_relocatable (flinfo->info))
11468 last_offset += o->output_section->vma;
11469 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
11470 {
11471 unsigned long r_symndx;
11472 asection *sec;
11473 Elf_Internal_Sym sym;
11474
11475 if (next_erel == bed->s->int_rels_per_ext_rel)
11476 {
11477 rel_hash++;
11478 next_erel = 0;
11479 }
11480
11481 if (irela == irelamid)
11482 {
11483 rel_hash = PTR_ADD (esdo->rela.hashes, esdo->rela.count);
11484 rela_hash_list = rel_hash;
11485 rela_normal = bed->rela_normal;
11486 }
11487
11488 irela->r_offset = _bfd_elf_section_offset (output_bfd,
11489 flinfo->info, o,
11490 irela->r_offset);
11491 if (irela->r_offset >= (bfd_vma) -2)
11492 {
11493 /* This is a reloc for a deleted entry or somesuch.
11494 Turn it into an R_*_NONE reloc, at the same
11495 offset as the last reloc. elf_eh_frame.c and
11496 bfd_elf_discard_info rely on reloc offsets
11497 being ordered. */
11498 irela->r_offset = last_offset;
11499 irela->r_info = 0;
11500 irela->r_addend = 0;
11501 continue;
11502 }
11503
11504 irela->r_offset += o->output_offset;
11505
11506 /* Relocs in an executable have to be virtual addresses. */
11507 if (!bfd_link_relocatable (flinfo->info))
11508 irela->r_offset += o->output_section->vma;
11509
11510 last_offset = irela->r_offset;
11511
11512 r_symndx = irela->r_info >> r_sym_shift;
11513 if (r_symndx == STN_UNDEF)
11514 continue;
11515
11516 if (r_symndx >= locsymcount
11517 || (elf_bad_symtab (input_bfd)
11518 && flinfo->sections[r_symndx] == NULL))
11519 {
11520 struct elf_link_hash_entry *rh;
11521 unsigned long indx;
11522
11523 /* This is a reloc against a global symbol. We
11524 have not yet output all the local symbols, so
11525 we do not know the symbol index of any global
11526 symbol. We set the rel_hash entry for this
11527 reloc to point to the global hash table entry
11528 for this symbol. The symbol index is then
11529 set at the end of bfd_elf_final_link. */
11530 indx = r_symndx - extsymoff;
11531 rh = elf_sym_hashes (input_bfd)[indx];
11532 while (rh->root.type == bfd_link_hash_indirect
11533 || rh->root.type == bfd_link_hash_warning)
11534 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
11535
11536 /* Setting the index to -2 tells
11537 elf_link_output_extsym that this symbol is
11538 used by a reloc. */
11539 BFD_ASSERT (rh->indx < 0);
11540 rh->indx = -2;
11541 *rel_hash = rh;
11542
11543 continue;
11544 }
11545
11546 /* This is a reloc against a local symbol. */
11547
11548 *rel_hash = NULL;
11549 sym = isymbuf[r_symndx];
11550 sec = flinfo->sections[r_symndx];
11551 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
11552 {
11553 /* I suppose the backend ought to fill in the
11554 section of any STT_SECTION symbol against a
11555 processor specific section. */
11556 r_symndx = STN_UNDEF;
11557 if (bfd_is_abs_section (sec))
11558 ;
11559 else if (sec == NULL || sec->owner == NULL)
11560 {
11561 bfd_set_error (bfd_error_bad_value);
11562 return false;
11563 }
11564 else
11565 {
11566 asection *osec = sec->output_section;
11567
11568 /* If we have discarded a section, the output
11569 section will be the absolute section. In
11570 case of discarded SEC_MERGE sections, use
11571 the kept section. relocate_section should
11572 have already handled discarded linkonce
11573 sections. */
11574 if (bfd_is_abs_section (osec)
11575 && sec->kept_section != NULL
11576 && sec->kept_section->output_section != NULL)
11577 {
11578 osec = sec->kept_section->output_section;
11579 irela->r_addend -= osec->vma;
11580 }
11581
11582 if (!bfd_is_abs_section (osec))
11583 {
11584 r_symndx = osec->target_index;
11585 if (r_symndx == STN_UNDEF)
11586 {
11587 irela->r_addend += osec->vma;
11588 osec = _bfd_nearby_section (output_bfd, osec,
11589 osec->vma);
11590 irela->r_addend -= osec->vma;
11591 r_symndx = osec->target_index;
11592 }
11593 }
11594 }
11595
11596 /* Adjust the addend according to where the
11597 section winds up in the output section. */
11598 if (rela_normal)
11599 irela->r_addend += sec->output_offset;
11600 }
11601 else
11602 {
11603 if (flinfo->indices[r_symndx] == -1)
11604 {
11605 unsigned long shlink;
11606 const char *name;
11607 asection *osec;
11608 long indx;
11609
11610 if (flinfo->info->strip == strip_all)
11611 {
11612 /* You can't do ld -r -s. */
11613 bfd_set_error (bfd_error_invalid_operation);
11614 return false;
11615 }
11616
11617 /* This symbol was skipped earlier, but
11618 since it is needed by a reloc, we
11619 must output it now. */
11620 shlink = symtab_hdr->sh_link;
11621 name = (bfd_elf_string_from_elf_section
11622 (input_bfd, shlink, sym.st_name));
11623 if (name == NULL)
11624 return false;
11625
11626 osec = sec->output_section;
11627 sym.st_shndx =
11628 _bfd_elf_section_from_bfd_section (output_bfd,
11629 osec);
11630 if (sym.st_shndx == SHN_BAD)
11631 return false;
11632
11633 sym.st_value += sec->output_offset;
11634 if (!bfd_link_relocatable (flinfo->info))
11635 {
11636 sym.st_value += osec->vma;
11637 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
11638 {
11639 struct elf_link_hash_table *htab
11640 = elf_hash_table (flinfo->info);
11641
11642 /* STT_TLS symbols are relative to PT_TLS
11643 segment base. */
11644 if (htab->tls_sec != NULL)
11645 sym.st_value -= htab->tls_sec->vma;
11646 else
11647 sym.st_info
11648 = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
11649 STT_NOTYPE);
11650 }
11651 }
11652
11653 indx = bfd_get_symcount (output_bfd);
11654 ret = elf_link_output_symstrtab (flinfo, name,
11655 &sym, sec,
11656 NULL);
11657 if (ret == 0)
11658 return false;
11659 else if (ret == 1)
11660 flinfo->indices[r_symndx] = indx;
11661 else
11662 abort ();
11663 }
11664
11665 r_symndx = flinfo->indices[r_symndx];
11666 }
11667
11668 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
11669 | (irela->r_info & r_type_mask));
11670 }
11671
11672 /* Swap out the relocs. */
11673 input_rel_hdr = esdi->rel.hdr;
11674 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
11675 {
11676 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11677 input_rel_hdr,
11678 internal_relocs,
11679 rel_hash_list))
11680 return false;
11681 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
11682 * bed->s->int_rels_per_ext_rel);
11683 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
11684 }
11685
11686 input_rela_hdr = esdi->rela.hdr;
11687 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
11688 {
11689 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11690 input_rela_hdr,
11691 internal_relocs,
11692 rela_hash_list))
11693 return false;
11694 }
11695 }
11696 }
11697
11698 /* Write out the modified section contents. */
11699 if (bed->elf_backend_write_section
11700 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
11701 contents))
11702 {
11703 /* Section written out. */
11704 }
11705 else switch (o->sec_info_type)
11706 {
11707 case SEC_INFO_TYPE_STABS:
11708 if (! (_bfd_write_section_stabs
11709 (output_bfd,
11710 &elf_hash_table (flinfo->info)->stab_info,
11711 o, &elf_section_data (o)->sec_info, contents)))
11712 return false;
11713 break;
11714 case SEC_INFO_TYPE_MERGE:
11715 if (! _bfd_write_merged_section (output_bfd, o,
11716 elf_section_data (o)->sec_info))
11717 return false;
11718 break;
11719 case SEC_INFO_TYPE_EH_FRAME:
11720 {
11721 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
11722 o, contents))
11723 return false;
11724 }
11725 break;
11726 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11727 {
11728 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11729 flinfo->info,
11730 o, contents))
11731 return false;
11732 }
11733 break;
11734 default:
11735 {
11736 if (! (o->flags & SEC_EXCLUDE))
11737 {
11738 file_ptr offset = (file_ptr) o->output_offset;
11739 bfd_size_type todo = o->size;
11740
11741 offset *= bfd_octets_per_byte (output_bfd, o);
11742
11743 if ((o->flags & SEC_ELF_REVERSE_COPY))
11744 {
11745 /* Reverse-copy input section to output. */
11746 do
11747 {
11748 todo -= address_size;
11749 if (! bfd_set_section_contents (output_bfd,
11750 o->output_section,
11751 contents + todo,
11752 offset,
11753 address_size))
11754 return false;
11755 if (todo == 0)
11756 break;
11757 offset += address_size;
11758 }
11759 while (1);
11760 }
11761 else if (! bfd_set_section_contents (output_bfd,
11762 o->output_section,
11763 contents,
11764 offset, todo))
11765 return false;
11766 }
11767 }
11768 break;
11769 }
11770 }
11771
11772 return true;
11773 }
11774
11775 /* Generate a reloc when linking an ELF file. This is a reloc
11776 requested by the linker, and does not come from any input file. This
11777 is used to build constructor and destructor tables when linking
11778 with -Ur. */
11779
11780 static bool
11781 elf_reloc_link_order (bfd *output_bfd,
11782 struct bfd_link_info *info,
11783 asection *output_section,
11784 struct bfd_link_order *link_order)
11785 {
11786 reloc_howto_type *howto;
11787 long indx;
11788 bfd_vma offset;
11789 bfd_vma addend;
11790 struct bfd_elf_section_reloc_data *reldata;
11791 struct elf_link_hash_entry **rel_hash_ptr;
11792 Elf_Internal_Shdr *rel_hdr;
11793 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11794 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11795 bfd_byte *erel;
11796 unsigned int i;
11797 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
11798
11799 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11800 if (howto == NULL)
11801 {
11802 bfd_set_error (bfd_error_bad_value);
11803 return false;
11804 }
11805
11806 addend = link_order->u.reloc.p->addend;
11807
11808 if (esdo->rel.hdr)
11809 reldata = &esdo->rel;
11810 else if (esdo->rela.hdr)
11811 reldata = &esdo->rela;
11812 else
11813 {
11814 reldata = NULL;
11815 BFD_ASSERT (0);
11816 }
11817
11818 /* Figure out the symbol index. */
11819 rel_hash_ptr = reldata->hashes + reldata->count;
11820 if (link_order->type == bfd_section_reloc_link_order)
11821 {
11822 indx = link_order->u.reloc.p->u.section->target_index;
11823 BFD_ASSERT (indx != 0);
11824 *rel_hash_ptr = NULL;
11825 }
11826 else
11827 {
11828 struct elf_link_hash_entry *h;
11829
11830 /* Treat a reloc against a defined symbol as though it were
11831 actually against the section. */
11832 h = ((struct elf_link_hash_entry *)
11833 bfd_wrapped_link_hash_lookup (output_bfd, info,
11834 link_order->u.reloc.p->u.name,
11835 false, false, true));
11836 if (h != NULL
11837 && (h->root.type == bfd_link_hash_defined
11838 || h->root.type == bfd_link_hash_defweak))
11839 {
11840 asection *section;
11841
11842 section = h->root.u.def.section;
11843 indx = section->output_section->target_index;
11844 *rel_hash_ptr = NULL;
11845 /* It seems that we ought to add the symbol value to the
11846 addend here, but in practice it has already been added
11847 because it was passed to constructor_callback. */
11848 addend += section->output_section->vma + section->output_offset;
11849 }
11850 else if (h != NULL)
11851 {
11852 /* Setting the index to -2 tells elf_link_output_extsym that
11853 this symbol is used by a reloc. */
11854 h->indx = -2;
11855 *rel_hash_ptr = h;
11856 indx = 0;
11857 }
11858 else
11859 {
11860 (*info->callbacks->unattached_reloc)
11861 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
11862 indx = 0;
11863 }
11864 }
11865
11866 /* If this is an inplace reloc, we must write the addend into the
11867 object file. */
11868 if (howto->partial_inplace && addend != 0)
11869 {
11870 bfd_size_type size;
11871 bfd_reloc_status_type rstat;
11872 bfd_byte *buf;
11873 bool ok;
11874 const char *sym_name;
11875 bfd_size_type octets;
11876
11877 size = (bfd_size_type) bfd_get_reloc_size (howto);
11878 buf = (bfd_byte *) bfd_zmalloc (size);
11879 if (buf == NULL && size != 0)
11880 return false;
11881 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11882 switch (rstat)
11883 {
11884 case bfd_reloc_ok:
11885 break;
11886
11887 default:
11888 case bfd_reloc_outofrange:
11889 abort ();
11890
11891 case bfd_reloc_overflow:
11892 if (link_order->type == bfd_section_reloc_link_order)
11893 sym_name = bfd_section_name (link_order->u.reloc.p->u.section);
11894 else
11895 sym_name = link_order->u.reloc.p->u.name;
11896 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11897 howto->name, addend, NULL, NULL,
11898 (bfd_vma) 0);
11899 break;
11900 }
11901
11902 octets = link_order->offset * bfd_octets_per_byte (output_bfd,
11903 output_section);
11904 ok = bfd_set_section_contents (output_bfd, output_section, buf,
11905 octets, size);
11906 free (buf);
11907 if (! ok)
11908 return false;
11909 }
11910
11911 /* The address of a reloc is relative to the section in a
11912 relocatable file, and is a virtual address in an executable
11913 file. */
11914 offset = link_order->offset;
11915 if (! bfd_link_relocatable (info))
11916 offset += output_section->vma;
11917
11918 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11919 {
11920 irel[i].r_offset = offset;
11921 irel[i].r_info = 0;
11922 irel[i].r_addend = 0;
11923 }
11924 if (bed->s->arch_size == 32)
11925 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11926 else
11927 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11928
11929 rel_hdr = reldata->hdr;
11930 erel = rel_hdr->contents;
11931 if (rel_hdr->sh_type == SHT_REL)
11932 {
11933 erel += reldata->count * bed->s->sizeof_rel;
11934 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11935 }
11936 else
11937 {
11938 irel[0].r_addend = addend;
11939 erel += reldata->count * bed->s->sizeof_rela;
11940 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11941 }
11942
11943 ++reldata->count;
11944
11945 return true;
11946 }
11947
11948 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11949 Returns TRUE upon success, FALSE otherwise. */
11950
11951 static bool
11952 elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11953 {
11954 bool ret = false;
11955 bfd *implib_bfd;
11956 const struct elf_backend_data *bed;
11957 flagword flags;
11958 enum bfd_architecture arch;
11959 unsigned int mach;
11960 asymbol **sympp = NULL;
11961 long symsize;
11962 long symcount;
11963 long src_count;
11964 elf_symbol_type *osymbuf;
11965 size_t amt;
11966
11967 implib_bfd = info->out_implib_bfd;
11968 bed = get_elf_backend_data (abfd);
11969
11970 if (!bfd_set_format (implib_bfd, bfd_object))
11971 return false;
11972
11973 /* Use flag from executable but make it a relocatable object. */
11974 flags = bfd_get_file_flags (abfd);
11975 flags &= ~HAS_RELOC;
11976 if (!bfd_set_start_address (implib_bfd, 0)
11977 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
11978 return false;
11979
11980 /* Copy architecture of output file to import library file. */
11981 arch = bfd_get_arch (abfd);
11982 mach = bfd_get_mach (abfd);
11983 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11984 && (abfd->target_defaulted
11985 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11986 return false;
11987
11988 /* Get symbol table size. */
11989 symsize = bfd_get_symtab_upper_bound (abfd);
11990 if (symsize < 0)
11991 return false;
11992
11993 /* Read in the symbol table. */
11994 sympp = (asymbol **) bfd_malloc (symsize);
11995 if (sympp == NULL)
11996 return false;
11997
11998 symcount = bfd_canonicalize_symtab (abfd, sympp);
11999 if (symcount < 0)
12000 goto free_sym_buf;
12001
12002 /* Allow the BFD backend to copy any private header data it
12003 understands from the output BFD to the import library BFD. */
12004 if (! bfd_copy_private_header_data (abfd, implib_bfd))
12005 goto free_sym_buf;
12006
12007 /* Filter symbols to appear in the import library. */
12008 if (bed->elf_backend_filter_implib_symbols)
12009 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
12010 symcount);
12011 else
12012 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
12013 if (symcount == 0)
12014 {
12015 bfd_set_error (bfd_error_no_symbols);
12016 _bfd_error_handler (_("%pB: no symbol found for import library"),
12017 implib_bfd);
12018 goto free_sym_buf;
12019 }
12020
12021
12022 /* Make symbols absolute. */
12023 amt = symcount * sizeof (*osymbuf);
12024 osymbuf = (elf_symbol_type *) bfd_alloc (implib_bfd, amt);
12025 if (osymbuf == NULL)
12026 goto free_sym_buf;
12027
12028 for (src_count = 0; src_count < symcount; src_count++)
12029 {
12030 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
12031 sizeof (*osymbuf));
12032 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
12033 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
12034 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
12035 osymbuf[src_count].internal_elf_sym.st_value =
12036 osymbuf[src_count].symbol.value;
12037 sympp[src_count] = &osymbuf[src_count].symbol;
12038 }
12039
12040 bfd_set_symtab (implib_bfd, sympp, symcount);
12041
12042 /* Allow the BFD backend to copy any private data it understands
12043 from the output BFD to the import library BFD. This is done last
12044 to permit the routine to look at the filtered symbol table. */
12045 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
12046 goto free_sym_buf;
12047
12048 if (!bfd_close (implib_bfd))
12049 goto free_sym_buf;
12050
12051 ret = true;
12052
12053 free_sym_buf:
12054 free (sympp);
12055 return ret;
12056 }
12057
12058 static void
12059 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
12060 {
12061 asection *o;
12062
12063 if (flinfo->symstrtab != NULL)
12064 _bfd_elf_strtab_free (flinfo->symstrtab);
12065 free (flinfo->contents);
12066 free (flinfo->external_relocs);
12067 free (flinfo->internal_relocs);
12068 free (flinfo->external_syms);
12069 free (flinfo->locsym_shndx);
12070 free (flinfo->internal_syms);
12071 free (flinfo->indices);
12072 free (flinfo->sections);
12073 if (flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1)
12074 free (flinfo->symshndxbuf);
12075 for (o = obfd->sections; o != NULL; o = o->next)
12076 {
12077 struct bfd_elf_section_data *esdo = elf_section_data (o);
12078 free (esdo->rel.hashes);
12079 free (esdo->rela.hashes);
12080 }
12081 }
12082
12083 /* Do the final step of an ELF link. */
12084
12085 bool
12086 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
12087 {
12088 bool dynamic;
12089 bool emit_relocs;
12090 bfd *dynobj;
12091 struct elf_final_link_info flinfo;
12092 asection *o;
12093 struct bfd_link_order *p;
12094 bfd *sub;
12095 bfd_size_type max_contents_size;
12096 bfd_size_type max_external_reloc_size;
12097 bfd_size_type max_internal_reloc_count;
12098 bfd_size_type max_sym_count;
12099 bfd_size_type max_sym_shndx_count;
12100 Elf_Internal_Sym elfsym;
12101 unsigned int i;
12102 Elf_Internal_Shdr *symtab_hdr;
12103 Elf_Internal_Shdr *symtab_shndx_hdr;
12104 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12105 struct elf_outext_info eoinfo;
12106 bool merged;
12107 size_t relativecount = 0;
12108 asection *reldyn = 0;
12109 bfd_size_type amt;
12110 asection *attr_section = NULL;
12111 bfd_vma attr_size = 0;
12112 const char *std_attrs_section;
12113 struct elf_link_hash_table *htab = elf_hash_table (info);
12114 bool sections_removed;
12115 bool ret;
12116
12117 if (!is_elf_hash_table (&htab->root))
12118 return false;
12119
12120 if (bfd_link_pic (info))
12121 abfd->flags |= DYNAMIC;
12122
12123 dynamic = htab->dynamic_sections_created;
12124 dynobj = htab->dynobj;
12125
12126 emit_relocs = (bfd_link_relocatable (info)
12127 || info->emitrelocations);
12128
12129 memset (&flinfo, 0, sizeof (flinfo));
12130 flinfo.info = info;
12131 flinfo.output_bfd = abfd;
12132 flinfo.symstrtab = _bfd_elf_strtab_init ();
12133 if (flinfo.symstrtab == NULL)
12134 return false;
12135
12136 if (! dynamic)
12137 {
12138 flinfo.hash_sec = NULL;
12139 flinfo.symver_sec = NULL;
12140 }
12141 else
12142 {
12143 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
12144 /* Note that dynsym_sec can be NULL (on VMS). */
12145 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
12146 /* Note that it is OK if symver_sec is NULL. */
12147 }
12148
12149 if (info->unique_symbol
12150 && !bfd_hash_table_init (&flinfo.local_hash_table,
12151 local_hash_newfunc,
12152 sizeof (struct local_hash_entry)))
12153 return false;
12154
12155 /* The object attributes have been merged. Remove the input
12156 sections from the link, and set the contents of the output
12157 section. */
12158 sections_removed = false;
12159 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
12160 for (o = abfd->sections; o != NULL; o = o->next)
12161 {
12162 bool remove_section = false;
12163
12164 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
12165 || strcmp (o->name, ".gnu.attributes") == 0)
12166 {
12167 for (p = o->map_head.link_order; p != NULL; p = p->next)
12168 {
12169 asection *input_section;
12170
12171 if (p->type != bfd_indirect_link_order)
12172 continue;
12173 input_section = p->u.indirect.section;
12174 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12175 elf_link_input_bfd ignores this section. */
12176 input_section->flags &= ~SEC_HAS_CONTENTS;
12177 }
12178
12179 attr_size = bfd_elf_obj_attr_size (abfd);
12180 bfd_set_section_size (o, attr_size);
12181 /* Skip this section later on. */
12182 o->map_head.link_order = NULL;
12183 if (attr_size)
12184 attr_section = o;
12185 else
12186 remove_section = true;
12187 }
12188 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
12189 {
12190 /* Remove empty group section from linker output. */
12191 remove_section = true;
12192 }
12193 if (remove_section)
12194 {
12195 o->flags |= SEC_EXCLUDE;
12196 bfd_section_list_remove (abfd, o);
12197 abfd->section_count--;
12198 sections_removed = true;
12199 }
12200 }
12201 if (sections_removed)
12202 _bfd_fix_excluded_sec_syms (abfd, info);
12203
12204 /* Count up the number of relocations we will output for each output
12205 section, so that we know the sizes of the reloc sections. We
12206 also figure out some maximum sizes. */
12207 max_contents_size = 0;
12208 max_external_reloc_size = 0;
12209 max_internal_reloc_count = 0;
12210 max_sym_count = 0;
12211 max_sym_shndx_count = 0;
12212 merged = false;
12213 for (o = abfd->sections; o != NULL; o = o->next)
12214 {
12215 struct bfd_elf_section_data *esdo = elf_section_data (o);
12216 o->reloc_count = 0;
12217
12218 for (p = o->map_head.link_order; p != NULL; p = p->next)
12219 {
12220 unsigned int reloc_count = 0;
12221 unsigned int additional_reloc_count = 0;
12222 struct bfd_elf_section_data *esdi = NULL;
12223
12224 if (p->type == bfd_section_reloc_link_order
12225 || p->type == bfd_symbol_reloc_link_order)
12226 reloc_count = 1;
12227 else if (p->type == bfd_indirect_link_order)
12228 {
12229 asection *sec;
12230
12231 sec = p->u.indirect.section;
12232
12233 /* Mark all sections which are to be included in the
12234 link. This will normally be every section. We need
12235 to do this so that we can identify any sections which
12236 the linker has decided to not include. */
12237 sec->linker_mark = true;
12238
12239 if (sec->flags & SEC_MERGE)
12240 merged = true;
12241
12242 if (sec->rawsize > max_contents_size)
12243 max_contents_size = sec->rawsize;
12244 if (sec->size > max_contents_size)
12245 max_contents_size = sec->size;
12246
12247 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
12248 && (sec->owner->flags & DYNAMIC) == 0)
12249 {
12250 size_t sym_count;
12251
12252 /* We are interested in just local symbols, not all
12253 symbols. */
12254 if (elf_bad_symtab (sec->owner))
12255 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
12256 / bed->s->sizeof_sym);
12257 else
12258 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
12259
12260 if (sym_count > max_sym_count)
12261 max_sym_count = sym_count;
12262
12263 if (sym_count > max_sym_shndx_count
12264 && elf_symtab_shndx_list (sec->owner) != NULL)
12265 max_sym_shndx_count = sym_count;
12266
12267 if (esdo->this_hdr.sh_type == SHT_REL
12268 || esdo->this_hdr.sh_type == SHT_RELA)
12269 /* Some backends use reloc_count in relocation sections
12270 to count particular types of relocs. Of course,
12271 reloc sections themselves can't have relocations. */
12272 ;
12273 else if (emit_relocs)
12274 {
12275 reloc_count = sec->reloc_count;
12276 if (bed->elf_backend_count_additional_relocs)
12277 {
12278 int c;
12279 c = (*bed->elf_backend_count_additional_relocs) (sec);
12280 additional_reloc_count += c;
12281 }
12282 }
12283 else if (bed->elf_backend_count_relocs)
12284 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
12285
12286 esdi = elf_section_data (sec);
12287
12288 if ((sec->flags & SEC_RELOC) != 0)
12289 {
12290 size_t ext_size = 0;
12291
12292 if (esdi->rel.hdr != NULL)
12293 ext_size = esdi->rel.hdr->sh_size;
12294 if (esdi->rela.hdr != NULL)
12295 ext_size += esdi->rela.hdr->sh_size;
12296
12297 if (ext_size > max_external_reloc_size)
12298 max_external_reloc_size = ext_size;
12299 if (sec->reloc_count > max_internal_reloc_count)
12300 max_internal_reloc_count = sec->reloc_count;
12301 }
12302 }
12303 }
12304
12305 if (reloc_count == 0)
12306 continue;
12307
12308 reloc_count += additional_reloc_count;
12309 o->reloc_count += reloc_count;
12310
12311 if (p->type == bfd_indirect_link_order && emit_relocs)
12312 {
12313 if (esdi->rel.hdr)
12314 {
12315 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
12316 esdo->rel.count += additional_reloc_count;
12317 }
12318 if (esdi->rela.hdr)
12319 {
12320 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
12321 esdo->rela.count += additional_reloc_count;
12322 }
12323 }
12324 else
12325 {
12326 if (o->use_rela_p)
12327 esdo->rela.count += reloc_count;
12328 else
12329 esdo->rel.count += reloc_count;
12330 }
12331 }
12332
12333 if (o->reloc_count > 0)
12334 o->flags |= SEC_RELOC;
12335 else
12336 {
12337 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12338 set it (this is probably a bug) and if it is set
12339 assign_section_numbers will create a reloc section. */
12340 o->flags &=~ SEC_RELOC;
12341 }
12342
12343 /* If the SEC_ALLOC flag is not set, force the section VMA to
12344 zero. This is done in elf_fake_sections as well, but forcing
12345 the VMA to 0 here will ensure that relocs against these
12346 sections are handled correctly. */
12347 if ((o->flags & SEC_ALLOC) == 0
12348 && ! o->user_set_vma)
12349 o->vma = 0;
12350 }
12351
12352 if (! bfd_link_relocatable (info) && merged)
12353 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
12354
12355 /* Figure out the file positions for everything but the symbol table
12356 and the relocs. We set symcount to force assign_section_numbers
12357 to create a symbol table. */
12358 abfd->symcount = info->strip != strip_all || emit_relocs;
12359 BFD_ASSERT (! abfd->output_has_begun);
12360 if (! _bfd_elf_compute_section_file_positions (abfd, info))
12361 goto error_return;
12362
12363 /* Set sizes, and assign file positions for reloc sections. */
12364 for (o = abfd->sections; o != NULL; o = o->next)
12365 {
12366 struct bfd_elf_section_data *esdo = elf_section_data (o);
12367 if ((o->flags & SEC_RELOC) != 0)
12368 {
12369 if (esdo->rel.hdr
12370 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
12371 goto error_return;
12372
12373 if (esdo->rela.hdr
12374 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
12375 goto error_return;
12376 }
12377
12378 /* _bfd_elf_compute_section_file_positions makes temporary use
12379 of target_index. Reset it. */
12380 o->target_index = 0;
12381
12382 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12383 to count upwards while actually outputting the relocations. */
12384 esdo->rel.count = 0;
12385 esdo->rela.count = 0;
12386
12387 if ((esdo->this_hdr.sh_offset == (file_ptr) -1)
12388 && !bfd_section_is_ctf (o))
12389 {
12390 /* Cache the section contents so that they can be compressed
12391 later. Use bfd_malloc since it will be freed by
12392 bfd_compress_section_contents. */
12393 unsigned char *contents = esdo->this_hdr.contents;
12394 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
12395 abort ();
12396 contents
12397 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
12398 if (contents == NULL)
12399 goto error_return;
12400 esdo->this_hdr.contents = contents;
12401 }
12402 }
12403
12404 /* We have now assigned file positions for all the sections except .symtab,
12405 .strtab, and non-loaded reloc and compressed debugging sections. We start
12406 the .symtab section at the current file position, and write directly to it.
12407 We build the .strtab section in memory. */
12408 abfd->symcount = 0;
12409 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12410 /* sh_name is set in prep_headers. */
12411 symtab_hdr->sh_type = SHT_SYMTAB;
12412 /* sh_flags, sh_addr and sh_size all start off zero. */
12413 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
12414 /* sh_link is set in assign_section_numbers. */
12415 /* sh_info is set below. */
12416 /* sh_offset is set just below. */
12417 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
12418
12419 if (max_sym_count < 20)
12420 max_sym_count = 20;
12421 htab->strtabsize = max_sym_count;
12422 amt = max_sym_count * sizeof (struct elf_sym_strtab);
12423 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
12424 if (htab->strtab == NULL)
12425 goto error_return;
12426 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12427 flinfo.symshndxbuf
12428 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
12429 ? (Elf_External_Sym_Shndx *) -1 : NULL);
12430
12431 if (info->strip != strip_all || emit_relocs)
12432 {
12433 file_ptr off = elf_next_file_pos (abfd);
12434
12435 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, true);
12436
12437 /* Note that at this point elf_next_file_pos (abfd) is
12438 incorrect. We do not yet know the size of the .symtab section.
12439 We correct next_file_pos below, after we do know the size. */
12440
12441 /* Start writing out the symbol table. The first symbol is always a
12442 dummy symbol. */
12443 elfsym.st_value = 0;
12444 elfsym.st_size = 0;
12445 elfsym.st_info = 0;
12446 elfsym.st_other = 0;
12447 elfsym.st_shndx = SHN_UNDEF;
12448 elfsym.st_target_internal = 0;
12449 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
12450 bfd_und_section_ptr, NULL) != 1)
12451 goto error_return;
12452
12453 /* Output a symbol for each section if asked or they are used for
12454 relocs. These symbols usually have no names. We store the
12455 index of each one in the index field of the section, so that
12456 we can find it again when outputting relocs. */
12457
12458 if (bfd_keep_unused_section_symbols (abfd) || emit_relocs)
12459 {
12460 bool name_local_sections
12461 = (bed->elf_backend_name_local_section_symbols
12462 && bed->elf_backend_name_local_section_symbols (abfd));
12463 const char *name = NULL;
12464
12465 elfsym.st_size = 0;
12466 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12467 elfsym.st_other = 0;
12468 elfsym.st_value = 0;
12469 elfsym.st_target_internal = 0;
12470 for (i = 1; i < elf_numsections (abfd); i++)
12471 {
12472 o = bfd_section_from_elf_index (abfd, i);
12473 if (o != NULL)
12474 {
12475 o->target_index = bfd_get_symcount (abfd);
12476 elfsym.st_shndx = i;
12477 if (!bfd_link_relocatable (info))
12478 elfsym.st_value = o->vma;
12479 if (name_local_sections)
12480 name = o->name;
12481 if (elf_link_output_symstrtab (&flinfo, name, &elfsym, o,
12482 NULL) != 1)
12483 goto error_return;
12484 }
12485 }
12486 }
12487 }
12488
12489 /* On some targets like Irix 5 the symbol split between local and global
12490 ones recorded in the sh_info field needs to be done between section
12491 and all other symbols. */
12492 if (bed->elf_backend_elfsym_local_is_section
12493 && bed->elf_backend_elfsym_local_is_section (abfd))
12494 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12495
12496 /* Allocate some memory to hold information read in from the input
12497 files. */
12498 if (max_contents_size != 0)
12499 {
12500 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12501 if (flinfo.contents == NULL)
12502 goto error_return;
12503 }
12504
12505 if (max_external_reloc_size != 0)
12506 {
12507 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
12508 if (flinfo.external_relocs == NULL)
12509 goto error_return;
12510 }
12511
12512 if (max_internal_reloc_count != 0)
12513 {
12514 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
12515 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
12516 if (flinfo.internal_relocs == NULL)
12517 goto error_return;
12518 }
12519
12520 if (max_sym_count != 0)
12521 {
12522 amt = max_sym_count * bed->s->sizeof_sym;
12523 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
12524 if (flinfo.external_syms == NULL)
12525 goto error_return;
12526
12527 amt = max_sym_count * sizeof (Elf_Internal_Sym);
12528 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
12529 if (flinfo.internal_syms == NULL)
12530 goto error_return;
12531
12532 amt = max_sym_count * sizeof (long);
12533 flinfo.indices = (long int *) bfd_malloc (amt);
12534 if (flinfo.indices == NULL)
12535 goto error_return;
12536
12537 amt = max_sym_count * sizeof (asection *);
12538 flinfo.sections = (asection **) bfd_malloc (amt);
12539 if (flinfo.sections == NULL)
12540 goto error_return;
12541 }
12542
12543 if (max_sym_shndx_count != 0)
12544 {
12545 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
12546 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
12547 if (flinfo.locsym_shndx == NULL)
12548 goto error_return;
12549 }
12550
12551 if (htab->tls_sec)
12552 {
12553 bfd_vma base, end = 0; /* Both bytes. */
12554 asection *sec;
12555
12556 for (sec = htab->tls_sec;
12557 sec && (sec->flags & SEC_THREAD_LOCAL);
12558 sec = sec->next)
12559 {
12560 bfd_size_type size = sec->size;
12561 unsigned int opb = bfd_octets_per_byte (abfd, sec);
12562
12563 if (size == 0
12564 && (sec->flags & SEC_HAS_CONTENTS) == 0)
12565 {
12566 struct bfd_link_order *ord = sec->map_tail.link_order;
12567
12568 if (ord != NULL)
12569 size = ord->offset * opb + ord->size;
12570 }
12571 end = sec->vma + size / opb;
12572 }
12573 base = htab->tls_sec->vma;
12574 /* Only align end of TLS section if static TLS doesn't have special
12575 alignment requirements. */
12576 if (bed->static_tls_alignment == 1)
12577 end = align_power (end, htab->tls_sec->alignment_power);
12578 htab->tls_size = end - base;
12579 }
12580
12581 if (!_bfd_elf_fixup_eh_frame_hdr (info))
12582 return false;
12583
12584 /* Since ELF permits relocations to be against local symbols, we
12585 must have the local symbols available when we do the relocations.
12586 Since we would rather only read the local symbols once, and we
12587 would rather not keep them in memory, we handle all the
12588 relocations for a single input file at the same time.
12589
12590 Unfortunately, there is no way to know the total number of local
12591 symbols until we have seen all of them, and the local symbol
12592 indices precede the global symbol indices. This means that when
12593 we are generating relocatable output, and we see a reloc against
12594 a global symbol, we can not know the symbol index until we have
12595 finished examining all the local symbols to see which ones we are
12596 going to output. To deal with this, we keep the relocations in
12597 memory, and don't output them until the end of the link. This is
12598 an unfortunate waste of memory, but I don't see a good way around
12599 it. Fortunately, it only happens when performing a relocatable
12600 link, which is not the common case. FIXME: If keep_memory is set
12601 we could write the relocs out and then read them again; I don't
12602 know how bad the memory loss will be. */
12603
12604 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12605 sub->output_has_begun = false;
12606 for (o = abfd->sections; o != NULL; o = o->next)
12607 {
12608 for (p = o->map_head.link_order; p != NULL; p = p->next)
12609 {
12610 if (p->type == bfd_indirect_link_order
12611 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12612 == bfd_target_elf_flavour)
12613 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12614 {
12615 if (! sub->output_has_begun)
12616 {
12617 if (! elf_link_input_bfd (&flinfo, sub))
12618 goto error_return;
12619 sub->output_has_begun = true;
12620 }
12621 }
12622 else if (p->type == bfd_section_reloc_link_order
12623 || p->type == bfd_symbol_reloc_link_order)
12624 {
12625 if (! elf_reloc_link_order (abfd, info, o, p))
12626 goto error_return;
12627 }
12628 else
12629 {
12630 if (! _bfd_default_link_order (abfd, info, o, p))
12631 {
12632 if (p->type == bfd_indirect_link_order
12633 && (bfd_get_flavour (sub)
12634 == bfd_target_elf_flavour)
12635 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12636 != bed->s->elfclass))
12637 {
12638 const char *iclass, *oclass;
12639
12640 switch (bed->s->elfclass)
12641 {
12642 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12643 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12644 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12645 default: abort ();
12646 }
12647
12648 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
12649 {
12650 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12651 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12652 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12653 default: abort ();
12654 }
12655
12656 bfd_set_error (bfd_error_wrong_format);
12657 _bfd_error_handler
12658 /* xgettext:c-format */
12659 (_("%pB: file class %s incompatible with %s"),
12660 sub, iclass, oclass);
12661 }
12662
12663 goto error_return;
12664 }
12665 }
12666 }
12667 }
12668
12669 /* Free symbol buffer if needed. */
12670 if (!info->reduce_memory_overheads)
12671 {
12672 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12673 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
12674 {
12675 free (elf_tdata (sub)->symbuf);
12676 elf_tdata (sub)->symbuf = NULL;
12677 }
12678 }
12679
12680 ret = true;
12681
12682 /* Output any global symbols that got converted to local in a
12683 version script or due to symbol visibility. We do this in a
12684 separate step since ELF requires all local symbols to appear
12685 prior to any global symbols. FIXME: We should only do this if
12686 some global symbols were, in fact, converted to become local.
12687 FIXME: Will this work correctly with the Irix 5 linker? */
12688 eoinfo.failed = false;
12689 eoinfo.flinfo = &flinfo;
12690 eoinfo.localsyms = true;
12691 eoinfo.file_sym_done = false;
12692 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12693 if (eoinfo.failed)
12694 {
12695 ret = false;
12696 goto return_local_hash_table;
12697 }
12698
12699 /* If backend needs to output some local symbols not present in the hash
12700 table, do it now. */
12701 if (bed->elf_backend_output_arch_local_syms
12702 && (info->strip != strip_all || emit_relocs))
12703 {
12704 if (! ((*bed->elf_backend_output_arch_local_syms)
12705 (abfd, info, &flinfo, elf_link_output_symstrtab)))
12706 {
12707 ret = false;
12708 goto return_local_hash_table;
12709 }
12710 }
12711
12712 /* That wrote out all the local symbols. Finish up the symbol table
12713 with the global symbols. Even if we want to strip everything we
12714 can, we still need to deal with those global symbols that got
12715 converted to local in a version script. */
12716
12717 /* The sh_info field records the index of the first non local symbol. */
12718 if (!symtab_hdr->sh_info)
12719 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12720
12721 if (dynamic
12722 && htab->dynsym != NULL
12723 && htab->dynsym->output_section != bfd_abs_section_ptr)
12724 {
12725 Elf_Internal_Sym sym;
12726 bfd_byte *dynsym = htab->dynsym->contents;
12727
12728 o = htab->dynsym->output_section;
12729 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
12730
12731 /* Write out the section symbols for the output sections. */
12732 if (bfd_link_pic (info)
12733 || htab->is_relocatable_executable)
12734 {
12735 asection *s;
12736
12737 sym.st_size = 0;
12738 sym.st_name = 0;
12739 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12740 sym.st_other = 0;
12741 sym.st_target_internal = 0;
12742
12743 for (s = abfd->sections; s != NULL; s = s->next)
12744 {
12745 int indx;
12746 bfd_byte *dest;
12747 long dynindx;
12748
12749 dynindx = elf_section_data (s)->dynindx;
12750 if (dynindx <= 0)
12751 continue;
12752 indx = elf_section_data (s)->this_idx;
12753 BFD_ASSERT (indx > 0);
12754 sym.st_shndx = indx;
12755 if (! check_dynsym (abfd, &sym))
12756 {
12757 ret = false;
12758 goto return_local_hash_table;
12759 }
12760 sym.st_value = s->vma;
12761 dest = dynsym + dynindx * bed->s->sizeof_sym;
12762
12763 /* Inform the linker of the addition of this symbol. */
12764
12765 if (info->callbacks->ctf_new_dynsym)
12766 info->callbacks->ctf_new_dynsym (dynindx, &sym);
12767
12768 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12769 }
12770 }
12771
12772 /* Write out the local dynsyms. */
12773 if (htab->dynlocal)
12774 {
12775 struct elf_link_local_dynamic_entry *e;
12776 for (e = htab->dynlocal; e ; e = e->next)
12777 {
12778 asection *s;
12779 bfd_byte *dest;
12780
12781 /* Copy the internal symbol and turn off visibility.
12782 Note that we saved a word of storage and overwrote
12783 the original st_name with the dynstr_index. */
12784 sym = e->isym;
12785 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
12786 sym.st_shndx = SHN_UNDEF;
12787
12788 s = bfd_section_from_elf_index (e->input_bfd,
12789 e->isym.st_shndx);
12790 if (s != NULL
12791 && s->output_section != NULL
12792 && elf_section_data (s->output_section) != NULL)
12793 {
12794 sym.st_shndx =
12795 elf_section_data (s->output_section)->this_idx;
12796 if (! check_dynsym (abfd, &sym))
12797 {
12798 ret = false;
12799 goto return_local_hash_table;
12800 }
12801 sym.st_value = (s->output_section->vma
12802 + s->output_offset
12803 + e->isym.st_value);
12804 }
12805
12806 /* Inform the linker of the addition of this symbol. */
12807
12808 if (info->callbacks->ctf_new_dynsym)
12809 info->callbacks->ctf_new_dynsym (e->dynindx, &sym);
12810
12811 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12812 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12813 }
12814 }
12815 }
12816
12817 /* We get the global symbols from the hash table. */
12818 eoinfo.failed = false;
12819 eoinfo.localsyms = false;
12820 eoinfo.flinfo = &flinfo;
12821 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12822 if (eoinfo.failed)
12823 {
12824 ret = false;
12825 goto return_local_hash_table;
12826 }
12827
12828 /* If backend needs to output some symbols not present in the hash
12829 table, do it now. */
12830 if (bed->elf_backend_output_arch_syms
12831 && (info->strip != strip_all || emit_relocs))
12832 {
12833 if (! ((*bed->elf_backend_output_arch_syms)
12834 (abfd, info, &flinfo, elf_link_output_symstrtab)))
12835 {
12836 ret = false;
12837 goto return_local_hash_table;
12838 }
12839 }
12840
12841 /* Finalize the .strtab section. */
12842 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12843
12844 /* Swap out the .strtab section. */
12845 if (!elf_link_swap_symbols_out (&flinfo))
12846 {
12847 ret = false;
12848 goto return_local_hash_table;
12849 }
12850
12851 /* Now we know the size of the symtab section. */
12852 if (bfd_get_symcount (abfd) > 0)
12853 {
12854 /* Finish up and write out the symbol string table (.strtab)
12855 section. */
12856 Elf_Internal_Shdr *symstrtab_hdr = NULL;
12857 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12858
12859 if (elf_symtab_shndx_list (abfd))
12860 {
12861 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
12862
12863 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12864 {
12865 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12866 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12867 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12868 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12869 symtab_shndx_hdr->sh_size = amt;
12870
12871 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12872 off, true);
12873
12874 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12875 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12876 {
12877 ret = false;
12878 goto return_local_hash_table;
12879 }
12880 }
12881 }
12882
12883 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12884 /* sh_name was set in prep_headers. */
12885 symstrtab_hdr->sh_type = SHT_STRTAB;
12886 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
12887 symstrtab_hdr->sh_addr = 0;
12888 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
12889 symstrtab_hdr->sh_entsize = 0;
12890 symstrtab_hdr->sh_link = 0;
12891 symstrtab_hdr->sh_info = 0;
12892 /* sh_offset is set just below. */
12893 symstrtab_hdr->sh_addralign = 1;
12894
12895 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12896 off, true);
12897 elf_next_file_pos (abfd) = off;
12898
12899 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
12900 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
12901 {
12902 ret = false;
12903 goto return_local_hash_table;
12904 }
12905 }
12906
12907 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12908 {
12909 _bfd_error_handler (_("%pB: failed to generate import library"),
12910 info->out_implib_bfd);
12911 ret = false;
12912 goto return_local_hash_table;
12913 }
12914
12915 /* Adjust the relocs to have the correct symbol indices. */
12916 for (o = abfd->sections; o != NULL; o = o->next)
12917 {
12918 struct bfd_elf_section_data *esdo = elf_section_data (o);
12919 bool sort;
12920
12921 if ((o->flags & SEC_RELOC) == 0)
12922 continue;
12923
12924 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
12925 if (esdo->rel.hdr != NULL
12926 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
12927 {
12928 ret = false;
12929 goto return_local_hash_table;
12930 }
12931 if (esdo->rela.hdr != NULL
12932 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
12933 {
12934 ret = false;
12935 goto return_local_hash_table;
12936 }
12937
12938 /* Set the reloc_count field to 0 to prevent write_relocs from
12939 trying to swap the relocs out itself. */
12940 o->reloc_count = 0;
12941 }
12942
12943 if (dynamic && info->combreloc && dynobj != NULL)
12944 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12945
12946 /* If we are linking against a dynamic object, or generating a
12947 shared library, finish up the dynamic linking information. */
12948 if (dynamic)
12949 {
12950 bfd_byte *dyncon, *dynconend;
12951
12952 /* Fix up .dynamic entries. */
12953 o = bfd_get_linker_section (dynobj, ".dynamic");
12954 BFD_ASSERT (o != NULL);
12955
12956 dyncon = o->contents;
12957 dynconend = PTR_ADD (o->contents, o->size);
12958 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12959 {
12960 Elf_Internal_Dyn dyn;
12961 const char *name;
12962 unsigned int type;
12963 bfd_size_type sh_size;
12964 bfd_vma sh_addr;
12965
12966 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12967
12968 switch (dyn.d_tag)
12969 {
12970 default:
12971 continue;
12972 case DT_NULL:
12973 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12974 {
12975 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12976 {
12977 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12978 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12979 default: continue;
12980 }
12981 dyn.d_un.d_val = relativecount;
12982 relativecount = 0;
12983 break;
12984 }
12985 continue;
12986
12987 case DT_INIT:
12988 name = info->init_function;
12989 goto get_sym;
12990 case DT_FINI:
12991 name = info->fini_function;
12992 get_sym:
12993 {
12994 struct elf_link_hash_entry *h;
12995
12996 h = elf_link_hash_lookup (htab, name, false, false, true);
12997 if (h != NULL
12998 && (h->root.type == bfd_link_hash_defined
12999 || h->root.type == bfd_link_hash_defweak))
13000 {
13001 dyn.d_un.d_ptr = h->root.u.def.value;
13002 o = h->root.u.def.section;
13003 if (o->output_section != NULL)
13004 dyn.d_un.d_ptr += (o->output_section->vma
13005 + o->output_offset);
13006 else
13007 {
13008 /* The symbol is imported from another shared
13009 library and does not apply to this one. */
13010 dyn.d_un.d_ptr = 0;
13011 }
13012 break;
13013 }
13014 }
13015 continue;
13016
13017 case DT_PREINIT_ARRAYSZ:
13018 name = ".preinit_array";
13019 goto get_out_size;
13020 case DT_INIT_ARRAYSZ:
13021 name = ".init_array";
13022 goto get_out_size;
13023 case DT_FINI_ARRAYSZ:
13024 name = ".fini_array";
13025 get_out_size:
13026 o = bfd_get_section_by_name (abfd, name);
13027 if (o == NULL)
13028 {
13029 _bfd_error_handler
13030 (_("could not find section %s"), name);
13031 goto error_return;
13032 }
13033 if (o->size == 0)
13034 _bfd_error_handler
13035 (_("warning: %s section has zero size"), name);
13036 dyn.d_un.d_val = o->size;
13037 break;
13038
13039 case DT_PREINIT_ARRAY:
13040 name = ".preinit_array";
13041 goto get_out_vma;
13042 case DT_INIT_ARRAY:
13043 name = ".init_array";
13044 goto get_out_vma;
13045 case DT_FINI_ARRAY:
13046 name = ".fini_array";
13047 get_out_vma:
13048 o = bfd_get_section_by_name (abfd, name);
13049 goto do_vma;
13050
13051 case DT_HASH:
13052 name = ".hash";
13053 goto get_vma;
13054 case DT_GNU_HASH:
13055 name = ".gnu.hash";
13056 goto get_vma;
13057 case DT_STRTAB:
13058 name = ".dynstr";
13059 goto get_vma;
13060 case DT_SYMTAB:
13061 name = ".dynsym";
13062 goto get_vma;
13063 case DT_VERDEF:
13064 name = ".gnu.version_d";
13065 goto get_vma;
13066 case DT_VERNEED:
13067 name = ".gnu.version_r";
13068 goto get_vma;
13069 case DT_VERSYM:
13070 name = ".gnu.version";
13071 get_vma:
13072 o = bfd_get_linker_section (dynobj, name);
13073 do_vma:
13074 if (o == NULL || bfd_is_abs_section (o->output_section))
13075 {
13076 _bfd_error_handler
13077 (_("could not find section %s"), name);
13078 goto error_return;
13079 }
13080 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
13081 {
13082 _bfd_error_handler
13083 (_("warning: section '%s' is being made into a note"), name);
13084 bfd_set_error (bfd_error_nonrepresentable_section);
13085 goto error_return;
13086 }
13087 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
13088 break;
13089
13090 case DT_REL:
13091 case DT_RELA:
13092 case DT_RELSZ:
13093 case DT_RELASZ:
13094 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
13095 type = SHT_REL;
13096 else
13097 type = SHT_RELA;
13098 sh_size = 0;
13099 sh_addr = 0;
13100 for (i = 1; i < elf_numsections (abfd); i++)
13101 {
13102 Elf_Internal_Shdr *hdr;
13103
13104 hdr = elf_elfsections (abfd)[i];
13105 if (hdr->sh_type == type
13106 && (hdr->sh_flags & SHF_ALLOC) != 0)
13107 {
13108 sh_size += hdr->sh_size;
13109 if (sh_addr == 0
13110 || sh_addr > hdr->sh_addr)
13111 sh_addr = hdr->sh_addr;
13112 }
13113 }
13114
13115 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
13116 {
13117 unsigned int opb = bfd_octets_per_byte (abfd, o);
13118
13119 /* Don't count procedure linkage table relocs in the
13120 overall reloc count. */
13121 sh_size -= htab->srelplt->size;
13122 if (sh_size == 0)
13123 /* If the size is zero, make the address zero too.
13124 This is to avoid a glibc bug. If the backend
13125 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
13126 zero, then we'll put DT_RELA at the end of
13127 DT_JMPREL. glibc will interpret the end of
13128 DT_RELA matching the end of DT_JMPREL as the
13129 case where DT_RELA includes DT_JMPREL, and for
13130 LD_BIND_NOW will decide that processing DT_RELA
13131 will process the PLT relocs too. Net result:
13132 No PLT relocs applied. */
13133 sh_addr = 0;
13134
13135 /* If .rela.plt is the first .rela section, exclude
13136 it from DT_RELA. */
13137 else if (sh_addr == (htab->srelplt->output_section->vma
13138 + htab->srelplt->output_offset) * opb)
13139 sh_addr += htab->srelplt->size;
13140 }
13141
13142 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
13143 dyn.d_un.d_val = sh_size;
13144 else
13145 dyn.d_un.d_ptr = sh_addr;
13146 break;
13147 }
13148 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13149 }
13150 }
13151
13152 /* If we have created any dynamic sections, then output them. */
13153 if (dynobj != NULL)
13154 {
13155 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
13156 goto error_return;
13157
13158 /* Check for DT_TEXTREL (late, in case the backend removes it). */
13159 if (bfd_link_textrel_check (info)
13160 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL
13161 && o->size != 0)
13162 {
13163 bfd_byte *dyncon, *dynconend;
13164
13165 dyncon = o->contents;
13166 dynconend = o->contents + o->size;
13167 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13168 {
13169 Elf_Internal_Dyn dyn;
13170
13171 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13172
13173 if (dyn.d_tag == DT_TEXTREL)
13174 {
13175 if (info->textrel_check == textrel_check_error)
13176 info->callbacks->einfo
13177 (_("%P%X: read-only segment has dynamic relocations\n"));
13178 else if (bfd_link_dll (info))
13179 info->callbacks->einfo
13180 (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
13181 else if (bfd_link_pde (info))
13182 info->callbacks->einfo
13183 (_("%P: warning: creating DT_TEXTREL in a PDE\n"));
13184 else
13185 info->callbacks->einfo
13186 (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
13187 break;
13188 }
13189 }
13190 }
13191
13192 for (o = dynobj->sections; o != NULL; o = o->next)
13193 {
13194 if ((o->flags & SEC_HAS_CONTENTS) == 0
13195 || o->size == 0
13196 || o->output_section == bfd_abs_section_ptr)
13197 continue;
13198 if ((o->flags & SEC_LINKER_CREATED) == 0)
13199 {
13200 /* At this point, we are only interested in sections
13201 created by _bfd_elf_link_create_dynamic_sections. */
13202 continue;
13203 }
13204 if (htab->stab_info.stabstr == o)
13205 continue;
13206 if (htab->eh_info.hdr_sec == o)
13207 continue;
13208 if (strcmp (o->name, ".dynstr") != 0)
13209 {
13210 bfd_size_type octets = ((file_ptr) o->output_offset
13211 * bfd_octets_per_byte (abfd, o));
13212 if (!bfd_set_section_contents (abfd, o->output_section,
13213 o->contents, octets, o->size))
13214 goto error_return;
13215 }
13216 else
13217 {
13218 /* The contents of the .dynstr section are actually in a
13219 stringtab. */
13220 file_ptr off;
13221
13222 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
13223 if (bfd_seek (abfd, off, SEEK_SET) != 0
13224 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
13225 goto error_return;
13226 }
13227 }
13228 }
13229
13230 if (!info->resolve_section_groups)
13231 {
13232 bool failed = false;
13233
13234 BFD_ASSERT (bfd_link_relocatable (info));
13235 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
13236 if (failed)
13237 goto error_return;
13238 }
13239
13240 /* If we have optimized stabs strings, output them. */
13241 if (htab->stab_info.stabstr != NULL)
13242 {
13243 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
13244 goto error_return;
13245 }
13246
13247 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
13248 goto error_return;
13249
13250 if (info->callbacks->emit_ctf)
13251 info->callbacks->emit_ctf ();
13252
13253 elf_final_link_free (abfd, &flinfo);
13254
13255 if (attr_section)
13256 {
13257 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
13258 if (contents == NULL)
13259 {
13260 /* Bail out and fail. */
13261 ret = false;
13262 goto return_local_hash_table;
13263 }
13264 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
13265 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
13266 free (contents);
13267 }
13268
13269 return_local_hash_table:
13270 if (info->unique_symbol)
13271 bfd_hash_table_free (&flinfo.local_hash_table);
13272 return ret;
13273
13274 error_return:
13275 elf_final_link_free (abfd, &flinfo);
13276 ret = false;
13277 goto return_local_hash_table;
13278 }
13279 \f
13280 /* Initialize COOKIE for input bfd ABFD. */
13281
13282 static bool
13283 init_reloc_cookie (struct elf_reloc_cookie *cookie,
13284 struct bfd_link_info *info, bfd *abfd)
13285 {
13286 Elf_Internal_Shdr *symtab_hdr;
13287 const struct elf_backend_data *bed;
13288
13289 bed = get_elf_backend_data (abfd);
13290 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13291
13292 cookie->abfd = abfd;
13293 cookie->sym_hashes = elf_sym_hashes (abfd);
13294 cookie->bad_symtab = elf_bad_symtab (abfd);
13295 if (cookie->bad_symtab)
13296 {
13297 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13298 cookie->extsymoff = 0;
13299 }
13300 else
13301 {
13302 cookie->locsymcount = symtab_hdr->sh_info;
13303 cookie->extsymoff = symtab_hdr->sh_info;
13304 }
13305
13306 if (bed->s->arch_size == 32)
13307 cookie->r_sym_shift = 8;
13308 else
13309 cookie->r_sym_shift = 32;
13310
13311 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
13312 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
13313 {
13314 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
13315 cookie->locsymcount, 0,
13316 NULL, NULL, NULL);
13317 if (cookie->locsyms == NULL)
13318 {
13319 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
13320 return false;
13321 }
13322 if (_bfd_link_keep_memory (info) )
13323 {
13324 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
13325 info->cache_size += (cookie->locsymcount
13326 * sizeof (Elf_External_Sym_Shndx));
13327 }
13328 }
13329 return true;
13330 }
13331
13332 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
13333
13334 static void
13335 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
13336 {
13337 Elf_Internal_Shdr *symtab_hdr;
13338
13339 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13340 if (symtab_hdr->contents != (unsigned char *) cookie->locsyms)
13341 free (cookie->locsyms);
13342 }
13343
13344 /* Initialize the relocation information in COOKIE for input section SEC
13345 of input bfd ABFD. */
13346
13347 static bool
13348 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13349 struct bfd_link_info *info, bfd *abfd,
13350 asection *sec)
13351 {
13352 if (sec->reloc_count == 0)
13353 {
13354 cookie->rels = NULL;
13355 cookie->relend = NULL;
13356 }
13357 else
13358 {
13359 cookie->rels = _bfd_elf_link_info_read_relocs (abfd, info, sec,
13360 NULL, NULL,
13361 _bfd_link_keep_memory (info));
13362 if (cookie->rels == NULL)
13363 return false;
13364 cookie->rel = cookie->rels;
13365 cookie->relend = cookie->rels + sec->reloc_count;
13366 }
13367 cookie->rel = cookie->rels;
13368 return true;
13369 }
13370
13371 /* Free the memory allocated by init_reloc_cookie_rels,
13372 if appropriate. */
13373
13374 static void
13375 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13376 asection *sec)
13377 {
13378 if (elf_section_data (sec)->relocs != cookie->rels)
13379 free (cookie->rels);
13380 }
13381
13382 /* Initialize the whole of COOKIE for input section SEC. */
13383
13384 static bool
13385 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13386 struct bfd_link_info *info,
13387 asection *sec)
13388 {
13389 if (!init_reloc_cookie (cookie, info, sec->owner))
13390 goto error1;
13391 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
13392 goto error2;
13393 return true;
13394
13395 error2:
13396 fini_reloc_cookie (cookie, sec->owner);
13397 error1:
13398 return false;
13399 }
13400
13401 /* Free the memory allocated by init_reloc_cookie_for_section,
13402 if appropriate. */
13403
13404 static void
13405 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13406 asection *sec)
13407 {
13408 fini_reloc_cookie_rels (cookie, sec);
13409 fini_reloc_cookie (cookie, sec->owner);
13410 }
13411 \f
13412 /* Garbage collect unused sections. */
13413
13414 /* Default gc_mark_hook. */
13415
13416 asection *
13417 _bfd_elf_gc_mark_hook (asection *sec,
13418 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13419 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13420 struct elf_link_hash_entry *h,
13421 Elf_Internal_Sym *sym)
13422 {
13423 if (h != NULL)
13424 {
13425 switch (h->root.type)
13426 {
13427 case bfd_link_hash_defined:
13428 case bfd_link_hash_defweak:
13429 return h->root.u.def.section;
13430
13431 case bfd_link_hash_common:
13432 return h->root.u.c.p->section;
13433
13434 default:
13435 break;
13436 }
13437 }
13438 else
13439 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
13440
13441 return NULL;
13442 }
13443
13444 /* Return the debug definition section. */
13445
13446 static asection *
13447 elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
13448 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13449 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13450 struct elf_link_hash_entry *h,
13451 Elf_Internal_Sym *sym)
13452 {
13453 if (h != NULL)
13454 {
13455 /* Return the global debug definition section. */
13456 if ((h->root.type == bfd_link_hash_defined
13457 || h->root.type == bfd_link_hash_defweak)
13458 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
13459 return h->root.u.def.section;
13460 }
13461 else
13462 {
13463 /* Return the local debug definition section. */
13464 asection *isec = bfd_section_from_elf_index (sec->owner,
13465 sym->st_shndx);
13466 if ((isec->flags & SEC_DEBUGGING) != 0)
13467 return isec;
13468 }
13469
13470 return NULL;
13471 }
13472
13473 /* COOKIE->rel describes a relocation against section SEC, which is
13474 a section we've decided to keep. Return the section that contains
13475 the relocation symbol, or NULL if no section contains it. */
13476
13477 asection *
13478 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
13479 elf_gc_mark_hook_fn gc_mark_hook,
13480 struct elf_reloc_cookie *cookie,
13481 bool *start_stop)
13482 {
13483 unsigned long r_symndx;
13484 struct elf_link_hash_entry *h, *hw;
13485
13486 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
13487 if (r_symndx == STN_UNDEF)
13488 return NULL;
13489
13490 if (r_symndx >= cookie->locsymcount
13491 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13492 {
13493 bool was_marked;
13494
13495 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
13496 if (h == NULL)
13497 {
13498 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
13499 sec->owner);
13500 return NULL;
13501 }
13502 while (h->root.type == bfd_link_hash_indirect
13503 || h->root.type == bfd_link_hash_warning)
13504 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13505
13506 was_marked = h->mark;
13507 h->mark = 1;
13508 /* Keep all aliases of the symbol too. If an object symbol
13509 needs to be copied into .dynbss then all of its aliases
13510 should be present as dynamic symbols, not just the one used
13511 on the copy relocation. */
13512 hw = h;
13513 while (hw->is_weakalias)
13514 {
13515 hw = hw->u.alias;
13516 hw->mark = 1;
13517 }
13518
13519 if (!was_marked && h->start_stop && !h->root.ldscript_def)
13520 {
13521 if (info->start_stop_gc)
13522 return NULL;
13523
13524 /* To work around a glibc bug, mark XXX input sections
13525 when there is a reference to __start_XXX or __stop_XXX
13526 symbols. */
13527 else if (start_stop != NULL)
13528 {
13529 asection *s = h->u2.start_stop_section;
13530 *start_stop = true;
13531 return s;
13532 }
13533 }
13534
13535 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
13536 }
13537
13538 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
13539 &cookie->locsyms[r_symndx]);
13540 }
13541
13542 /* COOKIE->rel describes a relocation against section SEC, which is
13543 a section we've decided to keep. Mark the section that contains
13544 the relocation symbol. */
13545
13546 bool
13547 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
13548 asection *sec,
13549 elf_gc_mark_hook_fn gc_mark_hook,
13550 struct elf_reloc_cookie *cookie)
13551 {
13552 asection *rsec;
13553 bool start_stop = false;
13554
13555 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
13556 while (rsec != NULL)
13557 {
13558 if (!rsec->gc_mark)
13559 {
13560 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
13561 || (rsec->owner->flags & DYNAMIC) != 0)
13562 rsec->gc_mark = 1;
13563 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
13564 return false;
13565 }
13566 if (!start_stop)
13567 break;
13568 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
13569 }
13570 return true;
13571 }
13572
13573 /* The mark phase of garbage collection. For a given section, mark
13574 it and any sections in this section's group, and all the sections
13575 which define symbols to which it refers. */
13576
13577 bool
13578 _bfd_elf_gc_mark (struct bfd_link_info *info,
13579 asection *sec,
13580 elf_gc_mark_hook_fn gc_mark_hook)
13581 {
13582 bool ret;
13583 asection *group_sec, *eh_frame;
13584
13585 sec->gc_mark = 1;
13586
13587 /* Mark all the sections in the group. */
13588 group_sec = elf_section_data (sec)->next_in_group;
13589 if (group_sec && !group_sec->gc_mark)
13590 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
13591 return false;
13592
13593 /* Look through the section relocs. */
13594 ret = true;
13595 eh_frame = elf_eh_frame_section (sec->owner);
13596 if ((sec->flags & SEC_RELOC) != 0
13597 && sec->reloc_count > 0
13598 && sec != eh_frame)
13599 {
13600 struct elf_reloc_cookie cookie;
13601
13602 if (!init_reloc_cookie_for_section (&cookie, info, sec))
13603 ret = false;
13604 else
13605 {
13606 for (; cookie.rel < cookie.relend; cookie.rel++)
13607 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
13608 {
13609 ret = false;
13610 break;
13611 }
13612 fini_reloc_cookie_for_section (&cookie, sec);
13613 }
13614 }
13615
13616 if (ret && eh_frame && elf_fde_list (sec))
13617 {
13618 struct elf_reloc_cookie cookie;
13619
13620 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
13621 ret = false;
13622 else
13623 {
13624 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
13625 gc_mark_hook, &cookie))
13626 ret = false;
13627 fini_reloc_cookie_for_section (&cookie, eh_frame);
13628 }
13629 }
13630
13631 eh_frame = elf_section_eh_frame_entry (sec);
13632 if (ret && eh_frame && !eh_frame->gc_mark)
13633 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
13634 ret = false;
13635
13636 return ret;
13637 }
13638
13639 /* Scan and mark sections in a special or debug section group. */
13640
13641 static void
13642 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
13643 {
13644 /* Point to first section of section group. */
13645 asection *ssec;
13646 /* Used to iterate the section group. */
13647 asection *msec;
13648
13649 bool is_special_grp = true;
13650 bool is_debug_grp = true;
13651
13652 /* First scan to see if group contains any section other than debug
13653 and special section. */
13654 ssec = msec = elf_next_in_group (grp);
13655 do
13656 {
13657 if ((msec->flags & SEC_DEBUGGING) == 0)
13658 is_debug_grp = false;
13659
13660 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
13661 is_special_grp = false;
13662
13663 msec = elf_next_in_group (msec);
13664 }
13665 while (msec != ssec);
13666
13667 /* If this is a pure debug section group or pure special section group,
13668 keep all sections in this group. */
13669 if (is_debug_grp || is_special_grp)
13670 {
13671 do
13672 {
13673 msec->gc_mark = 1;
13674 msec = elf_next_in_group (msec);
13675 }
13676 while (msec != ssec);
13677 }
13678 }
13679
13680 /* Keep debug and special sections. */
13681
13682 bool
13683 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
13684 elf_gc_mark_hook_fn mark_hook)
13685 {
13686 bfd *ibfd;
13687
13688 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13689 {
13690 asection *isec;
13691 bool some_kept;
13692 bool debug_frag_seen;
13693 bool has_kept_debug_info;
13694
13695 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13696 continue;
13697 isec = ibfd->sections;
13698 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13699 continue;
13700
13701 /* Ensure all linker created sections are kept,
13702 see if any other section is already marked,
13703 and note if we have any fragmented debug sections. */
13704 debug_frag_seen = some_kept = has_kept_debug_info = false;
13705 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13706 {
13707 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13708 isec->gc_mark = 1;
13709 else if (isec->gc_mark
13710 && (isec->flags & SEC_ALLOC) != 0
13711 && elf_section_type (isec) != SHT_NOTE)
13712 some_kept = true;
13713 else
13714 {
13715 /* Since all sections, except for backend specific ones,
13716 have been garbage collected, call mark_hook on this
13717 section if any of its linked-to sections is marked. */
13718 asection *linked_to_sec;
13719 for (linked_to_sec = elf_linked_to_section (isec);
13720 linked_to_sec != NULL && !linked_to_sec->linker_mark;
13721 linked_to_sec = elf_linked_to_section (linked_to_sec))
13722 {
13723 if (linked_to_sec->gc_mark)
13724 {
13725 if (!_bfd_elf_gc_mark (info, isec, mark_hook))
13726 return false;
13727 break;
13728 }
13729 linked_to_sec->linker_mark = 1;
13730 }
13731 for (linked_to_sec = elf_linked_to_section (isec);
13732 linked_to_sec != NULL && linked_to_sec->linker_mark;
13733 linked_to_sec = elf_linked_to_section (linked_to_sec))
13734 linked_to_sec->linker_mark = 0;
13735 }
13736
13737 if (!debug_frag_seen
13738 && (isec->flags & SEC_DEBUGGING)
13739 && startswith (isec->name, ".debug_line."))
13740 debug_frag_seen = true;
13741 else if (strcmp (bfd_section_name (isec),
13742 "__patchable_function_entries") == 0
13743 && elf_linked_to_section (isec) == NULL)
13744 info->callbacks->einfo (_("%F%P: %pB(%pA): error: "
13745 "need linked-to section "
13746 "for --gc-sections\n"),
13747 isec->owner, isec);
13748 }
13749
13750 /* If no non-note alloc section in this file will be kept, then
13751 we can toss out the debug and special sections. */
13752 if (!some_kept)
13753 continue;
13754
13755 /* Keep debug and special sections like .comment when they are
13756 not part of a group. Also keep section groups that contain
13757 just debug sections or special sections. NB: Sections with
13758 linked-to section has been handled above. */
13759 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13760 {
13761 if ((isec->flags & SEC_GROUP) != 0)
13762 _bfd_elf_gc_mark_debug_special_section_group (isec);
13763 else if (((isec->flags & SEC_DEBUGGING) != 0
13764 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
13765 && elf_next_in_group (isec) == NULL
13766 && elf_linked_to_section (isec) == NULL)
13767 isec->gc_mark = 1;
13768 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
13769 has_kept_debug_info = true;
13770 }
13771
13772 /* Look for CODE sections which are going to be discarded,
13773 and find and discard any fragmented debug sections which
13774 are associated with that code section. */
13775 if (debug_frag_seen)
13776 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13777 if ((isec->flags & SEC_CODE) != 0
13778 && isec->gc_mark == 0)
13779 {
13780 unsigned int ilen;
13781 asection *dsec;
13782
13783 ilen = strlen (isec->name);
13784
13785 /* Association is determined by the name of the debug
13786 section containing the name of the code section as
13787 a suffix. For example .debug_line.text.foo is a
13788 debug section associated with .text.foo. */
13789 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
13790 {
13791 unsigned int dlen;
13792
13793 if (dsec->gc_mark == 0
13794 || (dsec->flags & SEC_DEBUGGING) == 0)
13795 continue;
13796
13797 dlen = strlen (dsec->name);
13798
13799 if (dlen > ilen
13800 && strncmp (dsec->name + (dlen - ilen),
13801 isec->name, ilen) == 0)
13802 dsec->gc_mark = 0;
13803 }
13804 }
13805
13806 /* Mark debug sections referenced by kept debug sections. */
13807 if (has_kept_debug_info)
13808 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13809 if (isec->gc_mark
13810 && (isec->flags & SEC_DEBUGGING) != 0)
13811 if (!_bfd_elf_gc_mark (info, isec,
13812 elf_gc_mark_debug_section))
13813 return false;
13814 }
13815 return true;
13816 }
13817
13818 static bool
13819 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
13820 {
13821 bfd *sub;
13822 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13823
13824 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13825 {
13826 asection *o;
13827
13828 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13829 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
13830 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13831 continue;
13832 o = sub->sections;
13833 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13834 continue;
13835
13836 for (o = sub->sections; o != NULL; o = o->next)
13837 {
13838 /* When any section in a section group is kept, we keep all
13839 sections in the section group. If the first member of
13840 the section group is excluded, we will also exclude the
13841 group section. */
13842 if (o->flags & SEC_GROUP)
13843 {
13844 asection *first = elf_next_in_group (o);
13845 o->gc_mark = first->gc_mark;
13846 }
13847
13848 if (o->gc_mark)
13849 continue;
13850
13851 /* Skip sweeping sections already excluded. */
13852 if (o->flags & SEC_EXCLUDE)
13853 continue;
13854
13855 /* Since this is early in the link process, it is simple
13856 to remove a section from the output. */
13857 o->flags |= SEC_EXCLUDE;
13858
13859 if (info->print_gc_sections && o->size != 0)
13860 /* xgettext:c-format */
13861 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
13862 o, sub);
13863 }
13864 }
13865
13866 return true;
13867 }
13868
13869 /* Propagate collected vtable information. This is called through
13870 elf_link_hash_traverse. */
13871
13872 static bool
13873 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13874 {
13875 /* Those that are not vtables. */
13876 if (h->start_stop
13877 || h->u2.vtable == NULL
13878 || h->u2.vtable->parent == NULL)
13879 return true;
13880
13881 /* Those vtables that do not have parents, we cannot merge. */
13882 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
13883 return true;
13884
13885 /* If we've already been done, exit. */
13886 if (h->u2.vtable->used && h->u2.vtable->used[-1])
13887 return true;
13888
13889 /* Make sure the parent's table is up to date. */
13890 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
13891
13892 if (h->u2.vtable->used == NULL)
13893 {
13894 /* None of this table's entries were referenced. Re-use the
13895 parent's table. */
13896 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13897 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
13898 }
13899 else
13900 {
13901 size_t n;
13902 bool *cu, *pu;
13903
13904 /* Or the parent's entries into ours. */
13905 cu = h->u2.vtable->used;
13906 cu[-1] = true;
13907 pu = h->u2.vtable->parent->u2.vtable->used;
13908 if (pu != NULL)
13909 {
13910 const struct elf_backend_data *bed;
13911 unsigned int log_file_align;
13912
13913 bed = get_elf_backend_data (h->root.u.def.section->owner);
13914 log_file_align = bed->s->log_file_align;
13915 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
13916 while (n--)
13917 {
13918 if (*pu)
13919 *cu = true;
13920 pu++;
13921 cu++;
13922 }
13923 }
13924 }
13925
13926 return true;
13927 }
13928
13929 struct link_info_ok
13930 {
13931 struct bfd_link_info *info;
13932 bool ok;
13933 };
13934
13935 static bool
13936 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h,
13937 void *ptr)
13938 {
13939 asection *sec;
13940 bfd_vma hstart, hend;
13941 Elf_Internal_Rela *relstart, *relend, *rel;
13942 const struct elf_backend_data *bed;
13943 unsigned int log_file_align;
13944 struct link_info_ok *info = (struct link_info_ok *) ptr;
13945
13946 /* Take care of both those symbols that do not describe vtables as
13947 well as those that are not loaded. */
13948 if (h->start_stop
13949 || h->u2.vtable == NULL
13950 || h->u2.vtable->parent == NULL)
13951 return true;
13952
13953 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13954 || h->root.type == bfd_link_hash_defweak);
13955
13956 sec = h->root.u.def.section;
13957 hstart = h->root.u.def.value;
13958 hend = hstart + h->size;
13959
13960 relstart = _bfd_elf_link_info_read_relocs (sec->owner, info->info,
13961 sec, NULL, NULL, true);
13962 if (!relstart)
13963 return info->ok = false;
13964 bed = get_elf_backend_data (sec->owner);
13965 log_file_align = bed->s->log_file_align;
13966
13967 relend = relstart + sec->reloc_count;
13968
13969 for (rel = relstart; rel < relend; ++rel)
13970 if (rel->r_offset >= hstart && rel->r_offset < hend)
13971 {
13972 /* If the entry is in use, do nothing. */
13973 if (h->u2.vtable->used
13974 && (rel->r_offset - hstart) < h->u2.vtable->size)
13975 {
13976 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
13977 if (h->u2.vtable->used[entry])
13978 continue;
13979 }
13980 /* Otherwise, kill it. */
13981 rel->r_offset = rel->r_info = rel->r_addend = 0;
13982 }
13983
13984 return true;
13985 }
13986
13987 /* Mark sections containing dynamically referenced symbols. When
13988 building shared libraries, we must assume that any visible symbol is
13989 referenced. */
13990
13991 bool
13992 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
13993 {
13994 struct bfd_link_info *info = (struct bfd_link_info *) inf;
13995 struct bfd_elf_dynamic_list *d = info->dynamic_list;
13996
13997 if ((h->root.type == bfd_link_hash_defined
13998 || h->root.type == bfd_link_hash_defweak)
13999 && (!h->start_stop
14000 || h->root.ldscript_def
14001 || !info->start_stop_gc)
14002 && ((h->ref_dynamic && !h->forced_local)
14003 || ((h->def_regular || ELF_COMMON_DEF_P (h))
14004 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
14005 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
14006 && (!bfd_link_executable (info)
14007 || info->gc_keep_exported
14008 || info->export_dynamic
14009 || (h->dynamic
14010 && d != NULL
14011 && (*d->match) (&d->head, NULL, h->root.root.string)))
14012 && (h->versioned >= versioned
14013 || !bfd_hide_sym_by_version (info->version_info,
14014 h->root.root.string)))))
14015 h->root.u.def.section->flags |= SEC_KEEP;
14016
14017 return true;
14018 }
14019
14020 /* Keep all sections containing symbols undefined on the command-line,
14021 and the section containing the entry symbol. */
14022
14023 void
14024 _bfd_elf_gc_keep (struct bfd_link_info *info)
14025 {
14026 struct bfd_sym_chain *sym;
14027
14028 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
14029 {
14030 struct elf_link_hash_entry *h;
14031
14032 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
14033 false, false, false);
14034
14035 if (h != NULL
14036 && (h->root.type == bfd_link_hash_defined
14037 || h->root.type == bfd_link_hash_defweak)
14038 && !bfd_is_const_section (h->root.u.def.section))
14039 h->root.u.def.section->flags |= SEC_KEEP;
14040 }
14041 }
14042
14043 bool
14044 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
14045 struct bfd_link_info *info)
14046 {
14047 bfd *ibfd = info->input_bfds;
14048
14049 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14050 {
14051 asection *sec;
14052 struct elf_reloc_cookie cookie;
14053
14054 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
14055 continue;
14056 sec = ibfd->sections;
14057 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14058 continue;
14059
14060 if (!init_reloc_cookie (&cookie, info, ibfd))
14061 return false;
14062
14063 for (sec = ibfd->sections; sec; sec = sec->next)
14064 {
14065 if (startswith (bfd_section_name (sec), ".eh_frame_entry")
14066 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
14067 {
14068 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
14069 fini_reloc_cookie_rels (&cookie, sec);
14070 }
14071 }
14072 }
14073 return true;
14074 }
14075
14076 /* Do mark and sweep of unused sections. */
14077
14078 bool
14079 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
14080 {
14081 bool ok = true;
14082 bfd *sub;
14083 elf_gc_mark_hook_fn gc_mark_hook;
14084 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14085 struct elf_link_hash_table *htab;
14086 struct link_info_ok info_ok;
14087
14088 if (!bed->can_gc_sections
14089 || !is_elf_hash_table (info->hash))
14090 {
14091 _bfd_error_handler(_("warning: gc-sections option ignored"));
14092 return true;
14093 }
14094
14095 bed->gc_keep (info);
14096 htab = elf_hash_table (info);
14097
14098 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
14099 at the .eh_frame section if we can mark the FDEs individually. */
14100 for (sub = info->input_bfds;
14101 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
14102 sub = sub->link.next)
14103 {
14104 asection *sec;
14105 struct elf_reloc_cookie cookie;
14106
14107 sec = sub->sections;
14108 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14109 continue;
14110 sec = bfd_get_section_by_name (sub, ".eh_frame");
14111 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
14112 {
14113 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
14114 if (elf_section_data (sec)->sec_info
14115 && (sec->flags & SEC_LINKER_CREATED) == 0)
14116 elf_eh_frame_section (sub) = sec;
14117 fini_reloc_cookie_for_section (&cookie, sec);
14118 sec = bfd_get_next_section_by_name (NULL, sec);
14119 }
14120 }
14121
14122 /* Apply transitive closure to the vtable entry usage info. */
14123 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
14124 if (!ok)
14125 return false;
14126
14127 /* Kill the vtable relocations that were not used. */
14128 info_ok.info = info;
14129 info_ok.ok = true;
14130 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &info_ok);
14131 if (!info_ok.ok)
14132 return false;
14133
14134 /* Mark dynamically referenced symbols. */
14135 if (htab->dynamic_sections_created || info->gc_keep_exported)
14136 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
14137
14138 /* Grovel through relocs to find out who stays ... */
14139 gc_mark_hook = bed->gc_mark_hook;
14140 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
14141 {
14142 asection *o;
14143
14144 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
14145 || elf_object_id (sub) != elf_hash_table_id (htab)
14146 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
14147 continue;
14148
14149 o = sub->sections;
14150 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14151 continue;
14152
14153 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
14154 Also treat note sections as a root, if the section is not part
14155 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
14156 well as FINI_ARRAY sections for ld -r. */
14157 for (o = sub->sections; o != NULL; o = o->next)
14158 if (!o->gc_mark
14159 && (o->flags & SEC_EXCLUDE) == 0
14160 && ((o->flags & SEC_KEEP) != 0
14161 || (bfd_link_relocatable (info)
14162 && ((elf_section_data (o)->this_hdr.sh_type
14163 == SHT_PREINIT_ARRAY)
14164 || (elf_section_data (o)->this_hdr.sh_type
14165 == SHT_INIT_ARRAY)
14166 || (elf_section_data (o)->this_hdr.sh_type
14167 == SHT_FINI_ARRAY)))
14168 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
14169 && elf_next_in_group (o) == NULL
14170 && elf_linked_to_section (o) == NULL)
14171 || ((elf_tdata (sub)->has_gnu_osabi & elf_gnu_osabi_retain)
14172 && (elf_section_flags (o) & SHF_GNU_RETAIN))))
14173 {
14174 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
14175 return false;
14176 }
14177 }
14178
14179 /* Allow the backend to mark additional target specific sections. */
14180 bed->gc_mark_extra_sections (info, gc_mark_hook);
14181
14182 /* ... and mark SEC_EXCLUDE for those that go. */
14183 return elf_gc_sweep (abfd, info);
14184 }
14185 \f
14186 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
14187
14188 bool
14189 bfd_elf_gc_record_vtinherit (bfd *abfd,
14190 asection *sec,
14191 struct elf_link_hash_entry *h,
14192 bfd_vma offset)
14193 {
14194 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
14195 struct elf_link_hash_entry **search, *child;
14196 size_t extsymcount;
14197 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14198
14199 /* The sh_info field of the symtab header tells us where the
14200 external symbols start. We don't care about the local symbols at
14201 this point. */
14202 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
14203 if (!elf_bad_symtab (abfd))
14204 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
14205
14206 sym_hashes = elf_sym_hashes (abfd);
14207 sym_hashes_end = PTR_ADD (sym_hashes, extsymcount);
14208
14209 /* Hunt down the child symbol, which is in this section at the same
14210 offset as the relocation. */
14211 for (search = sym_hashes; search != sym_hashes_end; ++search)
14212 {
14213 if ((child = *search) != NULL
14214 && (child->root.type == bfd_link_hash_defined
14215 || child->root.type == bfd_link_hash_defweak)
14216 && child->root.u.def.section == sec
14217 && child->root.u.def.value == offset)
14218 goto win;
14219 }
14220
14221 /* xgettext:c-format */
14222 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
14223 abfd, sec, (uint64_t) offset);
14224 bfd_set_error (bfd_error_invalid_operation);
14225 return false;
14226
14227 win:
14228 if (!child->u2.vtable)
14229 {
14230 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
14231 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
14232 if (!child->u2.vtable)
14233 return false;
14234 }
14235 if (!h)
14236 {
14237 /* This *should* only be the absolute section. It could potentially
14238 be that someone has defined a non-global vtable though, which
14239 would be bad. It isn't worth paging in the local symbols to be
14240 sure though; that case should simply be handled by the assembler. */
14241
14242 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
14243 }
14244 else
14245 child->u2.vtable->parent = h;
14246
14247 return true;
14248 }
14249
14250 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
14251
14252 bool
14253 bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec,
14254 struct elf_link_hash_entry *h,
14255 bfd_vma addend)
14256 {
14257 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14258 unsigned int log_file_align = bed->s->log_file_align;
14259
14260 if (!h)
14261 {
14262 /* xgettext:c-format */
14263 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
14264 abfd, sec);
14265 bfd_set_error (bfd_error_bad_value);
14266 return false;
14267 }
14268
14269 if (!h->u2.vtable)
14270 {
14271 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
14272 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
14273 if (!h->u2.vtable)
14274 return false;
14275 }
14276
14277 if (addend >= h->u2.vtable->size)
14278 {
14279 size_t size, bytes, file_align;
14280 bool *ptr = h->u2.vtable->used;
14281
14282 /* While the symbol is undefined, we have to be prepared to handle
14283 a zero size. */
14284 file_align = 1 << log_file_align;
14285 if (h->root.type == bfd_link_hash_undefined)
14286 size = addend + file_align;
14287 else
14288 {
14289 size = h->size;
14290 if (addend >= size)
14291 {
14292 /* Oops! We've got a reference past the defined end of
14293 the table. This is probably a bug -- shall we warn? */
14294 size = addend + file_align;
14295 }
14296 }
14297 size = (size + file_align - 1) & -file_align;
14298
14299 /* Allocate one extra entry for use as a "done" flag for the
14300 consolidation pass. */
14301 bytes = ((size >> log_file_align) + 1) * sizeof (bool);
14302
14303 if (ptr)
14304 {
14305 ptr = (bool *) bfd_realloc (ptr - 1, bytes);
14306
14307 if (ptr != NULL)
14308 {
14309 size_t oldbytes;
14310
14311 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
14312 * sizeof (bool));
14313 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
14314 }
14315 }
14316 else
14317 ptr = (bool *) bfd_zmalloc (bytes);
14318
14319 if (ptr == NULL)
14320 return false;
14321
14322 /* And arrange for that done flag to be at index -1. */
14323 h->u2.vtable->used = ptr + 1;
14324 h->u2.vtable->size = size;
14325 }
14326
14327 h->u2.vtable->used[addend >> log_file_align] = true;
14328
14329 return true;
14330 }
14331
14332 /* Map an ELF section header flag to its corresponding string. */
14333 typedef struct
14334 {
14335 char *flag_name;
14336 flagword flag_value;
14337 } elf_flags_to_name_table;
14338
14339 static const elf_flags_to_name_table elf_flags_to_names [] =
14340 {
14341 { "SHF_WRITE", SHF_WRITE },
14342 { "SHF_ALLOC", SHF_ALLOC },
14343 { "SHF_EXECINSTR", SHF_EXECINSTR },
14344 { "SHF_MERGE", SHF_MERGE },
14345 { "SHF_STRINGS", SHF_STRINGS },
14346 { "SHF_INFO_LINK", SHF_INFO_LINK},
14347 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
14348 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
14349 { "SHF_GROUP", SHF_GROUP },
14350 { "SHF_TLS", SHF_TLS },
14351 { "SHF_MASKOS", SHF_MASKOS },
14352 { "SHF_EXCLUDE", SHF_EXCLUDE },
14353 };
14354
14355 /* Returns TRUE if the section is to be included, otherwise FALSE. */
14356 bool
14357 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
14358 struct flag_info *flaginfo,
14359 asection *section)
14360 {
14361 const bfd_vma sh_flags = elf_section_flags (section);
14362
14363 if (!flaginfo->flags_initialized)
14364 {
14365 bfd *obfd = info->output_bfd;
14366 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14367 struct flag_info_list *tf = flaginfo->flag_list;
14368 int with_hex = 0;
14369 int without_hex = 0;
14370
14371 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
14372 {
14373 unsigned i;
14374 flagword (*lookup) (char *);
14375
14376 lookup = bed->elf_backend_lookup_section_flags_hook;
14377 if (lookup != NULL)
14378 {
14379 flagword hexval = (*lookup) ((char *) tf->name);
14380
14381 if (hexval != 0)
14382 {
14383 if (tf->with == with_flags)
14384 with_hex |= hexval;
14385 else if (tf->with == without_flags)
14386 without_hex |= hexval;
14387 tf->valid = true;
14388 continue;
14389 }
14390 }
14391 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
14392 {
14393 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
14394 {
14395 if (tf->with == with_flags)
14396 with_hex |= elf_flags_to_names[i].flag_value;
14397 else if (tf->with == without_flags)
14398 without_hex |= elf_flags_to_names[i].flag_value;
14399 tf->valid = true;
14400 break;
14401 }
14402 }
14403 if (!tf->valid)
14404 {
14405 info->callbacks->einfo
14406 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
14407 return false;
14408 }
14409 }
14410 flaginfo->flags_initialized = true;
14411 flaginfo->only_with_flags |= with_hex;
14412 flaginfo->not_with_flags |= without_hex;
14413 }
14414
14415 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
14416 return false;
14417
14418 if ((flaginfo->not_with_flags & sh_flags) != 0)
14419 return false;
14420
14421 return true;
14422 }
14423
14424 struct alloc_got_off_arg {
14425 bfd_vma gotoff;
14426 struct bfd_link_info *info;
14427 };
14428
14429 /* We need a special top-level link routine to convert got reference counts
14430 to real got offsets. */
14431
14432 static bool
14433 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
14434 {
14435 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
14436 bfd *obfd = gofarg->info->output_bfd;
14437 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14438
14439 if (h->got.refcount > 0)
14440 {
14441 h->got.offset = gofarg->gotoff;
14442 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
14443 }
14444 else
14445 h->got.offset = (bfd_vma) -1;
14446
14447 return true;
14448 }
14449
14450 /* And an accompanying bit to work out final got entry offsets once
14451 we're done. Should be called from final_link. */
14452
14453 bool
14454 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
14455 struct bfd_link_info *info)
14456 {
14457 bfd *i;
14458 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14459 bfd_vma gotoff;
14460 struct alloc_got_off_arg gofarg;
14461
14462 BFD_ASSERT (abfd == info->output_bfd);
14463
14464 if (! is_elf_hash_table (info->hash))
14465 return false;
14466
14467 /* The GOT offset is relative to the .got section, but the GOT header is
14468 put into the .got.plt section, if the backend uses it. */
14469 if (bed->want_got_plt)
14470 gotoff = 0;
14471 else
14472 gotoff = bed->got_header_size;
14473
14474 /* Do the local .got entries first. */
14475 for (i = info->input_bfds; i; i = i->link.next)
14476 {
14477 bfd_signed_vma *local_got;
14478 size_t j, locsymcount;
14479 Elf_Internal_Shdr *symtab_hdr;
14480
14481 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
14482 continue;
14483
14484 local_got = elf_local_got_refcounts (i);
14485 if (!local_got)
14486 continue;
14487
14488 symtab_hdr = &elf_tdata (i)->symtab_hdr;
14489 if (elf_bad_symtab (i))
14490 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
14491 else
14492 locsymcount = symtab_hdr->sh_info;
14493
14494 for (j = 0; j < locsymcount; ++j)
14495 {
14496 if (local_got[j] > 0)
14497 {
14498 local_got[j] = gotoff;
14499 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
14500 }
14501 else
14502 local_got[j] = (bfd_vma) -1;
14503 }
14504 }
14505
14506 /* Then the global .got entries. .plt refcounts are handled by
14507 adjust_dynamic_symbol */
14508 gofarg.gotoff = gotoff;
14509 gofarg.info = info;
14510 elf_link_hash_traverse (elf_hash_table (info),
14511 elf_gc_allocate_got_offsets,
14512 &gofarg);
14513 return true;
14514 }
14515
14516 /* Many folk need no more in the way of final link than this, once
14517 got entry reference counting is enabled. */
14518
14519 bool
14520 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
14521 {
14522 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
14523 return false;
14524
14525 /* Invoke the regular ELF backend linker to do all the work. */
14526 return bfd_elf_final_link (abfd, info);
14527 }
14528
14529 bool
14530 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
14531 {
14532 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
14533
14534 if (rcookie->bad_symtab)
14535 rcookie->rel = rcookie->rels;
14536
14537 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
14538 {
14539 unsigned long r_symndx;
14540
14541 if (! rcookie->bad_symtab)
14542 if (rcookie->rel->r_offset > offset)
14543 return false;
14544 if (rcookie->rel->r_offset != offset)
14545 continue;
14546
14547 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
14548 if (r_symndx == STN_UNDEF)
14549 return true;
14550
14551 if (r_symndx >= rcookie->locsymcount
14552 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
14553 {
14554 struct elf_link_hash_entry *h;
14555
14556 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
14557
14558 while (h->root.type == bfd_link_hash_indirect
14559 || h->root.type == bfd_link_hash_warning)
14560 h = (struct elf_link_hash_entry *) h->root.u.i.link;
14561
14562 if ((h->root.type == bfd_link_hash_defined
14563 || h->root.type == bfd_link_hash_defweak)
14564 && (h->root.u.def.section->owner != rcookie->abfd
14565 || h->root.u.def.section->kept_section != NULL
14566 || discarded_section (h->root.u.def.section)))
14567 return true;
14568 }
14569 else
14570 {
14571 /* It's not a relocation against a global symbol,
14572 but it could be a relocation against a local
14573 symbol for a discarded section. */
14574 asection *isec;
14575 Elf_Internal_Sym *isym;
14576
14577 /* Need to: get the symbol; get the section. */
14578 isym = &rcookie->locsyms[r_symndx];
14579 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
14580 if (isec != NULL
14581 && (isec->kept_section != NULL
14582 || discarded_section (isec)))
14583 return true;
14584 }
14585 return false;
14586 }
14587 return false;
14588 }
14589
14590 /* Discard unneeded references to discarded sections.
14591 Returns -1 on error, 1 if any section's size was changed, 0 if
14592 nothing changed. This function assumes that the relocations are in
14593 sorted order, which is true for all known assemblers. */
14594
14595 int
14596 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
14597 {
14598 struct elf_reloc_cookie cookie;
14599 asection *o;
14600 bfd *abfd;
14601 int changed = 0;
14602
14603 if (info->traditional_format
14604 || !is_elf_hash_table (info->hash))
14605 return 0;
14606
14607 o = bfd_get_section_by_name (output_bfd, ".stab");
14608 if (o != NULL)
14609 {
14610 asection *i;
14611
14612 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14613 {
14614 if (i->size == 0
14615 || i->reloc_count == 0
14616 || i->sec_info_type != SEC_INFO_TYPE_STABS)
14617 continue;
14618
14619 abfd = i->owner;
14620 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14621 continue;
14622
14623 if (!init_reloc_cookie_for_section (&cookie, info, i))
14624 return -1;
14625
14626 if (_bfd_discard_section_stabs (abfd, i,
14627 elf_section_data (i)->sec_info,
14628 bfd_elf_reloc_symbol_deleted_p,
14629 &cookie))
14630 changed = 1;
14631
14632 fini_reloc_cookie_for_section (&cookie, i);
14633 }
14634 }
14635
14636 o = NULL;
14637 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
14638 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
14639 if (o != NULL)
14640 {
14641 asection *i;
14642 int eh_changed = 0;
14643 unsigned int eh_alignment; /* Octets. */
14644
14645 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14646 {
14647 if (i->size == 0)
14648 continue;
14649
14650 abfd = i->owner;
14651 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14652 continue;
14653
14654 if (!init_reloc_cookie_for_section (&cookie, info, i))
14655 return -1;
14656
14657 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
14658 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
14659 bfd_elf_reloc_symbol_deleted_p,
14660 &cookie))
14661 {
14662 eh_changed = 1;
14663 if (i->size != i->rawsize)
14664 changed = 1;
14665 }
14666
14667 fini_reloc_cookie_for_section (&cookie, i);
14668 }
14669
14670 eh_alignment = ((1 << o->alignment_power)
14671 * bfd_octets_per_byte (output_bfd, o));
14672 /* Skip over zero terminator, and prevent empty sections from
14673 adding alignment padding at the end. */
14674 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
14675 if (i->size == 0)
14676 i->flags |= SEC_EXCLUDE;
14677 else if (i->size > 4)
14678 break;
14679 /* The last non-empty eh_frame section doesn't need padding. */
14680 if (i != NULL)
14681 i = i->map_tail.s;
14682 /* Any prior sections must pad the last FDE out to the output
14683 section alignment. Otherwise we might have zero padding
14684 between sections, which would be seen as a terminator. */
14685 for (; i != NULL; i = i->map_tail.s)
14686 if (i->size == 4)
14687 /* All but the last zero terminator should have been removed. */
14688 BFD_FAIL ();
14689 else
14690 {
14691 bfd_size_type size
14692 = (i->size + eh_alignment - 1) & -eh_alignment;
14693 if (i->size != size)
14694 {
14695 i->size = size;
14696 changed = 1;
14697 eh_changed = 1;
14698 }
14699 }
14700 if (eh_changed)
14701 elf_link_hash_traverse (elf_hash_table (info),
14702 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
14703 }
14704
14705 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
14706 {
14707 const struct elf_backend_data *bed;
14708 asection *s;
14709
14710 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14711 continue;
14712 s = abfd->sections;
14713 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14714 continue;
14715
14716 bed = get_elf_backend_data (abfd);
14717
14718 if (bed->elf_backend_discard_info != NULL)
14719 {
14720 if (!init_reloc_cookie (&cookie, info, abfd))
14721 return -1;
14722
14723 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
14724 changed = 1;
14725
14726 fini_reloc_cookie (&cookie, abfd);
14727 }
14728 }
14729
14730 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
14731 _bfd_elf_end_eh_frame_parsing (info);
14732
14733 if (info->eh_frame_hdr_type
14734 && !bfd_link_relocatable (info)
14735 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
14736 changed = 1;
14737
14738 return changed;
14739 }
14740
14741 bool
14742 _bfd_elf_section_already_linked (bfd *abfd,
14743 asection *sec,
14744 struct bfd_link_info *info)
14745 {
14746 flagword flags;
14747 const char *name, *key;
14748 struct bfd_section_already_linked *l;
14749 struct bfd_section_already_linked_hash_entry *already_linked_list;
14750
14751 if (sec->output_section == bfd_abs_section_ptr)
14752 return false;
14753
14754 flags = sec->flags;
14755
14756 /* Return if it isn't a linkonce section. A comdat group section
14757 also has SEC_LINK_ONCE set. */
14758 if ((flags & SEC_LINK_ONCE) == 0)
14759 return false;
14760
14761 /* Don't put group member sections on our list of already linked
14762 sections. They are handled as a group via their group section. */
14763 if (elf_sec_group (sec) != NULL)
14764 return false;
14765
14766 /* For a SHT_GROUP section, use the group signature as the key. */
14767 name = sec->name;
14768 if ((flags & SEC_GROUP) != 0
14769 && elf_next_in_group (sec) != NULL
14770 && elf_group_name (elf_next_in_group (sec)) != NULL)
14771 key = elf_group_name (elf_next_in_group (sec));
14772 else
14773 {
14774 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
14775 if (startswith (name, ".gnu.linkonce.")
14776 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
14777 key++;
14778 else
14779 /* Must be a user linkonce section that doesn't follow gcc's
14780 naming convention. In this case we won't be matching
14781 single member groups. */
14782 key = name;
14783 }
14784
14785 already_linked_list = bfd_section_already_linked_table_lookup (key);
14786
14787 for (l = already_linked_list->entry; l != NULL; l = l->next)
14788 {
14789 /* We may have 2 different types of sections on the list: group
14790 sections with a signature of <key> (<key> is some string),
14791 and linkonce sections named .gnu.linkonce.<type>.<key>.
14792 Match like sections. LTO plugin sections are an exception.
14793 They are always named .gnu.linkonce.t.<key> and match either
14794 type of section. */
14795 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
14796 && ((flags & SEC_GROUP) != 0
14797 || strcmp (name, l->sec->name) == 0))
14798 || (l->sec->owner->flags & BFD_PLUGIN) != 0
14799 || (sec->owner->flags & BFD_PLUGIN) != 0)
14800 {
14801 /* The section has already been linked. See if we should
14802 issue a warning. */
14803 if (!_bfd_handle_already_linked (sec, l, info))
14804 return false;
14805
14806 if (flags & SEC_GROUP)
14807 {
14808 asection *first = elf_next_in_group (sec);
14809 asection *s = first;
14810
14811 while (s != NULL)
14812 {
14813 s->output_section = bfd_abs_section_ptr;
14814 /* Record which group discards it. */
14815 s->kept_section = l->sec;
14816 s = elf_next_in_group (s);
14817 /* These lists are circular. */
14818 if (s == first)
14819 break;
14820 }
14821 }
14822
14823 return true;
14824 }
14825 }
14826
14827 /* A single member comdat group section may be discarded by a
14828 linkonce section and vice versa. */
14829 if ((flags & SEC_GROUP) != 0)
14830 {
14831 asection *first = elf_next_in_group (sec);
14832
14833 if (first != NULL && elf_next_in_group (first) == first)
14834 /* Check this single member group against linkonce sections. */
14835 for (l = already_linked_list->entry; l != NULL; l = l->next)
14836 if ((l->sec->flags & SEC_GROUP) == 0
14837 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14838 {
14839 first->output_section = bfd_abs_section_ptr;
14840 first->kept_section = l->sec;
14841 sec->output_section = bfd_abs_section_ptr;
14842 break;
14843 }
14844 }
14845 else
14846 /* Check this linkonce section against single member groups. */
14847 for (l = already_linked_list->entry; l != NULL; l = l->next)
14848 if (l->sec->flags & SEC_GROUP)
14849 {
14850 asection *first = elf_next_in_group (l->sec);
14851
14852 if (first != NULL
14853 && elf_next_in_group (first) == first
14854 && bfd_elf_match_symbols_in_sections (first, sec, info))
14855 {
14856 sec->output_section = bfd_abs_section_ptr;
14857 sec->kept_section = first;
14858 break;
14859 }
14860 }
14861
14862 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14863 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14864 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14865 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14866 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14867 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14868 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14869 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14870 The reverse order cannot happen as there is never a bfd with only the
14871 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14872 matter as here were are looking only for cross-bfd sections. */
14873
14874 if ((flags & SEC_GROUP) == 0 && startswith (name, ".gnu.linkonce.r."))
14875 for (l = already_linked_list->entry; l != NULL; l = l->next)
14876 if ((l->sec->flags & SEC_GROUP) == 0
14877 && startswith (l->sec->name, ".gnu.linkonce.t."))
14878 {
14879 if (abfd != l->sec->owner)
14880 sec->output_section = bfd_abs_section_ptr;
14881 break;
14882 }
14883
14884 /* This is the first section with this name. Record it. */
14885 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
14886 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
14887 return sec->output_section == bfd_abs_section_ptr;
14888 }
14889
14890 bool
14891 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
14892 {
14893 return sym->st_shndx == SHN_COMMON;
14894 }
14895
14896 unsigned int
14897 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14898 {
14899 return SHN_COMMON;
14900 }
14901
14902 asection *
14903 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14904 {
14905 return bfd_com_section_ptr;
14906 }
14907
14908 bfd_vma
14909 _bfd_elf_default_got_elt_size (bfd *abfd,
14910 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14911 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14912 bfd *ibfd ATTRIBUTE_UNUSED,
14913 unsigned long symndx ATTRIBUTE_UNUSED)
14914 {
14915 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14916 return bed->s->arch_size / 8;
14917 }
14918
14919 /* Routines to support the creation of dynamic relocs. */
14920
14921 /* Returns the name of the dynamic reloc section associated with SEC. */
14922
14923 static const char *
14924 get_dynamic_reloc_section_name (bfd * abfd,
14925 asection * sec,
14926 bool is_rela)
14927 {
14928 char *name;
14929 const char *old_name = bfd_section_name (sec);
14930 const char *prefix = is_rela ? ".rela" : ".rel";
14931
14932 if (old_name == NULL)
14933 return NULL;
14934
14935 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
14936 sprintf (name, "%s%s", prefix, old_name);
14937
14938 return name;
14939 }
14940
14941 /* Returns the dynamic reloc section associated with SEC.
14942 If necessary compute the name of the dynamic reloc section based
14943 on SEC's name (looked up in ABFD's string table) and the setting
14944 of IS_RELA. */
14945
14946 asection *
14947 _bfd_elf_get_dynamic_reloc_section (bfd *abfd,
14948 asection *sec,
14949 bool is_rela)
14950 {
14951 asection *reloc_sec = elf_section_data (sec)->sreloc;
14952
14953 if (reloc_sec == NULL)
14954 {
14955 const char *name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14956
14957 if (name != NULL)
14958 {
14959 reloc_sec = bfd_get_linker_section (abfd, name);
14960
14961 if (reloc_sec != NULL)
14962 elf_section_data (sec)->sreloc = reloc_sec;
14963 }
14964 }
14965
14966 return reloc_sec;
14967 }
14968
14969 /* Returns the dynamic reloc section associated with SEC. If the
14970 section does not exist it is created and attached to the DYNOBJ
14971 bfd and stored in the SRELOC field of SEC's elf_section_data
14972 structure.
14973
14974 ALIGNMENT is the alignment for the newly created section and
14975 IS_RELA defines whether the name should be .rela.<SEC's name>
14976 or .rel.<SEC's name>. The section name is looked up in the
14977 string table associated with ABFD. */
14978
14979 asection *
14980 _bfd_elf_make_dynamic_reloc_section (asection *sec,
14981 bfd *dynobj,
14982 unsigned int alignment,
14983 bfd *abfd,
14984 bool is_rela)
14985 {
14986 asection * reloc_sec = elf_section_data (sec)->sreloc;
14987
14988 if (reloc_sec == NULL)
14989 {
14990 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14991
14992 if (name == NULL)
14993 return NULL;
14994
14995 reloc_sec = bfd_get_linker_section (dynobj, name);
14996
14997 if (reloc_sec == NULL)
14998 {
14999 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
15000 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
15001 if ((sec->flags & SEC_ALLOC) != 0)
15002 flags |= SEC_ALLOC | SEC_LOAD;
15003
15004 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
15005 if (reloc_sec != NULL)
15006 {
15007 /* _bfd_elf_get_sec_type_attr chooses a section type by
15008 name. Override as it may be wrong, eg. for a user
15009 section named "auto" we'll get ".relauto" which is
15010 seen to be a .rela section. */
15011 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
15012 if (!bfd_set_section_alignment (reloc_sec, alignment))
15013 reloc_sec = NULL;
15014 }
15015 }
15016
15017 elf_section_data (sec)->sreloc = reloc_sec;
15018 }
15019
15020 return reloc_sec;
15021 }
15022
15023 /* Copy the ELF symbol type and other attributes for a linker script
15024 assignment from HSRC to HDEST. Generally this should be treated as
15025 if we found a strong non-dynamic definition for HDEST (except that
15026 ld ignores multiple definition errors). */
15027 void
15028 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
15029 struct bfd_link_hash_entry *hdest,
15030 struct bfd_link_hash_entry *hsrc)
15031 {
15032 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
15033 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
15034 Elf_Internal_Sym isym;
15035
15036 ehdest->type = ehsrc->type;
15037 ehdest->target_internal = ehsrc->target_internal;
15038
15039 isym.st_other = ehsrc->other;
15040 elf_merge_st_other (abfd, ehdest, isym.st_other, NULL, true, false);
15041 }
15042
15043 /* Append a RELA relocation REL to section S in BFD. */
15044
15045 void
15046 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15047 {
15048 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15049 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
15050 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
15051 bed->s->swap_reloca_out (abfd, rel, loc);
15052 }
15053
15054 /* Append a REL relocation REL to section S in BFD. */
15055
15056 void
15057 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15058 {
15059 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15060 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
15061 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
15062 bed->s->swap_reloc_out (abfd, rel, loc);
15063 }
15064
15065 /* Define __start, __stop, .startof. or .sizeof. symbol. */
15066
15067 struct bfd_link_hash_entry *
15068 bfd_elf_define_start_stop (struct bfd_link_info *info,
15069 const char *symbol, asection *sec)
15070 {
15071 struct elf_link_hash_entry *h;
15072
15073 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
15074 false, false, true);
15075 /* NB: Common symbols will be turned into definition later. */
15076 if (h != NULL
15077 && !h->root.ldscript_def
15078 && (h->root.type == bfd_link_hash_undefined
15079 || h->root.type == bfd_link_hash_undefweak
15080 || ((h->ref_regular || h->def_dynamic)
15081 && !h->def_regular
15082 && h->root.type != bfd_link_hash_common)))
15083 {
15084 bool was_dynamic = h->ref_dynamic || h->def_dynamic;
15085 h->verinfo.verdef = NULL;
15086 h->root.type = bfd_link_hash_defined;
15087 h->root.u.def.section = sec;
15088 h->root.u.def.value = 0;
15089 h->def_regular = 1;
15090 h->def_dynamic = 0;
15091 h->start_stop = 1;
15092 h->u2.start_stop_section = sec;
15093 if (symbol[0] == '.')
15094 {
15095 /* .startof. and .sizeof. symbols are local. */
15096 const struct elf_backend_data *bed;
15097 bed = get_elf_backend_data (info->output_bfd);
15098 (*bed->elf_backend_hide_symbol) (info, h, true);
15099 }
15100 else
15101 {
15102 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
15103 h->other = ((h->other & ~ELF_ST_VISIBILITY (-1))
15104 | info->start_stop_visibility);
15105 if (was_dynamic)
15106 bfd_elf_link_record_dynamic_symbol (info, h);
15107 }
15108 return &h->root;
15109 }
15110 return NULL;
15111 }
15112
15113 /* Find dynamic relocs for H that apply to read-only sections. */
15114
15115 asection *
15116 _bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry *h)
15117 {
15118 struct elf_dyn_relocs *p;
15119
15120 for (p = h->dyn_relocs; p != NULL; p = p->next)
15121 {
15122 asection *s = p->sec->output_section;
15123
15124 if (s != NULL && (s->flags & SEC_READONLY) != 0)
15125 return p->sec;
15126 }
15127 return NULL;
15128 }
15129
15130 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
15131 read-only sections. */
15132
15133 bool
15134 _bfd_elf_maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
15135 {
15136 asection *sec;
15137
15138 if (h->root.type == bfd_link_hash_indirect)
15139 return true;
15140
15141 sec = _bfd_elf_readonly_dynrelocs (h);
15142 if (sec != NULL)
15143 {
15144 struct bfd_link_info *info = (struct bfd_link_info *) inf;
15145
15146 info->flags |= DF_TEXTREL;
15147 /* xgettext:c-format */
15148 info->callbacks->minfo (_("%pB: dynamic relocation against `%pT' "
15149 "in read-only section `%pA'\n"),
15150 sec->owner, h->root.root.string, sec);
15151
15152 if (bfd_link_textrel_check (info))
15153 /* xgettext:c-format */
15154 info->callbacks->einfo (_("%P: %pB: warning: relocation against `%s' "
15155 "in read-only section `%pA'\n"),
15156 sec->owner, h->root.root.string, sec);
15157
15158 /* Not an error, just cut short the traversal. */
15159 return false;
15160 }
15161 return true;
15162 }
15163
15164 /* Add dynamic tags. */
15165
15166 bool
15167 _bfd_elf_add_dynamic_tags (bfd *output_bfd, struct bfd_link_info *info,
15168 bool need_dynamic_reloc)
15169 {
15170 struct elf_link_hash_table *htab = elf_hash_table (info);
15171
15172 if (htab->dynamic_sections_created)
15173 {
15174 /* Add some entries to the .dynamic section. We fill in the
15175 values later, in finish_dynamic_sections, but we must add
15176 the entries now so that we get the correct size for the
15177 .dynamic section. The DT_DEBUG entry is filled in by the
15178 dynamic linker and used by the debugger. */
15179 #define add_dynamic_entry(TAG, VAL) \
15180 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
15181
15182 const struct elf_backend_data *bed
15183 = get_elf_backend_data (output_bfd);
15184
15185 if (bfd_link_executable (info))
15186 {
15187 if (!add_dynamic_entry (DT_DEBUG, 0))
15188 return false;
15189 }
15190
15191 if (htab->dt_pltgot_required || htab->splt->size != 0)
15192 {
15193 /* DT_PLTGOT is used by prelink even if there is no PLT
15194 relocation. */
15195 if (!add_dynamic_entry (DT_PLTGOT, 0))
15196 return false;
15197 }
15198
15199 if (htab->dt_jmprel_required || htab->srelplt->size != 0)
15200 {
15201 if (!add_dynamic_entry (DT_PLTRELSZ, 0)
15202 || !add_dynamic_entry (DT_PLTREL,
15203 (bed->rela_plts_and_copies_p
15204 ? DT_RELA : DT_REL))
15205 || !add_dynamic_entry (DT_JMPREL, 0))
15206 return false;
15207 }
15208
15209 if (htab->tlsdesc_plt
15210 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
15211 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
15212 return false;
15213
15214 if (need_dynamic_reloc)
15215 {
15216 if (bed->rela_plts_and_copies_p)
15217 {
15218 if (!add_dynamic_entry (DT_RELA, 0)
15219 || !add_dynamic_entry (DT_RELASZ, 0)
15220 || !add_dynamic_entry (DT_RELAENT,
15221 bed->s->sizeof_rela))
15222 return false;
15223 }
15224 else
15225 {
15226 if (!add_dynamic_entry (DT_REL, 0)
15227 || !add_dynamic_entry (DT_RELSZ, 0)
15228 || !add_dynamic_entry (DT_RELENT,
15229 bed->s->sizeof_rel))
15230 return false;
15231 }
15232
15233 /* If any dynamic relocs apply to a read-only section,
15234 then we need a DT_TEXTREL entry. */
15235 if ((info->flags & DF_TEXTREL) == 0)
15236 elf_link_hash_traverse (htab, _bfd_elf_maybe_set_textrel,
15237 info);
15238
15239 if ((info->flags & DF_TEXTREL) != 0)
15240 {
15241 if (htab->ifunc_resolvers)
15242 info->callbacks->einfo
15243 (_("%P: warning: GNU indirect functions with DT_TEXTREL "
15244 "may result in a segfault at runtime; recompile with %s\n"),
15245 bfd_link_dll (info) ? "-fPIC" : "-fPIE");
15246
15247 if (!add_dynamic_entry (DT_TEXTREL, 0))
15248 return false;
15249 }
15250 }
15251 }
15252 #undef add_dynamic_entry
15253
15254 return true;
15255 }