]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - bfd/elflink.c
ld: Extract _bfd_elf_link_iterate_on_relocs
[thirdparty/binutils-gdb.git] / bfd / elflink.c
1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2022 Free Software Foundation, Inc.
3
4 This file is part of BFD, the Binary File Descriptor library.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
20
21 #include "sysdep.h"
22 #include "bfd.h"
23 #include "bfdlink.h"
24 #include "libbfd.h"
25 #define ARCH_SIZE 0
26 #include "elf-bfd.h"
27 #include "safe-ctype.h"
28 #include "libiberty.h"
29 #include "objalloc.h"
30 #if BFD_SUPPORTS_PLUGINS
31 #include "plugin-api.h"
32 #include "plugin.h"
33 #endif
34
35 #include <limits.h>
36 #ifndef CHAR_BIT
37 #define CHAR_BIT 8
38 #endif
39
40 /* This struct is used to pass information to routines called via
41 elf_link_hash_traverse which must return failure. */
42
43 struct elf_info_failed
44 {
45 struct bfd_link_info *info;
46 bool failed;
47 };
48
49 /* This structure is used to pass information to
50 _bfd_elf_link_find_version_dependencies. */
51
52 struct elf_find_verdep_info
53 {
54 /* General link information. */
55 struct bfd_link_info *info;
56 /* The number of dependencies. */
57 unsigned int vers;
58 /* Whether we had a failure. */
59 bool failed;
60 };
61
62 static bool _bfd_elf_fix_symbol_flags
63 (struct elf_link_hash_entry *, struct elf_info_failed *);
64
65 asection *
66 _bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
67 unsigned long r_symndx,
68 bool discard)
69 {
70 if (r_symndx >= cookie->locsymcount
71 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
72 {
73 struct elf_link_hash_entry *h;
74
75 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
76
77 while (h->root.type == bfd_link_hash_indirect
78 || h->root.type == bfd_link_hash_warning)
79 h = (struct elf_link_hash_entry *) h->root.u.i.link;
80
81 if ((h->root.type == bfd_link_hash_defined
82 || h->root.type == bfd_link_hash_defweak)
83 && discarded_section (h->root.u.def.section))
84 return h->root.u.def.section;
85 else
86 return NULL;
87 }
88 else
89 {
90 /* It's not a relocation against a global symbol,
91 but it could be a relocation against a local
92 symbol for a discarded section. */
93 asection *isec;
94 Elf_Internal_Sym *isym;
95
96 /* Need to: get the symbol; get the section. */
97 isym = &cookie->locsyms[r_symndx];
98 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
99 if (isec != NULL
100 && discard ? discarded_section (isec) : 1)
101 return isec;
102 }
103 return NULL;
104 }
105
106 /* Define a symbol in a dynamic linkage section. */
107
108 struct elf_link_hash_entry *
109 _bfd_elf_define_linkage_sym (bfd *abfd,
110 struct bfd_link_info *info,
111 asection *sec,
112 const char *name)
113 {
114 struct elf_link_hash_entry *h;
115 struct bfd_link_hash_entry *bh;
116 const struct elf_backend_data *bed;
117
118 h = elf_link_hash_lookup (elf_hash_table (info), name, false, false, false);
119 if (h != NULL)
120 {
121 /* Zap symbol defined in an as-needed lib that wasn't linked.
122 This is a symptom of a larger problem: Absolute symbols
123 defined in shared libraries can't be overridden, because we
124 lose the link to the bfd which is via the symbol section. */
125 h->root.type = bfd_link_hash_new;
126 bh = &h->root;
127 }
128 else
129 bh = NULL;
130
131 bed = get_elf_backend_data (abfd);
132 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
133 sec, 0, NULL, false, bed->collect,
134 &bh))
135 return NULL;
136 h = (struct elf_link_hash_entry *) bh;
137 BFD_ASSERT (h != NULL);
138 h->def_regular = 1;
139 h->non_elf = 0;
140 h->root.linker_def = 1;
141 h->type = STT_OBJECT;
142 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
143 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
144
145 (*bed->elf_backend_hide_symbol) (info, h, true);
146 return h;
147 }
148
149 bool
150 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
151 {
152 flagword flags;
153 asection *s;
154 struct elf_link_hash_entry *h;
155 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
156 struct elf_link_hash_table *htab = elf_hash_table (info);
157
158 /* This function may be called more than once. */
159 if (htab->sgot != NULL)
160 return true;
161
162 flags = bed->dynamic_sec_flags;
163
164 s = bfd_make_section_anyway_with_flags (abfd,
165 (bed->rela_plts_and_copies_p
166 ? ".rela.got" : ".rel.got"),
167 (bed->dynamic_sec_flags
168 | SEC_READONLY));
169 if (s == NULL
170 || !bfd_set_section_alignment (s, bed->s->log_file_align))
171 return false;
172 htab->srelgot = s;
173
174 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
175 if (s == NULL
176 || !bfd_set_section_alignment (s, bed->s->log_file_align))
177 return false;
178 htab->sgot = s;
179
180 if (bed->want_got_plt)
181 {
182 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
183 if (s == NULL
184 || !bfd_set_section_alignment (s, bed->s->log_file_align))
185 return false;
186 htab->sgotplt = s;
187 }
188
189 /* The first bit of the global offset table is the header. */
190 s->size += bed->got_header_size;
191
192 if (bed->want_got_sym)
193 {
194 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
195 (or .got.plt) section. We don't do this in the linker script
196 because we don't want to define the symbol if we are not creating
197 a global offset table. */
198 h = _bfd_elf_define_linkage_sym (abfd, info, s,
199 "_GLOBAL_OFFSET_TABLE_");
200 elf_hash_table (info)->hgot = h;
201 if (h == NULL)
202 return false;
203 }
204
205 return true;
206 }
207 \f
208 /* Create a strtab to hold the dynamic symbol names. */
209 static bool
210 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
211 {
212 struct elf_link_hash_table *hash_table;
213
214 hash_table = elf_hash_table (info);
215 if (hash_table->dynobj == NULL)
216 {
217 /* We may not set dynobj, an input file holding linker created
218 dynamic sections to abfd, which may be a dynamic object with
219 its own dynamic sections. We need to find a normal input file
220 to hold linker created sections if possible. */
221 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
222 {
223 bfd *ibfd;
224 asection *s;
225 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
226 if ((ibfd->flags
227 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
228 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
229 && elf_object_id (ibfd) == elf_hash_table_id (hash_table)
230 && !((s = ibfd->sections) != NULL
231 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS))
232 {
233 abfd = ibfd;
234 break;
235 }
236 }
237 hash_table->dynobj = abfd;
238 }
239
240 if (hash_table->dynstr == NULL)
241 {
242 hash_table->dynstr = _bfd_elf_strtab_init ();
243 if (hash_table->dynstr == NULL)
244 return false;
245 }
246 return true;
247 }
248
249 /* Create some sections which will be filled in with dynamic linking
250 information. ABFD is an input file which requires dynamic sections
251 to be created. The dynamic sections take up virtual memory space
252 when the final executable is run, so we need to create them before
253 addresses are assigned to the output sections. We work out the
254 actual contents and size of these sections later. */
255
256 bool
257 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
258 {
259 flagword flags;
260 asection *s;
261 const struct elf_backend_data *bed;
262 struct elf_link_hash_entry *h;
263
264 if (! is_elf_hash_table (info->hash))
265 return false;
266
267 if (elf_hash_table (info)->dynamic_sections_created)
268 return true;
269
270 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
271 return false;
272
273 abfd = elf_hash_table (info)->dynobj;
274 bed = get_elf_backend_data (abfd);
275
276 flags = bed->dynamic_sec_flags;
277
278 /* A dynamically linked executable has a .interp section, but a
279 shared library does not. */
280 if (bfd_link_executable (info) && !info->nointerp)
281 {
282 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
283 flags | SEC_READONLY);
284 if (s == NULL)
285 return false;
286 }
287
288 /* Create sections to hold version informations. These are removed
289 if they are not needed. */
290 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
291 flags | SEC_READONLY);
292 if (s == NULL
293 || !bfd_set_section_alignment (s, bed->s->log_file_align))
294 return false;
295
296 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
297 flags | SEC_READONLY);
298 if (s == NULL
299 || !bfd_set_section_alignment (s, 1))
300 return false;
301
302 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
303 flags | SEC_READONLY);
304 if (s == NULL
305 || !bfd_set_section_alignment (s, bed->s->log_file_align))
306 return false;
307
308 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
309 flags | SEC_READONLY);
310 if (s == NULL
311 || !bfd_set_section_alignment (s, bed->s->log_file_align))
312 return false;
313 elf_hash_table (info)->dynsym = s;
314
315 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
316 flags | SEC_READONLY);
317 if (s == NULL)
318 return false;
319
320 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
321 if (s == NULL
322 || !bfd_set_section_alignment (s, bed->s->log_file_align))
323 return false;
324
325 /* The special symbol _DYNAMIC is always set to the start of the
326 .dynamic section. We could set _DYNAMIC in a linker script, but we
327 only want to define it if we are, in fact, creating a .dynamic
328 section. We don't want to define it if there is no .dynamic
329 section, since on some ELF platforms the start up code examines it
330 to decide how to initialize the process. */
331 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
332 elf_hash_table (info)->hdynamic = h;
333 if (h == NULL)
334 return false;
335
336 if (info->emit_hash)
337 {
338 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
339 flags | SEC_READONLY);
340 if (s == NULL
341 || !bfd_set_section_alignment (s, bed->s->log_file_align))
342 return false;
343 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
344 }
345
346 if (info->emit_gnu_hash && bed->record_xhash_symbol == NULL)
347 {
348 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
349 flags | SEC_READONLY);
350 if (s == NULL
351 || !bfd_set_section_alignment (s, bed->s->log_file_align))
352 return false;
353 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
354 4 32-bit words followed by variable count of 64-bit words, then
355 variable count of 32-bit words. */
356 if (bed->s->arch_size == 64)
357 elf_section_data (s)->this_hdr.sh_entsize = 0;
358 else
359 elf_section_data (s)->this_hdr.sh_entsize = 4;
360 }
361
362 /* Let the backend create the rest of the sections. This lets the
363 backend set the right flags. The backend will normally create
364 the .got and .plt sections. */
365 if (bed->elf_backend_create_dynamic_sections == NULL
366 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
367 return false;
368
369 elf_hash_table (info)->dynamic_sections_created = true;
370
371 return true;
372 }
373
374 /* Create dynamic sections when linking against a dynamic object. */
375
376 bool
377 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
378 {
379 flagword flags, pltflags;
380 struct elf_link_hash_entry *h;
381 asection *s;
382 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
383 struct elf_link_hash_table *htab = elf_hash_table (info);
384
385 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
386 .rel[a].bss sections. */
387 flags = bed->dynamic_sec_flags;
388
389 pltflags = flags;
390 if (bed->plt_not_loaded)
391 /* We do not clear SEC_ALLOC here because we still want the OS to
392 allocate space for the section; it's just that there's nothing
393 to read in from the object file. */
394 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
395 else
396 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
397 if (bed->plt_readonly)
398 pltflags |= SEC_READONLY;
399
400 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
401 if (s == NULL
402 || !bfd_set_section_alignment (s, bed->plt_alignment))
403 return false;
404 htab->splt = s;
405
406 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
407 .plt section. */
408 if (bed->want_plt_sym)
409 {
410 h = _bfd_elf_define_linkage_sym (abfd, info, s,
411 "_PROCEDURE_LINKAGE_TABLE_");
412 elf_hash_table (info)->hplt = h;
413 if (h == NULL)
414 return false;
415 }
416
417 s = bfd_make_section_anyway_with_flags (abfd,
418 (bed->rela_plts_and_copies_p
419 ? ".rela.plt" : ".rel.plt"),
420 flags | SEC_READONLY);
421 if (s == NULL
422 || !bfd_set_section_alignment (s, bed->s->log_file_align))
423 return false;
424 htab->srelplt = s;
425
426 if (! _bfd_elf_create_got_section (abfd, info))
427 return false;
428
429 if (bed->want_dynbss)
430 {
431 /* The .dynbss section is a place to put symbols which are defined
432 by dynamic objects, are referenced by regular objects, and are
433 not functions. We must allocate space for them in the process
434 image and use a R_*_COPY reloc to tell the dynamic linker to
435 initialize them at run time. The linker script puts the .dynbss
436 section into the .bss section of the final image. */
437 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
438 SEC_ALLOC | SEC_LINKER_CREATED);
439 if (s == NULL)
440 return false;
441 htab->sdynbss = s;
442
443 if (bed->want_dynrelro)
444 {
445 /* Similarly, but for symbols that were originally in read-only
446 sections. This section doesn't really need to have contents,
447 but make it like other .data.rel.ro sections. */
448 s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
449 flags);
450 if (s == NULL)
451 return false;
452 htab->sdynrelro = s;
453 }
454
455 /* The .rel[a].bss section holds copy relocs. This section is not
456 normally needed. We need to create it here, though, so that the
457 linker will map it to an output section. We can't just create it
458 only if we need it, because we will not know whether we need it
459 until we have seen all the input files, and the first time the
460 main linker code calls BFD after examining all the input files
461 (size_dynamic_sections) the input sections have already been
462 mapped to the output sections. If the section turns out not to
463 be needed, we can discard it later. We will never need this
464 section when generating a shared object, since they do not use
465 copy relocs. */
466 if (bfd_link_executable (info))
467 {
468 s = bfd_make_section_anyway_with_flags (abfd,
469 (bed->rela_plts_and_copies_p
470 ? ".rela.bss" : ".rel.bss"),
471 flags | SEC_READONLY);
472 if (s == NULL
473 || !bfd_set_section_alignment (s, bed->s->log_file_align))
474 return false;
475 htab->srelbss = s;
476
477 if (bed->want_dynrelro)
478 {
479 s = (bfd_make_section_anyway_with_flags
480 (abfd, (bed->rela_plts_and_copies_p
481 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
482 flags | SEC_READONLY));
483 if (s == NULL
484 || !bfd_set_section_alignment (s, bed->s->log_file_align))
485 return false;
486 htab->sreldynrelro = s;
487 }
488 }
489 }
490
491 return true;
492 }
493 \f
494 /* Record a new dynamic symbol. We record the dynamic symbols as we
495 read the input files, since we need to have a list of all of them
496 before we can determine the final sizes of the output sections.
497 Note that we may actually call this function even though we are not
498 going to output any dynamic symbols; in some cases we know that a
499 symbol should be in the dynamic symbol table, but only if there is
500 one. */
501
502 bool
503 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
504 struct elf_link_hash_entry *h)
505 {
506 if (h->dynindx == -1)
507 {
508 struct elf_strtab_hash *dynstr;
509 char *p;
510 const char *name;
511 size_t indx;
512
513 if (h->root.type == bfd_link_hash_defined
514 || h->root.type == bfd_link_hash_defweak)
515 {
516 /* An IR symbol should not be made dynamic. */
517 if (h->root.u.def.section != NULL
518 && h->root.u.def.section->owner != NULL
519 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)
520 return true;
521 }
522
523 /* XXX: The ABI draft says the linker must turn hidden and
524 internal symbols into STB_LOCAL symbols when producing the
525 DSO. However, if ld.so honors st_other in the dynamic table,
526 this would not be necessary. */
527 switch (ELF_ST_VISIBILITY (h->other))
528 {
529 case STV_INTERNAL:
530 case STV_HIDDEN:
531 if (h->root.type != bfd_link_hash_undefined
532 && h->root.type != bfd_link_hash_undefweak)
533 {
534 h->forced_local = 1;
535 if (!elf_hash_table (info)->is_relocatable_executable
536 || ((h->root.type == bfd_link_hash_defined
537 || h->root.type == bfd_link_hash_defweak)
538 && h->root.u.def.section->owner != NULL
539 && h->root.u.def.section->owner->no_export)
540 || (h->root.type == bfd_link_hash_common
541 && h->root.u.c.p->section->owner != NULL
542 && h->root.u.c.p->section->owner->no_export))
543 return true;
544 }
545
546 default:
547 break;
548 }
549
550 h->dynindx = elf_hash_table (info)->dynsymcount;
551 ++elf_hash_table (info)->dynsymcount;
552
553 dynstr = elf_hash_table (info)->dynstr;
554 if (dynstr == NULL)
555 {
556 /* Create a strtab to hold the dynamic symbol names. */
557 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
558 if (dynstr == NULL)
559 return false;
560 }
561
562 /* We don't put any version information in the dynamic string
563 table. */
564 name = h->root.root.string;
565 p = strchr (name, ELF_VER_CHR);
566 if (p != NULL)
567 /* We know that the p points into writable memory. In fact,
568 there are only a few symbols that have read-only names, being
569 those like _GLOBAL_OFFSET_TABLE_ that are created specially
570 by the backends. Most symbols will have names pointing into
571 an ELF string table read from a file, or to objalloc memory. */
572 *p = 0;
573
574 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
575
576 if (p != NULL)
577 *p = ELF_VER_CHR;
578
579 if (indx == (size_t) -1)
580 return false;
581 h->dynstr_index = indx;
582 }
583
584 return true;
585 }
586 \f
587 /* Mark a symbol dynamic. */
588
589 static void
590 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
591 struct elf_link_hash_entry *h,
592 Elf_Internal_Sym *sym)
593 {
594 struct bfd_elf_dynamic_list *d = info->dynamic_list;
595
596 /* It may be called more than once on the same H. */
597 if(h->dynamic || bfd_link_relocatable (info))
598 return;
599
600 if ((info->dynamic_data
601 && (h->type == STT_OBJECT
602 || h->type == STT_COMMON
603 || (sym != NULL
604 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
605 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
606 || (d != NULL
607 && h->non_elf
608 && (*d->match) (&d->head, NULL, h->root.root.string)))
609 {
610 h->dynamic = 1;
611 /* NB: If a symbol is made dynamic by --dynamic-list, it has
612 non-IR reference. */
613 h->root.non_ir_ref_dynamic = 1;
614 }
615 }
616
617 /* Record an assignment to a symbol made by a linker script. We need
618 this in case some dynamic object refers to this symbol. */
619
620 bool
621 bfd_elf_record_link_assignment (bfd *output_bfd,
622 struct bfd_link_info *info,
623 const char *name,
624 bool provide,
625 bool hidden)
626 {
627 struct elf_link_hash_entry *h, *hv;
628 struct elf_link_hash_table *htab;
629 const struct elf_backend_data *bed;
630
631 if (!is_elf_hash_table (info->hash))
632 return true;
633
634 htab = elf_hash_table (info);
635 h = elf_link_hash_lookup (htab, name, !provide, true, false);
636 if (h == NULL)
637 return provide;
638
639 if (h->root.type == bfd_link_hash_warning)
640 h = (struct elf_link_hash_entry *) h->root.u.i.link;
641
642 if (h->versioned == unknown)
643 {
644 /* Set versioned if symbol version is unknown. */
645 char *version = strrchr (name, ELF_VER_CHR);
646 if (version)
647 {
648 if (version > name && version[-1] != ELF_VER_CHR)
649 h->versioned = versioned_hidden;
650 else
651 h->versioned = versioned;
652 }
653 }
654
655 /* Symbols defined in a linker script but not referenced anywhere
656 else will have non_elf set. */
657 if (h->non_elf)
658 {
659 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
660 h->non_elf = 0;
661 }
662
663 switch (h->root.type)
664 {
665 case bfd_link_hash_defined:
666 case bfd_link_hash_defweak:
667 case bfd_link_hash_common:
668 break;
669 case bfd_link_hash_undefweak:
670 case bfd_link_hash_undefined:
671 /* Since we're defining the symbol, don't let it seem to have not
672 been defined. record_dynamic_symbol and size_dynamic_sections
673 may depend on this. */
674 h->root.type = bfd_link_hash_new;
675 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
676 bfd_link_repair_undef_list (&htab->root);
677 break;
678 case bfd_link_hash_new:
679 break;
680 case bfd_link_hash_indirect:
681 /* We had a versioned symbol in a dynamic library. We make the
682 the versioned symbol point to this one. */
683 bed = get_elf_backend_data (output_bfd);
684 hv = h;
685 while (hv->root.type == bfd_link_hash_indirect
686 || hv->root.type == bfd_link_hash_warning)
687 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
688 /* We don't need to update h->root.u since linker will set them
689 later. */
690 h->root.type = bfd_link_hash_undefined;
691 hv->root.type = bfd_link_hash_indirect;
692 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
693 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
694 break;
695 default:
696 BFD_FAIL ();
697 return false;
698 }
699
700 /* If this symbol is being provided by the linker script, and it is
701 currently defined by a dynamic object, but not by a regular
702 object, then mark it as undefined so that the generic linker will
703 force the correct value. */
704 if (provide
705 && h->def_dynamic
706 && !h->def_regular)
707 h->root.type = bfd_link_hash_undefined;
708
709 /* If this symbol is currently defined by a dynamic object, but not
710 by a regular object, then clear out any version information because
711 the symbol will not be associated with the dynamic object any
712 more. */
713 if (h->def_dynamic && !h->def_regular)
714 h->verinfo.verdef = NULL;
715
716 /* Make sure this symbol is not garbage collected. */
717 h->mark = 1;
718
719 h->def_regular = 1;
720
721 if (hidden)
722 {
723 bed = get_elf_backend_data (output_bfd);
724 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
725 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
726 (*bed->elf_backend_hide_symbol) (info, h, true);
727 }
728
729 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
730 and executables. */
731 if (!bfd_link_relocatable (info)
732 && h->dynindx != -1
733 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
734 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
735 h->forced_local = 1;
736
737 if ((h->def_dynamic
738 || h->ref_dynamic
739 || bfd_link_dll (info)
740 || elf_hash_table (info)->is_relocatable_executable)
741 && !h->forced_local
742 && h->dynindx == -1)
743 {
744 if (! bfd_elf_link_record_dynamic_symbol (info, h))
745 return false;
746
747 /* If this is a weak defined symbol, and we know a corresponding
748 real symbol from the same dynamic object, make sure the real
749 symbol is also made into a dynamic symbol. */
750 if (h->is_weakalias)
751 {
752 struct elf_link_hash_entry *def = weakdef (h);
753
754 if (def->dynindx == -1
755 && !bfd_elf_link_record_dynamic_symbol (info, def))
756 return false;
757 }
758 }
759
760 return true;
761 }
762
763 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
764 success, and 2 on a failure caused by attempting to record a symbol
765 in a discarded section, eg. a discarded link-once section symbol. */
766
767 int
768 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
769 bfd *input_bfd,
770 long input_indx)
771 {
772 size_t amt;
773 struct elf_link_local_dynamic_entry *entry;
774 struct elf_link_hash_table *eht;
775 struct elf_strtab_hash *dynstr;
776 size_t dynstr_index;
777 char *name;
778 Elf_External_Sym_Shndx eshndx;
779 char esym[sizeof (Elf64_External_Sym)];
780
781 if (! is_elf_hash_table (info->hash))
782 return 0;
783
784 /* See if the entry exists already. */
785 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
786 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
787 return 1;
788
789 amt = sizeof (*entry);
790 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
791 if (entry == NULL)
792 return 0;
793
794 /* Go find the symbol, so that we can find it's name. */
795 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
796 1, input_indx, &entry->isym, esym, &eshndx))
797 {
798 bfd_release (input_bfd, entry);
799 return 0;
800 }
801
802 if (entry->isym.st_shndx != SHN_UNDEF
803 && entry->isym.st_shndx < SHN_LORESERVE)
804 {
805 asection *s;
806
807 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
808 if (s == NULL || bfd_is_abs_section (s->output_section))
809 {
810 /* We can still bfd_release here as nothing has done another
811 bfd_alloc. We can't do this later in this function. */
812 bfd_release (input_bfd, entry);
813 return 2;
814 }
815 }
816
817 name = (bfd_elf_string_from_elf_section
818 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
819 entry->isym.st_name));
820
821 dynstr = elf_hash_table (info)->dynstr;
822 if (dynstr == NULL)
823 {
824 /* Create a strtab to hold the dynamic symbol names. */
825 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
826 if (dynstr == NULL)
827 return 0;
828 }
829
830 dynstr_index = _bfd_elf_strtab_add (dynstr, name, false);
831 if (dynstr_index == (size_t) -1)
832 return 0;
833 entry->isym.st_name = dynstr_index;
834
835 eht = elf_hash_table (info);
836
837 entry->next = eht->dynlocal;
838 eht->dynlocal = entry;
839 entry->input_bfd = input_bfd;
840 entry->input_indx = input_indx;
841 eht->dynsymcount++;
842
843 /* Whatever binding the symbol had before, it's now local. */
844 entry->isym.st_info
845 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
846
847 /* The dynindx will be set at the end of size_dynamic_sections. */
848
849 return 1;
850 }
851
852 /* Return the dynindex of a local dynamic symbol. */
853
854 long
855 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
856 bfd *input_bfd,
857 long input_indx)
858 {
859 struct elf_link_local_dynamic_entry *e;
860
861 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
862 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
863 return e->dynindx;
864 return -1;
865 }
866
867 /* This function is used to renumber the dynamic symbols, if some of
868 them are removed because they are marked as local. This is called
869 via elf_link_hash_traverse. */
870
871 static bool
872 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
873 void *data)
874 {
875 size_t *count = (size_t *) data;
876
877 if (h->forced_local)
878 return true;
879
880 if (h->dynindx != -1)
881 h->dynindx = ++(*count);
882
883 return true;
884 }
885
886
887 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
888 STB_LOCAL binding. */
889
890 static bool
891 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
892 void *data)
893 {
894 size_t *count = (size_t *) data;
895
896 if (!h->forced_local)
897 return true;
898
899 if (h->dynindx != -1)
900 h->dynindx = ++(*count);
901
902 return true;
903 }
904
905 /* Return true if the dynamic symbol for a given section should be
906 omitted when creating a shared library. */
907 bool
908 _bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED,
909 struct bfd_link_info *info,
910 asection *p)
911 {
912 struct elf_link_hash_table *htab;
913 asection *ip;
914
915 switch (elf_section_data (p)->this_hdr.sh_type)
916 {
917 case SHT_PROGBITS:
918 case SHT_NOBITS:
919 /* If sh_type is yet undecided, assume it could be
920 SHT_PROGBITS/SHT_NOBITS. */
921 case SHT_NULL:
922 htab = elf_hash_table (info);
923 if (htab->text_index_section != NULL)
924 return p != htab->text_index_section && p != htab->data_index_section;
925
926 return (htab->dynobj != NULL
927 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
928 && ip->output_section == p);
929
930 /* There shouldn't be section relative relocations
931 against any other section. */
932 default:
933 return true;
934 }
935 }
936
937 bool
938 _bfd_elf_omit_section_dynsym_all
939 (bfd *output_bfd ATTRIBUTE_UNUSED,
940 struct bfd_link_info *info ATTRIBUTE_UNUSED,
941 asection *p ATTRIBUTE_UNUSED)
942 {
943 return true;
944 }
945
946 /* Assign dynsym indices. In a shared library we generate a section
947 symbol for each output section, which come first. Next come symbols
948 which have been forced to local binding. Then all of the back-end
949 allocated local dynamic syms, followed by the rest of the global
950 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
951 (This prevents the early call before elf_backend_init_index_section
952 and strip_excluded_output_sections setting dynindx for sections
953 that are stripped.) */
954
955 static unsigned long
956 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
957 struct bfd_link_info *info,
958 unsigned long *section_sym_count)
959 {
960 unsigned long dynsymcount = 0;
961 bool do_sec = section_sym_count != NULL;
962
963 if (bfd_link_pic (info)
964 || elf_hash_table (info)->is_relocatable_executable)
965 {
966 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
967 asection *p;
968 for (p = output_bfd->sections; p ; p = p->next)
969 if ((p->flags & SEC_EXCLUDE) == 0
970 && (p->flags & SEC_ALLOC) != 0
971 && elf_hash_table (info)->dynamic_relocs
972 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
973 {
974 ++dynsymcount;
975 if (do_sec)
976 elf_section_data (p)->dynindx = dynsymcount;
977 }
978 else if (do_sec)
979 elf_section_data (p)->dynindx = 0;
980 }
981 if (do_sec)
982 *section_sym_count = dynsymcount;
983
984 elf_link_hash_traverse (elf_hash_table (info),
985 elf_link_renumber_local_hash_table_dynsyms,
986 &dynsymcount);
987
988 if (elf_hash_table (info)->dynlocal)
989 {
990 struct elf_link_local_dynamic_entry *p;
991 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
992 p->dynindx = ++dynsymcount;
993 }
994 elf_hash_table (info)->local_dynsymcount = dynsymcount;
995
996 elf_link_hash_traverse (elf_hash_table (info),
997 elf_link_renumber_hash_table_dynsyms,
998 &dynsymcount);
999
1000 /* There is an unused NULL entry at the head of the table which we
1001 must account for in our count even if the table is empty since it
1002 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
1003 .dynamic section. */
1004 dynsymcount++;
1005
1006 elf_hash_table (info)->dynsymcount = dynsymcount;
1007 return dynsymcount;
1008 }
1009
1010 /* Merge st_other field. */
1011
1012 static void
1013 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
1014 unsigned int st_other, asection *sec,
1015 bool definition, bool dynamic)
1016 {
1017 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1018
1019 /* If st_other has a processor-specific meaning, specific
1020 code might be needed here. */
1021 if (bed->elf_backend_merge_symbol_attribute)
1022 (*bed->elf_backend_merge_symbol_attribute) (h, st_other, definition,
1023 dynamic);
1024
1025 if (!dynamic)
1026 {
1027 unsigned symvis = ELF_ST_VISIBILITY (st_other);
1028 unsigned hvis = ELF_ST_VISIBILITY (h->other);
1029
1030 /* Keep the most constraining visibility. Leave the remainder
1031 of the st_other field to elf_backend_merge_symbol_attribute. */
1032 if (symvis - 1 < hvis - 1)
1033 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
1034 }
1035 else if (definition
1036 && ELF_ST_VISIBILITY (st_other) != STV_DEFAULT
1037 && (sec->flags & SEC_READONLY) == 0)
1038 h->protected_def = 1;
1039 }
1040
1041 /* This function is called when we want to merge a new symbol with an
1042 existing symbol. It handles the various cases which arise when we
1043 find a definition in a dynamic object, or when there is already a
1044 definition in a dynamic object. The new symbol is described by
1045 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1046 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1047 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1048 of an old common symbol. We set OVERRIDE if the old symbol is
1049 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1050 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1051 to change. By OK to change, we mean that we shouldn't warn if the
1052 type or size does change. */
1053
1054 static bool
1055 _bfd_elf_merge_symbol (bfd *abfd,
1056 struct bfd_link_info *info,
1057 const char *name,
1058 Elf_Internal_Sym *sym,
1059 asection **psec,
1060 bfd_vma *pvalue,
1061 struct elf_link_hash_entry **sym_hash,
1062 bfd **poldbfd,
1063 bool *pold_weak,
1064 unsigned int *pold_alignment,
1065 bool *skip,
1066 bfd **override,
1067 bool *type_change_ok,
1068 bool *size_change_ok,
1069 bool *matched)
1070 {
1071 asection *sec, *oldsec;
1072 struct elf_link_hash_entry *h;
1073 struct elf_link_hash_entry *hi;
1074 struct elf_link_hash_entry *flip;
1075 int bind;
1076 bfd *oldbfd;
1077 bool newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
1078 bool newweak, oldweak, newfunc, oldfunc;
1079 const struct elf_backend_data *bed;
1080 char *new_version;
1081 bool default_sym = *matched;
1082
1083 *skip = false;
1084 *override = NULL;
1085
1086 sec = *psec;
1087 bind = ELF_ST_BIND (sym->st_info);
1088
1089 if (! bfd_is_und_section (sec))
1090 h = elf_link_hash_lookup (elf_hash_table (info), name, true, false, false);
1091 else
1092 h = ((struct elf_link_hash_entry *)
1093 bfd_wrapped_link_hash_lookup (abfd, info, name, true, false, false));
1094 if (h == NULL)
1095 return false;
1096 *sym_hash = h;
1097
1098 bed = get_elf_backend_data (abfd);
1099
1100 /* NEW_VERSION is the symbol version of the new symbol. */
1101 if (h->versioned != unversioned)
1102 {
1103 /* Symbol version is unknown or versioned. */
1104 new_version = strrchr (name, ELF_VER_CHR);
1105 if (new_version)
1106 {
1107 if (h->versioned == unknown)
1108 {
1109 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1110 h->versioned = versioned_hidden;
1111 else
1112 h->versioned = versioned;
1113 }
1114 new_version += 1;
1115 if (new_version[0] == '\0')
1116 new_version = NULL;
1117 }
1118 else
1119 h->versioned = unversioned;
1120 }
1121 else
1122 new_version = NULL;
1123
1124 /* For merging, we only care about real symbols. But we need to make
1125 sure that indirect symbol dynamic flags are updated. */
1126 hi = h;
1127 while (h->root.type == bfd_link_hash_indirect
1128 || h->root.type == bfd_link_hash_warning)
1129 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1130
1131 if (!*matched)
1132 {
1133 if (hi == h || h->root.type == bfd_link_hash_new)
1134 *matched = true;
1135 else
1136 {
1137 /* OLD_HIDDEN is true if the existing symbol is only visible
1138 to the symbol with the same symbol version. NEW_HIDDEN is
1139 true if the new symbol is only visible to the symbol with
1140 the same symbol version. */
1141 bool old_hidden = h->versioned == versioned_hidden;
1142 bool new_hidden = hi->versioned == versioned_hidden;
1143 if (!old_hidden && !new_hidden)
1144 /* The new symbol matches the existing symbol if both
1145 aren't hidden. */
1146 *matched = true;
1147 else
1148 {
1149 /* OLD_VERSION is the symbol version of the existing
1150 symbol. */
1151 char *old_version;
1152
1153 if (h->versioned >= versioned)
1154 old_version = strrchr (h->root.root.string,
1155 ELF_VER_CHR) + 1;
1156 else
1157 old_version = NULL;
1158
1159 /* The new symbol matches the existing symbol if they
1160 have the same symbol version. */
1161 *matched = (old_version == new_version
1162 || (old_version != NULL
1163 && new_version != NULL
1164 && strcmp (old_version, new_version) == 0));
1165 }
1166 }
1167 }
1168
1169 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1170 existing symbol. */
1171
1172 oldbfd = NULL;
1173 oldsec = NULL;
1174 switch (h->root.type)
1175 {
1176 default:
1177 break;
1178
1179 case bfd_link_hash_undefined:
1180 case bfd_link_hash_undefweak:
1181 oldbfd = h->root.u.undef.abfd;
1182 break;
1183
1184 case bfd_link_hash_defined:
1185 case bfd_link_hash_defweak:
1186 oldbfd = h->root.u.def.section->owner;
1187 oldsec = h->root.u.def.section;
1188 break;
1189
1190 case bfd_link_hash_common:
1191 oldbfd = h->root.u.c.p->section->owner;
1192 oldsec = h->root.u.c.p->section;
1193 if (pold_alignment)
1194 *pold_alignment = h->root.u.c.p->alignment_power;
1195 break;
1196 }
1197 if (poldbfd && *poldbfd == NULL)
1198 *poldbfd = oldbfd;
1199
1200 /* Differentiate strong and weak symbols. */
1201 newweak = bind == STB_WEAK;
1202 oldweak = (h->root.type == bfd_link_hash_defweak
1203 || h->root.type == bfd_link_hash_undefweak);
1204 if (pold_weak)
1205 *pold_weak = oldweak;
1206
1207 /* We have to check it for every instance since the first few may be
1208 references and not all compilers emit symbol type for undefined
1209 symbols. */
1210 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1211
1212 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1213 respectively, is from a dynamic object. */
1214
1215 newdyn = (abfd->flags & DYNAMIC) != 0;
1216
1217 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1218 syms and defined syms in dynamic libraries respectively.
1219 ref_dynamic on the other hand can be set for a symbol defined in
1220 a dynamic library, and def_dynamic may not be set; When the
1221 definition in a dynamic lib is overridden by a definition in the
1222 executable use of the symbol in the dynamic lib becomes a
1223 reference to the executable symbol. */
1224 if (newdyn)
1225 {
1226 if (bfd_is_und_section (sec))
1227 {
1228 if (bind != STB_WEAK)
1229 {
1230 h->ref_dynamic_nonweak = 1;
1231 hi->ref_dynamic_nonweak = 1;
1232 }
1233 }
1234 else
1235 {
1236 /* Update the existing symbol only if they match. */
1237 if (*matched)
1238 h->dynamic_def = 1;
1239 hi->dynamic_def = 1;
1240 }
1241 }
1242
1243 /* If we just created the symbol, mark it as being an ELF symbol.
1244 Other than that, there is nothing to do--there is no merge issue
1245 with a newly defined symbol--so we just return. */
1246
1247 if (h->root.type == bfd_link_hash_new)
1248 {
1249 h->non_elf = 0;
1250 return true;
1251 }
1252
1253 /* In cases involving weak versioned symbols, we may wind up trying
1254 to merge a symbol with itself. Catch that here, to avoid the
1255 confusion that results if we try to override a symbol with
1256 itself. The additional tests catch cases like
1257 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1258 dynamic object, which we do want to handle here. */
1259 if (abfd == oldbfd
1260 && (newweak || oldweak)
1261 && ((abfd->flags & DYNAMIC) == 0
1262 || !h->def_regular))
1263 return true;
1264
1265 olddyn = false;
1266 if (oldbfd != NULL)
1267 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1268 else if (oldsec != NULL)
1269 {
1270 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1271 indices used by MIPS ELF. */
1272 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1273 }
1274
1275 if (oldbfd != NULL
1276 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN))
1277 {
1278 if (newdyn != olddyn)
1279 {
1280 /* Handle a case where plugin_notice won't be called and thus
1281 won't set the non_ir_ref flags on the first pass over
1282 symbols. */
1283 h->root.non_ir_ref_dynamic = true;
1284 hi->root.non_ir_ref_dynamic = true;
1285 }
1286
1287 if ((oldbfd->flags & BFD_PLUGIN) != 0
1288 && hi->root.type == bfd_link_hash_indirect)
1289 {
1290 /* Change indirect symbol from IR to undefined. */
1291 hi->root.type = bfd_link_hash_undefined;
1292 hi->root.u.undef.abfd = oldbfd;
1293 }
1294 }
1295
1296 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1297 respectively, appear to be a definition rather than reference. */
1298
1299 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1300
1301 olddef = (h->root.type != bfd_link_hash_undefined
1302 && h->root.type != bfd_link_hash_undefweak
1303 && h->root.type != bfd_link_hash_common);
1304
1305 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1306 respectively, appear to be a function. */
1307
1308 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1309 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1310
1311 oldfunc = (h->type != STT_NOTYPE
1312 && bed->is_function_type (h->type));
1313
1314 if (!(newfunc && oldfunc)
1315 && ELF_ST_TYPE (sym->st_info) != h->type
1316 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1317 && h->type != STT_NOTYPE
1318 && (newdef || bfd_is_com_section (sec))
1319 && (olddef || h->root.type == bfd_link_hash_common))
1320 {
1321 /* If creating a default indirect symbol ("foo" or "foo@") from
1322 a dynamic versioned definition ("foo@@") skip doing so if
1323 there is an existing regular definition with a different
1324 type. We don't want, for example, a "time" variable in the
1325 executable overriding a "time" function in a shared library. */
1326 if (newdyn
1327 && !olddyn)
1328 {
1329 *skip = true;
1330 return true;
1331 }
1332
1333 /* When adding a symbol from a regular object file after we have
1334 created indirect symbols, undo the indirection and any
1335 dynamic state. */
1336 if (hi != h
1337 && !newdyn
1338 && olddyn)
1339 {
1340 h = hi;
1341 (*bed->elf_backend_hide_symbol) (info, h, true);
1342 h->forced_local = 0;
1343 h->ref_dynamic = 0;
1344 h->def_dynamic = 0;
1345 h->dynamic_def = 0;
1346 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1347 {
1348 h->root.type = bfd_link_hash_undefined;
1349 h->root.u.undef.abfd = abfd;
1350 }
1351 else
1352 {
1353 h->root.type = bfd_link_hash_new;
1354 h->root.u.undef.abfd = NULL;
1355 }
1356 return true;
1357 }
1358 }
1359
1360 /* Check TLS symbols. We don't check undefined symbols introduced
1361 by "ld -u" which have no type (and oldbfd NULL), and we don't
1362 check symbols from plugins because they also have no type. */
1363 if (oldbfd != NULL
1364 && (oldbfd->flags & BFD_PLUGIN) == 0
1365 && (abfd->flags & BFD_PLUGIN) == 0
1366 && ELF_ST_TYPE (sym->st_info) != h->type
1367 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1368 {
1369 bfd *ntbfd, *tbfd;
1370 bool ntdef, tdef;
1371 asection *ntsec, *tsec;
1372
1373 if (h->type == STT_TLS)
1374 {
1375 ntbfd = abfd;
1376 ntsec = sec;
1377 ntdef = newdef;
1378 tbfd = oldbfd;
1379 tsec = oldsec;
1380 tdef = olddef;
1381 }
1382 else
1383 {
1384 ntbfd = oldbfd;
1385 ntsec = oldsec;
1386 ntdef = olddef;
1387 tbfd = abfd;
1388 tsec = sec;
1389 tdef = newdef;
1390 }
1391
1392 if (tdef && ntdef)
1393 _bfd_error_handler
1394 /* xgettext:c-format */
1395 (_("%s: TLS definition in %pB section %pA "
1396 "mismatches non-TLS definition in %pB section %pA"),
1397 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
1398 else if (!tdef && !ntdef)
1399 _bfd_error_handler
1400 /* xgettext:c-format */
1401 (_("%s: TLS reference in %pB "
1402 "mismatches non-TLS reference in %pB"),
1403 h->root.root.string, tbfd, ntbfd);
1404 else if (tdef)
1405 _bfd_error_handler
1406 /* xgettext:c-format */
1407 (_("%s: TLS definition in %pB section %pA "
1408 "mismatches non-TLS reference in %pB"),
1409 h->root.root.string, tbfd, tsec, ntbfd);
1410 else
1411 _bfd_error_handler
1412 /* xgettext:c-format */
1413 (_("%s: TLS reference in %pB "
1414 "mismatches non-TLS definition in %pB section %pA"),
1415 h->root.root.string, tbfd, ntbfd, ntsec);
1416
1417 bfd_set_error (bfd_error_bad_value);
1418 return false;
1419 }
1420
1421 /* If the old symbol has non-default visibility, we ignore the new
1422 definition from a dynamic object. */
1423 if (newdyn
1424 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1425 && !bfd_is_und_section (sec))
1426 {
1427 *skip = true;
1428 /* Make sure this symbol is dynamic. */
1429 h->ref_dynamic = 1;
1430 hi->ref_dynamic = 1;
1431 /* A protected symbol has external availability. Make sure it is
1432 recorded as dynamic.
1433
1434 FIXME: Should we check type and size for protected symbol? */
1435 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1436 return bfd_elf_link_record_dynamic_symbol (info, h);
1437 else
1438 return true;
1439 }
1440 else if (!newdyn
1441 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1442 && h->def_dynamic)
1443 {
1444 /* If the new symbol with non-default visibility comes from a
1445 relocatable file and the old definition comes from a dynamic
1446 object, we remove the old definition. */
1447 if (hi->root.type == bfd_link_hash_indirect)
1448 {
1449 /* Handle the case where the old dynamic definition is
1450 default versioned. We need to copy the symbol info from
1451 the symbol with default version to the normal one if it
1452 was referenced before. */
1453 if (h->ref_regular)
1454 {
1455 hi->root.type = h->root.type;
1456 h->root.type = bfd_link_hash_indirect;
1457 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1458
1459 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1460 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1461 {
1462 /* If the new symbol is hidden or internal, completely undo
1463 any dynamic link state. */
1464 (*bed->elf_backend_hide_symbol) (info, h, true);
1465 h->forced_local = 0;
1466 h->ref_dynamic = 0;
1467 }
1468 else
1469 h->ref_dynamic = 1;
1470
1471 h->def_dynamic = 0;
1472 /* FIXME: Should we check type and size for protected symbol? */
1473 h->size = 0;
1474 h->type = 0;
1475
1476 h = hi;
1477 }
1478 else
1479 h = hi;
1480 }
1481
1482 /* If the old symbol was undefined before, then it will still be
1483 on the undefs list. If the new symbol is undefined or
1484 common, we can't make it bfd_link_hash_new here, because new
1485 undefined or common symbols will be added to the undefs list
1486 by _bfd_generic_link_add_one_symbol. Symbols may not be
1487 added twice to the undefs list. Also, if the new symbol is
1488 undefweak then we don't want to lose the strong undef. */
1489 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1490 {
1491 h->root.type = bfd_link_hash_undefined;
1492 h->root.u.undef.abfd = abfd;
1493 }
1494 else
1495 {
1496 h->root.type = bfd_link_hash_new;
1497 h->root.u.undef.abfd = NULL;
1498 }
1499
1500 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1501 {
1502 /* If the new symbol is hidden or internal, completely undo
1503 any dynamic link state. */
1504 (*bed->elf_backend_hide_symbol) (info, h, true);
1505 h->forced_local = 0;
1506 h->ref_dynamic = 0;
1507 }
1508 else
1509 h->ref_dynamic = 1;
1510 h->def_dynamic = 0;
1511 /* FIXME: Should we check type and size for protected symbol? */
1512 h->size = 0;
1513 h->type = 0;
1514 return true;
1515 }
1516
1517 /* If a new weak symbol definition comes from a regular file and the
1518 old symbol comes from a dynamic library, we treat the new one as
1519 strong. Similarly, an old weak symbol definition from a regular
1520 file is treated as strong when the new symbol comes from a dynamic
1521 library. Further, an old weak symbol from a dynamic library is
1522 treated as strong if the new symbol is from a dynamic library.
1523 This reflects the way glibc's ld.so works.
1524
1525 Also allow a weak symbol to override a linker script symbol
1526 defined by an early pass over the script. This is done so the
1527 linker knows the symbol is defined in an object file, for the
1528 DEFINED script function.
1529
1530 Do this before setting *type_change_ok or *size_change_ok so that
1531 we warn properly when dynamic library symbols are overridden. */
1532
1533 if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
1534 newweak = false;
1535 if (olddef && newdyn)
1536 oldweak = false;
1537
1538 /* Allow changes between different types of function symbol. */
1539 if (newfunc && oldfunc)
1540 *type_change_ok = true;
1541
1542 /* It's OK to change the type if either the existing symbol or the
1543 new symbol is weak. A type change is also OK if the old symbol
1544 is undefined and the new symbol is defined. */
1545
1546 if (oldweak
1547 || newweak
1548 || (newdef
1549 && h->root.type == bfd_link_hash_undefined))
1550 *type_change_ok = true;
1551
1552 /* It's OK to change the size if either the existing symbol or the
1553 new symbol is weak, or if the old symbol is undefined. */
1554
1555 if (*type_change_ok
1556 || h->root.type == bfd_link_hash_undefined)
1557 *size_change_ok = true;
1558
1559 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1560 symbol, respectively, appears to be a common symbol in a dynamic
1561 object. If a symbol appears in an uninitialized section, and is
1562 not weak, and is not a function, then it may be a common symbol
1563 which was resolved when the dynamic object was created. We want
1564 to treat such symbols specially, because they raise special
1565 considerations when setting the symbol size: if the symbol
1566 appears as a common symbol in a regular object, and the size in
1567 the regular object is larger, we must make sure that we use the
1568 larger size. This problematic case can always be avoided in C,
1569 but it must be handled correctly when using Fortran shared
1570 libraries.
1571
1572 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1573 likewise for OLDDYNCOMMON and OLDDEF.
1574
1575 Note that this test is just a heuristic, and that it is quite
1576 possible to have an uninitialized symbol in a shared object which
1577 is really a definition, rather than a common symbol. This could
1578 lead to some minor confusion when the symbol really is a common
1579 symbol in some regular object. However, I think it will be
1580 harmless. */
1581
1582 if (newdyn
1583 && newdef
1584 && !newweak
1585 && (sec->flags & SEC_ALLOC) != 0
1586 && (sec->flags & SEC_LOAD) == 0
1587 && sym->st_size > 0
1588 && !newfunc)
1589 newdyncommon = true;
1590 else
1591 newdyncommon = false;
1592
1593 if (olddyn
1594 && olddef
1595 && h->root.type == bfd_link_hash_defined
1596 && h->def_dynamic
1597 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1598 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1599 && h->size > 0
1600 && !oldfunc)
1601 olddyncommon = true;
1602 else
1603 olddyncommon = false;
1604
1605 /* We now know everything about the old and new symbols. We ask the
1606 backend to check if we can merge them. */
1607 if (bed->merge_symbol != NULL)
1608 {
1609 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1610 return false;
1611 sec = *psec;
1612 }
1613
1614 /* There are multiple definitions of a normal symbol. Skip the
1615 default symbol as well as definition from an IR object. */
1616 if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
1617 && !default_sym && h->def_regular
1618 && !(oldbfd != NULL
1619 && (oldbfd->flags & BFD_PLUGIN) != 0
1620 && (abfd->flags & BFD_PLUGIN) == 0))
1621 {
1622 /* Handle a multiple definition. */
1623 (*info->callbacks->multiple_definition) (info, &h->root,
1624 abfd, sec, *pvalue);
1625 *skip = true;
1626 return true;
1627 }
1628
1629 /* If both the old and the new symbols look like common symbols in a
1630 dynamic object, set the size of the symbol to the larger of the
1631 two. */
1632
1633 if (olddyncommon
1634 && newdyncommon
1635 && sym->st_size != h->size)
1636 {
1637 /* Since we think we have two common symbols, issue a multiple
1638 common warning if desired. Note that we only warn if the
1639 size is different. If the size is the same, we simply let
1640 the old symbol override the new one as normally happens with
1641 symbols defined in dynamic objects. */
1642
1643 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1644 bfd_link_hash_common, sym->st_size);
1645 if (sym->st_size > h->size)
1646 h->size = sym->st_size;
1647
1648 *size_change_ok = true;
1649 }
1650
1651 /* If we are looking at a dynamic object, and we have found a
1652 definition, we need to see if the symbol was already defined by
1653 some other object. If so, we want to use the existing
1654 definition, and we do not want to report a multiple symbol
1655 definition error; we do this by clobbering *PSEC to be
1656 bfd_und_section_ptr.
1657
1658 We treat a common symbol as a definition if the symbol in the
1659 shared library is a function, since common symbols always
1660 represent variables; this can cause confusion in principle, but
1661 any such confusion would seem to indicate an erroneous program or
1662 shared library. We also permit a common symbol in a regular
1663 object to override a weak symbol in a shared object. */
1664
1665 if (newdyn
1666 && newdef
1667 && (olddef
1668 || (h->root.type == bfd_link_hash_common
1669 && (newweak || newfunc))))
1670 {
1671 *override = abfd;
1672 newdef = false;
1673 newdyncommon = false;
1674
1675 *psec = sec = bfd_und_section_ptr;
1676 *size_change_ok = true;
1677
1678 /* If we get here when the old symbol is a common symbol, then
1679 we are explicitly letting it override a weak symbol or
1680 function in a dynamic object, and we don't want to warn about
1681 a type change. If the old symbol is a defined symbol, a type
1682 change warning may still be appropriate. */
1683
1684 if (h->root.type == bfd_link_hash_common)
1685 *type_change_ok = true;
1686 }
1687
1688 /* Handle the special case of an old common symbol merging with a
1689 new symbol which looks like a common symbol in a shared object.
1690 We change *PSEC and *PVALUE to make the new symbol look like a
1691 common symbol, and let _bfd_generic_link_add_one_symbol do the
1692 right thing. */
1693
1694 if (newdyncommon
1695 && h->root.type == bfd_link_hash_common)
1696 {
1697 *override = oldbfd;
1698 newdef = false;
1699 newdyncommon = false;
1700 *pvalue = sym->st_size;
1701 *psec = sec = bed->common_section (oldsec);
1702 *size_change_ok = true;
1703 }
1704
1705 /* Skip weak definitions of symbols that are already defined. */
1706 if (newdef && olddef && newweak)
1707 {
1708 /* Don't skip new non-IR weak syms. */
1709 if (!(oldbfd != NULL
1710 && (oldbfd->flags & BFD_PLUGIN) != 0
1711 && (abfd->flags & BFD_PLUGIN) == 0))
1712 {
1713 newdef = false;
1714 *skip = true;
1715 }
1716
1717 /* Merge st_other. If the symbol already has a dynamic index,
1718 but visibility says it should not be visible, turn it into a
1719 local symbol. */
1720 elf_merge_st_other (abfd, h, sym->st_other, sec, newdef, newdyn);
1721 if (h->dynindx != -1)
1722 switch (ELF_ST_VISIBILITY (h->other))
1723 {
1724 case STV_INTERNAL:
1725 case STV_HIDDEN:
1726 (*bed->elf_backend_hide_symbol) (info, h, true);
1727 break;
1728 }
1729 }
1730
1731 /* If the old symbol is from a dynamic object, and the new symbol is
1732 a definition which is not from a dynamic object, then the new
1733 symbol overrides the old symbol. Symbols from regular files
1734 always take precedence over symbols from dynamic objects, even if
1735 they are defined after the dynamic object in the link.
1736
1737 As above, we again permit a common symbol in a regular object to
1738 override a definition in a shared object if the shared object
1739 symbol is a function or is weak. */
1740
1741 flip = NULL;
1742 if (!newdyn
1743 && (newdef
1744 || (bfd_is_com_section (sec)
1745 && (oldweak || oldfunc)))
1746 && olddyn
1747 && olddef
1748 && h->def_dynamic)
1749 {
1750 /* Change the hash table entry to undefined, and let
1751 _bfd_generic_link_add_one_symbol do the right thing with the
1752 new definition. */
1753
1754 h->root.type = bfd_link_hash_undefined;
1755 h->root.u.undef.abfd = h->root.u.def.section->owner;
1756 *size_change_ok = true;
1757
1758 olddef = false;
1759 olddyncommon = false;
1760
1761 /* We again permit a type change when a common symbol may be
1762 overriding a function. */
1763
1764 if (bfd_is_com_section (sec))
1765 {
1766 if (oldfunc)
1767 {
1768 /* If a common symbol overrides a function, make sure
1769 that it isn't defined dynamically nor has type
1770 function. */
1771 h->def_dynamic = 0;
1772 h->type = STT_NOTYPE;
1773 }
1774 *type_change_ok = true;
1775 }
1776
1777 if (hi->root.type == bfd_link_hash_indirect)
1778 flip = hi;
1779 else
1780 /* This union may have been set to be non-NULL when this symbol
1781 was seen in a dynamic object. We must force the union to be
1782 NULL, so that it is correct for a regular symbol. */
1783 h->verinfo.vertree = NULL;
1784 }
1785
1786 /* Handle the special case of a new common symbol merging with an
1787 old symbol that looks like it might be a common symbol defined in
1788 a shared object. Note that we have already handled the case in
1789 which a new common symbol should simply override the definition
1790 in the shared library. */
1791
1792 if (! newdyn
1793 && bfd_is_com_section (sec)
1794 && olddyncommon)
1795 {
1796 /* It would be best if we could set the hash table entry to a
1797 common symbol, but we don't know what to use for the section
1798 or the alignment. */
1799 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1800 bfd_link_hash_common, sym->st_size);
1801
1802 /* If the presumed common symbol in the dynamic object is
1803 larger, pretend that the new symbol has its size. */
1804
1805 if (h->size > *pvalue)
1806 *pvalue = h->size;
1807
1808 /* We need to remember the alignment required by the symbol
1809 in the dynamic object. */
1810 BFD_ASSERT (pold_alignment);
1811 *pold_alignment = h->root.u.def.section->alignment_power;
1812
1813 olddef = false;
1814 olddyncommon = false;
1815
1816 h->root.type = bfd_link_hash_undefined;
1817 h->root.u.undef.abfd = h->root.u.def.section->owner;
1818
1819 *size_change_ok = true;
1820 *type_change_ok = true;
1821
1822 if (hi->root.type == bfd_link_hash_indirect)
1823 flip = hi;
1824 else
1825 h->verinfo.vertree = NULL;
1826 }
1827
1828 if (flip != NULL)
1829 {
1830 /* Handle the case where we had a versioned symbol in a dynamic
1831 library and now find a definition in a normal object. In this
1832 case, we make the versioned symbol point to the normal one. */
1833 flip->root.type = h->root.type;
1834 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1835 h->root.type = bfd_link_hash_indirect;
1836 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1837 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1838 if (h->def_dynamic)
1839 {
1840 h->def_dynamic = 0;
1841 flip->ref_dynamic = 1;
1842 }
1843 }
1844
1845 return true;
1846 }
1847
1848 /* This function is called to create an indirect symbol from the
1849 default for the symbol with the default version if needed. The
1850 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1851 set DYNSYM if the new indirect symbol is dynamic. */
1852
1853 static bool
1854 _bfd_elf_add_default_symbol (bfd *abfd,
1855 struct bfd_link_info *info,
1856 struct elf_link_hash_entry *h,
1857 const char *name,
1858 Elf_Internal_Sym *sym,
1859 asection *sec,
1860 bfd_vma value,
1861 bfd **poldbfd,
1862 bool *dynsym)
1863 {
1864 bool type_change_ok;
1865 bool size_change_ok;
1866 bool skip;
1867 char *shortname;
1868 struct elf_link_hash_entry *hi;
1869 struct bfd_link_hash_entry *bh;
1870 const struct elf_backend_data *bed;
1871 bool collect;
1872 bool dynamic;
1873 bfd *override;
1874 char *p;
1875 size_t len, shortlen;
1876 asection *tmp_sec;
1877 bool matched;
1878
1879 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1880 return true;
1881
1882 /* If this symbol has a version, and it is the default version, we
1883 create an indirect symbol from the default name to the fully
1884 decorated name. This will cause external references which do not
1885 specify a version to be bound to this version of the symbol. */
1886 p = strchr (name, ELF_VER_CHR);
1887 if (h->versioned == unknown)
1888 {
1889 if (p == NULL)
1890 {
1891 h->versioned = unversioned;
1892 return true;
1893 }
1894 else
1895 {
1896 if (p[1] != ELF_VER_CHR)
1897 {
1898 h->versioned = versioned_hidden;
1899 return true;
1900 }
1901 else
1902 h->versioned = versioned;
1903 }
1904 }
1905 else
1906 {
1907 /* PR ld/19073: We may see an unversioned definition after the
1908 default version. */
1909 if (p == NULL)
1910 return true;
1911 }
1912
1913 bed = get_elf_backend_data (abfd);
1914 collect = bed->collect;
1915 dynamic = (abfd->flags & DYNAMIC) != 0;
1916
1917 shortlen = p - name;
1918 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1919 if (shortname == NULL)
1920 return false;
1921 memcpy (shortname, name, shortlen);
1922 shortname[shortlen] = '\0';
1923
1924 /* We are going to create a new symbol. Merge it with any existing
1925 symbol with this name. For the purposes of the merge, act as
1926 though we were defining the symbol we just defined, although we
1927 actually going to define an indirect symbol. */
1928 type_change_ok = false;
1929 size_change_ok = false;
1930 matched = true;
1931 tmp_sec = sec;
1932 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1933 &hi, poldbfd, NULL, NULL, &skip, &override,
1934 &type_change_ok, &size_change_ok, &matched))
1935 return false;
1936
1937 if (skip)
1938 goto nondefault;
1939
1940 if (hi->def_regular || ELF_COMMON_DEF_P (hi))
1941 {
1942 /* If the undecorated symbol will have a version added by a
1943 script different to H, then don't indirect to/from the
1944 undecorated symbol. This isn't ideal because we may not yet
1945 have seen symbol versions, if given by a script on the
1946 command line rather than via --version-script. */
1947 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1948 {
1949 bool hide;
1950
1951 hi->verinfo.vertree
1952 = bfd_find_version_for_sym (info->version_info,
1953 hi->root.root.string, &hide);
1954 if (hi->verinfo.vertree != NULL && hide)
1955 {
1956 (*bed->elf_backend_hide_symbol) (info, hi, true);
1957 goto nondefault;
1958 }
1959 }
1960 if (hi->verinfo.vertree != NULL
1961 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1962 goto nondefault;
1963 }
1964
1965 if (! override)
1966 {
1967 /* Add the default symbol if not performing a relocatable link. */
1968 if (! bfd_link_relocatable (info))
1969 {
1970 bh = &hi->root;
1971 if (bh->type == bfd_link_hash_defined
1972 && bh->u.def.section->owner != NULL
1973 && (bh->u.def.section->owner->flags & BFD_PLUGIN) != 0)
1974 {
1975 /* Mark the previous definition from IR object as
1976 undefined so that the generic linker will override
1977 it. */
1978 bh->type = bfd_link_hash_undefined;
1979 bh->u.undef.abfd = bh->u.def.section->owner;
1980 }
1981 if (! (_bfd_generic_link_add_one_symbol
1982 (info, abfd, shortname, BSF_INDIRECT,
1983 bfd_ind_section_ptr,
1984 0, name, false, collect, &bh)))
1985 return false;
1986 hi = (struct elf_link_hash_entry *) bh;
1987 }
1988 }
1989 else
1990 {
1991 /* In this case the symbol named SHORTNAME is overriding the
1992 indirect symbol we want to add. We were planning on making
1993 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1994 is the name without a version. NAME is the fully versioned
1995 name, and it is the default version.
1996
1997 Overriding means that we already saw a definition for the
1998 symbol SHORTNAME in a regular object, and it is overriding
1999 the symbol defined in the dynamic object.
2000
2001 When this happens, we actually want to change NAME, the
2002 symbol we just added, to refer to SHORTNAME. This will cause
2003 references to NAME in the shared object to become references
2004 to SHORTNAME in the regular object. This is what we expect
2005 when we override a function in a shared object: that the
2006 references in the shared object will be mapped to the
2007 definition in the regular object. */
2008
2009 while (hi->root.type == bfd_link_hash_indirect
2010 || hi->root.type == bfd_link_hash_warning)
2011 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2012
2013 h->root.type = bfd_link_hash_indirect;
2014 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
2015 if (h->def_dynamic)
2016 {
2017 h->def_dynamic = 0;
2018 hi->ref_dynamic = 1;
2019 if (hi->ref_regular
2020 || hi->def_regular)
2021 {
2022 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
2023 return false;
2024 }
2025 }
2026
2027 /* Now set HI to H, so that the following code will set the
2028 other fields correctly. */
2029 hi = h;
2030 }
2031
2032 /* Check if HI is a warning symbol. */
2033 if (hi->root.type == bfd_link_hash_warning)
2034 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2035
2036 /* If there is a duplicate definition somewhere, then HI may not
2037 point to an indirect symbol. We will have reported an error to
2038 the user in that case. */
2039
2040 if (hi->root.type == bfd_link_hash_indirect)
2041 {
2042 struct elf_link_hash_entry *ht;
2043
2044 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
2045 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
2046
2047 /* If we first saw a reference to SHORTNAME with non-default
2048 visibility, merge that visibility to the @@VER symbol. */
2049 elf_merge_st_other (abfd, ht, hi->other, sec, true, dynamic);
2050
2051 /* A reference to the SHORTNAME symbol from a dynamic library
2052 will be satisfied by the versioned symbol at runtime. In
2053 effect, we have a reference to the versioned symbol. */
2054 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2055 hi->dynamic_def |= ht->dynamic_def;
2056
2057 /* See if the new flags lead us to realize that the symbol must
2058 be dynamic. */
2059 if (! *dynsym)
2060 {
2061 if (! dynamic)
2062 {
2063 if (! bfd_link_executable (info)
2064 || hi->def_dynamic
2065 || hi->ref_dynamic)
2066 *dynsym = true;
2067 }
2068 else
2069 {
2070 if (hi->ref_regular)
2071 *dynsym = true;
2072 }
2073 }
2074 }
2075
2076 /* We also need to define an indirection from the nondefault version
2077 of the symbol. */
2078
2079 nondefault:
2080 len = strlen (name);
2081 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
2082 if (shortname == NULL)
2083 return false;
2084 memcpy (shortname, name, shortlen);
2085 memcpy (shortname + shortlen, p + 1, len - shortlen);
2086
2087 /* Once again, merge with any existing symbol. */
2088 type_change_ok = false;
2089 size_change_ok = false;
2090 tmp_sec = sec;
2091 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
2092 &hi, poldbfd, NULL, NULL, &skip, &override,
2093 &type_change_ok, &size_change_ok, &matched))
2094 return false;
2095
2096 if (skip)
2097 {
2098 if (!dynamic
2099 && h->root.type == bfd_link_hash_defweak
2100 && hi->root.type == bfd_link_hash_defined)
2101 {
2102 /* We are handling a weak sym@@ver and attempting to define
2103 a weak sym@ver, but _bfd_elf_merge_symbol said to skip the
2104 new weak sym@ver because there is already a strong sym@ver.
2105 However, sym@ver and sym@@ver are really the same symbol.
2106 The existing strong sym@ver ought to override sym@@ver. */
2107 h->root.type = bfd_link_hash_defined;
2108 h->root.u.def.section = hi->root.u.def.section;
2109 h->root.u.def.value = hi->root.u.def.value;
2110 hi->root.type = bfd_link_hash_indirect;
2111 hi->root.u.i.link = &h->root;
2112 }
2113 else
2114 return true;
2115 }
2116 else if (override)
2117 {
2118 /* Here SHORTNAME is a versioned name, so we don't expect to see
2119 the type of override we do in the case above unless it is
2120 overridden by a versioned definition. */
2121 if (hi->root.type != bfd_link_hash_defined
2122 && hi->root.type != bfd_link_hash_defweak)
2123 _bfd_error_handler
2124 /* xgettext:c-format */
2125 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
2126 abfd, shortname);
2127 return true;
2128 }
2129 else
2130 {
2131 bh = &hi->root;
2132 if (! (_bfd_generic_link_add_one_symbol
2133 (info, abfd, shortname, BSF_INDIRECT,
2134 bfd_ind_section_ptr, 0, name, false, collect, &bh)))
2135 return false;
2136 hi = (struct elf_link_hash_entry *) bh;
2137 }
2138
2139 /* If there is a duplicate definition somewhere, then HI may not
2140 point to an indirect symbol. We will have reported an error
2141 to the user in that case. */
2142 if (hi->root.type == bfd_link_hash_indirect)
2143 {
2144 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
2145 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2146 hi->dynamic_def |= h->dynamic_def;
2147
2148 /* If we first saw a reference to @VER symbol with
2149 non-default visibility, merge that visibility to the
2150 @@VER symbol. */
2151 elf_merge_st_other (abfd, h, hi->other, sec, true, dynamic);
2152
2153 /* See if the new flags lead us to realize that the symbol
2154 must be dynamic. */
2155 if (! *dynsym)
2156 {
2157 if (! dynamic)
2158 {
2159 if (! bfd_link_executable (info)
2160 || hi->ref_dynamic)
2161 *dynsym = true;
2162 }
2163 else
2164 {
2165 if (hi->ref_regular)
2166 *dynsym = true;
2167 }
2168 }
2169 }
2170
2171 return true;
2172 }
2173 \f
2174 /* This routine is used to export all defined symbols into the dynamic
2175 symbol table. It is called via elf_link_hash_traverse. */
2176
2177 static bool
2178 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
2179 {
2180 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2181
2182 /* Ignore indirect symbols. These are added by the versioning code. */
2183 if (h->root.type == bfd_link_hash_indirect)
2184 return true;
2185
2186 /* Ignore this if we won't export it. */
2187 if (!eif->info->export_dynamic && !h->dynamic)
2188 return true;
2189
2190 if (h->dynindx == -1
2191 && (h->def_regular || h->ref_regular)
2192 && ! bfd_hide_sym_by_version (eif->info->version_info,
2193 h->root.root.string))
2194 {
2195 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2196 {
2197 eif->failed = true;
2198 return false;
2199 }
2200 }
2201
2202 return true;
2203 }
2204 \f
2205 /* Look through the symbols which are defined in other shared
2206 libraries and referenced here. Update the list of version
2207 dependencies. This will be put into the .gnu.version_r section.
2208 This function is called via elf_link_hash_traverse. */
2209
2210 static bool
2211 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2212 void *data)
2213 {
2214 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2215 Elf_Internal_Verneed *t;
2216 Elf_Internal_Vernaux *a;
2217 size_t amt;
2218
2219 /* We only care about symbols defined in shared objects with version
2220 information. */
2221 if (!h->def_dynamic
2222 || h->def_regular
2223 || h->dynindx == -1
2224 || h->verinfo.verdef == NULL
2225 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2226 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2227 return true;
2228
2229 /* See if we already know about this version. */
2230 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2231 t != NULL;
2232 t = t->vn_nextref)
2233 {
2234 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2235 continue;
2236
2237 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2238 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2239 return true;
2240
2241 break;
2242 }
2243
2244 /* This is a new version. Add it to tree we are building. */
2245
2246 if (t == NULL)
2247 {
2248 amt = sizeof *t;
2249 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2250 if (t == NULL)
2251 {
2252 rinfo->failed = true;
2253 return false;
2254 }
2255
2256 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2257 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2258 elf_tdata (rinfo->info->output_bfd)->verref = t;
2259 }
2260
2261 amt = sizeof *a;
2262 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2263 if (a == NULL)
2264 {
2265 rinfo->failed = true;
2266 return false;
2267 }
2268
2269 /* Note that we are copying a string pointer here, and testing it
2270 above. If bfd_elf_string_from_elf_section is ever changed to
2271 discard the string data when low in memory, this will have to be
2272 fixed. */
2273 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2274
2275 a->vna_flags = h->verinfo.verdef->vd_flags;
2276 a->vna_nextptr = t->vn_auxptr;
2277
2278 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2279 ++rinfo->vers;
2280
2281 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2282
2283 t->vn_auxptr = a;
2284
2285 return true;
2286 }
2287
2288 /* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2289 hidden. Set *T_P to NULL if there is no match. */
2290
2291 static bool
2292 _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
2293 struct elf_link_hash_entry *h,
2294 const char *version_p,
2295 struct bfd_elf_version_tree **t_p,
2296 bool *hide)
2297 {
2298 struct bfd_elf_version_tree *t;
2299
2300 /* Look for the version. If we find it, it is no longer weak. */
2301 for (t = info->version_info; t != NULL; t = t->next)
2302 {
2303 if (strcmp (t->name, version_p) == 0)
2304 {
2305 size_t len;
2306 char *alc;
2307 struct bfd_elf_version_expr *d;
2308
2309 len = version_p - h->root.root.string;
2310 alc = (char *) bfd_malloc (len);
2311 if (alc == NULL)
2312 return false;
2313 memcpy (alc, h->root.root.string, len - 1);
2314 alc[len - 1] = '\0';
2315 if (alc[len - 2] == ELF_VER_CHR)
2316 alc[len - 2] = '\0';
2317
2318 h->verinfo.vertree = t;
2319 t->used = true;
2320 d = NULL;
2321
2322 if (t->globals.list != NULL)
2323 d = (*t->match) (&t->globals, NULL, alc);
2324
2325 /* See if there is anything to force this symbol to
2326 local scope. */
2327 if (d == NULL && t->locals.list != NULL)
2328 {
2329 d = (*t->match) (&t->locals, NULL, alc);
2330 if (d != NULL
2331 && h->dynindx != -1
2332 && ! info->export_dynamic)
2333 *hide = true;
2334 }
2335
2336 free (alc);
2337 break;
2338 }
2339 }
2340
2341 *t_p = t;
2342
2343 return true;
2344 }
2345
2346 /* Return TRUE if the symbol H is hidden by version script. */
2347
2348 bool
2349 _bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
2350 struct elf_link_hash_entry *h)
2351 {
2352 const char *p;
2353 bool hide = false;
2354 const struct elf_backend_data *bed
2355 = get_elf_backend_data (info->output_bfd);
2356
2357 /* Version script only hides symbols defined in regular objects. */
2358 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2359 return true;
2360
2361 p = strchr (h->root.root.string, ELF_VER_CHR);
2362 if (p != NULL && h->verinfo.vertree == NULL)
2363 {
2364 struct bfd_elf_version_tree *t;
2365
2366 ++p;
2367 if (*p == ELF_VER_CHR)
2368 ++p;
2369
2370 if (*p != '\0'
2371 && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
2372 && hide)
2373 {
2374 if (hide)
2375 (*bed->elf_backend_hide_symbol) (info, h, true);
2376 return true;
2377 }
2378 }
2379
2380 /* If we don't have a version for this symbol, see if we can find
2381 something. */
2382 if (h->verinfo.vertree == NULL && info->version_info != NULL)
2383 {
2384 h->verinfo.vertree
2385 = bfd_find_version_for_sym (info->version_info,
2386 h->root.root.string, &hide);
2387 if (h->verinfo.vertree != NULL && hide)
2388 {
2389 (*bed->elf_backend_hide_symbol) (info, h, true);
2390 return true;
2391 }
2392 }
2393
2394 return false;
2395 }
2396
2397 /* Figure out appropriate versions for all the symbols. We may not
2398 have the version number script until we have read all of the input
2399 files, so until that point we don't know which symbols should be
2400 local. This function is called via elf_link_hash_traverse. */
2401
2402 static bool
2403 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2404 {
2405 struct elf_info_failed *sinfo;
2406 struct bfd_link_info *info;
2407 const struct elf_backend_data *bed;
2408 struct elf_info_failed eif;
2409 char *p;
2410 bool hide;
2411
2412 sinfo = (struct elf_info_failed *) data;
2413 info = sinfo->info;
2414
2415 /* Fix the symbol flags. */
2416 eif.failed = false;
2417 eif.info = info;
2418 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2419 {
2420 if (eif.failed)
2421 sinfo->failed = true;
2422 return false;
2423 }
2424
2425 bed = get_elf_backend_data (info->output_bfd);
2426
2427 /* We only need version numbers for symbols defined in regular
2428 objects. */
2429 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2430 {
2431 /* Hide symbols defined in discarded input sections. */
2432 if ((h->root.type == bfd_link_hash_defined
2433 || h->root.type == bfd_link_hash_defweak)
2434 && discarded_section (h->root.u.def.section))
2435 (*bed->elf_backend_hide_symbol) (info, h, true);
2436 return true;
2437 }
2438
2439 hide = false;
2440 p = strchr (h->root.root.string, ELF_VER_CHR);
2441 if (p != NULL && h->verinfo.vertree == NULL)
2442 {
2443 struct bfd_elf_version_tree *t;
2444
2445 ++p;
2446 if (*p == ELF_VER_CHR)
2447 ++p;
2448
2449 /* If there is no version string, we can just return out. */
2450 if (*p == '\0')
2451 return true;
2452
2453 if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
2454 {
2455 sinfo->failed = true;
2456 return false;
2457 }
2458
2459 if (hide)
2460 (*bed->elf_backend_hide_symbol) (info, h, true);
2461
2462 /* If we are building an application, we need to create a
2463 version node for this version. */
2464 if (t == NULL && bfd_link_executable (info))
2465 {
2466 struct bfd_elf_version_tree **pp;
2467 int version_index;
2468
2469 /* If we aren't going to export this symbol, we don't need
2470 to worry about it. */
2471 if (h->dynindx == -1)
2472 return true;
2473
2474 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2475 sizeof *t);
2476 if (t == NULL)
2477 {
2478 sinfo->failed = true;
2479 return false;
2480 }
2481
2482 t->name = p;
2483 t->name_indx = (unsigned int) -1;
2484 t->used = true;
2485
2486 version_index = 1;
2487 /* Don't count anonymous version tag. */
2488 if (sinfo->info->version_info != NULL
2489 && sinfo->info->version_info->vernum == 0)
2490 version_index = 0;
2491 for (pp = &sinfo->info->version_info;
2492 *pp != NULL;
2493 pp = &(*pp)->next)
2494 ++version_index;
2495 t->vernum = version_index;
2496
2497 *pp = t;
2498
2499 h->verinfo.vertree = t;
2500 }
2501 else if (t == NULL)
2502 {
2503 /* We could not find the version for a symbol when
2504 generating a shared archive. Return an error. */
2505 _bfd_error_handler
2506 /* xgettext:c-format */
2507 (_("%pB: version node not found for symbol %s"),
2508 info->output_bfd, h->root.root.string);
2509 bfd_set_error (bfd_error_bad_value);
2510 sinfo->failed = true;
2511 return false;
2512 }
2513 }
2514
2515 /* If we don't have a version for this symbol, see if we can find
2516 something. */
2517 if (!hide
2518 && h->verinfo.vertree == NULL
2519 && sinfo->info->version_info != NULL)
2520 {
2521 h->verinfo.vertree
2522 = bfd_find_version_for_sym (sinfo->info->version_info,
2523 h->root.root.string, &hide);
2524 if (h->verinfo.vertree != NULL && hide)
2525 (*bed->elf_backend_hide_symbol) (info, h, true);
2526 }
2527
2528 return true;
2529 }
2530 \f
2531 /* Read and swap the relocs from the section indicated by SHDR. This
2532 may be either a REL or a RELA section. The relocations are
2533 translated into RELA relocations and stored in INTERNAL_RELOCS,
2534 which should have already been allocated to contain enough space.
2535 The EXTERNAL_RELOCS are a buffer where the external form of the
2536 relocations should be stored.
2537
2538 Returns FALSE if something goes wrong. */
2539
2540 static bool
2541 elf_link_read_relocs_from_section (bfd *abfd,
2542 asection *sec,
2543 Elf_Internal_Shdr *shdr,
2544 void *external_relocs,
2545 Elf_Internal_Rela *internal_relocs)
2546 {
2547 const struct elf_backend_data *bed;
2548 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2549 const bfd_byte *erela;
2550 const bfd_byte *erelaend;
2551 Elf_Internal_Rela *irela;
2552 Elf_Internal_Shdr *symtab_hdr;
2553 size_t nsyms;
2554
2555 /* Position ourselves at the start of the section. */
2556 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2557 return false;
2558
2559 /* Read the relocations. */
2560 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2561 return false;
2562
2563 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2564 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2565
2566 bed = get_elf_backend_data (abfd);
2567
2568 /* Convert the external relocations to the internal format. */
2569 if (shdr->sh_entsize == bed->s->sizeof_rel)
2570 swap_in = bed->s->swap_reloc_in;
2571 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2572 swap_in = bed->s->swap_reloca_in;
2573 else
2574 {
2575 bfd_set_error (bfd_error_wrong_format);
2576 return false;
2577 }
2578
2579 erela = (const bfd_byte *) external_relocs;
2580 /* Setting erelaend like this and comparing with <= handles case of
2581 a fuzzed object with sh_size not a multiple of sh_entsize. */
2582 erelaend = erela + shdr->sh_size - shdr->sh_entsize;
2583 irela = internal_relocs;
2584 while (erela <= erelaend)
2585 {
2586 bfd_vma r_symndx;
2587
2588 (*swap_in) (abfd, erela, irela);
2589 r_symndx = ELF32_R_SYM (irela->r_info);
2590 if (bed->s->arch_size == 64)
2591 r_symndx >>= 24;
2592 if (nsyms > 0)
2593 {
2594 if ((size_t) r_symndx >= nsyms)
2595 {
2596 _bfd_error_handler
2597 /* xgettext:c-format */
2598 (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
2599 " for offset %#" PRIx64 " in section `%pA'"),
2600 abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
2601 (uint64_t) irela->r_offset, sec);
2602 bfd_set_error (bfd_error_bad_value);
2603 return false;
2604 }
2605 }
2606 else if (r_symndx != STN_UNDEF)
2607 {
2608 _bfd_error_handler
2609 /* xgettext:c-format */
2610 (_("%pB: non-zero symbol index (%#" PRIx64 ")"
2611 " for offset %#" PRIx64 " in section `%pA'"
2612 " when the object file has no symbol table"),
2613 abfd, (uint64_t) r_symndx,
2614 (uint64_t) irela->r_offset, sec);
2615 bfd_set_error (bfd_error_bad_value);
2616 return false;
2617 }
2618 irela += bed->s->int_rels_per_ext_rel;
2619 erela += shdr->sh_entsize;
2620 }
2621
2622 return true;
2623 }
2624
2625 /* Read and swap the relocs for a section O. They may have been
2626 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2627 not NULL, they are used as buffers to read into. They are known to
2628 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2629 the return value is allocated using either malloc or bfd_alloc,
2630 according to the KEEP_MEMORY argument. If O has two relocation
2631 sections (both REL and RELA relocations), then the REL_HDR
2632 relocations will appear first in INTERNAL_RELOCS, followed by the
2633 RELA_HDR relocations. If INFO isn't NULL and KEEP_MEMORY is true,
2634 update cache_size. */
2635
2636 Elf_Internal_Rela *
2637 _bfd_elf_link_info_read_relocs (bfd *abfd,
2638 struct bfd_link_info *info,
2639 asection *o,
2640 void *external_relocs,
2641 Elf_Internal_Rela *internal_relocs,
2642 bool keep_memory)
2643 {
2644 void *alloc1 = NULL;
2645 Elf_Internal_Rela *alloc2 = NULL;
2646 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2647 struct bfd_elf_section_data *esdo = elf_section_data (o);
2648 Elf_Internal_Rela *internal_rela_relocs;
2649
2650 if (esdo->relocs != NULL)
2651 return esdo->relocs;
2652
2653 if (o->reloc_count == 0)
2654 return NULL;
2655
2656 if (internal_relocs == NULL)
2657 {
2658 bfd_size_type size;
2659
2660 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
2661 if (keep_memory)
2662 {
2663 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2664 if (info)
2665 info->cache_size += size;
2666 }
2667 else
2668 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2669 if (internal_relocs == NULL)
2670 goto error_return;
2671 }
2672
2673 if (external_relocs == NULL)
2674 {
2675 bfd_size_type size = 0;
2676
2677 if (esdo->rel.hdr)
2678 size += esdo->rel.hdr->sh_size;
2679 if (esdo->rela.hdr)
2680 size += esdo->rela.hdr->sh_size;
2681
2682 alloc1 = bfd_malloc (size);
2683 if (alloc1 == NULL)
2684 goto error_return;
2685 external_relocs = alloc1;
2686 }
2687
2688 internal_rela_relocs = internal_relocs;
2689 if (esdo->rel.hdr)
2690 {
2691 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2692 external_relocs,
2693 internal_relocs))
2694 goto error_return;
2695 external_relocs = (((bfd_byte *) external_relocs)
2696 + esdo->rel.hdr->sh_size);
2697 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2698 * bed->s->int_rels_per_ext_rel);
2699 }
2700
2701 if (esdo->rela.hdr
2702 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2703 external_relocs,
2704 internal_rela_relocs)))
2705 goto error_return;
2706
2707 /* Cache the results for next time, if we can. */
2708 if (keep_memory)
2709 esdo->relocs = internal_relocs;
2710
2711 free (alloc1);
2712
2713 /* Don't free alloc2, since if it was allocated we are passing it
2714 back (under the name of internal_relocs). */
2715
2716 return internal_relocs;
2717
2718 error_return:
2719 free (alloc1);
2720 if (alloc2 != NULL)
2721 {
2722 if (keep_memory)
2723 bfd_release (abfd, alloc2);
2724 else
2725 free (alloc2);
2726 }
2727 return NULL;
2728 }
2729
2730 /* This is similar to _bfd_elf_link_info_read_relocs, except for that
2731 NULL is passed to _bfd_elf_link_info_read_relocs for pointer to
2732 struct bfd_link_info. */
2733
2734 Elf_Internal_Rela *
2735 _bfd_elf_link_read_relocs (bfd *abfd,
2736 asection *o,
2737 void *external_relocs,
2738 Elf_Internal_Rela *internal_relocs,
2739 bool keep_memory)
2740 {
2741 return _bfd_elf_link_info_read_relocs (abfd, NULL, o, external_relocs,
2742 internal_relocs, keep_memory);
2743
2744 }
2745
2746 /* Compute the size of, and allocate space for, REL_HDR which is the
2747 section header for a section containing relocations for O. */
2748
2749 static bool
2750 _bfd_elf_link_size_reloc_section (bfd *abfd,
2751 struct bfd_elf_section_reloc_data *reldata)
2752 {
2753 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2754
2755 /* That allows us to calculate the size of the section. */
2756 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2757
2758 /* The contents field must last into write_object_contents, so we
2759 allocate it with bfd_alloc rather than malloc. Also since we
2760 cannot be sure that the contents will actually be filled in,
2761 we zero the allocated space. */
2762 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2763 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2764 return false;
2765
2766 if (reldata->hashes == NULL && reldata->count)
2767 {
2768 struct elf_link_hash_entry **p;
2769
2770 p = ((struct elf_link_hash_entry **)
2771 bfd_zmalloc (reldata->count * sizeof (*p)));
2772 if (p == NULL)
2773 return false;
2774
2775 reldata->hashes = p;
2776 }
2777
2778 return true;
2779 }
2780
2781 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2782 originated from the section given by INPUT_REL_HDR) to the
2783 OUTPUT_BFD. */
2784
2785 bool
2786 _bfd_elf_link_output_relocs (bfd *output_bfd,
2787 asection *input_section,
2788 Elf_Internal_Shdr *input_rel_hdr,
2789 Elf_Internal_Rela *internal_relocs,
2790 struct elf_link_hash_entry **rel_hash
2791 ATTRIBUTE_UNUSED)
2792 {
2793 Elf_Internal_Rela *irela;
2794 Elf_Internal_Rela *irelaend;
2795 bfd_byte *erel;
2796 struct bfd_elf_section_reloc_data *output_reldata;
2797 asection *output_section;
2798 const struct elf_backend_data *bed;
2799 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2800 struct bfd_elf_section_data *esdo;
2801
2802 output_section = input_section->output_section;
2803
2804 bed = get_elf_backend_data (output_bfd);
2805 esdo = elf_section_data (output_section);
2806 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2807 {
2808 output_reldata = &esdo->rel;
2809 swap_out = bed->s->swap_reloc_out;
2810 }
2811 else if (esdo->rela.hdr
2812 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2813 {
2814 output_reldata = &esdo->rela;
2815 swap_out = bed->s->swap_reloca_out;
2816 }
2817 else
2818 {
2819 _bfd_error_handler
2820 /* xgettext:c-format */
2821 (_("%pB: relocation size mismatch in %pB section %pA"),
2822 output_bfd, input_section->owner, input_section);
2823 bfd_set_error (bfd_error_wrong_format);
2824 return false;
2825 }
2826
2827 erel = output_reldata->hdr->contents;
2828 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2829 irela = internal_relocs;
2830 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2831 * bed->s->int_rels_per_ext_rel);
2832 while (irela < irelaend)
2833 {
2834 (*swap_out) (output_bfd, irela, erel);
2835 irela += bed->s->int_rels_per_ext_rel;
2836 erel += input_rel_hdr->sh_entsize;
2837 }
2838
2839 /* Bump the counter, so that we know where to add the next set of
2840 relocations. */
2841 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2842
2843 return true;
2844 }
2845 \f
2846 /* Make weak undefined symbols in PIE dynamic. */
2847
2848 bool
2849 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2850 struct elf_link_hash_entry *h)
2851 {
2852 if (bfd_link_pie (info)
2853 && h->dynindx == -1
2854 && h->root.type == bfd_link_hash_undefweak)
2855 return bfd_elf_link_record_dynamic_symbol (info, h);
2856
2857 return true;
2858 }
2859
2860 /* Fix up the flags for a symbol. This handles various cases which
2861 can only be fixed after all the input files are seen. This is
2862 currently called by both adjust_dynamic_symbol and
2863 assign_sym_version, which is unnecessary but perhaps more robust in
2864 the face of future changes. */
2865
2866 static bool
2867 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2868 struct elf_info_failed *eif)
2869 {
2870 const struct elf_backend_data *bed;
2871
2872 /* If this symbol was mentioned in a non-ELF file, try to set
2873 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2874 permit a non-ELF file to correctly refer to a symbol defined in
2875 an ELF dynamic object. */
2876 if (h->non_elf)
2877 {
2878 while (h->root.type == bfd_link_hash_indirect)
2879 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2880
2881 if (h->root.type != bfd_link_hash_defined
2882 && h->root.type != bfd_link_hash_defweak)
2883 {
2884 h->ref_regular = 1;
2885 h->ref_regular_nonweak = 1;
2886 }
2887 else
2888 {
2889 if (h->root.u.def.section->owner != NULL
2890 && (bfd_get_flavour (h->root.u.def.section->owner)
2891 == bfd_target_elf_flavour))
2892 {
2893 h->ref_regular = 1;
2894 h->ref_regular_nonweak = 1;
2895 }
2896 else
2897 h->def_regular = 1;
2898 }
2899
2900 if (h->dynindx == -1
2901 && (h->def_dynamic
2902 || h->ref_dynamic))
2903 {
2904 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2905 {
2906 eif->failed = true;
2907 return false;
2908 }
2909 }
2910 }
2911 else
2912 {
2913 /* Unfortunately, NON_ELF is only correct if the symbol
2914 was first seen in a non-ELF file. Fortunately, if the symbol
2915 was first seen in an ELF file, we're probably OK unless the
2916 symbol was defined in a non-ELF file. Catch that case here.
2917 FIXME: We're still in trouble if the symbol was first seen in
2918 a dynamic object, and then later in a non-ELF regular object. */
2919 if ((h->root.type == bfd_link_hash_defined
2920 || h->root.type == bfd_link_hash_defweak)
2921 && !h->def_regular
2922 && (h->root.u.def.section->owner != NULL
2923 ? (bfd_get_flavour (h->root.u.def.section->owner)
2924 != bfd_target_elf_flavour)
2925 : (bfd_is_abs_section (h->root.u.def.section)
2926 && !h->def_dynamic)))
2927 h->def_regular = 1;
2928 }
2929
2930 /* Backend specific symbol fixup. */
2931 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2932 if (bed->elf_backend_fixup_symbol
2933 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2934 return false;
2935
2936 /* If this is a final link, and the symbol was defined as a common
2937 symbol in a regular object file, and there was no definition in
2938 any dynamic object, then the linker will have allocated space for
2939 the symbol in a common section but the DEF_REGULAR
2940 flag will not have been set. */
2941 if (h->root.type == bfd_link_hash_defined
2942 && !h->def_regular
2943 && h->ref_regular
2944 && !h->def_dynamic
2945 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
2946 h->def_regular = 1;
2947
2948 /* Symbols defined in discarded sections shouldn't be dynamic. */
2949 if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
2950 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
2951
2952 /* If a weak undefined symbol has non-default visibility, we also
2953 hide it from the dynamic linker. */
2954 else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2955 && h->root.type == bfd_link_hash_undefweak)
2956 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
2957
2958 /* A hidden versioned symbol in executable should be forced local if
2959 it is is locally defined, not referenced by shared library and not
2960 exported. */
2961 else if (bfd_link_executable (eif->info)
2962 && h->versioned == versioned_hidden
2963 && !eif->info->export_dynamic
2964 && !h->dynamic
2965 && !h->ref_dynamic
2966 && h->def_regular)
2967 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
2968
2969 /* If -Bsymbolic was used (which means to bind references to global
2970 symbols to the definition within the shared object), and this
2971 symbol was defined in a regular object, then it actually doesn't
2972 need a PLT entry. Likewise, if the symbol has non-default
2973 visibility. If the symbol has hidden or internal visibility, we
2974 will force it local. */
2975 else if (h->needs_plt
2976 && bfd_link_pic (eif->info)
2977 && is_elf_hash_table (eif->info->hash)
2978 && (SYMBOLIC_BIND (eif->info, h)
2979 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2980 && h->def_regular)
2981 {
2982 bool force_local;
2983
2984 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2985 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2986 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2987 }
2988
2989 /* If this is a weak defined symbol in a dynamic object, and we know
2990 the real definition in the dynamic object, copy interesting flags
2991 over to the real definition. */
2992 if (h->is_weakalias)
2993 {
2994 struct elf_link_hash_entry *def = weakdef (h);
2995
2996 /* If the real definition is defined by a regular object file,
2997 don't do anything special. See the longer description in
2998 _bfd_elf_adjust_dynamic_symbol, below. If the def is not
2999 bfd_link_hash_defined as it was when put on the alias list
3000 then it must have originally been a versioned symbol (for
3001 which a non-versioned indirect symbol is created) and later
3002 a definition for the non-versioned symbol is found. In that
3003 case the indirection is flipped with the versioned symbol
3004 becoming an indirect pointing at the non-versioned symbol.
3005 Thus, not an alias any more. */
3006 if (def->def_regular
3007 || def->root.type != bfd_link_hash_defined)
3008 {
3009 h = def;
3010 while ((h = h->u.alias) != def)
3011 h->is_weakalias = 0;
3012 }
3013 else
3014 {
3015 while (h->root.type == bfd_link_hash_indirect)
3016 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3017 BFD_ASSERT (h->root.type == bfd_link_hash_defined
3018 || h->root.type == bfd_link_hash_defweak);
3019 BFD_ASSERT (def->def_dynamic);
3020 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
3021 }
3022 }
3023
3024 return true;
3025 }
3026
3027 /* Make the backend pick a good value for a dynamic symbol. This is
3028 called via elf_link_hash_traverse, and also calls itself
3029 recursively. */
3030
3031 static bool
3032 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
3033 {
3034 struct elf_info_failed *eif = (struct elf_info_failed *) data;
3035 struct elf_link_hash_table *htab;
3036 const struct elf_backend_data *bed;
3037
3038 if (! is_elf_hash_table (eif->info->hash))
3039 return false;
3040
3041 /* Ignore indirect symbols. These are added by the versioning code. */
3042 if (h->root.type == bfd_link_hash_indirect)
3043 return true;
3044
3045 /* Fix the symbol flags. */
3046 if (! _bfd_elf_fix_symbol_flags (h, eif))
3047 return false;
3048
3049 htab = elf_hash_table (eif->info);
3050 bed = get_elf_backend_data (htab->dynobj);
3051
3052 if (h->root.type == bfd_link_hash_undefweak)
3053 {
3054 if (eif->info->dynamic_undefined_weak == 0)
3055 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3056 else if (eif->info->dynamic_undefined_weak > 0
3057 && h->ref_regular
3058 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3059 && !bfd_hide_sym_by_version (eif->info->version_info,
3060 h->root.root.string))
3061 {
3062 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
3063 {
3064 eif->failed = true;
3065 return false;
3066 }
3067 }
3068 }
3069
3070 /* If this symbol does not require a PLT entry, and it is not
3071 defined by a dynamic object, or is not referenced by a regular
3072 object, ignore it. We do have to handle a weak defined symbol,
3073 even if no regular object refers to it, if we decided to add it
3074 to the dynamic symbol table. FIXME: Do we normally need to worry
3075 about symbols which are defined by one dynamic object and
3076 referenced by another one? */
3077 if (!h->needs_plt
3078 && h->type != STT_GNU_IFUNC
3079 && (h->def_regular
3080 || !h->def_dynamic
3081 || (!h->ref_regular
3082 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
3083 {
3084 h->plt = elf_hash_table (eif->info)->init_plt_offset;
3085 return true;
3086 }
3087
3088 /* If we've already adjusted this symbol, don't do it again. This
3089 can happen via a recursive call. */
3090 if (h->dynamic_adjusted)
3091 return true;
3092
3093 /* Don't look at this symbol again. Note that we must set this
3094 after checking the above conditions, because we may look at a
3095 symbol once, decide not to do anything, and then get called
3096 recursively later after REF_REGULAR is set below. */
3097 h->dynamic_adjusted = 1;
3098
3099 /* If this is a weak definition, and we know a real definition, and
3100 the real symbol is not itself defined by a regular object file,
3101 then get a good value for the real definition. We handle the
3102 real symbol first, for the convenience of the backend routine.
3103
3104 Note that there is a confusing case here. If the real definition
3105 is defined by a regular object file, we don't get the real symbol
3106 from the dynamic object, but we do get the weak symbol. If the
3107 processor backend uses a COPY reloc, then if some routine in the
3108 dynamic object changes the real symbol, we will not see that
3109 change in the corresponding weak symbol. This is the way other
3110 ELF linkers work as well, and seems to be a result of the shared
3111 library model.
3112
3113 I will clarify this issue. Most SVR4 shared libraries define the
3114 variable _timezone and define timezone as a weak synonym. The
3115 tzset call changes _timezone. If you write
3116 extern int timezone;
3117 int _timezone = 5;
3118 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3119 you might expect that, since timezone is a synonym for _timezone,
3120 the same number will print both times. However, if the processor
3121 backend uses a COPY reloc, then actually timezone will be copied
3122 into your process image, and, since you define _timezone
3123 yourself, _timezone will not. Thus timezone and _timezone will
3124 wind up at different memory locations. The tzset call will set
3125 _timezone, leaving timezone unchanged. */
3126
3127 if (h->is_weakalias)
3128 {
3129 struct elf_link_hash_entry *def = weakdef (h);
3130
3131 /* If we get to this point, there is an implicit reference to
3132 the alias by a regular object file via the weak symbol H. */
3133 def->ref_regular = 1;
3134
3135 /* Ensure that the backend adjust_dynamic_symbol function sees
3136 the strong alias before H by recursively calling ourselves. */
3137 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
3138 return false;
3139 }
3140
3141 /* If a symbol has no type and no size and does not require a PLT
3142 entry, then we are probably about to do the wrong thing here: we
3143 are probably going to create a COPY reloc for an empty object.
3144 This case can arise when a shared object is built with assembly
3145 code, and the assembly code fails to set the symbol type. */
3146 if (h->size == 0
3147 && h->type == STT_NOTYPE
3148 && !h->needs_plt)
3149 _bfd_error_handler
3150 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3151 h->root.root.string);
3152
3153 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3154 {
3155 eif->failed = true;
3156 return false;
3157 }
3158
3159 return true;
3160 }
3161
3162 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3163 DYNBSS. */
3164
3165 bool
3166 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3167 struct elf_link_hash_entry *h,
3168 asection *dynbss)
3169 {
3170 unsigned int power_of_two;
3171 bfd_vma mask;
3172 asection *sec = h->root.u.def.section;
3173
3174 /* The section alignment of the definition is the maximum alignment
3175 requirement of symbols defined in the section. Since we don't
3176 know the symbol alignment requirement, we start with the
3177 maximum alignment and check low bits of the symbol address
3178 for the minimum alignment. */
3179 power_of_two = bfd_section_alignment (sec);
3180 mask = ((bfd_vma) 1 << power_of_two) - 1;
3181 while ((h->root.u.def.value & mask) != 0)
3182 {
3183 mask >>= 1;
3184 --power_of_two;
3185 }
3186
3187 if (power_of_two > bfd_section_alignment (dynbss))
3188 {
3189 /* Adjust the section alignment if needed. */
3190 if (!bfd_set_section_alignment (dynbss, power_of_two))
3191 return false;
3192 }
3193
3194 /* We make sure that the symbol will be aligned properly. */
3195 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3196
3197 /* Define the symbol as being at this point in DYNBSS. */
3198 h->root.u.def.section = dynbss;
3199 h->root.u.def.value = dynbss->size;
3200
3201 /* Increment the size of DYNBSS to make room for the symbol. */
3202 dynbss->size += h->size;
3203
3204 /* No error if extern_protected_data is true. */
3205 if (h->protected_def
3206 && (!info->extern_protected_data
3207 || (info->extern_protected_data < 0
3208 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
3209 info->callbacks->einfo
3210 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3211 h->root.root.string);
3212
3213 return true;
3214 }
3215
3216 /* Adjust all external symbols pointing into SEC_MERGE sections
3217 to reflect the object merging within the sections. */
3218
3219 static bool
3220 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
3221 {
3222 asection *sec;
3223
3224 if ((h->root.type == bfd_link_hash_defined
3225 || h->root.type == bfd_link_hash_defweak)
3226 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
3227 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3228 {
3229 bfd *output_bfd = (bfd *) data;
3230
3231 h->root.u.def.value =
3232 _bfd_merged_section_offset (output_bfd,
3233 &h->root.u.def.section,
3234 elf_section_data (sec)->sec_info,
3235 h->root.u.def.value);
3236 }
3237
3238 return true;
3239 }
3240
3241 /* Returns false if the symbol referred to by H should be considered
3242 to resolve local to the current module, and true if it should be
3243 considered to bind dynamically. */
3244
3245 bool
3246 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3247 struct bfd_link_info *info,
3248 bool not_local_protected)
3249 {
3250 bool binding_stays_local_p;
3251 const struct elf_backend_data *bed;
3252 struct elf_link_hash_table *hash_table;
3253
3254 if (h == NULL)
3255 return false;
3256
3257 while (h->root.type == bfd_link_hash_indirect
3258 || h->root.type == bfd_link_hash_warning)
3259 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3260
3261 /* If it was forced local, then clearly it's not dynamic. */
3262 if (h->dynindx == -1)
3263 return false;
3264 if (h->forced_local)
3265 return false;
3266
3267 /* Identify the cases where name binding rules say that a
3268 visible symbol resolves locally. */
3269 binding_stays_local_p = (bfd_link_executable (info)
3270 || SYMBOLIC_BIND (info, h));
3271
3272 switch (ELF_ST_VISIBILITY (h->other))
3273 {
3274 case STV_INTERNAL:
3275 case STV_HIDDEN:
3276 return false;
3277
3278 case STV_PROTECTED:
3279 hash_table = elf_hash_table (info);
3280 if (!is_elf_hash_table (&hash_table->root))
3281 return false;
3282
3283 bed = get_elf_backend_data (hash_table->dynobj);
3284
3285 /* Proper resolution for function pointer equality may require
3286 that these symbols perhaps be resolved dynamically, even though
3287 we should be resolving them to the current module. */
3288 if (!not_local_protected || !bed->is_function_type (h->type))
3289 binding_stays_local_p = true;
3290 break;
3291
3292 default:
3293 break;
3294 }
3295
3296 /* If it isn't defined locally, then clearly it's dynamic. */
3297 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
3298 return true;
3299
3300 /* Otherwise, the symbol is dynamic if binding rules don't tell
3301 us that it remains local. */
3302 return !binding_stays_local_p;
3303 }
3304
3305 /* Return true if the symbol referred to by H should be considered
3306 to resolve local to the current module, and false otherwise. Differs
3307 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3308 undefined symbols. The two functions are virtually identical except
3309 for the place where dynindx == -1 is tested. If that test is true,
3310 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3311 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3312 defined symbols.
3313 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3314 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3315 treatment of undefined weak symbols. For those that do not make
3316 undefined weak symbols dynamic, both functions may return false. */
3317
3318 bool
3319 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3320 struct bfd_link_info *info,
3321 bool local_protected)
3322 {
3323 const struct elf_backend_data *bed;
3324 struct elf_link_hash_table *hash_table;
3325
3326 /* If it's a local sym, of course we resolve locally. */
3327 if (h == NULL)
3328 return true;
3329
3330 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3331 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3332 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3333 return true;
3334
3335 /* Forced local symbols resolve locally. */
3336 if (h->forced_local)
3337 return true;
3338
3339 /* Common symbols that become definitions don't get the DEF_REGULAR
3340 flag set, so test it first, and don't bail out. */
3341 if (ELF_COMMON_DEF_P (h))
3342 /* Do nothing. */;
3343 /* If we don't have a definition in a regular file, then we can't
3344 resolve locally. The sym is either undefined or dynamic. */
3345 else if (!h->def_regular)
3346 return false;
3347
3348 /* Non-dynamic symbols resolve locally. */
3349 if (h->dynindx == -1)
3350 return true;
3351
3352 /* At this point, we know the symbol is defined and dynamic. In an
3353 executable it must resolve locally, likewise when building symbolic
3354 shared libraries. */
3355 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3356 return true;
3357
3358 /* Now deal with defined dynamic symbols in shared libraries. Ones
3359 with default visibility might not resolve locally. */
3360 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3361 return false;
3362
3363 hash_table = elf_hash_table (info);
3364 if (!is_elf_hash_table (&hash_table->root))
3365 return true;
3366
3367 /* STV_PROTECTED symbols with indirect external access are local. */
3368 if (info->indirect_extern_access > 0)
3369 return true;
3370
3371 bed = get_elf_backend_data (hash_table->dynobj);
3372
3373 /* If extern_protected_data is false, STV_PROTECTED non-function
3374 symbols are local. */
3375 if ((!info->extern_protected_data
3376 || (info->extern_protected_data < 0
3377 && !bed->extern_protected_data))
3378 && !bed->is_function_type (h->type))
3379 return true;
3380
3381 /* Function pointer equality tests may require that STV_PROTECTED
3382 symbols be treated as dynamic symbols. If the address of a
3383 function not defined in an executable is set to that function's
3384 plt entry in the executable, then the address of the function in
3385 a shared library must also be the plt entry in the executable. */
3386 return local_protected;
3387 }
3388
3389 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3390 aligned. Returns the first TLS output section. */
3391
3392 struct bfd_section *
3393 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3394 {
3395 struct bfd_section *sec, *tls;
3396 unsigned int align = 0;
3397
3398 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3399 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3400 break;
3401 tls = sec;
3402
3403 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3404 if (sec->alignment_power > align)
3405 align = sec->alignment_power;
3406
3407 elf_hash_table (info)->tls_sec = tls;
3408
3409 /* Ensure the alignment of the first section (usually .tdata) is the largest
3410 alignment, so that the tls segment starts aligned. */
3411 if (tls != NULL)
3412 tls->alignment_power = align;
3413
3414 return tls;
3415 }
3416
3417 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3418 static bool
3419 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3420 Elf_Internal_Sym *sym)
3421 {
3422 const struct elf_backend_data *bed;
3423
3424 /* Local symbols do not count, but target specific ones might. */
3425 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3426 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3427 return false;
3428
3429 bed = get_elf_backend_data (abfd);
3430 /* Function symbols do not count. */
3431 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3432 return false;
3433
3434 /* If the section is undefined, then so is the symbol. */
3435 if (sym->st_shndx == SHN_UNDEF)
3436 return false;
3437
3438 /* If the symbol is defined in the common section, then
3439 it is a common definition and so does not count. */
3440 if (bed->common_definition (sym))
3441 return false;
3442
3443 /* If the symbol is in a target specific section then we
3444 must rely upon the backend to tell us what it is. */
3445 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3446 /* FIXME - this function is not coded yet:
3447
3448 return _bfd_is_global_symbol_definition (abfd, sym);
3449
3450 Instead for now assume that the definition is not global,
3451 Even if this is wrong, at least the linker will behave
3452 in the same way that it used to do. */
3453 return false;
3454
3455 return true;
3456 }
3457
3458 /* Search the symbol table of the archive element of the archive ABFD
3459 whose archive map contains a mention of SYMDEF, and determine if
3460 the symbol is defined in this element. */
3461 static bool
3462 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3463 {
3464 Elf_Internal_Shdr * hdr;
3465 size_t symcount;
3466 size_t extsymcount;
3467 size_t extsymoff;
3468 Elf_Internal_Sym *isymbuf;
3469 Elf_Internal_Sym *isym;
3470 Elf_Internal_Sym *isymend;
3471 bool result;
3472
3473 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset, NULL);
3474 if (abfd == NULL)
3475 return false;
3476
3477 if (! bfd_check_format (abfd, bfd_object))
3478 return false;
3479
3480 /* Select the appropriate symbol table. If we don't know if the
3481 object file is an IR object, give linker LTO plugin a chance to
3482 get the correct symbol table. */
3483 if (abfd->plugin_format == bfd_plugin_yes
3484 #if BFD_SUPPORTS_PLUGINS
3485 || (abfd->plugin_format == bfd_plugin_unknown
3486 && bfd_link_plugin_object_p (abfd))
3487 #endif
3488 )
3489 {
3490 /* Use the IR symbol table if the object has been claimed by
3491 plugin. */
3492 abfd = abfd->plugin_dummy_bfd;
3493 hdr = &elf_tdata (abfd)->symtab_hdr;
3494 }
3495 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3496 hdr = &elf_tdata (abfd)->symtab_hdr;
3497 else
3498 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3499
3500 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3501
3502 /* The sh_info field of the symtab header tells us where the
3503 external symbols start. We don't care about the local symbols. */
3504 if (elf_bad_symtab (abfd))
3505 {
3506 extsymcount = symcount;
3507 extsymoff = 0;
3508 }
3509 else
3510 {
3511 extsymcount = symcount - hdr->sh_info;
3512 extsymoff = hdr->sh_info;
3513 }
3514
3515 if (extsymcount == 0)
3516 return false;
3517
3518 /* Read in the symbol table. */
3519 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3520 NULL, NULL, NULL);
3521 if (isymbuf == NULL)
3522 return false;
3523
3524 /* Scan the symbol table looking for SYMDEF. */
3525 result = false;
3526 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3527 {
3528 const char *name;
3529
3530 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3531 isym->st_name);
3532 if (name == NULL)
3533 break;
3534
3535 if (strcmp (name, symdef->name) == 0)
3536 {
3537 result = is_global_data_symbol_definition (abfd, isym);
3538 break;
3539 }
3540 }
3541
3542 free (isymbuf);
3543
3544 return result;
3545 }
3546 \f
3547 /* Add an entry to the .dynamic table. */
3548
3549 bool
3550 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3551 bfd_vma tag,
3552 bfd_vma val)
3553 {
3554 struct elf_link_hash_table *hash_table;
3555 const struct elf_backend_data *bed;
3556 asection *s;
3557 bfd_size_type newsize;
3558 bfd_byte *newcontents;
3559 Elf_Internal_Dyn dyn;
3560
3561 hash_table = elf_hash_table (info);
3562 if (! is_elf_hash_table (&hash_table->root))
3563 return false;
3564
3565 if (tag == DT_RELA || tag == DT_REL)
3566 hash_table->dynamic_relocs = true;
3567
3568 bed = get_elf_backend_data (hash_table->dynobj);
3569 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3570 BFD_ASSERT (s != NULL);
3571
3572 newsize = s->size + bed->s->sizeof_dyn;
3573 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3574 if (newcontents == NULL)
3575 return false;
3576
3577 dyn.d_tag = tag;
3578 dyn.d_un.d_val = val;
3579 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3580
3581 s->size = newsize;
3582 s->contents = newcontents;
3583
3584 return true;
3585 }
3586
3587 /* Strip zero-sized dynamic sections. */
3588
3589 bool
3590 _bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info *info)
3591 {
3592 struct elf_link_hash_table *hash_table;
3593 const struct elf_backend_data *bed;
3594 asection *s, *sdynamic, **pp;
3595 asection *rela_dyn, *rel_dyn;
3596 Elf_Internal_Dyn dyn;
3597 bfd_byte *extdyn, *next;
3598 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3599 bool strip_zero_sized;
3600 bool strip_zero_sized_plt;
3601
3602 if (bfd_link_relocatable (info))
3603 return true;
3604
3605 hash_table = elf_hash_table (info);
3606 if (!is_elf_hash_table (&hash_table->root))
3607 return false;
3608
3609 if (!hash_table->dynobj)
3610 return true;
3611
3612 sdynamic= bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3613 if (!sdynamic)
3614 return true;
3615
3616 bed = get_elf_backend_data (hash_table->dynobj);
3617 swap_dyn_in = bed->s->swap_dyn_in;
3618
3619 strip_zero_sized = false;
3620 strip_zero_sized_plt = false;
3621
3622 /* Strip zero-sized dynamic sections. */
3623 rela_dyn = bfd_get_section_by_name (info->output_bfd, ".rela.dyn");
3624 rel_dyn = bfd_get_section_by_name (info->output_bfd, ".rel.dyn");
3625 for (pp = &info->output_bfd->sections; (s = *pp) != NULL;)
3626 if (s->size == 0
3627 && (s == rela_dyn
3628 || s == rel_dyn
3629 || s == hash_table->srelplt->output_section
3630 || s == hash_table->splt->output_section))
3631 {
3632 *pp = s->next;
3633 info->output_bfd->section_count--;
3634 strip_zero_sized = true;
3635 if (s == rela_dyn)
3636 s = rela_dyn;
3637 if (s == rel_dyn)
3638 s = rel_dyn;
3639 else if (s == hash_table->splt->output_section)
3640 {
3641 s = hash_table->splt;
3642 strip_zero_sized_plt = true;
3643 }
3644 else
3645 s = hash_table->srelplt;
3646 s->flags |= SEC_EXCLUDE;
3647 s->output_section = bfd_abs_section_ptr;
3648 }
3649 else
3650 pp = &s->next;
3651
3652 if (strip_zero_sized_plt && sdynamic->size != 0)
3653 for (extdyn = sdynamic->contents;
3654 extdyn < sdynamic->contents + sdynamic->size;
3655 extdyn = next)
3656 {
3657 next = extdyn + bed->s->sizeof_dyn;
3658 swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3659 switch (dyn.d_tag)
3660 {
3661 default:
3662 break;
3663 case DT_JMPREL:
3664 case DT_PLTRELSZ:
3665 case DT_PLTREL:
3666 /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
3667 the procedure linkage table (the .plt section) has been
3668 removed. */
3669 memmove (extdyn, next,
3670 sdynamic->size - (next - sdynamic->contents));
3671 next = extdyn;
3672 }
3673 }
3674
3675 if (strip_zero_sized)
3676 {
3677 /* Regenerate program headers. */
3678 elf_seg_map (info->output_bfd) = NULL;
3679 return _bfd_elf_map_sections_to_segments (info->output_bfd, info);
3680 }
3681
3682 return true;
3683 }
3684
3685 /* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error,
3686 1 if a DT_NEEDED tag already exists, and 0 on success. */
3687
3688 int
3689 bfd_elf_add_dt_needed_tag (bfd *abfd, struct bfd_link_info *info)
3690 {
3691 struct elf_link_hash_table *hash_table;
3692 size_t strindex;
3693 const char *soname;
3694
3695 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3696 return -1;
3697
3698 hash_table = elf_hash_table (info);
3699 soname = elf_dt_name (abfd);
3700 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, false);
3701 if (strindex == (size_t) -1)
3702 return -1;
3703
3704 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3705 {
3706 asection *sdyn;
3707 const struct elf_backend_data *bed;
3708 bfd_byte *extdyn;
3709
3710 bed = get_elf_backend_data (hash_table->dynobj);
3711 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3712 if (sdyn != NULL && sdyn->size != 0)
3713 for (extdyn = sdyn->contents;
3714 extdyn < sdyn->contents + sdyn->size;
3715 extdyn += bed->s->sizeof_dyn)
3716 {
3717 Elf_Internal_Dyn dyn;
3718
3719 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3720 if (dyn.d_tag == DT_NEEDED
3721 && dyn.d_un.d_val == strindex)
3722 {
3723 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3724 return 1;
3725 }
3726 }
3727 }
3728
3729 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3730 return -1;
3731
3732 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3733 return -1;
3734
3735 return 0;
3736 }
3737
3738 /* Return true if SONAME is on the needed list between NEEDED and STOP
3739 (or the end of list if STOP is NULL), and needed by a library that
3740 will be loaded. */
3741
3742 static bool
3743 on_needed_list (const char *soname,
3744 struct bfd_link_needed_list *needed,
3745 struct bfd_link_needed_list *stop)
3746 {
3747 struct bfd_link_needed_list *look;
3748 for (look = needed; look != stop; look = look->next)
3749 if (strcmp (soname, look->name) == 0
3750 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3751 /* If needed by a library that itself is not directly
3752 needed, recursively check whether that library is
3753 indirectly needed. Since we add DT_NEEDED entries to
3754 the end of the list, library dependencies appear after
3755 the library. Therefore search prior to the current
3756 LOOK, preventing possible infinite recursion. */
3757 || on_needed_list (elf_dt_name (look->by), needed, look)))
3758 return true;
3759
3760 return false;
3761 }
3762
3763 /* Sort symbol by value, section, size, and type. */
3764 static int
3765 elf_sort_symbol (const void *arg1, const void *arg2)
3766 {
3767 const struct elf_link_hash_entry *h1;
3768 const struct elf_link_hash_entry *h2;
3769 bfd_signed_vma vdiff;
3770 int sdiff;
3771 const char *n1;
3772 const char *n2;
3773
3774 h1 = *(const struct elf_link_hash_entry **) arg1;
3775 h2 = *(const struct elf_link_hash_entry **) arg2;
3776 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3777 if (vdiff != 0)
3778 return vdiff > 0 ? 1 : -1;
3779
3780 sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3781 if (sdiff != 0)
3782 return sdiff;
3783
3784 /* Sort so that sized symbols are selected over zero size symbols. */
3785 vdiff = h1->size - h2->size;
3786 if (vdiff != 0)
3787 return vdiff > 0 ? 1 : -1;
3788
3789 /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */
3790 if (h1->type != h2->type)
3791 return h1->type - h2->type;
3792
3793 /* If symbols are properly sized and typed, and multiple strong
3794 aliases are not defined in a shared library by the user we
3795 shouldn't get here. Unfortunately linker script symbols like
3796 __bss_start sometimes match a user symbol defined at the start of
3797 .bss without proper size and type. We'd like to preference the
3798 user symbol over reserved system symbols. Sort on leading
3799 underscores. */
3800 n1 = h1->root.root.string;
3801 n2 = h2->root.root.string;
3802 while (*n1 == *n2)
3803 {
3804 if (*n1 == 0)
3805 break;
3806 ++n1;
3807 ++n2;
3808 }
3809 if (*n1 == '_')
3810 return -1;
3811 if (*n2 == '_')
3812 return 1;
3813
3814 /* Final sort on name selects user symbols like '_u' over reserved
3815 system symbols like '_Z' and also will avoid qsort instability. */
3816 return *n1 - *n2;
3817 }
3818
3819 /* This function is used to adjust offsets into .dynstr for
3820 dynamic symbols. This is called via elf_link_hash_traverse. */
3821
3822 static bool
3823 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3824 {
3825 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3826
3827 if (h->dynindx != -1)
3828 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3829 return true;
3830 }
3831
3832 /* Assign string offsets in .dynstr, update all structures referencing
3833 them. */
3834
3835 static bool
3836 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3837 {
3838 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3839 struct elf_link_local_dynamic_entry *entry;
3840 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3841 bfd *dynobj = hash_table->dynobj;
3842 asection *sdyn;
3843 bfd_size_type size;
3844 const struct elf_backend_data *bed;
3845 bfd_byte *extdyn;
3846
3847 _bfd_elf_strtab_finalize (dynstr);
3848 size = _bfd_elf_strtab_size (dynstr);
3849
3850 /* Allow the linker to examine the dynsymtab now it's fully populated. */
3851
3852 if (info->callbacks->examine_strtab)
3853 info->callbacks->examine_strtab (dynstr);
3854
3855 bed = get_elf_backend_data (dynobj);
3856 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3857 BFD_ASSERT (sdyn != NULL);
3858
3859 /* Update all .dynamic entries referencing .dynstr strings. */
3860 for (extdyn = sdyn->contents;
3861 extdyn < PTR_ADD (sdyn->contents, sdyn->size);
3862 extdyn += bed->s->sizeof_dyn)
3863 {
3864 Elf_Internal_Dyn dyn;
3865
3866 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3867 switch (dyn.d_tag)
3868 {
3869 case DT_STRSZ:
3870 dyn.d_un.d_val = size;
3871 break;
3872 case DT_NEEDED:
3873 case DT_SONAME:
3874 case DT_RPATH:
3875 case DT_RUNPATH:
3876 case DT_FILTER:
3877 case DT_AUXILIARY:
3878 case DT_AUDIT:
3879 case DT_DEPAUDIT:
3880 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3881 break;
3882 default:
3883 continue;
3884 }
3885 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3886 }
3887
3888 /* Now update local dynamic symbols. */
3889 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3890 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3891 entry->isym.st_name);
3892
3893 /* And the rest of dynamic symbols. */
3894 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3895
3896 /* Adjust version definitions. */
3897 if (elf_tdata (output_bfd)->cverdefs)
3898 {
3899 asection *s;
3900 bfd_byte *p;
3901 size_t i;
3902 Elf_Internal_Verdef def;
3903 Elf_Internal_Verdaux defaux;
3904
3905 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3906 p = s->contents;
3907 do
3908 {
3909 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3910 &def);
3911 p += sizeof (Elf_External_Verdef);
3912 if (def.vd_aux != sizeof (Elf_External_Verdef))
3913 continue;
3914 for (i = 0; i < def.vd_cnt; ++i)
3915 {
3916 _bfd_elf_swap_verdaux_in (output_bfd,
3917 (Elf_External_Verdaux *) p, &defaux);
3918 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3919 defaux.vda_name);
3920 _bfd_elf_swap_verdaux_out (output_bfd,
3921 &defaux, (Elf_External_Verdaux *) p);
3922 p += sizeof (Elf_External_Verdaux);
3923 }
3924 }
3925 while (def.vd_next);
3926 }
3927
3928 /* Adjust version references. */
3929 if (elf_tdata (output_bfd)->verref)
3930 {
3931 asection *s;
3932 bfd_byte *p;
3933 size_t i;
3934 Elf_Internal_Verneed need;
3935 Elf_Internal_Vernaux needaux;
3936
3937 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3938 p = s->contents;
3939 do
3940 {
3941 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3942 &need);
3943 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3944 _bfd_elf_swap_verneed_out (output_bfd, &need,
3945 (Elf_External_Verneed *) p);
3946 p += sizeof (Elf_External_Verneed);
3947 for (i = 0; i < need.vn_cnt; ++i)
3948 {
3949 _bfd_elf_swap_vernaux_in (output_bfd,
3950 (Elf_External_Vernaux *) p, &needaux);
3951 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3952 needaux.vna_name);
3953 _bfd_elf_swap_vernaux_out (output_bfd,
3954 &needaux,
3955 (Elf_External_Vernaux *) p);
3956 p += sizeof (Elf_External_Vernaux);
3957 }
3958 }
3959 while (need.vn_next);
3960 }
3961
3962 return true;
3963 }
3964 \f
3965 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3966 The default is to only match when the INPUT and OUTPUT are exactly
3967 the same target. */
3968
3969 bool
3970 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3971 const bfd_target *output)
3972 {
3973 return input == output;
3974 }
3975
3976 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3977 This version is used when different targets for the same architecture
3978 are virtually identical. */
3979
3980 bool
3981 _bfd_elf_relocs_compatible (const bfd_target *input,
3982 const bfd_target *output)
3983 {
3984 const struct elf_backend_data *obed, *ibed;
3985
3986 if (input == output)
3987 return true;
3988
3989 ibed = xvec_get_elf_backend_data (input);
3990 obed = xvec_get_elf_backend_data (output);
3991
3992 if (ibed->arch != obed->arch)
3993 return false;
3994
3995 /* If both backends are using this function, deem them compatible. */
3996 return ibed->relocs_compatible == obed->relocs_compatible;
3997 }
3998
3999 /* Make a special call to the linker "notice" function to tell it that
4000 we are about to handle an as-needed lib, or have finished
4001 processing the lib. */
4002
4003 bool
4004 _bfd_elf_notice_as_needed (bfd *ibfd,
4005 struct bfd_link_info *info,
4006 enum notice_asneeded_action act)
4007 {
4008 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
4009 }
4010
4011 /* Call ACTION on each relocation in an ELF object file. */
4012
4013 bool
4014 _bfd_elf_link_iterate_on_relocs
4015 (bfd *abfd, struct bfd_link_info *info,
4016 bool (*action) (bfd *, struct bfd_link_info *, asection *,
4017 const Elf_Internal_Rela *))
4018 {
4019 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4020 struct elf_link_hash_table *htab = elf_hash_table (info);
4021
4022 /* If this object is the same format as the output object, and it is
4023 not a shared library, then let the backend look through the
4024 relocs.
4025
4026 This is required to build global offset table entries and to
4027 arrange for dynamic relocs. It is not required for the
4028 particular common case of linking non PIC code, even when linking
4029 against shared libraries, but unfortunately there is no way of
4030 knowing whether an object file has been compiled PIC or not.
4031 Looking through the relocs is not particularly time consuming.
4032 The problem is that we must either (1) keep the relocs in memory,
4033 which causes the linker to require additional runtime memory or
4034 (2) read the relocs twice from the input file, which wastes time.
4035 This would be a good case for using mmap.
4036
4037 I have no idea how to handle linking PIC code into a file of a
4038 different format. It probably can't be done. */
4039 if ((abfd->flags & DYNAMIC) == 0
4040 && is_elf_hash_table (&htab->root)
4041 && elf_object_id (abfd) == elf_hash_table_id (htab)
4042 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4043 {
4044 asection *o;
4045
4046 for (o = abfd->sections; o != NULL; o = o->next)
4047 {
4048 Elf_Internal_Rela *internal_relocs;
4049 bool ok;
4050
4051 /* Don't check relocations in excluded sections. Don't do
4052 anything special with non-loaded, non-alloced sections.
4053 In particular, any relocs in such sections should not
4054 affect GOT and PLT reference counting (ie. we don't
4055 allow them to create GOT or PLT entries), there's no
4056 possibility or desire to optimize TLS relocs, and
4057 there's not much point in propagating relocs to shared
4058 libs that the dynamic linker won't relocate. */
4059 if ((o->flags & SEC_ALLOC) == 0
4060 || (o->flags & SEC_RELOC) == 0
4061 || (o->flags & SEC_EXCLUDE) != 0
4062 || o->reloc_count == 0
4063 || ((info->strip == strip_all || info->strip == strip_debugger)
4064 && (o->flags & SEC_DEBUGGING) != 0)
4065 || bfd_is_abs_section (o->output_section))
4066 continue;
4067
4068 internal_relocs = _bfd_elf_link_info_read_relocs (abfd, info,
4069 o, NULL,
4070 NULL,
4071 _bfd_link_keep_memory (info));
4072 if (internal_relocs == NULL)
4073 return false;
4074
4075 ok = action (abfd, info, o, internal_relocs);
4076
4077 if (elf_section_data (o)->relocs != internal_relocs)
4078 free (internal_relocs);
4079
4080 if (! ok)
4081 return false;
4082 }
4083 }
4084
4085 return true;
4086 }
4087
4088 /* Check relocations in an ELF object file. This is called after
4089 all input files have been opened. */
4090
4091 bool
4092 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
4093 {
4094 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4095 if (bed->check_relocs != NULL)
4096 return _bfd_elf_link_iterate_on_relocs (abfd, info,
4097 bed->check_relocs);
4098 return true;
4099 }
4100
4101 /* Add symbols from an ELF object file to the linker hash table. */
4102
4103 static bool
4104 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
4105 {
4106 Elf_Internal_Ehdr *ehdr;
4107 Elf_Internal_Shdr *hdr;
4108 size_t symcount;
4109 size_t extsymcount;
4110 size_t extsymoff;
4111 struct elf_link_hash_entry **sym_hash;
4112 bool dynamic;
4113 Elf_External_Versym *extversym = NULL;
4114 Elf_External_Versym *extversym_end = NULL;
4115 Elf_External_Versym *ever;
4116 struct elf_link_hash_entry *weaks;
4117 struct elf_link_hash_entry **nondeflt_vers = NULL;
4118 size_t nondeflt_vers_cnt = 0;
4119 Elf_Internal_Sym *isymbuf = NULL;
4120 Elf_Internal_Sym *isym;
4121 Elf_Internal_Sym *isymend;
4122 const struct elf_backend_data *bed;
4123 bool add_needed;
4124 struct elf_link_hash_table *htab;
4125 void *alloc_mark = NULL;
4126 struct bfd_hash_entry **old_table = NULL;
4127 unsigned int old_size = 0;
4128 unsigned int old_count = 0;
4129 void *old_tab = NULL;
4130 void *old_ent;
4131 struct bfd_link_hash_entry *old_undefs = NULL;
4132 struct bfd_link_hash_entry *old_undefs_tail = NULL;
4133 void *old_strtab = NULL;
4134 size_t tabsize = 0;
4135 asection *s;
4136 bool just_syms;
4137
4138 htab = elf_hash_table (info);
4139 bed = get_elf_backend_data (abfd);
4140
4141 if ((abfd->flags & DYNAMIC) == 0)
4142 dynamic = false;
4143 else
4144 {
4145 dynamic = true;
4146
4147 /* You can't use -r against a dynamic object. Also, there's no
4148 hope of using a dynamic object which does not exactly match
4149 the format of the output file. */
4150 if (bfd_link_relocatable (info)
4151 || !is_elf_hash_table (&htab->root)
4152 || info->output_bfd->xvec != abfd->xvec)
4153 {
4154 if (bfd_link_relocatable (info))
4155 bfd_set_error (bfd_error_invalid_operation);
4156 else
4157 bfd_set_error (bfd_error_wrong_format);
4158 goto error_return;
4159 }
4160 }
4161
4162 ehdr = elf_elfheader (abfd);
4163 if (info->warn_alternate_em
4164 && bed->elf_machine_code != ehdr->e_machine
4165 && ((bed->elf_machine_alt1 != 0
4166 && ehdr->e_machine == bed->elf_machine_alt1)
4167 || (bed->elf_machine_alt2 != 0
4168 && ehdr->e_machine == bed->elf_machine_alt2)))
4169 _bfd_error_handler
4170 /* xgettext:c-format */
4171 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
4172 ehdr->e_machine, abfd, bed->elf_machine_code);
4173
4174 /* As a GNU extension, any input sections which are named
4175 .gnu.warning.SYMBOL are treated as warning symbols for the given
4176 symbol. This differs from .gnu.warning sections, which generate
4177 warnings when they are included in an output file. */
4178 /* PR 12761: Also generate this warning when building shared libraries. */
4179 for (s = abfd->sections; s != NULL; s = s->next)
4180 {
4181 const char *name;
4182
4183 name = bfd_section_name (s);
4184 if (startswith (name, ".gnu.warning."))
4185 {
4186 char *msg;
4187 bfd_size_type sz;
4188
4189 name += sizeof ".gnu.warning." - 1;
4190
4191 /* If this is a shared object, then look up the symbol
4192 in the hash table. If it is there, and it is already
4193 been defined, then we will not be using the entry
4194 from this shared object, so we don't need to warn.
4195 FIXME: If we see the definition in a regular object
4196 later on, we will warn, but we shouldn't. The only
4197 fix is to keep track of what warnings we are supposed
4198 to emit, and then handle them all at the end of the
4199 link. */
4200 if (dynamic)
4201 {
4202 struct elf_link_hash_entry *h;
4203
4204 h = elf_link_hash_lookup (htab, name, false, false, true);
4205
4206 /* FIXME: What about bfd_link_hash_common? */
4207 if (h != NULL
4208 && (h->root.type == bfd_link_hash_defined
4209 || h->root.type == bfd_link_hash_defweak))
4210 continue;
4211 }
4212
4213 sz = s->size;
4214 msg = (char *) bfd_alloc (abfd, sz + 1);
4215 if (msg == NULL)
4216 goto error_return;
4217
4218 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
4219 goto error_return;
4220
4221 msg[sz] = '\0';
4222
4223 if (! (_bfd_generic_link_add_one_symbol
4224 (info, abfd, name, BSF_WARNING, s, 0, msg,
4225 false, bed->collect, NULL)))
4226 goto error_return;
4227
4228 if (bfd_link_executable (info))
4229 {
4230 /* Clobber the section size so that the warning does
4231 not get copied into the output file. */
4232 s->size = 0;
4233
4234 /* Also set SEC_EXCLUDE, so that symbols defined in
4235 the warning section don't get copied to the output. */
4236 s->flags |= SEC_EXCLUDE;
4237 }
4238 }
4239 }
4240
4241 just_syms = ((s = abfd->sections) != NULL
4242 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
4243
4244 add_needed = true;
4245 if (! dynamic)
4246 {
4247 /* If we are creating a shared library, create all the dynamic
4248 sections immediately. We need to attach them to something,
4249 so we attach them to this BFD, provided it is the right
4250 format and is not from ld --just-symbols. Always create the
4251 dynamic sections for -E/--dynamic-list. FIXME: If there
4252 are no input BFD's of the same format as the output, we can't
4253 make a shared library. */
4254 if (!just_syms
4255 && (bfd_link_pic (info)
4256 || (!bfd_link_relocatable (info)
4257 && info->nointerp
4258 && (info->export_dynamic || info->dynamic)))
4259 && is_elf_hash_table (&htab->root)
4260 && info->output_bfd->xvec == abfd->xvec
4261 && !htab->dynamic_sections_created)
4262 {
4263 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4264 goto error_return;
4265 }
4266 }
4267 else if (!is_elf_hash_table (&htab->root))
4268 goto error_return;
4269 else
4270 {
4271 const char *soname = NULL;
4272 char *audit = NULL;
4273 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
4274 const Elf_Internal_Phdr *phdr;
4275 struct elf_link_loaded_list *loaded_lib;
4276
4277 /* ld --just-symbols and dynamic objects don't mix very well.
4278 ld shouldn't allow it. */
4279 if (just_syms)
4280 abort ();
4281
4282 /* If this dynamic lib was specified on the command line with
4283 --as-needed in effect, then we don't want to add a DT_NEEDED
4284 tag unless the lib is actually used. Similary for libs brought
4285 in by another lib's DT_NEEDED. When --no-add-needed is used
4286 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4287 any dynamic library in DT_NEEDED tags in the dynamic lib at
4288 all. */
4289 add_needed = (elf_dyn_lib_class (abfd)
4290 & (DYN_AS_NEEDED | DYN_DT_NEEDED
4291 | DYN_NO_NEEDED)) == 0;
4292
4293 s = bfd_get_section_by_name (abfd, ".dynamic");
4294 if (s != NULL && s->size != 0)
4295 {
4296 bfd_byte *dynbuf;
4297 bfd_byte *extdyn;
4298 unsigned int elfsec;
4299 unsigned long shlink;
4300
4301 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
4302 {
4303 error_free_dyn:
4304 free (dynbuf);
4305 goto error_return;
4306 }
4307
4308 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
4309 if (elfsec == SHN_BAD)
4310 goto error_free_dyn;
4311 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4312
4313 for (extdyn = dynbuf;
4314 extdyn <= dynbuf + s->size - bed->s->sizeof_dyn;
4315 extdyn += bed->s->sizeof_dyn)
4316 {
4317 Elf_Internal_Dyn dyn;
4318
4319 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4320 if (dyn.d_tag == DT_SONAME)
4321 {
4322 unsigned int tagv = dyn.d_un.d_val;
4323 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4324 if (soname == NULL)
4325 goto error_free_dyn;
4326 }
4327 if (dyn.d_tag == DT_NEEDED)
4328 {
4329 struct bfd_link_needed_list *n, **pn;
4330 char *fnm, *anm;
4331 unsigned int tagv = dyn.d_un.d_val;
4332 size_t amt = sizeof (struct bfd_link_needed_list);
4333
4334 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4335 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4336 if (n == NULL || fnm == NULL)
4337 goto error_free_dyn;
4338 amt = strlen (fnm) + 1;
4339 anm = (char *) bfd_alloc (abfd, amt);
4340 if (anm == NULL)
4341 goto error_free_dyn;
4342 memcpy (anm, fnm, amt);
4343 n->name = anm;
4344 n->by = abfd;
4345 n->next = NULL;
4346 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4347 ;
4348 *pn = n;
4349 }
4350 if (dyn.d_tag == DT_RUNPATH)
4351 {
4352 struct bfd_link_needed_list *n, **pn;
4353 char *fnm, *anm;
4354 unsigned int tagv = dyn.d_un.d_val;
4355 size_t amt = sizeof (struct bfd_link_needed_list);
4356
4357 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4358 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4359 if (n == NULL || fnm == NULL)
4360 goto error_free_dyn;
4361 amt = strlen (fnm) + 1;
4362 anm = (char *) bfd_alloc (abfd, amt);
4363 if (anm == NULL)
4364 goto error_free_dyn;
4365 memcpy (anm, fnm, amt);
4366 n->name = anm;
4367 n->by = abfd;
4368 n->next = NULL;
4369 for (pn = & runpath;
4370 *pn != NULL;
4371 pn = &(*pn)->next)
4372 ;
4373 *pn = n;
4374 }
4375 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4376 if (!runpath && dyn.d_tag == DT_RPATH)
4377 {
4378 struct bfd_link_needed_list *n, **pn;
4379 char *fnm, *anm;
4380 unsigned int tagv = dyn.d_un.d_val;
4381 size_t amt = sizeof (struct bfd_link_needed_list);
4382
4383 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4384 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4385 if (n == NULL || fnm == NULL)
4386 goto error_free_dyn;
4387 amt = strlen (fnm) + 1;
4388 anm = (char *) bfd_alloc (abfd, amt);
4389 if (anm == NULL)
4390 goto error_free_dyn;
4391 memcpy (anm, fnm, amt);
4392 n->name = anm;
4393 n->by = abfd;
4394 n->next = NULL;
4395 for (pn = & rpath;
4396 *pn != NULL;
4397 pn = &(*pn)->next)
4398 ;
4399 *pn = n;
4400 }
4401 if (dyn.d_tag == DT_AUDIT)
4402 {
4403 unsigned int tagv = dyn.d_un.d_val;
4404 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4405 }
4406 if (dyn.d_tag == DT_FLAGS_1)
4407 elf_tdata (abfd)->is_pie = (dyn.d_un.d_val & DF_1_PIE) != 0;
4408 }
4409
4410 free (dynbuf);
4411 }
4412
4413 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4414 frees all more recently bfd_alloc'd blocks as well. */
4415 if (runpath)
4416 rpath = runpath;
4417
4418 if (rpath)
4419 {
4420 struct bfd_link_needed_list **pn;
4421 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4422 ;
4423 *pn = rpath;
4424 }
4425
4426 /* If we have a PT_GNU_RELRO program header, mark as read-only
4427 all sections contained fully therein. This makes relro
4428 shared library sections appear as they will at run-time. */
4429 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4430 while (phdr-- > elf_tdata (abfd)->phdr)
4431 if (phdr->p_type == PT_GNU_RELRO)
4432 {
4433 for (s = abfd->sections; s != NULL; s = s->next)
4434 {
4435 unsigned int opb = bfd_octets_per_byte (abfd, s);
4436
4437 if ((s->flags & SEC_ALLOC) != 0
4438 && s->vma * opb >= phdr->p_vaddr
4439 && s->vma * opb + s->size <= phdr->p_vaddr + phdr->p_memsz)
4440 s->flags |= SEC_READONLY;
4441 }
4442 break;
4443 }
4444
4445 /* We do not want to include any of the sections in a dynamic
4446 object in the output file. We hack by simply clobbering the
4447 list of sections in the BFD. This could be handled more
4448 cleanly by, say, a new section flag; the existing
4449 SEC_NEVER_LOAD flag is not the one we want, because that one
4450 still implies that the section takes up space in the output
4451 file. */
4452 bfd_section_list_clear (abfd);
4453
4454 /* Find the name to use in a DT_NEEDED entry that refers to this
4455 object. If the object has a DT_SONAME entry, we use it.
4456 Otherwise, if the generic linker stuck something in
4457 elf_dt_name, we use that. Otherwise, we just use the file
4458 name. */
4459 if (soname == NULL || *soname == '\0')
4460 {
4461 soname = elf_dt_name (abfd);
4462 if (soname == NULL || *soname == '\0')
4463 soname = bfd_get_filename (abfd);
4464 }
4465
4466 /* Save the SONAME because sometimes the linker emulation code
4467 will need to know it. */
4468 elf_dt_name (abfd) = soname;
4469
4470 /* If we have already included this dynamic object in the
4471 link, just ignore it. There is no reason to include a
4472 particular dynamic object more than once. */
4473 for (loaded_lib = htab->dyn_loaded;
4474 loaded_lib != NULL;
4475 loaded_lib = loaded_lib->next)
4476 {
4477 if (strcmp (elf_dt_name (loaded_lib->abfd), soname) == 0)
4478 return true;
4479 }
4480
4481 /* Create dynamic sections for backends that require that be done
4482 before setup_gnu_properties. */
4483 if (add_needed
4484 && !_bfd_elf_link_create_dynamic_sections (abfd, info))
4485 return false;
4486
4487 /* Save the DT_AUDIT entry for the linker emulation code. */
4488 elf_dt_audit (abfd) = audit;
4489 }
4490
4491 /* If this is a dynamic object, we always link against the .dynsym
4492 symbol table, not the .symtab symbol table. The dynamic linker
4493 will only see the .dynsym symbol table, so there is no reason to
4494 look at .symtab for a dynamic object. */
4495
4496 if (! dynamic || elf_dynsymtab (abfd) == 0)
4497 hdr = &elf_tdata (abfd)->symtab_hdr;
4498 else
4499 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4500
4501 symcount = hdr->sh_size / bed->s->sizeof_sym;
4502
4503 /* The sh_info field of the symtab header tells us where the
4504 external symbols start. We don't care about the local symbols at
4505 this point. */
4506 if (elf_bad_symtab (abfd))
4507 {
4508 extsymcount = symcount;
4509 extsymoff = 0;
4510 }
4511 else
4512 {
4513 extsymcount = symcount - hdr->sh_info;
4514 extsymoff = hdr->sh_info;
4515 }
4516
4517 sym_hash = elf_sym_hashes (abfd);
4518 if (extsymcount != 0)
4519 {
4520 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4521 NULL, NULL, NULL);
4522 if (isymbuf == NULL)
4523 goto error_return;
4524
4525 if (sym_hash == NULL)
4526 {
4527 /* We store a pointer to the hash table entry for each
4528 external symbol. */
4529 size_t amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4530 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4531 if (sym_hash == NULL)
4532 goto error_free_sym;
4533 elf_sym_hashes (abfd) = sym_hash;
4534 }
4535 }
4536
4537 if (dynamic)
4538 {
4539 /* Read in any version definitions. */
4540 if (!_bfd_elf_slurp_version_tables (abfd,
4541 info->default_imported_symver))
4542 goto error_free_sym;
4543
4544 /* Read in the symbol versions, but don't bother to convert them
4545 to internal format. */
4546 if (elf_dynversym (abfd) != 0)
4547 {
4548 Elf_Internal_Shdr *versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4549 bfd_size_type amt = versymhdr->sh_size;
4550
4551 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0)
4552 goto error_free_sym;
4553 extversym = (Elf_External_Versym *)
4554 _bfd_malloc_and_read (abfd, amt, amt);
4555 if (extversym == NULL)
4556 goto error_free_sym;
4557 extversym_end = extversym + amt / sizeof (*extversym);
4558 }
4559 }
4560
4561 /* If we are loading an as-needed shared lib, save the symbol table
4562 state before we start adding symbols. If the lib turns out
4563 to be unneeded, restore the state. */
4564 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4565 {
4566 unsigned int i;
4567 size_t entsize;
4568
4569 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4570 {
4571 struct bfd_hash_entry *p;
4572 struct elf_link_hash_entry *h;
4573
4574 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4575 {
4576 h = (struct elf_link_hash_entry *) p;
4577 entsize += htab->root.table.entsize;
4578 if (h->root.type == bfd_link_hash_warning)
4579 {
4580 entsize += htab->root.table.entsize;
4581 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4582 }
4583 if (h->root.type == bfd_link_hash_common)
4584 entsize += sizeof (*h->root.u.c.p);
4585 }
4586 }
4587
4588 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4589 old_tab = bfd_malloc (tabsize + entsize);
4590 if (old_tab == NULL)
4591 goto error_free_vers;
4592
4593 /* Remember the current objalloc pointer, so that all mem for
4594 symbols added can later be reclaimed. */
4595 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4596 if (alloc_mark == NULL)
4597 goto error_free_vers;
4598
4599 /* Make a special call to the linker "notice" function to
4600 tell it that we are about to handle an as-needed lib. */
4601 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4602 goto error_free_vers;
4603
4604 /* Clone the symbol table. Remember some pointers into the
4605 symbol table, and dynamic symbol count. */
4606 old_ent = (char *) old_tab + tabsize;
4607 memcpy (old_tab, htab->root.table.table, tabsize);
4608 old_undefs = htab->root.undefs;
4609 old_undefs_tail = htab->root.undefs_tail;
4610 old_table = htab->root.table.table;
4611 old_size = htab->root.table.size;
4612 old_count = htab->root.table.count;
4613 old_strtab = NULL;
4614 if (htab->dynstr != NULL)
4615 {
4616 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4617 if (old_strtab == NULL)
4618 goto error_free_vers;
4619 }
4620
4621 for (i = 0; i < htab->root.table.size; i++)
4622 {
4623 struct bfd_hash_entry *p;
4624 struct elf_link_hash_entry *h;
4625
4626 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4627 {
4628 h = (struct elf_link_hash_entry *) p;
4629 memcpy (old_ent, h, htab->root.table.entsize);
4630 old_ent = (char *) old_ent + htab->root.table.entsize;
4631 if (h->root.type == bfd_link_hash_warning)
4632 {
4633 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4634 memcpy (old_ent, h, htab->root.table.entsize);
4635 old_ent = (char *) old_ent + htab->root.table.entsize;
4636 }
4637 if (h->root.type == bfd_link_hash_common)
4638 {
4639 memcpy (old_ent, h->root.u.c.p, sizeof (*h->root.u.c.p));
4640 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
4641 }
4642 }
4643 }
4644 }
4645
4646 weaks = NULL;
4647 if (extversym == NULL)
4648 ever = NULL;
4649 else if (extversym + extsymoff < extversym_end)
4650 ever = extversym + extsymoff;
4651 else
4652 {
4653 /* xgettext:c-format */
4654 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4655 abfd, (long) extsymoff,
4656 (long) (extversym_end - extversym) / sizeof (* extversym));
4657 bfd_set_error (bfd_error_bad_value);
4658 goto error_free_vers;
4659 }
4660
4661 if (!bfd_link_relocatable (info)
4662 && abfd->lto_slim_object)
4663 {
4664 _bfd_error_handler
4665 (_("%pB: plugin needed to handle lto object"), abfd);
4666 }
4667
4668 for (isym = isymbuf, isymend = PTR_ADD (isymbuf, extsymcount);
4669 isym < isymend;
4670 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4671 {
4672 int bind;
4673 bfd_vma value;
4674 asection *sec, *new_sec;
4675 flagword flags;
4676 const char *name;
4677 struct elf_link_hash_entry *h;
4678 struct elf_link_hash_entry *hi;
4679 bool definition;
4680 bool size_change_ok;
4681 bool type_change_ok;
4682 bool new_weak;
4683 bool old_weak;
4684 bfd *override;
4685 bool common;
4686 bool discarded;
4687 unsigned int old_alignment;
4688 unsigned int shindex;
4689 bfd *old_bfd;
4690 bool matched;
4691
4692 override = NULL;
4693
4694 flags = BSF_NO_FLAGS;
4695 sec = NULL;
4696 value = isym->st_value;
4697 common = bed->common_definition (isym);
4698 if (common && info->inhibit_common_definition)
4699 {
4700 /* Treat common symbol as undefined for --no-define-common. */
4701 isym->st_shndx = SHN_UNDEF;
4702 common = false;
4703 }
4704 discarded = false;
4705
4706 bind = ELF_ST_BIND (isym->st_info);
4707 switch (bind)
4708 {
4709 case STB_LOCAL:
4710 /* This should be impossible, since ELF requires that all
4711 global symbols follow all local symbols, and that sh_info
4712 point to the first global symbol. Unfortunately, Irix 5
4713 screws this up. */
4714 if (elf_bad_symtab (abfd))
4715 continue;
4716
4717 /* If we aren't prepared to handle locals within the globals
4718 then we'll likely segfault on a NULL symbol hash if the
4719 symbol is ever referenced in relocations. */
4720 shindex = elf_elfheader (abfd)->e_shstrndx;
4721 name = bfd_elf_string_from_elf_section (abfd, shindex, hdr->sh_name);
4722 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4723 " (>= sh_info of %lu)"),
4724 abfd, name, (long) (isym - isymbuf + extsymoff),
4725 (long) extsymoff);
4726
4727 /* Dynamic object relocations are not processed by ld, so
4728 ld won't run into the problem mentioned above. */
4729 if (dynamic)
4730 continue;
4731 bfd_set_error (bfd_error_bad_value);
4732 goto error_free_vers;
4733
4734 case STB_GLOBAL:
4735 if (isym->st_shndx != SHN_UNDEF && !common)
4736 flags = BSF_GLOBAL;
4737 break;
4738
4739 case STB_WEAK:
4740 flags = BSF_WEAK;
4741 break;
4742
4743 case STB_GNU_UNIQUE:
4744 flags = BSF_GNU_UNIQUE;
4745 break;
4746
4747 default:
4748 /* Leave it up to the processor backend. */
4749 break;
4750 }
4751
4752 if (isym->st_shndx == SHN_UNDEF)
4753 sec = bfd_und_section_ptr;
4754 else if (isym->st_shndx == SHN_ABS)
4755 sec = bfd_abs_section_ptr;
4756 else if (isym->st_shndx == SHN_COMMON)
4757 {
4758 sec = bfd_com_section_ptr;
4759 /* What ELF calls the size we call the value. What ELF
4760 calls the value we call the alignment. */
4761 value = isym->st_size;
4762 }
4763 else
4764 {
4765 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4766 if (sec == NULL)
4767 sec = bfd_abs_section_ptr;
4768 else if (discarded_section (sec))
4769 {
4770 /* Symbols from discarded section are undefined. We keep
4771 its visibility. */
4772 sec = bfd_und_section_ptr;
4773 discarded = true;
4774 isym->st_shndx = SHN_UNDEF;
4775 }
4776 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4777 value -= sec->vma;
4778 }
4779
4780 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4781 isym->st_name);
4782 if (name == NULL)
4783 goto error_free_vers;
4784
4785 if (isym->st_shndx == SHN_COMMON
4786 && (abfd->flags & BFD_PLUGIN) != 0)
4787 {
4788 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4789
4790 if (xc == NULL)
4791 {
4792 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4793 | SEC_EXCLUDE);
4794 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4795 if (xc == NULL)
4796 goto error_free_vers;
4797 }
4798 sec = xc;
4799 }
4800 else if (isym->st_shndx == SHN_COMMON
4801 && ELF_ST_TYPE (isym->st_info) == STT_TLS
4802 && !bfd_link_relocatable (info))
4803 {
4804 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4805
4806 if (tcomm == NULL)
4807 {
4808 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4809 | SEC_LINKER_CREATED);
4810 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4811 if (tcomm == NULL)
4812 goto error_free_vers;
4813 }
4814 sec = tcomm;
4815 }
4816 else if (bed->elf_add_symbol_hook)
4817 {
4818 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4819 &sec, &value))
4820 goto error_free_vers;
4821
4822 /* The hook function sets the name to NULL if this symbol
4823 should be skipped for some reason. */
4824 if (name == NULL)
4825 continue;
4826 }
4827
4828 /* Sanity check that all possibilities were handled. */
4829 if (sec == NULL)
4830 abort ();
4831
4832 /* Silently discard TLS symbols from --just-syms. There's
4833 no way to combine a static TLS block with a new TLS block
4834 for this executable. */
4835 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4836 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4837 continue;
4838
4839 if (bfd_is_und_section (sec)
4840 || bfd_is_com_section (sec))
4841 definition = false;
4842 else
4843 definition = true;
4844
4845 size_change_ok = false;
4846 type_change_ok = bed->type_change_ok;
4847 old_weak = false;
4848 matched = false;
4849 old_alignment = 0;
4850 old_bfd = NULL;
4851 new_sec = sec;
4852
4853 if (is_elf_hash_table (&htab->root))
4854 {
4855 Elf_Internal_Versym iver;
4856 unsigned int vernum = 0;
4857 bool skip;
4858
4859 if (ever == NULL)
4860 {
4861 if (info->default_imported_symver)
4862 /* Use the default symbol version created earlier. */
4863 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4864 else
4865 iver.vs_vers = 0;
4866 }
4867 else if (ever >= extversym_end)
4868 {
4869 /* xgettext:c-format */
4870 _bfd_error_handler (_("%pB: not enough version information"),
4871 abfd);
4872 bfd_set_error (bfd_error_bad_value);
4873 goto error_free_vers;
4874 }
4875 else
4876 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4877
4878 vernum = iver.vs_vers & VERSYM_VERSION;
4879
4880 /* If this is a hidden symbol, or if it is not version
4881 1, we append the version name to the symbol name.
4882 However, we do not modify a non-hidden absolute symbol
4883 if it is not a function, because it might be the version
4884 symbol itself. FIXME: What if it isn't? */
4885 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4886 || (vernum > 1
4887 && (!bfd_is_abs_section (sec)
4888 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4889 {
4890 const char *verstr;
4891 size_t namelen, verlen, newlen;
4892 char *newname, *p;
4893
4894 if (isym->st_shndx != SHN_UNDEF)
4895 {
4896 if (vernum > elf_tdata (abfd)->cverdefs)
4897 verstr = NULL;
4898 else if (vernum > 1)
4899 verstr =
4900 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4901 else
4902 verstr = "";
4903
4904 if (verstr == NULL)
4905 {
4906 _bfd_error_handler
4907 /* xgettext:c-format */
4908 (_("%pB: %s: invalid version %u (max %d)"),
4909 abfd, name, vernum,
4910 elf_tdata (abfd)->cverdefs);
4911 bfd_set_error (bfd_error_bad_value);
4912 goto error_free_vers;
4913 }
4914 }
4915 else
4916 {
4917 /* We cannot simply test for the number of
4918 entries in the VERNEED section since the
4919 numbers for the needed versions do not start
4920 at 0. */
4921 Elf_Internal_Verneed *t;
4922
4923 verstr = NULL;
4924 for (t = elf_tdata (abfd)->verref;
4925 t != NULL;
4926 t = t->vn_nextref)
4927 {
4928 Elf_Internal_Vernaux *a;
4929
4930 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4931 {
4932 if (a->vna_other == vernum)
4933 {
4934 verstr = a->vna_nodename;
4935 break;
4936 }
4937 }
4938 if (a != NULL)
4939 break;
4940 }
4941 if (verstr == NULL)
4942 {
4943 _bfd_error_handler
4944 /* xgettext:c-format */
4945 (_("%pB: %s: invalid needed version %d"),
4946 abfd, name, vernum);
4947 bfd_set_error (bfd_error_bad_value);
4948 goto error_free_vers;
4949 }
4950 }
4951
4952 namelen = strlen (name);
4953 verlen = strlen (verstr);
4954 newlen = namelen + verlen + 2;
4955 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4956 && isym->st_shndx != SHN_UNDEF)
4957 ++newlen;
4958
4959 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4960 if (newname == NULL)
4961 goto error_free_vers;
4962 memcpy (newname, name, namelen);
4963 p = newname + namelen;
4964 *p++ = ELF_VER_CHR;
4965 /* If this is a defined non-hidden version symbol,
4966 we add another @ to the name. This indicates the
4967 default version of the symbol. */
4968 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4969 && isym->st_shndx != SHN_UNDEF)
4970 *p++ = ELF_VER_CHR;
4971 memcpy (p, verstr, verlen + 1);
4972
4973 name = newname;
4974 }
4975
4976 /* If this symbol has default visibility and the user has
4977 requested we not re-export it, then mark it as hidden. */
4978 if (!bfd_is_und_section (sec)
4979 && !dynamic
4980 && abfd->no_export
4981 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4982 isym->st_other = (STV_HIDDEN
4983 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4984
4985 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4986 sym_hash, &old_bfd, &old_weak,
4987 &old_alignment, &skip, &override,
4988 &type_change_ok, &size_change_ok,
4989 &matched))
4990 goto error_free_vers;
4991
4992 if (skip)
4993 continue;
4994
4995 /* Override a definition only if the new symbol matches the
4996 existing one. */
4997 if (override && matched)
4998 definition = false;
4999
5000 h = *sym_hash;
5001 while (h->root.type == bfd_link_hash_indirect
5002 || h->root.type == bfd_link_hash_warning)
5003 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5004
5005 if (h->versioned != unversioned
5006 && elf_tdata (abfd)->verdef != NULL
5007 && vernum > 1
5008 && definition)
5009 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
5010 }
5011
5012 if (! (_bfd_generic_link_add_one_symbol
5013 (info, override ? override : abfd, name, flags, sec, value,
5014 NULL, false, bed->collect,
5015 (struct bfd_link_hash_entry **) sym_hash)))
5016 goto error_free_vers;
5017
5018 h = *sym_hash;
5019 /* We need to make sure that indirect symbol dynamic flags are
5020 updated. */
5021 hi = h;
5022 while (h->root.type == bfd_link_hash_indirect
5023 || h->root.type == bfd_link_hash_warning)
5024 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5025
5026 *sym_hash = h;
5027
5028 /* Setting the index to -3 tells elf_link_output_extsym that
5029 this symbol is defined in a discarded section. */
5030 if (discarded && is_elf_hash_table (&htab->root))
5031 h->indx = -3;
5032
5033 new_weak = (flags & BSF_WEAK) != 0;
5034 if (dynamic
5035 && definition
5036 && new_weak
5037 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
5038 && is_elf_hash_table (&htab->root)
5039 && h->u.alias == NULL)
5040 {
5041 /* Keep a list of all weak defined non function symbols from
5042 a dynamic object, using the alias field. Later in this
5043 function we will set the alias field to the correct
5044 value. We only put non-function symbols from dynamic
5045 objects on this list, because that happens to be the only
5046 time we need to know the normal symbol corresponding to a
5047 weak symbol, and the information is time consuming to
5048 figure out. If the alias field is not already NULL,
5049 then this symbol was already defined by some previous
5050 dynamic object, and we will be using that previous
5051 definition anyhow. */
5052
5053 h->u.alias = weaks;
5054 weaks = h;
5055 }
5056
5057 /* Set the alignment of a common symbol. */
5058 if ((common || bfd_is_com_section (sec))
5059 && h->root.type == bfd_link_hash_common)
5060 {
5061 unsigned int align;
5062
5063 if (common)
5064 align = bfd_log2 (isym->st_value);
5065 else
5066 {
5067 /* The new symbol is a common symbol in a shared object.
5068 We need to get the alignment from the section. */
5069 align = new_sec->alignment_power;
5070 }
5071 if (align > old_alignment)
5072 h->root.u.c.p->alignment_power = align;
5073 else
5074 h->root.u.c.p->alignment_power = old_alignment;
5075 }
5076
5077 if (is_elf_hash_table (&htab->root))
5078 {
5079 /* Set a flag in the hash table entry indicating the type of
5080 reference or definition we just found. A dynamic symbol
5081 is one which is referenced or defined by both a regular
5082 object and a shared object. */
5083 bool dynsym = false;
5084
5085 /* Plugin symbols aren't normal. Don't set def/ref flags. */
5086 if ((abfd->flags & BFD_PLUGIN) != 0)
5087 {
5088 /* Except for this flag to track nonweak references. */
5089 if (!definition
5090 && bind != STB_WEAK)
5091 h->ref_ir_nonweak = 1;
5092 }
5093 else if (!dynamic)
5094 {
5095 if (! definition)
5096 {
5097 h->ref_regular = 1;
5098 if (bind != STB_WEAK)
5099 h->ref_regular_nonweak = 1;
5100 }
5101 else
5102 {
5103 h->def_regular = 1;
5104 if (h->def_dynamic)
5105 {
5106 h->def_dynamic = 0;
5107 h->ref_dynamic = 1;
5108 }
5109 }
5110 }
5111 else
5112 {
5113 if (! definition)
5114 {
5115 h->ref_dynamic = 1;
5116 hi->ref_dynamic = 1;
5117 }
5118 else
5119 {
5120 h->def_dynamic = 1;
5121 hi->def_dynamic = 1;
5122 }
5123 }
5124
5125 /* If an indirect symbol has been forced local, don't
5126 make the real symbol dynamic. */
5127 if (h != hi && hi->forced_local)
5128 ;
5129 else if (!dynamic)
5130 {
5131 if (bfd_link_dll (info)
5132 || h->def_dynamic
5133 || h->ref_dynamic)
5134 dynsym = true;
5135 }
5136 else
5137 {
5138 if (h->def_regular
5139 || h->ref_regular
5140 || (h->is_weakalias
5141 && weakdef (h)->dynindx != -1))
5142 dynsym = true;
5143 }
5144
5145 /* Check to see if we need to add an indirect symbol for
5146 the default name. */
5147 if ((definition
5148 || (!override && h->root.type == bfd_link_hash_common))
5149 && !(hi != h
5150 && hi->versioned == versioned_hidden))
5151 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
5152 sec, value, &old_bfd, &dynsym))
5153 goto error_free_vers;
5154
5155 /* Check the alignment when a common symbol is involved. This
5156 can change when a common symbol is overridden by a normal
5157 definition or a common symbol is ignored due to the old
5158 normal definition. We need to make sure the maximum
5159 alignment is maintained. */
5160 if ((old_alignment || common)
5161 && h->root.type != bfd_link_hash_common)
5162 {
5163 unsigned int common_align;
5164 unsigned int normal_align;
5165 unsigned int symbol_align;
5166 bfd *normal_bfd;
5167 bfd *common_bfd;
5168
5169 BFD_ASSERT (h->root.type == bfd_link_hash_defined
5170 || h->root.type == bfd_link_hash_defweak);
5171
5172 symbol_align = ffs (h->root.u.def.value) - 1;
5173 if (h->root.u.def.section->owner != NULL
5174 && (h->root.u.def.section->owner->flags
5175 & (DYNAMIC | BFD_PLUGIN)) == 0)
5176 {
5177 normal_align = h->root.u.def.section->alignment_power;
5178 if (normal_align > symbol_align)
5179 normal_align = symbol_align;
5180 }
5181 else
5182 normal_align = symbol_align;
5183
5184 if (old_alignment)
5185 {
5186 common_align = old_alignment;
5187 common_bfd = old_bfd;
5188 normal_bfd = abfd;
5189 }
5190 else
5191 {
5192 common_align = bfd_log2 (isym->st_value);
5193 common_bfd = abfd;
5194 normal_bfd = old_bfd;
5195 }
5196
5197 if (normal_align < common_align)
5198 {
5199 /* PR binutils/2735 */
5200 if (normal_bfd == NULL)
5201 _bfd_error_handler
5202 /* xgettext:c-format */
5203 (_("warning: alignment %u of common symbol `%s' in %pB is"
5204 " greater than the alignment (%u) of its section %pA"),
5205 1 << common_align, name, common_bfd,
5206 1 << normal_align, h->root.u.def.section);
5207 else
5208 _bfd_error_handler
5209 /* xgettext:c-format */
5210 (_("warning: alignment %u of symbol `%s' in %pB"
5211 " is smaller than %u in %pB"),
5212 1 << normal_align, name, normal_bfd,
5213 1 << common_align, common_bfd);
5214 }
5215 }
5216
5217 /* Remember the symbol size if it isn't undefined. */
5218 if (isym->st_size != 0
5219 && isym->st_shndx != SHN_UNDEF
5220 && (definition || h->size == 0))
5221 {
5222 if (h->size != 0
5223 && h->size != isym->st_size
5224 && ! size_change_ok)
5225 _bfd_error_handler
5226 /* xgettext:c-format */
5227 (_("warning: size of symbol `%s' changed"
5228 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
5229 name, (uint64_t) h->size, old_bfd,
5230 (uint64_t) isym->st_size, abfd);
5231
5232 h->size = isym->st_size;
5233 }
5234
5235 /* If this is a common symbol, then we always want H->SIZE
5236 to be the size of the common symbol. The code just above
5237 won't fix the size if a common symbol becomes larger. We
5238 don't warn about a size change here, because that is
5239 covered by --warn-common. Allow changes between different
5240 function types. */
5241 if (h->root.type == bfd_link_hash_common)
5242 h->size = h->root.u.c.size;
5243
5244 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
5245 && ((definition && !new_weak)
5246 || (old_weak && h->root.type == bfd_link_hash_common)
5247 || h->type == STT_NOTYPE))
5248 {
5249 unsigned int type = ELF_ST_TYPE (isym->st_info);
5250
5251 /* Turn an IFUNC symbol from a DSO into a normal FUNC
5252 symbol. */
5253 if (type == STT_GNU_IFUNC
5254 && (abfd->flags & DYNAMIC) != 0)
5255 type = STT_FUNC;
5256
5257 if (h->type != type)
5258 {
5259 if (h->type != STT_NOTYPE && ! type_change_ok)
5260 /* xgettext:c-format */
5261 _bfd_error_handler
5262 (_("warning: type of symbol `%s' changed"
5263 " from %d to %d in %pB"),
5264 name, h->type, type, abfd);
5265
5266 h->type = type;
5267 }
5268 }
5269
5270 /* Merge st_other field. */
5271 elf_merge_st_other (abfd, h, isym->st_other, sec,
5272 definition, dynamic);
5273
5274 /* We don't want to make debug symbol dynamic. */
5275 if (definition
5276 && (sec->flags & SEC_DEBUGGING)
5277 && !bfd_link_relocatable (info))
5278 dynsym = false;
5279
5280 /* Nor should we make plugin symbols dynamic. */
5281 if ((abfd->flags & BFD_PLUGIN) != 0)
5282 dynsym = false;
5283
5284 if (definition)
5285 {
5286 h->target_internal = isym->st_target_internal;
5287 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5288 }
5289
5290 if (definition && !dynamic)
5291 {
5292 char *p = strchr (name, ELF_VER_CHR);
5293 if (p != NULL && p[1] != ELF_VER_CHR)
5294 {
5295 /* Queue non-default versions so that .symver x, x@FOO
5296 aliases can be checked. */
5297 if (!nondeflt_vers)
5298 {
5299 size_t amt = ((isymend - isym + 1)
5300 * sizeof (struct elf_link_hash_entry *));
5301 nondeflt_vers
5302 = (struct elf_link_hash_entry **) bfd_malloc (amt);
5303 if (!nondeflt_vers)
5304 goto error_free_vers;
5305 }
5306 nondeflt_vers[nondeflt_vers_cnt++] = h;
5307 }
5308 }
5309
5310 if (dynsym && h->dynindx == -1)
5311 {
5312 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5313 goto error_free_vers;
5314 if (h->is_weakalias
5315 && weakdef (h)->dynindx == -1)
5316 {
5317 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
5318 goto error_free_vers;
5319 }
5320 }
5321 else if (h->dynindx != -1)
5322 /* If the symbol already has a dynamic index, but
5323 visibility says it should not be visible, turn it into
5324 a local symbol. */
5325 switch (ELF_ST_VISIBILITY (h->other))
5326 {
5327 case STV_INTERNAL:
5328 case STV_HIDDEN:
5329 (*bed->elf_backend_hide_symbol) (info, h, true);
5330 dynsym = false;
5331 break;
5332 }
5333
5334 if (!add_needed
5335 && matched
5336 && definition
5337 && h->root.type != bfd_link_hash_indirect
5338 && ((dynsym
5339 && h->ref_regular_nonweak)
5340 || (old_bfd != NULL
5341 && (old_bfd->flags & BFD_PLUGIN) != 0
5342 && h->ref_ir_nonweak
5343 && !info->lto_all_symbols_read)
5344 || (h->ref_dynamic_nonweak
5345 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5346 && !on_needed_list (elf_dt_name (abfd),
5347 htab->needed, NULL))))
5348 {
5349 const char *soname = elf_dt_name (abfd);
5350
5351 info->callbacks->minfo ("%!", soname, old_bfd,
5352 h->root.root.string);
5353
5354 /* A symbol from a library loaded via DT_NEEDED of some
5355 other library is referenced by a regular object.
5356 Add a DT_NEEDED entry for it. Issue an error if
5357 --no-add-needed is used and the reference was not
5358 a weak one. */
5359 if (old_bfd != NULL
5360 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
5361 {
5362 _bfd_error_handler
5363 /* xgettext:c-format */
5364 (_("%pB: undefined reference to symbol '%s'"),
5365 old_bfd, name);
5366 bfd_set_error (bfd_error_missing_dso);
5367 goto error_free_vers;
5368 }
5369
5370 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
5371 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
5372
5373 /* Create dynamic sections for backends that require
5374 that be done before setup_gnu_properties. */
5375 if (!_bfd_elf_link_create_dynamic_sections (abfd, info))
5376 return false;
5377 add_needed = true;
5378 }
5379 }
5380 }
5381
5382 if (info->lto_plugin_active
5383 && !bfd_link_relocatable (info)
5384 && (abfd->flags & BFD_PLUGIN) == 0
5385 && !just_syms
5386 && extsymcount)
5387 {
5388 int r_sym_shift;
5389
5390 if (bed->s->arch_size == 32)
5391 r_sym_shift = 8;
5392 else
5393 r_sym_shift = 32;
5394
5395 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5396 referenced in regular objects so that linker plugin will get
5397 the correct symbol resolution. */
5398
5399 sym_hash = elf_sym_hashes (abfd);
5400 for (s = abfd->sections; s != NULL; s = s->next)
5401 {
5402 Elf_Internal_Rela *internal_relocs;
5403 Elf_Internal_Rela *rel, *relend;
5404
5405 /* Don't check relocations in excluded sections. */
5406 if ((s->flags & SEC_RELOC) == 0
5407 || s->reloc_count == 0
5408 || (s->flags & SEC_EXCLUDE) != 0
5409 || ((info->strip == strip_all
5410 || info->strip == strip_debugger)
5411 && (s->flags & SEC_DEBUGGING) != 0))
5412 continue;
5413
5414 internal_relocs = _bfd_elf_link_info_read_relocs (abfd, info,
5415 s, NULL,
5416 NULL,
5417 _bfd_link_keep_memory (info));
5418 if (internal_relocs == NULL)
5419 goto error_free_vers;
5420
5421 rel = internal_relocs;
5422 relend = rel + s->reloc_count;
5423 for ( ; rel < relend; rel++)
5424 {
5425 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5426 struct elf_link_hash_entry *h;
5427
5428 /* Skip local symbols. */
5429 if (r_symndx < extsymoff)
5430 continue;
5431
5432 h = sym_hash[r_symndx - extsymoff];
5433 if (h != NULL)
5434 h->root.non_ir_ref_regular = 1;
5435 }
5436
5437 if (elf_section_data (s)->relocs != internal_relocs)
5438 free (internal_relocs);
5439 }
5440 }
5441
5442 free (extversym);
5443 extversym = NULL;
5444 free (isymbuf);
5445 isymbuf = NULL;
5446
5447 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5448 {
5449 unsigned int i;
5450
5451 /* Restore the symbol table. */
5452 old_ent = (char *) old_tab + tabsize;
5453 memset (elf_sym_hashes (abfd), 0,
5454 extsymcount * sizeof (struct elf_link_hash_entry *));
5455 htab->root.table.table = old_table;
5456 htab->root.table.size = old_size;
5457 htab->root.table.count = old_count;
5458 memcpy (htab->root.table.table, old_tab, tabsize);
5459 htab->root.undefs = old_undefs;
5460 htab->root.undefs_tail = old_undefs_tail;
5461 if (htab->dynstr != NULL)
5462 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5463 free (old_strtab);
5464 old_strtab = NULL;
5465 for (i = 0; i < htab->root.table.size; i++)
5466 {
5467 struct bfd_hash_entry *p;
5468 struct elf_link_hash_entry *h;
5469 unsigned int non_ir_ref_dynamic;
5470
5471 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5472 {
5473 /* Preserve non_ir_ref_dynamic so that this symbol
5474 will be exported when the dynamic lib becomes needed
5475 in the second pass. */
5476 h = (struct elf_link_hash_entry *) p;
5477 if (h->root.type == bfd_link_hash_warning)
5478 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5479 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
5480
5481 h = (struct elf_link_hash_entry *) p;
5482 memcpy (h, old_ent, htab->root.table.entsize);
5483 old_ent = (char *) old_ent + htab->root.table.entsize;
5484 if (h->root.type == bfd_link_hash_warning)
5485 {
5486 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5487 memcpy (h, old_ent, htab->root.table.entsize);
5488 old_ent = (char *) old_ent + htab->root.table.entsize;
5489 }
5490 if (h->root.type == bfd_link_hash_common)
5491 {
5492 memcpy (h->root.u.c.p, old_ent, sizeof (*h->root.u.c.p));
5493 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
5494 }
5495 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
5496 }
5497 }
5498
5499 /* Make a special call to the linker "notice" function to
5500 tell it that symbols added for crefs may need to be removed. */
5501 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
5502 goto error_free_vers;
5503
5504 free (old_tab);
5505 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5506 alloc_mark);
5507 free (nondeflt_vers);
5508 return true;
5509 }
5510
5511 if (old_tab != NULL)
5512 {
5513 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
5514 goto error_free_vers;
5515 free (old_tab);
5516 old_tab = NULL;
5517 }
5518
5519 /* Now that all the symbols from this input file are created, if
5520 not performing a relocatable link, handle .symver foo, foo@BAR
5521 such that any relocs against foo become foo@BAR. */
5522 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
5523 {
5524 size_t cnt, symidx;
5525
5526 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5527 {
5528 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5529 char *shortname, *p;
5530 size_t amt;
5531
5532 p = strchr (h->root.root.string, ELF_VER_CHR);
5533 if (p == NULL
5534 || (h->root.type != bfd_link_hash_defined
5535 && h->root.type != bfd_link_hash_defweak))
5536 continue;
5537
5538 amt = p - h->root.root.string;
5539 shortname = (char *) bfd_malloc (amt + 1);
5540 if (!shortname)
5541 goto error_free_vers;
5542 memcpy (shortname, h->root.root.string, amt);
5543 shortname[amt] = '\0';
5544
5545 hi = (struct elf_link_hash_entry *)
5546 bfd_link_hash_lookup (&htab->root, shortname,
5547 false, false, false);
5548 if (hi != NULL
5549 && hi->root.type == h->root.type
5550 && hi->root.u.def.value == h->root.u.def.value
5551 && hi->root.u.def.section == h->root.u.def.section)
5552 {
5553 (*bed->elf_backend_hide_symbol) (info, hi, true);
5554 hi->root.type = bfd_link_hash_indirect;
5555 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
5556 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
5557 sym_hash = elf_sym_hashes (abfd);
5558 if (sym_hash)
5559 for (symidx = 0; symidx < extsymcount; ++symidx)
5560 if (sym_hash[symidx] == hi)
5561 {
5562 sym_hash[symidx] = h;
5563 break;
5564 }
5565 }
5566 free (shortname);
5567 }
5568 free (nondeflt_vers);
5569 nondeflt_vers = NULL;
5570 }
5571
5572 /* Now set the alias field correctly for all the weak defined
5573 symbols we found. The only way to do this is to search all the
5574 symbols. Since we only need the information for non functions in
5575 dynamic objects, that's the only time we actually put anything on
5576 the list WEAKS. We need this information so that if a regular
5577 object refers to a symbol defined weakly in a dynamic object, the
5578 real symbol in the dynamic object is also put in the dynamic
5579 symbols; we also must arrange for both symbols to point to the
5580 same memory location. We could handle the general case of symbol
5581 aliasing, but a general symbol alias can only be generated in
5582 assembler code, handling it correctly would be very time
5583 consuming, and other ELF linkers don't handle general aliasing
5584 either. */
5585 if (weaks != NULL)
5586 {
5587 struct elf_link_hash_entry **hpp;
5588 struct elf_link_hash_entry **hppend;
5589 struct elf_link_hash_entry **sorted_sym_hash;
5590 struct elf_link_hash_entry *h;
5591 size_t sym_count, amt;
5592
5593 /* Since we have to search the whole symbol list for each weak
5594 defined symbol, search time for N weak defined symbols will be
5595 O(N^2). Binary search will cut it down to O(NlogN). */
5596 amt = extsymcount * sizeof (*sorted_sym_hash);
5597 sorted_sym_hash = bfd_malloc (amt);
5598 if (sorted_sym_hash == NULL)
5599 goto error_return;
5600 sym_hash = sorted_sym_hash;
5601 hpp = elf_sym_hashes (abfd);
5602 hppend = hpp + extsymcount;
5603 sym_count = 0;
5604 for (; hpp < hppend; hpp++)
5605 {
5606 h = *hpp;
5607 if (h != NULL
5608 && h->root.type == bfd_link_hash_defined
5609 && !bed->is_function_type (h->type))
5610 {
5611 *sym_hash = h;
5612 sym_hash++;
5613 sym_count++;
5614 }
5615 }
5616
5617 qsort (sorted_sym_hash, sym_count, sizeof (*sorted_sym_hash),
5618 elf_sort_symbol);
5619
5620 while (weaks != NULL)
5621 {
5622 struct elf_link_hash_entry *hlook;
5623 asection *slook;
5624 bfd_vma vlook;
5625 size_t i, j, idx = 0;
5626
5627 hlook = weaks;
5628 weaks = hlook->u.alias;
5629 hlook->u.alias = NULL;
5630
5631 if (hlook->root.type != bfd_link_hash_defined
5632 && hlook->root.type != bfd_link_hash_defweak)
5633 continue;
5634
5635 slook = hlook->root.u.def.section;
5636 vlook = hlook->root.u.def.value;
5637
5638 i = 0;
5639 j = sym_count;
5640 while (i != j)
5641 {
5642 bfd_signed_vma vdiff;
5643 idx = (i + j) / 2;
5644 h = sorted_sym_hash[idx];
5645 vdiff = vlook - h->root.u.def.value;
5646 if (vdiff < 0)
5647 j = idx;
5648 else if (vdiff > 0)
5649 i = idx + 1;
5650 else
5651 {
5652 int sdiff = slook->id - h->root.u.def.section->id;
5653 if (sdiff < 0)
5654 j = idx;
5655 else if (sdiff > 0)
5656 i = idx + 1;
5657 else
5658 break;
5659 }
5660 }
5661
5662 /* We didn't find a value/section match. */
5663 if (i == j)
5664 continue;
5665
5666 /* With multiple aliases, or when the weak symbol is already
5667 strongly defined, we have multiple matching symbols and
5668 the binary search above may land on any of them. Step
5669 one past the matching symbol(s). */
5670 while (++idx != j)
5671 {
5672 h = sorted_sym_hash[idx];
5673 if (h->root.u.def.section != slook
5674 || h->root.u.def.value != vlook)
5675 break;
5676 }
5677
5678 /* Now look back over the aliases. Since we sorted by size
5679 as well as value and section, we'll choose the one with
5680 the largest size. */
5681 while (idx-- != i)
5682 {
5683 h = sorted_sym_hash[idx];
5684
5685 /* Stop if value or section doesn't match. */
5686 if (h->root.u.def.section != slook
5687 || h->root.u.def.value != vlook)
5688 break;
5689 else if (h != hlook)
5690 {
5691 struct elf_link_hash_entry *t;
5692
5693 hlook->u.alias = h;
5694 hlook->is_weakalias = 1;
5695 t = h;
5696 if (t->u.alias != NULL)
5697 while (t->u.alias != h)
5698 t = t->u.alias;
5699 t->u.alias = hlook;
5700
5701 /* If the weak definition is in the list of dynamic
5702 symbols, make sure the real definition is put
5703 there as well. */
5704 if (hlook->dynindx != -1 && h->dynindx == -1)
5705 {
5706 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5707 {
5708 err_free_sym_hash:
5709 free (sorted_sym_hash);
5710 goto error_return;
5711 }
5712 }
5713
5714 /* If the real definition is in the list of dynamic
5715 symbols, make sure the weak definition is put
5716 there as well. If we don't do this, then the
5717 dynamic loader might not merge the entries for the
5718 real definition and the weak definition. */
5719 if (h->dynindx != -1 && hlook->dynindx == -1)
5720 {
5721 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
5722 goto err_free_sym_hash;
5723 }
5724 break;
5725 }
5726 }
5727 }
5728
5729 free (sorted_sym_hash);
5730 }
5731
5732 if (bed->check_directives
5733 && !(*bed->check_directives) (abfd, info))
5734 return false;
5735
5736 /* If this is a non-traditional link, try to optimize the handling
5737 of the .stab/.stabstr sections. */
5738 if (! dynamic
5739 && ! info->traditional_format
5740 && is_elf_hash_table (&htab->root)
5741 && (info->strip != strip_all && info->strip != strip_debugger))
5742 {
5743 asection *stabstr;
5744
5745 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5746 if (stabstr != NULL)
5747 {
5748 bfd_size_type string_offset = 0;
5749 asection *stab;
5750
5751 for (stab = abfd->sections; stab; stab = stab->next)
5752 if (startswith (stab->name, ".stab")
5753 && (!stab->name[5] ||
5754 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5755 && (stab->flags & SEC_MERGE) == 0
5756 && !bfd_is_abs_section (stab->output_section))
5757 {
5758 struct bfd_elf_section_data *secdata;
5759
5760 secdata = elf_section_data (stab);
5761 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5762 stabstr, &secdata->sec_info,
5763 &string_offset))
5764 goto error_return;
5765 if (secdata->sec_info)
5766 stab->sec_info_type = SEC_INFO_TYPE_STABS;
5767 }
5768 }
5769 }
5770
5771 if (dynamic && add_needed)
5772 {
5773 /* Add this bfd to the loaded list. */
5774 struct elf_link_loaded_list *n;
5775
5776 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5777 if (n == NULL)
5778 goto error_return;
5779 n->abfd = abfd;
5780 n->next = htab->dyn_loaded;
5781 htab->dyn_loaded = n;
5782 }
5783 if (dynamic && !add_needed
5784 && (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) != 0)
5785 elf_dyn_lib_class (abfd) |= DYN_NO_NEEDED;
5786
5787 return true;
5788
5789 error_free_vers:
5790 free (old_tab);
5791 free (old_strtab);
5792 free (nondeflt_vers);
5793 free (extversym);
5794 error_free_sym:
5795 free (isymbuf);
5796 error_return:
5797 return false;
5798 }
5799
5800 /* Return the linker hash table entry of a symbol that might be
5801 satisfied by an archive symbol. Return -1 on error. */
5802
5803 struct bfd_link_hash_entry *
5804 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5805 struct bfd_link_info *info,
5806 const char *name)
5807 {
5808 struct bfd_link_hash_entry *h;
5809 char *p, *copy;
5810 size_t len, first;
5811
5812 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
5813 if (h != NULL)
5814 return h;
5815
5816 /* If this is a default version (the name contains @@), look up the
5817 symbol again with only one `@' as well as without the version.
5818 The effect is that references to the symbol with and without the
5819 version will be matched by the default symbol in the archive. */
5820
5821 p = strchr (name, ELF_VER_CHR);
5822 if (p == NULL || p[1] != ELF_VER_CHR)
5823 return h;
5824
5825 /* First check with only one `@'. */
5826 len = strlen (name);
5827 copy = (char *) bfd_alloc (abfd, len);
5828 if (copy == NULL)
5829 return (struct bfd_link_hash_entry *) -1;
5830
5831 first = p - name + 1;
5832 memcpy (copy, name, first);
5833 memcpy (copy + first, name + first + 1, len - first);
5834
5835 h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
5836 if (h == NULL)
5837 {
5838 /* We also need to check references to the symbol without the
5839 version. */
5840 copy[first - 1] = '\0';
5841 h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
5842 }
5843
5844 bfd_release (abfd, copy);
5845 return h;
5846 }
5847
5848 /* Add symbols from an ELF archive file to the linker hash table. We
5849 don't use _bfd_generic_link_add_archive_symbols because we need to
5850 handle versioned symbols.
5851
5852 Fortunately, ELF archive handling is simpler than that done by
5853 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5854 oddities. In ELF, if we find a symbol in the archive map, and the
5855 symbol is currently undefined, we know that we must pull in that
5856 object file.
5857
5858 Unfortunately, we do have to make multiple passes over the symbol
5859 table until nothing further is resolved. */
5860
5861 static bool
5862 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5863 {
5864 symindex c;
5865 unsigned char *included = NULL;
5866 carsym *symdefs;
5867 bool loop;
5868 size_t amt;
5869 const struct elf_backend_data *bed;
5870 struct bfd_link_hash_entry * (*archive_symbol_lookup)
5871 (bfd *, struct bfd_link_info *, const char *);
5872
5873 if (! bfd_has_map (abfd))
5874 {
5875 /* An empty archive is a special case. */
5876 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5877 return true;
5878 bfd_set_error (bfd_error_no_armap);
5879 return false;
5880 }
5881
5882 /* Keep track of all symbols we know to be already defined, and all
5883 files we know to be already included. This is to speed up the
5884 second and subsequent passes. */
5885 c = bfd_ardata (abfd)->symdef_count;
5886 if (c == 0)
5887 return true;
5888 amt = c * sizeof (*included);
5889 included = (unsigned char *) bfd_zmalloc (amt);
5890 if (included == NULL)
5891 return false;
5892
5893 symdefs = bfd_ardata (abfd)->symdefs;
5894 bed = get_elf_backend_data (abfd);
5895 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5896
5897 do
5898 {
5899 file_ptr last;
5900 symindex i;
5901 carsym *symdef;
5902 carsym *symdefend;
5903
5904 loop = false;
5905 last = -1;
5906
5907 symdef = symdefs;
5908 symdefend = symdef + c;
5909 for (i = 0; symdef < symdefend; symdef++, i++)
5910 {
5911 struct bfd_link_hash_entry *h;
5912 bfd *element;
5913 struct bfd_link_hash_entry *undefs_tail;
5914 symindex mark;
5915
5916 if (included[i])
5917 continue;
5918 if (symdef->file_offset == last)
5919 {
5920 included[i] = true;
5921 continue;
5922 }
5923
5924 h = archive_symbol_lookup (abfd, info, symdef->name);
5925 if (h == (struct bfd_link_hash_entry *) -1)
5926 goto error_return;
5927
5928 if (h == NULL)
5929 continue;
5930
5931 if (h->type == bfd_link_hash_undefined)
5932 {
5933 /* If the archive element has already been loaded then one
5934 of the symbols defined by that element might have been
5935 made undefined due to being in a discarded section. */
5936 if (is_elf_hash_table (info->hash)
5937 && ((struct elf_link_hash_entry *) h)->indx == -3)
5938 continue;
5939 }
5940 else if (h->type == bfd_link_hash_common)
5941 {
5942 /* We currently have a common symbol. The archive map contains
5943 a reference to this symbol, so we may want to include it. We
5944 only want to include it however, if this archive element
5945 contains a definition of the symbol, not just another common
5946 declaration of it.
5947
5948 Unfortunately some archivers (including GNU ar) will put
5949 declarations of common symbols into their archive maps, as
5950 well as real definitions, so we cannot just go by the archive
5951 map alone. Instead we must read in the element's symbol
5952 table and check that to see what kind of symbol definition
5953 this is. */
5954 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5955 continue;
5956 }
5957 else
5958 {
5959 if (h->type != bfd_link_hash_undefweak)
5960 /* Symbol must be defined. Don't check it again. */
5961 included[i] = true;
5962 continue;
5963 }
5964
5965 /* We need to include this archive member. */
5966 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset,
5967 info);
5968 if (element == NULL)
5969 goto error_return;
5970
5971 if (! bfd_check_format (element, bfd_object))
5972 goto error_return;
5973
5974 undefs_tail = info->hash->undefs_tail;
5975
5976 if (!(*info->callbacks
5977 ->add_archive_element) (info, element, symdef->name, &element))
5978 continue;
5979 if (!bfd_link_add_symbols (element, info))
5980 goto error_return;
5981
5982 /* If there are any new undefined symbols, we need to make
5983 another pass through the archive in order to see whether
5984 they can be defined. FIXME: This isn't perfect, because
5985 common symbols wind up on undefs_tail and because an
5986 undefined symbol which is defined later on in this pass
5987 does not require another pass. This isn't a bug, but it
5988 does make the code less efficient than it could be. */
5989 if (undefs_tail != info->hash->undefs_tail)
5990 loop = true;
5991
5992 /* Look backward to mark all symbols from this object file
5993 which we have already seen in this pass. */
5994 mark = i;
5995 do
5996 {
5997 included[mark] = true;
5998 if (mark == 0)
5999 break;
6000 --mark;
6001 }
6002 while (symdefs[mark].file_offset == symdef->file_offset);
6003
6004 /* We mark subsequent symbols from this object file as we go
6005 on through the loop. */
6006 last = symdef->file_offset;
6007 }
6008 }
6009 while (loop);
6010
6011 free (included);
6012 return true;
6013
6014 error_return:
6015 free (included);
6016 return false;
6017 }
6018
6019 /* Given an ELF BFD, add symbols to the global hash table as
6020 appropriate. */
6021
6022 bool
6023 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
6024 {
6025 switch (bfd_get_format (abfd))
6026 {
6027 case bfd_object:
6028 return elf_link_add_object_symbols (abfd, info);
6029 case bfd_archive:
6030 return elf_link_add_archive_symbols (abfd, info);
6031 default:
6032 bfd_set_error (bfd_error_wrong_format);
6033 return false;
6034 }
6035 }
6036 \f
6037 struct hash_codes_info
6038 {
6039 unsigned long *hashcodes;
6040 bool error;
6041 };
6042
6043 /* This function will be called though elf_link_hash_traverse to store
6044 all hash value of the exported symbols in an array. */
6045
6046 static bool
6047 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
6048 {
6049 struct hash_codes_info *inf = (struct hash_codes_info *) data;
6050 const char *name;
6051 unsigned long ha;
6052 char *alc = NULL;
6053
6054 /* Ignore indirect symbols. These are added by the versioning code. */
6055 if (h->dynindx == -1)
6056 return true;
6057
6058 name = h->root.root.string;
6059 if (h->versioned >= versioned)
6060 {
6061 char *p = strchr (name, ELF_VER_CHR);
6062 if (p != NULL)
6063 {
6064 alc = (char *) bfd_malloc (p - name + 1);
6065 if (alc == NULL)
6066 {
6067 inf->error = true;
6068 return false;
6069 }
6070 memcpy (alc, name, p - name);
6071 alc[p - name] = '\0';
6072 name = alc;
6073 }
6074 }
6075
6076 /* Compute the hash value. */
6077 ha = bfd_elf_hash (name);
6078
6079 /* Store the found hash value in the array given as the argument. */
6080 *(inf->hashcodes)++ = ha;
6081
6082 /* And store it in the struct so that we can put it in the hash table
6083 later. */
6084 h->u.elf_hash_value = ha;
6085
6086 free (alc);
6087 return true;
6088 }
6089
6090 struct collect_gnu_hash_codes
6091 {
6092 bfd *output_bfd;
6093 const struct elf_backend_data *bed;
6094 unsigned long int nsyms;
6095 unsigned long int maskbits;
6096 unsigned long int *hashcodes;
6097 unsigned long int *hashval;
6098 unsigned long int *indx;
6099 unsigned long int *counts;
6100 bfd_vma *bitmask;
6101 bfd_byte *contents;
6102 bfd_size_type xlat;
6103 long int min_dynindx;
6104 unsigned long int bucketcount;
6105 unsigned long int symindx;
6106 long int local_indx;
6107 long int shift1, shift2;
6108 unsigned long int mask;
6109 bool error;
6110 };
6111
6112 /* This function will be called though elf_link_hash_traverse to store
6113 all hash value of the exported symbols in an array. */
6114
6115 static bool
6116 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
6117 {
6118 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6119 const char *name;
6120 unsigned long ha;
6121 char *alc = NULL;
6122
6123 /* Ignore indirect symbols. These are added by the versioning code. */
6124 if (h->dynindx == -1)
6125 return true;
6126
6127 /* Ignore also local symbols and undefined symbols. */
6128 if (! (*s->bed->elf_hash_symbol) (h))
6129 return true;
6130
6131 name = h->root.root.string;
6132 if (h->versioned >= versioned)
6133 {
6134 char *p = strchr (name, ELF_VER_CHR);
6135 if (p != NULL)
6136 {
6137 alc = (char *) bfd_malloc (p - name + 1);
6138 if (alc == NULL)
6139 {
6140 s->error = true;
6141 return false;
6142 }
6143 memcpy (alc, name, p - name);
6144 alc[p - name] = '\0';
6145 name = alc;
6146 }
6147 }
6148
6149 /* Compute the hash value. */
6150 ha = bfd_elf_gnu_hash (name);
6151
6152 /* Store the found hash value in the array for compute_bucket_count,
6153 and also for .dynsym reordering purposes. */
6154 s->hashcodes[s->nsyms] = ha;
6155 s->hashval[h->dynindx] = ha;
6156 ++s->nsyms;
6157 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
6158 s->min_dynindx = h->dynindx;
6159
6160 free (alc);
6161 return true;
6162 }
6163
6164 /* This function will be called though elf_link_hash_traverse to do
6165 final dynamic symbol renumbering in case of .gnu.hash.
6166 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6167 to the translation table. */
6168
6169 static bool
6170 elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data)
6171 {
6172 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6173 unsigned long int bucket;
6174 unsigned long int val;
6175
6176 /* Ignore indirect symbols. */
6177 if (h->dynindx == -1)
6178 return true;
6179
6180 /* Ignore also local symbols and undefined symbols. */
6181 if (! (*s->bed->elf_hash_symbol) (h))
6182 {
6183 if (h->dynindx >= s->min_dynindx)
6184 {
6185 if (s->bed->record_xhash_symbol != NULL)
6186 {
6187 (*s->bed->record_xhash_symbol) (h, 0);
6188 s->local_indx++;
6189 }
6190 else
6191 h->dynindx = s->local_indx++;
6192 }
6193 return true;
6194 }
6195
6196 bucket = s->hashval[h->dynindx] % s->bucketcount;
6197 val = (s->hashval[h->dynindx] >> s->shift1)
6198 & ((s->maskbits >> s->shift1) - 1);
6199 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
6200 s->bitmask[val]
6201 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
6202 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
6203 if (s->counts[bucket] == 1)
6204 /* Last element terminates the chain. */
6205 val |= 1;
6206 bfd_put_32 (s->output_bfd, val,
6207 s->contents + (s->indx[bucket] - s->symindx) * 4);
6208 --s->counts[bucket];
6209 if (s->bed->record_xhash_symbol != NULL)
6210 {
6211 bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4;
6212
6213 (*s->bed->record_xhash_symbol) (h, xlat_loc);
6214 }
6215 else
6216 h->dynindx = s->indx[bucket]++;
6217 return true;
6218 }
6219
6220 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6221
6222 bool
6223 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
6224 {
6225 return !(h->forced_local
6226 || h->root.type == bfd_link_hash_undefined
6227 || h->root.type == bfd_link_hash_undefweak
6228 || ((h->root.type == bfd_link_hash_defined
6229 || h->root.type == bfd_link_hash_defweak)
6230 && h->root.u.def.section->output_section == NULL));
6231 }
6232
6233 /* Array used to determine the number of hash table buckets to use
6234 based on the number of symbols there are. If there are fewer than
6235 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6236 fewer than 37 we use 17 buckets, and so forth. We never use more
6237 than 32771 buckets. */
6238
6239 static const size_t elf_buckets[] =
6240 {
6241 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6242 16411, 32771, 0
6243 };
6244
6245 /* Compute bucket count for hashing table. We do not use a static set
6246 of possible tables sizes anymore. Instead we determine for all
6247 possible reasonable sizes of the table the outcome (i.e., the
6248 number of collisions etc) and choose the best solution. The
6249 weighting functions are not too simple to allow the table to grow
6250 without bounds. Instead one of the weighting factors is the size.
6251 Therefore the result is always a good payoff between few collisions
6252 (= short chain lengths) and table size. */
6253 static size_t
6254 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
6255 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
6256 unsigned long int nsyms,
6257 int gnu_hash)
6258 {
6259 size_t best_size = 0;
6260 unsigned long int i;
6261
6262 /* We have a problem here. The following code to optimize the table
6263 size requires an integer type with more the 32 bits. If
6264 BFD_HOST_U_64_BIT is set we know about such a type. */
6265 #ifdef BFD_HOST_U_64_BIT
6266 if (info->optimize)
6267 {
6268 size_t minsize;
6269 size_t maxsize;
6270 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
6271 bfd *dynobj = elf_hash_table (info)->dynobj;
6272 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
6273 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
6274 unsigned long int *counts;
6275 bfd_size_type amt;
6276 unsigned int no_improvement_count = 0;
6277
6278 /* Possible optimization parameters: if we have NSYMS symbols we say
6279 that the hashing table must at least have NSYMS/4 and at most
6280 2*NSYMS buckets. */
6281 minsize = nsyms / 4;
6282 if (minsize == 0)
6283 minsize = 1;
6284 best_size = maxsize = nsyms * 2;
6285 if (gnu_hash)
6286 {
6287 if (minsize < 2)
6288 minsize = 2;
6289 if ((best_size & 31) == 0)
6290 ++best_size;
6291 }
6292
6293 /* Create array where we count the collisions in. We must use bfd_malloc
6294 since the size could be large. */
6295 amt = maxsize;
6296 amt *= sizeof (unsigned long int);
6297 counts = (unsigned long int *) bfd_malloc (amt);
6298 if (counts == NULL)
6299 return 0;
6300
6301 /* Compute the "optimal" size for the hash table. The criteria is a
6302 minimal chain length. The minor criteria is (of course) the size
6303 of the table. */
6304 for (i = minsize; i < maxsize; ++i)
6305 {
6306 /* Walk through the array of hashcodes and count the collisions. */
6307 BFD_HOST_U_64_BIT max;
6308 unsigned long int j;
6309 unsigned long int fact;
6310
6311 if (gnu_hash && (i & 31) == 0)
6312 continue;
6313
6314 memset (counts, '\0', i * sizeof (unsigned long int));
6315
6316 /* Determine how often each hash bucket is used. */
6317 for (j = 0; j < nsyms; ++j)
6318 ++counts[hashcodes[j] % i];
6319
6320 /* For the weight function we need some information about the
6321 pagesize on the target. This is information need not be 100%
6322 accurate. Since this information is not available (so far) we
6323 define it here to a reasonable default value. If it is crucial
6324 to have a better value some day simply define this value. */
6325 # ifndef BFD_TARGET_PAGESIZE
6326 # define BFD_TARGET_PAGESIZE (4096)
6327 # endif
6328
6329 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6330 and the chains. */
6331 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
6332
6333 # if 1
6334 /* Variant 1: optimize for short chains. We add the squares
6335 of all the chain lengths (which favors many small chain
6336 over a few long chains). */
6337 for (j = 0; j < i; ++j)
6338 max += counts[j] * counts[j];
6339
6340 /* This adds penalties for the overall size of the table. */
6341 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6342 max *= fact * fact;
6343 # else
6344 /* Variant 2: Optimize a lot more for small table. Here we
6345 also add squares of the size but we also add penalties for
6346 empty slots (the +1 term). */
6347 for (j = 0; j < i; ++j)
6348 max += (1 + counts[j]) * (1 + counts[j]);
6349
6350 /* The overall size of the table is considered, but not as
6351 strong as in variant 1, where it is squared. */
6352 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6353 max *= fact;
6354 # endif
6355
6356 /* Compare with current best results. */
6357 if (max < best_chlen)
6358 {
6359 best_chlen = max;
6360 best_size = i;
6361 no_improvement_count = 0;
6362 }
6363 /* PR 11843: Avoid futile long searches for the best bucket size
6364 when there are a large number of symbols. */
6365 else if (++no_improvement_count == 100)
6366 break;
6367 }
6368
6369 free (counts);
6370 }
6371 else
6372 #endif /* defined (BFD_HOST_U_64_BIT) */
6373 {
6374 /* This is the fallback solution if no 64bit type is available or if we
6375 are not supposed to spend much time on optimizations. We select the
6376 bucket count using a fixed set of numbers. */
6377 for (i = 0; elf_buckets[i] != 0; i++)
6378 {
6379 best_size = elf_buckets[i];
6380 if (nsyms < elf_buckets[i + 1])
6381 break;
6382 }
6383 if (gnu_hash && best_size < 2)
6384 best_size = 2;
6385 }
6386
6387 return best_size;
6388 }
6389
6390 /* Size any SHT_GROUP section for ld -r. */
6391
6392 bool
6393 _bfd_elf_size_group_sections (struct bfd_link_info *info)
6394 {
6395 bfd *ibfd;
6396 asection *s;
6397
6398 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6399 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6400 && (s = ibfd->sections) != NULL
6401 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
6402 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6403 return false;
6404 return true;
6405 }
6406
6407 /* Set a default stack segment size. The value in INFO wins. If it
6408 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6409 undefined it is initialized. */
6410
6411 bool
6412 bfd_elf_stack_segment_size (bfd *output_bfd,
6413 struct bfd_link_info *info,
6414 const char *legacy_symbol,
6415 bfd_vma default_size)
6416 {
6417 struct elf_link_hash_entry *h = NULL;
6418
6419 /* Look for legacy symbol. */
6420 if (legacy_symbol)
6421 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6422 false, false, false);
6423 if (h && (h->root.type == bfd_link_hash_defined
6424 || h->root.type == bfd_link_hash_defweak)
6425 && h->def_regular
6426 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6427 {
6428 /* The symbol has no type if specified on the command line. */
6429 h->type = STT_OBJECT;
6430 if (info->stacksize)
6431 /* xgettext:c-format */
6432 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6433 output_bfd, legacy_symbol);
6434 else if (h->root.u.def.section != bfd_abs_section_ptr)
6435 /* xgettext:c-format */
6436 _bfd_error_handler (_("%pB: %s not absolute"),
6437 output_bfd, legacy_symbol);
6438 else
6439 info->stacksize = h->root.u.def.value;
6440 }
6441
6442 if (!info->stacksize)
6443 /* If the user didn't set a size, or explicitly inhibit the
6444 size, set it now. */
6445 info->stacksize = default_size;
6446
6447 /* Provide the legacy symbol, if it is referenced. */
6448 if (h && (h->root.type == bfd_link_hash_undefined
6449 || h->root.type == bfd_link_hash_undefweak))
6450 {
6451 struct bfd_link_hash_entry *bh = NULL;
6452
6453 if (!(_bfd_generic_link_add_one_symbol
6454 (info, output_bfd, legacy_symbol,
6455 BSF_GLOBAL, bfd_abs_section_ptr,
6456 info->stacksize >= 0 ? info->stacksize : 0,
6457 NULL, false, get_elf_backend_data (output_bfd)->collect, &bh)))
6458 return false;
6459
6460 h = (struct elf_link_hash_entry *) bh;
6461 h->def_regular = 1;
6462 h->type = STT_OBJECT;
6463 }
6464
6465 return true;
6466 }
6467
6468 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6469
6470 struct elf_gc_sweep_symbol_info
6471 {
6472 struct bfd_link_info *info;
6473 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6474 bool);
6475 };
6476
6477 static bool
6478 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6479 {
6480 if (!h->mark
6481 && (((h->root.type == bfd_link_hash_defined
6482 || h->root.type == bfd_link_hash_defweak)
6483 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6484 && h->root.u.def.section->gc_mark))
6485 || h->root.type == bfd_link_hash_undefined
6486 || h->root.type == bfd_link_hash_undefweak))
6487 {
6488 struct elf_gc_sweep_symbol_info *inf;
6489
6490 inf = (struct elf_gc_sweep_symbol_info *) data;
6491 (*inf->hide_symbol) (inf->info, h, true);
6492 h->def_regular = 0;
6493 h->ref_regular = 0;
6494 h->ref_regular_nonweak = 0;
6495 }
6496
6497 return true;
6498 }
6499
6500 /* Set up the sizes and contents of the ELF dynamic sections. This is
6501 called by the ELF linker emulation before_allocation routine. We
6502 must set the sizes of the sections before the linker sets the
6503 addresses of the various sections. */
6504
6505 bool
6506 bfd_elf_size_dynamic_sections (bfd *output_bfd,
6507 const char *soname,
6508 const char *rpath,
6509 const char *filter_shlib,
6510 const char *audit,
6511 const char *depaudit,
6512 const char * const *auxiliary_filters,
6513 struct bfd_link_info *info,
6514 asection **sinterpptr)
6515 {
6516 bfd *dynobj;
6517 const struct elf_backend_data *bed;
6518
6519 *sinterpptr = NULL;
6520
6521 if (!is_elf_hash_table (info->hash))
6522 return true;
6523
6524 /* Any syms created from now on start with -1 in
6525 got.refcount/offset and plt.refcount/offset. */
6526 elf_hash_table (info)->init_got_refcount
6527 = elf_hash_table (info)->init_got_offset;
6528 elf_hash_table (info)->init_plt_refcount
6529 = elf_hash_table (info)->init_plt_offset;
6530
6531 bed = get_elf_backend_data (output_bfd);
6532
6533 /* The backend may have to create some sections regardless of whether
6534 we're dynamic or not. */
6535 if (bed->elf_backend_always_size_sections
6536 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6537 return false;
6538
6539 dynobj = elf_hash_table (info)->dynobj;
6540
6541 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6542 {
6543 struct bfd_elf_version_tree *verdefs;
6544 struct elf_info_failed asvinfo;
6545 struct bfd_elf_version_tree *t;
6546 struct bfd_elf_version_expr *d;
6547 asection *s;
6548 size_t soname_indx;
6549
6550 /* If we are supposed to export all symbols into the dynamic symbol
6551 table (this is not the normal case), then do so. */
6552 if (info->export_dynamic
6553 || (bfd_link_executable (info) && info->dynamic))
6554 {
6555 struct elf_info_failed eif;
6556
6557 eif.info = info;
6558 eif.failed = false;
6559 elf_link_hash_traverse (elf_hash_table (info),
6560 _bfd_elf_export_symbol,
6561 &eif);
6562 if (eif.failed)
6563 return false;
6564 }
6565
6566 if (soname != NULL)
6567 {
6568 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6569 soname, true);
6570 if (soname_indx == (size_t) -1
6571 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6572 return false;
6573 }
6574 else
6575 soname_indx = (size_t) -1;
6576
6577 /* Make all global versions with definition. */
6578 for (t = info->version_info; t != NULL; t = t->next)
6579 for (d = t->globals.list; d != NULL; d = d->next)
6580 if (!d->symver && d->literal)
6581 {
6582 const char *verstr, *name;
6583 size_t namelen, verlen, newlen;
6584 char *newname, *p, leading_char;
6585 struct elf_link_hash_entry *newh;
6586
6587 leading_char = bfd_get_symbol_leading_char (output_bfd);
6588 name = d->pattern;
6589 namelen = strlen (name) + (leading_char != '\0');
6590 verstr = t->name;
6591 verlen = strlen (verstr);
6592 newlen = namelen + verlen + 3;
6593
6594 newname = (char *) bfd_malloc (newlen);
6595 if (newname == NULL)
6596 return false;
6597 newname[0] = leading_char;
6598 memcpy (newname + (leading_char != '\0'), name, namelen);
6599
6600 /* Check the hidden versioned definition. */
6601 p = newname + namelen;
6602 *p++ = ELF_VER_CHR;
6603 memcpy (p, verstr, verlen + 1);
6604 newh = elf_link_hash_lookup (elf_hash_table (info),
6605 newname, false, false,
6606 false);
6607 if (newh == NULL
6608 || (newh->root.type != bfd_link_hash_defined
6609 && newh->root.type != bfd_link_hash_defweak))
6610 {
6611 /* Check the default versioned definition. */
6612 *p++ = ELF_VER_CHR;
6613 memcpy (p, verstr, verlen + 1);
6614 newh = elf_link_hash_lookup (elf_hash_table (info),
6615 newname, false, false,
6616 false);
6617 }
6618 free (newname);
6619
6620 /* Mark this version if there is a definition and it is
6621 not defined in a shared object. */
6622 if (newh != NULL
6623 && !newh->def_dynamic
6624 && (newh->root.type == bfd_link_hash_defined
6625 || newh->root.type == bfd_link_hash_defweak))
6626 d->symver = 1;
6627 }
6628
6629 /* Attach all the symbols to their version information. */
6630 asvinfo.info = info;
6631 asvinfo.failed = false;
6632
6633 elf_link_hash_traverse (elf_hash_table (info),
6634 _bfd_elf_link_assign_sym_version,
6635 &asvinfo);
6636 if (asvinfo.failed)
6637 return false;
6638
6639 if (!info->allow_undefined_version)
6640 {
6641 /* Check if all global versions have a definition. */
6642 bool all_defined = true;
6643 for (t = info->version_info; t != NULL; t = t->next)
6644 for (d = t->globals.list; d != NULL; d = d->next)
6645 if (d->literal && !d->symver && !d->script)
6646 {
6647 _bfd_error_handler
6648 (_("%s: undefined version: %s"),
6649 d->pattern, t->name);
6650 all_defined = false;
6651 }
6652
6653 if (!all_defined)
6654 {
6655 bfd_set_error (bfd_error_bad_value);
6656 return false;
6657 }
6658 }
6659
6660 /* Set up the version definition section. */
6661 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6662 BFD_ASSERT (s != NULL);
6663
6664 /* We may have created additional version definitions if we are
6665 just linking a regular application. */
6666 verdefs = info->version_info;
6667
6668 /* Skip anonymous version tag. */
6669 if (verdefs != NULL && verdefs->vernum == 0)
6670 verdefs = verdefs->next;
6671
6672 if (verdefs == NULL && !info->create_default_symver)
6673 s->flags |= SEC_EXCLUDE;
6674 else
6675 {
6676 unsigned int cdefs;
6677 bfd_size_type size;
6678 bfd_byte *p;
6679 Elf_Internal_Verdef def;
6680 Elf_Internal_Verdaux defaux;
6681 struct bfd_link_hash_entry *bh;
6682 struct elf_link_hash_entry *h;
6683 const char *name;
6684
6685 cdefs = 0;
6686 size = 0;
6687
6688 /* Make space for the base version. */
6689 size += sizeof (Elf_External_Verdef);
6690 size += sizeof (Elf_External_Verdaux);
6691 ++cdefs;
6692
6693 /* Make space for the default version. */
6694 if (info->create_default_symver)
6695 {
6696 size += sizeof (Elf_External_Verdef);
6697 ++cdefs;
6698 }
6699
6700 for (t = verdefs; t != NULL; t = t->next)
6701 {
6702 struct bfd_elf_version_deps *n;
6703
6704 /* Don't emit base version twice. */
6705 if (t->vernum == 0)
6706 continue;
6707
6708 size += sizeof (Elf_External_Verdef);
6709 size += sizeof (Elf_External_Verdaux);
6710 ++cdefs;
6711
6712 for (n = t->deps; n != NULL; n = n->next)
6713 size += sizeof (Elf_External_Verdaux);
6714 }
6715
6716 s->size = size;
6717 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6718 if (s->contents == NULL && s->size != 0)
6719 return false;
6720
6721 /* Fill in the version definition section. */
6722
6723 p = s->contents;
6724
6725 def.vd_version = VER_DEF_CURRENT;
6726 def.vd_flags = VER_FLG_BASE;
6727 def.vd_ndx = 1;
6728 def.vd_cnt = 1;
6729 if (info->create_default_symver)
6730 {
6731 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6732 def.vd_next = sizeof (Elf_External_Verdef);
6733 }
6734 else
6735 {
6736 def.vd_aux = sizeof (Elf_External_Verdef);
6737 def.vd_next = (sizeof (Elf_External_Verdef)
6738 + sizeof (Elf_External_Verdaux));
6739 }
6740
6741 if (soname_indx != (size_t) -1)
6742 {
6743 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6744 soname_indx);
6745 def.vd_hash = bfd_elf_hash (soname);
6746 defaux.vda_name = soname_indx;
6747 name = soname;
6748 }
6749 else
6750 {
6751 size_t indx;
6752
6753 name = lbasename (bfd_get_filename (output_bfd));
6754 def.vd_hash = bfd_elf_hash (name);
6755 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6756 name, false);
6757 if (indx == (size_t) -1)
6758 return false;
6759 defaux.vda_name = indx;
6760 }
6761 defaux.vda_next = 0;
6762
6763 _bfd_elf_swap_verdef_out (output_bfd, &def,
6764 (Elf_External_Verdef *) p);
6765 p += sizeof (Elf_External_Verdef);
6766 if (info->create_default_symver)
6767 {
6768 /* Add a symbol representing this version. */
6769 bh = NULL;
6770 if (! (_bfd_generic_link_add_one_symbol
6771 (info, dynobj, 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 = NULL;
6780
6781 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6782 return false;
6783
6784 /* Create a duplicate of the base version with the same
6785 aux block, but different flags. */
6786 def.vd_flags = 0;
6787 def.vd_ndx = 2;
6788 def.vd_aux = sizeof (Elf_External_Verdef);
6789 if (verdefs)
6790 def.vd_next = (sizeof (Elf_External_Verdef)
6791 + sizeof (Elf_External_Verdaux));
6792 else
6793 def.vd_next = 0;
6794 _bfd_elf_swap_verdef_out (output_bfd, &def,
6795 (Elf_External_Verdef *) p);
6796 p += sizeof (Elf_External_Verdef);
6797 }
6798 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6799 (Elf_External_Verdaux *) p);
6800 p += sizeof (Elf_External_Verdaux);
6801
6802 for (t = verdefs; t != NULL; t = t->next)
6803 {
6804 unsigned int cdeps;
6805 struct bfd_elf_version_deps *n;
6806
6807 /* Don't emit the base version twice. */
6808 if (t->vernum == 0)
6809 continue;
6810
6811 cdeps = 0;
6812 for (n = t->deps; n != NULL; n = n->next)
6813 ++cdeps;
6814
6815 /* Add a symbol representing this version. */
6816 bh = NULL;
6817 if (! (_bfd_generic_link_add_one_symbol
6818 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6819 0, NULL, false,
6820 get_elf_backend_data (dynobj)->collect, &bh)))
6821 return false;
6822 h = (struct elf_link_hash_entry *) bh;
6823 h->non_elf = 0;
6824 h->def_regular = 1;
6825 h->type = STT_OBJECT;
6826 h->verinfo.vertree = t;
6827
6828 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6829 return false;
6830
6831 def.vd_version = VER_DEF_CURRENT;
6832 def.vd_flags = 0;
6833 if (t->globals.list == NULL
6834 && t->locals.list == NULL
6835 && ! t->used)
6836 def.vd_flags |= VER_FLG_WEAK;
6837 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6838 def.vd_cnt = cdeps + 1;
6839 def.vd_hash = bfd_elf_hash (t->name);
6840 def.vd_aux = sizeof (Elf_External_Verdef);
6841 def.vd_next = 0;
6842
6843 /* If a basever node is next, it *must* be the last node in
6844 the chain, otherwise Verdef construction breaks. */
6845 if (t->next != NULL && t->next->vernum == 0)
6846 BFD_ASSERT (t->next->next == NULL);
6847
6848 if (t->next != NULL && t->next->vernum != 0)
6849 def.vd_next = (sizeof (Elf_External_Verdef)
6850 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6851
6852 _bfd_elf_swap_verdef_out (output_bfd, &def,
6853 (Elf_External_Verdef *) p);
6854 p += sizeof (Elf_External_Verdef);
6855
6856 defaux.vda_name = h->dynstr_index;
6857 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6858 h->dynstr_index);
6859 defaux.vda_next = 0;
6860 if (t->deps != NULL)
6861 defaux.vda_next = sizeof (Elf_External_Verdaux);
6862 t->name_indx = defaux.vda_name;
6863
6864 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6865 (Elf_External_Verdaux *) p);
6866 p += sizeof (Elf_External_Verdaux);
6867
6868 for (n = t->deps; n != NULL; n = n->next)
6869 {
6870 if (n->version_needed == NULL)
6871 {
6872 /* This can happen if there was an error in the
6873 version script. */
6874 defaux.vda_name = 0;
6875 }
6876 else
6877 {
6878 defaux.vda_name = n->version_needed->name_indx;
6879 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6880 defaux.vda_name);
6881 }
6882 if (n->next == NULL)
6883 defaux.vda_next = 0;
6884 else
6885 defaux.vda_next = sizeof (Elf_External_Verdaux);
6886
6887 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6888 (Elf_External_Verdaux *) p);
6889 p += sizeof (Elf_External_Verdaux);
6890 }
6891 }
6892
6893 elf_tdata (output_bfd)->cverdefs = cdefs;
6894 }
6895 }
6896
6897 if (info->gc_sections && bed->can_gc_sections)
6898 {
6899 struct elf_gc_sweep_symbol_info sweep_info;
6900
6901 /* Remove the symbols that were in the swept sections from the
6902 dynamic symbol table. */
6903 sweep_info.info = info;
6904 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6905 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6906 &sweep_info);
6907 }
6908
6909 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6910 {
6911 asection *s;
6912 struct elf_find_verdep_info sinfo;
6913
6914 /* Work out the size of the version reference section. */
6915
6916 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6917 BFD_ASSERT (s != NULL);
6918
6919 sinfo.info = info;
6920 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6921 if (sinfo.vers == 0)
6922 sinfo.vers = 1;
6923 sinfo.failed = false;
6924
6925 elf_link_hash_traverse (elf_hash_table (info),
6926 _bfd_elf_link_find_version_dependencies,
6927 &sinfo);
6928 if (sinfo.failed)
6929 return false;
6930
6931 if (elf_tdata (output_bfd)->verref == NULL)
6932 s->flags |= SEC_EXCLUDE;
6933 else
6934 {
6935 Elf_Internal_Verneed *vn;
6936 unsigned int size;
6937 unsigned int crefs;
6938 bfd_byte *p;
6939
6940 /* Build the version dependency section. */
6941 size = 0;
6942 crefs = 0;
6943 for (vn = elf_tdata (output_bfd)->verref;
6944 vn != NULL;
6945 vn = vn->vn_nextref)
6946 {
6947 Elf_Internal_Vernaux *a;
6948
6949 size += sizeof (Elf_External_Verneed);
6950 ++crefs;
6951 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6952 size += sizeof (Elf_External_Vernaux);
6953 }
6954
6955 s->size = size;
6956 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6957 if (s->contents == NULL)
6958 return false;
6959
6960 p = s->contents;
6961 for (vn = elf_tdata (output_bfd)->verref;
6962 vn != NULL;
6963 vn = vn->vn_nextref)
6964 {
6965 unsigned int caux;
6966 Elf_Internal_Vernaux *a;
6967 size_t indx;
6968
6969 caux = 0;
6970 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6971 ++caux;
6972
6973 vn->vn_version = VER_NEED_CURRENT;
6974 vn->vn_cnt = caux;
6975 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6976 elf_dt_name (vn->vn_bfd) != NULL
6977 ? elf_dt_name (vn->vn_bfd)
6978 : lbasename (bfd_get_filename
6979 (vn->vn_bfd)),
6980 false);
6981 if (indx == (size_t) -1)
6982 return false;
6983 vn->vn_file = indx;
6984 vn->vn_aux = sizeof (Elf_External_Verneed);
6985 if (vn->vn_nextref == NULL)
6986 vn->vn_next = 0;
6987 else
6988 vn->vn_next = (sizeof (Elf_External_Verneed)
6989 + caux * sizeof (Elf_External_Vernaux));
6990
6991 _bfd_elf_swap_verneed_out (output_bfd, vn,
6992 (Elf_External_Verneed *) p);
6993 p += sizeof (Elf_External_Verneed);
6994
6995 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6996 {
6997 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6998 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6999 a->vna_nodename, false);
7000 if (indx == (size_t) -1)
7001 return false;
7002 a->vna_name = indx;
7003 if (a->vna_nextptr == NULL)
7004 a->vna_next = 0;
7005 else
7006 a->vna_next = sizeof (Elf_External_Vernaux);
7007
7008 _bfd_elf_swap_vernaux_out (output_bfd, a,
7009 (Elf_External_Vernaux *) p);
7010 p += sizeof (Elf_External_Vernaux);
7011 }
7012 }
7013
7014 elf_tdata (output_bfd)->cverrefs = crefs;
7015 }
7016 }
7017
7018 if (bfd_link_relocatable (info)
7019 && !_bfd_elf_size_group_sections (info))
7020 return false;
7021
7022 /* Determine any GNU_STACK segment requirements, after the backend
7023 has had a chance to set a default segment size. */
7024 if (info->execstack)
7025 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
7026 else if (info->noexecstack)
7027 elf_stack_flags (output_bfd) = PF_R | PF_W;
7028 else
7029 {
7030 bfd *inputobj;
7031 asection *notesec = NULL;
7032 int exec = 0;
7033
7034 for (inputobj = info->input_bfds;
7035 inputobj;
7036 inputobj = inputobj->link.next)
7037 {
7038 asection *s;
7039
7040 if (inputobj->flags
7041 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
7042 continue;
7043 s = inputobj->sections;
7044 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7045 continue;
7046
7047 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
7048 if (s)
7049 {
7050 if (s->flags & SEC_CODE)
7051 exec = PF_X;
7052 notesec = s;
7053 }
7054 else if (bed->default_execstack)
7055 exec = PF_X;
7056 }
7057 if (notesec || info->stacksize > 0)
7058 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
7059 if (notesec && exec && bfd_link_relocatable (info)
7060 && notesec->output_section != bfd_abs_section_ptr)
7061 notesec->output_section->flags |= SEC_CODE;
7062 }
7063
7064 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7065 {
7066 struct elf_info_failed eif;
7067 struct elf_link_hash_entry *h;
7068 asection *dynstr;
7069 asection *s;
7070
7071 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
7072 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
7073
7074 if (info->symbolic)
7075 {
7076 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
7077 return false;
7078 info->flags |= DF_SYMBOLIC;
7079 }
7080
7081 if (rpath != NULL)
7082 {
7083 size_t indx;
7084 bfd_vma tag;
7085
7086 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
7087 true);
7088 if (indx == (size_t) -1)
7089 return false;
7090
7091 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
7092 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
7093 return false;
7094 }
7095
7096 if (filter_shlib != NULL)
7097 {
7098 size_t indx;
7099
7100 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7101 filter_shlib, true);
7102 if (indx == (size_t) -1
7103 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
7104 return false;
7105 }
7106
7107 if (auxiliary_filters != NULL)
7108 {
7109 const char * const *p;
7110
7111 for (p = auxiliary_filters; *p != NULL; p++)
7112 {
7113 size_t indx;
7114
7115 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7116 *p, true);
7117 if (indx == (size_t) -1
7118 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
7119 return false;
7120 }
7121 }
7122
7123 if (audit != NULL)
7124 {
7125 size_t indx;
7126
7127 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
7128 true);
7129 if (indx == (size_t) -1
7130 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
7131 return false;
7132 }
7133
7134 if (depaudit != NULL)
7135 {
7136 size_t indx;
7137
7138 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
7139 true);
7140 if (indx == (size_t) -1
7141 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
7142 return false;
7143 }
7144
7145 eif.info = info;
7146 eif.failed = false;
7147
7148 /* Find all symbols which were defined in a dynamic object and make
7149 the backend pick a reasonable value for them. */
7150 elf_link_hash_traverse (elf_hash_table (info),
7151 _bfd_elf_adjust_dynamic_symbol,
7152 &eif);
7153 if (eif.failed)
7154 return false;
7155
7156 /* Add some entries to the .dynamic section. We fill in some of the
7157 values later, in bfd_elf_final_link, but we must add the entries
7158 now so that we know the final size of the .dynamic section. */
7159
7160 /* If there are initialization and/or finalization functions to
7161 call then add the corresponding DT_INIT/DT_FINI entries. */
7162 h = (info->init_function
7163 ? elf_link_hash_lookup (elf_hash_table (info),
7164 info->init_function, false,
7165 false, false)
7166 : NULL);
7167 if (h != NULL
7168 && (h->ref_regular
7169 || h->def_regular))
7170 {
7171 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
7172 return false;
7173 }
7174 h = (info->fini_function
7175 ? elf_link_hash_lookup (elf_hash_table (info),
7176 info->fini_function, false,
7177 false, false)
7178 : NULL);
7179 if (h != NULL
7180 && (h->ref_regular
7181 || h->def_regular))
7182 {
7183 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
7184 return false;
7185 }
7186
7187 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
7188 if (s != NULL && s->linker_has_input)
7189 {
7190 /* DT_PREINIT_ARRAY is not allowed in shared library. */
7191 if (! bfd_link_executable (info))
7192 {
7193 bfd *sub;
7194 asection *o;
7195
7196 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
7197 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
7198 && (o = sub->sections) != NULL
7199 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
7200 for (o = sub->sections; o != NULL; o = o->next)
7201 if (elf_section_data (o)->this_hdr.sh_type
7202 == SHT_PREINIT_ARRAY)
7203 {
7204 _bfd_error_handler
7205 (_("%pB: .preinit_array section is not allowed in DSO"),
7206 sub);
7207 break;
7208 }
7209
7210 bfd_set_error (bfd_error_nonrepresentable_section);
7211 return false;
7212 }
7213
7214 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
7215 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
7216 return false;
7217 }
7218 s = bfd_get_section_by_name (output_bfd, ".init_array");
7219 if (s != NULL && s->linker_has_input)
7220 {
7221 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
7222 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
7223 return false;
7224 }
7225 s = bfd_get_section_by_name (output_bfd, ".fini_array");
7226 if (s != NULL && s->linker_has_input)
7227 {
7228 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
7229 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
7230 return false;
7231 }
7232
7233 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
7234 /* If .dynstr is excluded from the link, we don't want any of
7235 these tags. Strictly, we should be checking each section
7236 individually; This quick check covers for the case where
7237 someone does a /DISCARD/ : { *(*) }. */
7238 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
7239 {
7240 bfd_size_type strsize;
7241
7242 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7243 if ((info->emit_hash
7244 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
7245 || (info->emit_gnu_hash
7246 && (bed->record_xhash_symbol == NULL
7247 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)))
7248 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
7249 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
7250 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
7251 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
7252 bed->s->sizeof_sym)
7253 || (info->gnu_flags_1
7254 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_FLAGS_1,
7255 info->gnu_flags_1)))
7256 return false;
7257 }
7258 }
7259
7260 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
7261 return false;
7262
7263 /* The backend must work out the sizes of all the other dynamic
7264 sections. */
7265 if (dynobj != NULL
7266 && bed->elf_backend_size_dynamic_sections != NULL
7267 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
7268 return false;
7269
7270 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7271 {
7272 if (elf_tdata (output_bfd)->cverdefs)
7273 {
7274 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
7275
7276 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
7277 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
7278 return false;
7279 }
7280
7281 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
7282 {
7283 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
7284 return false;
7285 }
7286 else if (info->flags & DF_BIND_NOW)
7287 {
7288 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
7289 return false;
7290 }
7291
7292 if (info->flags_1)
7293 {
7294 if (bfd_link_executable (info))
7295 info->flags_1 &= ~ (DF_1_INITFIRST
7296 | DF_1_NODELETE
7297 | DF_1_NOOPEN);
7298 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
7299 return false;
7300 }
7301
7302 if (elf_tdata (output_bfd)->cverrefs)
7303 {
7304 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
7305
7306 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
7307 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
7308 return false;
7309 }
7310
7311 if ((elf_tdata (output_bfd)->cverrefs == 0
7312 && elf_tdata (output_bfd)->cverdefs == 0)
7313 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
7314 {
7315 asection *s;
7316
7317 s = bfd_get_linker_section (dynobj, ".gnu.version");
7318 s->flags |= SEC_EXCLUDE;
7319 }
7320 }
7321 return true;
7322 }
7323
7324 /* Find the first non-excluded output section. We'll use its
7325 section symbol for some emitted relocs. */
7326 void
7327 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7328 {
7329 asection *s;
7330 asection *found = NULL;
7331
7332 for (s = output_bfd->sections; s != NULL; s = s->next)
7333 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7334 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7335 {
7336 found = s;
7337 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7338 break;
7339 }
7340 elf_hash_table (info)->text_index_section = found;
7341 }
7342
7343 /* Find two non-excluded output sections, one for code, one for data.
7344 We'll use their section symbols for some emitted relocs. */
7345 void
7346 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7347 {
7348 asection *s;
7349 asection *found = NULL;
7350
7351 /* Data first, since setting text_index_section changes
7352 _bfd_elf_omit_section_dynsym_default. */
7353 for (s = output_bfd->sections; s != NULL; s = s->next)
7354 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7355 && !(s->flags & SEC_READONLY)
7356 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7357 {
7358 found = s;
7359 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7360 break;
7361 }
7362 elf_hash_table (info)->data_index_section = found;
7363
7364 for (s = output_bfd->sections; s != NULL; s = s->next)
7365 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7366 && (s->flags & SEC_READONLY)
7367 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7368 {
7369 found = s;
7370 break;
7371 }
7372 elf_hash_table (info)->text_index_section = found;
7373 }
7374
7375 #define GNU_HASH_SECTION_NAME(bed) \
7376 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7377
7378 bool
7379 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7380 {
7381 const struct elf_backend_data *bed;
7382 unsigned long section_sym_count;
7383 bfd_size_type dynsymcount = 0;
7384
7385 if (!is_elf_hash_table (info->hash))
7386 return true;
7387
7388 bed = get_elf_backend_data (output_bfd);
7389 (*bed->elf_backend_init_index_section) (output_bfd, info);
7390
7391 /* Assign dynsym indices. In a shared library we generate a section
7392 symbol for each output section, which come first. Next come all
7393 of the back-end allocated local dynamic syms, followed by the rest
7394 of the global symbols.
7395
7396 This is usually not needed for static binaries, however backends
7397 can request to always do it, e.g. the MIPS backend uses dynamic
7398 symbol counts to lay out GOT, which will be produced in the
7399 presence of GOT relocations even in static binaries (holding fixed
7400 data in that case, to satisfy those relocations). */
7401
7402 if (elf_hash_table (info)->dynamic_sections_created
7403 || bed->always_renumber_dynsyms)
7404 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7405 &section_sym_count);
7406
7407 if (elf_hash_table (info)->dynamic_sections_created)
7408 {
7409 bfd *dynobj;
7410 asection *s;
7411 unsigned int dtagcount;
7412
7413 dynobj = elf_hash_table (info)->dynobj;
7414
7415 /* Work out the size of the symbol version section. */
7416 s = bfd_get_linker_section (dynobj, ".gnu.version");
7417 BFD_ASSERT (s != NULL);
7418 if ((s->flags & SEC_EXCLUDE) == 0)
7419 {
7420 s->size = dynsymcount * sizeof (Elf_External_Versym);
7421 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7422 if (s->contents == NULL)
7423 return false;
7424
7425 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7426 return false;
7427 }
7428
7429 /* Set the size of the .dynsym and .hash sections. We counted
7430 the number of dynamic symbols in elf_link_add_object_symbols.
7431 We will build the contents of .dynsym and .hash when we build
7432 the final symbol table, because until then we do not know the
7433 correct value to give the symbols. We built the .dynstr
7434 section as we went along in elf_link_add_object_symbols. */
7435 s = elf_hash_table (info)->dynsym;
7436 BFD_ASSERT (s != NULL);
7437 s->size = dynsymcount * bed->s->sizeof_sym;
7438
7439 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7440 if (s->contents == NULL)
7441 return false;
7442
7443 /* The first entry in .dynsym is a dummy symbol. Clear all the
7444 section syms, in case we don't output them all. */
7445 ++section_sym_count;
7446 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
7447
7448 elf_hash_table (info)->bucketcount = 0;
7449
7450 /* Compute the size of the hashing table. As a side effect this
7451 computes the hash values for all the names we export. */
7452 if (info->emit_hash)
7453 {
7454 unsigned long int *hashcodes;
7455 struct hash_codes_info hashinf;
7456 bfd_size_type amt;
7457 unsigned long int nsyms;
7458 size_t bucketcount;
7459 size_t hash_entry_size;
7460
7461 /* Compute the hash values for all exported symbols. At the same
7462 time store the values in an array so that we could use them for
7463 optimizations. */
7464 amt = dynsymcount * sizeof (unsigned long int);
7465 hashcodes = (unsigned long int *) bfd_malloc (amt);
7466 if (hashcodes == NULL)
7467 return false;
7468 hashinf.hashcodes = hashcodes;
7469 hashinf.error = false;
7470
7471 /* Put all hash values in HASHCODES. */
7472 elf_link_hash_traverse (elf_hash_table (info),
7473 elf_collect_hash_codes, &hashinf);
7474 if (hashinf.error)
7475 {
7476 free (hashcodes);
7477 return false;
7478 }
7479
7480 nsyms = hashinf.hashcodes - hashcodes;
7481 bucketcount
7482 = compute_bucket_count (info, hashcodes, nsyms, 0);
7483 free (hashcodes);
7484
7485 if (bucketcount == 0 && nsyms > 0)
7486 return false;
7487
7488 elf_hash_table (info)->bucketcount = bucketcount;
7489
7490 s = bfd_get_linker_section (dynobj, ".hash");
7491 BFD_ASSERT (s != NULL);
7492 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7493 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
7494 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7495 if (s->contents == NULL)
7496 return false;
7497
7498 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7499 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7500 s->contents + hash_entry_size);
7501 }
7502
7503 if (info->emit_gnu_hash)
7504 {
7505 size_t i, cnt;
7506 unsigned char *contents;
7507 struct collect_gnu_hash_codes cinfo;
7508 bfd_size_type amt;
7509 size_t bucketcount;
7510
7511 memset (&cinfo, 0, sizeof (cinfo));
7512
7513 /* Compute the hash values for all exported symbols. At the same
7514 time store the values in an array so that we could use them for
7515 optimizations. */
7516 amt = dynsymcount * 2 * sizeof (unsigned long int);
7517 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
7518 if (cinfo.hashcodes == NULL)
7519 return false;
7520
7521 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7522 cinfo.min_dynindx = -1;
7523 cinfo.output_bfd = output_bfd;
7524 cinfo.bed = bed;
7525
7526 /* Put all hash values in HASHCODES. */
7527 elf_link_hash_traverse (elf_hash_table (info),
7528 elf_collect_gnu_hash_codes, &cinfo);
7529 if (cinfo.error)
7530 {
7531 free (cinfo.hashcodes);
7532 return false;
7533 }
7534
7535 bucketcount
7536 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7537
7538 if (bucketcount == 0)
7539 {
7540 free (cinfo.hashcodes);
7541 return false;
7542 }
7543
7544 s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (bed));
7545 BFD_ASSERT (s != NULL);
7546
7547 if (cinfo.nsyms == 0)
7548 {
7549 /* Empty .gnu.hash or .MIPS.xhash section is special. */
7550 BFD_ASSERT (cinfo.min_dynindx == -1);
7551 free (cinfo.hashcodes);
7552 s->size = 5 * 4 + bed->s->arch_size / 8;
7553 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7554 if (contents == NULL)
7555 return false;
7556 s->contents = contents;
7557 /* 1 empty bucket. */
7558 bfd_put_32 (output_bfd, 1, contents);
7559 /* SYMIDX above the special symbol 0. */
7560 bfd_put_32 (output_bfd, 1, contents + 4);
7561 /* Just one word for bitmask. */
7562 bfd_put_32 (output_bfd, 1, contents + 8);
7563 /* Only hash fn bloom filter. */
7564 bfd_put_32 (output_bfd, 0, contents + 12);
7565 /* No hashes are valid - empty bitmask. */
7566 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7567 /* No hashes in the only bucket. */
7568 bfd_put_32 (output_bfd, 0,
7569 contents + 16 + bed->s->arch_size / 8);
7570 }
7571 else
7572 {
7573 unsigned long int maskwords, maskbitslog2, x;
7574 BFD_ASSERT (cinfo.min_dynindx != -1);
7575
7576 x = cinfo.nsyms;
7577 maskbitslog2 = 1;
7578 while ((x >>= 1) != 0)
7579 ++maskbitslog2;
7580 if (maskbitslog2 < 3)
7581 maskbitslog2 = 5;
7582 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7583 maskbitslog2 = maskbitslog2 + 3;
7584 else
7585 maskbitslog2 = maskbitslog2 + 2;
7586 if (bed->s->arch_size == 64)
7587 {
7588 if (maskbitslog2 == 5)
7589 maskbitslog2 = 6;
7590 cinfo.shift1 = 6;
7591 }
7592 else
7593 cinfo.shift1 = 5;
7594 cinfo.mask = (1 << cinfo.shift1) - 1;
7595 cinfo.shift2 = maskbitslog2;
7596 cinfo.maskbits = 1 << maskbitslog2;
7597 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7598 amt = bucketcount * sizeof (unsigned long int) * 2;
7599 amt += maskwords * sizeof (bfd_vma);
7600 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
7601 if (cinfo.bitmask == NULL)
7602 {
7603 free (cinfo.hashcodes);
7604 return false;
7605 }
7606
7607 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
7608 cinfo.indx = cinfo.counts + bucketcount;
7609 cinfo.symindx = dynsymcount - cinfo.nsyms;
7610 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7611
7612 /* Determine how often each hash bucket is used. */
7613 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7614 for (i = 0; i < cinfo.nsyms; ++i)
7615 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7616
7617 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7618 if (cinfo.counts[i] != 0)
7619 {
7620 cinfo.indx[i] = cnt;
7621 cnt += cinfo.counts[i];
7622 }
7623 BFD_ASSERT (cnt == dynsymcount);
7624 cinfo.bucketcount = bucketcount;
7625 cinfo.local_indx = cinfo.min_dynindx;
7626
7627 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7628 s->size += cinfo.maskbits / 8;
7629 if (bed->record_xhash_symbol != NULL)
7630 s->size += cinfo.nsyms * 4;
7631 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7632 if (contents == NULL)
7633 {
7634 free (cinfo.bitmask);
7635 free (cinfo.hashcodes);
7636 return false;
7637 }
7638
7639 s->contents = contents;
7640 bfd_put_32 (output_bfd, bucketcount, contents);
7641 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7642 bfd_put_32 (output_bfd, maskwords, contents + 8);
7643 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7644 contents += 16 + cinfo.maskbits / 8;
7645
7646 for (i = 0; i < bucketcount; ++i)
7647 {
7648 if (cinfo.counts[i] == 0)
7649 bfd_put_32 (output_bfd, 0, contents);
7650 else
7651 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7652 contents += 4;
7653 }
7654
7655 cinfo.contents = contents;
7656
7657 cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents;
7658 /* Renumber dynamic symbols, if populating .gnu.hash section.
7659 If using .MIPS.xhash, populate the translation table. */
7660 elf_link_hash_traverse (elf_hash_table (info),
7661 elf_gnu_hash_process_symidx, &cinfo);
7662
7663 contents = s->contents + 16;
7664 for (i = 0; i < maskwords; ++i)
7665 {
7666 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7667 contents);
7668 contents += bed->s->arch_size / 8;
7669 }
7670
7671 free (cinfo.bitmask);
7672 free (cinfo.hashcodes);
7673 }
7674 }
7675
7676 s = bfd_get_linker_section (dynobj, ".dynstr");
7677 BFD_ASSERT (s != NULL);
7678
7679 elf_finalize_dynstr (output_bfd, info);
7680
7681 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7682
7683 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7684 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7685 return false;
7686 }
7687
7688 return true;
7689 }
7690 \f
7691 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7692
7693 static void
7694 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7695 asection *sec)
7696 {
7697 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7698 sec->sec_info_type = SEC_INFO_TYPE_NONE;
7699 }
7700
7701 /* Finish SHF_MERGE section merging. */
7702
7703 bool
7704 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
7705 {
7706 bfd *ibfd;
7707 asection *sec;
7708
7709 if (!is_elf_hash_table (info->hash))
7710 return false;
7711
7712 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7713 if ((ibfd->flags & DYNAMIC) == 0
7714 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
7715 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7716 == get_elf_backend_data (obfd)->s->elfclass))
7717 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7718 if ((sec->flags & SEC_MERGE) != 0
7719 && !bfd_is_abs_section (sec->output_section))
7720 {
7721 struct bfd_elf_section_data *secdata;
7722
7723 secdata = elf_section_data (sec);
7724 if (! _bfd_add_merge_section (obfd,
7725 &elf_hash_table (info)->merge_info,
7726 sec, &secdata->sec_info))
7727 return false;
7728 else if (secdata->sec_info)
7729 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
7730 }
7731
7732 if (elf_hash_table (info)->merge_info != NULL)
7733 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
7734 merge_sections_remove_hook);
7735 return true;
7736 }
7737
7738 /* Create an entry in an ELF linker hash table. */
7739
7740 struct bfd_hash_entry *
7741 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7742 struct bfd_hash_table *table,
7743 const char *string)
7744 {
7745 /* Allocate the structure if it has not already been allocated by a
7746 subclass. */
7747 if (entry == NULL)
7748 {
7749 entry = (struct bfd_hash_entry *)
7750 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
7751 if (entry == NULL)
7752 return entry;
7753 }
7754
7755 /* Call the allocation method of the superclass. */
7756 entry = _bfd_link_hash_newfunc (entry, table, string);
7757 if (entry != NULL)
7758 {
7759 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7760 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7761
7762 /* Set local fields. */
7763 ret->indx = -1;
7764 ret->dynindx = -1;
7765 ret->got = htab->init_got_refcount;
7766 ret->plt = htab->init_plt_refcount;
7767 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7768 - offsetof (struct elf_link_hash_entry, size)));
7769 /* Assume that we have been called by a non-ELF symbol reader.
7770 This flag is then reset by the code which reads an ELF input
7771 file. This ensures that a symbol created by a non-ELF symbol
7772 reader will have the flag set correctly. */
7773 ret->non_elf = 1;
7774 }
7775
7776 return entry;
7777 }
7778
7779 /* Copy data from an indirect symbol to its direct symbol, hiding the
7780 old indirect symbol. Also used for copying flags to a weakdef. */
7781
7782 void
7783 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7784 struct elf_link_hash_entry *dir,
7785 struct elf_link_hash_entry *ind)
7786 {
7787 struct elf_link_hash_table *htab;
7788
7789 if (ind->dyn_relocs != NULL)
7790 {
7791 if (dir->dyn_relocs != NULL)
7792 {
7793 struct elf_dyn_relocs **pp;
7794 struct elf_dyn_relocs *p;
7795
7796 /* Add reloc counts against the indirect sym to the direct sym
7797 list. Merge any entries against the same section. */
7798 for (pp = &ind->dyn_relocs; (p = *pp) != NULL; )
7799 {
7800 struct elf_dyn_relocs *q;
7801
7802 for (q = dir->dyn_relocs; q != NULL; q = q->next)
7803 if (q->sec == p->sec)
7804 {
7805 q->pc_count += p->pc_count;
7806 q->count += p->count;
7807 *pp = p->next;
7808 break;
7809 }
7810 if (q == NULL)
7811 pp = &p->next;
7812 }
7813 *pp = dir->dyn_relocs;
7814 }
7815
7816 dir->dyn_relocs = ind->dyn_relocs;
7817 ind->dyn_relocs = NULL;
7818 }
7819
7820 /* Copy down any references that we may have already seen to the
7821 symbol which just became indirect. */
7822
7823 if (dir->versioned != versioned_hidden)
7824 dir->ref_dynamic |= ind->ref_dynamic;
7825 dir->ref_regular |= ind->ref_regular;
7826 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7827 dir->non_got_ref |= ind->non_got_ref;
7828 dir->needs_plt |= ind->needs_plt;
7829 dir->pointer_equality_needed |= ind->pointer_equality_needed;
7830
7831 if (ind->root.type != bfd_link_hash_indirect)
7832 return;
7833
7834 /* Copy over the global and procedure linkage table refcount entries.
7835 These may have been already set up by a check_relocs routine. */
7836 htab = elf_hash_table (info);
7837 if (ind->got.refcount > htab->init_got_refcount.refcount)
7838 {
7839 if (dir->got.refcount < 0)
7840 dir->got.refcount = 0;
7841 dir->got.refcount += ind->got.refcount;
7842 ind->got.refcount = htab->init_got_refcount.refcount;
7843 }
7844
7845 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7846 {
7847 if (dir->plt.refcount < 0)
7848 dir->plt.refcount = 0;
7849 dir->plt.refcount += ind->plt.refcount;
7850 ind->plt.refcount = htab->init_plt_refcount.refcount;
7851 }
7852
7853 if (ind->dynindx != -1)
7854 {
7855 if (dir->dynindx != -1)
7856 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7857 dir->dynindx = ind->dynindx;
7858 dir->dynstr_index = ind->dynstr_index;
7859 ind->dynindx = -1;
7860 ind->dynstr_index = 0;
7861 }
7862 }
7863
7864 void
7865 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7866 struct elf_link_hash_entry *h,
7867 bool force_local)
7868 {
7869 /* STT_GNU_IFUNC symbol must go through PLT. */
7870 if (h->type != STT_GNU_IFUNC)
7871 {
7872 h->plt = elf_hash_table (info)->init_plt_offset;
7873 h->needs_plt = 0;
7874 }
7875 if (force_local)
7876 {
7877 h->forced_local = 1;
7878 if (h->dynindx != -1)
7879 {
7880 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7881 h->dynstr_index);
7882 h->dynindx = -1;
7883 h->dynstr_index = 0;
7884 }
7885 }
7886 }
7887
7888 /* Hide a symbol. */
7889
7890 void
7891 _bfd_elf_link_hide_symbol (bfd *output_bfd,
7892 struct bfd_link_info *info,
7893 struct bfd_link_hash_entry *h)
7894 {
7895 if (is_elf_hash_table (info->hash))
7896 {
7897 const struct elf_backend_data *bed
7898 = get_elf_backend_data (output_bfd);
7899 struct elf_link_hash_entry *eh
7900 = (struct elf_link_hash_entry *) h;
7901 bed->elf_backend_hide_symbol (info, eh, true);
7902 eh->def_dynamic = 0;
7903 eh->ref_dynamic = 0;
7904 eh->dynamic_def = 0;
7905 }
7906 }
7907
7908 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7909 caller. */
7910
7911 bool
7912 _bfd_elf_link_hash_table_init
7913 (struct elf_link_hash_table *table,
7914 bfd *abfd,
7915 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7916 struct bfd_hash_table *,
7917 const char *),
7918 unsigned int entsize,
7919 enum elf_target_id target_id)
7920 {
7921 bool ret;
7922 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7923
7924 table->init_got_refcount.refcount = can_refcount - 1;
7925 table->init_plt_refcount.refcount = can_refcount - 1;
7926 table->init_got_offset.offset = -(bfd_vma) 1;
7927 table->init_plt_offset.offset = -(bfd_vma) 1;
7928 /* The first dynamic symbol is a dummy. */
7929 table->dynsymcount = 1;
7930
7931 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
7932
7933 table->root.type = bfd_link_elf_hash_table;
7934 table->hash_table_id = target_id;
7935 table->target_os = get_elf_backend_data (abfd)->target_os;
7936
7937 return ret;
7938 }
7939
7940 /* Create an ELF linker hash table. */
7941
7942 struct bfd_link_hash_table *
7943 _bfd_elf_link_hash_table_create (bfd *abfd)
7944 {
7945 struct elf_link_hash_table *ret;
7946 size_t amt = sizeof (struct elf_link_hash_table);
7947
7948 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
7949 if (ret == NULL)
7950 return NULL;
7951
7952 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
7953 sizeof (struct elf_link_hash_entry),
7954 GENERIC_ELF_DATA))
7955 {
7956 free (ret);
7957 return NULL;
7958 }
7959 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
7960
7961 return &ret->root;
7962 }
7963
7964 /* Destroy an ELF linker hash table. */
7965
7966 void
7967 _bfd_elf_link_hash_table_free (bfd *obfd)
7968 {
7969 struct elf_link_hash_table *htab;
7970
7971 htab = (struct elf_link_hash_table *) obfd->link.hash;
7972 if (htab->dynstr != NULL)
7973 _bfd_elf_strtab_free (htab->dynstr);
7974 _bfd_merge_sections_free (htab->merge_info);
7975 _bfd_generic_link_hash_table_free (obfd);
7976 }
7977
7978 /* This is a hook for the ELF emulation code in the generic linker to
7979 tell the backend linker what file name to use for the DT_NEEDED
7980 entry for a dynamic object. */
7981
7982 void
7983 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7984 {
7985 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7986 && bfd_get_format (abfd) == bfd_object)
7987 elf_dt_name (abfd) = name;
7988 }
7989
7990 int
7991 bfd_elf_get_dyn_lib_class (bfd *abfd)
7992 {
7993 int lib_class;
7994 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7995 && bfd_get_format (abfd) == bfd_object)
7996 lib_class = elf_dyn_lib_class (abfd);
7997 else
7998 lib_class = 0;
7999 return lib_class;
8000 }
8001
8002 void
8003 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
8004 {
8005 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8006 && bfd_get_format (abfd) == bfd_object)
8007 elf_dyn_lib_class (abfd) = lib_class;
8008 }
8009
8010 /* Get the list of DT_NEEDED entries for a link. This is a hook for
8011 the linker ELF emulation code. */
8012
8013 struct bfd_link_needed_list *
8014 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
8015 struct bfd_link_info *info)
8016 {
8017 if (! is_elf_hash_table (info->hash))
8018 return NULL;
8019 return elf_hash_table (info)->needed;
8020 }
8021
8022 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
8023 hook for the linker ELF emulation code. */
8024
8025 struct bfd_link_needed_list *
8026 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
8027 struct bfd_link_info *info)
8028 {
8029 if (! is_elf_hash_table (info->hash))
8030 return NULL;
8031 return elf_hash_table (info)->runpath;
8032 }
8033
8034 /* Get the name actually used for a dynamic object for a link. This
8035 is the SONAME entry if there is one. Otherwise, it is the string
8036 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
8037
8038 const char *
8039 bfd_elf_get_dt_soname (bfd *abfd)
8040 {
8041 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8042 && bfd_get_format (abfd) == bfd_object)
8043 return elf_dt_name (abfd);
8044 return NULL;
8045 }
8046
8047 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
8048 the ELF linker emulation code. */
8049
8050 bool
8051 bfd_elf_get_bfd_needed_list (bfd *abfd,
8052 struct bfd_link_needed_list **pneeded)
8053 {
8054 asection *s;
8055 bfd_byte *dynbuf = NULL;
8056 unsigned int elfsec;
8057 unsigned long shlink;
8058 bfd_byte *extdyn, *extdynend;
8059 size_t extdynsize;
8060 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
8061
8062 *pneeded = NULL;
8063
8064 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
8065 || bfd_get_format (abfd) != bfd_object)
8066 return true;
8067
8068 s = bfd_get_section_by_name (abfd, ".dynamic");
8069 if (s == NULL || s->size == 0)
8070 return true;
8071
8072 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
8073 goto error_return;
8074
8075 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
8076 if (elfsec == SHN_BAD)
8077 goto error_return;
8078
8079 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
8080
8081 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
8082 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
8083
8084 extdyn = dynbuf;
8085 extdynend = extdyn + s->size;
8086 for (; extdyn < extdynend; extdyn += extdynsize)
8087 {
8088 Elf_Internal_Dyn dyn;
8089
8090 (*swap_dyn_in) (abfd, extdyn, &dyn);
8091
8092 if (dyn.d_tag == DT_NULL)
8093 break;
8094
8095 if (dyn.d_tag == DT_NEEDED)
8096 {
8097 const char *string;
8098 struct bfd_link_needed_list *l;
8099 unsigned int tagv = dyn.d_un.d_val;
8100 size_t amt;
8101
8102 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
8103 if (string == NULL)
8104 goto error_return;
8105
8106 amt = sizeof *l;
8107 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
8108 if (l == NULL)
8109 goto error_return;
8110
8111 l->by = abfd;
8112 l->name = string;
8113 l->next = *pneeded;
8114 *pneeded = l;
8115 }
8116 }
8117
8118 free (dynbuf);
8119
8120 return true;
8121
8122 error_return:
8123 free (dynbuf);
8124 return false;
8125 }
8126
8127 struct elf_symbuf_symbol
8128 {
8129 unsigned long st_name; /* Symbol name, index in string tbl */
8130 unsigned char st_info; /* Type and binding attributes */
8131 unsigned char st_other; /* Visibilty, and target specific */
8132 };
8133
8134 struct elf_symbuf_head
8135 {
8136 struct elf_symbuf_symbol *ssym;
8137 size_t count;
8138 unsigned int st_shndx;
8139 };
8140
8141 struct elf_symbol
8142 {
8143 union
8144 {
8145 Elf_Internal_Sym *isym;
8146 struct elf_symbuf_symbol *ssym;
8147 void *p;
8148 } u;
8149 const char *name;
8150 };
8151
8152 /* Sort references to symbols by ascending section number. */
8153
8154 static int
8155 elf_sort_elf_symbol (const void *arg1, const void *arg2)
8156 {
8157 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
8158 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
8159
8160 if (s1->st_shndx != s2->st_shndx)
8161 return s1->st_shndx > s2->st_shndx ? 1 : -1;
8162 /* Final sort by the address of the sym in the symbuf ensures
8163 a stable sort. */
8164 if (s1 != s2)
8165 return s1 > s2 ? 1 : -1;
8166 return 0;
8167 }
8168
8169 static int
8170 elf_sym_name_compare (const void *arg1, const void *arg2)
8171 {
8172 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
8173 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
8174 int ret = strcmp (s1->name, s2->name);
8175 if (ret != 0)
8176 return ret;
8177 if (s1->u.p != s2->u.p)
8178 return s1->u.p > s2->u.p ? 1 : -1;
8179 return 0;
8180 }
8181
8182 static struct elf_symbuf_head *
8183 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
8184 {
8185 Elf_Internal_Sym **ind, **indbufend, **indbuf;
8186 struct elf_symbuf_symbol *ssym;
8187 struct elf_symbuf_head *ssymbuf, *ssymhead;
8188 size_t i, shndx_count, total_size, amt;
8189
8190 amt = symcount * sizeof (*indbuf);
8191 indbuf = (Elf_Internal_Sym **) bfd_malloc (amt);
8192 if (indbuf == NULL)
8193 return NULL;
8194
8195 for (ind = indbuf, i = 0; i < symcount; i++)
8196 if (isymbuf[i].st_shndx != SHN_UNDEF)
8197 *ind++ = &isymbuf[i];
8198 indbufend = ind;
8199
8200 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
8201 elf_sort_elf_symbol);
8202
8203 shndx_count = 0;
8204 if (indbufend > indbuf)
8205 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
8206 if (ind[0]->st_shndx != ind[1]->st_shndx)
8207 shndx_count++;
8208
8209 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
8210 + (indbufend - indbuf) * sizeof (*ssym));
8211 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
8212 if (ssymbuf == NULL)
8213 {
8214 free (indbuf);
8215 return NULL;
8216 }
8217
8218 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
8219 ssymbuf->ssym = NULL;
8220 ssymbuf->count = shndx_count;
8221 ssymbuf->st_shndx = 0;
8222 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
8223 {
8224 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
8225 {
8226 ssymhead++;
8227 ssymhead->ssym = ssym;
8228 ssymhead->count = 0;
8229 ssymhead->st_shndx = (*ind)->st_shndx;
8230 }
8231 ssym->st_name = (*ind)->st_name;
8232 ssym->st_info = (*ind)->st_info;
8233 ssym->st_other = (*ind)->st_other;
8234 ssymhead->count++;
8235 }
8236 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
8237 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
8238 == total_size));
8239
8240 free (indbuf);
8241 return ssymbuf;
8242 }
8243
8244 /* Check if 2 sections define the same set of local and global
8245 symbols. */
8246
8247 static bool
8248 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
8249 struct bfd_link_info *info)
8250 {
8251 bfd *bfd1, *bfd2;
8252 const struct elf_backend_data *bed1, *bed2;
8253 Elf_Internal_Shdr *hdr1, *hdr2;
8254 size_t symcount1, symcount2;
8255 Elf_Internal_Sym *isymbuf1, *isymbuf2;
8256 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
8257 Elf_Internal_Sym *isym, *isymend;
8258 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
8259 size_t count1, count2, sec_count1, sec_count2, i;
8260 unsigned int shndx1, shndx2;
8261 bool result;
8262 bool ignore_section_symbol_p;
8263
8264 bfd1 = sec1->owner;
8265 bfd2 = sec2->owner;
8266
8267 /* Both sections have to be in ELF. */
8268 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
8269 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
8270 return false;
8271
8272 if (elf_section_type (sec1) != elf_section_type (sec2))
8273 return false;
8274
8275 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
8276 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
8277 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
8278 return false;
8279
8280 bed1 = get_elf_backend_data (bfd1);
8281 bed2 = get_elf_backend_data (bfd2);
8282 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
8283 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
8284 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
8285 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
8286
8287 if (symcount1 == 0 || symcount2 == 0)
8288 return false;
8289
8290 result = false;
8291 isymbuf1 = NULL;
8292 isymbuf2 = NULL;
8293 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
8294 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
8295
8296 /* Ignore section symbols only when matching non-debugging sections
8297 or linkonce section with comdat section. */
8298 ignore_section_symbol_p
8299 = ((sec1->flags & SEC_DEBUGGING) == 0
8300 || ((elf_section_flags (sec1) & SHF_GROUP)
8301 != (elf_section_flags (sec2) & SHF_GROUP)));
8302
8303 if (ssymbuf1 == NULL)
8304 {
8305 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
8306 NULL, NULL, NULL);
8307 if (isymbuf1 == NULL)
8308 goto done;
8309
8310 if (info != NULL && !info->reduce_memory_overheads)
8311 {
8312 ssymbuf1 = elf_create_symbuf (symcount1, isymbuf1);
8313 elf_tdata (bfd1)->symbuf = ssymbuf1;
8314 }
8315 }
8316
8317 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
8318 {
8319 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
8320 NULL, NULL, NULL);
8321 if (isymbuf2 == NULL)
8322 goto done;
8323
8324 if (ssymbuf1 != NULL && info != NULL && !info->reduce_memory_overheads)
8325 {
8326 ssymbuf2 = elf_create_symbuf (symcount2, isymbuf2);
8327 elf_tdata (bfd2)->symbuf = ssymbuf2;
8328 }
8329 }
8330
8331 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
8332 {
8333 /* Optimized faster version. */
8334 size_t lo, hi, mid;
8335 struct elf_symbol *symp;
8336 struct elf_symbuf_symbol *ssym, *ssymend;
8337
8338 lo = 0;
8339 hi = ssymbuf1->count;
8340 ssymbuf1++;
8341 count1 = 0;
8342 sec_count1 = 0;
8343 while (lo < hi)
8344 {
8345 mid = (lo + hi) / 2;
8346 if (shndx1 < ssymbuf1[mid].st_shndx)
8347 hi = mid;
8348 else if (shndx1 > ssymbuf1[mid].st_shndx)
8349 lo = mid + 1;
8350 else
8351 {
8352 count1 = ssymbuf1[mid].count;
8353 ssymbuf1 += mid;
8354 break;
8355 }
8356 }
8357 if (ignore_section_symbol_p)
8358 {
8359 for (i = 0; i < count1; i++)
8360 if (ELF_ST_TYPE (ssymbuf1->ssym[i].st_info) == STT_SECTION)
8361 sec_count1++;
8362 count1 -= sec_count1;
8363 }
8364
8365 lo = 0;
8366 hi = ssymbuf2->count;
8367 ssymbuf2++;
8368 count2 = 0;
8369 sec_count2 = 0;
8370 while (lo < hi)
8371 {
8372 mid = (lo + hi) / 2;
8373 if (shndx2 < ssymbuf2[mid].st_shndx)
8374 hi = mid;
8375 else if (shndx2 > ssymbuf2[mid].st_shndx)
8376 lo = mid + 1;
8377 else
8378 {
8379 count2 = ssymbuf2[mid].count;
8380 ssymbuf2 += mid;
8381 break;
8382 }
8383 }
8384 if (ignore_section_symbol_p)
8385 {
8386 for (i = 0; i < count2; i++)
8387 if (ELF_ST_TYPE (ssymbuf2->ssym[i].st_info) == STT_SECTION)
8388 sec_count2++;
8389 count2 -= sec_count2;
8390 }
8391
8392 if (count1 == 0 || count2 == 0 || count1 != count2)
8393 goto done;
8394
8395 symtable1
8396 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
8397 symtable2
8398 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
8399 if (symtable1 == NULL || symtable2 == NULL)
8400 goto done;
8401
8402 symp = symtable1;
8403 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1 + sec_count1;
8404 ssym < ssymend; ssym++)
8405 if (sec_count1 == 0
8406 || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
8407 {
8408 symp->u.ssym = ssym;
8409 symp->name = bfd_elf_string_from_elf_section (bfd1,
8410 hdr1->sh_link,
8411 ssym->st_name);
8412 symp++;
8413 }
8414
8415 symp = symtable2;
8416 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2 + sec_count2;
8417 ssym < ssymend; ssym++)
8418 if (sec_count2 == 0
8419 || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
8420 {
8421 symp->u.ssym = ssym;
8422 symp->name = bfd_elf_string_from_elf_section (bfd2,
8423 hdr2->sh_link,
8424 ssym->st_name);
8425 symp++;
8426 }
8427
8428 /* Sort symbol by name. */
8429 qsort (symtable1, count1, sizeof (struct elf_symbol),
8430 elf_sym_name_compare);
8431 qsort (symtable2, count1, sizeof (struct elf_symbol),
8432 elf_sym_name_compare);
8433
8434 for (i = 0; i < count1; i++)
8435 /* Two symbols must have the same binding, type and name. */
8436 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8437 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8438 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8439 goto done;
8440
8441 result = true;
8442 goto done;
8443 }
8444
8445 symtable1 = (struct elf_symbol *)
8446 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8447 symtable2 = (struct elf_symbol *)
8448 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
8449 if (symtable1 == NULL || symtable2 == NULL)
8450 goto done;
8451
8452 /* Count definitions in the section. */
8453 count1 = 0;
8454 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
8455 if (isym->st_shndx == shndx1
8456 && (!ignore_section_symbol_p
8457 || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
8458 symtable1[count1++].u.isym = isym;
8459
8460 count2 = 0;
8461 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
8462 if (isym->st_shndx == shndx2
8463 && (!ignore_section_symbol_p
8464 || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
8465 symtable2[count2++].u.isym = isym;
8466
8467 if (count1 == 0 || count2 == 0 || count1 != count2)
8468 goto done;
8469
8470 for (i = 0; i < count1; i++)
8471 symtable1[i].name
8472 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8473 symtable1[i].u.isym->st_name);
8474
8475 for (i = 0; i < count2; i++)
8476 symtable2[i].name
8477 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8478 symtable2[i].u.isym->st_name);
8479
8480 /* Sort symbol by name. */
8481 qsort (symtable1, count1, sizeof (struct elf_symbol),
8482 elf_sym_name_compare);
8483 qsort (symtable2, count1, sizeof (struct elf_symbol),
8484 elf_sym_name_compare);
8485
8486 for (i = 0; i < count1; i++)
8487 /* Two symbols must have the same binding, type and name. */
8488 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8489 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8490 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8491 goto done;
8492
8493 result = true;
8494
8495 done:
8496 free (symtable1);
8497 free (symtable2);
8498 free (isymbuf1);
8499 free (isymbuf2);
8500
8501 return result;
8502 }
8503
8504 /* Return TRUE if 2 section types are compatible. */
8505
8506 bool
8507 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8508 bfd *bbfd, const asection *bsec)
8509 {
8510 if (asec == NULL
8511 || bsec == NULL
8512 || abfd->xvec->flavour != bfd_target_elf_flavour
8513 || bbfd->xvec->flavour != bfd_target_elf_flavour)
8514 return true;
8515
8516 return elf_section_type (asec) == elf_section_type (bsec);
8517 }
8518 \f
8519 /* Final phase of ELF linker. */
8520
8521 /* A structure we use to avoid passing large numbers of arguments. */
8522
8523 struct elf_final_link_info
8524 {
8525 /* General link information. */
8526 struct bfd_link_info *info;
8527 /* Output BFD. */
8528 bfd *output_bfd;
8529 /* Symbol string table. */
8530 struct elf_strtab_hash *symstrtab;
8531 /* .hash section. */
8532 asection *hash_sec;
8533 /* symbol version section (.gnu.version). */
8534 asection *symver_sec;
8535 /* Buffer large enough to hold contents of any section. */
8536 bfd_byte *contents;
8537 /* Buffer large enough to hold external relocs of any section. */
8538 void *external_relocs;
8539 /* Buffer large enough to hold internal relocs of any section. */
8540 Elf_Internal_Rela *internal_relocs;
8541 /* Buffer large enough to hold external local symbols of any input
8542 BFD. */
8543 bfd_byte *external_syms;
8544 /* And a buffer for symbol section indices. */
8545 Elf_External_Sym_Shndx *locsym_shndx;
8546 /* Buffer large enough to hold internal local symbols of any input
8547 BFD. */
8548 Elf_Internal_Sym *internal_syms;
8549 /* Array large enough to hold a symbol index for each local symbol
8550 of any input BFD. */
8551 long *indices;
8552 /* Array large enough to hold a section pointer for each local
8553 symbol of any input BFD. */
8554 asection **sections;
8555 /* Buffer for SHT_SYMTAB_SHNDX section. */
8556 Elf_External_Sym_Shndx *symshndxbuf;
8557 /* Number of STT_FILE syms seen. */
8558 size_t filesym_count;
8559 /* Local symbol hash table. */
8560 struct bfd_hash_table local_hash_table;
8561 };
8562
8563 struct local_hash_entry
8564 {
8565 /* Base hash table entry structure. */
8566 struct bfd_hash_entry root;
8567 /* Size of the local symbol name. */
8568 size_t size;
8569 /* Number of the duplicated local symbol names. */
8570 long count;
8571 };
8572
8573 /* Create an entry in the local symbol hash table. */
8574
8575 static struct bfd_hash_entry *
8576 local_hash_newfunc (struct bfd_hash_entry *entry,
8577 struct bfd_hash_table *table,
8578 const char *string)
8579 {
8580
8581 /* Allocate the structure if it has not already been allocated by a
8582 subclass. */
8583 if (entry == NULL)
8584 {
8585 entry = bfd_hash_allocate (table,
8586 sizeof (struct local_hash_entry));
8587 if (entry == NULL)
8588 return entry;
8589 }
8590
8591 /* Call the allocation method of the superclass. */
8592 entry = bfd_hash_newfunc (entry, table, string);
8593 if (entry != NULL)
8594 {
8595 ((struct local_hash_entry *) entry)->count = 0;
8596 ((struct local_hash_entry *) entry)->size = 0;
8597 }
8598
8599 return entry;
8600 }
8601
8602 /* This struct is used to pass information to elf_link_output_extsym. */
8603
8604 struct elf_outext_info
8605 {
8606 bool failed;
8607 bool localsyms;
8608 bool file_sym_done;
8609 struct elf_final_link_info *flinfo;
8610 };
8611
8612
8613 /* Support for evaluating a complex relocation.
8614
8615 Complex relocations are generalized, self-describing relocations. The
8616 implementation of them consists of two parts: complex symbols, and the
8617 relocations themselves.
8618
8619 The relocations use a reserved elf-wide relocation type code (R_RELC
8620 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8621 information (start bit, end bit, word width, etc) into the addend. This
8622 information is extracted from CGEN-generated operand tables within gas.
8623
8624 Complex symbols are mangled symbols (STT_RELC external / BSF_RELC
8625 internal) representing prefix-notation expressions, including but not
8626 limited to those sorts of expressions normally encoded as addends in the
8627 addend field. The symbol mangling format is:
8628
8629 <node> := <literal>
8630 | <unary-operator> ':' <node>
8631 | <binary-operator> ':' <node> ':' <node>
8632 ;
8633
8634 <literal> := 's' <digits=N> ':' <N character symbol name>
8635 | 'S' <digits=N> ':' <N character section name>
8636 | '#' <hexdigits>
8637 ;
8638
8639 <binary-operator> := as in C
8640 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8641
8642 static void
8643 set_symbol_value (bfd *bfd_with_globals,
8644 Elf_Internal_Sym *isymbuf,
8645 size_t locsymcount,
8646 size_t symidx,
8647 bfd_vma val)
8648 {
8649 struct elf_link_hash_entry **sym_hashes;
8650 struct elf_link_hash_entry *h;
8651 size_t extsymoff = locsymcount;
8652
8653 if (symidx < locsymcount)
8654 {
8655 Elf_Internal_Sym *sym;
8656
8657 sym = isymbuf + symidx;
8658 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8659 {
8660 /* It is a local symbol: move it to the
8661 "absolute" section and give it a value. */
8662 sym->st_shndx = SHN_ABS;
8663 sym->st_value = val;
8664 return;
8665 }
8666 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8667 extsymoff = 0;
8668 }
8669
8670 /* It is a global symbol: set its link type
8671 to "defined" and give it a value. */
8672
8673 sym_hashes = elf_sym_hashes (bfd_with_globals);
8674 h = sym_hashes [symidx - extsymoff];
8675 while (h->root.type == bfd_link_hash_indirect
8676 || h->root.type == bfd_link_hash_warning)
8677 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8678 h->root.type = bfd_link_hash_defined;
8679 h->root.u.def.value = val;
8680 h->root.u.def.section = bfd_abs_section_ptr;
8681 }
8682
8683 static bool
8684 resolve_symbol (const char *name,
8685 bfd *input_bfd,
8686 struct elf_final_link_info *flinfo,
8687 bfd_vma *result,
8688 Elf_Internal_Sym *isymbuf,
8689 size_t locsymcount)
8690 {
8691 Elf_Internal_Sym *sym;
8692 struct bfd_link_hash_entry *global_entry;
8693 const char *candidate = NULL;
8694 Elf_Internal_Shdr *symtab_hdr;
8695 size_t i;
8696
8697 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8698
8699 for (i = 0; i < locsymcount; ++ i)
8700 {
8701 sym = isymbuf + i;
8702
8703 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8704 continue;
8705
8706 candidate = bfd_elf_string_from_elf_section (input_bfd,
8707 symtab_hdr->sh_link,
8708 sym->st_name);
8709 #ifdef DEBUG
8710 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8711 name, candidate, (unsigned long) sym->st_value);
8712 #endif
8713 if (candidate && strcmp (candidate, name) == 0)
8714 {
8715 asection *sec = flinfo->sections [i];
8716
8717 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8718 *result += sec->output_offset + sec->output_section->vma;
8719 #ifdef DEBUG
8720 printf ("Found symbol with value %8.8lx\n",
8721 (unsigned long) *result);
8722 #endif
8723 return true;
8724 }
8725 }
8726
8727 /* Hmm, haven't found it yet. perhaps it is a global. */
8728 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
8729 false, false, true);
8730 if (!global_entry)
8731 return false;
8732
8733 if (global_entry->type == bfd_link_hash_defined
8734 || global_entry->type == bfd_link_hash_defweak)
8735 {
8736 *result = (global_entry->u.def.value
8737 + global_entry->u.def.section->output_section->vma
8738 + global_entry->u.def.section->output_offset);
8739 #ifdef DEBUG
8740 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8741 global_entry->root.string, (unsigned long) *result);
8742 #endif
8743 return true;
8744 }
8745
8746 return false;
8747 }
8748
8749 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8750 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8751 names like "foo.end" which is the end address of section "foo". */
8752
8753 static bool
8754 resolve_section (const char *name,
8755 asection *sections,
8756 bfd_vma *result,
8757 bfd * abfd)
8758 {
8759 asection *curr;
8760 unsigned int len;
8761
8762 for (curr = sections; curr; curr = curr->next)
8763 if (strcmp (curr->name, name) == 0)
8764 {
8765 *result = curr->vma;
8766 return true;
8767 }
8768
8769 /* Hmm. still haven't found it. try pseudo-section names. */
8770 /* FIXME: This could be coded more efficiently... */
8771 for (curr = sections; curr; curr = curr->next)
8772 {
8773 len = strlen (curr->name);
8774 if (len > strlen (name))
8775 continue;
8776
8777 if (strncmp (curr->name, name, len) == 0)
8778 {
8779 if (startswith (name + len, ".end"))
8780 {
8781 *result = (curr->vma
8782 + curr->size / bfd_octets_per_byte (abfd, curr));
8783 return true;
8784 }
8785
8786 /* Insert more pseudo-section names here, if you like. */
8787 }
8788 }
8789
8790 return false;
8791 }
8792
8793 static void
8794 undefined_reference (const char *reftype, const char *name)
8795 {
8796 /* xgettext:c-format */
8797 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8798 reftype, name);
8799 bfd_set_error (bfd_error_bad_value);
8800 }
8801
8802 static bool
8803 eval_symbol (bfd_vma *result,
8804 const char **symp,
8805 bfd *input_bfd,
8806 struct elf_final_link_info *flinfo,
8807 bfd_vma dot,
8808 Elf_Internal_Sym *isymbuf,
8809 size_t locsymcount,
8810 int signed_p)
8811 {
8812 size_t len;
8813 size_t symlen;
8814 bfd_vma a;
8815 bfd_vma b;
8816 char symbuf[4096];
8817 const char *sym = *symp;
8818 const char *symend;
8819 bool symbol_is_section = false;
8820
8821 len = strlen (sym);
8822 symend = sym + len;
8823
8824 if (len < 1 || len > sizeof (symbuf))
8825 {
8826 bfd_set_error (bfd_error_invalid_operation);
8827 return false;
8828 }
8829
8830 switch (* sym)
8831 {
8832 case '.':
8833 *result = dot;
8834 *symp = sym + 1;
8835 return true;
8836
8837 case '#':
8838 ++sym;
8839 *result = strtoul (sym, (char **) symp, 16);
8840 return true;
8841
8842 case 'S':
8843 symbol_is_section = true;
8844 /* Fall through. */
8845 case 's':
8846 ++sym;
8847 symlen = strtol (sym, (char **) symp, 10);
8848 sym = *symp + 1; /* Skip the trailing ':'. */
8849
8850 if (symend < sym || symlen + 1 > sizeof (symbuf))
8851 {
8852 bfd_set_error (bfd_error_invalid_operation);
8853 return false;
8854 }
8855
8856 memcpy (symbuf, sym, symlen);
8857 symbuf[symlen] = '\0';
8858 *symp = sym + symlen;
8859
8860 /* Is it always possible, with complex symbols, that gas "mis-guessed"
8861 the symbol as a section, or vice-versa. so we're pretty liberal in our
8862 interpretation here; section means "try section first", not "must be a
8863 section", and likewise with symbol. */
8864
8865 if (symbol_is_section)
8866 {
8867 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8868 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8869 isymbuf, locsymcount))
8870 {
8871 undefined_reference ("section", symbuf);
8872 return false;
8873 }
8874 }
8875 else
8876 {
8877 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8878 isymbuf, locsymcount)
8879 && !resolve_section (symbuf, flinfo->output_bfd->sections,
8880 result, input_bfd))
8881 {
8882 undefined_reference ("symbol", symbuf);
8883 return false;
8884 }
8885 }
8886
8887 return true;
8888
8889 /* All that remains are operators. */
8890
8891 #define UNARY_OP(op) \
8892 if (startswith (sym, #op)) \
8893 { \
8894 sym += strlen (#op); \
8895 if (*sym == ':') \
8896 ++sym; \
8897 *symp = sym; \
8898 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8899 isymbuf, locsymcount, signed_p)) \
8900 return false; \
8901 if (signed_p) \
8902 *result = op ((bfd_signed_vma) a); \
8903 else \
8904 *result = op a; \
8905 return true; \
8906 }
8907
8908 #define BINARY_OP_HEAD(op) \
8909 if (startswith (sym, #op)) \
8910 { \
8911 sym += strlen (#op); \
8912 if (*sym == ':') \
8913 ++sym; \
8914 *symp = sym; \
8915 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8916 isymbuf, locsymcount, signed_p)) \
8917 return false; \
8918 ++*symp; \
8919 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8920 isymbuf, locsymcount, signed_p)) \
8921 return false;
8922 #define BINARY_OP_TAIL(op) \
8923 if (signed_p) \
8924 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8925 else \
8926 *result = a op b; \
8927 return true; \
8928 }
8929 #define BINARY_OP(op) BINARY_OP_HEAD(op) BINARY_OP_TAIL(op)
8930
8931 default:
8932 UNARY_OP (0-);
8933 BINARY_OP_HEAD (<<);
8934 if (b >= sizeof (a) * CHAR_BIT)
8935 {
8936 *result = 0;
8937 return true;
8938 }
8939 signed_p = 0;
8940 BINARY_OP_TAIL (<<);
8941 BINARY_OP_HEAD (>>);
8942 if (b >= sizeof (a) * CHAR_BIT)
8943 {
8944 *result = signed_p && (bfd_signed_vma) a < 0 ? -1 : 0;
8945 return true;
8946 }
8947 BINARY_OP_TAIL (>>);
8948 BINARY_OP (==);
8949 BINARY_OP (!=);
8950 BINARY_OP (<=);
8951 BINARY_OP (>=);
8952 BINARY_OP (&&);
8953 BINARY_OP (||);
8954 UNARY_OP (~);
8955 UNARY_OP (!);
8956 BINARY_OP (*);
8957 BINARY_OP_HEAD (/);
8958 if (b == 0)
8959 {
8960 _bfd_error_handler (_("division by zero"));
8961 bfd_set_error (bfd_error_bad_value);
8962 return false;
8963 }
8964 BINARY_OP_TAIL (/);
8965 BINARY_OP_HEAD (%);
8966 if (b == 0)
8967 {
8968 _bfd_error_handler (_("division by zero"));
8969 bfd_set_error (bfd_error_bad_value);
8970 return false;
8971 }
8972 BINARY_OP_TAIL (%);
8973 BINARY_OP (^);
8974 BINARY_OP (|);
8975 BINARY_OP (&);
8976 BINARY_OP (+);
8977 BINARY_OP (-);
8978 BINARY_OP (<);
8979 BINARY_OP (>);
8980 #undef UNARY_OP
8981 #undef BINARY_OP
8982 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8983 bfd_set_error (bfd_error_invalid_operation);
8984 return false;
8985 }
8986 }
8987
8988 static void
8989 put_value (bfd_vma size,
8990 unsigned long chunksz,
8991 bfd *input_bfd,
8992 bfd_vma x,
8993 bfd_byte *location)
8994 {
8995 location += (size - chunksz);
8996
8997 for (; size; size -= chunksz, location -= chunksz)
8998 {
8999 switch (chunksz)
9000 {
9001 case 1:
9002 bfd_put_8 (input_bfd, x, location);
9003 x >>= 8;
9004 break;
9005 case 2:
9006 bfd_put_16 (input_bfd, x, location);
9007 x >>= 16;
9008 break;
9009 case 4:
9010 bfd_put_32 (input_bfd, x, location);
9011 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
9012 x >>= 16;
9013 x >>= 16;
9014 break;
9015 #ifdef BFD64
9016 case 8:
9017 bfd_put_64 (input_bfd, x, location);
9018 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
9019 x >>= 32;
9020 x >>= 32;
9021 break;
9022 #endif
9023 default:
9024 abort ();
9025 break;
9026 }
9027 }
9028 }
9029
9030 static bfd_vma
9031 get_value (bfd_vma size,
9032 unsigned long chunksz,
9033 bfd *input_bfd,
9034 bfd_byte *location)
9035 {
9036 int shift;
9037 bfd_vma x = 0;
9038
9039 /* Sanity checks. */
9040 BFD_ASSERT (chunksz <= sizeof (x)
9041 && size >= chunksz
9042 && chunksz != 0
9043 && (size % chunksz) == 0
9044 && input_bfd != NULL
9045 && location != NULL);
9046
9047 if (chunksz == sizeof (x))
9048 {
9049 BFD_ASSERT (size == chunksz);
9050
9051 /* Make sure that we do not perform an undefined shift operation.
9052 We know that size == chunksz so there will only be one iteration
9053 of the loop below. */
9054 shift = 0;
9055 }
9056 else
9057 shift = 8 * chunksz;
9058
9059 for (; size; size -= chunksz, location += chunksz)
9060 {
9061 switch (chunksz)
9062 {
9063 case 1:
9064 x = (x << shift) | bfd_get_8 (input_bfd, location);
9065 break;
9066 case 2:
9067 x = (x << shift) | bfd_get_16 (input_bfd, location);
9068 break;
9069 case 4:
9070 x = (x << shift) | bfd_get_32 (input_bfd, location);
9071 break;
9072 #ifdef BFD64
9073 case 8:
9074 x = (x << shift) | bfd_get_64 (input_bfd, location);
9075 break;
9076 #endif
9077 default:
9078 abort ();
9079 }
9080 }
9081 return x;
9082 }
9083
9084 static void
9085 decode_complex_addend (unsigned long *start, /* in bits */
9086 unsigned long *oplen, /* in bits */
9087 unsigned long *len, /* in bits */
9088 unsigned long *wordsz, /* in bytes */
9089 unsigned long *chunksz, /* in bytes */
9090 unsigned long *lsb0_p,
9091 unsigned long *signed_p,
9092 unsigned long *trunc_p,
9093 unsigned long encoded)
9094 {
9095 * start = encoded & 0x3F;
9096 * len = (encoded >> 6) & 0x3F;
9097 * oplen = (encoded >> 12) & 0x3F;
9098 * wordsz = (encoded >> 18) & 0xF;
9099 * chunksz = (encoded >> 22) & 0xF;
9100 * lsb0_p = (encoded >> 27) & 1;
9101 * signed_p = (encoded >> 28) & 1;
9102 * trunc_p = (encoded >> 29) & 1;
9103 }
9104
9105 bfd_reloc_status_type
9106 bfd_elf_perform_complex_relocation (bfd *input_bfd,
9107 asection *input_section,
9108 bfd_byte *contents,
9109 Elf_Internal_Rela *rel,
9110 bfd_vma relocation)
9111 {
9112 bfd_vma shift, x, mask;
9113 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
9114 bfd_reloc_status_type r;
9115 bfd_size_type octets;
9116
9117 /* Perform this reloc, since it is complex.
9118 (this is not to say that it necessarily refers to a complex
9119 symbol; merely that it is a self-describing CGEN based reloc.
9120 i.e. the addend has the complete reloc information (bit start, end,
9121 word size, etc) encoded within it.). */
9122
9123 decode_complex_addend (&start, &oplen, &len, &wordsz,
9124 &chunksz, &lsb0_p, &signed_p,
9125 &trunc_p, rel->r_addend);
9126
9127 mask = (((1L << (len - 1)) - 1) << 1) | 1;
9128
9129 if (lsb0_p)
9130 shift = (start + 1) - len;
9131 else
9132 shift = (8 * wordsz) - (start + len);
9133
9134 octets = rel->r_offset * bfd_octets_per_byte (input_bfd, input_section);
9135 x = get_value (wordsz, chunksz, input_bfd, contents + octets);
9136
9137 #ifdef DEBUG
9138 printf ("Doing complex reloc: "
9139 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
9140 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
9141 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
9142 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9143 oplen, (unsigned long) x, (unsigned long) mask,
9144 (unsigned long) relocation);
9145 #endif
9146
9147 r = bfd_reloc_ok;
9148 if (! trunc_p)
9149 /* Now do an overflow check. */
9150 r = bfd_check_overflow ((signed_p
9151 ? complain_overflow_signed
9152 : complain_overflow_unsigned),
9153 len, 0, (8 * wordsz),
9154 relocation);
9155
9156 /* Do the deed. */
9157 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
9158
9159 #ifdef DEBUG
9160 printf (" relocation: %8.8lx\n"
9161 " shifted mask: %8.8lx\n"
9162 " shifted/masked reloc: %8.8lx\n"
9163 " result: %8.8lx\n",
9164 (unsigned long) relocation, (unsigned long) (mask << shift),
9165 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
9166 #endif
9167 put_value (wordsz, chunksz, input_bfd, x, contents + octets);
9168 return r;
9169 }
9170
9171 /* Functions to read r_offset from external (target order) reloc
9172 entry. Faster than bfd_getl32 et al, because we let the compiler
9173 know the value is aligned. */
9174
9175 static bfd_vma
9176 ext32l_r_offset (const void *p)
9177 {
9178 union aligned32
9179 {
9180 uint32_t v;
9181 unsigned char c[4];
9182 };
9183 const union aligned32 *a
9184 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9185
9186 uint32_t aval = ( (uint32_t) a->c[0]
9187 | (uint32_t) a->c[1] << 8
9188 | (uint32_t) a->c[2] << 16
9189 | (uint32_t) a->c[3] << 24);
9190 return aval;
9191 }
9192
9193 static bfd_vma
9194 ext32b_r_offset (const void *p)
9195 {
9196 union aligned32
9197 {
9198 uint32_t v;
9199 unsigned char c[4];
9200 };
9201 const union aligned32 *a
9202 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9203
9204 uint32_t aval = ( (uint32_t) a->c[0] << 24
9205 | (uint32_t) a->c[1] << 16
9206 | (uint32_t) a->c[2] << 8
9207 | (uint32_t) a->c[3]);
9208 return aval;
9209 }
9210
9211 #ifdef BFD_HOST_64_BIT
9212 static bfd_vma
9213 ext64l_r_offset (const void *p)
9214 {
9215 union aligned64
9216 {
9217 uint64_t v;
9218 unsigned char c[8];
9219 };
9220 const union aligned64 *a
9221 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9222
9223 uint64_t aval = ( (uint64_t) a->c[0]
9224 | (uint64_t) a->c[1] << 8
9225 | (uint64_t) a->c[2] << 16
9226 | (uint64_t) a->c[3] << 24
9227 | (uint64_t) a->c[4] << 32
9228 | (uint64_t) a->c[5] << 40
9229 | (uint64_t) a->c[6] << 48
9230 | (uint64_t) a->c[7] << 56);
9231 return aval;
9232 }
9233
9234 static bfd_vma
9235 ext64b_r_offset (const void *p)
9236 {
9237 union aligned64
9238 {
9239 uint64_t v;
9240 unsigned char c[8];
9241 };
9242 const union aligned64 *a
9243 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9244
9245 uint64_t aval = ( (uint64_t) a->c[0] << 56
9246 | (uint64_t) a->c[1] << 48
9247 | (uint64_t) a->c[2] << 40
9248 | (uint64_t) a->c[3] << 32
9249 | (uint64_t) a->c[4] << 24
9250 | (uint64_t) a->c[5] << 16
9251 | (uint64_t) a->c[6] << 8
9252 | (uint64_t) a->c[7]);
9253 return aval;
9254 }
9255 #endif
9256
9257 /* When performing a relocatable link, the input relocations are
9258 preserved. But, if they reference global symbols, the indices
9259 referenced must be updated. Update all the relocations found in
9260 RELDATA. */
9261
9262 static bool
9263 elf_link_adjust_relocs (bfd *abfd,
9264 asection *sec,
9265 struct bfd_elf_section_reloc_data *reldata,
9266 bool sort,
9267 struct bfd_link_info *info)
9268 {
9269 unsigned int i;
9270 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9271 bfd_byte *erela;
9272 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9273 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9274 bfd_vma r_type_mask;
9275 int r_sym_shift;
9276 unsigned int count = reldata->count;
9277 struct elf_link_hash_entry **rel_hash = reldata->hashes;
9278
9279 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
9280 {
9281 swap_in = bed->s->swap_reloc_in;
9282 swap_out = bed->s->swap_reloc_out;
9283 }
9284 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
9285 {
9286 swap_in = bed->s->swap_reloca_in;
9287 swap_out = bed->s->swap_reloca_out;
9288 }
9289 else
9290 abort ();
9291
9292 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
9293 abort ();
9294
9295 if (bed->s->arch_size == 32)
9296 {
9297 r_type_mask = 0xff;
9298 r_sym_shift = 8;
9299 }
9300 else
9301 {
9302 r_type_mask = 0xffffffff;
9303 r_sym_shift = 32;
9304 }
9305
9306 erela = reldata->hdr->contents;
9307 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
9308 {
9309 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
9310 unsigned int j;
9311
9312 if (*rel_hash == NULL)
9313 continue;
9314
9315 if ((*rel_hash)->indx == -2
9316 && info->gc_sections
9317 && ! info->gc_keep_exported)
9318 {
9319 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9320 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
9321 abfd, sec,
9322 (*rel_hash)->root.root.string);
9323 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
9324 abfd, sec);
9325 bfd_set_error (bfd_error_invalid_operation);
9326 return false;
9327 }
9328 BFD_ASSERT ((*rel_hash)->indx >= 0);
9329
9330 (*swap_in) (abfd, erela, irela);
9331 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
9332 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
9333 | (irela[j].r_info & r_type_mask));
9334 (*swap_out) (abfd, irela, erela);
9335 }
9336
9337 if (bed->elf_backend_update_relocs)
9338 (*bed->elf_backend_update_relocs) (sec, reldata);
9339
9340 if (sort && count != 0)
9341 {
9342 bfd_vma (*ext_r_off) (const void *);
9343 bfd_vma r_off;
9344 size_t elt_size;
9345 bfd_byte *base, *end, *p, *loc;
9346 bfd_byte *buf = NULL;
9347
9348 if (bed->s->arch_size == 32)
9349 {
9350 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9351 ext_r_off = ext32l_r_offset;
9352 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9353 ext_r_off = ext32b_r_offset;
9354 else
9355 abort ();
9356 }
9357 else
9358 {
9359 #ifdef BFD_HOST_64_BIT
9360 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9361 ext_r_off = ext64l_r_offset;
9362 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9363 ext_r_off = ext64b_r_offset;
9364 else
9365 #endif
9366 abort ();
9367 }
9368
9369 /* Must use a stable sort here. A modified insertion sort,
9370 since the relocs are mostly sorted already. */
9371 elt_size = reldata->hdr->sh_entsize;
9372 base = reldata->hdr->contents;
9373 end = base + count * elt_size;
9374 if (elt_size > sizeof (Elf64_External_Rela))
9375 abort ();
9376
9377 /* Ensure the first element is lowest. This acts as a sentinel,
9378 speeding the main loop below. */
9379 r_off = (*ext_r_off) (base);
9380 for (p = loc = base; (p += elt_size) < end; )
9381 {
9382 bfd_vma r_off2 = (*ext_r_off) (p);
9383 if (r_off > r_off2)
9384 {
9385 r_off = r_off2;
9386 loc = p;
9387 }
9388 }
9389 if (loc != base)
9390 {
9391 /* Don't just swap *base and *loc as that changes the order
9392 of the original base[0] and base[1] if they happen to
9393 have the same r_offset. */
9394 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
9395 memcpy (onebuf, loc, elt_size);
9396 memmove (base + elt_size, base, loc - base);
9397 memcpy (base, onebuf, elt_size);
9398 }
9399
9400 for (p = base + elt_size; (p += elt_size) < end; )
9401 {
9402 /* base to p is sorted, *p is next to insert. */
9403 r_off = (*ext_r_off) (p);
9404 /* Search the sorted region for location to insert. */
9405 loc = p - elt_size;
9406 while (r_off < (*ext_r_off) (loc))
9407 loc -= elt_size;
9408 loc += elt_size;
9409 if (loc != p)
9410 {
9411 /* Chances are there is a run of relocs to insert here,
9412 from one of more input files. Files are not always
9413 linked in order due to the way elf_link_input_bfd is
9414 called. See pr17666. */
9415 size_t sortlen = p - loc;
9416 bfd_vma r_off2 = (*ext_r_off) (loc);
9417 size_t runlen = elt_size;
9418 size_t buf_size = 96 * 1024;
9419 while (p + runlen < end
9420 && (sortlen <= buf_size
9421 || runlen + elt_size <= buf_size)
9422 && r_off2 > (*ext_r_off) (p + runlen))
9423 runlen += elt_size;
9424 if (buf == NULL)
9425 {
9426 buf = bfd_malloc (buf_size);
9427 if (buf == NULL)
9428 return false;
9429 }
9430 if (runlen < sortlen)
9431 {
9432 memcpy (buf, p, runlen);
9433 memmove (loc + runlen, loc, sortlen);
9434 memcpy (loc, buf, runlen);
9435 }
9436 else
9437 {
9438 memcpy (buf, loc, sortlen);
9439 memmove (loc, p, runlen);
9440 memcpy (loc + runlen, buf, sortlen);
9441 }
9442 p += runlen - elt_size;
9443 }
9444 }
9445 /* Hashes are no longer valid. */
9446 free (reldata->hashes);
9447 reldata->hashes = NULL;
9448 free (buf);
9449 }
9450 return true;
9451 }
9452
9453 struct elf_link_sort_rela
9454 {
9455 union {
9456 bfd_vma offset;
9457 bfd_vma sym_mask;
9458 } u;
9459 enum elf_reloc_type_class type;
9460 /* We use this as an array of size int_rels_per_ext_rel. */
9461 Elf_Internal_Rela rela[1];
9462 };
9463
9464 /* qsort stability here and for cmp2 is only an issue if multiple
9465 dynamic relocations are emitted at the same address. But targets
9466 that apply a series of dynamic relocations each operating on the
9467 result of the prior relocation can't use -z combreloc as
9468 implemented anyway. Such schemes tend to be broken by sorting on
9469 symbol index. That leaves dynamic NONE relocs as the only other
9470 case where ld might emit multiple relocs at the same address, and
9471 those are only emitted due to target bugs. */
9472
9473 static int
9474 elf_link_sort_cmp1 (const void *A, const void *B)
9475 {
9476 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9477 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9478 int relativea, relativeb;
9479
9480 relativea = a->type == reloc_class_relative;
9481 relativeb = b->type == reloc_class_relative;
9482
9483 if (relativea < relativeb)
9484 return 1;
9485 if (relativea > relativeb)
9486 return -1;
9487 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
9488 return -1;
9489 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
9490 return 1;
9491 if (a->rela->r_offset < b->rela->r_offset)
9492 return -1;
9493 if (a->rela->r_offset > b->rela->r_offset)
9494 return 1;
9495 return 0;
9496 }
9497
9498 static int
9499 elf_link_sort_cmp2 (const void *A, const void *B)
9500 {
9501 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9502 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9503
9504 if (a->type < b->type)
9505 return -1;
9506 if (a->type > b->type)
9507 return 1;
9508 if (a->u.offset < b->u.offset)
9509 return -1;
9510 if (a->u.offset > b->u.offset)
9511 return 1;
9512 if (a->rela->r_offset < b->rela->r_offset)
9513 return -1;
9514 if (a->rela->r_offset > b->rela->r_offset)
9515 return 1;
9516 return 0;
9517 }
9518
9519 static size_t
9520 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9521 {
9522 asection *dynamic_relocs;
9523 asection *rela_dyn;
9524 asection *rel_dyn;
9525 bfd_size_type count, size;
9526 size_t i, ret, sort_elt, ext_size;
9527 bfd_byte *sort, *s_non_relative, *p;
9528 struct elf_link_sort_rela *sq;
9529 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9530 int i2e = bed->s->int_rels_per_ext_rel;
9531 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
9532 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9533 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9534 struct bfd_link_order *lo;
9535 bfd_vma r_sym_mask;
9536 bool use_rela;
9537
9538 /* Find a dynamic reloc section. */
9539 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9540 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
9541 if (rela_dyn != NULL && rela_dyn->size > 0
9542 && rel_dyn != NULL && rel_dyn->size > 0)
9543 {
9544 bool use_rela_initialised = false;
9545
9546 /* This is just here to stop gcc from complaining.
9547 Its initialization checking code is not perfect. */
9548 use_rela = true;
9549
9550 /* Both sections are present. Examine the sizes
9551 of the indirect sections to help us choose. */
9552 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9553 if (lo->type == bfd_indirect_link_order)
9554 {
9555 asection *o = lo->u.indirect.section;
9556
9557 if ((o->size % bed->s->sizeof_rela) == 0)
9558 {
9559 if ((o->size % bed->s->sizeof_rel) == 0)
9560 /* Section size is divisible by both rel and rela sizes.
9561 It is of no help to us. */
9562 ;
9563 else
9564 {
9565 /* Section size is only divisible by rela. */
9566 if (use_rela_initialised && !use_rela)
9567 {
9568 _bfd_error_handler (_("%pB: unable to sort relocs - "
9569 "they are in more than one size"),
9570 abfd);
9571 bfd_set_error (bfd_error_invalid_operation);
9572 return 0;
9573 }
9574 else
9575 {
9576 use_rela = true;
9577 use_rela_initialised = true;
9578 }
9579 }
9580 }
9581 else if ((o->size % bed->s->sizeof_rel) == 0)
9582 {
9583 /* Section size is only divisible by rel. */
9584 if (use_rela_initialised && use_rela)
9585 {
9586 _bfd_error_handler (_("%pB: unable to sort relocs - "
9587 "they are in more than one size"),
9588 abfd);
9589 bfd_set_error (bfd_error_invalid_operation);
9590 return 0;
9591 }
9592 else
9593 {
9594 use_rela = false;
9595 use_rela_initialised = true;
9596 }
9597 }
9598 else
9599 {
9600 /* The section size is not divisible by either -
9601 something is wrong. */
9602 _bfd_error_handler (_("%pB: unable to sort relocs - "
9603 "they are of an unknown size"), abfd);
9604 bfd_set_error (bfd_error_invalid_operation);
9605 return 0;
9606 }
9607 }
9608
9609 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9610 if (lo->type == bfd_indirect_link_order)
9611 {
9612 asection *o = lo->u.indirect.section;
9613
9614 if ((o->size % bed->s->sizeof_rela) == 0)
9615 {
9616 if ((o->size % bed->s->sizeof_rel) == 0)
9617 /* Section size is divisible by both rel and rela sizes.
9618 It is of no help to us. */
9619 ;
9620 else
9621 {
9622 /* Section size is only divisible by rela. */
9623 if (use_rela_initialised && !use_rela)
9624 {
9625 _bfd_error_handler (_("%pB: unable to sort relocs - "
9626 "they are in more than one size"),
9627 abfd);
9628 bfd_set_error (bfd_error_invalid_operation);
9629 return 0;
9630 }
9631 else
9632 {
9633 use_rela = true;
9634 use_rela_initialised = true;
9635 }
9636 }
9637 }
9638 else if ((o->size % bed->s->sizeof_rel) == 0)
9639 {
9640 /* Section size is only divisible by rel. */
9641 if (use_rela_initialised && use_rela)
9642 {
9643 _bfd_error_handler (_("%pB: unable to sort relocs - "
9644 "they are in more than one size"),
9645 abfd);
9646 bfd_set_error (bfd_error_invalid_operation);
9647 return 0;
9648 }
9649 else
9650 {
9651 use_rela = false;
9652 use_rela_initialised = true;
9653 }
9654 }
9655 else
9656 {
9657 /* The section size is not divisible by either -
9658 something is wrong. */
9659 _bfd_error_handler (_("%pB: unable to sort relocs - "
9660 "they are of an unknown size"), abfd);
9661 bfd_set_error (bfd_error_invalid_operation);
9662 return 0;
9663 }
9664 }
9665
9666 if (! use_rela_initialised)
9667 /* Make a guess. */
9668 use_rela = true;
9669 }
9670 else if (rela_dyn != NULL && rela_dyn->size > 0)
9671 use_rela = true;
9672 else if (rel_dyn != NULL && rel_dyn->size > 0)
9673 use_rela = false;
9674 else
9675 return 0;
9676
9677 if (use_rela)
9678 {
9679 dynamic_relocs = rela_dyn;
9680 ext_size = bed->s->sizeof_rela;
9681 swap_in = bed->s->swap_reloca_in;
9682 swap_out = bed->s->swap_reloca_out;
9683 }
9684 else
9685 {
9686 dynamic_relocs = rel_dyn;
9687 ext_size = bed->s->sizeof_rel;
9688 swap_in = bed->s->swap_reloc_in;
9689 swap_out = bed->s->swap_reloc_out;
9690 }
9691
9692 size = 0;
9693 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9694 if (lo->type == bfd_indirect_link_order)
9695 size += lo->u.indirect.section->size;
9696
9697 if (size != dynamic_relocs->size)
9698 return 0;
9699
9700 sort_elt = (sizeof (struct elf_link_sort_rela)
9701 + (i2e - 1) * sizeof (Elf_Internal_Rela));
9702
9703 count = dynamic_relocs->size / ext_size;
9704 if (count == 0)
9705 return 0;
9706 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
9707
9708 if (sort == NULL)
9709 {
9710 (*info->callbacks->warning)
9711 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
9712 return 0;
9713 }
9714
9715 if (bed->s->arch_size == 32)
9716 r_sym_mask = ~(bfd_vma) 0xff;
9717 else
9718 r_sym_mask = ~(bfd_vma) 0xffffffff;
9719
9720 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9721 if (lo->type == bfd_indirect_link_order)
9722 {
9723 bfd_byte *erel, *erelend;
9724 asection *o = lo->u.indirect.section;
9725
9726 if (o->contents == NULL && o->size != 0)
9727 {
9728 /* This is a reloc section that is being handled as a normal
9729 section. See bfd_section_from_shdr. We can't combine
9730 relocs in this case. */
9731 free (sort);
9732 return 0;
9733 }
9734 erel = o->contents;
9735 erelend = o->contents + o->size;
9736 p = sort + o->output_offset * opb / ext_size * sort_elt;
9737
9738 while (erel < erelend)
9739 {
9740 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9741
9742 (*swap_in) (abfd, erel, s->rela);
9743 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
9744 s->u.sym_mask = r_sym_mask;
9745 p += sort_elt;
9746 erel += ext_size;
9747 }
9748 }
9749
9750 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9751
9752 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9753 {
9754 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9755 if (s->type != reloc_class_relative)
9756 break;
9757 }
9758 ret = i;
9759 s_non_relative = p;
9760
9761 sq = (struct elf_link_sort_rela *) s_non_relative;
9762 for (; i < count; i++, p += sort_elt)
9763 {
9764 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9765 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9766 sq = sp;
9767 sp->u.offset = sq->rela->r_offset;
9768 }
9769
9770 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9771
9772 struct elf_link_hash_table *htab = elf_hash_table (info);
9773 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9774 {
9775 /* We have plt relocs in .rela.dyn. */
9776 sq = (struct elf_link_sort_rela *) sort;
9777 for (i = 0; i < count; i++)
9778 if (sq[count - i - 1].type != reloc_class_plt)
9779 break;
9780 if (i != 0 && htab->srelplt->size == i * ext_size)
9781 {
9782 struct bfd_link_order **plo;
9783 /* Put srelplt link_order last. This is so the output_offset
9784 set in the next loop is correct for DT_JMPREL. */
9785 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9786 if ((*plo)->type == bfd_indirect_link_order
9787 && (*plo)->u.indirect.section == htab->srelplt)
9788 {
9789 lo = *plo;
9790 *plo = lo->next;
9791 }
9792 else
9793 plo = &(*plo)->next;
9794 *plo = lo;
9795 lo->next = NULL;
9796 dynamic_relocs->map_tail.link_order = lo;
9797 }
9798 }
9799
9800 p = sort;
9801 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9802 if (lo->type == bfd_indirect_link_order)
9803 {
9804 bfd_byte *erel, *erelend;
9805 asection *o = lo->u.indirect.section;
9806
9807 erel = o->contents;
9808 erelend = o->contents + o->size;
9809 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
9810 while (erel < erelend)
9811 {
9812 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9813 (*swap_out) (abfd, s->rela, erel);
9814 p += sort_elt;
9815 erel += ext_size;
9816 }
9817 }
9818
9819 free (sort);
9820 *psec = dynamic_relocs;
9821 return ret;
9822 }
9823
9824 /* Add a symbol to the output symbol string table. */
9825
9826 static int
9827 elf_link_output_symstrtab (void *finf,
9828 const char *name,
9829 Elf_Internal_Sym *elfsym,
9830 asection *input_sec,
9831 struct elf_link_hash_entry *h)
9832 {
9833 struct elf_final_link_info *flinfo = finf;
9834 int (*output_symbol_hook)
9835 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9836 struct elf_link_hash_entry *);
9837 struct elf_link_hash_table *hash_table;
9838 const struct elf_backend_data *bed;
9839 bfd_size_type strtabsize;
9840
9841 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9842
9843 bed = get_elf_backend_data (flinfo->output_bfd);
9844 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9845 if (output_symbol_hook != NULL)
9846 {
9847 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
9848 if (ret != 1)
9849 return ret;
9850 }
9851
9852 if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC)
9853 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
9854 if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE)
9855 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique;
9856
9857 if (name == NULL
9858 || *name == '\0'
9859 || (input_sec->flags & SEC_EXCLUDE))
9860 elfsym->st_name = (unsigned long) -1;
9861 else
9862 {
9863 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9864 to get the final offset for st_name. */
9865 char *versioned_name = (char *) name;
9866 if (h != NULL)
9867 {
9868 if (h->versioned == versioned && h->def_dynamic)
9869 {
9870 /* Keep only one '@' for versioned symbols defined in
9871 shared objects. */
9872 char *version = strrchr (name, ELF_VER_CHR);
9873 char *base_end = strchr (name, ELF_VER_CHR);
9874 if (version != base_end)
9875 {
9876 size_t base_len;
9877 size_t len = strlen (name);
9878 versioned_name = bfd_alloc (flinfo->output_bfd, len);
9879 if (versioned_name == NULL)
9880 return 0;
9881 base_len = base_end - name;
9882 memcpy (versioned_name, name, base_len);
9883 memcpy (versioned_name + base_len, version,
9884 len - base_len);
9885 }
9886 }
9887 }
9888 else if (flinfo->info->unique_symbol
9889 && ELF_ST_BIND (elfsym->st_info) == STB_LOCAL)
9890 {
9891 struct local_hash_entry *lh;
9892 size_t count_len;
9893 size_t base_len;
9894 char buf[30];
9895 switch (ELF_ST_TYPE (elfsym->st_info))
9896 {
9897 case STT_FILE:
9898 case STT_SECTION:
9899 break;
9900 default:
9901 lh = (struct local_hash_entry *) bfd_hash_lookup
9902 (&flinfo->local_hash_table, name, true, false);
9903 if (lh == NULL)
9904 return 0;
9905 /* Always append ".COUNT" to local symbols to avoid
9906 potential conflicts with local symbol "XXX.COUNT". */
9907 sprintf (buf, "%lx", lh->count);
9908 base_len = lh->size;
9909 if (!base_len)
9910 {
9911 base_len = strlen (name);
9912 lh->size = base_len;
9913 }
9914 count_len = strlen (buf);
9915 versioned_name = bfd_alloc (flinfo->output_bfd,
9916 base_len + count_len + 2);
9917 if (versioned_name == NULL)
9918 return 0;
9919 memcpy (versioned_name, name, base_len);
9920 versioned_name[base_len] = '.';
9921 memcpy (versioned_name + base_len + 1, buf,
9922 count_len + 1);
9923 lh->count++;
9924 break;
9925 }
9926 }
9927 elfsym->st_name
9928 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
9929 versioned_name, false);
9930 if (elfsym->st_name == (unsigned long) -1)
9931 return 0;
9932 }
9933
9934 hash_table = elf_hash_table (flinfo->info);
9935 strtabsize = hash_table->strtabsize;
9936 if (strtabsize <= flinfo->output_bfd->symcount)
9937 {
9938 strtabsize += strtabsize;
9939 hash_table->strtabsize = strtabsize;
9940 strtabsize *= sizeof (*hash_table->strtab);
9941 hash_table->strtab
9942 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9943 strtabsize);
9944 if (hash_table->strtab == NULL)
9945 return 0;
9946 }
9947 hash_table->strtab[flinfo->output_bfd->symcount].sym = *elfsym;
9948 hash_table->strtab[flinfo->output_bfd->symcount].dest_index
9949 = flinfo->output_bfd->symcount;
9950 flinfo->output_bfd->symcount += 1;
9951
9952 return 1;
9953 }
9954
9955 /* Swap symbols out to the symbol table and flush the output symbols to
9956 the file. */
9957
9958 static bool
9959 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9960 {
9961 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
9962 size_t amt;
9963 size_t i;
9964 const struct elf_backend_data *bed;
9965 bfd_byte *symbuf;
9966 Elf_Internal_Shdr *hdr;
9967 file_ptr pos;
9968 bool ret;
9969
9970 if (flinfo->output_bfd->symcount == 0)
9971 return true;
9972
9973 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9974
9975 bed = get_elf_backend_data (flinfo->output_bfd);
9976
9977 amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount;
9978 symbuf = (bfd_byte *) bfd_malloc (amt);
9979 if (symbuf == NULL)
9980 return false;
9981
9982 if (flinfo->symshndxbuf)
9983 {
9984 amt = sizeof (Elf_External_Sym_Shndx);
9985 amt *= bfd_get_symcount (flinfo->output_bfd);
9986 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9987 if (flinfo->symshndxbuf == NULL)
9988 {
9989 free (symbuf);
9990 return false;
9991 }
9992 }
9993
9994 /* Now swap out the symbols. */
9995 for (i = 0; i < flinfo->output_bfd->symcount; i++)
9996 {
9997 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9998 if (elfsym->sym.st_name == (unsigned long) -1)
9999 elfsym->sym.st_name = 0;
10000 else
10001 elfsym->sym.st_name
10002 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
10003 elfsym->sym.st_name);
10004
10005 /* Inform the linker of the addition of this symbol. */
10006
10007 if (flinfo->info->callbacks->ctf_new_symbol)
10008 flinfo->info->callbacks->ctf_new_symbol (elfsym->dest_index,
10009 &elfsym->sym);
10010
10011 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
10012 ((bfd_byte *) symbuf
10013 + (elfsym->dest_index
10014 * bed->s->sizeof_sym)),
10015 NPTR_ADD (flinfo->symshndxbuf,
10016 elfsym->dest_index));
10017 }
10018
10019 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
10020 pos = hdr->sh_offset + hdr->sh_size;
10021 amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount;
10022 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
10023 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
10024 {
10025 hdr->sh_size += amt;
10026 ret = true;
10027 }
10028 else
10029 ret = false;
10030
10031 free (symbuf);
10032
10033 free (hash_table->strtab);
10034 hash_table->strtab = NULL;
10035
10036 return ret;
10037 }
10038
10039 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
10040
10041 static bool
10042 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
10043 {
10044 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
10045 && sym->st_shndx < SHN_LORESERVE)
10046 {
10047 /* The gABI doesn't support dynamic symbols in output sections
10048 beyond 64k. */
10049 _bfd_error_handler
10050 /* xgettext:c-format */
10051 (_("%pB: too many sections: %d (>= %d)"),
10052 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
10053 bfd_set_error (bfd_error_nonrepresentable_section);
10054 return false;
10055 }
10056 return true;
10057 }
10058
10059 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
10060 allowing an unsatisfied unversioned symbol in the DSO to match a
10061 versioned symbol that would normally require an explicit version.
10062 We also handle the case that a DSO references a hidden symbol
10063 which may be satisfied by a versioned symbol in another DSO. */
10064
10065 static bool
10066 elf_link_check_versioned_symbol (struct bfd_link_info *info,
10067 const struct elf_backend_data *bed,
10068 struct elf_link_hash_entry *h)
10069 {
10070 bfd *abfd;
10071 struct elf_link_loaded_list *loaded;
10072
10073 if (!is_elf_hash_table (info->hash))
10074 return false;
10075
10076 /* Check indirect symbol. */
10077 while (h->root.type == bfd_link_hash_indirect)
10078 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10079
10080 switch (h->root.type)
10081 {
10082 default:
10083 abfd = NULL;
10084 break;
10085
10086 case bfd_link_hash_undefined:
10087 case bfd_link_hash_undefweak:
10088 abfd = h->root.u.undef.abfd;
10089 if (abfd == NULL
10090 || (abfd->flags & DYNAMIC) == 0
10091 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
10092 return false;
10093 break;
10094
10095 case bfd_link_hash_defined:
10096 case bfd_link_hash_defweak:
10097 abfd = h->root.u.def.section->owner;
10098 break;
10099
10100 case bfd_link_hash_common:
10101 abfd = h->root.u.c.p->section->owner;
10102 break;
10103 }
10104 BFD_ASSERT (abfd != NULL);
10105
10106 for (loaded = elf_hash_table (info)->dyn_loaded;
10107 loaded != NULL;
10108 loaded = loaded->next)
10109 {
10110 bfd *input;
10111 Elf_Internal_Shdr *hdr;
10112 size_t symcount;
10113 size_t extsymcount;
10114 size_t extsymoff;
10115 Elf_Internal_Shdr *versymhdr;
10116 Elf_Internal_Sym *isym;
10117 Elf_Internal_Sym *isymend;
10118 Elf_Internal_Sym *isymbuf;
10119 Elf_External_Versym *ever;
10120 Elf_External_Versym *extversym;
10121
10122 input = loaded->abfd;
10123
10124 /* We check each DSO for a possible hidden versioned definition. */
10125 if (input == abfd
10126 || elf_dynversym (input) == 0)
10127 continue;
10128
10129 hdr = &elf_tdata (input)->dynsymtab_hdr;
10130
10131 symcount = hdr->sh_size / bed->s->sizeof_sym;
10132 if (elf_bad_symtab (input))
10133 {
10134 extsymcount = symcount;
10135 extsymoff = 0;
10136 }
10137 else
10138 {
10139 extsymcount = symcount - hdr->sh_info;
10140 extsymoff = hdr->sh_info;
10141 }
10142
10143 if (extsymcount == 0)
10144 continue;
10145
10146 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
10147 NULL, NULL, NULL);
10148 if (isymbuf == NULL)
10149 return false;
10150
10151 /* Read in any version definitions. */
10152 versymhdr = &elf_tdata (input)->dynversym_hdr;
10153 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
10154 || (extversym = (Elf_External_Versym *)
10155 _bfd_malloc_and_read (input, versymhdr->sh_size,
10156 versymhdr->sh_size)) == NULL)
10157 {
10158 free (isymbuf);
10159 return false;
10160 }
10161
10162 ever = extversym + extsymoff;
10163 isymend = isymbuf + extsymcount;
10164 for (isym = isymbuf; isym < isymend; isym++, ever++)
10165 {
10166 const char *name;
10167 Elf_Internal_Versym iver;
10168 unsigned short version_index;
10169
10170 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
10171 || isym->st_shndx == SHN_UNDEF)
10172 continue;
10173
10174 name = bfd_elf_string_from_elf_section (input,
10175 hdr->sh_link,
10176 isym->st_name);
10177 if (strcmp (name, h->root.root.string) != 0)
10178 continue;
10179
10180 _bfd_elf_swap_versym_in (input, ever, &iver);
10181
10182 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
10183 && !(h->def_regular
10184 && h->forced_local))
10185 {
10186 /* If we have a non-hidden versioned sym, then it should
10187 have provided a definition for the undefined sym unless
10188 it is defined in a non-shared object and forced local.
10189 */
10190 abort ();
10191 }
10192
10193 version_index = iver.vs_vers & VERSYM_VERSION;
10194 if (version_index == 1 || version_index == 2)
10195 {
10196 /* This is the base or first version. We can use it. */
10197 free (extversym);
10198 free (isymbuf);
10199 return true;
10200 }
10201 }
10202
10203 free (extversym);
10204 free (isymbuf);
10205 }
10206
10207 return false;
10208 }
10209
10210 /* Convert ELF common symbol TYPE. */
10211
10212 static int
10213 elf_link_convert_common_type (struct bfd_link_info *info, int type)
10214 {
10215 /* Commom symbol can only appear in relocatable link. */
10216 if (!bfd_link_relocatable (info))
10217 abort ();
10218 switch (info->elf_stt_common)
10219 {
10220 case unchanged:
10221 break;
10222 case elf_stt_common:
10223 type = STT_COMMON;
10224 break;
10225 case no_elf_stt_common:
10226 type = STT_OBJECT;
10227 break;
10228 }
10229 return type;
10230 }
10231
10232 /* Add an external symbol to the symbol table. This is called from
10233 the hash table traversal routine. When generating a shared object,
10234 we go through the symbol table twice. The first time we output
10235 anything that might have been forced to local scope in a version
10236 script. The second time we output the symbols that are still
10237 global symbols. */
10238
10239 static bool
10240 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
10241 {
10242 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
10243 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
10244 struct elf_final_link_info *flinfo = eoinfo->flinfo;
10245 bool strip;
10246 Elf_Internal_Sym sym;
10247 asection *input_sec;
10248 const struct elf_backend_data *bed;
10249 long indx;
10250 int ret;
10251 unsigned int type;
10252
10253 if (h->root.type == bfd_link_hash_warning)
10254 {
10255 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10256 if (h->root.type == bfd_link_hash_new)
10257 return true;
10258 }
10259
10260 /* Decide whether to output this symbol in this pass. */
10261 if (eoinfo->localsyms)
10262 {
10263 if (!h->forced_local)
10264 return true;
10265 }
10266 else
10267 {
10268 if (h->forced_local)
10269 return true;
10270 }
10271
10272 bed = get_elf_backend_data (flinfo->output_bfd);
10273
10274 if (h->root.type == bfd_link_hash_undefined)
10275 {
10276 /* If we have an undefined symbol reference here then it must have
10277 come from a shared library that is being linked in. (Undefined
10278 references in regular files have already been handled unless
10279 they are in unreferenced sections which are removed by garbage
10280 collection). */
10281 bool ignore_undef = false;
10282
10283 /* Some symbols may be special in that the fact that they're
10284 undefined can be safely ignored - let backend determine that. */
10285 if (bed->elf_backend_ignore_undef_symbol)
10286 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
10287
10288 /* If we are reporting errors for this situation then do so now. */
10289 if (!ignore_undef
10290 && h->ref_dynamic_nonweak
10291 && (!h->ref_regular || flinfo->info->gc_sections)
10292 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
10293 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
10294 {
10295 flinfo->info->callbacks->undefined_symbol
10296 (flinfo->info, h->root.root.string,
10297 h->ref_regular ? NULL : h->root.u.undef.abfd, NULL, 0,
10298 flinfo->info->unresolved_syms_in_shared_libs == RM_DIAGNOSE
10299 && !flinfo->info->warn_unresolved_syms);
10300 }
10301
10302 /* Strip a global symbol defined in a discarded section. */
10303 if (h->indx == -3)
10304 return true;
10305 }
10306
10307 /* We should also warn if a forced local symbol is referenced from
10308 shared libraries. */
10309 if (bfd_link_executable (flinfo->info)
10310 && h->forced_local
10311 && h->ref_dynamic
10312 && h->def_regular
10313 && !h->dynamic_def
10314 && h->ref_dynamic_nonweak
10315 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
10316 {
10317 bfd *def_bfd;
10318 const char *msg;
10319 struct elf_link_hash_entry *hi = h;
10320
10321 /* Check indirect symbol. */
10322 while (hi->root.type == bfd_link_hash_indirect)
10323 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
10324
10325 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
10326 /* xgettext:c-format */
10327 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
10328 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
10329 /* xgettext:c-format */
10330 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
10331 else
10332 /* xgettext:c-format */
10333 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
10334 def_bfd = flinfo->output_bfd;
10335 if (hi->root.u.def.section != bfd_abs_section_ptr)
10336 def_bfd = hi->root.u.def.section->owner;
10337 _bfd_error_handler (msg, flinfo->output_bfd,
10338 h->root.root.string, def_bfd);
10339 bfd_set_error (bfd_error_bad_value);
10340 eoinfo->failed = true;
10341 return false;
10342 }
10343
10344 /* We don't want to output symbols that have never been mentioned by
10345 a regular file, or that we have been told to strip. However, if
10346 h->indx is set to -2, the symbol is used by a reloc and we must
10347 output it. */
10348 strip = false;
10349 if (h->indx == -2)
10350 ;
10351 else if ((h->def_dynamic
10352 || h->ref_dynamic
10353 || h->root.type == bfd_link_hash_new)
10354 && !h->def_regular
10355 && !h->ref_regular)
10356 strip = true;
10357 else if (flinfo->info->strip == strip_all)
10358 strip = true;
10359 else if (flinfo->info->strip == strip_some
10360 && bfd_hash_lookup (flinfo->info->keep_hash,
10361 h->root.root.string, false, false) == NULL)
10362 strip = true;
10363 else if ((h->root.type == bfd_link_hash_defined
10364 || h->root.type == bfd_link_hash_defweak)
10365 && ((flinfo->info->strip_discarded
10366 && discarded_section (h->root.u.def.section))
10367 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
10368 && h->root.u.def.section->owner != NULL
10369 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
10370 strip = true;
10371 else if ((h->root.type == bfd_link_hash_undefined
10372 || h->root.type == bfd_link_hash_undefweak)
10373 && h->root.u.undef.abfd != NULL
10374 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
10375 strip = true;
10376
10377 type = h->type;
10378
10379 /* If we're stripping it, and it's not a dynamic symbol, there's
10380 nothing else to do. However, if it is a forced local symbol or
10381 an ifunc symbol we need to give the backend finish_dynamic_symbol
10382 function a chance to make it dynamic. */
10383 if (strip
10384 && h->dynindx == -1
10385 && type != STT_GNU_IFUNC
10386 && !h->forced_local)
10387 return true;
10388
10389 sym.st_value = 0;
10390 sym.st_size = h->size;
10391 sym.st_other = h->other;
10392 switch (h->root.type)
10393 {
10394 default:
10395 case bfd_link_hash_new:
10396 case bfd_link_hash_warning:
10397 abort ();
10398 return false;
10399
10400 case bfd_link_hash_undefined:
10401 case bfd_link_hash_undefweak:
10402 input_sec = bfd_und_section_ptr;
10403 sym.st_shndx = SHN_UNDEF;
10404 break;
10405
10406 case bfd_link_hash_defined:
10407 case bfd_link_hash_defweak:
10408 {
10409 input_sec = h->root.u.def.section;
10410 if (input_sec->output_section != NULL)
10411 {
10412 sym.st_shndx =
10413 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
10414 input_sec->output_section);
10415 if (sym.st_shndx == SHN_BAD)
10416 {
10417 _bfd_error_handler
10418 /* xgettext:c-format */
10419 (_("%pB: could not find output section %pA for input section %pA"),
10420 flinfo->output_bfd, input_sec->output_section, input_sec);
10421 bfd_set_error (bfd_error_nonrepresentable_section);
10422 eoinfo->failed = true;
10423 return false;
10424 }
10425
10426 /* ELF symbols in relocatable files are section relative,
10427 but in nonrelocatable files they are virtual
10428 addresses. */
10429 sym.st_value = h->root.u.def.value + input_sec->output_offset;
10430 if (!bfd_link_relocatable (flinfo->info))
10431 {
10432 sym.st_value += input_sec->output_section->vma;
10433 if (h->type == STT_TLS)
10434 {
10435 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
10436 if (tls_sec != NULL)
10437 sym.st_value -= tls_sec->vma;
10438 }
10439 }
10440 }
10441 else
10442 {
10443 BFD_ASSERT (input_sec->owner == NULL
10444 || (input_sec->owner->flags & DYNAMIC) != 0);
10445 sym.st_shndx = SHN_UNDEF;
10446 input_sec = bfd_und_section_ptr;
10447 }
10448 }
10449 break;
10450
10451 case bfd_link_hash_common:
10452 input_sec = h->root.u.c.p->section;
10453 sym.st_shndx = bed->common_section_index (input_sec);
10454 sym.st_value = 1 << h->root.u.c.p->alignment_power;
10455 break;
10456
10457 case bfd_link_hash_indirect:
10458 /* These symbols are created by symbol versioning. They point
10459 to the decorated version of the name. For example, if the
10460 symbol foo@@GNU_1.2 is the default, which should be used when
10461 foo is used with no version, then we add an indirect symbol
10462 foo which points to foo@@GNU_1.2. We ignore these symbols,
10463 since the indirected symbol is already in the hash table. */
10464 return true;
10465 }
10466
10467 if (type == STT_COMMON || type == STT_OBJECT)
10468 switch (h->root.type)
10469 {
10470 case bfd_link_hash_common:
10471 type = elf_link_convert_common_type (flinfo->info, type);
10472 break;
10473 case bfd_link_hash_defined:
10474 case bfd_link_hash_defweak:
10475 if (bed->common_definition (&sym))
10476 type = elf_link_convert_common_type (flinfo->info, type);
10477 else
10478 type = STT_OBJECT;
10479 break;
10480 case bfd_link_hash_undefined:
10481 case bfd_link_hash_undefweak:
10482 break;
10483 default:
10484 abort ();
10485 }
10486
10487 if (h->forced_local)
10488 {
10489 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
10490 /* Turn off visibility on local symbol. */
10491 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
10492 }
10493 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10494 else if (h->unique_global && h->def_regular)
10495 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
10496 else if (h->root.type == bfd_link_hash_undefweak
10497 || h->root.type == bfd_link_hash_defweak)
10498 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
10499 else
10500 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
10501 sym.st_target_internal = h->target_internal;
10502
10503 /* Give the processor backend a chance to tweak the symbol value,
10504 and also to finish up anything that needs to be done for this
10505 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
10506 forced local syms when non-shared is due to a historical quirk.
10507 STT_GNU_IFUNC symbol must go through PLT. */
10508 if ((h->type == STT_GNU_IFUNC
10509 && h->def_regular
10510 && !bfd_link_relocatable (flinfo->info))
10511 || ((h->dynindx != -1
10512 || h->forced_local)
10513 && ((bfd_link_pic (flinfo->info)
10514 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
10515 || h->root.type != bfd_link_hash_undefweak))
10516 || !h->forced_local)
10517 && elf_hash_table (flinfo->info)->dynamic_sections_created))
10518 {
10519 if (! ((*bed->elf_backend_finish_dynamic_symbol)
10520 (flinfo->output_bfd, flinfo->info, h, &sym)))
10521 {
10522 eoinfo->failed = true;
10523 return false;
10524 }
10525 }
10526
10527 /* If we are marking the symbol as undefined, and there are no
10528 non-weak references to this symbol from a regular object, then
10529 mark the symbol as weak undefined; if there are non-weak
10530 references, mark the symbol as strong. We can't do this earlier,
10531 because it might not be marked as undefined until the
10532 finish_dynamic_symbol routine gets through with it. */
10533 if (sym.st_shndx == SHN_UNDEF
10534 && h->ref_regular
10535 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
10536 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
10537 {
10538 int bindtype;
10539 type = ELF_ST_TYPE (sym.st_info);
10540
10541 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10542 if (type == STT_GNU_IFUNC)
10543 type = STT_FUNC;
10544
10545 if (h->ref_regular_nonweak)
10546 bindtype = STB_GLOBAL;
10547 else
10548 bindtype = STB_WEAK;
10549 sym.st_info = ELF_ST_INFO (bindtype, type);
10550 }
10551
10552 /* If this is a symbol defined in a dynamic library, don't use the
10553 symbol size from the dynamic library. Relinking an executable
10554 against a new library may introduce gratuitous changes in the
10555 executable's symbols if we keep the size. */
10556 if (sym.st_shndx == SHN_UNDEF
10557 && !h->def_regular
10558 && h->def_dynamic)
10559 sym.st_size = 0;
10560
10561 /* If a non-weak symbol with non-default visibility is not defined
10562 locally, it is a fatal error. */
10563 if (!bfd_link_relocatable (flinfo->info)
10564 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
10565 && ELF_ST_BIND (sym.st_info) != STB_WEAK
10566 && h->root.type == bfd_link_hash_undefined
10567 && !h->def_regular)
10568 {
10569 const char *msg;
10570
10571 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
10572 /* xgettext:c-format */
10573 msg = _("%pB: protected symbol `%s' isn't defined");
10574 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
10575 /* xgettext:c-format */
10576 msg = _("%pB: internal symbol `%s' isn't defined");
10577 else
10578 /* xgettext:c-format */
10579 msg = _("%pB: hidden symbol `%s' isn't defined");
10580 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
10581 bfd_set_error (bfd_error_bad_value);
10582 eoinfo->failed = true;
10583 return false;
10584 }
10585
10586 /* If this symbol should be put in the .dynsym section, then put it
10587 there now. We already know the symbol index. We also fill in
10588 the entry in the .hash section. */
10589 if (h->dynindx != -1
10590 && elf_hash_table (flinfo->info)->dynamic_sections_created
10591 && elf_hash_table (flinfo->info)->dynsym != NULL
10592 && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
10593 {
10594 bfd_byte *esym;
10595
10596 /* Since there is no version information in the dynamic string,
10597 if there is no version info in symbol version section, we will
10598 have a run-time problem if not linking executable, referenced
10599 by shared library, or not bound locally. */
10600 if (h->verinfo.verdef == NULL
10601 && (!bfd_link_executable (flinfo->info)
10602 || h->ref_dynamic
10603 || !h->def_regular))
10604 {
10605 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
10606
10607 if (p && p [1] != '\0')
10608 {
10609 _bfd_error_handler
10610 /* xgettext:c-format */
10611 (_("%pB: no symbol version section for versioned symbol `%s'"),
10612 flinfo->output_bfd, h->root.root.string);
10613 eoinfo->failed = true;
10614 return false;
10615 }
10616 }
10617
10618 sym.st_name = h->dynstr_index;
10619 esym = (elf_hash_table (flinfo->info)->dynsym->contents
10620 + h->dynindx * bed->s->sizeof_sym);
10621 if (!check_dynsym (flinfo->output_bfd, &sym))
10622 {
10623 eoinfo->failed = true;
10624 return false;
10625 }
10626
10627 /* Inform the linker of the addition of this symbol. */
10628
10629 if (flinfo->info->callbacks->ctf_new_dynsym)
10630 flinfo->info->callbacks->ctf_new_dynsym (h->dynindx, &sym);
10631
10632 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
10633
10634 if (flinfo->hash_sec != NULL)
10635 {
10636 size_t hash_entry_size;
10637 bfd_byte *bucketpos;
10638 bfd_vma chain;
10639 size_t bucketcount;
10640 size_t bucket;
10641
10642 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
10643 bucket = h->u.elf_hash_value % bucketcount;
10644
10645 hash_entry_size
10646 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
10647 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
10648 + (bucket + 2) * hash_entry_size);
10649 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
10650 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
10651 bucketpos);
10652 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
10653 ((bfd_byte *) flinfo->hash_sec->contents
10654 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
10655 }
10656
10657 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
10658 {
10659 Elf_Internal_Versym iversym;
10660 Elf_External_Versym *eversym;
10661
10662 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
10663 {
10664 if (h->verinfo.verdef == NULL
10665 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
10666 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
10667 iversym.vs_vers = 1;
10668 else
10669 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
10670 }
10671 else
10672 {
10673 if (h->verinfo.vertree == NULL)
10674 iversym.vs_vers = 1;
10675 else
10676 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
10677 if (flinfo->info->create_default_symver)
10678 iversym.vs_vers++;
10679 }
10680
10681 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
10682 defined locally. */
10683 if (h->versioned == versioned_hidden && h->def_regular)
10684 iversym.vs_vers |= VERSYM_HIDDEN;
10685
10686 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
10687 eversym += h->dynindx;
10688 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
10689 }
10690 }
10691
10692 /* If the symbol is undefined, and we didn't output it to .dynsym,
10693 strip it from .symtab too. Obviously we can't do this for
10694 relocatable output or when needed for --emit-relocs. */
10695 else if (input_sec == bfd_und_section_ptr
10696 && h->indx != -2
10697 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10698 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
10699 && !bfd_link_relocatable (flinfo->info))
10700 return true;
10701
10702 /* Also strip others that we couldn't earlier due to dynamic symbol
10703 processing. */
10704 if (strip)
10705 return true;
10706 if ((input_sec->flags & SEC_EXCLUDE) != 0)
10707 return true;
10708
10709 /* Output a FILE symbol so that following locals are not associated
10710 with the wrong input file. We need one for forced local symbols
10711 if we've seen more than one FILE symbol or when we have exactly
10712 one FILE symbol but global symbols are present in a file other
10713 than the one with the FILE symbol. We also need one if linker
10714 defined symbols are present. In practice these conditions are
10715 always met, so just emit the FILE symbol unconditionally. */
10716 if (eoinfo->localsyms
10717 && !eoinfo->file_sym_done
10718 && eoinfo->flinfo->filesym_count != 0)
10719 {
10720 Elf_Internal_Sym fsym;
10721
10722 memset (&fsym, 0, sizeof (fsym));
10723 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10724 fsym.st_shndx = SHN_ABS;
10725 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10726 bfd_und_section_ptr, NULL))
10727 return false;
10728
10729 eoinfo->file_sym_done = true;
10730 }
10731
10732 indx = bfd_get_symcount (flinfo->output_bfd);
10733 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10734 input_sec, h);
10735 if (ret == 0)
10736 {
10737 eoinfo->failed = true;
10738 return false;
10739 }
10740 else if (ret == 1)
10741 h->indx = indx;
10742 else if (h->indx == -2)
10743 abort();
10744
10745 return true;
10746 }
10747
10748 /* Return TRUE if special handling is done for relocs in SEC against
10749 symbols defined in discarded sections. */
10750
10751 static bool
10752 elf_section_ignore_discarded_relocs (asection *sec)
10753 {
10754 const struct elf_backend_data *bed;
10755
10756 switch (sec->sec_info_type)
10757 {
10758 case SEC_INFO_TYPE_STABS:
10759 case SEC_INFO_TYPE_EH_FRAME:
10760 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10761 return true;
10762 default:
10763 break;
10764 }
10765
10766 bed = get_elf_backend_data (sec->owner);
10767 if (bed->elf_backend_ignore_discarded_relocs != NULL
10768 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10769 return true;
10770
10771 return false;
10772 }
10773
10774 /* Return a mask saying how ld should treat relocations in SEC against
10775 symbols defined in discarded sections. If this function returns
10776 COMPLAIN set, ld will issue a warning message. If this function
10777 returns PRETEND set, and the discarded section was link-once and the
10778 same size as the kept link-once section, ld will pretend that the
10779 symbol was actually defined in the kept section. Otherwise ld will
10780 zero the reloc (at least that is the intent, but some cooperation by
10781 the target dependent code is needed, particularly for REL targets). */
10782
10783 unsigned int
10784 _bfd_elf_default_action_discarded (asection *sec)
10785 {
10786 if (sec->flags & SEC_DEBUGGING)
10787 return PRETEND;
10788
10789 if (strcmp (".eh_frame", sec->name) == 0)
10790 return 0;
10791
10792 if (strcmp (".gcc_except_table", sec->name) == 0)
10793 return 0;
10794
10795 return COMPLAIN | PRETEND;
10796 }
10797
10798 /* Find a match between a section and a member of a section group. */
10799
10800 static asection *
10801 match_group_member (asection *sec, asection *group,
10802 struct bfd_link_info *info)
10803 {
10804 asection *first = elf_next_in_group (group);
10805 asection *s = first;
10806
10807 while (s != NULL)
10808 {
10809 if (bfd_elf_match_symbols_in_sections (s, sec, info))
10810 return s;
10811
10812 s = elf_next_in_group (s);
10813 if (s == first)
10814 break;
10815 }
10816
10817 return NULL;
10818 }
10819
10820 /* Check if the kept section of a discarded section SEC can be used
10821 to replace it. Return the replacement if it is OK. Otherwise return
10822 NULL. */
10823
10824 asection *
10825 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
10826 {
10827 asection *kept;
10828
10829 kept = sec->kept_section;
10830 if (kept != NULL)
10831 {
10832 if ((kept->flags & SEC_GROUP) != 0)
10833 kept = match_group_member (sec, kept, info);
10834 if (kept != NULL)
10835 {
10836 if ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10837 != (kept->rawsize != 0 ? kept->rawsize : kept->size))
10838 kept = NULL;
10839 else
10840 {
10841 /* Get the real kept section. */
10842 asection *next;
10843 for (next = kept->kept_section;
10844 next != NULL;
10845 next = next->kept_section)
10846 kept = next;
10847 }
10848 }
10849 sec->kept_section = kept;
10850 }
10851 return kept;
10852 }
10853
10854 /* Link an input file into the linker output file. This function
10855 handles all the sections and relocations of the input file at once.
10856 This is so that we only have to read the local symbols once, and
10857 don't have to keep them in memory. */
10858
10859 static bool
10860 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
10861 {
10862 int (*relocate_section)
10863 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10864 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10865 bfd *output_bfd;
10866 Elf_Internal_Shdr *symtab_hdr;
10867 size_t locsymcount;
10868 size_t extsymoff;
10869 Elf_Internal_Sym *isymbuf;
10870 Elf_Internal_Sym *isym;
10871 Elf_Internal_Sym *isymend;
10872 long *pindex;
10873 asection **ppsection;
10874 asection *o;
10875 const struct elf_backend_data *bed;
10876 struct elf_link_hash_entry **sym_hashes;
10877 bfd_size_type address_size;
10878 bfd_vma r_type_mask;
10879 int r_sym_shift;
10880 bool have_file_sym = false;
10881
10882 output_bfd = flinfo->output_bfd;
10883 bed = get_elf_backend_data (output_bfd);
10884 relocate_section = bed->elf_backend_relocate_section;
10885
10886 /* If this is a dynamic object, we don't want to do anything here:
10887 we don't want the local symbols, and we don't want the section
10888 contents. */
10889 if ((input_bfd->flags & DYNAMIC) != 0)
10890 return true;
10891
10892 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10893 if (elf_bad_symtab (input_bfd))
10894 {
10895 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10896 extsymoff = 0;
10897 }
10898 else
10899 {
10900 locsymcount = symtab_hdr->sh_info;
10901 extsymoff = symtab_hdr->sh_info;
10902 }
10903
10904 /* Enable GNU OSABI features in the output BFD that are used in the input
10905 BFD. */
10906 if (bed->elf_osabi == ELFOSABI_NONE
10907 || bed->elf_osabi == ELFOSABI_GNU
10908 || bed->elf_osabi == ELFOSABI_FREEBSD)
10909 elf_tdata (output_bfd)->has_gnu_osabi
10910 |= (elf_tdata (input_bfd)->has_gnu_osabi
10911 & (bfd_link_relocatable (flinfo->info)
10912 ? -1 : ~elf_gnu_osabi_retain));
10913
10914 /* Read the local symbols. */
10915 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10916 if (isymbuf == NULL && locsymcount != 0)
10917 {
10918 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
10919 flinfo->internal_syms,
10920 flinfo->external_syms,
10921 flinfo->locsym_shndx);
10922 if (isymbuf == NULL)
10923 return false;
10924 }
10925
10926 /* Find local symbol sections and adjust values of symbols in
10927 SEC_MERGE sections. Write out those local symbols we know are
10928 going into the output file. */
10929 isymend = PTR_ADD (isymbuf, locsymcount);
10930 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
10931 isym < isymend;
10932 isym++, pindex++, ppsection++)
10933 {
10934 asection *isec;
10935 const char *name;
10936 Elf_Internal_Sym osym;
10937 long indx;
10938 int ret;
10939
10940 *pindex = -1;
10941
10942 if (elf_bad_symtab (input_bfd))
10943 {
10944 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10945 {
10946 *ppsection = NULL;
10947 continue;
10948 }
10949 }
10950
10951 if (isym->st_shndx == SHN_UNDEF)
10952 isec = bfd_und_section_ptr;
10953 else if (isym->st_shndx == SHN_ABS)
10954 isec = bfd_abs_section_ptr;
10955 else if (isym->st_shndx == SHN_COMMON)
10956 isec = bfd_com_section_ptr;
10957 else
10958 {
10959 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10960 if (isec == NULL)
10961 {
10962 /* Don't attempt to output symbols with st_shnx in the
10963 reserved range other than SHN_ABS and SHN_COMMON. */
10964 isec = bfd_und_section_ptr;
10965 }
10966 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
10967 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10968 isym->st_value =
10969 _bfd_merged_section_offset (output_bfd, &isec,
10970 elf_section_data (isec)->sec_info,
10971 isym->st_value);
10972 }
10973
10974 *ppsection = isec;
10975
10976 /* Don't output the first, undefined, symbol. In fact, don't
10977 output any undefined local symbol. */
10978 if (isec == bfd_und_section_ptr)
10979 continue;
10980
10981 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10982 {
10983 /* We never output section symbols. Instead, we use the
10984 section symbol of the corresponding section in the output
10985 file. */
10986 continue;
10987 }
10988
10989 /* If we are stripping all symbols, we don't want to output this
10990 one. */
10991 if (flinfo->info->strip == strip_all)
10992 continue;
10993
10994 /* If we are discarding all local symbols, we don't want to
10995 output this one. If we are generating a relocatable output
10996 file, then some of the local symbols may be required by
10997 relocs; we output them below as we discover that they are
10998 needed. */
10999 if (flinfo->info->discard == discard_all)
11000 continue;
11001
11002 /* If this symbol is defined in a section which we are
11003 discarding, we don't need to keep it. */
11004 if (isym->st_shndx != SHN_UNDEF
11005 && isym->st_shndx < SHN_LORESERVE
11006 && isec->output_section == NULL
11007 && flinfo->info->non_contiguous_regions
11008 && flinfo->info->non_contiguous_regions_warnings)
11009 {
11010 _bfd_error_handler (_("warning: --enable-non-contiguous-regions "
11011 "discards section `%s' from '%s'\n"),
11012 isec->name, bfd_get_filename (isec->owner));
11013 continue;
11014 }
11015
11016 if (isym->st_shndx != SHN_UNDEF
11017 && isym->st_shndx < SHN_LORESERVE
11018 && bfd_section_removed_from_list (output_bfd,
11019 isec->output_section))
11020 continue;
11021
11022 /* Get the name of the symbol. */
11023 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
11024 isym->st_name);
11025 if (name == NULL)
11026 return false;
11027
11028 /* See if we are discarding symbols with this name. */
11029 if ((flinfo->info->strip == strip_some
11030 && (bfd_hash_lookup (flinfo->info->keep_hash, name, false, false)
11031 == NULL))
11032 || (((flinfo->info->discard == discard_sec_merge
11033 && (isec->flags & SEC_MERGE)
11034 && !bfd_link_relocatable (flinfo->info))
11035 || flinfo->info->discard == discard_l)
11036 && bfd_is_local_label_name (input_bfd, name)))
11037 continue;
11038
11039 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
11040 {
11041 if (input_bfd->lto_output)
11042 /* -flto puts a temp file name here. This means builds
11043 are not reproducible. Discard the symbol. */
11044 continue;
11045 have_file_sym = true;
11046 flinfo->filesym_count += 1;
11047 }
11048 if (!have_file_sym)
11049 {
11050 /* In the absence of debug info, bfd_find_nearest_line uses
11051 FILE symbols to determine the source file for local
11052 function symbols. Provide a FILE symbol here if input
11053 files lack such, so that their symbols won't be
11054 associated with a previous input file. It's not the
11055 source file, but the best we can do. */
11056 const char *filename;
11057 have_file_sym = true;
11058 flinfo->filesym_count += 1;
11059 memset (&osym, 0, sizeof (osym));
11060 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
11061 osym.st_shndx = SHN_ABS;
11062 if (input_bfd->lto_output)
11063 filename = NULL;
11064 else
11065 filename = lbasename (bfd_get_filename (input_bfd));
11066 if (!elf_link_output_symstrtab (flinfo, filename, &osym,
11067 bfd_abs_section_ptr, NULL))
11068 return false;
11069 }
11070
11071 osym = *isym;
11072
11073 /* Adjust the section index for the output file. */
11074 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11075 isec->output_section);
11076 if (osym.st_shndx == SHN_BAD)
11077 return false;
11078
11079 /* ELF symbols in relocatable files are section relative, but
11080 in executable files they are virtual addresses. Note that
11081 this code assumes that all ELF sections have an associated
11082 BFD section with a reasonable value for output_offset; below
11083 we assume that they also have a reasonable value for
11084 output_section. Any special sections must be set up to meet
11085 these requirements. */
11086 osym.st_value += isec->output_offset;
11087 if (!bfd_link_relocatable (flinfo->info))
11088 {
11089 osym.st_value += isec->output_section->vma;
11090 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
11091 {
11092 /* STT_TLS symbols are relative to PT_TLS segment base. */
11093 if (elf_hash_table (flinfo->info)->tls_sec != NULL)
11094 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
11095 else
11096 osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
11097 STT_NOTYPE);
11098 }
11099 }
11100
11101 indx = bfd_get_symcount (output_bfd);
11102 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
11103 if (ret == 0)
11104 return false;
11105 else if (ret == 1)
11106 *pindex = indx;
11107 }
11108
11109 if (bed->s->arch_size == 32)
11110 {
11111 r_type_mask = 0xff;
11112 r_sym_shift = 8;
11113 address_size = 4;
11114 }
11115 else
11116 {
11117 r_type_mask = 0xffffffff;
11118 r_sym_shift = 32;
11119 address_size = 8;
11120 }
11121
11122 /* Relocate the contents of each section. */
11123 sym_hashes = elf_sym_hashes (input_bfd);
11124 for (o = input_bfd->sections; o != NULL; o = o->next)
11125 {
11126 bfd_byte *contents;
11127
11128 if (! o->linker_mark)
11129 {
11130 /* This section was omitted from the link. */
11131 continue;
11132 }
11133
11134 if (!flinfo->info->resolve_section_groups
11135 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
11136 {
11137 /* Deal with the group signature symbol. */
11138 struct bfd_elf_section_data *sec_data = elf_section_data (o);
11139 unsigned long symndx = sec_data->this_hdr.sh_info;
11140 asection *osec = o->output_section;
11141
11142 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
11143 if (symndx >= locsymcount
11144 || (elf_bad_symtab (input_bfd)
11145 && flinfo->sections[symndx] == NULL))
11146 {
11147 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
11148 while (h->root.type == bfd_link_hash_indirect
11149 || h->root.type == bfd_link_hash_warning)
11150 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11151 /* Arrange for symbol to be output. */
11152 h->indx = -2;
11153 elf_section_data (osec)->this_hdr.sh_info = -2;
11154 }
11155 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
11156 {
11157 /* We'll use the output section target_index. */
11158 asection *sec = flinfo->sections[symndx]->output_section;
11159 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
11160 }
11161 else
11162 {
11163 if (flinfo->indices[symndx] == -1)
11164 {
11165 /* Otherwise output the local symbol now. */
11166 Elf_Internal_Sym sym = isymbuf[symndx];
11167 asection *sec = flinfo->sections[symndx]->output_section;
11168 const char *name;
11169 long indx;
11170 int ret;
11171
11172 name = bfd_elf_string_from_elf_section (input_bfd,
11173 symtab_hdr->sh_link,
11174 sym.st_name);
11175 if (name == NULL)
11176 return false;
11177
11178 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11179 sec);
11180 if (sym.st_shndx == SHN_BAD)
11181 return false;
11182
11183 sym.st_value += o->output_offset;
11184
11185 indx = bfd_get_symcount (output_bfd);
11186 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
11187 NULL);
11188 if (ret == 0)
11189 return false;
11190 else if (ret == 1)
11191 flinfo->indices[symndx] = indx;
11192 else
11193 abort ();
11194 }
11195 elf_section_data (osec)->this_hdr.sh_info
11196 = flinfo->indices[symndx];
11197 }
11198 }
11199
11200 if ((o->flags & SEC_HAS_CONTENTS) == 0
11201 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
11202 continue;
11203
11204 if ((o->flags & SEC_LINKER_CREATED) != 0)
11205 {
11206 /* Section was created by _bfd_elf_link_create_dynamic_sections
11207 or somesuch. */
11208 continue;
11209 }
11210
11211 /* Get the contents of the section. They have been cached by a
11212 relaxation routine. Note that o is a section in an input
11213 file, so the contents field will not have been set by any of
11214 the routines which work on output files. */
11215 if (elf_section_data (o)->this_hdr.contents != NULL)
11216 {
11217 contents = elf_section_data (o)->this_hdr.contents;
11218 if (bed->caches_rawsize
11219 && o->rawsize != 0
11220 && o->rawsize < o->size)
11221 {
11222 memcpy (flinfo->contents, contents, o->rawsize);
11223 contents = flinfo->contents;
11224 }
11225 }
11226 else
11227 {
11228 contents = flinfo->contents;
11229 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
11230 return false;
11231 }
11232
11233 if ((o->flags & SEC_RELOC) != 0)
11234 {
11235 Elf_Internal_Rela *internal_relocs;
11236 Elf_Internal_Rela *rel, *relend;
11237 int action_discarded;
11238 int ret;
11239
11240 /* Get the swapped relocs. */
11241 internal_relocs
11242 = _bfd_elf_link_info_read_relocs (input_bfd, flinfo->info, o,
11243 flinfo->external_relocs,
11244 flinfo->internal_relocs,
11245 false);
11246 if (internal_relocs == NULL
11247 && o->reloc_count > 0)
11248 return false;
11249
11250 /* We need to reverse-copy input .ctors/.dtors sections if
11251 they are placed in .init_array/.finit_array for output. */
11252 if (o->size > address_size
11253 && ((startswith (o->name, ".ctors")
11254 && strcmp (o->output_section->name,
11255 ".init_array") == 0)
11256 || (startswith (o->name, ".dtors")
11257 && strcmp (o->output_section->name,
11258 ".fini_array") == 0))
11259 && (o->name[6] == 0 || o->name[6] == '.'))
11260 {
11261 if (o->size * bed->s->int_rels_per_ext_rel
11262 != o->reloc_count * address_size)
11263 {
11264 _bfd_error_handler
11265 /* xgettext:c-format */
11266 (_("error: %pB: size of section %pA is not "
11267 "multiple of address size"),
11268 input_bfd, o);
11269 bfd_set_error (bfd_error_bad_value);
11270 return false;
11271 }
11272 o->flags |= SEC_ELF_REVERSE_COPY;
11273 }
11274
11275 action_discarded = -1;
11276 if (!elf_section_ignore_discarded_relocs (o))
11277 action_discarded = (*bed->action_discarded) (o);
11278
11279 /* Run through the relocs evaluating complex reloc symbols and
11280 looking for relocs against symbols from discarded sections
11281 or section symbols from removed link-once sections.
11282 Complain about relocs against discarded sections. Zero
11283 relocs against removed link-once sections. */
11284
11285 rel = internal_relocs;
11286 relend = rel + o->reloc_count;
11287 for ( ; rel < relend; rel++)
11288 {
11289 unsigned long r_symndx = rel->r_info >> r_sym_shift;
11290 unsigned int s_type;
11291 asection **ps, *sec;
11292 struct elf_link_hash_entry *h = NULL;
11293 const char *sym_name;
11294
11295 if (r_symndx == STN_UNDEF)
11296 continue;
11297
11298 if (r_symndx >= locsymcount
11299 || (elf_bad_symtab (input_bfd)
11300 && flinfo->sections[r_symndx] == NULL))
11301 {
11302 h = sym_hashes[r_symndx - extsymoff];
11303
11304 /* Badly formatted input files can contain relocs that
11305 reference non-existant symbols. Check here so that
11306 we do not seg fault. */
11307 if (h == NULL)
11308 {
11309 _bfd_error_handler
11310 /* xgettext:c-format */
11311 (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
11312 "that references a non-existent global symbol"),
11313 input_bfd, (uint64_t) rel->r_info, o);
11314 bfd_set_error (bfd_error_bad_value);
11315 return false;
11316 }
11317
11318 while (h->root.type == bfd_link_hash_indirect
11319 || h->root.type == bfd_link_hash_warning)
11320 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11321
11322 s_type = h->type;
11323
11324 /* If a plugin symbol is referenced from a non-IR file,
11325 mark the symbol as undefined. Note that the
11326 linker may attach linker created dynamic sections
11327 to the plugin bfd. Symbols defined in linker
11328 created sections are not plugin symbols. */
11329 if ((h->root.non_ir_ref_regular
11330 || h->root.non_ir_ref_dynamic)
11331 && (h->root.type == bfd_link_hash_defined
11332 || h->root.type == bfd_link_hash_defweak)
11333 && (h->root.u.def.section->flags
11334 & SEC_LINKER_CREATED) == 0
11335 && h->root.u.def.section->owner != NULL
11336 && (h->root.u.def.section->owner->flags
11337 & BFD_PLUGIN) != 0)
11338 {
11339 h->root.type = bfd_link_hash_undefined;
11340 h->root.u.undef.abfd = h->root.u.def.section->owner;
11341 }
11342
11343 ps = NULL;
11344 if (h->root.type == bfd_link_hash_defined
11345 || h->root.type == bfd_link_hash_defweak)
11346 ps = &h->root.u.def.section;
11347
11348 sym_name = h->root.root.string;
11349 }
11350 else
11351 {
11352 Elf_Internal_Sym *sym = isymbuf + r_symndx;
11353
11354 s_type = ELF_ST_TYPE (sym->st_info);
11355 ps = &flinfo->sections[r_symndx];
11356 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
11357 sym, *ps);
11358 }
11359
11360 if ((s_type == STT_RELC || s_type == STT_SRELC)
11361 && !bfd_link_relocatable (flinfo->info))
11362 {
11363 bfd_vma val;
11364 bfd_vma dot = (rel->r_offset
11365 + o->output_offset + o->output_section->vma);
11366 #ifdef DEBUG
11367 printf ("Encountered a complex symbol!");
11368 printf (" (input_bfd %s, section %s, reloc %ld\n",
11369 bfd_get_filename (input_bfd), o->name,
11370 (long) (rel - internal_relocs));
11371 printf (" symbol: idx %8.8lx, name %s\n",
11372 r_symndx, sym_name);
11373 printf (" reloc : info %8.8lx, addr %8.8lx\n",
11374 (unsigned long) rel->r_info,
11375 (unsigned long) rel->r_offset);
11376 #endif
11377 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
11378 isymbuf, locsymcount, s_type == STT_SRELC))
11379 return false;
11380
11381 /* Symbol evaluated OK. Update to absolute value. */
11382 set_symbol_value (input_bfd, isymbuf, locsymcount,
11383 r_symndx, val);
11384 continue;
11385 }
11386
11387 if (action_discarded != -1 && ps != NULL)
11388 {
11389 /* Complain if the definition comes from a
11390 discarded section. */
11391 if ((sec = *ps) != NULL && discarded_section (sec))
11392 {
11393 BFD_ASSERT (r_symndx != STN_UNDEF);
11394 if (action_discarded & COMPLAIN)
11395 (*flinfo->info->callbacks->einfo)
11396 /* xgettext:c-format */
11397 (_("%X`%s' referenced in section `%pA' of %pB: "
11398 "defined in discarded section `%pA' of %pB\n"),
11399 sym_name, o, input_bfd, sec, sec->owner);
11400
11401 /* Try to do the best we can to support buggy old
11402 versions of gcc. Pretend that the symbol is
11403 really defined in the kept linkonce section.
11404 FIXME: This is quite broken. Modifying the
11405 symbol here means we will be changing all later
11406 uses of the symbol, not just in this section. */
11407 if (action_discarded & PRETEND)
11408 {
11409 asection *kept;
11410
11411 kept = _bfd_elf_check_kept_section (sec,
11412 flinfo->info);
11413 if (kept != NULL)
11414 {
11415 *ps = kept;
11416 continue;
11417 }
11418 }
11419 }
11420 }
11421 }
11422
11423 /* Relocate the section by invoking a back end routine.
11424
11425 The back end routine is responsible for adjusting the
11426 section contents as necessary, and (if using Rela relocs
11427 and generating a relocatable output file) adjusting the
11428 reloc addend as necessary.
11429
11430 The back end routine does not have to worry about setting
11431 the reloc address or the reloc symbol index.
11432
11433 The back end routine is given a pointer to the swapped in
11434 internal symbols, and can access the hash table entries
11435 for the external symbols via elf_sym_hashes (input_bfd).
11436
11437 When generating relocatable output, the back end routine
11438 must handle STB_LOCAL/STT_SECTION symbols specially. The
11439 output symbol is going to be a section symbol
11440 corresponding to the output section, which will require
11441 the addend to be adjusted. */
11442
11443 ret = (*relocate_section) (output_bfd, flinfo->info,
11444 input_bfd, o, contents,
11445 internal_relocs,
11446 isymbuf,
11447 flinfo->sections);
11448 if (!ret)
11449 return false;
11450
11451 if (ret == 2
11452 || bfd_link_relocatable (flinfo->info)
11453 || flinfo->info->emitrelocations)
11454 {
11455 Elf_Internal_Rela *irela;
11456 Elf_Internal_Rela *irelaend, *irelamid;
11457 bfd_vma last_offset;
11458 struct elf_link_hash_entry **rel_hash;
11459 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
11460 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
11461 unsigned int next_erel;
11462 bool rela_normal;
11463 struct bfd_elf_section_data *esdi, *esdo;
11464
11465 esdi = elf_section_data (o);
11466 esdo = elf_section_data (o->output_section);
11467 rela_normal = false;
11468
11469 /* Adjust the reloc addresses and symbol indices. */
11470
11471 irela = internal_relocs;
11472 irelaend = irela + o->reloc_count;
11473 rel_hash = PTR_ADD (esdo->rel.hashes, esdo->rel.count);
11474 /* We start processing the REL relocs, if any. When we reach
11475 IRELAMID in the loop, we switch to the RELA relocs. */
11476 irelamid = irela;
11477 if (esdi->rel.hdr != NULL)
11478 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
11479 * bed->s->int_rels_per_ext_rel);
11480 rel_hash_list = rel_hash;
11481 rela_hash_list = NULL;
11482 last_offset = o->output_offset;
11483 if (!bfd_link_relocatable (flinfo->info))
11484 last_offset += o->output_section->vma;
11485 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
11486 {
11487 unsigned long r_symndx;
11488 asection *sec;
11489 Elf_Internal_Sym sym;
11490
11491 if (next_erel == bed->s->int_rels_per_ext_rel)
11492 {
11493 rel_hash++;
11494 next_erel = 0;
11495 }
11496
11497 if (irela == irelamid)
11498 {
11499 rel_hash = PTR_ADD (esdo->rela.hashes, esdo->rela.count);
11500 rela_hash_list = rel_hash;
11501 rela_normal = bed->rela_normal;
11502 }
11503
11504 irela->r_offset = _bfd_elf_section_offset (output_bfd,
11505 flinfo->info, o,
11506 irela->r_offset);
11507 if (irela->r_offset >= (bfd_vma) -2)
11508 {
11509 /* This is a reloc for a deleted entry or somesuch.
11510 Turn it into an R_*_NONE reloc, at the same
11511 offset as the last reloc. elf_eh_frame.c and
11512 bfd_elf_discard_info rely on reloc offsets
11513 being ordered. */
11514 irela->r_offset = last_offset;
11515 irela->r_info = 0;
11516 irela->r_addend = 0;
11517 continue;
11518 }
11519
11520 irela->r_offset += o->output_offset;
11521
11522 /* Relocs in an executable have to be virtual addresses. */
11523 if (!bfd_link_relocatable (flinfo->info))
11524 irela->r_offset += o->output_section->vma;
11525
11526 last_offset = irela->r_offset;
11527
11528 r_symndx = irela->r_info >> r_sym_shift;
11529 if (r_symndx == STN_UNDEF)
11530 continue;
11531
11532 if (r_symndx >= locsymcount
11533 || (elf_bad_symtab (input_bfd)
11534 && flinfo->sections[r_symndx] == NULL))
11535 {
11536 struct elf_link_hash_entry *rh;
11537 unsigned long indx;
11538
11539 /* This is a reloc against a global symbol. We
11540 have not yet output all the local symbols, so
11541 we do not know the symbol index of any global
11542 symbol. We set the rel_hash entry for this
11543 reloc to point to the global hash table entry
11544 for this symbol. The symbol index is then
11545 set at the end of bfd_elf_final_link. */
11546 indx = r_symndx - extsymoff;
11547 rh = elf_sym_hashes (input_bfd)[indx];
11548 while (rh->root.type == bfd_link_hash_indirect
11549 || rh->root.type == bfd_link_hash_warning)
11550 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
11551
11552 /* Setting the index to -2 tells
11553 elf_link_output_extsym that this symbol is
11554 used by a reloc. */
11555 BFD_ASSERT (rh->indx < 0);
11556 rh->indx = -2;
11557 *rel_hash = rh;
11558
11559 continue;
11560 }
11561
11562 /* This is a reloc against a local symbol. */
11563
11564 *rel_hash = NULL;
11565 sym = isymbuf[r_symndx];
11566 sec = flinfo->sections[r_symndx];
11567 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
11568 {
11569 /* I suppose the backend ought to fill in the
11570 section of any STT_SECTION symbol against a
11571 processor specific section. */
11572 r_symndx = STN_UNDEF;
11573 if (bfd_is_abs_section (sec))
11574 ;
11575 else if (sec == NULL || sec->owner == NULL)
11576 {
11577 bfd_set_error (bfd_error_bad_value);
11578 return false;
11579 }
11580 else
11581 {
11582 asection *osec = sec->output_section;
11583
11584 /* If we have discarded a section, the output
11585 section will be the absolute section. In
11586 case of discarded SEC_MERGE sections, use
11587 the kept section. relocate_section should
11588 have already handled discarded linkonce
11589 sections. */
11590 if (bfd_is_abs_section (osec)
11591 && sec->kept_section != NULL
11592 && sec->kept_section->output_section != NULL)
11593 {
11594 osec = sec->kept_section->output_section;
11595 irela->r_addend -= osec->vma;
11596 }
11597
11598 if (!bfd_is_abs_section (osec))
11599 {
11600 r_symndx = osec->target_index;
11601 if (r_symndx == STN_UNDEF)
11602 {
11603 irela->r_addend += osec->vma;
11604 osec = _bfd_nearby_section (output_bfd, osec,
11605 osec->vma);
11606 irela->r_addend -= osec->vma;
11607 r_symndx = osec->target_index;
11608 }
11609 }
11610 }
11611
11612 /* Adjust the addend according to where the
11613 section winds up in the output section. */
11614 if (rela_normal)
11615 irela->r_addend += sec->output_offset;
11616 }
11617 else
11618 {
11619 if (flinfo->indices[r_symndx] == -1)
11620 {
11621 unsigned long shlink;
11622 const char *name;
11623 asection *osec;
11624 long indx;
11625
11626 if (flinfo->info->strip == strip_all)
11627 {
11628 /* You can't do ld -r -s. */
11629 bfd_set_error (bfd_error_invalid_operation);
11630 return false;
11631 }
11632
11633 /* This symbol was skipped earlier, but
11634 since it is needed by a reloc, we
11635 must output it now. */
11636 shlink = symtab_hdr->sh_link;
11637 name = (bfd_elf_string_from_elf_section
11638 (input_bfd, shlink, sym.st_name));
11639 if (name == NULL)
11640 return false;
11641
11642 osec = sec->output_section;
11643 sym.st_shndx =
11644 _bfd_elf_section_from_bfd_section (output_bfd,
11645 osec);
11646 if (sym.st_shndx == SHN_BAD)
11647 return false;
11648
11649 sym.st_value += sec->output_offset;
11650 if (!bfd_link_relocatable (flinfo->info))
11651 {
11652 sym.st_value += osec->vma;
11653 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
11654 {
11655 struct elf_link_hash_table *htab
11656 = elf_hash_table (flinfo->info);
11657
11658 /* STT_TLS symbols are relative to PT_TLS
11659 segment base. */
11660 if (htab->tls_sec != NULL)
11661 sym.st_value -= htab->tls_sec->vma;
11662 else
11663 sym.st_info
11664 = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
11665 STT_NOTYPE);
11666 }
11667 }
11668
11669 indx = bfd_get_symcount (output_bfd);
11670 ret = elf_link_output_symstrtab (flinfo, name,
11671 &sym, sec,
11672 NULL);
11673 if (ret == 0)
11674 return false;
11675 else if (ret == 1)
11676 flinfo->indices[r_symndx] = indx;
11677 else
11678 abort ();
11679 }
11680
11681 r_symndx = flinfo->indices[r_symndx];
11682 }
11683
11684 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
11685 | (irela->r_info & r_type_mask));
11686 }
11687
11688 /* Swap out the relocs. */
11689 input_rel_hdr = esdi->rel.hdr;
11690 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
11691 {
11692 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11693 input_rel_hdr,
11694 internal_relocs,
11695 rel_hash_list))
11696 return false;
11697 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
11698 * bed->s->int_rels_per_ext_rel);
11699 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
11700 }
11701
11702 input_rela_hdr = esdi->rela.hdr;
11703 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
11704 {
11705 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11706 input_rela_hdr,
11707 internal_relocs,
11708 rela_hash_list))
11709 return false;
11710 }
11711 }
11712 }
11713
11714 /* Write out the modified section contents. */
11715 if (bed->elf_backend_write_section
11716 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
11717 contents))
11718 {
11719 /* Section written out. */
11720 }
11721 else switch (o->sec_info_type)
11722 {
11723 case SEC_INFO_TYPE_STABS:
11724 if (! (_bfd_write_section_stabs
11725 (output_bfd,
11726 &elf_hash_table (flinfo->info)->stab_info,
11727 o, &elf_section_data (o)->sec_info, contents)))
11728 return false;
11729 break;
11730 case SEC_INFO_TYPE_MERGE:
11731 if (! _bfd_write_merged_section (output_bfd, o,
11732 elf_section_data (o)->sec_info))
11733 return false;
11734 break;
11735 case SEC_INFO_TYPE_EH_FRAME:
11736 {
11737 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
11738 o, contents))
11739 return false;
11740 }
11741 break;
11742 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11743 {
11744 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11745 flinfo->info,
11746 o, contents))
11747 return false;
11748 }
11749 break;
11750 default:
11751 {
11752 if (! (o->flags & SEC_EXCLUDE))
11753 {
11754 file_ptr offset = (file_ptr) o->output_offset;
11755 bfd_size_type todo = o->size;
11756
11757 offset *= bfd_octets_per_byte (output_bfd, o);
11758
11759 if ((o->flags & SEC_ELF_REVERSE_COPY))
11760 {
11761 /* Reverse-copy input section to output. */
11762 do
11763 {
11764 todo -= address_size;
11765 if (! bfd_set_section_contents (output_bfd,
11766 o->output_section,
11767 contents + todo,
11768 offset,
11769 address_size))
11770 return false;
11771 if (todo == 0)
11772 break;
11773 offset += address_size;
11774 }
11775 while (1);
11776 }
11777 else if (! bfd_set_section_contents (output_bfd,
11778 o->output_section,
11779 contents,
11780 offset, todo))
11781 return false;
11782 }
11783 }
11784 break;
11785 }
11786 }
11787
11788 return true;
11789 }
11790
11791 /* Generate a reloc when linking an ELF file. This is a reloc
11792 requested by the linker, and does not come from any input file. This
11793 is used to build constructor and destructor tables when linking
11794 with -Ur. */
11795
11796 static bool
11797 elf_reloc_link_order (bfd *output_bfd,
11798 struct bfd_link_info *info,
11799 asection *output_section,
11800 struct bfd_link_order *link_order)
11801 {
11802 reloc_howto_type *howto;
11803 long indx;
11804 bfd_vma offset;
11805 bfd_vma addend;
11806 struct bfd_elf_section_reloc_data *reldata;
11807 struct elf_link_hash_entry **rel_hash_ptr;
11808 Elf_Internal_Shdr *rel_hdr;
11809 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11810 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11811 bfd_byte *erel;
11812 unsigned int i;
11813 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
11814
11815 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11816 if (howto == NULL)
11817 {
11818 bfd_set_error (bfd_error_bad_value);
11819 return false;
11820 }
11821
11822 addend = link_order->u.reloc.p->addend;
11823
11824 if (esdo->rel.hdr)
11825 reldata = &esdo->rel;
11826 else if (esdo->rela.hdr)
11827 reldata = &esdo->rela;
11828 else
11829 {
11830 reldata = NULL;
11831 BFD_ASSERT (0);
11832 }
11833
11834 /* Figure out the symbol index. */
11835 rel_hash_ptr = reldata->hashes + reldata->count;
11836 if (link_order->type == bfd_section_reloc_link_order)
11837 {
11838 indx = link_order->u.reloc.p->u.section->target_index;
11839 BFD_ASSERT (indx != 0);
11840 *rel_hash_ptr = NULL;
11841 }
11842 else
11843 {
11844 struct elf_link_hash_entry *h;
11845
11846 /* Treat a reloc against a defined symbol as though it were
11847 actually against the section. */
11848 h = ((struct elf_link_hash_entry *)
11849 bfd_wrapped_link_hash_lookup (output_bfd, info,
11850 link_order->u.reloc.p->u.name,
11851 false, false, true));
11852 if (h != NULL
11853 && (h->root.type == bfd_link_hash_defined
11854 || h->root.type == bfd_link_hash_defweak))
11855 {
11856 asection *section;
11857
11858 section = h->root.u.def.section;
11859 indx = section->output_section->target_index;
11860 *rel_hash_ptr = NULL;
11861 /* It seems that we ought to add the symbol value to the
11862 addend here, but in practice it has already been added
11863 because it was passed to constructor_callback. */
11864 addend += section->output_section->vma + section->output_offset;
11865 }
11866 else if (h != NULL)
11867 {
11868 /* Setting the index to -2 tells elf_link_output_extsym that
11869 this symbol is used by a reloc. */
11870 h->indx = -2;
11871 *rel_hash_ptr = h;
11872 indx = 0;
11873 }
11874 else
11875 {
11876 (*info->callbacks->unattached_reloc)
11877 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
11878 indx = 0;
11879 }
11880 }
11881
11882 /* If this is an inplace reloc, we must write the addend into the
11883 object file. */
11884 if (howto->partial_inplace && addend != 0)
11885 {
11886 bfd_size_type size;
11887 bfd_reloc_status_type rstat;
11888 bfd_byte *buf;
11889 bool ok;
11890 const char *sym_name;
11891 bfd_size_type octets;
11892
11893 size = (bfd_size_type) bfd_get_reloc_size (howto);
11894 buf = (bfd_byte *) bfd_zmalloc (size);
11895 if (buf == NULL && size != 0)
11896 return false;
11897 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11898 switch (rstat)
11899 {
11900 case bfd_reloc_ok:
11901 break;
11902
11903 default:
11904 case bfd_reloc_outofrange:
11905 abort ();
11906
11907 case bfd_reloc_overflow:
11908 if (link_order->type == bfd_section_reloc_link_order)
11909 sym_name = bfd_section_name (link_order->u.reloc.p->u.section);
11910 else
11911 sym_name = link_order->u.reloc.p->u.name;
11912 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11913 howto->name, addend, NULL, NULL,
11914 (bfd_vma) 0);
11915 break;
11916 }
11917
11918 octets = link_order->offset * bfd_octets_per_byte (output_bfd,
11919 output_section);
11920 ok = bfd_set_section_contents (output_bfd, output_section, buf,
11921 octets, size);
11922 free (buf);
11923 if (! ok)
11924 return false;
11925 }
11926
11927 /* The address of a reloc is relative to the section in a
11928 relocatable file, and is a virtual address in an executable
11929 file. */
11930 offset = link_order->offset;
11931 if (! bfd_link_relocatable (info))
11932 offset += output_section->vma;
11933
11934 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11935 {
11936 irel[i].r_offset = offset;
11937 irel[i].r_info = 0;
11938 irel[i].r_addend = 0;
11939 }
11940 if (bed->s->arch_size == 32)
11941 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11942 else
11943 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11944
11945 rel_hdr = reldata->hdr;
11946 erel = rel_hdr->contents;
11947 if (rel_hdr->sh_type == SHT_REL)
11948 {
11949 erel += reldata->count * bed->s->sizeof_rel;
11950 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11951 }
11952 else
11953 {
11954 irel[0].r_addend = addend;
11955 erel += reldata->count * bed->s->sizeof_rela;
11956 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11957 }
11958
11959 ++reldata->count;
11960
11961 return true;
11962 }
11963
11964 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11965 Returns TRUE upon success, FALSE otherwise. */
11966
11967 static bool
11968 elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11969 {
11970 bool ret = false;
11971 bfd *implib_bfd;
11972 const struct elf_backend_data *bed;
11973 flagword flags;
11974 enum bfd_architecture arch;
11975 unsigned int mach;
11976 asymbol **sympp = NULL;
11977 long symsize;
11978 long symcount;
11979 long src_count;
11980 elf_symbol_type *osymbuf;
11981 size_t amt;
11982
11983 implib_bfd = info->out_implib_bfd;
11984 bed = get_elf_backend_data (abfd);
11985
11986 if (!bfd_set_format (implib_bfd, bfd_object))
11987 return false;
11988
11989 /* Use flag from executable but make it a relocatable object. */
11990 flags = bfd_get_file_flags (abfd);
11991 flags &= ~HAS_RELOC;
11992 if (!bfd_set_start_address (implib_bfd, 0)
11993 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
11994 return false;
11995
11996 /* Copy architecture of output file to import library file. */
11997 arch = bfd_get_arch (abfd);
11998 mach = bfd_get_mach (abfd);
11999 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
12000 && (abfd->target_defaulted
12001 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
12002 return false;
12003
12004 /* Get symbol table size. */
12005 symsize = bfd_get_symtab_upper_bound (abfd);
12006 if (symsize < 0)
12007 return false;
12008
12009 /* Read in the symbol table. */
12010 sympp = (asymbol **) bfd_malloc (symsize);
12011 if (sympp == NULL)
12012 return false;
12013
12014 symcount = bfd_canonicalize_symtab (abfd, sympp);
12015 if (symcount < 0)
12016 goto free_sym_buf;
12017
12018 /* Allow the BFD backend to copy any private header data it
12019 understands from the output BFD to the import library BFD. */
12020 if (! bfd_copy_private_header_data (abfd, implib_bfd))
12021 goto free_sym_buf;
12022
12023 /* Filter symbols to appear in the import library. */
12024 if (bed->elf_backend_filter_implib_symbols)
12025 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
12026 symcount);
12027 else
12028 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
12029 if (symcount == 0)
12030 {
12031 bfd_set_error (bfd_error_no_symbols);
12032 _bfd_error_handler (_("%pB: no symbol found for import library"),
12033 implib_bfd);
12034 goto free_sym_buf;
12035 }
12036
12037
12038 /* Make symbols absolute. */
12039 amt = symcount * sizeof (*osymbuf);
12040 osymbuf = (elf_symbol_type *) bfd_alloc (implib_bfd, amt);
12041 if (osymbuf == NULL)
12042 goto free_sym_buf;
12043
12044 for (src_count = 0; src_count < symcount; src_count++)
12045 {
12046 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
12047 sizeof (*osymbuf));
12048 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
12049 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
12050 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
12051 osymbuf[src_count].internal_elf_sym.st_value =
12052 osymbuf[src_count].symbol.value;
12053 sympp[src_count] = &osymbuf[src_count].symbol;
12054 }
12055
12056 bfd_set_symtab (implib_bfd, sympp, symcount);
12057
12058 /* Allow the BFD backend to copy any private data it understands
12059 from the output BFD to the import library BFD. This is done last
12060 to permit the routine to look at the filtered symbol table. */
12061 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
12062 goto free_sym_buf;
12063
12064 if (!bfd_close (implib_bfd))
12065 goto free_sym_buf;
12066
12067 ret = true;
12068
12069 free_sym_buf:
12070 free (sympp);
12071 return ret;
12072 }
12073
12074 static void
12075 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
12076 {
12077 asection *o;
12078
12079 if (flinfo->symstrtab != NULL)
12080 _bfd_elf_strtab_free (flinfo->symstrtab);
12081 free (flinfo->contents);
12082 free (flinfo->external_relocs);
12083 free (flinfo->internal_relocs);
12084 free (flinfo->external_syms);
12085 free (flinfo->locsym_shndx);
12086 free (flinfo->internal_syms);
12087 free (flinfo->indices);
12088 free (flinfo->sections);
12089 if (flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1)
12090 free (flinfo->symshndxbuf);
12091 for (o = obfd->sections; o != NULL; o = o->next)
12092 {
12093 struct bfd_elf_section_data *esdo = elf_section_data (o);
12094 free (esdo->rel.hashes);
12095 free (esdo->rela.hashes);
12096 }
12097 }
12098
12099 /* Do the final step of an ELF link. */
12100
12101 bool
12102 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
12103 {
12104 bool dynamic;
12105 bool emit_relocs;
12106 bfd *dynobj;
12107 struct elf_final_link_info flinfo;
12108 asection *o;
12109 struct bfd_link_order *p;
12110 bfd *sub;
12111 bfd_size_type max_contents_size;
12112 bfd_size_type max_external_reloc_size;
12113 bfd_size_type max_internal_reloc_count;
12114 bfd_size_type max_sym_count;
12115 bfd_size_type max_sym_shndx_count;
12116 Elf_Internal_Sym elfsym;
12117 unsigned int i;
12118 Elf_Internal_Shdr *symtab_hdr;
12119 Elf_Internal_Shdr *symtab_shndx_hdr;
12120 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12121 struct elf_outext_info eoinfo;
12122 bool merged;
12123 size_t relativecount = 0;
12124 asection *reldyn = 0;
12125 bfd_size_type amt;
12126 asection *attr_section = NULL;
12127 bfd_vma attr_size = 0;
12128 const char *std_attrs_section;
12129 struct elf_link_hash_table *htab = elf_hash_table (info);
12130 bool sections_removed;
12131 bool ret;
12132
12133 if (!is_elf_hash_table (&htab->root))
12134 return false;
12135
12136 if (bfd_link_pic (info))
12137 abfd->flags |= DYNAMIC;
12138
12139 dynamic = htab->dynamic_sections_created;
12140 dynobj = htab->dynobj;
12141
12142 emit_relocs = (bfd_link_relocatable (info)
12143 || info->emitrelocations);
12144
12145 memset (&flinfo, 0, sizeof (flinfo));
12146 flinfo.info = info;
12147 flinfo.output_bfd = abfd;
12148 flinfo.symstrtab = _bfd_elf_strtab_init ();
12149 if (flinfo.symstrtab == NULL)
12150 return false;
12151
12152 if (! dynamic)
12153 {
12154 flinfo.hash_sec = NULL;
12155 flinfo.symver_sec = NULL;
12156 }
12157 else
12158 {
12159 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
12160 /* Note that dynsym_sec can be NULL (on VMS). */
12161 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
12162 /* Note that it is OK if symver_sec is NULL. */
12163 }
12164
12165 if (info->unique_symbol
12166 && !bfd_hash_table_init (&flinfo.local_hash_table,
12167 local_hash_newfunc,
12168 sizeof (struct local_hash_entry)))
12169 return false;
12170
12171 /* The object attributes have been merged. Remove the input
12172 sections from the link, and set the contents of the output
12173 section. */
12174 sections_removed = false;
12175 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
12176 for (o = abfd->sections; o != NULL; o = o->next)
12177 {
12178 bool remove_section = false;
12179
12180 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
12181 || strcmp (o->name, ".gnu.attributes") == 0)
12182 {
12183 for (p = o->map_head.link_order; p != NULL; p = p->next)
12184 {
12185 asection *input_section;
12186
12187 if (p->type != bfd_indirect_link_order)
12188 continue;
12189 input_section = p->u.indirect.section;
12190 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12191 elf_link_input_bfd ignores this section. */
12192 input_section->flags &= ~SEC_HAS_CONTENTS;
12193 }
12194
12195 attr_size = bfd_elf_obj_attr_size (abfd);
12196 bfd_set_section_size (o, attr_size);
12197 /* Skip this section later on. */
12198 o->map_head.link_order = NULL;
12199 if (attr_size)
12200 attr_section = o;
12201 else
12202 remove_section = true;
12203 }
12204 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
12205 {
12206 /* Remove empty group section from linker output. */
12207 remove_section = true;
12208 }
12209 if (remove_section)
12210 {
12211 o->flags |= SEC_EXCLUDE;
12212 bfd_section_list_remove (abfd, o);
12213 abfd->section_count--;
12214 sections_removed = true;
12215 }
12216 }
12217 if (sections_removed)
12218 _bfd_fix_excluded_sec_syms (abfd, info);
12219
12220 /* Count up the number of relocations we will output for each output
12221 section, so that we know the sizes of the reloc sections. We
12222 also figure out some maximum sizes. */
12223 max_contents_size = 0;
12224 max_external_reloc_size = 0;
12225 max_internal_reloc_count = 0;
12226 max_sym_count = 0;
12227 max_sym_shndx_count = 0;
12228 merged = false;
12229 for (o = abfd->sections; o != NULL; o = o->next)
12230 {
12231 struct bfd_elf_section_data *esdo = elf_section_data (o);
12232 o->reloc_count = 0;
12233
12234 for (p = o->map_head.link_order; p != NULL; p = p->next)
12235 {
12236 unsigned int reloc_count = 0;
12237 unsigned int additional_reloc_count = 0;
12238 struct bfd_elf_section_data *esdi = NULL;
12239
12240 if (p->type == bfd_section_reloc_link_order
12241 || p->type == bfd_symbol_reloc_link_order)
12242 reloc_count = 1;
12243 else if (p->type == bfd_indirect_link_order)
12244 {
12245 asection *sec;
12246
12247 sec = p->u.indirect.section;
12248
12249 /* Mark all sections which are to be included in the
12250 link. This will normally be every section. We need
12251 to do this so that we can identify any sections which
12252 the linker has decided to not include. */
12253 sec->linker_mark = true;
12254
12255 if (sec->flags & SEC_MERGE)
12256 merged = true;
12257
12258 if (sec->rawsize > max_contents_size)
12259 max_contents_size = sec->rawsize;
12260 if (sec->size > max_contents_size)
12261 max_contents_size = sec->size;
12262
12263 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
12264 && (sec->owner->flags & DYNAMIC) == 0)
12265 {
12266 size_t sym_count;
12267
12268 /* We are interested in just local symbols, not all
12269 symbols. */
12270 if (elf_bad_symtab (sec->owner))
12271 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
12272 / bed->s->sizeof_sym);
12273 else
12274 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
12275
12276 if (sym_count > max_sym_count)
12277 max_sym_count = sym_count;
12278
12279 if (sym_count > max_sym_shndx_count
12280 && elf_symtab_shndx_list (sec->owner) != NULL)
12281 max_sym_shndx_count = sym_count;
12282
12283 if (esdo->this_hdr.sh_type == SHT_REL
12284 || esdo->this_hdr.sh_type == SHT_RELA)
12285 /* Some backends use reloc_count in relocation sections
12286 to count particular types of relocs. Of course,
12287 reloc sections themselves can't have relocations. */
12288 ;
12289 else if (emit_relocs)
12290 {
12291 reloc_count = sec->reloc_count;
12292 if (bed->elf_backend_count_additional_relocs)
12293 {
12294 int c;
12295 c = (*bed->elf_backend_count_additional_relocs) (sec);
12296 additional_reloc_count += c;
12297 }
12298 }
12299 else if (bed->elf_backend_count_relocs)
12300 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
12301
12302 esdi = elf_section_data (sec);
12303
12304 if ((sec->flags & SEC_RELOC) != 0)
12305 {
12306 size_t ext_size = 0;
12307
12308 if (esdi->rel.hdr != NULL)
12309 ext_size = esdi->rel.hdr->sh_size;
12310 if (esdi->rela.hdr != NULL)
12311 ext_size += esdi->rela.hdr->sh_size;
12312
12313 if (ext_size > max_external_reloc_size)
12314 max_external_reloc_size = ext_size;
12315 if (sec->reloc_count > max_internal_reloc_count)
12316 max_internal_reloc_count = sec->reloc_count;
12317 }
12318 }
12319 }
12320
12321 if (reloc_count == 0)
12322 continue;
12323
12324 reloc_count += additional_reloc_count;
12325 o->reloc_count += reloc_count;
12326
12327 if (p->type == bfd_indirect_link_order && emit_relocs)
12328 {
12329 if (esdi->rel.hdr)
12330 {
12331 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
12332 esdo->rel.count += additional_reloc_count;
12333 }
12334 if (esdi->rela.hdr)
12335 {
12336 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
12337 esdo->rela.count += additional_reloc_count;
12338 }
12339 }
12340 else
12341 {
12342 if (o->use_rela_p)
12343 esdo->rela.count += reloc_count;
12344 else
12345 esdo->rel.count += reloc_count;
12346 }
12347 }
12348
12349 if (o->reloc_count > 0)
12350 o->flags |= SEC_RELOC;
12351 else
12352 {
12353 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12354 set it (this is probably a bug) and if it is set
12355 assign_section_numbers will create a reloc section. */
12356 o->flags &=~ SEC_RELOC;
12357 }
12358
12359 /* If the SEC_ALLOC flag is not set, force the section VMA to
12360 zero. This is done in elf_fake_sections as well, but forcing
12361 the VMA to 0 here will ensure that relocs against these
12362 sections are handled correctly. */
12363 if ((o->flags & SEC_ALLOC) == 0
12364 && ! o->user_set_vma)
12365 o->vma = 0;
12366 }
12367
12368 if (! bfd_link_relocatable (info) && merged)
12369 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
12370
12371 /* Figure out the file positions for everything but the symbol table
12372 and the relocs. We set symcount to force assign_section_numbers
12373 to create a symbol table. */
12374 abfd->symcount = info->strip != strip_all || emit_relocs;
12375 BFD_ASSERT (! abfd->output_has_begun);
12376 if (! _bfd_elf_compute_section_file_positions (abfd, info))
12377 goto error_return;
12378
12379 /* Set sizes, and assign file positions for reloc sections. */
12380 for (o = abfd->sections; o != NULL; o = o->next)
12381 {
12382 struct bfd_elf_section_data *esdo = elf_section_data (o);
12383 if ((o->flags & SEC_RELOC) != 0)
12384 {
12385 if (esdo->rel.hdr
12386 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
12387 goto error_return;
12388
12389 if (esdo->rela.hdr
12390 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
12391 goto error_return;
12392 }
12393
12394 /* _bfd_elf_compute_section_file_positions makes temporary use
12395 of target_index. Reset it. */
12396 o->target_index = 0;
12397
12398 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12399 to count upwards while actually outputting the relocations. */
12400 esdo->rel.count = 0;
12401 esdo->rela.count = 0;
12402
12403 if ((esdo->this_hdr.sh_offset == (file_ptr) -1)
12404 && !bfd_section_is_ctf (o))
12405 {
12406 /* Cache the section contents so that they can be compressed
12407 later. Use bfd_malloc since it will be freed by
12408 bfd_compress_section_contents. */
12409 unsigned char *contents = esdo->this_hdr.contents;
12410 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
12411 abort ();
12412 contents
12413 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
12414 if (contents == NULL)
12415 goto error_return;
12416 esdo->this_hdr.contents = contents;
12417 }
12418 }
12419
12420 /* We have now assigned file positions for all the sections except .symtab,
12421 .strtab, and non-loaded reloc and compressed debugging sections. We start
12422 the .symtab section at the current file position, and write directly to it.
12423 We build the .strtab section in memory. */
12424 abfd->symcount = 0;
12425 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12426 /* sh_name is set in prep_headers. */
12427 symtab_hdr->sh_type = SHT_SYMTAB;
12428 /* sh_flags, sh_addr and sh_size all start off zero. */
12429 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
12430 /* sh_link is set in assign_section_numbers. */
12431 /* sh_info is set below. */
12432 /* sh_offset is set just below. */
12433 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
12434
12435 if (max_sym_count < 20)
12436 max_sym_count = 20;
12437 htab->strtabsize = max_sym_count;
12438 amt = max_sym_count * sizeof (struct elf_sym_strtab);
12439 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
12440 if (htab->strtab == NULL)
12441 goto error_return;
12442 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12443 flinfo.symshndxbuf
12444 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
12445 ? (Elf_External_Sym_Shndx *) -1 : NULL);
12446
12447 if (info->strip != strip_all || emit_relocs)
12448 {
12449 file_ptr off = elf_next_file_pos (abfd);
12450
12451 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, true);
12452
12453 /* Note that at this point elf_next_file_pos (abfd) is
12454 incorrect. We do not yet know the size of the .symtab section.
12455 We correct next_file_pos below, after we do know the size. */
12456
12457 /* Start writing out the symbol table. The first symbol is always a
12458 dummy symbol. */
12459 elfsym.st_value = 0;
12460 elfsym.st_size = 0;
12461 elfsym.st_info = 0;
12462 elfsym.st_other = 0;
12463 elfsym.st_shndx = SHN_UNDEF;
12464 elfsym.st_target_internal = 0;
12465 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
12466 bfd_und_section_ptr, NULL) != 1)
12467 goto error_return;
12468
12469 /* Output a symbol for each section if asked or they are used for
12470 relocs. These symbols usually have no names. We store the
12471 index of each one in the index field of the section, so that
12472 we can find it again when outputting relocs. */
12473
12474 if (bfd_keep_unused_section_symbols (abfd) || emit_relocs)
12475 {
12476 bool name_local_sections
12477 = (bed->elf_backend_name_local_section_symbols
12478 && bed->elf_backend_name_local_section_symbols (abfd));
12479 const char *name = NULL;
12480
12481 elfsym.st_size = 0;
12482 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12483 elfsym.st_other = 0;
12484 elfsym.st_value = 0;
12485 elfsym.st_target_internal = 0;
12486 for (i = 1; i < elf_numsections (abfd); i++)
12487 {
12488 o = bfd_section_from_elf_index (abfd, i);
12489 if (o != NULL)
12490 {
12491 o->target_index = bfd_get_symcount (abfd);
12492 elfsym.st_shndx = i;
12493 if (!bfd_link_relocatable (info))
12494 elfsym.st_value = o->vma;
12495 if (name_local_sections)
12496 name = o->name;
12497 if (elf_link_output_symstrtab (&flinfo, name, &elfsym, o,
12498 NULL) != 1)
12499 goto error_return;
12500 }
12501 }
12502 }
12503 }
12504
12505 /* On some targets like Irix 5 the symbol split between local and global
12506 ones recorded in the sh_info field needs to be done between section
12507 and all other symbols. */
12508 if (bed->elf_backend_elfsym_local_is_section
12509 && bed->elf_backend_elfsym_local_is_section (abfd))
12510 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12511
12512 /* Allocate some memory to hold information read in from the input
12513 files. */
12514 if (max_contents_size != 0)
12515 {
12516 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12517 if (flinfo.contents == NULL)
12518 goto error_return;
12519 }
12520
12521 if (max_external_reloc_size != 0)
12522 {
12523 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
12524 if (flinfo.external_relocs == NULL)
12525 goto error_return;
12526 }
12527
12528 if (max_internal_reloc_count != 0)
12529 {
12530 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
12531 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
12532 if (flinfo.internal_relocs == NULL)
12533 goto error_return;
12534 }
12535
12536 if (max_sym_count != 0)
12537 {
12538 amt = max_sym_count * bed->s->sizeof_sym;
12539 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
12540 if (flinfo.external_syms == NULL)
12541 goto error_return;
12542
12543 amt = max_sym_count * sizeof (Elf_Internal_Sym);
12544 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
12545 if (flinfo.internal_syms == NULL)
12546 goto error_return;
12547
12548 amt = max_sym_count * sizeof (long);
12549 flinfo.indices = (long int *) bfd_malloc (amt);
12550 if (flinfo.indices == NULL)
12551 goto error_return;
12552
12553 amt = max_sym_count * sizeof (asection *);
12554 flinfo.sections = (asection **) bfd_malloc (amt);
12555 if (flinfo.sections == NULL)
12556 goto error_return;
12557 }
12558
12559 if (max_sym_shndx_count != 0)
12560 {
12561 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
12562 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
12563 if (flinfo.locsym_shndx == NULL)
12564 goto error_return;
12565 }
12566
12567 if (htab->tls_sec)
12568 {
12569 bfd_vma base, end = 0; /* Both bytes. */
12570 asection *sec;
12571
12572 for (sec = htab->tls_sec;
12573 sec && (sec->flags & SEC_THREAD_LOCAL);
12574 sec = sec->next)
12575 {
12576 bfd_size_type size = sec->size;
12577 unsigned int opb = bfd_octets_per_byte (abfd, sec);
12578
12579 if (size == 0
12580 && (sec->flags & SEC_HAS_CONTENTS) == 0)
12581 {
12582 struct bfd_link_order *ord = sec->map_tail.link_order;
12583
12584 if (ord != NULL)
12585 size = ord->offset * opb + ord->size;
12586 }
12587 end = sec->vma + size / opb;
12588 }
12589 base = htab->tls_sec->vma;
12590 /* Only align end of TLS section if static TLS doesn't have special
12591 alignment requirements. */
12592 if (bed->static_tls_alignment == 1)
12593 end = align_power (end, htab->tls_sec->alignment_power);
12594 htab->tls_size = end - base;
12595 }
12596
12597 if (!_bfd_elf_fixup_eh_frame_hdr (info))
12598 return false;
12599
12600 /* Since ELF permits relocations to be against local symbols, we
12601 must have the local symbols available when we do the relocations.
12602 Since we would rather only read the local symbols once, and we
12603 would rather not keep them in memory, we handle all the
12604 relocations for a single input file at the same time.
12605
12606 Unfortunately, there is no way to know the total number of local
12607 symbols until we have seen all of them, and the local symbol
12608 indices precede the global symbol indices. This means that when
12609 we are generating relocatable output, and we see a reloc against
12610 a global symbol, we can not know the symbol index until we have
12611 finished examining all the local symbols to see which ones we are
12612 going to output. To deal with this, we keep the relocations in
12613 memory, and don't output them until the end of the link. This is
12614 an unfortunate waste of memory, but I don't see a good way around
12615 it. Fortunately, it only happens when performing a relocatable
12616 link, which is not the common case. FIXME: If keep_memory is set
12617 we could write the relocs out and then read them again; I don't
12618 know how bad the memory loss will be. */
12619
12620 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12621 sub->output_has_begun = false;
12622 for (o = abfd->sections; o != NULL; o = o->next)
12623 {
12624 for (p = o->map_head.link_order; p != NULL; p = p->next)
12625 {
12626 if (p->type == bfd_indirect_link_order
12627 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12628 == bfd_target_elf_flavour)
12629 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12630 {
12631 if (! sub->output_has_begun)
12632 {
12633 if (! elf_link_input_bfd (&flinfo, sub))
12634 goto error_return;
12635 sub->output_has_begun = true;
12636 }
12637 }
12638 else if (p->type == bfd_section_reloc_link_order
12639 || p->type == bfd_symbol_reloc_link_order)
12640 {
12641 if (! elf_reloc_link_order (abfd, info, o, p))
12642 goto error_return;
12643 }
12644 else
12645 {
12646 if (! _bfd_default_link_order (abfd, info, o, p))
12647 {
12648 if (p->type == bfd_indirect_link_order
12649 && (bfd_get_flavour (sub)
12650 == bfd_target_elf_flavour)
12651 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12652 != bed->s->elfclass))
12653 {
12654 const char *iclass, *oclass;
12655
12656 switch (bed->s->elfclass)
12657 {
12658 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12659 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12660 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12661 default: abort ();
12662 }
12663
12664 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
12665 {
12666 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12667 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12668 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12669 default: abort ();
12670 }
12671
12672 bfd_set_error (bfd_error_wrong_format);
12673 _bfd_error_handler
12674 /* xgettext:c-format */
12675 (_("%pB: file class %s incompatible with %s"),
12676 sub, iclass, oclass);
12677 }
12678
12679 goto error_return;
12680 }
12681 }
12682 }
12683 }
12684
12685 /* Free symbol buffer if needed. */
12686 if (!info->reduce_memory_overheads)
12687 {
12688 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12689 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
12690 {
12691 free (elf_tdata (sub)->symbuf);
12692 elf_tdata (sub)->symbuf = NULL;
12693 }
12694 }
12695
12696 ret = true;
12697
12698 /* Output any global symbols that got converted to local in a
12699 version script or due to symbol visibility. We do this in a
12700 separate step since ELF requires all local symbols to appear
12701 prior to any global symbols. FIXME: We should only do this if
12702 some global symbols were, in fact, converted to become local.
12703 FIXME: Will this work correctly with the Irix 5 linker? */
12704 eoinfo.failed = false;
12705 eoinfo.flinfo = &flinfo;
12706 eoinfo.localsyms = true;
12707 eoinfo.file_sym_done = false;
12708 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12709 if (eoinfo.failed)
12710 {
12711 ret = false;
12712 goto return_local_hash_table;
12713 }
12714
12715 /* If backend needs to output some local symbols not present in the hash
12716 table, do it now. */
12717 if (bed->elf_backend_output_arch_local_syms
12718 && (info->strip != strip_all || emit_relocs))
12719 {
12720 if (! ((*bed->elf_backend_output_arch_local_syms)
12721 (abfd, info, &flinfo, elf_link_output_symstrtab)))
12722 {
12723 ret = false;
12724 goto return_local_hash_table;
12725 }
12726 }
12727
12728 /* That wrote out all the local symbols. Finish up the symbol table
12729 with the global symbols. Even if we want to strip everything we
12730 can, we still need to deal with those global symbols that got
12731 converted to local in a version script. */
12732
12733 /* The sh_info field records the index of the first non local symbol. */
12734 if (!symtab_hdr->sh_info)
12735 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12736
12737 if (dynamic
12738 && htab->dynsym != NULL
12739 && htab->dynsym->output_section != bfd_abs_section_ptr)
12740 {
12741 Elf_Internal_Sym sym;
12742 bfd_byte *dynsym = htab->dynsym->contents;
12743
12744 o = htab->dynsym->output_section;
12745 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
12746
12747 /* Write out the section symbols for the output sections. */
12748 if (bfd_link_pic (info)
12749 || htab->is_relocatable_executable)
12750 {
12751 asection *s;
12752
12753 sym.st_size = 0;
12754 sym.st_name = 0;
12755 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12756 sym.st_other = 0;
12757 sym.st_target_internal = 0;
12758
12759 for (s = abfd->sections; s != NULL; s = s->next)
12760 {
12761 int indx;
12762 bfd_byte *dest;
12763 long dynindx;
12764
12765 dynindx = elf_section_data (s)->dynindx;
12766 if (dynindx <= 0)
12767 continue;
12768 indx = elf_section_data (s)->this_idx;
12769 BFD_ASSERT (indx > 0);
12770 sym.st_shndx = indx;
12771 if (! check_dynsym (abfd, &sym))
12772 {
12773 ret = false;
12774 goto return_local_hash_table;
12775 }
12776 sym.st_value = s->vma;
12777 dest = dynsym + dynindx * bed->s->sizeof_sym;
12778
12779 /* Inform the linker of the addition of this symbol. */
12780
12781 if (info->callbacks->ctf_new_dynsym)
12782 info->callbacks->ctf_new_dynsym (dynindx, &sym);
12783
12784 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12785 }
12786 }
12787
12788 /* Write out the local dynsyms. */
12789 if (htab->dynlocal)
12790 {
12791 struct elf_link_local_dynamic_entry *e;
12792 for (e = htab->dynlocal; e ; e = e->next)
12793 {
12794 asection *s;
12795 bfd_byte *dest;
12796
12797 /* Copy the internal symbol and turn off visibility.
12798 Note that we saved a word of storage and overwrote
12799 the original st_name with the dynstr_index. */
12800 sym = e->isym;
12801 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
12802 sym.st_shndx = SHN_UNDEF;
12803
12804 s = bfd_section_from_elf_index (e->input_bfd,
12805 e->isym.st_shndx);
12806 if (s != NULL
12807 && s->output_section != NULL
12808 && elf_section_data (s->output_section) != NULL)
12809 {
12810 sym.st_shndx =
12811 elf_section_data (s->output_section)->this_idx;
12812 if (! check_dynsym (abfd, &sym))
12813 {
12814 ret = false;
12815 goto return_local_hash_table;
12816 }
12817 sym.st_value = (s->output_section->vma
12818 + s->output_offset
12819 + e->isym.st_value);
12820 }
12821
12822 /* Inform the linker of the addition of this symbol. */
12823
12824 if (info->callbacks->ctf_new_dynsym)
12825 info->callbacks->ctf_new_dynsym (e->dynindx, &sym);
12826
12827 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12828 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12829 }
12830 }
12831 }
12832
12833 /* We get the global symbols from the hash table. */
12834 eoinfo.failed = false;
12835 eoinfo.localsyms = false;
12836 eoinfo.flinfo = &flinfo;
12837 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12838 if (eoinfo.failed)
12839 {
12840 ret = false;
12841 goto return_local_hash_table;
12842 }
12843
12844 /* If backend needs to output some symbols not present in the hash
12845 table, do it now. */
12846 if (bed->elf_backend_output_arch_syms
12847 && (info->strip != strip_all || emit_relocs))
12848 {
12849 if (! ((*bed->elf_backend_output_arch_syms)
12850 (abfd, info, &flinfo, elf_link_output_symstrtab)))
12851 {
12852 ret = false;
12853 goto return_local_hash_table;
12854 }
12855 }
12856
12857 /* Finalize the .strtab section. */
12858 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12859
12860 /* Swap out the .strtab section. */
12861 if (!elf_link_swap_symbols_out (&flinfo))
12862 {
12863 ret = false;
12864 goto return_local_hash_table;
12865 }
12866
12867 /* Now we know the size of the symtab section. */
12868 if (bfd_get_symcount (abfd) > 0)
12869 {
12870 /* Finish up and write out the symbol string table (.strtab)
12871 section. */
12872 Elf_Internal_Shdr *symstrtab_hdr = NULL;
12873 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12874
12875 if (elf_symtab_shndx_list (abfd))
12876 {
12877 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
12878
12879 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12880 {
12881 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12882 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12883 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12884 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12885 symtab_shndx_hdr->sh_size = amt;
12886
12887 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12888 off, true);
12889
12890 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12891 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12892 {
12893 ret = false;
12894 goto return_local_hash_table;
12895 }
12896 }
12897 }
12898
12899 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12900 /* sh_name was set in prep_headers. */
12901 symstrtab_hdr->sh_type = SHT_STRTAB;
12902 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
12903 symstrtab_hdr->sh_addr = 0;
12904 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
12905 symstrtab_hdr->sh_entsize = 0;
12906 symstrtab_hdr->sh_link = 0;
12907 symstrtab_hdr->sh_info = 0;
12908 /* sh_offset is set just below. */
12909 symstrtab_hdr->sh_addralign = 1;
12910
12911 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12912 off, true);
12913 elf_next_file_pos (abfd) = off;
12914
12915 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
12916 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
12917 {
12918 ret = false;
12919 goto return_local_hash_table;
12920 }
12921 }
12922
12923 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12924 {
12925 _bfd_error_handler (_("%pB: failed to generate import library"),
12926 info->out_implib_bfd);
12927 ret = false;
12928 goto return_local_hash_table;
12929 }
12930
12931 /* Adjust the relocs to have the correct symbol indices. */
12932 for (o = abfd->sections; o != NULL; o = o->next)
12933 {
12934 struct bfd_elf_section_data *esdo = elf_section_data (o);
12935 bool sort;
12936
12937 if ((o->flags & SEC_RELOC) == 0)
12938 continue;
12939
12940 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
12941 if (esdo->rel.hdr != NULL
12942 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
12943 {
12944 ret = false;
12945 goto return_local_hash_table;
12946 }
12947 if (esdo->rela.hdr != NULL
12948 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
12949 {
12950 ret = false;
12951 goto return_local_hash_table;
12952 }
12953
12954 /* Set the reloc_count field to 0 to prevent write_relocs from
12955 trying to swap the relocs out itself. */
12956 o->reloc_count = 0;
12957 }
12958
12959 if (dynamic && info->combreloc && dynobj != NULL)
12960 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12961
12962 /* If we are linking against a dynamic object, or generating a
12963 shared library, finish up the dynamic linking information. */
12964 if (dynamic)
12965 {
12966 bfd_byte *dyncon, *dynconend;
12967
12968 /* Fix up .dynamic entries. */
12969 o = bfd_get_linker_section (dynobj, ".dynamic");
12970 BFD_ASSERT (o != NULL);
12971
12972 dyncon = o->contents;
12973 dynconend = PTR_ADD (o->contents, o->size);
12974 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12975 {
12976 Elf_Internal_Dyn dyn;
12977 const char *name;
12978 unsigned int type;
12979 bfd_size_type sh_size;
12980 bfd_vma sh_addr;
12981
12982 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12983
12984 switch (dyn.d_tag)
12985 {
12986 default:
12987 continue;
12988 case DT_NULL:
12989 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12990 {
12991 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12992 {
12993 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12994 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12995 default: continue;
12996 }
12997 dyn.d_un.d_val = relativecount;
12998 relativecount = 0;
12999 break;
13000 }
13001 continue;
13002
13003 case DT_INIT:
13004 name = info->init_function;
13005 goto get_sym;
13006 case DT_FINI:
13007 name = info->fini_function;
13008 get_sym:
13009 {
13010 struct elf_link_hash_entry *h;
13011
13012 h = elf_link_hash_lookup (htab, name, false, false, true);
13013 if (h != NULL
13014 && (h->root.type == bfd_link_hash_defined
13015 || h->root.type == bfd_link_hash_defweak))
13016 {
13017 dyn.d_un.d_ptr = h->root.u.def.value;
13018 o = h->root.u.def.section;
13019 if (o->output_section != NULL)
13020 dyn.d_un.d_ptr += (o->output_section->vma
13021 + o->output_offset);
13022 else
13023 {
13024 /* The symbol is imported from another shared
13025 library and does not apply to this one. */
13026 dyn.d_un.d_ptr = 0;
13027 }
13028 break;
13029 }
13030 }
13031 continue;
13032
13033 case DT_PREINIT_ARRAYSZ:
13034 name = ".preinit_array";
13035 goto get_out_size;
13036 case DT_INIT_ARRAYSZ:
13037 name = ".init_array";
13038 goto get_out_size;
13039 case DT_FINI_ARRAYSZ:
13040 name = ".fini_array";
13041 get_out_size:
13042 o = bfd_get_section_by_name (abfd, name);
13043 if (o == NULL)
13044 {
13045 _bfd_error_handler
13046 (_("could not find section %s"), name);
13047 goto error_return;
13048 }
13049 if (o->size == 0)
13050 _bfd_error_handler
13051 (_("warning: %s section has zero size"), name);
13052 dyn.d_un.d_val = o->size;
13053 break;
13054
13055 case DT_PREINIT_ARRAY:
13056 name = ".preinit_array";
13057 goto get_out_vma;
13058 case DT_INIT_ARRAY:
13059 name = ".init_array";
13060 goto get_out_vma;
13061 case DT_FINI_ARRAY:
13062 name = ".fini_array";
13063 get_out_vma:
13064 o = bfd_get_section_by_name (abfd, name);
13065 goto do_vma;
13066
13067 case DT_HASH:
13068 name = ".hash";
13069 goto get_vma;
13070 case DT_GNU_HASH:
13071 name = ".gnu.hash";
13072 goto get_vma;
13073 case DT_STRTAB:
13074 name = ".dynstr";
13075 goto get_vma;
13076 case DT_SYMTAB:
13077 name = ".dynsym";
13078 goto get_vma;
13079 case DT_VERDEF:
13080 name = ".gnu.version_d";
13081 goto get_vma;
13082 case DT_VERNEED:
13083 name = ".gnu.version_r";
13084 goto get_vma;
13085 case DT_VERSYM:
13086 name = ".gnu.version";
13087 get_vma:
13088 o = bfd_get_linker_section (dynobj, name);
13089 do_vma:
13090 if (o == NULL || bfd_is_abs_section (o->output_section))
13091 {
13092 _bfd_error_handler
13093 (_("could not find section %s"), name);
13094 goto error_return;
13095 }
13096 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
13097 {
13098 _bfd_error_handler
13099 (_("warning: section '%s' is being made into a note"), name);
13100 bfd_set_error (bfd_error_nonrepresentable_section);
13101 goto error_return;
13102 }
13103 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
13104 break;
13105
13106 case DT_REL:
13107 case DT_RELA:
13108 case DT_RELSZ:
13109 case DT_RELASZ:
13110 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
13111 type = SHT_REL;
13112 else
13113 type = SHT_RELA;
13114 sh_size = 0;
13115 sh_addr = 0;
13116 for (i = 1; i < elf_numsections (abfd); i++)
13117 {
13118 Elf_Internal_Shdr *hdr;
13119
13120 hdr = elf_elfsections (abfd)[i];
13121 if (hdr->sh_type == type
13122 && (hdr->sh_flags & SHF_ALLOC) != 0)
13123 {
13124 sh_size += hdr->sh_size;
13125 if (sh_addr == 0
13126 || sh_addr > hdr->sh_addr)
13127 sh_addr = hdr->sh_addr;
13128 }
13129 }
13130
13131 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
13132 {
13133 unsigned int opb = bfd_octets_per_byte (abfd, o);
13134
13135 /* Don't count procedure linkage table relocs in the
13136 overall reloc count. */
13137 sh_size -= htab->srelplt->size;
13138 if (sh_size == 0)
13139 /* If the size is zero, make the address zero too.
13140 This is to avoid a glibc bug. If the backend
13141 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
13142 zero, then we'll put DT_RELA at the end of
13143 DT_JMPREL. glibc will interpret the end of
13144 DT_RELA matching the end of DT_JMPREL as the
13145 case where DT_RELA includes DT_JMPREL, and for
13146 LD_BIND_NOW will decide that processing DT_RELA
13147 will process the PLT relocs too. Net result:
13148 No PLT relocs applied. */
13149 sh_addr = 0;
13150
13151 /* If .rela.plt is the first .rela section, exclude
13152 it from DT_RELA. */
13153 else if (sh_addr == (htab->srelplt->output_section->vma
13154 + htab->srelplt->output_offset) * opb)
13155 sh_addr += htab->srelplt->size;
13156 }
13157
13158 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
13159 dyn.d_un.d_val = sh_size;
13160 else
13161 dyn.d_un.d_ptr = sh_addr;
13162 break;
13163 }
13164 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13165 }
13166 }
13167
13168 /* If we have created any dynamic sections, then output them. */
13169 if (dynobj != NULL)
13170 {
13171 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
13172 goto error_return;
13173
13174 /* Check for DT_TEXTREL (late, in case the backend removes it). */
13175 if (bfd_link_textrel_check (info)
13176 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL
13177 && o->size != 0)
13178 {
13179 bfd_byte *dyncon, *dynconend;
13180
13181 dyncon = o->contents;
13182 dynconend = o->contents + o->size;
13183 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13184 {
13185 Elf_Internal_Dyn dyn;
13186
13187 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13188
13189 if (dyn.d_tag == DT_TEXTREL)
13190 {
13191 if (info->textrel_check == textrel_check_error)
13192 info->callbacks->einfo
13193 (_("%P%X: read-only segment has dynamic relocations\n"));
13194 else if (bfd_link_dll (info))
13195 info->callbacks->einfo
13196 (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
13197 else if (bfd_link_pde (info))
13198 info->callbacks->einfo
13199 (_("%P: warning: creating DT_TEXTREL in a PDE\n"));
13200 else
13201 info->callbacks->einfo
13202 (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
13203 break;
13204 }
13205 }
13206 }
13207
13208 for (o = dynobj->sections; o != NULL; o = o->next)
13209 {
13210 if ((o->flags & SEC_HAS_CONTENTS) == 0
13211 || o->size == 0
13212 || o->output_section == bfd_abs_section_ptr)
13213 continue;
13214 if ((o->flags & SEC_LINKER_CREATED) == 0)
13215 {
13216 /* At this point, we are only interested in sections
13217 created by _bfd_elf_link_create_dynamic_sections. */
13218 continue;
13219 }
13220 if (htab->stab_info.stabstr == o)
13221 continue;
13222 if (htab->eh_info.hdr_sec == o)
13223 continue;
13224 if (strcmp (o->name, ".dynstr") != 0)
13225 {
13226 bfd_size_type octets = ((file_ptr) o->output_offset
13227 * bfd_octets_per_byte (abfd, o));
13228 if (!bfd_set_section_contents (abfd, o->output_section,
13229 o->contents, octets, o->size))
13230 goto error_return;
13231 }
13232 else
13233 {
13234 /* The contents of the .dynstr section are actually in a
13235 stringtab. */
13236 file_ptr off;
13237
13238 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
13239 if (bfd_seek (abfd, off, SEEK_SET) != 0
13240 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
13241 goto error_return;
13242 }
13243 }
13244 }
13245
13246 if (!info->resolve_section_groups)
13247 {
13248 bool failed = false;
13249
13250 BFD_ASSERT (bfd_link_relocatable (info));
13251 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
13252 if (failed)
13253 goto error_return;
13254 }
13255
13256 /* If we have optimized stabs strings, output them. */
13257 if (htab->stab_info.stabstr != NULL)
13258 {
13259 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
13260 goto error_return;
13261 }
13262
13263 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
13264 goto error_return;
13265
13266 if (info->callbacks->emit_ctf)
13267 info->callbacks->emit_ctf ();
13268
13269 elf_final_link_free (abfd, &flinfo);
13270
13271 if (attr_section)
13272 {
13273 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
13274 if (contents == NULL)
13275 {
13276 /* Bail out and fail. */
13277 ret = false;
13278 goto return_local_hash_table;
13279 }
13280 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
13281 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
13282 free (contents);
13283 }
13284
13285 return_local_hash_table:
13286 if (info->unique_symbol)
13287 bfd_hash_table_free (&flinfo.local_hash_table);
13288 return ret;
13289
13290 error_return:
13291 elf_final_link_free (abfd, &flinfo);
13292 ret = false;
13293 goto return_local_hash_table;
13294 }
13295 \f
13296 /* Initialize COOKIE for input bfd ABFD. */
13297
13298 static bool
13299 init_reloc_cookie (struct elf_reloc_cookie *cookie,
13300 struct bfd_link_info *info, bfd *abfd)
13301 {
13302 Elf_Internal_Shdr *symtab_hdr;
13303 const struct elf_backend_data *bed;
13304
13305 bed = get_elf_backend_data (abfd);
13306 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13307
13308 cookie->abfd = abfd;
13309 cookie->sym_hashes = elf_sym_hashes (abfd);
13310 cookie->bad_symtab = elf_bad_symtab (abfd);
13311 if (cookie->bad_symtab)
13312 {
13313 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13314 cookie->extsymoff = 0;
13315 }
13316 else
13317 {
13318 cookie->locsymcount = symtab_hdr->sh_info;
13319 cookie->extsymoff = symtab_hdr->sh_info;
13320 }
13321
13322 if (bed->s->arch_size == 32)
13323 cookie->r_sym_shift = 8;
13324 else
13325 cookie->r_sym_shift = 32;
13326
13327 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
13328 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
13329 {
13330 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
13331 cookie->locsymcount, 0,
13332 NULL, NULL, NULL);
13333 if (cookie->locsyms == NULL)
13334 {
13335 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
13336 return false;
13337 }
13338 if (_bfd_link_keep_memory (info) )
13339 {
13340 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
13341 info->cache_size += (cookie->locsymcount
13342 * sizeof (Elf_External_Sym_Shndx));
13343 }
13344 }
13345 return true;
13346 }
13347
13348 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
13349
13350 static void
13351 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
13352 {
13353 Elf_Internal_Shdr *symtab_hdr;
13354
13355 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13356 if (symtab_hdr->contents != (unsigned char *) cookie->locsyms)
13357 free (cookie->locsyms);
13358 }
13359
13360 /* Initialize the relocation information in COOKIE for input section SEC
13361 of input bfd ABFD. */
13362
13363 static bool
13364 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13365 struct bfd_link_info *info, bfd *abfd,
13366 asection *sec)
13367 {
13368 if (sec->reloc_count == 0)
13369 {
13370 cookie->rels = NULL;
13371 cookie->relend = NULL;
13372 }
13373 else
13374 {
13375 cookie->rels = _bfd_elf_link_info_read_relocs (abfd, info, sec,
13376 NULL, NULL,
13377 _bfd_link_keep_memory (info));
13378 if (cookie->rels == NULL)
13379 return false;
13380 cookie->rel = cookie->rels;
13381 cookie->relend = cookie->rels + sec->reloc_count;
13382 }
13383 cookie->rel = cookie->rels;
13384 return true;
13385 }
13386
13387 /* Free the memory allocated by init_reloc_cookie_rels,
13388 if appropriate. */
13389
13390 static void
13391 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13392 asection *sec)
13393 {
13394 if (elf_section_data (sec)->relocs != cookie->rels)
13395 free (cookie->rels);
13396 }
13397
13398 /* Initialize the whole of COOKIE for input section SEC. */
13399
13400 static bool
13401 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13402 struct bfd_link_info *info,
13403 asection *sec)
13404 {
13405 if (!init_reloc_cookie (cookie, info, sec->owner))
13406 goto error1;
13407 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
13408 goto error2;
13409 return true;
13410
13411 error2:
13412 fini_reloc_cookie (cookie, sec->owner);
13413 error1:
13414 return false;
13415 }
13416
13417 /* Free the memory allocated by init_reloc_cookie_for_section,
13418 if appropriate. */
13419
13420 static void
13421 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13422 asection *sec)
13423 {
13424 fini_reloc_cookie_rels (cookie, sec);
13425 fini_reloc_cookie (cookie, sec->owner);
13426 }
13427 \f
13428 /* Garbage collect unused sections. */
13429
13430 /* Default gc_mark_hook. */
13431
13432 asection *
13433 _bfd_elf_gc_mark_hook (asection *sec,
13434 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13435 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13436 struct elf_link_hash_entry *h,
13437 Elf_Internal_Sym *sym)
13438 {
13439 if (h != NULL)
13440 {
13441 switch (h->root.type)
13442 {
13443 case bfd_link_hash_defined:
13444 case bfd_link_hash_defweak:
13445 return h->root.u.def.section;
13446
13447 case bfd_link_hash_common:
13448 return h->root.u.c.p->section;
13449
13450 default:
13451 break;
13452 }
13453 }
13454 else
13455 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
13456
13457 return NULL;
13458 }
13459
13460 /* Return the debug definition section. */
13461
13462 static asection *
13463 elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
13464 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13465 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13466 struct elf_link_hash_entry *h,
13467 Elf_Internal_Sym *sym)
13468 {
13469 if (h != NULL)
13470 {
13471 /* Return the global debug definition section. */
13472 if ((h->root.type == bfd_link_hash_defined
13473 || h->root.type == bfd_link_hash_defweak)
13474 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
13475 return h->root.u.def.section;
13476 }
13477 else
13478 {
13479 /* Return the local debug definition section. */
13480 asection *isec = bfd_section_from_elf_index (sec->owner,
13481 sym->st_shndx);
13482 if ((isec->flags & SEC_DEBUGGING) != 0)
13483 return isec;
13484 }
13485
13486 return NULL;
13487 }
13488
13489 /* COOKIE->rel describes a relocation against section SEC, which is
13490 a section we've decided to keep. Return the section that contains
13491 the relocation symbol, or NULL if no section contains it. */
13492
13493 asection *
13494 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
13495 elf_gc_mark_hook_fn gc_mark_hook,
13496 struct elf_reloc_cookie *cookie,
13497 bool *start_stop)
13498 {
13499 unsigned long r_symndx;
13500 struct elf_link_hash_entry *h, *hw;
13501
13502 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
13503 if (r_symndx == STN_UNDEF)
13504 return NULL;
13505
13506 if (r_symndx >= cookie->locsymcount
13507 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13508 {
13509 bool was_marked;
13510
13511 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
13512 if (h == NULL)
13513 {
13514 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
13515 sec->owner);
13516 return NULL;
13517 }
13518 while (h->root.type == bfd_link_hash_indirect
13519 || h->root.type == bfd_link_hash_warning)
13520 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13521
13522 was_marked = h->mark;
13523 h->mark = 1;
13524 /* Keep all aliases of the symbol too. If an object symbol
13525 needs to be copied into .dynbss then all of its aliases
13526 should be present as dynamic symbols, not just the one used
13527 on the copy relocation. */
13528 hw = h;
13529 while (hw->is_weakalias)
13530 {
13531 hw = hw->u.alias;
13532 hw->mark = 1;
13533 }
13534
13535 if (!was_marked && h->start_stop && !h->root.ldscript_def)
13536 {
13537 if (info->start_stop_gc)
13538 return NULL;
13539
13540 /* To work around a glibc bug, mark XXX input sections
13541 when there is a reference to __start_XXX or __stop_XXX
13542 symbols. */
13543 else if (start_stop != NULL)
13544 {
13545 asection *s = h->u2.start_stop_section;
13546 *start_stop = true;
13547 return s;
13548 }
13549 }
13550
13551 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
13552 }
13553
13554 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
13555 &cookie->locsyms[r_symndx]);
13556 }
13557
13558 /* COOKIE->rel describes a relocation against section SEC, which is
13559 a section we've decided to keep. Mark the section that contains
13560 the relocation symbol. */
13561
13562 bool
13563 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
13564 asection *sec,
13565 elf_gc_mark_hook_fn gc_mark_hook,
13566 struct elf_reloc_cookie *cookie)
13567 {
13568 asection *rsec;
13569 bool start_stop = false;
13570
13571 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
13572 while (rsec != NULL)
13573 {
13574 if (!rsec->gc_mark)
13575 {
13576 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
13577 || (rsec->owner->flags & DYNAMIC) != 0)
13578 rsec->gc_mark = 1;
13579 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
13580 return false;
13581 }
13582 if (!start_stop)
13583 break;
13584 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
13585 }
13586 return true;
13587 }
13588
13589 /* The mark phase of garbage collection. For a given section, mark
13590 it and any sections in this section's group, and all the sections
13591 which define symbols to which it refers. */
13592
13593 bool
13594 _bfd_elf_gc_mark (struct bfd_link_info *info,
13595 asection *sec,
13596 elf_gc_mark_hook_fn gc_mark_hook)
13597 {
13598 bool ret;
13599 asection *group_sec, *eh_frame;
13600
13601 sec->gc_mark = 1;
13602
13603 /* Mark all the sections in the group. */
13604 group_sec = elf_section_data (sec)->next_in_group;
13605 if (group_sec && !group_sec->gc_mark)
13606 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
13607 return false;
13608
13609 /* Look through the section relocs. */
13610 ret = true;
13611 eh_frame = elf_eh_frame_section (sec->owner);
13612 if ((sec->flags & SEC_RELOC) != 0
13613 && sec->reloc_count > 0
13614 && sec != eh_frame)
13615 {
13616 struct elf_reloc_cookie cookie;
13617
13618 if (!init_reloc_cookie_for_section (&cookie, info, sec))
13619 ret = false;
13620 else
13621 {
13622 for (; cookie.rel < cookie.relend; cookie.rel++)
13623 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
13624 {
13625 ret = false;
13626 break;
13627 }
13628 fini_reloc_cookie_for_section (&cookie, sec);
13629 }
13630 }
13631
13632 if (ret && eh_frame && elf_fde_list (sec))
13633 {
13634 struct elf_reloc_cookie cookie;
13635
13636 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
13637 ret = false;
13638 else
13639 {
13640 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
13641 gc_mark_hook, &cookie))
13642 ret = false;
13643 fini_reloc_cookie_for_section (&cookie, eh_frame);
13644 }
13645 }
13646
13647 eh_frame = elf_section_eh_frame_entry (sec);
13648 if (ret && eh_frame && !eh_frame->gc_mark)
13649 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
13650 ret = false;
13651
13652 return ret;
13653 }
13654
13655 /* Scan and mark sections in a special or debug section group. */
13656
13657 static void
13658 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
13659 {
13660 /* Point to first section of section group. */
13661 asection *ssec;
13662 /* Used to iterate the section group. */
13663 asection *msec;
13664
13665 bool is_special_grp = true;
13666 bool is_debug_grp = true;
13667
13668 /* First scan to see if group contains any section other than debug
13669 and special section. */
13670 ssec = msec = elf_next_in_group (grp);
13671 do
13672 {
13673 if ((msec->flags & SEC_DEBUGGING) == 0)
13674 is_debug_grp = false;
13675
13676 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
13677 is_special_grp = false;
13678
13679 msec = elf_next_in_group (msec);
13680 }
13681 while (msec != ssec);
13682
13683 /* If this is a pure debug section group or pure special section group,
13684 keep all sections in this group. */
13685 if (is_debug_grp || is_special_grp)
13686 {
13687 do
13688 {
13689 msec->gc_mark = 1;
13690 msec = elf_next_in_group (msec);
13691 }
13692 while (msec != ssec);
13693 }
13694 }
13695
13696 /* Keep debug and special sections. */
13697
13698 bool
13699 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
13700 elf_gc_mark_hook_fn mark_hook)
13701 {
13702 bfd *ibfd;
13703
13704 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13705 {
13706 asection *isec;
13707 bool some_kept;
13708 bool debug_frag_seen;
13709 bool has_kept_debug_info;
13710
13711 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13712 continue;
13713 isec = ibfd->sections;
13714 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13715 continue;
13716
13717 /* Ensure all linker created sections are kept,
13718 see if any other section is already marked,
13719 and note if we have any fragmented debug sections. */
13720 debug_frag_seen = some_kept = has_kept_debug_info = false;
13721 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13722 {
13723 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13724 isec->gc_mark = 1;
13725 else if (isec->gc_mark
13726 && (isec->flags & SEC_ALLOC) != 0
13727 && elf_section_type (isec) != SHT_NOTE)
13728 some_kept = true;
13729 else
13730 {
13731 /* Since all sections, except for backend specific ones,
13732 have been garbage collected, call mark_hook on this
13733 section if any of its linked-to sections is marked. */
13734 asection *linked_to_sec;
13735 for (linked_to_sec = elf_linked_to_section (isec);
13736 linked_to_sec != NULL && !linked_to_sec->linker_mark;
13737 linked_to_sec = elf_linked_to_section (linked_to_sec))
13738 {
13739 if (linked_to_sec->gc_mark)
13740 {
13741 if (!_bfd_elf_gc_mark (info, isec, mark_hook))
13742 return false;
13743 break;
13744 }
13745 linked_to_sec->linker_mark = 1;
13746 }
13747 for (linked_to_sec = elf_linked_to_section (isec);
13748 linked_to_sec != NULL && linked_to_sec->linker_mark;
13749 linked_to_sec = elf_linked_to_section (linked_to_sec))
13750 linked_to_sec->linker_mark = 0;
13751 }
13752
13753 if (!debug_frag_seen
13754 && (isec->flags & SEC_DEBUGGING)
13755 && startswith (isec->name, ".debug_line."))
13756 debug_frag_seen = true;
13757 else if (strcmp (bfd_section_name (isec),
13758 "__patchable_function_entries") == 0
13759 && elf_linked_to_section (isec) == NULL)
13760 info->callbacks->einfo (_("%F%P: %pB(%pA): error: "
13761 "need linked-to section "
13762 "for --gc-sections\n"),
13763 isec->owner, isec);
13764 }
13765
13766 /* If no non-note alloc section in this file will be kept, then
13767 we can toss out the debug and special sections. */
13768 if (!some_kept)
13769 continue;
13770
13771 /* Keep debug and special sections like .comment when they are
13772 not part of a group. Also keep section groups that contain
13773 just debug sections or special sections. NB: Sections with
13774 linked-to section has been handled above. */
13775 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13776 {
13777 if ((isec->flags & SEC_GROUP) != 0)
13778 _bfd_elf_gc_mark_debug_special_section_group (isec);
13779 else if (((isec->flags & SEC_DEBUGGING) != 0
13780 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
13781 && elf_next_in_group (isec) == NULL
13782 && elf_linked_to_section (isec) == NULL)
13783 isec->gc_mark = 1;
13784 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
13785 has_kept_debug_info = true;
13786 }
13787
13788 /* Look for CODE sections which are going to be discarded,
13789 and find and discard any fragmented debug sections which
13790 are associated with that code section. */
13791 if (debug_frag_seen)
13792 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13793 if ((isec->flags & SEC_CODE) != 0
13794 && isec->gc_mark == 0)
13795 {
13796 unsigned int ilen;
13797 asection *dsec;
13798
13799 ilen = strlen (isec->name);
13800
13801 /* Association is determined by the name of the debug
13802 section containing the name of the code section as
13803 a suffix. For example .debug_line.text.foo is a
13804 debug section associated with .text.foo. */
13805 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
13806 {
13807 unsigned int dlen;
13808
13809 if (dsec->gc_mark == 0
13810 || (dsec->flags & SEC_DEBUGGING) == 0)
13811 continue;
13812
13813 dlen = strlen (dsec->name);
13814
13815 if (dlen > ilen
13816 && strncmp (dsec->name + (dlen - ilen),
13817 isec->name, ilen) == 0)
13818 dsec->gc_mark = 0;
13819 }
13820 }
13821
13822 /* Mark debug sections referenced by kept debug sections. */
13823 if (has_kept_debug_info)
13824 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13825 if (isec->gc_mark
13826 && (isec->flags & SEC_DEBUGGING) != 0)
13827 if (!_bfd_elf_gc_mark (info, isec,
13828 elf_gc_mark_debug_section))
13829 return false;
13830 }
13831 return true;
13832 }
13833
13834 static bool
13835 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
13836 {
13837 bfd *sub;
13838 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13839
13840 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13841 {
13842 asection *o;
13843
13844 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13845 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
13846 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13847 continue;
13848 o = sub->sections;
13849 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13850 continue;
13851
13852 for (o = sub->sections; o != NULL; o = o->next)
13853 {
13854 /* When any section in a section group is kept, we keep all
13855 sections in the section group. If the first member of
13856 the section group is excluded, we will also exclude the
13857 group section. */
13858 if (o->flags & SEC_GROUP)
13859 {
13860 asection *first = elf_next_in_group (o);
13861 o->gc_mark = first->gc_mark;
13862 }
13863
13864 if (o->gc_mark)
13865 continue;
13866
13867 /* Skip sweeping sections already excluded. */
13868 if (o->flags & SEC_EXCLUDE)
13869 continue;
13870
13871 /* Since this is early in the link process, it is simple
13872 to remove a section from the output. */
13873 o->flags |= SEC_EXCLUDE;
13874
13875 if (info->print_gc_sections && o->size != 0)
13876 /* xgettext:c-format */
13877 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
13878 o, sub);
13879 }
13880 }
13881
13882 return true;
13883 }
13884
13885 /* Propagate collected vtable information. This is called through
13886 elf_link_hash_traverse. */
13887
13888 static bool
13889 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13890 {
13891 /* Those that are not vtables. */
13892 if (h->start_stop
13893 || h->u2.vtable == NULL
13894 || h->u2.vtable->parent == NULL)
13895 return true;
13896
13897 /* Those vtables that do not have parents, we cannot merge. */
13898 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
13899 return true;
13900
13901 /* If we've already been done, exit. */
13902 if (h->u2.vtable->used && h->u2.vtable->used[-1])
13903 return true;
13904
13905 /* Make sure the parent's table is up to date. */
13906 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
13907
13908 if (h->u2.vtable->used == NULL)
13909 {
13910 /* None of this table's entries were referenced. Re-use the
13911 parent's table. */
13912 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13913 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
13914 }
13915 else
13916 {
13917 size_t n;
13918 bool *cu, *pu;
13919
13920 /* Or the parent's entries into ours. */
13921 cu = h->u2.vtable->used;
13922 cu[-1] = true;
13923 pu = h->u2.vtable->parent->u2.vtable->used;
13924 if (pu != NULL)
13925 {
13926 const struct elf_backend_data *bed;
13927 unsigned int log_file_align;
13928
13929 bed = get_elf_backend_data (h->root.u.def.section->owner);
13930 log_file_align = bed->s->log_file_align;
13931 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
13932 while (n--)
13933 {
13934 if (*pu)
13935 *cu = true;
13936 pu++;
13937 cu++;
13938 }
13939 }
13940 }
13941
13942 return true;
13943 }
13944
13945 struct link_info_ok
13946 {
13947 struct bfd_link_info *info;
13948 bool ok;
13949 };
13950
13951 static bool
13952 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h,
13953 void *ptr)
13954 {
13955 asection *sec;
13956 bfd_vma hstart, hend;
13957 Elf_Internal_Rela *relstart, *relend, *rel;
13958 const struct elf_backend_data *bed;
13959 unsigned int log_file_align;
13960 struct link_info_ok *info = (struct link_info_ok *) ptr;
13961
13962 /* Take care of both those symbols that do not describe vtables as
13963 well as those that are not loaded. */
13964 if (h->start_stop
13965 || h->u2.vtable == NULL
13966 || h->u2.vtable->parent == NULL)
13967 return true;
13968
13969 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13970 || h->root.type == bfd_link_hash_defweak);
13971
13972 sec = h->root.u.def.section;
13973 hstart = h->root.u.def.value;
13974 hend = hstart + h->size;
13975
13976 relstart = _bfd_elf_link_info_read_relocs (sec->owner, info->info,
13977 sec, NULL, NULL, true);
13978 if (!relstart)
13979 return info->ok = false;
13980 bed = get_elf_backend_data (sec->owner);
13981 log_file_align = bed->s->log_file_align;
13982
13983 relend = relstart + sec->reloc_count;
13984
13985 for (rel = relstart; rel < relend; ++rel)
13986 if (rel->r_offset >= hstart && rel->r_offset < hend)
13987 {
13988 /* If the entry is in use, do nothing. */
13989 if (h->u2.vtable->used
13990 && (rel->r_offset - hstart) < h->u2.vtable->size)
13991 {
13992 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
13993 if (h->u2.vtable->used[entry])
13994 continue;
13995 }
13996 /* Otherwise, kill it. */
13997 rel->r_offset = rel->r_info = rel->r_addend = 0;
13998 }
13999
14000 return true;
14001 }
14002
14003 /* Mark sections containing dynamically referenced symbols. When
14004 building shared libraries, we must assume that any visible symbol is
14005 referenced. */
14006
14007 bool
14008 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
14009 {
14010 struct bfd_link_info *info = (struct bfd_link_info *) inf;
14011 struct bfd_elf_dynamic_list *d = info->dynamic_list;
14012
14013 if ((h->root.type == bfd_link_hash_defined
14014 || h->root.type == bfd_link_hash_defweak)
14015 && (!h->start_stop
14016 || h->root.ldscript_def
14017 || !info->start_stop_gc)
14018 && ((h->ref_dynamic && !h->forced_local)
14019 || ((h->def_regular || ELF_COMMON_DEF_P (h))
14020 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
14021 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
14022 && (!bfd_link_executable (info)
14023 || info->gc_keep_exported
14024 || info->export_dynamic
14025 || (h->dynamic
14026 && d != NULL
14027 && (*d->match) (&d->head, NULL, h->root.root.string)))
14028 && (h->versioned >= versioned
14029 || !bfd_hide_sym_by_version (info->version_info,
14030 h->root.root.string)))))
14031 h->root.u.def.section->flags |= SEC_KEEP;
14032
14033 return true;
14034 }
14035
14036 /* Keep all sections containing symbols undefined on the command-line,
14037 and the section containing the entry symbol. */
14038
14039 void
14040 _bfd_elf_gc_keep (struct bfd_link_info *info)
14041 {
14042 struct bfd_sym_chain *sym;
14043
14044 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
14045 {
14046 struct elf_link_hash_entry *h;
14047
14048 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
14049 false, false, false);
14050
14051 if (h != NULL
14052 && (h->root.type == bfd_link_hash_defined
14053 || h->root.type == bfd_link_hash_defweak)
14054 && !bfd_is_const_section (h->root.u.def.section))
14055 h->root.u.def.section->flags |= SEC_KEEP;
14056 }
14057 }
14058
14059 bool
14060 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
14061 struct bfd_link_info *info)
14062 {
14063 bfd *ibfd = info->input_bfds;
14064
14065 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14066 {
14067 asection *sec;
14068 struct elf_reloc_cookie cookie;
14069
14070 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
14071 continue;
14072 sec = ibfd->sections;
14073 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14074 continue;
14075
14076 if (!init_reloc_cookie (&cookie, info, ibfd))
14077 return false;
14078
14079 for (sec = ibfd->sections; sec; sec = sec->next)
14080 {
14081 if (startswith (bfd_section_name (sec), ".eh_frame_entry")
14082 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
14083 {
14084 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
14085 fini_reloc_cookie_rels (&cookie, sec);
14086 }
14087 }
14088 }
14089 return true;
14090 }
14091
14092 /* Do mark and sweep of unused sections. */
14093
14094 bool
14095 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
14096 {
14097 bool ok = true;
14098 bfd *sub;
14099 elf_gc_mark_hook_fn gc_mark_hook;
14100 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14101 struct elf_link_hash_table *htab;
14102 struct link_info_ok info_ok;
14103
14104 if (!bed->can_gc_sections
14105 || !is_elf_hash_table (info->hash))
14106 {
14107 _bfd_error_handler(_("warning: gc-sections option ignored"));
14108 return true;
14109 }
14110
14111 bed->gc_keep (info);
14112 htab = elf_hash_table (info);
14113
14114 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
14115 at the .eh_frame section if we can mark the FDEs individually. */
14116 for (sub = info->input_bfds;
14117 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
14118 sub = sub->link.next)
14119 {
14120 asection *sec;
14121 struct elf_reloc_cookie cookie;
14122
14123 sec = sub->sections;
14124 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14125 continue;
14126 sec = bfd_get_section_by_name (sub, ".eh_frame");
14127 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
14128 {
14129 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
14130 if (elf_section_data (sec)->sec_info
14131 && (sec->flags & SEC_LINKER_CREATED) == 0)
14132 elf_eh_frame_section (sub) = sec;
14133 fini_reloc_cookie_for_section (&cookie, sec);
14134 sec = bfd_get_next_section_by_name (NULL, sec);
14135 }
14136 }
14137
14138 /* Apply transitive closure to the vtable entry usage info. */
14139 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
14140 if (!ok)
14141 return false;
14142
14143 /* Kill the vtable relocations that were not used. */
14144 info_ok.info = info;
14145 info_ok.ok = true;
14146 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &info_ok);
14147 if (!info_ok.ok)
14148 return false;
14149
14150 /* Mark dynamically referenced symbols. */
14151 if (htab->dynamic_sections_created || info->gc_keep_exported)
14152 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
14153
14154 /* Grovel through relocs to find out who stays ... */
14155 gc_mark_hook = bed->gc_mark_hook;
14156 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
14157 {
14158 asection *o;
14159
14160 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
14161 || elf_object_id (sub) != elf_hash_table_id (htab)
14162 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
14163 continue;
14164
14165 o = sub->sections;
14166 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14167 continue;
14168
14169 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
14170 Also treat note sections as a root, if the section is not part
14171 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
14172 well as FINI_ARRAY sections for ld -r. */
14173 for (o = sub->sections; o != NULL; o = o->next)
14174 if (!o->gc_mark
14175 && (o->flags & SEC_EXCLUDE) == 0
14176 && ((o->flags & SEC_KEEP) != 0
14177 || (bfd_link_relocatable (info)
14178 && ((elf_section_data (o)->this_hdr.sh_type
14179 == SHT_PREINIT_ARRAY)
14180 || (elf_section_data (o)->this_hdr.sh_type
14181 == SHT_INIT_ARRAY)
14182 || (elf_section_data (o)->this_hdr.sh_type
14183 == SHT_FINI_ARRAY)))
14184 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
14185 && elf_next_in_group (o) == NULL
14186 && elf_linked_to_section (o) == NULL)
14187 || ((elf_tdata (sub)->has_gnu_osabi & elf_gnu_osabi_retain)
14188 && (elf_section_flags (o) & SHF_GNU_RETAIN))))
14189 {
14190 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
14191 return false;
14192 }
14193 }
14194
14195 /* Allow the backend to mark additional target specific sections. */
14196 bed->gc_mark_extra_sections (info, gc_mark_hook);
14197
14198 /* ... and mark SEC_EXCLUDE for those that go. */
14199 return elf_gc_sweep (abfd, info);
14200 }
14201 \f
14202 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
14203
14204 bool
14205 bfd_elf_gc_record_vtinherit (bfd *abfd,
14206 asection *sec,
14207 struct elf_link_hash_entry *h,
14208 bfd_vma offset)
14209 {
14210 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
14211 struct elf_link_hash_entry **search, *child;
14212 size_t extsymcount;
14213 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14214
14215 /* The sh_info field of the symtab header tells us where the
14216 external symbols start. We don't care about the local symbols at
14217 this point. */
14218 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
14219 if (!elf_bad_symtab (abfd))
14220 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
14221
14222 sym_hashes = elf_sym_hashes (abfd);
14223 sym_hashes_end = PTR_ADD (sym_hashes, extsymcount);
14224
14225 /* Hunt down the child symbol, which is in this section at the same
14226 offset as the relocation. */
14227 for (search = sym_hashes; search != sym_hashes_end; ++search)
14228 {
14229 if ((child = *search) != NULL
14230 && (child->root.type == bfd_link_hash_defined
14231 || child->root.type == bfd_link_hash_defweak)
14232 && child->root.u.def.section == sec
14233 && child->root.u.def.value == offset)
14234 goto win;
14235 }
14236
14237 /* xgettext:c-format */
14238 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
14239 abfd, sec, (uint64_t) offset);
14240 bfd_set_error (bfd_error_invalid_operation);
14241 return false;
14242
14243 win:
14244 if (!child->u2.vtable)
14245 {
14246 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
14247 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
14248 if (!child->u2.vtable)
14249 return false;
14250 }
14251 if (!h)
14252 {
14253 /* This *should* only be the absolute section. It could potentially
14254 be that someone has defined a non-global vtable though, which
14255 would be bad. It isn't worth paging in the local symbols to be
14256 sure though; that case should simply be handled by the assembler. */
14257
14258 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
14259 }
14260 else
14261 child->u2.vtable->parent = h;
14262
14263 return true;
14264 }
14265
14266 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
14267
14268 bool
14269 bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec,
14270 struct elf_link_hash_entry *h,
14271 bfd_vma addend)
14272 {
14273 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14274 unsigned int log_file_align = bed->s->log_file_align;
14275
14276 if (!h)
14277 {
14278 /* xgettext:c-format */
14279 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
14280 abfd, sec);
14281 bfd_set_error (bfd_error_bad_value);
14282 return false;
14283 }
14284
14285 if (!h->u2.vtable)
14286 {
14287 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
14288 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
14289 if (!h->u2.vtable)
14290 return false;
14291 }
14292
14293 if (addend >= h->u2.vtable->size)
14294 {
14295 size_t size, bytes, file_align;
14296 bool *ptr = h->u2.vtable->used;
14297
14298 /* While the symbol is undefined, we have to be prepared to handle
14299 a zero size. */
14300 file_align = 1 << log_file_align;
14301 if (h->root.type == bfd_link_hash_undefined)
14302 size = addend + file_align;
14303 else
14304 {
14305 size = h->size;
14306 if (addend >= size)
14307 {
14308 /* Oops! We've got a reference past the defined end of
14309 the table. This is probably a bug -- shall we warn? */
14310 size = addend + file_align;
14311 }
14312 }
14313 size = (size + file_align - 1) & -file_align;
14314
14315 /* Allocate one extra entry for use as a "done" flag for the
14316 consolidation pass. */
14317 bytes = ((size >> log_file_align) + 1) * sizeof (bool);
14318
14319 if (ptr)
14320 {
14321 ptr = (bool *) bfd_realloc (ptr - 1, bytes);
14322
14323 if (ptr != NULL)
14324 {
14325 size_t oldbytes;
14326
14327 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
14328 * sizeof (bool));
14329 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
14330 }
14331 }
14332 else
14333 ptr = (bool *) bfd_zmalloc (bytes);
14334
14335 if (ptr == NULL)
14336 return false;
14337
14338 /* And arrange for that done flag to be at index -1. */
14339 h->u2.vtable->used = ptr + 1;
14340 h->u2.vtable->size = size;
14341 }
14342
14343 h->u2.vtable->used[addend >> log_file_align] = true;
14344
14345 return true;
14346 }
14347
14348 /* Map an ELF section header flag to its corresponding string. */
14349 typedef struct
14350 {
14351 char *flag_name;
14352 flagword flag_value;
14353 } elf_flags_to_name_table;
14354
14355 static const elf_flags_to_name_table elf_flags_to_names [] =
14356 {
14357 { "SHF_WRITE", SHF_WRITE },
14358 { "SHF_ALLOC", SHF_ALLOC },
14359 { "SHF_EXECINSTR", SHF_EXECINSTR },
14360 { "SHF_MERGE", SHF_MERGE },
14361 { "SHF_STRINGS", SHF_STRINGS },
14362 { "SHF_INFO_LINK", SHF_INFO_LINK},
14363 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
14364 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
14365 { "SHF_GROUP", SHF_GROUP },
14366 { "SHF_TLS", SHF_TLS },
14367 { "SHF_MASKOS", SHF_MASKOS },
14368 { "SHF_EXCLUDE", SHF_EXCLUDE },
14369 };
14370
14371 /* Returns TRUE if the section is to be included, otherwise FALSE. */
14372 bool
14373 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
14374 struct flag_info *flaginfo,
14375 asection *section)
14376 {
14377 const bfd_vma sh_flags = elf_section_flags (section);
14378
14379 if (!flaginfo->flags_initialized)
14380 {
14381 bfd *obfd = info->output_bfd;
14382 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14383 struct flag_info_list *tf = flaginfo->flag_list;
14384 int with_hex = 0;
14385 int without_hex = 0;
14386
14387 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
14388 {
14389 unsigned i;
14390 flagword (*lookup) (char *);
14391
14392 lookup = bed->elf_backend_lookup_section_flags_hook;
14393 if (lookup != NULL)
14394 {
14395 flagword hexval = (*lookup) ((char *) tf->name);
14396
14397 if (hexval != 0)
14398 {
14399 if (tf->with == with_flags)
14400 with_hex |= hexval;
14401 else if (tf->with == without_flags)
14402 without_hex |= hexval;
14403 tf->valid = true;
14404 continue;
14405 }
14406 }
14407 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
14408 {
14409 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
14410 {
14411 if (tf->with == with_flags)
14412 with_hex |= elf_flags_to_names[i].flag_value;
14413 else if (tf->with == without_flags)
14414 without_hex |= elf_flags_to_names[i].flag_value;
14415 tf->valid = true;
14416 break;
14417 }
14418 }
14419 if (!tf->valid)
14420 {
14421 info->callbacks->einfo
14422 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
14423 return false;
14424 }
14425 }
14426 flaginfo->flags_initialized = true;
14427 flaginfo->only_with_flags |= with_hex;
14428 flaginfo->not_with_flags |= without_hex;
14429 }
14430
14431 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
14432 return false;
14433
14434 if ((flaginfo->not_with_flags & sh_flags) != 0)
14435 return false;
14436
14437 return true;
14438 }
14439
14440 struct alloc_got_off_arg {
14441 bfd_vma gotoff;
14442 struct bfd_link_info *info;
14443 };
14444
14445 /* We need a special top-level link routine to convert got reference counts
14446 to real got offsets. */
14447
14448 static bool
14449 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
14450 {
14451 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
14452 bfd *obfd = gofarg->info->output_bfd;
14453 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14454
14455 if (h->got.refcount > 0)
14456 {
14457 h->got.offset = gofarg->gotoff;
14458 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
14459 }
14460 else
14461 h->got.offset = (bfd_vma) -1;
14462
14463 return true;
14464 }
14465
14466 /* And an accompanying bit to work out final got entry offsets once
14467 we're done. Should be called from final_link. */
14468
14469 bool
14470 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
14471 struct bfd_link_info *info)
14472 {
14473 bfd *i;
14474 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14475 bfd_vma gotoff;
14476 struct alloc_got_off_arg gofarg;
14477
14478 BFD_ASSERT (abfd == info->output_bfd);
14479
14480 if (! is_elf_hash_table (info->hash))
14481 return false;
14482
14483 /* The GOT offset is relative to the .got section, but the GOT header is
14484 put into the .got.plt section, if the backend uses it. */
14485 if (bed->want_got_plt)
14486 gotoff = 0;
14487 else
14488 gotoff = bed->got_header_size;
14489
14490 /* Do the local .got entries first. */
14491 for (i = info->input_bfds; i; i = i->link.next)
14492 {
14493 bfd_signed_vma *local_got;
14494 size_t j, locsymcount;
14495 Elf_Internal_Shdr *symtab_hdr;
14496
14497 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
14498 continue;
14499
14500 local_got = elf_local_got_refcounts (i);
14501 if (!local_got)
14502 continue;
14503
14504 symtab_hdr = &elf_tdata (i)->symtab_hdr;
14505 if (elf_bad_symtab (i))
14506 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
14507 else
14508 locsymcount = symtab_hdr->sh_info;
14509
14510 for (j = 0; j < locsymcount; ++j)
14511 {
14512 if (local_got[j] > 0)
14513 {
14514 local_got[j] = gotoff;
14515 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
14516 }
14517 else
14518 local_got[j] = (bfd_vma) -1;
14519 }
14520 }
14521
14522 /* Then the global .got entries. .plt refcounts are handled by
14523 adjust_dynamic_symbol */
14524 gofarg.gotoff = gotoff;
14525 gofarg.info = info;
14526 elf_link_hash_traverse (elf_hash_table (info),
14527 elf_gc_allocate_got_offsets,
14528 &gofarg);
14529 return true;
14530 }
14531
14532 /* Many folk need no more in the way of final link than this, once
14533 got entry reference counting is enabled. */
14534
14535 bool
14536 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
14537 {
14538 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
14539 return false;
14540
14541 /* Invoke the regular ELF backend linker to do all the work. */
14542 return bfd_elf_final_link (abfd, info);
14543 }
14544
14545 bool
14546 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
14547 {
14548 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
14549
14550 if (rcookie->bad_symtab)
14551 rcookie->rel = rcookie->rels;
14552
14553 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
14554 {
14555 unsigned long r_symndx;
14556
14557 if (! rcookie->bad_symtab)
14558 if (rcookie->rel->r_offset > offset)
14559 return false;
14560 if (rcookie->rel->r_offset != offset)
14561 continue;
14562
14563 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
14564 if (r_symndx == STN_UNDEF)
14565 return true;
14566
14567 if (r_symndx >= rcookie->locsymcount
14568 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
14569 {
14570 struct elf_link_hash_entry *h;
14571
14572 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
14573
14574 while (h->root.type == bfd_link_hash_indirect
14575 || h->root.type == bfd_link_hash_warning)
14576 h = (struct elf_link_hash_entry *) h->root.u.i.link;
14577
14578 if ((h->root.type == bfd_link_hash_defined
14579 || h->root.type == bfd_link_hash_defweak)
14580 && (h->root.u.def.section->owner != rcookie->abfd
14581 || h->root.u.def.section->kept_section != NULL
14582 || discarded_section (h->root.u.def.section)))
14583 return true;
14584 }
14585 else
14586 {
14587 /* It's not a relocation against a global symbol,
14588 but it could be a relocation against a local
14589 symbol for a discarded section. */
14590 asection *isec;
14591 Elf_Internal_Sym *isym;
14592
14593 /* Need to: get the symbol; get the section. */
14594 isym = &rcookie->locsyms[r_symndx];
14595 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
14596 if (isec != NULL
14597 && (isec->kept_section != NULL
14598 || discarded_section (isec)))
14599 return true;
14600 }
14601 return false;
14602 }
14603 return false;
14604 }
14605
14606 /* Discard unneeded references to discarded sections.
14607 Returns -1 on error, 1 if any section's size was changed, 0 if
14608 nothing changed. This function assumes that the relocations are in
14609 sorted order, which is true for all known assemblers. */
14610
14611 int
14612 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
14613 {
14614 struct elf_reloc_cookie cookie;
14615 asection *o;
14616 bfd *abfd;
14617 int changed = 0;
14618
14619 if (info->traditional_format
14620 || !is_elf_hash_table (info->hash))
14621 return 0;
14622
14623 o = bfd_get_section_by_name (output_bfd, ".stab");
14624 if (o != NULL)
14625 {
14626 asection *i;
14627
14628 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14629 {
14630 if (i->size == 0
14631 || i->reloc_count == 0
14632 || i->sec_info_type != SEC_INFO_TYPE_STABS)
14633 continue;
14634
14635 abfd = i->owner;
14636 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14637 continue;
14638
14639 if (!init_reloc_cookie_for_section (&cookie, info, i))
14640 return -1;
14641
14642 if (_bfd_discard_section_stabs (abfd, i,
14643 elf_section_data (i)->sec_info,
14644 bfd_elf_reloc_symbol_deleted_p,
14645 &cookie))
14646 changed = 1;
14647
14648 fini_reloc_cookie_for_section (&cookie, i);
14649 }
14650 }
14651
14652 o = NULL;
14653 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
14654 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
14655 if (o != NULL)
14656 {
14657 asection *i;
14658 int eh_changed = 0;
14659 unsigned int eh_alignment; /* Octets. */
14660
14661 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14662 {
14663 if (i->size == 0)
14664 continue;
14665
14666 abfd = i->owner;
14667 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14668 continue;
14669
14670 if (!init_reloc_cookie_for_section (&cookie, info, i))
14671 return -1;
14672
14673 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
14674 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
14675 bfd_elf_reloc_symbol_deleted_p,
14676 &cookie))
14677 {
14678 eh_changed = 1;
14679 if (i->size != i->rawsize)
14680 changed = 1;
14681 }
14682
14683 fini_reloc_cookie_for_section (&cookie, i);
14684 }
14685
14686 eh_alignment = ((1 << o->alignment_power)
14687 * bfd_octets_per_byte (output_bfd, o));
14688 /* Skip over zero terminator, and prevent empty sections from
14689 adding alignment padding at the end. */
14690 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
14691 if (i->size == 0)
14692 i->flags |= SEC_EXCLUDE;
14693 else if (i->size > 4)
14694 break;
14695 /* The last non-empty eh_frame section doesn't need padding. */
14696 if (i != NULL)
14697 i = i->map_tail.s;
14698 /* Any prior sections must pad the last FDE out to the output
14699 section alignment. Otherwise we might have zero padding
14700 between sections, which would be seen as a terminator. */
14701 for (; i != NULL; i = i->map_tail.s)
14702 if (i->size == 4)
14703 /* All but the last zero terminator should have been removed. */
14704 BFD_FAIL ();
14705 else
14706 {
14707 bfd_size_type size
14708 = (i->size + eh_alignment - 1) & -eh_alignment;
14709 if (i->size != size)
14710 {
14711 i->size = size;
14712 changed = 1;
14713 eh_changed = 1;
14714 }
14715 }
14716 if (eh_changed)
14717 elf_link_hash_traverse (elf_hash_table (info),
14718 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
14719 }
14720
14721 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
14722 {
14723 const struct elf_backend_data *bed;
14724 asection *s;
14725
14726 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14727 continue;
14728 s = abfd->sections;
14729 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14730 continue;
14731
14732 bed = get_elf_backend_data (abfd);
14733
14734 if (bed->elf_backend_discard_info != NULL)
14735 {
14736 if (!init_reloc_cookie (&cookie, info, abfd))
14737 return -1;
14738
14739 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
14740 changed = 1;
14741
14742 fini_reloc_cookie (&cookie, abfd);
14743 }
14744 }
14745
14746 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
14747 _bfd_elf_end_eh_frame_parsing (info);
14748
14749 if (info->eh_frame_hdr_type
14750 && !bfd_link_relocatable (info)
14751 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
14752 changed = 1;
14753
14754 return changed;
14755 }
14756
14757 bool
14758 _bfd_elf_section_already_linked (bfd *abfd,
14759 asection *sec,
14760 struct bfd_link_info *info)
14761 {
14762 flagword flags;
14763 const char *name, *key;
14764 struct bfd_section_already_linked *l;
14765 struct bfd_section_already_linked_hash_entry *already_linked_list;
14766
14767 if (sec->output_section == bfd_abs_section_ptr)
14768 return false;
14769
14770 flags = sec->flags;
14771
14772 /* Return if it isn't a linkonce section. A comdat group section
14773 also has SEC_LINK_ONCE set. */
14774 if ((flags & SEC_LINK_ONCE) == 0)
14775 return false;
14776
14777 /* Don't put group member sections on our list of already linked
14778 sections. They are handled as a group via their group section. */
14779 if (elf_sec_group (sec) != NULL)
14780 return false;
14781
14782 /* For a SHT_GROUP section, use the group signature as the key. */
14783 name = sec->name;
14784 if ((flags & SEC_GROUP) != 0
14785 && elf_next_in_group (sec) != NULL
14786 && elf_group_name (elf_next_in_group (sec)) != NULL)
14787 key = elf_group_name (elf_next_in_group (sec));
14788 else
14789 {
14790 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
14791 if (startswith (name, ".gnu.linkonce.")
14792 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
14793 key++;
14794 else
14795 /* Must be a user linkonce section that doesn't follow gcc's
14796 naming convention. In this case we won't be matching
14797 single member groups. */
14798 key = name;
14799 }
14800
14801 already_linked_list = bfd_section_already_linked_table_lookup (key);
14802
14803 for (l = already_linked_list->entry; l != NULL; l = l->next)
14804 {
14805 /* We may have 2 different types of sections on the list: group
14806 sections with a signature of <key> (<key> is some string),
14807 and linkonce sections named .gnu.linkonce.<type>.<key>.
14808 Match like sections. LTO plugin sections are an exception.
14809 They are always named .gnu.linkonce.t.<key> and match either
14810 type of section. */
14811 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
14812 && ((flags & SEC_GROUP) != 0
14813 || strcmp (name, l->sec->name) == 0))
14814 || (l->sec->owner->flags & BFD_PLUGIN) != 0
14815 || (sec->owner->flags & BFD_PLUGIN) != 0)
14816 {
14817 /* The section has already been linked. See if we should
14818 issue a warning. */
14819 if (!_bfd_handle_already_linked (sec, l, info))
14820 return false;
14821
14822 if (flags & SEC_GROUP)
14823 {
14824 asection *first = elf_next_in_group (sec);
14825 asection *s = first;
14826
14827 while (s != NULL)
14828 {
14829 s->output_section = bfd_abs_section_ptr;
14830 /* Record which group discards it. */
14831 s->kept_section = l->sec;
14832 s = elf_next_in_group (s);
14833 /* These lists are circular. */
14834 if (s == first)
14835 break;
14836 }
14837 }
14838
14839 return true;
14840 }
14841 }
14842
14843 /* A single member comdat group section may be discarded by a
14844 linkonce section and vice versa. */
14845 if ((flags & SEC_GROUP) != 0)
14846 {
14847 asection *first = elf_next_in_group (sec);
14848
14849 if (first != NULL && elf_next_in_group (first) == first)
14850 /* Check this single member group against linkonce sections. */
14851 for (l = already_linked_list->entry; l != NULL; l = l->next)
14852 if ((l->sec->flags & SEC_GROUP) == 0
14853 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14854 {
14855 first->output_section = bfd_abs_section_ptr;
14856 first->kept_section = l->sec;
14857 sec->output_section = bfd_abs_section_ptr;
14858 break;
14859 }
14860 }
14861 else
14862 /* Check this linkonce section against single member groups. */
14863 for (l = already_linked_list->entry; l != NULL; l = l->next)
14864 if (l->sec->flags & SEC_GROUP)
14865 {
14866 asection *first = elf_next_in_group (l->sec);
14867
14868 if (first != NULL
14869 && elf_next_in_group (first) == first
14870 && bfd_elf_match_symbols_in_sections (first, sec, info))
14871 {
14872 sec->output_section = bfd_abs_section_ptr;
14873 sec->kept_section = first;
14874 break;
14875 }
14876 }
14877
14878 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14879 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14880 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14881 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14882 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14883 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14884 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14885 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14886 The reverse order cannot happen as there is never a bfd with only the
14887 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14888 matter as here were are looking only for cross-bfd sections. */
14889
14890 if ((flags & SEC_GROUP) == 0 && startswith (name, ".gnu.linkonce.r."))
14891 for (l = already_linked_list->entry; l != NULL; l = l->next)
14892 if ((l->sec->flags & SEC_GROUP) == 0
14893 && startswith (l->sec->name, ".gnu.linkonce.t."))
14894 {
14895 if (abfd != l->sec->owner)
14896 sec->output_section = bfd_abs_section_ptr;
14897 break;
14898 }
14899
14900 /* This is the first section with this name. Record it. */
14901 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
14902 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
14903 return sec->output_section == bfd_abs_section_ptr;
14904 }
14905
14906 bool
14907 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
14908 {
14909 return sym->st_shndx == SHN_COMMON;
14910 }
14911
14912 unsigned int
14913 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14914 {
14915 return SHN_COMMON;
14916 }
14917
14918 asection *
14919 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14920 {
14921 return bfd_com_section_ptr;
14922 }
14923
14924 bfd_vma
14925 _bfd_elf_default_got_elt_size (bfd *abfd,
14926 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14927 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14928 bfd *ibfd ATTRIBUTE_UNUSED,
14929 unsigned long symndx ATTRIBUTE_UNUSED)
14930 {
14931 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14932 return bed->s->arch_size / 8;
14933 }
14934
14935 /* Routines to support the creation of dynamic relocs. */
14936
14937 /* Returns the name of the dynamic reloc section associated with SEC. */
14938
14939 static const char *
14940 get_dynamic_reloc_section_name (bfd * abfd,
14941 asection * sec,
14942 bool is_rela)
14943 {
14944 char *name;
14945 const char *old_name = bfd_section_name (sec);
14946 const char *prefix = is_rela ? ".rela" : ".rel";
14947
14948 if (old_name == NULL)
14949 return NULL;
14950
14951 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
14952 sprintf (name, "%s%s", prefix, old_name);
14953
14954 return name;
14955 }
14956
14957 /* Returns the dynamic reloc section associated with SEC.
14958 If necessary compute the name of the dynamic reloc section based
14959 on SEC's name (looked up in ABFD's string table) and the setting
14960 of IS_RELA. */
14961
14962 asection *
14963 _bfd_elf_get_dynamic_reloc_section (bfd *abfd,
14964 asection *sec,
14965 bool is_rela)
14966 {
14967 asection *reloc_sec = elf_section_data (sec)->sreloc;
14968
14969 if (reloc_sec == NULL)
14970 {
14971 const char *name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14972
14973 if (name != NULL)
14974 {
14975 reloc_sec = bfd_get_linker_section (abfd, name);
14976
14977 if (reloc_sec != NULL)
14978 elf_section_data (sec)->sreloc = reloc_sec;
14979 }
14980 }
14981
14982 return reloc_sec;
14983 }
14984
14985 /* Returns the dynamic reloc section associated with SEC. If the
14986 section does not exist it is created and attached to the DYNOBJ
14987 bfd and stored in the SRELOC field of SEC's elf_section_data
14988 structure.
14989
14990 ALIGNMENT is the alignment for the newly created section and
14991 IS_RELA defines whether the name should be .rela.<SEC's name>
14992 or .rel.<SEC's name>. The section name is looked up in the
14993 string table associated with ABFD. */
14994
14995 asection *
14996 _bfd_elf_make_dynamic_reloc_section (asection *sec,
14997 bfd *dynobj,
14998 unsigned int alignment,
14999 bfd *abfd,
15000 bool is_rela)
15001 {
15002 asection * reloc_sec = elf_section_data (sec)->sreloc;
15003
15004 if (reloc_sec == NULL)
15005 {
15006 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
15007
15008 if (name == NULL)
15009 return NULL;
15010
15011 reloc_sec = bfd_get_linker_section (dynobj, name);
15012
15013 if (reloc_sec == NULL)
15014 {
15015 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
15016 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
15017 if ((sec->flags & SEC_ALLOC) != 0)
15018 flags |= SEC_ALLOC | SEC_LOAD;
15019
15020 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
15021 if (reloc_sec != NULL)
15022 {
15023 /* _bfd_elf_get_sec_type_attr chooses a section type by
15024 name. Override as it may be wrong, eg. for a user
15025 section named "auto" we'll get ".relauto" which is
15026 seen to be a .rela section. */
15027 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
15028 if (!bfd_set_section_alignment (reloc_sec, alignment))
15029 reloc_sec = NULL;
15030 }
15031 }
15032
15033 elf_section_data (sec)->sreloc = reloc_sec;
15034 }
15035
15036 return reloc_sec;
15037 }
15038
15039 /* Copy the ELF symbol type and other attributes for a linker script
15040 assignment from HSRC to HDEST. Generally this should be treated as
15041 if we found a strong non-dynamic definition for HDEST (except that
15042 ld ignores multiple definition errors). */
15043 void
15044 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
15045 struct bfd_link_hash_entry *hdest,
15046 struct bfd_link_hash_entry *hsrc)
15047 {
15048 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
15049 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
15050 Elf_Internal_Sym isym;
15051
15052 ehdest->type = ehsrc->type;
15053 ehdest->target_internal = ehsrc->target_internal;
15054
15055 isym.st_other = ehsrc->other;
15056 elf_merge_st_other (abfd, ehdest, isym.st_other, NULL, true, false);
15057 }
15058
15059 /* Append a RELA relocation REL to section S in BFD. */
15060
15061 void
15062 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15063 {
15064 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15065 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
15066 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
15067 bed->s->swap_reloca_out (abfd, rel, loc);
15068 }
15069
15070 /* Append a REL relocation REL to section S in BFD. */
15071
15072 void
15073 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15074 {
15075 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15076 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
15077 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
15078 bed->s->swap_reloc_out (abfd, rel, loc);
15079 }
15080
15081 /* Define __start, __stop, .startof. or .sizeof. symbol. */
15082
15083 struct bfd_link_hash_entry *
15084 bfd_elf_define_start_stop (struct bfd_link_info *info,
15085 const char *symbol, asection *sec)
15086 {
15087 struct elf_link_hash_entry *h;
15088
15089 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
15090 false, false, true);
15091 /* NB: Common symbols will be turned into definition later. */
15092 if (h != NULL
15093 && !h->root.ldscript_def
15094 && (h->root.type == bfd_link_hash_undefined
15095 || h->root.type == bfd_link_hash_undefweak
15096 || ((h->ref_regular || h->def_dynamic)
15097 && !h->def_regular
15098 && h->root.type != bfd_link_hash_common)))
15099 {
15100 bool was_dynamic = h->ref_dynamic || h->def_dynamic;
15101 h->verinfo.verdef = NULL;
15102 h->root.type = bfd_link_hash_defined;
15103 h->root.u.def.section = sec;
15104 h->root.u.def.value = 0;
15105 h->def_regular = 1;
15106 h->def_dynamic = 0;
15107 h->start_stop = 1;
15108 h->u2.start_stop_section = sec;
15109 if (symbol[0] == '.')
15110 {
15111 /* .startof. and .sizeof. symbols are local. */
15112 const struct elf_backend_data *bed;
15113 bed = get_elf_backend_data (info->output_bfd);
15114 (*bed->elf_backend_hide_symbol) (info, h, true);
15115 }
15116 else
15117 {
15118 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
15119 h->other = ((h->other & ~ELF_ST_VISIBILITY (-1))
15120 | info->start_stop_visibility);
15121 if (was_dynamic)
15122 bfd_elf_link_record_dynamic_symbol (info, h);
15123 }
15124 return &h->root;
15125 }
15126 return NULL;
15127 }
15128
15129 /* Find dynamic relocs for H that apply to read-only sections. */
15130
15131 asection *
15132 _bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry *h)
15133 {
15134 struct elf_dyn_relocs *p;
15135
15136 for (p = h->dyn_relocs; p != NULL; p = p->next)
15137 {
15138 asection *s = p->sec->output_section;
15139
15140 if (s != NULL && (s->flags & SEC_READONLY) != 0)
15141 return p->sec;
15142 }
15143 return NULL;
15144 }
15145
15146 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
15147 read-only sections. */
15148
15149 bool
15150 _bfd_elf_maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
15151 {
15152 asection *sec;
15153
15154 if (h->root.type == bfd_link_hash_indirect)
15155 return true;
15156
15157 sec = _bfd_elf_readonly_dynrelocs (h);
15158 if (sec != NULL)
15159 {
15160 struct bfd_link_info *info = (struct bfd_link_info *) inf;
15161
15162 info->flags |= DF_TEXTREL;
15163 /* xgettext:c-format */
15164 info->callbacks->minfo (_("%pB: dynamic relocation against `%pT' "
15165 "in read-only section `%pA'\n"),
15166 sec->owner, h->root.root.string, sec);
15167
15168 if (bfd_link_textrel_check (info))
15169 /* xgettext:c-format */
15170 info->callbacks->einfo (_("%P: %pB: warning: relocation against `%s' "
15171 "in read-only section `%pA'\n"),
15172 sec->owner, h->root.root.string, sec);
15173
15174 /* Not an error, just cut short the traversal. */
15175 return false;
15176 }
15177 return true;
15178 }
15179
15180 /* Add dynamic tags. */
15181
15182 bool
15183 _bfd_elf_add_dynamic_tags (bfd *output_bfd, struct bfd_link_info *info,
15184 bool need_dynamic_reloc)
15185 {
15186 struct elf_link_hash_table *htab = elf_hash_table (info);
15187
15188 if (htab->dynamic_sections_created)
15189 {
15190 /* Add some entries to the .dynamic section. We fill in the
15191 values later, in finish_dynamic_sections, but we must add
15192 the entries now so that we get the correct size for the
15193 .dynamic section. The DT_DEBUG entry is filled in by the
15194 dynamic linker and used by the debugger. */
15195 #define add_dynamic_entry(TAG, VAL) \
15196 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
15197
15198 const struct elf_backend_data *bed
15199 = get_elf_backend_data (output_bfd);
15200
15201 if (bfd_link_executable (info))
15202 {
15203 if (!add_dynamic_entry (DT_DEBUG, 0))
15204 return false;
15205 }
15206
15207 if (htab->dt_pltgot_required || htab->splt->size != 0)
15208 {
15209 /* DT_PLTGOT is used by prelink even if there is no PLT
15210 relocation. */
15211 if (!add_dynamic_entry (DT_PLTGOT, 0))
15212 return false;
15213 }
15214
15215 if (htab->dt_jmprel_required || htab->srelplt->size != 0)
15216 {
15217 if (!add_dynamic_entry (DT_PLTRELSZ, 0)
15218 || !add_dynamic_entry (DT_PLTREL,
15219 (bed->rela_plts_and_copies_p
15220 ? DT_RELA : DT_REL))
15221 || !add_dynamic_entry (DT_JMPREL, 0))
15222 return false;
15223 }
15224
15225 if (htab->tlsdesc_plt
15226 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
15227 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
15228 return false;
15229
15230 if (need_dynamic_reloc)
15231 {
15232 if (bed->rela_plts_and_copies_p)
15233 {
15234 if (!add_dynamic_entry (DT_RELA, 0)
15235 || !add_dynamic_entry (DT_RELASZ, 0)
15236 || !add_dynamic_entry (DT_RELAENT,
15237 bed->s->sizeof_rela))
15238 return false;
15239 }
15240 else
15241 {
15242 if (!add_dynamic_entry (DT_REL, 0)
15243 || !add_dynamic_entry (DT_RELSZ, 0)
15244 || !add_dynamic_entry (DT_RELENT,
15245 bed->s->sizeof_rel))
15246 return false;
15247 }
15248
15249 /* If any dynamic relocs apply to a read-only section,
15250 then we need a DT_TEXTREL entry. */
15251 if ((info->flags & DF_TEXTREL) == 0)
15252 elf_link_hash_traverse (htab, _bfd_elf_maybe_set_textrel,
15253 info);
15254
15255 if ((info->flags & DF_TEXTREL) != 0)
15256 {
15257 if (htab->ifunc_resolvers)
15258 info->callbacks->einfo
15259 (_("%P: warning: GNU indirect functions with DT_TEXTREL "
15260 "may result in a segfault at runtime; recompile with %s\n"),
15261 bfd_link_dll (info) ? "-fPIC" : "-fPIE");
15262
15263 if (!add_dynamic_entry (DT_TEXTREL, 0))
15264 return false;
15265 }
15266 }
15267 }
15268 #undef add_dynamic_entry
15269
15270 return true;
15271 }