ld: Limit cache size and add --max-cache-size=SIZE
[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 /* Handle a case where plugin_notice won't be called and thus won't
1276 set the non_ir_ref flags on the first pass over symbols. */
1277 if (oldbfd != NULL
1278 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN)
1279 && newdyn != olddyn)
1280 {
1281 h->root.non_ir_ref_dynamic = true;
1282 hi->root.non_ir_ref_dynamic = true;
1283 }
1284
1285 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1286 respectively, appear to be a definition rather than reference. */
1287
1288 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1289
1290 olddef = (h->root.type != bfd_link_hash_undefined
1291 && h->root.type != bfd_link_hash_undefweak
1292 && h->root.type != bfd_link_hash_common);
1293
1294 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1295 respectively, appear to be a function. */
1296
1297 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1298 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1299
1300 oldfunc = (h->type != STT_NOTYPE
1301 && bed->is_function_type (h->type));
1302
1303 if (!(newfunc && oldfunc)
1304 && ELF_ST_TYPE (sym->st_info) != h->type
1305 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1306 && h->type != STT_NOTYPE
1307 && (newdef || bfd_is_com_section (sec))
1308 && (olddef || h->root.type == bfd_link_hash_common))
1309 {
1310 /* If creating a default indirect symbol ("foo" or "foo@") from
1311 a dynamic versioned definition ("foo@@") skip doing so if
1312 there is an existing regular definition with a different
1313 type. We don't want, for example, a "time" variable in the
1314 executable overriding a "time" function in a shared library. */
1315 if (newdyn
1316 && !olddyn)
1317 {
1318 *skip = true;
1319 return true;
1320 }
1321
1322 /* When adding a symbol from a regular object file after we have
1323 created indirect symbols, undo the indirection and any
1324 dynamic state. */
1325 if (hi != h
1326 && !newdyn
1327 && olddyn)
1328 {
1329 h = hi;
1330 (*bed->elf_backend_hide_symbol) (info, h, true);
1331 h->forced_local = 0;
1332 h->ref_dynamic = 0;
1333 h->def_dynamic = 0;
1334 h->dynamic_def = 0;
1335 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1336 {
1337 h->root.type = bfd_link_hash_undefined;
1338 h->root.u.undef.abfd = abfd;
1339 }
1340 else
1341 {
1342 h->root.type = bfd_link_hash_new;
1343 h->root.u.undef.abfd = NULL;
1344 }
1345 return true;
1346 }
1347 }
1348
1349 /* Check TLS symbols. We don't check undefined symbols introduced
1350 by "ld -u" which have no type (and oldbfd NULL), and we don't
1351 check symbols from plugins because they also have no type. */
1352 if (oldbfd != NULL
1353 && (oldbfd->flags & BFD_PLUGIN) == 0
1354 && (abfd->flags & BFD_PLUGIN) == 0
1355 && ELF_ST_TYPE (sym->st_info) != h->type
1356 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1357 {
1358 bfd *ntbfd, *tbfd;
1359 bool ntdef, tdef;
1360 asection *ntsec, *tsec;
1361
1362 if (h->type == STT_TLS)
1363 {
1364 ntbfd = abfd;
1365 ntsec = sec;
1366 ntdef = newdef;
1367 tbfd = oldbfd;
1368 tsec = oldsec;
1369 tdef = olddef;
1370 }
1371 else
1372 {
1373 ntbfd = oldbfd;
1374 ntsec = oldsec;
1375 ntdef = olddef;
1376 tbfd = abfd;
1377 tsec = sec;
1378 tdef = newdef;
1379 }
1380
1381 if (tdef && ntdef)
1382 _bfd_error_handler
1383 /* xgettext:c-format */
1384 (_("%s: TLS definition in %pB section %pA "
1385 "mismatches non-TLS definition in %pB section %pA"),
1386 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
1387 else if (!tdef && !ntdef)
1388 _bfd_error_handler
1389 /* xgettext:c-format */
1390 (_("%s: TLS reference in %pB "
1391 "mismatches non-TLS reference in %pB"),
1392 h->root.root.string, tbfd, ntbfd);
1393 else if (tdef)
1394 _bfd_error_handler
1395 /* xgettext:c-format */
1396 (_("%s: TLS definition in %pB section %pA "
1397 "mismatches non-TLS reference in %pB"),
1398 h->root.root.string, tbfd, tsec, ntbfd);
1399 else
1400 _bfd_error_handler
1401 /* xgettext:c-format */
1402 (_("%s: TLS reference in %pB "
1403 "mismatches non-TLS definition in %pB section %pA"),
1404 h->root.root.string, tbfd, ntbfd, ntsec);
1405
1406 bfd_set_error (bfd_error_bad_value);
1407 return false;
1408 }
1409
1410 /* If the old symbol has non-default visibility, we ignore the new
1411 definition from a dynamic object. */
1412 if (newdyn
1413 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1414 && !bfd_is_und_section (sec))
1415 {
1416 *skip = true;
1417 /* Make sure this symbol is dynamic. */
1418 h->ref_dynamic = 1;
1419 hi->ref_dynamic = 1;
1420 /* A protected symbol has external availability. Make sure it is
1421 recorded as dynamic.
1422
1423 FIXME: Should we check type and size for protected symbol? */
1424 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1425 return bfd_elf_link_record_dynamic_symbol (info, h);
1426 else
1427 return true;
1428 }
1429 else if (!newdyn
1430 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1431 && h->def_dynamic)
1432 {
1433 /* If the new symbol with non-default visibility comes from a
1434 relocatable file and the old definition comes from a dynamic
1435 object, we remove the old definition. */
1436 if (hi->root.type == bfd_link_hash_indirect)
1437 {
1438 /* Handle the case where the old dynamic definition is
1439 default versioned. We need to copy the symbol info from
1440 the symbol with default version to the normal one if it
1441 was referenced before. */
1442 if (h->ref_regular)
1443 {
1444 hi->root.type = h->root.type;
1445 h->root.type = bfd_link_hash_indirect;
1446 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1447
1448 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1449 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1450 {
1451 /* If the new symbol is hidden or internal, completely undo
1452 any dynamic link state. */
1453 (*bed->elf_backend_hide_symbol) (info, h, true);
1454 h->forced_local = 0;
1455 h->ref_dynamic = 0;
1456 }
1457 else
1458 h->ref_dynamic = 1;
1459
1460 h->def_dynamic = 0;
1461 /* FIXME: Should we check type and size for protected symbol? */
1462 h->size = 0;
1463 h->type = 0;
1464
1465 h = hi;
1466 }
1467 else
1468 h = hi;
1469 }
1470
1471 /* If the old symbol was undefined before, then it will still be
1472 on the undefs list. If the new symbol is undefined or
1473 common, we can't make it bfd_link_hash_new here, because new
1474 undefined or common symbols will be added to the undefs list
1475 by _bfd_generic_link_add_one_symbol. Symbols may not be
1476 added twice to the undefs list. Also, if the new symbol is
1477 undefweak then we don't want to lose the strong undef. */
1478 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1479 {
1480 h->root.type = bfd_link_hash_undefined;
1481 h->root.u.undef.abfd = abfd;
1482 }
1483 else
1484 {
1485 h->root.type = bfd_link_hash_new;
1486 h->root.u.undef.abfd = NULL;
1487 }
1488
1489 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1490 {
1491 /* If the new symbol is hidden or internal, completely undo
1492 any dynamic link state. */
1493 (*bed->elf_backend_hide_symbol) (info, h, true);
1494 h->forced_local = 0;
1495 h->ref_dynamic = 0;
1496 }
1497 else
1498 h->ref_dynamic = 1;
1499 h->def_dynamic = 0;
1500 /* FIXME: Should we check type and size for protected symbol? */
1501 h->size = 0;
1502 h->type = 0;
1503 return true;
1504 }
1505
1506 /* If a new weak symbol definition comes from a regular file and the
1507 old symbol comes from a dynamic library, we treat the new one as
1508 strong. Similarly, an old weak symbol definition from a regular
1509 file is treated as strong when the new symbol comes from a dynamic
1510 library. Further, an old weak symbol from a dynamic library is
1511 treated as strong if the new symbol is from a dynamic library.
1512 This reflects the way glibc's ld.so works.
1513
1514 Also allow a weak symbol to override a linker script symbol
1515 defined by an early pass over the script. This is done so the
1516 linker knows the symbol is defined in an object file, for the
1517 DEFINED script function.
1518
1519 Do this before setting *type_change_ok or *size_change_ok so that
1520 we warn properly when dynamic library symbols are overridden. */
1521
1522 if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
1523 newweak = false;
1524 if (olddef && newdyn)
1525 oldweak = false;
1526
1527 /* Allow changes between different types of function symbol. */
1528 if (newfunc && oldfunc)
1529 *type_change_ok = true;
1530
1531 /* It's OK to change the type if either the existing symbol or the
1532 new symbol is weak. A type change is also OK if the old symbol
1533 is undefined and the new symbol is defined. */
1534
1535 if (oldweak
1536 || newweak
1537 || (newdef
1538 && h->root.type == bfd_link_hash_undefined))
1539 *type_change_ok = true;
1540
1541 /* It's OK to change the size if either the existing symbol or the
1542 new symbol is weak, or if the old symbol is undefined. */
1543
1544 if (*type_change_ok
1545 || h->root.type == bfd_link_hash_undefined)
1546 *size_change_ok = true;
1547
1548 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1549 symbol, respectively, appears to be a common symbol in a dynamic
1550 object. If a symbol appears in an uninitialized section, and is
1551 not weak, and is not a function, then it may be a common symbol
1552 which was resolved when the dynamic object was created. We want
1553 to treat such symbols specially, because they raise special
1554 considerations when setting the symbol size: if the symbol
1555 appears as a common symbol in a regular object, and the size in
1556 the regular object is larger, we must make sure that we use the
1557 larger size. This problematic case can always be avoided in C,
1558 but it must be handled correctly when using Fortran shared
1559 libraries.
1560
1561 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1562 likewise for OLDDYNCOMMON and OLDDEF.
1563
1564 Note that this test is just a heuristic, and that it is quite
1565 possible to have an uninitialized symbol in a shared object which
1566 is really a definition, rather than a common symbol. This could
1567 lead to some minor confusion when the symbol really is a common
1568 symbol in some regular object. However, I think it will be
1569 harmless. */
1570
1571 if (newdyn
1572 && newdef
1573 && !newweak
1574 && (sec->flags & SEC_ALLOC) != 0
1575 && (sec->flags & SEC_LOAD) == 0
1576 && sym->st_size > 0
1577 && !newfunc)
1578 newdyncommon = true;
1579 else
1580 newdyncommon = false;
1581
1582 if (olddyn
1583 && olddef
1584 && h->root.type == bfd_link_hash_defined
1585 && h->def_dynamic
1586 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1587 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1588 && h->size > 0
1589 && !oldfunc)
1590 olddyncommon = true;
1591 else
1592 olddyncommon = false;
1593
1594 /* We now know everything about the old and new symbols. We ask the
1595 backend to check if we can merge them. */
1596 if (bed->merge_symbol != NULL)
1597 {
1598 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1599 return false;
1600 sec = *psec;
1601 }
1602
1603 /* There are multiple definitions of a normal symbol. Skip the
1604 default symbol as well as definition from an IR object. */
1605 if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
1606 && !default_sym && h->def_regular
1607 && !(oldbfd != NULL
1608 && (oldbfd->flags & BFD_PLUGIN) != 0
1609 && (abfd->flags & BFD_PLUGIN) == 0))
1610 {
1611 /* Handle a multiple definition. */
1612 (*info->callbacks->multiple_definition) (info, &h->root,
1613 abfd, sec, *pvalue);
1614 *skip = true;
1615 return true;
1616 }
1617
1618 /* If both the old and the new symbols look like common symbols in a
1619 dynamic object, set the size of the symbol to the larger of the
1620 two. */
1621
1622 if (olddyncommon
1623 && newdyncommon
1624 && sym->st_size != h->size)
1625 {
1626 /* Since we think we have two common symbols, issue a multiple
1627 common warning if desired. Note that we only warn if the
1628 size is different. If the size is the same, we simply let
1629 the old symbol override the new one as normally happens with
1630 symbols defined in dynamic objects. */
1631
1632 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1633 bfd_link_hash_common, sym->st_size);
1634 if (sym->st_size > h->size)
1635 h->size = sym->st_size;
1636
1637 *size_change_ok = true;
1638 }
1639
1640 /* If we are looking at a dynamic object, and we have found a
1641 definition, we need to see if the symbol was already defined by
1642 some other object. If so, we want to use the existing
1643 definition, and we do not want to report a multiple symbol
1644 definition error; we do this by clobbering *PSEC to be
1645 bfd_und_section_ptr.
1646
1647 We treat a common symbol as a definition if the symbol in the
1648 shared library is a function, since common symbols always
1649 represent variables; this can cause confusion in principle, but
1650 any such confusion would seem to indicate an erroneous program or
1651 shared library. We also permit a common symbol in a regular
1652 object to override a weak symbol in a shared object. */
1653
1654 if (newdyn
1655 && newdef
1656 && (olddef
1657 || (h->root.type == bfd_link_hash_common
1658 && (newweak || newfunc))))
1659 {
1660 *override = abfd;
1661 newdef = false;
1662 newdyncommon = false;
1663
1664 *psec = sec = bfd_und_section_ptr;
1665 *size_change_ok = true;
1666
1667 /* If we get here when the old symbol is a common symbol, then
1668 we are explicitly letting it override a weak symbol or
1669 function in a dynamic object, and we don't want to warn about
1670 a type change. If the old symbol is a defined symbol, a type
1671 change warning may still be appropriate. */
1672
1673 if (h->root.type == bfd_link_hash_common)
1674 *type_change_ok = true;
1675 }
1676
1677 /* Handle the special case of an old common symbol merging with a
1678 new symbol which looks like a common symbol in a shared object.
1679 We change *PSEC and *PVALUE to make the new symbol look like a
1680 common symbol, and let _bfd_generic_link_add_one_symbol do the
1681 right thing. */
1682
1683 if (newdyncommon
1684 && h->root.type == bfd_link_hash_common)
1685 {
1686 *override = oldbfd;
1687 newdef = false;
1688 newdyncommon = false;
1689 *pvalue = sym->st_size;
1690 *psec = sec = bed->common_section (oldsec);
1691 *size_change_ok = true;
1692 }
1693
1694 /* Skip weak definitions of symbols that are already defined. */
1695 if (newdef && olddef && newweak)
1696 {
1697 /* Don't skip new non-IR weak syms. */
1698 if (!(oldbfd != NULL
1699 && (oldbfd->flags & BFD_PLUGIN) != 0
1700 && (abfd->flags & BFD_PLUGIN) == 0))
1701 {
1702 newdef = false;
1703 *skip = true;
1704 }
1705
1706 /* Merge st_other. If the symbol already has a dynamic index,
1707 but visibility says it should not be visible, turn it into a
1708 local symbol. */
1709 elf_merge_st_other (abfd, h, sym->st_other, sec, newdef, newdyn);
1710 if (h->dynindx != -1)
1711 switch (ELF_ST_VISIBILITY (h->other))
1712 {
1713 case STV_INTERNAL:
1714 case STV_HIDDEN:
1715 (*bed->elf_backend_hide_symbol) (info, h, true);
1716 break;
1717 }
1718 }
1719
1720 /* If the old symbol is from a dynamic object, and the new symbol is
1721 a definition which is not from a dynamic object, then the new
1722 symbol overrides the old symbol. Symbols from regular files
1723 always take precedence over symbols from dynamic objects, even if
1724 they are defined after the dynamic object in the link.
1725
1726 As above, we again permit a common symbol in a regular object to
1727 override a definition in a shared object if the shared object
1728 symbol is a function or is weak. */
1729
1730 flip = NULL;
1731 if (!newdyn
1732 && (newdef
1733 || (bfd_is_com_section (sec)
1734 && (oldweak || oldfunc)))
1735 && olddyn
1736 && olddef
1737 && h->def_dynamic)
1738 {
1739 /* Change the hash table entry to undefined, and let
1740 _bfd_generic_link_add_one_symbol do the right thing with the
1741 new definition. */
1742
1743 h->root.type = bfd_link_hash_undefined;
1744 h->root.u.undef.abfd = h->root.u.def.section->owner;
1745 *size_change_ok = true;
1746
1747 olddef = false;
1748 olddyncommon = false;
1749
1750 /* We again permit a type change when a common symbol may be
1751 overriding a function. */
1752
1753 if (bfd_is_com_section (sec))
1754 {
1755 if (oldfunc)
1756 {
1757 /* If a common symbol overrides a function, make sure
1758 that it isn't defined dynamically nor has type
1759 function. */
1760 h->def_dynamic = 0;
1761 h->type = STT_NOTYPE;
1762 }
1763 *type_change_ok = true;
1764 }
1765
1766 if (hi->root.type == bfd_link_hash_indirect)
1767 flip = hi;
1768 else
1769 /* This union may have been set to be non-NULL when this symbol
1770 was seen in a dynamic object. We must force the union to be
1771 NULL, so that it is correct for a regular symbol. */
1772 h->verinfo.vertree = NULL;
1773 }
1774
1775 /* Handle the special case of a new common symbol merging with an
1776 old symbol that looks like it might be a common symbol defined in
1777 a shared object. Note that we have already handled the case in
1778 which a new common symbol should simply override the definition
1779 in the shared library. */
1780
1781 if (! newdyn
1782 && bfd_is_com_section (sec)
1783 && olddyncommon)
1784 {
1785 /* It would be best if we could set the hash table entry to a
1786 common symbol, but we don't know what to use for the section
1787 or the alignment. */
1788 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1789 bfd_link_hash_common, sym->st_size);
1790
1791 /* If the presumed common symbol in the dynamic object is
1792 larger, pretend that the new symbol has its size. */
1793
1794 if (h->size > *pvalue)
1795 *pvalue = h->size;
1796
1797 /* We need to remember the alignment required by the symbol
1798 in the dynamic object. */
1799 BFD_ASSERT (pold_alignment);
1800 *pold_alignment = h->root.u.def.section->alignment_power;
1801
1802 olddef = false;
1803 olddyncommon = false;
1804
1805 h->root.type = bfd_link_hash_undefined;
1806 h->root.u.undef.abfd = h->root.u.def.section->owner;
1807
1808 *size_change_ok = true;
1809 *type_change_ok = true;
1810
1811 if (hi->root.type == bfd_link_hash_indirect)
1812 flip = hi;
1813 else
1814 h->verinfo.vertree = NULL;
1815 }
1816
1817 if (flip != NULL)
1818 {
1819 /* Handle the case where we had a versioned symbol in a dynamic
1820 library and now find a definition in a normal object. In this
1821 case, we make the versioned symbol point to the normal one. */
1822 flip->root.type = h->root.type;
1823 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1824 h->root.type = bfd_link_hash_indirect;
1825 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1826 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1827 if (h->def_dynamic)
1828 {
1829 h->def_dynamic = 0;
1830 flip->ref_dynamic = 1;
1831 }
1832 }
1833
1834 return true;
1835 }
1836
1837 /* This function is called to create an indirect symbol from the
1838 default for the symbol with the default version if needed. The
1839 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1840 set DYNSYM if the new indirect symbol is dynamic. */
1841
1842 static bool
1843 _bfd_elf_add_default_symbol (bfd *abfd,
1844 struct bfd_link_info *info,
1845 struct elf_link_hash_entry *h,
1846 const char *name,
1847 Elf_Internal_Sym *sym,
1848 asection *sec,
1849 bfd_vma value,
1850 bfd **poldbfd,
1851 bool *dynsym)
1852 {
1853 bool type_change_ok;
1854 bool size_change_ok;
1855 bool skip;
1856 char *shortname;
1857 struct elf_link_hash_entry *hi;
1858 struct bfd_link_hash_entry *bh;
1859 const struct elf_backend_data *bed;
1860 bool collect;
1861 bool dynamic;
1862 bfd *override;
1863 char *p;
1864 size_t len, shortlen;
1865 asection *tmp_sec;
1866 bool matched;
1867
1868 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1869 return true;
1870
1871 /* If this symbol has a version, and it is the default version, we
1872 create an indirect symbol from the default name to the fully
1873 decorated name. This will cause external references which do not
1874 specify a version to be bound to this version of the symbol. */
1875 p = strchr (name, ELF_VER_CHR);
1876 if (h->versioned == unknown)
1877 {
1878 if (p == NULL)
1879 {
1880 h->versioned = unversioned;
1881 return true;
1882 }
1883 else
1884 {
1885 if (p[1] != ELF_VER_CHR)
1886 {
1887 h->versioned = versioned_hidden;
1888 return true;
1889 }
1890 else
1891 h->versioned = versioned;
1892 }
1893 }
1894 else
1895 {
1896 /* PR ld/19073: We may see an unversioned definition after the
1897 default version. */
1898 if (p == NULL)
1899 return true;
1900 }
1901
1902 bed = get_elf_backend_data (abfd);
1903 collect = bed->collect;
1904 dynamic = (abfd->flags & DYNAMIC) != 0;
1905
1906 shortlen = p - name;
1907 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1908 if (shortname == NULL)
1909 return false;
1910 memcpy (shortname, name, shortlen);
1911 shortname[shortlen] = '\0';
1912
1913 /* We are going to create a new symbol. Merge it with any existing
1914 symbol with this name. For the purposes of the merge, act as
1915 though we were defining the symbol we just defined, although we
1916 actually going to define an indirect symbol. */
1917 type_change_ok = false;
1918 size_change_ok = false;
1919 matched = true;
1920 tmp_sec = sec;
1921 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1922 &hi, poldbfd, NULL, NULL, &skip, &override,
1923 &type_change_ok, &size_change_ok, &matched))
1924 return false;
1925
1926 if (skip)
1927 goto nondefault;
1928
1929 if (hi->def_regular || ELF_COMMON_DEF_P (hi))
1930 {
1931 /* If the undecorated symbol will have a version added by a
1932 script different to H, then don't indirect to/from the
1933 undecorated symbol. This isn't ideal because we may not yet
1934 have seen symbol versions, if given by a script on the
1935 command line rather than via --version-script. */
1936 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1937 {
1938 bool hide;
1939
1940 hi->verinfo.vertree
1941 = bfd_find_version_for_sym (info->version_info,
1942 hi->root.root.string, &hide);
1943 if (hi->verinfo.vertree != NULL && hide)
1944 {
1945 (*bed->elf_backend_hide_symbol) (info, hi, true);
1946 goto nondefault;
1947 }
1948 }
1949 if (hi->verinfo.vertree != NULL
1950 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1951 goto nondefault;
1952 }
1953
1954 if (! override)
1955 {
1956 /* Add the default symbol if not performing a relocatable link. */
1957 if (! bfd_link_relocatable (info))
1958 {
1959 bh = &hi->root;
1960 if (bh->type == bfd_link_hash_defined
1961 && bh->u.def.section->owner != NULL
1962 && (bh->u.def.section->owner->flags & BFD_PLUGIN) != 0)
1963 {
1964 /* Mark the previous definition from IR object as
1965 undefined so that the generic linker will override
1966 it. */
1967 bh->type = bfd_link_hash_undefined;
1968 bh->u.undef.abfd = bh->u.def.section->owner;
1969 }
1970 if (! (_bfd_generic_link_add_one_symbol
1971 (info, abfd, shortname, BSF_INDIRECT,
1972 bfd_ind_section_ptr,
1973 0, name, false, collect, &bh)))
1974 return false;
1975 hi = (struct elf_link_hash_entry *) bh;
1976 }
1977 }
1978 else
1979 {
1980 /* In this case the symbol named SHORTNAME is overriding the
1981 indirect symbol we want to add. We were planning on making
1982 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1983 is the name without a version. NAME is the fully versioned
1984 name, and it is the default version.
1985
1986 Overriding means that we already saw a definition for the
1987 symbol SHORTNAME in a regular object, and it is overriding
1988 the symbol defined in the dynamic object.
1989
1990 When this happens, we actually want to change NAME, the
1991 symbol we just added, to refer to SHORTNAME. This will cause
1992 references to NAME in the shared object to become references
1993 to SHORTNAME in the regular object. This is what we expect
1994 when we override a function in a shared object: that the
1995 references in the shared object will be mapped to the
1996 definition in the regular object. */
1997
1998 while (hi->root.type == bfd_link_hash_indirect
1999 || hi->root.type == bfd_link_hash_warning)
2000 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2001
2002 h->root.type = bfd_link_hash_indirect;
2003 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
2004 if (h->def_dynamic)
2005 {
2006 h->def_dynamic = 0;
2007 hi->ref_dynamic = 1;
2008 if (hi->ref_regular
2009 || hi->def_regular)
2010 {
2011 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
2012 return false;
2013 }
2014 }
2015
2016 /* Now set HI to H, so that the following code will set the
2017 other fields correctly. */
2018 hi = h;
2019 }
2020
2021 /* Check if HI is a warning symbol. */
2022 if (hi->root.type == bfd_link_hash_warning)
2023 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2024
2025 /* If there is a duplicate definition somewhere, then HI may not
2026 point to an indirect symbol. We will have reported an error to
2027 the user in that case. */
2028
2029 if (hi->root.type == bfd_link_hash_indirect)
2030 {
2031 struct elf_link_hash_entry *ht;
2032
2033 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
2034 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
2035
2036 /* If we first saw a reference to SHORTNAME with non-default
2037 visibility, merge that visibility to the @@VER symbol. */
2038 elf_merge_st_other (abfd, ht, hi->other, sec, true, dynamic);
2039
2040 /* A reference to the SHORTNAME symbol from a dynamic library
2041 will be satisfied by the versioned symbol at runtime. In
2042 effect, we have a reference to the versioned symbol. */
2043 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2044 hi->dynamic_def |= ht->dynamic_def;
2045
2046 /* See if the new flags lead us to realize that the symbol must
2047 be dynamic. */
2048 if (! *dynsym)
2049 {
2050 if (! dynamic)
2051 {
2052 if (! bfd_link_executable (info)
2053 || hi->def_dynamic
2054 || hi->ref_dynamic)
2055 *dynsym = true;
2056 }
2057 else
2058 {
2059 if (hi->ref_regular)
2060 *dynsym = true;
2061 }
2062 }
2063 }
2064
2065 /* We also need to define an indirection from the nondefault version
2066 of the symbol. */
2067
2068 nondefault:
2069 len = strlen (name);
2070 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
2071 if (shortname == NULL)
2072 return false;
2073 memcpy (shortname, name, shortlen);
2074 memcpy (shortname + shortlen, p + 1, len - shortlen);
2075
2076 /* Once again, merge with any existing symbol. */
2077 type_change_ok = false;
2078 size_change_ok = false;
2079 tmp_sec = sec;
2080 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
2081 &hi, poldbfd, NULL, NULL, &skip, &override,
2082 &type_change_ok, &size_change_ok, &matched))
2083 return false;
2084
2085 if (skip)
2086 {
2087 if (!dynamic
2088 && h->root.type == bfd_link_hash_defweak
2089 && hi->root.type == bfd_link_hash_defined)
2090 {
2091 /* We are handling a weak sym@@ver and attempting to define
2092 a weak sym@ver, but _bfd_elf_merge_symbol said to skip the
2093 new weak sym@ver because there is already a strong sym@ver.
2094 However, sym@ver and sym@@ver are really the same symbol.
2095 The existing strong sym@ver ought to override sym@@ver. */
2096 h->root.type = bfd_link_hash_defined;
2097 h->root.u.def.section = hi->root.u.def.section;
2098 h->root.u.def.value = hi->root.u.def.value;
2099 hi->root.type = bfd_link_hash_indirect;
2100 hi->root.u.i.link = &h->root;
2101 }
2102 else
2103 return true;
2104 }
2105 else if (override)
2106 {
2107 /* Here SHORTNAME is a versioned name, so we don't expect to see
2108 the type of override we do in the case above unless it is
2109 overridden by a versioned definition. */
2110 if (hi->root.type != bfd_link_hash_defined
2111 && hi->root.type != bfd_link_hash_defweak)
2112 _bfd_error_handler
2113 /* xgettext:c-format */
2114 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
2115 abfd, shortname);
2116 return true;
2117 }
2118 else
2119 {
2120 bh = &hi->root;
2121 if (! (_bfd_generic_link_add_one_symbol
2122 (info, abfd, shortname, BSF_INDIRECT,
2123 bfd_ind_section_ptr, 0, name, false, collect, &bh)))
2124 return false;
2125 hi = (struct elf_link_hash_entry *) bh;
2126 }
2127
2128 /* If there is a duplicate definition somewhere, then HI may not
2129 point to an indirect symbol. We will have reported an error
2130 to the user in that case. */
2131 if (hi->root.type == bfd_link_hash_indirect)
2132 {
2133 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
2134 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2135 hi->dynamic_def |= h->dynamic_def;
2136
2137 /* If we first saw a reference to @VER symbol with
2138 non-default visibility, merge that visibility to the
2139 @@VER symbol. */
2140 elf_merge_st_other (abfd, h, hi->other, sec, true, dynamic);
2141
2142 /* See if the new flags lead us to realize that the symbol
2143 must be dynamic. */
2144 if (! *dynsym)
2145 {
2146 if (! dynamic)
2147 {
2148 if (! bfd_link_executable (info)
2149 || hi->ref_dynamic)
2150 *dynsym = true;
2151 }
2152 else
2153 {
2154 if (hi->ref_regular)
2155 *dynsym = true;
2156 }
2157 }
2158 }
2159
2160 return true;
2161 }
2162 \f
2163 /* This routine is used to export all defined symbols into the dynamic
2164 symbol table. It is called via elf_link_hash_traverse. */
2165
2166 static bool
2167 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
2168 {
2169 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2170
2171 /* Ignore indirect symbols. These are added by the versioning code. */
2172 if (h->root.type == bfd_link_hash_indirect)
2173 return true;
2174
2175 /* Ignore this if we won't export it. */
2176 if (!eif->info->export_dynamic && !h->dynamic)
2177 return true;
2178
2179 if (h->dynindx == -1
2180 && (h->def_regular || h->ref_regular)
2181 && ! bfd_hide_sym_by_version (eif->info->version_info,
2182 h->root.root.string))
2183 {
2184 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2185 {
2186 eif->failed = true;
2187 return false;
2188 }
2189 }
2190
2191 return true;
2192 }
2193 \f
2194 /* Look through the symbols which are defined in other shared
2195 libraries and referenced here. Update the list of version
2196 dependencies. This will be put into the .gnu.version_r section.
2197 This function is called via elf_link_hash_traverse. */
2198
2199 static bool
2200 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2201 void *data)
2202 {
2203 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2204 Elf_Internal_Verneed *t;
2205 Elf_Internal_Vernaux *a;
2206 size_t amt;
2207
2208 /* We only care about symbols defined in shared objects with version
2209 information. */
2210 if (!h->def_dynamic
2211 || h->def_regular
2212 || h->dynindx == -1
2213 || h->verinfo.verdef == NULL
2214 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2215 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2216 return true;
2217
2218 /* See if we already know about this version. */
2219 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2220 t != NULL;
2221 t = t->vn_nextref)
2222 {
2223 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2224 continue;
2225
2226 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2227 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2228 return true;
2229
2230 break;
2231 }
2232
2233 /* This is a new version. Add it to tree we are building. */
2234
2235 if (t == NULL)
2236 {
2237 amt = sizeof *t;
2238 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2239 if (t == NULL)
2240 {
2241 rinfo->failed = true;
2242 return false;
2243 }
2244
2245 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2246 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2247 elf_tdata (rinfo->info->output_bfd)->verref = t;
2248 }
2249
2250 amt = sizeof *a;
2251 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2252 if (a == NULL)
2253 {
2254 rinfo->failed = true;
2255 return false;
2256 }
2257
2258 /* Note that we are copying a string pointer here, and testing it
2259 above. If bfd_elf_string_from_elf_section is ever changed to
2260 discard the string data when low in memory, this will have to be
2261 fixed. */
2262 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2263
2264 a->vna_flags = h->verinfo.verdef->vd_flags;
2265 a->vna_nextptr = t->vn_auxptr;
2266
2267 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2268 ++rinfo->vers;
2269
2270 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2271
2272 t->vn_auxptr = a;
2273
2274 return true;
2275 }
2276
2277 /* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2278 hidden. Set *T_P to NULL if there is no match. */
2279
2280 static bool
2281 _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
2282 struct elf_link_hash_entry *h,
2283 const char *version_p,
2284 struct bfd_elf_version_tree **t_p,
2285 bool *hide)
2286 {
2287 struct bfd_elf_version_tree *t;
2288
2289 /* Look for the version. If we find it, it is no longer weak. */
2290 for (t = info->version_info; t != NULL; t = t->next)
2291 {
2292 if (strcmp (t->name, version_p) == 0)
2293 {
2294 size_t len;
2295 char *alc;
2296 struct bfd_elf_version_expr *d;
2297
2298 len = version_p - h->root.root.string;
2299 alc = (char *) bfd_malloc (len);
2300 if (alc == NULL)
2301 return false;
2302 memcpy (alc, h->root.root.string, len - 1);
2303 alc[len - 1] = '\0';
2304 if (alc[len - 2] == ELF_VER_CHR)
2305 alc[len - 2] = '\0';
2306
2307 h->verinfo.vertree = t;
2308 t->used = true;
2309 d = NULL;
2310
2311 if (t->globals.list != NULL)
2312 d = (*t->match) (&t->globals, NULL, alc);
2313
2314 /* See if there is anything to force this symbol to
2315 local scope. */
2316 if (d == NULL && t->locals.list != NULL)
2317 {
2318 d = (*t->match) (&t->locals, NULL, alc);
2319 if (d != NULL
2320 && h->dynindx != -1
2321 && ! info->export_dynamic)
2322 *hide = true;
2323 }
2324
2325 free (alc);
2326 break;
2327 }
2328 }
2329
2330 *t_p = t;
2331
2332 return true;
2333 }
2334
2335 /* Return TRUE if the symbol H is hidden by version script. */
2336
2337 bool
2338 _bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
2339 struct elf_link_hash_entry *h)
2340 {
2341 const char *p;
2342 bool hide = false;
2343 const struct elf_backend_data *bed
2344 = get_elf_backend_data (info->output_bfd);
2345
2346 /* Version script only hides symbols defined in regular objects. */
2347 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2348 return true;
2349
2350 p = strchr (h->root.root.string, ELF_VER_CHR);
2351 if (p != NULL && h->verinfo.vertree == NULL)
2352 {
2353 struct bfd_elf_version_tree *t;
2354
2355 ++p;
2356 if (*p == ELF_VER_CHR)
2357 ++p;
2358
2359 if (*p != '\0'
2360 && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
2361 && hide)
2362 {
2363 if (hide)
2364 (*bed->elf_backend_hide_symbol) (info, h, true);
2365 return true;
2366 }
2367 }
2368
2369 /* If we don't have a version for this symbol, see if we can find
2370 something. */
2371 if (h->verinfo.vertree == NULL && info->version_info != NULL)
2372 {
2373 h->verinfo.vertree
2374 = bfd_find_version_for_sym (info->version_info,
2375 h->root.root.string, &hide);
2376 if (h->verinfo.vertree != NULL && hide)
2377 {
2378 (*bed->elf_backend_hide_symbol) (info, h, true);
2379 return true;
2380 }
2381 }
2382
2383 return false;
2384 }
2385
2386 /* Figure out appropriate versions for all the symbols. We may not
2387 have the version number script until we have read all of the input
2388 files, so until that point we don't know which symbols should be
2389 local. This function is called via elf_link_hash_traverse. */
2390
2391 static bool
2392 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2393 {
2394 struct elf_info_failed *sinfo;
2395 struct bfd_link_info *info;
2396 const struct elf_backend_data *bed;
2397 struct elf_info_failed eif;
2398 char *p;
2399 bool hide;
2400
2401 sinfo = (struct elf_info_failed *) data;
2402 info = sinfo->info;
2403
2404 /* Fix the symbol flags. */
2405 eif.failed = false;
2406 eif.info = info;
2407 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2408 {
2409 if (eif.failed)
2410 sinfo->failed = true;
2411 return false;
2412 }
2413
2414 bed = get_elf_backend_data (info->output_bfd);
2415
2416 /* We only need version numbers for symbols defined in regular
2417 objects. */
2418 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2419 {
2420 /* Hide symbols defined in discarded input sections. */
2421 if ((h->root.type == bfd_link_hash_defined
2422 || h->root.type == bfd_link_hash_defweak)
2423 && discarded_section (h->root.u.def.section))
2424 (*bed->elf_backend_hide_symbol) (info, h, true);
2425 return true;
2426 }
2427
2428 hide = false;
2429 p = strchr (h->root.root.string, ELF_VER_CHR);
2430 if (p != NULL && h->verinfo.vertree == NULL)
2431 {
2432 struct bfd_elf_version_tree *t;
2433
2434 ++p;
2435 if (*p == ELF_VER_CHR)
2436 ++p;
2437
2438 /* If there is no version string, we can just return out. */
2439 if (*p == '\0')
2440 return true;
2441
2442 if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
2443 {
2444 sinfo->failed = true;
2445 return false;
2446 }
2447
2448 if (hide)
2449 (*bed->elf_backend_hide_symbol) (info, h, true);
2450
2451 /* If we are building an application, we need to create a
2452 version node for this version. */
2453 if (t == NULL && bfd_link_executable (info))
2454 {
2455 struct bfd_elf_version_tree **pp;
2456 int version_index;
2457
2458 /* If we aren't going to export this symbol, we don't need
2459 to worry about it. */
2460 if (h->dynindx == -1)
2461 return true;
2462
2463 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2464 sizeof *t);
2465 if (t == NULL)
2466 {
2467 sinfo->failed = true;
2468 return false;
2469 }
2470
2471 t->name = p;
2472 t->name_indx = (unsigned int) -1;
2473 t->used = true;
2474
2475 version_index = 1;
2476 /* Don't count anonymous version tag. */
2477 if (sinfo->info->version_info != NULL
2478 && sinfo->info->version_info->vernum == 0)
2479 version_index = 0;
2480 for (pp = &sinfo->info->version_info;
2481 *pp != NULL;
2482 pp = &(*pp)->next)
2483 ++version_index;
2484 t->vernum = version_index;
2485
2486 *pp = t;
2487
2488 h->verinfo.vertree = t;
2489 }
2490 else if (t == NULL)
2491 {
2492 /* We could not find the version for a symbol when
2493 generating a shared archive. Return an error. */
2494 _bfd_error_handler
2495 /* xgettext:c-format */
2496 (_("%pB: version node not found for symbol %s"),
2497 info->output_bfd, h->root.root.string);
2498 bfd_set_error (bfd_error_bad_value);
2499 sinfo->failed = true;
2500 return false;
2501 }
2502 }
2503
2504 /* If we don't have a version for this symbol, see if we can find
2505 something. */
2506 if (!hide
2507 && h->verinfo.vertree == NULL
2508 && sinfo->info->version_info != NULL)
2509 {
2510 h->verinfo.vertree
2511 = bfd_find_version_for_sym (sinfo->info->version_info,
2512 h->root.root.string, &hide);
2513 if (h->verinfo.vertree != NULL && hide)
2514 (*bed->elf_backend_hide_symbol) (info, h, true);
2515 }
2516
2517 return true;
2518 }
2519 \f
2520 /* Read and swap the relocs from the section indicated by SHDR. This
2521 may be either a REL or a RELA section. The relocations are
2522 translated into RELA relocations and stored in INTERNAL_RELOCS,
2523 which should have already been allocated to contain enough space.
2524 The EXTERNAL_RELOCS are a buffer where the external form of the
2525 relocations should be stored.
2526
2527 Returns FALSE if something goes wrong. */
2528
2529 static bool
2530 elf_link_read_relocs_from_section (bfd *abfd,
2531 asection *sec,
2532 Elf_Internal_Shdr *shdr,
2533 void *external_relocs,
2534 Elf_Internal_Rela *internal_relocs)
2535 {
2536 const struct elf_backend_data *bed;
2537 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2538 const bfd_byte *erela;
2539 const bfd_byte *erelaend;
2540 Elf_Internal_Rela *irela;
2541 Elf_Internal_Shdr *symtab_hdr;
2542 size_t nsyms;
2543
2544 /* Position ourselves at the start of the section. */
2545 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2546 return false;
2547
2548 /* Read the relocations. */
2549 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2550 return false;
2551
2552 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2553 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2554
2555 bed = get_elf_backend_data (abfd);
2556
2557 /* Convert the external relocations to the internal format. */
2558 if (shdr->sh_entsize == bed->s->sizeof_rel)
2559 swap_in = bed->s->swap_reloc_in;
2560 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2561 swap_in = bed->s->swap_reloca_in;
2562 else
2563 {
2564 bfd_set_error (bfd_error_wrong_format);
2565 return false;
2566 }
2567
2568 erela = (const bfd_byte *) external_relocs;
2569 /* Setting erelaend like this and comparing with <= handles case of
2570 a fuzzed object with sh_size not a multiple of sh_entsize. */
2571 erelaend = erela + shdr->sh_size - shdr->sh_entsize;
2572 irela = internal_relocs;
2573 while (erela <= erelaend)
2574 {
2575 bfd_vma r_symndx;
2576
2577 (*swap_in) (abfd, erela, irela);
2578 r_symndx = ELF32_R_SYM (irela->r_info);
2579 if (bed->s->arch_size == 64)
2580 r_symndx >>= 24;
2581 if (nsyms > 0)
2582 {
2583 if ((size_t) r_symndx >= nsyms)
2584 {
2585 _bfd_error_handler
2586 /* xgettext:c-format */
2587 (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
2588 " for offset %#" PRIx64 " in section `%pA'"),
2589 abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
2590 (uint64_t) irela->r_offset, sec);
2591 bfd_set_error (bfd_error_bad_value);
2592 return false;
2593 }
2594 }
2595 else if (r_symndx != STN_UNDEF)
2596 {
2597 _bfd_error_handler
2598 /* xgettext:c-format */
2599 (_("%pB: non-zero symbol index (%#" PRIx64 ")"
2600 " for offset %#" PRIx64 " in section `%pA'"
2601 " when the object file has no symbol table"),
2602 abfd, (uint64_t) r_symndx,
2603 (uint64_t) irela->r_offset, sec);
2604 bfd_set_error (bfd_error_bad_value);
2605 return false;
2606 }
2607 irela += bed->s->int_rels_per_ext_rel;
2608 erela += shdr->sh_entsize;
2609 }
2610
2611 return true;
2612 }
2613
2614 /* Read and swap the relocs for a section O. They may have been
2615 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2616 not NULL, they are used as buffers to read into. They are known to
2617 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2618 the return value is allocated using either malloc or bfd_alloc,
2619 according to the KEEP_MEMORY argument. If O has two relocation
2620 sections (both REL and RELA relocations), then the REL_HDR
2621 relocations will appear first in INTERNAL_RELOCS, followed by the
2622 RELA_HDR relocations. If INFO isn't NULL and KEEP_MEMORY is true,
2623 update cache_size. */
2624
2625 Elf_Internal_Rela *
2626 _bfd_elf_link_info_read_relocs (bfd *abfd,
2627 struct bfd_link_info *info,
2628 asection *o,
2629 void *external_relocs,
2630 Elf_Internal_Rela *internal_relocs,
2631 bool keep_memory)
2632 {
2633 void *alloc1 = NULL;
2634 Elf_Internal_Rela *alloc2 = NULL;
2635 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2636 struct bfd_elf_section_data *esdo = elf_section_data (o);
2637 Elf_Internal_Rela *internal_rela_relocs;
2638
2639 if (esdo->relocs != NULL)
2640 return esdo->relocs;
2641
2642 if (o->reloc_count == 0)
2643 return NULL;
2644
2645 if (internal_relocs == NULL)
2646 {
2647 bfd_size_type size;
2648
2649 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
2650 if (keep_memory)
2651 {
2652 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2653 if (info)
2654 info->cache_size += size;
2655 }
2656 else
2657 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2658 if (internal_relocs == NULL)
2659 goto error_return;
2660 }
2661
2662 if (external_relocs == NULL)
2663 {
2664 bfd_size_type size = 0;
2665
2666 if (esdo->rel.hdr)
2667 size += esdo->rel.hdr->sh_size;
2668 if (esdo->rela.hdr)
2669 size += esdo->rela.hdr->sh_size;
2670
2671 alloc1 = bfd_malloc (size);
2672 if (alloc1 == NULL)
2673 goto error_return;
2674 external_relocs = alloc1;
2675 }
2676
2677 internal_rela_relocs = internal_relocs;
2678 if (esdo->rel.hdr)
2679 {
2680 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2681 external_relocs,
2682 internal_relocs))
2683 goto error_return;
2684 external_relocs = (((bfd_byte *) external_relocs)
2685 + esdo->rel.hdr->sh_size);
2686 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2687 * bed->s->int_rels_per_ext_rel);
2688 }
2689
2690 if (esdo->rela.hdr
2691 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2692 external_relocs,
2693 internal_rela_relocs)))
2694 goto error_return;
2695
2696 /* Cache the results for next time, if we can. */
2697 if (keep_memory)
2698 esdo->relocs = internal_relocs;
2699
2700 free (alloc1);
2701
2702 /* Don't free alloc2, since if it was allocated we are passing it
2703 back (under the name of internal_relocs). */
2704
2705 return internal_relocs;
2706
2707 error_return:
2708 free (alloc1);
2709 if (alloc2 != NULL)
2710 {
2711 if (keep_memory)
2712 bfd_release (abfd, alloc2);
2713 else
2714 free (alloc2);
2715 }
2716 return NULL;
2717 }
2718
2719 /* This is similar to _bfd_elf_link_info_read_relocs, except for that
2720 NULL is passed to _bfd_elf_link_info_read_relocs for pointer to
2721 struct bfd_link_info. */
2722
2723 Elf_Internal_Rela *
2724 _bfd_elf_link_read_relocs (bfd *abfd,
2725 asection *o,
2726 void *external_relocs,
2727 Elf_Internal_Rela *internal_relocs,
2728 bool keep_memory)
2729 {
2730 return _bfd_elf_link_info_read_relocs (abfd, NULL, o, external_relocs,
2731 internal_relocs, keep_memory);
2732
2733 }
2734
2735 /* Compute the size of, and allocate space for, REL_HDR which is the
2736 section header for a section containing relocations for O. */
2737
2738 static bool
2739 _bfd_elf_link_size_reloc_section (bfd *abfd,
2740 struct bfd_elf_section_reloc_data *reldata)
2741 {
2742 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2743
2744 /* That allows us to calculate the size of the section. */
2745 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2746
2747 /* The contents field must last into write_object_contents, so we
2748 allocate it with bfd_alloc rather than malloc. Also since we
2749 cannot be sure that the contents will actually be filled in,
2750 we zero the allocated space. */
2751 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2752 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2753 return false;
2754
2755 if (reldata->hashes == NULL && reldata->count)
2756 {
2757 struct elf_link_hash_entry **p;
2758
2759 p = ((struct elf_link_hash_entry **)
2760 bfd_zmalloc (reldata->count * sizeof (*p)));
2761 if (p == NULL)
2762 return false;
2763
2764 reldata->hashes = p;
2765 }
2766
2767 return true;
2768 }
2769
2770 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2771 originated from the section given by INPUT_REL_HDR) to the
2772 OUTPUT_BFD. */
2773
2774 bool
2775 _bfd_elf_link_output_relocs (bfd *output_bfd,
2776 asection *input_section,
2777 Elf_Internal_Shdr *input_rel_hdr,
2778 Elf_Internal_Rela *internal_relocs,
2779 struct elf_link_hash_entry **rel_hash
2780 ATTRIBUTE_UNUSED)
2781 {
2782 Elf_Internal_Rela *irela;
2783 Elf_Internal_Rela *irelaend;
2784 bfd_byte *erel;
2785 struct bfd_elf_section_reloc_data *output_reldata;
2786 asection *output_section;
2787 const struct elf_backend_data *bed;
2788 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2789 struct bfd_elf_section_data *esdo;
2790
2791 output_section = input_section->output_section;
2792
2793 bed = get_elf_backend_data (output_bfd);
2794 esdo = elf_section_data (output_section);
2795 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2796 {
2797 output_reldata = &esdo->rel;
2798 swap_out = bed->s->swap_reloc_out;
2799 }
2800 else if (esdo->rela.hdr
2801 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2802 {
2803 output_reldata = &esdo->rela;
2804 swap_out = bed->s->swap_reloca_out;
2805 }
2806 else
2807 {
2808 _bfd_error_handler
2809 /* xgettext:c-format */
2810 (_("%pB: relocation size mismatch in %pB section %pA"),
2811 output_bfd, input_section->owner, input_section);
2812 bfd_set_error (bfd_error_wrong_format);
2813 return false;
2814 }
2815
2816 erel = output_reldata->hdr->contents;
2817 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2818 irela = internal_relocs;
2819 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2820 * bed->s->int_rels_per_ext_rel);
2821 while (irela < irelaend)
2822 {
2823 (*swap_out) (output_bfd, irela, erel);
2824 irela += bed->s->int_rels_per_ext_rel;
2825 erel += input_rel_hdr->sh_entsize;
2826 }
2827
2828 /* Bump the counter, so that we know where to add the next set of
2829 relocations. */
2830 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2831
2832 return true;
2833 }
2834 \f
2835 /* Make weak undefined symbols in PIE dynamic. */
2836
2837 bool
2838 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2839 struct elf_link_hash_entry *h)
2840 {
2841 if (bfd_link_pie (info)
2842 && h->dynindx == -1
2843 && h->root.type == bfd_link_hash_undefweak)
2844 return bfd_elf_link_record_dynamic_symbol (info, h);
2845
2846 return true;
2847 }
2848
2849 /* Fix up the flags for a symbol. This handles various cases which
2850 can only be fixed after all the input files are seen. This is
2851 currently called by both adjust_dynamic_symbol and
2852 assign_sym_version, which is unnecessary but perhaps more robust in
2853 the face of future changes. */
2854
2855 static bool
2856 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2857 struct elf_info_failed *eif)
2858 {
2859 const struct elf_backend_data *bed;
2860
2861 /* If this symbol was mentioned in a non-ELF file, try to set
2862 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2863 permit a non-ELF file to correctly refer to a symbol defined in
2864 an ELF dynamic object. */
2865 if (h->non_elf)
2866 {
2867 while (h->root.type == bfd_link_hash_indirect)
2868 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2869
2870 if (h->root.type != bfd_link_hash_defined
2871 && h->root.type != bfd_link_hash_defweak)
2872 {
2873 h->ref_regular = 1;
2874 h->ref_regular_nonweak = 1;
2875 }
2876 else
2877 {
2878 if (h->root.u.def.section->owner != NULL
2879 && (bfd_get_flavour (h->root.u.def.section->owner)
2880 == bfd_target_elf_flavour))
2881 {
2882 h->ref_regular = 1;
2883 h->ref_regular_nonweak = 1;
2884 }
2885 else
2886 h->def_regular = 1;
2887 }
2888
2889 if (h->dynindx == -1
2890 && (h->def_dynamic
2891 || h->ref_dynamic))
2892 {
2893 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2894 {
2895 eif->failed = true;
2896 return false;
2897 }
2898 }
2899 }
2900 else
2901 {
2902 /* Unfortunately, NON_ELF is only correct if the symbol
2903 was first seen in a non-ELF file. Fortunately, if the symbol
2904 was first seen in an ELF file, we're probably OK unless the
2905 symbol was defined in a non-ELF file. Catch that case here.
2906 FIXME: We're still in trouble if the symbol was first seen in
2907 a dynamic object, and then later in a non-ELF regular object. */
2908 if ((h->root.type == bfd_link_hash_defined
2909 || h->root.type == bfd_link_hash_defweak)
2910 && !h->def_regular
2911 && (h->root.u.def.section->owner != NULL
2912 ? (bfd_get_flavour (h->root.u.def.section->owner)
2913 != bfd_target_elf_flavour)
2914 : (bfd_is_abs_section (h->root.u.def.section)
2915 && !h->def_dynamic)))
2916 h->def_regular = 1;
2917 }
2918
2919 /* Backend specific symbol fixup. */
2920 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2921 if (bed->elf_backend_fixup_symbol
2922 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2923 return false;
2924
2925 /* If this is a final link, and the symbol was defined as a common
2926 symbol in a regular object file, and there was no definition in
2927 any dynamic object, then the linker will have allocated space for
2928 the symbol in a common section but the DEF_REGULAR
2929 flag will not have been set. */
2930 if (h->root.type == bfd_link_hash_defined
2931 && !h->def_regular
2932 && h->ref_regular
2933 && !h->def_dynamic
2934 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
2935 h->def_regular = 1;
2936
2937 /* Symbols defined in discarded sections shouldn't be dynamic. */
2938 if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
2939 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
2940
2941 /* If a weak undefined symbol has non-default visibility, we also
2942 hide it from the dynamic linker. */
2943 else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2944 && h->root.type == bfd_link_hash_undefweak)
2945 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
2946
2947 /* A hidden versioned symbol in executable should be forced local if
2948 it is is locally defined, not referenced by shared library and not
2949 exported. */
2950 else if (bfd_link_executable (eif->info)
2951 && h->versioned == versioned_hidden
2952 && !eif->info->export_dynamic
2953 && !h->dynamic
2954 && !h->ref_dynamic
2955 && h->def_regular)
2956 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
2957
2958 /* If -Bsymbolic was used (which means to bind references to global
2959 symbols to the definition within the shared object), and this
2960 symbol was defined in a regular object, then it actually doesn't
2961 need a PLT entry. Likewise, if the symbol has non-default
2962 visibility. If the symbol has hidden or internal visibility, we
2963 will force it local. */
2964 else if (h->needs_plt
2965 && bfd_link_pic (eif->info)
2966 && is_elf_hash_table (eif->info->hash)
2967 && (SYMBOLIC_BIND (eif->info, h)
2968 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2969 && h->def_regular)
2970 {
2971 bool force_local;
2972
2973 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2974 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2975 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2976 }
2977
2978 /* If this is a weak defined symbol in a dynamic object, and we know
2979 the real definition in the dynamic object, copy interesting flags
2980 over to the real definition. */
2981 if (h->is_weakalias)
2982 {
2983 struct elf_link_hash_entry *def = weakdef (h);
2984
2985 /* If the real definition is defined by a regular object file,
2986 don't do anything special. See the longer description in
2987 _bfd_elf_adjust_dynamic_symbol, below. If the def is not
2988 bfd_link_hash_defined as it was when put on the alias list
2989 then it must have originally been a versioned symbol (for
2990 which a non-versioned indirect symbol is created) and later
2991 a definition for the non-versioned symbol is found. In that
2992 case the indirection is flipped with the versioned symbol
2993 becoming an indirect pointing at the non-versioned symbol.
2994 Thus, not an alias any more. */
2995 if (def->def_regular
2996 || def->root.type != bfd_link_hash_defined)
2997 {
2998 h = def;
2999 while ((h = h->u.alias) != def)
3000 h->is_weakalias = 0;
3001 }
3002 else
3003 {
3004 while (h->root.type == bfd_link_hash_indirect)
3005 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3006 BFD_ASSERT (h->root.type == bfd_link_hash_defined
3007 || h->root.type == bfd_link_hash_defweak);
3008 BFD_ASSERT (def->def_dynamic);
3009 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
3010 }
3011 }
3012
3013 return true;
3014 }
3015
3016 /* Make the backend pick a good value for a dynamic symbol. This is
3017 called via elf_link_hash_traverse, and also calls itself
3018 recursively. */
3019
3020 static bool
3021 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
3022 {
3023 struct elf_info_failed *eif = (struct elf_info_failed *) data;
3024 struct elf_link_hash_table *htab;
3025 const struct elf_backend_data *bed;
3026
3027 if (! is_elf_hash_table (eif->info->hash))
3028 return false;
3029
3030 /* Ignore indirect symbols. These are added by the versioning code. */
3031 if (h->root.type == bfd_link_hash_indirect)
3032 return true;
3033
3034 /* Fix the symbol flags. */
3035 if (! _bfd_elf_fix_symbol_flags (h, eif))
3036 return false;
3037
3038 htab = elf_hash_table (eif->info);
3039 bed = get_elf_backend_data (htab->dynobj);
3040
3041 if (h->root.type == bfd_link_hash_undefweak)
3042 {
3043 if (eif->info->dynamic_undefined_weak == 0)
3044 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3045 else if (eif->info->dynamic_undefined_weak > 0
3046 && h->ref_regular
3047 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3048 && !bfd_hide_sym_by_version (eif->info->version_info,
3049 h->root.root.string))
3050 {
3051 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
3052 {
3053 eif->failed = true;
3054 return false;
3055 }
3056 }
3057 }
3058
3059 /* If this symbol does not require a PLT entry, and it is not
3060 defined by a dynamic object, or is not referenced by a regular
3061 object, ignore it. We do have to handle a weak defined symbol,
3062 even if no regular object refers to it, if we decided to add it
3063 to the dynamic symbol table. FIXME: Do we normally need to worry
3064 about symbols which are defined by one dynamic object and
3065 referenced by another one? */
3066 if (!h->needs_plt
3067 && h->type != STT_GNU_IFUNC
3068 && (h->def_regular
3069 || !h->def_dynamic
3070 || (!h->ref_regular
3071 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
3072 {
3073 h->plt = elf_hash_table (eif->info)->init_plt_offset;
3074 return true;
3075 }
3076
3077 /* If we've already adjusted this symbol, don't do it again. This
3078 can happen via a recursive call. */
3079 if (h->dynamic_adjusted)
3080 return true;
3081
3082 /* Don't look at this symbol again. Note that we must set this
3083 after checking the above conditions, because we may look at a
3084 symbol once, decide not to do anything, and then get called
3085 recursively later after REF_REGULAR is set below. */
3086 h->dynamic_adjusted = 1;
3087
3088 /* If this is a weak definition, and we know a real definition, and
3089 the real symbol is not itself defined by a regular object file,
3090 then get a good value for the real definition. We handle the
3091 real symbol first, for the convenience of the backend routine.
3092
3093 Note that there is a confusing case here. If the real definition
3094 is defined by a regular object file, we don't get the real symbol
3095 from the dynamic object, but we do get the weak symbol. If the
3096 processor backend uses a COPY reloc, then if some routine in the
3097 dynamic object changes the real symbol, we will not see that
3098 change in the corresponding weak symbol. This is the way other
3099 ELF linkers work as well, and seems to be a result of the shared
3100 library model.
3101
3102 I will clarify this issue. Most SVR4 shared libraries define the
3103 variable _timezone and define timezone as a weak synonym. The
3104 tzset call changes _timezone. If you write
3105 extern int timezone;
3106 int _timezone = 5;
3107 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3108 you might expect that, since timezone is a synonym for _timezone,
3109 the same number will print both times. However, if the processor
3110 backend uses a COPY reloc, then actually timezone will be copied
3111 into your process image, and, since you define _timezone
3112 yourself, _timezone will not. Thus timezone and _timezone will
3113 wind up at different memory locations. The tzset call will set
3114 _timezone, leaving timezone unchanged. */
3115
3116 if (h->is_weakalias)
3117 {
3118 struct elf_link_hash_entry *def = weakdef (h);
3119
3120 /* If we get to this point, there is an implicit reference to
3121 the alias by a regular object file via the weak symbol H. */
3122 def->ref_regular = 1;
3123
3124 /* Ensure that the backend adjust_dynamic_symbol function sees
3125 the strong alias before H by recursively calling ourselves. */
3126 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
3127 return false;
3128 }
3129
3130 /* If a symbol has no type and no size and does not require a PLT
3131 entry, then we are probably about to do the wrong thing here: we
3132 are probably going to create a COPY reloc for an empty object.
3133 This case can arise when a shared object is built with assembly
3134 code, and the assembly code fails to set the symbol type. */
3135 if (h->size == 0
3136 && h->type == STT_NOTYPE
3137 && !h->needs_plt)
3138 _bfd_error_handler
3139 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3140 h->root.root.string);
3141
3142 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3143 {
3144 eif->failed = true;
3145 return false;
3146 }
3147
3148 return true;
3149 }
3150
3151 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3152 DYNBSS. */
3153
3154 bool
3155 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3156 struct elf_link_hash_entry *h,
3157 asection *dynbss)
3158 {
3159 unsigned int power_of_two;
3160 bfd_vma mask;
3161 asection *sec = h->root.u.def.section;
3162
3163 /* The section alignment of the definition is the maximum alignment
3164 requirement of symbols defined in the section. Since we don't
3165 know the symbol alignment requirement, we start with the
3166 maximum alignment and check low bits of the symbol address
3167 for the minimum alignment. */
3168 power_of_two = bfd_section_alignment (sec);
3169 mask = ((bfd_vma) 1 << power_of_two) - 1;
3170 while ((h->root.u.def.value & mask) != 0)
3171 {
3172 mask >>= 1;
3173 --power_of_two;
3174 }
3175
3176 if (power_of_two > bfd_section_alignment (dynbss))
3177 {
3178 /* Adjust the section alignment if needed. */
3179 if (!bfd_set_section_alignment (dynbss, power_of_two))
3180 return false;
3181 }
3182
3183 /* We make sure that the symbol will be aligned properly. */
3184 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3185
3186 /* Define the symbol as being at this point in DYNBSS. */
3187 h->root.u.def.section = dynbss;
3188 h->root.u.def.value = dynbss->size;
3189
3190 /* Increment the size of DYNBSS to make room for the symbol. */
3191 dynbss->size += h->size;
3192
3193 /* No error if extern_protected_data is true. */
3194 if (h->protected_def
3195 && (!info->extern_protected_data
3196 || (info->extern_protected_data < 0
3197 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
3198 info->callbacks->einfo
3199 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3200 h->root.root.string);
3201
3202 return true;
3203 }
3204
3205 /* Adjust all external symbols pointing into SEC_MERGE sections
3206 to reflect the object merging within the sections. */
3207
3208 static bool
3209 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
3210 {
3211 asection *sec;
3212
3213 if ((h->root.type == bfd_link_hash_defined
3214 || h->root.type == bfd_link_hash_defweak)
3215 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
3216 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3217 {
3218 bfd *output_bfd = (bfd *) data;
3219
3220 h->root.u.def.value =
3221 _bfd_merged_section_offset (output_bfd,
3222 &h->root.u.def.section,
3223 elf_section_data (sec)->sec_info,
3224 h->root.u.def.value);
3225 }
3226
3227 return true;
3228 }
3229
3230 /* Returns false if the symbol referred to by H should be considered
3231 to resolve local to the current module, and true if it should be
3232 considered to bind dynamically. */
3233
3234 bool
3235 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3236 struct bfd_link_info *info,
3237 bool not_local_protected)
3238 {
3239 bool binding_stays_local_p;
3240 const struct elf_backend_data *bed;
3241 struct elf_link_hash_table *hash_table;
3242
3243 if (h == NULL)
3244 return false;
3245
3246 while (h->root.type == bfd_link_hash_indirect
3247 || h->root.type == bfd_link_hash_warning)
3248 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3249
3250 /* If it was forced local, then clearly it's not dynamic. */
3251 if (h->dynindx == -1)
3252 return false;
3253 if (h->forced_local)
3254 return false;
3255
3256 /* Identify the cases where name binding rules say that a
3257 visible symbol resolves locally. */
3258 binding_stays_local_p = (bfd_link_executable (info)
3259 || SYMBOLIC_BIND (info, h));
3260
3261 switch (ELF_ST_VISIBILITY (h->other))
3262 {
3263 case STV_INTERNAL:
3264 case STV_HIDDEN:
3265 return false;
3266
3267 case STV_PROTECTED:
3268 hash_table = elf_hash_table (info);
3269 if (!is_elf_hash_table (&hash_table->root))
3270 return false;
3271
3272 bed = get_elf_backend_data (hash_table->dynobj);
3273
3274 /* Proper resolution for function pointer equality may require
3275 that these symbols perhaps be resolved dynamically, even though
3276 we should be resolving them to the current module. */
3277 if (!not_local_protected || !bed->is_function_type (h->type))
3278 binding_stays_local_p = true;
3279 break;
3280
3281 default:
3282 break;
3283 }
3284
3285 /* If it isn't defined locally, then clearly it's dynamic. */
3286 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
3287 return true;
3288
3289 /* Otherwise, the symbol is dynamic if binding rules don't tell
3290 us that it remains local. */
3291 return !binding_stays_local_p;
3292 }
3293
3294 /* Return true if the symbol referred to by H should be considered
3295 to resolve local to the current module, and false otherwise. Differs
3296 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3297 undefined symbols. The two functions are virtually identical except
3298 for the place where dynindx == -1 is tested. If that test is true,
3299 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3300 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3301 defined symbols.
3302 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3303 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3304 treatment of undefined weak symbols. For those that do not make
3305 undefined weak symbols dynamic, both functions may return false. */
3306
3307 bool
3308 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3309 struct bfd_link_info *info,
3310 bool local_protected)
3311 {
3312 const struct elf_backend_data *bed;
3313 struct elf_link_hash_table *hash_table;
3314
3315 /* If it's a local sym, of course we resolve locally. */
3316 if (h == NULL)
3317 return true;
3318
3319 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3320 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3321 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3322 return true;
3323
3324 /* Forced local symbols resolve locally. */
3325 if (h->forced_local)
3326 return true;
3327
3328 /* Common symbols that become definitions don't get the DEF_REGULAR
3329 flag set, so test it first, and don't bail out. */
3330 if (ELF_COMMON_DEF_P (h))
3331 /* Do nothing. */;
3332 /* If we don't have a definition in a regular file, then we can't
3333 resolve locally. The sym is either undefined or dynamic. */
3334 else if (!h->def_regular)
3335 return false;
3336
3337 /* Non-dynamic symbols resolve locally. */
3338 if (h->dynindx == -1)
3339 return true;
3340
3341 /* At this point, we know the symbol is defined and dynamic. In an
3342 executable it must resolve locally, likewise when building symbolic
3343 shared libraries. */
3344 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3345 return true;
3346
3347 /* Now deal with defined dynamic symbols in shared libraries. Ones
3348 with default visibility might not resolve locally. */
3349 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3350 return false;
3351
3352 hash_table = elf_hash_table (info);
3353 if (!is_elf_hash_table (&hash_table->root))
3354 return true;
3355
3356 bed = get_elf_backend_data (hash_table->dynobj);
3357
3358 /* If extern_protected_data is false, STV_PROTECTED non-function
3359 symbols are local. */
3360 if ((!info->extern_protected_data
3361 || (info->extern_protected_data < 0
3362 && !bed->extern_protected_data))
3363 && !bed->is_function_type (h->type))
3364 return true;
3365
3366 /* Function pointer equality tests may require that STV_PROTECTED
3367 symbols be treated as dynamic symbols. If the address of a
3368 function not defined in an executable is set to that function's
3369 plt entry in the executable, then the address of the function in
3370 a shared library must also be the plt entry in the executable. */
3371 return local_protected;
3372 }
3373
3374 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3375 aligned. Returns the first TLS output section. */
3376
3377 struct bfd_section *
3378 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3379 {
3380 struct bfd_section *sec, *tls;
3381 unsigned int align = 0;
3382
3383 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3384 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3385 break;
3386 tls = sec;
3387
3388 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3389 if (sec->alignment_power > align)
3390 align = sec->alignment_power;
3391
3392 elf_hash_table (info)->tls_sec = tls;
3393
3394 /* Ensure the alignment of the first section (usually .tdata) is the largest
3395 alignment, so that the tls segment starts aligned. */
3396 if (tls != NULL)
3397 tls->alignment_power = align;
3398
3399 return tls;
3400 }
3401
3402 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3403 static bool
3404 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3405 Elf_Internal_Sym *sym)
3406 {
3407 const struct elf_backend_data *bed;
3408
3409 /* Local symbols do not count, but target specific ones might. */
3410 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3411 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3412 return false;
3413
3414 bed = get_elf_backend_data (abfd);
3415 /* Function symbols do not count. */
3416 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3417 return false;
3418
3419 /* If the section is undefined, then so is the symbol. */
3420 if (sym->st_shndx == SHN_UNDEF)
3421 return false;
3422
3423 /* If the symbol is defined in the common section, then
3424 it is a common definition and so does not count. */
3425 if (bed->common_definition (sym))
3426 return false;
3427
3428 /* If the symbol is in a target specific section then we
3429 must rely upon the backend to tell us what it is. */
3430 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3431 /* FIXME - this function is not coded yet:
3432
3433 return _bfd_is_global_symbol_definition (abfd, sym);
3434
3435 Instead for now assume that the definition is not global,
3436 Even if this is wrong, at least the linker will behave
3437 in the same way that it used to do. */
3438 return false;
3439
3440 return true;
3441 }
3442
3443 /* Search the symbol table of the archive element of the archive ABFD
3444 whose archive map contains a mention of SYMDEF, and determine if
3445 the symbol is defined in this element. */
3446 static bool
3447 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3448 {
3449 Elf_Internal_Shdr * hdr;
3450 size_t symcount;
3451 size_t extsymcount;
3452 size_t extsymoff;
3453 Elf_Internal_Sym *isymbuf;
3454 Elf_Internal_Sym *isym;
3455 Elf_Internal_Sym *isymend;
3456 bool result;
3457
3458 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3459 if (abfd == NULL)
3460 return false;
3461
3462 if (! bfd_check_format (abfd, bfd_object))
3463 return false;
3464
3465 /* Select the appropriate symbol table. If we don't know if the
3466 object file is an IR object, give linker LTO plugin a chance to
3467 get the correct symbol table. */
3468 if (abfd->plugin_format == bfd_plugin_yes
3469 #if BFD_SUPPORTS_PLUGINS
3470 || (abfd->plugin_format == bfd_plugin_unknown
3471 && bfd_link_plugin_object_p (abfd))
3472 #endif
3473 )
3474 {
3475 /* Use the IR symbol table if the object has been claimed by
3476 plugin. */
3477 abfd = abfd->plugin_dummy_bfd;
3478 hdr = &elf_tdata (abfd)->symtab_hdr;
3479 }
3480 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3481 hdr = &elf_tdata (abfd)->symtab_hdr;
3482 else
3483 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3484
3485 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3486
3487 /* The sh_info field of the symtab header tells us where the
3488 external symbols start. We don't care about the local symbols. */
3489 if (elf_bad_symtab (abfd))
3490 {
3491 extsymcount = symcount;
3492 extsymoff = 0;
3493 }
3494 else
3495 {
3496 extsymcount = symcount - hdr->sh_info;
3497 extsymoff = hdr->sh_info;
3498 }
3499
3500 if (extsymcount == 0)
3501 return false;
3502
3503 /* Read in the symbol table. */
3504 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3505 NULL, NULL, NULL);
3506 if (isymbuf == NULL)
3507 return false;
3508
3509 /* Scan the symbol table looking for SYMDEF. */
3510 result = false;
3511 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3512 {
3513 const char *name;
3514
3515 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3516 isym->st_name);
3517 if (name == NULL)
3518 break;
3519
3520 if (strcmp (name, symdef->name) == 0)
3521 {
3522 result = is_global_data_symbol_definition (abfd, isym);
3523 break;
3524 }
3525 }
3526
3527 free (isymbuf);
3528
3529 return result;
3530 }
3531 \f
3532 /* Add an entry to the .dynamic table. */
3533
3534 bool
3535 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3536 bfd_vma tag,
3537 bfd_vma val)
3538 {
3539 struct elf_link_hash_table *hash_table;
3540 const struct elf_backend_data *bed;
3541 asection *s;
3542 bfd_size_type newsize;
3543 bfd_byte *newcontents;
3544 Elf_Internal_Dyn dyn;
3545
3546 hash_table = elf_hash_table (info);
3547 if (! is_elf_hash_table (&hash_table->root))
3548 return false;
3549
3550 if (tag == DT_RELA || tag == DT_REL)
3551 hash_table->dynamic_relocs = true;
3552
3553 bed = get_elf_backend_data (hash_table->dynobj);
3554 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3555 BFD_ASSERT (s != NULL);
3556
3557 newsize = s->size + bed->s->sizeof_dyn;
3558 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3559 if (newcontents == NULL)
3560 return false;
3561
3562 dyn.d_tag = tag;
3563 dyn.d_un.d_val = val;
3564 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3565
3566 s->size = newsize;
3567 s->contents = newcontents;
3568
3569 return true;
3570 }
3571
3572 /* Strip zero-sized dynamic sections. */
3573
3574 bool
3575 _bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info *info)
3576 {
3577 struct elf_link_hash_table *hash_table;
3578 const struct elf_backend_data *bed;
3579 asection *s, *sdynamic, **pp;
3580 asection *rela_dyn, *rel_dyn;
3581 Elf_Internal_Dyn dyn;
3582 bfd_byte *extdyn, *next;
3583 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3584 bool strip_zero_sized;
3585 bool strip_zero_sized_plt;
3586
3587 if (bfd_link_relocatable (info))
3588 return true;
3589
3590 hash_table = elf_hash_table (info);
3591 if (!is_elf_hash_table (&hash_table->root))
3592 return false;
3593
3594 if (!hash_table->dynobj)
3595 return true;
3596
3597 sdynamic= bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3598 if (!sdynamic)
3599 return true;
3600
3601 bed = get_elf_backend_data (hash_table->dynobj);
3602 swap_dyn_in = bed->s->swap_dyn_in;
3603
3604 strip_zero_sized = false;
3605 strip_zero_sized_plt = false;
3606
3607 /* Strip zero-sized dynamic sections. */
3608 rela_dyn = bfd_get_section_by_name (info->output_bfd, ".rela.dyn");
3609 rel_dyn = bfd_get_section_by_name (info->output_bfd, ".rel.dyn");
3610 for (pp = &info->output_bfd->sections; (s = *pp) != NULL;)
3611 if (s->size == 0
3612 && (s == rela_dyn
3613 || s == rel_dyn
3614 || s == hash_table->srelplt->output_section
3615 || s == hash_table->splt->output_section))
3616 {
3617 *pp = s->next;
3618 info->output_bfd->section_count--;
3619 strip_zero_sized = true;
3620 if (s == rela_dyn)
3621 s = rela_dyn;
3622 if (s == rel_dyn)
3623 s = rel_dyn;
3624 else if (s == hash_table->splt->output_section)
3625 {
3626 s = hash_table->splt;
3627 strip_zero_sized_plt = true;
3628 }
3629 else
3630 s = hash_table->srelplt;
3631 s->flags |= SEC_EXCLUDE;
3632 s->output_section = bfd_abs_section_ptr;
3633 }
3634 else
3635 pp = &s->next;
3636
3637 if (strip_zero_sized_plt)
3638 for (extdyn = sdynamic->contents;
3639 extdyn < sdynamic->contents + sdynamic->size;
3640 extdyn = next)
3641 {
3642 next = extdyn + bed->s->sizeof_dyn;
3643 swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3644 switch (dyn.d_tag)
3645 {
3646 default:
3647 break;
3648 case DT_JMPREL:
3649 case DT_PLTRELSZ:
3650 case DT_PLTREL:
3651 /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
3652 the procedure linkage table (the .plt section) has been
3653 removed. */
3654 memmove (extdyn, next,
3655 sdynamic->size - (next - sdynamic->contents));
3656 next = extdyn;
3657 }
3658 }
3659
3660 if (strip_zero_sized)
3661 {
3662 /* Regenerate program headers. */
3663 elf_seg_map (info->output_bfd) = NULL;
3664 return _bfd_elf_map_sections_to_segments (info->output_bfd, info);
3665 }
3666
3667 return true;
3668 }
3669
3670 /* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error,
3671 1 if a DT_NEEDED tag already exists, and 0 on success. */
3672
3673 int
3674 bfd_elf_add_dt_needed_tag (bfd *abfd, struct bfd_link_info *info)
3675 {
3676 struct elf_link_hash_table *hash_table;
3677 size_t strindex;
3678 const char *soname;
3679
3680 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3681 return -1;
3682
3683 hash_table = elf_hash_table (info);
3684 soname = elf_dt_name (abfd);
3685 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, false);
3686 if (strindex == (size_t) -1)
3687 return -1;
3688
3689 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3690 {
3691 asection *sdyn;
3692 const struct elf_backend_data *bed;
3693 bfd_byte *extdyn;
3694
3695 bed = get_elf_backend_data (hash_table->dynobj);
3696 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3697 if (sdyn != NULL)
3698 for (extdyn = sdyn->contents;
3699 extdyn < sdyn->contents + sdyn->size;
3700 extdyn += bed->s->sizeof_dyn)
3701 {
3702 Elf_Internal_Dyn dyn;
3703
3704 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3705 if (dyn.d_tag == DT_NEEDED
3706 && dyn.d_un.d_val == strindex)
3707 {
3708 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3709 return 1;
3710 }
3711 }
3712 }
3713
3714 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3715 return -1;
3716
3717 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3718 return -1;
3719
3720 return 0;
3721 }
3722
3723 /* Return true if SONAME is on the needed list between NEEDED and STOP
3724 (or the end of list if STOP is NULL), and needed by a library that
3725 will be loaded. */
3726
3727 static bool
3728 on_needed_list (const char *soname,
3729 struct bfd_link_needed_list *needed,
3730 struct bfd_link_needed_list *stop)
3731 {
3732 struct bfd_link_needed_list *look;
3733 for (look = needed; look != stop; look = look->next)
3734 if (strcmp (soname, look->name) == 0
3735 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3736 /* If needed by a library that itself is not directly
3737 needed, recursively check whether that library is
3738 indirectly needed. Since we add DT_NEEDED entries to
3739 the end of the list, library dependencies appear after
3740 the library. Therefore search prior to the current
3741 LOOK, preventing possible infinite recursion. */
3742 || on_needed_list (elf_dt_name (look->by), needed, look)))
3743 return true;
3744
3745 return false;
3746 }
3747
3748 /* Sort symbol by value, section, size, and type. */
3749 static int
3750 elf_sort_symbol (const void *arg1, const void *arg2)
3751 {
3752 const struct elf_link_hash_entry *h1;
3753 const struct elf_link_hash_entry *h2;
3754 bfd_signed_vma vdiff;
3755 int sdiff;
3756 const char *n1;
3757 const char *n2;
3758
3759 h1 = *(const struct elf_link_hash_entry **) arg1;
3760 h2 = *(const struct elf_link_hash_entry **) arg2;
3761 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3762 if (vdiff != 0)
3763 return vdiff > 0 ? 1 : -1;
3764
3765 sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3766 if (sdiff != 0)
3767 return sdiff;
3768
3769 /* Sort so that sized symbols are selected over zero size symbols. */
3770 vdiff = h1->size - h2->size;
3771 if (vdiff != 0)
3772 return vdiff > 0 ? 1 : -1;
3773
3774 /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */
3775 if (h1->type != h2->type)
3776 return h1->type - h2->type;
3777
3778 /* If symbols are properly sized and typed, and multiple strong
3779 aliases are not defined in a shared library by the user we
3780 shouldn't get here. Unfortunately linker script symbols like
3781 __bss_start sometimes match a user symbol defined at the start of
3782 .bss without proper size and type. We'd like to preference the
3783 user symbol over reserved system symbols. Sort on leading
3784 underscores. */
3785 n1 = h1->root.root.string;
3786 n2 = h2->root.root.string;
3787 while (*n1 == *n2)
3788 {
3789 if (*n1 == 0)
3790 break;
3791 ++n1;
3792 ++n2;
3793 }
3794 if (*n1 == '_')
3795 return -1;
3796 if (*n2 == '_')
3797 return 1;
3798
3799 /* Final sort on name selects user symbols like '_u' over reserved
3800 system symbols like '_Z' and also will avoid qsort instability. */
3801 return *n1 - *n2;
3802 }
3803
3804 /* This function is used to adjust offsets into .dynstr for
3805 dynamic symbols. This is called via elf_link_hash_traverse. */
3806
3807 static bool
3808 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3809 {
3810 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3811
3812 if (h->dynindx != -1)
3813 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3814 return true;
3815 }
3816
3817 /* Assign string offsets in .dynstr, update all structures referencing
3818 them. */
3819
3820 static bool
3821 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3822 {
3823 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3824 struct elf_link_local_dynamic_entry *entry;
3825 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3826 bfd *dynobj = hash_table->dynobj;
3827 asection *sdyn;
3828 bfd_size_type size;
3829 const struct elf_backend_data *bed;
3830 bfd_byte *extdyn;
3831
3832 _bfd_elf_strtab_finalize (dynstr);
3833 size = _bfd_elf_strtab_size (dynstr);
3834
3835 /* Allow the linker to examine the dynsymtab now it's fully populated. */
3836
3837 if (info->callbacks->examine_strtab)
3838 info->callbacks->examine_strtab (dynstr);
3839
3840 bed = get_elf_backend_data (dynobj);
3841 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3842 BFD_ASSERT (sdyn != NULL);
3843
3844 /* Update all .dynamic entries referencing .dynstr strings. */
3845 for (extdyn = sdyn->contents;
3846 extdyn < sdyn->contents + sdyn->size;
3847 extdyn += bed->s->sizeof_dyn)
3848 {
3849 Elf_Internal_Dyn dyn;
3850
3851 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3852 switch (dyn.d_tag)
3853 {
3854 case DT_STRSZ:
3855 dyn.d_un.d_val = size;
3856 break;
3857 case DT_NEEDED:
3858 case DT_SONAME:
3859 case DT_RPATH:
3860 case DT_RUNPATH:
3861 case DT_FILTER:
3862 case DT_AUXILIARY:
3863 case DT_AUDIT:
3864 case DT_DEPAUDIT:
3865 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3866 break;
3867 default:
3868 continue;
3869 }
3870 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3871 }
3872
3873 /* Now update local dynamic symbols. */
3874 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3875 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3876 entry->isym.st_name);
3877
3878 /* And the rest of dynamic symbols. */
3879 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3880
3881 /* Adjust version definitions. */
3882 if (elf_tdata (output_bfd)->cverdefs)
3883 {
3884 asection *s;
3885 bfd_byte *p;
3886 size_t i;
3887 Elf_Internal_Verdef def;
3888 Elf_Internal_Verdaux defaux;
3889
3890 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3891 p = s->contents;
3892 do
3893 {
3894 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3895 &def);
3896 p += sizeof (Elf_External_Verdef);
3897 if (def.vd_aux != sizeof (Elf_External_Verdef))
3898 continue;
3899 for (i = 0; i < def.vd_cnt; ++i)
3900 {
3901 _bfd_elf_swap_verdaux_in (output_bfd,
3902 (Elf_External_Verdaux *) p, &defaux);
3903 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3904 defaux.vda_name);
3905 _bfd_elf_swap_verdaux_out (output_bfd,
3906 &defaux, (Elf_External_Verdaux *) p);
3907 p += sizeof (Elf_External_Verdaux);
3908 }
3909 }
3910 while (def.vd_next);
3911 }
3912
3913 /* Adjust version references. */
3914 if (elf_tdata (output_bfd)->verref)
3915 {
3916 asection *s;
3917 bfd_byte *p;
3918 size_t i;
3919 Elf_Internal_Verneed need;
3920 Elf_Internal_Vernaux needaux;
3921
3922 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3923 p = s->contents;
3924 do
3925 {
3926 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3927 &need);
3928 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3929 _bfd_elf_swap_verneed_out (output_bfd, &need,
3930 (Elf_External_Verneed *) p);
3931 p += sizeof (Elf_External_Verneed);
3932 for (i = 0; i < need.vn_cnt; ++i)
3933 {
3934 _bfd_elf_swap_vernaux_in (output_bfd,
3935 (Elf_External_Vernaux *) p, &needaux);
3936 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3937 needaux.vna_name);
3938 _bfd_elf_swap_vernaux_out (output_bfd,
3939 &needaux,
3940 (Elf_External_Vernaux *) p);
3941 p += sizeof (Elf_External_Vernaux);
3942 }
3943 }
3944 while (need.vn_next);
3945 }
3946
3947 return true;
3948 }
3949 \f
3950 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3951 The default is to only match when the INPUT and OUTPUT are exactly
3952 the same target. */
3953
3954 bool
3955 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3956 const bfd_target *output)
3957 {
3958 return input == output;
3959 }
3960
3961 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3962 This version is used when different targets for the same architecture
3963 are virtually identical. */
3964
3965 bool
3966 _bfd_elf_relocs_compatible (const bfd_target *input,
3967 const bfd_target *output)
3968 {
3969 const struct elf_backend_data *obed, *ibed;
3970
3971 if (input == output)
3972 return true;
3973
3974 ibed = xvec_get_elf_backend_data (input);
3975 obed = xvec_get_elf_backend_data (output);
3976
3977 if (ibed->arch != obed->arch)
3978 return false;
3979
3980 /* If both backends are using this function, deem them compatible. */
3981 return ibed->relocs_compatible == obed->relocs_compatible;
3982 }
3983
3984 /* Make a special call to the linker "notice" function to tell it that
3985 we are about to handle an as-needed lib, or have finished
3986 processing the lib. */
3987
3988 bool
3989 _bfd_elf_notice_as_needed (bfd *ibfd,
3990 struct bfd_link_info *info,
3991 enum notice_asneeded_action act)
3992 {
3993 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
3994 }
3995
3996 /* Check relocations an ELF object file. */
3997
3998 bool
3999 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
4000 {
4001 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4002 struct elf_link_hash_table *htab = elf_hash_table (info);
4003
4004 /* If this object is the same format as the output object, and it is
4005 not a shared library, then let the backend look through the
4006 relocs.
4007
4008 This is required to build global offset table entries and to
4009 arrange for dynamic relocs. It is not required for the
4010 particular common case of linking non PIC code, even when linking
4011 against shared libraries, but unfortunately there is no way of
4012 knowing whether an object file has been compiled PIC or not.
4013 Looking through the relocs is not particularly time consuming.
4014 The problem is that we must either (1) keep the relocs in memory,
4015 which causes the linker to require additional runtime memory or
4016 (2) read the relocs twice from the input file, which wastes time.
4017 This would be a good case for using mmap.
4018
4019 I have no idea how to handle linking PIC code into a file of a
4020 different format. It probably can't be done. */
4021 if ((abfd->flags & DYNAMIC) == 0
4022 && is_elf_hash_table (&htab->root)
4023 && bed->check_relocs != NULL
4024 && elf_object_id (abfd) == elf_hash_table_id (htab)
4025 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4026 {
4027 asection *o;
4028
4029 for (o = abfd->sections; o != NULL; o = o->next)
4030 {
4031 Elf_Internal_Rela *internal_relocs;
4032 bool ok;
4033
4034 /* Don't check relocations in excluded sections. Don't do
4035 anything special with non-loaded, non-alloced sections.
4036 In particular, any relocs in such sections should not
4037 affect GOT and PLT reference counting (ie. we don't
4038 allow them to create GOT or PLT entries), there's no
4039 possibility or desire to optimize TLS relocs, and
4040 there's not much point in propagating relocs to shared
4041 libs that the dynamic linker won't relocate. */
4042 if ((o->flags & SEC_ALLOC) == 0
4043 || (o->flags & SEC_RELOC) == 0
4044 || (o->flags & SEC_EXCLUDE) != 0
4045 || o->reloc_count == 0
4046 || ((info->strip == strip_all || info->strip == strip_debugger)
4047 && (o->flags & SEC_DEBUGGING) != 0)
4048 || bfd_is_abs_section (o->output_section))
4049 continue;
4050
4051 internal_relocs = _bfd_elf_link_info_read_relocs (abfd, info,
4052 o, NULL,
4053 NULL,
4054 _bfd_link_keep_memory (info));
4055 if (internal_relocs == NULL)
4056 return false;
4057
4058 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
4059
4060 if (elf_section_data (o)->relocs != internal_relocs)
4061 free (internal_relocs);
4062
4063 if (! ok)
4064 return false;
4065 }
4066 }
4067
4068 return true;
4069 }
4070
4071 /* Add symbols from an ELF object file to the linker hash table. */
4072
4073 static bool
4074 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
4075 {
4076 Elf_Internal_Ehdr *ehdr;
4077 Elf_Internal_Shdr *hdr;
4078 size_t symcount;
4079 size_t extsymcount;
4080 size_t extsymoff;
4081 struct elf_link_hash_entry **sym_hash;
4082 bool dynamic;
4083 Elf_External_Versym *extversym = NULL;
4084 Elf_External_Versym *extversym_end = NULL;
4085 Elf_External_Versym *ever;
4086 struct elf_link_hash_entry *weaks;
4087 struct elf_link_hash_entry **nondeflt_vers = NULL;
4088 size_t nondeflt_vers_cnt = 0;
4089 Elf_Internal_Sym *isymbuf = NULL;
4090 Elf_Internal_Sym *isym;
4091 Elf_Internal_Sym *isymend;
4092 const struct elf_backend_data *bed;
4093 bool add_needed;
4094 struct elf_link_hash_table *htab;
4095 void *alloc_mark = NULL;
4096 struct bfd_hash_entry **old_table = NULL;
4097 unsigned int old_size = 0;
4098 unsigned int old_count = 0;
4099 void *old_tab = NULL;
4100 void *old_ent;
4101 struct bfd_link_hash_entry *old_undefs = NULL;
4102 struct bfd_link_hash_entry *old_undefs_tail = NULL;
4103 void *old_strtab = NULL;
4104 size_t tabsize = 0;
4105 asection *s;
4106 bool just_syms;
4107
4108 htab = elf_hash_table (info);
4109 bed = get_elf_backend_data (abfd);
4110
4111 if ((abfd->flags & DYNAMIC) == 0)
4112 dynamic = false;
4113 else
4114 {
4115 dynamic = true;
4116
4117 /* You can't use -r against a dynamic object. Also, there's no
4118 hope of using a dynamic object which does not exactly match
4119 the format of the output file. */
4120 if (bfd_link_relocatable (info)
4121 || !is_elf_hash_table (&htab->root)
4122 || info->output_bfd->xvec != abfd->xvec)
4123 {
4124 if (bfd_link_relocatable (info))
4125 bfd_set_error (bfd_error_invalid_operation);
4126 else
4127 bfd_set_error (bfd_error_wrong_format);
4128 goto error_return;
4129 }
4130 }
4131
4132 ehdr = elf_elfheader (abfd);
4133 if (info->warn_alternate_em
4134 && bed->elf_machine_code != ehdr->e_machine
4135 && ((bed->elf_machine_alt1 != 0
4136 && ehdr->e_machine == bed->elf_machine_alt1)
4137 || (bed->elf_machine_alt2 != 0
4138 && ehdr->e_machine == bed->elf_machine_alt2)))
4139 _bfd_error_handler
4140 /* xgettext:c-format */
4141 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
4142 ehdr->e_machine, abfd, bed->elf_machine_code);
4143
4144 /* As a GNU extension, any input sections which are named
4145 .gnu.warning.SYMBOL are treated as warning symbols for the given
4146 symbol. This differs from .gnu.warning sections, which generate
4147 warnings when they are included in an output file. */
4148 /* PR 12761: Also generate this warning when building shared libraries. */
4149 for (s = abfd->sections; s != NULL; s = s->next)
4150 {
4151 const char *name;
4152
4153 name = bfd_section_name (s);
4154 if (startswith (name, ".gnu.warning."))
4155 {
4156 char *msg;
4157 bfd_size_type sz;
4158
4159 name += sizeof ".gnu.warning." - 1;
4160
4161 /* If this is a shared object, then look up the symbol
4162 in the hash table. If it is there, and it is already
4163 been defined, then we will not be using the entry
4164 from this shared object, so we don't need to warn.
4165 FIXME: If we see the definition in a regular object
4166 later on, we will warn, but we shouldn't. The only
4167 fix is to keep track of what warnings we are supposed
4168 to emit, and then handle them all at the end of the
4169 link. */
4170 if (dynamic)
4171 {
4172 struct elf_link_hash_entry *h;
4173
4174 h = elf_link_hash_lookup (htab, name, false, false, true);
4175
4176 /* FIXME: What about bfd_link_hash_common? */
4177 if (h != NULL
4178 && (h->root.type == bfd_link_hash_defined
4179 || h->root.type == bfd_link_hash_defweak))
4180 continue;
4181 }
4182
4183 sz = s->size;
4184 msg = (char *) bfd_alloc (abfd, sz + 1);
4185 if (msg == NULL)
4186 goto error_return;
4187
4188 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
4189 goto error_return;
4190
4191 msg[sz] = '\0';
4192
4193 if (! (_bfd_generic_link_add_one_symbol
4194 (info, abfd, name, BSF_WARNING, s, 0, msg,
4195 false, bed->collect, NULL)))
4196 goto error_return;
4197
4198 if (bfd_link_executable (info))
4199 {
4200 /* Clobber the section size so that the warning does
4201 not get copied into the output file. */
4202 s->size = 0;
4203
4204 /* Also set SEC_EXCLUDE, so that symbols defined in
4205 the warning section don't get copied to the output. */
4206 s->flags |= SEC_EXCLUDE;
4207 }
4208 }
4209 }
4210
4211 just_syms = ((s = abfd->sections) != NULL
4212 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
4213
4214 add_needed = true;
4215 if (! dynamic)
4216 {
4217 /* If we are creating a shared library, create all the dynamic
4218 sections immediately. We need to attach them to something,
4219 so we attach them to this BFD, provided it is the right
4220 format and is not from ld --just-symbols. Always create the
4221 dynamic sections for -E/--dynamic-list. FIXME: If there
4222 are no input BFD's of the same format as the output, we can't
4223 make a shared library. */
4224 if (!just_syms
4225 && (bfd_link_pic (info)
4226 || (!bfd_link_relocatable (info)
4227 && info->nointerp
4228 && (info->export_dynamic || info->dynamic)))
4229 && is_elf_hash_table (&htab->root)
4230 && info->output_bfd->xvec == abfd->xvec
4231 && !htab->dynamic_sections_created)
4232 {
4233 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4234 goto error_return;
4235 }
4236 }
4237 else if (!is_elf_hash_table (&htab->root))
4238 goto error_return;
4239 else
4240 {
4241 const char *soname = NULL;
4242 char *audit = NULL;
4243 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
4244 const Elf_Internal_Phdr *phdr;
4245 struct elf_link_loaded_list *loaded_lib;
4246
4247 /* ld --just-symbols and dynamic objects don't mix very well.
4248 ld shouldn't allow it. */
4249 if (just_syms)
4250 abort ();
4251
4252 /* If this dynamic lib was specified on the command line with
4253 --as-needed in effect, then we don't want to add a DT_NEEDED
4254 tag unless the lib is actually used. Similary for libs brought
4255 in by another lib's DT_NEEDED. When --no-add-needed is used
4256 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4257 any dynamic library in DT_NEEDED tags in the dynamic lib at
4258 all. */
4259 add_needed = (elf_dyn_lib_class (abfd)
4260 & (DYN_AS_NEEDED | DYN_DT_NEEDED
4261 | DYN_NO_NEEDED)) == 0;
4262
4263 s = bfd_get_section_by_name (abfd, ".dynamic");
4264 if (s != NULL)
4265 {
4266 bfd_byte *dynbuf;
4267 bfd_byte *extdyn;
4268 unsigned int elfsec;
4269 unsigned long shlink;
4270
4271 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
4272 {
4273 error_free_dyn:
4274 free (dynbuf);
4275 goto error_return;
4276 }
4277
4278 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
4279 if (elfsec == SHN_BAD)
4280 goto error_free_dyn;
4281 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4282
4283 for (extdyn = dynbuf;
4284 extdyn <= dynbuf + s->size - bed->s->sizeof_dyn;
4285 extdyn += bed->s->sizeof_dyn)
4286 {
4287 Elf_Internal_Dyn dyn;
4288
4289 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4290 if (dyn.d_tag == DT_SONAME)
4291 {
4292 unsigned int tagv = dyn.d_un.d_val;
4293 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4294 if (soname == NULL)
4295 goto error_free_dyn;
4296 }
4297 if (dyn.d_tag == DT_NEEDED)
4298 {
4299 struct bfd_link_needed_list *n, **pn;
4300 char *fnm, *anm;
4301 unsigned int tagv = dyn.d_un.d_val;
4302 size_t amt = sizeof (struct bfd_link_needed_list);
4303
4304 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4305 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4306 if (n == NULL || fnm == NULL)
4307 goto error_free_dyn;
4308 amt = strlen (fnm) + 1;
4309 anm = (char *) bfd_alloc (abfd, amt);
4310 if (anm == NULL)
4311 goto error_free_dyn;
4312 memcpy (anm, fnm, amt);
4313 n->name = anm;
4314 n->by = abfd;
4315 n->next = NULL;
4316 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4317 ;
4318 *pn = n;
4319 }
4320 if (dyn.d_tag == DT_RUNPATH)
4321 {
4322 struct bfd_link_needed_list *n, **pn;
4323 char *fnm, *anm;
4324 unsigned int tagv = dyn.d_un.d_val;
4325 size_t amt = sizeof (struct bfd_link_needed_list);
4326
4327 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4328 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4329 if (n == NULL || fnm == NULL)
4330 goto error_free_dyn;
4331 amt = strlen (fnm) + 1;
4332 anm = (char *) bfd_alloc (abfd, amt);
4333 if (anm == NULL)
4334 goto error_free_dyn;
4335 memcpy (anm, fnm, amt);
4336 n->name = anm;
4337 n->by = abfd;
4338 n->next = NULL;
4339 for (pn = & runpath;
4340 *pn != NULL;
4341 pn = &(*pn)->next)
4342 ;
4343 *pn = n;
4344 }
4345 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4346 if (!runpath && dyn.d_tag == DT_RPATH)
4347 {
4348 struct bfd_link_needed_list *n, **pn;
4349 char *fnm, *anm;
4350 unsigned int tagv = dyn.d_un.d_val;
4351 size_t amt = sizeof (struct bfd_link_needed_list);
4352
4353 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4354 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4355 if (n == NULL || fnm == NULL)
4356 goto error_free_dyn;
4357 amt = strlen (fnm) + 1;
4358 anm = (char *) bfd_alloc (abfd, amt);
4359 if (anm == NULL)
4360 goto error_free_dyn;
4361 memcpy (anm, fnm, amt);
4362 n->name = anm;
4363 n->by = abfd;
4364 n->next = NULL;
4365 for (pn = & rpath;
4366 *pn != NULL;
4367 pn = &(*pn)->next)
4368 ;
4369 *pn = n;
4370 }
4371 if (dyn.d_tag == DT_AUDIT)
4372 {
4373 unsigned int tagv = dyn.d_un.d_val;
4374 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4375 }
4376 if (dyn.d_tag == DT_FLAGS_1)
4377 elf_tdata (abfd)->is_pie = (dyn.d_un.d_val & DF_1_PIE) != 0;
4378 }
4379
4380 free (dynbuf);
4381 }
4382
4383 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4384 frees all more recently bfd_alloc'd blocks as well. */
4385 if (runpath)
4386 rpath = runpath;
4387
4388 if (rpath)
4389 {
4390 struct bfd_link_needed_list **pn;
4391 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4392 ;
4393 *pn = rpath;
4394 }
4395
4396 /* If we have a PT_GNU_RELRO program header, mark as read-only
4397 all sections contained fully therein. This makes relro
4398 shared library sections appear as they will at run-time. */
4399 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4400 while (phdr-- > elf_tdata (abfd)->phdr)
4401 if (phdr->p_type == PT_GNU_RELRO)
4402 {
4403 for (s = abfd->sections; s != NULL; s = s->next)
4404 {
4405 unsigned int opb = bfd_octets_per_byte (abfd, s);
4406
4407 if ((s->flags & SEC_ALLOC) != 0
4408 && s->vma * opb >= phdr->p_vaddr
4409 && s->vma * opb + s->size <= phdr->p_vaddr + phdr->p_memsz)
4410 s->flags |= SEC_READONLY;
4411 }
4412 break;
4413 }
4414
4415 /* We do not want to include any of the sections in a dynamic
4416 object in the output file. We hack by simply clobbering the
4417 list of sections in the BFD. This could be handled more
4418 cleanly by, say, a new section flag; the existing
4419 SEC_NEVER_LOAD flag is not the one we want, because that one
4420 still implies that the section takes up space in the output
4421 file. */
4422 bfd_section_list_clear (abfd);
4423
4424 /* Find the name to use in a DT_NEEDED entry that refers to this
4425 object. If the object has a DT_SONAME entry, we use it.
4426 Otherwise, if the generic linker stuck something in
4427 elf_dt_name, we use that. Otherwise, we just use the file
4428 name. */
4429 if (soname == NULL || *soname == '\0')
4430 {
4431 soname = elf_dt_name (abfd);
4432 if (soname == NULL || *soname == '\0')
4433 soname = bfd_get_filename (abfd);
4434 }
4435
4436 /* Save the SONAME because sometimes the linker emulation code
4437 will need to know it. */
4438 elf_dt_name (abfd) = soname;
4439
4440 /* If we have already included this dynamic object in the
4441 link, just ignore it. There is no reason to include a
4442 particular dynamic object more than once. */
4443 for (loaded_lib = htab->dyn_loaded;
4444 loaded_lib != NULL;
4445 loaded_lib = loaded_lib->next)
4446 {
4447 if (strcmp (elf_dt_name (loaded_lib->abfd), soname) == 0)
4448 return true;
4449 }
4450
4451 /* Create dynamic sections for backends that require that be done
4452 before setup_gnu_properties. */
4453 if (add_needed
4454 && !_bfd_elf_link_create_dynamic_sections (abfd, info))
4455 return false;
4456
4457 /* Save the DT_AUDIT entry for the linker emulation code. */
4458 elf_dt_audit (abfd) = audit;
4459 }
4460
4461 /* If this is a dynamic object, we always link against the .dynsym
4462 symbol table, not the .symtab symbol table. The dynamic linker
4463 will only see the .dynsym symbol table, so there is no reason to
4464 look at .symtab for a dynamic object. */
4465
4466 if (! dynamic || elf_dynsymtab (abfd) == 0)
4467 hdr = &elf_tdata (abfd)->symtab_hdr;
4468 else
4469 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4470
4471 symcount = hdr->sh_size / bed->s->sizeof_sym;
4472
4473 /* The sh_info field of the symtab header tells us where the
4474 external symbols start. We don't care about the local symbols at
4475 this point. */
4476 if (elf_bad_symtab (abfd))
4477 {
4478 extsymcount = symcount;
4479 extsymoff = 0;
4480 }
4481 else
4482 {
4483 extsymcount = symcount - hdr->sh_info;
4484 extsymoff = hdr->sh_info;
4485 }
4486
4487 sym_hash = elf_sym_hashes (abfd);
4488 if (extsymcount != 0)
4489 {
4490 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4491 NULL, NULL, NULL);
4492 if (isymbuf == NULL)
4493 goto error_return;
4494
4495 if (sym_hash == NULL)
4496 {
4497 /* We store a pointer to the hash table entry for each
4498 external symbol. */
4499 size_t amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4500 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4501 if (sym_hash == NULL)
4502 goto error_free_sym;
4503 elf_sym_hashes (abfd) = sym_hash;
4504 }
4505 }
4506
4507 if (dynamic)
4508 {
4509 /* Read in any version definitions. */
4510 if (!_bfd_elf_slurp_version_tables (abfd,
4511 info->default_imported_symver))
4512 goto error_free_sym;
4513
4514 /* Read in the symbol versions, but don't bother to convert them
4515 to internal format. */
4516 if (elf_dynversym (abfd) != 0)
4517 {
4518 Elf_Internal_Shdr *versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4519 bfd_size_type amt = versymhdr->sh_size;
4520
4521 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0)
4522 goto error_free_sym;
4523 extversym = (Elf_External_Versym *)
4524 _bfd_malloc_and_read (abfd, amt, amt);
4525 if (extversym == NULL)
4526 goto error_free_sym;
4527 extversym_end = extversym + amt / sizeof (*extversym);
4528 }
4529 }
4530
4531 /* If we are loading an as-needed shared lib, save the symbol table
4532 state before we start adding symbols. If the lib turns out
4533 to be unneeded, restore the state. */
4534 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4535 {
4536 unsigned int i;
4537 size_t entsize;
4538
4539 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4540 {
4541 struct bfd_hash_entry *p;
4542 struct elf_link_hash_entry *h;
4543
4544 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4545 {
4546 h = (struct elf_link_hash_entry *) p;
4547 entsize += htab->root.table.entsize;
4548 if (h->root.type == bfd_link_hash_warning)
4549 {
4550 entsize += htab->root.table.entsize;
4551 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4552 }
4553 if (h->root.type == bfd_link_hash_common)
4554 entsize += sizeof (*h->root.u.c.p);
4555 }
4556 }
4557
4558 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4559 old_tab = bfd_malloc (tabsize + entsize);
4560 if (old_tab == NULL)
4561 goto error_free_vers;
4562
4563 /* Remember the current objalloc pointer, so that all mem for
4564 symbols added can later be reclaimed. */
4565 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4566 if (alloc_mark == NULL)
4567 goto error_free_vers;
4568
4569 /* Make a special call to the linker "notice" function to
4570 tell it that we are about to handle an as-needed lib. */
4571 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4572 goto error_free_vers;
4573
4574 /* Clone the symbol table. Remember some pointers into the
4575 symbol table, and dynamic symbol count. */
4576 old_ent = (char *) old_tab + tabsize;
4577 memcpy (old_tab, htab->root.table.table, tabsize);
4578 old_undefs = htab->root.undefs;
4579 old_undefs_tail = htab->root.undefs_tail;
4580 old_table = htab->root.table.table;
4581 old_size = htab->root.table.size;
4582 old_count = htab->root.table.count;
4583 old_strtab = NULL;
4584 if (htab->dynstr != NULL)
4585 {
4586 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4587 if (old_strtab == NULL)
4588 goto error_free_vers;
4589 }
4590
4591 for (i = 0; i < htab->root.table.size; i++)
4592 {
4593 struct bfd_hash_entry *p;
4594 struct elf_link_hash_entry *h;
4595
4596 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4597 {
4598 h = (struct elf_link_hash_entry *) p;
4599 memcpy (old_ent, h, htab->root.table.entsize);
4600 old_ent = (char *) old_ent + htab->root.table.entsize;
4601 if (h->root.type == bfd_link_hash_warning)
4602 {
4603 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4604 memcpy (old_ent, h, htab->root.table.entsize);
4605 old_ent = (char *) old_ent + htab->root.table.entsize;
4606 }
4607 if (h->root.type == bfd_link_hash_common)
4608 {
4609 memcpy (old_ent, h->root.u.c.p, sizeof (*h->root.u.c.p));
4610 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
4611 }
4612 }
4613 }
4614 }
4615
4616 weaks = NULL;
4617 if (extversym == NULL)
4618 ever = NULL;
4619 else if (extversym + extsymoff < extversym_end)
4620 ever = extversym + extsymoff;
4621 else
4622 {
4623 /* xgettext:c-format */
4624 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4625 abfd, (long) extsymoff,
4626 (long) (extversym_end - extversym) / sizeof (* extversym));
4627 bfd_set_error (bfd_error_bad_value);
4628 goto error_free_vers;
4629 }
4630
4631 if (!bfd_link_relocatable (info)
4632 && abfd->lto_slim_object)
4633 {
4634 _bfd_error_handler
4635 (_("%pB: plugin needed to handle lto object"), abfd);
4636 }
4637
4638 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4639 isym < isymend;
4640 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4641 {
4642 int bind;
4643 bfd_vma value;
4644 asection *sec, *new_sec;
4645 flagword flags;
4646 const char *name;
4647 struct elf_link_hash_entry *h;
4648 struct elf_link_hash_entry *hi;
4649 bool definition;
4650 bool size_change_ok;
4651 bool type_change_ok;
4652 bool new_weak;
4653 bool old_weak;
4654 bfd *override;
4655 bool common;
4656 bool discarded;
4657 unsigned int old_alignment;
4658 unsigned int shindex;
4659 bfd *old_bfd;
4660 bool matched;
4661
4662 override = NULL;
4663
4664 flags = BSF_NO_FLAGS;
4665 sec = NULL;
4666 value = isym->st_value;
4667 common = bed->common_definition (isym);
4668 if (common && info->inhibit_common_definition)
4669 {
4670 /* Treat common symbol as undefined for --no-define-common. */
4671 isym->st_shndx = SHN_UNDEF;
4672 common = false;
4673 }
4674 discarded = false;
4675
4676 bind = ELF_ST_BIND (isym->st_info);
4677 switch (bind)
4678 {
4679 case STB_LOCAL:
4680 /* This should be impossible, since ELF requires that all
4681 global symbols follow all local symbols, and that sh_info
4682 point to the first global symbol. Unfortunately, Irix 5
4683 screws this up. */
4684 if (elf_bad_symtab (abfd))
4685 continue;
4686
4687 /* If we aren't prepared to handle locals within the globals
4688 then we'll likely segfault on a NULL symbol hash if the
4689 symbol is ever referenced in relocations. */
4690 shindex = elf_elfheader (abfd)->e_shstrndx;
4691 name = bfd_elf_string_from_elf_section (abfd, shindex, hdr->sh_name);
4692 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4693 " (>= sh_info of %lu)"),
4694 abfd, name, (long) (isym - isymbuf + extsymoff),
4695 (long) extsymoff);
4696
4697 /* Dynamic object relocations are not processed by ld, so
4698 ld won't run into the problem mentioned above. */
4699 if (dynamic)
4700 continue;
4701 bfd_set_error (bfd_error_bad_value);
4702 goto error_free_vers;
4703
4704 case STB_GLOBAL:
4705 if (isym->st_shndx != SHN_UNDEF && !common)
4706 flags = BSF_GLOBAL;
4707 break;
4708
4709 case STB_WEAK:
4710 flags = BSF_WEAK;
4711 break;
4712
4713 case STB_GNU_UNIQUE:
4714 flags = BSF_GNU_UNIQUE;
4715 break;
4716
4717 default:
4718 /* Leave it up to the processor backend. */
4719 break;
4720 }
4721
4722 if (isym->st_shndx == SHN_UNDEF)
4723 sec = bfd_und_section_ptr;
4724 else if (isym->st_shndx == SHN_ABS)
4725 sec = bfd_abs_section_ptr;
4726 else if (isym->st_shndx == SHN_COMMON)
4727 {
4728 sec = bfd_com_section_ptr;
4729 /* What ELF calls the size we call the value. What ELF
4730 calls the value we call the alignment. */
4731 value = isym->st_size;
4732 }
4733 else
4734 {
4735 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4736 if (sec == NULL)
4737 sec = bfd_abs_section_ptr;
4738 else if (discarded_section (sec))
4739 {
4740 /* Symbols from discarded section are undefined. We keep
4741 its visibility. */
4742 sec = bfd_und_section_ptr;
4743 discarded = true;
4744 isym->st_shndx = SHN_UNDEF;
4745 }
4746 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4747 value -= sec->vma;
4748 }
4749
4750 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4751 isym->st_name);
4752 if (name == NULL)
4753 goto error_free_vers;
4754
4755 if (isym->st_shndx == SHN_COMMON
4756 && (abfd->flags & BFD_PLUGIN) != 0)
4757 {
4758 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4759
4760 if (xc == NULL)
4761 {
4762 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4763 | SEC_EXCLUDE);
4764 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4765 if (xc == NULL)
4766 goto error_free_vers;
4767 }
4768 sec = xc;
4769 }
4770 else if (isym->st_shndx == SHN_COMMON
4771 && ELF_ST_TYPE (isym->st_info) == STT_TLS
4772 && !bfd_link_relocatable (info))
4773 {
4774 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4775
4776 if (tcomm == NULL)
4777 {
4778 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4779 | SEC_LINKER_CREATED);
4780 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4781 if (tcomm == NULL)
4782 goto error_free_vers;
4783 }
4784 sec = tcomm;
4785 }
4786 else if (bed->elf_add_symbol_hook)
4787 {
4788 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4789 &sec, &value))
4790 goto error_free_vers;
4791
4792 /* The hook function sets the name to NULL if this symbol
4793 should be skipped for some reason. */
4794 if (name == NULL)
4795 continue;
4796 }
4797
4798 /* Sanity check that all possibilities were handled. */
4799 if (sec == NULL)
4800 abort ();
4801
4802 /* Silently discard TLS symbols from --just-syms. There's
4803 no way to combine a static TLS block with a new TLS block
4804 for this executable. */
4805 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4806 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4807 continue;
4808
4809 if (bfd_is_und_section (sec)
4810 || bfd_is_com_section (sec))
4811 definition = false;
4812 else
4813 definition = true;
4814
4815 size_change_ok = false;
4816 type_change_ok = bed->type_change_ok;
4817 old_weak = false;
4818 matched = false;
4819 old_alignment = 0;
4820 old_bfd = NULL;
4821 new_sec = sec;
4822
4823 if (is_elf_hash_table (&htab->root))
4824 {
4825 Elf_Internal_Versym iver;
4826 unsigned int vernum = 0;
4827 bool skip;
4828
4829 if (ever == NULL)
4830 {
4831 if (info->default_imported_symver)
4832 /* Use the default symbol version created earlier. */
4833 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4834 else
4835 iver.vs_vers = 0;
4836 }
4837 else if (ever >= extversym_end)
4838 {
4839 /* xgettext:c-format */
4840 _bfd_error_handler (_("%pB: not enough version information"),
4841 abfd);
4842 bfd_set_error (bfd_error_bad_value);
4843 goto error_free_vers;
4844 }
4845 else
4846 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4847
4848 vernum = iver.vs_vers & VERSYM_VERSION;
4849
4850 /* If this is a hidden symbol, or if it is not version
4851 1, we append the version name to the symbol name.
4852 However, we do not modify a non-hidden absolute symbol
4853 if it is not a function, because it might be the version
4854 symbol itself. FIXME: What if it isn't? */
4855 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4856 || (vernum > 1
4857 && (!bfd_is_abs_section (sec)
4858 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4859 {
4860 const char *verstr;
4861 size_t namelen, verlen, newlen;
4862 char *newname, *p;
4863
4864 if (isym->st_shndx != SHN_UNDEF)
4865 {
4866 if (vernum > elf_tdata (abfd)->cverdefs)
4867 verstr = NULL;
4868 else if (vernum > 1)
4869 verstr =
4870 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4871 else
4872 verstr = "";
4873
4874 if (verstr == NULL)
4875 {
4876 _bfd_error_handler
4877 /* xgettext:c-format */
4878 (_("%pB: %s: invalid version %u (max %d)"),
4879 abfd, name, vernum,
4880 elf_tdata (abfd)->cverdefs);
4881 bfd_set_error (bfd_error_bad_value);
4882 goto error_free_vers;
4883 }
4884 }
4885 else
4886 {
4887 /* We cannot simply test for the number of
4888 entries in the VERNEED section since the
4889 numbers for the needed versions do not start
4890 at 0. */
4891 Elf_Internal_Verneed *t;
4892
4893 verstr = NULL;
4894 for (t = elf_tdata (abfd)->verref;
4895 t != NULL;
4896 t = t->vn_nextref)
4897 {
4898 Elf_Internal_Vernaux *a;
4899
4900 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4901 {
4902 if (a->vna_other == vernum)
4903 {
4904 verstr = a->vna_nodename;
4905 break;
4906 }
4907 }
4908 if (a != NULL)
4909 break;
4910 }
4911 if (verstr == NULL)
4912 {
4913 _bfd_error_handler
4914 /* xgettext:c-format */
4915 (_("%pB: %s: invalid needed version %d"),
4916 abfd, name, vernum);
4917 bfd_set_error (bfd_error_bad_value);
4918 goto error_free_vers;
4919 }
4920 }
4921
4922 namelen = strlen (name);
4923 verlen = strlen (verstr);
4924 newlen = namelen + verlen + 2;
4925 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4926 && isym->st_shndx != SHN_UNDEF)
4927 ++newlen;
4928
4929 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4930 if (newname == NULL)
4931 goto error_free_vers;
4932 memcpy (newname, name, namelen);
4933 p = newname + namelen;
4934 *p++ = ELF_VER_CHR;
4935 /* If this is a defined non-hidden version symbol,
4936 we add another @ to the name. This indicates the
4937 default version of the symbol. */
4938 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4939 && isym->st_shndx != SHN_UNDEF)
4940 *p++ = ELF_VER_CHR;
4941 memcpy (p, verstr, verlen + 1);
4942
4943 name = newname;
4944 }
4945
4946 /* If this symbol has default visibility and the user has
4947 requested we not re-export it, then mark it as hidden. */
4948 if (!bfd_is_und_section (sec)
4949 && !dynamic
4950 && abfd->no_export
4951 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4952 isym->st_other = (STV_HIDDEN
4953 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4954
4955 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4956 sym_hash, &old_bfd, &old_weak,
4957 &old_alignment, &skip, &override,
4958 &type_change_ok, &size_change_ok,
4959 &matched))
4960 goto error_free_vers;
4961
4962 if (skip)
4963 continue;
4964
4965 /* Override a definition only if the new symbol matches the
4966 existing one. */
4967 if (override && matched)
4968 definition = false;
4969
4970 h = *sym_hash;
4971 while (h->root.type == bfd_link_hash_indirect
4972 || h->root.type == bfd_link_hash_warning)
4973 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4974
4975 if (elf_tdata (abfd)->verdef != NULL
4976 && vernum > 1
4977 && definition)
4978 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4979 }
4980
4981 if (! (_bfd_generic_link_add_one_symbol
4982 (info, override ? override : abfd, name, flags, sec, value,
4983 NULL, false, bed->collect,
4984 (struct bfd_link_hash_entry **) sym_hash)))
4985 goto error_free_vers;
4986
4987 h = *sym_hash;
4988 /* We need to make sure that indirect symbol dynamic flags are
4989 updated. */
4990 hi = h;
4991 while (h->root.type == bfd_link_hash_indirect
4992 || h->root.type == bfd_link_hash_warning)
4993 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4994
4995 *sym_hash = h;
4996
4997 /* Setting the index to -3 tells elf_link_output_extsym that
4998 this symbol is defined in a discarded section. */
4999 if (discarded && is_elf_hash_table (&htab->root))
5000 h->indx = -3;
5001
5002 new_weak = (flags & BSF_WEAK) != 0;
5003 if (dynamic
5004 && definition
5005 && new_weak
5006 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
5007 && is_elf_hash_table (&htab->root)
5008 && h->u.alias == NULL)
5009 {
5010 /* Keep a list of all weak defined non function symbols from
5011 a dynamic object, using the alias field. Later in this
5012 function we will set the alias field to the correct
5013 value. We only put non-function symbols from dynamic
5014 objects on this list, because that happens to be the only
5015 time we need to know the normal symbol corresponding to a
5016 weak symbol, and the information is time consuming to
5017 figure out. If the alias field is not already NULL,
5018 then this symbol was already defined by some previous
5019 dynamic object, and we will be using that previous
5020 definition anyhow. */
5021
5022 h->u.alias = weaks;
5023 weaks = h;
5024 }
5025
5026 /* Set the alignment of a common symbol. */
5027 if ((common || bfd_is_com_section (sec))
5028 && h->root.type == bfd_link_hash_common)
5029 {
5030 unsigned int align;
5031
5032 if (common)
5033 align = bfd_log2 (isym->st_value);
5034 else
5035 {
5036 /* The new symbol is a common symbol in a shared object.
5037 We need to get the alignment from the section. */
5038 align = new_sec->alignment_power;
5039 }
5040 if (align > old_alignment)
5041 h->root.u.c.p->alignment_power = align;
5042 else
5043 h->root.u.c.p->alignment_power = old_alignment;
5044 }
5045
5046 if (is_elf_hash_table (&htab->root))
5047 {
5048 /* Set a flag in the hash table entry indicating the type of
5049 reference or definition we just found. A dynamic symbol
5050 is one which is referenced or defined by both a regular
5051 object and a shared object. */
5052 bool dynsym = false;
5053
5054 /* Plugin symbols aren't normal. Don't set def/ref flags. */
5055 if ((abfd->flags & BFD_PLUGIN) != 0)
5056 {
5057 /* Except for this flag to track nonweak references. */
5058 if (!definition
5059 && bind != STB_WEAK)
5060 h->ref_ir_nonweak = 1;
5061 }
5062 else if (!dynamic)
5063 {
5064 if (! definition)
5065 {
5066 h->ref_regular = 1;
5067 if (bind != STB_WEAK)
5068 h->ref_regular_nonweak = 1;
5069 }
5070 else
5071 {
5072 h->def_regular = 1;
5073 if (h->def_dynamic)
5074 {
5075 h->def_dynamic = 0;
5076 h->ref_dynamic = 1;
5077 }
5078 }
5079 }
5080 else
5081 {
5082 if (! definition)
5083 {
5084 h->ref_dynamic = 1;
5085 hi->ref_dynamic = 1;
5086 }
5087 else
5088 {
5089 h->def_dynamic = 1;
5090 hi->def_dynamic = 1;
5091 }
5092 }
5093
5094 /* If an indirect symbol has been forced local, don't
5095 make the real symbol dynamic. */
5096 if (h != hi && hi->forced_local)
5097 ;
5098 else if (!dynamic)
5099 {
5100 if (bfd_link_dll (info)
5101 || h->def_dynamic
5102 || h->ref_dynamic)
5103 dynsym = true;
5104 }
5105 else
5106 {
5107 if (h->def_regular
5108 || h->ref_regular
5109 || (h->is_weakalias
5110 && weakdef (h)->dynindx != -1))
5111 dynsym = true;
5112 }
5113
5114 /* Check to see if we need to add an indirect symbol for
5115 the default name. */
5116 if ((definition
5117 || (!override && h->root.type == bfd_link_hash_common))
5118 && !(hi != h
5119 && hi->versioned == versioned_hidden))
5120 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
5121 sec, value, &old_bfd, &dynsym))
5122 goto error_free_vers;
5123
5124 /* Check the alignment when a common symbol is involved. This
5125 can change when a common symbol is overridden by a normal
5126 definition or a common symbol is ignored due to the old
5127 normal definition. We need to make sure the maximum
5128 alignment is maintained. */
5129 if ((old_alignment || common)
5130 && h->root.type != bfd_link_hash_common)
5131 {
5132 unsigned int common_align;
5133 unsigned int normal_align;
5134 unsigned int symbol_align;
5135 bfd *normal_bfd;
5136 bfd *common_bfd;
5137
5138 BFD_ASSERT (h->root.type == bfd_link_hash_defined
5139 || h->root.type == bfd_link_hash_defweak);
5140
5141 symbol_align = ffs (h->root.u.def.value) - 1;
5142 if (h->root.u.def.section->owner != NULL
5143 && (h->root.u.def.section->owner->flags
5144 & (DYNAMIC | BFD_PLUGIN)) == 0)
5145 {
5146 normal_align = h->root.u.def.section->alignment_power;
5147 if (normal_align > symbol_align)
5148 normal_align = symbol_align;
5149 }
5150 else
5151 normal_align = symbol_align;
5152
5153 if (old_alignment)
5154 {
5155 common_align = old_alignment;
5156 common_bfd = old_bfd;
5157 normal_bfd = abfd;
5158 }
5159 else
5160 {
5161 common_align = bfd_log2 (isym->st_value);
5162 common_bfd = abfd;
5163 normal_bfd = old_bfd;
5164 }
5165
5166 if (normal_align < common_align)
5167 {
5168 /* PR binutils/2735 */
5169 if (normal_bfd == NULL)
5170 _bfd_error_handler
5171 /* xgettext:c-format */
5172 (_("warning: alignment %u of common symbol `%s' in %pB is"
5173 " greater than the alignment (%u) of its section %pA"),
5174 1 << common_align, name, common_bfd,
5175 1 << normal_align, h->root.u.def.section);
5176 else
5177 _bfd_error_handler
5178 /* xgettext:c-format */
5179 (_("warning: alignment %u of symbol `%s' in %pB"
5180 " is smaller than %u in %pB"),
5181 1 << normal_align, name, normal_bfd,
5182 1 << common_align, common_bfd);
5183 }
5184 }
5185
5186 /* Remember the symbol size if it isn't undefined. */
5187 if (isym->st_size != 0
5188 && isym->st_shndx != SHN_UNDEF
5189 && (definition || h->size == 0))
5190 {
5191 if (h->size != 0
5192 && h->size != isym->st_size
5193 && ! size_change_ok)
5194 _bfd_error_handler
5195 /* xgettext:c-format */
5196 (_("warning: size of symbol `%s' changed"
5197 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
5198 name, (uint64_t) h->size, old_bfd,
5199 (uint64_t) isym->st_size, abfd);
5200
5201 h->size = isym->st_size;
5202 }
5203
5204 /* If this is a common symbol, then we always want H->SIZE
5205 to be the size of the common symbol. The code just above
5206 won't fix the size if a common symbol becomes larger. We
5207 don't warn about a size change here, because that is
5208 covered by --warn-common. Allow changes between different
5209 function types. */
5210 if (h->root.type == bfd_link_hash_common)
5211 h->size = h->root.u.c.size;
5212
5213 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
5214 && ((definition && !new_weak)
5215 || (old_weak && h->root.type == bfd_link_hash_common)
5216 || h->type == STT_NOTYPE))
5217 {
5218 unsigned int type = ELF_ST_TYPE (isym->st_info);
5219
5220 /* Turn an IFUNC symbol from a DSO into a normal FUNC
5221 symbol. */
5222 if (type == STT_GNU_IFUNC
5223 && (abfd->flags & DYNAMIC) != 0)
5224 type = STT_FUNC;
5225
5226 if (h->type != type)
5227 {
5228 if (h->type != STT_NOTYPE && ! type_change_ok)
5229 /* xgettext:c-format */
5230 _bfd_error_handler
5231 (_("warning: type of symbol `%s' changed"
5232 " from %d to %d in %pB"),
5233 name, h->type, type, abfd);
5234
5235 h->type = type;
5236 }
5237 }
5238
5239 /* Merge st_other field. */
5240 elf_merge_st_other (abfd, h, isym->st_other, sec,
5241 definition, dynamic);
5242
5243 /* We don't want to make debug symbol dynamic. */
5244 if (definition
5245 && (sec->flags & SEC_DEBUGGING)
5246 && !bfd_link_relocatable (info))
5247 dynsym = false;
5248
5249 /* Nor should we make plugin symbols dynamic. */
5250 if ((abfd->flags & BFD_PLUGIN) != 0)
5251 dynsym = false;
5252
5253 if (definition)
5254 {
5255 h->target_internal = isym->st_target_internal;
5256 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5257 }
5258
5259 if (definition && !dynamic)
5260 {
5261 char *p = strchr (name, ELF_VER_CHR);
5262 if (p != NULL && p[1] != ELF_VER_CHR)
5263 {
5264 /* Queue non-default versions so that .symver x, x@FOO
5265 aliases can be checked. */
5266 if (!nondeflt_vers)
5267 {
5268 size_t amt = ((isymend - isym + 1)
5269 * sizeof (struct elf_link_hash_entry *));
5270 nondeflt_vers
5271 = (struct elf_link_hash_entry **) bfd_malloc (amt);
5272 if (!nondeflt_vers)
5273 goto error_free_vers;
5274 }
5275 nondeflt_vers[nondeflt_vers_cnt++] = h;
5276 }
5277 }
5278
5279 if (dynsym && h->dynindx == -1)
5280 {
5281 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5282 goto error_free_vers;
5283 if (h->is_weakalias
5284 && weakdef (h)->dynindx == -1)
5285 {
5286 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
5287 goto error_free_vers;
5288 }
5289 }
5290 else if (h->dynindx != -1)
5291 /* If the symbol already has a dynamic index, but
5292 visibility says it should not be visible, turn it into
5293 a local symbol. */
5294 switch (ELF_ST_VISIBILITY (h->other))
5295 {
5296 case STV_INTERNAL:
5297 case STV_HIDDEN:
5298 (*bed->elf_backend_hide_symbol) (info, h, true);
5299 dynsym = false;
5300 break;
5301 }
5302
5303 if (!add_needed
5304 && matched
5305 && definition
5306 && h->root.type != bfd_link_hash_indirect
5307 && ((dynsym
5308 && h->ref_regular_nonweak)
5309 || (old_bfd != NULL
5310 && (old_bfd->flags & BFD_PLUGIN) != 0
5311 && h->ref_ir_nonweak
5312 && !info->lto_all_symbols_read)
5313 || (h->ref_dynamic_nonweak
5314 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5315 && !on_needed_list (elf_dt_name (abfd),
5316 htab->needed, NULL))))
5317 {
5318 const char *soname = elf_dt_name (abfd);
5319
5320 info->callbacks->minfo ("%!", soname, old_bfd,
5321 h->root.root.string);
5322
5323 /* A symbol from a library loaded via DT_NEEDED of some
5324 other library is referenced by a regular object.
5325 Add a DT_NEEDED entry for it. Issue an error if
5326 --no-add-needed is used and the reference was not
5327 a weak one. */
5328 if (old_bfd != NULL
5329 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
5330 {
5331 _bfd_error_handler
5332 /* xgettext:c-format */
5333 (_("%pB: undefined reference to symbol '%s'"),
5334 old_bfd, name);
5335 bfd_set_error (bfd_error_missing_dso);
5336 goto error_free_vers;
5337 }
5338
5339 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
5340 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
5341
5342 /* Create dynamic sections for backends that require
5343 that be done before setup_gnu_properties. */
5344 if (!_bfd_elf_link_create_dynamic_sections (abfd, info))
5345 return false;
5346 add_needed = true;
5347 }
5348 }
5349 }
5350
5351 if (info->lto_plugin_active
5352 && !bfd_link_relocatable (info)
5353 && (abfd->flags & BFD_PLUGIN) == 0
5354 && !just_syms
5355 && extsymcount)
5356 {
5357 int r_sym_shift;
5358
5359 if (bed->s->arch_size == 32)
5360 r_sym_shift = 8;
5361 else
5362 r_sym_shift = 32;
5363
5364 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5365 referenced in regular objects so that linker plugin will get
5366 the correct symbol resolution. */
5367
5368 sym_hash = elf_sym_hashes (abfd);
5369 for (s = abfd->sections; s != NULL; s = s->next)
5370 {
5371 Elf_Internal_Rela *internal_relocs;
5372 Elf_Internal_Rela *rel, *relend;
5373
5374 /* Don't check relocations in excluded sections. */
5375 if ((s->flags & SEC_RELOC) == 0
5376 || s->reloc_count == 0
5377 || (s->flags & SEC_EXCLUDE) != 0
5378 || ((info->strip == strip_all
5379 || info->strip == strip_debugger)
5380 && (s->flags & SEC_DEBUGGING) != 0))
5381 continue;
5382
5383 internal_relocs = _bfd_elf_link_info_read_relocs (abfd, info,
5384 s, NULL,
5385 NULL,
5386 _bfd_link_keep_memory (info));
5387 if (internal_relocs == NULL)
5388 goto error_free_vers;
5389
5390 rel = internal_relocs;
5391 relend = rel + s->reloc_count;
5392 for ( ; rel < relend; rel++)
5393 {
5394 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5395 struct elf_link_hash_entry *h;
5396
5397 /* Skip local symbols. */
5398 if (r_symndx < extsymoff)
5399 continue;
5400
5401 h = sym_hash[r_symndx - extsymoff];
5402 if (h != NULL)
5403 h->root.non_ir_ref_regular = 1;
5404 }
5405
5406 if (elf_section_data (s)->relocs != internal_relocs)
5407 free (internal_relocs);
5408 }
5409 }
5410
5411 free (extversym);
5412 extversym = NULL;
5413 free (isymbuf);
5414 isymbuf = NULL;
5415
5416 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5417 {
5418 unsigned int i;
5419
5420 /* Restore the symbol table. */
5421 old_ent = (char *) old_tab + tabsize;
5422 memset (elf_sym_hashes (abfd), 0,
5423 extsymcount * sizeof (struct elf_link_hash_entry *));
5424 htab->root.table.table = old_table;
5425 htab->root.table.size = old_size;
5426 htab->root.table.count = old_count;
5427 memcpy (htab->root.table.table, old_tab, tabsize);
5428 htab->root.undefs = old_undefs;
5429 htab->root.undefs_tail = old_undefs_tail;
5430 if (htab->dynstr != NULL)
5431 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5432 free (old_strtab);
5433 old_strtab = NULL;
5434 for (i = 0; i < htab->root.table.size; i++)
5435 {
5436 struct bfd_hash_entry *p;
5437 struct elf_link_hash_entry *h;
5438 unsigned int non_ir_ref_dynamic;
5439
5440 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5441 {
5442 /* Preserve non_ir_ref_dynamic so that this symbol
5443 will be exported when the dynamic lib becomes needed
5444 in the second pass. */
5445 h = (struct elf_link_hash_entry *) p;
5446 if (h->root.type == bfd_link_hash_warning)
5447 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5448 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
5449
5450 h = (struct elf_link_hash_entry *) p;
5451 memcpy (h, old_ent, htab->root.table.entsize);
5452 old_ent = (char *) old_ent + htab->root.table.entsize;
5453 if (h->root.type == bfd_link_hash_warning)
5454 {
5455 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5456 memcpy (h, old_ent, htab->root.table.entsize);
5457 old_ent = (char *) old_ent + htab->root.table.entsize;
5458 }
5459 if (h->root.type == bfd_link_hash_common)
5460 {
5461 memcpy (h->root.u.c.p, old_ent, sizeof (*h->root.u.c.p));
5462 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
5463 }
5464 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
5465 }
5466 }
5467
5468 /* Make a special call to the linker "notice" function to
5469 tell it that symbols added for crefs may need to be removed. */
5470 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
5471 goto error_free_vers;
5472
5473 free (old_tab);
5474 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5475 alloc_mark);
5476 free (nondeflt_vers);
5477 return true;
5478 }
5479
5480 if (old_tab != NULL)
5481 {
5482 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
5483 goto error_free_vers;
5484 free (old_tab);
5485 old_tab = NULL;
5486 }
5487
5488 /* Now that all the symbols from this input file are created, if
5489 not performing a relocatable link, handle .symver foo, foo@BAR
5490 such that any relocs against foo become foo@BAR. */
5491 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
5492 {
5493 size_t cnt, symidx;
5494
5495 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5496 {
5497 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5498 char *shortname, *p;
5499 size_t amt;
5500
5501 p = strchr (h->root.root.string, ELF_VER_CHR);
5502 if (p == NULL
5503 || (h->root.type != bfd_link_hash_defined
5504 && h->root.type != bfd_link_hash_defweak))
5505 continue;
5506
5507 amt = p - h->root.root.string;
5508 shortname = (char *) bfd_malloc (amt + 1);
5509 if (!shortname)
5510 goto error_free_vers;
5511 memcpy (shortname, h->root.root.string, amt);
5512 shortname[amt] = '\0';
5513
5514 hi = (struct elf_link_hash_entry *)
5515 bfd_link_hash_lookup (&htab->root, shortname,
5516 false, false, false);
5517 if (hi != NULL
5518 && hi->root.type == h->root.type
5519 && hi->root.u.def.value == h->root.u.def.value
5520 && hi->root.u.def.section == h->root.u.def.section)
5521 {
5522 (*bed->elf_backend_hide_symbol) (info, hi, true);
5523 hi->root.type = bfd_link_hash_indirect;
5524 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
5525 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
5526 sym_hash = elf_sym_hashes (abfd);
5527 if (sym_hash)
5528 for (symidx = 0; symidx < extsymcount; ++symidx)
5529 if (sym_hash[symidx] == hi)
5530 {
5531 sym_hash[symidx] = h;
5532 break;
5533 }
5534 }
5535 free (shortname);
5536 }
5537 free (nondeflt_vers);
5538 nondeflt_vers = NULL;
5539 }
5540
5541 /* Now set the alias field correctly for all the weak defined
5542 symbols we found. The only way to do this is to search all the
5543 symbols. Since we only need the information for non functions in
5544 dynamic objects, that's the only time we actually put anything on
5545 the list WEAKS. We need this information so that if a regular
5546 object refers to a symbol defined weakly in a dynamic object, the
5547 real symbol in the dynamic object is also put in the dynamic
5548 symbols; we also must arrange for both symbols to point to the
5549 same memory location. We could handle the general case of symbol
5550 aliasing, but a general symbol alias can only be generated in
5551 assembler code, handling it correctly would be very time
5552 consuming, and other ELF linkers don't handle general aliasing
5553 either. */
5554 if (weaks != NULL)
5555 {
5556 struct elf_link_hash_entry **hpp;
5557 struct elf_link_hash_entry **hppend;
5558 struct elf_link_hash_entry **sorted_sym_hash;
5559 struct elf_link_hash_entry *h;
5560 size_t sym_count, amt;
5561
5562 /* Since we have to search the whole symbol list for each weak
5563 defined symbol, search time for N weak defined symbols will be
5564 O(N^2). Binary search will cut it down to O(NlogN). */
5565 amt = extsymcount * sizeof (*sorted_sym_hash);
5566 sorted_sym_hash = bfd_malloc (amt);
5567 if (sorted_sym_hash == NULL)
5568 goto error_return;
5569 sym_hash = sorted_sym_hash;
5570 hpp = elf_sym_hashes (abfd);
5571 hppend = hpp + extsymcount;
5572 sym_count = 0;
5573 for (; hpp < hppend; hpp++)
5574 {
5575 h = *hpp;
5576 if (h != NULL
5577 && h->root.type == bfd_link_hash_defined
5578 && !bed->is_function_type (h->type))
5579 {
5580 *sym_hash = h;
5581 sym_hash++;
5582 sym_count++;
5583 }
5584 }
5585
5586 qsort (sorted_sym_hash, sym_count, sizeof (*sorted_sym_hash),
5587 elf_sort_symbol);
5588
5589 while (weaks != NULL)
5590 {
5591 struct elf_link_hash_entry *hlook;
5592 asection *slook;
5593 bfd_vma vlook;
5594 size_t i, j, idx = 0;
5595
5596 hlook = weaks;
5597 weaks = hlook->u.alias;
5598 hlook->u.alias = NULL;
5599
5600 if (hlook->root.type != bfd_link_hash_defined
5601 && hlook->root.type != bfd_link_hash_defweak)
5602 continue;
5603
5604 slook = hlook->root.u.def.section;
5605 vlook = hlook->root.u.def.value;
5606
5607 i = 0;
5608 j = sym_count;
5609 while (i != j)
5610 {
5611 bfd_signed_vma vdiff;
5612 idx = (i + j) / 2;
5613 h = sorted_sym_hash[idx];
5614 vdiff = vlook - h->root.u.def.value;
5615 if (vdiff < 0)
5616 j = idx;
5617 else if (vdiff > 0)
5618 i = idx + 1;
5619 else
5620 {
5621 int sdiff = slook->id - h->root.u.def.section->id;
5622 if (sdiff < 0)
5623 j = idx;
5624 else if (sdiff > 0)
5625 i = idx + 1;
5626 else
5627 break;
5628 }
5629 }
5630
5631 /* We didn't find a value/section match. */
5632 if (i == j)
5633 continue;
5634
5635 /* With multiple aliases, or when the weak symbol is already
5636 strongly defined, we have multiple matching symbols and
5637 the binary search above may land on any of them. Step
5638 one past the matching symbol(s). */
5639 while (++idx != j)
5640 {
5641 h = sorted_sym_hash[idx];
5642 if (h->root.u.def.section != slook
5643 || h->root.u.def.value != vlook)
5644 break;
5645 }
5646
5647 /* Now look back over the aliases. Since we sorted by size
5648 as well as value and section, we'll choose the one with
5649 the largest size. */
5650 while (idx-- != i)
5651 {
5652 h = sorted_sym_hash[idx];
5653
5654 /* Stop if value or section doesn't match. */
5655 if (h->root.u.def.section != slook
5656 || h->root.u.def.value != vlook)
5657 break;
5658 else if (h != hlook)
5659 {
5660 struct elf_link_hash_entry *t;
5661
5662 hlook->u.alias = h;
5663 hlook->is_weakalias = 1;
5664 t = h;
5665 if (t->u.alias != NULL)
5666 while (t->u.alias != h)
5667 t = t->u.alias;
5668 t->u.alias = hlook;
5669
5670 /* If the weak definition is in the list of dynamic
5671 symbols, make sure the real definition is put
5672 there as well. */
5673 if (hlook->dynindx != -1 && h->dynindx == -1)
5674 {
5675 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5676 {
5677 err_free_sym_hash:
5678 free (sorted_sym_hash);
5679 goto error_return;
5680 }
5681 }
5682
5683 /* If the real definition is in the list of dynamic
5684 symbols, make sure the weak definition is put
5685 there as well. If we don't do this, then the
5686 dynamic loader might not merge the entries for the
5687 real definition and the weak definition. */
5688 if (h->dynindx != -1 && hlook->dynindx == -1)
5689 {
5690 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
5691 goto err_free_sym_hash;
5692 }
5693 break;
5694 }
5695 }
5696 }
5697
5698 free (sorted_sym_hash);
5699 }
5700
5701 if (bed->check_directives
5702 && !(*bed->check_directives) (abfd, info))
5703 return false;
5704
5705 /* If this is a non-traditional link, try to optimize the handling
5706 of the .stab/.stabstr sections. */
5707 if (! dynamic
5708 && ! info->traditional_format
5709 && is_elf_hash_table (&htab->root)
5710 && (info->strip != strip_all && info->strip != strip_debugger))
5711 {
5712 asection *stabstr;
5713
5714 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5715 if (stabstr != NULL)
5716 {
5717 bfd_size_type string_offset = 0;
5718 asection *stab;
5719
5720 for (stab = abfd->sections; stab; stab = stab->next)
5721 if (startswith (stab->name, ".stab")
5722 && (!stab->name[5] ||
5723 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5724 && (stab->flags & SEC_MERGE) == 0
5725 && !bfd_is_abs_section (stab->output_section))
5726 {
5727 struct bfd_elf_section_data *secdata;
5728
5729 secdata = elf_section_data (stab);
5730 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5731 stabstr, &secdata->sec_info,
5732 &string_offset))
5733 goto error_return;
5734 if (secdata->sec_info)
5735 stab->sec_info_type = SEC_INFO_TYPE_STABS;
5736 }
5737 }
5738 }
5739
5740 if (dynamic && add_needed)
5741 {
5742 /* Add this bfd to the loaded list. */
5743 struct elf_link_loaded_list *n;
5744
5745 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5746 if (n == NULL)
5747 goto error_return;
5748 n->abfd = abfd;
5749 n->next = htab->dyn_loaded;
5750 htab->dyn_loaded = n;
5751 }
5752 if (dynamic && !add_needed
5753 && (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) != 0)
5754 elf_dyn_lib_class (abfd) |= DYN_NO_NEEDED;
5755
5756 return true;
5757
5758 error_free_vers:
5759 free (old_tab);
5760 free (old_strtab);
5761 free (nondeflt_vers);
5762 free (extversym);
5763 error_free_sym:
5764 free (isymbuf);
5765 error_return:
5766 return false;
5767 }
5768
5769 /* Return the linker hash table entry of a symbol that might be
5770 satisfied by an archive symbol. Return -1 on error. */
5771
5772 struct bfd_link_hash_entry *
5773 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5774 struct bfd_link_info *info,
5775 const char *name)
5776 {
5777 struct bfd_link_hash_entry *h;
5778 char *p, *copy;
5779 size_t len, first;
5780
5781 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
5782 if (h != NULL)
5783 return h;
5784
5785 /* If this is a default version (the name contains @@), look up the
5786 symbol again with only one `@' as well as without the version.
5787 The effect is that references to the symbol with and without the
5788 version will be matched by the default symbol in the archive. */
5789
5790 p = strchr (name, ELF_VER_CHR);
5791 if (p == NULL || p[1] != ELF_VER_CHR)
5792 return h;
5793
5794 /* First check with only one `@'. */
5795 len = strlen (name);
5796 copy = (char *) bfd_alloc (abfd, len);
5797 if (copy == NULL)
5798 return (struct bfd_link_hash_entry *) -1;
5799
5800 first = p - name + 1;
5801 memcpy (copy, name, first);
5802 memcpy (copy + first, name + first + 1, len - first);
5803
5804 h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
5805 if (h == NULL)
5806 {
5807 /* We also need to check references to the symbol without the
5808 version. */
5809 copy[first - 1] = '\0';
5810 h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
5811 }
5812
5813 bfd_release (abfd, copy);
5814 return h;
5815 }
5816
5817 /* Add symbols from an ELF archive file to the linker hash table. We
5818 don't use _bfd_generic_link_add_archive_symbols because we need to
5819 handle versioned symbols.
5820
5821 Fortunately, ELF archive handling is simpler than that done by
5822 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5823 oddities. In ELF, if we find a symbol in the archive map, and the
5824 symbol is currently undefined, we know that we must pull in that
5825 object file.
5826
5827 Unfortunately, we do have to make multiple passes over the symbol
5828 table until nothing further is resolved. */
5829
5830 static bool
5831 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5832 {
5833 symindex c;
5834 unsigned char *included = NULL;
5835 carsym *symdefs;
5836 bool loop;
5837 size_t amt;
5838 const struct elf_backend_data *bed;
5839 struct bfd_link_hash_entry * (*archive_symbol_lookup)
5840 (bfd *, struct bfd_link_info *, const char *);
5841
5842 if (! bfd_has_map (abfd))
5843 {
5844 /* An empty archive is a special case. */
5845 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5846 return true;
5847 bfd_set_error (bfd_error_no_armap);
5848 return false;
5849 }
5850
5851 /* Keep track of all symbols we know to be already defined, and all
5852 files we know to be already included. This is to speed up the
5853 second and subsequent passes. */
5854 c = bfd_ardata (abfd)->symdef_count;
5855 if (c == 0)
5856 return true;
5857 amt = c * sizeof (*included);
5858 included = (unsigned char *) bfd_zmalloc (amt);
5859 if (included == NULL)
5860 return false;
5861
5862 symdefs = bfd_ardata (abfd)->symdefs;
5863 bed = get_elf_backend_data (abfd);
5864 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5865
5866 do
5867 {
5868 file_ptr last;
5869 symindex i;
5870 carsym *symdef;
5871 carsym *symdefend;
5872
5873 loop = false;
5874 last = -1;
5875
5876 symdef = symdefs;
5877 symdefend = symdef + c;
5878 for (i = 0; symdef < symdefend; symdef++, i++)
5879 {
5880 struct bfd_link_hash_entry *h;
5881 bfd *element;
5882 struct bfd_link_hash_entry *undefs_tail;
5883 symindex mark;
5884
5885 if (included[i])
5886 continue;
5887 if (symdef->file_offset == last)
5888 {
5889 included[i] = true;
5890 continue;
5891 }
5892
5893 h = archive_symbol_lookup (abfd, info, symdef->name);
5894 if (h == (struct bfd_link_hash_entry *) -1)
5895 goto error_return;
5896
5897 if (h == NULL)
5898 continue;
5899
5900 if (h->type == bfd_link_hash_undefined)
5901 {
5902 /* If the archive element has already been loaded then one
5903 of the symbols defined by that element might have been
5904 made undefined due to being in a discarded section. */
5905 if (is_elf_hash_table (info->hash)
5906 && ((struct elf_link_hash_entry *) h)->indx == -3)
5907 continue;
5908 }
5909 else if (h->type == bfd_link_hash_common)
5910 {
5911 /* We currently have a common symbol. The archive map contains
5912 a reference to this symbol, so we may want to include it. We
5913 only want to include it however, if this archive element
5914 contains a definition of the symbol, not just another common
5915 declaration of it.
5916
5917 Unfortunately some archivers (including GNU ar) will put
5918 declarations of common symbols into their archive maps, as
5919 well as real definitions, so we cannot just go by the archive
5920 map alone. Instead we must read in the element's symbol
5921 table and check that to see what kind of symbol definition
5922 this is. */
5923 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5924 continue;
5925 }
5926 else
5927 {
5928 if (h->type != bfd_link_hash_undefweak)
5929 /* Symbol must be defined. Don't check it again. */
5930 included[i] = true;
5931 continue;
5932 }
5933
5934 /* We need to include this archive member. */
5935 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5936 if (element == NULL)
5937 goto error_return;
5938
5939 if (! bfd_check_format (element, bfd_object))
5940 goto error_return;
5941
5942 undefs_tail = info->hash->undefs_tail;
5943
5944 if (!(*info->callbacks
5945 ->add_archive_element) (info, element, symdef->name, &element))
5946 continue;
5947 if (!bfd_link_add_symbols (element, info))
5948 goto error_return;
5949
5950 /* If there are any new undefined symbols, we need to make
5951 another pass through the archive in order to see whether
5952 they can be defined. FIXME: This isn't perfect, because
5953 common symbols wind up on undefs_tail and because an
5954 undefined symbol which is defined later on in this pass
5955 does not require another pass. This isn't a bug, but it
5956 does make the code less efficient than it could be. */
5957 if (undefs_tail != info->hash->undefs_tail)
5958 loop = true;
5959
5960 /* Look backward to mark all symbols from this object file
5961 which we have already seen in this pass. */
5962 mark = i;
5963 do
5964 {
5965 included[mark] = true;
5966 if (mark == 0)
5967 break;
5968 --mark;
5969 }
5970 while (symdefs[mark].file_offset == symdef->file_offset);
5971
5972 /* We mark subsequent symbols from this object file as we go
5973 on through the loop. */
5974 last = symdef->file_offset;
5975 }
5976 }
5977 while (loop);
5978
5979 free (included);
5980 return true;
5981
5982 error_return:
5983 free (included);
5984 return false;
5985 }
5986
5987 /* Given an ELF BFD, add symbols to the global hash table as
5988 appropriate. */
5989
5990 bool
5991 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5992 {
5993 switch (bfd_get_format (abfd))
5994 {
5995 case bfd_object:
5996 return elf_link_add_object_symbols (abfd, info);
5997 case bfd_archive:
5998 return elf_link_add_archive_symbols (abfd, info);
5999 default:
6000 bfd_set_error (bfd_error_wrong_format);
6001 return false;
6002 }
6003 }
6004 \f
6005 struct hash_codes_info
6006 {
6007 unsigned long *hashcodes;
6008 bool error;
6009 };
6010
6011 /* This function will be called though elf_link_hash_traverse to store
6012 all hash value of the exported symbols in an array. */
6013
6014 static bool
6015 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
6016 {
6017 struct hash_codes_info *inf = (struct hash_codes_info *) data;
6018 const char *name;
6019 unsigned long ha;
6020 char *alc = NULL;
6021
6022 /* Ignore indirect symbols. These are added by the versioning code. */
6023 if (h->dynindx == -1)
6024 return true;
6025
6026 name = h->root.root.string;
6027 if (h->versioned >= versioned)
6028 {
6029 char *p = strchr (name, ELF_VER_CHR);
6030 if (p != NULL)
6031 {
6032 alc = (char *) bfd_malloc (p - name + 1);
6033 if (alc == NULL)
6034 {
6035 inf->error = true;
6036 return false;
6037 }
6038 memcpy (alc, name, p - name);
6039 alc[p - name] = '\0';
6040 name = alc;
6041 }
6042 }
6043
6044 /* Compute the hash value. */
6045 ha = bfd_elf_hash (name);
6046
6047 /* Store the found hash value in the array given as the argument. */
6048 *(inf->hashcodes)++ = ha;
6049
6050 /* And store it in the struct so that we can put it in the hash table
6051 later. */
6052 h->u.elf_hash_value = ha;
6053
6054 free (alc);
6055 return true;
6056 }
6057
6058 struct collect_gnu_hash_codes
6059 {
6060 bfd *output_bfd;
6061 const struct elf_backend_data *bed;
6062 unsigned long int nsyms;
6063 unsigned long int maskbits;
6064 unsigned long int *hashcodes;
6065 unsigned long int *hashval;
6066 unsigned long int *indx;
6067 unsigned long int *counts;
6068 bfd_vma *bitmask;
6069 bfd_byte *contents;
6070 bfd_size_type xlat;
6071 long int min_dynindx;
6072 unsigned long int bucketcount;
6073 unsigned long int symindx;
6074 long int local_indx;
6075 long int shift1, shift2;
6076 unsigned long int mask;
6077 bool error;
6078 };
6079
6080 /* This function will be called though elf_link_hash_traverse to store
6081 all hash value of the exported symbols in an array. */
6082
6083 static bool
6084 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
6085 {
6086 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6087 const char *name;
6088 unsigned long ha;
6089 char *alc = NULL;
6090
6091 /* Ignore indirect symbols. These are added by the versioning code. */
6092 if (h->dynindx == -1)
6093 return true;
6094
6095 /* Ignore also local symbols and undefined symbols. */
6096 if (! (*s->bed->elf_hash_symbol) (h))
6097 return true;
6098
6099 name = h->root.root.string;
6100 if (h->versioned >= versioned)
6101 {
6102 char *p = strchr (name, ELF_VER_CHR);
6103 if (p != NULL)
6104 {
6105 alc = (char *) bfd_malloc (p - name + 1);
6106 if (alc == NULL)
6107 {
6108 s->error = true;
6109 return false;
6110 }
6111 memcpy (alc, name, p - name);
6112 alc[p - name] = '\0';
6113 name = alc;
6114 }
6115 }
6116
6117 /* Compute the hash value. */
6118 ha = bfd_elf_gnu_hash (name);
6119
6120 /* Store the found hash value in the array for compute_bucket_count,
6121 and also for .dynsym reordering purposes. */
6122 s->hashcodes[s->nsyms] = ha;
6123 s->hashval[h->dynindx] = ha;
6124 ++s->nsyms;
6125 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
6126 s->min_dynindx = h->dynindx;
6127
6128 free (alc);
6129 return true;
6130 }
6131
6132 /* This function will be called though elf_link_hash_traverse to do
6133 final dynamic symbol renumbering in case of .gnu.hash.
6134 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6135 to the translation table. */
6136
6137 static bool
6138 elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data)
6139 {
6140 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6141 unsigned long int bucket;
6142 unsigned long int val;
6143
6144 /* Ignore indirect symbols. */
6145 if (h->dynindx == -1)
6146 return true;
6147
6148 /* Ignore also local symbols and undefined symbols. */
6149 if (! (*s->bed->elf_hash_symbol) (h))
6150 {
6151 if (h->dynindx >= s->min_dynindx)
6152 {
6153 if (s->bed->record_xhash_symbol != NULL)
6154 {
6155 (*s->bed->record_xhash_symbol) (h, 0);
6156 s->local_indx++;
6157 }
6158 else
6159 h->dynindx = s->local_indx++;
6160 }
6161 return true;
6162 }
6163
6164 bucket = s->hashval[h->dynindx] % s->bucketcount;
6165 val = (s->hashval[h->dynindx] >> s->shift1)
6166 & ((s->maskbits >> s->shift1) - 1);
6167 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
6168 s->bitmask[val]
6169 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
6170 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
6171 if (s->counts[bucket] == 1)
6172 /* Last element terminates the chain. */
6173 val |= 1;
6174 bfd_put_32 (s->output_bfd, val,
6175 s->contents + (s->indx[bucket] - s->symindx) * 4);
6176 --s->counts[bucket];
6177 if (s->bed->record_xhash_symbol != NULL)
6178 {
6179 bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4;
6180
6181 (*s->bed->record_xhash_symbol) (h, xlat_loc);
6182 }
6183 else
6184 h->dynindx = s->indx[bucket]++;
6185 return true;
6186 }
6187
6188 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6189
6190 bool
6191 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
6192 {
6193 return !(h->forced_local
6194 || h->root.type == bfd_link_hash_undefined
6195 || h->root.type == bfd_link_hash_undefweak
6196 || ((h->root.type == bfd_link_hash_defined
6197 || h->root.type == bfd_link_hash_defweak)
6198 && h->root.u.def.section->output_section == NULL));
6199 }
6200
6201 /* Array used to determine the number of hash table buckets to use
6202 based on the number of symbols there are. If there are fewer than
6203 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6204 fewer than 37 we use 17 buckets, and so forth. We never use more
6205 than 32771 buckets. */
6206
6207 static const size_t elf_buckets[] =
6208 {
6209 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6210 16411, 32771, 0
6211 };
6212
6213 /* Compute bucket count for hashing table. We do not use a static set
6214 of possible tables sizes anymore. Instead we determine for all
6215 possible reasonable sizes of the table the outcome (i.e., the
6216 number of collisions etc) and choose the best solution. The
6217 weighting functions are not too simple to allow the table to grow
6218 without bounds. Instead one of the weighting factors is the size.
6219 Therefore the result is always a good payoff between few collisions
6220 (= short chain lengths) and table size. */
6221 static size_t
6222 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
6223 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
6224 unsigned long int nsyms,
6225 int gnu_hash)
6226 {
6227 size_t best_size = 0;
6228 unsigned long int i;
6229
6230 /* We have a problem here. The following code to optimize the table
6231 size requires an integer type with more the 32 bits. If
6232 BFD_HOST_U_64_BIT is set we know about such a type. */
6233 #ifdef BFD_HOST_U_64_BIT
6234 if (info->optimize)
6235 {
6236 size_t minsize;
6237 size_t maxsize;
6238 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
6239 bfd *dynobj = elf_hash_table (info)->dynobj;
6240 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
6241 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
6242 unsigned long int *counts;
6243 bfd_size_type amt;
6244 unsigned int no_improvement_count = 0;
6245
6246 /* Possible optimization parameters: if we have NSYMS symbols we say
6247 that the hashing table must at least have NSYMS/4 and at most
6248 2*NSYMS buckets. */
6249 minsize = nsyms / 4;
6250 if (minsize == 0)
6251 minsize = 1;
6252 best_size = maxsize = nsyms * 2;
6253 if (gnu_hash)
6254 {
6255 if (minsize < 2)
6256 minsize = 2;
6257 if ((best_size & 31) == 0)
6258 ++best_size;
6259 }
6260
6261 /* Create array where we count the collisions in. We must use bfd_malloc
6262 since the size could be large. */
6263 amt = maxsize;
6264 amt *= sizeof (unsigned long int);
6265 counts = (unsigned long int *) bfd_malloc (amt);
6266 if (counts == NULL)
6267 return 0;
6268
6269 /* Compute the "optimal" size for the hash table. The criteria is a
6270 minimal chain length. The minor criteria is (of course) the size
6271 of the table. */
6272 for (i = minsize; i < maxsize; ++i)
6273 {
6274 /* Walk through the array of hashcodes and count the collisions. */
6275 BFD_HOST_U_64_BIT max;
6276 unsigned long int j;
6277 unsigned long int fact;
6278
6279 if (gnu_hash && (i & 31) == 0)
6280 continue;
6281
6282 memset (counts, '\0', i * sizeof (unsigned long int));
6283
6284 /* Determine how often each hash bucket is used. */
6285 for (j = 0; j < nsyms; ++j)
6286 ++counts[hashcodes[j] % i];
6287
6288 /* For the weight function we need some information about the
6289 pagesize on the target. This is information need not be 100%
6290 accurate. Since this information is not available (so far) we
6291 define it here to a reasonable default value. If it is crucial
6292 to have a better value some day simply define this value. */
6293 # ifndef BFD_TARGET_PAGESIZE
6294 # define BFD_TARGET_PAGESIZE (4096)
6295 # endif
6296
6297 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6298 and the chains. */
6299 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
6300
6301 # if 1
6302 /* Variant 1: optimize for short chains. We add the squares
6303 of all the chain lengths (which favors many small chain
6304 over a few long chains). */
6305 for (j = 0; j < i; ++j)
6306 max += counts[j] * counts[j];
6307
6308 /* This adds penalties for the overall size of the table. */
6309 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6310 max *= fact * fact;
6311 # else
6312 /* Variant 2: Optimize a lot more for small table. Here we
6313 also add squares of the size but we also add penalties for
6314 empty slots (the +1 term). */
6315 for (j = 0; j < i; ++j)
6316 max += (1 + counts[j]) * (1 + counts[j]);
6317
6318 /* The overall size of the table is considered, but not as
6319 strong as in variant 1, where it is squared. */
6320 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6321 max *= fact;
6322 # endif
6323
6324 /* Compare with current best results. */
6325 if (max < best_chlen)
6326 {
6327 best_chlen = max;
6328 best_size = i;
6329 no_improvement_count = 0;
6330 }
6331 /* PR 11843: Avoid futile long searches for the best bucket size
6332 when there are a large number of symbols. */
6333 else if (++no_improvement_count == 100)
6334 break;
6335 }
6336
6337 free (counts);
6338 }
6339 else
6340 #endif /* defined (BFD_HOST_U_64_BIT) */
6341 {
6342 /* This is the fallback solution if no 64bit type is available or if we
6343 are not supposed to spend much time on optimizations. We select the
6344 bucket count using a fixed set of numbers. */
6345 for (i = 0; elf_buckets[i] != 0; i++)
6346 {
6347 best_size = elf_buckets[i];
6348 if (nsyms < elf_buckets[i + 1])
6349 break;
6350 }
6351 if (gnu_hash && best_size < 2)
6352 best_size = 2;
6353 }
6354
6355 return best_size;
6356 }
6357
6358 /* Size any SHT_GROUP section for ld -r. */
6359
6360 bool
6361 _bfd_elf_size_group_sections (struct bfd_link_info *info)
6362 {
6363 bfd *ibfd;
6364 asection *s;
6365
6366 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6367 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6368 && (s = ibfd->sections) != NULL
6369 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
6370 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6371 return false;
6372 return true;
6373 }
6374
6375 /* Set a default stack segment size. The value in INFO wins. If it
6376 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6377 undefined it is initialized. */
6378
6379 bool
6380 bfd_elf_stack_segment_size (bfd *output_bfd,
6381 struct bfd_link_info *info,
6382 const char *legacy_symbol,
6383 bfd_vma default_size)
6384 {
6385 struct elf_link_hash_entry *h = NULL;
6386
6387 /* Look for legacy symbol. */
6388 if (legacy_symbol)
6389 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6390 false, false, false);
6391 if (h && (h->root.type == bfd_link_hash_defined
6392 || h->root.type == bfd_link_hash_defweak)
6393 && h->def_regular
6394 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6395 {
6396 /* The symbol has no type if specified on the command line. */
6397 h->type = STT_OBJECT;
6398 if (info->stacksize)
6399 /* xgettext:c-format */
6400 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6401 output_bfd, legacy_symbol);
6402 else if (h->root.u.def.section != bfd_abs_section_ptr)
6403 /* xgettext:c-format */
6404 _bfd_error_handler (_("%pB: %s not absolute"),
6405 output_bfd, legacy_symbol);
6406 else
6407 info->stacksize = h->root.u.def.value;
6408 }
6409
6410 if (!info->stacksize)
6411 /* If the user didn't set a size, or explicitly inhibit the
6412 size, set it now. */
6413 info->stacksize = default_size;
6414
6415 /* Provide the legacy symbol, if it is referenced. */
6416 if (h && (h->root.type == bfd_link_hash_undefined
6417 || h->root.type == bfd_link_hash_undefweak))
6418 {
6419 struct bfd_link_hash_entry *bh = NULL;
6420
6421 if (!(_bfd_generic_link_add_one_symbol
6422 (info, output_bfd, legacy_symbol,
6423 BSF_GLOBAL, bfd_abs_section_ptr,
6424 info->stacksize >= 0 ? info->stacksize : 0,
6425 NULL, false, get_elf_backend_data (output_bfd)->collect, &bh)))
6426 return false;
6427
6428 h = (struct elf_link_hash_entry *) bh;
6429 h->def_regular = 1;
6430 h->type = STT_OBJECT;
6431 }
6432
6433 return true;
6434 }
6435
6436 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6437
6438 struct elf_gc_sweep_symbol_info
6439 {
6440 struct bfd_link_info *info;
6441 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6442 bool);
6443 };
6444
6445 static bool
6446 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6447 {
6448 if (!h->mark
6449 && (((h->root.type == bfd_link_hash_defined
6450 || h->root.type == bfd_link_hash_defweak)
6451 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6452 && h->root.u.def.section->gc_mark))
6453 || h->root.type == bfd_link_hash_undefined
6454 || h->root.type == bfd_link_hash_undefweak))
6455 {
6456 struct elf_gc_sweep_symbol_info *inf;
6457
6458 inf = (struct elf_gc_sweep_symbol_info *) data;
6459 (*inf->hide_symbol) (inf->info, h, true);
6460 h->def_regular = 0;
6461 h->ref_regular = 0;
6462 h->ref_regular_nonweak = 0;
6463 }
6464
6465 return true;
6466 }
6467
6468 /* Set up the sizes and contents of the ELF dynamic sections. This is
6469 called by the ELF linker emulation before_allocation routine. We
6470 must set the sizes of the sections before the linker sets the
6471 addresses of the various sections. */
6472
6473 bool
6474 bfd_elf_size_dynamic_sections (bfd *output_bfd,
6475 const char *soname,
6476 const char *rpath,
6477 const char *filter_shlib,
6478 const char *audit,
6479 const char *depaudit,
6480 const char * const *auxiliary_filters,
6481 struct bfd_link_info *info,
6482 asection **sinterpptr)
6483 {
6484 bfd *dynobj;
6485 const struct elf_backend_data *bed;
6486
6487 *sinterpptr = NULL;
6488
6489 if (!is_elf_hash_table (info->hash))
6490 return true;
6491
6492 dynobj = elf_hash_table (info)->dynobj;
6493
6494 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6495 {
6496 struct bfd_elf_version_tree *verdefs;
6497 struct elf_info_failed asvinfo;
6498 struct bfd_elf_version_tree *t;
6499 struct bfd_elf_version_expr *d;
6500 asection *s;
6501 size_t soname_indx;
6502
6503 /* If we are supposed to export all symbols into the dynamic symbol
6504 table (this is not the normal case), then do so. */
6505 if (info->export_dynamic
6506 || (bfd_link_executable (info) && info->dynamic))
6507 {
6508 struct elf_info_failed eif;
6509
6510 eif.info = info;
6511 eif.failed = false;
6512 elf_link_hash_traverse (elf_hash_table (info),
6513 _bfd_elf_export_symbol,
6514 &eif);
6515 if (eif.failed)
6516 return false;
6517 }
6518
6519 if (soname != NULL)
6520 {
6521 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6522 soname, true);
6523 if (soname_indx == (size_t) -1
6524 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6525 return false;
6526 }
6527 else
6528 soname_indx = (size_t) -1;
6529
6530 /* Make all global versions with definition. */
6531 for (t = info->version_info; t != NULL; t = t->next)
6532 for (d = t->globals.list; d != NULL; d = d->next)
6533 if (!d->symver && d->literal)
6534 {
6535 const char *verstr, *name;
6536 size_t namelen, verlen, newlen;
6537 char *newname, *p, leading_char;
6538 struct elf_link_hash_entry *newh;
6539
6540 leading_char = bfd_get_symbol_leading_char (output_bfd);
6541 name = d->pattern;
6542 namelen = strlen (name) + (leading_char != '\0');
6543 verstr = t->name;
6544 verlen = strlen (verstr);
6545 newlen = namelen + verlen + 3;
6546
6547 newname = (char *) bfd_malloc (newlen);
6548 if (newname == NULL)
6549 return false;
6550 newname[0] = leading_char;
6551 memcpy (newname + (leading_char != '\0'), name, namelen);
6552
6553 /* Check the hidden versioned definition. */
6554 p = newname + namelen;
6555 *p++ = ELF_VER_CHR;
6556 memcpy (p, verstr, verlen + 1);
6557 newh = elf_link_hash_lookup (elf_hash_table (info),
6558 newname, false, false,
6559 false);
6560 if (newh == NULL
6561 || (newh->root.type != bfd_link_hash_defined
6562 && newh->root.type != bfd_link_hash_defweak))
6563 {
6564 /* Check the default versioned definition. */
6565 *p++ = ELF_VER_CHR;
6566 memcpy (p, verstr, verlen + 1);
6567 newh = elf_link_hash_lookup (elf_hash_table (info),
6568 newname, false, false,
6569 false);
6570 }
6571 free (newname);
6572
6573 /* Mark this version if there is a definition and it is
6574 not defined in a shared object. */
6575 if (newh != NULL
6576 && !newh->def_dynamic
6577 && (newh->root.type == bfd_link_hash_defined
6578 || newh->root.type == bfd_link_hash_defweak))
6579 d->symver = 1;
6580 }
6581
6582 /* Attach all the symbols to their version information. */
6583 asvinfo.info = info;
6584 asvinfo.failed = false;
6585
6586 elf_link_hash_traverse (elf_hash_table (info),
6587 _bfd_elf_link_assign_sym_version,
6588 &asvinfo);
6589 if (asvinfo.failed)
6590 return false;
6591
6592 if (!info->allow_undefined_version)
6593 {
6594 /* Check if all global versions have a definition. */
6595 bool all_defined = true;
6596 for (t = info->version_info; t != NULL; t = t->next)
6597 for (d = t->globals.list; d != NULL; d = d->next)
6598 if (d->literal && !d->symver && !d->script)
6599 {
6600 _bfd_error_handler
6601 (_("%s: undefined version: %s"),
6602 d->pattern, t->name);
6603 all_defined = false;
6604 }
6605
6606 if (!all_defined)
6607 {
6608 bfd_set_error (bfd_error_bad_value);
6609 return false;
6610 }
6611 }
6612
6613 /* Set up the version definition section. */
6614 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6615 BFD_ASSERT (s != NULL);
6616
6617 /* We may have created additional version definitions if we are
6618 just linking a regular application. */
6619 verdefs = info->version_info;
6620
6621 /* Skip anonymous version tag. */
6622 if (verdefs != NULL && verdefs->vernum == 0)
6623 verdefs = verdefs->next;
6624
6625 if (verdefs == NULL && !info->create_default_symver)
6626 s->flags |= SEC_EXCLUDE;
6627 else
6628 {
6629 unsigned int cdefs;
6630 bfd_size_type size;
6631 bfd_byte *p;
6632 Elf_Internal_Verdef def;
6633 Elf_Internal_Verdaux defaux;
6634 struct bfd_link_hash_entry *bh;
6635 struct elf_link_hash_entry *h;
6636 const char *name;
6637
6638 cdefs = 0;
6639 size = 0;
6640
6641 /* Make space for the base version. */
6642 size += sizeof (Elf_External_Verdef);
6643 size += sizeof (Elf_External_Verdaux);
6644 ++cdefs;
6645
6646 /* Make space for the default version. */
6647 if (info->create_default_symver)
6648 {
6649 size += sizeof (Elf_External_Verdef);
6650 ++cdefs;
6651 }
6652
6653 for (t = verdefs; t != NULL; t = t->next)
6654 {
6655 struct bfd_elf_version_deps *n;
6656
6657 /* Don't emit base version twice. */
6658 if (t->vernum == 0)
6659 continue;
6660
6661 size += sizeof (Elf_External_Verdef);
6662 size += sizeof (Elf_External_Verdaux);
6663 ++cdefs;
6664
6665 for (n = t->deps; n != NULL; n = n->next)
6666 size += sizeof (Elf_External_Verdaux);
6667 }
6668
6669 s->size = size;
6670 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6671 if (s->contents == NULL && s->size != 0)
6672 return false;
6673
6674 /* Fill in the version definition section. */
6675
6676 p = s->contents;
6677
6678 def.vd_version = VER_DEF_CURRENT;
6679 def.vd_flags = VER_FLG_BASE;
6680 def.vd_ndx = 1;
6681 def.vd_cnt = 1;
6682 if (info->create_default_symver)
6683 {
6684 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6685 def.vd_next = sizeof (Elf_External_Verdef);
6686 }
6687 else
6688 {
6689 def.vd_aux = sizeof (Elf_External_Verdef);
6690 def.vd_next = (sizeof (Elf_External_Verdef)
6691 + sizeof (Elf_External_Verdaux));
6692 }
6693
6694 if (soname_indx != (size_t) -1)
6695 {
6696 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6697 soname_indx);
6698 def.vd_hash = bfd_elf_hash (soname);
6699 defaux.vda_name = soname_indx;
6700 name = soname;
6701 }
6702 else
6703 {
6704 size_t indx;
6705
6706 name = lbasename (bfd_get_filename (output_bfd));
6707 def.vd_hash = bfd_elf_hash (name);
6708 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6709 name, false);
6710 if (indx == (size_t) -1)
6711 return false;
6712 defaux.vda_name = indx;
6713 }
6714 defaux.vda_next = 0;
6715
6716 _bfd_elf_swap_verdef_out (output_bfd, &def,
6717 (Elf_External_Verdef *) p);
6718 p += sizeof (Elf_External_Verdef);
6719 if (info->create_default_symver)
6720 {
6721 /* Add a symbol representing this version. */
6722 bh = NULL;
6723 if (! (_bfd_generic_link_add_one_symbol
6724 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6725 0, NULL, false,
6726 get_elf_backend_data (dynobj)->collect, &bh)))
6727 return false;
6728 h = (struct elf_link_hash_entry *) bh;
6729 h->non_elf = 0;
6730 h->def_regular = 1;
6731 h->type = STT_OBJECT;
6732 h->verinfo.vertree = NULL;
6733
6734 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6735 return false;
6736
6737 /* Create a duplicate of the base version with the same
6738 aux block, but different flags. */
6739 def.vd_flags = 0;
6740 def.vd_ndx = 2;
6741 def.vd_aux = sizeof (Elf_External_Verdef);
6742 if (verdefs)
6743 def.vd_next = (sizeof (Elf_External_Verdef)
6744 + sizeof (Elf_External_Verdaux));
6745 else
6746 def.vd_next = 0;
6747 _bfd_elf_swap_verdef_out (output_bfd, &def,
6748 (Elf_External_Verdef *) p);
6749 p += sizeof (Elf_External_Verdef);
6750 }
6751 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6752 (Elf_External_Verdaux *) p);
6753 p += sizeof (Elf_External_Verdaux);
6754
6755 for (t = verdefs; t != NULL; t = t->next)
6756 {
6757 unsigned int cdeps;
6758 struct bfd_elf_version_deps *n;
6759
6760 /* Don't emit the base version twice. */
6761 if (t->vernum == 0)
6762 continue;
6763
6764 cdeps = 0;
6765 for (n = t->deps; n != NULL; n = n->next)
6766 ++cdeps;
6767
6768 /* Add a symbol representing this version. */
6769 bh = NULL;
6770 if (! (_bfd_generic_link_add_one_symbol
6771 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6772 0, NULL, false,
6773 get_elf_backend_data (dynobj)->collect, &bh)))
6774 return false;
6775 h = (struct elf_link_hash_entry *) bh;
6776 h->non_elf = 0;
6777 h->def_regular = 1;
6778 h->type = STT_OBJECT;
6779 h->verinfo.vertree = t;
6780
6781 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6782 return false;
6783
6784 def.vd_version = VER_DEF_CURRENT;
6785 def.vd_flags = 0;
6786 if (t->globals.list == NULL
6787 && t->locals.list == NULL
6788 && ! t->used)
6789 def.vd_flags |= VER_FLG_WEAK;
6790 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6791 def.vd_cnt = cdeps + 1;
6792 def.vd_hash = bfd_elf_hash (t->name);
6793 def.vd_aux = sizeof (Elf_External_Verdef);
6794 def.vd_next = 0;
6795
6796 /* If a basever node is next, it *must* be the last node in
6797 the chain, otherwise Verdef construction breaks. */
6798 if (t->next != NULL && t->next->vernum == 0)
6799 BFD_ASSERT (t->next->next == NULL);
6800
6801 if (t->next != NULL && t->next->vernum != 0)
6802 def.vd_next = (sizeof (Elf_External_Verdef)
6803 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6804
6805 _bfd_elf_swap_verdef_out (output_bfd, &def,
6806 (Elf_External_Verdef *) p);
6807 p += sizeof (Elf_External_Verdef);
6808
6809 defaux.vda_name = h->dynstr_index;
6810 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6811 h->dynstr_index);
6812 defaux.vda_next = 0;
6813 if (t->deps != NULL)
6814 defaux.vda_next = sizeof (Elf_External_Verdaux);
6815 t->name_indx = defaux.vda_name;
6816
6817 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6818 (Elf_External_Verdaux *) p);
6819 p += sizeof (Elf_External_Verdaux);
6820
6821 for (n = t->deps; n != NULL; n = n->next)
6822 {
6823 if (n->version_needed == NULL)
6824 {
6825 /* This can happen if there was an error in the
6826 version script. */
6827 defaux.vda_name = 0;
6828 }
6829 else
6830 {
6831 defaux.vda_name = n->version_needed->name_indx;
6832 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6833 defaux.vda_name);
6834 }
6835 if (n->next == NULL)
6836 defaux.vda_next = 0;
6837 else
6838 defaux.vda_next = sizeof (Elf_External_Verdaux);
6839
6840 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6841 (Elf_External_Verdaux *) p);
6842 p += sizeof (Elf_External_Verdaux);
6843 }
6844 }
6845
6846 elf_tdata (output_bfd)->cverdefs = cdefs;
6847 }
6848 }
6849
6850 bed = get_elf_backend_data (output_bfd);
6851
6852 if (info->gc_sections && bed->can_gc_sections)
6853 {
6854 struct elf_gc_sweep_symbol_info sweep_info;
6855
6856 /* Remove the symbols that were in the swept sections from the
6857 dynamic symbol table. */
6858 sweep_info.info = info;
6859 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6860 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6861 &sweep_info);
6862 }
6863
6864 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6865 {
6866 asection *s;
6867 struct elf_find_verdep_info sinfo;
6868
6869 /* Work out the size of the version reference section. */
6870
6871 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6872 BFD_ASSERT (s != NULL);
6873
6874 sinfo.info = info;
6875 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6876 if (sinfo.vers == 0)
6877 sinfo.vers = 1;
6878 sinfo.failed = false;
6879
6880 elf_link_hash_traverse (elf_hash_table (info),
6881 _bfd_elf_link_find_version_dependencies,
6882 &sinfo);
6883 if (sinfo.failed)
6884 return false;
6885
6886 if (elf_tdata (output_bfd)->verref == NULL)
6887 s->flags |= SEC_EXCLUDE;
6888 else
6889 {
6890 Elf_Internal_Verneed *vn;
6891 unsigned int size;
6892 unsigned int crefs;
6893 bfd_byte *p;
6894
6895 /* Build the version dependency section. */
6896 size = 0;
6897 crefs = 0;
6898 for (vn = elf_tdata (output_bfd)->verref;
6899 vn != NULL;
6900 vn = vn->vn_nextref)
6901 {
6902 Elf_Internal_Vernaux *a;
6903
6904 size += sizeof (Elf_External_Verneed);
6905 ++crefs;
6906 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6907 size += sizeof (Elf_External_Vernaux);
6908 }
6909
6910 s->size = size;
6911 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6912 if (s->contents == NULL)
6913 return false;
6914
6915 p = s->contents;
6916 for (vn = elf_tdata (output_bfd)->verref;
6917 vn != NULL;
6918 vn = vn->vn_nextref)
6919 {
6920 unsigned int caux;
6921 Elf_Internal_Vernaux *a;
6922 size_t indx;
6923
6924 caux = 0;
6925 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6926 ++caux;
6927
6928 vn->vn_version = VER_NEED_CURRENT;
6929 vn->vn_cnt = caux;
6930 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6931 elf_dt_name (vn->vn_bfd) != NULL
6932 ? elf_dt_name (vn->vn_bfd)
6933 : lbasename (bfd_get_filename
6934 (vn->vn_bfd)),
6935 false);
6936 if (indx == (size_t) -1)
6937 return false;
6938 vn->vn_file = indx;
6939 vn->vn_aux = sizeof (Elf_External_Verneed);
6940 if (vn->vn_nextref == NULL)
6941 vn->vn_next = 0;
6942 else
6943 vn->vn_next = (sizeof (Elf_External_Verneed)
6944 + caux * sizeof (Elf_External_Vernaux));
6945
6946 _bfd_elf_swap_verneed_out (output_bfd, vn,
6947 (Elf_External_Verneed *) p);
6948 p += sizeof (Elf_External_Verneed);
6949
6950 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6951 {
6952 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6953 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6954 a->vna_nodename, false);
6955 if (indx == (size_t) -1)
6956 return false;
6957 a->vna_name = indx;
6958 if (a->vna_nextptr == NULL)
6959 a->vna_next = 0;
6960 else
6961 a->vna_next = sizeof (Elf_External_Vernaux);
6962
6963 _bfd_elf_swap_vernaux_out (output_bfd, a,
6964 (Elf_External_Vernaux *) p);
6965 p += sizeof (Elf_External_Vernaux);
6966 }
6967 }
6968
6969 elf_tdata (output_bfd)->cverrefs = crefs;
6970 }
6971 }
6972
6973 /* Any syms created from now on start with -1 in
6974 got.refcount/offset and plt.refcount/offset. */
6975 elf_hash_table (info)->init_got_refcount
6976 = elf_hash_table (info)->init_got_offset;
6977 elf_hash_table (info)->init_plt_refcount
6978 = elf_hash_table (info)->init_plt_offset;
6979
6980 if (bfd_link_relocatable (info)
6981 && !_bfd_elf_size_group_sections (info))
6982 return false;
6983
6984 /* The backend may have to create some sections regardless of whether
6985 we're dynamic or not. */
6986 if (bed->elf_backend_always_size_sections
6987 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6988 return false;
6989
6990 /* Determine any GNU_STACK segment requirements, after the backend
6991 has had a chance to set a default segment size. */
6992 if (info->execstack)
6993 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6994 else if (info->noexecstack)
6995 elf_stack_flags (output_bfd) = PF_R | PF_W;
6996 else
6997 {
6998 bfd *inputobj;
6999 asection *notesec = NULL;
7000 int exec = 0;
7001
7002 for (inputobj = info->input_bfds;
7003 inputobj;
7004 inputobj = inputobj->link.next)
7005 {
7006 asection *s;
7007
7008 if (inputobj->flags
7009 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
7010 continue;
7011 s = inputobj->sections;
7012 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7013 continue;
7014
7015 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
7016 if (s)
7017 {
7018 if (s->flags & SEC_CODE)
7019 exec = PF_X;
7020 notesec = s;
7021 }
7022 else if (bed->default_execstack)
7023 exec = PF_X;
7024 }
7025 if (notesec || info->stacksize > 0)
7026 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
7027 if (notesec && exec && bfd_link_relocatable (info)
7028 && notesec->output_section != bfd_abs_section_ptr)
7029 notesec->output_section->flags |= SEC_CODE;
7030 }
7031
7032 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7033 {
7034 struct elf_info_failed eif;
7035 struct elf_link_hash_entry *h;
7036 asection *dynstr;
7037 asection *s;
7038
7039 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
7040 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
7041
7042 if (info->symbolic)
7043 {
7044 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
7045 return false;
7046 info->flags |= DF_SYMBOLIC;
7047 }
7048
7049 if (rpath != NULL)
7050 {
7051 size_t indx;
7052 bfd_vma tag;
7053
7054 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
7055 true);
7056 if (indx == (size_t) -1)
7057 return false;
7058
7059 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
7060 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
7061 return false;
7062 }
7063
7064 if (filter_shlib != NULL)
7065 {
7066 size_t indx;
7067
7068 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7069 filter_shlib, true);
7070 if (indx == (size_t) -1
7071 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
7072 return false;
7073 }
7074
7075 if (auxiliary_filters != NULL)
7076 {
7077 const char * const *p;
7078
7079 for (p = auxiliary_filters; *p != NULL; p++)
7080 {
7081 size_t indx;
7082
7083 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7084 *p, true);
7085 if (indx == (size_t) -1
7086 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
7087 return false;
7088 }
7089 }
7090
7091 if (audit != NULL)
7092 {
7093 size_t indx;
7094
7095 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
7096 true);
7097 if (indx == (size_t) -1
7098 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
7099 return false;
7100 }
7101
7102 if (depaudit != NULL)
7103 {
7104 size_t indx;
7105
7106 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
7107 true);
7108 if (indx == (size_t) -1
7109 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
7110 return false;
7111 }
7112
7113 eif.info = info;
7114 eif.failed = false;
7115
7116 /* Find all symbols which were defined in a dynamic object and make
7117 the backend pick a reasonable value for them. */
7118 elf_link_hash_traverse (elf_hash_table (info),
7119 _bfd_elf_adjust_dynamic_symbol,
7120 &eif);
7121 if (eif.failed)
7122 return false;
7123
7124 /* Add some entries to the .dynamic section. We fill in some of the
7125 values later, in bfd_elf_final_link, but we must add the entries
7126 now so that we know the final size of the .dynamic section. */
7127
7128 /* If there are initialization and/or finalization functions to
7129 call then add the corresponding DT_INIT/DT_FINI entries. */
7130 h = (info->init_function
7131 ? elf_link_hash_lookup (elf_hash_table (info),
7132 info->init_function, false,
7133 false, false)
7134 : NULL);
7135 if (h != NULL
7136 && (h->ref_regular
7137 || h->def_regular))
7138 {
7139 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
7140 return false;
7141 }
7142 h = (info->fini_function
7143 ? elf_link_hash_lookup (elf_hash_table (info),
7144 info->fini_function, false,
7145 false, false)
7146 : NULL);
7147 if (h != NULL
7148 && (h->ref_regular
7149 || h->def_regular))
7150 {
7151 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
7152 return false;
7153 }
7154
7155 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
7156 if (s != NULL && s->linker_has_input)
7157 {
7158 /* DT_PREINIT_ARRAY is not allowed in shared library. */
7159 if (! bfd_link_executable (info))
7160 {
7161 bfd *sub;
7162 asection *o;
7163
7164 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
7165 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
7166 && (o = sub->sections) != NULL
7167 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
7168 for (o = sub->sections; o != NULL; o = o->next)
7169 if (elf_section_data (o)->this_hdr.sh_type
7170 == SHT_PREINIT_ARRAY)
7171 {
7172 _bfd_error_handler
7173 (_("%pB: .preinit_array section is not allowed in DSO"),
7174 sub);
7175 break;
7176 }
7177
7178 bfd_set_error (bfd_error_nonrepresentable_section);
7179 return false;
7180 }
7181
7182 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
7183 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
7184 return false;
7185 }
7186 s = bfd_get_section_by_name (output_bfd, ".init_array");
7187 if (s != NULL && s->linker_has_input)
7188 {
7189 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
7190 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
7191 return false;
7192 }
7193 s = bfd_get_section_by_name (output_bfd, ".fini_array");
7194 if (s != NULL && s->linker_has_input)
7195 {
7196 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
7197 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
7198 return false;
7199 }
7200
7201 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
7202 /* If .dynstr is excluded from the link, we don't want any of
7203 these tags. Strictly, we should be checking each section
7204 individually; This quick check covers for the case where
7205 someone does a /DISCARD/ : { *(*) }. */
7206 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
7207 {
7208 bfd_size_type strsize;
7209
7210 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7211 if ((info->emit_hash
7212 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
7213 || (info->emit_gnu_hash
7214 && (bed->record_xhash_symbol == NULL
7215 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)))
7216 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
7217 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
7218 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
7219 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
7220 bed->s->sizeof_sym)
7221 || (info->gnu_flags_1
7222 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_FLAGS_1,
7223 info->gnu_flags_1)))
7224 return false;
7225 }
7226 }
7227
7228 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
7229 return false;
7230
7231 /* The backend must work out the sizes of all the other dynamic
7232 sections. */
7233 if (dynobj != NULL
7234 && bed->elf_backend_size_dynamic_sections != NULL
7235 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
7236 return false;
7237
7238 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7239 {
7240 if (elf_tdata (output_bfd)->cverdefs)
7241 {
7242 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
7243
7244 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
7245 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
7246 return false;
7247 }
7248
7249 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
7250 {
7251 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
7252 return false;
7253 }
7254 else if (info->flags & DF_BIND_NOW)
7255 {
7256 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
7257 return false;
7258 }
7259
7260 if (info->flags_1)
7261 {
7262 if (bfd_link_executable (info))
7263 info->flags_1 &= ~ (DF_1_INITFIRST
7264 | DF_1_NODELETE
7265 | DF_1_NOOPEN);
7266 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
7267 return false;
7268 }
7269
7270 if (elf_tdata (output_bfd)->cverrefs)
7271 {
7272 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
7273
7274 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
7275 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
7276 return false;
7277 }
7278
7279 if ((elf_tdata (output_bfd)->cverrefs == 0
7280 && elf_tdata (output_bfd)->cverdefs == 0)
7281 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
7282 {
7283 asection *s;
7284
7285 s = bfd_get_linker_section (dynobj, ".gnu.version");
7286 s->flags |= SEC_EXCLUDE;
7287 }
7288 }
7289 return true;
7290 }
7291
7292 /* Find the first non-excluded output section. We'll use its
7293 section symbol for some emitted relocs. */
7294 void
7295 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7296 {
7297 asection *s;
7298 asection *found = NULL;
7299
7300 for (s = output_bfd->sections; s != NULL; s = s->next)
7301 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7302 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7303 {
7304 found = s;
7305 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7306 break;
7307 }
7308 elf_hash_table (info)->text_index_section = found;
7309 }
7310
7311 /* Find two non-excluded output sections, one for code, one for data.
7312 We'll use their section symbols for some emitted relocs. */
7313 void
7314 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7315 {
7316 asection *s;
7317 asection *found = NULL;
7318
7319 /* Data first, since setting text_index_section changes
7320 _bfd_elf_omit_section_dynsym_default. */
7321 for (s = output_bfd->sections; s != NULL; s = s->next)
7322 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7323 && !(s->flags & SEC_READONLY)
7324 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7325 {
7326 found = s;
7327 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7328 break;
7329 }
7330 elf_hash_table (info)->data_index_section = found;
7331
7332 for (s = output_bfd->sections; s != NULL; s = s->next)
7333 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7334 && (s->flags & SEC_READONLY)
7335 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7336 {
7337 found = s;
7338 break;
7339 }
7340 elf_hash_table (info)->text_index_section = found;
7341 }
7342
7343 #define GNU_HASH_SECTION_NAME(bed) \
7344 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7345
7346 bool
7347 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7348 {
7349 const struct elf_backend_data *bed;
7350 unsigned long section_sym_count;
7351 bfd_size_type dynsymcount = 0;
7352
7353 if (!is_elf_hash_table (info->hash))
7354 return true;
7355
7356 bed = get_elf_backend_data (output_bfd);
7357 (*bed->elf_backend_init_index_section) (output_bfd, info);
7358
7359 /* Assign dynsym indices. In a shared library we generate a section
7360 symbol for each output section, which come first. Next come all
7361 of the back-end allocated local dynamic syms, followed by the rest
7362 of the global symbols.
7363
7364 This is usually not needed for static binaries, however backends
7365 can request to always do it, e.g. the MIPS backend uses dynamic
7366 symbol counts to lay out GOT, which will be produced in the
7367 presence of GOT relocations even in static binaries (holding fixed
7368 data in that case, to satisfy those relocations). */
7369
7370 if (elf_hash_table (info)->dynamic_sections_created
7371 || bed->always_renumber_dynsyms)
7372 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7373 &section_sym_count);
7374
7375 if (elf_hash_table (info)->dynamic_sections_created)
7376 {
7377 bfd *dynobj;
7378 asection *s;
7379 unsigned int dtagcount;
7380
7381 dynobj = elf_hash_table (info)->dynobj;
7382
7383 /* Work out the size of the symbol version section. */
7384 s = bfd_get_linker_section (dynobj, ".gnu.version");
7385 BFD_ASSERT (s != NULL);
7386 if ((s->flags & SEC_EXCLUDE) == 0)
7387 {
7388 s->size = dynsymcount * sizeof (Elf_External_Versym);
7389 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7390 if (s->contents == NULL)
7391 return false;
7392
7393 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7394 return false;
7395 }
7396
7397 /* Set the size of the .dynsym and .hash sections. We counted
7398 the number of dynamic symbols in elf_link_add_object_symbols.
7399 We will build the contents of .dynsym and .hash when we build
7400 the final symbol table, because until then we do not know the
7401 correct value to give the symbols. We built the .dynstr
7402 section as we went along in elf_link_add_object_symbols. */
7403 s = elf_hash_table (info)->dynsym;
7404 BFD_ASSERT (s != NULL);
7405 s->size = dynsymcount * bed->s->sizeof_sym;
7406
7407 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7408 if (s->contents == NULL)
7409 return false;
7410
7411 /* The first entry in .dynsym is a dummy symbol. Clear all the
7412 section syms, in case we don't output them all. */
7413 ++section_sym_count;
7414 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
7415
7416 elf_hash_table (info)->bucketcount = 0;
7417
7418 /* Compute the size of the hashing table. As a side effect this
7419 computes the hash values for all the names we export. */
7420 if (info->emit_hash)
7421 {
7422 unsigned long int *hashcodes;
7423 struct hash_codes_info hashinf;
7424 bfd_size_type amt;
7425 unsigned long int nsyms;
7426 size_t bucketcount;
7427 size_t hash_entry_size;
7428
7429 /* Compute the hash values for all exported symbols. At the same
7430 time store the values in an array so that we could use them for
7431 optimizations. */
7432 amt = dynsymcount * sizeof (unsigned long int);
7433 hashcodes = (unsigned long int *) bfd_malloc (amt);
7434 if (hashcodes == NULL)
7435 return false;
7436 hashinf.hashcodes = hashcodes;
7437 hashinf.error = false;
7438
7439 /* Put all hash values in HASHCODES. */
7440 elf_link_hash_traverse (elf_hash_table (info),
7441 elf_collect_hash_codes, &hashinf);
7442 if (hashinf.error)
7443 {
7444 free (hashcodes);
7445 return false;
7446 }
7447
7448 nsyms = hashinf.hashcodes - hashcodes;
7449 bucketcount
7450 = compute_bucket_count (info, hashcodes, nsyms, 0);
7451 free (hashcodes);
7452
7453 if (bucketcount == 0 && nsyms > 0)
7454 return false;
7455
7456 elf_hash_table (info)->bucketcount = bucketcount;
7457
7458 s = bfd_get_linker_section (dynobj, ".hash");
7459 BFD_ASSERT (s != NULL);
7460 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7461 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
7462 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7463 if (s->contents == NULL)
7464 return false;
7465
7466 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7467 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7468 s->contents + hash_entry_size);
7469 }
7470
7471 if (info->emit_gnu_hash)
7472 {
7473 size_t i, cnt;
7474 unsigned char *contents;
7475 struct collect_gnu_hash_codes cinfo;
7476 bfd_size_type amt;
7477 size_t bucketcount;
7478
7479 memset (&cinfo, 0, sizeof (cinfo));
7480
7481 /* Compute the hash values for all exported symbols. At the same
7482 time store the values in an array so that we could use them for
7483 optimizations. */
7484 amt = dynsymcount * 2 * sizeof (unsigned long int);
7485 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
7486 if (cinfo.hashcodes == NULL)
7487 return false;
7488
7489 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7490 cinfo.min_dynindx = -1;
7491 cinfo.output_bfd = output_bfd;
7492 cinfo.bed = bed;
7493
7494 /* Put all hash values in HASHCODES. */
7495 elf_link_hash_traverse (elf_hash_table (info),
7496 elf_collect_gnu_hash_codes, &cinfo);
7497 if (cinfo.error)
7498 {
7499 free (cinfo.hashcodes);
7500 return false;
7501 }
7502
7503 bucketcount
7504 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7505
7506 if (bucketcount == 0)
7507 {
7508 free (cinfo.hashcodes);
7509 return false;
7510 }
7511
7512 s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (bed));
7513 BFD_ASSERT (s != NULL);
7514
7515 if (cinfo.nsyms == 0)
7516 {
7517 /* Empty .gnu.hash or .MIPS.xhash section is special. */
7518 BFD_ASSERT (cinfo.min_dynindx == -1);
7519 free (cinfo.hashcodes);
7520 s->size = 5 * 4 + bed->s->arch_size / 8;
7521 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7522 if (contents == NULL)
7523 return false;
7524 s->contents = contents;
7525 /* 1 empty bucket. */
7526 bfd_put_32 (output_bfd, 1, contents);
7527 /* SYMIDX above the special symbol 0. */
7528 bfd_put_32 (output_bfd, 1, contents + 4);
7529 /* Just one word for bitmask. */
7530 bfd_put_32 (output_bfd, 1, contents + 8);
7531 /* Only hash fn bloom filter. */
7532 bfd_put_32 (output_bfd, 0, contents + 12);
7533 /* No hashes are valid - empty bitmask. */
7534 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7535 /* No hashes in the only bucket. */
7536 bfd_put_32 (output_bfd, 0,
7537 contents + 16 + bed->s->arch_size / 8);
7538 }
7539 else
7540 {
7541 unsigned long int maskwords, maskbitslog2, x;
7542 BFD_ASSERT (cinfo.min_dynindx != -1);
7543
7544 x = cinfo.nsyms;
7545 maskbitslog2 = 1;
7546 while ((x >>= 1) != 0)
7547 ++maskbitslog2;
7548 if (maskbitslog2 < 3)
7549 maskbitslog2 = 5;
7550 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7551 maskbitslog2 = maskbitslog2 + 3;
7552 else
7553 maskbitslog2 = maskbitslog2 + 2;
7554 if (bed->s->arch_size == 64)
7555 {
7556 if (maskbitslog2 == 5)
7557 maskbitslog2 = 6;
7558 cinfo.shift1 = 6;
7559 }
7560 else
7561 cinfo.shift1 = 5;
7562 cinfo.mask = (1 << cinfo.shift1) - 1;
7563 cinfo.shift2 = maskbitslog2;
7564 cinfo.maskbits = 1 << maskbitslog2;
7565 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7566 amt = bucketcount * sizeof (unsigned long int) * 2;
7567 amt += maskwords * sizeof (bfd_vma);
7568 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
7569 if (cinfo.bitmask == NULL)
7570 {
7571 free (cinfo.hashcodes);
7572 return false;
7573 }
7574
7575 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
7576 cinfo.indx = cinfo.counts + bucketcount;
7577 cinfo.symindx = dynsymcount - cinfo.nsyms;
7578 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7579
7580 /* Determine how often each hash bucket is used. */
7581 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7582 for (i = 0; i < cinfo.nsyms; ++i)
7583 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7584
7585 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7586 if (cinfo.counts[i] != 0)
7587 {
7588 cinfo.indx[i] = cnt;
7589 cnt += cinfo.counts[i];
7590 }
7591 BFD_ASSERT (cnt == dynsymcount);
7592 cinfo.bucketcount = bucketcount;
7593 cinfo.local_indx = cinfo.min_dynindx;
7594
7595 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7596 s->size += cinfo.maskbits / 8;
7597 if (bed->record_xhash_symbol != NULL)
7598 s->size += cinfo.nsyms * 4;
7599 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7600 if (contents == NULL)
7601 {
7602 free (cinfo.bitmask);
7603 free (cinfo.hashcodes);
7604 return false;
7605 }
7606
7607 s->contents = contents;
7608 bfd_put_32 (output_bfd, bucketcount, contents);
7609 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7610 bfd_put_32 (output_bfd, maskwords, contents + 8);
7611 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7612 contents += 16 + cinfo.maskbits / 8;
7613
7614 for (i = 0; i < bucketcount; ++i)
7615 {
7616 if (cinfo.counts[i] == 0)
7617 bfd_put_32 (output_bfd, 0, contents);
7618 else
7619 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7620 contents += 4;
7621 }
7622
7623 cinfo.contents = contents;
7624
7625 cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents;
7626 /* Renumber dynamic symbols, if populating .gnu.hash section.
7627 If using .MIPS.xhash, populate the translation table. */
7628 elf_link_hash_traverse (elf_hash_table (info),
7629 elf_gnu_hash_process_symidx, &cinfo);
7630
7631 contents = s->contents + 16;
7632 for (i = 0; i < maskwords; ++i)
7633 {
7634 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7635 contents);
7636 contents += bed->s->arch_size / 8;
7637 }
7638
7639 free (cinfo.bitmask);
7640 free (cinfo.hashcodes);
7641 }
7642 }
7643
7644 s = bfd_get_linker_section (dynobj, ".dynstr");
7645 BFD_ASSERT (s != NULL);
7646
7647 elf_finalize_dynstr (output_bfd, info);
7648
7649 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7650
7651 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7652 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7653 return false;
7654 }
7655
7656 return true;
7657 }
7658 \f
7659 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7660
7661 static void
7662 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7663 asection *sec)
7664 {
7665 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7666 sec->sec_info_type = SEC_INFO_TYPE_NONE;
7667 }
7668
7669 /* Finish SHF_MERGE section merging. */
7670
7671 bool
7672 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
7673 {
7674 bfd *ibfd;
7675 asection *sec;
7676
7677 if (!is_elf_hash_table (info->hash))
7678 return false;
7679
7680 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7681 if ((ibfd->flags & DYNAMIC) == 0
7682 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
7683 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7684 == get_elf_backend_data (obfd)->s->elfclass))
7685 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7686 if ((sec->flags & SEC_MERGE) != 0
7687 && !bfd_is_abs_section (sec->output_section))
7688 {
7689 struct bfd_elf_section_data *secdata;
7690
7691 secdata = elf_section_data (sec);
7692 if (! _bfd_add_merge_section (obfd,
7693 &elf_hash_table (info)->merge_info,
7694 sec, &secdata->sec_info))
7695 return false;
7696 else if (secdata->sec_info)
7697 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
7698 }
7699
7700 if (elf_hash_table (info)->merge_info != NULL)
7701 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
7702 merge_sections_remove_hook);
7703 return true;
7704 }
7705
7706 /* Create an entry in an ELF linker hash table. */
7707
7708 struct bfd_hash_entry *
7709 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7710 struct bfd_hash_table *table,
7711 const char *string)
7712 {
7713 /* Allocate the structure if it has not already been allocated by a
7714 subclass. */
7715 if (entry == NULL)
7716 {
7717 entry = (struct bfd_hash_entry *)
7718 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
7719 if (entry == NULL)
7720 return entry;
7721 }
7722
7723 /* Call the allocation method of the superclass. */
7724 entry = _bfd_link_hash_newfunc (entry, table, string);
7725 if (entry != NULL)
7726 {
7727 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7728 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7729
7730 /* Set local fields. */
7731 ret->indx = -1;
7732 ret->dynindx = -1;
7733 ret->got = htab->init_got_refcount;
7734 ret->plt = htab->init_plt_refcount;
7735 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7736 - offsetof (struct elf_link_hash_entry, size)));
7737 /* Assume that we have been called by a non-ELF symbol reader.
7738 This flag is then reset by the code which reads an ELF input
7739 file. This ensures that a symbol created by a non-ELF symbol
7740 reader will have the flag set correctly. */
7741 ret->non_elf = 1;
7742 }
7743
7744 return entry;
7745 }
7746
7747 /* Copy data from an indirect symbol to its direct symbol, hiding the
7748 old indirect symbol. Also used for copying flags to a weakdef. */
7749
7750 void
7751 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7752 struct elf_link_hash_entry *dir,
7753 struct elf_link_hash_entry *ind)
7754 {
7755 struct elf_link_hash_table *htab;
7756
7757 if (ind->dyn_relocs != NULL)
7758 {
7759 if (dir->dyn_relocs != NULL)
7760 {
7761 struct elf_dyn_relocs **pp;
7762 struct elf_dyn_relocs *p;
7763
7764 /* Add reloc counts against the indirect sym to the direct sym
7765 list. Merge any entries against the same section. */
7766 for (pp = &ind->dyn_relocs; (p = *pp) != NULL; )
7767 {
7768 struct elf_dyn_relocs *q;
7769
7770 for (q = dir->dyn_relocs; q != NULL; q = q->next)
7771 if (q->sec == p->sec)
7772 {
7773 q->pc_count += p->pc_count;
7774 q->count += p->count;
7775 *pp = p->next;
7776 break;
7777 }
7778 if (q == NULL)
7779 pp = &p->next;
7780 }
7781 *pp = dir->dyn_relocs;
7782 }
7783
7784 dir->dyn_relocs = ind->dyn_relocs;
7785 ind->dyn_relocs = NULL;
7786 }
7787
7788 /* Copy down any references that we may have already seen to the
7789 symbol which just became indirect. */
7790
7791 if (dir->versioned != versioned_hidden)
7792 dir->ref_dynamic |= ind->ref_dynamic;
7793 dir->ref_regular |= ind->ref_regular;
7794 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7795 dir->non_got_ref |= ind->non_got_ref;
7796 dir->needs_plt |= ind->needs_plt;
7797 dir->pointer_equality_needed |= ind->pointer_equality_needed;
7798
7799 if (ind->root.type != bfd_link_hash_indirect)
7800 return;
7801
7802 /* Copy over the global and procedure linkage table refcount entries.
7803 These may have been already set up by a check_relocs routine. */
7804 htab = elf_hash_table (info);
7805 if (ind->got.refcount > htab->init_got_refcount.refcount)
7806 {
7807 if (dir->got.refcount < 0)
7808 dir->got.refcount = 0;
7809 dir->got.refcount += ind->got.refcount;
7810 ind->got.refcount = htab->init_got_refcount.refcount;
7811 }
7812
7813 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7814 {
7815 if (dir->plt.refcount < 0)
7816 dir->plt.refcount = 0;
7817 dir->plt.refcount += ind->plt.refcount;
7818 ind->plt.refcount = htab->init_plt_refcount.refcount;
7819 }
7820
7821 if (ind->dynindx != -1)
7822 {
7823 if (dir->dynindx != -1)
7824 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7825 dir->dynindx = ind->dynindx;
7826 dir->dynstr_index = ind->dynstr_index;
7827 ind->dynindx = -1;
7828 ind->dynstr_index = 0;
7829 }
7830 }
7831
7832 void
7833 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7834 struct elf_link_hash_entry *h,
7835 bool force_local)
7836 {
7837 /* STT_GNU_IFUNC symbol must go through PLT. */
7838 if (h->type != STT_GNU_IFUNC)
7839 {
7840 h->plt = elf_hash_table (info)->init_plt_offset;
7841 h->needs_plt = 0;
7842 }
7843 if (force_local)
7844 {
7845 h->forced_local = 1;
7846 if (h->dynindx != -1)
7847 {
7848 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7849 h->dynstr_index);
7850 h->dynindx = -1;
7851 h->dynstr_index = 0;
7852 }
7853 }
7854 }
7855
7856 /* Hide a symbol. */
7857
7858 void
7859 _bfd_elf_link_hide_symbol (bfd *output_bfd,
7860 struct bfd_link_info *info,
7861 struct bfd_link_hash_entry *h)
7862 {
7863 if (is_elf_hash_table (info->hash))
7864 {
7865 const struct elf_backend_data *bed
7866 = get_elf_backend_data (output_bfd);
7867 struct elf_link_hash_entry *eh
7868 = (struct elf_link_hash_entry *) h;
7869 bed->elf_backend_hide_symbol (info, eh, true);
7870 eh->def_dynamic = 0;
7871 eh->ref_dynamic = 0;
7872 eh->dynamic_def = 0;
7873 }
7874 }
7875
7876 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7877 caller. */
7878
7879 bool
7880 _bfd_elf_link_hash_table_init
7881 (struct elf_link_hash_table *table,
7882 bfd *abfd,
7883 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7884 struct bfd_hash_table *,
7885 const char *),
7886 unsigned int entsize,
7887 enum elf_target_id target_id)
7888 {
7889 bool ret;
7890 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7891
7892 table->init_got_refcount.refcount = can_refcount - 1;
7893 table->init_plt_refcount.refcount = can_refcount - 1;
7894 table->init_got_offset.offset = -(bfd_vma) 1;
7895 table->init_plt_offset.offset = -(bfd_vma) 1;
7896 /* The first dynamic symbol is a dummy. */
7897 table->dynsymcount = 1;
7898
7899 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
7900
7901 table->root.type = bfd_link_elf_hash_table;
7902 table->hash_table_id = target_id;
7903 table->target_os = get_elf_backend_data (abfd)->target_os;
7904
7905 return ret;
7906 }
7907
7908 /* Create an ELF linker hash table. */
7909
7910 struct bfd_link_hash_table *
7911 _bfd_elf_link_hash_table_create (bfd *abfd)
7912 {
7913 struct elf_link_hash_table *ret;
7914 size_t amt = sizeof (struct elf_link_hash_table);
7915
7916 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
7917 if (ret == NULL)
7918 return NULL;
7919
7920 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
7921 sizeof (struct elf_link_hash_entry),
7922 GENERIC_ELF_DATA))
7923 {
7924 free (ret);
7925 return NULL;
7926 }
7927 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
7928
7929 return &ret->root;
7930 }
7931
7932 /* Destroy an ELF linker hash table. */
7933
7934 void
7935 _bfd_elf_link_hash_table_free (bfd *obfd)
7936 {
7937 struct elf_link_hash_table *htab;
7938
7939 htab = (struct elf_link_hash_table *) obfd->link.hash;
7940 if (htab->dynstr != NULL)
7941 _bfd_elf_strtab_free (htab->dynstr);
7942 _bfd_merge_sections_free (htab->merge_info);
7943 _bfd_generic_link_hash_table_free (obfd);
7944 }
7945
7946 /* This is a hook for the ELF emulation code in the generic linker to
7947 tell the backend linker what file name to use for the DT_NEEDED
7948 entry for a dynamic object. */
7949
7950 void
7951 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7952 {
7953 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7954 && bfd_get_format (abfd) == bfd_object)
7955 elf_dt_name (abfd) = name;
7956 }
7957
7958 int
7959 bfd_elf_get_dyn_lib_class (bfd *abfd)
7960 {
7961 int lib_class;
7962 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7963 && bfd_get_format (abfd) == bfd_object)
7964 lib_class = elf_dyn_lib_class (abfd);
7965 else
7966 lib_class = 0;
7967 return lib_class;
7968 }
7969
7970 void
7971 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7972 {
7973 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7974 && bfd_get_format (abfd) == bfd_object)
7975 elf_dyn_lib_class (abfd) = lib_class;
7976 }
7977
7978 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7979 the linker ELF emulation code. */
7980
7981 struct bfd_link_needed_list *
7982 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7983 struct bfd_link_info *info)
7984 {
7985 if (! is_elf_hash_table (info->hash))
7986 return NULL;
7987 return elf_hash_table (info)->needed;
7988 }
7989
7990 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7991 hook for the linker ELF emulation code. */
7992
7993 struct bfd_link_needed_list *
7994 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7995 struct bfd_link_info *info)
7996 {
7997 if (! is_elf_hash_table (info->hash))
7998 return NULL;
7999 return elf_hash_table (info)->runpath;
8000 }
8001
8002 /* Get the name actually used for a dynamic object for a link. This
8003 is the SONAME entry if there is one. Otherwise, it is the string
8004 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
8005
8006 const char *
8007 bfd_elf_get_dt_soname (bfd *abfd)
8008 {
8009 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8010 && bfd_get_format (abfd) == bfd_object)
8011 return elf_dt_name (abfd);
8012 return NULL;
8013 }
8014
8015 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
8016 the ELF linker emulation code. */
8017
8018 bool
8019 bfd_elf_get_bfd_needed_list (bfd *abfd,
8020 struct bfd_link_needed_list **pneeded)
8021 {
8022 asection *s;
8023 bfd_byte *dynbuf = NULL;
8024 unsigned int elfsec;
8025 unsigned long shlink;
8026 bfd_byte *extdyn, *extdynend;
8027 size_t extdynsize;
8028 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
8029
8030 *pneeded = NULL;
8031
8032 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
8033 || bfd_get_format (abfd) != bfd_object)
8034 return true;
8035
8036 s = bfd_get_section_by_name (abfd, ".dynamic");
8037 if (s == NULL || s->size == 0)
8038 return true;
8039
8040 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
8041 goto error_return;
8042
8043 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
8044 if (elfsec == SHN_BAD)
8045 goto error_return;
8046
8047 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
8048
8049 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
8050 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
8051
8052 extdyn = dynbuf;
8053 extdynend = extdyn + s->size;
8054 for (; extdyn < extdynend; extdyn += extdynsize)
8055 {
8056 Elf_Internal_Dyn dyn;
8057
8058 (*swap_dyn_in) (abfd, extdyn, &dyn);
8059
8060 if (dyn.d_tag == DT_NULL)
8061 break;
8062
8063 if (dyn.d_tag == DT_NEEDED)
8064 {
8065 const char *string;
8066 struct bfd_link_needed_list *l;
8067 unsigned int tagv = dyn.d_un.d_val;
8068 size_t amt;
8069
8070 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
8071 if (string == NULL)
8072 goto error_return;
8073
8074 amt = sizeof *l;
8075 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
8076 if (l == NULL)
8077 goto error_return;
8078
8079 l->by = abfd;
8080 l->name = string;
8081 l->next = *pneeded;
8082 *pneeded = l;
8083 }
8084 }
8085
8086 free (dynbuf);
8087
8088 return true;
8089
8090 error_return:
8091 free (dynbuf);
8092 return false;
8093 }
8094
8095 struct elf_symbuf_symbol
8096 {
8097 unsigned long st_name; /* Symbol name, index in string tbl */
8098 unsigned char st_info; /* Type and binding attributes */
8099 unsigned char st_other; /* Visibilty, and target specific */
8100 };
8101
8102 struct elf_symbuf_head
8103 {
8104 struct elf_symbuf_symbol *ssym;
8105 size_t count;
8106 unsigned int st_shndx;
8107 };
8108
8109 struct elf_symbol
8110 {
8111 union
8112 {
8113 Elf_Internal_Sym *isym;
8114 struct elf_symbuf_symbol *ssym;
8115 void *p;
8116 } u;
8117 const char *name;
8118 };
8119
8120 /* Sort references to symbols by ascending section number. */
8121
8122 static int
8123 elf_sort_elf_symbol (const void *arg1, const void *arg2)
8124 {
8125 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
8126 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
8127
8128 if (s1->st_shndx != s2->st_shndx)
8129 return s1->st_shndx > s2->st_shndx ? 1 : -1;
8130 /* Final sort by the address of the sym in the symbuf ensures
8131 a stable sort. */
8132 if (s1 != s2)
8133 return s1 > s2 ? 1 : -1;
8134 return 0;
8135 }
8136
8137 static int
8138 elf_sym_name_compare (const void *arg1, const void *arg2)
8139 {
8140 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
8141 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
8142 int ret = strcmp (s1->name, s2->name);
8143 if (ret != 0)
8144 return ret;
8145 if (s1->u.p != s2->u.p)
8146 return s1->u.p > s2->u.p ? 1 : -1;
8147 return 0;
8148 }
8149
8150 static struct elf_symbuf_head *
8151 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
8152 {
8153 Elf_Internal_Sym **ind, **indbufend, **indbuf;
8154 struct elf_symbuf_symbol *ssym;
8155 struct elf_symbuf_head *ssymbuf, *ssymhead;
8156 size_t i, shndx_count, total_size, amt;
8157
8158 amt = symcount * sizeof (*indbuf);
8159 indbuf = (Elf_Internal_Sym **) bfd_malloc (amt);
8160 if (indbuf == NULL)
8161 return NULL;
8162
8163 for (ind = indbuf, i = 0; i < symcount; i++)
8164 if (isymbuf[i].st_shndx != SHN_UNDEF)
8165 *ind++ = &isymbuf[i];
8166 indbufend = ind;
8167
8168 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
8169 elf_sort_elf_symbol);
8170
8171 shndx_count = 0;
8172 if (indbufend > indbuf)
8173 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
8174 if (ind[0]->st_shndx != ind[1]->st_shndx)
8175 shndx_count++;
8176
8177 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
8178 + (indbufend - indbuf) * sizeof (*ssym));
8179 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
8180 if (ssymbuf == NULL)
8181 {
8182 free (indbuf);
8183 return NULL;
8184 }
8185
8186 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
8187 ssymbuf->ssym = NULL;
8188 ssymbuf->count = shndx_count;
8189 ssymbuf->st_shndx = 0;
8190 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
8191 {
8192 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
8193 {
8194 ssymhead++;
8195 ssymhead->ssym = ssym;
8196 ssymhead->count = 0;
8197 ssymhead->st_shndx = (*ind)->st_shndx;
8198 }
8199 ssym->st_name = (*ind)->st_name;
8200 ssym->st_info = (*ind)->st_info;
8201 ssym->st_other = (*ind)->st_other;
8202 ssymhead->count++;
8203 }
8204 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
8205 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
8206 == total_size));
8207
8208 free (indbuf);
8209 return ssymbuf;
8210 }
8211
8212 /* Check if 2 sections define the same set of local and global
8213 symbols. */
8214
8215 static bool
8216 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
8217 struct bfd_link_info *info)
8218 {
8219 bfd *bfd1, *bfd2;
8220 const struct elf_backend_data *bed1, *bed2;
8221 Elf_Internal_Shdr *hdr1, *hdr2;
8222 size_t symcount1, symcount2;
8223 Elf_Internal_Sym *isymbuf1, *isymbuf2;
8224 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
8225 Elf_Internal_Sym *isym, *isymend;
8226 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
8227 size_t count1, count2, sec_count1, sec_count2, i;
8228 unsigned int shndx1, shndx2;
8229 bool result;
8230 bool ignore_section_symbol_p;
8231
8232 bfd1 = sec1->owner;
8233 bfd2 = sec2->owner;
8234
8235 /* Both sections have to be in ELF. */
8236 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
8237 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
8238 return false;
8239
8240 if (elf_section_type (sec1) != elf_section_type (sec2))
8241 return false;
8242
8243 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
8244 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
8245 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
8246 return false;
8247
8248 bed1 = get_elf_backend_data (bfd1);
8249 bed2 = get_elf_backend_data (bfd2);
8250 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
8251 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
8252 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
8253 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
8254
8255 if (symcount1 == 0 || symcount2 == 0)
8256 return false;
8257
8258 result = false;
8259 isymbuf1 = NULL;
8260 isymbuf2 = NULL;
8261 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
8262 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
8263
8264 /* Ignore section symbols only when matching non-debugging sections
8265 or linkonce section with comdat section. */
8266 ignore_section_symbol_p
8267 = ((sec1->flags & SEC_DEBUGGING) == 0
8268 || ((elf_section_flags (sec1) & SHF_GROUP)
8269 != (elf_section_flags (sec2) & SHF_GROUP)));
8270
8271 if (ssymbuf1 == NULL)
8272 {
8273 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
8274 NULL, NULL, NULL);
8275 if (isymbuf1 == NULL)
8276 goto done;
8277
8278 if (info != NULL && !info->reduce_memory_overheads)
8279 {
8280 ssymbuf1 = elf_create_symbuf (symcount1, isymbuf1);
8281 elf_tdata (bfd1)->symbuf = ssymbuf1;
8282 }
8283 }
8284
8285 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
8286 {
8287 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
8288 NULL, NULL, NULL);
8289 if (isymbuf2 == NULL)
8290 goto done;
8291
8292 if (ssymbuf1 != NULL && info != NULL && !info->reduce_memory_overheads)
8293 {
8294 ssymbuf2 = elf_create_symbuf (symcount2, isymbuf2);
8295 elf_tdata (bfd2)->symbuf = ssymbuf2;
8296 }
8297 }
8298
8299 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
8300 {
8301 /* Optimized faster version. */
8302 size_t lo, hi, mid;
8303 struct elf_symbol *symp;
8304 struct elf_symbuf_symbol *ssym, *ssymend;
8305
8306 lo = 0;
8307 hi = ssymbuf1->count;
8308 ssymbuf1++;
8309 count1 = 0;
8310 sec_count1 = 0;
8311 while (lo < hi)
8312 {
8313 mid = (lo + hi) / 2;
8314 if (shndx1 < ssymbuf1[mid].st_shndx)
8315 hi = mid;
8316 else if (shndx1 > ssymbuf1[mid].st_shndx)
8317 lo = mid + 1;
8318 else
8319 {
8320 count1 = ssymbuf1[mid].count;
8321 ssymbuf1 += mid;
8322 break;
8323 }
8324 }
8325 if (ignore_section_symbol_p)
8326 {
8327 for (i = 0; i < count1; i++)
8328 if (ELF_ST_TYPE (ssymbuf1->ssym[i].st_info) == STT_SECTION)
8329 sec_count1++;
8330 count1 -= sec_count1;
8331 }
8332
8333 lo = 0;
8334 hi = ssymbuf2->count;
8335 ssymbuf2++;
8336 count2 = 0;
8337 sec_count2 = 0;
8338 while (lo < hi)
8339 {
8340 mid = (lo + hi) / 2;
8341 if (shndx2 < ssymbuf2[mid].st_shndx)
8342 hi = mid;
8343 else if (shndx2 > ssymbuf2[mid].st_shndx)
8344 lo = mid + 1;
8345 else
8346 {
8347 count2 = ssymbuf2[mid].count;
8348 ssymbuf2 += mid;
8349 break;
8350 }
8351 }
8352 if (ignore_section_symbol_p)
8353 {
8354 for (i = 0; i < count2; i++)
8355 if (ELF_ST_TYPE (ssymbuf2->ssym[i].st_info) == STT_SECTION)
8356 sec_count2++;
8357 count2 -= sec_count2;
8358 }
8359
8360 if (count1 == 0 || count2 == 0 || count1 != count2)
8361 goto done;
8362
8363 symtable1
8364 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
8365 symtable2
8366 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
8367 if (symtable1 == NULL || symtable2 == NULL)
8368 goto done;
8369
8370 symp = symtable1;
8371 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1 + sec_count1;
8372 ssym < ssymend; ssym++)
8373 if (sec_count1 == 0
8374 || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
8375 {
8376 symp->u.ssym = ssym;
8377 symp->name = bfd_elf_string_from_elf_section (bfd1,
8378 hdr1->sh_link,
8379 ssym->st_name);
8380 symp++;
8381 }
8382
8383 symp = symtable2;
8384 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2 + sec_count2;
8385 ssym < ssymend; ssym++)
8386 if (sec_count2 == 0
8387 || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
8388 {
8389 symp->u.ssym = ssym;
8390 symp->name = bfd_elf_string_from_elf_section (bfd2,
8391 hdr2->sh_link,
8392 ssym->st_name);
8393 symp++;
8394 }
8395
8396 /* Sort symbol by name. */
8397 qsort (symtable1, count1, sizeof (struct elf_symbol),
8398 elf_sym_name_compare);
8399 qsort (symtable2, count1, sizeof (struct elf_symbol),
8400 elf_sym_name_compare);
8401
8402 for (i = 0; i < count1; i++)
8403 /* Two symbols must have the same binding, type and name. */
8404 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8405 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8406 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8407 goto done;
8408
8409 result = true;
8410 goto done;
8411 }
8412
8413 symtable1 = (struct elf_symbol *)
8414 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8415 symtable2 = (struct elf_symbol *)
8416 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
8417 if (symtable1 == NULL || symtable2 == NULL)
8418 goto done;
8419
8420 /* Count definitions in the section. */
8421 count1 = 0;
8422 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
8423 if (isym->st_shndx == shndx1
8424 && (!ignore_section_symbol_p
8425 || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
8426 symtable1[count1++].u.isym = isym;
8427
8428 count2 = 0;
8429 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
8430 if (isym->st_shndx == shndx2
8431 && (!ignore_section_symbol_p
8432 || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
8433 symtable2[count2++].u.isym = isym;
8434
8435 if (count1 == 0 || count2 == 0 || count1 != count2)
8436 goto done;
8437
8438 for (i = 0; i < count1; i++)
8439 symtable1[i].name
8440 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8441 symtable1[i].u.isym->st_name);
8442
8443 for (i = 0; i < count2; i++)
8444 symtable2[i].name
8445 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8446 symtable2[i].u.isym->st_name);
8447
8448 /* Sort symbol by name. */
8449 qsort (symtable1, count1, sizeof (struct elf_symbol),
8450 elf_sym_name_compare);
8451 qsort (symtable2, count1, sizeof (struct elf_symbol),
8452 elf_sym_name_compare);
8453
8454 for (i = 0; i < count1; i++)
8455 /* Two symbols must have the same binding, type and name. */
8456 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8457 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8458 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8459 goto done;
8460
8461 result = true;
8462
8463 done:
8464 free (symtable1);
8465 free (symtable2);
8466 free (isymbuf1);
8467 free (isymbuf2);
8468
8469 return result;
8470 }
8471
8472 /* Return TRUE if 2 section types are compatible. */
8473
8474 bool
8475 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8476 bfd *bbfd, const asection *bsec)
8477 {
8478 if (asec == NULL
8479 || bsec == NULL
8480 || abfd->xvec->flavour != bfd_target_elf_flavour
8481 || bbfd->xvec->flavour != bfd_target_elf_flavour)
8482 return true;
8483
8484 return elf_section_type (asec) == elf_section_type (bsec);
8485 }
8486 \f
8487 /* Final phase of ELF linker. */
8488
8489 /* A structure we use to avoid passing large numbers of arguments. */
8490
8491 struct elf_final_link_info
8492 {
8493 /* General link information. */
8494 struct bfd_link_info *info;
8495 /* Output BFD. */
8496 bfd *output_bfd;
8497 /* Symbol string table. */
8498 struct elf_strtab_hash *symstrtab;
8499 /* .hash section. */
8500 asection *hash_sec;
8501 /* symbol version section (.gnu.version). */
8502 asection *symver_sec;
8503 /* Buffer large enough to hold contents of any section. */
8504 bfd_byte *contents;
8505 /* Buffer large enough to hold external relocs of any section. */
8506 void *external_relocs;
8507 /* Buffer large enough to hold internal relocs of any section. */
8508 Elf_Internal_Rela *internal_relocs;
8509 /* Buffer large enough to hold external local symbols of any input
8510 BFD. */
8511 bfd_byte *external_syms;
8512 /* And a buffer for symbol section indices. */
8513 Elf_External_Sym_Shndx *locsym_shndx;
8514 /* Buffer large enough to hold internal local symbols of any input
8515 BFD. */
8516 Elf_Internal_Sym *internal_syms;
8517 /* Array large enough to hold a symbol index for each local symbol
8518 of any input BFD. */
8519 long *indices;
8520 /* Array large enough to hold a section pointer for each local
8521 symbol of any input BFD. */
8522 asection **sections;
8523 /* Buffer for SHT_SYMTAB_SHNDX section. */
8524 Elf_External_Sym_Shndx *symshndxbuf;
8525 /* Number of STT_FILE syms seen. */
8526 size_t filesym_count;
8527 /* Local symbol hash table. */
8528 struct bfd_hash_table local_hash_table;
8529 };
8530
8531 struct local_hash_entry
8532 {
8533 /* Base hash table entry structure. */
8534 struct bfd_hash_entry root;
8535 /* Size of the local symbol name. */
8536 size_t size;
8537 /* Number of the duplicated local symbol names. */
8538 long count;
8539 };
8540
8541 /* Create an entry in the local symbol hash table. */
8542
8543 static struct bfd_hash_entry *
8544 local_hash_newfunc (struct bfd_hash_entry *entry,
8545 struct bfd_hash_table *table,
8546 const char *string)
8547 {
8548
8549 /* Allocate the structure if it has not already been allocated by a
8550 subclass. */
8551 if (entry == NULL)
8552 {
8553 entry = bfd_hash_allocate (table,
8554 sizeof (struct local_hash_entry));
8555 if (entry == NULL)
8556 return entry;
8557 }
8558
8559 /* Call the allocation method of the superclass. */
8560 entry = bfd_hash_newfunc (entry, table, string);
8561 if (entry != NULL)
8562 {
8563 ((struct local_hash_entry *) entry)->count = 0;
8564 ((struct local_hash_entry *) entry)->size = 0;
8565 }
8566
8567 return entry;
8568 }
8569
8570 /* This struct is used to pass information to elf_link_output_extsym. */
8571
8572 struct elf_outext_info
8573 {
8574 bool failed;
8575 bool localsyms;
8576 bool file_sym_done;
8577 struct elf_final_link_info *flinfo;
8578 };
8579
8580
8581 /* Support for evaluating a complex relocation.
8582
8583 Complex relocations are generalized, self-describing relocations. The
8584 implementation of them consists of two parts: complex symbols, and the
8585 relocations themselves.
8586
8587 The relocations use a reserved elf-wide relocation type code (R_RELC
8588 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8589 information (start bit, end bit, word width, etc) into the addend. This
8590 information is extracted from CGEN-generated operand tables within gas.
8591
8592 Complex symbols are mangled symbols (STT_RELC external / BSF_RELC
8593 internal) representing prefix-notation expressions, including but not
8594 limited to those sorts of expressions normally encoded as addends in the
8595 addend field. The symbol mangling format is:
8596
8597 <node> := <literal>
8598 | <unary-operator> ':' <node>
8599 | <binary-operator> ':' <node> ':' <node>
8600 ;
8601
8602 <literal> := 's' <digits=N> ':' <N character symbol name>
8603 | 'S' <digits=N> ':' <N character section name>
8604 | '#' <hexdigits>
8605 ;
8606
8607 <binary-operator> := as in C
8608 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8609
8610 static void
8611 set_symbol_value (bfd *bfd_with_globals,
8612 Elf_Internal_Sym *isymbuf,
8613 size_t locsymcount,
8614 size_t symidx,
8615 bfd_vma val)
8616 {
8617 struct elf_link_hash_entry **sym_hashes;
8618 struct elf_link_hash_entry *h;
8619 size_t extsymoff = locsymcount;
8620
8621 if (symidx < locsymcount)
8622 {
8623 Elf_Internal_Sym *sym;
8624
8625 sym = isymbuf + symidx;
8626 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8627 {
8628 /* It is a local symbol: move it to the
8629 "absolute" section and give it a value. */
8630 sym->st_shndx = SHN_ABS;
8631 sym->st_value = val;
8632 return;
8633 }
8634 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8635 extsymoff = 0;
8636 }
8637
8638 /* It is a global symbol: set its link type
8639 to "defined" and give it a value. */
8640
8641 sym_hashes = elf_sym_hashes (bfd_with_globals);
8642 h = sym_hashes [symidx - extsymoff];
8643 while (h->root.type == bfd_link_hash_indirect
8644 || h->root.type == bfd_link_hash_warning)
8645 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8646 h->root.type = bfd_link_hash_defined;
8647 h->root.u.def.value = val;
8648 h->root.u.def.section = bfd_abs_section_ptr;
8649 }
8650
8651 static bool
8652 resolve_symbol (const char *name,
8653 bfd *input_bfd,
8654 struct elf_final_link_info *flinfo,
8655 bfd_vma *result,
8656 Elf_Internal_Sym *isymbuf,
8657 size_t locsymcount)
8658 {
8659 Elf_Internal_Sym *sym;
8660 struct bfd_link_hash_entry *global_entry;
8661 const char *candidate = NULL;
8662 Elf_Internal_Shdr *symtab_hdr;
8663 size_t i;
8664
8665 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8666
8667 for (i = 0; i < locsymcount; ++ i)
8668 {
8669 sym = isymbuf + i;
8670
8671 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8672 continue;
8673
8674 candidate = bfd_elf_string_from_elf_section (input_bfd,
8675 symtab_hdr->sh_link,
8676 sym->st_name);
8677 #ifdef DEBUG
8678 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8679 name, candidate, (unsigned long) sym->st_value);
8680 #endif
8681 if (candidate && strcmp (candidate, name) == 0)
8682 {
8683 asection *sec = flinfo->sections [i];
8684
8685 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8686 *result += sec->output_offset + sec->output_section->vma;
8687 #ifdef DEBUG
8688 printf ("Found symbol with value %8.8lx\n",
8689 (unsigned long) *result);
8690 #endif
8691 return true;
8692 }
8693 }
8694
8695 /* Hmm, haven't found it yet. perhaps it is a global. */
8696 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
8697 false, false, true);
8698 if (!global_entry)
8699 return false;
8700
8701 if (global_entry->type == bfd_link_hash_defined
8702 || global_entry->type == bfd_link_hash_defweak)
8703 {
8704 *result = (global_entry->u.def.value
8705 + global_entry->u.def.section->output_section->vma
8706 + global_entry->u.def.section->output_offset);
8707 #ifdef DEBUG
8708 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8709 global_entry->root.string, (unsigned long) *result);
8710 #endif
8711 return true;
8712 }
8713
8714 return false;
8715 }
8716
8717 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8718 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8719 names like "foo.end" which is the end address of section "foo". */
8720
8721 static bool
8722 resolve_section (const char *name,
8723 asection *sections,
8724 bfd_vma *result,
8725 bfd * abfd)
8726 {
8727 asection *curr;
8728 unsigned int len;
8729
8730 for (curr = sections; curr; curr = curr->next)
8731 if (strcmp (curr->name, name) == 0)
8732 {
8733 *result = curr->vma;
8734 return true;
8735 }
8736
8737 /* Hmm. still haven't found it. try pseudo-section names. */
8738 /* FIXME: This could be coded more efficiently... */
8739 for (curr = sections; curr; curr = curr->next)
8740 {
8741 len = strlen (curr->name);
8742 if (len > strlen (name))
8743 continue;
8744
8745 if (strncmp (curr->name, name, len) == 0)
8746 {
8747 if (startswith (name + len, ".end"))
8748 {
8749 *result = (curr->vma
8750 + curr->size / bfd_octets_per_byte (abfd, curr));
8751 return true;
8752 }
8753
8754 /* Insert more pseudo-section names here, if you like. */
8755 }
8756 }
8757
8758 return false;
8759 }
8760
8761 static void
8762 undefined_reference (const char *reftype, const char *name)
8763 {
8764 /* xgettext:c-format */
8765 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8766 reftype, name);
8767 bfd_set_error (bfd_error_bad_value);
8768 }
8769
8770 static bool
8771 eval_symbol (bfd_vma *result,
8772 const char **symp,
8773 bfd *input_bfd,
8774 struct elf_final_link_info *flinfo,
8775 bfd_vma dot,
8776 Elf_Internal_Sym *isymbuf,
8777 size_t locsymcount,
8778 int signed_p)
8779 {
8780 size_t len;
8781 size_t symlen;
8782 bfd_vma a;
8783 bfd_vma b;
8784 char symbuf[4096];
8785 const char *sym = *symp;
8786 const char *symend;
8787 bool symbol_is_section = false;
8788
8789 len = strlen (sym);
8790 symend = sym + len;
8791
8792 if (len < 1 || len > sizeof (symbuf))
8793 {
8794 bfd_set_error (bfd_error_invalid_operation);
8795 return false;
8796 }
8797
8798 switch (* sym)
8799 {
8800 case '.':
8801 *result = dot;
8802 *symp = sym + 1;
8803 return true;
8804
8805 case '#':
8806 ++sym;
8807 *result = strtoul (sym, (char **) symp, 16);
8808 return true;
8809
8810 case 'S':
8811 symbol_is_section = true;
8812 /* Fall through. */
8813 case 's':
8814 ++sym;
8815 symlen = strtol (sym, (char **) symp, 10);
8816 sym = *symp + 1; /* Skip the trailing ':'. */
8817
8818 if (symend < sym || symlen + 1 > sizeof (symbuf))
8819 {
8820 bfd_set_error (bfd_error_invalid_operation);
8821 return false;
8822 }
8823
8824 memcpy (symbuf, sym, symlen);
8825 symbuf[symlen] = '\0';
8826 *symp = sym + symlen;
8827
8828 /* Is it always possible, with complex symbols, that gas "mis-guessed"
8829 the symbol as a section, or vice-versa. so we're pretty liberal in our
8830 interpretation here; section means "try section first", not "must be a
8831 section", and likewise with symbol. */
8832
8833 if (symbol_is_section)
8834 {
8835 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8836 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8837 isymbuf, locsymcount))
8838 {
8839 undefined_reference ("section", symbuf);
8840 return false;
8841 }
8842 }
8843 else
8844 {
8845 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8846 isymbuf, locsymcount)
8847 && !resolve_section (symbuf, flinfo->output_bfd->sections,
8848 result, input_bfd))
8849 {
8850 undefined_reference ("symbol", symbuf);
8851 return false;
8852 }
8853 }
8854
8855 return true;
8856
8857 /* All that remains are operators. */
8858
8859 #define UNARY_OP(op) \
8860 if (startswith (sym, #op)) \
8861 { \
8862 sym += strlen (#op); \
8863 if (*sym == ':') \
8864 ++sym; \
8865 *symp = sym; \
8866 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8867 isymbuf, locsymcount, signed_p)) \
8868 return false; \
8869 if (signed_p) \
8870 *result = op ((bfd_signed_vma) a); \
8871 else \
8872 *result = op a; \
8873 return true; \
8874 }
8875
8876 #define BINARY_OP_HEAD(op) \
8877 if (startswith (sym, #op)) \
8878 { \
8879 sym += strlen (#op); \
8880 if (*sym == ':') \
8881 ++sym; \
8882 *symp = sym; \
8883 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8884 isymbuf, locsymcount, signed_p)) \
8885 return false; \
8886 ++*symp; \
8887 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8888 isymbuf, locsymcount, signed_p)) \
8889 return false;
8890 #define BINARY_OP_TAIL(op) \
8891 if (signed_p) \
8892 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8893 else \
8894 *result = a op b; \
8895 return true; \
8896 }
8897 #define BINARY_OP(op) BINARY_OP_HEAD(op) BINARY_OP_TAIL(op)
8898
8899 default:
8900 UNARY_OP (0-);
8901 BINARY_OP_HEAD (<<);
8902 if (b >= sizeof (a) * CHAR_BIT)
8903 {
8904 *result = 0;
8905 return true;
8906 }
8907 signed_p = 0;
8908 BINARY_OP_TAIL (<<);
8909 BINARY_OP_HEAD (>>);
8910 if (b >= sizeof (a) * CHAR_BIT)
8911 {
8912 *result = signed_p && (bfd_signed_vma) a < 0 ? -1 : 0;
8913 return true;
8914 }
8915 BINARY_OP_TAIL (>>);
8916 BINARY_OP (==);
8917 BINARY_OP (!=);
8918 BINARY_OP (<=);
8919 BINARY_OP (>=);
8920 BINARY_OP (&&);
8921 BINARY_OP (||);
8922 UNARY_OP (~);
8923 UNARY_OP (!);
8924 BINARY_OP (*);
8925 BINARY_OP_HEAD (/);
8926 if (b == 0)
8927 {
8928 _bfd_error_handler (_("division by zero"));
8929 bfd_set_error (bfd_error_bad_value);
8930 return false;
8931 }
8932 BINARY_OP_TAIL (/);
8933 BINARY_OP_HEAD (%);
8934 if (b == 0)
8935 {
8936 _bfd_error_handler (_("division by zero"));
8937 bfd_set_error (bfd_error_bad_value);
8938 return false;
8939 }
8940 BINARY_OP_TAIL (%);
8941 BINARY_OP (^);
8942 BINARY_OP (|);
8943 BINARY_OP (&);
8944 BINARY_OP (+);
8945 BINARY_OP (-);
8946 BINARY_OP (<);
8947 BINARY_OP (>);
8948 #undef UNARY_OP
8949 #undef BINARY_OP
8950 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8951 bfd_set_error (bfd_error_invalid_operation);
8952 return false;
8953 }
8954 }
8955
8956 static void
8957 put_value (bfd_vma size,
8958 unsigned long chunksz,
8959 bfd *input_bfd,
8960 bfd_vma x,
8961 bfd_byte *location)
8962 {
8963 location += (size - chunksz);
8964
8965 for (; size; size -= chunksz, location -= chunksz)
8966 {
8967 switch (chunksz)
8968 {
8969 case 1:
8970 bfd_put_8 (input_bfd, x, location);
8971 x >>= 8;
8972 break;
8973 case 2:
8974 bfd_put_16 (input_bfd, x, location);
8975 x >>= 16;
8976 break;
8977 case 4:
8978 bfd_put_32 (input_bfd, x, location);
8979 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8980 x >>= 16;
8981 x >>= 16;
8982 break;
8983 #ifdef BFD64
8984 case 8:
8985 bfd_put_64 (input_bfd, x, location);
8986 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8987 x >>= 32;
8988 x >>= 32;
8989 break;
8990 #endif
8991 default:
8992 abort ();
8993 break;
8994 }
8995 }
8996 }
8997
8998 static bfd_vma
8999 get_value (bfd_vma size,
9000 unsigned long chunksz,
9001 bfd *input_bfd,
9002 bfd_byte *location)
9003 {
9004 int shift;
9005 bfd_vma x = 0;
9006
9007 /* Sanity checks. */
9008 BFD_ASSERT (chunksz <= sizeof (x)
9009 && size >= chunksz
9010 && chunksz != 0
9011 && (size % chunksz) == 0
9012 && input_bfd != NULL
9013 && location != NULL);
9014
9015 if (chunksz == sizeof (x))
9016 {
9017 BFD_ASSERT (size == chunksz);
9018
9019 /* Make sure that we do not perform an undefined shift operation.
9020 We know that size == chunksz so there will only be one iteration
9021 of the loop below. */
9022 shift = 0;
9023 }
9024 else
9025 shift = 8 * chunksz;
9026
9027 for (; size; size -= chunksz, location += chunksz)
9028 {
9029 switch (chunksz)
9030 {
9031 case 1:
9032 x = (x << shift) | bfd_get_8 (input_bfd, location);
9033 break;
9034 case 2:
9035 x = (x << shift) | bfd_get_16 (input_bfd, location);
9036 break;
9037 case 4:
9038 x = (x << shift) | bfd_get_32 (input_bfd, location);
9039 break;
9040 #ifdef BFD64
9041 case 8:
9042 x = (x << shift) | bfd_get_64 (input_bfd, location);
9043 break;
9044 #endif
9045 default:
9046 abort ();
9047 }
9048 }
9049 return x;
9050 }
9051
9052 static void
9053 decode_complex_addend (unsigned long *start, /* in bits */
9054 unsigned long *oplen, /* in bits */
9055 unsigned long *len, /* in bits */
9056 unsigned long *wordsz, /* in bytes */
9057 unsigned long *chunksz, /* in bytes */
9058 unsigned long *lsb0_p,
9059 unsigned long *signed_p,
9060 unsigned long *trunc_p,
9061 unsigned long encoded)
9062 {
9063 * start = encoded & 0x3F;
9064 * len = (encoded >> 6) & 0x3F;
9065 * oplen = (encoded >> 12) & 0x3F;
9066 * wordsz = (encoded >> 18) & 0xF;
9067 * chunksz = (encoded >> 22) & 0xF;
9068 * lsb0_p = (encoded >> 27) & 1;
9069 * signed_p = (encoded >> 28) & 1;
9070 * trunc_p = (encoded >> 29) & 1;
9071 }
9072
9073 bfd_reloc_status_type
9074 bfd_elf_perform_complex_relocation (bfd *input_bfd,
9075 asection *input_section,
9076 bfd_byte *contents,
9077 Elf_Internal_Rela *rel,
9078 bfd_vma relocation)
9079 {
9080 bfd_vma shift, x, mask;
9081 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
9082 bfd_reloc_status_type r;
9083 bfd_size_type octets;
9084
9085 /* Perform this reloc, since it is complex.
9086 (this is not to say that it necessarily refers to a complex
9087 symbol; merely that it is a self-describing CGEN based reloc.
9088 i.e. the addend has the complete reloc information (bit start, end,
9089 word size, etc) encoded within it.). */
9090
9091 decode_complex_addend (&start, &oplen, &len, &wordsz,
9092 &chunksz, &lsb0_p, &signed_p,
9093 &trunc_p, rel->r_addend);
9094
9095 mask = (((1L << (len - 1)) - 1) << 1) | 1;
9096
9097 if (lsb0_p)
9098 shift = (start + 1) - len;
9099 else
9100 shift = (8 * wordsz) - (start + len);
9101
9102 octets = rel->r_offset * bfd_octets_per_byte (input_bfd, input_section);
9103 x = get_value (wordsz, chunksz, input_bfd, contents + octets);
9104
9105 #ifdef DEBUG
9106 printf ("Doing complex reloc: "
9107 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
9108 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
9109 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
9110 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9111 oplen, (unsigned long) x, (unsigned long) mask,
9112 (unsigned long) relocation);
9113 #endif
9114
9115 r = bfd_reloc_ok;
9116 if (! trunc_p)
9117 /* Now do an overflow check. */
9118 r = bfd_check_overflow ((signed_p
9119 ? complain_overflow_signed
9120 : complain_overflow_unsigned),
9121 len, 0, (8 * wordsz),
9122 relocation);
9123
9124 /* Do the deed. */
9125 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
9126
9127 #ifdef DEBUG
9128 printf (" relocation: %8.8lx\n"
9129 " shifted mask: %8.8lx\n"
9130 " shifted/masked reloc: %8.8lx\n"
9131 " result: %8.8lx\n",
9132 (unsigned long) relocation, (unsigned long) (mask << shift),
9133 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
9134 #endif
9135 put_value (wordsz, chunksz, input_bfd, x, contents + octets);
9136 return r;
9137 }
9138
9139 /* Functions to read r_offset from external (target order) reloc
9140 entry. Faster than bfd_getl32 et al, because we let the compiler
9141 know the value is aligned. */
9142
9143 static bfd_vma
9144 ext32l_r_offset (const void *p)
9145 {
9146 union aligned32
9147 {
9148 uint32_t v;
9149 unsigned char c[4];
9150 };
9151 const union aligned32 *a
9152 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9153
9154 uint32_t aval = ( (uint32_t) a->c[0]
9155 | (uint32_t) a->c[1] << 8
9156 | (uint32_t) a->c[2] << 16
9157 | (uint32_t) a->c[3] << 24);
9158 return aval;
9159 }
9160
9161 static bfd_vma
9162 ext32b_r_offset (const void *p)
9163 {
9164 union aligned32
9165 {
9166 uint32_t v;
9167 unsigned char c[4];
9168 };
9169 const union aligned32 *a
9170 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9171
9172 uint32_t aval = ( (uint32_t) a->c[0] << 24
9173 | (uint32_t) a->c[1] << 16
9174 | (uint32_t) a->c[2] << 8
9175 | (uint32_t) a->c[3]);
9176 return aval;
9177 }
9178
9179 #ifdef BFD_HOST_64_BIT
9180 static bfd_vma
9181 ext64l_r_offset (const void *p)
9182 {
9183 union aligned64
9184 {
9185 uint64_t v;
9186 unsigned char c[8];
9187 };
9188 const union aligned64 *a
9189 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9190
9191 uint64_t aval = ( (uint64_t) a->c[0]
9192 | (uint64_t) a->c[1] << 8
9193 | (uint64_t) a->c[2] << 16
9194 | (uint64_t) a->c[3] << 24
9195 | (uint64_t) a->c[4] << 32
9196 | (uint64_t) a->c[5] << 40
9197 | (uint64_t) a->c[6] << 48
9198 | (uint64_t) a->c[7] << 56);
9199 return aval;
9200 }
9201
9202 static bfd_vma
9203 ext64b_r_offset (const void *p)
9204 {
9205 union aligned64
9206 {
9207 uint64_t v;
9208 unsigned char c[8];
9209 };
9210 const union aligned64 *a
9211 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9212
9213 uint64_t aval = ( (uint64_t) a->c[0] << 56
9214 | (uint64_t) a->c[1] << 48
9215 | (uint64_t) a->c[2] << 40
9216 | (uint64_t) a->c[3] << 32
9217 | (uint64_t) a->c[4] << 24
9218 | (uint64_t) a->c[5] << 16
9219 | (uint64_t) a->c[6] << 8
9220 | (uint64_t) a->c[7]);
9221 return aval;
9222 }
9223 #endif
9224
9225 /* When performing a relocatable link, the input relocations are
9226 preserved. But, if they reference global symbols, the indices
9227 referenced must be updated. Update all the relocations found in
9228 RELDATA. */
9229
9230 static bool
9231 elf_link_adjust_relocs (bfd *abfd,
9232 asection *sec,
9233 struct bfd_elf_section_reloc_data *reldata,
9234 bool sort,
9235 struct bfd_link_info *info)
9236 {
9237 unsigned int i;
9238 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9239 bfd_byte *erela;
9240 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9241 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9242 bfd_vma r_type_mask;
9243 int r_sym_shift;
9244 unsigned int count = reldata->count;
9245 struct elf_link_hash_entry **rel_hash = reldata->hashes;
9246
9247 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
9248 {
9249 swap_in = bed->s->swap_reloc_in;
9250 swap_out = bed->s->swap_reloc_out;
9251 }
9252 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
9253 {
9254 swap_in = bed->s->swap_reloca_in;
9255 swap_out = bed->s->swap_reloca_out;
9256 }
9257 else
9258 abort ();
9259
9260 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
9261 abort ();
9262
9263 if (bed->s->arch_size == 32)
9264 {
9265 r_type_mask = 0xff;
9266 r_sym_shift = 8;
9267 }
9268 else
9269 {
9270 r_type_mask = 0xffffffff;
9271 r_sym_shift = 32;
9272 }
9273
9274 erela = reldata->hdr->contents;
9275 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
9276 {
9277 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
9278 unsigned int j;
9279
9280 if (*rel_hash == NULL)
9281 continue;
9282
9283 if ((*rel_hash)->indx == -2
9284 && info->gc_sections
9285 && ! info->gc_keep_exported)
9286 {
9287 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9288 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
9289 abfd, sec,
9290 (*rel_hash)->root.root.string);
9291 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
9292 abfd, sec);
9293 bfd_set_error (bfd_error_invalid_operation);
9294 return false;
9295 }
9296 BFD_ASSERT ((*rel_hash)->indx >= 0);
9297
9298 (*swap_in) (abfd, erela, irela);
9299 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
9300 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
9301 | (irela[j].r_info & r_type_mask));
9302 (*swap_out) (abfd, irela, erela);
9303 }
9304
9305 if (bed->elf_backend_update_relocs)
9306 (*bed->elf_backend_update_relocs) (sec, reldata);
9307
9308 if (sort && count != 0)
9309 {
9310 bfd_vma (*ext_r_off) (const void *);
9311 bfd_vma r_off;
9312 size_t elt_size;
9313 bfd_byte *base, *end, *p, *loc;
9314 bfd_byte *buf = NULL;
9315
9316 if (bed->s->arch_size == 32)
9317 {
9318 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9319 ext_r_off = ext32l_r_offset;
9320 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9321 ext_r_off = ext32b_r_offset;
9322 else
9323 abort ();
9324 }
9325 else
9326 {
9327 #ifdef BFD_HOST_64_BIT
9328 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9329 ext_r_off = ext64l_r_offset;
9330 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9331 ext_r_off = ext64b_r_offset;
9332 else
9333 #endif
9334 abort ();
9335 }
9336
9337 /* Must use a stable sort here. A modified insertion sort,
9338 since the relocs are mostly sorted already. */
9339 elt_size = reldata->hdr->sh_entsize;
9340 base = reldata->hdr->contents;
9341 end = base + count * elt_size;
9342 if (elt_size > sizeof (Elf64_External_Rela))
9343 abort ();
9344
9345 /* Ensure the first element is lowest. This acts as a sentinel,
9346 speeding the main loop below. */
9347 r_off = (*ext_r_off) (base);
9348 for (p = loc = base; (p += elt_size) < end; )
9349 {
9350 bfd_vma r_off2 = (*ext_r_off) (p);
9351 if (r_off > r_off2)
9352 {
9353 r_off = r_off2;
9354 loc = p;
9355 }
9356 }
9357 if (loc != base)
9358 {
9359 /* Don't just swap *base and *loc as that changes the order
9360 of the original base[0] and base[1] if they happen to
9361 have the same r_offset. */
9362 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
9363 memcpy (onebuf, loc, elt_size);
9364 memmove (base + elt_size, base, loc - base);
9365 memcpy (base, onebuf, elt_size);
9366 }
9367
9368 for (p = base + elt_size; (p += elt_size) < end; )
9369 {
9370 /* base to p is sorted, *p is next to insert. */
9371 r_off = (*ext_r_off) (p);
9372 /* Search the sorted region for location to insert. */
9373 loc = p - elt_size;
9374 while (r_off < (*ext_r_off) (loc))
9375 loc -= elt_size;
9376 loc += elt_size;
9377 if (loc != p)
9378 {
9379 /* Chances are there is a run of relocs to insert here,
9380 from one of more input files. Files are not always
9381 linked in order due to the way elf_link_input_bfd is
9382 called. See pr17666. */
9383 size_t sortlen = p - loc;
9384 bfd_vma r_off2 = (*ext_r_off) (loc);
9385 size_t runlen = elt_size;
9386 size_t buf_size = 96 * 1024;
9387 while (p + runlen < end
9388 && (sortlen <= buf_size
9389 || runlen + elt_size <= buf_size)
9390 && r_off2 > (*ext_r_off) (p + runlen))
9391 runlen += elt_size;
9392 if (buf == NULL)
9393 {
9394 buf = bfd_malloc (buf_size);
9395 if (buf == NULL)
9396 return false;
9397 }
9398 if (runlen < sortlen)
9399 {
9400 memcpy (buf, p, runlen);
9401 memmove (loc + runlen, loc, sortlen);
9402 memcpy (loc, buf, runlen);
9403 }
9404 else
9405 {
9406 memcpy (buf, loc, sortlen);
9407 memmove (loc, p, runlen);
9408 memcpy (loc + runlen, buf, sortlen);
9409 }
9410 p += runlen - elt_size;
9411 }
9412 }
9413 /* Hashes are no longer valid. */
9414 free (reldata->hashes);
9415 reldata->hashes = NULL;
9416 free (buf);
9417 }
9418 return true;
9419 }
9420
9421 struct elf_link_sort_rela
9422 {
9423 union {
9424 bfd_vma offset;
9425 bfd_vma sym_mask;
9426 } u;
9427 enum elf_reloc_type_class type;
9428 /* We use this as an array of size int_rels_per_ext_rel. */
9429 Elf_Internal_Rela rela[1];
9430 };
9431
9432 /* qsort stability here and for cmp2 is only an issue if multiple
9433 dynamic relocations are emitted at the same address. But targets
9434 that apply a series of dynamic relocations each operating on the
9435 result of the prior relocation can't use -z combreloc as
9436 implemented anyway. Such schemes tend to be broken by sorting on
9437 symbol index. That leaves dynamic NONE relocs as the only other
9438 case where ld might emit multiple relocs at the same address, and
9439 those are only emitted due to target bugs. */
9440
9441 static int
9442 elf_link_sort_cmp1 (const void *A, const void *B)
9443 {
9444 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9445 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9446 int relativea, relativeb;
9447
9448 relativea = a->type == reloc_class_relative;
9449 relativeb = b->type == reloc_class_relative;
9450
9451 if (relativea < relativeb)
9452 return 1;
9453 if (relativea > relativeb)
9454 return -1;
9455 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
9456 return -1;
9457 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
9458 return 1;
9459 if (a->rela->r_offset < b->rela->r_offset)
9460 return -1;
9461 if (a->rela->r_offset > b->rela->r_offset)
9462 return 1;
9463 return 0;
9464 }
9465
9466 static int
9467 elf_link_sort_cmp2 (const void *A, const void *B)
9468 {
9469 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9470 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9471
9472 if (a->type < b->type)
9473 return -1;
9474 if (a->type > b->type)
9475 return 1;
9476 if (a->u.offset < b->u.offset)
9477 return -1;
9478 if (a->u.offset > b->u.offset)
9479 return 1;
9480 if (a->rela->r_offset < b->rela->r_offset)
9481 return -1;
9482 if (a->rela->r_offset > b->rela->r_offset)
9483 return 1;
9484 return 0;
9485 }
9486
9487 static size_t
9488 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9489 {
9490 asection *dynamic_relocs;
9491 asection *rela_dyn;
9492 asection *rel_dyn;
9493 bfd_size_type count, size;
9494 size_t i, ret, sort_elt, ext_size;
9495 bfd_byte *sort, *s_non_relative, *p;
9496 struct elf_link_sort_rela *sq;
9497 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9498 int i2e = bed->s->int_rels_per_ext_rel;
9499 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
9500 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9501 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9502 struct bfd_link_order *lo;
9503 bfd_vma r_sym_mask;
9504 bool use_rela;
9505
9506 /* Find a dynamic reloc section. */
9507 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9508 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
9509 if (rela_dyn != NULL && rela_dyn->size > 0
9510 && rel_dyn != NULL && rel_dyn->size > 0)
9511 {
9512 bool use_rela_initialised = false;
9513
9514 /* This is just here to stop gcc from complaining.
9515 Its initialization checking code is not perfect. */
9516 use_rela = true;
9517
9518 /* Both sections are present. Examine the sizes
9519 of the indirect sections to help us choose. */
9520 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9521 if (lo->type == bfd_indirect_link_order)
9522 {
9523 asection *o = lo->u.indirect.section;
9524
9525 if ((o->size % bed->s->sizeof_rela) == 0)
9526 {
9527 if ((o->size % bed->s->sizeof_rel) == 0)
9528 /* Section size is divisible by both rel and rela sizes.
9529 It is of no help to us. */
9530 ;
9531 else
9532 {
9533 /* Section size is only divisible by rela. */
9534 if (use_rela_initialised && !use_rela)
9535 {
9536 _bfd_error_handler (_("%pB: unable to sort relocs - "
9537 "they are in more than one size"),
9538 abfd);
9539 bfd_set_error (bfd_error_invalid_operation);
9540 return 0;
9541 }
9542 else
9543 {
9544 use_rela = true;
9545 use_rela_initialised = true;
9546 }
9547 }
9548 }
9549 else if ((o->size % bed->s->sizeof_rel) == 0)
9550 {
9551 /* Section size is only divisible by rel. */
9552 if (use_rela_initialised && use_rela)
9553 {
9554 _bfd_error_handler (_("%pB: unable to sort relocs - "
9555 "they are in more than one size"),
9556 abfd);
9557 bfd_set_error (bfd_error_invalid_operation);
9558 return 0;
9559 }
9560 else
9561 {
9562 use_rela = false;
9563 use_rela_initialised = true;
9564 }
9565 }
9566 else
9567 {
9568 /* The section size is not divisible by either -
9569 something is wrong. */
9570 _bfd_error_handler (_("%pB: unable to sort relocs - "
9571 "they are of an unknown size"), abfd);
9572 bfd_set_error (bfd_error_invalid_operation);
9573 return 0;
9574 }
9575 }
9576
9577 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9578 if (lo->type == bfd_indirect_link_order)
9579 {
9580 asection *o = lo->u.indirect.section;
9581
9582 if ((o->size % bed->s->sizeof_rela) == 0)
9583 {
9584 if ((o->size % bed->s->sizeof_rel) == 0)
9585 /* Section size is divisible by both rel and rela sizes.
9586 It is of no help to us. */
9587 ;
9588 else
9589 {
9590 /* Section size is only divisible by rela. */
9591 if (use_rela_initialised && !use_rela)
9592 {
9593 _bfd_error_handler (_("%pB: unable to sort relocs - "
9594 "they are in more than one size"),
9595 abfd);
9596 bfd_set_error (bfd_error_invalid_operation);
9597 return 0;
9598 }
9599 else
9600 {
9601 use_rela = true;
9602 use_rela_initialised = true;
9603 }
9604 }
9605 }
9606 else if ((o->size % bed->s->sizeof_rel) == 0)
9607 {
9608 /* Section size is only divisible by rel. */
9609 if (use_rela_initialised && use_rela)
9610 {
9611 _bfd_error_handler (_("%pB: unable to sort relocs - "
9612 "they are in more than one size"),
9613 abfd);
9614 bfd_set_error (bfd_error_invalid_operation);
9615 return 0;
9616 }
9617 else
9618 {
9619 use_rela = false;
9620 use_rela_initialised = true;
9621 }
9622 }
9623 else
9624 {
9625 /* The section size is not divisible by either -
9626 something is wrong. */
9627 _bfd_error_handler (_("%pB: unable to sort relocs - "
9628 "they are of an unknown size"), abfd);
9629 bfd_set_error (bfd_error_invalid_operation);
9630 return 0;
9631 }
9632 }
9633
9634 if (! use_rela_initialised)
9635 /* Make a guess. */
9636 use_rela = true;
9637 }
9638 else if (rela_dyn != NULL && rela_dyn->size > 0)
9639 use_rela = true;
9640 else if (rel_dyn != NULL && rel_dyn->size > 0)
9641 use_rela = false;
9642 else
9643 return 0;
9644
9645 if (use_rela)
9646 {
9647 dynamic_relocs = rela_dyn;
9648 ext_size = bed->s->sizeof_rela;
9649 swap_in = bed->s->swap_reloca_in;
9650 swap_out = bed->s->swap_reloca_out;
9651 }
9652 else
9653 {
9654 dynamic_relocs = rel_dyn;
9655 ext_size = bed->s->sizeof_rel;
9656 swap_in = bed->s->swap_reloc_in;
9657 swap_out = bed->s->swap_reloc_out;
9658 }
9659
9660 size = 0;
9661 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9662 if (lo->type == bfd_indirect_link_order)
9663 size += lo->u.indirect.section->size;
9664
9665 if (size != dynamic_relocs->size)
9666 return 0;
9667
9668 sort_elt = (sizeof (struct elf_link_sort_rela)
9669 + (i2e - 1) * sizeof (Elf_Internal_Rela));
9670
9671 count = dynamic_relocs->size / ext_size;
9672 if (count == 0)
9673 return 0;
9674 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
9675
9676 if (sort == NULL)
9677 {
9678 (*info->callbacks->warning)
9679 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
9680 return 0;
9681 }
9682
9683 if (bed->s->arch_size == 32)
9684 r_sym_mask = ~(bfd_vma) 0xff;
9685 else
9686 r_sym_mask = ~(bfd_vma) 0xffffffff;
9687
9688 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9689 if (lo->type == bfd_indirect_link_order)
9690 {
9691 bfd_byte *erel, *erelend;
9692 asection *o = lo->u.indirect.section;
9693
9694 if (o->contents == NULL && o->size != 0)
9695 {
9696 /* This is a reloc section that is being handled as a normal
9697 section. See bfd_section_from_shdr. We can't combine
9698 relocs in this case. */
9699 free (sort);
9700 return 0;
9701 }
9702 erel = o->contents;
9703 erelend = o->contents + o->size;
9704 p = sort + o->output_offset * opb / ext_size * sort_elt;
9705
9706 while (erel < erelend)
9707 {
9708 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9709
9710 (*swap_in) (abfd, erel, s->rela);
9711 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
9712 s->u.sym_mask = r_sym_mask;
9713 p += sort_elt;
9714 erel += ext_size;
9715 }
9716 }
9717
9718 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9719
9720 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9721 {
9722 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9723 if (s->type != reloc_class_relative)
9724 break;
9725 }
9726 ret = i;
9727 s_non_relative = p;
9728
9729 sq = (struct elf_link_sort_rela *) s_non_relative;
9730 for (; i < count; i++, p += sort_elt)
9731 {
9732 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9733 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9734 sq = sp;
9735 sp->u.offset = sq->rela->r_offset;
9736 }
9737
9738 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9739
9740 struct elf_link_hash_table *htab = elf_hash_table (info);
9741 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9742 {
9743 /* We have plt relocs in .rela.dyn. */
9744 sq = (struct elf_link_sort_rela *) sort;
9745 for (i = 0; i < count; i++)
9746 if (sq[count - i - 1].type != reloc_class_plt)
9747 break;
9748 if (i != 0 && htab->srelplt->size == i * ext_size)
9749 {
9750 struct bfd_link_order **plo;
9751 /* Put srelplt link_order last. This is so the output_offset
9752 set in the next loop is correct for DT_JMPREL. */
9753 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9754 if ((*plo)->type == bfd_indirect_link_order
9755 && (*plo)->u.indirect.section == htab->srelplt)
9756 {
9757 lo = *plo;
9758 *plo = lo->next;
9759 }
9760 else
9761 plo = &(*plo)->next;
9762 *plo = lo;
9763 lo->next = NULL;
9764 dynamic_relocs->map_tail.link_order = lo;
9765 }
9766 }
9767
9768 p = sort;
9769 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9770 if (lo->type == bfd_indirect_link_order)
9771 {
9772 bfd_byte *erel, *erelend;
9773 asection *o = lo->u.indirect.section;
9774
9775 erel = o->contents;
9776 erelend = o->contents + o->size;
9777 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
9778 while (erel < erelend)
9779 {
9780 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9781 (*swap_out) (abfd, s->rela, erel);
9782 p += sort_elt;
9783 erel += ext_size;
9784 }
9785 }
9786
9787 free (sort);
9788 *psec = dynamic_relocs;
9789 return ret;
9790 }
9791
9792 /* Add a symbol to the output symbol string table. */
9793
9794 static int
9795 elf_link_output_symstrtab (void *finf,
9796 const char *name,
9797 Elf_Internal_Sym *elfsym,
9798 asection *input_sec,
9799 struct elf_link_hash_entry *h)
9800 {
9801 struct elf_final_link_info *flinfo = finf;
9802 int (*output_symbol_hook)
9803 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9804 struct elf_link_hash_entry *);
9805 struct elf_link_hash_table *hash_table;
9806 const struct elf_backend_data *bed;
9807 bfd_size_type strtabsize;
9808
9809 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9810
9811 bed = get_elf_backend_data (flinfo->output_bfd);
9812 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9813 if (output_symbol_hook != NULL)
9814 {
9815 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
9816 if (ret != 1)
9817 return ret;
9818 }
9819
9820 if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC)
9821 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
9822 if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE)
9823 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique;
9824
9825 if (name == NULL
9826 || *name == '\0'
9827 || (input_sec->flags & SEC_EXCLUDE))
9828 elfsym->st_name = (unsigned long) -1;
9829 else
9830 {
9831 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9832 to get the final offset for st_name. */
9833 char *versioned_name = (char *) name;
9834 if (h != NULL)
9835 {
9836 if (h->versioned == versioned && h->def_dynamic)
9837 {
9838 /* Keep only one '@' for versioned symbols defined in
9839 shared objects. */
9840 char *version = strrchr (name, ELF_VER_CHR);
9841 char *base_end = strchr (name, ELF_VER_CHR);
9842 if (version != base_end)
9843 {
9844 size_t base_len;
9845 size_t len = strlen (name);
9846 versioned_name = bfd_alloc (flinfo->output_bfd, len);
9847 if (versioned_name == NULL)
9848 return 0;
9849 base_len = base_end - name;
9850 memcpy (versioned_name, name, base_len);
9851 memcpy (versioned_name + base_len, version,
9852 len - base_len);
9853 }
9854 }
9855 }
9856 else if (flinfo->info->unique_symbol
9857 && ELF_ST_BIND (elfsym->st_info) == STB_LOCAL)
9858 {
9859 struct local_hash_entry *lh;
9860 size_t count_len;
9861 size_t base_len;
9862 char buf[30];
9863 switch (ELF_ST_TYPE (elfsym->st_info))
9864 {
9865 case STT_FILE:
9866 case STT_SECTION:
9867 break;
9868 default:
9869 lh = (struct local_hash_entry *) bfd_hash_lookup
9870 (&flinfo->local_hash_table, name, true, false);
9871 if (lh == NULL)
9872 return 0;
9873 /* Always append ".COUNT" to local symbols to avoid
9874 potential conflicts with local symbol "XXX.COUNT". */
9875 sprintf (buf, "%lx", lh->count);
9876 base_len = lh->size;
9877 if (!base_len)
9878 {
9879 base_len = strlen (name);
9880 lh->size = base_len;
9881 }
9882 count_len = strlen (buf);
9883 versioned_name = bfd_alloc (flinfo->output_bfd,
9884 base_len + count_len + 2);
9885 if (versioned_name == NULL)
9886 return 0;
9887 memcpy (versioned_name, name, base_len);
9888 versioned_name[base_len] = '.';
9889 memcpy (versioned_name + base_len + 1, buf,
9890 count_len + 1);
9891 lh->count++;
9892 break;
9893 }
9894 }
9895 elfsym->st_name
9896 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
9897 versioned_name, false);
9898 if (elfsym->st_name == (unsigned long) -1)
9899 return 0;
9900 }
9901
9902 hash_table = elf_hash_table (flinfo->info);
9903 strtabsize = hash_table->strtabsize;
9904 if (strtabsize <= hash_table->strtabcount)
9905 {
9906 strtabsize += strtabsize;
9907 hash_table->strtabsize = strtabsize;
9908 strtabsize *= sizeof (*hash_table->strtab);
9909 hash_table->strtab
9910 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9911 strtabsize);
9912 if (hash_table->strtab == NULL)
9913 return 0;
9914 }
9915 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9916 hash_table->strtab[hash_table->strtabcount].dest_index
9917 = hash_table->strtabcount;
9918 hash_table->strtab[hash_table->strtabcount].destshndx_index
9919 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9920
9921 flinfo->output_bfd->symcount += 1;
9922 hash_table->strtabcount += 1;
9923
9924 return 1;
9925 }
9926
9927 /* Swap symbols out to the symbol table and flush the output symbols to
9928 the file. */
9929
9930 static bool
9931 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9932 {
9933 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
9934 size_t amt;
9935 size_t i;
9936 const struct elf_backend_data *bed;
9937 bfd_byte *symbuf;
9938 Elf_Internal_Shdr *hdr;
9939 file_ptr pos;
9940 bool ret;
9941
9942 if (!hash_table->strtabcount)
9943 return true;
9944
9945 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9946
9947 bed = get_elf_backend_data (flinfo->output_bfd);
9948
9949 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9950 symbuf = (bfd_byte *) bfd_malloc (amt);
9951 if (symbuf == NULL)
9952 return false;
9953
9954 if (flinfo->symshndxbuf)
9955 {
9956 amt = sizeof (Elf_External_Sym_Shndx);
9957 amt *= bfd_get_symcount (flinfo->output_bfd);
9958 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9959 if (flinfo->symshndxbuf == NULL)
9960 {
9961 free (symbuf);
9962 return false;
9963 }
9964 }
9965
9966 /* Now swap out the symbols. */
9967 for (i = 0; i < hash_table->strtabcount; i++)
9968 {
9969 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9970 if (elfsym->sym.st_name == (unsigned long) -1)
9971 elfsym->sym.st_name = 0;
9972 else
9973 elfsym->sym.st_name
9974 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9975 elfsym->sym.st_name);
9976
9977 /* Inform the linker of the addition of this symbol. */
9978
9979 if (flinfo->info->callbacks->ctf_new_symbol)
9980 flinfo->info->callbacks->ctf_new_symbol (elfsym->dest_index,
9981 &elfsym->sym);
9982
9983 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9984 ((bfd_byte *) symbuf
9985 + (elfsym->dest_index
9986 * bed->s->sizeof_sym)),
9987 (flinfo->symshndxbuf
9988 + elfsym->destshndx_index));
9989 }
9990
9991 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9992 pos = hdr->sh_offset + hdr->sh_size;
9993 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9994 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9995 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9996 {
9997 hdr->sh_size += amt;
9998 ret = true;
9999 }
10000 else
10001 ret = false;
10002
10003 free (symbuf);
10004
10005 free (hash_table->strtab);
10006 hash_table->strtab = NULL;
10007
10008 return ret;
10009 }
10010
10011 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
10012
10013 static bool
10014 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
10015 {
10016 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
10017 && sym->st_shndx < SHN_LORESERVE)
10018 {
10019 /* The gABI doesn't support dynamic symbols in output sections
10020 beyond 64k. */
10021 _bfd_error_handler
10022 /* xgettext:c-format */
10023 (_("%pB: too many sections: %d (>= %d)"),
10024 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
10025 bfd_set_error (bfd_error_nonrepresentable_section);
10026 return false;
10027 }
10028 return true;
10029 }
10030
10031 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
10032 allowing an unsatisfied unversioned symbol in the DSO to match a
10033 versioned symbol that would normally require an explicit version.
10034 We also handle the case that a DSO references a hidden symbol
10035 which may be satisfied by a versioned symbol in another DSO. */
10036
10037 static bool
10038 elf_link_check_versioned_symbol (struct bfd_link_info *info,
10039 const struct elf_backend_data *bed,
10040 struct elf_link_hash_entry *h)
10041 {
10042 bfd *abfd;
10043 struct elf_link_loaded_list *loaded;
10044
10045 if (!is_elf_hash_table (info->hash))
10046 return false;
10047
10048 /* Check indirect symbol. */
10049 while (h->root.type == bfd_link_hash_indirect)
10050 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10051
10052 switch (h->root.type)
10053 {
10054 default:
10055 abfd = NULL;
10056 break;
10057
10058 case bfd_link_hash_undefined:
10059 case bfd_link_hash_undefweak:
10060 abfd = h->root.u.undef.abfd;
10061 if (abfd == NULL
10062 || (abfd->flags & DYNAMIC) == 0
10063 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
10064 return false;
10065 break;
10066
10067 case bfd_link_hash_defined:
10068 case bfd_link_hash_defweak:
10069 abfd = h->root.u.def.section->owner;
10070 break;
10071
10072 case bfd_link_hash_common:
10073 abfd = h->root.u.c.p->section->owner;
10074 break;
10075 }
10076 BFD_ASSERT (abfd != NULL);
10077
10078 for (loaded = elf_hash_table (info)->dyn_loaded;
10079 loaded != NULL;
10080 loaded = loaded->next)
10081 {
10082 bfd *input;
10083 Elf_Internal_Shdr *hdr;
10084 size_t symcount;
10085 size_t extsymcount;
10086 size_t extsymoff;
10087 Elf_Internal_Shdr *versymhdr;
10088 Elf_Internal_Sym *isym;
10089 Elf_Internal_Sym *isymend;
10090 Elf_Internal_Sym *isymbuf;
10091 Elf_External_Versym *ever;
10092 Elf_External_Versym *extversym;
10093
10094 input = loaded->abfd;
10095
10096 /* We check each DSO for a possible hidden versioned definition. */
10097 if (input == abfd
10098 || elf_dynversym (input) == 0)
10099 continue;
10100
10101 hdr = &elf_tdata (input)->dynsymtab_hdr;
10102
10103 symcount = hdr->sh_size / bed->s->sizeof_sym;
10104 if (elf_bad_symtab (input))
10105 {
10106 extsymcount = symcount;
10107 extsymoff = 0;
10108 }
10109 else
10110 {
10111 extsymcount = symcount - hdr->sh_info;
10112 extsymoff = hdr->sh_info;
10113 }
10114
10115 if (extsymcount == 0)
10116 continue;
10117
10118 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
10119 NULL, NULL, NULL);
10120 if (isymbuf == NULL)
10121 return false;
10122
10123 /* Read in any version definitions. */
10124 versymhdr = &elf_tdata (input)->dynversym_hdr;
10125 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
10126 || (extversym = (Elf_External_Versym *)
10127 _bfd_malloc_and_read (input, versymhdr->sh_size,
10128 versymhdr->sh_size)) == NULL)
10129 {
10130 free (isymbuf);
10131 return false;
10132 }
10133
10134 ever = extversym + extsymoff;
10135 isymend = isymbuf + extsymcount;
10136 for (isym = isymbuf; isym < isymend; isym++, ever++)
10137 {
10138 const char *name;
10139 Elf_Internal_Versym iver;
10140 unsigned short version_index;
10141
10142 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
10143 || isym->st_shndx == SHN_UNDEF)
10144 continue;
10145
10146 name = bfd_elf_string_from_elf_section (input,
10147 hdr->sh_link,
10148 isym->st_name);
10149 if (strcmp (name, h->root.root.string) != 0)
10150 continue;
10151
10152 _bfd_elf_swap_versym_in (input, ever, &iver);
10153
10154 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
10155 && !(h->def_regular
10156 && h->forced_local))
10157 {
10158 /* If we have a non-hidden versioned sym, then it should
10159 have provided a definition for the undefined sym unless
10160 it is defined in a non-shared object and forced local.
10161 */
10162 abort ();
10163 }
10164
10165 version_index = iver.vs_vers & VERSYM_VERSION;
10166 if (version_index == 1 || version_index == 2)
10167 {
10168 /* This is the base or first version. We can use it. */
10169 free (extversym);
10170 free (isymbuf);
10171 return true;
10172 }
10173 }
10174
10175 free (extversym);
10176 free (isymbuf);
10177 }
10178
10179 return false;
10180 }
10181
10182 /* Convert ELF common symbol TYPE. */
10183
10184 static int
10185 elf_link_convert_common_type (struct bfd_link_info *info, int type)
10186 {
10187 /* Commom symbol can only appear in relocatable link. */
10188 if (!bfd_link_relocatable (info))
10189 abort ();
10190 switch (info->elf_stt_common)
10191 {
10192 case unchanged:
10193 break;
10194 case elf_stt_common:
10195 type = STT_COMMON;
10196 break;
10197 case no_elf_stt_common:
10198 type = STT_OBJECT;
10199 break;
10200 }
10201 return type;
10202 }
10203
10204 /* Add an external symbol to the symbol table. This is called from
10205 the hash table traversal routine. When generating a shared object,
10206 we go through the symbol table twice. The first time we output
10207 anything that might have been forced to local scope in a version
10208 script. The second time we output the symbols that are still
10209 global symbols. */
10210
10211 static bool
10212 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
10213 {
10214 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
10215 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
10216 struct elf_final_link_info *flinfo = eoinfo->flinfo;
10217 bool strip;
10218 Elf_Internal_Sym sym;
10219 asection *input_sec;
10220 const struct elf_backend_data *bed;
10221 long indx;
10222 int ret;
10223 unsigned int type;
10224
10225 if (h->root.type == bfd_link_hash_warning)
10226 {
10227 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10228 if (h->root.type == bfd_link_hash_new)
10229 return true;
10230 }
10231
10232 /* Decide whether to output this symbol in this pass. */
10233 if (eoinfo->localsyms)
10234 {
10235 if (!h->forced_local)
10236 return true;
10237 }
10238 else
10239 {
10240 if (h->forced_local)
10241 return true;
10242 }
10243
10244 bed = get_elf_backend_data (flinfo->output_bfd);
10245
10246 if (h->root.type == bfd_link_hash_undefined)
10247 {
10248 /* If we have an undefined symbol reference here then it must have
10249 come from a shared library that is being linked in. (Undefined
10250 references in regular files have already been handled unless
10251 they are in unreferenced sections which are removed by garbage
10252 collection). */
10253 bool ignore_undef = false;
10254
10255 /* Some symbols may be special in that the fact that they're
10256 undefined can be safely ignored - let backend determine that. */
10257 if (bed->elf_backend_ignore_undef_symbol)
10258 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
10259
10260 /* If we are reporting errors for this situation then do so now. */
10261 if (!ignore_undef
10262 && h->ref_dynamic_nonweak
10263 && (!h->ref_regular || flinfo->info->gc_sections)
10264 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
10265 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
10266 {
10267 flinfo->info->callbacks->undefined_symbol
10268 (flinfo->info, h->root.root.string,
10269 h->ref_regular ? NULL : h->root.u.undef.abfd, NULL, 0,
10270 flinfo->info->unresolved_syms_in_shared_libs == RM_DIAGNOSE
10271 && !flinfo->info->warn_unresolved_syms);
10272 }
10273
10274 /* Strip a global symbol defined in a discarded section. */
10275 if (h->indx == -3)
10276 return true;
10277 }
10278
10279 /* We should also warn if a forced local symbol is referenced from
10280 shared libraries. */
10281 if (bfd_link_executable (flinfo->info)
10282 && h->forced_local
10283 && h->ref_dynamic
10284 && h->def_regular
10285 && !h->dynamic_def
10286 && h->ref_dynamic_nonweak
10287 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
10288 {
10289 bfd *def_bfd;
10290 const char *msg;
10291 struct elf_link_hash_entry *hi = h;
10292
10293 /* Check indirect symbol. */
10294 while (hi->root.type == bfd_link_hash_indirect)
10295 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
10296
10297 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
10298 /* xgettext:c-format */
10299 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
10300 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
10301 /* xgettext:c-format */
10302 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
10303 else
10304 /* xgettext:c-format */
10305 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
10306 def_bfd = flinfo->output_bfd;
10307 if (hi->root.u.def.section != bfd_abs_section_ptr)
10308 def_bfd = hi->root.u.def.section->owner;
10309 _bfd_error_handler (msg, flinfo->output_bfd,
10310 h->root.root.string, def_bfd);
10311 bfd_set_error (bfd_error_bad_value);
10312 eoinfo->failed = true;
10313 return false;
10314 }
10315
10316 /* We don't want to output symbols that have never been mentioned by
10317 a regular file, or that we have been told to strip. However, if
10318 h->indx is set to -2, the symbol is used by a reloc and we must
10319 output it. */
10320 strip = false;
10321 if (h->indx == -2)
10322 ;
10323 else if ((h->def_dynamic
10324 || h->ref_dynamic
10325 || h->root.type == bfd_link_hash_new)
10326 && !h->def_regular
10327 && !h->ref_regular)
10328 strip = true;
10329 else if (flinfo->info->strip == strip_all)
10330 strip = true;
10331 else if (flinfo->info->strip == strip_some
10332 && bfd_hash_lookup (flinfo->info->keep_hash,
10333 h->root.root.string, false, false) == NULL)
10334 strip = true;
10335 else if ((h->root.type == bfd_link_hash_defined
10336 || h->root.type == bfd_link_hash_defweak)
10337 && ((flinfo->info->strip_discarded
10338 && discarded_section (h->root.u.def.section))
10339 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
10340 && h->root.u.def.section->owner != NULL
10341 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
10342 strip = true;
10343 else if ((h->root.type == bfd_link_hash_undefined
10344 || h->root.type == bfd_link_hash_undefweak)
10345 && h->root.u.undef.abfd != NULL
10346 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
10347 strip = true;
10348
10349 type = h->type;
10350
10351 /* If we're stripping it, and it's not a dynamic symbol, there's
10352 nothing else to do. However, if it is a forced local symbol or
10353 an ifunc symbol we need to give the backend finish_dynamic_symbol
10354 function a chance to make it dynamic. */
10355 if (strip
10356 && h->dynindx == -1
10357 && type != STT_GNU_IFUNC
10358 && !h->forced_local)
10359 return true;
10360
10361 sym.st_value = 0;
10362 sym.st_size = h->size;
10363 sym.st_other = h->other;
10364 switch (h->root.type)
10365 {
10366 default:
10367 case bfd_link_hash_new:
10368 case bfd_link_hash_warning:
10369 abort ();
10370 return false;
10371
10372 case bfd_link_hash_undefined:
10373 case bfd_link_hash_undefweak:
10374 input_sec = bfd_und_section_ptr;
10375 sym.st_shndx = SHN_UNDEF;
10376 break;
10377
10378 case bfd_link_hash_defined:
10379 case bfd_link_hash_defweak:
10380 {
10381 input_sec = h->root.u.def.section;
10382 if (input_sec->output_section != NULL)
10383 {
10384 sym.st_shndx =
10385 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
10386 input_sec->output_section);
10387 if (sym.st_shndx == SHN_BAD)
10388 {
10389 _bfd_error_handler
10390 /* xgettext:c-format */
10391 (_("%pB: could not find output section %pA for input section %pA"),
10392 flinfo->output_bfd, input_sec->output_section, input_sec);
10393 bfd_set_error (bfd_error_nonrepresentable_section);
10394 eoinfo->failed = true;
10395 return false;
10396 }
10397
10398 /* ELF symbols in relocatable files are section relative,
10399 but in nonrelocatable files they are virtual
10400 addresses. */
10401 sym.st_value = h->root.u.def.value + input_sec->output_offset;
10402 if (!bfd_link_relocatable (flinfo->info))
10403 {
10404 sym.st_value += input_sec->output_section->vma;
10405 if (h->type == STT_TLS)
10406 {
10407 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
10408 if (tls_sec != NULL)
10409 sym.st_value -= tls_sec->vma;
10410 }
10411 }
10412 }
10413 else
10414 {
10415 BFD_ASSERT (input_sec->owner == NULL
10416 || (input_sec->owner->flags & DYNAMIC) != 0);
10417 sym.st_shndx = SHN_UNDEF;
10418 input_sec = bfd_und_section_ptr;
10419 }
10420 }
10421 break;
10422
10423 case bfd_link_hash_common:
10424 input_sec = h->root.u.c.p->section;
10425 sym.st_shndx = bed->common_section_index (input_sec);
10426 sym.st_value = 1 << h->root.u.c.p->alignment_power;
10427 break;
10428
10429 case bfd_link_hash_indirect:
10430 /* These symbols are created by symbol versioning. They point
10431 to the decorated version of the name. For example, if the
10432 symbol foo@@GNU_1.2 is the default, which should be used when
10433 foo is used with no version, then we add an indirect symbol
10434 foo which points to foo@@GNU_1.2. We ignore these symbols,
10435 since the indirected symbol is already in the hash table. */
10436 return true;
10437 }
10438
10439 if (type == STT_COMMON || type == STT_OBJECT)
10440 switch (h->root.type)
10441 {
10442 case bfd_link_hash_common:
10443 type = elf_link_convert_common_type (flinfo->info, type);
10444 break;
10445 case bfd_link_hash_defined:
10446 case bfd_link_hash_defweak:
10447 if (bed->common_definition (&sym))
10448 type = elf_link_convert_common_type (flinfo->info, type);
10449 else
10450 type = STT_OBJECT;
10451 break;
10452 case bfd_link_hash_undefined:
10453 case bfd_link_hash_undefweak:
10454 break;
10455 default:
10456 abort ();
10457 }
10458
10459 if (h->forced_local)
10460 {
10461 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
10462 /* Turn off visibility on local symbol. */
10463 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
10464 }
10465 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10466 else if (h->unique_global && h->def_regular)
10467 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
10468 else if (h->root.type == bfd_link_hash_undefweak
10469 || h->root.type == bfd_link_hash_defweak)
10470 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
10471 else
10472 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
10473 sym.st_target_internal = h->target_internal;
10474
10475 /* Give the processor backend a chance to tweak the symbol value,
10476 and also to finish up anything that needs to be done for this
10477 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
10478 forced local syms when non-shared is due to a historical quirk.
10479 STT_GNU_IFUNC symbol must go through PLT. */
10480 if ((h->type == STT_GNU_IFUNC
10481 && h->def_regular
10482 && !bfd_link_relocatable (flinfo->info))
10483 || ((h->dynindx != -1
10484 || h->forced_local)
10485 && ((bfd_link_pic (flinfo->info)
10486 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
10487 || h->root.type != bfd_link_hash_undefweak))
10488 || !h->forced_local)
10489 && elf_hash_table (flinfo->info)->dynamic_sections_created))
10490 {
10491 if (! ((*bed->elf_backend_finish_dynamic_symbol)
10492 (flinfo->output_bfd, flinfo->info, h, &sym)))
10493 {
10494 eoinfo->failed = true;
10495 return false;
10496 }
10497 }
10498
10499 /* If we are marking the symbol as undefined, and there are no
10500 non-weak references to this symbol from a regular object, then
10501 mark the symbol as weak undefined; if there are non-weak
10502 references, mark the symbol as strong. We can't do this earlier,
10503 because it might not be marked as undefined until the
10504 finish_dynamic_symbol routine gets through with it. */
10505 if (sym.st_shndx == SHN_UNDEF
10506 && h->ref_regular
10507 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
10508 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
10509 {
10510 int bindtype;
10511 type = ELF_ST_TYPE (sym.st_info);
10512
10513 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10514 if (type == STT_GNU_IFUNC)
10515 type = STT_FUNC;
10516
10517 if (h->ref_regular_nonweak)
10518 bindtype = STB_GLOBAL;
10519 else
10520 bindtype = STB_WEAK;
10521 sym.st_info = ELF_ST_INFO (bindtype, type);
10522 }
10523
10524 /* If this is a symbol defined in a dynamic library, don't use the
10525 symbol size from the dynamic library. Relinking an executable
10526 against a new library may introduce gratuitous changes in the
10527 executable's symbols if we keep the size. */
10528 if (sym.st_shndx == SHN_UNDEF
10529 && !h->def_regular
10530 && h->def_dynamic)
10531 sym.st_size = 0;
10532
10533 /* If a non-weak symbol with non-default visibility is not defined
10534 locally, it is a fatal error. */
10535 if (!bfd_link_relocatable (flinfo->info)
10536 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
10537 && ELF_ST_BIND (sym.st_info) != STB_WEAK
10538 && h->root.type == bfd_link_hash_undefined
10539 && !h->def_regular)
10540 {
10541 const char *msg;
10542
10543 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
10544 /* xgettext:c-format */
10545 msg = _("%pB: protected symbol `%s' isn't defined");
10546 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
10547 /* xgettext:c-format */
10548 msg = _("%pB: internal symbol `%s' isn't defined");
10549 else
10550 /* xgettext:c-format */
10551 msg = _("%pB: hidden symbol `%s' isn't defined");
10552 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
10553 bfd_set_error (bfd_error_bad_value);
10554 eoinfo->failed = true;
10555 return false;
10556 }
10557
10558 /* If this symbol should be put in the .dynsym section, then put it
10559 there now. We already know the symbol index. We also fill in
10560 the entry in the .hash section. */
10561 if (h->dynindx != -1
10562 && elf_hash_table (flinfo->info)->dynamic_sections_created
10563 && elf_hash_table (flinfo->info)->dynsym != NULL
10564 && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
10565 {
10566 bfd_byte *esym;
10567
10568 /* Since there is no version information in the dynamic string,
10569 if there is no version info in symbol version section, we will
10570 have a run-time problem if not linking executable, referenced
10571 by shared library, or not bound locally. */
10572 if (h->verinfo.verdef == NULL
10573 && (!bfd_link_executable (flinfo->info)
10574 || h->ref_dynamic
10575 || !h->def_regular))
10576 {
10577 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
10578
10579 if (p && p [1] != '\0')
10580 {
10581 _bfd_error_handler
10582 /* xgettext:c-format */
10583 (_("%pB: no symbol version section for versioned symbol `%s'"),
10584 flinfo->output_bfd, h->root.root.string);
10585 eoinfo->failed = true;
10586 return false;
10587 }
10588 }
10589
10590 sym.st_name = h->dynstr_index;
10591 esym = (elf_hash_table (flinfo->info)->dynsym->contents
10592 + h->dynindx * bed->s->sizeof_sym);
10593 if (!check_dynsym (flinfo->output_bfd, &sym))
10594 {
10595 eoinfo->failed = true;
10596 return false;
10597 }
10598
10599 /* Inform the linker of the addition of this symbol. */
10600
10601 if (flinfo->info->callbacks->ctf_new_dynsym)
10602 flinfo->info->callbacks->ctf_new_dynsym (h->dynindx, &sym);
10603
10604 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
10605
10606 if (flinfo->hash_sec != NULL)
10607 {
10608 size_t hash_entry_size;
10609 bfd_byte *bucketpos;
10610 bfd_vma chain;
10611 size_t bucketcount;
10612 size_t bucket;
10613
10614 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
10615 bucket = h->u.elf_hash_value % bucketcount;
10616
10617 hash_entry_size
10618 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
10619 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
10620 + (bucket + 2) * hash_entry_size);
10621 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
10622 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
10623 bucketpos);
10624 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
10625 ((bfd_byte *) flinfo->hash_sec->contents
10626 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
10627 }
10628
10629 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
10630 {
10631 Elf_Internal_Versym iversym;
10632 Elf_External_Versym *eversym;
10633
10634 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
10635 {
10636 if (h->verinfo.verdef == NULL
10637 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
10638 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
10639 iversym.vs_vers = 1;
10640 else
10641 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
10642 }
10643 else
10644 {
10645 if (h->verinfo.vertree == NULL)
10646 iversym.vs_vers = 1;
10647 else
10648 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
10649 if (flinfo->info->create_default_symver)
10650 iversym.vs_vers++;
10651 }
10652
10653 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
10654 defined locally. */
10655 if (h->versioned == versioned_hidden && h->def_regular)
10656 iversym.vs_vers |= VERSYM_HIDDEN;
10657
10658 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
10659 eversym += h->dynindx;
10660 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
10661 }
10662 }
10663
10664 /* If the symbol is undefined, and we didn't output it to .dynsym,
10665 strip it from .symtab too. Obviously we can't do this for
10666 relocatable output or when needed for --emit-relocs. */
10667 else if (input_sec == bfd_und_section_ptr
10668 && h->indx != -2
10669 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10670 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
10671 && !bfd_link_relocatable (flinfo->info))
10672 return true;
10673
10674 /* Also strip others that we couldn't earlier due to dynamic symbol
10675 processing. */
10676 if (strip)
10677 return true;
10678 if ((input_sec->flags & SEC_EXCLUDE) != 0)
10679 return true;
10680
10681 /* Output a FILE symbol so that following locals are not associated
10682 with the wrong input file. We need one for forced local symbols
10683 if we've seen more than one FILE symbol or when we have exactly
10684 one FILE symbol but global symbols are present in a file other
10685 than the one with the FILE symbol. We also need one if linker
10686 defined symbols are present. In practice these conditions are
10687 always met, so just emit the FILE symbol unconditionally. */
10688 if (eoinfo->localsyms
10689 && !eoinfo->file_sym_done
10690 && eoinfo->flinfo->filesym_count != 0)
10691 {
10692 Elf_Internal_Sym fsym;
10693
10694 memset (&fsym, 0, sizeof (fsym));
10695 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10696 fsym.st_shndx = SHN_ABS;
10697 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10698 bfd_und_section_ptr, NULL))
10699 return false;
10700
10701 eoinfo->file_sym_done = true;
10702 }
10703
10704 indx = bfd_get_symcount (flinfo->output_bfd);
10705 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10706 input_sec, h);
10707 if (ret == 0)
10708 {
10709 eoinfo->failed = true;
10710 return false;
10711 }
10712 else if (ret == 1)
10713 h->indx = indx;
10714 else if (h->indx == -2)
10715 abort();
10716
10717 return true;
10718 }
10719
10720 /* Return TRUE if special handling is done for relocs in SEC against
10721 symbols defined in discarded sections. */
10722
10723 static bool
10724 elf_section_ignore_discarded_relocs (asection *sec)
10725 {
10726 const struct elf_backend_data *bed;
10727
10728 switch (sec->sec_info_type)
10729 {
10730 case SEC_INFO_TYPE_STABS:
10731 case SEC_INFO_TYPE_EH_FRAME:
10732 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10733 return true;
10734 default:
10735 break;
10736 }
10737
10738 bed = get_elf_backend_data (sec->owner);
10739 if (bed->elf_backend_ignore_discarded_relocs != NULL
10740 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10741 return true;
10742
10743 return false;
10744 }
10745
10746 /* Return a mask saying how ld should treat relocations in SEC against
10747 symbols defined in discarded sections. If this function returns
10748 COMPLAIN set, ld will issue a warning message. If this function
10749 returns PRETEND set, and the discarded section was link-once and the
10750 same size as the kept link-once section, ld will pretend that the
10751 symbol was actually defined in the kept section. Otherwise ld will
10752 zero the reloc (at least that is the intent, but some cooperation by
10753 the target dependent code is needed, particularly for REL targets). */
10754
10755 unsigned int
10756 _bfd_elf_default_action_discarded (asection *sec)
10757 {
10758 if (sec->flags & SEC_DEBUGGING)
10759 return PRETEND;
10760
10761 if (strcmp (".eh_frame", sec->name) == 0)
10762 return 0;
10763
10764 if (strcmp (".gcc_except_table", sec->name) == 0)
10765 return 0;
10766
10767 return COMPLAIN | PRETEND;
10768 }
10769
10770 /* Find a match between a section and a member of a section group. */
10771
10772 static asection *
10773 match_group_member (asection *sec, asection *group,
10774 struct bfd_link_info *info)
10775 {
10776 asection *first = elf_next_in_group (group);
10777 asection *s = first;
10778
10779 while (s != NULL)
10780 {
10781 if (bfd_elf_match_symbols_in_sections (s, sec, info))
10782 return s;
10783
10784 s = elf_next_in_group (s);
10785 if (s == first)
10786 break;
10787 }
10788
10789 return NULL;
10790 }
10791
10792 /* Check if the kept section of a discarded section SEC can be used
10793 to replace it. Return the replacement if it is OK. Otherwise return
10794 NULL. */
10795
10796 asection *
10797 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
10798 {
10799 asection *kept;
10800
10801 kept = sec->kept_section;
10802 if (kept != NULL)
10803 {
10804 if ((kept->flags & SEC_GROUP) != 0)
10805 kept = match_group_member (sec, kept, info);
10806 if (kept != NULL)
10807 {
10808 if ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10809 != (kept->rawsize != 0 ? kept->rawsize : kept->size))
10810 kept = NULL;
10811 else
10812 {
10813 /* Get the real kept section. */
10814 asection *next;
10815 for (next = kept->kept_section;
10816 next != NULL;
10817 next = next->kept_section)
10818 kept = next;
10819 }
10820 }
10821 sec->kept_section = kept;
10822 }
10823 return kept;
10824 }
10825
10826 /* Link an input file into the linker output file. This function
10827 handles all the sections and relocations of the input file at once.
10828 This is so that we only have to read the local symbols once, and
10829 don't have to keep them in memory. */
10830
10831 static bool
10832 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
10833 {
10834 int (*relocate_section)
10835 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10836 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10837 bfd *output_bfd;
10838 Elf_Internal_Shdr *symtab_hdr;
10839 size_t locsymcount;
10840 size_t extsymoff;
10841 Elf_Internal_Sym *isymbuf;
10842 Elf_Internal_Sym *isym;
10843 Elf_Internal_Sym *isymend;
10844 long *pindex;
10845 asection **ppsection;
10846 asection *o;
10847 const struct elf_backend_data *bed;
10848 struct elf_link_hash_entry **sym_hashes;
10849 bfd_size_type address_size;
10850 bfd_vma r_type_mask;
10851 int r_sym_shift;
10852 bool have_file_sym = false;
10853
10854 output_bfd = flinfo->output_bfd;
10855 bed = get_elf_backend_data (output_bfd);
10856 relocate_section = bed->elf_backend_relocate_section;
10857
10858 /* If this is a dynamic object, we don't want to do anything here:
10859 we don't want the local symbols, and we don't want the section
10860 contents. */
10861 if ((input_bfd->flags & DYNAMIC) != 0)
10862 return true;
10863
10864 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10865 if (elf_bad_symtab (input_bfd))
10866 {
10867 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10868 extsymoff = 0;
10869 }
10870 else
10871 {
10872 locsymcount = symtab_hdr->sh_info;
10873 extsymoff = symtab_hdr->sh_info;
10874 }
10875
10876 /* Enable GNU OSABI features in the output BFD that are used in the input
10877 BFD. */
10878 if (bed->elf_osabi == ELFOSABI_NONE
10879 || bed->elf_osabi == ELFOSABI_GNU
10880 || bed->elf_osabi == ELFOSABI_FREEBSD)
10881 elf_tdata (output_bfd)->has_gnu_osabi
10882 |= (elf_tdata (input_bfd)->has_gnu_osabi
10883 & (bfd_link_relocatable (flinfo->info)
10884 ? -1 : ~elf_gnu_osabi_retain));
10885
10886 /* Read the local symbols. */
10887 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10888 if (isymbuf == NULL && locsymcount != 0)
10889 {
10890 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
10891 flinfo->internal_syms,
10892 flinfo->external_syms,
10893 flinfo->locsym_shndx);
10894 if (isymbuf == NULL)
10895 return false;
10896 }
10897
10898 /* Find local symbol sections and adjust values of symbols in
10899 SEC_MERGE sections. Write out those local symbols we know are
10900 going into the output file. */
10901 isymend = isymbuf + locsymcount;
10902 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
10903 isym < isymend;
10904 isym++, pindex++, ppsection++)
10905 {
10906 asection *isec;
10907 const char *name;
10908 Elf_Internal_Sym osym;
10909 long indx;
10910 int ret;
10911
10912 *pindex = -1;
10913
10914 if (elf_bad_symtab (input_bfd))
10915 {
10916 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10917 {
10918 *ppsection = NULL;
10919 continue;
10920 }
10921 }
10922
10923 if (isym->st_shndx == SHN_UNDEF)
10924 isec = bfd_und_section_ptr;
10925 else if (isym->st_shndx == SHN_ABS)
10926 isec = bfd_abs_section_ptr;
10927 else if (isym->st_shndx == SHN_COMMON)
10928 isec = bfd_com_section_ptr;
10929 else
10930 {
10931 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10932 if (isec == NULL)
10933 {
10934 /* Don't attempt to output symbols with st_shnx in the
10935 reserved range other than SHN_ABS and SHN_COMMON. */
10936 isec = bfd_und_section_ptr;
10937 }
10938 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
10939 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10940 isym->st_value =
10941 _bfd_merged_section_offset (output_bfd, &isec,
10942 elf_section_data (isec)->sec_info,
10943 isym->st_value);
10944 }
10945
10946 *ppsection = isec;
10947
10948 /* Don't output the first, undefined, symbol. In fact, don't
10949 output any undefined local symbol. */
10950 if (isec == bfd_und_section_ptr)
10951 continue;
10952
10953 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10954 {
10955 /* We never output section symbols. Instead, we use the
10956 section symbol of the corresponding section in the output
10957 file. */
10958 continue;
10959 }
10960
10961 /* If we are stripping all symbols, we don't want to output this
10962 one. */
10963 if (flinfo->info->strip == strip_all)
10964 continue;
10965
10966 /* If we are discarding all local symbols, we don't want to
10967 output this one. If we are generating a relocatable output
10968 file, then some of the local symbols may be required by
10969 relocs; we output them below as we discover that they are
10970 needed. */
10971 if (flinfo->info->discard == discard_all)
10972 continue;
10973
10974 /* If this symbol is defined in a section which we are
10975 discarding, we don't need to keep it. */
10976 if (isym->st_shndx != SHN_UNDEF
10977 && isym->st_shndx < SHN_LORESERVE
10978 && isec->output_section == NULL
10979 && flinfo->info->non_contiguous_regions
10980 && flinfo->info->non_contiguous_regions_warnings)
10981 {
10982 _bfd_error_handler (_("warning: --enable-non-contiguous-regions "
10983 "discards section `%s' from '%s'\n"),
10984 isec->name, bfd_get_filename (isec->owner));
10985 continue;
10986 }
10987
10988 if (isym->st_shndx != SHN_UNDEF
10989 && isym->st_shndx < SHN_LORESERVE
10990 && bfd_section_removed_from_list (output_bfd,
10991 isec->output_section))
10992 continue;
10993
10994 /* Get the name of the symbol. */
10995 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10996 isym->st_name);
10997 if (name == NULL)
10998 return false;
10999
11000 /* See if we are discarding symbols with this name. */
11001 if ((flinfo->info->strip == strip_some
11002 && (bfd_hash_lookup (flinfo->info->keep_hash, name, false, false)
11003 == NULL))
11004 || (((flinfo->info->discard == discard_sec_merge
11005 && (isec->flags & SEC_MERGE)
11006 && !bfd_link_relocatable (flinfo->info))
11007 || flinfo->info->discard == discard_l)
11008 && bfd_is_local_label_name (input_bfd, name)))
11009 continue;
11010
11011 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
11012 {
11013 if (input_bfd->lto_output)
11014 /* -flto puts a temp file name here. This means builds
11015 are not reproducible. Discard the symbol. */
11016 continue;
11017 have_file_sym = true;
11018 flinfo->filesym_count += 1;
11019 }
11020 if (!have_file_sym)
11021 {
11022 /* In the absence of debug info, bfd_find_nearest_line uses
11023 FILE symbols to determine the source file for local
11024 function symbols. Provide a FILE symbol here if input
11025 files lack such, so that their symbols won't be
11026 associated with a previous input file. It's not the
11027 source file, but the best we can do. */
11028 const char *filename;
11029 have_file_sym = true;
11030 flinfo->filesym_count += 1;
11031 memset (&osym, 0, sizeof (osym));
11032 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
11033 osym.st_shndx = SHN_ABS;
11034 if (input_bfd->lto_output)
11035 filename = NULL;
11036 else
11037 filename = lbasename (bfd_get_filename (input_bfd));
11038 if (!elf_link_output_symstrtab (flinfo, filename, &osym,
11039 bfd_abs_section_ptr, NULL))
11040 return false;
11041 }
11042
11043 osym = *isym;
11044
11045 /* Adjust the section index for the output file. */
11046 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11047 isec->output_section);
11048 if (osym.st_shndx == SHN_BAD)
11049 return false;
11050
11051 /* ELF symbols in relocatable files are section relative, but
11052 in executable files they are virtual addresses. Note that
11053 this code assumes that all ELF sections have an associated
11054 BFD section with a reasonable value for output_offset; below
11055 we assume that they also have a reasonable value for
11056 output_section. Any special sections must be set up to meet
11057 these requirements. */
11058 osym.st_value += isec->output_offset;
11059 if (!bfd_link_relocatable (flinfo->info))
11060 {
11061 osym.st_value += isec->output_section->vma;
11062 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
11063 {
11064 /* STT_TLS symbols are relative to PT_TLS segment base. */
11065 if (elf_hash_table (flinfo->info)->tls_sec != NULL)
11066 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
11067 else
11068 osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
11069 STT_NOTYPE);
11070 }
11071 }
11072
11073 indx = bfd_get_symcount (output_bfd);
11074 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
11075 if (ret == 0)
11076 return false;
11077 else if (ret == 1)
11078 *pindex = indx;
11079 }
11080
11081 if (bed->s->arch_size == 32)
11082 {
11083 r_type_mask = 0xff;
11084 r_sym_shift = 8;
11085 address_size = 4;
11086 }
11087 else
11088 {
11089 r_type_mask = 0xffffffff;
11090 r_sym_shift = 32;
11091 address_size = 8;
11092 }
11093
11094 /* Relocate the contents of each section. */
11095 sym_hashes = elf_sym_hashes (input_bfd);
11096 for (o = input_bfd->sections; o != NULL; o = o->next)
11097 {
11098 bfd_byte *contents;
11099
11100 if (! o->linker_mark)
11101 {
11102 /* This section was omitted from the link. */
11103 continue;
11104 }
11105
11106 if (!flinfo->info->resolve_section_groups
11107 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
11108 {
11109 /* Deal with the group signature symbol. */
11110 struct bfd_elf_section_data *sec_data = elf_section_data (o);
11111 unsigned long symndx = sec_data->this_hdr.sh_info;
11112 asection *osec = o->output_section;
11113
11114 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
11115 if (symndx >= locsymcount
11116 || (elf_bad_symtab (input_bfd)
11117 && flinfo->sections[symndx] == NULL))
11118 {
11119 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
11120 while (h->root.type == bfd_link_hash_indirect
11121 || h->root.type == bfd_link_hash_warning)
11122 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11123 /* Arrange for symbol to be output. */
11124 h->indx = -2;
11125 elf_section_data (osec)->this_hdr.sh_info = -2;
11126 }
11127 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
11128 {
11129 /* We'll use the output section target_index. */
11130 asection *sec = flinfo->sections[symndx]->output_section;
11131 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
11132 }
11133 else
11134 {
11135 if (flinfo->indices[symndx] == -1)
11136 {
11137 /* Otherwise output the local symbol now. */
11138 Elf_Internal_Sym sym = isymbuf[symndx];
11139 asection *sec = flinfo->sections[symndx]->output_section;
11140 const char *name;
11141 long indx;
11142 int ret;
11143
11144 name = bfd_elf_string_from_elf_section (input_bfd,
11145 symtab_hdr->sh_link,
11146 sym.st_name);
11147 if (name == NULL)
11148 return false;
11149
11150 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11151 sec);
11152 if (sym.st_shndx == SHN_BAD)
11153 return false;
11154
11155 sym.st_value += o->output_offset;
11156
11157 indx = bfd_get_symcount (output_bfd);
11158 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
11159 NULL);
11160 if (ret == 0)
11161 return false;
11162 else if (ret == 1)
11163 flinfo->indices[symndx] = indx;
11164 else
11165 abort ();
11166 }
11167 elf_section_data (osec)->this_hdr.sh_info
11168 = flinfo->indices[symndx];
11169 }
11170 }
11171
11172 if ((o->flags & SEC_HAS_CONTENTS) == 0
11173 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
11174 continue;
11175
11176 if ((o->flags & SEC_LINKER_CREATED) != 0)
11177 {
11178 /* Section was created by _bfd_elf_link_create_dynamic_sections
11179 or somesuch. */
11180 continue;
11181 }
11182
11183 /* Get the contents of the section. They have been cached by a
11184 relaxation routine. Note that o is a section in an input
11185 file, so the contents field will not have been set by any of
11186 the routines which work on output files. */
11187 if (elf_section_data (o)->this_hdr.contents != NULL)
11188 {
11189 contents = elf_section_data (o)->this_hdr.contents;
11190 if (bed->caches_rawsize
11191 && o->rawsize != 0
11192 && o->rawsize < o->size)
11193 {
11194 memcpy (flinfo->contents, contents, o->rawsize);
11195 contents = flinfo->contents;
11196 }
11197 }
11198 else
11199 {
11200 contents = flinfo->contents;
11201 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
11202 return false;
11203 }
11204
11205 if ((o->flags & SEC_RELOC) != 0)
11206 {
11207 Elf_Internal_Rela *internal_relocs;
11208 Elf_Internal_Rela *rel, *relend;
11209 int action_discarded;
11210 int ret;
11211
11212 /* Get the swapped relocs. */
11213 internal_relocs
11214 = _bfd_elf_link_info_read_relocs (input_bfd, flinfo->info, o,
11215 flinfo->external_relocs,
11216 flinfo->internal_relocs,
11217 false);
11218 if (internal_relocs == NULL
11219 && o->reloc_count > 0)
11220 return false;
11221
11222 /* We need to reverse-copy input .ctors/.dtors sections if
11223 they are placed in .init_array/.finit_array for output. */
11224 if (o->size > address_size
11225 && ((startswith (o->name, ".ctors")
11226 && strcmp (o->output_section->name,
11227 ".init_array") == 0)
11228 || (startswith (o->name, ".dtors")
11229 && strcmp (o->output_section->name,
11230 ".fini_array") == 0))
11231 && (o->name[6] == 0 || o->name[6] == '.'))
11232 {
11233 if (o->size * bed->s->int_rels_per_ext_rel
11234 != o->reloc_count * address_size)
11235 {
11236 _bfd_error_handler
11237 /* xgettext:c-format */
11238 (_("error: %pB: size of section %pA is not "
11239 "multiple of address size"),
11240 input_bfd, o);
11241 bfd_set_error (bfd_error_bad_value);
11242 return false;
11243 }
11244 o->flags |= SEC_ELF_REVERSE_COPY;
11245 }
11246
11247 action_discarded = -1;
11248 if (!elf_section_ignore_discarded_relocs (o))
11249 action_discarded = (*bed->action_discarded) (o);
11250
11251 /* Run through the relocs evaluating complex reloc symbols and
11252 looking for relocs against symbols from discarded sections
11253 or section symbols from removed link-once sections.
11254 Complain about relocs against discarded sections. Zero
11255 relocs against removed link-once sections. */
11256
11257 rel = internal_relocs;
11258 relend = rel + o->reloc_count;
11259 for ( ; rel < relend; rel++)
11260 {
11261 unsigned long r_symndx = rel->r_info >> r_sym_shift;
11262 unsigned int s_type;
11263 asection **ps, *sec;
11264 struct elf_link_hash_entry *h = NULL;
11265 const char *sym_name;
11266
11267 if (r_symndx == STN_UNDEF)
11268 continue;
11269
11270 if (r_symndx >= locsymcount
11271 || (elf_bad_symtab (input_bfd)
11272 && flinfo->sections[r_symndx] == NULL))
11273 {
11274 h = sym_hashes[r_symndx - extsymoff];
11275
11276 /* Badly formatted input files can contain relocs that
11277 reference non-existant symbols. Check here so that
11278 we do not seg fault. */
11279 if (h == NULL)
11280 {
11281 _bfd_error_handler
11282 /* xgettext:c-format */
11283 (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
11284 "that references a non-existent global symbol"),
11285 input_bfd, (uint64_t) rel->r_info, o);
11286 bfd_set_error (bfd_error_bad_value);
11287 return false;
11288 }
11289
11290 while (h->root.type == bfd_link_hash_indirect
11291 || h->root.type == bfd_link_hash_warning)
11292 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11293
11294 s_type = h->type;
11295
11296 /* If a plugin symbol is referenced from a non-IR file,
11297 mark the symbol as undefined. Note that the
11298 linker may attach linker created dynamic sections
11299 to the plugin bfd. Symbols defined in linker
11300 created sections are not plugin symbols. */
11301 if ((h->root.non_ir_ref_regular
11302 || h->root.non_ir_ref_dynamic)
11303 && (h->root.type == bfd_link_hash_defined
11304 || h->root.type == bfd_link_hash_defweak)
11305 && (h->root.u.def.section->flags
11306 & SEC_LINKER_CREATED) == 0
11307 && h->root.u.def.section->owner != NULL
11308 && (h->root.u.def.section->owner->flags
11309 & BFD_PLUGIN) != 0)
11310 {
11311 h->root.type = bfd_link_hash_undefined;
11312 h->root.u.undef.abfd = h->root.u.def.section->owner;
11313 }
11314
11315 ps = NULL;
11316 if (h->root.type == bfd_link_hash_defined
11317 || h->root.type == bfd_link_hash_defweak)
11318 ps = &h->root.u.def.section;
11319
11320 sym_name = h->root.root.string;
11321 }
11322 else
11323 {
11324 Elf_Internal_Sym *sym = isymbuf + r_symndx;
11325
11326 s_type = ELF_ST_TYPE (sym->st_info);
11327 ps = &flinfo->sections[r_symndx];
11328 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
11329 sym, *ps);
11330 }
11331
11332 if ((s_type == STT_RELC || s_type == STT_SRELC)
11333 && !bfd_link_relocatable (flinfo->info))
11334 {
11335 bfd_vma val;
11336 bfd_vma dot = (rel->r_offset
11337 + o->output_offset + o->output_section->vma);
11338 #ifdef DEBUG
11339 printf ("Encountered a complex symbol!");
11340 printf (" (input_bfd %s, section %s, reloc %ld\n",
11341 bfd_get_filename (input_bfd), o->name,
11342 (long) (rel - internal_relocs));
11343 printf (" symbol: idx %8.8lx, name %s\n",
11344 r_symndx, sym_name);
11345 printf (" reloc : info %8.8lx, addr %8.8lx\n",
11346 (unsigned long) rel->r_info,
11347 (unsigned long) rel->r_offset);
11348 #endif
11349 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
11350 isymbuf, locsymcount, s_type == STT_SRELC))
11351 return false;
11352
11353 /* Symbol evaluated OK. Update to absolute value. */
11354 set_symbol_value (input_bfd, isymbuf, locsymcount,
11355 r_symndx, val);
11356 continue;
11357 }
11358
11359 if (action_discarded != -1 && ps != NULL)
11360 {
11361 /* Complain if the definition comes from a
11362 discarded section. */
11363 if ((sec = *ps) != NULL && discarded_section (sec))
11364 {
11365 BFD_ASSERT (r_symndx != STN_UNDEF);
11366 if (action_discarded & COMPLAIN)
11367 (*flinfo->info->callbacks->einfo)
11368 /* xgettext:c-format */
11369 (_("%X`%s' referenced in section `%pA' of %pB: "
11370 "defined in discarded section `%pA' of %pB\n"),
11371 sym_name, o, input_bfd, sec, sec->owner);
11372
11373 /* Try to do the best we can to support buggy old
11374 versions of gcc. Pretend that the symbol is
11375 really defined in the kept linkonce section.
11376 FIXME: This is quite broken. Modifying the
11377 symbol here means we will be changing all later
11378 uses of the symbol, not just in this section. */
11379 if (action_discarded & PRETEND)
11380 {
11381 asection *kept;
11382
11383 kept = _bfd_elf_check_kept_section (sec,
11384 flinfo->info);
11385 if (kept != NULL)
11386 {
11387 *ps = kept;
11388 continue;
11389 }
11390 }
11391 }
11392 }
11393 }
11394
11395 /* Relocate the section by invoking a back end routine.
11396
11397 The back end routine is responsible for adjusting the
11398 section contents as necessary, and (if using Rela relocs
11399 and generating a relocatable output file) adjusting the
11400 reloc addend as necessary.
11401
11402 The back end routine does not have to worry about setting
11403 the reloc address or the reloc symbol index.
11404
11405 The back end routine is given a pointer to the swapped in
11406 internal symbols, and can access the hash table entries
11407 for the external symbols via elf_sym_hashes (input_bfd).
11408
11409 When generating relocatable output, the back end routine
11410 must handle STB_LOCAL/STT_SECTION symbols specially. The
11411 output symbol is going to be a section symbol
11412 corresponding to the output section, which will require
11413 the addend to be adjusted. */
11414
11415 ret = (*relocate_section) (output_bfd, flinfo->info,
11416 input_bfd, o, contents,
11417 internal_relocs,
11418 isymbuf,
11419 flinfo->sections);
11420 if (!ret)
11421 return false;
11422
11423 if (ret == 2
11424 || bfd_link_relocatable (flinfo->info)
11425 || flinfo->info->emitrelocations)
11426 {
11427 Elf_Internal_Rela *irela;
11428 Elf_Internal_Rela *irelaend, *irelamid;
11429 bfd_vma last_offset;
11430 struct elf_link_hash_entry **rel_hash;
11431 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
11432 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
11433 unsigned int next_erel;
11434 bool rela_normal;
11435 struct bfd_elf_section_data *esdi, *esdo;
11436
11437 esdi = elf_section_data (o);
11438 esdo = elf_section_data (o->output_section);
11439 rela_normal = false;
11440
11441 /* Adjust the reloc addresses and symbol indices. */
11442
11443 irela = internal_relocs;
11444 irelaend = irela + o->reloc_count;
11445 rel_hash = esdo->rel.hashes + esdo->rel.count;
11446 /* We start processing the REL relocs, if any. When we reach
11447 IRELAMID in the loop, we switch to the RELA relocs. */
11448 irelamid = irela;
11449 if (esdi->rel.hdr != NULL)
11450 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
11451 * bed->s->int_rels_per_ext_rel);
11452 rel_hash_list = rel_hash;
11453 rela_hash_list = NULL;
11454 last_offset = o->output_offset;
11455 if (!bfd_link_relocatable (flinfo->info))
11456 last_offset += o->output_section->vma;
11457 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
11458 {
11459 unsigned long r_symndx;
11460 asection *sec;
11461 Elf_Internal_Sym sym;
11462
11463 if (next_erel == bed->s->int_rels_per_ext_rel)
11464 {
11465 rel_hash++;
11466 next_erel = 0;
11467 }
11468
11469 if (irela == irelamid)
11470 {
11471 rel_hash = esdo->rela.hashes + esdo->rela.count;
11472 rela_hash_list = rel_hash;
11473 rela_normal = bed->rela_normal;
11474 }
11475
11476 irela->r_offset = _bfd_elf_section_offset (output_bfd,
11477 flinfo->info, o,
11478 irela->r_offset);
11479 if (irela->r_offset >= (bfd_vma) -2)
11480 {
11481 /* This is a reloc for a deleted entry or somesuch.
11482 Turn it into an R_*_NONE reloc, at the same
11483 offset as the last reloc. elf_eh_frame.c and
11484 bfd_elf_discard_info rely on reloc offsets
11485 being ordered. */
11486 irela->r_offset = last_offset;
11487 irela->r_info = 0;
11488 irela->r_addend = 0;
11489 continue;
11490 }
11491
11492 irela->r_offset += o->output_offset;
11493
11494 /* Relocs in an executable have to be virtual addresses. */
11495 if (!bfd_link_relocatable (flinfo->info))
11496 irela->r_offset += o->output_section->vma;
11497
11498 last_offset = irela->r_offset;
11499
11500 r_symndx = irela->r_info >> r_sym_shift;
11501 if (r_symndx == STN_UNDEF)
11502 continue;
11503
11504 if (r_symndx >= locsymcount
11505 || (elf_bad_symtab (input_bfd)
11506 && flinfo->sections[r_symndx] == NULL))
11507 {
11508 struct elf_link_hash_entry *rh;
11509 unsigned long indx;
11510
11511 /* This is a reloc against a global symbol. We
11512 have not yet output all the local symbols, so
11513 we do not know the symbol index of any global
11514 symbol. We set the rel_hash entry for this
11515 reloc to point to the global hash table entry
11516 for this symbol. The symbol index is then
11517 set at the end of bfd_elf_final_link. */
11518 indx = r_symndx - extsymoff;
11519 rh = elf_sym_hashes (input_bfd)[indx];
11520 while (rh->root.type == bfd_link_hash_indirect
11521 || rh->root.type == bfd_link_hash_warning)
11522 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
11523
11524 /* Setting the index to -2 tells
11525 elf_link_output_extsym that this symbol is
11526 used by a reloc. */
11527 BFD_ASSERT (rh->indx < 0);
11528 rh->indx = -2;
11529 *rel_hash = rh;
11530
11531 continue;
11532 }
11533
11534 /* This is a reloc against a local symbol. */
11535
11536 *rel_hash = NULL;
11537 sym = isymbuf[r_symndx];
11538 sec = flinfo->sections[r_symndx];
11539 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
11540 {
11541 /* I suppose the backend ought to fill in the
11542 section of any STT_SECTION symbol against a
11543 processor specific section. */
11544 r_symndx = STN_UNDEF;
11545 if (bfd_is_abs_section (sec))
11546 ;
11547 else if (sec == NULL || sec->owner == NULL)
11548 {
11549 bfd_set_error (bfd_error_bad_value);
11550 return false;
11551 }
11552 else
11553 {
11554 asection *osec = sec->output_section;
11555
11556 /* If we have discarded a section, the output
11557 section will be the absolute section. In
11558 case of discarded SEC_MERGE sections, use
11559 the kept section. relocate_section should
11560 have already handled discarded linkonce
11561 sections. */
11562 if (bfd_is_abs_section (osec)
11563 && sec->kept_section != NULL
11564 && sec->kept_section->output_section != NULL)
11565 {
11566 osec = sec->kept_section->output_section;
11567 irela->r_addend -= osec->vma;
11568 }
11569
11570 if (!bfd_is_abs_section (osec))
11571 {
11572 r_symndx = osec->target_index;
11573 if (r_symndx == STN_UNDEF)
11574 {
11575 irela->r_addend += osec->vma;
11576 osec = _bfd_nearby_section (output_bfd, osec,
11577 osec->vma);
11578 irela->r_addend -= osec->vma;
11579 r_symndx = osec->target_index;
11580 }
11581 }
11582 }
11583
11584 /* Adjust the addend according to where the
11585 section winds up in the output section. */
11586 if (rela_normal)
11587 irela->r_addend += sec->output_offset;
11588 }
11589 else
11590 {
11591 if (flinfo->indices[r_symndx] == -1)
11592 {
11593 unsigned long shlink;
11594 const char *name;
11595 asection *osec;
11596 long indx;
11597
11598 if (flinfo->info->strip == strip_all)
11599 {
11600 /* You can't do ld -r -s. */
11601 bfd_set_error (bfd_error_invalid_operation);
11602 return false;
11603 }
11604
11605 /* This symbol was skipped earlier, but
11606 since it is needed by a reloc, we
11607 must output it now. */
11608 shlink = symtab_hdr->sh_link;
11609 name = (bfd_elf_string_from_elf_section
11610 (input_bfd, shlink, sym.st_name));
11611 if (name == NULL)
11612 return false;
11613
11614 osec = sec->output_section;
11615 sym.st_shndx =
11616 _bfd_elf_section_from_bfd_section (output_bfd,
11617 osec);
11618 if (sym.st_shndx == SHN_BAD)
11619 return false;
11620
11621 sym.st_value += sec->output_offset;
11622 if (!bfd_link_relocatable (flinfo->info))
11623 {
11624 sym.st_value += osec->vma;
11625 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
11626 {
11627 struct elf_link_hash_table *htab
11628 = elf_hash_table (flinfo->info);
11629
11630 /* STT_TLS symbols are relative to PT_TLS
11631 segment base. */
11632 if (htab->tls_sec != NULL)
11633 sym.st_value -= htab->tls_sec->vma;
11634 else
11635 sym.st_info
11636 = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
11637 STT_NOTYPE);
11638 }
11639 }
11640
11641 indx = bfd_get_symcount (output_bfd);
11642 ret = elf_link_output_symstrtab (flinfo, name,
11643 &sym, sec,
11644 NULL);
11645 if (ret == 0)
11646 return false;
11647 else if (ret == 1)
11648 flinfo->indices[r_symndx] = indx;
11649 else
11650 abort ();
11651 }
11652
11653 r_symndx = flinfo->indices[r_symndx];
11654 }
11655
11656 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
11657 | (irela->r_info & r_type_mask));
11658 }
11659
11660 /* Swap out the relocs. */
11661 input_rel_hdr = esdi->rel.hdr;
11662 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
11663 {
11664 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11665 input_rel_hdr,
11666 internal_relocs,
11667 rel_hash_list))
11668 return false;
11669 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
11670 * bed->s->int_rels_per_ext_rel);
11671 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
11672 }
11673
11674 input_rela_hdr = esdi->rela.hdr;
11675 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
11676 {
11677 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11678 input_rela_hdr,
11679 internal_relocs,
11680 rela_hash_list))
11681 return false;
11682 }
11683 }
11684 }
11685
11686 /* Write out the modified section contents. */
11687 if (bed->elf_backend_write_section
11688 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
11689 contents))
11690 {
11691 /* Section written out. */
11692 }
11693 else switch (o->sec_info_type)
11694 {
11695 case SEC_INFO_TYPE_STABS:
11696 if (! (_bfd_write_section_stabs
11697 (output_bfd,
11698 &elf_hash_table (flinfo->info)->stab_info,
11699 o, &elf_section_data (o)->sec_info, contents)))
11700 return false;
11701 break;
11702 case SEC_INFO_TYPE_MERGE:
11703 if (! _bfd_write_merged_section (output_bfd, o,
11704 elf_section_data (o)->sec_info))
11705 return false;
11706 break;
11707 case SEC_INFO_TYPE_EH_FRAME:
11708 {
11709 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
11710 o, contents))
11711 return false;
11712 }
11713 break;
11714 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11715 {
11716 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11717 flinfo->info,
11718 o, contents))
11719 return false;
11720 }
11721 break;
11722 default:
11723 {
11724 if (! (o->flags & SEC_EXCLUDE))
11725 {
11726 file_ptr offset = (file_ptr) o->output_offset;
11727 bfd_size_type todo = o->size;
11728
11729 offset *= bfd_octets_per_byte (output_bfd, o);
11730
11731 if ((o->flags & SEC_ELF_REVERSE_COPY))
11732 {
11733 /* Reverse-copy input section to output. */
11734 do
11735 {
11736 todo -= address_size;
11737 if (! bfd_set_section_contents (output_bfd,
11738 o->output_section,
11739 contents + todo,
11740 offset,
11741 address_size))
11742 return false;
11743 if (todo == 0)
11744 break;
11745 offset += address_size;
11746 }
11747 while (1);
11748 }
11749 else if (! bfd_set_section_contents (output_bfd,
11750 o->output_section,
11751 contents,
11752 offset, todo))
11753 return false;
11754 }
11755 }
11756 break;
11757 }
11758 }
11759
11760 return true;
11761 }
11762
11763 /* Generate a reloc when linking an ELF file. This is a reloc
11764 requested by the linker, and does not come from any input file. This
11765 is used to build constructor and destructor tables when linking
11766 with -Ur. */
11767
11768 static bool
11769 elf_reloc_link_order (bfd *output_bfd,
11770 struct bfd_link_info *info,
11771 asection *output_section,
11772 struct bfd_link_order *link_order)
11773 {
11774 reloc_howto_type *howto;
11775 long indx;
11776 bfd_vma offset;
11777 bfd_vma addend;
11778 struct bfd_elf_section_reloc_data *reldata;
11779 struct elf_link_hash_entry **rel_hash_ptr;
11780 Elf_Internal_Shdr *rel_hdr;
11781 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11782 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11783 bfd_byte *erel;
11784 unsigned int i;
11785 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
11786
11787 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11788 if (howto == NULL)
11789 {
11790 bfd_set_error (bfd_error_bad_value);
11791 return false;
11792 }
11793
11794 addend = link_order->u.reloc.p->addend;
11795
11796 if (esdo->rel.hdr)
11797 reldata = &esdo->rel;
11798 else if (esdo->rela.hdr)
11799 reldata = &esdo->rela;
11800 else
11801 {
11802 reldata = NULL;
11803 BFD_ASSERT (0);
11804 }
11805
11806 /* Figure out the symbol index. */
11807 rel_hash_ptr = reldata->hashes + reldata->count;
11808 if (link_order->type == bfd_section_reloc_link_order)
11809 {
11810 indx = link_order->u.reloc.p->u.section->target_index;
11811 BFD_ASSERT (indx != 0);
11812 *rel_hash_ptr = NULL;
11813 }
11814 else
11815 {
11816 struct elf_link_hash_entry *h;
11817
11818 /* Treat a reloc against a defined symbol as though it were
11819 actually against the section. */
11820 h = ((struct elf_link_hash_entry *)
11821 bfd_wrapped_link_hash_lookup (output_bfd, info,
11822 link_order->u.reloc.p->u.name,
11823 false, false, true));
11824 if (h != NULL
11825 && (h->root.type == bfd_link_hash_defined
11826 || h->root.type == bfd_link_hash_defweak))
11827 {
11828 asection *section;
11829
11830 section = h->root.u.def.section;
11831 indx = section->output_section->target_index;
11832 *rel_hash_ptr = NULL;
11833 /* It seems that we ought to add the symbol value to the
11834 addend here, but in practice it has already been added
11835 because it was passed to constructor_callback. */
11836 addend += section->output_section->vma + section->output_offset;
11837 }
11838 else if (h != NULL)
11839 {
11840 /* Setting the index to -2 tells elf_link_output_extsym that
11841 this symbol is used by a reloc. */
11842 h->indx = -2;
11843 *rel_hash_ptr = h;
11844 indx = 0;
11845 }
11846 else
11847 {
11848 (*info->callbacks->unattached_reloc)
11849 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
11850 indx = 0;
11851 }
11852 }
11853
11854 /* If this is an inplace reloc, we must write the addend into the
11855 object file. */
11856 if (howto->partial_inplace && addend != 0)
11857 {
11858 bfd_size_type size;
11859 bfd_reloc_status_type rstat;
11860 bfd_byte *buf;
11861 bool ok;
11862 const char *sym_name;
11863 bfd_size_type octets;
11864
11865 size = (bfd_size_type) bfd_get_reloc_size (howto);
11866 buf = (bfd_byte *) bfd_zmalloc (size);
11867 if (buf == NULL && size != 0)
11868 return false;
11869 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11870 switch (rstat)
11871 {
11872 case bfd_reloc_ok:
11873 break;
11874
11875 default:
11876 case bfd_reloc_outofrange:
11877 abort ();
11878
11879 case bfd_reloc_overflow:
11880 if (link_order->type == bfd_section_reloc_link_order)
11881 sym_name = bfd_section_name (link_order->u.reloc.p->u.section);
11882 else
11883 sym_name = link_order->u.reloc.p->u.name;
11884 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11885 howto->name, addend, NULL, NULL,
11886 (bfd_vma) 0);
11887 break;
11888 }
11889
11890 octets = link_order->offset * bfd_octets_per_byte (output_bfd,
11891 output_section);
11892 ok = bfd_set_section_contents (output_bfd, output_section, buf,
11893 octets, size);
11894 free (buf);
11895 if (! ok)
11896 return false;
11897 }
11898
11899 /* The address of a reloc is relative to the section in a
11900 relocatable file, and is a virtual address in an executable
11901 file. */
11902 offset = link_order->offset;
11903 if (! bfd_link_relocatable (info))
11904 offset += output_section->vma;
11905
11906 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11907 {
11908 irel[i].r_offset = offset;
11909 irel[i].r_info = 0;
11910 irel[i].r_addend = 0;
11911 }
11912 if (bed->s->arch_size == 32)
11913 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11914 else
11915 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11916
11917 rel_hdr = reldata->hdr;
11918 erel = rel_hdr->contents;
11919 if (rel_hdr->sh_type == SHT_REL)
11920 {
11921 erel += reldata->count * bed->s->sizeof_rel;
11922 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11923 }
11924 else
11925 {
11926 irel[0].r_addend = addend;
11927 erel += reldata->count * bed->s->sizeof_rela;
11928 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11929 }
11930
11931 ++reldata->count;
11932
11933 return true;
11934 }
11935
11936 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11937 Returns TRUE upon success, FALSE otherwise. */
11938
11939 static bool
11940 elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11941 {
11942 bool ret = false;
11943 bfd *implib_bfd;
11944 const struct elf_backend_data *bed;
11945 flagword flags;
11946 enum bfd_architecture arch;
11947 unsigned int mach;
11948 asymbol **sympp = NULL;
11949 long symsize;
11950 long symcount;
11951 long src_count;
11952 elf_symbol_type *osymbuf;
11953 size_t amt;
11954
11955 implib_bfd = info->out_implib_bfd;
11956 bed = get_elf_backend_data (abfd);
11957
11958 if (!bfd_set_format (implib_bfd, bfd_object))
11959 return false;
11960
11961 /* Use flag from executable but make it a relocatable object. */
11962 flags = bfd_get_file_flags (abfd);
11963 flags &= ~HAS_RELOC;
11964 if (!bfd_set_start_address (implib_bfd, 0)
11965 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
11966 return false;
11967
11968 /* Copy architecture of output file to import library file. */
11969 arch = bfd_get_arch (abfd);
11970 mach = bfd_get_mach (abfd);
11971 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11972 && (abfd->target_defaulted
11973 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11974 return false;
11975
11976 /* Get symbol table size. */
11977 symsize = bfd_get_symtab_upper_bound (abfd);
11978 if (symsize < 0)
11979 return false;
11980
11981 /* Read in the symbol table. */
11982 sympp = (asymbol **) bfd_malloc (symsize);
11983 if (sympp == NULL)
11984 return false;
11985
11986 symcount = bfd_canonicalize_symtab (abfd, sympp);
11987 if (symcount < 0)
11988 goto free_sym_buf;
11989
11990 /* Allow the BFD backend to copy any private header data it
11991 understands from the output BFD to the import library BFD. */
11992 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11993 goto free_sym_buf;
11994
11995 /* Filter symbols to appear in the import library. */
11996 if (bed->elf_backend_filter_implib_symbols)
11997 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11998 symcount);
11999 else
12000 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
12001 if (symcount == 0)
12002 {
12003 bfd_set_error (bfd_error_no_symbols);
12004 _bfd_error_handler (_("%pB: no symbol found for import library"),
12005 implib_bfd);
12006 goto free_sym_buf;
12007 }
12008
12009
12010 /* Make symbols absolute. */
12011 amt = symcount * sizeof (*osymbuf);
12012 osymbuf = (elf_symbol_type *) bfd_alloc (implib_bfd, amt);
12013 if (osymbuf == NULL)
12014 goto free_sym_buf;
12015
12016 for (src_count = 0; src_count < symcount; src_count++)
12017 {
12018 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
12019 sizeof (*osymbuf));
12020 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
12021 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
12022 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
12023 osymbuf[src_count].internal_elf_sym.st_value =
12024 osymbuf[src_count].symbol.value;
12025 sympp[src_count] = &osymbuf[src_count].symbol;
12026 }
12027
12028 bfd_set_symtab (implib_bfd, sympp, symcount);
12029
12030 /* Allow the BFD backend to copy any private data it understands
12031 from the output BFD to the import library BFD. This is done last
12032 to permit the routine to look at the filtered symbol table. */
12033 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
12034 goto free_sym_buf;
12035
12036 if (!bfd_close (implib_bfd))
12037 goto free_sym_buf;
12038
12039 ret = true;
12040
12041 free_sym_buf:
12042 free (sympp);
12043 return ret;
12044 }
12045
12046 static void
12047 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
12048 {
12049 asection *o;
12050
12051 if (flinfo->symstrtab != NULL)
12052 _bfd_elf_strtab_free (flinfo->symstrtab);
12053 free (flinfo->contents);
12054 free (flinfo->external_relocs);
12055 free (flinfo->internal_relocs);
12056 free (flinfo->external_syms);
12057 free (flinfo->locsym_shndx);
12058 free (flinfo->internal_syms);
12059 free (flinfo->indices);
12060 free (flinfo->sections);
12061 if (flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1)
12062 free (flinfo->symshndxbuf);
12063 for (o = obfd->sections; o != NULL; o = o->next)
12064 {
12065 struct bfd_elf_section_data *esdo = elf_section_data (o);
12066 free (esdo->rel.hashes);
12067 free (esdo->rela.hashes);
12068 }
12069 }
12070
12071 /* Do the final step of an ELF link. */
12072
12073 bool
12074 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
12075 {
12076 bool dynamic;
12077 bool emit_relocs;
12078 bfd *dynobj;
12079 struct elf_final_link_info flinfo;
12080 asection *o;
12081 struct bfd_link_order *p;
12082 bfd *sub;
12083 bfd_size_type max_contents_size;
12084 bfd_size_type max_external_reloc_size;
12085 bfd_size_type max_internal_reloc_count;
12086 bfd_size_type max_sym_count;
12087 bfd_size_type max_sym_shndx_count;
12088 Elf_Internal_Sym elfsym;
12089 unsigned int i;
12090 Elf_Internal_Shdr *symtab_hdr;
12091 Elf_Internal_Shdr *symtab_shndx_hdr;
12092 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12093 struct elf_outext_info eoinfo;
12094 bool merged;
12095 size_t relativecount = 0;
12096 asection *reldyn = 0;
12097 bfd_size_type amt;
12098 asection *attr_section = NULL;
12099 bfd_vma attr_size = 0;
12100 const char *std_attrs_section;
12101 struct elf_link_hash_table *htab = elf_hash_table (info);
12102 bool sections_removed;
12103 bool ret;
12104
12105 if (!is_elf_hash_table (&htab->root))
12106 return false;
12107
12108 if (bfd_link_pic (info))
12109 abfd->flags |= DYNAMIC;
12110
12111 dynamic = htab->dynamic_sections_created;
12112 dynobj = htab->dynobj;
12113
12114 emit_relocs = (bfd_link_relocatable (info)
12115 || info->emitrelocations);
12116
12117 memset (&flinfo, 0, sizeof (flinfo));
12118 flinfo.info = info;
12119 flinfo.output_bfd = abfd;
12120 flinfo.symstrtab = _bfd_elf_strtab_init ();
12121 if (flinfo.symstrtab == NULL)
12122 return false;
12123
12124 if (! dynamic)
12125 {
12126 flinfo.hash_sec = NULL;
12127 flinfo.symver_sec = NULL;
12128 }
12129 else
12130 {
12131 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
12132 /* Note that dynsym_sec can be NULL (on VMS). */
12133 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
12134 /* Note that it is OK if symver_sec is NULL. */
12135 }
12136
12137 if (info->unique_symbol
12138 && !bfd_hash_table_init (&flinfo.local_hash_table,
12139 local_hash_newfunc,
12140 sizeof (struct local_hash_entry)))
12141 return false;
12142
12143 /* The object attributes have been merged. Remove the input
12144 sections from the link, and set the contents of the output
12145 section. */
12146 sections_removed = false;
12147 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
12148 for (o = abfd->sections; o != NULL; o = o->next)
12149 {
12150 bool remove_section = false;
12151
12152 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
12153 || strcmp (o->name, ".gnu.attributes") == 0)
12154 {
12155 for (p = o->map_head.link_order; p != NULL; p = p->next)
12156 {
12157 asection *input_section;
12158
12159 if (p->type != bfd_indirect_link_order)
12160 continue;
12161 input_section = p->u.indirect.section;
12162 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12163 elf_link_input_bfd ignores this section. */
12164 input_section->flags &= ~SEC_HAS_CONTENTS;
12165 }
12166
12167 attr_size = bfd_elf_obj_attr_size (abfd);
12168 bfd_set_section_size (o, attr_size);
12169 /* Skip this section later on. */
12170 o->map_head.link_order = NULL;
12171 if (attr_size)
12172 attr_section = o;
12173 else
12174 remove_section = true;
12175 }
12176 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
12177 {
12178 /* Remove empty group section from linker output. */
12179 remove_section = true;
12180 }
12181 if (remove_section)
12182 {
12183 o->flags |= SEC_EXCLUDE;
12184 bfd_section_list_remove (abfd, o);
12185 abfd->section_count--;
12186 sections_removed = true;
12187 }
12188 }
12189 if (sections_removed)
12190 _bfd_fix_excluded_sec_syms (abfd, info);
12191
12192 /* Count up the number of relocations we will output for each output
12193 section, so that we know the sizes of the reloc sections. We
12194 also figure out some maximum sizes. */
12195 max_contents_size = 0;
12196 max_external_reloc_size = 0;
12197 max_internal_reloc_count = 0;
12198 max_sym_count = 0;
12199 max_sym_shndx_count = 0;
12200 merged = false;
12201 for (o = abfd->sections; o != NULL; o = o->next)
12202 {
12203 struct bfd_elf_section_data *esdo = elf_section_data (o);
12204 o->reloc_count = 0;
12205
12206 for (p = o->map_head.link_order; p != NULL; p = p->next)
12207 {
12208 unsigned int reloc_count = 0;
12209 unsigned int additional_reloc_count = 0;
12210 struct bfd_elf_section_data *esdi = NULL;
12211
12212 if (p->type == bfd_section_reloc_link_order
12213 || p->type == bfd_symbol_reloc_link_order)
12214 reloc_count = 1;
12215 else if (p->type == bfd_indirect_link_order)
12216 {
12217 asection *sec;
12218
12219 sec = p->u.indirect.section;
12220
12221 /* Mark all sections which are to be included in the
12222 link. This will normally be every section. We need
12223 to do this so that we can identify any sections which
12224 the linker has decided to not include. */
12225 sec->linker_mark = true;
12226
12227 if (sec->flags & SEC_MERGE)
12228 merged = true;
12229
12230 if (sec->rawsize > max_contents_size)
12231 max_contents_size = sec->rawsize;
12232 if (sec->size > max_contents_size)
12233 max_contents_size = sec->size;
12234
12235 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
12236 && (sec->owner->flags & DYNAMIC) == 0)
12237 {
12238 size_t sym_count;
12239
12240 /* We are interested in just local symbols, not all
12241 symbols. */
12242 if (elf_bad_symtab (sec->owner))
12243 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
12244 / bed->s->sizeof_sym);
12245 else
12246 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
12247
12248 if (sym_count > max_sym_count)
12249 max_sym_count = sym_count;
12250
12251 if (sym_count > max_sym_shndx_count
12252 && elf_symtab_shndx_list (sec->owner) != NULL)
12253 max_sym_shndx_count = sym_count;
12254
12255 if (esdo->this_hdr.sh_type == SHT_REL
12256 || esdo->this_hdr.sh_type == SHT_RELA)
12257 /* Some backends use reloc_count in relocation sections
12258 to count particular types of relocs. Of course,
12259 reloc sections themselves can't have relocations. */
12260 ;
12261 else if (emit_relocs)
12262 {
12263 reloc_count = sec->reloc_count;
12264 if (bed->elf_backend_count_additional_relocs)
12265 {
12266 int c;
12267 c = (*bed->elf_backend_count_additional_relocs) (sec);
12268 additional_reloc_count += c;
12269 }
12270 }
12271 else if (bed->elf_backend_count_relocs)
12272 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
12273
12274 esdi = elf_section_data (sec);
12275
12276 if ((sec->flags & SEC_RELOC) != 0)
12277 {
12278 size_t ext_size = 0;
12279
12280 if (esdi->rel.hdr != NULL)
12281 ext_size = esdi->rel.hdr->sh_size;
12282 if (esdi->rela.hdr != NULL)
12283 ext_size += esdi->rela.hdr->sh_size;
12284
12285 if (ext_size > max_external_reloc_size)
12286 max_external_reloc_size = ext_size;
12287 if (sec->reloc_count > max_internal_reloc_count)
12288 max_internal_reloc_count = sec->reloc_count;
12289 }
12290 }
12291 }
12292
12293 if (reloc_count == 0)
12294 continue;
12295
12296 reloc_count += additional_reloc_count;
12297 o->reloc_count += reloc_count;
12298
12299 if (p->type == bfd_indirect_link_order && emit_relocs)
12300 {
12301 if (esdi->rel.hdr)
12302 {
12303 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
12304 esdo->rel.count += additional_reloc_count;
12305 }
12306 if (esdi->rela.hdr)
12307 {
12308 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
12309 esdo->rela.count += additional_reloc_count;
12310 }
12311 }
12312 else
12313 {
12314 if (o->use_rela_p)
12315 esdo->rela.count += reloc_count;
12316 else
12317 esdo->rel.count += reloc_count;
12318 }
12319 }
12320
12321 if (o->reloc_count > 0)
12322 o->flags |= SEC_RELOC;
12323 else
12324 {
12325 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12326 set it (this is probably a bug) and if it is set
12327 assign_section_numbers will create a reloc section. */
12328 o->flags &=~ SEC_RELOC;
12329 }
12330
12331 /* If the SEC_ALLOC flag is not set, force the section VMA to
12332 zero. This is done in elf_fake_sections as well, but forcing
12333 the VMA to 0 here will ensure that relocs against these
12334 sections are handled correctly. */
12335 if ((o->flags & SEC_ALLOC) == 0
12336 && ! o->user_set_vma)
12337 o->vma = 0;
12338 }
12339
12340 if (! bfd_link_relocatable (info) && merged)
12341 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
12342
12343 /* Figure out the file positions for everything but the symbol table
12344 and the relocs. We set symcount to force assign_section_numbers
12345 to create a symbol table. */
12346 abfd->symcount = info->strip != strip_all || emit_relocs;
12347 BFD_ASSERT (! abfd->output_has_begun);
12348 if (! _bfd_elf_compute_section_file_positions (abfd, info))
12349 goto error_return;
12350
12351 /* Set sizes, and assign file positions for reloc sections. */
12352 for (o = abfd->sections; o != NULL; o = o->next)
12353 {
12354 struct bfd_elf_section_data *esdo = elf_section_data (o);
12355 if ((o->flags & SEC_RELOC) != 0)
12356 {
12357 if (esdo->rel.hdr
12358 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
12359 goto error_return;
12360
12361 if (esdo->rela.hdr
12362 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
12363 goto error_return;
12364 }
12365
12366 /* _bfd_elf_compute_section_file_positions makes temporary use
12367 of target_index. Reset it. */
12368 o->target_index = 0;
12369
12370 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12371 to count upwards while actually outputting the relocations. */
12372 esdo->rel.count = 0;
12373 esdo->rela.count = 0;
12374
12375 if ((esdo->this_hdr.sh_offset == (file_ptr) -1)
12376 && !bfd_section_is_ctf (o))
12377 {
12378 /* Cache the section contents so that they can be compressed
12379 later. Use bfd_malloc since it will be freed by
12380 bfd_compress_section_contents. */
12381 unsigned char *contents = esdo->this_hdr.contents;
12382 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
12383 abort ();
12384 contents
12385 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
12386 if (contents == NULL)
12387 goto error_return;
12388 esdo->this_hdr.contents = contents;
12389 }
12390 }
12391
12392 /* We have now assigned file positions for all the sections except .symtab,
12393 .strtab, and non-loaded reloc and compressed debugging sections. We start
12394 the .symtab section at the current file position, and write directly to it.
12395 We build the .strtab section in memory. */
12396 abfd->symcount = 0;
12397 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12398 /* sh_name is set in prep_headers. */
12399 symtab_hdr->sh_type = SHT_SYMTAB;
12400 /* sh_flags, sh_addr and sh_size all start off zero. */
12401 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
12402 /* sh_link is set in assign_section_numbers. */
12403 /* sh_info is set below. */
12404 /* sh_offset is set just below. */
12405 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
12406
12407 if (max_sym_count < 20)
12408 max_sym_count = 20;
12409 htab->strtabsize = max_sym_count;
12410 amt = max_sym_count * sizeof (struct elf_sym_strtab);
12411 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
12412 if (htab->strtab == NULL)
12413 goto error_return;
12414 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12415 flinfo.symshndxbuf
12416 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
12417 ? (Elf_External_Sym_Shndx *) -1 : NULL);
12418
12419 if (info->strip != strip_all || emit_relocs)
12420 {
12421 file_ptr off = elf_next_file_pos (abfd);
12422
12423 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, true);
12424
12425 /* Note that at this point elf_next_file_pos (abfd) is
12426 incorrect. We do not yet know the size of the .symtab section.
12427 We correct next_file_pos below, after we do know the size. */
12428
12429 /* Start writing out the symbol table. The first symbol is always a
12430 dummy symbol. */
12431 elfsym.st_value = 0;
12432 elfsym.st_size = 0;
12433 elfsym.st_info = 0;
12434 elfsym.st_other = 0;
12435 elfsym.st_shndx = SHN_UNDEF;
12436 elfsym.st_target_internal = 0;
12437 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
12438 bfd_und_section_ptr, NULL) != 1)
12439 goto error_return;
12440
12441 /* Output a symbol for each section if asked or they are used for
12442 relocs. These symbols usually have no names. We store the
12443 index of each one in the index field of the section, so that
12444 we can find it again when outputting relocs. */
12445
12446 if (bfd_keep_unused_section_symbols (abfd) || emit_relocs)
12447 {
12448 bool name_local_sections
12449 = (bed->elf_backend_name_local_section_symbols
12450 && bed->elf_backend_name_local_section_symbols (abfd));
12451 const char *name = NULL;
12452
12453 elfsym.st_size = 0;
12454 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12455 elfsym.st_other = 0;
12456 elfsym.st_value = 0;
12457 elfsym.st_target_internal = 0;
12458 for (i = 1; i < elf_numsections (abfd); i++)
12459 {
12460 o = bfd_section_from_elf_index (abfd, i);
12461 if (o != NULL)
12462 {
12463 o->target_index = bfd_get_symcount (abfd);
12464 elfsym.st_shndx = i;
12465 if (!bfd_link_relocatable (info))
12466 elfsym.st_value = o->vma;
12467 if (name_local_sections)
12468 name = o->name;
12469 if (elf_link_output_symstrtab (&flinfo, name, &elfsym, o,
12470 NULL) != 1)
12471 goto error_return;
12472 }
12473 }
12474 }
12475 }
12476
12477 /* On some targets like Irix 5 the symbol split between local and global
12478 ones recorded in the sh_info field needs to be done between section
12479 and all other symbols. */
12480 if (bed->elf_backend_elfsym_local_is_section
12481 && bed->elf_backend_elfsym_local_is_section (abfd))
12482 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12483
12484 /* Allocate some memory to hold information read in from the input
12485 files. */
12486 if (max_contents_size != 0)
12487 {
12488 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12489 if (flinfo.contents == NULL)
12490 goto error_return;
12491 }
12492
12493 if (max_external_reloc_size != 0)
12494 {
12495 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
12496 if (flinfo.external_relocs == NULL)
12497 goto error_return;
12498 }
12499
12500 if (max_internal_reloc_count != 0)
12501 {
12502 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
12503 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
12504 if (flinfo.internal_relocs == NULL)
12505 goto error_return;
12506 }
12507
12508 if (max_sym_count != 0)
12509 {
12510 amt = max_sym_count * bed->s->sizeof_sym;
12511 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
12512 if (flinfo.external_syms == NULL)
12513 goto error_return;
12514
12515 amt = max_sym_count * sizeof (Elf_Internal_Sym);
12516 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
12517 if (flinfo.internal_syms == NULL)
12518 goto error_return;
12519
12520 amt = max_sym_count * sizeof (long);
12521 flinfo.indices = (long int *) bfd_malloc (amt);
12522 if (flinfo.indices == NULL)
12523 goto error_return;
12524
12525 amt = max_sym_count * sizeof (asection *);
12526 flinfo.sections = (asection **) bfd_malloc (amt);
12527 if (flinfo.sections == NULL)
12528 goto error_return;
12529 }
12530
12531 if (max_sym_shndx_count != 0)
12532 {
12533 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
12534 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
12535 if (flinfo.locsym_shndx == NULL)
12536 goto error_return;
12537 }
12538
12539 if (htab->tls_sec)
12540 {
12541 bfd_vma base, end = 0; /* Both bytes. */
12542 asection *sec;
12543
12544 for (sec = htab->tls_sec;
12545 sec && (sec->flags & SEC_THREAD_LOCAL);
12546 sec = sec->next)
12547 {
12548 bfd_size_type size = sec->size;
12549 unsigned int opb = bfd_octets_per_byte (abfd, sec);
12550
12551 if (size == 0
12552 && (sec->flags & SEC_HAS_CONTENTS) == 0)
12553 {
12554 struct bfd_link_order *ord = sec->map_tail.link_order;
12555
12556 if (ord != NULL)
12557 size = ord->offset * opb + ord->size;
12558 }
12559 end = sec->vma + size / opb;
12560 }
12561 base = htab->tls_sec->vma;
12562 /* Only align end of TLS section if static TLS doesn't have special
12563 alignment requirements. */
12564 if (bed->static_tls_alignment == 1)
12565 end = align_power (end, htab->tls_sec->alignment_power);
12566 htab->tls_size = end - base;
12567 }
12568
12569 if (!_bfd_elf_fixup_eh_frame_hdr (info))
12570 return false;
12571
12572 /* Since ELF permits relocations to be against local symbols, we
12573 must have the local symbols available when we do the relocations.
12574 Since we would rather only read the local symbols once, and we
12575 would rather not keep them in memory, we handle all the
12576 relocations for a single input file at the same time.
12577
12578 Unfortunately, there is no way to know the total number of local
12579 symbols until we have seen all of them, and the local symbol
12580 indices precede the global symbol indices. This means that when
12581 we are generating relocatable output, and we see a reloc against
12582 a global symbol, we can not know the symbol index until we have
12583 finished examining all the local symbols to see which ones we are
12584 going to output. To deal with this, we keep the relocations in
12585 memory, and don't output them until the end of the link. This is
12586 an unfortunate waste of memory, but I don't see a good way around
12587 it. Fortunately, it only happens when performing a relocatable
12588 link, which is not the common case. FIXME: If keep_memory is set
12589 we could write the relocs out and then read them again; I don't
12590 know how bad the memory loss will be. */
12591
12592 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12593 sub->output_has_begun = false;
12594 for (o = abfd->sections; o != NULL; o = o->next)
12595 {
12596 for (p = o->map_head.link_order; p != NULL; p = p->next)
12597 {
12598 if (p->type == bfd_indirect_link_order
12599 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12600 == bfd_target_elf_flavour)
12601 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12602 {
12603 if (! sub->output_has_begun)
12604 {
12605 if (! elf_link_input_bfd (&flinfo, sub))
12606 goto error_return;
12607 sub->output_has_begun = true;
12608 }
12609 }
12610 else if (p->type == bfd_section_reloc_link_order
12611 || p->type == bfd_symbol_reloc_link_order)
12612 {
12613 if (! elf_reloc_link_order (abfd, info, o, p))
12614 goto error_return;
12615 }
12616 else
12617 {
12618 if (! _bfd_default_link_order (abfd, info, o, p))
12619 {
12620 if (p->type == bfd_indirect_link_order
12621 && (bfd_get_flavour (sub)
12622 == bfd_target_elf_flavour)
12623 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12624 != bed->s->elfclass))
12625 {
12626 const char *iclass, *oclass;
12627
12628 switch (bed->s->elfclass)
12629 {
12630 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12631 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12632 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12633 default: abort ();
12634 }
12635
12636 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
12637 {
12638 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12639 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12640 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12641 default: abort ();
12642 }
12643
12644 bfd_set_error (bfd_error_wrong_format);
12645 _bfd_error_handler
12646 /* xgettext:c-format */
12647 (_("%pB: file class %s incompatible with %s"),
12648 sub, iclass, oclass);
12649 }
12650
12651 goto error_return;
12652 }
12653 }
12654 }
12655 }
12656
12657 /* Free symbol buffer if needed. */
12658 if (!info->reduce_memory_overheads)
12659 {
12660 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12661 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
12662 {
12663 free (elf_tdata (sub)->symbuf);
12664 elf_tdata (sub)->symbuf = NULL;
12665 }
12666 }
12667
12668 ret = true;
12669
12670 /* Output any global symbols that got converted to local in a
12671 version script or due to symbol visibility. We do this in a
12672 separate step since ELF requires all local symbols to appear
12673 prior to any global symbols. FIXME: We should only do this if
12674 some global symbols were, in fact, converted to become local.
12675 FIXME: Will this work correctly with the Irix 5 linker? */
12676 eoinfo.failed = false;
12677 eoinfo.flinfo = &flinfo;
12678 eoinfo.localsyms = true;
12679 eoinfo.file_sym_done = false;
12680 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12681 if (eoinfo.failed)
12682 {
12683 ret = false;
12684 goto return_local_hash_table;
12685 }
12686
12687 /* If backend needs to output some local symbols not present in the hash
12688 table, do it now. */
12689 if (bed->elf_backend_output_arch_local_syms
12690 && (info->strip != strip_all || emit_relocs))
12691 {
12692 if (! ((*bed->elf_backend_output_arch_local_syms)
12693 (abfd, info, &flinfo, elf_link_output_symstrtab)))
12694 {
12695 ret = false;
12696 goto return_local_hash_table;
12697 }
12698 }
12699
12700 /* That wrote out all the local symbols. Finish up the symbol table
12701 with the global symbols. Even if we want to strip everything we
12702 can, we still need to deal with those global symbols that got
12703 converted to local in a version script. */
12704
12705 /* The sh_info field records the index of the first non local symbol. */
12706 if (!symtab_hdr->sh_info)
12707 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12708
12709 if (dynamic
12710 && htab->dynsym != NULL
12711 && htab->dynsym->output_section != bfd_abs_section_ptr)
12712 {
12713 Elf_Internal_Sym sym;
12714 bfd_byte *dynsym = htab->dynsym->contents;
12715
12716 o = htab->dynsym->output_section;
12717 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
12718
12719 /* Write out the section symbols for the output sections. */
12720 if (bfd_link_pic (info)
12721 || htab->is_relocatable_executable)
12722 {
12723 asection *s;
12724
12725 sym.st_size = 0;
12726 sym.st_name = 0;
12727 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12728 sym.st_other = 0;
12729 sym.st_target_internal = 0;
12730
12731 for (s = abfd->sections; s != NULL; s = s->next)
12732 {
12733 int indx;
12734 bfd_byte *dest;
12735 long dynindx;
12736
12737 dynindx = elf_section_data (s)->dynindx;
12738 if (dynindx <= 0)
12739 continue;
12740 indx = elf_section_data (s)->this_idx;
12741 BFD_ASSERT (indx > 0);
12742 sym.st_shndx = indx;
12743 if (! check_dynsym (abfd, &sym))
12744 {
12745 ret = false;
12746 goto return_local_hash_table;
12747 }
12748 sym.st_value = s->vma;
12749 dest = dynsym + dynindx * bed->s->sizeof_sym;
12750
12751 /* Inform the linker of the addition of this symbol. */
12752
12753 if (info->callbacks->ctf_new_dynsym)
12754 info->callbacks->ctf_new_dynsym (dynindx, &sym);
12755
12756 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12757 }
12758 }
12759
12760 /* Write out the local dynsyms. */
12761 if (htab->dynlocal)
12762 {
12763 struct elf_link_local_dynamic_entry *e;
12764 for (e = htab->dynlocal; e ; e = e->next)
12765 {
12766 asection *s;
12767 bfd_byte *dest;
12768
12769 /* Copy the internal symbol and turn off visibility.
12770 Note that we saved a word of storage and overwrote
12771 the original st_name with the dynstr_index. */
12772 sym = e->isym;
12773 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
12774 sym.st_shndx = SHN_UNDEF;
12775
12776 s = bfd_section_from_elf_index (e->input_bfd,
12777 e->isym.st_shndx);
12778 if (s != NULL
12779 && s->output_section != NULL
12780 && elf_section_data (s->output_section) != NULL)
12781 {
12782 sym.st_shndx =
12783 elf_section_data (s->output_section)->this_idx;
12784 if (! check_dynsym (abfd, &sym))
12785 {
12786 ret = false;
12787 goto return_local_hash_table;
12788 }
12789 sym.st_value = (s->output_section->vma
12790 + s->output_offset
12791 + e->isym.st_value);
12792 }
12793
12794 /* Inform the linker of the addition of this symbol. */
12795
12796 if (info->callbacks->ctf_new_dynsym)
12797 info->callbacks->ctf_new_dynsym (e->dynindx, &sym);
12798
12799 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12800 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12801 }
12802 }
12803 }
12804
12805 /* We get the global symbols from the hash table. */
12806 eoinfo.failed = false;
12807 eoinfo.localsyms = false;
12808 eoinfo.flinfo = &flinfo;
12809 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12810 if (eoinfo.failed)
12811 {
12812 ret = false;
12813 goto return_local_hash_table;
12814 }
12815
12816 /* If backend needs to output some symbols not present in the hash
12817 table, do it now. */
12818 if (bed->elf_backend_output_arch_syms
12819 && (info->strip != strip_all || emit_relocs))
12820 {
12821 if (! ((*bed->elf_backend_output_arch_syms)
12822 (abfd, info, &flinfo, elf_link_output_symstrtab)))
12823 {
12824 ret = false;
12825 goto return_local_hash_table;
12826 }
12827 }
12828
12829 /* Finalize the .strtab section. */
12830 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12831
12832 /* Swap out the .strtab section. */
12833 if (!elf_link_swap_symbols_out (&flinfo))
12834 {
12835 ret = false;
12836 goto return_local_hash_table;
12837 }
12838
12839 /* Now we know the size of the symtab section. */
12840 if (bfd_get_symcount (abfd) > 0)
12841 {
12842 /* Finish up and write out the symbol string table (.strtab)
12843 section. */
12844 Elf_Internal_Shdr *symstrtab_hdr = NULL;
12845 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12846
12847 if (elf_symtab_shndx_list (abfd))
12848 {
12849 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
12850
12851 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12852 {
12853 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12854 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12855 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12856 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12857 symtab_shndx_hdr->sh_size = amt;
12858
12859 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12860 off, true);
12861
12862 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12863 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12864 {
12865 ret = false;
12866 goto return_local_hash_table;
12867 }
12868 }
12869 }
12870
12871 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12872 /* sh_name was set in prep_headers. */
12873 symstrtab_hdr->sh_type = SHT_STRTAB;
12874 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
12875 symstrtab_hdr->sh_addr = 0;
12876 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
12877 symstrtab_hdr->sh_entsize = 0;
12878 symstrtab_hdr->sh_link = 0;
12879 symstrtab_hdr->sh_info = 0;
12880 /* sh_offset is set just below. */
12881 symstrtab_hdr->sh_addralign = 1;
12882
12883 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12884 off, true);
12885 elf_next_file_pos (abfd) = off;
12886
12887 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
12888 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
12889 {
12890 ret = false;
12891 goto return_local_hash_table;
12892 }
12893 }
12894
12895 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12896 {
12897 _bfd_error_handler (_("%pB: failed to generate import library"),
12898 info->out_implib_bfd);
12899 ret = false;
12900 goto return_local_hash_table;
12901 }
12902
12903 /* Adjust the relocs to have the correct symbol indices. */
12904 for (o = abfd->sections; o != NULL; o = o->next)
12905 {
12906 struct bfd_elf_section_data *esdo = elf_section_data (o);
12907 bool sort;
12908
12909 if ((o->flags & SEC_RELOC) == 0)
12910 continue;
12911
12912 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
12913 if (esdo->rel.hdr != NULL
12914 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
12915 {
12916 ret = false;
12917 goto return_local_hash_table;
12918 }
12919 if (esdo->rela.hdr != NULL
12920 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
12921 {
12922 ret = false;
12923 goto return_local_hash_table;
12924 }
12925
12926 /* Set the reloc_count field to 0 to prevent write_relocs from
12927 trying to swap the relocs out itself. */
12928 o->reloc_count = 0;
12929 }
12930
12931 if (dynamic && info->combreloc && dynobj != NULL)
12932 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12933
12934 /* If we are linking against a dynamic object, or generating a
12935 shared library, finish up the dynamic linking information. */
12936 if (dynamic)
12937 {
12938 bfd_byte *dyncon, *dynconend;
12939
12940 /* Fix up .dynamic entries. */
12941 o = bfd_get_linker_section (dynobj, ".dynamic");
12942 BFD_ASSERT (o != NULL);
12943
12944 dyncon = o->contents;
12945 dynconend = o->contents + o->size;
12946 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12947 {
12948 Elf_Internal_Dyn dyn;
12949 const char *name;
12950 unsigned int type;
12951 bfd_size_type sh_size;
12952 bfd_vma sh_addr;
12953
12954 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12955
12956 switch (dyn.d_tag)
12957 {
12958 default:
12959 continue;
12960 case DT_NULL:
12961 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12962 {
12963 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12964 {
12965 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12966 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12967 default: continue;
12968 }
12969 dyn.d_un.d_val = relativecount;
12970 relativecount = 0;
12971 break;
12972 }
12973 continue;
12974
12975 case DT_INIT:
12976 name = info->init_function;
12977 goto get_sym;
12978 case DT_FINI:
12979 name = info->fini_function;
12980 get_sym:
12981 {
12982 struct elf_link_hash_entry *h;
12983
12984 h = elf_link_hash_lookup (htab, name, false, false, true);
12985 if (h != NULL
12986 && (h->root.type == bfd_link_hash_defined
12987 || h->root.type == bfd_link_hash_defweak))
12988 {
12989 dyn.d_un.d_ptr = h->root.u.def.value;
12990 o = h->root.u.def.section;
12991 if (o->output_section != NULL)
12992 dyn.d_un.d_ptr += (o->output_section->vma
12993 + o->output_offset);
12994 else
12995 {
12996 /* The symbol is imported from another shared
12997 library and does not apply to this one. */
12998 dyn.d_un.d_ptr = 0;
12999 }
13000 break;
13001 }
13002 }
13003 continue;
13004
13005 case DT_PREINIT_ARRAYSZ:
13006 name = ".preinit_array";
13007 goto get_out_size;
13008 case DT_INIT_ARRAYSZ:
13009 name = ".init_array";
13010 goto get_out_size;
13011 case DT_FINI_ARRAYSZ:
13012 name = ".fini_array";
13013 get_out_size:
13014 o = bfd_get_section_by_name (abfd, name);
13015 if (o == NULL)
13016 {
13017 _bfd_error_handler
13018 (_("could not find section %s"), name);
13019 goto error_return;
13020 }
13021 if (o->size == 0)
13022 _bfd_error_handler
13023 (_("warning: %s section has zero size"), name);
13024 dyn.d_un.d_val = o->size;
13025 break;
13026
13027 case DT_PREINIT_ARRAY:
13028 name = ".preinit_array";
13029 goto get_out_vma;
13030 case DT_INIT_ARRAY:
13031 name = ".init_array";
13032 goto get_out_vma;
13033 case DT_FINI_ARRAY:
13034 name = ".fini_array";
13035 get_out_vma:
13036 o = bfd_get_section_by_name (abfd, name);
13037 goto do_vma;
13038
13039 case DT_HASH:
13040 name = ".hash";
13041 goto get_vma;
13042 case DT_GNU_HASH:
13043 name = ".gnu.hash";
13044 goto get_vma;
13045 case DT_STRTAB:
13046 name = ".dynstr";
13047 goto get_vma;
13048 case DT_SYMTAB:
13049 name = ".dynsym";
13050 goto get_vma;
13051 case DT_VERDEF:
13052 name = ".gnu.version_d";
13053 goto get_vma;
13054 case DT_VERNEED:
13055 name = ".gnu.version_r";
13056 goto get_vma;
13057 case DT_VERSYM:
13058 name = ".gnu.version";
13059 get_vma:
13060 o = bfd_get_linker_section (dynobj, name);
13061 do_vma:
13062 if (o == NULL || bfd_is_abs_section (o->output_section))
13063 {
13064 _bfd_error_handler
13065 (_("could not find section %s"), name);
13066 goto error_return;
13067 }
13068 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
13069 {
13070 _bfd_error_handler
13071 (_("warning: section '%s' is being made into a note"), name);
13072 bfd_set_error (bfd_error_nonrepresentable_section);
13073 goto error_return;
13074 }
13075 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
13076 break;
13077
13078 case DT_REL:
13079 case DT_RELA:
13080 case DT_RELSZ:
13081 case DT_RELASZ:
13082 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
13083 type = SHT_REL;
13084 else
13085 type = SHT_RELA;
13086 sh_size = 0;
13087 sh_addr = 0;
13088 for (i = 1; i < elf_numsections (abfd); i++)
13089 {
13090 Elf_Internal_Shdr *hdr;
13091
13092 hdr = elf_elfsections (abfd)[i];
13093 if (hdr->sh_type == type
13094 && (hdr->sh_flags & SHF_ALLOC) != 0)
13095 {
13096 sh_size += hdr->sh_size;
13097 if (sh_addr == 0
13098 || sh_addr > hdr->sh_addr)
13099 sh_addr = hdr->sh_addr;
13100 }
13101 }
13102
13103 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
13104 {
13105 unsigned int opb = bfd_octets_per_byte (abfd, o);
13106
13107 /* Don't count procedure linkage table relocs in the
13108 overall reloc count. */
13109 sh_size -= htab->srelplt->size;
13110 if (sh_size == 0)
13111 /* If the size is zero, make the address zero too.
13112 This is to avoid a glibc bug. If the backend
13113 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
13114 zero, then we'll put DT_RELA at the end of
13115 DT_JMPREL. glibc will interpret the end of
13116 DT_RELA matching the end of DT_JMPREL as the
13117 case where DT_RELA includes DT_JMPREL, and for
13118 LD_BIND_NOW will decide that processing DT_RELA
13119 will process the PLT relocs too. Net result:
13120 No PLT relocs applied. */
13121 sh_addr = 0;
13122
13123 /* If .rela.plt is the first .rela section, exclude
13124 it from DT_RELA. */
13125 else if (sh_addr == (htab->srelplt->output_section->vma
13126 + htab->srelplt->output_offset) * opb)
13127 sh_addr += htab->srelplt->size;
13128 }
13129
13130 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
13131 dyn.d_un.d_val = sh_size;
13132 else
13133 dyn.d_un.d_ptr = sh_addr;
13134 break;
13135 }
13136 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13137 }
13138 }
13139
13140 /* If we have created any dynamic sections, then output them. */
13141 if (dynobj != NULL)
13142 {
13143 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
13144 goto error_return;
13145
13146 /* Check for DT_TEXTREL (late, in case the backend removes it). */
13147 if (bfd_link_textrel_check (info)
13148 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
13149 {
13150 bfd_byte *dyncon, *dynconend;
13151
13152 dyncon = o->contents;
13153 dynconend = o->contents + o->size;
13154 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13155 {
13156 Elf_Internal_Dyn dyn;
13157
13158 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13159
13160 if (dyn.d_tag == DT_TEXTREL)
13161 {
13162 if (info->textrel_check == textrel_check_error)
13163 info->callbacks->einfo
13164 (_("%P%X: read-only segment has dynamic relocations\n"));
13165 else if (bfd_link_dll (info))
13166 info->callbacks->einfo
13167 (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
13168 else if (bfd_link_pde (info))
13169 info->callbacks->einfo
13170 (_("%P: warning: creating DT_TEXTREL in a PDE\n"));
13171 else
13172 info->callbacks->einfo
13173 (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
13174 break;
13175 }
13176 }
13177 }
13178
13179 for (o = dynobj->sections; o != NULL; o = o->next)
13180 {
13181 if ((o->flags & SEC_HAS_CONTENTS) == 0
13182 || o->size == 0
13183 || o->output_section == bfd_abs_section_ptr)
13184 continue;
13185 if ((o->flags & SEC_LINKER_CREATED) == 0)
13186 {
13187 /* At this point, we are only interested in sections
13188 created by _bfd_elf_link_create_dynamic_sections. */
13189 continue;
13190 }
13191 if (htab->stab_info.stabstr == o)
13192 continue;
13193 if (htab->eh_info.hdr_sec == o)
13194 continue;
13195 if (strcmp (o->name, ".dynstr") != 0)
13196 {
13197 bfd_size_type octets = ((file_ptr) o->output_offset
13198 * bfd_octets_per_byte (abfd, o));
13199 if (!bfd_set_section_contents (abfd, o->output_section,
13200 o->contents, octets, o->size))
13201 goto error_return;
13202 }
13203 else
13204 {
13205 /* The contents of the .dynstr section are actually in a
13206 stringtab. */
13207 file_ptr off;
13208
13209 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
13210 if (bfd_seek (abfd, off, SEEK_SET) != 0
13211 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
13212 goto error_return;
13213 }
13214 }
13215 }
13216
13217 if (!info->resolve_section_groups)
13218 {
13219 bool failed = false;
13220
13221 BFD_ASSERT (bfd_link_relocatable (info));
13222 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
13223 if (failed)
13224 goto error_return;
13225 }
13226
13227 /* If we have optimized stabs strings, output them. */
13228 if (htab->stab_info.stabstr != NULL)
13229 {
13230 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
13231 goto error_return;
13232 }
13233
13234 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
13235 goto error_return;
13236
13237 if (info->callbacks->emit_ctf)
13238 info->callbacks->emit_ctf ();
13239
13240 elf_final_link_free (abfd, &flinfo);
13241
13242 if (attr_section)
13243 {
13244 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
13245 if (contents == NULL)
13246 {
13247 /* Bail out and fail. */
13248 ret = false;
13249 goto return_local_hash_table;
13250 }
13251 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
13252 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
13253 free (contents);
13254 }
13255
13256 return_local_hash_table:
13257 if (info->unique_symbol)
13258 bfd_hash_table_free (&flinfo.local_hash_table);
13259 return ret;
13260
13261 error_return:
13262 elf_final_link_free (abfd, &flinfo);
13263 ret = false;
13264 goto return_local_hash_table;
13265 }
13266 \f
13267 /* Initialize COOKIE for input bfd ABFD. */
13268
13269 static bool
13270 init_reloc_cookie (struct elf_reloc_cookie *cookie,
13271 struct bfd_link_info *info, bfd *abfd)
13272 {
13273 Elf_Internal_Shdr *symtab_hdr;
13274 const struct elf_backend_data *bed;
13275
13276 bed = get_elf_backend_data (abfd);
13277 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13278
13279 cookie->abfd = abfd;
13280 cookie->sym_hashes = elf_sym_hashes (abfd);
13281 cookie->bad_symtab = elf_bad_symtab (abfd);
13282 if (cookie->bad_symtab)
13283 {
13284 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13285 cookie->extsymoff = 0;
13286 }
13287 else
13288 {
13289 cookie->locsymcount = symtab_hdr->sh_info;
13290 cookie->extsymoff = symtab_hdr->sh_info;
13291 }
13292
13293 if (bed->s->arch_size == 32)
13294 cookie->r_sym_shift = 8;
13295 else
13296 cookie->r_sym_shift = 32;
13297
13298 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
13299 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
13300 {
13301 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
13302 cookie->locsymcount, 0,
13303 NULL, NULL, NULL);
13304 if (cookie->locsyms == NULL)
13305 {
13306 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
13307 return false;
13308 }
13309 if (_bfd_link_keep_memory (info) )
13310 {
13311 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
13312 info->cache_size += (cookie->locsymcount
13313 * sizeof (Elf_External_Sym_Shndx));
13314 }
13315 }
13316 return true;
13317 }
13318
13319 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
13320
13321 static void
13322 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
13323 {
13324 Elf_Internal_Shdr *symtab_hdr;
13325
13326 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13327 if (symtab_hdr->contents != (unsigned char *) cookie->locsyms)
13328 free (cookie->locsyms);
13329 }
13330
13331 /* Initialize the relocation information in COOKIE for input section SEC
13332 of input bfd ABFD. */
13333
13334 static bool
13335 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13336 struct bfd_link_info *info, bfd *abfd,
13337 asection *sec)
13338 {
13339 if (sec->reloc_count == 0)
13340 {
13341 cookie->rels = NULL;
13342 cookie->relend = NULL;
13343 }
13344 else
13345 {
13346 cookie->rels = _bfd_elf_link_info_read_relocs (abfd, info, sec,
13347 NULL, NULL,
13348 _bfd_link_keep_memory (info));
13349 if (cookie->rels == NULL)
13350 return false;
13351 cookie->rel = cookie->rels;
13352 cookie->relend = cookie->rels + sec->reloc_count;
13353 }
13354 cookie->rel = cookie->rels;
13355 return true;
13356 }
13357
13358 /* Free the memory allocated by init_reloc_cookie_rels,
13359 if appropriate. */
13360
13361 static void
13362 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13363 asection *sec)
13364 {
13365 if (elf_section_data (sec)->relocs != cookie->rels)
13366 free (cookie->rels);
13367 }
13368
13369 /* Initialize the whole of COOKIE for input section SEC. */
13370
13371 static bool
13372 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13373 struct bfd_link_info *info,
13374 asection *sec)
13375 {
13376 if (!init_reloc_cookie (cookie, info, sec->owner))
13377 goto error1;
13378 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
13379 goto error2;
13380 return true;
13381
13382 error2:
13383 fini_reloc_cookie (cookie, sec->owner);
13384 error1:
13385 return false;
13386 }
13387
13388 /* Free the memory allocated by init_reloc_cookie_for_section,
13389 if appropriate. */
13390
13391 static void
13392 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13393 asection *sec)
13394 {
13395 fini_reloc_cookie_rels (cookie, sec);
13396 fini_reloc_cookie (cookie, sec->owner);
13397 }
13398 \f
13399 /* Garbage collect unused sections. */
13400
13401 /* Default gc_mark_hook. */
13402
13403 asection *
13404 _bfd_elf_gc_mark_hook (asection *sec,
13405 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13406 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13407 struct elf_link_hash_entry *h,
13408 Elf_Internal_Sym *sym)
13409 {
13410 if (h != NULL)
13411 {
13412 switch (h->root.type)
13413 {
13414 case bfd_link_hash_defined:
13415 case bfd_link_hash_defweak:
13416 return h->root.u.def.section;
13417
13418 case bfd_link_hash_common:
13419 return h->root.u.c.p->section;
13420
13421 default:
13422 break;
13423 }
13424 }
13425 else
13426 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
13427
13428 return NULL;
13429 }
13430
13431 /* Return the debug definition section. */
13432
13433 static asection *
13434 elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
13435 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13436 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13437 struct elf_link_hash_entry *h,
13438 Elf_Internal_Sym *sym)
13439 {
13440 if (h != NULL)
13441 {
13442 /* Return the global debug definition section. */
13443 if ((h->root.type == bfd_link_hash_defined
13444 || h->root.type == bfd_link_hash_defweak)
13445 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
13446 return h->root.u.def.section;
13447 }
13448 else
13449 {
13450 /* Return the local debug definition section. */
13451 asection *isec = bfd_section_from_elf_index (sec->owner,
13452 sym->st_shndx);
13453 if ((isec->flags & SEC_DEBUGGING) != 0)
13454 return isec;
13455 }
13456
13457 return NULL;
13458 }
13459
13460 /* COOKIE->rel describes a relocation against section SEC, which is
13461 a section we've decided to keep. Return the section that contains
13462 the relocation symbol, or NULL if no section contains it. */
13463
13464 asection *
13465 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
13466 elf_gc_mark_hook_fn gc_mark_hook,
13467 struct elf_reloc_cookie *cookie,
13468 bool *start_stop)
13469 {
13470 unsigned long r_symndx;
13471 struct elf_link_hash_entry *h, *hw;
13472
13473 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
13474 if (r_symndx == STN_UNDEF)
13475 return NULL;
13476
13477 if (r_symndx >= cookie->locsymcount
13478 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13479 {
13480 bool was_marked;
13481
13482 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
13483 if (h == NULL)
13484 {
13485 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
13486 sec->owner);
13487 return NULL;
13488 }
13489 while (h->root.type == bfd_link_hash_indirect
13490 || h->root.type == bfd_link_hash_warning)
13491 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13492
13493 was_marked = h->mark;
13494 h->mark = 1;
13495 /* Keep all aliases of the symbol too. If an object symbol
13496 needs to be copied into .dynbss then all of its aliases
13497 should be present as dynamic symbols, not just the one used
13498 on the copy relocation. */
13499 hw = h;
13500 while (hw->is_weakalias)
13501 {
13502 hw = hw->u.alias;
13503 hw->mark = 1;
13504 }
13505
13506 if (!was_marked && h->start_stop && !h->root.ldscript_def)
13507 {
13508 if (info->start_stop_gc)
13509 return NULL;
13510
13511 /* To work around a glibc bug, mark XXX input sections
13512 when there is a reference to __start_XXX or __stop_XXX
13513 symbols. */
13514 else if (start_stop != NULL)
13515 {
13516 asection *s = h->u2.start_stop_section;
13517 *start_stop = true;
13518 return s;
13519 }
13520 }
13521
13522 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
13523 }
13524
13525 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
13526 &cookie->locsyms[r_symndx]);
13527 }
13528
13529 /* COOKIE->rel describes a relocation against section SEC, which is
13530 a section we've decided to keep. Mark the section that contains
13531 the relocation symbol. */
13532
13533 bool
13534 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
13535 asection *sec,
13536 elf_gc_mark_hook_fn gc_mark_hook,
13537 struct elf_reloc_cookie *cookie)
13538 {
13539 asection *rsec;
13540 bool start_stop = false;
13541
13542 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
13543 while (rsec != NULL)
13544 {
13545 if (!rsec->gc_mark)
13546 {
13547 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
13548 || (rsec->owner->flags & DYNAMIC) != 0)
13549 rsec->gc_mark = 1;
13550 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
13551 return false;
13552 }
13553 if (!start_stop)
13554 break;
13555 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
13556 }
13557 return true;
13558 }
13559
13560 /* The mark phase of garbage collection. For a given section, mark
13561 it and any sections in this section's group, and all the sections
13562 which define symbols to which it refers. */
13563
13564 bool
13565 _bfd_elf_gc_mark (struct bfd_link_info *info,
13566 asection *sec,
13567 elf_gc_mark_hook_fn gc_mark_hook)
13568 {
13569 bool ret;
13570 asection *group_sec, *eh_frame;
13571
13572 sec->gc_mark = 1;
13573
13574 /* Mark all the sections in the group. */
13575 group_sec = elf_section_data (sec)->next_in_group;
13576 if (group_sec && !group_sec->gc_mark)
13577 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
13578 return false;
13579
13580 /* Look through the section relocs. */
13581 ret = true;
13582 eh_frame = elf_eh_frame_section (sec->owner);
13583 if ((sec->flags & SEC_RELOC) != 0
13584 && sec->reloc_count > 0
13585 && sec != eh_frame)
13586 {
13587 struct elf_reloc_cookie cookie;
13588
13589 if (!init_reloc_cookie_for_section (&cookie, info, sec))
13590 ret = false;
13591 else
13592 {
13593 for (; cookie.rel < cookie.relend; cookie.rel++)
13594 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
13595 {
13596 ret = false;
13597 break;
13598 }
13599 fini_reloc_cookie_for_section (&cookie, sec);
13600 }
13601 }
13602
13603 if (ret && eh_frame && elf_fde_list (sec))
13604 {
13605 struct elf_reloc_cookie cookie;
13606
13607 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
13608 ret = false;
13609 else
13610 {
13611 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
13612 gc_mark_hook, &cookie))
13613 ret = false;
13614 fini_reloc_cookie_for_section (&cookie, eh_frame);
13615 }
13616 }
13617
13618 eh_frame = elf_section_eh_frame_entry (sec);
13619 if (ret && eh_frame && !eh_frame->gc_mark)
13620 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
13621 ret = false;
13622
13623 return ret;
13624 }
13625
13626 /* Scan and mark sections in a special or debug section group. */
13627
13628 static void
13629 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
13630 {
13631 /* Point to first section of section group. */
13632 asection *ssec;
13633 /* Used to iterate the section group. */
13634 asection *msec;
13635
13636 bool is_special_grp = true;
13637 bool is_debug_grp = true;
13638
13639 /* First scan to see if group contains any section other than debug
13640 and special section. */
13641 ssec = msec = elf_next_in_group (grp);
13642 do
13643 {
13644 if ((msec->flags & SEC_DEBUGGING) == 0)
13645 is_debug_grp = false;
13646
13647 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
13648 is_special_grp = false;
13649
13650 msec = elf_next_in_group (msec);
13651 }
13652 while (msec != ssec);
13653
13654 /* If this is a pure debug section group or pure special section group,
13655 keep all sections in this group. */
13656 if (is_debug_grp || is_special_grp)
13657 {
13658 do
13659 {
13660 msec->gc_mark = 1;
13661 msec = elf_next_in_group (msec);
13662 }
13663 while (msec != ssec);
13664 }
13665 }
13666
13667 /* Keep debug and special sections. */
13668
13669 bool
13670 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
13671 elf_gc_mark_hook_fn mark_hook)
13672 {
13673 bfd *ibfd;
13674
13675 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13676 {
13677 asection *isec;
13678 bool some_kept;
13679 bool debug_frag_seen;
13680 bool has_kept_debug_info;
13681
13682 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13683 continue;
13684 isec = ibfd->sections;
13685 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13686 continue;
13687
13688 /* Ensure all linker created sections are kept,
13689 see if any other section is already marked,
13690 and note if we have any fragmented debug sections. */
13691 debug_frag_seen = some_kept = has_kept_debug_info = false;
13692 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13693 {
13694 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13695 isec->gc_mark = 1;
13696 else if (isec->gc_mark
13697 && (isec->flags & SEC_ALLOC) != 0
13698 && elf_section_type (isec) != SHT_NOTE)
13699 some_kept = true;
13700 else
13701 {
13702 /* Since all sections, except for backend specific ones,
13703 have been garbage collected, call mark_hook on this
13704 section if any of its linked-to sections is marked. */
13705 asection *linked_to_sec;
13706 for (linked_to_sec = elf_linked_to_section (isec);
13707 linked_to_sec != NULL && !linked_to_sec->linker_mark;
13708 linked_to_sec = elf_linked_to_section (linked_to_sec))
13709 {
13710 if (linked_to_sec->gc_mark)
13711 {
13712 if (!_bfd_elf_gc_mark (info, isec, mark_hook))
13713 return false;
13714 break;
13715 }
13716 linked_to_sec->linker_mark = 1;
13717 }
13718 for (linked_to_sec = elf_linked_to_section (isec);
13719 linked_to_sec != NULL && linked_to_sec->linker_mark;
13720 linked_to_sec = elf_linked_to_section (linked_to_sec))
13721 linked_to_sec->linker_mark = 0;
13722 }
13723
13724 if (!debug_frag_seen
13725 && (isec->flags & SEC_DEBUGGING)
13726 && startswith (isec->name, ".debug_line."))
13727 debug_frag_seen = true;
13728 else if (strcmp (bfd_section_name (isec),
13729 "__patchable_function_entries") == 0
13730 && elf_linked_to_section (isec) == NULL)
13731 info->callbacks->einfo (_("%F%P: %pB(%pA): error: "
13732 "need linked-to section "
13733 "for --gc-sections\n"),
13734 isec->owner, isec);
13735 }
13736
13737 /* If no non-note alloc section in this file will be kept, then
13738 we can toss out the debug and special sections. */
13739 if (!some_kept)
13740 continue;
13741
13742 /* Keep debug and special sections like .comment when they are
13743 not part of a group. Also keep section groups that contain
13744 just debug sections or special sections. NB: Sections with
13745 linked-to section has been handled above. */
13746 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13747 {
13748 if ((isec->flags & SEC_GROUP) != 0)
13749 _bfd_elf_gc_mark_debug_special_section_group (isec);
13750 else if (((isec->flags & SEC_DEBUGGING) != 0
13751 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
13752 && elf_next_in_group (isec) == NULL
13753 && elf_linked_to_section (isec) == NULL)
13754 isec->gc_mark = 1;
13755 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
13756 has_kept_debug_info = true;
13757 }
13758
13759 /* Look for CODE sections which are going to be discarded,
13760 and find and discard any fragmented debug sections which
13761 are associated with that code section. */
13762 if (debug_frag_seen)
13763 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13764 if ((isec->flags & SEC_CODE) != 0
13765 && isec->gc_mark == 0)
13766 {
13767 unsigned int ilen;
13768 asection *dsec;
13769
13770 ilen = strlen (isec->name);
13771
13772 /* Association is determined by the name of the debug
13773 section containing the name of the code section as
13774 a suffix. For example .debug_line.text.foo is a
13775 debug section associated with .text.foo. */
13776 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
13777 {
13778 unsigned int dlen;
13779
13780 if (dsec->gc_mark == 0
13781 || (dsec->flags & SEC_DEBUGGING) == 0)
13782 continue;
13783
13784 dlen = strlen (dsec->name);
13785
13786 if (dlen > ilen
13787 && strncmp (dsec->name + (dlen - ilen),
13788 isec->name, ilen) == 0)
13789 dsec->gc_mark = 0;
13790 }
13791 }
13792
13793 /* Mark debug sections referenced by kept debug sections. */
13794 if (has_kept_debug_info)
13795 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13796 if (isec->gc_mark
13797 && (isec->flags & SEC_DEBUGGING) != 0)
13798 if (!_bfd_elf_gc_mark (info, isec,
13799 elf_gc_mark_debug_section))
13800 return false;
13801 }
13802 return true;
13803 }
13804
13805 static bool
13806 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
13807 {
13808 bfd *sub;
13809 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13810
13811 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13812 {
13813 asection *o;
13814
13815 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13816 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
13817 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13818 continue;
13819 o = sub->sections;
13820 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13821 continue;
13822
13823 for (o = sub->sections; o != NULL; o = o->next)
13824 {
13825 /* When any section in a section group is kept, we keep all
13826 sections in the section group. If the first member of
13827 the section group is excluded, we will also exclude the
13828 group section. */
13829 if (o->flags & SEC_GROUP)
13830 {
13831 asection *first = elf_next_in_group (o);
13832 o->gc_mark = first->gc_mark;
13833 }
13834
13835 if (o->gc_mark)
13836 continue;
13837
13838 /* Skip sweeping sections already excluded. */
13839 if (o->flags & SEC_EXCLUDE)
13840 continue;
13841
13842 /* Since this is early in the link process, it is simple
13843 to remove a section from the output. */
13844 o->flags |= SEC_EXCLUDE;
13845
13846 if (info->print_gc_sections && o->size != 0)
13847 /* xgettext:c-format */
13848 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
13849 o, sub);
13850 }
13851 }
13852
13853 return true;
13854 }
13855
13856 /* Propagate collected vtable information. This is called through
13857 elf_link_hash_traverse. */
13858
13859 static bool
13860 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13861 {
13862 /* Those that are not vtables. */
13863 if (h->start_stop
13864 || h->u2.vtable == NULL
13865 || h->u2.vtable->parent == NULL)
13866 return true;
13867
13868 /* Those vtables that do not have parents, we cannot merge. */
13869 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
13870 return true;
13871
13872 /* If we've already been done, exit. */
13873 if (h->u2.vtable->used && h->u2.vtable->used[-1])
13874 return true;
13875
13876 /* Make sure the parent's table is up to date. */
13877 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
13878
13879 if (h->u2.vtable->used == NULL)
13880 {
13881 /* None of this table's entries were referenced. Re-use the
13882 parent's table. */
13883 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13884 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
13885 }
13886 else
13887 {
13888 size_t n;
13889 bool *cu, *pu;
13890
13891 /* Or the parent's entries into ours. */
13892 cu = h->u2.vtable->used;
13893 cu[-1] = true;
13894 pu = h->u2.vtable->parent->u2.vtable->used;
13895 if (pu != NULL)
13896 {
13897 const struct elf_backend_data *bed;
13898 unsigned int log_file_align;
13899
13900 bed = get_elf_backend_data (h->root.u.def.section->owner);
13901 log_file_align = bed->s->log_file_align;
13902 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
13903 while (n--)
13904 {
13905 if (*pu)
13906 *cu = true;
13907 pu++;
13908 cu++;
13909 }
13910 }
13911 }
13912
13913 return true;
13914 }
13915
13916 struct link_info_ok
13917 {
13918 struct bfd_link_info *info;
13919 bool ok;
13920 };
13921
13922 static bool
13923 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h,
13924 void *ptr)
13925 {
13926 asection *sec;
13927 bfd_vma hstart, hend;
13928 Elf_Internal_Rela *relstart, *relend, *rel;
13929 const struct elf_backend_data *bed;
13930 unsigned int log_file_align;
13931 struct link_info_ok *info = (struct link_info_ok *) ptr;
13932
13933 /* Take care of both those symbols that do not describe vtables as
13934 well as those that are not loaded. */
13935 if (h->start_stop
13936 || h->u2.vtable == NULL
13937 || h->u2.vtable->parent == NULL)
13938 return true;
13939
13940 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13941 || h->root.type == bfd_link_hash_defweak);
13942
13943 sec = h->root.u.def.section;
13944 hstart = h->root.u.def.value;
13945 hend = hstart + h->size;
13946
13947 relstart = _bfd_elf_link_info_read_relocs (sec->owner, info->info,
13948 sec, NULL, NULL, true);
13949 if (!relstart)
13950 return info->ok = false;
13951 bed = get_elf_backend_data (sec->owner);
13952 log_file_align = bed->s->log_file_align;
13953
13954 relend = relstart + sec->reloc_count;
13955
13956 for (rel = relstart; rel < relend; ++rel)
13957 if (rel->r_offset >= hstart && rel->r_offset < hend)
13958 {
13959 /* If the entry is in use, do nothing. */
13960 if (h->u2.vtable->used
13961 && (rel->r_offset - hstart) < h->u2.vtable->size)
13962 {
13963 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
13964 if (h->u2.vtable->used[entry])
13965 continue;
13966 }
13967 /* Otherwise, kill it. */
13968 rel->r_offset = rel->r_info = rel->r_addend = 0;
13969 }
13970
13971 return true;
13972 }
13973
13974 /* Mark sections containing dynamically referenced symbols. When
13975 building shared libraries, we must assume that any visible symbol is
13976 referenced. */
13977
13978 bool
13979 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
13980 {
13981 struct bfd_link_info *info = (struct bfd_link_info *) inf;
13982 struct bfd_elf_dynamic_list *d = info->dynamic_list;
13983
13984 if ((h->root.type == bfd_link_hash_defined
13985 || h->root.type == bfd_link_hash_defweak)
13986 && (!h->start_stop
13987 || h->root.ldscript_def
13988 || !info->start_stop_gc)
13989 && ((h->ref_dynamic && !h->forced_local)
13990 || ((h->def_regular || ELF_COMMON_DEF_P (h))
13991 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
13992 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
13993 && (!bfd_link_executable (info)
13994 || info->gc_keep_exported
13995 || info->export_dynamic
13996 || (h->dynamic
13997 && d != NULL
13998 && (*d->match) (&d->head, NULL, h->root.root.string)))
13999 && (h->versioned >= versioned
14000 || !bfd_hide_sym_by_version (info->version_info,
14001 h->root.root.string)))))
14002 h->root.u.def.section->flags |= SEC_KEEP;
14003
14004 return true;
14005 }
14006
14007 /* Keep all sections containing symbols undefined on the command-line,
14008 and the section containing the entry symbol. */
14009
14010 void
14011 _bfd_elf_gc_keep (struct bfd_link_info *info)
14012 {
14013 struct bfd_sym_chain *sym;
14014
14015 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
14016 {
14017 struct elf_link_hash_entry *h;
14018
14019 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
14020 false, false, false);
14021
14022 if (h != NULL
14023 && (h->root.type == bfd_link_hash_defined
14024 || h->root.type == bfd_link_hash_defweak)
14025 && !bfd_is_const_section (h->root.u.def.section))
14026 h->root.u.def.section->flags |= SEC_KEEP;
14027 }
14028 }
14029
14030 bool
14031 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
14032 struct bfd_link_info *info)
14033 {
14034 bfd *ibfd = info->input_bfds;
14035
14036 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14037 {
14038 asection *sec;
14039 struct elf_reloc_cookie cookie;
14040
14041 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
14042 continue;
14043 sec = ibfd->sections;
14044 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14045 continue;
14046
14047 if (!init_reloc_cookie (&cookie, info, ibfd))
14048 return false;
14049
14050 for (sec = ibfd->sections; sec; sec = sec->next)
14051 {
14052 if (startswith (bfd_section_name (sec), ".eh_frame_entry")
14053 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
14054 {
14055 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
14056 fini_reloc_cookie_rels (&cookie, sec);
14057 }
14058 }
14059 }
14060 return true;
14061 }
14062
14063 /* Do mark and sweep of unused sections. */
14064
14065 bool
14066 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
14067 {
14068 bool ok = true;
14069 bfd *sub;
14070 elf_gc_mark_hook_fn gc_mark_hook;
14071 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14072 struct elf_link_hash_table *htab;
14073 struct link_info_ok info_ok;
14074
14075 if (!bed->can_gc_sections
14076 || !is_elf_hash_table (info->hash))
14077 {
14078 _bfd_error_handler(_("warning: gc-sections option ignored"));
14079 return true;
14080 }
14081
14082 bed->gc_keep (info);
14083 htab = elf_hash_table (info);
14084
14085 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
14086 at the .eh_frame section if we can mark the FDEs individually. */
14087 for (sub = info->input_bfds;
14088 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
14089 sub = sub->link.next)
14090 {
14091 asection *sec;
14092 struct elf_reloc_cookie cookie;
14093
14094 sec = sub->sections;
14095 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14096 continue;
14097 sec = bfd_get_section_by_name (sub, ".eh_frame");
14098 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
14099 {
14100 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
14101 if (elf_section_data (sec)->sec_info
14102 && (sec->flags & SEC_LINKER_CREATED) == 0)
14103 elf_eh_frame_section (sub) = sec;
14104 fini_reloc_cookie_for_section (&cookie, sec);
14105 sec = bfd_get_next_section_by_name (NULL, sec);
14106 }
14107 }
14108
14109 /* Apply transitive closure to the vtable entry usage info. */
14110 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
14111 if (!ok)
14112 return false;
14113
14114 /* Kill the vtable relocations that were not used. */
14115 info_ok.info = info;
14116 info_ok.ok = true;
14117 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &info_ok);
14118 if (!info_ok.ok)
14119 return false;
14120
14121 /* Mark dynamically referenced symbols. */
14122 if (htab->dynamic_sections_created || info->gc_keep_exported)
14123 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
14124
14125 /* Grovel through relocs to find out who stays ... */
14126 gc_mark_hook = bed->gc_mark_hook;
14127 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
14128 {
14129 asection *o;
14130
14131 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
14132 || elf_object_id (sub) != elf_hash_table_id (htab)
14133 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
14134 continue;
14135
14136 o = sub->sections;
14137 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14138 continue;
14139
14140 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
14141 Also treat note sections as a root, if the section is not part
14142 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
14143 well as FINI_ARRAY sections for ld -r. */
14144 for (o = sub->sections; o != NULL; o = o->next)
14145 if (!o->gc_mark
14146 && (o->flags & SEC_EXCLUDE) == 0
14147 && ((o->flags & SEC_KEEP) != 0
14148 || (bfd_link_relocatable (info)
14149 && ((elf_section_data (o)->this_hdr.sh_type
14150 == SHT_PREINIT_ARRAY)
14151 || (elf_section_data (o)->this_hdr.sh_type
14152 == SHT_INIT_ARRAY)
14153 || (elf_section_data (o)->this_hdr.sh_type
14154 == SHT_FINI_ARRAY)))
14155 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
14156 && elf_next_in_group (o) == NULL
14157 && elf_linked_to_section (o) == NULL)
14158 || ((elf_tdata (sub)->has_gnu_osabi & elf_gnu_osabi_retain)
14159 && (elf_section_flags (o) & SHF_GNU_RETAIN))))
14160 {
14161 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
14162 return false;
14163 }
14164 }
14165
14166 /* Allow the backend to mark additional target specific sections. */
14167 bed->gc_mark_extra_sections (info, gc_mark_hook);
14168
14169 /* ... and mark SEC_EXCLUDE for those that go. */
14170 return elf_gc_sweep (abfd, info);
14171 }
14172 \f
14173 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
14174
14175 bool
14176 bfd_elf_gc_record_vtinherit (bfd *abfd,
14177 asection *sec,
14178 struct elf_link_hash_entry *h,
14179 bfd_vma offset)
14180 {
14181 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
14182 struct elf_link_hash_entry **search, *child;
14183 size_t extsymcount;
14184 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14185
14186 /* The sh_info field of the symtab header tells us where the
14187 external symbols start. We don't care about the local symbols at
14188 this point. */
14189 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
14190 if (!elf_bad_symtab (abfd))
14191 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
14192
14193 sym_hashes = elf_sym_hashes (abfd);
14194 sym_hashes_end = sym_hashes + extsymcount;
14195
14196 /* Hunt down the child symbol, which is in this section at the same
14197 offset as the relocation. */
14198 for (search = sym_hashes; search != sym_hashes_end; ++search)
14199 {
14200 if ((child = *search) != NULL
14201 && (child->root.type == bfd_link_hash_defined
14202 || child->root.type == bfd_link_hash_defweak)
14203 && child->root.u.def.section == sec
14204 && child->root.u.def.value == offset)
14205 goto win;
14206 }
14207
14208 /* xgettext:c-format */
14209 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
14210 abfd, sec, (uint64_t) offset);
14211 bfd_set_error (bfd_error_invalid_operation);
14212 return false;
14213
14214 win:
14215 if (!child->u2.vtable)
14216 {
14217 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
14218 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
14219 if (!child->u2.vtable)
14220 return false;
14221 }
14222 if (!h)
14223 {
14224 /* This *should* only be the absolute section. It could potentially
14225 be that someone has defined a non-global vtable though, which
14226 would be bad. It isn't worth paging in the local symbols to be
14227 sure though; that case should simply be handled by the assembler. */
14228
14229 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
14230 }
14231 else
14232 child->u2.vtable->parent = h;
14233
14234 return true;
14235 }
14236
14237 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
14238
14239 bool
14240 bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec,
14241 struct elf_link_hash_entry *h,
14242 bfd_vma addend)
14243 {
14244 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14245 unsigned int log_file_align = bed->s->log_file_align;
14246
14247 if (!h)
14248 {
14249 /* xgettext:c-format */
14250 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
14251 abfd, sec);
14252 bfd_set_error (bfd_error_bad_value);
14253 return false;
14254 }
14255
14256 if (!h->u2.vtable)
14257 {
14258 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
14259 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
14260 if (!h->u2.vtable)
14261 return false;
14262 }
14263
14264 if (addend >= h->u2.vtable->size)
14265 {
14266 size_t size, bytes, file_align;
14267 bool *ptr = h->u2.vtable->used;
14268
14269 /* While the symbol is undefined, we have to be prepared to handle
14270 a zero size. */
14271 file_align = 1 << log_file_align;
14272 if (h->root.type == bfd_link_hash_undefined)
14273 size = addend + file_align;
14274 else
14275 {
14276 size = h->size;
14277 if (addend >= size)
14278 {
14279 /* Oops! We've got a reference past the defined end of
14280 the table. This is probably a bug -- shall we warn? */
14281 size = addend + file_align;
14282 }
14283 }
14284 size = (size + file_align - 1) & -file_align;
14285
14286 /* Allocate one extra entry for use as a "done" flag for the
14287 consolidation pass. */
14288 bytes = ((size >> log_file_align) + 1) * sizeof (bool);
14289
14290 if (ptr)
14291 {
14292 ptr = (bool *) bfd_realloc (ptr - 1, bytes);
14293
14294 if (ptr != NULL)
14295 {
14296 size_t oldbytes;
14297
14298 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
14299 * sizeof (bool));
14300 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
14301 }
14302 }
14303 else
14304 ptr = (bool *) bfd_zmalloc (bytes);
14305
14306 if (ptr == NULL)
14307 return false;
14308
14309 /* And arrange for that done flag to be at index -1. */
14310 h->u2.vtable->used = ptr + 1;
14311 h->u2.vtable->size = size;
14312 }
14313
14314 h->u2.vtable->used[addend >> log_file_align] = true;
14315
14316 return true;
14317 }
14318
14319 /* Map an ELF section header flag to its corresponding string. */
14320 typedef struct
14321 {
14322 char *flag_name;
14323 flagword flag_value;
14324 } elf_flags_to_name_table;
14325
14326 static const elf_flags_to_name_table elf_flags_to_names [] =
14327 {
14328 { "SHF_WRITE", SHF_WRITE },
14329 { "SHF_ALLOC", SHF_ALLOC },
14330 { "SHF_EXECINSTR", SHF_EXECINSTR },
14331 { "SHF_MERGE", SHF_MERGE },
14332 { "SHF_STRINGS", SHF_STRINGS },
14333 { "SHF_INFO_LINK", SHF_INFO_LINK},
14334 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
14335 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
14336 { "SHF_GROUP", SHF_GROUP },
14337 { "SHF_TLS", SHF_TLS },
14338 { "SHF_MASKOS", SHF_MASKOS },
14339 { "SHF_EXCLUDE", SHF_EXCLUDE },
14340 };
14341
14342 /* Returns TRUE if the section is to be included, otherwise FALSE. */
14343 bool
14344 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
14345 struct flag_info *flaginfo,
14346 asection *section)
14347 {
14348 const bfd_vma sh_flags = elf_section_flags (section);
14349
14350 if (!flaginfo->flags_initialized)
14351 {
14352 bfd *obfd = info->output_bfd;
14353 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14354 struct flag_info_list *tf = flaginfo->flag_list;
14355 int with_hex = 0;
14356 int without_hex = 0;
14357
14358 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
14359 {
14360 unsigned i;
14361 flagword (*lookup) (char *);
14362
14363 lookup = bed->elf_backend_lookup_section_flags_hook;
14364 if (lookup != NULL)
14365 {
14366 flagword hexval = (*lookup) ((char *) tf->name);
14367
14368 if (hexval != 0)
14369 {
14370 if (tf->with == with_flags)
14371 with_hex |= hexval;
14372 else if (tf->with == without_flags)
14373 without_hex |= hexval;
14374 tf->valid = true;
14375 continue;
14376 }
14377 }
14378 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
14379 {
14380 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
14381 {
14382 if (tf->with == with_flags)
14383 with_hex |= elf_flags_to_names[i].flag_value;
14384 else if (tf->with == without_flags)
14385 without_hex |= elf_flags_to_names[i].flag_value;
14386 tf->valid = true;
14387 break;
14388 }
14389 }
14390 if (!tf->valid)
14391 {
14392 info->callbacks->einfo
14393 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
14394 return false;
14395 }
14396 }
14397 flaginfo->flags_initialized = true;
14398 flaginfo->only_with_flags |= with_hex;
14399 flaginfo->not_with_flags |= without_hex;
14400 }
14401
14402 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
14403 return false;
14404
14405 if ((flaginfo->not_with_flags & sh_flags) != 0)
14406 return false;
14407
14408 return true;
14409 }
14410
14411 struct alloc_got_off_arg {
14412 bfd_vma gotoff;
14413 struct bfd_link_info *info;
14414 };
14415
14416 /* We need a special top-level link routine to convert got reference counts
14417 to real got offsets. */
14418
14419 static bool
14420 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
14421 {
14422 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
14423 bfd *obfd = gofarg->info->output_bfd;
14424 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14425
14426 if (h->got.refcount > 0)
14427 {
14428 h->got.offset = gofarg->gotoff;
14429 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
14430 }
14431 else
14432 h->got.offset = (bfd_vma) -1;
14433
14434 return true;
14435 }
14436
14437 /* And an accompanying bit to work out final got entry offsets once
14438 we're done. Should be called from final_link. */
14439
14440 bool
14441 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
14442 struct bfd_link_info *info)
14443 {
14444 bfd *i;
14445 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14446 bfd_vma gotoff;
14447 struct alloc_got_off_arg gofarg;
14448
14449 BFD_ASSERT (abfd == info->output_bfd);
14450
14451 if (! is_elf_hash_table (info->hash))
14452 return false;
14453
14454 /* The GOT offset is relative to the .got section, but the GOT header is
14455 put into the .got.plt section, if the backend uses it. */
14456 if (bed->want_got_plt)
14457 gotoff = 0;
14458 else
14459 gotoff = bed->got_header_size;
14460
14461 /* Do the local .got entries first. */
14462 for (i = info->input_bfds; i; i = i->link.next)
14463 {
14464 bfd_signed_vma *local_got;
14465 size_t j, locsymcount;
14466 Elf_Internal_Shdr *symtab_hdr;
14467
14468 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
14469 continue;
14470
14471 local_got = elf_local_got_refcounts (i);
14472 if (!local_got)
14473 continue;
14474
14475 symtab_hdr = &elf_tdata (i)->symtab_hdr;
14476 if (elf_bad_symtab (i))
14477 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
14478 else
14479 locsymcount = symtab_hdr->sh_info;
14480
14481 for (j = 0; j < locsymcount; ++j)
14482 {
14483 if (local_got[j] > 0)
14484 {
14485 local_got[j] = gotoff;
14486 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
14487 }
14488 else
14489 local_got[j] = (bfd_vma) -1;
14490 }
14491 }
14492
14493 /* Then the global .got entries. .plt refcounts are handled by
14494 adjust_dynamic_symbol */
14495 gofarg.gotoff = gotoff;
14496 gofarg.info = info;
14497 elf_link_hash_traverse (elf_hash_table (info),
14498 elf_gc_allocate_got_offsets,
14499 &gofarg);
14500 return true;
14501 }
14502
14503 /* Many folk need no more in the way of final link than this, once
14504 got entry reference counting is enabled. */
14505
14506 bool
14507 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
14508 {
14509 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
14510 return false;
14511
14512 /* Invoke the regular ELF backend linker to do all the work. */
14513 return bfd_elf_final_link (abfd, info);
14514 }
14515
14516 bool
14517 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
14518 {
14519 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
14520
14521 if (rcookie->bad_symtab)
14522 rcookie->rel = rcookie->rels;
14523
14524 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
14525 {
14526 unsigned long r_symndx;
14527
14528 if (! rcookie->bad_symtab)
14529 if (rcookie->rel->r_offset > offset)
14530 return false;
14531 if (rcookie->rel->r_offset != offset)
14532 continue;
14533
14534 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
14535 if (r_symndx == STN_UNDEF)
14536 return true;
14537
14538 if (r_symndx >= rcookie->locsymcount
14539 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
14540 {
14541 struct elf_link_hash_entry *h;
14542
14543 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
14544
14545 while (h->root.type == bfd_link_hash_indirect
14546 || h->root.type == bfd_link_hash_warning)
14547 h = (struct elf_link_hash_entry *) h->root.u.i.link;
14548
14549 if ((h->root.type == bfd_link_hash_defined
14550 || h->root.type == bfd_link_hash_defweak)
14551 && (h->root.u.def.section->owner != rcookie->abfd
14552 || h->root.u.def.section->kept_section != NULL
14553 || discarded_section (h->root.u.def.section)))
14554 return true;
14555 }
14556 else
14557 {
14558 /* It's not a relocation against a global symbol,
14559 but it could be a relocation against a local
14560 symbol for a discarded section. */
14561 asection *isec;
14562 Elf_Internal_Sym *isym;
14563
14564 /* Need to: get the symbol; get the section. */
14565 isym = &rcookie->locsyms[r_symndx];
14566 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
14567 if (isec != NULL
14568 && (isec->kept_section != NULL
14569 || discarded_section (isec)))
14570 return true;
14571 }
14572 return false;
14573 }
14574 return false;
14575 }
14576
14577 /* Discard unneeded references to discarded sections.
14578 Returns -1 on error, 1 if any section's size was changed, 0 if
14579 nothing changed. This function assumes that the relocations are in
14580 sorted order, which is true for all known assemblers. */
14581
14582 int
14583 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
14584 {
14585 struct elf_reloc_cookie cookie;
14586 asection *o;
14587 bfd *abfd;
14588 int changed = 0;
14589
14590 if (info->traditional_format
14591 || !is_elf_hash_table (info->hash))
14592 return 0;
14593
14594 o = bfd_get_section_by_name (output_bfd, ".stab");
14595 if (o != NULL)
14596 {
14597 asection *i;
14598
14599 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14600 {
14601 if (i->size == 0
14602 || i->reloc_count == 0
14603 || i->sec_info_type != SEC_INFO_TYPE_STABS)
14604 continue;
14605
14606 abfd = i->owner;
14607 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14608 continue;
14609
14610 if (!init_reloc_cookie_for_section (&cookie, info, i))
14611 return -1;
14612
14613 if (_bfd_discard_section_stabs (abfd, i,
14614 elf_section_data (i)->sec_info,
14615 bfd_elf_reloc_symbol_deleted_p,
14616 &cookie))
14617 changed = 1;
14618
14619 fini_reloc_cookie_for_section (&cookie, i);
14620 }
14621 }
14622
14623 o = NULL;
14624 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
14625 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
14626 if (o != NULL)
14627 {
14628 asection *i;
14629 int eh_changed = 0;
14630 unsigned int eh_alignment; /* Octets. */
14631
14632 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14633 {
14634 if (i->size == 0)
14635 continue;
14636
14637 abfd = i->owner;
14638 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14639 continue;
14640
14641 if (!init_reloc_cookie_for_section (&cookie, info, i))
14642 return -1;
14643
14644 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
14645 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
14646 bfd_elf_reloc_symbol_deleted_p,
14647 &cookie))
14648 {
14649 eh_changed = 1;
14650 if (i->size != i->rawsize)
14651 changed = 1;
14652 }
14653
14654 fini_reloc_cookie_for_section (&cookie, i);
14655 }
14656
14657 eh_alignment = ((1 << o->alignment_power)
14658 * bfd_octets_per_byte (output_bfd, o));
14659 /* Skip over zero terminator, and prevent empty sections from
14660 adding alignment padding at the end. */
14661 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
14662 if (i->size == 0)
14663 i->flags |= SEC_EXCLUDE;
14664 else if (i->size > 4)
14665 break;
14666 /* The last non-empty eh_frame section doesn't need padding. */
14667 if (i != NULL)
14668 i = i->map_tail.s;
14669 /* Any prior sections must pad the last FDE out to the output
14670 section alignment. Otherwise we might have zero padding
14671 between sections, which would be seen as a terminator. */
14672 for (; i != NULL; i = i->map_tail.s)
14673 if (i->size == 4)
14674 /* All but the last zero terminator should have been removed. */
14675 BFD_FAIL ();
14676 else
14677 {
14678 bfd_size_type size
14679 = (i->size + eh_alignment - 1) & -eh_alignment;
14680 if (i->size != size)
14681 {
14682 i->size = size;
14683 changed = 1;
14684 eh_changed = 1;
14685 }
14686 }
14687 if (eh_changed)
14688 elf_link_hash_traverse (elf_hash_table (info),
14689 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
14690 }
14691
14692 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
14693 {
14694 const struct elf_backend_data *bed;
14695 asection *s;
14696
14697 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14698 continue;
14699 s = abfd->sections;
14700 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14701 continue;
14702
14703 bed = get_elf_backend_data (abfd);
14704
14705 if (bed->elf_backend_discard_info != NULL)
14706 {
14707 if (!init_reloc_cookie (&cookie, info, abfd))
14708 return -1;
14709
14710 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
14711 changed = 1;
14712
14713 fini_reloc_cookie (&cookie, abfd);
14714 }
14715 }
14716
14717 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
14718 _bfd_elf_end_eh_frame_parsing (info);
14719
14720 if (info->eh_frame_hdr_type
14721 && !bfd_link_relocatable (info)
14722 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
14723 changed = 1;
14724
14725 return changed;
14726 }
14727
14728 bool
14729 _bfd_elf_section_already_linked (bfd *abfd,
14730 asection *sec,
14731 struct bfd_link_info *info)
14732 {
14733 flagword flags;
14734 const char *name, *key;
14735 struct bfd_section_already_linked *l;
14736 struct bfd_section_already_linked_hash_entry *already_linked_list;
14737
14738 if (sec->output_section == bfd_abs_section_ptr)
14739 return false;
14740
14741 flags = sec->flags;
14742
14743 /* Return if it isn't a linkonce section. A comdat group section
14744 also has SEC_LINK_ONCE set. */
14745 if ((flags & SEC_LINK_ONCE) == 0)
14746 return false;
14747
14748 /* Don't put group member sections on our list of already linked
14749 sections. They are handled as a group via their group section. */
14750 if (elf_sec_group (sec) != NULL)
14751 return false;
14752
14753 /* For a SHT_GROUP section, use the group signature as the key. */
14754 name = sec->name;
14755 if ((flags & SEC_GROUP) != 0
14756 && elf_next_in_group (sec) != NULL
14757 && elf_group_name (elf_next_in_group (sec)) != NULL)
14758 key = elf_group_name (elf_next_in_group (sec));
14759 else
14760 {
14761 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
14762 if (startswith (name, ".gnu.linkonce.")
14763 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
14764 key++;
14765 else
14766 /* Must be a user linkonce section that doesn't follow gcc's
14767 naming convention. In this case we won't be matching
14768 single member groups. */
14769 key = name;
14770 }
14771
14772 already_linked_list = bfd_section_already_linked_table_lookup (key);
14773
14774 for (l = already_linked_list->entry; l != NULL; l = l->next)
14775 {
14776 /* We may have 2 different types of sections on the list: group
14777 sections with a signature of <key> (<key> is some string),
14778 and linkonce sections named .gnu.linkonce.<type>.<key>.
14779 Match like sections. LTO plugin sections are an exception.
14780 They are always named .gnu.linkonce.t.<key> and match either
14781 type of section. */
14782 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
14783 && ((flags & SEC_GROUP) != 0
14784 || strcmp (name, l->sec->name) == 0))
14785 || (l->sec->owner->flags & BFD_PLUGIN) != 0
14786 || (sec->owner->flags & BFD_PLUGIN) != 0)
14787 {
14788 /* The section has already been linked. See if we should
14789 issue a warning. */
14790 if (!_bfd_handle_already_linked (sec, l, info))
14791 return false;
14792
14793 if (flags & SEC_GROUP)
14794 {
14795 asection *first = elf_next_in_group (sec);
14796 asection *s = first;
14797
14798 while (s != NULL)
14799 {
14800 s->output_section = bfd_abs_section_ptr;
14801 /* Record which group discards it. */
14802 s->kept_section = l->sec;
14803 s = elf_next_in_group (s);
14804 /* These lists are circular. */
14805 if (s == first)
14806 break;
14807 }
14808 }
14809
14810 return true;
14811 }
14812 }
14813
14814 /* A single member comdat group section may be discarded by a
14815 linkonce section and vice versa. */
14816 if ((flags & SEC_GROUP) != 0)
14817 {
14818 asection *first = elf_next_in_group (sec);
14819
14820 if (first != NULL && elf_next_in_group (first) == first)
14821 /* Check this single member group against linkonce sections. */
14822 for (l = already_linked_list->entry; l != NULL; l = l->next)
14823 if ((l->sec->flags & SEC_GROUP) == 0
14824 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14825 {
14826 first->output_section = bfd_abs_section_ptr;
14827 first->kept_section = l->sec;
14828 sec->output_section = bfd_abs_section_ptr;
14829 break;
14830 }
14831 }
14832 else
14833 /* Check this linkonce section against single member groups. */
14834 for (l = already_linked_list->entry; l != NULL; l = l->next)
14835 if (l->sec->flags & SEC_GROUP)
14836 {
14837 asection *first = elf_next_in_group (l->sec);
14838
14839 if (first != NULL
14840 && elf_next_in_group (first) == first
14841 && bfd_elf_match_symbols_in_sections (first, sec, info))
14842 {
14843 sec->output_section = bfd_abs_section_ptr;
14844 sec->kept_section = first;
14845 break;
14846 }
14847 }
14848
14849 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14850 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14851 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14852 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14853 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14854 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14855 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14856 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14857 The reverse order cannot happen as there is never a bfd with only the
14858 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14859 matter as here were are looking only for cross-bfd sections. */
14860
14861 if ((flags & SEC_GROUP) == 0 && startswith (name, ".gnu.linkonce.r."))
14862 for (l = already_linked_list->entry; l != NULL; l = l->next)
14863 if ((l->sec->flags & SEC_GROUP) == 0
14864 && startswith (l->sec->name, ".gnu.linkonce.t."))
14865 {
14866 if (abfd != l->sec->owner)
14867 sec->output_section = bfd_abs_section_ptr;
14868 break;
14869 }
14870
14871 /* This is the first section with this name. Record it. */
14872 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
14873 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
14874 return sec->output_section == bfd_abs_section_ptr;
14875 }
14876
14877 bool
14878 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
14879 {
14880 return sym->st_shndx == SHN_COMMON;
14881 }
14882
14883 unsigned int
14884 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14885 {
14886 return SHN_COMMON;
14887 }
14888
14889 asection *
14890 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14891 {
14892 return bfd_com_section_ptr;
14893 }
14894
14895 bfd_vma
14896 _bfd_elf_default_got_elt_size (bfd *abfd,
14897 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14898 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14899 bfd *ibfd ATTRIBUTE_UNUSED,
14900 unsigned long symndx ATTRIBUTE_UNUSED)
14901 {
14902 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14903 return bed->s->arch_size / 8;
14904 }
14905
14906 /* Routines to support the creation of dynamic relocs. */
14907
14908 /* Returns the name of the dynamic reloc section associated with SEC. */
14909
14910 static const char *
14911 get_dynamic_reloc_section_name (bfd * abfd,
14912 asection * sec,
14913 bool is_rela)
14914 {
14915 char *name;
14916 const char *old_name = bfd_section_name (sec);
14917 const char *prefix = is_rela ? ".rela" : ".rel";
14918
14919 if (old_name == NULL)
14920 return NULL;
14921
14922 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
14923 sprintf (name, "%s%s", prefix, old_name);
14924
14925 return name;
14926 }
14927
14928 /* Returns the dynamic reloc section associated with SEC.
14929 If necessary compute the name of the dynamic reloc section based
14930 on SEC's name (looked up in ABFD's string table) and the setting
14931 of IS_RELA. */
14932
14933 asection *
14934 _bfd_elf_get_dynamic_reloc_section (bfd *abfd,
14935 asection *sec,
14936 bool is_rela)
14937 {
14938 asection *reloc_sec = elf_section_data (sec)->sreloc;
14939
14940 if (reloc_sec == NULL)
14941 {
14942 const char *name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14943
14944 if (name != NULL)
14945 {
14946 reloc_sec = bfd_get_linker_section (abfd, name);
14947
14948 if (reloc_sec != NULL)
14949 elf_section_data (sec)->sreloc = reloc_sec;
14950 }
14951 }
14952
14953 return reloc_sec;
14954 }
14955
14956 /* Returns the dynamic reloc section associated with SEC. If the
14957 section does not exist it is created and attached to the DYNOBJ
14958 bfd and stored in the SRELOC field of SEC's elf_section_data
14959 structure.
14960
14961 ALIGNMENT is the alignment for the newly created section and
14962 IS_RELA defines whether the name should be .rela.<SEC's name>
14963 or .rel.<SEC's name>. The section name is looked up in the
14964 string table associated with ABFD. */
14965
14966 asection *
14967 _bfd_elf_make_dynamic_reloc_section (asection *sec,
14968 bfd *dynobj,
14969 unsigned int alignment,
14970 bfd *abfd,
14971 bool is_rela)
14972 {
14973 asection * reloc_sec = elf_section_data (sec)->sreloc;
14974
14975 if (reloc_sec == NULL)
14976 {
14977 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14978
14979 if (name == NULL)
14980 return NULL;
14981
14982 reloc_sec = bfd_get_linker_section (dynobj, name);
14983
14984 if (reloc_sec == NULL)
14985 {
14986 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14987 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
14988 if ((sec->flags & SEC_ALLOC) != 0)
14989 flags |= SEC_ALLOC | SEC_LOAD;
14990
14991 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
14992 if (reloc_sec != NULL)
14993 {
14994 /* _bfd_elf_get_sec_type_attr chooses a section type by
14995 name. Override as it may be wrong, eg. for a user
14996 section named "auto" we'll get ".relauto" which is
14997 seen to be a .rela section. */
14998 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
14999 if (!bfd_set_section_alignment (reloc_sec, alignment))
15000 reloc_sec = NULL;
15001 }
15002 }
15003
15004 elf_section_data (sec)->sreloc = reloc_sec;
15005 }
15006
15007 return reloc_sec;
15008 }
15009
15010 /* Copy the ELF symbol type and other attributes for a linker script
15011 assignment from HSRC to HDEST. Generally this should be treated as
15012 if we found a strong non-dynamic definition for HDEST (except that
15013 ld ignores multiple definition errors). */
15014 void
15015 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
15016 struct bfd_link_hash_entry *hdest,
15017 struct bfd_link_hash_entry *hsrc)
15018 {
15019 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
15020 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
15021 Elf_Internal_Sym isym;
15022
15023 ehdest->type = ehsrc->type;
15024 ehdest->target_internal = ehsrc->target_internal;
15025
15026 isym.st_other = ehsrc->other;
15027 elf_merge_st_other (abfd, ehdest, isym.st_other, NULL, true, false);
15028 }
15029
15030 /* Append a RELA relocation REL to section S in BFD. */
15031
15032 void
15033 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15034 {
15035 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15036 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
15037 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
15038 bed->s->swap_reloca_out (abfd, rel, loc);
15039 }
15040
15041 /* Append a REL relocation REL to section S in BFD. */
15042
15043 void
15044 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15045 {
15046 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15047 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
15048 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
15049 bed->s->swap_reloc_out (abfd, rel, loc);
15050 }
15051
15052 /* Define __start, __stop, .startof. or .sizeof. symbol. */
15053
15054 struct bfd_link_hash_entry *
15055 bfd_elf_define_start_stop (struct bfd_link_info *info,
15056 const char *symbol, asection *sec)
15057 {
15058 struct elf_link_hash_entry *h;
15059
15060 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
15061 false, false, true);
15062 /* NB: Common symbols will be turned into definition later. */
15063 if (h != NULL
15064 && !h->root.ldscript_def
15065 && (h->root.type == bfd_link_hash_undefined
15066 || h->root.type == bfd_link_hash_undefweak
15067 || ((h->ref_regular || h->def_dynamic)
15068 && !h->def_regular
15069 && h->root.type != bfd_link_hash_common)))
15070 {
15071 bool was_dynamic = h->ref_dynamic || h->def_dynamic;
15072 h->verinfo.verdef = NULL;
15073 h->root.type = bfd_link_hash_defined;
15074 h->root.u.def.section = sec;
15075 h->root.u.def.value = 0;
15076 h->def_regular = 1;
15077 h->def_dynamic = 0;
15078 h->start_stop = 1;
15079 h->u2.start_stop_section = sec;
15080 if (symbol[0] == '.')
15081 {
15082 /* .startof. and .sizeof. symbols are local. */
15083 const struct elf_backend_data *bed;
15084 bed = get_elf_backend_data (info->output_bfd);
15085 (*bed->elf_backend_hide_symbol) (info, h, true);
15086 }
15087 else
15088 {
15089 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
15090 h->other = ((h->other & ~ELF_ST_VISIBILITY (-1))
15091 | info->start_stop_visibility);
15092 if (was_dynamic)
15093 bfd_elf_link_record_dynamic_symbol (info, h);
15094 }
15095 return &h->root;
15096 }
15097 return NULL;
15098 }
15099
15100 /* Find dynamic relocs for H that apply to read-only sections. */
15101
15102 asection *
15103 _bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry *h)
15104 {
15105 struct elf_dyn_relocs *p;
15106
15107 for (p = h->dyn_relocs; p != NULL; p = p->next)
15108 {
15109 asection *s = p->sec->output_section;
15110
15111 if (s != NULL && (s->flags & SEC_READONLY) != 0)
15112 return p->sec;
15113 }
15114 return NULL;
15115 }
15116
15117 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
15118 read-only sections. */
15119
15120 bool
15121 _bfd_elf_maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
15122 {
15123 asection *sec;
15124
15125 if (h->root.type == bfd_link_hash_indirect)
15126 return true;
15127
15128 sec = _bfd_elf_readonly_dynrelocs (h);
15129 if (sec != NULL)
15130 {
15131 struct bfd_link_info *info = (struct bfd_link_info *) inf;
15132
15133 info->flags |= DF_TEXTREL;
15134 /* xgettext:c-format */
15135 info->callbacks->minfo (_("%pB: dynamic relocation against `%pT' "
15136 "in read-only section `%pA'\n"),
15137 sec->owner, h->root.root.string, sec);
15138
15139 if (bfd_link_textrel_check (info))
15140 /* xgettext:c-format */
15141 info->callbacks->einfo (_("%P: %pB: warning: relocation against `%s' "
15142 "in read-only section `%pA'\n"),
15143 sec->owner, h->root.root.string, sec);
15144
15145 /* Not an error, just cut short the traversal. */
15146 return false;
15147 }
15148 return true;
15149 }
15150
15151 /* Add dynamic tags. */
15152
15153 bool
15154 _bfd_elf_add_dynamic_tags (bfd *output_bfd, struct bfd_link_info *info,
15155 bool need_dynamic_reloc)
15156 {
15157 struct elf_link_hash_table *htab = elf_hash_table (info);
15158
15159 if (htab->dynamic_sections_created)
15160 {
15161 /* Add some entries to the .dynamic section. We fill in the
15162 values later, in finish_dynamic_sections, but we must add
15163 the entries now so that we get the correct size for the
15164 .dynamic section. The DT_DEBUG entry is filled in by the
15165 dynamic linker and used by the debugger. */
15166 #define add_dynamic_entry(TAG, VAL) \
15167 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
15168
15169 const struct elf_backend_data *bed
15170 = get_elf_backend_data (output_bfd);
15171
15172 if (bfd_link_executable (info))
15173 {
15174 if (!add_dynamic_entry (DT_DEBUG, 0))
15175 return false;
15176 }
15177
15178 if (htab->dt_pltgot_required || htab->splt->size != 0)
15179 {
15180 /* DT_PLTGOT is used by prelink even if there is no PLT
15181 relocation. */
15182 if (!add_dynamic_entry (DT_PLTGOT, 0))
15183 return false;
15184 }
15185
15186 if (htab->dt_jmprel_required || htab->srelplt->size != 0)
15187 {
15188 if (!add_dynamic_entry (DT_PLTRELSZ, 0)
15189 || !add_dynamic_entry (DT_PLTREL,
15190 (bed->rela_plts_and_copies_p
15191 ? DT_RELA : DT_REL))
15192 || !add_dynamic_entry (DT_JMPREL, 0))
15193 return false;
15194 }
15195
15196 if (htab->tlsdesc_plt
15197 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
15198 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
15199 return false;
15200
15201 if (need_dynamic_reloc)
15202 {
15203 if (bed->rela_plts_and_copies_p)
15204 {
15205 if (!add_dynamic_entry (DT_RELA, 0)
15206 || !add_dynamic_entry (DT_RELASZ, 0)
15207 || !add_dynamic_entry (DT_RELAENT,
15208 bed->s->sizeof_rela))
15209 return false;
15210 }
15211 else
15212 {
15213 if (!add_dynamic_entry (DT_REL, 0)
15214 || !add_dynamic_entry (DT_RELSZ, 0)
15215 || !add_dynamic_entry (DT_RELENT,
15216 bed->s->sizeof_rel))
15217 return false;
15218 }
15219
15220 /* If any dynamic relocs apply to a read-only section,
15221 then we need a DT_TEXTREL entry. */
15222 if ((info->flags & DF_TEXTREL) == 0)
15223 elf_link_hash_traverse (htab, _bfd_elf_maybe_set_textrel,
15224 info);
15225
15226 if ((info->flags & DF_TEXTREL) != 0)
15227 {
15228 if (htab->ifunc_resolvers)
15229 info->callbacks->einfo
15230 (_("%P: warning: GNU indirect functions with DT_TEXTREL "
15231 "may result in a segfault at runtime; recompile with %s\n"),
15232 bfd_link_dll (info) ? "-fPIC" : "-fPIE");
15233
15234 if (!add_dynamic_entry (DT_TEXTREL, 0))
15235 return false;
15236 }
15237 }
15238 }
15239 #undef add_dynamic_entry
15240
15241 return true;
15242 }