ld: Keep indirect symbol from IR if referenced from shared object
[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 if (info->enable_dt_relr)
363 {
364 s = bfd_make_section_anyway_with_flags (abfd, ".relr.dyn",
365 (bed->dynamic_sec_flags
366 | SEC_READONLY));
367 if (s == NULL
368 || !bfd_set_section_alignment (s, bed->s->log_file_align))
369 return false;
370 elf_hash_table (info)->srelrdyn = s;
371 }
372
373 /* Let the backend create the rest of the sections. This lets the
374 backend set the right flags. The backend will normally create
375 the .got and .plt sections. */
376 if (bed->elf_backend_create_dynamic_sections == NULL
377 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
378 return false;
379
380 elf_hash_table (info)->dynamic_sections_created = true;
381
382 return true;
383 }
384
385 /* Create dynamic sections when linking against a dynamic object. */
386
387 bool
388 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
389 {
390 flagword flags, pltflags;
391 struct elf_link_hash_entry *h;
392 asection *s;
393 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
394 struct elf_link_hash_table *htab = elf_hash_table (info);
395
396 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
397 .rel[a].bss sections. */
398 flags = bed->dynamic_sec_flags;
399
400 pltflags = flags;
401 if (bed->plt_not_loaded)
402 /* We do not clear SEC_ALLOC here because we still want the OS to
403 allocate space for the section; it's just that there's nothing
404 to read in from the object file. */
405 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
406 else
407 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
408 if (bed->plt_readonly)
409 pltflags |= SEC_READONLY;
410
411 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
412 if (s == NULL
413 || !bfd_set_section_alignment (s, bed->plt_alignment))
414 return false;
415 htab->splt = s;
416
417 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
418 .plt section. */
419 if (bed->want_plt_sym)
420 {
421 h = _bfd_elf_define_linkage_sym (abfd, info, s,
422 "_PROCEDURE_LINKAGE_TABLE_");
423 elf_hash_table (info)->hplt = h;
424 if (h == NULL)
425 return false;
426 }
427
428 s = bfd_make_section_anyway_with_flags (abfd,
429 (bed->rela_plts_and_copies_p
430 ? ".rela.plt" : ".rel.plt"),
431 flags | SEC_READONLY);
432 if (s == NULL
433 || !bfd_set_section_alignment (s, bed->s->log_file_align))
434 return false;
435 htab->srelplt = s;
436
437 if (! _bfd_elf_create_got_section (abfd, info))
438 return false;
439
440 if (bed->want_dynbss)
441 {
442 /* The .dynbss section is a place to put symbols which are defined
443 by dynamic objects, are referenced by regular objects, and are
444 not functions. We must allocate space for them in the process
445 image and use a R_*_COPY reloc to tell the dynamic linker to
446 initialize them at run time. The linker script puts the .dynbss
447 section into the .bss section of the final image. */
448 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
449 SEC_ALLOC | SEC_LINKER_CREATED);
450 if (s == NULL)
451 return false;
452 htab->sdynbss = s;
453
454 if (bed->want_dynrelro)
455 {
456 /* Similarly, but for symbols that were originally in read-only
457 sections. This section doesn't really need to have contents,
458 but make it like other .data.rel.ro sections. */
459 s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
460 flags);
461 if (s == NULL)
462 return false;
463 htab->sdynrelro = s;
464 }
465
466 /* The .rel[a].bss section holds copy relocs. This section is not
467 normally needed. We need to create it here, though, so that the
468 linker will map it to an output section. We can't just create it
469 only if we need it, because we will not know whether we need it
470 until we have seen all the input files, and the first time the
471 main linker code calls BFD after examining all the input files
472 (size_dynamic_sections) the input sections have already been
473 mapped to the output sections. If the section turns out not to
474 be needed, we can discard it later. We will never need this
475 section when generating a shared object, since they do not use
476 copy relocs. */
477 if (bfd_link_executable (info))
478 {
479 s = bfd_make_section_anyway_with_flags (abfd,
480 (bed->rela_plts_and_copies_p
481 ? ".rela.bss" : ".rel.bss"),
482 flags | SEC_READONLY);
483 if (s == NULL
484 || !bfd_set_section_alignment (s, bed->s->log_file_align))
485 return false;
486 htab->srelbss = s;
487
488 if (bed->want_dynrelro)
489 {
490 s = (bfd_make_section_anyway_with_flags
491 (abfd, (bed->rela_plts_and_copies_p
492 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
493 flags | SEC_READONLY));
494 if (s == NULL
495 || !bfd_set_section_alignment (s, bed->s->log_file_align))
496 return false;
497 htab->sreldynrelro = s;
498 }
499 }
500 }
501
502 return true;
503 }
504 \f
505 /* Record a new dynamic symbol. We record the dynamic symbols as we
506 read the input files, since we need to have a list of all of them
507 before we can determine the final sizes of the output sections.
508 Note that we may actually call this function even though we are not
509 going to output any dynamic symbols; in some cases we know that a
510 symbol should be in the dynamic symbol table, but only if there is
511 one. */
512
513 bool
514 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
515 struct elf_link_hash_entry *h)
516 {
517 if (h->dynindx == -1)
518 {
519 struct elf_strtab_hash *dynstr;
520 char *p;
521 const char *name;
522 size_t indx;
523
524 if (h->root.type == bfd_link_hash_defined
525 || h->root.type == bfd_link_hash_defweak)
526 {
527 /* An IR symbol should not be made dynamic. */
528 if (h->root.u.def.section != NULL
529 && h->root.u.def.section->owner != NULL
530 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)
531 return true;
532 }
533
534 /* XXX: The ABI draft says the linker must turn hidden and
535 internal symbols into STB_LOCAL symbols when producing the
536 DSO. However, if ld.so honors st_other in the dynamic table,
537 this would not be necessary. */
538 switch (ELF_ST_VISIBILITY (h->other))
539 {
540 case STV_INTERNAL:
541 case STV_HIDDEN:
542 if (h->root.type != bfd_link_hash_undefined
543 && h->root.type != bfd_link_hash_undefweak)
544 {
545 h->forced_local = 1;
546 if (!elf_hash_table (info)->is_relocatable_executable
547 || ((h->root.type == bfd_link_hash_defined
548 || h->root.type == bfd_link_hash_defweak)
549 && h->root.u.def.section->owner != NULL
550 && h->root.u.def.section->owner->no_export)
551 || (h->root.type == bfd_link_hash_common
552 && h->root.u.c.p->section->owner != NULL
553 && h->root.u.c.p->section->owner->no_export))
554 return true;
555 }
556
557 default:
558 break;
559 }
560
561 h->dynindx = elf_hash_table (info)->dynsymcount;
562 ++elf_hash_table (info)->dynsymcount;
563
564 dynstr = elf_hash_table (info)->dynstr;
565 if (dynstr == NULL)
566 {
567 /* Create a strtab to hold the dynamic symbol names. */
568 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
569 if (dynstr == NULL)
570 return false;
571 }
572
573 /* We don't put any version information in the dynamic string
574 table. */
575 name = h->root.root.string;
576 p = strchr (name, ELF_VER_CHR);
577 if (p != NULL)
578 /* We know that the p points into writable memory. In fact,
579 there are only a few symbols that have read-only names, being
580 those like _GLOBAL_OFFSET_TABLE_ that are created specially
581 by the backends. Most symbols will have names pointing into
582 an ELF string table read from a file, or to objalloc memory. */
583 *p = 0;
584
585 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
586
587 if (p != NULL)
588 *p = ELF_VER_CHR;
589
590 if (indx == (size_t) -1)
591 return false;
592 h->dynstr_index = indx;
593 }
594
595 return true;
596 }
597 \f
598 /* Mark a symbol dynamic. */
599
600 static void
601 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
602 struct elf_link_hash_entry *h,
603 Elf_Internal_Sym *sym)
604 {
605 struct bfd_elf_dynamic_list *d = info->dynamic_list;
606
607 /* It may be called more than once on the same H. */
608 if(h->dynamic || bfd_link_relocatable (info))
609 return;
610
611 if ((info->dynamic_data
612 && (h->type == STT_OBJECT
613 || h->type == STT_COMMON
614 || (sym != NULL
615 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
616 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
617 || (d != NULL
618 && h->non_elf
619 && (*d->match) (&d->head, NULL, h->root.root.string)))
620 {
621 h->dynamic = 1;
622 /* NB: If a symbol is made dynamic by --dynamic-list, it has
623 non-IR reference. */
624 h->root.non_ir_ref_dynamic = 1;
625 }
626 }
627
628 /* Record an assignment to a symbol made by a linker script. We need
629 this in case some dynamic object refers to this symbol. */
630
631 bool
632 bfd_elf_record_link_assignment (bfd *output_bfd,
633 struct bfd_link_info *info,
634 const char *name,
635 bool provide,
636 bool hidden)
637 {
638 struct elf_link_hash_entry *h, *hv;
639 struct elf_link_hash_table *htab;
640 const struct elf_backend_data *bed;
641
642 if (!is_elf_hash_table (info->hash))
643 return true;
644
645 htab = elf_hash_table (info);
646 h = elf_link_hash_lookup (htab, name, !provide, true, false);
647 if (h == NULL)
648 return provide;
649
650 if (h->root.type == bfd_link_hash_warning)
651 h = (struct elf_link_hash_entry *) h->root.u.i.link;
652
653 if (h->versioned == unknown)
654 {
655 /* Set versioned if symbol version is unknown. */
656 char *version = strrchr (name, ELF_VER_CHR);
657 if (version)
658 {
659 if (version > name && version[-1] != ELF_VER_CHR)
660 h->versioned = versioned_hidden;
661 else
662 h->versioned = versioned;
663 }
664 }
665
666 /* Symbols defined in a linker script but not referenced anywhere
667 else will have non_elf set. */
668 if (h->non_elf)
669 {
670 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
671 h->non_elf = 0;
672 }
673
674 switch (h->root.type)
675 {
676 case bfd_link_hash_defined:
677 case bfd_link_hash_defweak:
678 case bfd_link_hash_common:
679 break;
680 case bfd_link_hash_undefweak:
681 case bfd_link_hash_undefined:
682 /* Since we're defining the symbol, don't let it seem to have not
683 been defined. record_dynamic_symbol and size_dynamic_sections
684 may depend on this. */
685 h->root.type = bfd_link_hash_new;
686 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
687 bfd_link_repair_undef_list (&htab->root);
688 break;
689 case bfd_link_hash_new:
690 break;
691 case bfd_link_hash_indirect:
692 /* We had a versioned symbol in a dynamic library. We make the
693 the versioned symbol point to this one. */
694 bed = get_elf_backend_data (output_bfd);
695 hv = h;
696 while (hv->root.type == bfd_link_hash_indirect
697 || hv->root.type == bfd_link_hash_warning)
698 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
699 /* We don't need to update h->root.u since linker will set them
700 later. */
701 h->root.type = bfd_link_hash_undefined;
702 hv->root.type = bfd_link_hash_indirect;
703 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
704 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
705 break;
706 default:
707 BFD_FAIL ();
708 return false;
709 }
710
711 /* If this symbol is being provided by the linker script, and it is
712 currently defined by a dynamic object, but not by a regular
713 object, then mark it as undefined so that the generic linker will
714 force the correct value. */
715 if (provide
716 && h->def_dynamic
717 && !h->def_regular)
718 h->root.type = bfd_link_hash_undefined;
719
720 /* If this symbol is currently defined by a dynamic object, but not
721 by a regular object, then clear out any version information because
722 the symbol will not be associated with the dynamic object any
723 more. */
724 if (h->def_dynamic && !h->def_regular)
725 h->verinfo.verdef = NULL;
726
727 /* Make sure this symbol is not garbage collected. */
728 h->mark = 1;
729
730 h->def_regular = 1;
731
732 if (hidden)
733 {
734 bed = get_elf_backend_data (output_bfd);
735 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
736 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
737 (*bed->elf_backend_hide_symbol) (info, h, true);
738 }
739
740 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
741 and executables. */
742 if (!bfd_link_relocatable (info)
743 && h->dynindx != -1
744 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
745 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
746 h->forced_local = 1;
747
748 if ((h->def_dynamic
749 || h->ref_dynamic
750 || bfd_link_dll (info)
751 || elf_hash_table (info)->is_relocatable_executable)
752 && !h->forced_local
753 && h->dynindx == -1)
754 {
755 if (! bfd_elf_link_record_dynamic_symbol (info, h))
756 return false;
757
758 /* If this is a weak defined symbol, and we know a corresponding
759 real symbol from the same dynamic object, make sure the real
760 symbol is also made into a dynamic symbol. */
761 if (h->is_weakalias)
762 {
763 struct elf_link_hash_entry *def = weakdef (h);
764
765 if (def->dynindx == -1
766 && !bfd_elf_link_record_dynamic_symbol (info, def))
767 return false;
768 }
769 }
770
771 return true;
772 }
773
774 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
775 success, and 2 on a failure caused by attempting to record a symbol
776 in a discarded section, eg. a discarded link-once section symbol. */
777
778 int
779 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
780 bfd *input_bfd,
781 long input_indx)
782 {
783 size_t amt;
784 struct elf_link_local_dynamic_entry *entry;
785 struct elf_link_hash_table *eht;
786 struct elf_strtab_hash *dynstr;
787 size_t dynstr_index;
788 char *name;
789 Elf_External_Sym_Shndx eshndx;
790 char esym[sizeof (Elf64_External_Sym)];
791
792 if (! is_elf_hash_table (info->hash))
793 return 0;
794
795 /* See if the entry exists already. */
796 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
797 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
798 return 1;
799
800 amt = sizeof (*entry);
801 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
802 if (entry == NULL)
803 return 0;
804
805 /* Go find the symbol, so that we can find it's name. */
806 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
807 1, input_indx, &entry->isym, esym, &eshndx))
808 {
809 bfd_release (input_bfd, entry);
810 return 0;
811 }
812
813 if (entry->isym.st_shndx != SHN_UNDEF
814 && entry->isym.st_shndx < SHN_LORESERVE)
815 {
816 asection *s;
817
818 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
819 if (s == NULL || bfd_is_abs_section (s->output_section))
820 {
821 /* We can still bfd_release here as nothing has done another
822 bfd_alloc. We can't do this later in this function. */
823 bfd_release (input_bfd, entry);
824 return 2;
825 }
826 }
827
828 name = (bfd_elf_string_from_elf_section
829 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
830 entry->isym.st_name));
831
832 dynstr = elf_hash_table (info)->dynstr;
833 if (dynstr == NULL)
834 {
835 /* Create a strtab to hold the dynamic symbol names. */
836 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
837 if (dynstr == NULL)
838 return 0;
839 }
840
841 dynstr_index = _bfd_elf_strtab_add (dynstr, name, false);
842 if (dynstr_index == (size_t) -1)
843 return 0;
844 entry->isym.st_name = dynstr_index;
845
846 eht = elf_hash_table (info);
847
848 entry->next = eht->dynlocal;
849 eht->dynlocal = entry;
850 entry->input_bfd = input_bfd;
851 entry->input_indx = input_indx;
852 eht->dynsymcount++;
853
854 /* Whatever binding the symbol had before, it's now local. */
855 entry->isym.st_info
856 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
857
858 /* The dynindx will be set at the end of size_dynamic_sections. */
859
860 return 1;
861 }
862
863 /* Return the dynindex of a local dynamic symbol. */
864
865 long
866 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
867 bfd *input_bfd,
868 long input_indx)
869 {
870 struct elf_link_local_dynamic_entry *e;
871
872 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
873 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
874 return e->dynindx;
875 return -1;
876 }
877
878 /* This function is used to renumber the dynamic symbols, if some of
879 them are removed because they are marked as local. This is called
880 via elf_link_hash_traverse. */
881
882 static bool
883 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
884 void *data)
885 {
886 size_t *count = (size_t *) data;
887
888 if (h->forced_local)
889 return true;
890
891 if (h->dynindx != -1)
892 h->dynindx = ++(*count);
893
894 return true;
895 }
896
897
898 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
899 STB_LOCAL binding. */
900
901 static bool
902 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
903 void *data)
904 {
905 size_t *count = (size_t *) data;
906
907 if (!h->forced_local)
908 return true;
909
910 if (h->dynindx != -1)
911 h->dynindx = ++(*count);
912
913 return true;
914 }
915
916 /* Return true if the dynamic symbol for a given section should be
917 omitted when creating a shared library. */
918 bool
919 _bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED,
920 struct bfd_link_info *info,
921 asection *p)
922 {
923 struct elf_link_hash_table *htab;
924 asection *ip;
925
926 switch (elf_section_data (p)->this_hdr.sh_type)
927 {
928 case SHT_PROGBITS:
929 case SHT_NOBITS:
930 /* If sh_type is yet undecided, assume it could be
931 SHT_PROGBITS/SHT_NOBITS. */
932 case SHT_NULL:
933 htab = elf_hash_table (info);
934 if (htab->text_index_section != NULL)
935 return p != htab->text_index_section && p != htab->data_index_section;
936
937 return (htab->dynobj != NULL
938 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
939 && ip->output_section == p);
940
941 /* There shouldn't be section relative relocations
942 against any other section. */
943 default:
944 return true;
945 }
946 }
947
948 bool
949 _bfd_elf_omit_section_dynsym_all
950 (bfd *output_bfd ATTRIBUTE_UNUSED,
951 struct bfd_link_info *info ATTRIBUTE_UNUSED,
952 asection *p ATTRIBUTE_UNUSED)
953 {
954 return true;
955 }
956
957 /* Assign dynsym indices. In a shared library we generate a section
958 symbol for each output section, which come first. Next come symbols
959 which have been forced to local binding. Then all of the back-end
960 allocated local dynamic syms, followed by the rest of the global
961 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
962 (This prevents the early call before elf_backend_init_index_section
963 and strip_excluded_output_sections setting dynindx for sections
964 that are stripped.) */
965
966 static unsigned long
967 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
968 struct bfd_link_info *info,
969 unsigned long *section_sym_count)
970 {
971 unsigned long dynsymcount = 0;
972 bool do_sec = section_sym_count != NULL;
973
974 if (bfd_link_pic (info)
975 || elf_hash_table (info)->is_relocatable_executable)
976 {
977 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
978 asection *p;
979 for (p = output_bfd->sections; p ; p = p->next)
980 if ((p->flags & SEC_EXCLUDE) == 0
981 && (p->flags & SEC_ALLOC) != 0
982 && elf_hash_table (info)->dynamic_relocs
983 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
984 {
985 ++dynsymcount;
986 if (do_sec)
987 elf_section_data (p)->dynindx = dynsymcount;
988 }
989 else if (do_sec)
990 elf_section_data (p)->dynindx = 0;
991 }
992 if (do_sec)
993 *section_sym_count = dynsymcount;
994
995 elf_link_hash_traverse (elf_hash_table (info),
996 elf_link_renumber_local_hash_table_dynsyms,
997 &dynsymcount);
998
999 if (elf_hash_table (info)->dynlocal)
1000 {
1001 struct elf_link_local_dynamic_entry *p;
1002 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
1003 p->dynindx = ++dynsymcount;
1004 }
1005 elf_hash_table (info)->local_dynsymcount = dynsymcount;
1006
1007 elf_link_hash_traverse (elf_hash_table (info),
1008 elf_link_renumber_hash_table_dynsyms,
1009 &dynsymcount);
1010
1011 /* There is an unused NULL entry at the head of the table which we
1012 must account for in our count even if the table is empty since it
1013 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
1014 .dynamic section. */
1015 dynsymcount++;
1016
1017 elf_hash_table (info)->dynsymcount = dynsymcount;
1018 return dynsymcount;
1019 }
1020
1021 /* Merge st_other field. */
1022
1023 static void
1024 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
1025 unsigned int st_other, asection *sec,
1026 bool definition, bool dynamic)
1027 {
1028 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1029
1030 /* If st_other has a processor-specific meaning, specific
1031 code might be needed here. */
1032 if (bed->elf_backend_merge_symbol_attribute)
1033 (*bed->elf_backend_merge_symbol_attribute) (h, st_other, definition,
1034 dynamic);
1035
1036 if (!dynamic)
1037 {
1038 unsigned symvis = ELF_ST_VISIBILITY (st_other);
1039 unsigned hvis = ELF_ST_VISIBILITY (h->other);
1040
1041 /* Keep the most constraining visibility. Leave the remainder
1042 of the st_other field to elf_backend_merge_symbol_attribute. */
1043 if (symvis - 1 < hvis - 1)
1044 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
1045 }
1046 else if (definition
1047 && ELF_ST_VISIBILITY (st_other) != STV_DEFAULT
1048 && (sec->flags & SEC_READONLY) == 0)
1049 h->protected_def = 1;
1050 }
1051
1052 /* This function is called when we want to merge a new symbol with an
1053 existing symbol. It handles the various cases which arise when we
1054 find a definition in a dynamic object, or when there is already a
1055 definition in a dynamic object. The new symbol is described by
1056 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1057 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1058 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1059 of an old common symbol. We set OVERRIDE if the old symbol is
1060 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1061 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1062 to change. By OK to change, we mean that we shouldn't warn if the
1063 type or size does change. */
1064
1065 static bool
1066 _bfd_elf_merge_symbol (bfd *abfd,
1067 struct bfd_link_info *info,
1068 const char *name,
1069 Elf_Internal_Sym *sym,
1070 asection **psec,
1071 bfd_vma *pvalue,
1072 struct elf_link_hash_entry **sym_hash,
1073 bfd **poldbfd,
1074 bool *pold_weak,
1075 unsigned int *pold_alignment,
1076 bool *skip,
1077 bfd **override,
1078 bool *type_change_ok,
1079 bool *size_change_ok,
1080 bool *matched)
1081 {
1082 asection *sec, *oldsec;
1083 struct elf_link_hash_entry *h;
1084 struct elf_link_hash_entry *hi;
1085 struct elf_link_hash_entry *flip;
1086 int bind;
1087 bfd *oldbfd;
1088 bool newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
1089 bool newweak, oldweak, newfunc, oldfunc;
1090 const struct elf_backend_data *bed;
1091 char *new_version;
1092 bool default_sym = *matched;
1093
1094 *skip = false;
1095 *override = NULL;
1096
1097 sec = *psec;
1098 bind = ELF_ST_BIND (sym->st_info);
1099
1100 if (! bfd_is_und_section (sec))
1101 h = elf_link_hash_lookup (elf_hash_table (info), name, true, false, false);
1102 else
1103 h = ((struct elf_link_hash_entry *)
1104 bfd_wrapped_link_hash_lookup (abfd, info, name, true, false, false));
1105 if (h == NULL)
1106 return false;
1107 *sym_hash = h;
1108
1109 bed = get_elf_backend_data (abfd);
1110
1111 /* NEW_VERSION is the symbol version of the new symbol. */
1112 if (h->versioned != unversioned)
1113 {
1114 /* Symbol version is unknown or versioned. */
1115 new_version = strrchr (name, ELF_VER_CHR);
1116 if (new_version)
1117 {
1118 if (h->versioned == unknown)
1119 {
1120 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1121 h->versioned = versioned_hidden;
1122 else
1123 h->versioned = versioned;
1124 }
1125 new_version += 1;
1126 if (new_version[0] == '\0')
1127 new_version = NULL;
1128 }
1129 else
1130 h->versioned = unversioned;
1131 }
1132 else
1133 new_version = NULL;
1134
1135 /* For merging, we only care about real symbols. But we need to make
1136 sure that indirect symbol dynamic flags are updated. */
1137 hi = h;
1138 while (h->root.type == bfd_link_hash_indirect
1139 || h->root.type == bfd_link_hash_warning)
1140 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1141
1142 if (!*matched)
1143 {
1144 if (hi == h || h->root.type == bfd_link_hash_new)
1145 *matched = true;
1146 else
1147 {
1148 /* OLD_HIDDEN is true if the existing symbol is only visible
1149 to the symbol with the same symbol version. NEW_HIDDEN is
1150 true if the new symbol is only visible to the symbol with
1151 the same symbol version. */
1152 bool old_hidden = h->versioned == versioned_hidden;
1153 bool new_hidden = hi->versioned == versioned_hidden;
1154 if (!old_hidden && !new_hidden)
1155 /* The new symbol matches the existing symbol if both
1156 aren't hidden. */
1157 *matched = true;
1158 else
1159 {
1160 /* OLD_VERSION is the symbol version of the existing
1161 symbol. */
1162 char *old_version;
1163
1164 if (h->versioned >= versioned)
1165 old_version = strrchr (h->root.root.string,
1166 ELF_VER_CHR) + 1;
1167 else
1168 old_version = NULL;
1169
1170 /* The new symbol matches the existing symbol if they
1171 have the same symbol version. */
1172 *matched = (old_version == new_version
1173 || (old_version != NULL
1174 && new_version != NULL
1175 && strcmp (old_version, new_version) == 0));
1176 }
1177 }
1178 }
1179
1180 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1181 existing symbol. */
1182
1183 oldbfd = NULL;
1184 oldsec = NULL;
1185 switch (h->root.type)
1186 {
1187 default:
1188 break;
1189
1190 case bfd_link_hash_undefined:
1191 case bfd_link_hash_undefweak:
1192 oldbfd = h->root.u.undef.abfd;
1193 break;
1194
1195 case bfd_link_hash_defined:
1196 case bfd_link_hash_defweak:
1197 oldbfd = h->root.u.def.section->owner;
1198 oldsec = h->root.u.def.section;
1199 break;
1200
1201 case bfd_link_hash_common:
1202 oldbfd = h->root.u.c.p->section->owner;
1203 oldsec = h->root.u.c.p->section;
1204 if (pold_alignment)
1205 *pold_alignment = h->root.u.c.p->alignment_power;
1206 break;
1207 }
1208 if (poldbfd && *poldbfd == NULL)
1209 *poldbfd = oldbfd;
1210
1211 /* Differentiate strong and weak symbols. */
1212 newweak = bind == STB_WEAK;
1213 oldweak = (h->root.type == bfd_link_hash_defweak
1214 || h->root.type == bfd_link_hash_undefweak);
1215 if (pold_weak)
1216 *pold_weak = oldweak;
1217
1218 /* We have to check it for every instance since the first few may be
1219 references and not all compilers emit symbol type for undefined
1220 symbols. */
1221 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1222
1223 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1224 respectively, is from a dynamic object. */
1225
1226 newdyn = (abfd->flags & DYNAMIC) != 0;
1227
1228 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1229 syms and defined syms in dynamic libraries respectively.
1230 ref_dynamic on the other hand can be set for a symbol defined in
1231 a dynamic library, and def_dynamic may not be set; When the
1232 definition in a dynamic lib is overridden by a definition in the
1233 executable use of the symbol in the dynamic lib becomes a
1234 reference to the executable symbol. */
1235 if (newdyn)
1236 {
1237 if (bfd_is_und_section (sec))
1238 {
1239 if (bind != STB_WEAK)
1240 {
1241 h->ref_dynamic_nonweak = 1;
1242 hi->ref_dynamic_nonweak = 1;
1243 }
1244 }
1245 else
1246 {
1247 /* Update the existing symbol only if they match. */
1248 if (*matched)
1249 h->dynamic_def = 1;
1250 hi->dynamic_def = 1;
1251 }
1252 }
1253
1254 /* If we just created the symbol, mark it as being an ELF symbol.
1255 Other than that, there is nothing to do--there is no merge issue
1256 with a newly defined symbol--so we just return. */
1257
1258 if (h->root.type == bfd_link_hash_new)
1259 {
1260 h->non_elf = 0;
1261 return true;
1262 }
1263
1264 /* In cases involving weak versioned symbols, we may wind up trying
1265 to merge a symbol with itself. Catch that here, to avoid the
1266 confusion that results if we try to override a symbol with
1267 itself. The additional tests catch cases like
1268 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1269 dynamic object, which we do want to handle here. */
1270 if (abfd == oldbfd
1271 && (newweak || oldweak)
1272 && ((abfd->flags & DYNAMIC) == 0
1273 || !h->def_regular))
1274 return true;
1275
1276 olddyn = false;
1277 if (oldbfd != NULL)
1278 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1279 else if (oldsec != NULL)
1280 {
1281 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1282 indices used by MIPS ELF. */
1283 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1284 }
1285
1286 if (oldbfd != NULL
1287 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN))
1288 {
1289 if (newdyn != olddyn)
1290 {
1291 /* Handle a case where plugin_notice won't be called and thus
1292 won't set the non_ir_ref flags on the first pass over
1293 symbols. */
1294 h->root.non_ir_ref_dynamic = true;
1295 hi->root.non_ir_ref_dynamic = true;
1296 }
1297 else if ((oldbfd->flags & BFD_PLUGIN) != 0
1298 && hi->root.type == bfd_link_hash_indirect)
1299 {
1300 /* Change indirect symbol from IR to undefined. */
1301 hi->root.type = bfd_link_hash_undefined;
1302 hi->root.u.undef.abfd = oldbfd;
1303 }
1304 }
1305
1306 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1307 respectively, appear to be a definition rather than reference. */
1308
1309 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1310
1311 olddef = (h->root.type != bfd_link_hash_undefined
1312 && h->root.type != bfd_link_hash_undefweak
1313 && h->root.type != bfd_link_hash_common);
1314
1315 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1316 respectively, appear to be a function. */
1317
1318 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1319 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1320
1321 oldfunc = (h->type != STT_NOTYPE
1322 && bed->is_function_type (h->type));
1323
1324 if (!(newfunc && oldfunc)
1325 && ELF_ST_TYPE (sym->st_info) != h->type
1326 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1327 && h->type != STT_NOTYPE
1328 && (newdef || bfd_is_com_section (sec))
1329 && (olddef || h->root.type == bfd_link_hash_common))
1330 {
1331 /* If creating a default indirect symbol ("foo" or "foo@") from
1332 a dynamic versioned definition ("foo@@") skip doing so if
1333 there is an existing regular definition with a different
1334 type. We don't want, for example, a "time" variable in the
1335 executable overriding a "time" function in a shared library. */
1336 if (newdyn
1337 && !olddyn)
1338 {
1339 *skip = true;
1340 return true;
1341 }
1342
1343 /* When adding a symbol from a regular object file after we have
1344 created indirect symbols, undo the indirection and any
1345 dynamic state. */
1346 if (hi != h
1347 && !newdyn
1348 && olddyn)
1349 {
1350 h = hi;
1351 (*bed->elf_backend_hide_symbol) (info, h, true);
1352 h->forced_local = 0;
1353 h->ref_dynamic = 0;
1354 h->def_dynamic = 0;
1355 h->dynamic_def = 0;
1356 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1357 {
1358 h->root.type = bfd_link_hash_undefined;
1359 h->root.u.undef.abfd = abfd;
1360 }
1361 else
1362 {
1363 h->root.type = bfd_link_hash_new;
1364 h->root.u.undef.abfd = NULL;
1365 }
1366 return true;
1367 }
1368 }
1369
1370 /* Check TLS symbols. We don't check undefined symbols introduced
1371 by "ld -u" which have no type (and oldbfd NULL), and we don't
1372 check symbols from plugins because they also have no type. */
1373 if (oldbfd != NULL
1374 && (oldbfd->flags & BFD_PLUGIN) == 0
1375 && (abfd->flags & BFD_PLUGIN) == 0
1376 && ELF_ST_TYPE (sym->st_info) != h->type
1377 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1378 {
1379 bfd *ntbfd, *tbfd;
1380 bool ntdef, tdef;
1381 asection *ntsec, *tsec;
1382
1383 if (h->type == STT_TLS)
1384 {
1385 ntbfd = abfd;
1386 ntsec = sec;
1387 ntdef = newdef;
1388 tbfd = oldbfd;
1389 tsec = oldsec;
1390 tdef = olddef;
1391 }
1392 else
1393 {
1394 ntbfd = oldbfd;
1395 ntsec = oldsec;
1396 ntdef = olddef;
1397 tbfd = abfd;
1398 tsec = sec;
1399 tdef = newdef;
1400 }
1401
1402 if (tdef && ntdef)
1403 _bfd_error_handler
1404 /* xgettext:c-format */
1405 (_("%s: TLS definition in %pB section %pA "
1406 "mismatches non-TLS definition in %pB section %pA"),
1407 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
1408 else if (!tdef && !ntdef)
1409 _bfd_error_handler
1410 /* xgettext:c-format */
1411 (_("%s: TLS reference in %pB "
1412 "mismatches non-TLS reference in %pB"),
1413 h->root.root.string, tbfd, ntbfd);
1414 else if (tdef)
1415 _bfd_error_handler
1416 /* xgettext:c-format */
1417 (_("%s: TLS definition in %pB section %pA "
1418 "mismatches non-TLS reference in %pB"),
1419 h->root.root.string, tbfd, tsec, ntbfd);
1420 else
1421 _bfd_error_handler
1422 /* xgettext:c-format */
1423 (_("%s: TLS reference in %pB "
1424 "mismatches non-TLS definition in %pB section %pA"),
1425 h->root.root.string, tbfd, ntbfd, ntsec);
1426
1427 bfd_set_error (bfd_error_bad_value);
1428 return false;
1429 }
1430
1431 /* If the old symbol has non-default visibility, we ignore the new
1432 definition from a dynamic object. */
1433 if (newdyn
1434 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1435 && !bfd_is_und_section (sec))
1436 {
1437 *skip = true;
1438 /* Make sure this symbol is dynamic. */
1439 h->ref_dynamic = 1;
1440 hi->ref_dynamic = 1;
1441 /* A protected symbol has external availability. Make sure it is
1442 recorded as dynamic.
1443
1444 FIXME: Should we check type and size for protected symbol? */
1445 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1446 return bfd_elf_link_record_dynamic_symbol (info, h);
1447 else
1448 return true;
1449 }
1450 else if (!newdyn
1451 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1452 && h->def_dynamic)
1453 {
1454 /* If the new symbol with non-default visibility comes from a
1455 relocatable file and the old definition comes from a dynamic
1456 object, we remove the old definition. */
1457 if (hi->root.type == bfd_link_hash_indirect)
1458 {
1459 /* Handle the case where the old dynamic definition is
1460 default versioned. We need to copy the symbol info from
1461 the symbol with default version to the normal one if it
1462 was referenced before. */
1463 if (h->ref_regular)
1464 {
1465 hi->root.type = h->root.type;
1466 h->root.type = bfd_link_hash_indirect;
1467 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1468
1469 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1470 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1471 {
1472 /* If the new symbol is hidden or internal, completely undo
1473 any dynamic link state. */
1474 (*bed->elf_backend_hide_symbol) (info, h, true);
1475 h->forced_local = 0;
1476 h->ref_dynamic = 0;
1477 }
1478 else
1479 h->ref_dynamic = 1;
1480
1481 h->def_dynamic = 0;
1482 /* FIXME: Should we check type and size for protected symbol? */
1483 h->size = 0;
1484 h->type = 0;
1485
1486 h = hi;
1487 }
1488 else
1489 h = hi;
1490 }
1491
1492 /* If the old symbol was undefined before, then it will still be
1493 on the undefs list. If the new symbol is undefined or
1494 common, we can't make it bfd_link_hash_new here, because new
1495 undefined or common symbols will be added to the undefs list
1496 by _bfd_generic_link_add_one_symbol. Symbols may not be
1497 added twice to the undefs list. Also, if the new symbol is
1498 undefweak then we don't want to lose the strong undef. */
1499 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1500 {
1501 h->root.type = bfd_link_hash_undefined;
1502 h->root.u.undef.abfd = abfd;
1503 }
1504 else
1505 {
1506 h->root.type = bfd_link_hash_new;
1507 h->root.u.undef.abfd = NULL;
1508 }
1509
1510 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1511 {
1512 /* If the new symbol is hidden or internal, completely undo
1513 any dynamic link state. */
1514 (*bed->elf_backend_hide_symbol) (info, h, true);
1515 h->forced_local = 0;
1516 h->ref_dynamic = 0;
1517 }
1518 else
1519 h->ref_dynamic = 1;
1520 h->def_dynamic = 0;
1521 /* FIXME: Should we check type and size for protected symbol? */
1522 h->size = 0;
1523 h->type = 0;
1524 return true;
1525 }
1526
1527 /* If a new weak symbol definition comes from a regular file and the
1528 old symbol comes from a dynamic library, we treat the new one as
1529 strong. Similarly, an old weak symbol definition from a regular
1530 file is treated as strong when the new symbol comes from a dynamic
1531 library. Further, an old weak symbol from a dynamic library is
1532 treated as strong if the new symbol is from a dynamic library.
1533 This reflects the way glibc's ld.so works.
1534
1535 Also allow a weak symbol to override a linker script symbol
1536 defined by an early pass over the script. This is done so the
1537 linker knows the symbol is defined in an object file, for the
1538 DEFINED script function.
1539
1540 Do this before setting *type_change_ok or *size_change_ok so that
1541 we warn properly when dynamic library symbols are overridden. */
1542
1543 if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
1544 newweak = false;
1545 if (olddef && newdyn)
1546 oldweak = false;
1547
1548 /* Allow changes between different types of function symbol. */
1549 if (newfunc && oldfunc)
1550 *type_change_ok = true;
1551
1552 /* It's OK to change the type if either the existing symbol or the
1553 new symbol is weak. A type change is also OK if the old symbol
1554 is undefined and the new symbol is defined. */
1555
1556 if (oldweak
1557 || newweak
1558 || (newdef
1559 && h->root.type == bfd_link_hash_undefined))
1560 *type_change_ok = true;
1561
1562 /* It's OK to change the size if either the existing symbol or the
1563 new symbol is weak, or if the old symbol is undefined. */
1564
1565 if (*type_change_ok
1566 || h->root.type == bfd_link_hash_undefined)
1567 *size_change_ok = true;
1568
1569 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1570 symbol, respectively, appears to be a common symbol in a dynamic
1571 object. If a symbol appears in an uninitialized section, and is
1572 not weak, and is not a function, then it may be a common symbol
1573 which was resolved when the dynamic object was created. We want
1574 to treat such symbols specially, because they raise special
1575 considerations when setting the symbol size: if the symbol
1576 appears as a common symbol in a regular object, and the size in
1577 the regular object is larger, we must make sure that we use the
1578 larger size. This problematic case can always be avoided in C,
1579 but it must be handled correctly when using Fortran shared
1580 libraries.
1581
1582 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1583 likewise for OLDDYNCOMMON and OLDDEF.
1584
1585 Note that this test is just a heuristic, and that it is quite
1586 possible to have an uninitialized symbol in a shared object which
1587 is really a definition, rather than a common symbol. This could
1588 lead to some minor confusion when the symbol really is a common
1589 symbol in some regular object. However, I think it will be
1590 harmless. */
1591
1592 if (newdyn
1593 && newdef
1594 && !newweak
1595 && (sec->flags & SEC_ALLOC) != 0
1596 && (sec->flags & SEC_LOAD) == 0
1597 && sym->st_size > 0
1598 && !newfunc)
1599 newdyncommon = true;
1600 else
1601 newdyncommon = false;
1602
1603 if (olddyn
1604 && olddef
1605 && h->root.type == bfd_link_hash_defined
1606 && h->def_dynamic
1607 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1608 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1609 && h->size > 0
1610 && !oldfunc)
1611 olddyncommon = true;
1612 else
1613 olddyncommon = false;
1614
1615 /* We now know everything about the old and new symbols. We ask the
1616 backend to check if we can merge them. */
1617 if (bed->merge_symbol != NULL)
1618 {
1619 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1620 return false;
1621 sec = *psec;
1622 }
1623
1624 /* There are multiple definitions of a normal symbol. Skip the
1625 default symbol as well as definition from an IR object. */
1626 if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
1627 && !default_sym && h->def_regular
1628 && !(oldbfd != NULL
1629 && (oldbfd->flags & BFD_PLUGIN) != 0
1630 && (abfd->flags & BFD_PLUGIN) == 0))
1631 {
1632 /* Handle a multiple definition. */
1633 (*info->callbacks->multiple_definition) (info, &h->root,
1634 abfd, sec, *pvalue);
1635 *skip = true;
1636 return true;
1637 }
1638
1639 /* If both the old and the new symbols look like common symbols in a
1640 dynamic object, set the size of the symbol to the larger of the
1641 two. */
1642
1643 if (olddyncommon
1644 && newdyncommon
1645 && sym->st_size != h->size)
1646 {
1647 /* Since we think we have two common symbols, issue a multiple
1648 common warning if desired. Note that we only warn if the
1649 size is different. If the size is the same, we simply let
1650 the old symbol override the new one as normally happens with
1651 symbols defined in dynamic objects. */
1652
1653 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1654 bfd_link_hash_common, sym->st_size);
1655 if (sym->st_size > h->size)
1656 h->size = sym->st_size;
1657
1658 *size_change_ok = true;
1659 }
1660
1661 /* If we are looking at a dynamic object, and we have found a
1662 definition, we need to see if the symbol was already defined by
1663 some other object. If so, we want to use the existing
1664 definition, and we do not want to report a multiple symbol
1665 definition error; we do this by clobbering *PSEC to be
1666 bfd_und_section_ptr.
1667
1668 We treat a common symbol as a definition if the symbol in the
1669 shared library is a function, since common symbols always
1670 represent variables; this can cause confusion in principle, but
1671 any such confusion would seem to indicate an erroneous program or
1672 shared library. We also permit a common symbol in a regular
1673 object to override a weak symbol in a shared object. */
1674
1675 if (newdyn
1676 && newdef
1677 && (olddef
1678 || (h->root.type == bfd_link_hash_common
1679 && (newweak || newfunc))))
1680 {
1681 *override = abfd;
1682 newdef = false;
1683 newdyncommon = false;
1684
1685 *psec = sec = bfd_und_section_ptr;
1686 *size_change_ok = true;
1687
1688 /* If we get here when the old symbol is a common symbol, then
1689 we are explicitly letting it override a weak symbol or
1690 function in a dynamic object, and we don't want to warn about
1691 a type change. If the old symbol is a defined symbol, a type
1692 change warning may still be appropriate. */
1693
1694 if (h->root.type == bfd_link_hash_common)
1695 *type_change_ok = true;
1696 }
1697
1698 /* Handle the special case of an old common symbol merging with a
1699 new symbol which looks like a common symbol in a shared object.
1700 We change *PSEC and *PVALUE to make the new symbol look like a
1701 common symbol, and let _bfd_generic_link_add_one_symbol do the
1702 right thing. */
1703
1704 if (newdyncommon
1705 && h->root.type == bfd_link_hash_common)
1706 {
1707 *override = oldbfd;
1708 newdef = false;
1709 newdyncommon = false;
1710 *pvalue = sym->st_size;
1711 *psec = sec = bed->common_section (oldsec);
1712 *size_change_ok = true;
1713 }
1714
1715 /* Skip weak definitions of symbols that are already defined. */
1716 if (newdef && olddef && newweak)
1717 {
1718 /* Don't skip new non-IR weak syms. */
1719 if (!(oldbfd != NULL
1720 && (oldbfd->flags & BFD_PLUGIN) != 0
1721 && (abfd->flags & BFD_PLUGIN) == 0))
1722 {
1723 newdef = false;
1724 *skip = true;
1725 }
1726
1727 /* Merge st_other. If the symbol already has a dynamic index,
1728 but visibility says it should not be visible, turn it into a
1729 local symbol. */
1730 elf_merge_st_other (abfd, h, sym->st_other, sec, newdef, newdyn);
1731 if (h->dynindx != -1)
1732 switch (ELF_ST_VISIBILITY (h->other))
1733 {
1734 case STV_INTERNAL:
1735 case STV_HIDDEN:
1736 (*bed->elf_backend_hide_symbol) (info, h, true);
1737 break;
1738 }
1739 }
1740
1741 /* If the old symbol is from a dynamic object, and the new symbol is
1742 a definition which is not from a dynamic object, then the new
1743 symbol overrides the old symbol. Symbols from regular files
1744 always take precedence over symbols from dynamic objects, even if
1745 they are defined after the dynamic object in the link.
1746
1747 As above, we again permit a common symbol in a regular object to
1748 override a definition in a shared object if the shared object
1749 symbol is a function or is weak. */
1750
1751 flip = NULL;
1752 if (!newdyn
1753 && (newdef
1754 || (bfd_is_com_section (sec)
1755 && (oldweak || oldfunc)))
1756 && olddyn
1757 && olddef
1758 && h->def_dynamic)
1759 {
1760 /* Change the hash table entry to undefined, and let
1761 _bfd_generic_link_add_one_symbol do the right thing with the
1762 new definition. */
1763
1764 h->root.type = bfd_link_hash_undefined;
1765 h->root.u.undef.abfd = h->root.u.def.section->owner;
1766 *size_change_ok = true;
1767
1768 olddef = false;
1769 olddyncommon = false;
1770
1771 /* We again permit a type change when a common symbol may be
1772 overriding a function. */
1773
1774 if (bfd_is_com_section (sec))
1775 {
1776 if (oldfunc)
1777 {
1778 /* If a common symbol overrides a function, make sure
1779 that it isn't defined dynamically nor has type
1780 function. */
1781 h->def_dynamic = 0;
1782 h->type = STT_NOTYPE;
1783 }
1784 *type_change_ok = true;
1785 }
1786
1787 if (hi->root.type == bfd_link_hash_indirect)
1788 flip = hi;
1789 else
1790 /* This union may have been set to be non-NULL when this symbol
1791 was seen in a dynamic object. We must force the union to be
1792 NULL, so that it is correct for a regular symbol. */
1793 h->verinfo.vertree = NULL;
1794 }
1795
1796 /* Handle the special case of a new common symbol merging with an
1797 old symbol that looks like it might be a common symbol defined in
1798 a shared object. Note that we have already handled the case in
1799 which a new common symbol should simply override the definition
1800 in the shared library. */
1801
1802 if (! newdyn
1803 && bfd_is_com_section (sec)
1804 && olddyncommon)
1805 {
1806 /* It would be best if we could set the hash table entry to a
1807 common symbol, but we don't know what to use for the section
1808 or the alignment. */
1809 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1810 bfd_link_hash_common, sym->st_size);
1811
1812 /* If the presumed common symbol in the dynamic object is
1813 larger, pretend that the new symbol has its size. */
1814
1815 if (h->size > *pvalue)
1816 *pvalue = h->size;
1817
1818 /* We need to remember the alignment required by the symbol
1819 in the dynamic object. */
1820 BFD_ASSERT (pold_alignment);
1821 *pold_alignment = h->root.u.def.section->alignment_power;
1822
1823 olddef = false;
1824 olddyncommon = false;
1825
1826 h->root.type = bfd_link_hash_undefined;
1827 h->root.u.undef.abfd = h->root.u.def.section->owner;
1828
1829 *size_change_ok = true;
1830 *type_change_ok = true;
1831
1832 if (hi->root.type == bfd_link_hash_indirect)
1833 flip = hi;
1834 else
1835 h->verinfo.vertree = NULL;
1836 }
1837
1838 if (flip != NULL)
1839 {
1840 /* Handle the case where we had a versioned symbol in a dynamic
1841 library and now find a definition in a normal object. In this
1842 case, we make the versioned symbol point to the normal one. */
1843 flip->root.type = h->root.type;
1844 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1845 h->root.type = bfd_link_hash_indirect;
1846 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1847 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1848 if (h->def_dynamic)
1849 {
1850 h->def_dynamic = 0;
1851 flip->ref_dynamic = 1;
1852 }
1853 }
1854
1855 return true;
1856 }
1857
1858 /* This function is called to create an indirect symbol from the
1859 default for the symbol with the default version if needed. The
1860 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1861 set DYNSYM if the new indirect symbol is dynamic. */
1862
1863 static bool
1864 _bfd_elf_add_default_symbol (bfd *abfd,
1865 struct bfd_link_info *info,
1866 struct elf_link_hash_entry *h,
1867 const char *name,
1868 Elf_Internal_Sym *sym,
1869 asection *sec,
1870 bfd_vma value,
1871 bfd **poldbfd,
1872 bool *dynsym)
1873 {
1874 bool type_change_ok;
1875 bool size_change_ok;
1876 bool skip;
1877 char *shortname;
1878 struct elf_link_hash_entry *hi;
1879 struct bfd_link_hash_entry *bh;
1880 const struct elf_backend_data *bed;
1881 bool collect;
1882 bool dynamic;
1883 bfd *override;
1884 char *p;
1885 size_t len, shortlen;
1886 asection *tmp_sec;
1887 bool matched;
1888
1889 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1890 return true;
1891
1892 /* If this symbol has a version, and it is the default version, we
1893 create an indirect symbol from the default name to the fully
1894 decorated name. This will cause external references which do not
1895 specify a version to be bound to this version of the symbol. */
1896 p = strchr (name, ELF_VER_CHR);
1897 if (h->versioned == unknown)
1898 {
1899 if (p == NULL)
1900 {
1901 h->versioned = unversioned;
1902 return true;
1903 }
1904 else
1905 {
1906 if (p[1] != ELF_VER_CHR)
1907 {
1908 h->versioned = versioned_hidden;
1909 return true;
1910 }
1911 else
1912 h->versioned = versioned;
1913 }
1914 }
1915 else
1916 {
1917 /* PR ld/19073: We may see an unversioned definition after the
1918 default version. */
1919 if (p == NULL)
1920 return true;
1921 }
1922
1923 bed = get_elf_backend_data (abfd);
1924 collect = bed->collect;
1925 dynamic = (abfd->flags & DYNAMIC) != 0;
1926
1927 shortlen = p - name;
1928 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1929 if (shortname == NULL)
1930 return false;
1931 memcpy (shortname, name, shortlen);
1932 shortname[shortlen] = '\0';
1933
1934 /* We are going to create a new symbol. Merge it with any existing
1935 symbol with this name. For the purposes of the merge, act as
1936 though we were defining the symbol we just defined, although we
1937 actually going to define an indirect symbol. */
1938 type_change_ok = false;
1939 size_change_ok = false;
1940 matched = true;
1941 tmp_sec = sec;
1942 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1943 &hi, poldbfd, NULL, NULL, &skip, &override,
1944 &type_change_ok, &size_change_ok, &matched))
1945 return false;
1946
1947 if (skip)
1948 goto nondefault;
1949
1950 if (hi->def_regular || ELF_COMMON_DEF_P (hi))
1951 {
1952 /* If the undecorated symbol will have a version added by a
1953 script different to H, then don't indirect to/from the
1954 undecorated symbol. This isn't ideal because we may not yet
1955 have seen symbol versions, if given by a script on the
1956 command line rather than via --version-script. */
1957 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1958 {
1959 bool hide;
1960
1961 hi->verinfo.vertree
1962 = bfd_find_version_for_sym (info->version_info,
1963 hi->root.root.string, &hide);
1964 if (hi->verinfo.vertree != NULL && hide)
1965 {
1966 (*bed->elf_backend_hide_symbol) (info, hi, true);
1967 goto nondefault;
1968 }
1969 }
1970 if (hi->verinfo.vertree != NULL
1971 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1972 goto nondefault;
1973 }
1974
1975 if (! override)
1976 {
1977 /* Add the default symbol if not performing a relocatable link. */
1978 if (! bfd_link_relocatable (info))
1979 {
1980 bh = &hi->root;
1981 if (bh->type == bfd_link_hash_defined
1982 && bh->u.def.section->owner != NULL
1983 && (bh->u.def.section->owner->flags & BFD_PLUGIN) != 0)
1984 {
1985 /* Mark the previous definition from IR object as
1986 undefined so that the generic linker will override
1987 it. */
1988 bh->type = bfd_link_hash_undefined;
1989 bh->u.undef.abfd = bh->u.def.section->owner;
1990 }
1991 if (! (_bfd_generic_link_add_one_symbol
1992 (info, abfd, shortname, BSF_INDIRECT,
1993 bfd_ind_section_ptr,
1994 0, name, false, collect, &bh)))
1995 return false;
1996 hi = (struct elf_link_hash_entry *) bh;
1997 }
1998 }
1999 else
2000 {
2001 /* In this case the symbol named SHORTNAME is overriding the
2002 indirect symbol we want to add. We were planning on making
2003 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
2004 is the name without a version. NAME is the fully versioned
2005 name, and it is the default version.
2006
2007 Overriding means that we already saw a definition for the
2008 symbol SHORTNAME in a regular object, and it is overriding
2009 the symbol defined in the dynamic object.
2010
2011 When this happens, we actually want to change NAME, the
2012 symbol we just added, to refer to SHORTNAME. This will cause
2013 references to NAME in the shared object to become references
2014 to SHORTNAME in the regular object. This is what we expect
2015 when we override a function in a shared object: that the
2016 references in the shared object will be mapped to the
2017 definition in the regular object. */
2018
2019 while (hi->root.type == bfd_link_hash_indirect
2020 || hi->root.type == bfd_link_hash_warning)
2021 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2022
2023 h->root.type = bfd_link_hash_indirect;
2024 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
2025 if (h->def_dynamic)
2026 {
2027 h->def_dynamic = 0;
2028 hi->ref_dynamic = 1;
2029 if (hi->ref_regular
2030 || hi->def_regular)
2031 {
2032 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
2033 return false;
2034 }
2035 }
2036
2037 /* Now set HI to H, so that the following code will set the
2038 other fields correctly. */
2039 hi = h;
2040 }
2041
2042 /* Check if HI is a warning symbol. */
2043 if (hi->root.type == bfd_link_hash_warning)
2044 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2045
2046 /* If there is a duplicate definition somewhere, then HI may not
2047 point to an indirect symbol. We will have reported an error to
2048 the user in that case. */
2049
2050 if (hi->root.type == bfd_link_hash_indirect)
2051 {
2052 struct elf_link_hash_entry *ht;
2053
2054 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
2055 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
2056
2057 /* If we first saw a reference to SHORTNAME with non-default
2058 visibility, merge that visibility to the @@VER symbol. */
2059 elf_merge_st_other (abfd, ht, hi->other, sec, true, dynamic);
2060
2061 /* A reference to the SHORTNAME symbol from a dynamic library
2062 will be satisfied by the versioned symbol at runtime. In
2063 effect, we have a reference to the versioned symbol. */
2064 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2065 hi->dynamic_def |= ht->dynamic_def;
2066
2067 /* See if the new flags lead us to realize that the symbol must
2068 be dynamic. */
2069 if (! *dynsym)
2070 {
2071 if (! dynamic)
2072 {
2073 if (! bfd_link_executable (info)
2074 || hi->def_dynamic
2075 || hi->ref_dynamic)
2076 *dynsym = true;
2077 }
2078 else
2079 {
2080 if (hi->ref_regular)
2081 *dynsym = true;
2082 }
2083 }
2084 }
2085
2086 /* We also need to define an indirection from the nondefault version
2087 of the symbol. */
2088
2089 nondefault:
2090 len = strlen (name);
2091 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
2092 if (shortname == NULL)
2093 return false;
2094 memcpy (shortname, name, shortlen);
2095 memcpy (shortname + shortlen, p + 1, len - shortlen);
2096
2097 /* Once again, merge with any existing symbol. */
2098 type_change_ok = false;
2099 size_change_ok = false;
2100 tmp_sec = sec;
2101 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
2102 &hi, poldbfd, NULL, NULL, &skip, &override,
2103 &type_change_ok, &size_change_ok, &matched))
2104 return false;
2105
2106 if (skip)
2107 {
2108 if (!dynamic
2109 && h->root.type == bfd_link_hash_defweak
2110 && hi->root.type == bfd_link_hash_defined)
2111 {
2112 /* We are handling a weak sym@@ver and attempting to define
2113 a weak sym@ver, but _bfd_elf_merge_symbol said to skip the
2114 new weak sym@ver because there is already a strong sym@ver.
2115 However, sym@ver and sym@@ver are really the same symbol.
2116 The existing strong sym@ver ought to override sym@@ver. */
2117 h->root.type = bfd_link_hash_defined;
2118 h->root.u.def.section = hi->root.u.def.section;
2119 h->root.u.def.value = hi->root.u.def.value;
2120 hi->root.type = bfd_link_hash_indirect;
2121 hi->root.u.i.link = &h->root;
2122 }
2123 else
2124 return true;
2125 }
2126 else if (override)
2127 {
2128 /* Here SHORTNAME is a versioned name, so we don't expect to see
2129 the type of override we do in the case above unless it is
2130 overridden by a versioned definition. */
2131 if (hi->root.type != bfd_link_hash_defined
2132 && hi->root.type != bfd_link_hash_defweak)
2133 _bfd_error_handler
2134 /* xgettext:c-format */
2135 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
2136 abfd, shortname);
2137 return true;
2138 }
2139 else
2140 {
2141 bh = &hi->root;
2142 if (! (_bfd_generic_link_add_one_symbol
2143 (info, abfd, shortname, BSF_INDIRECT,
2144 bfd_ind_section_ptr, 0, name, false, collect, &bh)))
2145 return false;
2146 hi = (struct elf_link_hash_entry *) bh;
2147 }
2148
2149 /* If there is a duplicate definition somewhere, then HI may not
2150 point to an indirect symbol. We will have reported an error
2151 to the user in that case. */
2152 if (hi->root.type == bfd_link_hash_indirect)
2153 {
2154 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
2155 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2156 hi->dynamic_def |= h->dynamic_def;
2157
2158 /* If we first saw a reference to @VER symbol with
2159 non-default visibility, merge that visibility to the
2160 @@VER symbol. */
2161 elf_merge_st_other (abfd, h, hi->other, sec, true, dynamic);
2162
2163 /* See if the new flags lead us to realize that the symbol
2164 must be dynamic. */
2165 if (! *dynsym)
2166 {
2167 if (! dynamic)
2168 {
2169 if (! bfd_link_executable (info)
2170 || hi->ref_dynamic)
2171 *dynsym = true;
2172 }
2173 else
2174 {
2175 if (hi->ref_regular)
2176 *dynsym = true;
2177 }
2178 }
2179 }
2180
2181 return true;
2182 }
2183 \f
2184 /* This routine is used to export all defined symbols into the dynamic
2185 symbol table. It is called via elf_link_hash_traverse. */
2186
2187 static bool
2188 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
2189 {
2190 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2191
2192 /* Ignore indirect symbols. These are added by the versioning code. */
2193 if (h->root.type == bfd_link_hash_indirect)
2194 return true;
2195
2196 /* Ignore this if we won't export it. */
2197 if (!eif->info->export_dynamic && !h->dynamic)
2198 return true;
2199
2200 if (h->dynindx == -1
2201 && (h->def_regular || h->ref_regular)
2202 && ! bfd_hide_sym_by_version (eif->info->version_info,
2203 h->root.root.string))
2204 {
2205 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2206 {
2207 eif->failed = true;
2208 return false;
2209 }
2210 }
2211
2212 return true;
2213 }
2214 \f
2215 /* Return true if GLIBC_ABI_DT_RELR is added to the list of version
2216 dependencies successfully. GLIBC_ABI_DT_RELR will be put into the
2217 .gnu.version_r section. */
2218
2219 static bool
2220 elf_link_add_dt_relr_dependency (struct elf_find_verdep_info *rinfo)
2221 {
2222 bfd *glibc_bfd = NULL;
2223 Elf_Internal_Verneed *t;
2224 Elf_Internal_Vernaux *a;
2225 size_t amt;
2226 const char *relr = "GLIBC_ABI_DT_RELR";
2227
2228 /* See if we already know about GLIBC_PRIVATE_DT_RELR. */
2229 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2230 t != NULL;
2231 t = t->vn_nextref)
2232 {
2233 const char *soname = bfd_elf_get_dt_soname (t->vn_bfd);
2234 /* Skip the shared library if it isn't libc.so. */
2235 if (!soname || !startswith (soname, "libc.so."))
2236 continue;
2237
2238 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2239 {
2240 /* Return if GLIBC_PRIVATE_DT_RELR dependency has been
2241 added. */
2242 if (a->vna_nodename == relr
2243 || strcmp (a->vna_nodename, relr) == 0)
2244 return true;
2245
2246 /* Check if libc.so provides GLIBC_2.XX version. */
2247 if (!glibc_bfd && startswith (a->vna_nodename, "GLIBC_2."))
2248 glibc_bfd = t->vn_bfd;
2249 }
2250
2251 break;
2252 }
2253
2254 /* Skip if it isn't linked against glibc. */
2255 if (glibc_bfd == NULL)
2256 return true;
2257
2258 /* This is a new version. Add it to tree we are building. */
2259 if (t == NULL)
2260 {
2261 amt = sizeof *t;
2262 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd,
2263 amt);
2264 if (t == NULL)
2265 {
2266 rinfo->failed = true;
2267 return false;
2268 }
2269
2270 t->vn_bfd = glibc_bfd;
2271 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2272 elf_tdata (rinfo->info->output_bfd)->verref = t;
2273 }
2274
2275 amt = sizeof *a;
2276 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2277 if (a == NULL)
2278 {
2279 rinfo->failed = true;
2280 return false;
2281 }
2282
2283 a->vna_nodename = relr;
2284 a->vna_flags = 0;
2285 a->vna_nextptr = t->vn_auxptr;
2286 a->vna_other = rinfo->vers + 1;
2287 ++rinfo->vers;
2288
2289 t->vn_auxptr = a;
2290
2291 return true;
2292 }
2293
2294 /* Look through the symbols which are defined in other shared
2295 libraries and referenced here. Update the list of version
2296 dependencies. This will be put into the .gnu.version_r section.
2297 This function is called via elf_link_hash_traverse. */
2298
2299 static bool
2300 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2301 void *data)
2302 {
2303 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2304 Elf_Internal_Verneed *t;
2305 Elf_Internal_Vernaux *a;
2306 size_t amt;
2307
2308 /* We only care about symbols defined in shared objects with version
2309 information. */
2310 if (!h->def_dynamic
2311 || h->def_regular
2312 || h->dynindx == -1
2313 || h->verinfo.verdef == NULL
2314 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2315 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2316 return true;
2317
2318 /* See if we already know about this version. */
2319 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2320 t != NULL;
2321 t = t->vn_nextref)
2322 {
2323 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2324 continue;
2325
2326 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2327 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2328 return true;
2329
2330 break;
2331 }
2332
2333 /* This is a new version. Add it to tree we are building. */
2334
2335 if (t == NULL)
2336 {
2337 amt = sizeof *t;
2338 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2339 if (t == NULL)
2340 {
2341 rinfo->failed = true;
2342 return false;
2343 }
2344
2345 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2346 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2347 elf_tdata (rinfo->info->output_bfd)->verref = t;
2348 }
2349
2350 amt = sizeof *a;
2351 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2352 if (a == NULL)
2353 {
2354 rinfo->failed = true;
2355 return false;
2356 }
2357
2358 /* Note that we are copying a string pointer here, and testing it
2359 above. If bfd_elf_string_from_elf_section is ever changed to
2360 discard the string data when low in memory, this will have to be
2361 fixed. */
2362 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2363
2364 a->vna_flags = h->verinfo.verdef->vd_flags;
2365 a->vna_nextptr = t->vn_auxptr;
2366
2367 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2368 ++rinfo->vers;
2369
2370 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2371
2372 t->vn_auxptr = a;
2373
2374 return true;
2375 }
2376
2377 /* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2378 hidden. Set *T_P to NULL if there is no match. */
2379
2380 static bool
2381 _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
2382 struct elf_link_hash_entry *h,
2383 const char *version_p,
2384 struct bfd_elf_version_tree **t_p,
2385 bool *hide)
2386 {
2387 struct bfd_elf_version_tree *t;
2388
2389 /* Look for the version. If we find it, it is no longer weak. */
2390 for (t = info->version_info; t != NULL; t = t->next)
2391 {
2392 if (strcmp (t->name, version_p) == 0)
2393 {
2394 size_t len;
2395 char *alc;
2396 struct bfd_elf_version_expr *d;
2397
2398 len = version_p - h->root.root.string;
2399 alc = (char *) bfd_malloc (len);
2400 if (alc == NULL)
2401 return false;
2402 memcpy (alc, h->root.root.string, len - 1);
2403 alc[len - 1] = '\0';
2404 if (alc[len - 2] == ELF_VER_CHR)
2405 alc[len - 2] = '\0';
2406
2407 h->verinfo.vertree = t;
2408 t->used = true;
2409 d = NULL;
2410
2411 if (t->globals.list != NULL)
2412 d = (*t->match) (&t->globals, NULL, alc);
2413
2414 /* See if there is anything to force this symbol to
2415 local scope. */
2416 if (d == NULL && t->locals.list != NULL)
2417 {
2418 d = (*t->match) (&t->locals, NULL, alc);
2419 if (d != NULL
2420 && h->dynindx != -1
2421 && ! info->export_dynamic)
2422 *hide = true;
2423 }
2424
2425 free (alc);
2426 break;
2427 }
2428 }
2429
2430 *t_p = t;
2431
2432 return true;
2433 }
2434
2435 /* Return TRUE if the symbol H is hidden by version script. */
2436
2437 bool
2438 _bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
2439 struct elf_link_hash_entry *h)
2440 {
2441 const char *p;
2442 bool hide = false;
2443 const struct elf_backend_data *bed
2444 = get_elf_backend_data (info->output_bfd);
2445
2446 /* Version script only hides symbols defined in regular objects. */
2447 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2448 return true;
2449
2450 p = strchr (h->root.root.string, ELF_VER_CHR);
2451 if (p != NULL && h->verinfo.vertree == NULL)
2452 {
2453 struct bfd_elf_version_tree *t;
2454
2455 ++p;
2456 if (*p == ELF_VER_CHR)
2457 ++p;
2458
2459 if (*p != '\0'
2460 && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
2461 && hide)
2462 {
2463 if (hide)
2464 (*bed->elf_backend_hide_symbol) (info, h, true);
2465 return true;
2466 }
2467 }
2468
2469 /* If we don't have a version for this symbol, see if we can find
2470 something. */
2471 if (h->verinfo.vertree == NULL && info->version_info != NULL)
2472 {
2473 h->verinfo.vertree
2474 = bfd_find_version_for_sym (info->version_info,
2475 h->root.root.string, &hide);
2476 if (h->verinfo.vertree != NULL && hide)
2477 {
2478 (*bed->elf_backend_hide_symbol) (info, h, true);
2479 return true;
2480 }
2481 }
2482
2483 return false;
2484 }
2485
2486 /* Figure out appropriate versions for all the symbols. We may not
2487 have the version number script until we have read all of the input
2488 files, so until that point we don't know which symbols should be
2489 local. This function is called via elf_link_hash_traverse. */
2490
2491 static bool
2492 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2493 {
2494 struct elf_info_failed *sinfo;
2495 struct bfd_link_info *info;
2496 const struct elf_backend_data *bed;
2497 struct elf_info_failed eif;
2498 char *p;
2499 bool hide;
2500
2501 sinfo = (struct elf_info_failed *) data;
2502 info = sinfo->info;
2503
2504 /* Fix the symbol flags. */
2505 eif.failed = false;
2506 eif.info = info;
2507 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2508 {
2509 if (eif.failed)
2510 sinfo->failed = true;
2511 return false;
2512 }
2513
2514 bed = get_elf_backend_data (info->output_bfd);
2515
2516 /* We only need version numbers for symbols defined in regular
2517 objects. */
2518 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2519 {
2520 /* Hide symbols defined in discarded input sections. */
2521 if ((h->root.type == bfd_link_hash_defined
2522 || h->root.type == bfd_link_hash_defweak)
2523 && discarded_section (h->root.u.def.section))
2524 (*bed->elf_backend_hide_symbol) (info, h, true);
2525 return true;
2526 }
2527
2528 hide = false;
2529 p = strchr (h->root.root.string, ELF_VER_CHR);
2530 if (p != NULL && h->verinfo.vertree == NULL)
2531 {
2532 struct bfd_elf_version_tree *t;
2533
2534 ++p;
2535 if (*p == ELF_VER_CHR)
2536 ++p;
2537
2538 /* If there is no version string, we can just return out. */
2539 if (*p == '\0')
2540 return true;
2541
2542 if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
2543 {
2544 sinfo->failed = true;
2545 return false;
2546 }
2547
2548 if (hide)
2549 (*bed->elf_backend_hide_symbol) (info, h, true);
2550
2551 /* If we are building an application, we need to create a
2552 version node for this version. */
2553 if (t == NULL && bfd_link_executable (info))
2554 {
2555 struct bfd_elf_version_tree **pp;
2556 int version_index;
2557
2558 /* If we aren't going to export this symbol, we don't need
2559 to worry about it. */
2560 if (h->dynindx == -1)
2561 return true;
2562
2563 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2564 sizeof *t);
2565 if (t == NULL)
2566 {
2567 sinfo->failed = true;
2568 return false;
2569 }
2570
2571 t->name = p;
2572 t->name_indx = (unsigned int) -1;
2573 t->used = true;
2574
2575 version_index = 1;
2576 /* Don't count anonymous version tag. */
2577 if (sinfo->info->version_info != NULL
2578 && sinfo->info->version_info->vernum == 0)
2579 version_index = 0;
2580 for (pp = &sinfo->info->version_info;
2581 *pp != NULL;
2582 pp = &(*pp)->next)
2583 ++version_index;
2584 t->vernum = version_index;
2585
2586 *pp = t;
2587
2588 h->verinfo.vertree = t;
2589 }
2590 else if (t == NULL)
2591 {
2592 /* We could not find the version for a symbol when
2593 generating a shared archive. Return an error. */
2594 _bfd_error_handler
2595 /* xgettext:c-format */
2596 (_("%pB: version node not found for symbol %s"),
2597 info->output_bfd, h->root.root.string);
2598 bfd_set_error (bfd_error_bad_value);
2599 sinfo->failed = true;
2600 return false;
2601 }
2602 }
2603
2604 /* If we don't have a version for this symbol, see if we can find
2605 something. */
2606 if (!hide
2607 && h->verinfo.vertree == NULL
2608 && sinfo->info->version_info != NULL)
2609 {
2610 h->verinfo.vertree
2611 = bfd_find_version_for_sym (sinfo->info->version_info,
2612 h->root.root.string, &hide);
2613 if (h->verinfo.vertree != NULL && hide)
2614 (*bed->elf_backend_hide_symbol) (info, h, true);
2615 }
2616
2617 return true;
2618 }
2619 \f
2620 /* Read and swap the relocs from the section indicated by SHDR. This
2621 may be either a REL or a RELA section. The relocations are
2622 translated into RELA relocations and stored in INTERNAL_RELOCS,
2623 which should have already been allocated to contain enough space.
2624 The EXTERNAL_RELOCS are a buffer where the external form of the
2625 relocations should be stored.
2626
2627 Returns FALSE if something goes wrong. */
2628
2629 static bool
2630 elf_link_read_relocs_from_section (bfd *abfd,
2631 asection *sec,
2632 Elf_Internal_Shdr *shdr,
2633 void *external_relocs,
2634 Elf_Internal_Rela *internal_relocs)
2635 {
2636 const struct elf_backend_data *bed;
2637 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2638 const bfd_byte *erela;
2639 const bfd_byte *erelaend;
2640 Elf_Internal_Rela *irela;
2641 Elf_Internal_Shdr *symtab_hdr;
2642 size_t nsyms;
2643
2644 /* Position ourselves at the start of the section. */
2645 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2646 return false;
2647
2648 /* Read the relocations. */
2649 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2650 return false;
2651
2652 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2653 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2654
2655 bed = get_elf_backend_data (abfd);
2656
2657 /* Convert the external relocations to the internal format. */
2658 if (shdr->sh_entsize == bed->s->sizeof_rel)
2659 swap_in = bed->s->swap_reloc_in;
2660 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2661 swap_in = bed->s->swap_reloca_in;
2662 else
2663 {
2664 bfd_set_error (bfd_error_wrong_format);
2665 return false;
2666 }
2667
2668 erela = (const bfd_byte *) external_relocs;
2669 /* Setting erelaend like this and comparing with <= handles case of
2670 a fuzzed object with sh_size not a multiple of sh_entsize. */
2671 erelaend = erela + shdr->sh_size - shdr->sh_entsize;
2672 irela = internal_relocs;
2673 while (erela <= erelaend)
2674 {
2675 bfd_vma r_symndx;
2676
2677 (*swap_in) (abfd, erela, irela);
2678 r_symndx = ELF32_R_SYM (irela->r_info);
2679 if (bed->s->arch_size == 64)
2680 r_symndx >>= 24;
2681 if (nsyms > 0)
2682 {
2683 if ((size_t) r_symndx >= nsyms)
2684 {
2685 _bfd_error_handler
2686 /* xgettext:c-format */
2687 (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
2688 " for offset %#" PRIx64 " in section `%pA'"),
2689 abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
2690 (uint64_t) irela->r_offset, sec);
2691 bfd_set_error (bfd_error_bad_value);
2692 return false;
2693 }
2694 }
2695 else if (r_symndx != STN_UNDEF)
2696 {
2697 _bfd_error_handler
2698 /* xgettext:c-format */
2699 (_("%pB: non-zero symbol index (%#" PRIx64 ")"
2700 " for offset %#" PRIx64 " in section `%pA'"
2701 " when the object file has no symbol table"),
2702 abfd, (uint64_t) r_symndx,
2703 (uint64_t) irela->r_offset, sec);
2704 bfd_set_error (bfd_error_bad_value);
2705 return false;
2706 }
2707 irela += bed->s->int_rels_per_ext_rel;
2708 erela += shdr->sh_entsize;
2709 }
2710
2711 return true;
2712 }
2713
2714 /* Read and swap the relocs for a section O. They may have been
2715 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2716 not NULL, they are used as buffers to read into. They are known to
2717 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2718 the return value is allocated using either malloc or bfd_alloc,
2719 according to the KEEP_MEMORY argument. If O has two relocation
2720 sections (both REL and RELA relocations), then the REL_HDR
2721 relocations will appear first in INTERNAL_RELOCS, followed by the
2722 RELA_HDR relocations. If INFO isn't NULL and KEEP_MEMORY is true,
2723 update cache_size. */
2724
2725 Elf_Internal_Rela *
2726 _bfd_elf_link_info_read_relocs (bfd *abfd,
2727 struct bfd_link_info *info,
2728 asection *o,
2729 void *external_relocs,
2730 Elf_Internal_Rela *internal_relocs,
2731 bool keep_memory)
2732 {
2733 void *alloc1 = NULL;
2734 Elf_Internal_Rela *alloc2 = NULL;
2735 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2736 struct bfd_elf_section_data *esdo = elf_section_data (o);
2737 Elf_Internal_Rela *internal_rela_relocs;
2738
2739 if (esdo->relocs != NULL)
2740 return esdo->relocs;
2741
2742 if (o->reloc_count == 0)
2743 return NULL;
2744
2745 if (internal_relocs == NULL)
2746 {
2747 bfd_size_type size;
2748
2749 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
2750 if (keep_memory)
2751 {
2752 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2753 if (info)
2754 info->cache_size += size;
2755 }
2756 else
2757 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2758 if (internal_relocs == NULL)
2759 goto error_return;
2760 }
2761
2762 if (external_relocs == NULL)
2763 {
2764 bfd_size_type size = 0;
2765
2766 if (esdo->rel.hdr)
2767 size += esdo->rel.hdr->sh_size;
2768 if (esdo->rela.hdr)
2769 size += esdo->rela.hdr->sh_size;
2770
2771 alloc1 = bfd_malloc (size);
2772 if (alloc1 == NULL)
2773 goto error_return;
2774 external_relocs = alloc1;
2775 }
2776
2777 internal_rela_relocs = internal_relocs;
2778 if (esdo->rel.hdr)
2779 {
2780 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2781 external_relocs,
2782 internal_relocs))
2783 goto error_return;
2784 external_relocs = (((bfd_byte *) external_relocs)
2785 + esdo->rel.hdr->sh_size);
2786 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2787 * bed->s->int_rels_per_ext_rel);
2788 }
2789
2790 if (esdo->rela.hdr
2791 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2792 external_relocs,
2793 internal_rela_relocs)))
2794 goto error_return;
2795
2796 /* Cache the results for next time, if we can. */
2797 if (keep_memory)
2798 esdo->relocs = internal_relocs;
2799
2800 free (alloc1);
2801
2802 /* Don't free alloc2, since if it was allocated we are passing it
2803 back (under the name of internal_relocs). */
2804
2805 return internal_relocs;
2806
2807 error_return:
2808 free (alloc1);
2809 if (alloc2 != NULL)
2810 {
2811 if (keep_memory)
2812 bfd_release (abfd, alloc2);
2813 else
2814 free (alloc2);
2815 }
2816 return NULL;
2817 }
2818
2819 /* This is similar to _bfd_elf_link_info_read_relocs, except for that
2820 NULL is passed to _bfd_elf_link_info_read_relocs for pointer to
2821 struct bfd_link_info. */
2822
2823 Elf_Internal_Rela *
2824 _bfd_elf_link_read_relocs (bfd *abfd,
2825 asection *o,
2826 void *external_relocs,
2827 Elf_Internal_Rela *internal_relocs,
2828 bool keep_memory)
2829 {
2830 return _bfd_elf_link_info_read_relocs (abfd, NULL, o, external_relocs,
2831 internal_relocs, keep_memory);
2832
2833 }
2834
2835 /* Compute the size of, and allocate space for, REL_HDR which is the
2836 section header for a section containing relocations for O. */
2837
2838 static bool
2839 _bfd_elf_link_size_reloc_section (bfd *abfd,
2840 struct bfd_elf_section_reloc_data *reldata)
2841 {
2842 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2843
2844 /* That allows us to calculate the size of the section. */
2845 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2846
2847 /* The contents field must last into write_object_contents, so we
2848 allocate it with bfd_alloc rather than malloc. Also since we
2849 cannot be sure that the contents will actually be filled in,
2850 we zero the allocated space. */
2851 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2852 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2853 return false;
2854
2855 if (reldata->hashes == NULL && reldata->count)
2856 {
2857 struct elf_link_hash_entry **p;
2858
2859 p = ((struct elf_link_hash_entry **)
2860 bfd_zmalloc (reldata->count * sizeof (*p)));
2861 if (p == NULL)
2862 return false;
2863
2864 reldata->hashes = p;
2865 }
2866
2867 return true;
2868 }
2869
2870 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2871 originated from the section given by INPUT_REL_HDR) to the
2872 OUTPUT_BFD. */
2873
2874 bool
2875 _bfd_elf_link_output_relocs (bfd *output_bfd,
2876 asection *input_section,
2877 Elf_Internal_Shdr *input_rel_hdr,
2878 Elf_Internal_Rela *internal_relocs,
2879 struct elf_link_hash_entry **rel_hash
2880 ATTRIBUTE_UNUSED)
2881 {
2882 Elf_Internal_Rela *irela;
2883 Elf_Internal_Rela *irelaend;
2884 bfd_byte *erel;
2885 struct bfd_elf_section_reloc_data *output_reldata;
2886 asection *output_section;
2887 const struct elf_backend_data *bed;
2888 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2889 struct bfd_elf_section_data *esdo;
2890
2891 output_section = input_section->output_section;
2892
2893 bed = get_elf_backend_data (output_bfd);
2894 esdo = elf_section_data (output_section);
2895 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2896 {
2897 output_reldata = &esdo->rel;
2898 swap_out = bed->s->swap_reloc_out;
2899 }
2900 else if (esdo->rela.hdr
2901 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2902 {
2903 output_reldata = &esdo->rela;
2904 swap_out = bed->s->swap_reloca_out;
2905 }
2906 else
2907 {
2908 _bfd_error_handler
2909 /* xgettext:c-format */
2910 (_("%pB: relocation size mismatch in %pB section %pA"),
2911 output_bfd, input_section->owner, input_section);
2912 bfd_set_error (bfd_error_wrong_format);
2913 return false;
2914 }
2915
2916 erel = output_reldata->hdr->contents;
2917 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2918 irela = internal_relocs;
2919 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2920 * bed->s->int_rels_per_ext_rel);
2921 while (irela < irelaend)
2922 {
2923 (*swap_out) (output_bfd, irela, erel);
2924 irela += bed->s->int_rels_per_ext_rel;
2925 erel += input_rel_hdr->sh_entsize;
2926 }
2927
2928 /* Bump the counter, so that we know where to add the next set of
2929 relocations. */
2930 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2931
2932 return true;
2933 }
2934 \f
2935 /* Make weak undefined symbols in PIE dynamic. */
2936
2937 bool
2938 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2939 struct elf_link_hash_entry *h)
2940 {
2941 if (bfd_link_pie (info)
2942 && h->dynindx == -1
2943 && h->root.type == bfd_link_hash_undefweak)
2944 return bfd_elf_link_record_dynamic_symbol (info, h);
2945
2946 return true;
2947 }
2948
2949 /* Fix up the flags for a symbol. This handles various cases which
2950 can only be fixed after all the input files are seen. This is
2951 currently called by both adjust_dynamic_symbol and
2952 assign_sym_version, which is unnecessary but perhaps more robust in
2953 the face of future changes. */
2954
2955 static bool
2956 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2957 struct elf_info_failed *eif)
2958 {
2959 const struct elf_backend_data *bed;
2960
2961 /* If this symbol was mentioned in a non-ELF file, try to set
2962 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2963 permit a non-ELF file to correctly refer to a symbol defined in
2964 an ELF dynamic object. */
2965 if (h->non_elf)
2966 {
2967 while (h->root.type == bfd_link_hash_indirect)
2968 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2969
2970 if (h->root.type != bfd_link_hash_defined
2971 && h->root.type != bfd_link_hash_defweak)
2972 {
2973 h->ref_regular = 1;
2974 h->ref_regular_nonweak = 1;
2975 }
2976 else
2977 {
2978 if (h->root.u.def.section->owner != NULL
2979 && (bfd_get_flavour (h->root.u.def.section->owner)
2980 == bfd_target_elf_flavour))
2981 {
2982 h->ref_regular = 1;
2983 h->ref_regular_nonweak = 1;
2984 }
2985 else
2986 h->def_regular = 1;
2987 }
2988
2989 if (h->dynindx == -1
2990 && (h->def_dynamic
2991 || h->ref_dynamic))
2992 {
2993 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2994 {
2995 eif->failed = true;
2996 return false;
2997 }
2998 }
2999 }
3000 else
3001 {
3002 /* Unfortunately, NON_ELF is only correct if the symbol
3003 was first seen in a non-ELF file. Fortunately, if the symbol
3004 was first seen in an ELF file, we're probably OK unless the
3005 symbol was defined in a non-ELF file. Catch that case here.
3006 FIXME: We're still in trouble if the symbol was first seen in
3007 a dynamic object, and then later in a non-ELF regular object. */
3008 if ((h->root.type == bfd_link_hash_defined
3009 || h->root.type == bfd_link_hash_defweak)
3010 && !h->def_regular
3011 && (h->root.u.def.section->owner != NULL
3012 ? (bfd_get_flavour (h->root.u.def.section->owner)
3013 != bfd_target_elf_flavour)
3014 : (bfd_is_abs_section (h->root.u.def.section)
3015 && !h->def_dynamic)))
3016 h->def_regular = 1;
3017 }
3018
3019 /* Backend specific symbol fixup. */
3020 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
3021 if (bed->elf_backend_fixup_symbol
3022 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
3023 return false;
3024
3025 /* If this is a final link, and the symbol was defined as a common
3026 symbol in a regular object file, and there was no definition in
3027 any dynamic object, then the linker will have allocated space for
3028 the symbol in a common section but the DEF_REGULAR
3029 flag will not have been set. */
3030 if (h->root.type == bfd_link_hash_defined
3031 && !h->def_regular
3032 && h->ref_regular
3033 && !h->def_dynamic
3034 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
3035 h->def_regular = 1;
3036
3037 /* Symbols defined in discarded sections shouldn't be dynamic. */
3038 if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
3039 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3040
3041 /* If a weak undefined symbol has non-default visibility, we also
3042 hide it from the dynamic linker. */
3043 else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3044 && h->root.type == bfd_link_hash_undefweak)
3045 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3046
3047 /* A hidden versioned symbol in executable should be forced local if
3048 it is is locally defined, not referenced by shared library and not
3049 exported. */
3050 else if (bfd_link_executable (eif->info)
3051 && h->versioned == versioned_hidden
3052 && !eif->info->export_dynamic
3053 && !h->dynamic
3054 && !h->ref_dynamic
3055 && h->def_regular)
3056 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3057
3058 /* If -Bsymbolic was used (which means to bind references to global
3059 symbols to the definition within the shared object), and this
3060 symbol was defined in a regular object, then it actually doesn't
3061 need a PLT entry. Likewise, if the symbol has non-default
3062 visibility. If the symbol has hidden or internal visibility, we
3063 will force it local. */
3064 else if (h->needs_plt
3065 && bfd_link_pic (eif->info)
3066 && is_elf_hash_table (eif->info->hash)
3067 && (SYMBOLIC_BIND (eif->info, h)
3068 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
3069 && h->def_regular)
3070 {
3071 bool force_local;
3072
3073 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
3074 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
3075 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
3076 }
3077
3078 /* If this is a weak defined symbol in a dynamic object, and we know
3079 the real definition in the dynamic object, copy interesting flags
3080 over to the real definition. */
3081 if (h->is_weakalias)
3082 {
3083 struct elf_link_hash_entry *def = weakdef (h);
3084
3085 /* If the real definition is defined by a regular object file,
3086 don't do anything special. See the longer description in
3087 _bfd_elf_adjust_dynamic_symbol, below. If the def is not
3088 bfd_link_hash_defined as it was when put on the alias list
3089 then it must have originally been a versioned symbol (for
3090 which a non-versioned indirect symbol is created) and later
3091 a definition for the non-versioned symbol is found. In that
3092 case the indirection is flipped with the versioned symbol
3093 becoming an indirect pointing at the non-versioned symbol.
3094 Thus, not an alias any more. */
3095 if (def->def_regular
3096 || def->root.type != bfd_link_hash_defined)
3097 {
3098 h = def;
3099 while ((h = h->u.alias) != def)
3100 h->is_weakalias = 0;
3101 }
3102 else
3103 {
3104 while (h->root.type == bfd_link_hash_indirect)
3105 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3106 BFD_ASSERT (h->root.type == bfd_link_hash_defined
3107 || h->root.type == bfd_link_hash_defweak);
3108 BFD_ASSERT (def->def_dynamic);
3109 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
3110 }
3111 }
3112
3113 return true;
3114 }
3115
3116 /* Make the backend pick a good value for a dynamic symbol. This is
3117 called via elf_link_hash_traverse, and also calls itself
3118 recursively. */
3119
3120 static bool
3121 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
3122 {
3123 struct elf_info_failed *eif = (struct elf_info_failed *) data;
3124 struct elf_link_hash_table *htab;
3125 const struct elf_backend_data *bed;
3126
3127 if (! is_elf_hash_table (eif->info->hash))
3128 return false;
3129
3130 /* Ignore indirect symbols. These are added by the versioning code. */
3131 if (h->root.type == bfd_link_hash_indirect)
3132 return true;
3133
3134 /* Fix the symbol flags. */
3135 if (! _bfd_elf_fix_symbol_flags (h, eif))
3136 return false;
3137
3138 htab = elf_hash_table (eif->info);
3139 bed = get_elf_backend_data (htab->dynobj);
3140
3141 if (h->root.type == bfd_link_hash_undefweak)
3142 {
3143 if (eif->info->dynamic_undefined_weak == 0)
3144 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3145 else if (eif->info->dynamic_undefined_weak > 0
3146 && h->ref_regular
3147 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3148 && !bfd_hide_sym_by_version (eif->info->version_info,
3149 h->root.root.string))
3150 {
3151 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
3152 {
3153 eif->failed = true;
3154 return false;
3155 }
3156 }
3157 }
3158
3159 /* If this symbol does not require a PLT entry, and it is not
3160 defined by a dynamic object, or is not referenced by a regular
3161 object, ignore it. We do have to handle a weak defined symbol,
3162 even if no regular object refers to it, if we decided to add it
3163 to the dynamic symbol table. FIXME: Do we normally need to worry
3164 about symbols which are defined by one dynamic object and
3165 referenced by another one? */
3166 if (!h->needs_plt
3167 && h->type != STT_GNU_IFUNC
3168 && (h->def_regular
3169 || !h->def_dynamic
3170 || (!h->ref_regular
3171 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
3172 {
3173 h->plt = elf_hash_table (eif->info)->init_plt_offset;
3174 return true;
3175 }
3176
3177 /* If we've already adjusted this symbol, don't do it again. This
3178 can happen via a recursive call. */
3179 if (h->dynamic_adjusted)
3180 return true;
3181
3182 /* Don't look at this symbol again. Note that we must set this
3183 after checking the above conditions, because we may look at a
3184 symbol once, decide not to do anything, and then get called
3185 recursively later after REF_REGULAR is set below. */
3186 h->dynamic_adjusted = 1;
3187
3188 /* If this is a weak definition, and we know a real definition, and
3189 the real symbol is not itself defined by a regular object file,
3190 then get a good value for the real definition. We handle the
3191 real symbol first, for the convenience of the backend routine.
3192
3193 Note that there is a confusing case here. If the real definition
3194 is defined by a regular object file, we don't get the real symbol
3195 from the dynamic object, but we do get the weak symbol. If the
3196 processor backend uses a COPY reloc, then if some routine in the
3197 dynamic object changes the real symbol, we will not see that
3198 change in the corresponding weak symbol. This is the way other
3199 ELF linkers work as well, and seems to be a result of the shared
3200 library model.
3201
3202 I will clarify this issue. Most SVR4 shared libraries define the
3203 variable _timezone and define timezone as a weak synonym. The
3204 tzset call changes _timezone. If you write
3205 extern int timezone;
3206 int _timezone = 5;
3207 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3208 you might expect that, since timezone is a synonym for _timezone,
3209 the same number will print both times. However, if the processor
3210 backend uses a COPY reloc, then actually timezone will be copied
3211 into your process image, and, since you define _timezone
3212 yourself, _timezone will not. Thus timezone and _timezone will
3213 wind up at different memory locations. The tzset call will set
3214 _timezone, leaving timezone unchanged. */
3215
3216 if (h->is_weakalias)
3217 {
3218 struct elf_link_hash_entry *def = weakdef (h);
3219
3220 /* If we get to this point, there is an implicit reference to
3221 the alias by a regular object file via the weak symbol H. */
3222 def->ref_regular = 1;
3223
3224 /* Ensure that the backend adjust_dynamic_symbol function sees
3225 the strong alias before H by recursively calling ourselves. */
3226 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
3227 return false;
3228 }
3229
3230 /* If a symbol has no type and no size and does not require a PLT
3231 entry, then we are probably about to do the wrong thing here: we
3232 are probably going to create a COPY reloc for an empty object.
3233 This case can arise when a shared object is built with assembly
3234 code, and the assembly code fails to set the symbol type. */
3235 if (h->size == 0
3236 && h->type == STT_NOTYPE
3237 && !h->needs_plt)
3238 _bfd_error_handler
3239 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3240 h->root.root.string);
3241
3242 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3243 {
3244 eif->failed = true;
3245 return false;
3246 }
3247
3248 return true;
3249 }
3250
3251 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3252 DYNBSS. */
3253
3254 bool
3255 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3256 struct elf_link_hash_entry *h,
3257 asection *dynbss)
3258 {
3259 unsigned int power_of_two;
3260 bfd_vma mask;
3261 asection *sec = h->root.u.def.section;
3262
3263 /* The section alignment of the definition is the maximum alignment
3264 requirement of symbols defined in the section. Since we don't
3265 know the symbol alignment requirement, we start with the
3266 maximum alignment and check low bits of the symbol address
3267 for the minimum alignment. */
3268 power_of_two = bfd_section_alignment (sec);
3269 mask = ((bfd_vma) 1 << power_of_two) - 1;
3270 while ((h->root.u.def.value & mask) != 0)
3271 {
3272 mask >>= 1;
3273 --power_of_two;
3274 }
3275
3276 if (power_of_two > bfd_section_alignment (dynbss))
3277 {
3278 /* Adjust the section alignment if needed. */
3279 if (!bfd_set_section_alignment (dynbss, power_of_two))
3280 return false;
3281 }
3282
3283 /* We make sure that the symbol will be aligned properly. */
3284 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3285
3286 /* Define the symbol as being at this point in DYNBSS. */
3287 h->root.u.def.section = dynbss;
3288 h->root.u.def.value = dynbss->size;
3289
3290 /* Increment the size of DYNBSS to make room for the symbol. */
3291 dynbss->size += h->size;
3292
3293 /* No error if extern_protected_data is true. */
3294 if (h->protected_def
3295 && (!info->extern_protected_data
3296 || (info->extern_protected_data < 0
3297 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
3298 info->callbacks->einfo
3299 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3300 h->root.root.string);
3301
3302 return true;
3303 }
3304
3305 /* Adjust all external symbols pointing into SEC_MERGE sections
3306 to reflect the object merging within the sections. */
3307
3308 static bool
3309 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
3310 {
3311 asection *sec;
3312
3313 if ((h->root.type == bfd_link_hash_defined
3314 || h->root.type == bfd_link_hash_defweak)
3315 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
3316 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3317 {
3318 bfd *output_bfd = (bfd *) data;
3319
3320 h->root.u.def.value =
3321 _bfd_merged_section_offset (output_bfd,
3322 &h->root.u.def.section,
3323 elf_section_data (sec)->sec_info,
3324 h->root.u.def.value);
3325 }
3326
3327 return true;
3328 }
3329
3330 /* Returns false if the symbol referred to by H should be considered
3331 to resolve local to the current module, and true if it should be
3332 considered to bind dynamically. */
3333
3334 bool
3335 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3336 struct bfd_link_info *info,
3337 bool not_local_protected)
3338 {
3339 bool binding_stays_local_p;
3340 const struct elf_backend_data *bed;
3341 struct elf_link_hash_table *hash_table;
3342
3343 if (h == NULL)
3344 return false;
3345
3346 while (h->root.type == bfd_link_hash_indirect
3347 || h->root.type == bfd_link_hash_warning)
3348 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3349
3350 /* If it was forced local, then clearly it's not dynamic. */
3351 if (h->dynindx == -1)
3352 return false;
3353 if (h->forced_local)
3354 return false;
3355
3356 /* Identify the cases where name binding rules say that a
3357 visible symbol resolves locally. */
3358 binding_stays_local_p = (bfd_link_executable (info)
3359 || SYMBOLIC_BIND (info, h));
3360
3361 switch (ELF_ST_VISIBILITY (h->other))
3362 {
3363 case STV_INTERNAL:
3364 case STV_HIDDEN:
3365 return false;
3366
3367 case STV_PROTECTED:
3368 hash_table = elf_hash_table (info);
3369 if (!is_elf_hash_table (&hash_table->root))
3370 return false;
3371
3372 bed = get_elf_backend_data (hash_table->dynobj);
3373
3374 /* Proper resolution for function pointer equality may require
3375 that these symbols perhaps be resolved dynamically, even though
3376 we should be resolving them to the current module. */
3377 if (!not_local_protected || !bed->is_function_type (h->type))
3378 binding_stays_local_p = true;
3379 break;
3380
3381 default:
3382 break;
3383 }
3384
3385 /* If it isn't defined locally, then clearly it's dynamic. */
3386 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
3387 return true;
3388
3389 /* Otherwise, the symbol is dynamic if binding rules don't tell
3390 us that it remains local. */
3391 return !binding_stays_local_p;
3392 }
3393
3394 /* Return true if the symbol referred to by H should be considered
3395 to resolve local to the current module, and false otherwise. Differs
3396 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3397 undefined symbols. The two functions are virtually identical except
3398 for the place where dynindx == -1 is tested. If that test is true,
3399 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3400 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3401 defined symbols.
3402 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3403 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3404 treatment of undefined weak symbols. For those that do not make
3405 undefined weak symbols dynamic, both functions may return false. */
3406
3407 bool
3408 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3409 struct bfd_link_info *info,
3410 bool local_protected)
3411 {
3412 const struct elf_backend_data *bed;
3413 struct elf_link_hash_table *hash_table;
3414
3415 /* If it's a local sym, of course we resolve locally. */
3416 if (h == NULL)
3417 return true;
3418
3419 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3420 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3421 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3422 return true;
3423
3424 /* Forced local symbols resolve locally. */
3425 if (h->forced_local)
3426 return true;
3427
3428 /* Common symbols that become definitions don't get the DEF_REGULAR
3429 flag set, so test it first, and don't bail out. */
3430 if (ELF_COMMON_DEF_P (h))
3431 /* Do nothing. */;
3432 /* If we don't have a definition in a regular file, then we can't
3433 resolve locally. The sym is either undefined or dynamic. */
3434 else if (!h->def_regular)
3435 return false;
3436
3437 /* Non-dynamic symbols resolve locally. */
3438 if (h->dynindx == -1)
3439 return true;
3440
3441 /* At this point, we know the symbol is defined and dynamic. In an
3442 executable it must resolve locally, likewise when building symbolic
3443 shared libraries. */
3444 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3445 return true;
3446
3447 /* Now deal with defined dynamic symbols in shared libraries. Ones
3448 with default visibility might not resolve locally. */
3449 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3450 return false;
3451
3452 hash_table = elf_hash_table (info);
3453 if (!is_elf_hash_table (&hash_table->root))
3454 return true;
3455
3456 /* STV_PROTECTED symbols with indirect external access are local. */
3457 if (info->indirect_extern_access > 0)
3458 return true;
3459
3460 bed = get_elf_backend_data (hash_table->dynobj);
3461
3462 /* If extern_protected_data is false, STV_PROTECTED non-function
3463 symbols are local. */
3464 if ((!info->extern_protected_data
3465 || (info->extern_protected_data < 0
3466 && !bed->extern_protected_data))
3467 && !bed->is_function_type (h->type))
3468 return true;
3469
3470 /* Function pointer equality tests may require that STV_PROTECTED
3471 symbols be treated as dynamic symbols. If the address of a
3472 function not defined in an executable is set to that function's
3473 plt entry in the executable, then the address of the function in
3474 a shared library must also be the plt entry in the executable. */
3475 return local_protected;
3476 }
3477
3478 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3479 aligned. Returns the first TLS output section. */
3480
3481 struct bfd_section *
3482 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3483 {
3484 struct bfd_section *sec, *tls;
3485 unsigned int align = 0;
3486
3487 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3488 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3489 break;
3490 tls = sec;
3491
3492 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3493 if (sec->alignment_power > align)
3494 align = sec->alignment_power;
3495
3496 elf_hash_table (info)->tls_sec = tls;
3497
3498 /* Ensure the alignment of the first section (usually .tdata) is the largest
3499 alignment, so that the tls segment starts aligned. */
3500 if (tls != NULL)
3501 tls->alignment_power = align;
3502
3503 return tls;
3504 }
3505
3506 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3507 static bool
3508 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3509 Elf_Internal_Sym *sym)
3510 {
3511 const struct elf_backend_data *bed;
3512
3513 /* Local symbols do not count, but target specific ones might. */
3514 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3515 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3516 return false;
3517
3518 bed = get_elf_backend_data (abfd);
3519 /* Function symbols do not count. */
3520 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3521 return false;
3522
3523 /* If the section is undefined, then so is the symbol. */
3524 if (sym->st_shndx == SHN_UNDEF)
3525 return false;
3526
3527 /* If the symbol is defined in the common section, then
3528 it is a common definition and so does not count. */
3529 if (bed->common_definition (sym))
3530 return false;
3531
3532 /* If the symbol is in a target specific section then we
3533 must rely upon the backend to tell us what it is. */
3534 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3535 /* FIXME - this function is not coded yet:
3536
3537 return _bfd_is_global_symbol_definition (abfd, sym);
3538
3539 Instead for now assume that the definition is not global,
3540 Even if this is wrong, at least the linker will behave
3541 in the same way that it used to do. */
3542 return false;
3543
3544 return true;
3545 }
3546
3547 /* Search the symbol table of the archive element of the archive ABFD
3548 whose archive map contains a mention of SYMDEF, and determine if
3549 the symbol is defined in this element. */
3550 static bool
3551 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3552 {
3553 Elf_Internal_Shdr * hdr;
3554 size_t symcount;
3555 size_t extsymcount;
3556 size_t extsymoff;
3557 Elf_Internal_Sym *isymbuf;
3558 Elf_Internal_Sym *isym;
3559 Elf_Internal_Sym *isymend;
3560 bool result;
3561
3562 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset, NULL);
3563 if (abfd == NULL)
3564 return false;
3565
3566 if (! bfd_check_format (abfd, bfd_object))
3567 return false;
3568
3569 /* Select the appropriate symbol table. If we don't know if the
3570 object file is an IR object, give linker LTO plugin a chance to
3571 get the correct symbol table. */
3572 if (abfd->plugin_format == bfd_plugin_yes
3573 #if BFD_SUPPORTS_PLUGINS
3574 || (abfd->plugin_format == bfd_plugin_unknown
3575 && bfd_link_plugin_object_p (abfd))
3576 #endif
3577 )
3578 {
3579 /* Use the IR symbol table if the object has been claimed by
3580 plugin. */
3581 abfd = abfd->plugin_dummy_bfd;
3582 hdr = &elf_tdata (abfd)->symtab_hdr;
3583 }
3584 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3585 hdr = &elf_tdata (abfd)->symtab_hdr;
3586 else
3587 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3588
3589 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3590
3591 /* The sh_info field of the symtab header tells us where the
3592 external symbols start. We don't care about the local symbols. */
3593 if (elf_bad_symtab (abfd))
3594 {
3595 extsymcount = symcount;
3596 extsymoff = 0;
3597 }
3598 else
3599 {
3600 extsymcount = symcount - hdr->sh_info;
3601 extsymoff = hdr->sh_info;
3602 }
3603
3604 if (extsymcount == 0)
3605 return false;
3606
3607 /* Read in the symbol table. */
3608 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3609 NULL, NULL, NULL);
3610 if (isymbuf == NULL)
3611 return false;
3612
3613 /* Scan the symbol table looking for SYMDEF. */
3614 result = false;
3615 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3616 {
3617 const char *name;
3618
3619 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3620 isym->st_name);
3621 if (name == NULL)
3622 break;
3623
3624 if (strcmp (name, symdef->name) == 0)
3625 {
3626 result = is_global_data_symbol_definition (abfd, isym);
3627 break;
3628 }
3629 }
3630
3631 free (isymbuf);
3632
3633 return result;
3634 }
3635 \f
3636 /* Add an entry to the .dynamic table. */
3637
3638 bool
3639 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3640 bfd_vma tag,
3641 bfd_vma val)
3642 {
3643 struct elf_link_hash_table *hash_table;
3644 const struct elf_backend_data *bed;
3645 asection *s;
3646 bfd_size_type newsize;
3647 bfd_byte *newcontents;
3648 Elf_Internal_Dyn dyn;
3649
3650 hash_table = elf_hash_table (info);
3651 if (! is_elf_hash_table (&hash_table->root))
3652 return false;
3653
3654 if (tag == DT_RELA || tag == DT_REL)
3655 hash_table->dynamic_relocs = true;
3656
3657 bed = get_elf_backend_data (hash_table->dynobj);
3658 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3659 BFD_ASSERT (s != NULL);
3660
3661 newsize = s->size + bed->s->sizeof_dyn;
3662 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3663 if (newcontents == NULL)
3664 return false;
3665
3666 dyn.d_tag = tag;
3667 dyn.d_un.d_val = val;
3668 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3669
3670 s->size = newsize;
3671 s->contents = newcontents;
3672
3673 return true;
3674 }
3675
3676 /* Strip zero-sized dynamic sections. */
3677
3678 bool
3679 _bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info *info)
3680 {
3681 struct elf_link_hash_table *hash_table;
3682 const struct elf_backend_data *bed;
3683 asection *s, *sdynamic, **pp;
3684 asection *rela_dyn, *rel_dyn;
3685 Elf_Internal_Dyn dyn;
3686 bfd_byte *extdyn, *next;
3687 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3688 bool strip_zero_sized;
3689 bool strip_zero_sized_plt;
3690
3691 if (bfd_link_relocatable (info))
3692 return true;
3693
3694 hash_table = elf_hash_table (info);
3695 if (!is_elf_hash_table (&hash_table->root))
3696 return false;
3697
3698 if (!hash_table->dynobj)
3699 return true;
3700
3701 sdynamic= bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3702 if (!sdynamic)
3703 return true;
3704
3705 bed = get_elf_backend_data (hash_table->dynobj);
3706 swap_dyn_in = bed->s->swap_dyn_in;
3707
3708 strip_zero_sized = false;
3709 strip_zero_sized_plt = false;
3710
3711 /* Strip zero-sized dynamic sections. */
3712 rela_dyn = bfd_get_section_by_name (info->output_bfd, ".rela.dyn");
3713 rel_dyn = bfd_get_section_by_name (info->output_bfd, ".rel.dyn");
3714 for (pp = &info->output_bfd->sections; (s = *pp) != NULL;)
3715 if (s->size == 0
3716 && (s == rela_dyn
3717 || s == rel_dyn
3718 || s == hash_table->srelplt->output_section
3719 || s == hash_table->splt->output_section))
3720 {
3721 *pp = s->next;
3722 info->output_bfd->section_count--;
3723 strip_zero_sized = true;
3724 if (s == rela_dyn)
3725 s = rela_dyn;
3726 if (s == rel_dyn)
3727 s = rel_dyn;
3728 else if (s == hash_table->splt->output_section)
3729 {
3730 s = hash_table->splt;
3731 strip_zero_sized_plt = true;
3732 }
3733 else
3734 s = hash_table->srelplt;
3735 s->flags |= SEC_EXCLUDE;
3736 s->output_section = bfd_abs_section_ptr;
3737 }
3738 else
3739 pp = &s->next;
3740
3741 if (strip_zero_sized_plt && sdynamic->size != 0)
3742 for (extdyn = sdynamic->contents;
3743 extdyn < sdynamic->contents + sdynamic->size;
3744 extdyn = next)
3745 {
3746 next = extdyn + bed->s->sizeof_dyn;
3747 swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3748 switch (dyn.d_tag)
3749 {
3750 default:
3751 break;
3752 case DT_JMPREL:
3753 case DT_PLTRELSZ:
3754 case DT_PLTREL:
3755 /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
3756 the procedure linkage table (the .plt section) has been
3757 removed. */
3758 memmove (extdyn, next,
3759 sdynamic->size - (next - sdynamic->contents));
3760 next = extdyn;
3761 }
3762 }
3763
3764 if (strip_zero_sized)
3765 {
3766 /* Regenerate program headers. */
3767 elf_seg_map (info->output_bfd) = NULL;
3768 return _bfd_elf_map_sections_to_segments (info->output_bfd, info,
3769 NULL);
3770 }
3771
3772 return true;
3773 }
3774
3775 /* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error,
3776 1 if a DT_NEEDED tag already exists, and 0 on success. */
3777
3778 int
3779 bfd_elf_add_dt_needed_tag (bfd *abfd, struct bfd_link_info *info)
3780 {
3781 struct elf_link_hash_table *hash_table;
3782 size_t strindex;
3783 const char *soname;
3784
3785 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3786 return -1;
3787
3788 hash_table = elf_hash_table (info);
3789 soname = elf_dt_name (abfd);
3790 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, false);
3791 if (strindex == (size_t) -1)
3792 return -1;
3793
3794 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3795 {
3796 asection *sdyn;
3797 const struct elf_backend_data *bed;
3798 bfd_byte *extdyn;
3799
3800 bed = get_elf_backend_data (hash_table->dynobj);
3801 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3802 if (sdyn != NULL && sdyn->size != 0)
3803 for (extdyn = sdyn->contents;
3804 extdyn < sdyn->contents + sdyn->size;
3805 extdyn += bed->s->sizeof_dyn)
3806 {
3807 Elf_Internal_Dyn dyn;
3808
3809 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3810 if (dyn.d_tag == DT_NEEDED
3811 && dyn.d_un.d_val == strindex)
3812 {
3813 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3814 return 1;
3815 }
3816 }
3817 }
3818
3819 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3820 return -1;
3821
3822 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3823 return -1;
3824
3825 return 0;
3826 }
3827
3828 /* Return true if SONAME is on the needed list between NEEDED and STOP
3829 (or the end of list if STOP is NULL), and needed by a library that
3830 will be loaded. */
3831
3832 static bool
3833 on_needed_list (const char *soname,
3834 struct bfd_link_needed_list *needed,
3835 struct bfd_link_needed_list *stop)
3836 {
3837 struct bfd_link_needed_list *look;
3838 for (look = needed; look != stop; look = look->next)
3839 if (strcmp (soname, look->name) == 0
3840 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3841 /* If needed by a library that itself is not directly
3842 needed, recursively check whether that library is
3843 indirectly needed. Since we add DT_NEEDED entries to
3844 the end of the list, library dependencies appear after
3845 the library. Therefore search prior to the current
3846 LOOK, preventing possible infinite recursion. */
3847 || on_needed_list (elf_dt_name (look->by), needed, look)))
3848 return true;
3849
3850 return false;
3851 }
3852
3853 /* Sort symbol by value, section, size, and type. */
3854 static int
3855 elf_sort_symbol (const void *arg1, const void *arg2)
3856 {
3857 const struct elf_link_hash_entry *h1;
3858 const struct elf_link_hash_entry *h2;
3859 bfd_signed_vma vdiff;
3860 int sdiff;
3861 const char *n1;
3862 const char *n2;
3863
3864 h1 = *(const struct elf_link_hash_entry **) arg1;
3865 h2 = *(const struct elf_link_hash_entry **) arg2;
3866 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3867 if (vdiff != 0)
3868 return vdiff > 0 ? 1 : -1;
3869
3870 sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3871 if (sdiff != 0)
3872 return sdiff;
3873
3874 /* Sort so that sized symbols are selected over zero size symbols. */
3875 vdiff = h1->size - h2->size;
3876 if (vdiff != 0)
3877 return vdiff > 0 ? 1 : -1;
3878
3879 /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */
3880 if (h1->type != h2->type)
3881 return h1->type - h2->type;
3882
3883 /* If symbols are properly sized and typed, and multiple strong
3884 aliases are not defined in a shared library by the user we
3885 shouldn't get here. Unfortunately linker script symbols like
3886 __bss_start sometimes match a user symbol defined at the start of
3887 .bss without proper size and type. We'd like to preference the
3888 user symbol over reserved system symbols. Sort on leading
3889 underscores. */
3890 n1 = h1->root.root.string;
3891 n2 = h2->root.root.string;
3892 while (*n1 == *n2)
3893 {
3894 if (*n1 == 0)
3895 break;
3896 ++n1;
3897 ++n2;
3898 }
3899 if (*n1 == '_')
3900 return -1;
3901 if (*n2 == '_')
3902 return 1;
3903
3904 /* Final sort on name selects user symbols like '_u' over reserved
3905 system symbols like '_Z' and also will avoid qsort instability. */
3906 return *n1 - *n2;
3907 }
3908
3909 /* This function is used to adjust offsets into .dynstr for
3910 dynamic symbols. This is called via elf_link_hash_traverse. */
3911
3912 static bool
3913 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3914 {
3915 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3916
3917 if (h->dynindx != -1)
3918 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3919 return true;
3920 }
3921
3922 /* Assign string offsets in .dynstr, update all structures referencing
3923 them. */
3924
3925 static bool
3926 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3927 {
3928 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3929 struct elf_link_local_dynamic_entry *entry;
3930 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3931 bfd *dynobj = hash_table->dynobj;
3932 asection *sdyn;
3933 bfd_size_type size;
3934 const struct elf_backend_data *bed;
3935 bfd_byte *extdyn;
3936
3937 _bfd_elf_strtab_finalize (dynstr);
3938 size = _bfd_elf_strtab_size (dynstr);
3939
3940 /* Allow the linker to examine the dynsymtab now it's fully populated. */
3941
3942 if (info->callbacks->examine_strtab)
3943 info->callbacks->examine_strtab (dynstr);
3944
3945 bed = get_elf_backend_data (dynobj);
3946 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3947 BFD_ASSERT (sdyn != NULL);
3948
3949 /* Update all .dynamic entries referencing .dynstr strings. */
3950 for (extdyn = sdyn->contents;
3951 extdyn < PTR_ADD (sdyn->contents, sdyn->size);
3952 extdyn += bed->s->sizeof_dyn)
3953 {
3954 Elf_Internal_Dyn dyn;
3955
3956 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3957 switch (dyn.d_tag)
3958 {
3959 case DT_STRSZ:
3960 dyn.d_un.d_val = size;
3961 break;
3962 case DT_NEEDED:
3963 case DT_SONAME:
3964 case DT_RPATH:
3965 case DT_RUNPATH:
3966 case DT_FILTER:
3967 case DT_AUXILIARY:
3968 case DT_AUDIT:
3969 case DT_DEPAUDIT:
3970 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3971 break;
3972 default:
3973 continue;
3974 }
3975 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3976 }
3977
3978 /* Now update local dynamic symbols. */
3979 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3980 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3981 entry->isym.st_name);
3982
3983 /* And the rest of dynamic symbols. */
3984 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3985
3986 /* Adjust version definitions. */
3987 if (elf_tdata (output_bfd)->cverdefs)
3988 {
3989 asection *s;
3990 bfd_byte *p;
3991 size_t i;
3992 Elf_Internal_Verdef def;
3993 Elf_Internal_Verdaux defaux;
3994
3995 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3996 p = s->contents;
3997 do
3998 {
3999 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
4000 &def);
4001 p += sizeof (Elf_External_Verdef);
4002 if (def.vd_aux != sizeof (Elf_External_Verdef))
4003 continue;
4004 for (i = 0; i < def.vd_cnt; ++i)
4005 {
4006 _bfd_elf_swap_verdaux_in (output_bfd,
4007 (Elf_External_Verdaux *) p, &defaux);
4008 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
4009 defaux.vda_name);
4010 _bfd_elf_swap_verdaux_out (output_bfd,
4011 &defaux, (Elf_External_Verdaux *) p);
4012 p += sizeof (Elf_External_Verdaux);
4013 }
4014 }
4015 while (def.vd_next);
4016 }
4017
4018 /* Adjust version references. */
4019 if (elf_tdata (output_bfd)->verref)
4020 {
4021 asection *s;
4022 bfd_byte *p;
4023 size_t i;
4024 Elf_Internal_Verneed need;
4025 Elf_Internal_Vernaux needaux;
4026
4027 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
4028 p = s->contents;
4029 do
4030 {
4031 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
4032 &need);
4033 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
4034 _bfd_elf_swap_verneed_out (output_bfd, &need,
4035 (Elf_External_Verneed *) p);
4036 p += sizeof (Elf_External_Verneed);
4037 for (i = 0; i < need.vn_cnt; ++i)
4038 {
4039 _bfd_elf_swap_vernaux_in (output_bfd,
4040 (Elf_External_Vernaux *) p, &needaux);
4041 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
4042 needaux.vna_name);
4043 _bfd_elf_swap_vernaux_out (output_bfd,
4044 &needaux,
4045 (Elf_External_Vernaux *) p);
4046 p += sizeof (Elf_External_Vernaux);
4047 }
4048 }
4049 while (need.vn_next);
4050 }
4051
4052 return true;
4053 }
4054 \f
4055 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4056 The default is to only match when the INPUT and OUTPUT are exactly
4057 the same target. */
4058
4059 bool
4060 _bfd_elf_default_relocs_compatible (const bfd_target *input,
4061 const bfd_target *output)
4062 {
4063 return input == output;
4064 }
4065
4066 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4067 This version is used when different targets for the same architecture
4068 are virtually identical. */
4069
4070 bool
4071 _bfd_elf_relocs_compatible (const bfd_target *input,
4072 const bfd_target *output)
4073 {
4074 const struct elf_backend_data *obed, *ibed;
4075
4076 if (input == output)
4077 return true;
4078
4079 ibed = xvec_get_elf_backend_data (input);
4080 obed = xvec_get_elf_backend_data (output);
4081
4082 if (ibed->arch != obed->arch)
4083 return false;
4084
4085 /* If both backends are using this function, deem them compatible. */
4086 return ibed->relocs_compatible == obed->relocs_compatible;
4087 }
4088
4089 /* Make a special call to the linker "notice" function to tell it that
4090 we are about to handle an as-needed lib, or have finished
4091 processing the lib. */
4092
4093 bool
4094 _bfd_elf_notice_as_needed (bfd *ibfd,
4095 struct bfd_link_info *info,
4096 enum notice_asneeded_action act)
4097 {
4098 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
4099 }
4100
4101 /* Call ACTION on each relocation in an ELF object file. */
4102
4103 bool
4104 _bfd_elf_link_iterate_on_relocs
4105 (bfd *abfd, struct bfd_link_info *info,
4106 bool (*action) (bfd *, struct bfd_link_info *, asection *,
4107 const Elf_Internal_Rela *))
4108 {
4109 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4110 struct elf_link_hash_table *htab = elf_hash_table (info);
4111
4112 /* If this object is the same format as the output object, and it is
4113 not a shared library, then let the backend look through the
4114 relocs.
4115
4116 This is required to build global offset table entries and to
4117 arrange for dynamic relocs. It is not required for the
4118 particular common case of linking non PIC code, even when linking
4119 against shared libraries, but unfortunately there is no way of
4120 knowing whether an object file has been compiled PIC or not.
4121 Looking through the relocs is not particularly time consuming.
4122 The problem is that we must either (1) keep the relocs in memory,
4123 which causes the linker to require additional runtime memory or
4124 (2) read the relocs twice from the input file, which wastes time.
4125 This would be a good case for using mmap.
4126
4127 I have no idea how to handle linking PIC code into a file of a
4128 different format. It probably can't be done. */
4129 if ((abfd->flags & DYNAMIC) == 0
4130 && is_elf_hash_table (&htab->root)
4131 && elf_object_id (abfd) == elf_hash_table_id (htab)
4132 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4133 {
4134 asection *o;
4135
4136 for (o = abfd->sections; o != NULL; o = o->next)
4137 {
4138 Elf_Internal_Rela *internal_relocs;
4139 bool ok;
4140
4141 /* Don't check relocations in excluded sections. Don't do
4142 anything special with non-loaded, non-alloced sections.
4143 In particular, any relocs in such sections should not
4144 affect GOT and PLT reference counting (ie. we don't
4145 allow them to create GOT or PLT entries), there's no
4146 possibility or desire to optimize TLS relocs, and
4147 there's not much point in propagating relocs to shared
4148 libs that the dynamic linker won't relocate. */
4149 if ((o->flags & SEC_ALLOC) == 0
4150 || (o->flags & SEC_RELOC) == 0
4151 || (o->flags & SEC_EXCLUDE) != 0
4152 || o->reloc_count == 0
4153 || ((info->strip == strip_all || info->strip == strip_debugger)
4154 && (o->flags & SEC_DEBUGGING) != 0)
4155 || bfd_is_abs_section (o->output_section))
4156 continue;
4157
4158 internal_relocs = _bfd_elf_link_info_read_relocs (abfd, info,
4159 o, NULL,
4160 NULL,
4161 _bfd_link_keep_memory (info));
4162 if (internal_relocs == NULL)
4163 return false;
4164
4165 ok = action (abfd, info, o, internal_relocs);
4166
4167 if (elf_section_data (o)->relocs != internal_relocs)
4168 free (internal_relocs);
4169
4170 if (! ok)
4171 return false;
4172 }
4173 }
4174
4175 return true;
4176 }
4177
4178 /* Check relocations in an ELF object file. This is called after
4179 all input files have been opened. */
4180
4181 bool
4182 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
4183 {
4184 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4185 if (bed->check_relocs != NULL)
4186 return _bfd_elf_link_iterate_on_relocs (abfd, info,
4187 bed->check_relocs);
4188 return true;
4189 }
4190
4191 /* Add symbols from an ELF object file to the linker hash table. */
4192
4193 static bool
4194 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
4195 {
4196 Elf_Internal_Ehdr *ehdr;
4197 Elf_Internal_Shdr *hdr;
4198 size_t symcount;
4199 size_t extsymcount;
4200 size_t extsymoff;
4201 struct elf_link_hash_entry **sym_hash;
4202 bool dynamic;
4203 Elf_External_Versym *extversym = NULL;
4204 Elf_External_Versym *extversym_end = NULL;
4205 Elf_External_Versym *ever;
4206 struct elf_link_hash_entry *weaks;
4207 struct elf_link_hash_entry **nondeflt_vers = NULL;
4208 size_t nondeflt_vers_cnt = 0;
4209 Elf_Internal_Sym *isymbuf = NULL;
4210 Elf_Internal_Sym *isym;
4211 Elf_Internal_Sym *isymend;
4212 const struct elf_backend_data *bed;
4213 bool add_needed;
4214 struct elf_link_hash_table *htab;
4215 void *alloc_mark = NULL;
4216 struct bfd_hash_entry **old_table = NULL;
4217 unsigned int old_size = 0;
4218 unsigned int old_count = 0;
4219 void *old_tab = NULL;
4220 void *old_ent;
4221 struct bfd_link_hash_entry *old_undefs = NULL;
4222 struct bfd_link_hash_entry *old_undefs_tail = NULL;
4223 void *old_strtab = NULL;
4224 size_t tabsize = 0;
4225 asection *s;
4226 bool just_syms;
4227
4228 htab = elf_hash_table (info);
4229 bed = get_elf_backend_data (abfd);
4230
4231 if ((abfd->flags & DYNAMIC) == 0)
4232 dynamic = false;
4233 else
4234 {
4235 dynamic = true;
4236
4237 /* You can't use -r against a dynamic object. Also, there's no
4238 hope of using a dynamic object which does not exactly match
4239 the format of the output file. */
4240 if (bfd_link_relocatable (info)
4241 || !is_elf_hash_table (&htab->root)
4242 || info->output_bfd->xvec != abfd->xvec)
4243 {
4244 if (bfd_link_relocatable (info))
4245 bfd_set_error (bfd_error_invalid_operation);
4246 else
4247 bfd_set_error (bfd_error_wrong_format);
4248 goto error_return;
4249 }
4250 }
4251
4252 ehdr = elf_elfheader (abfd);
4253 if (info->warn_alternate_em
4254 && bed->elf_machine_code != ehdr->e_machine
4255 && ((bed->elf_machine_alt1 != 0
4256 && ehdr->e_machine == bed->elf_machine_alt1)
4257 || (bed->elf_machine_alt2 != 0
4258 && ehdr->e_machine == bed->elf_machine_alt2)))
4259 _bfd_error_handler
4260 /* xgettext:c-format */
4261 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
4262 ehdr->e_machine, abfd, bed->elf_machine_code);
4263
4264 /* As a GNU extension, any input sections which are named
4265 .gnu.warning.SYMBOL are treated as warning symbols for the given
4266 symbol. This differs from .gnu.warning sections, which generate
4267 warnings when they are included in an output file. */
4268 /* PR 12761: Also generate this warning when building shared libraries. */
4269 for (s = abfd->sections; s != NULL; s = s->next)
4270 {
4271 const char *name;
4272
4273 name = bfd_section_name (s);
4274 if (startswith (name, ".gnu.warning."))
4275 {
4276 char *msg;
4277 bfd_size_type sz;
4278
4279 name += sizeof ".gnu.warning." - 1;
4280
4281 /* If this is a shared object, then look up the symbol
4282 in the hash table. If it is there, and it is already
4283 been defined, then we will not be using the entry
4284 from this shared object, so we don't need to warn.
4285 FIXME: If we see the definition in a regular object
4286 later on, we will warn, but we shouldn't. The only
4287 fix is to keep track of what warnings we are supposed
4288 to emit, and then handle them all at the end of the
4289 link. */
4290 if (dynamic)
4291 {
4292 struct elf_link_hash_entry *h;
4293
4294 h = elf_link_hash_lookup (htab, name, false, false, true);
4295
4296 /* FIXME: What about bfd_link_hash_common? */
4297 if (h != NULL
4298 && (h->root.type == bfd_link_hash_defined
4299 || h->root.type == bfd_link_hash_defweak))
4300 continue;
4301 }
4302
4303 sz = s->size;
4304 msg = (char *) bfd_alloc (abfd, sz + 1);
4305 if (msg == NULL)
4306 goto error_return;
4307
4308 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
4309 goto error_return;
4310
4311 msg[sz] = '\0';
4312
4313 if (! (_bfd_generic_link_add_one_symbol
4314 (info, abfd, name, BSF_WARNING, s, 0, msg,
4315 false, bed->collect, NULL)))
4316 goto error_return;
4317
4318 if (bfd_link_executable (info))
4319 {
4320 /* Clobber the section size so that the warning does
4321 not get copied into the output file. */
4322 s->size = 0;
4323
4324 /* Also set SEC_EXCLUDE, so that symbols defined in
4325 the warning section don't get copied to the output. */
4326 s->flags |= SEC_EXCLUDE;
4327 }
4328 }
4329 }
4330
4331 just_syms = ((s = abfd->sections) != NULL
4332 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
4333
4334 add_needed = true;
4335 if (! dynamic)
4336 {
4337 /* If we are creating a shared library, create all the dynamic
4338 sections immediately. We need to attach them to something,
4339 so we attach them to this BFD, provided it is the right
4340 format and is not from ld --just-symbols. Always create the
4341 dynamic sections for -E/--dynamic-list. FIXME: If there
4342 are no input BFD's of the same format as the output, we can't
4343 make a shared library. */
4344 if (!just_syms
4345 && (bfd_link_pic (info)
4346 || (!bfd_link_relocatable (info)
4347 && info->nointerp
4348 && (info->export_dynamic || info->dynamic)))
4349 && is_elf_hash_table (&htab->root)
4350 && info->output_bfd->xvec == abfd->xvec
4351 && !htab->dynamic_sections_created)
4352 {
4353 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4354 goto error_return;
4355 }
4356 }
4357 else if (!is_elf_hash_table (&htab->root))
4358 goto error_return;
4359 else
4360 {
4361 const char *soname = NULL;
4362 char *audit = NULL;
4363 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
4364 const Elf_Internal_Phdr *phdr;
4365 struct elf_link_loaded_list *loaded_lib;
4366
4367 /* ld --just-symbols and dynamic objects don't mix very well.
4368 ld shouldn't allow it. */
4369 if (just_syms)
4370 abort ();
4371
4372 /* If this dynamic lib was specified on the command line with
4373 --as-needed in effect, then we don't want to add a DT_NEEDED
4374 tag unless the lib is actually used. Similary for libs brought
4375 in by another lib's DT_NEEDED. When --no-add-needed is used
4376 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4377 any dynamic library in DT_NEEDED tags in the dynamic lib at
4378 all. */
4379 add_needed = (elf_dyn_lib_class (abfd)
4380 & (DYN_AS_NEEDED | DYN_DT_NEEDED
4381 | DYN_NO_NEEDED)) == 0;
4382
4383 s = bfd_get_section_by_name (abfd, ".dynamic");
4384 if (s != NULL && s->size != 0)
4385 {
4386 bfd_byte *dynbuf;
4387 bfd_byte *extdyn;
4388 unsigned int elfsec;
4389 unsigned long shlink;
4390
4391 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
4392 {
4393 error_free_dyn:
4394 free (dynbuf);
4395 goto error_return;
4396 }
4397
4398 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
4399 if (elfsec == SHN_BAD)
4400 goto error_free_dyn;
4401 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4402
4403 for (extdyn = dynbuf;
4404 extdyn <= dynbuf + s->size - bed->s->sizeof_dyn;
4405 extdyn += bed->s->sizeof_dyn)
4406 {
4407 Elf_Internal_Dyn dyn;
4408
4409 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4410 if (dyn.d_tag == DT_SONAME)
4411 {
4412 unsigned int tagv = dyn.d_un.d_val;
4413 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4414 if (soname == NULL)
4415 goto error_free_dyn;
4416 }
4417 if (dyn.d_tag == DT_NEEDED)
4418 {
4419 struct bfd_link_needed_list *n, **pn;
4420 char *fnm, *anm;
4421 unsigned int tagv = dyn.d_un.d_val;
4422 size_t amt = sizeof (struct bfd_link_needed_list);
4423
4424 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4425 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4426 if (n == NULL || fnm == NULL)
4427 goto error_free_dyn;
4428 amt = strlen (fnm) + 1;
4429 anm = (char *) bfd_alloc (abfd, amt);
4430 if (anm == NULL)
4431 goto error_free_dyn;
4432 memcpy (anm, fnm, amt);
4433 n->name = anm;
4434 n->by = abfd;
4435 n->next = NULL;
4436 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4437 ;
4438 *pn = n;
4439 }
4440 if (dyn.d_tag == DT_RUNPATH)
4441 {
4442 struct bfd_link_needed_list *n, **pn;
4443 char *fnm, *anm;
4444 unsigned int tagv = dyn.d_un.d_val;
4445 size_t amt = sizeof (struct bfd_link_needed_list);
4446
4447 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4448 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4449 if (n == NULL || fnm == NULL)
4450 goto error_free_dyn;
4451 amt = strlen (fnm) + 1;
4452 anm = (char *) bfd_alloc (abfd, amt);
4453 if (anm == NULL)
4454 goto error_free_dyn;
4455 memcpy (anm, fnm, amt);
4456 n->name = anm;
4457 n->by = abfd;
4458 n->next = NULL;
4459 for (pn = & runpath;
4460 *pn != NULL;
4461 pn = &(*pn)->next)
4462 ;
4463 *pn = n;
4464 }
4465 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4466 if (!runpath && dyn.d_tag == DT_RPATH)
4467 {
4468 struct bfd_link_needed_list *n, **pn;
4469 char *fnm, *anm;
4470 unsigned int tagv = dyn.d_un.d_val;
4471 size_t amt = sizeof (struct bfd_link_needed_list);
4472
4473 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4474 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4475 if (n == NULL || fnm == NULL)
4476 goto error_free_dyn;
4477 amt = strlen (fnm) + 1;
4478 anm = (char *) bfd_alloc (abfd, amt);
4479 if (anm == NULL)
4480 goto error_free_dyn;
4481 memcpy (anm, fnm, amt);
4482 n->name = anm;
4483 n->by = abfd;
4484 n->next = NULL;
4485 for (pn = & rpath;
4486 *pn != NULL;
4487 pn = &(*pn)->next)
4488 ;
4489 *pn = n;
4490 }
4491 if (dyn.d_tag == DT_AUDIT)
4492 {
4493 unsigned int tagv = dyn.d_un.d_val;
4494 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4495 }
4496 if (dyn.d_tag == DT_FLAGS_1)
4497 elf_tdata (abfd)->is_pie = (dyn.d_un.d_val & DF_1_PIE) != 0;
4498 }
4499
4500 free (dynbuf);
4501 }
4502
4503 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4504 frees all more recently bfd_alloc'd blocks as well. */
4505 if (runpath)
4506 rpath = runpath;
4507
4508 if (rpath)
4509 {
4510 struct bfd_link_needed_list **pn;
4511 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4512 ;
4513 *pn = rpath;
4514 }
4515
4516 /* If we have a PT_GNU_RELRO program header, mark as read-only
4517 all sections contained fully therein. This makes relro
4518 shared library sections appear as they will at run-time. */
4519 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4520 while (phdr-- > elf_tdata (abfd)->phdr)
4521 if (phdr->p_type == PT_GNU_RELRO)
4522 {
4523 for (s = abfd->sections; s != NULL; s = s->next)
4524 {
4525 unsigned int opb = bfd_octets_per_byte (abfd, s);
4526
4527 if ((s->flags & SEC_ALLOC) != 0
4528 && s->vma * opb >= phdr->p_vaddr
4529 && s->vma * opb + s->size <= phdr->p_vaddr + phdr->p_memsz)
4530 s->flags |= SEC_READONLY;
4531 }
4532 break;
4533 }
4534
4535 /* We do not want to include any of the sections in a dynamic
4536 object in the output file. We hack by simply clobbering the
4537 list of sections in the BFD. This could be handled more
4538 cleanly by, say, a new section flag; the existing
4539 SEC_NEVER_LOAD flag is not the one we want, because that one
4540 still implies that the section takes up space in the output
4541 file. */
4542 bfd_section_list_clear (abfd);
4543
4544 /* Find the name to use in a DT_NEEDED entry that refers to this
4545 object. If the object has a DT_SONAME entry, we use it.
4546 Otherwise, if the generic linker stuck something in
4547 elf_dt_name, we use that. Otherwise, we just use the file
4548 name. */
4549 if (soname == NULL || *soname == '\0')
4550 {
4551 soname = elf_dt_name (abfd);
4552 if (soname == NULL || *soname == '\0')
4553 soname = bfd_get_filename (abfd);
4554 }
4555
4556 /* Save the SONAME because sometimes the linker emulation code
4557 will need to know it. */
4558 elf_dt_name (abfd) = soname;
4559
4560 /* If we have already included this dynamic object in the
4561 link, just ignore it. There is no reason to include a
4562 particular dynamic object more than once. */
4563 for (loaded_lib = htab->dyn_loaded;
4564 loaded_lib != NULL;
4565 loaded_lib = loaded_lib->next)
4566 {
4567 if (strcmp (elf_dt_name (loaded_lib->abfd), soname) == 0)
4568 return true;
4569 }
4570
4571 /* Create dynamic sections for backends that require that be done
4572 before setup_gnu_properties. */
4573 if (add_needed
4574 && !_bfd_elf_link_create_dynamic_sections (abfd, info))
4575 return false;
4576
4577 /* Save the DT_AUDIT entry for the linker emulation code. */
4578 elf_dt_audit (abfd) = audit;
4579 }
4580
4581 /* If this is a dynamic object, we always link against the .dynsym
4582 symbol table, not the .symtab symbol table. The dynamic linker
4583 will only see the .dynsym symbol table, so there is no reason to
4584 look at .symtab for a dynamic object. */
4585
4586 if (! dynamic || elf_dynsymtab (abfd) == 0)
4587 hdr = &elf_tdata (abfd)->symtab_hdr;
4588 else
4589 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4590
4591 symcount = hdr->sh_size / bed->s->sizeof_sym;
4592
4593 /* The sh_info field of the symtab header tells us where the
4594 external symbols start. We don't care about the local symbols at
4595 this point. */
4596 if (elf_bad_symtab (abfd))
4597 {
4598 extsymcount = symcount;
4599 extsymoff = 0;
4600 }
4601 else
4602 {
4603 extsymcount = symcount - hdr->sh_info;
4604 extsymoff = hdr->sh_info;
4605 }
4606
4607 sym_hash = elf_sym_hashes (abfd);
4608 if (extsymcount != 0)
4609 {
4610 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4611 NULL, NULL, NULL);
4612 if (isymbuf == NULL)
4613 goto error_return;
4614
4615 if (sym_hash == NULL)
4616 {
4617 /* We store a pointer to the hash table entry for each
4618 external symbol. */
4619 size_t amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4620 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4621 if (sym_hash == NULL)
4622 goto error_free_sym;
4623 elf_sym_hashes (abfd) = sym_hash;
4624 }
4625 }
4626
4627 if (dynamic)
4628 {
4629 /* Read in any version definitions. */
4630 if (!_bfd_elf_slurp_version_tables (abfd,
4631 info->default_imported_symver))
4632 goto error_free_sym;
4633
4634 /* Read in the symbol versions, but don't bother to convert them
4635 to internal format. */
4636 if (elf_dynversym (abfd) != 0)
4637 {
4638 Elf_Internal_Shdr *versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4639 bfd_size_type amt = versymhdr->sh_size;
4640
4641 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0)
4642 goto error_free_sym;
4643 extversym = (Elf_External_Versym *)
4644 _bfd_malloc_and_read (abfd, amt, amt);
4645 if (extversym == NULL)
4646 goto error_free_sym;
4647 extversym_end = extversym + amt / sizeof (*extversym);
4648 }
4649 }
4650
4651 /* If we are loading an as-needed shared lib, save the symbol table
4652 state before we start adding symbols. If the lib turns out
4653 to be unneeded, restore the state. */
4654 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4655 {
4656 unsigned int i;
4657 size_t entsize;
4658
4659 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4660 {
4661 struct bfd_hash_entry *p;
4662 struct elf_link_hash_entry *h;
4663
4664 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4665 {
4666 h = (struct elf_link_hash_entry *) p;
4667 entsize += htab->root.table.entsize;
4668 if (h->root.type == bfd_link_hash_warning)
4669 {
4670 entsize += htab->root.table.entsize;
4671 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4672 }
4673 if (h->root.type == bfd_link_hash_common)
4674 entsize += sizeof (*h->root.u.c.p);
4675 }
4676 }
4677
4678 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4679 old_tab = bfd_malloc (tabsize + entsize);
4680 if (old_tab == NULL)
4681 goto error_free_vers;
4682
4683 /* Remember the current objalloc pointer, so that all mem for
4684 symbols added can later be reclaimed. */
4685 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4686 if (alloc_mark == NULL)
4687 goto error_free_vers;
4688
4689 /* Make a special call to the linker "notice" function to
4690 tell it that we are about to handle an as-needed lib. */
4691 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4692 goto error_free_vers;
4693
4694 /* Clone the symbol table. Remember some pointers into the
4695 symbol table, and dynamic symbol count. */
4696 old_ent = (char *) old_tab + tabsize;
4697 memcpy (old_tab, htab->root.table.table, tabsize);
4698 old_undefs = htab->root.undefs;
4699 old_undefs_tail = htab->root.undefs_tail;
4700 old_table = htab->root.table.table;
4701 old_size = htab->root.table.size;
4702 old_count = htab->root.table.count;
4703 old_strtab = NULL;
4704 if (htab->dynstr != NULL)
4705 {
4706 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4707 if (old_strtab == NULL)
4708 goto error_free_vers;
4709 }
4710
4711 for (i = 0; i < htab->root.table.size; i++)
4712 {
4713 struct bfd_hash_entry *p;
4714 struct elf_link_hash_entry *h;
4715
4716 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4717 {
4718 h = (struct elf_link_hash_entry *) p;
4719 memcpy (old_ent, h, htab->root.table.entsize);
4720 old_ent = (char *) old_ent + htab->root.table.entsize;
4721 if (h->root.type == bfd_link_hash_warning)
4722 {
4723 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4724 memcpy (old_ent, h, htab->root.table.entsize);
4725 old_ent = (char *) old_ent + htab->root.table.entsize;
4726 }
4727 if (h->root.type == bfd_link_hash_common)
4728 {
4729 memcpy (old_ent, h->root.u.c.p, sizeof (*h->root.u.c.p));
4730 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
4731 }
4732 }
4733 }
4734 }
4735
4736 weaks = NULL;
4737 if (extversym == NULL)
4738 ever = NULL;
4739 else if (extversym + extsymoff < extversym_end)
4740 ever = extversym + extsymoff;
4741 else
4742 {
4743 /* xgettext:c-format */
4744 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4745 abfd, (long) extsymoff,
4746 (long) (extversym_end - extversym) / sizeof (* extversym));
4747 bfd_set_error (bfd_error_bad_value);
4748 goto error_free_vers;
4749 }
4750
4751 if (!bfd_link_relocatable (info)
4752 && abfd->lto_slim_object)
4753 {
4754 _bfd_error_handler
4755 (_("%pB: plugin needed to handle lto object"), abfd);
4756 }
4757
4758 for (isym = isymbuf, isymend = PTR_ADD (isymbuf, extsymcount);
4759 isym < isymend;
4760 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4761 {
4762 int bind;
4763 bfd_vma value;
4764 asection *sec, *new_sec;
4765 flagword flags;
4766 const char *name;
4767 struct elf_link_hash_entry *h;
4768 struct elf_link_hash_entry *hi;
4769 bool definition;
4770 bool size_change_ok;
4771 bool type_change_ok;
4772 bool new_weak;
4773 bool old_weak;
4774 bfd *override;
4775 bool common;
4776 bool discarded;
4777 unsigned int old_alignment;
4778 unsigned int shindex;
4779 bfd *old_bfd;
4780 bool matched;
4781
4782 override = NULL;
4783
4784 flags = BSF_NO_FLAGS;
4785 sec = NULL;
4786 value = isym->st_value;
4787 common = bed->common_definition (isym);
4788 if (common && info->inhibit_common_definition)
4789 {
4790 /* Treat common symbol as undefined for --no-define-common. */
4791 isym->st_shndx = SHN_UNDEF;
4792 common = false;
4793 }
4794 discarded = false;
4795
4796 bind = ELF_ST_BIND (isym->st_info);
4797 switch (bind)
4798 {
4799 case STB_LOCAL:
4800 /* This should be impossible, since ELF requires that all
4801 global symbols follow all local symbols, and that sh_info
4802 point to the first global symbol. Unfortunately, Irix 5
4803 screws this up. */
4804 if (elf_bad_symtab (abfd))
4805 continue;
4806
4807 /* If we aren't prepared to handle locals within the globals
4808 then we'll likely segfault on a NULL symbol hash if the
4809 symbol is ever referenced in relocations. */
4810 shindex = elf_elfheader (abfd)->e_shstrndx;
4811 name = bfd_elf_string_from_elf_section (abfd, shindex, hdr->sh_name);
4812 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4813 " (>= sh_info of %lu)"),
4814 abfd, name, (long) (isym - isymbuf + extsymoff),
4815 (long) extsymoff);
4816
4817 /* Dynamic object relocations are not processed by ld, so
4818 ld won't run into the problem mentioned above. */
4819 if (dynamic)
4820 continue;
4821 bfd_set_error (bfd_error_bad_value);
4822 goto error_free_vers;
4823
4824 case STB_GLOBAL:
4825 if (isym->st_shndx != SHN_UNDEF && !common)
4826 flags = BSF_GLOBAL;
4827 break;
4828
4829 case STB_WEAK:
4830 flags = BSF_WEAK;
4831 break;
4832
4833 case STB_GNU_UNIQUE:
4834 flags = BSF_GNU_UNIQUE;
4835 break;
4836
4837 default:
4838 /* Leave it up to the processor backend. */
4839 break;
4840 }
4841
4842 if (isym->st_shndx == SHN_UNDEF)
4843 sec = bfd_und_section_ptr;
4844 else if (isym->st_shndx == SHN_ABS)
4845 sec = bfd_abs_section_ptr;
4846 else if (isym->st_shndx == SHN_COMMON)
4847 {
4848 sec = bfd_com_section_ptr;
4849 /* What ELF calls the size we call the value. What ELF
4850 calls the value we call the alignment. */
4851 value = isym->st_size;
4852 }
4853 else
4854 {
4855 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4856 if (sec == NULL)
4857 sec = bfd_abs_section_ptr;
4858 else if (discarded_section (sec))
4859 {
4860 /* Symbols from discarded section are undefined. We keep
4861 its visibility. */
4862 sec = bfd_und_section_ptr;
4863 discarded = true;
4864 isym->st_shndx = SHN_UNDEF;
4865 }
4866 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4867 value -= sec->vma;
4868 }
4869
4870 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4871 isym->st_name);
4872 if (name == NULL)
4873 goto error_free_vers;
4874
4875 if (isym->st_shndx == SHN_COMMON
4876 && (abfd->flags & BFD_PLUGIN) != 0)
4877 {
4878 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4879
4880 if (xc == NULL)
4881 {
4882 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4883 | SEC_EXCLUDE);
4884 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4885 if (xc == NULL)
4886 goto error_free_vers;
4887 }
4888 sec = xc;
4889 }
4890 else if (isym->st_shndx == SHN_COMMON
4891 && ELF_ST_TYPE (isym->st_info) == STT_TLS
4892 && !bfd_link_relocatable (info))
4893 {
4894 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4895
4896 if (tcomm == NULL)
4897 {
4898 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4899 | SEC_LINKER_CREATED);
4900 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4901 if (tcomm == NULL)
4902 goto error_free_vers;
4903 }
4904 sec = tcomm;
4905 }
4906 else if (bed->elf_add_symbol_hook)
4907 {
4908 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4909 &sec, &value))
4910 goto error_free_vers;
4911
4912 /* The hook function sets the name to NULL if this symbol
4913 should be skipped for some reason. */
4914 if (name == NULL)
4915 continue;
4916 }
4917
4918 /* Sanity check that all possibilities were handled. */
4919 if (sec == NULL)
4920 abort ();
4921
4922 /* Silently discard TLS symbols from --just-syms. There's
4923 no way to combine a static TLS block with a new TLS block
4924 for this executable. */
4925 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4926 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4927 continue;
4928
4929 if (bfd_is_und_section (sec)
4930 || bfd_is_com_section (sec))
4931 definition = false;
4932 else
4933 definition = true;
4934
4935 size_change_ok = false;
4936 type_change_ok = bed->type_change_ok;
4937 old_weak = false;
4938 matched = false;
4939 old_alignment = 0;
4940 old_bfd = NULL;
4941 new_sec = sec;
4942
4943 if (is_elf_hash_table (&htab->root))
4944 {
4945 Elf_Internal_Versym iver;
4946 unsigned int vernum = 0;
4947 bool skip;
4948
4949 if (ever == NULL)
4950 {
4951 if (info->default_imported_symver)
4952 /* Use the default symbol version created earlier. */
4953 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4954 else
4955 iver.vs_vers = 0;
4956 }
4957 else if (ever >= extversym_end)
4958 {
4959 /* xgettext:c-format */
4960 _bfd_error_handler (_("%pB: not enough version information"),
4961 abfd);
4962 bfd_set_error (bfd_error_bad_value);
4963 goto error_free_vers;
4964 }
4965 else
4966 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4967
4968 vernum = iver.vs_vers & VERSYM_VERSION;
4969
4970 /* If this is a hidden symbol, or if it is not version
4971 1, we append the version name to the symbol name.
4972 However, we do not modify a non-hidden absolute symbol
4973 if it is not a function, because it might be the version
4974 symbol itself. FIXME: What if it isn't? */
4975 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4976 || (vernum > 1
4977 && (!bfd_is_abs_section (sec)
4978 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4979 {
4980 const char *verstr;
4981 size_t namelen, verlen, newlen;
4982 char *newname, *p;
4983
4984 if (isym->st_shndx != SHN_UNDEF)
4985 {
4986 if (vernum > elf_tdata (abfd)->cverdefs)
4987 verstr = NULL;
4988 else if (vernum > 1)
4989 verstr =
4990 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4991 else
4992 verstr = "";
4993
4994 if (verstr == NULL)
4995 {
4996 _bfd_error_handler
4997 /* xgettext:c-format */
4998 (_("%pB: %s: invalid version %u (max %d)"),
4999 abfd, name, vernum,
5000 elf_tdata (abfd)->cverdefs);
5001 bfd_set_error (bfd_error_bad_value);
5002 goto error_free_vers;
5003 }
5004 }
5005 else
5006 {
5007 /* We cannot simply test for the number of
5008 entries in the VERNEED section since the
5009 numbers for the needed versions do not start
5010 at 0. */
5011 Elf_Internal_Verneed *t;
5012
5013 verstr = NULL;
5014 for (t = elf_tdata (abfd)->verref;
5015 t != NULL;
5016 t = t->vn_nextref)
5017 {
5018 Elf_Internal_Vernaux *a;
5019
5020 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5021 {
5022 if (a->vna_other == vernum)
5023 {
5024 verstr = a->vna_nodename;
5025 break;
5026 }
5027 }
5028 if (a != NULL)
5029 break;
5030 }
5031 if (verstr == NULL)
5032 {
5033 _bfd_error_handler
5034 /* xgettext:c-format */
5035 (_("%pB: %s: invalid needed version %d"),
5036 abfd, name, vernum);
5037 bfd_set_error (bfd_error_bad_value);
5038 goto error_free_vers;
5039 }
5040 }
5041
5042 namelen = strlen (name);
5043 verlen = strlen (verstr);
5044 newlen = namelen + verlen + 2;
5045 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
5046 && isym->st_shndx != SHN_UNDEF)
5047 ++newlen;
5048
5049 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
5050 if (newname == NULL)
5051 goto error_free_vers;
5052 memcpy (newname, name, namelen);
5053 p = newname + namelen;
5054 *p++ = ELF_VER_CHR;
5055 /* If this is a defined non-hidden version symbol,
5056 we add another @ to the name. This indicates the
5057 default version of the symbol. */
5058 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
5059 && isym->st_shndx != SHN_UNDEF)
5060 *p++ = ELF_VER_CHR;
5061 memcpy (p, verstr, verlen + 1);
5062
5063 name = newname;
5064 }
5065
5066 /* If this symbol has default visibility and the user has
5067 requested we not re-export it, then mark it as hidden. */
5068 if (!bfd_is_und_section (sec)
5069 && !dynamic
5070 && abfd->no_export
5071 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
5072 isym->st_other = (STV_HIDDEN
5073 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
5074
5075 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
5076 sym_hash, &old_bfd, &old_weak,
5077 &old_alignment, &skip, &override,
5078 &type_change_ok, &size_change_ok,
5079 &matched))
5080 goto error_free_vers;
5081
5082 if (skip)
5083 continue;
5084
5085 /* Override a definition only if the new symbol matches the
5086 existing one. */
5087 if (override && matched)
5088 definition = false;
5089
5090 h = *sym_hash;
5091 while (h->root.type == bfd_link_hash_indirect
5092 || h->root.type == bfd_link_hash_warning)
5093 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5094
5095 if (h->versioned != unversioned
5096 && elf_tdata (abfd)->verdef != NULL
5097 && vernum > 1
5098 && definition)
5099 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
5100 }
5101
5102 if (! (_bfd_generic_link_add_one_symbol
5103 (info, override ? override : abfd, name, flags, sec, value,
5104 NULL, false, bed->collect,
5105 (struct bfd_link_hash_entry **) sym_hash)))
5106 goto error_free_vers;
5107
5108 h = *sym_hash;
5109 /* We need to make sure that indirect symbol dynamic flags are
5110 updated. */
5111 hi = h;
5112 while (h->root.type == bfd_link_hash_indirect
5113 || h->root.type == bfd_link_hash_warning)
5114 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5115
5116 *sym_hash = h;
5117
5118 /* Setting the index to -3 tells elf_link_output_extsym that
5119 this symbol is defined in a discarded section. */
5120 if (discarded && is_elf_hash_table (&htab->root))
5121 h->indx = -3;
5122
5123 new_weak = (flags & BSF_WEAK) != 0;
5124 if (dynamic
5125 && definition
5126 && new_weak
5127 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
5128 && is_elf_hash_table (&htab->root)
5129 && h->u.alias == NULL)
5130 {
5131 /* Keep a list of all weak defined non function symbols from
5132 a dynamic object, using the alias field. Later in this
5133 function we will set the alias field to the correct
5134 value. We only put non-function symbols from dynamic
5135 objects on this list, because that happens to be the only
5136 time we need to know the normal symbol corresponding to a
5137 weak symbol, and the information is time consuming to
5138 figure out. If the alias field is not already NULL,
5139 then this symbol was already defined by some previous
5140 dynamic object, and we will be using that previous
5141 definition anyhow. */
5142
5143 h->u.alias = weaks;
5144 weaks = h;
5145 }
5146
5147 /* Set the alignment of a common symbol. */
5148 if ((common || bfd_is_com_section (sec))
5149 && h->root.type == bfd_link_hash_common)
5150 {
5151 unsigned int align;
5152
5153 if (common)
5154 align = bfd_log2 (isym->st_value);
5155 else
5156 {
5157 /* The new symbol is a common symbol in a shared object.
5158 We need to get the alignment from the section. */
5159 align = new_sec->alignment_power;
5160 }
5161 if (align > old_alignment)
5162 h->root.u.c.p->alignment_power = align;
5163 else
5164 h->root.u.c.p->alignment_power = old_alignment;
5165 }
5166
5167 if (is_elf_hash_table (&htab->root))
5168 {
5169 /* Set a flag in the hash table entry indicating the type of
5170 reference or definition we just found. A dynamic symbol
5171 is one which is referenced or defined by both a regular
5172 object and a shared object. */
5173 bool dynsym = false;
5174
5175 /* Plugin symbols aren't normal. Don't set def/ref flags. */
5176 if ((abfd->flags & BFD_PLUGIN) != 0)
5177 {
5178 /* Except for this flag to track nonweak references. */
5179 if (!definition
5180 && bind != STB_WEAK)
5181 h->ref_ir_nonweak = 1;
5182 }
5183 else if (!dynamic)
5184 {
5185 if (! definition)
5186 {
5187 h->ref_regular = 1;
5188 if (bind != STB_WEAK)
5189 h->ref_regular_nonweak = 1;
5190 }
5191 else
5192 {
5193 h->def_regular = 1;
5194 if (h->def_dynamic)
5195 {
5196 h->def_dynamic = 0;
5197 h->ref_dynamic = 1;
5198 }
5199 }
5200 }
5201 else
5202 {
5203 if (! definition)
5204 {
5205 h->ref_dynamic = 1;
5206 hi->ref_dynamic = 1;
5207 }
5208 else
5209 {
5210 h->def_dynamic = 1;
5211 hi->def_dynamic = 1;
5212 }
5213 }
5214
5215 /* If an indirect symbol has been forced local, don't
5216 make the real symbol dynamic. */
5217 if (h != hi && hi->forced_local)
5218 ;
5219 else if (!dynamic)
5220 {
5221 if (bfd_link_dll (info)
5222 || h->def_dynamic
5223 || h->ref_dynamic)
5224 dynsym = true;
5225 }
5226 else
5227 {
5228 if (h->def_regular
5229 || h->ref_regular
5230 || (h->is_weakalias
5231 && weakdef (h)->dynindx != -1))
5232 dynsym = true;
5233 }
5234
5235 /* Check to see if we need to add an indirect symbol for
5236 the default name. */
5237 if ((definition
5238 || (!override && h->root.type == bfd_link_hash_common))
5239 && !(hi != h
5240 && hi->versioned == versioned_hidden))
5241 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
5242 sec, value, &old_bfd, &dynsym))
5243 goto error_free_vers;
5244
5245 /* Check the alignment when a common symbol is involved. This
5246 can change when a common symbol is overridden by a normal
5247 definition or a common symbol is ignored due to the old
5248 normal definition. We need to make sure the maximum
5249 alignment is maintained. */
5250 if ((old_alignment || common)
5251 && h->root.type != bfd_link_hash_common)
5252 {
5253 unsigned int common_align;
5254 unsigned int normal_align;
5255 unsigned int symbol_align;
5256 bfd *normal_bfd;
5257 bfd *common_bfd;
5258
5259 BFD_ASSERT (h->root.type == bfd_link_hash_defined
5260 || h->root.type == bfd_link_hash_defweak);
5261
5262 symbol_align = ffs (h->root.u.def.value) - 1;
5263 if (h->root.u.def.section->owner != NULL
5264 && (h->root.u.def.section->owner->flags
5265 & (DYNAMIC | BFD_PLUGIN)) == 0)
5266 {
5267 normal_align = h->root.u.def.section->alignment_power;
5268 if (normal_align > symbol_align)
5269 normal_align = symbol_align;
5270 }
5271 else
5272 normal_align = symbol_align;
5273
5274 if (old_alignment)
5275 {
5276 common_align = old_alignment;
5277 common_bfd = old_bfd;
5278 normal_bfd = abfd;
5279 }
5280 else
5281 {
5282 common_align = bfd_log2 (isym->st_value);
5283 common_bfd = abfd;
5284 normal_bfd = old_bfd;
5285 }
5286
5287 if (normal_align < common_align)
5288 {
5289 /* PR binutils/2735 */
5290 if (normal_bfd == NULL)
5291 _bfd_error_handler
5292 /* xgettext:c-format */
5293 (_("warning: alignment %u of common symbol `%s' in %pB is"
5294 " greater than the alignment (%u) of its section %pA"),
5295 1 << common_align, name, common_bfd,
5296 1 << normal_align, h->root.u.def.section);
5297 else
5298 _bfd_error_handler
5299 /* xgettext:c-format */
5300 (_("warning: alignment %u of symbol `%s' in %pB"
5301 " is smaller than %u in %pB"),
5302 1 << normal_align, name, normal_bfd,
5303 1 << common_align, common_bfd);
5304 }
5305 }
5306
5307 /* Remember the symbol size if it isn't undefined. */
5308 if (isym->st_size != 0
5309 && isym->st_shndx != SHN_UNDEF
5310 && (definition || h->size == 0))
5311 {
5312 if (h->size != 0
5313 && h->size != isym->st_size
5314 && ! size_change_ok)
5315 _bfd_error_handler
5316 /* xgettext:c-format */
5317 (_("warning: size of symbol `%s' changed"
5318 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
5319 name, (uint64_t) h->size, old_bfd,
5320 (uint64_t) isym->st_size, abfd);
5321
5322 h->size = isym->st_size;
5323 }
5324
5325 /* If this is a common symbol, then we always want H->SIZE
5326 to be the size of the common symbol. The code just above
5327 won't fix the size if a common symbol becomes larger. We
5328 don't warn about a size change here, because that is
5329 covered by --warn-common. Allow changes between different
5330 function types. */
5331 if (h->root.type == bfd_link_hash_common)
5332 h->size = h->root.u.c.size;
5333
5334 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
5335 && ((definition && !new_weak)
5336 || (old_weak && h->root.type == bfd_link_hash_common)
5337 || h->type == STT_NOTYPE))
5338 {
5339 unsigned int type = ELF_ST_TYPE (isym->st_info);
5340
5341 /* Turn an IFUNC symbol from a DSO into a normal FUNC
5342 symbol. */
5343 if (type == STT_GNU_IFUNC
5344 && (abfd->flags & DYNAMIC) != 0)
5345 type = STT_FUNC;
5346
5347 if (h->type != type)
5348 {
5349 if (h->type != STT_NOTYPE && ! type_change_ok)
5350 /* xgettext:c-format */
5351 _bfd_error_handler
5352 (_("warning: type of symbol `%s' changed"
5353 " from %d to %d in %pB"),
5354 name, h->type, type, abfd);
5355
5356 h->type = type;
5357 }
5358 }
5359
5360 /* Merge st_other field. */
5361 elf_merge_st_other (abfd, h, isym->st_other, sec,
5362 definition, dynamic);
5363
5364 /* We don't want to make debug symbol dynamic. */
5365 if (definition
5366 && (sec->flags & SEC_DEBUGGING)
5367 && !bfd_link_relocatable (info))
5368 dynsym = false;
5369
5370 /* Nor should we make plugin symbols dynamic. */
5371 if ((abfd->flags & BFD_PLUGIN) != 0)
5372 dynsym = false;
5373
5374 if (definition)
5375 {
5376 h->target_internal = isym->st_target_internal;
5377 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5378 }
5379
5380 if (definition && !dynamic)
5381 {
5382 char *p = strchr (name, ELF_VER_CHR);
5383 if (p != NULL && p[1] != ELF_VER_CHR)
5384 {
5385 /* Queue non-default versions so that .symver x, x@FOO
5386 aliases can be checked. */
5387 if (!nondeflt_vers)
5388 {
5389 size_t amt = ((isymend - isym + 1)
5390 * sizeof (struct elf_link_hash_entry *));
5391 nondeflt_vers
5392 = (struct elf_link_hash_entry **) bfd_malloc (amt);
5393 if (!nondeflt_vers)
5394 goto error_free_vers;
5395 }
5396 nondeflt_vers[nondeflt_vers_cnt++] = h;
5397 }
5398 }
5399
5400 if (dynsym && h->dynindx == -1)
5401 {
5402 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5403 goto error_free_vers;
5404 if (h->is_weakalias
5405 && weakdef (h)->dynindx == -1)
5406 {
5407 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
5408 goto error_free_vers;
5409 }
5410 }
5411 else if (h->dynindx != -1)
5412 /* If the symbol already has a dynamic index, but
5413 visibility says it should not be visible, turn it into
5414 a local symbol. */
5415 switch (ELF_ST_VISIBILITY (h->other))
5416 {
5417 case STV_INTERNAL:
5418 case STV_HIDDEN:
5419 (*bed->elf_backend_hide_symbol) (info, h, true);
5420 dynsym = false;
5421 break;
5422 }
5423
5424 if (!add_needed
5425 && matched
5426 && definition
5427 && h->root.type != bfd_link_hash_indirect
5428 && ((dynsym
5429 && h->ref_regular_nonweak)
5430 || (old_bfd != NULL
5431 && (old_bfd->flags & BFD_PLUGIN) != 0
5432 && h->ref_ir_nonweak
5433 && !info->lto_all_symbols_read)
5434 || (h->ref_dynamic_nonweak
5435 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5436 && !on_needed_list (elf_dt_name (abfd),
5437 htab->needed, NULL))))
5438 {
5439 const char *soname = elf_dt_name (abfd);
5440
5441 info->callbacks->minfo ("%!", soname, old_bfd,
5442 h->root.root.string);
5443
5444 /* A symbol from a library loaded via DT_NEEDED of some
5445 other library is referenced by a regular object.
5446 Add a DT_NEEDED entry for it. Issue an error if
5447 --no-add-needed is used and the reference was not
5448 a weak one. */
5449 if (old_bfd != NULL
5450 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
5451 {
5452 _bfd_error_handler
5453 /* xgettext:c-format */
5454 (_("%pB: undefined reference to symbol '%s'"),
5455 old_bfd, name);
5456 bfd_set_error (bfd_error_missing_dso);
5457 goto error_free_vers;
5458 }
5459
5460 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
5461 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
5462
5463 /* Create dynamic sections for backends that require
5464 that be done before setup_gnu_properties. */
5465 if (!_bfd_elf_link_create_dynamic_sections (abfd, info))
5466 return false;
5467 add_needed = true;
5468 }
5469 }
5470 }
5471
5472 if (info->lto_plugin_active
5473 && !bfd_link_relocatable (info)
5474 && (abfd->flags & BFD_PLUGIN) == 0
5475 && !just_syms
5476 && extsymcount)
5477 {
5478 int r_sym_shift;
5479
5480 if (bed->s->arch_size == 32)
5481 r_sym_shift = 8;
5482 else
5483 r_sym_shift = 32;
5484
5485 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5486 referenced in regular objects so that linker plugin will get
5487 the correct symbol resolution. */
5488
5489 sym_hash = elf_sym_hashes (abfd);
5490 for (s = abfd->sections; s != NULL; s = s->next)
5491 {
5492 Elf_Internal_Rela *internal_relocs;
5493 Elf_Internal_Rela *rel, *relend;
5494
5495 /* Don't check relocations in excluded sections. */
5496 if ((s->flags & SEC_RELOC) == 0
5497 || s->reloc_count == 0
5498 || (s->flags & SEC_EXCLUDE) != 0
5499 || ((info->strip == strip_all
5500 || info->strip == strip_debugger)
5501 && (s->flags & SEC_DEBUGGING) != 0))
5502 continue;
5503
5504 internal_relocs = _bfd_elf_link_info_read_relocs (abfd, info,
5505 s, NULL,
5506 NULL,
5507 _bfd_link_keep_memory (info));
5508 if (internal_relocs == NULL)
5509 goto error_free_vers;
5510
5511 rel = internal_relocs;
5512 relend = rel + s->reloc_count;
5513 for ( ; rel < relend; rel++)
5514 {
5515 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5516 struct elf_link_hash_entry *h;
5517
5518 /* Skip local symbols. */
5519 if (r_symndx < extsymoff)
5520 continue;
5521
5522 h = sym_hash[r_symndx - extsymoff];
5523 if (h != NULL)
5524 h->root.non_ir_ref_regular = 1;
5525 }
5526
5527 if (elf_section_data (s)->relocs != internal_relocs)
5528 free (internal_relocs);
5529 }
5530 }
5531
5532 free (extversym);
5533 extversym = NULL;
5534 free (isymbuf);
5535 isymbuf = NULL;
5536
5537 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5538 {
5539 unsigned int i;
5540
5541 /* Restore the symbol table. */
5542 old_ent = (char *) old_tab + tabsize;
5543 memset (elf_sym_hashes (abfd), 0,
5544 extsymcount * sizeof (struct elf_link_hash_entry *));
5545 htab->root.table.table = old_table;
5546 htab->root.table.size = old_size;
5547 htab->root.table.count = old_count;
5548 memcpy (htab->root.table.table, old_tab, tabsize);
5549 htab->root.undefs = old_undefs;
5550 htab->root.undefs_tail = old_undefs_tail;
5551 if (htab->dynstr != NULL)
5552 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5553 free (old_strtab);
5554 old_strtab = NULL;
5555 for (i = 0; i < htab->root.table.size; i++)
5556 {
5557 struct bfd_hash_entry *p;
5558 struct elf_link_hash_entry *h;
5559 unsigned int non_ir_ref_dynamic;
5560
5561 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5562 {
5563 /* Preserve non_ir_ref_dynamic so that this symbol
5564 will be exported when the dynamic lib becomes needed
5565 in the second pass. */
5566 h = (struct elf_link_hash_entry *) p;
5567 if (h->root.type == bfd_link_hash_warning)
5568 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5569 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
5570
5571 h = (struct elf_link_hash_entry *) p;
5572 memcpy (h, old_ent, htab->root.table.entsize);
5573 old_ent = (char *) old_ent + htab->root.table.entsize;
5574 if (h->root.type == bfd_link_hash_warning)
5575 {
5576 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5577 memcpy (h, old_ent, htab->root.table.entsize);
5578 old_ent = (char *) old_ent + htab->root.table.entsize;
5579 }
5580 if (h->root.type == bfd_link_hash_common)
5581 {
5582 memcpy (h->root.u.c.p, old_ent, sizeof (*h->root.u.c.p));
5583 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
5584 }
5585 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
5586 }
5587 }
5588
5589 /* Make a special call to the linker "notice" function to
5590 tell it that symbols added for crefs may need to be removed. */
5591 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
5592 goto error_free_vers;
5593
5594 free (old_tab);
5595 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5596 alloc_mark);
5597 free (nondeflt_vers);
5598 return true;
5599 }
5600
5601 if (old_tab != NULL)
5602 {
5603 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
5604 goto error_free_vers;
5605 free (old_tab);
5606 old_tab = NULL;
5607 }
5608
5609 /* Now that all the symbols from this input file are created, if
5610 not performing a relocatable link, handle .symver foo, foo@BAR
5611 such that any relocs against foo become foo@BAR. */
5612 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
5613 {
5614 size_t cnt, symidx;
5615
5616 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5617 {
5618 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5619 char *shortname, *p;
5620 size_t amt;
5621
5622 p = strchr (h->root.root.string, ELF_VER_CHR);
5623 if (p == NULL
5624 || (h->root.type != bfd_link_hash_defined
5625 && h->root.type != bfd_link_hash_defweak))
5626 continue;
5627
5628 amt = p - h->root.root.string;
5629 shortname = (char *) bfd_malloc (amt + 1);
5630 if (!shortname)
5631 goto error_free_vers;
5632 memcpy (shortname, h->root.root.string, amt);
5633 shortname[amt] = '\0';
5634
5635 hi = (struct elf_link_hash_entry *)
5636 bfd_link_hash_lookup (&htab->root, shortname,
5637 false, false, false);
5638 if (hi != NULL
5639 && hi->root.type == h->root.type
5640 && hi->root.u.def.value == h->root.u.def.value
5641 && hi->root.u.def.section == h->root.u.def.section)
5642 {
5643 (*bed->elf_backend_hide_symbol) (info, hi, true);
5644 hi->root.type = bfd_link_hash_indirect;
5645 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
5646 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
5647 sym_hash = elf_sym_hashes (abfd);
5648 if (sym_hash)
5649 for (symidx = 0; symidx < extsymcount; ++symidx)
5650 if (sym_hash[symidx] == hi)
5651 {
5652 sym_hash[symidx] = h;
5653 break;
5654 }
5655 }
5656 free (shortname);
5657 }
5658 free (nondeflt_vers);
5659 nondeflt_vers = NULL;
5660 }
5661
5662 /* Now set the alias field correctly for all the weak defined
5663 symbols we found. The only way to do this is to search all the
5664 symbols. Since we only need the information for non functions in
5665 dynamic objects, that's the only time we actually put anything on
5666 the list WEAKS. We need this information so that if a regular
5667 object refers to a symbol defined weakly in a dynamic object, the
5668 real symbol in the dynamic object is also put in the dynamic
5669 symbols; we also must arrange for both symbols to point to the
5670 same memory location. We could handle the general case of symbol
5671 aliasing, but a general symbol alias can only be generated in
5672 assembler code, handling it correctly would be very time
5673 consuming, and other ELF linkers don't handle general aliasing
5674 either. */
5675 if (weaks != NULL)
5676 {
5677 struct elf_link_hash_entry **hpp;
5678 struct elf_link_hash_entry **hppend;
5679 struct elf_link_hash_entry **sorted_sym_hash;
5680 struct elf_link_hash_entry *h;
5681 size_t sym_count, amt;
5682
5683 /* Since we have to search the whole symbol list for each weak
5684 defined symbol, search time for N weak defined symbols will be
5685 O(N^2). Binary search will cut it down to O(NlogN). */
5686 amt = extsymcount * sizeof (*sorted_sym_hash);
5687 sorted_sym_hash = bfd_malloc (amt);
5688 if (sorted_sym_hash == NULL)
5689 goto error_return;
5690 sym_hash = sorted_sym_hash;
5691 hpp = elf_sym_hashes (abfd);
5692 hppend = hpp + extsymcount;
5693 sym_count = 0;
5694 for (; hpp < hppend; hpp++)
5695 {
5696 h = *hpp;
5697 if (h != NULL
5698 && h->root.type == bfd_link_hash_defined
5699 && !bed->is_function_type (h->type))
5700 {
5701 *sym_hash = h;
5702 sym_hash++;
5703 sym_count++;
5704 }
5705 }
5706
5707 qsort (sorted_sym_hash, sym_count, sizeof (*sorted_sym_hash),
5708 elf_sort_symbol);
5709
5710 while (weaks != NULL)
5711 {
5712 struct elf_link_hash_entry *hlook;
5713 asection *slook;
5714 bfd_vma vlook;
5715 size_t i, j, idx = 0;
5716
5717 hlook = weaks;
5718 weaks = hlook->u.alias;
5719 hlook->u.alias = NULL;
5720
5721 if (hlook->root.type != bfd_link_hash_defined
5722 && hlook->root.type != bfd_link_hash_defweak)
5723 continue;
5724
5725 slook = hlook->root.u.def.section;
5726 vlook = hlook->root.u.def.value;
5727
5728 i = 0;
5729 j = sym_count;
5730 while (i != j)
5731 {
5732 bfd_signed_vma vdiff;
5733 idx = (i + j) / 2;
5734 h = sorted_sym_hash[idx];
5735 vdiff = vlook - h->root.u.def.value;
5736 if (vdiff < 0)
5737 j = idx;
5738 else if (vdiff > 0)
5739 i = idx + 1;
5740 else
5741 {
5742 int sdiff = slook->id - h->root.u.def.section->id;
5743 if (sdiff < 0)
5744 j = idx;
5745 else if (sdiff > 0)
5746 i = idx + 1;
5747 else
5748 break;
5749 }
5750 }
5751
5752 /* We didn't find a value/section match. */
5753 if (i == j)
5754 continue;
5755
5756 /* With multiple aliases, or when the weak symbol is already
5757 strongly defined, we have multiple matching symbols and
5758 the binary search above may land on any of them. Step
5759 one past the matching symbol(s). */
5760 while (++idx != j)
5761 {
5762 h = sorted_sym_hash[idx];
5763 if (h->root.u.def.section != slook
5764 || h->root.u.def.value != vlook)
5765 break;
5766 }
5767
5768 /* Now look back over the aliases. Since we sorted by size
5769 as well as value and section, we'll choose the one with
5770 the largest size. */
5771 while (idx-- != i)
5772 {
5773 h = sorted_sym_hash[idx];
5774
5775 /* Stop if value or section doesn't match. */
5776 if (h->root.u.def.section != slook
5777 || h->root.u.def.value != vlook)
5778 break;
5779 else if (h != hlook)
5780 {
5781 struct elf_link_hash_entry *t;
5782
5783 hlook->u.alias = h;
5784 hlook->is_weakalias = 1;
5785 t = h;
5786 if (t->u.alias != NULL)
5787 while (t->u.alias != h)
5788 t = t->u.alias;
5789 t->u.alias = hlook;
5790
5791 /* If the weak definition is in the list of dynamic
5792 symbols, make sure the real definition is put
5793 there as well. */
5794 if (hlook->dynindx != -1 && h->dynindx == -1)
5795 {
5796 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5797 {
5798 err_free_sym_hash:
5799 free (sorted_sym_hash);
5800 goto error_return;
5801 }
5802 }
5803
5804 /* If the real definition is in the list of dynamic
5805 symbols, make sure the weak definition is put
5806 there as well. If we don't do this, then the
5807 dynamic loader might not merge the entries for the
5808 real definition and the weak definition. */
5809 if (h->dynindx != -1 && hlook->dynindx == -1)
5810 {
5811 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
5812 goto err_free_sym_hash;
5813 }
5814 break;
5815 }
5816 }
5817 }
5818
5819 free (sorted_sym_hash);
5820 }
5821
5822 if (bed->check_directives
5823 && !(*bed->check_directives) (abfd, info))
5824 return false;
5825
5826 /* If this is a non-traditional link, try to optimize the handling
5827 of the .stab/.stabstr sections. */
5828 if (! dynamic
5829 && ! info->traditional_format
5830 && is_elf_hash_table (&htab->root)
5831 && (info->strip != strip_all && info->strip != strip_debugger))
5832 {
5833 asection *stabstr;
5834
5835 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5836 if (stabstr != NULL)
5837 {
5838 bfd_size_type string_offset = 0;
5839 asection *stab;
5840
5841 for (stab = abfd->sections; stab; stab = stab->next)
5842 if (startswith (stab->name, ".stab")
5843 && (!stab->name[5] ||
5844 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5845 && (stab->flags & SEC_MERGE) == 0
5846 && !bfd_is_abs_section (stab->output_section))
5847 {
5848 struct bfd_elf_section_data *secdata;
5849
5850 secdata = elf_section_data (stab);
5851 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5852 stabstr, &secdata->sec_info,
5853 &string_offset))
5854 goto error_return;
5855 if (secdata->sec_info)
5856 stab->sec_info_type = SEC_INFO_TYPE_STABS;
5857 }
5858 }
5859 }
5860
5861 if (dynamic && add_needed)
5862 {
5863 /* Add this bfd to the loaded list. */
5864 struct elf_link_loaded_list *n;
5865
5866 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5867 if (n == NULL)
5868 goto error_return;
5869 n->abfd = abfd;
5870 n->next = htab->dyn_loaded;
5871 htab->dyn_loaded = n;
5872 }
5873 if (dynamic && !add_needed
5874 && (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) != 0)
5875 elf_dyn_lib_class (abfd) |= DYN_NO_NEEDED;
5876
5877 return true;
5878
5879 error_free_vers:
5880 free (old_tab);
5881 free (old_strtab);
5882 free (nondeflt_vers);
5883 free (extversym);
5884 error_free_sym:
5885 free (isymbuf);
5886 error_return:
5887 return false;
5888 }
5889
5890 /* Return the linker hash table entry of a symbol that might be
5891 satisfied by an archive symbol. Return -1 on error. */
5892
5893 struct bfd_link_hash_entry *
5894 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5895 struct bfd_link_info *info,
5896 const char *name)
5897 {
5898 struct bfd_link_hash_entry *h;
5899 char *p, *copy;
5900 size_t len, first;
5901
5902 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
5903 if (h != NULL)
5904 return h;
5905
5906 /* If this is a default version (the name contains @@), look up the
5907 symbol again with only one `@' as well as without the version.
5908 The effect is that references to the symbol with and without the
5909 version will be matched by the default symbol in the archive. */
5910
5911 p = strchr (name, ELF_VER_CHR);
5912 if (p == NULL || p[1] != ELF_VER_CHR)
5913 return h;
5914
5915 /* First check with only one `@'. */
5916 len = strlen (name);
5917 copy = (char *) bfd_alloc (abfd, len);
5918 if (copy == NULL)
5919 return (struct bfd_link_hash_entry *) -1;
5920
5921 first = p - name + 1;
5922 memcpy (copy, name, first);
5923 memcpy (copy + first, name + first + 1, len - first);
5924
5925 h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
5926 if (h == NULL)
5927 {
5928 /* We also need to check references to the symbol without the
5929 version. */
5930 copy[first - 1] = '\0';
5931 h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
5932 }
5933
5934 bfd_release (abfd, copy);
5935 return h;
5936 }
5937
5938 /* Add symbols from an ELF archive file to the linker hash table. We
5939 don't use _bfd_generic_link_add_archive_symbols because we need to
5940 handle versioned symbols.
5941
5942 Fortunately, ELF archive handling is simpler than that done by
5943 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5944 oddities. In ELF, if we find a symbol in the archive map, and the
5945 symbol is currently undefined, we know that we must pull in that
5946 object file.
5947
5948 Unfortunately, we do have to make multiple passes over the symbol
5949 table until nothing further is resolved. */
5950
5951 static bool
5952 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5953 {
5954 symindex c;
5955 unsigned char *included = NULL;
5956 carsym *symdefs;
5957 bool loop;
5958 size_t amt;
5959 const struct elf_backend_data *bed;
5960 struct bfd_link_hash_entry * (*archive_symbol_lookup)
5961 (bfd *, struct bfd_link_info *, const char *);
5962
5963 if (! bfd_has_map (abfd))
5964 {
5965 /* An empty archive is a special case. */
5966 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5967 return true;
5968 bfd_set_error (bfd_error_no_armap);
5969 return false;
5970 }
5971
5972 /* Keep track of all symbols we know to be already defined, and all
5973 files we know to be already included. This is to speed up the
5974 second and subsequent passes. */
5975 c = bfd_ardata (abfd)->symdef_count;
5976 if (c == 0)
5977 return true;
5978 amt = c * sizeof (*included);
5979 included = (unsigned char *) bfd_zmalloc (amt);
5980 if (included == NULL)
5981 return false;
5982
5983 symdefs = bfd_ardata (abfd)->symdefs;
5984 bed = get_elf_backend_data (abfd);
5985 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5986
5987 do
5988 {
5989 file_ptr last;
5990 symindex i;
5991 carsym *symdef;
5992 carsym *symdefend;
5993
5994 loop = false;
5995 last = -1;
5996
5997 symdef = symdefs;
5998 symdefend = symdef + c;
5999 for (i = 0; symdef < symdefend; symdef++, i++)
6000 {
6001 struct bfd_link_hash_entry *h;
6002 bfd *element;
6003 struct bfd_link_hash_entry *undefs_tail;
6004 symindex mark;
6005
6006 if (included[i])
6007 continue;
6008 if (symdef->file_offset == last)
6009 {
6010 included[i] = true;
6011 continue;
6012 }
6013
6014 h = archive_symbol_lookup (abfd, info, symdef->name);
6015 if (h == (struct bfd_link_hash_entry *) -1)
6016 goto error_return;
6017
6018 if (h == NULL)
6019 continue;
6020
6021 if (h->type == bfd_link_hash_undefined)
6022 {
6023 /* If the archive element has already been loaded then one
6024 of the symbols defined by that element might have been
6025 made undefined due to being in a discarded section. */
6026 if (is_elf_hash_table (info->hash)
6027 && ((struct elf_link_hash_entry *) h)->indx == -3)
6028 continue;
6029 }
6030 else if (h->type == bfd_link_hash_common)
6031 {
6032 /* We currently have a common symbol. The archive map contains
6033 a reference to this symbol, so we may want to include it. We
6034 only want to include it however, if this archive element
6035 contains a definition of the symbol, not just another common
6036 declaration of it.
6037
6038 Unfortunately some archivers (including GNU ar) will put
6039 declarations of common symbols into their archive maps, as
6040 well as real definitions, so we cannot just go by the archive
6041 map alone. Instead we must read in the element's symbol
6042 table and check that to see what kind of symbol definition
6043 this is. */
6044 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
6045 continue;
6046 }
6047 else
6048 {
6049 if (h->type != bfd_link_hash_undefweak)
6050 /* Symbol must be defined. Don't check it again. */
6051 included[i] = true;
6052 continue;
6053 }
6054
6055 /* We need to include this archive member. */
6056 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset,
6057 info);
6058 if (element == NULL)
6059 goto error_return;
6060
6061 if (! bfd_check_format (element, bfd_object))
6062 goto error_return;
6063
6064 undefs_tail = info->hash->undefs_tail;
6065
6066 if (!(*info->callbacks
6067 ->add_archive_element) (info, element, symdef->name, &element))
6068 continue;
6069 if (!bfd_link_add_symbols (element, info))
6070 goto error_return;
6071
6072 /* If there are any new undefined symbols, we need to make
6073 another pass through the archive in order to see whether
6074 they can be defined. FIXME: This isn't perfect, because
6075 common symbols wind up on undefs_tail and because an
6076 undefined symbol which is defined later on in this pass
6077 does not require another pass. This isn't a bug, but it
6078 does make the code less efficient than it could be. */
6079 if (undefs_tail != info->hash->undefs_tail)
6080 loop = true;
6081
6082 /* Look backward to mark all symbols from this object file
6083 which we have already seen in this pass. */
6084 mark = i;
6085 do
6086 {
6087 included[mark] = true;
6088 if (mark == 0)
6089 break;
6090 --mark;
6091 }
6092 while (symdefs[mark].file_offset == symdef->file_offset);
6093
6094 /* We mark subsequent symbols from this object file as we go
6095 on through the loop. */
6096 last = symdef->file_offset;
6097 }
6098 }
6099 while (loop);
6100
6101 free (included);
6102 return true;
6103
6104 error_return:
6105 free (included);
6106 return false;
6107 }
6108
6109 /* Given an ELF BFD, add symbols to the global hash table as
6110 appropriate. */
6111
6112 bool
6113 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
6114 {
6115 switch (bfd_get_format (abfd))
6116 {
6117 case bfd_object:
6118 return elf_link_add_object_symbols (abfd, info);
6119 case bfd_archive:
6120 return elf_link_add_archive_symbols (abfd, info);
6121 default:
6122 bfd_set_error (bfd_error_wrong_format);
6123 return false;
6124 }
6125 }
6126 \f
6127 struct hash_codes_info
6128 {
6129 unsigned long *hashcodes;
6130 bool error;
6131 };
6132
6133 /* This function will be called though elf_link_hash_traverse to store
6134 all hash value of the exported symbols in an array. */
6135
6136 static bool
6137 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
6138 {
6139 struct hash_codes_info *inf = (struct hash_codes_info *) data;
6140 const char *name;
6141 unsigned long ha;
6142 char *alc = NULL;
6143
6144 /* Ignore indirect symbols. These are added by the versioning code. */
6145 if (h->dynindx == -1)
6146 return true;
6147
6148 name = h->root.root.string;
6149 if (h->versioned >= versioned)
6150 {
6151 char *p = strchr (name, ELF_VER_CHR);
6152 if (p != NULL)
6153 {
6154 alc = (char *) bfd_malloc (p - name + 1);
6155 if (alc == NULL)
6156 {
6157 inf->error = true;
6158 return false;
6159 }
6160 memcpy (alc, name, p - name);
6161 alc[p - name] = '\0';
6162 name = alc;
6163 }
6164 }
6165
6166 /* Compute the hash value. */
6167 ha = bfd_elf_hash (name);
6168
6169 /* Store the found hash value in the array given as the argument. */
6170 *(inf->hashcodes)++ = ha;
6171
6172 /* And store it in the struct so that we can put it in the hash table
6173 later. */
6174 h->u.elf_hash_value = ha;
6175
6176 free (alc);
6177 return true;
6178 }
6179
6180 struct collect_gnu_hash_codes
6181 {
6182 bfd *output_bfd;
6183 const struct elf_backend_data *bed;
6184 unsigned long int nsyms;
6185 unsigned long int maskbits;
6186 unsigned long int *hashcodes;
6187 unsigned long int *hashval;
6188 unsigned long int *indx;
6189 unsigned long int *counts;
6190 bfd_vma *bitmask;
6191 bfd_byte *contents;
6192 bfd_size_type xlat;
6193 long int min_dynindx;
6194 unsigned long int bucketcount;
6195 unsigned long int symindx;
6196 long int local_indx;
6197 long int shift1, shift2;
6198 unsigned long int mask;
6199 bool error;
6200 };
6201
6202 /* This function will be called though elf_link_hash_traverse to store
6203 all hash value of the exported symbols in an array. */
6204
6205 static bool
6206 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
6207 {
6208 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6209 const char *name;
6210 unsigned long ha;
6211 char *alc = NULL;
6212
6213 /* Ignore indirect symbols. These are added by the versioning code. */
6214 if (h->dynindx == -1)
6215 return true;
6216
6217 /* Ignore also local symbols and undefined symbols. */
6218 if (! (*s->bed->elf_hash_symbol) (h))
6219 return true;
6220
6221 name = h->root.root.string;
6222 if (h->versioned >= versioned)
6223 {
6224 char *p = strchr (name, ELF_VER_CHR);
6225 if (p != NULL)
6226 {
6227 alc = (char *) bfd_malloc (p - name + 1);
6228 if (alc == NULL)
6229 {
6230 s->error = true;
6231 return false;
6232 }
6233 memcpy (alc, name, p - name);
6234 alc[p - name] = '\0';
6235 name = alc;
6236 }
6237 }
6238
6239 /* Compute the hash value. */
6240 ha = bfd_elf_gnu_hash (name);
6241
6242 /* Store the found hash value in the array for compute_bucket_count,
6243 and also for .dynsym reordering purposes. */
6244 s->hashcodes[s->nsyms] = ha;
6245 s->hashval[h->dynindx] = ha;
6246 ++s->nsyms;
6247 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
6248 s->min_dynindx = h->dynindx;
6249
6250 free (alc);
6251 return true;
6252 }
6253
6254 /* This function will be called though elf_link_hash_traverse to do
6255 final dynamic symbol renumbering in case of .gnu.hash.
6256 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6257 to the translation table. */
6258
6259 static bool
6260 elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data)
6261 {
6262 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6263 unsigned long int bucket;
6264 unsigned long int val;
6265
6266 /* Ignore indirect symbols. */
6267 if (h->dynindx == -1)
6268 return true;
6269
6270 /* Ignore also local symbols and undefined symbols. */
6271 if (! (*s->bed->elf_hash_symbol) (h))
6272 {
6273 if (h->dynindx >= s->min_dynindx)
6274 {
6275 if (s->bed->record_xhash_symbol != NULL)
6276 {
6277 (*s->bed->record_xhash_symbol) (h, 0);
6278 s->local_indx++;
6279 }
6280 else
6281 h->dynindx = s->local_indx++;
6282 }
6283 return true;
6284 }
6285
6286 bucket = s->hashval[h->dynindx] % s->bucketcount;
6287 val = (s->hashval[h->dynindx] >> s->shift1)
6288 & ((s->maskbits >> s->shift1) - 1);
6289 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
6290 s->bitmask[val]
6291 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
6292 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
6293 if (s->counts[bucket] == 1)
6294 /* Last element terminates the chain. */
6295 val |= 1;
6296 bfd_put_32 (s->output_bfd, val,
6297 s->contents + (s->indx[bucket] - s->symindx) * 4);
6298 --s->counts[bucket];
6299 if (s->bed->record_xhash_symbol != NULL)
6300 {
6301 bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4;
6302
6303 (*s->bed->record_xhash_symbol) (h, xlat_loc);
6304 }
6305 else
6306 h->dynindx = s->indx[bucket]++;
6307 return true;
6308 }
6309
6310 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6311
6312 bool
6313 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
6314 {
6315 return !(h->forced_local
6316 || h->root.type == bfd_link_hash_undefined
6317 || h->root.type == bfd_link_hash_undefweak
6318 || ((h->root.type == bfd_link_hash_defined
6319 || h->root.type == bfd_link_hash_defweak)
6320 && h->root.u.def.section->output_section == NULL));
6321 }
6322
6323 /* Array used to determine the number of hash table buckets to use
6324 based on the number of symbols there are. If there are fewer than
6325 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6326 fewer than 37 we use 17 buckets, and so forth. We never use more
6327 than 32771 buckets. */
6328
6329 static const size_t elf_buckets[] =
6330 {
6331 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6332 16411, 32771, 0
6333 };
6334
6335 /* Compute bucket count for hashing table. We do not use a static set
6336 of possible tables sizes anymore. Instead we determine for all
6337 possible reasonable sizes of the table the outcome (i.e., the
6338 number of collisions etc) and choose the best solution. The
6339 weighting functions are not too simple to allow the table to grow
6340 without bounds. Instead one of the weighting factors is the size.
6341 Therefore the result is always a good payoff between few collisions
6342 (= short chain lengths) and table size. */
6343 static size_t
6344 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
6345 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
6346 unsigned long int nsyms,
6347 int gnu_hash)
6348 {
6349 size_t best_size = 0;
6350 unsigned long int i;
6351
6352 /* We have a problem here. The following code to optimize the table
6353 size requires an integer type with more the 32 bits. If
6354 BFD_HOST_U_64_BIT is set we know about such a type. */
6355 #ifdef BFD_HOST_U_64_BIT
6356 if (info->optimize)
6357 {
6358 size_t minsize;
6359 size_t maxsize;
6360 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
6361 bfd *dynobj = elf_hash_table (info)->dynobj;
6362 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
6363 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
6364 unsigned long int *counts;
6365 bfd_size_type amt;
6366 unsigned int no_improvement_count = 0;
6367
6368 /* Possible optimization parameters: if we have NSYMS symbols we say
6369 that the hashing table must at least have NSYMS/4 and at most
6370 2*NSYMS buckets. */
6371 minsize = nsyms / 4;
6372 if (minsize == 0)
6373 minsize = 1;
6374 best_size = maxsize = nsyms * 2;
6375 if (gnu_hash)
6376 {
6377 if (minsize < 2)
6378 minsize = 2;
6379 if ((best_size & 31) == 0)
6380 ++best_size;
6381 }
6382
6383 /* Create array where we count the collisions in. We must use bfd_malloc
6384 since the size could be large. */
6385 amt = maxsize;
6386 amt *= sizeof (unsigned long int);
6387 counts = (unsigned long int *) bfd_malloc (amt);
6388 if (counts == NULL)
6389 return 0;
6390
6391 /* Compute the "optimal" size for the hash table. The criteria is a
6392 minimal chain length. The minor criteria is (of course) the size
6393 of the table. */
6394 for (i = minsize; i < maxsize; ++i)
6395 {
6396 /* Walk through the array of hashcodes and count the collisions. */
6397 BFD_HOST_U_64_BIT max;
6398 unsigned long int j;
6399 unsigned long int fact;
6400
6401 if (gnu_hash && (i & 31) == 0)
6402 continue;
6403
6404 memset (counts, '\0', i * sizeof (unsigned long int));
6405
6406 /* Determine how often each hash bucket is used. */
6407 for (j = 0; j < nsyms; ++j)
6408 ++counts[hashcodes[j] % i];
6409
6410 /* For the weight function we need some information about the
6411 pagesize on the target. This is information need not be 100%
6412 accurate. Since this information is not available (so far) we
6413 define it here to a reasonable default value. If it is crucial
6414 to have a better value some day simply define this value. */
6415 # ifndef BFD_TARGET_PAGESIZE
6416 # define BFD_TARGET_PAGESIZE (4096)
6417 # endif
6418
6419 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6420 and the chains. */
6421 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
6422
6423 # if 1
6424 /* Variant 1: optimize for short chains. We add the squares
6425 of all the chain lengths (which favors many small chain
6426 over a few long chains). */
6427 for (j = 0; j < i; ++j)
6428 max += counts[j] * counts[j];
6429
6430 /* This adds penalties for the overall size of the table. */
6431 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6432 max *= fact * fact;
6433 # else
6434 /* Variant 2: Optimize a lot more for small table. Here we
6435 also add squares of the size but we also add penalties for
6436 empty slots (the +1 term). */
6437 for (j = 0; j < i; ++j)
6438 max += (1 + counts[j]) * (1 + counts[j]);
6439
6440 /* The overall size of the table is considered, but not as
6441 strong as in variant 1, where it is squared. */
6442 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6443 max *= fact;
6444 # endif
6445
6446 /* Compare with current best results. */
6447 if (max < best_chlen)
6448 {
6449 best_chlen = max;
6450 best_size = i;
6451 no_improvement_count = 0;
6452 }
6453 /* PR 11843: Avoid futile long searches for the best bucket size
6454 when there are a large number of symbols. */
6455 else if (++no_improvement_count == 100)
6456 break;
6457 }
6458
6459 free (counts);
6460 }
6461 else
6462 #endif /* defined (BFD_HOST_U_64_BIT) */
6463 {
6464 /* This is the fallback solution if no 64bit type is available or if we
6465 are not supposed to spend much time on optimizations. We select the
6466 bucket count using a fixed set of numbers. */
6467 for (i = 0; elf_buckets[i] != 0; i++)
6468 {
6469 best_size = elf_buckets[i];
6470 if (nsyms < elf_buckets[i + 1])
6471 break;
6472 }
6473 if (gnu_hash && best_size < 2)
6474 best_size = 2;
6475 }
6476
6477 return best_size;
6478 }
6479
6480 /* Size any SHT_GROUP section for ld -r. */
6481
6482 bool
6483 _bfd_elf_size_group_sections (struct bfd_link_info *info)
6484 {
6485 bfd *ibfd;
6486 asection *s;
6487
6488 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6489 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6490 && (s = ibfd->sections) != NULL
6491 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
6492 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6493 return false;
6494 return true;
6495 }
6496
6497 /* Set a default stack segment size. The value in INFO wins. If it
6498 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6499 undefined it is initialized. */
6500
6501 bool
6502 bfd_elf_stack_segment_size (bfd *output_bfd,
6503 struct bfd_link_info *info,
6504 const char *legacy_symbol,
6505 bfd_vma default_size)
6506 {
6507 struct elf_link_hash_entry *h = NULL;
6508
6509 /* Look for legacy symbol. */
6510 if (legacy_symbol)
6511 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6512 false, false, false);
6513 if (h && (h->root.type == bfd_link_hash_defined
6514 || h->root.type == bfd_link_hash_defweak)
6515 && h->def_regular
6516 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6517 {
6518 /* The symbol has no type if specified on the command line. */
6519 h->type = STT_OBJECT;
6520 if (info->stacksize)
6521 /* xgettext:c-format */
6522 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6523 output_bfd, legacy_symbol);
6524 else if (h->root.u.def.section != bfd_abs_section_ptr)
6525 /* xgettext:c-format */
6526 _bfd_error_handler (_("%pB: %s not absolute"),
6527 output_bfd, legacy_symbol);
6528 else
6529 info->stacksize = h->root.u.def.value;
6530 }
6531
6532 if (!info->stacksize)
6533 /* If the user didn't set a size, or explicitly inhibit the
6534 size, set it now. */
6535 info->stacksize = default_size;
6536
6537 /* Provide the legacy symbol, if it is referenced. */
6538 if (h && (h->root.type == bfd_link_hash_undefined
6539 || h->root.type == bfd_link_hash_undefweak))
6540 {
6541 struct bfd_link_hash_entry *bh = NULL;
6542
6543 if (!(_bfd_generic_link_add_one_symbol
6544 (info, output_bfd, legacy_symbol,
6545 BSF_GLOBAL, bfd_abs_section_ptr,
6546 info->stacksize >= 0 ? info->stacksize : 0,
6547 NULL, false, get_elf_backend_data (output_bfd)->collect, &bh)))
6548 return false;
6549
6550 h = (struct elf_link_hash_entry *) bh;
6551 h->def_regular = 1;
6552 h->type = STT_OBJECT;
6553 }
6554
6555 return true;
6556 }
6557
6558 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6559
6560 struct elf_gc_sweep_symbol_info
6561 {
6562 struct bfd_link_info *info;
6563 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6564 bool);
6565 };
6566
6567 static bool
6568 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6569 {
6570 if (!h->mark
6571 && (((h->root.type == bfd_link_hash_defined
6572 || h->root.type == bfd_link_hash_defweak)
6573 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6574 && h->root.u.def.section->gc_mark))
6575 || h->root.type == bfd_link_hash_undefined
6576 || h->root.type == bfd_link_hash_undefweak))
6577 {
6578 struct elf_gc_sweep_symbol_info *inf;
6579
6580 inf = (struct elf_gc_sweep_symbol_info *) data;
6581 (*inf->hide_symbol) (inf->info, h, true);
6582 h->def_regular = 0;
6583 h->ref_regular = 0;
6584 h->ref_regular_nonweak = 0;
6585 }
6586
6587 return true;
6588 }
6589
6590 /* Set up the sizes and contents of the ELF dynamic sections. This is
6591 called by the ELF linker emulation before_allocation routine. We
6592 must set the sizes of the sections before the linker sets the
6593 addresses of the various sections. */
6594
6595 bool
6596 bfd_elf_size_dynamic_sections (bfd *output_bfd,
6597 const char *soname,
6598 const char *rpath,
6599 const char *filter_shlib,
6600 const char *audit,
6601 const char *depaudit,
6602 const char * const *auxiliary_filters,
6603 struct bfd_link_info *info,
6604 asection **sinterpptr)
6605 {
6606 bfd *dynobj;
6607 const struct elf_backend_data *bed;
6608
6609 *sinterpptr = NULL;
6610
6611 if (!is_elf_hash_table (info->hash))
6612 return true;
6613
6614 /* Any syms created from now on start with -1 in
6615 got.refcount/offset and plt.refcount/offset. */
6616 elf_hash_table (info)->init_got_refcount
6617 = elf_hash_table (info)->init_got_offset;
6618 elf_hash_table (info)->init_plt_refcount
6619 = elf_hash_table (info)->init_plt_offset;
6620
6621 bed = get_elf_backend_data (output_bfd);
6622
6623 /* The backend may have to create some sections regardless of whether
6624 we're dynamic or not. */
6625 if (bed->elf_backend_always_size_sections
6626 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6627 return false;
6628
6629 dynobj = elf_hash_table (info)->dynobj;
6630
6631 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6632 {
6633 struct bfd_elf_version_tree *verdefs;
6634 struct elf_info_failed asvinfo;
6635 struct bfd_elf_version_tree *t;
6636 struct bfd_elf_version_expr *d;
6637 asection *s;
6638 size_t soname_indx;
6639
6640 /* If we are supposed to export all symbols into the dynamic symbol
6641 table (this is not the normal case), then do so. */
6642 if (info->export_dynamic
6643 || (bfd_link_executable (info) && info->dynamic))
6644 {
6645 struct elf_info_failed eif;
6646
6647 eif.info = info;
6648 eif.failed = false;
6649 elf_link_hash_traverse (elf_hash_table (info),
6650 _bfd_elf_export_symbol,
6651 &eif);
6652 if (eif.failed)
6653 return false;
6654 }
6655
6656 if (soname != NULL)
6657 {
6658 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6659 soname, true);
6660 if (soname_indx == (size_t) -1
6661 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6662 return false;
6663 }
6664 else
6665 soname_indx = (size_t) -1;
6666
6667 /* Make all global versions with definition. */
6668 for (t = info->version_info; t != NULL; t = t->next)
6669 for (d = t->globals.list; d != NULL; d = d->next)
6670 if (!d->symver && d->literal)
6671 {
6672 const char *verstr, *name;
6673 size_t namelen, verlen, newlen;
6674 char *newname, *p, leading_char;
6675 struct elf_link_hash_entry *newh;
6676
6677 leading_char = bfd_get_symbol_leading_char (output_bfd);
6678 name = d->pattern;
6679 namelen = strlen (name) + (leading_char != '\0');
6680 verstr = t->name;
6681 verlen = strlen (verstr);
6682 newlen = namelen + verlen + 3;
6683
6684 newname = (char *) bfd_malloc (newlen);
6685 if (newname == NULL)
6686 return false;
6687 newname[0] = leading_char;
6688 memcpy (newname + (leading_char != '\0'), name, namelen);
6689
6690 /* Check the hidden versioned definition. */
6691 p = newname + namelen;
6692 *p++ = ELF_VER_CHR;
6693 memcpy (p, verstr, verlen + 1);
6694 newh = elf_link_hash_lookup (elf_hash_table (info),
6695 newname, false, false,
6696 false);
6697 if (newh == NULL
6698 || (newh->root.type != bfd_link_hash_defined
6699 && newh->root.type != bfd_link_hash_defweak))
6700 {
6701 /* Check the default versioned definition. */
6702 *p++ = ELF_VER_CHR;
6703 memcpy (p, verstr, verlen + 1);
6704 newh = elf_link_hash_lookup (elf_hash_table (info),
6705 newname, false, false,
6706 false);
6707 }
6708 free (newname);
6709
6710 /* Mark this version if there is a definition and it is
6711 not defined in a shared object. */
6712 if (newh != NULL
6713 && !newh->def_dynamic
6714 && (newh->root.type == bfd_link_hash_defined
6715 || newh->root.type == bfd_link_hash_defweak))
6716 d->symver = 1;
6717 }
6718
6719 /* Attach all the symbols to their version information. */
6720 asvinfo.info = info;
6721 asvinfo.failed = false;
6722
6723 elf_link_hash_traverse (elf_hash_table (info),
6724 _bfd_elf_link_assign_sym_version,
6725 &asvinfo);
6726 if (asvinfo.failed)
6727 return false;
6728
6729 if (!info->allow_undefined_version)
6730 {
6731 /* Check if all global versions have a definition. */
6732 bool all_defined = true;
6733 for (t = info->version_info; t != NULL; t = t->next)
6734 for (d = t->globals.list; d != NULL; d = d->next)
6735 if (d->literal && !d->symver && !d->script)
6736 {
6737 _bfd_error_handler
6738 (_("%s: undefined version: %s"),
6739 d->pattern, t->name);
6740 all_defined = false;
6741 }
6742
6743 if (!all_defined)
6744 {
6745 bfd_set_error (bfd_error_bad_value);
6746 return false;
6747 }
6748 }
6749
6750 /* Set up the version definition section. */
6751 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6752 BFD_ASSERT (s != NULL);
6753
6754 /* We may have created additional version definitions if we are
6755 just linking a regular application. */
6756 verdefs = info->version_info;
6757
6758 /* Skip anonymous version tag. */
6759 if (verdefs != NULL && verdefs->vernum == 0)
6760 verdefs = verdefs->next;
6761
6762 if (verdefs == NULL && !info->create_default_symver)
6763 s->flags |= SEC_EXCLUDE;
6764 else
6765 {
6766 unsigned int cdefs;
6767 bfd_size_type size;
6768 bfd_byte *p;
6769 Elf_Internal_Verdef def;
6770 Elf_Internal_Verdaux defaux;
6771 struct bfd_link_hash_entry *bh;
6772 struct elf_link_hash_entry *h;
6773 const char *name;
6774
6775 cdefs = 0;
6776 size = 0;
6777
6778 /* Make space for the base version. */
6779 size += sizeof (Elf_External_Verdef);
6780 size += sizeof (Elf_External_Verdaux);
6781 ++cdefs;
6782
6783 /* Make space for the default version. */
6784 if (info->create_default_symver)
6785 {
6786 size += sizeof (Elf_External_Verdef);
6787 ++cdefs;
6788 }
6789
6790 for (t = verdefs; t != NULL; t = t->next)
6791 {
6792 struct bfd_elf_version_deps *n;
6793
6794 /* Don't emit base version twice. */
6795 if (t->vernum == 0)
6796 continue;
6797
6798 size += sizeof (Elf_External_Verdef);
6799 size += sizeof (Elf_External_Verdaux);
6800 ++cdefs;
6801
6802 for (n = t->deps; n != NULL; n = n->next)
6803 size += sizeof (Elf_External_Verdaux);
6804 }
6805
6806 s->size = size;
6807 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6808 if (s->contents == NULL && s->size != 0)
6809 return false;
6810
6811 /* Fill in the version definition section. */
6812
6813 p = s->contents;
6814
6815 def.vd_version = VER_DEF_CURRENT;
6816 def.vd_flags = VER_FLG_BASE;
6817 def.vd_ndx = 1;
6818 def.vd_cnt = 1;
6819 if (info->create_default_symver)
6820 {
6821 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6822 def.vd_next = sizeof (Elf_External_Verdef);
6823 }
6824 else
6825 {
6826 def.vd_aux = sizeof (Elf_External_Verdef);
6827 def.vd_next = (sizeof (Elf_External_Verdef)
6828 + sizeof (Elf_External_Verdaux));
6829 }
6830
6831 if (soname_indx != (size_t) -1)
6832 {
6833 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6834 soname_indx);
6835 def.vd_hash = bfd_elf_hash (soname);
6836 defaux.vda_name = soname_indx;
6837 name = soname;
6838 }
6839 else
6840 {
6841 size_t indx;
6842
6843 name = lbasename (bfd_get_filename (output_bfd));
6844 def.vd_hash = bfd_elf_hash (name);
6845 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6846 name, false);
6847 if (indx == (size_t) -1)
6848 return false;
6849 defaux.vda_name = indx;
6850 }
6851 defaux.vda_next = 0;
6852
6853 _bfd_elf_swap_verdef_out (output_bfd, &def,
6854 (Elf_External_Verdef *) p);
6855 p += sizeof (Elf_External_Verdef);
6856 if (info->create_default_symver)
6857 {
6858 /* Add a symbol representing this version. */
6859 bh = NULL;
6860 if (! (_bfd_generic_link_add_one_symbol
6861 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6862 0, NULL, false,
6863 get_elf_backend_data (dynobj)->collect, &bh)))
6864 return false;
6865 h = (struct elf_link_hash_entry *) bh;
6866 h->non_elf = 0;
6867 h->def_regular = 1;
6868 h->type = STT_OBJECT;
6869 h->verinfo.vertree = NULL;
6870
6871 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6872 return false;
6873
6874 /* Create a duplicate of the base version with the same
6875 aux block, but different flags. */
6876 def.vd_flags = 0;
6877 def.vd_ndx = 2;
6878 def.vd_aux = sizeof (Elf_External_Verdef);
6879 if (verdefs)
6880 def.vd_next = (sizeof (Elf_External_Verdef)
6881 + sizeof (Elf_External_Verdaux));
6882 else
6883 def.vd_next = 0;
6884 _bfd_elf_swap_verdef_out (output_bfd, &def,
6885 (Elf_External_Verdef *) p);
6886 p += sizeof (Elf_External_Verdef);
6887 }
6888 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6889 (Elf_External_Verdaux *) p);
6890 p += sizeof (Elf_External_Verdaux);
6891
6892 for (t = verdefs; t != NULL; t = t->next)
6893 {
6894 unsigned int cdeps;
6895 struct bfd_elf_version_deps *n;
6896
6897 /* Don't emit the base version twice. */
6898 if (t->vernum == 0)
6899 continue;
6900
6901 cdeps = 0;
6902 for (n = t->deps; n != NULL; n = n->next)
6903 ++cdeps;
6904
6905 /* Add a symbol representing this version. */
6906 bh = NULL;
6907 if (! (_bfd_generic_link_add_one_symbol
6908 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6909 0, NULL, false,
6910 get_elf_backend_data (dynobj)->collect, &bh)))
6911 return false;
6912 h = (struct elf_link_hash_entry *) bh;
6913 h->non_elf = 0;
6914 h->def_regular = 1;
6915 h->type = STT_OBJECT;
6916 h->verinfo.vertree = t;
6917
6918 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6919 return false;
6920
6921 def.vd_version = VER_DEF_CURRENT;
6922 def.vd_flags = 0;
6923 if (t->globals.list == NULL
6924 && t->locals.list == NULL
6925 && ! t->used)
6926 def.vd_flags |= VER_FLG_WEAK;
6927 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6928 def.vd_cnt = cdeps + 1;
6929 def.vd_hash = bfd_elf_hash (t->name);
6930 def.vd_aux = sizeof (Elf_External_Verdef);
6931 def.vd_next = 0;
6932
6933 /* If a basever node is next, it *must* be the last node in
6934 the chain, otherwise Verdef construction breaks. */
6935 if (t->next != NULL && t->next->vernum == 0)
6936 BFD_ASSERT (t->next->next == NULL);
6937
6938 if (t->next != NULL && t->next->vernum != 0)
6939 def.vd_next = (sizeof (Elf_External_Verdef)
6940 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6941
6942 _bfd_elf_swap_verdef_out (output_bfd, &def,
6943 (Elf_External_Verdef *) p);
6944 p += sizeof (Elf_External_Verdef);
6945
6946 defaux.vda_name = h->dynstr_index;
6947 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6948 h->dynstr_index);
6949 defaux.vda_next = 0;
6950 if (t->deps != NULL)
6951 defaux.vda_next = sizeof (Elf_External_Verdaux);
6952 t->name_indx = defaux.vda_name;
6953
6954 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6955 (Elf_External_Verdaux *) p);
6956 p += sizeof (Elf_External_Verdaux);
6957
6958 for (n = t->deps; n != NULL; n = n->next)
6959 {
6960 if (n->version_needed == NULL)
6961 {
6962 /* This can happen if there was an error in the
6963 version script. */
6964 defaux.vda_name = 0;
6965 }
6966 else
6967 {
6968 defaux.vda_name = n->version_needed->name_indx;
6969 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6970 defaux.vda_name);
6971 }
6972 if (n->next == NULL)
6973 defaux.vda_next = 0;
6974 else
6975 defaux.vda_next = sizeof (Elf_External_Verdaux);
6976
6977 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6978 (Elf_External_Verdaux *) p);
6979 p += sizeof (Elf_External_Verdaux);
6980 }
6981 }
6982
6983 elf_tdata (output_bfd)->cverdefs = cdefs;
6984 }
6985 }
6986
6987 if (info->gc_sections && bed->can_gc_sections)
6988 {
6989 struct elf_gc_sweep_symbol_info sweep_info;
6990
6991 /* Remove the symbols that were in the swept sections from the
6992 dynamic symbol table. */
6993 sweep_info.info = info;
6994 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6995 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6996 &sweep_info);
6997 }
6998
6999 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7000 {
7001 asection *s;
7002 struct elf_find_verdep_info sinfo;
7003
7004 /* Work out the size of the version reference section. */
7005
7006 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
7007 BFD_ASSERT (s != NULL);
7008
7009 sinfo.info = info;
7010 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
7011 if (sinfo.vers == 0)
7012 sinfo.vers = 1;
7013 sinfo.failed = false;
7014
7015 elf_link_hash_traverse (elf_hash_table (info),
7016 _bfd_elf_link_find_version_dependencies,
7017 &sinfo);
7018 if (sinfo.failed)
7019 return false;
7020
7021 if (info->enable_dt_relr)
7022 {
7023 elf_link_add_dt_relr_dependency (&sinfo);
7024 if (sinfo.failed)
7025 return false;
7026 }
7027
7028 if (elf_tdata (output_bfd)->verref == NULL)
7029 s->flags |= SEC_EXCLUDE;
7030 else
7031 {
7032 Elf_Internal_Verneed *vn;
7033 unsigned int size;
7034 unsigned int crefs;
7035 bfd_byte *p;
7036
7037 /* Build the version dependency section. */
7038 size = 0;
7039 crefs = 0;
7040 for (vn = elf_tdata (output_bfd)->verref;
7041 vn != NULL;
7042 vn = vn->vn_nextref)
7043 {
7044 Elf_Internal_Vernaux *a;
7045
7046 size += sizeof (Elf_External_Verneed);
7047 ++crefs;
7048 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7049 size += sizeof (Elf_External_Vernaux);
7050 }
7051
7052 s->size = size;
7053 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7054 if (s->contents == NULL)
7055 return false;
7056
7057 p = s->contents;
7058 for (vn = elf_tdata (output_bfd)->verref;
7059 vn != NULL;
7060 vn = vn->vn_nextref)
7061 {
7062 unsigned int caux;
7063 Elf_Internal_Vernaux *a;
7064 size_t indx;
7065
7066 caux = 0;
7067 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7068 ++caux;
7069
7070 vn->vn_version = VER_NEED_CURRENT;
7071 vn->vn_cnt = caux;
7072 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7073 elf_dt_name (vn->vn_bfd) != NULL
7074 ? elf_dt_name (vn->vn_bfd)
7075 : lbasename (bfd_get_filename
7076 (vn->vn_bfd)),
7077 false);
7078 if (indx == (size_t) -1)
7079 return false;
7080 vn->vn_file = indx;
7081 vn->vn_aux = sizeof (Elf_External_Verneed);
7082 if (vn->vn_nextref == NULL)
7083 vn->vn_next = 0;
7084 else
7085 vn->vn_next = (sizeof (Elf_External_Verneed)
7086 + caux * sizeof (Elf_External_Vernaux));
7087
7088 _bfd_elf_swap_verneed_out (output_bfd, vn,
7089 (Elf_External_Verneed *) p);
7090 p += sizeof (Elf_External_Verneed);
7091
7092 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7093 {
7094 a->vna_hash = bfd_elf_hash (a->vna_nodename);
7095 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7096 a->vna_nodename, false);
7097 if (indx == (size_t) -1)
7098 return false;
7099 a->vna_name = indx;
7100 if (a->vna_nextptr == NULL)
7101 a->vna_next = 0;
7102 else
7103 a->vna_next = sizeof (Elf_External_Vernaux);
7104
7105 _bfd_elf_swap_vernaux_out (output_bfd, a,
7106 (Elf_External_Vernaux *) p);
7107 p += sizeof (Elf_External_Vernaux);
7108 }
7109 }
7110
7111 elf_tdata (output_bfd)->cverrefs = crefs;
7112 }
7113 }
7114
7115 if (bfd_link_relocatable (info)
7116 && !_bfd_elf_size_group_sections (info))
7117 return false;
7118
7119 /* Determine any GNU_STACK segment requirements, after the backend
7120 has had a chance to set a default segment size. */
7121 if (info->execstack)
7122 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
7123 else if (info->noexecstack)
7124 elf_stack_flags (output_bfd) = PF_R | PF_W;
7125 else
7126 {
7127 bfd *inputobj;
7128 asection *notesec = NULL;
7129 int exec = 0;
7130
7131 for (inputobj = info->input_bfds;
7132 inputobj;
7133 inputobj = inputobj->link.next)
7134 {
7135 asection *s;
7136
7137 if (inputobj->flags
7138 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
7139 continue;
7140 s = inputobj->sections;
7141 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7142 continue;
7143
7144 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
7145 if (s)
7146 {
7147 if (s->flags & SEC_CODE)
7148 exec = PF_X;
7149 notesec = s;
7150 }
7151 else if (bed->default_execstack)
7152 exec = PF_X;
7153 }
7154 if (notesec || info->stacksize > 0)
7155 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
7156 if (notesec && exec && bfd_link_relocatable (info)
7157 && notesec->output_section != bfd_abs_section_ptr)
7158 notesec->output_section->flags |= SEC_CODE;
7159 }
7160
7161 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7162 {
7163 struct elf_info_failed eif;
7164 struct elf_link_hash_entry *h;
7165 asection *dynstr;
7166 asection *s;
7167
7168 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
7169 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
7170
7171 if (info->symbolic)
7172 {
7173 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
7174 return false;
7175 info->flags |= DF_SYMBOLIC;
7176 }
7177
7178 if (rpath != NULL)
7179 {
7180 size_t indx;
7181 bfd_vma tag;
7182
7183 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
7184 true);
7185 if (indx == (size_t) -1)
7186 return false;
7187
7188 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
7189 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
7190 return false;
7191 }
7192
7193 if (filter_shlib != NULL)
7194 {
7195 size_t indx;
7196
7197 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7198 filter_shlib, true);
7199 if (indx == (size_t) -1
7200 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
7201 return false;
7202 }
7203
7204 if (auxiliary_filters != NULL)
7205 {
7206 const char * const *p;
7207
7208 for (p = auxiliary_filters; *p != NULL; p++)
7209 {
7210 size_t indx;
7211
7212 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7213 *p, true);
7214 if (indx == (size_t) -1
7215 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
7216 return false;
7217 }
7218 }
7219
7220 if (audit != NULL)
7221 {
7222 size_t indx;
7223
7224 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
7225 true);
7226 if (indx == (size_t) -1
7227 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
7228 return false;
7229 }
7230
7231 if (depaudit != NULL)
7232 {
7233 size_t indx;
7234
7235 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
7236 true);
7237 if (indx == (size_t) -1
7238 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
7239 return false;
7240 }
7241
7242 eif.info = info;
7243 eif.failed = false;
7244
7245 /* Find all symbols which were defined in a dynamic object and make
7246 the backend pick a reasonable value for them. */
7247 elf_link_hash_traverse (elf_hash_table (info),
7248 _bfd_elf_adjust_dynamic_symbol,
7249 &eif);
7250 if (eif.failed)
7251 return false;
7252
7253 /* Add some entries to the .dynamic section. We fill in some of the
7254 values later, in bfd_elf_final_link, but we must add the entries
7255 now so that we know the final size of the .dynamic section. */
7256
7257 /* If there are initialization and/or finalization functions to
7258 call then add the corresponding DT_INIT/DT_FINI entries. */
7259 h = (info->init_function
7260 ? elf_link_hash_lookup (elf_hash_table (info),
7261 info->init_function, false,
7262 false, false)
7263 : NULL);
7264 if (h != NULL
7265 && (h->ref_regular
7266 || h->def_regular))
7267 {
7268 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
7269 return false;
7270 }
7271 h = (info->fini_function
7272 ? elf_link_hash_lookup (elf_hash_table (info),
7273 info->fini_function, false,
7274 false, false)
7275 : NULL);
7276 if (h != NULL
7277 && (h->ref_regular
7278 || h->def_regular))
7279 {
7280 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
7281 return false;
7282 }
7283
7284 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
7285 if (s != NULL && s->linker_has_input)
7286 {
7287 /* DT_PREINIT_ARRAY is not allowed in shared library. */
7288 if (! bfd_link_executable (info))
7289 {
7290 bfd *sub;
7291 asection *o;
7292
7293 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
7294 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
7295 && (o = sub->sections) != NULL
7296 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
7297 for (o = sub->sections; o != NULL; o = o->next)
7298 if (elf_section_data (o)->this_hdr.sh_type
7299 == SHT_PREINIT_ARRAY)
7300 {
7301 _bfd_error_handler
7302 (_("%pB: .preinit_array section is not allowed in DSO"),
7303 sub);
7304 break;
7305 }
7306
7307 bfd_set_error (bfd_error_nonrepresentable_section);
7308 return false;
7309 }
7310
7311 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
7312 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
7313 return false;
7314 }
7315 s = bfd_get_section_by_name (output_bfd, ".init_array");
7316 if (s != NULL && s->linker_has_input)
7317 {
7318 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
7319 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
7320 return false;
7321 }
7322 s = bfd_get_section_by_name (output_bfd, ".fini_array");
7323 if (s != NULL && s->linker_has_input)
7324 {
7325 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
7326 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
7327 return false;
7328 }
7329
7330 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
7331 /* If .dynstr is excluded from the link, we don't want any of
7332 these tags. Strictly, we should be checking each section
7333 individually; This quick check covers for the case where
7334 someone does a /DISCARD/ : { *(*) }. */
7335 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
7336 {
7337 bfd_size_type strsize;
7338
7339 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7340 if ((info->emit_hash
7341 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
7342 || (info->emit_gnu_hash
7343 && (bed->record_xhash_symbol == NULL
7344 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)))
7345 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
7346 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
7347 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
7348 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
7349 bed->s->sizeof_sym)
7350 || (info->gnu_flags_1
7351 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_FLAGS_1,
7352 info->gnu_flags_1)))
7353 return false;
7354 }
7355 }
7356
7357 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
7358 return false;
7359
7360 /* The backend must work out the sizes of all the other dynamic
7361 sections. */
7362 if (dynobj != NULL
7363 && bed->elf_backend_size_dynamic_sections != NULL
7364 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
7365 return false;
7366
7367 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7368 {
7369 if (elf_tdata (output_bfd)->cverdefs)
7370 {
7371 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
7372
7373 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
7374 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
7375 return false;
7376 }
7377
7378 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
7379 {
7380 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
7381 return false;
7382 }
7383 else if (info->flags & DF_BIND_NOW)
7384 {
7385 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
7386 return false;
7387 }
7388
7389 if (info->flags_1)
7390 {
7391 if (bfd_link_executable (info))
7392 info->flags_1 &= ~ (DF_1_INITFIRST
7393 | DF_1_NODELETE
7394 | DF_1_NOOPEN);
7395 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
7396 return false;
7397 }
7398
7399 if (elf_tdata (output_bfd)->cverrefs)
7400 {
7401 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
7402
7403 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
7404 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
7405 return false;
7406 }
7407
7408 if ((elf_tdata (output_bfd)->cverrefs == 0
7409 && elf_tdata (output_bfd)->cverdefs == 0)
7410 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
7411 {
7412 asection *s;
7413
7414 s = bfd_get_linker_section (dynobj, ".gnu.version");
7415 s->flags |= SEC_EXCLUDE;
7416 }
7417 }
7418 return true;
7419 }
7420
7421 /* Find the first non-excluded output section. We'll use its
7422 section symbol for some emitted relocs. */
7423 void
7424 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7425 {
7426 asection *s;
7427 asection *found = NULL;
7428
7429 for (s = output_bfd->sections; s != NULL; s = s->next)
7430 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7431 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7432 {
7433 found = s;
7434 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7435 break;
7436 }
7437 elf_hash_table (info)->text_index_section = found;
7438 }
7439
7440 /* Find two non-excluded output sections, one for code, one for data.
7441 We'll use their section symbols for some emitted relocs. */
7442 void
7443 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7444 {
7445 asection *s;
7446 asection *found = NULL;
7447
7448 /* Data first, since setting text_index_section changes
7449 _bfd_elf_omit_section_dynsym_default. */
7450 for (s = output_bfd->sections; s != NULL; s = s->next)
7451 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7452 && !(s->flags & SEC_READONLY)
7453 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7454 {
7455 found = s;
7456 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7457 break;
7458 }
7459 elf_hash_table (info)->data_index_section = found;
7460
7461 for (s = output_bfd->sections; s != NULL; s = s->next)
7462 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7463 && (s->flags & SEC_READONLY)
7464 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7465 {
7466 found = s;
7467 break;
7468 }
7469 elf_hash_table (info)->text_index_section = found;
7470 }
7471
7472 #define GNU_HASH_SECTION_NAME(bed) \
7473 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7474
7475 bool
7476 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7477 {
7478 const struct elf_backend_data *bed;
7479 unsigned long section_sym_count;
7480 bfd_size_type dynsymcount = 0;
7481
7482 if (!is_elf_hash_table (info->hash))
7483 return true;
7484
7485 bed = get_elf_backend_data (output_bfd);
7486 (*bed->elf_backend_init_index_section) (output_bfd, info);
7487
7488 /* Assign dynsym indices. In a shared library we generate a section
7489 symbol for each output section, which come first. Next come all
7490 of the back-end allocated local dynamic syms, followed by the rest
7491 of the global symbols.
7492
7493 This is usually not needed for static binaries, however backends
7494 can request to always do it, e.g. the MIPS backend uses dynamic
7495 symbol counts to lay out GOT, which will be produced in the
7496 presence of GOT relocations even in static binaries (holding fixed
7497 data in that case, to satisfy those relocations). */
7498
7499 if (elf_hash_table (info)->dynamic_sections_created
7500 || bed->always_renumber_dynsyms)
7501 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7502 &section_sym_count);
7503
7504 if (elf_hash_table (info)->dynamic_sections_created)
7505 {
7506 bfd *dynobj;
7507 asection *s;
7508 unsigned int dtagcount;
7509
7510 dynobj = elf_hash_table (info)->dynobj;
7511
7512 /* Work out the size of the symbol version section. */
7513 s = bfd_get_linker_section (dynobj, ".gnu.version");
7514 BFD_ASSERT (s != NULL);
7515 if ((s->flags & SEC_EXCLUDE) == 0)
7516 {
7517 s->size = dynsymcount * sizeof (Elf_External_Versym);
7518 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7519 if (s->contents == NULL)
7520 return false;
7521
7522 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7523 return false;
7524 }
7525
7526 /* Set the size of the .dynsym and .hash sections. We counted
7527 the number of dynamic symbols in elf_link_add_object_symbols.
7528 We will build the contents of .dynsym and .hash when we build
7529 the final symbol table, because until then we do not know the
7530 correct value to give the symbols. We built the .dynstr
7531 section as we went along in elf_link_add_object_symbols. */
7532 s = elf_hash_table (info)->dynsym;
7533 BFD_ASSERT (s != NULL);
7534 s->size = dynsymcount * bed->s->sizeof_sym;
7535
7536 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7537 if (s->contents == NULL)
7538 return false;
7539
7540 /* The first entry in .dynsym is a dummy symbol. Clear all the
7541 section syms, in case we don't output them all. */
7542 ++section_sym_count;
7543 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
7544
7545 elf_hash_table (info)->bucketcount = 0;
7546
7547 /* Compute the size of the hashing table. As a side effect this
7548 computes the hash values for all the names we export. */
7549 if (info->emit_hash)
7550 {
7551 unsigned long int *hashcodes;
7552 struct hash_codes_info hashinf;
7553 bfd_size_type amt;
7554 unsigned long int nsyms;
7555 size_t bucketcount;
7556 size_t hash_entry_size;
7557
7558 /* Compute the hash values for all exported symbols. At the same
7559 time store the values in an array so that we could use them for
7560 optimizations. */
7561 amt = dynsymcount * sizeof (unsigned long int);
7562 hashcodes = (unsigned long int *) bfd_malloc (amt);
7563 if (hashcodes == NULL)
7564 return false;
7565 hashinf.hashcodes = hashcodes;
7566 hashinf.error = false;
7567
7568 /* Put all hash values in HASHCODES. */
7569 elf_link_hash_traverse (elf_hash_table (info),
7570 elf_collect_hash_codes, &hashinf);
7571 if (hashinf.error)
7572 {
7573 free (hashcodes);
7574 return false;
7575 }
7576
7577 nsyms = hashinf.hashcodes - hashcodes;
7578 bucketcount
7579 = compute_bucket_count (info, hashcodes, nsyms, 0);
7580 free (hashcodes);
7581
7582 if (bucketcount == 0 && nsyms > 0)
7583 return false;
7584
7585 elf_hash_table (info)->bucketcount = bucketcount;
7586
7587 s = bfd_get_linker_section (dynobj, ".hash");
7588 BFD_ASSERT (s != NULL);
7589 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7590 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
7591 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7592 if (s->contents == NULL)
7593 return false;
7594
7595 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7596 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7597 s->contents + hash_entry_size);
7598 }
7599
7600 if (info->emit_gnu_hash)
7601 {
7602 size_t i, cnt;
7603 unsigned char *contents;
7604 struct collect_gnu_hash_codes cinfo;
7605 bfd_size_type amt;
7606 size_t bucketcount;
7607
7608 memset (&cinfo, 0, sizeof (cinfo));
7609
7610 /* Compute the hash values for all exported symbols. At the same
7611 time store the values in an array so that we could use them for
7612 optimizations. */
7613 amt = dynsymcount * 2 * sizeof (unsigned long int);
7614 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
7615 if (cinfo.hashcodes == NULL)
7616 return false;
7617
7618 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7619 cinfo.min_dynindx = -1;
7620 cinfo.output_bfd = output_bfd;
7621 cinfo.bed = bed;
7622
7623 /* Put all hash values in HASHCODES. */
7624 elf_link_hash_traverse (elf_hash_table (info),
7625 elf_collect_gnu_hash_codes, &cinfo);
7626 if (cinfo.error)
7627 {
7628 free (cinfo.hashcodes);
7629 return false;
7630 }
7631
7632 bucketcount
7633 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7634
7635 if (bucketcount == 0)
7636 {
7637 free (cinfo.hashcodes);
7638 return false;
7639 }
7640
7641 s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (bed));
7642 BFD_ASSERT (s != NULL);
7643
7644 if (cinfo.nsyms == 0)
7645 {
7646 /* Empty .gnu.hash or .MIPS.xhash section is special. */
7647 BFD_ASSERT (cinfo.min_dynindx == -1);
7648 free (cinfo.hashcodes);
7649 s->size = 5 * 4 + bed->s->arch_size / 8;
7650 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7651 if (contents == NULL)
7652 return false;
7653 s->contents = contents;
7654 /* 1 empty bucket. */
7655 bfd_put_32 (output_bfd, 1, contents);
7656 /* SYMIDX above the special symbol 0. */
7657 bfd_put_32 (output_bfd, 1, contents + 4);
7658 /* Just one word for bitmask. */
7659 bfd_put_32 (output_bfd, 1, contents + 8);
7660 /* Only hash fn bloom filter. */
7661 bfd_put_32 (output_bfd, 0, contents + 12);
7662 /* No hashes are valid - empty bitmask. */
7663 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7664 /* No hashes in the only bucket. */
7665 bfd_put_32 (output_bfd, 0,
7666 contents + 16 + bed->s->arch_size / 8);
7667 }
7668 else
7669 {
7670 unsigned long int maskwords, maskbitslog2, x;
7671 BFD_ASSERT (cinfo.min_dynindx != -1);
7672
7673 x = cinfo.nsyms;
7674 maskbitslog2 = 1;
7675 while ((x >>= 1) != 0)
7676 ++maskbitslog2;
7677 if (maskbitslog2 < 3)
7678 maskbitslog2 = 5;
7679 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7680 maskbitslog2 = maskbitslog2 + 3;
7681 else
7682 maskbitslog2 = maskbitslog2 + 2;
7683 if (bed->s->arch_size == 64)
7684 {
7685 if (maskbitslog2 == 5)
7686 maskbitslog2 = 6;
7687 cinfo.shift1 = 6;
7688 }
7689 else
7690 cinfo.shift1 = 5;
7691 cinfo.mask = (1 << cinfo.shift1) - 1;
7692 cinfo.shift2 = maskbitslog2;
7693 cinfo.maskbits = 1 << maskbitslog2;
7694 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7695 amt = bucketcount * sizeof (unsigned long int) * 2;
7696 amt += maskwords * sizeof (bfd_vma);
7697 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
7698 if (cinfo.bitmask == NULL)
7699 {
7700 free (cinfo.hashcodes);
7701 return false;
7702 }
7703
7704 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
7705 cinfo.indx = cinfo.counts + bucketcount;
7706 cinfo.symindx = dynsymcount - cinfo.nsyms;
7707 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7708
7709 /* Determine how often each hash bucket is used. */
7710 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7711 for (i = 0; i < cinfo.nsyms; ++i)
7712 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7713
7714 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7715 if (cinfo.counts[i] != 0)
7716 {
7717 cinfo.indx[i] = cnt;
7718 cnt += cinfo.counts[i];
7719 }
7720 BFD_ASSERT (cnt == dynsymcount);
7721 cinfo.bucketcount = bucketcount;
7722 cinfo.local_indx = cinfo.min_dynindx;
7723
7724 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7725 s->size += cinfo.maskbits / 8;
7726 if (bed->record_xhash_symbol != NULL)
7727 s->size += cinfo.nsyms * 4;
7728 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7729 if (contents == NULL)
7730 {
7731 free (cinfo.bitmask);
7732 free (cinfo.hashcodes);
7733 return false;
7734 }
7735
7736 s->contents = contents;
7737 bfd_put_32 (output_bfd, bucketcount, contents);
7738 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7739 bfd_put_32 (output_bfd, maskwords, contents + 8);
7740 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7741 contents += 16 + cinfo.maskbits / 8;
7742
7743 for (i = 0; i < bucketcount; ++i)
7744 {
7745 if (cinfo.counts[i] == 0)
7746 bfd_put_32 (output_bfd, 0, contents);
7747 else
7748 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7749 contents += 4;
7750 }
7751
7752 cinfo.contents = contents;
7753
7754 cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents;
7755 /* Renumber dynamic symbols, if populating .gnu.hash section.
7756 If using .MIPS.xhash, populate the translation table. */
7757 elf_link_hash_traverse (elf_hash_table (info),
7758 elf_gnu_hash_process_symidx, &cinfo);
7759
7760 contents = s->contents + 16;
7761 for (i = 0; i < maskwords; ++i)
7762 {
7763 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7764 contents);
7765 contents += bed->s->arch_size / 8;
7766 }
7767
7768 free (cinfo.bitmask);
7769 free (cinfo.hashcodes);
7770 }
7771 }
7772
7773 s = bfd_get_linker_section (dynobj, ".dynstr");
7774 BFD_ASSERT (s != NULL);
7775
7776 elf_finalize_dynstr (output_bfd, info);
7777
7778 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7779
7780 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7781 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7782 return false;
7783 }
7784
7785 return true;
7786 }
7787 \f
7788 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7789
7790 static void
7791 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7792 asection *sec)
7793 {
7794 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7795 sec->sec_info_type = SEC_INFO_TYPE_NONE;
7796 }
7797
7798 /* Finish SHF_MERGE section merging. */
7799
7800 bool
7801 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
7802 {
7803 bfd *ibfd;
7804 asection *sec;
7805
7806 if (!is_elf_hash_table (info->hash))
7807 return false;
7808
7809 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7810 if ((ibfd->flags & DYNAMIC) == 0
7811 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
7812 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7813 == get_elf_backend_data (obfd)->s->elfclass))
7814 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7815 if ((sec->flags & SEC_MERGE) != 0
7816 && !bfd_is_abs_section (sec->output_section))
7817 {
7818 struct bfd_elf_section_data *secdata;
7819
7820 secdata = elf_section_data (sec);
7821 if (! _bfd_add_merge_section (obfd,
7822 &elf_hash_table (info)->merge_info,
7823 sec, &secdata->sec_info))
7824 return false;
7825 else if (secdata->sec_info)
7826 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
7827 }
7828
7829 if (elf_hash_table (info)->merge_info != NULL)
7830 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
7831 merge_sections_remove_hook);
7832 return true;
7833 }
7834
7835 /* Create an entry in an ELF linker hash table. */
7836
7837 struct bfd_hash_entry *
7838 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7839 struct bfd_hash_table *table,
7840 const char *string)
7841 {
7842 /* Allocate the structure if it has not already been allocated by a
7843 subclass. */
7844 if (entry == NULL)
7845 {
7846 entry = (struct bfd_hash_entry *)
7847 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
7848 if (entry == NULL)
7849 return entry;
7850 }
7851
7852 /* Call the allocation method of the superclass. */
7853 entry = _bfd_link_hash_newfunc (entry, table, string);
7854 if (entry != NULL)
7855 {
7856 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7857 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7858
7859 /* Set local fields. */
7860 ret->indx = -1;
7861 ret->dynindx = -1;
7862 ret->got = htab->init_got_refcount;
7863 ret->plt = htab->init_plt_refcount;
7864 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7865 - offsetof (struct elf_link_hash_entry, size)));
7866 /* Assume that we have been called by a non-ELF symbol reader.
7867 This flag is then reset by the code which reads an ELF input
7868 file. This ensures that a symbol created by a non-ELF symbol
7869 reader will have the flag set correctly. */
7870 ret->non_elf = 1;
7871 }
7872
7873 return entry;
7874 }
7875
7876 /* Copy data from an indirect symbol to its direct symbol, hiding the
7877 old indirect symbol. Also used for copying flags to a weakdef. */
7878
7879 void
7880 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7881 struct elf_link_hash_entry *dir,
7882 struct elf_link_hash_entry *ind)
7883 {
7884 struct elf_link_hash_table *htab;
7885
7886 if (ind->dyn_relocs != NULL)
7887 {
7888 if (dir->dyn_relocs != NULL)
7889 {
7890 struct elf_dyn_relocs **pp;
7891 struct elf_dyn_relocs *p;
7892
7893 /* Add reloc counts against the indirect sym to the direct sym
7894 list. Merge any entries against the same section. */
7895 for (pp = &ind->dyn_relocs; (p = *pp) != NULL; )
7896 {
7897 struct elf_dyn_relocs *q;
7898
7899 for (q = dir->dyn_relocs; q != NULL; q = q->next)
7900 if (q->sec == p->sec)
7901 {
7902 q->pc_count += p->pc_count;
7903 q->count += p->count;
7904 *pp = p->next;
7905 break;
7906 }
7907 if (q == NULL)
7908 pp = &p->next;
7909 }
7910 *pp = dir->dyn_relocs;
7911 }
7912
7913 dir->dyn_relocs = ind->dyn_relocs;
7914 ind->dyn_relocs = NULL;
7915 }
7916
7917 /* Copy down any references that we may have already seen to the
7918 symbol which just became indirect. */
7919
7920 if (dir->versioned != versioned_hidden)
7921 dir->ref_dynamic |= ind->ref_dynamic;
7922 dir->ref_regular |= ind->ref_regular;
7923 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7924 dir->non_got_ref |= ind->non_got_ref;
7925 dir->needs_plt |= ind->needs_plt;
7926 dir->pointer_equality_needed |= ind->pointer_equality_needed;
7927
7928 if (ind->root.type != bfd_link_hash_indirect)
7929 return;
7930
7931 /* Copy over the global and procedure linkage table refcount entries.
7932 These may have been already set up by a check_relocs routine. */
7933 htab = elf_hash_table (info);
7934 if (ind->got.refcount > htab->init_got_refcount.refcount)
7935 {
7936 if (dir->got.refcount < 0)
7937 dir->got.refcount = 0;
7938 dir->got.refcount += ind->got.refcount;
7939 ind->got.refcount = htab->init_got_refcount.refcount;
7940 }
7941
7942 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7943 {
7944 if (dir->plt.refcount < 0)
7945 dir->plt.refcount = 0;
7946 dir->plt.refcount += ind->plt.refcount;
7947 ind->plt.refcount = htab->init_plt_refcount.refcount;
7948 }
7949
7950 if (ind->dynindx != -1)
7951 {
7952 if (dir->dynindx != -1)
7953 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7954 dir->dynindx = ind->dynindx;
7955 dir->dynstr_index = ind->dynstr_index;
7956 ind->dynindx = -1;
7957 ind->dynstr_index = 0;
7958 }
7959 }
7960
7961 void
7962 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7963 struct elf_link_hash_entry *h,
7964 bool force_local)
7965 {
7966 /* STT_GNU_IFUNC symbol must go through PLT. */
7967 if (h->type != STT_GNU_IFUNC)
7968 {
7969 h->plt = elf_hash_table (info)->init_plt_offset;
7970 h->needs_plt = 0;
7971 }
7972 if (force_local)
7973 {
7974 h->forced_local = 1;
7975 if (h->dynindx != -1)
7976 {
7977 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7978 h->dynstr_index);
7979 h->dynindx = -1;
7980 h->dynstr_index = 0;
7981 }
7982 }
7983 }
7984
7985 /* Hide a symbol. */
7986
7987 void
7988 _bfd_elf_link_hide_symbol (bfd *output_bfd,
7989 struct bfd_link_info *info,
7990 struct bfd_link_hash_entry *h)
7991 {
7992 if (is_elf_hash_table (info->hash))
7993 {
7994 const struct elf_backend_data *bed
7995 = get_elf_backend_data (output_bfd);
7996 struct elf_link_hash_entry *eh
7997 = (struct elf_link_hash_entry *) h;
7998 bed->elf_backend_hide_symbol (info, eh, true);
7999 eh->def_dynamic = 0;
8000 eh->ref_dynamic = 0;
8001 eh->dynamic_def = 0;
8002 }
8003 }
8004
8005 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
8006 caller. */
8007
8008 bool
8009 _bfd_elf_link_hash_table_init
8010 (struct elf_link_hash_table *table,
8011 bfd *abfd,
8012 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
8013 struct bfd_hash_table *,
8014 const char *),
8015 unsigned int entsize,
8016 enum elf_target_id target_id)
8017 {
8018 bool ret;
8019 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
8020
8021 table->init_got_refcount.refcount = can_refcount - 1;
8022 table->init_plt_refcount.refcount = can_refcount - 1;
8023 table->init_got_offset.offset = -(bfd_vma) 1;
8024 table->init_plt_offset.offset = -(bfd_vma) 1;
8025 /* The first dynamic symbol is a dummy. */
8026 table->dynsymcount = 1;
8027
8028 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
8029
8030 table->root.type = bfd_link_elf_hash_table;
8031 table->hash_table_id = target_id;
8032 table->target_os = get_elf_backend_data (abfd)->target_os;
8033
8034 return ret;
8035 }
8036
8037 /* Create an ELF linker hash table. */
8038
8039 struct bfd_link_hash_table *
8040 _bfd_elf_link_hash_table_create (bfd *abfd)
8041 {
8042 struct elf_link_hash_table *ret;
8043 size_t amt = sizeof (struct elf_link_hash_table);
8044
8045 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
8046 if (ret == NULL)
8047 return NULL;
8048
8049 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
8050 sizeof (struct elf_link_hash_entry),
8051 GENERIC_ELF_DATA))
8052 {
8053 free (ret);
8054 return NULL;
8055 }
8056 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
8057
8058 return &ret->root;
8059 }
8060
8061 /* Destroy an ELF linker hash table. */
8062
8063 void
8064 _bfd_elf_link_hash_table_free (bfd *obfd)
8065 {
8066 struct elf_link_hash_table *htab;
8067
8068 htab = (struct elf_link_hash_table *) obfd->link.hash;
8069 if (htab->dynstr != NULL)
8070 _bfd_elf_strtab_free (htab->dynstr);
8071 _bfd_merge_sections_free (htab->merge_info);
8072 _bfd_generic_link_hash_table_free (obfd);
8073 }
8074
8075 /* This is a hook for the ELF emulation code in the generic linker to
8076 tell the backend linker what file name to use for the DT_NEEDED
8077 entry for a dynamic object. */
8078
8079 void
8080 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
8081 {
8082 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8083 && bfd_get_format (abfd) == bfd_object)
8084 elf_dt_name (abfd) = name;
8085 }
8086
8087 int
8088 bfd_elf_get_dyn_lib_class (bfd *abfd)
8089 {
8090 int lib_class;
8091 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8092 && bfd_get_format (abfd) == bfd_object)
8093 lib_class = elf_dyn_lib_class (abfd);
8094 else
8095 lib_class = 0;
8096 return lib_class;
8097 }
8098
8099 void
8100 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
8101 {
8102 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8103 && bfd_get_format (abfd) == bfd_object)
8104 elf_dyn_lib_class (abfd) = lib_class;
8105 }
8106
8107 /* Get the list of DT_NEEDED entries for a link. This is a hook for
8108 the linker ELF emulation code. */
8109
8110 struct bfd_link_needed_list *
8111 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
8112 struct bfd_link_info *info)
8113 {
8114 if (! is_elf_hash_table (info->hash))
8115 return NULL;
8116 return elf_hash_table (info)->needed;
8117 }
8118
8119 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
8120 hook for the linker ELF emulation code. */
8121
8122 struct bfd_link_needed_list *
8123 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
8124 struct bfd_link_info *info)
8125 {
8126 if (! is_elf_hash_table (info->hash))
8127 return NULL;
8128 return elf_hash_table (info)->runpath;
8129 }
8130
8131 /* Get the name actually used for a dynamic object for a link. This
8132 is the SONAME entry if there is one. Otherwise, it is the string
8133 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
8134
8135 const char *
8136 bfd_elf_get_dt_soname (bfd *abfd)
8137 {
8138 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8139 && bfd_get_format (abfd) == bfd_object)
8140 return elf_dt_name (abfd);
8141 return NULL;
8142 }
8143
8144 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
8145 the ELF linker emulation code. */
8146
8147 bool
8148 bfd_elf_get_bfd_needed_list (bfd *abfd,
8149 struct bfd_link_needed_list **pneeded)
8150 {
8151 asection *s;
8152 bfd_byte *dynbuf = NULL;
8153 unsigned int elfsec;
8154 unsigned long shlink;
8155 bfd_byte *extdyn, *extdynend;
8156 size_t extdynsize;
8157 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
8158
8159 *pneeded = NULL;
8160
8161 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
8162 || bfd_get_format (abfd) != bfd_object)
8163 return true;
8164
8165 s = bfd_get_section_by_name (abfd, ".dynamic");
8166 if (s == NULL || s->size == 0)
8167 return true;
8168
8169 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
8170 goto error_return;
8171
8172 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
8173 if (elfsec == SHN_BAD)
8174 goto error_return;
8175
8176 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
8177
8178 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
8179 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
8180
8181 extdyn = dynbuf;
8182 extdynend = extdyn + s->size;
8183 for (; extdyn < extdynend; extdyn += extdynsize)
8184 {
8185 Elf_Internal_Dyn dyn;
8186
8187 (*swap_dyn_in) (abfd, extdyn, &dyn);
8188
8189 if (dyn.d_tag == DT_NULL)
8190 break;
8191
8192 if (dyn.d_tag == DT_NEEDED)
8193 {
8194 const char *string;
8195 struct bfd_link_needed_list *l;
8196 unsigned int tagv = dyn.d_un.d_val;
8197 size_t amt;
8198
8199 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
8200 if (string == NULL)
8201 goto error_return;
8202
8203 amt = sizeof *l;
8204 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
8205 if (l == NULL)
8206 goto error_return;
8207
8208 l->by = abfd;
8209 l->name = string;
8210 l->next = *pneeded;
8211 *pneeded = l;
8212 }
8213 }
8214
8215 free (dynbuf);
8216
8217 return true;
8218
8219 error_return:
8220 free (dynbuf);
8221 return false;
8222 }
8223
8224 struct elf_symbuf_symbol
8225 {
8226 unsigned long st_name; /* Symbol name, index in string tbl */
8227 unsigned char st_info; /* Type and binding attributes */
8228 unsigned char st_other; /* Visibilty, and target specific */
8229 };
8230
8231 struct elf_symbuf_head
8232 {
8233 struct elf_symbuf_symbol *ssym;
8234 size_t count;
8235 unsigned int st_shndx;
8236 };
8237
8238 struct elf_symbol
8239 {
8240 union
8241 {
8242 Elf_Internal_Sym *isym;
8243 struct elf_symbuf_symbol *ssym;
8244 void *p;
8245 } u;
8246 const char *name;
8247 };
8248
8249 /* Sort references to symbols by ascending section number. */
8250
8251 static int
8252 elf_sort_elf_symbol (const void *arg1, const void *arg2)
8253 {
8254 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
8255 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
8256
8257 if (s1->st_shndx != s2->st_shndx)
8258 return s1->st_shndx > s2->st_shndx ? 1 : -1;
8259 /* Final sort by the address of the sym in the symbuf ensures
8260 a stable sort. */
8261 if (s1 != s2)
8262 return s1 > s2 ? 1 : -1;
8263 return 0;
8264 }
8265
8266 static int
8267 elf_sym_name_compare (const void *arg1, const void *arg2)
8268 {
8269 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
8270 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
8271 int ret = strcmp (s1->name, s2->name);
8272 if (ret != 0)
8273 return ret;
8274 if (s1->u.p != s2->u.p)
8275 return s1->u.p > s2->u.p ? 1 : -1;
8276 return 0;
8277 }
8278
8279 static struct elf_symbuf_head *
8280 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
8281 {
8282 Elf_Internal_Sym **ind, **indbufend, **indbuf;
8283 struct elf_symbuf_symbol *ssym;
8284 struct elf_symbuf_head *ssymbuf, *ssymhead;
8285 size_t i, shndx_count, total_size, amt;
8286
8287 amt = symcount * sizeof (*indbuf);
8288 indbuf = (Elf_Internal_Sym **) bfd_malloc (amt);
8289 if (indbuf == NULL)
8290 return NULL;
8291
8292 for (ind = indbuf, i = 0; i < symcount; i++)
8293 if (isymbuf[i].st_shndx != SHN_UNDEF)
8294 *ind++ = &isymbuf[i];
8295 indbufend = ind;
8296
8297 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
8298 elf_sort_elf_symbol);
8299
8300 shndx_count = 0;
8301 if (indbufend > indbuf)
8302 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
8303 if (ind[0]->st_shndx != ind[1]->st_shndx)
8304 shndx_count++;
8305
8306 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
8307 + (indbufend - indbuf) * sizeof (*ssym));
8308 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
8309 if (ssymbuf == NULL)
8310 {
8311 free (indbuf);
8312 return NULL;
8313 }
8314
8315 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
8316 ssymbuf->ssym = NULL;
8317 ssymbuf->count = shndx_count;
8318 ssymbuf->st_shndx = 0;
8319 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
8320 {
8321 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
8322 {
8323 ssymhead++;
8324 ssymhead->ssym = ssym;
8325 ssymhead->count = 0;
8326 ssymhead->st_shndx = (*ind)->st_shndx;
8327 }
8328 ssym->st_name = (*ind)->st_name;
8329 ssym->st_info = (*ind)->st_info;
8330 ssym->st_other = (*ind)->st_other;
8331 ssymhead->count++;
8332 }
8333 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
8334 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
8335 == total_size));
8336
8337 free (indbuf);
8338 return ssymbuf;
8339 }
8340
8341 /* Check if 2 sections define the same set of local and global
8342 symbols. */
8343
8344 static bool
8345 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
8346 struct bfd_link_info *info)
8347 {
8348 bfd *bfd1, *bfd2;
8349 const struct elf_backend_data *bed1, *bed2;
8350 Elf_Internal_Shdr *hdr1, *hdr2;
8351 size_t symcount1, symcount2;
8352 Elf_Internal_Sym *isymbuf1, *isymbuf2;
8353 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
8354 Elf_Internal_Sym *isym, *isymend;
8355 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
8356 size_t count1, count2, sec_count1, sec_count2, i;
8357 unsigned int shndx1, shndx2;
8358 bool result;
8359 bool ignore_section_symbol_p;
8360
8361 bfd1 = sec1->owner;
8362 bfd2 = sec2->owner;
8363
8364 /* Both sections have to be in ELF. */
8365 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
8366 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
8367 return false;
8368
8369 if (elf_section_type (sec1) != elf_section_type (sec2))
8370 return false;
8371
8372 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
8373 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
8374 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
8375 return false;
8376
8377 bed1 = get_elf_backend_data (bfd1);
8378 bed2 = get_elf_backend_data (bfd2);
8379 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
8380 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
8381 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
8382 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
8383
8384 if (symcount1 == 0 || symcount2 == 0)
8385 return false;
8386
8387 result = false;
8388 isymbuf1 = NULL;
8389 isymbuf2 = NULL;
8390 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
8391 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
8392
8393 /* Ignore section symbols only when matching non-debugging sections
8394 or linkonce section with comdat section. */
8395 ignore_section_symbol_p
8396 = ((sec1->flags & SEC_DEBUGGING) == 0
8397 || ((elf_section_flags (sec1) & SHF_GROUP)
8398 != (elf_section_flags (sec2) & SHF_GROUP)));
8399
8400 if (ssymbuf1 == NULL)
8401 {
8402 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
8403 NULL, NULL, NULL);
8404 if (isymbuf1 == NULL)
8405 goto done;
8406
8407 if (info != NULL && !info->reduce_memory_overheads)
8408 {
8409 ssymbuf1 = elf_create_symbuf (symcount1, isymbuf1);
8410 elf_tdata (bfd1)->symbuf = ssymbuf1;
8411 }
8412 }
8413
8414 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
8415 {
8416 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
8417 NULL, NULL, NULL);
8418 if (isymbuf2 == NULL)
8419 goto done;
8420
8421 if (ssymbuf1 != NULL && info != NULL && !info->reduce_memory_overheads)
8422 {
8423 ssymbuf2 = elf_create_symbuf (symcount2, isymbuf2);
8424 elf_tdata (bfd2)->symbuf = ssymbuf2;
8425 }
8426 }
8427
8428 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
8429 {
8430 /* Optimized faster version. */
8431 size_t lo, hi, mid;
8432 struct elf_symbol *symp;
8433 struct elf_symbuf_symbol *ssym, *ssymend;
8434
8435 lo = 0;
8436 hi = ssymbuf1->count;
8437 ssymbuf1++;
8438 count1 = 0;
8439 sec_count1 = 0;
8440 while (lo < hi)
8441 {
8442 mid = (lo + hi) / 2;
8443 if (shndx1 < ssymbuf1[mid].st_shndx)
8444 hi = mid;
8445 else if (shndx1 > ssymbuf1[mid].st_shndx)
8446 lo = mid + 1;
8447 else
8448 {
8449 count1 = ssymbuf1[mid].count;
8450 ssymbuf1 += mid;
8451 break;
8452 }
8453 }
8454 if (ignore_section_symbol_p)
8455 {
8456 for (i = 0; i < count1; i++)
8457 if (ELF_ST_TYPE (ssymbuf1->ssym[i].st_info) == STT_SECTION)
8458 sec_count1++;
8459 count1 -= sec_count1;
8460 }
8461
8462 lo = 0;
8463 hi = ssymbuf2->count;
8464 ssymbuf2++;
8465 count2 = 0;
8466 sec_count2 = 0;
8467 while (lo < hi)
8468 {
8469 mid = (lo + hi) / 2;
8470 if (shndx2 < ssymbuf2[mid].st_shndx)
8471 hi = mid;
8472 else if (shndx2 > ssymbuf2[mid].st_shndx)
8473 lo = mid + 1;
8474 else
8475 {
8476 count2 = ssymbuf2[mid].count;
8477 ssymbuf2 += mid;
8478 break;
8479 }
8480 }
8481 if (ignore_section_symbol_p)
8482 {
8483 for (i = 0; i < count2; i++)
8484 if (ELF_ST_TYPE (ssymbuf2->ssym[i].st_info) == STT_SECTION)
8485 sec_count2++;
8486 count2 -= sec_count2;
8487 }
8488
8489 if (count1 == 0 || count2 == 0 || count1 != count2)
8490 goto done;
8491
8492 symtable1
8493 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
8494 symtable2
8495 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
8496 if (symtable1 == NULL || symtable2 == NULL)
8497 goto done;
8498
8499 symp = symtable1;
8500 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1 + sec_count1;
8501 ssym < ssymend; ssym++)
8502 if (sec_count1 == 0
8503 || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
8504 {
8505 symp->u.ssym = ssym;
8506 symp->name = bfd_elf_string_from_elf_section (bfd1,
8507 hdr1->sh_link,
8508 ssym->st_name);
8509 symp++;
8510 }
8511
8512 symp = symtable2;
8513 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2 + sec_count2;
8514 ssym < ssymend; ssym++)
8515 if (sec_count2 == 0
8516 || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
8517 {
8518 symp->u.ssym = ssym;
8519 symp->name = bfd_elf_string_from_elf_section (bfd2,
8520 hdr2->sh_link,
8521 ssym->st_name);
8522 symp++;
8523 }
8524
8525 /* Sort symbol by name. */
8526 qsort (symtable1, count1, sizeof (struct elf_symbol),
8527 elf_sym_name_compare);
8528 qsort (symtable2, count1, sizeof (struct elf_symbol),
8529 elf_sym_name_compare);
8530
8531 for (i = 0; i < count1; i++)
8532 /* Two symbols must have the same binding, type and name. */
8533 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8534 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8535 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8536 goto done;
8537
8538 result = true;
8539 goto done;
8540 }
8541
8542 symtable1 = (struct elf_symbol *)
8543 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8544 symtable2 = (struct elf_symbol *)
8545 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
8546 if (symtable1 == NULL || symtable2 == NULL)
8547 goto done;
8548
8549 /* Count definitions in the section. */
8550 count1 = 0;
8551 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
8552 if (isym->st_shndx == shndx1
8553 && (!ignore_section_symbol_p
8554 || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
8555 symtable1[count1++].u.isym = isym;
8556
8557 count2 = 0;
8558 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
8559 if (isym->st_shndx == shndx2
8560 && (!ignore_section_symbol_p
8561 || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
8562 symtable2[count2++].u.isym = isym;
8563
8564 if (count1 == 0 || count2 == 0 || count1 != count2)
8565 goto done;
8566
8567 for (i = 0; i < count1; i++)
8568 symtable1[i].name
8569 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8570 symtable1[i].u.isym->st_name);
8571
8572 for (i = 0; i < count2; i++)
8573 symtable2[i].name
8574 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8575 symtable2[i].u.isym->st_name);
8576
8577 /* Sort symbol by name. */
8578 qsort (symtable1, count1, sizeof (struct elf_symbol),
8579 elf_sym_name_compare);
8580 qsort (symtable2, count1, sizeof (struct elf_symbol),
8581 elf_sym_name_compare);
8582
8583 for (i = 0; i < count1; i++)
8584 /* Two symbols must have the same binding, type and name. */
8585 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8586 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8587 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8588 goto done;
8589
8590 result = true;
8591
8592 done:
8593 free (symtable1);
8594 free (symtable2);
8595 free (isymbuf1);
8596 free (isymbuf2);
8597
8598 return result;
8599 }
8600
8601 /* Return TRUE if 2 section types are compatible. */
8602
8603 bool
8604 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8605 bfd *bbfd, const asection *bsec)
8606 {
8607 if (asec == NULL
8608 || bsec == NULL
8609 || abfd->xvec->flavour != bfd_target_elf_flavour
8610 || bbfd->xvec->flavour != bfd_target_elf_flavour)
8611 return true;
8612
8613 return elf_section_type (asec) == elf_section_type (bsec);
8614 }
8615 \f
8616 /* Final phase of ELF linker. */
8617
8618 /* A structure we use to avoid passing large numbers of arguments. */
8619
8620 struct elf_final_link_info
8621 {
8622 /* General link information. */
8623 struct bfd_link_info *info;
8624 /* Output BFD. */
8625 bfd *output_bfd;
8626 /* Symbol string table. */
8627 struct elf_strtab_hash *symstrtab;
8628 /* .hash section. */
8629 asection *hash_sec;
8630 /* symbol version section (.gnu.version). */
8631 asection *symver_sec;
8632 /* Buffer large enough to hold contents of any section. */
8633 bfd_byte *contents;
8634 /* Buffer large enough to hold external relocs of any section. */
8635 void *external_relocs;
8636 /* Buffer large enough to hold internal relocs of any section. */
8637 Elf_Internal_Rela *internal_relocs;
8638 /* Buffer large enough to hold external local symbols of any input
8639 BFD. */
8640 bfd_byte *external_syms;
8641 /* And a buffer for symbol section indices. */
8642 Elf_External_Sym_Shndx *locsym_shndx;
8643 /* Buffer large enough to hold internal local symbols of any input
8644 BFD. */
8645 Elf_Internal_Sym *internal_syms;
8646 /* Array large enough to hold a symbol index for each local symbol
8647 of any input BFD. */
8648 long *indices;
8649 /* Array large enough to hold a section pointer for each local
8650 symbol of any input BFD. */
8651 asection **sections;
8652 /* Buffer for SHT_SYMTAB_SHNDX section. */
8653 Elf_External_Sym_Shndx *symshndxbuf;
8654 /* Number of STT_FILE syms seen. */
8655 size_t filesym_count;
8656 /* Local symbol hash table. */
8657 struct bfd_hash_table local_hash_table;
8658 };
8659
8660 struct local_hash_entry
8661 {
8662 /* Base hash table entry structure. */
8663 struct bfd_hash_entry root;
8664 /* Size of the local symbol name. */
8665 size_t size;
8666 /* Number of the duplicated local symbol names. */
8667 long count;
8668 };
8669
8670 /* Create an entry in the local symbol hash table. */
8671
8672 static struct bfd_hash_entry *
8673 local_hash_newfunc (struct bfd_hash_entry *entry,
8674 struct bfd_hash_table *table,
8675 const char *string)
8676 {
8677
8678 /* Allocate the structure if it has not already been allocated by a
8679 subclass. */
8680 if (entry == NULL)
8681 {
8682 entry = bfd_hash_allocate (table,
8683 sizeof (struct local_hash_entry));
8684 if (entry == NULL)
8685 return entry;
8686 }
8687
8688 /* Call the allocation method of the superclass. */
8689 entry = bfd_hash_newfunc (entry, table, string);
8690 if (entry != NULL)
8691 {
8692 ((struct local_hash_entry *) entry)->count = 0;
8693 ((struct local_hash_entry *) entry)->size = 0;
8694 }
8695
8696 return entry;
8697 }
8698
8699 /* This struct is used to pass information to elf_link_output_extsym. */
8700
8701 struct elf_outext_info
8702 {
8703 bool failed;
8704 bool localsyms;
8705 bool file_sym_done;
8706 struct elf_final_link_info *flinfo;
8707 };
8708
8709
8710 /* Support for evaluating a complex relocation.
8711
8712 Complex relocations are generalized, self-describing relocations. The
8713 implementation of them consists of two parts: complex symbols, and the
8714 relocations themselves.
8715
8716 The relocations use a reserved elf-wide relocation type code (R_RELC
8717 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8718 information (start bit, end bit, word width, etc) into the addend. This
8719 information is extracted from CGEN-generated operand tables within gas.
8720
8721 Complex symbols are mangled symbols (STT_RELC external / BSF_RELC
8722 internal) representing prefix-notation expressions, including but not
8723 limited to those sorts of expressions normally encoded as addends in the
8724 addend field. The symbol mangling format is:
8725
8726 <node> := <literal>
8727 | <unary-operator> ':' <node>
8728 | <binary-operator> ':' <node> ':' <node>
8729 ;
8730
8731 <literal> := 's' <digits=N> ':' <N character symbol name>
8732 | 'S' <digits=N> ':' <N character section name>
8733 | '#' <hexdigits>
8734 ;
8735
8736 <binary-operator> := as in C
8737 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8738
8739 static void
8740 set_symbol_value (bfd *bfd_with_globals,
8741 Elf_Internal_Sym *isymbuf,
8742 size_t locsymcount,
8743 size_t symidx,
8744 bfd_vma val)
8745 {
8746 struct elf_link_hash_entry **sym_hashes;
8747 struct elf_link_hash_entry *h;
8748 size_t extsymoff = locsymcount;
8749
8750 if (symidx < locsymcount)
8751 {
8752 Elf_Internal_Sym *sym;
8753
8754 sym = isymbuf + symidx;
8755 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8756 {
8757 /* It is a local symbol: move it to the
8758 "absolute" section and give it a value. */
8759 sym->st_shndx = SHN_ABS;
8760 sym->st_value = val;
8761 return;
8762 }
8763 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8764 extsymoff = 0;
8765 }
8766
8767 /* It is a global symbol: set its link type
8768 to "defined" and give it a value. */
8769
8770 sym_hashes = elf_sym_hashes (bfd_with_globals);
8771 h = sym_hashes [symidx - extsymoff];
8772 while (h->root.type == bfd_link_hash_indirect
8773 || h->root.type == bfd_link_hash_warning)
8774 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8775 h->root.type = bfd_link_hash_defined;
8776 h->root.u.def.value = val;
8777 h->root.u.def.section = bfd_abs_section_ptr;
8778 }
8779
8780 static bool
8781 resolve_symbol (const char *name,
8782 bfd *input_bfd,
8783 struct elf_final_link_info *flinfo,
8784 bfd_vma *result,
8785 Elf_Internal_Sym *isymbuf,
8786 size_t locsymcount)
8787 {
8788 Elf_Internal_Sym *sym;
8789 struct bfd_link_hash_entry *global_entry;
8790 const char *candidate = NULL;
8791 Elf_Internal_Shdr *symtab_hdr;
8792 size_t i;
8793
8794 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8795
8796 for (i = 0; i < locsymcount; ++ i)
8797 {
8798 sym = isymbuf + i;
8799
8800 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8801 continue;
8802
8803 candidate = bfd_elf_string_from_elf_section (input_bfd,
8804 symtab_hdr->sh_link,
8805 sym->st_name);
8806 #ifdef DEBUG
8807 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8808 name, candidate, (unsigned long) sym->st_value);
8809 #endif
8810 if (candidate && strcmp (candidate, name) == 0)
8811 {
8812 asection *sec = flinfo->sections [i];
8813
8814 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8815 *result += sec->output_offset + sec->output_section->vma;
8816 #ifdef DEBUG
8817 printf ("Found symbol with value %8.8lx\n",
8818 (unsigned long) *result);
8819 #endif
8820 return true;
8821 }
8822 }
8823
8824 /* Hmm, haven't found it yet. perhaps it is a global. */
8825 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
8826 false, false, true);
8827 if (!global_entry)
8828 return false;
8829
8830 if (global_entry->type == bfd_link_hash_defined
8831 || global_entry->type == bfd_link_hash_defweak)
8832 {
8833 *result = (global_entry->u.def.value
8834 + global_entry->u.def.section->output_section->vma
8835 + global_entry->u.def.section->output_offset);
8836 #ifdef DEBUG
8837 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8838 global_entry->root.string, (unsigned long) *result);
8839 #endif
8840 return true;
8841 }
8842
8843 return false;
8844 }
8845
8846 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8847 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8848 names like "foo.end" which is the end address of section "foo". */
8849
8850 static bool
8851 resolve_section (const char *name,
8852 asection *sections,
8853 bfd_vma *result,
8854 bfd * abfd)
8855 {
8856 asection *curr;
8857 unsigned int len;
8858
8859 for (curr = sections; curr; curr = curr->next)
8860 if (strcmp (curr->name, name) == 0)
8861 {
8862 *result = curr->vma;
8863 return true;
8864 }
8865
8866 /* Hmm. still haven't found it. try pseudo-section names. */
8867 /* FIXME: This could be coded more efficiently... */
8868 for (curr = sections; curr; curr = curr->next)
8869 {
8870 len = strlen (curr->name);
8871 if (len > strlen (name))
8872 continue;
8873
8874 if (strncmp (curr->name, name, len) == 0)
8875 {
8876 if (startswith (name + len, ".end"))
8877 {
8878 *result = (curr->vma
8879 + curr->size / bfd_octets_per_byte (abfd, curr));
8880 return true;
8881 }
8882
8883 /* Insert more pseudo-section names here, if you like. */
8884 }
8885 }
8886
8887 return false;
8888 }
8889
8890 static void
8891 undefined_reference (const char *reftype, const char *name)
8892 {
8893 /* xgettext:c-format */
8894 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8895 reftype, name);
8896 bfd_set_error (bfd_error_bad_value);
8897 }
8898
8899 static bool
8900 eval_symbol (bfd_vma *result,
8901 const char **symp,
8902 bfd *input_bfd,
8903 struct elf_final_link_info *flinfo,
8904 bfd_vma dot,
8905 Elf_Internal_Sym *isymbuf,
8906 size_t locsymcount,
8907 int signed_p)
8908 {
8909 size_t len;
8910 size_t symlen;
8911 bfd_vma a;
8912 bfd_vma b;
8913 char symbuf[4096];
8914 const char *sym = *symp;
8915 const char *symend;
8916 bool symbol_is_section = false;
8917
8918 len = strlen (sym);
8919 symend = sym + len;
8920
8921 if (len < 1 || len > sizeof (symbuf))
8922 {
8923 bfd_set_error (bfd_error_invalid_operation);
8924 return false;
8925 }
8926
8927 switch (* sym)
8928 {
8929 case '.':
8930 *result = dot;
8931 *symp = sym + 1;
8932 return true;
8933
8934 case '#':
8935 ++sym;
8936 *result = strtoul (sym, (char **) symp, 16);
8937 return true;
8938
8939 case 'S':
8940 symbol_is_section = true;
8941 /* Fall through. */
8942 case 's':
8943 ++sym;
8944 symlen = strtol (sym, (char **) symp, 10);
8945 sym = *symp + 1; /* Skip the trailing ':'. */
8946
8947 if (symend < sym || symlen + 1 > sizeof (symbuf))
8948 {
8949 bfd_set_error (bfd_error_invalid_operation);
8950 return false;
8951 }
8952
8953 memcpy (symbuf, sym, symlen);
8954 symbuf[symlen] = '\0';
8955 *symp = sym + symlen;
8956
8957 /* Is it always possible, with complex symbols, that gas "mis-guessed"
8958 the symbol as a section, or vice-versa. so we're pretty liberal in our
8959 interpretation here; section means "try section first", not "must be a
8960 section", and likewise with symbol. */
8961
8962 if (symbol_is_section)
8963 {
8964 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8965 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8966 isymbuf, locsymcount))
8967 {
8968 undefined_reference ("section", symbuf);
8969 return false;
8970 }
8971 }
8972 else
8973 {
8974 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8975 isymbuf, locsymcount)
8976 && !resolve_section (symbuf, flinfo->output_bfd->sections,
8977 result, input_bfd))
8978 {
8979 undefined_reference ("symbol", symbuf);
8980 return false;
8981 }
8982 }
8983
8984 return true;
8985
8986 /* All that remains are operators. */
8987
8988 #define UNARY_OP(op) \
8989 if (startswith (sym, #op)) \
8990 { \
8991 sym += strlen (#op); \
8992 if (*sym == ':') \
8993 ++sym; \
8994 *symp = sym; \
8995 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8996 isymbuf, locsymcount, signed_p)) \
8997 return false; \
8998 if (signed_p) \
8999 *result = op ((bfd_signed_vma) a); \
9000 else \
9001 *result = op a; \
9002 return true; \
9003 }
9004
9005 #define BINARY_OP_HEAD(op) \
9006 if (startswith (sym, #op)) \
9007 { \
9008 sym += strlen (#op); \
9009 if (*sym == ':') \
9010 ++sym; \
9011 *symp = sym; \
9012 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
9013 isymbuf, locsymcount, signed_p)) \
9014 return false; \
9015 ++*symp; \
9016 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
9017 isymbuf, locsymcount, signed_p)) \
9018 return false;
9019 #define BINARY_OP_TAIL(op) \
9020 if (signed_p) \
9021 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
9022 else \
9023 *result = a op b; \
9024 return true; \
9025 }
9026 #define BINARY_OP(op) BINARY_OP_HEAD(op) BINARY_OP_TAIL(op)
9027
9028 default:
9029 UNARY_OP (0-);
9030 BINARY_OP_HEAD (<<);
9031 if (b >= sizeof (a) * CHAR_BIT)
9032 {
9033 *result = 0;
9034 return true;
9035 }
9036 signed_p = 0;
9037 BINARY_OP_TAIL (<<);
9038 BINARY_OP_HEAD (>>);
9039 if (b >= sizeof (a) * CHAR_BIT)
9040 {
9041 *result = signed_p && (bfd_signed_vma) a < 0 ? -1 : 0;
9042 return true;
9043 }
9044 BINARY_OP_TAIL (>>);
9045 BINARY_OP (==);
9046 BINARY_OP (!=);
9047 BINARY_OP (<=);
9048 BINARY_OP (>=);
9049 BINARY_OP (&&);
9050 BINARY_OP (||);
9051 UNARY_OP (~);
9052 UNARY_OP (!);
9053 BINARY_OP (*);
9054 BINARY_OP_HEAD (/);
9055 if (b == 0)
9056 {
9057 _bfd_error_handler (_("division by zero"));
9058 bfd_set_error (bfd_error_bad_value);
9059 return false;
9060 }
9061 BINARY_OP_TAIL (/);
9062 BINARY_OP_HEAD (%);
9063 if (b == 0)
9064 {
9065 _bfd_error_handler (_("division by zero"));
9066 bfd_set_error (bfd_error_bad_value);
9067 return false;
9068 }
9069 BINARY_OP_TAIL (%);
9070 BINARY_OP (^);
9071 BINARY_OP (|);
9072 BINARY_OP (&);
9073 BINARY_OP (+);
9074 BINARY_OP (-);
9075 BINARY_OP (<);
9076 BINARY_OP (>);
9077 #undef UNARY_OP
9078 #undef BINARY_OP
9079 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
9080 bfd_set_error (bfd_error_invalid_operation);
9081 return false;
9082 }
9083 }
9084
9085 static void
9086 put_value (bfd_vma size,
9087 unsigned long chunksz,
9088 bfd *input_bfd,
9089 bfd_vma x,
9090 bfd_byte *location)
9091 {
9092 location += (size - chunksz);
9093
9094 for (; size; size -= chunksz, location -= chunksz)
9095 {
9096 switch (chunksz)
9097 {
9098 case 1:
9099 bfd_put_8 (input_bfd, x, location);
9100 x >>= 8;
9101 break;
9102 case 2:
9103 bfd_put_16 (input_bfd, x, location);
9104 x >>= 16;
9105 break;
9106 case 4:
9107 bfd_put_32 (input_bfd, x, location);
9108 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
9109 x >>= 16;
9110 x >>= 16;
9111 break;
9112 #ifdef BFD64
9113 case 8:
9114 bfd_put_64 (input_bfd, x, location);
9115 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
9116 x >>= 32;
9117 x >>= 32;
9118 break;
9119 #endif
9120 default:
9121 abort ();
9122 break;
9123 }
9124 }
9125 }
9126
9127 static bfd_vma
9128 get_value (bfd_vma size,
9129 unsigned long chunksz,
9130 bfd *input_bfd,
9131 bfd_byte *location)
9132 {
9133 int shift;
9134 bfd_vma x = 0;
9135
9136 /* Sanity checks. */
9137 BFD_ASSERT (chunksz <= sizeof (x)
9138 && size >= chunksz
9139 && chunksz != 0
9140 && (size % chunksz) == 0
9141 && input_bfd != NULL
9142 && location != NULL);
9143
9144 if (chunksz == sizeof (x))
9145 {
9146 BFD_ASSERT (size == chunksz);
9147
9148 /* Make sure that we do not perform an undefined shift operation.
9149 We know that size == chunksz so there will only be one iteration
9150 of the loop below. */
9151 shift = 0;
9152 }
9153 else
9154 shift = 8 * chunksz;
9155
9156 for (; size; size -= chunksz, location += chunksz)
9157 {
9158 switch (chunksz)
9159 {
9160 case 1:
9161 x = (x << shift) | bfd_get_8 (input_bfd, location);
9162 break;
9163 case 2:
9164 x = (x << shift) | bfd_get_16 (input_bfd, location);
9165 break;
9166 case 4:
9167 x = (x << shift) | bfd_get_32 (input_bfd, location);
9168 break;
9169 #ifdef BFD64
9170 case 8:
9171 x = (x << shift) | bfd_get_64 (input_bfd, location);
9172 break;
9173 #endif
9174 default:
9175 abort ();
9176 }
9177 }
9178 return x;
9179 }
9180
9181 static void
9182 decode_complex_addend (unsigned long *start, /* in bits */
9183 unsigned long *oplen, /* in bits */
9184 unsigned long *len, /* in bits */
9185 unsigned long *wordsz, /* in bytes */
9186 unsigned long *chunksz, /* in bytes */
9187 unsigned long *lsb0_p,
9188 unsigned long *signed_p,
9189 unsigned long *trunc_p,
9190 unsigned long encoded)
9191 {
9192 * start = encoded & 0x3F;
9193 * len = (encoded >> 6) & 0x3F;
9194 * oplen = (encoded >> 12) & 0x3F;
9195 * wordsz = (encoded >> 18) & 0xF;
9196 * chunksz = (encoded >> 22) & 0xF;
9197 * lsb0_p = (encoded >> 27) & 1;
9198 * signed_p = (encoded >> 28) & 1;
9199 * trunc_p = (encoded >> 29) & 1;
9200 }
9201
9202 bfd_reloc_status_type
9203 bfd_elf_perform_complex_relocation (bfd *input_bfd,
9204 asection *input_section,
9205 bfd_byte *contents,
9206 Elf_Internal_Rela *rel,
9207 bfd_vma relocation)
9208 {
9209 bfd_vma shift, x, mask;
9210 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
9211 bfd_reloc_status_type r;
9212 bfd_size_type octets;
9213
9214 /* Perform this reloc, since it is complex.
9215 (this is not to say that it necessarily refers to a complex
9216 symbol; merely that it is a self-describing CGEN based reloc.
9217 i.e. the addend has the complete reloc information (bit start, end,
9218 word size, etc) encoded within it.). */
9219
9220 decode_complex_addend (&start, &oplen, &len, &wordsz,
9221 &chunksz, &lsb0_p, &signed_p,
9222 &trunc_p, rel->r_addend);
9223
9224 mask = (((1L << (len - 1)) - 1) << 1) | 1;
9225
9226 if (lsb0_p)
9227 shift = (start + 1) - len;
9228 else
9229 shift = (8 * wordsz) - (start + len);
9230
9231 octets = rel->r_offset * bfd_octets_per_byte (input_bfd, input_section);
9232 x = get_value (wordsz, chunksz, input_bfd, contents + octets);
9233
9234 #ifdef DEBUG
9235 printf ("Doing complex reloc: "
9236 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
9237 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
9238 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
9239 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9240 oplen, (unsigned long) x, (unsigned long) mask,
9241 (unsigned long) relocation);
9242 #endif
9243
9244 r = bfd_reloc_ok;
9245 if (! trunc_p)
9246 /* Now do an overflow check. */
9247 r = bfd_check_overflow ((signed_p
9248 ? complain_overflow_signed
9249 : complain_overflow_unsigned),
9250 len, 0, (8 * wordsz),
9251 relocation);
9252
9253 /* Do the deed. */
9254 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
9255
9256 #ifdef DEBUG
9257 printf (" relocation: %8.8lx\n"
9258 " shifted mask: %8.8lx\n"
9259 " shifted/masked reloc: %8.8lx\n"
9260 " result: %8.8lx\n",
9261 (unsigned long) relocation, (unsigned long) (mask << shift),
9262 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
9263 #endif
9264 put_value (wordsz, chunksz, input_bfd, x, contents + octets);
9265 return r;
9266 }
9267
9268 /* Functions to read r_offset from external (target order) reloc
9269 entry. Faster than bfd_getl32 et al, because we let the compiler
9270 know the value is aligned. */
9271
9272 static bfd_vma
9273 ext32l_r_offset (const void *p)
9274 {
9275 union aligned32
9276 {
9277 uint32_t v;
9278 unsigned char c[4];
9279 };
9280 const union aligned32 *a
9281 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9282
9283 uint32_t aval = ( (uint32_t) a->c[0]
9284 | (uint32_t) a->c[1] << 8
9285 | (uint32_t) a->c[2] << 16
9286 | (uint32_t) a->c[3] << 24);
9287 return aval;
9288 }
9289
9290 static bfd_vma
9291 ext32b_r_offset (const void *p)
9292 {
9293 union aligned32
9294 {
9295 uint32_t v;
9296 unsigned char c[4];
9297 };
9298 const union aligned32 *a
9299 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9300
9301 uint32_t aval = ( (uint32_t) a->c[0] << 24
9302 | (uint32_t) a->c[1] << 16
9303 | (uint32_t) a->c[2] << 8
9304 | (uint32_t) a->c[3]);
9305 return aval;
9306 }
9307
9308 #ifdef BFD_HOST_64_BIT
9309 static bfd_vma
9310 ext64l_r_offset (const void *p)
9311 {
9312 union aligned64
9313 {
9314 uint64_t v;
9315 unsigned char c[8];
9316 };
9317 const union aligned64 *a
9318 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9319
9320 uint64_t aval = ( (uint64_t) a->c[0]
9321 | (uint64_t) a->c[1] << 8
9322 | (uint64_t) a->c[2] << 16
9323 | (uint64_t) a->c[3] << 24
9324 | (uint64_t) a->c[4] << 32
9325 | (uint64_t) a->c[5] << 40
9326 | (uint64_t) a->c[6] << 48
9327 | (uint64_t) a->c[7] << 56);
9328 return aval;
9329 }
9330
9331 static bfd_vma
9332 ext64b_r_offset (const void *p)
9333 {
9334 union aligned64
9335 {
9336 uint64_t v;
9337 unsigned char c[8];
9338 };
9339 const union aligned64 *a
9340 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9341
9342 uint64_t aval = ( (uint64_t) a->c[0] << 56
9343 | (uint64_t) a->c[1] << 48
9344 | (uint64_t) a->c[2] << 40
9345 | (uint64_t) a->c[3] << 32
9346 | (uint64_t) a->c[4] << 24
9347 | (uint64_t) a->c[5] << 16
9348 | (uint64_t) a->c[6] << 8
9349 | (uint64_t) a->c[7]);
9350 return aval;
9351 }
9352 #endif
9353
9354 /* When performing a relocatable link, the input relocations are
9355 preserved. But, if they reference global symbols, the indices
9356 referenced must be updated. Update all the relocations found in
9357 RELDATA. */
9358
9359 static bool
9360 elf_link_adjust_relocs (bfd *abfd,
9361 asection *sec,
9362 struct bfd_elf_section_reloc_data *reldata,
9363 bool sort,
9364 struct bfd_link_info *info)
9365 {
9366 unsigned int i;
9367 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9368 bfd_byte *erela;
9369 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9370 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9371 bfd_vma r_type_mask;
9372 int r_sym_shift;
9373 unsigned int count = reldata->count;
9374 struct elf_link_hash_entry **rel_hash = reldata->hashes;
9375
9376 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
9377 {
9378 swap_in = bed->s->swap_reloc_in;
9379 swap_out = bed->s->swap_reloc_out;
9380 }
9381 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
9382 {
9383 swap_in = bed->s->swap_reloca_in;
9384 swap_out = bed->s->swap_reloca_out;
9385 }
9386 else
9387 abort ();
9388
9389 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
9390 abort ();
9391
9392 if (bed->s->arch_size == 32)
9393 {
9394 r_type_mask = 0xff;
9395 r_sym_shift = 8;
9396 }
9397 else
9398 {
9399 r_type_mask = 0xffffffff;
9400 r_sym_shift = 32;
9401 }
9402
9403 erela = reldata->hdr->contents;
9404 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
9405 {
9406 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
9407 unsigned int j;
9408
9409 if (*rel_hash == NULL)
9410 continue;
9411
9412 if ((*rel_hash)->indx == -2
9413 && info->gc_sections
9414 && ! info->gc_keep_exported)
9415 {
9416 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9417 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
9418 abfd, sec,
9419 (*rel_hash)->root.root.string);
9420 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
9421 abfd, sec);
9422 bfd_set_error (bfd_error_invalid_operation);
9423 return false;
9424 }
9425 BFD_ASSERT ((*rel_hash)->indx >= 0);
9426
9427 (*swap_in) (abfd, erela, irela);
9428 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
9429 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
9430 | (irela[j].r_info & r_type_mask));
9431 (*swap_out) (abfd, irela, erela);
9432 }
9433
9434 if (bed->elf_backend_update_relocs)
9435 (*bed->elf_backend_update_relocs) (sec, reldata);
9436
9437 if (sort && count != 0)
9438 {
9439 bfd_vma (*ext_r_off) (const void *);
9440 bfd_vma r_off;
9441 size_t elt_size;
9442 bfd_byte *base, *end, *p, *loc;
9443 bfd_byte *buf = NULL;
9444
9445 if (bed->s->arch_size == 32)
9446 {
9447 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9448 ext_r_off = ext32l_r_offset;
9449 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9450 ext_r_off = ext32b_r_offset;
9451 else
9452 abort ();
9453 }
9454 else
9455 {
9456 #ifdef BFD_HOST_64_BIT
9457 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9458 ext_r_off = ext64l_r_offset;
9459 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9460 ext_r_off = ext64b_r_offset;
9461 else
9462 #endif
9463 abort ();
9464 }
9465
9466 /* Must use a stable sort here. A modified insertion sort,
9467 since the relocs are mostly sorted already. */
9468 elt_size = reldata->hdr->sh_entsize;
9469 base = reldata->hdr->contents;
9470 end = base + count * elt_size;
9471 if (elt_size > sizeof (Elf64_External_Rela))
9472 abort ();
9473
9474 /* Ensure the first element is lowest. This acts as a sentinel,
9475 speeding the main loop below. */
9476 r_off = (*ext_r_off) (base);
9477 for (p = loc = base; (p += elt_size) < end; )
9478 {
9479 bfd_vma r_off2 = (*ext_r_off) (p);
9480 if (r_off > r_off2)
9481 {
9482 r_off = r_off2;
9483 loc = p;
9484 }
9485 }
9486 if (loc != base)
9487 {
9488 /* Don't just swap *base and *loc as that changes the order
9489 of the original base[0] and base[1] if they happen to
9490 have the same r_offset. */
9491 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
9492 memcpy (onebuf, loc, elt_size);
9493 memmove (base + elt_size, base, loc - base);
9494 memcpy (base, onebuf, elt_size);
9495 }
9496
9497 for (p = base + elt_size; (p += elt_size) < end; )
9498 {
9499 /* base to p is sorted, *p is next to insert. */
9500 r_off = (*ext_r_off) (p);
9501 /* Search the sorted region for location to insert. */
9502 loc = p - elt_size;
9503 while (r_off < (*ext_r_off) (loc))
9504 loc -= elt_size;
9505 loc += elt_size;
9506 if (loc != p)
9507 {
9508 /* Chances are there is a run of relocs to insert here,
9509 from one of more input files. Files are not always
9510 linked in order due to the way elf_link_input_bfd is
9511 called. See pr17666. */
9512 size_t sortlen = p - loc;
9513 bfd_vma r_off2 = (*ext_r_off) (loc);
9514 size_t runlen = elt_size;
9515 size_t buf_size = 96 * 1024;
9516 while (p + runlen < end
9517 && (sortlen <= buf_size
9518 || runlen + elt_size <= buf_size)
9519 && r_off2 > (*ext_r_off) (p + runlen))
9520 runlen += elt_size;
9521 if (buf == NULL)
9522 {
9523 buf = bfd_malloc (buf_size);
9524 if (buf == NULL)
9525 return false;
9526 }
9527 if (runlen < sortlen)
9528 {
9529 memcpy (buf, p, runlen);
9530 memmove (loc + runlen, loc, sortlen);
9531 memcpy (loc, buf, runlen);
9532 }
9533 else
9534 {
9535 memcpy (buf, loc, sortlen);
9536 memmove (loc, p, runlen);
9537 memcpy (loc + runlen, buf, sortlen);
9538 }
9539 p += runlen - elt_size;
9540 }
9541 }
9542 /* Hashes are no longer valid. */
9543 free (reldata->hashes);
9544 reldata->hashes = NULL;
9545 free (buf);
9546 }
9547 return true;
9548 }
9549
9550 struct elf_link_sort_rela
9551 {
9552 union {
9553 bfd_vma offset;
9554 bfd_vma sym_mask;
9555 } u;
9556 enum elf_reloc_type_class type;
9557 /* We use this as an array of size int_rels_per_ext_rel. */
9558 Elf_Internal_Rela rela[1];
9559 };
9560
9561 /* qsort stability here and for cmp2 is only an issue if multiple
9562 dynamic relocations are emitted at the same address. But targets
9563 that apply a series of dynamic relocations each operating on the
9564 result of the prior relocation can't use -z combreloc as
9565 implemented anyway. Such schemes tend to be broken by sorting on
9566 symbol index. That leaves dynamic NONE relocs as the only other
9567 case where ld might emit multiple relocs at the same address, and
9568 those are only emitted due to target bugs. */
9569
9570 static int
9571 elf_link_sort_cmp1 (const void *A, const void *B)
9572 {
9573 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9574 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9575 int relativea, relativeb;
9576
9577 relativea = a->type == reloc_class_relative;
9578 relativeb = b->type == reloc_class_relative;
9579
9580 if (relativea < relativeb)
9581 return 1;
9582 if (relativea > relativeb)
9583 return -1;
9584 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
9585 return -1;
9586 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
9587 return 1;
9588 if (a->rela->r_offset < b->rela->r_offset)
9589 return -1;
9590 if (a->rela->r_offset > b->rela->r_offset)
9591 return 1;
9592 return 0;
9593 }
9594
9595 static int
9596 elf_link_sort_cmp2 (const void *A, const void *B)
9597 {
9598 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9599 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9600
9601 if (a->type < b->type)
9602 return -1;
9603 if (a->type > b->type)
9604 return 1;
9605 if (a->u.offset < b->u.offset)
9606 return -1;
9607 if (a->u.offset > b->u.offset)
9608 return 1;
9609 if (a->rela->r_offset < b->rela->r_offset)
9610 return -1;
9611 if (a->rela->r_offset > b->rela->r_offset)
9612 return 1;
9613 return 0;
9614 }
9615
9616 static size_t
9617 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9618 {
9619 asection *dynamic_relocs;
9620 asection *rela_dyn;
9621 asection *rel_dyn;
9622 bfd_size_type count, size;
9623 size_t i, ret, sort_elt, ext_size;
9624 bfd_byte *sort, *s_non_relative, *p;
9625 struct elf_link_sort_rela *sq;
9626 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9627 int i2e = bed->s->int_rels_per_ext_rel;
9628 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
9629 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9630 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9631 struct bfd_link_order *lo;
9632 bfd_vma r_sym_mask;
9633 bool use_rela;
9634
9635 /* Find a dynamic reloc section. */
9636 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9637 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
9638 if (rela_dyn != NULL && rela_dyn->size > 0
9639 && rel_dyn != NULL && rel_dyn->size > 0)
9640 {
9641 bool use_rela_initialised = false;
9642
9643 /* This is just here to stop gcc from complaining.
9644 Its initialization checking code is not perfect. */
9645 use_rela = true;
9646
9647 /* Both sections are present. Examine the sizes
9648 of the indirect sections to help us choose. */
9649 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9650 if (lo->type == bfd_indirect_link_order)
9651 {
9652 asection *o = lo->u.indirect.section;
9653
9654 if ((o->size % bed->s->sizeof_rela) == 0)
9655 {
9656 if ((o->size % bed->s->sizeof_rel) == 0)
9657 /* Section size is divisible by both rel and rela sizes.
9658 It is of no help to us. */
9659 ;
9660 else
9661 {
9662 /* Section size is only divisible by rela. */
9663 if (use_rela_initialised && !use_rela)
9664 {
9665 _bfd_error_handler (_("%pB: unable to sort relocs - "
9666 "they are in more than one size"),
9667 abfd);
9668 bfd_set_error (bfd_error_invalid_operation);
9669 return 0;
9670 }
9671 else
9672 {
9673 use_rela = true;
9674 use_rela_initialised = true;
9675 }
9676 }
9677 }
9678 else if ((o->size % bed->s->sizeof_rel) == 0)
9679 {
9680 /* Section size is only divisible by rel. */
9681 if (use_rela_initialised && use_rela)
9682 {
9683 _bfd_error_handler (_("%pB: unable to sort relocs - "
9684 "they are in more than one size"),
9685 abfd);
9686 bfd_set_error (bfd_error_invalid_operation);
9687 return 0;
9688 }
9689 else
9690 {
9691 use_rela = false;
9692 use_rela_initialised = true;
9693 }
9694 }
9695 else
9696 {
9697 /* The section size is not divisible by either -
9698 something is wrong. */
9699 _bfd_error_handler (_("%pB: unable to sort relocs - "
9700 "they are of an unknown size"), abfd);
9701 bfd_set_error (bfd_error_invalid_operation);
9702 return 0;
9703 }
9704 }
9705
9706 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9707 if (lo->type == bfd_indirect_link_order)
9708 {
9709 asection *o = lo->u.indirect.section;
9710
9711 if ((o->size % bed->s->sizeof_rela) == 0)
9712 {
9713 if ((o->size % bed->s->sizeof_rel) == 0)
9714 /* Section size is divisible by both rel and rela sizes.
9715 It is of no help to us. */
9716 ;
9717 else
9718 {
9719 /* Section size is only divisible by rela. */
9720 if (use_rela_initialised && !use_rela)
9721 {
9722 _bfd_error_handler (_("%pB: unable to sort relocs - "
9723 "they are in more than one size"),
9724 abfd);
9725 bfd_set_error (bfd_error_invalid_operation);
9726 return 0;
9727 }
9728 else
9729 {
9730 use_rela = true;
9731 use_rela_initialised = true;
9732 }
9733 }
9734 }
9735 else if ((o->size % bed->s->sizeof_rel) == 0)
9736 {
9737 /* Section size is only divisible by rel. */
9738 if (use_rela_initialised && use_rela)
9739 {
9740 _bfd_error_handler (_("%pB: unable to sort relocs - "
9741 "they are in more than one size"),
9742 abfd);
9743 bfd_set_error (bfd_error_invalid_operation);
9744 return 0;
9745 }
9746 else
9747 {
9748 use_rela = false;
9749 use_rela_initialised = true;
9750 }
9751 }
9752 else
9753 {
9754 /* The section size is not divisible by either -
9755 something is wrong. */
9756 _bfd_error_handler (_("%pB: unable to sort relocs - "
9757 "they are of an unknown size"), abfd);
9758 bfd_set_error (bfd_error_invalid_operation);
9759 return 0;
9760 }
9761 }
9762
9763 if (! use_rela_initialised)
9764 /* Make a guess. */
9765 use_rela = true;
9766 }
9767 else if (rela_dyn != NULL && rela_dyn->size > 0)
9768 use_rela = true;
9769 else if (rel_dyn != NULL && rel_dyn->size > 0)
9770 use_rela = false;
9771 else
9772 return 0;
9773
9774 if (use_rela)
9775 {
9776 dynamic_relocs = rela_dyn;
9777 ext_size = bed->s->sizeof_rela;
9778 swap_in = bed->s->swap_reloca_in;
9779 swap_out = bed->s->swap_reloca_out;
9780 }
9781 else
9782 {
9783 dynamic_relocs = rel_dyn;
9784 ext_size = bed->s->sizeof_rel;
9785 swap_in = bed->s->swap_reloc_in;
9786 swap_out = bed->s->swap_reloc_out;
9787 }
9788
9789 size = 0;
9790 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9791 if (lo->type == bfd_indirect_link_order)
9792 size += lo->u.indirect.section->size;
9793
9794 if (size != dynamic_relocs->size)
9795 return 0;
9796
9797 sort_elt = (sizeof (struct elf_link_sort_rela)
9798 + (i2e - 1) * sizeof (Elf_Internal_Rela));
9799
9800 count = dynamic_relocs->size / ext_size;
9801 if (count == 0)
9802 return 0;
9803 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
9804
9805 if (sort == NULL)
9806 {
9807 (*info->callbacks->warning)
9808 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
9809 return 0;
9810 }
9811
9812 if (bed->s->arch_size == 32)
9813 r_sym_mask = ~(bfd_vma) 0xff;
9814 else
9815 r_sym_mask = ~(bfd_vma) 0xffffffff;
9816
9817 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9818 if (lo->type == bfd_indirect_link_order)
9819 {
9820 bfd_byte *erel, *erelend;
9821 asection *o = lo->u.indirect.section;
9822
9823 if (o->contents == NULL && o->size != 0)
9824 {
9825 /* This is a reloc section that is being handled as a normal
9826 section. See bfd_section_from_shdr. We can't combine
9827 relocs in this case. */
9828 free (sort);
9829 return 0;
9830 }
9831 erel = o->contents;
9832 erelend = o->contents + o->size;
9833 p = sort + o->output_offset * opb / ext_size * sort_elt;
9834
9835 while (erel < erelend)
9836 {
9837 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9838
9839 (*swap_in) (abfd, erel, s->rela);
9840 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
9841 s->u.sym_mask = r_sym_mask;
9842 p += sort_elt;
9843 erel += ext_size;
9844 }
9845 }
9846
9847 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9848
9849 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9850 {
9851 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9852 if (s->type != reloc_class_relative)
9853 break;
9854 }
9855 ret = i;
9856 s_non_relative = p;
9857
9858 sq = (struct elf_link_sort_rela *) s_non_relative;
9859 for (; i < count; i++, p += sort_elt)
9860 {
9861 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9862 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9863 sq = sp;
9864 sp->u.offset = sq->rela->r_offset;
9865 }
9866
9867 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9868
9869 struct elf_link_hash_table *htab = elf_hash_table (info);
9870 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9871 {
9872 /* We have plt relocs in .rela.dyn. */
9873 sq = (struct elf_link_sort_rela *) sort;
9874 for (i = 0; i < count; i++)
9875 if (sq[count - i - 1].type != reloc_class_plt)
9876 break;
9877 if (i != 0 && htab->srelplt->size == i * ext_size)
9878 {
9879 struct bfd_link_order **plo;
9880 /* Put srelplt link_order last. This is so the output_offset
9881 set in the next loop is correct for DT_JMPREL. */
9882 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9883 if ((*plo)->type == bfd_indirect_link_order
9884 && (*plo)->u.indirect.section == htab->srelplt)
9885 {
9886 lo = *plo;
9887 *plo = lo->next;
9888 }
9889 else
9890 plo = &(*plo)->next;
9891 *plo = lo;
9892 lo->next = NULL;
9893 dynamic_relocs->map_tail.link_order = lo;
9894 }
9895 }
9896
9897 p = sort;
9898 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9899 if (lo->type == bfd_indirect_link_order)
9900 {
9901 bfd_byte *erel, *erelend;
9902 asection *o = lo->u.indirect.section;
9903
9904 erel = o->contents;
9905 erelend = o->contents + o->size;
9906 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
9907 while (erel < erelend)
9908 {
9909 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9910 (*swap_out) (abfd, s->rela, erel);
9911 p += sort_elt;
9912 erel += ext_size;
9913 }
9914 }
9915
9916 free (sort);
9917 *psec = dynamic_relocs;
9918 return ret;
9919 }
9920
9921 /* Add a symbol to the output symbol string table. */
9922
9923 static int
9924 elf_link_output_symstrtab (void *finf,
9925 const char *name,
9926 Elf_Internal_Sym *elfsym,
9927 asection *input_sec,
9928 struct elf_link_hash_entry *h)
9929 {
9930 struct elf_final_link_info *flinfo = finf;
9931 int (*output_symbol_hook)
9932 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9933 struct elf_link_hash_entry *);
9934 struct elf_link_hash_table *hash_table;
9935 const struct elf_backend_data *bed;
9936 bfd_size_type strtabsize;
9937
9938 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9939
9940 bed = get_elf_backend_data (flinfo->output_bfd);
9941 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9942 if (output_symbol_hook != NULL)
9943 {
9944 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
9945 if (ret != 1)
9946 return ret;
9947 }
9948
9949 if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC)
9950 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
9951 if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE)
9952 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique;
9953
9954 if (name == NULL
9955 || *name == '\0'
9956 || (input_sec->flags & SEC_EXCLUDE))
9957 elfsym->st_name = (unsigned long) -1;
9958 else
9959 {
9960 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9961 to get the final offset for st_name. */
9962 char *versioned_name = (char *) name;
9963 if (h != NULL)
9964 {
9965 if (h->versioned == versioned && h->def_dynamic)
9966 {
9967 /* Keep only one '@' for versioned symbols defined in
9968 shared objects. */
9969 char *version = strrchr (name, ELF_VER_CHR);
9970 char *base_end = strchr (name, ELF_VER_CHR);
9971 if (version != base_end)
9972 {
9973 size_t base_len;
9974 size_t len = strlen (name);
9975 versioned_name = bfd_alloc (flinfo->output_bfd, len);
9976 if (versioned_name == NULL)
9977 return 0;
9978 base_len = base_end - name;
9979 memcpy (versioned_name, name, base_len);
9980 memcpy (versioned_name + base_len, version,
9981 len - base_len);
9982 }
9983 }
9984 }
9985 else if (flinfo->info->unique_symbol
9986 && ELF_ST_BIND (elfsym->st_info) == STB_LOCAL)
9987 {
9988 struct local_hash_entry *lh;
9989 size_t count_len;
9990 size_t base_len;
9991 char buf[30];
9992 switch (ELF_ST_TYPE (elfsym->st_info))
9993 {
9994 case STT_FILE:
9995 case STT_SECTION:
9996 break;
9997 default:
9998 lh = (struct local_hash_entry *) bfd_hash_lookup
9999 (&flinfo->local_hash_table, name, true, false);
10000 if (lh == NULL)
10001 return 0;
10002 /* Always append ".COUNT" to local symbols to avoid
10003 potential conflicts with local symbol "XXX.COUNT". */
10004 sprintf (buf, "%lx", lh->count);
10005 base_len = lh->size;
10006 if (!base_len)
10007 {
10008 base_len = strlen (name);
10009 lh->size = base_len;
10010 }
10011 count_len = strlen (buf);
10012 versioned_name = bfd_alloc (flinfo->output_bfd,
10013 base_len + count_len + 2);
10014 if (versioned_name == NULL)
10015 return 0;
10016 memcpy (versioned_name, name, base_len);
10017 versioned_name[base_len] = '.';
10018 memcpy (versioned_name + base_len + 1, buf,
10019 count_len + 1);
10020 lh->count++;
10021 break;
10022 }
10023 }
10024 elfsym->st_name
10025 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
10026 versioned_name, false);
10027 if (elfsym->st_name == (unsigned long) -1)
10028 return 0;
10029 }
10030
10031 hash_table = elf_hash_table (flinfo->info);
10032 strtabsize = hash_table->strtabsize;
10033 if (strtabsize <= flinfo->output_bfd->symcount)
10034 {
10035 strtabsize += strtabsize;
10036 hash_table->strtabsize = strtabsize;
10037 strtabsize *= sizeof (*hash_table->strtab);
10038 hash_table->strtab
10039 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
10040 strtabsize);
10041 if (hash_table->strtab == NULL)
10042 return 0;
10043 }
10044 hash_table->strtab[flinfo->output_bfd->symcount].sym = *elfsym;
10045 hash_table->strtab[flinfo->output_bfd->symcount].dest_index
10046 = flinfo->output_bfd->symcount;
10047 flinfo->output_bfd->symcount += 1;
10048
10049 return 1;
10050 }
10051
10052 /* Swap symbols out to the symbol table and flush the output symbols to
10053 the file. */
10054
10055 static bool
10056 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
10057 {
10058 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
10059 size_t amt;
10060 size_t i;
10061 const struct elf_backend_data *bed;
10062 bfd_byte *symbuf;
10063 Elf_Internal_Shdr *hdr;
10064 file_ptr pos;
10065 bool ret;
10066
10067 if (flinfo->output_bfd->symcount == 0)
10068 return true;
10069
10070 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
10071
10072 bed = get_elf_backend_data (flinfo->output_bfd);
10073
10074 amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount;
10075 symbuf = (bfd_byte *) bfd_malloc (amt);
10076 if (symbuf == NULL)
10077 return false;
10078
10079 if (flinfo->symshndxbuf)
10080 {
10081 amt = sizeof (Elf_External_Sym_Shndx);
10082 amt *= bfd_get_symcount (flinfo->output_bfd);
10083 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
10084 if (flinfo->symshndxbuf == NULL)
10085 {
10086 free (symbuf);
10087 return false;
10088 }
10089 }
10090
10091 /* Now swap out the symbols. */
10092 for (i = 0; i < flinfo->output_bfd->symcount; i++)
10093 {
10094 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
10095 if (elfsym->sym.st_name == (unsigned long) -1)
10096 elfsym->sym.st_name = 0;
10097 else
10098 elfsym->sym.st_name
10099 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
10100 elfsym->sym.st_name);
10101
10102 /* Inform the linker of the addition of this symbol. */
10103
10104 if (flinfo->info->callbacks->ctf_new_symbol)
10105 flinfo->info->callbacks->ctf_new_symbol (elfsym->dest_index,
10106 &elfsym->sym);
10107
10108 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
10109 ((bfd_byte *) symbuf
10110 + (elfsym->dest_index
10111 * bed->s->sizeof_sym)),
10112 NPTR_ADD (flinfo->symshndxbuf,
10113 elfsym->dest_index));
10114 }
10115
10116 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
10117 pos = hdr->sh_offset + hdr->sh_size;
10118 amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount;
10119 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
10120 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
10121 {
10122 hdr->sh_size += amt;
10123 ret = true;
10124 }
10125 else
10126 ret = false;
10127
10128 free (symbuf);
10129
10130 free (hash_table->strtab);
10131 hash_table->strtab = NULL;
10132
10133 return ret;
10134 }
10135
10136 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
10137
10138 static bool
10139 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
10140 {
10141 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
10142 && sym->st_shndx < SHN_LORESERVE)
10143 {
10144 /* The gABI doesn't support dynamic symbols in output sections
10145 beyond 64k. */
10146 _bfd_error_handler
10147 /* xgettext:c-format */
10148 (_("%pB: too many sections: %d (>= %d)"),
10149 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
10150 bfd_set_error (bfd_error_nonrepresentable_section);
10151 return false;
10152 }
10153 return true;
10154 }
10155
10156 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
10157 allowing an unsatisfied unversioned symbol in the DSO to match a
10158 versioned symbol that would normally require an explicit version.
10159 We also handle the case that a DSO references a hidden symbol
10160 which may be satisfied by a versioned symbol in another DSO. */
10161
10162 static bool
10163 elf_link_check_versioned_symbol (struct bfd_link_info *info,
10164 const struct elf_backend_data *bed,
10165 struct elf_link_hash_entry *h)
10166 {
10167 bfd *abfd;
10168 struct elf_link_loaded_list *loaded;
10169
10170 if (!is_elf_hash_table (info->hash))
10171 return false;
10172
10173 /* Check indirect symbol. */
10174 while (h->root.type == bfd_link_hash_indirect)
10175 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10176
10177 switch (h->root.type)
10178 {
10179 default:
10180 abfd = NULL;
10181 break;
10182
10183 case bfd_link_hash_undefined:
10184 case bfd_link_hash_undefweak:
10185 abfd = h->root.u.undef.abfd;
10186 if (abfd == NULL
10187 || (abfd->flags & DYNAMIC) == 0
10188 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
10189 return false;
10190 break;
10191
10192 case bfd_link_hash_defined:
10193 case bfd_link_hash_defweak:
10194 abfd = h->root.u.def.section->owner;
10195 break;
10196
10197 case bfd_link_hash_common:
10198 abfd = h->root.u.c.p->section->owner;
10199 break;
10200 }
10201 BFD_ASSERT (abfd != NULL);
10202
10203 for (loaded = elf_hash_table (info)->dyn_loaded;
10204 loaded != NULL;
10205 loaded = loaded->next)
10206 {
10207 bfd *input;
10208 Elf_Internal_Shdr *hdr;
10209 size_t symcount;
10210 size_t extsymcount;
10211 size_t extsymoff;
10212 Elf_Internal_Shdr *versymhdr;
10213 Elf_Internal_Sym *isym;
10214 Elf_Internal_Sym *isymend;
10215 Elf_Internal_Sym *isymbuf;
10216 Elf_External_Versym *ever;
10217 Elf_External_Versym *extversym;
10218
10219 input = loaded->abfd;
10220
10221 /* We check each DSO for a possible hidden versioned definition. */
10222 if (input == abfd
10223 || elf_dynversym (input) == 0)
10224 continue;
10225
10226 hdr = &elf_tdata (input)->dynsymtab_hdr;
10227
10228 symcount = hdr->sh_size / bed->s->sizeof_sym;
10229 if (elf_bad_symtab (input))
10230 {
10231 extsymcount = symcount;
10232 extsymoff = 0;
10233 }
10234 else
10235 {
10236 extsymcount = symcount - hdr->sh_info;
10237 extsymoff = hdr->sh_info;
10238 }
10239
10240 if (extsymcount == 0)
10241 continue;
10242
10243 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
10244 NULL, NULL, NULL);
10245 if (isymbuf == NULL)
10246 return false;
10247
10248 /* Read in any version definitions. */
10249 versymhdr = &elf_tdata (input)->dynversym_hdr;
10250 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
10251 || (extversym = (Elf_External_Versym *)
10252 _bfd_malloc_and_read (input, versymhdr->sh_size,
10253 versymhdr->sh_size)) == NULL)
10254 {
10255 free (isymbuf);
10256 return false;
10257 }
10258
10259 ever = extversym + extsymoff;
10260 isymend = isymbuf + extsymcount;
10261 for (isym = isymbuf; isym < isymend; isym++, ever++)
10262 {
10263 const char *name;
10264 Elf_Internal_Versym iver;
10265 unsigned short version_index;
10266
10267 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
10268 || isym->st_shndx == SHN_UNDEF)
10269 continue;
10270
10271 name = bfd_elf_string_from_elf_section (input,
10272 hdr->sh_link,
10273 isym->st_name);
10274 if (strcmp (name, h->root.root.string) != 0)
10275 continue;
10276
10277 _bfd_elf_swap_versym_in (input, ever, &iver);
10278
10279 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
10280 && !(h->def_regular
10281 && h->forced_local))
10282 {
10283 /* If we have a non-hidden versioned sym, then it should
10284 have provided a definition for the undefined sym unless
10285 it is defined in a non-shared object and forced local.
10286 */
10287 abort ();
10288 }
10289
10290 version_index = iver.vs_vers & VERSYM_VERSION;
10291 if (version_index == 1 || version_index == 2)
10292 {
10293 /* This is the base or first version. We can use it. */
10294 free (extversym);
10295 free (isymbuf);
10296 return true;
10297 }
10298 }
10299
10300 free (extversym);
10301 free (isymbuf);
10302 }
10303
10304 return false;
10305 }
10306
10307 /* Convert ELF common symbol TYPE. */
10308
10309 static int
10310 elf_link_convert_common_type (struct bfd_link_info *info, int type)
10311 {
10312 /* Commom symbol can only appear in relocatable link. */
10313 if (!bfd_link_relocatable (info))
10314 abort ();
10315 switch (info->elf_stt_common)
10316 {
10317 case unchanged:
10318 break;
10319 case elf_stt_common:
10320 type = STT_COMMON;
10321 break;
10322 case no_elf_stt_common:
10323 type = STT_OBJECT;
10324 break;
10325 }
10326 return type;
10327 }
10328
10329 /* Add an external symbol to the symbol table. This is called from
10330 the hash table traversal routine. When generating a shared object,
10331 we go through the symbol table twice. The first time we output
10332 anything that might have been forced to local scope in a version
10333 script. The second time we output the symbols that are still
10334 global symbols. */
10335
10336 static bool
10337 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
10338 {
10339 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
10340 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
10341 struct elf_final_link_info *flinfo = eoinfo->flinfo;
10342 bool strip;
10343 Elf_Internal_Sym sym;
10344 asection *input_sec;
10345 const struct elf_backend_data *bed;
10346 long indx;
10347 int ret;
10348 unsigned int type;
10349
10350 if (h->root.type == bfd_link_hash_warning)
10351 {
10352 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10353 if (h->root.type == bfd_link_hash_new)
10354 return true;
10355 }
10356
10357 /* Decide whether to output this symbol in this pass. */
10358 if (eoinfo->localsyms)
10359 {
10360 if (!h->forced_local)
10361 return true;
10362 }
10363 else
10364 {
10365 if (h->forced_local)
10366 return true;
10367 }
10368
10369 bed = get_elf_backend_data (flinfo->output_bfd);
10370
10371 if (h->root.type == bfd_link_hash_undefined)
10372 {
10373 /* If we have an undefined symbol reference here then it must have
10374 come from a shared library that is being linked in. (Undefined
10375 references in regular files have already been handled unless
10376 they are in unreferenced sections which are removed by garbage
10377 collection). */
10378 bool ignore_undef = false;
10379
10380 /* Some symbols may be special in that the fact that they're
10381 undefined can be safely ignored - let backend determine that. */
10382 if (bed->elf_backend_ignore_undef_symbol)
10383 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
10384
10385 /* If we are reporting errors for this situation then do so now. */
10386 if (!ignore_undef
10387 && h->ref_dynamic_nonweak
10388 && (!h->ref_regular || flinfo->info->gc_sections)
10389 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
10390 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
10391 {
10392 flinfo->info->callbacks->undefined_symbol
10393 (flinfo->info, h->root.root.string,
10394 h->ref_regular ? NULL : h->root.u.undef.abfd, NULL, 0,
10395 flinfo->info->unresolved_syms_in_shared_libs == RM_DIAGNOSE
10396 && !flinfo->info->warn_unresolved_syms);
10397 }
10398
10399 /* Strip a global symbol defined in a discarded section. */
10400 if (h->indx == -3)
10401 return true;
10402 }
10403
10404 /* We should also warn if a forced local symbol is referenced from
10405 shared libraries. */
10406 if (bfd_link_executable (flinfo->info)
10407 && h->forced_local
10408 && h->ref_dynamic
10409 && h->def_regular
10410 && !h->dynamic_def
10411 && h->ref_dynamic_nonweak
10412 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
10413 {
10414 bfd *def_bfd;
10415 const char *msg;
10416 struct elf_link_hash_entry *hi = h;
10417
10418 /* Check indirect symbol. */
10419 while (hi->root.type == bfd_link_hash_indirect)
10420 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
10421
10422 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
10423 /* xgettext:c-format */
10424 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
10425 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
10426 /* xgettext:c-format */
10427 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
10428 else
10429 /* xgettext:c-format */
10430 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
10431 def_bfd = flinfo->output_bfd;
10432 if (hi->root.u.def.section != bfd_abs_section_ptr)
10433 def_bfd = hi->root.u.def.section->owner;
10434 _bfd_error_handler (msg, flinfo->output_bfd,
10435 h->root.root.string, def_bfd);
10436 bfd_set_error (bfd_error_bad_value);
10437 eoinfo->failed = true;
10438 return false;
10439 }
10440
10441 /* We don't want to output symbols that have never been mentioned by
10442 a regular file, or that we have been told to strip. However, if
10443 h->indx is set to -2, the symbol is used by a reloc and we must
10444 output it. */
10445 strip = false;
10446 if (h->indx == -2)
10447 ;
10448 else if ((h->def_dynamic
10449 || h->ref_dynamic
10450 || h->root.type == bfd_link_hash_new)
10451 && !h->def_regular
10452 && !h->ref_regular)
10453 strip = true;
10454 else if (flinfo->info->strip == strip_all)
10455 strip = true;
10456 else if (flinfo->info->strip == strip_some
10457 && bfd_hash_lookup (flinfo->info->keep_hash,
10458 h->root.root.string, false, false) == NULL)
10459 strip = true;
10460 else if ((h->root.type == bfd_link_hash_defined
10461 || h->root.type == bfd_link_hash_defweak)
10462 && ((flinfo->info->strip_discarded
10463 && discarded_section (h->root.u.def.section))
10464 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
10465 && h->root.u.def.section->owner != NULL
10466 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
10467 strip = true;
10468 else if ((h->root.type == bfd_link_hash_undefined
10469 || h->root.type == bfd_link_hash_undefweak)
10470 && h->root.u.undef.abfd != NULL
10471 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
10472 strip = true;
10473
10474 type = h->type;
10475
10476 /* If we're stripping it, and it's not a dynamic symbol, there's
10477 nothing else to do. However, if it is a forced local symbol or
10478 an ifunc symbol we need to give the backend finish_dynamic_symbol
10479 function a chance to make it dynamic. */
10480 if (strip
10481 && h->dynindx == -1
10482 && type != STT_GNU_IFUNC
10483 && !h->forced_local)
10484 return true;
10485
10486 sym.st_value = 0;
10487 sym.st_size = h->size;
10488 sym.st_other = h->other;
10489 switch (h->root.type)
10490 {
10491 default:
10492 case bfd_link_hash_new:
10493 case bfd_link_hash_warning:
10494 abort ();
10495 return false;
10496
10497 case bfd_link_hash_undefined:
10498 case bfd_link_hash_undefweak:
10499 input_sec = bfd_und_section_ptr;
10500 sym.st_shndx = SHN_UNDEF;
10501 break;
10502
10503 case bfd_link_hash_defined:
10504 case bfd_link_hash_defweak:
10505 {
10506 input_sec = h->root.u.def.section;
10507 if (input_sec->output_section != NULL)
10508 {
10509 sym.st_shndx =
10510 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
10511 input_sec->output_section);
10512 if (sym.st_shndx == SHN_BAD)
10513 {
10514 _bfd_error_handler
10515 /* xgettext:c-format */
10516 (_("%pB: could not find output section %pA for input section %pA"),
10517 flinfo->output_bfd, input_sec->output_section, input_sec);
10518 bfd_set_error (bfd_error_nonrepresentable_section);
10519 eoinfo->failed = true;
10520 return false;
10521 }
10522
10523 /* ELF symbols in relocatable files are section relative,
10524 but in nonrelocatable files they are virtual
10525 addresses. */
10526 sym.st_value = h->root.u.def.value + input_sec->output_offset;
10527 if (!bfd_link_relocatable (flinfo->info))
10528 {
10529 sym.st_value += input_sec->output_section->vma;
10530 if (h->type == STT_TLS)
10531 {
10532 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
10533 if (tls_sec != NULL)
10534 sym.st_value -= tls_sec->vma;
10535 }
10536 }
10537 }
10538 else
10539 {
10540 BFD_ASSERT (input_sec->owner == NULL
10541 || (input_sec->owner->flags & DYNAMIC) != 0);
10542 sym.st_shndx = SHN_UNDEF;
10543 input_sec = bfd_und_section_ptr;
10544 }
10545 }
10546 break;
10547
10548 case bfd_link_hash_common:
10549 input_sec = h->root.u.c.p->section;
10550 sym.st_shndx = bed->common_section_index (input_sec);
10551 sym.st_value = 1 << h->root.u.c.p->alignment_power;
10552 break;
10553
10554 case bfd_link_hash_indirect:
10555 /* These symbols are created by symbol versioning. They point
10556 to the decorated version of the name. For example, if the
10557 symbol foo@@GNU_1.2 is the default, which should be used when
10558 foo is used with no version, then we add an indirect symbol
10559 foo which points to foo@@GNU_1.2. We ignore these symbols,
10560 since the indirected symbol is already in the hash table. */
10561 return true;
10562 }
10563
10564 if (type == STT_COMMON || type == STT_OBJECT)
10565 switch (h->root.type)
10566 {
10567 case bfd_link_hash_common:
10568 type = elf_link_convert_common_type (flinfo->info, type);
10569 break;
10570 case bfd_link_hash_defined:
10571 case bfd_link_hash_defweak:
10572 if (bed->common_definition (&sym))
10573 type = elf_link_convert_common_type (flinfo->info, type);
10574 else
10575 type = STT_OBJECT;
10576 break;
10577 case bfd_link_hash_undefined:
10578 case bfd_link_hash_undefweak:
10579 break;
10580 default:
10581 abort ();
10582 }
10583
10584 if (h->forced_local)
10585 {
10586 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
10587 /* Turn off visibility on local symbol. */
10588 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
10589 }
10590 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10591 else if (h->unique_global && h->def_regular)
10592 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
10593 else if (h->root.type == bfd_link_hash_undefweak
10594 || h->root.type == bfd_link_hash_defweak)
10595 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
10596 else
10597 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
10598 sym.st_target_internal = h->target_internal;
10599
10600 /* Give the processor backend a chance to tweak the symbol value,
10601 and also to finish up anything that needs to be done for this
10602 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
10603 forced local syms when non-shared is due to a historical quirk.
10604 STT_GNU_IFUNC symbol must go through PLT. */
10605 if ((h->type == STT_GNU_IFUNC
10606 && h->def_regular
10607 && !bfd_link_relocatable (flinfo->info))
10608 || ((h->dynindx != -1
10609 || h->forced_local)
10610 && ((bfd_link_pic (flinfo->info)
10611 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
10612 || h->root.type != bfd_link_hash_undefweak))
10613 || !h->forced_local)
10614 && elf_hash_table (flinfo->info)->dynamic_sections_created))
10615 {
10616 if (! ((*bed->elf_backend_finish_dynamic_symbol)
10617 (flinfo->output_bfd, flinfo->info, h, &sym)))
10618 {
10619 eoinfo->failed = true;
10620 return false;
10621 }
10622 }
10623
10624 /* If we are marking the symbol as undefined, and there are no
10625 non-weak references to this symbol from a regular object, then
10626 mark the symbol as weak undefined; if there are non-weak
10627 references, mark the symbol as strong. We can't do this earlier,
10628 because it might not be marked as undefined until the
10629 finish_dynamic_symbol routine gets through with it. */
10630 if (sym.st_shndx == SHN_UNDEF
10631 && h->ref_regular
10632 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
10633 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
10634 {
10635 int bindtype;
10636 type = ELF_ST_TYPE (sym.st_info);
10637
10638 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10639 if (type == STT_GNU_IFUNC)
10640 type = STT_FUNC;
10641
10642 if (h->ref_regular_nonweak)
10643 bindtype = STB_GLOBAL;
10644 else
10645 bindtype = STB_WEAK;
10646 sym.st_info = ELF_ST_INFO (bindtype, type);
10647 }
10648
10649 /* If this is a symbol defined in a dynamic library, don't use the
10650 symbol size from the dynamic library. Relinking an executable
10651 against a new library may introduce gratuitous changes in the
10652 executable's symbols if we keep the size. */
10653 if (sym.st_shndx == SHN_UNDEF
10654 && !h->def_regular
10655 && h->def_dynamic)
10656 sym.st_size = 0;
10657
10658 /* If a non-weak symbol with non-default visibility is not defined
10659 locally, it is a fatal error. */
10660 if (!bfd_link_relocatable (flinfo->info)
10661 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
10662 && ELF_ST_BIND (sym.st_info) != STB_WEAK
10663 && h->root.type == bfd_link_hash_undefined
10664 && !h->def_regular)
10665 {
10666 const char *msg;
10667
10668 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
10669 /* xgettext:c-format */
10670 msg = _("%pB: protected symbol `%s' isn't defined");
10671 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
10672 /* xgettext:c-format */
10673 msg = _("%pB: internal symbol `%s' isn't defined");
10674 else
10675 /* xgettext:c-format */
10676 msg = _("%pB: hidden symbol `%s' isn't defined");
10677 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
10678 bfd_set_error (bfd_error_bad_value);
10679 eoinfo->failed = true;
10680 return false;
10681 }
10682
10683 /* If this symbol should be put in the .dynsym section, then put it
10684 there now. We already know the symbol index. We also fill in
10685 the entry in the .hash section. */
10686 if (h->dynindx != -1
10687 && elf_hash_table (flinfo->info)->dynamic_sections_created
10688 && elf_hash_table (flinfo->info)->dynsym != NULL
10689 && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
10690 {
10691 bfd_byte *esym;
10692
10693 /* Since there is no version information in the dynamic string,
10694 if there is no version info in symbol version section, we will
10695 have a run-time problem if not linking executable, referenced
10696 by shared library, or not bound locally. */
10697 if (h->verinfo.verdef == NULL
10698 && (!bfd_link_executable (flinfo->info)
10699 || h->ref_dynamic
10700 || !h->def_regular))
10701 {
10702 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
10703
10704 if (p && p [1] != '\0')
10705 {
10706 _bfd_error_handler
10707 /* xgettext:c-format */
10708 (_("%pB: no symbol version section for versioned symbol `%s'"),
10709 flinfo->output_bfd, h->root.root.string);
10710 eoinfo->failed = true;
10711 return false;
10712 }
10713 }
10714
10715 sym.st_name = h->dynstr_index;
10716 esym = (elf_hash_table (flinfo->info)->dynsym->contents
10717 + h->dynindx * bed->s->sizeof_sym);
10718 if (!check_dynsym (flinfo->output_bfd, &sym))
10719 {
10720 eoinfo->failed = true;
10721 return false;
10722 }
10723
10724 /* Inform the linker of the addition of this symbol. */
10725
10726 if (flinfo->info->callbacks->ctf_new_dynsym)
10727 flinfo->info->callbacks->ctf_new_dynsym (h->dynindx, &sym);
10728
10729 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
10730
10731 if (flinfo->hash_sec != NULL)
10732 {
10733 size_t hash_entry_size;
10734 bfd_byte *bucketpos;
10735 bfd_vma chain;
10736 size_t bucketcount;
10737 size_t bucket;
10738
10739 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
10740 bucket = h->u.elf_hash_value % bucketcount;
10741
10742 hash_entry_size
10743 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
10744 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
10745 + (bucket + 2) * hash_entry_size);
10746 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
10747 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
10748 bucketpos);
10749 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
10750 ((bfd_byte *) flinfo->hash_sec->contents
10751 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
10752 }
10753
10754 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
10755 {
10756 Elf_Internal_Versym iversym;
10757 Elf_External_Versym *eversym;
10758
10759 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
10760 {
10761 if (h->verinfo.verdef == NULL
10762 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
10763 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
10764 iversym.vs_vers = 1;
10765 else
10766 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
10767 }
10768 else
10769 {
10770 if (h->verinfo.vertree == NULL)
10771 iversym.vs_vers = 1;
10772 else
10773 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
10774 if (flinfo->info->create_default_symver)
10775 iversym.vs_vers++;
10776 }
10777
10778 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
10779 defined locally. */
10780 if (h->versioned == versioned_hidden && h->def_regular)
10781 iversym.vs_vers |= VERSYM_HIDDEN;
10782
10783 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
10784 eversym += h->dynindx;
10785 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
10786 }
10787 }
10788
10789 /* If the symbol is undefined, and we didn't output it to .dynsym,
10790 strip it from .symtab too. Obviously we can't do this for
10791 relocatable output or when needed for --emit-relocs. */
10792 else if (input_sec == bfd_und_section_ptr
10793 && h->indx != -2
10794 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10795 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
10796 && !bfd_link_relocatable (flinfo->info))
10797 return true;
10798
10799 /* Also strip others that we couldn't earlier due to dynamic symbol
10800 processing. */
10801 if (strip)
10802 return true;
10803 if ((input_sec->flags & SEC_EXCLUDE) != 0)
10804 return true;
10805
10806 /* Output a FILE symbol so that following locals are not associated
10807 with the wrong input file. We need one for forced local symbols
10808 if we've seen more than one FILE symbol or when we have exactly
10809 one FILE symbol but global symbols are present in a file other
10810 than the one with the FILE symbol. We also need one if linker
10811 defined symbols are present. In practice these conditions are
10812 always met, so just emit the FILE symbol unconditionally. */
10813 if (eoinfo->localsyms
10814 && !eoinfo->file_sym_done
10815 && eoinfo->flinfo->filesym_count != 0)
10816 {
10817 Elf_Internal_Sym fsym;
10818
10819 memset (&fsym, 0, sizeof (fsym));
10820 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10821 fsym.st_shndx = SHN_ABS;
10822 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10823 bfd_und_section_ptr, NULL))
10824 return false;
10825
10826 eoinfo->file_sym_done = true;
10827 }
10828
10829 indx = bfd_get_symcount (flinfo->output_bfd);
10830 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10831 input_sec, h);
10832 if (ret == 0)
10833 {
10834 eoinfo->failed = true;
10835 return false;
10836 }
10837 else if (ret == 1)
10838 h->indx = indx;
10839 else if (h->indx == -2)
10840 abort();
10841
10842 return true;
10843 }
10844
10845 /* Return TRUE if special handling is done for relocs in SEC against
10846 symbols defined in discarded sections. */
10847
10848 static bool
10849 elf_section_ignore_discarded_relocs (asection *sec)
10850 {
10851 const struct elf_backend_data *bed;
10852
10853 switch (sec->sec_info_type)
10854 {
10855 case SEC_INFO_TYPE_STABS:
10856 case SEC_INFO_TYPE_EH_FRAME:
10857 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10858 return true;
10859 default:
10860 break;
10861 }
10862
10863 bed = get_elf_backend_data (sec->owner);
10864 if (bed->elf_backend_ignore_discarded_relocs != NULL
10865 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10866 return true;
10867
10868 return false;
10869 }
10870
10871 /* Return a mask saying how ld should treat relocations in SEC against
10872 symbols defined in discarded sections. If this function returns
10873 COMPLAIN set, ld will issue a warning message. If this function
10874 returns PRETEND set, and the discarded section was link-once and the
10875 same size as the kept link-once section, ld will pretend that the
10876 symbol was actually defined in the kept section. Otherwise ld will
10877 zero the reloc (at least that is the intent, but some cooperation by
10878 the target dependent code is needed, particularly for REL targets). */
10879
10880 unsigned int
10881 _bfd_elf_default_action_discarded (asection *sec)
10882 {
10883 if (sec->flags & SEC_DEBUGGING)
10884 return PRETEND;
10885
10886 if (strcmp (".eh_frame", sec->name) == 0)
10887 return 0;
10888
10889 if (strcmp (".gcc_except_table", sec->name) == 0)
10890 return 0;
10891
10892 return COMPLAIN | PRETEND;
10893 }
10894
10895 /* Find a match between a section and a member of a section group. */
10896
10897 static asection *
10898 match_group_member (asection *sec, asection *group,
10899 struct bfd_link_info *info)
10900 {
10901 asection *first = elf_next_in_group (group);
10902 asection *s = first;
10903
10904 while (s != NULL)
10905 {
10906 if (bfd_elf_match_symbols_in_sections (s, sec, info))
10907 return s;
10908
10909 s = elf_next_in_group (s);
10910 if (s == first)
10911 break;
10912 }
10913
10914 return NULL;
10915 }
10916
10917 /* Check if the kept section of a discarded section SEC can be used
10918 to replace it. Return the replacement if it is OK. Otherwise return
10919 NULL. */
10920
10921 asection *
10922 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
10923 {
10924 asection *kept;
10925
10926 kept = sec->kept_section;
10927 if (kept != NULL)
10928 {
10929 if ((kept->flags & SEC_GROUP) != 0)
10930 kept = match_group_member (sec, kept, info);
10931 if (kept != NULL)
10932 {
10933 if ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10934 != (kept->rawsize != 0 ? kept->rawsize : kept->size))
10935 kept = NULL;
10936 else
10937 {
10938 /* Get the real kept section. */
10939 asection *next;
10940 for (next = kept->kept_section;
10941 next != NULL;
10942 next = next->kept_section)
10943 kept = next;
10944 }
10945 }
10946 sec->kept_section = kept;
10947 }
10948 return kept;
10949 }
10950
10951 /* Link an input file into the linker output file. This function
10952 handles all the sections and relocations of the input file at once.
10953 This is so that we only have to read the local symbols once, and
10954 don't have to keep them in memory. */
10955
10956 static bool
10957 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
10958 {
10959 int (*relocate_section)
10960 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10961 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10962 bfd *output_bfd;
10963 Elf_Internal_Shdr *symtab_hdr;
10964 size_t locsymcount;
10965 size_t extsymoff;
10966 Elf_Internal_Sym *isymbuf;
10967 Elf_Internal_Sym *isym;
10968 Elf_Internal_Sym *isymend;
10969 long *pindex;
10970 asection **ppsection;
10971 asection *o;
10972 const struct elf_backend_data *bed;
10973 struct elf_link_hash_entry **sym_hashes;
10974 bfd_size_type address_size;
10975 bfd_vma r_type_mask;
10976 int r_sym_shift;
10977 bool have_file_sym = false;
10978
10979 output_bfd = flinfo->output_bfd;
10980 bed = get_elf_backend_data (output_bfd);
10981 relocate_section = bed->elf_backend_relocate_section;
10982
10983 /* If this is a dynamic object, we don't want to do anything here:
10984 we don't want the local symbols, and we don't want the section
10985 contents. */
10986 if ((input_bfd->flags & DYNAMIC) != 0)
10987 return true;
10988
10989 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10990 if (elf_bad_symtab (input_bfd))
10991 {
10992 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10993 extsymoff = 0;
10994 }
10995 else
10996 {
10997 locsymcount = symtab_hdr->sh_info;
10998 extsymoff = symtab_hdr->sh_info;
10999 }
11000
11001 /* Enable GNU OSABI features in the output BFD that are used in the input
11002 BFD. */
11003 if (bed->elf_osabi == ELFOSABI_NONE
11004 || bed->elf_osabi == ELFOSABI_GNU
11005 || bed->elf_osabi == ELFOSABI_FREEBSD)
11006 elf_tdata (output_bfd)->has_gnu_osabi
11007 |= (elf_tdata (input_bfd)->has_gnu_osabi
11008 & (bfd_link_relocatable (flinfo->info)
11009 ? -1 : ~elf_gnu_osabi_retain));
11010
11011 /* Read the local symbols. */
11012 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
11013 if (isymbuf == NULL && locsymcount != 0)
11014 {
11015 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
11016 flinfo->internal_syms,
11017 flinfo->external_syms,
11018 flinfo->locsym_shndx);
11019 if (isymbuf == NULL)
11020 return false;
11021 }
11022
11023 /* Find local symbol sections and adjust values of symbols in
11024 SEC_MERGE sections. Write out those local symbols we know are
11025 going into the output file. */
11026 isymend = PTR_ADD (isymbuf, locsymcount);
11027 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
11028 isym < isymend;
11029 isym++, pindex++, ppsection++)
11030 {
11031 asection *isec;
11032 const char *name;
11033 Elf_Internal_Sym osym;
11034 long indx;
11035 int ret;
11036
11037 *pindex = -1;
11038
11039 if (elf_bad_symtab (input_bfd))
11040 {
11041 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
11042 {
11043 *ppsection = NULL;
11044 continue;
11045 }
11046 }
11047
11048 if (isym->st_shndx == SHN_UNDEF)
11049 isec = bfd_und_section_ptr;
11050 else if (isym->st_shndx == SHN_ABS)
11051 isec = bfd_abs_section_ptr;
11052 else if (isym->st_shndx == SHN_COMMON)
11053 isec = bfd_com_section_ptr;
11054 else
11055 {
11056 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
11057 if (isec == NULL)
11058 {
11059 /* Don't attempt to output symbols with st_shnx in the
11060 reserved range other than SHN_ABS and SHN_COMMON. */
11061 isec = bfd_und_section_ptr;
11062 }
11063 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
11064 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
11065 isym->st_value =
11066 _bfd_merged_section_offset (output_bfd, &isec,
11067 elf_section_data (isec)->sec_info,
11068 isym->st_value);
11069 }
11070
11071 *ppsection = isec;
11072
11073 /* Don't output the first, undefined, symbol. In fact, don't
11074 output any undefined local symbol. */
11075 if (isec == bfd_und_section_ptr)
11076 continue;
11077
11078 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
11079 {
11080 /* We never output section symbols. Instead, we use the
11081 section symbol of the corresponding section in the output
11082 file. */
11083 continue;
11084 }
11085
11086 /* If we are stripping all symbols, we don't want to output this
11087 one. */
11088 if (flinfo->info->strip == strip_all)
11089 continue;
11090
11091 /* If we are discarding all local symbols, we don't want to
11092 output this one. If we are generating a relocatable output
11093 file, then some of the local symbols may be required by
11094 relocs; we output them below as we discover that they are
11095 needed. */
11096 if (flinfo->info->discard == discard_all)
11097 continue;
11098
11099 /* If this symbol is defined in a section which we are
11100 discarding, we don't need to keep it. */
11101 if (isym->st_shndx != SHN_UNDEF
11102 && isym->st_shndx < SHN_LORESERVE
11103 && isec->output_section == NULL
11104 && flinfo->info->non_contiguous_regions
11105 && flinfo->info->non_contiguous_regions_warnings)
11106 {
11107 _bfd_error_handler (_("warning: --enable-non-contiguous-regions "
11108 "discards section `%s' from '%s'\n"),
11109 isec->name, bfd_get_filename (isec->owner));
11110 continue;
11111 }
11112
11113 if (isym->st_shndx != SHN_UNDEF
11114 && isym->st_shndx < SHN_LORESERVE
11115 && bfd_section_removed_from_list (output_bfd,
11116 isec->output_section))
11117 continue;
11118
11119 /* Get the name of the symbol. */
11120 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
11121 isym->st_name);
11122 if (name == NULL)
11123 return false;
11124
11125 /* See if we are discarding symbols with this name. */
11126 if ((flinfo->info->strip == strip_some
11127 && (bfd_hash_lookup (flinfo->info->keep_hash, name, false, false)
11128 == NULL))
11129 || (((flinfo->info->discard == discard_sec_merge
11130 && (isec->flags & SEC_MERGE)
11131 && !bfd_link_relocatable (flinfo->info))
11132 || flinfo->info->discard == discard_l)
11133 && bfd_is_local_label_name (input_bfd, name)))
11134 continue;
11135
11136 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
11137 {
11138 if (input_bfd->lto_output)
11139 /* -flto puts a temp file name here. This means builds
11140 are not reproducible. Discard the symbol. */
11141 continue;
11142 have_file_sym = true;
11143 flinfo->filesym_count += 1;
11144 }
11145 if (!have_file_sym)
11146 {
11147 /* In the absence of debug info, bfd_find_nearest_line uses
11148 FILE symbols to determine the source file for local
11149 function symbols. Provide a FILE symbol here if input
11150 files lack such, so that their symbols won't be
11151 associated with a previous input file. It's not the
11152 source file, but the best we can do. */
11153 const char *filename;
11154 have_file_sym = true;
11155 flinfo->filesym_count += 1;
11156 memset (&osym, 0, sizeof (osym));
11157 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
11158 osym.st_shndx = SHN_ABS;
11159 if (input_bfd->lto_output)
11160 filename = NULL;
11161 else
11162 filename = lbasename (bfd_get_filename (input_bfd));
11163 if (!elf_link_output_symstrtab (flinfo, filename, &osym,
11164 bfd_abs_section_ptr, NULL))
11165 return false;
11166 }
11167
11168 osym = *isym;
11169
11170 /* Adjust the section index for the output file. */
11171 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11172 isec->output_section);
11173 if (osym.st_shndx == SHN_BAD)
11174 return false;
11175
11176 /* ELF symbols in relocatable files are section relative, but
11177 in executable files they are virtual addresses. Note that
11178 this code assumes that all ELF sections have an associated
11179 BFD section with a reasonable value for output_offset; below
11180 we assume that they also have a reasonable value for
11181 output_section. Any special sections must be set up to meet
11182 these requirements. */
11183 osym.st_value += isec->output_offset;
11184 if (!bfd_link_relocatable (flinfo->info))
11185 {
11186 osym.st_value += isec->output_section->vma;
11187 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
11188 {
11189 /* STT_TLS symbols are relative to PT_TLS segment base. */
11190 if (elf_hash_table (flinfo->info)->tls_sec != NULL)
11191 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
11192 else
11193 osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
11194 STT_NOTYPE);
11195 }
11196 }
11197
11198 indx = bfd_get_symcount (output_bfd);
11199 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
11200 if (ret == 0)
11201 return false;
11202 else if (ret == 1)
11203 *pindex = indx;
11204 }
11205
11206 if (bed->s->arch_size == 32)
11207 {
11208 r_type_mask = 0xff;
11209 r_sym_shift = 8;
11210 address_size = 4;
11211 }
11212 else
11213 {
11214 r_type_mask = 0xffffffff;
11215 r_sym_shift = 32;
11216 address_size = 8;
11217 }
11218
11219 /* Relocate the contents of each section. */
11220 sym_hashes = elf_sym_hashes (input_bfd);
11221 for (o = input_bfd->sections; o != NULL; o = o->next)
11222 {
11223 bfd_byte *contents;
11224
11225 if (! o->linker_mark)
11226 {
11227 /* This section was omitted from the link. */
11228 continue;
11229 }
11230
11231 if (!flinfo->info->resolve_section_groups
11232 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
11233 {
11234 /* Deal with the group signature symbol. */
11235 struct bfd_elf_section_data *sec_data = elf_section_data (o);
11236 unsigned long symndx = sec_data->this_hdr.sh_info;
11237 asection *osec = o->output_section;
11238
11239 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
11240 if (symndx >= locsymcount
11241 || (elf_bad_symtab (input_bfd)
11242 && flinfo->sections[symndx] == NULL))
11243 {
11244 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
11245 while (h->root.type == bfd_link_hash_indirect
11246 || h->root.type == bfd_link_hash_warning)
11247 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11248 /* Arrange for symbol to be output. */
11249 h->indx = -2;
11250 elf_section_data (osec)->this_hdr.sh_info = -2;
11251 }
11252 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
11253 {
11254 /* We'll use the output section target_index. */
11255 asection *sec = flinfo->sections[symndx]->output_section;
11256 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
11257 }
11258 else
11259 {
11260 if (flinfo->indices[symndx] == -1)
11261 {
11262 /* Otherwise output the local symbol now. */
11263 Elf_Internal_Sym sym = isymbuf[symndx];
11264 asection *sec = flinfo->sections[symndx]->output_section;
11265 const char *name;
11266 long indx;
11267 int ret;
11268
11269 name = bfd_elf_string_from_elf_section (input_bfd,
11270 symtab_hdr->sh_link,
11271 sym.st_name);
11272 if (name == NULL)
11273 return false;
11274
11275 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11276 sec);
11277 if (sym.st_shndx == SHN_BAD)
11278 return false;
11279
11280 sym.st_value += o->output_offset;
11281
11282 indx = bfd_get_symcount (output_bfd);
11283 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
11284 NULL);
11285 if (ret == 0)
11286 return false;
11287 else if (ret == 1)
11288 flinfo->indices[symndx] = indx;
11289 else
11290 abort ();
11291 }
11292 elf_section_data (osec)->this_hdr.sh_info
11293 = flinfo->indices[symndx];
11294 }
11295 }
11296
11297 if ((o->flags & SEC_HAS_CONTENTS) == 0
11298 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
11299 continue;
11300
11301 if ((o->flags & SEC_LINKER_CREATED) != 0)
11302 {
11303 /* Section was created by _bfd_elf_link_create_dynamic_sections
11304 or somesuch. */
11305 continue;
11306 }
11307
11308 /* Get the contents of the section. They have been cached by a
11309 relaxation routine. Note that o is a section in an input
11310 file, so the contents field will not have been set by any of
11311 the routines which work on output files. */
11312 if (elf_section_data (o)->this_hdr.contents != NULL)
11313 {
11314 contents = elf_section_data (o)->this_hdr.contents;
11315 if (bed->caches_rawsize
11316 && o->rawsize != 0
11317 && o->rawsize < o->size)
11318 {
11319 memcpy (flinfo->contents, contents, o->rawsize);
11320 contents = flinfo->contents;
11321 }
11322 }
11323 else
11324 {
11325 contents = flinfo->contents;
11326 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
11327 return false;
11328 }
11329
11330 if ((o->flags & SEC_RELOC) != 0)
11331 {
11332 Elf_Internal_Rela *internal_relocs;
11333 Elf_Internal_Rela *rel, *relend;
11334 int action_discarded;
11335 int ret;
11336
11337 /* Get the swapped relocs. */
11338 internal_relocs
11339 = _bfd_elf_link_info_read_relocs (input_bfd, flinfo->info, o,
11340 flinfo->external_relocs,
11341 flinfo->internal_relocs,
11342 false);
11343 if (internal_relocs == NULL
11344 && o->reloc_count > 0)
11345 return false;
11346
11347 action_discarded = -1;
11348 if (!elf_section_ignore_discarded_relocs (o))
11349 action_discarded = (*bed->action_discarded) (o);
11350
11351 /* Run through the relocs evaluating complex reloc symbols and
11352 looking for relocs against symbols from discarded sections
11353 or section symbols from removed link-once sections.
11354 Complain about relocs against discarded sections. Zero
11355 relocs against removed link-once sections. */
11356
11357 rel = internal_relocs;
11358 relend = rel + o->reloc_count;
11359 for ( ; rel < relend; rel++)
11360 {
11361 unsigned long r_symndx = rel->r_info >> r_sym_shift;
11362 unsigned int s_type;
11363 asection **ps, *sec;
11364 struct elf_link_hash_entry *h = NULL;
11365 const char *sym_name;
11366
11367 if (r_symndx == STN_UNDEF)
11368 continue;
11369
11370 if (r_symndx >= locsymcount
11371 || (elf_bad_symtab (input_bfd)
11372 && flinfo->sections[r_symndx] == NULL))
11373 {
11374 h = sym_hashes[r_symndx - extsymoff];
11375
11376 /* Badly formatted input files can contain relocs that
11377 reference non-existant symbols. Check here so that
11378 we do not seg fault. */
11379 if (h == NULL)
11380 {
11381 _bfd_error_handler
11382 /* xgettext:c-format */
11383 (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
11384 "that references a non-existent global symbol"),
11385 input_bfd, (uint64_t) rel->r_info, o);
11386 bfd_set_error (bfd_error_bad_value);
11387 return false;
11388 }
11389
11390 while (h->root.type == bfd_link_hash_indirect
11391 || h->root.type == bfd_link_hash_warning)
11392 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11393
11394 s_type = h->type;
11395
11396 /* If a plugin symbol is referenced from a non-IR file,
11397 mark the symbol as undefined. Note that the
11398 linker may attach linker created dynamic sections
11399 to the plugin bfd. Symbols defined in linker
11400 created sections are not plugin symbols. */
11401 if ((h->root.non_ir_ref_regular
11402 || h->root.non_ir_ref_dynamic)
11403 && (h->root.type == bfd_link_hash_defined
11404 || h->root.type == bfd_link_hash_defweak)
11405 && (h->root.u.def.section->flags
11406 & SEC_LINKER_CREATED) == 0
11407 && h->root.u.def.section->owner != NULL
11408 && (h->root.u.def.section->owner->flags
11409 & BFD_PLUGIN) != 0)
11410 {
11411 h->root.type = bfd_link_hash_undefined;
11412 h->root.u.undef.abfd = h->root.u.def.section->owner;
11413 }
11414
11415 ps = NULL;
11416 if (h->root.type == bfd_link_hash_defined
11417 || h->root.type == bfd_link_hash_defweak)
11418 ps = &h->root.u.def.section;
11419
11420 sym_name = h->root.root.string;
11421 }
11422 else
11423 {
11424 Elf_Internal_Sym *sym = isymbuf + r_symndx;
11425
11426 s_type = ELF_ST_TYPE (sym->st_info);
11427 ps = &flinfo->sections[r_symndx];
11428 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
11429 sym, *ps);
11430 }
11431
11432 if ((s_type == STT_RELC || s_type == STT_SRELC)
11433 && !bfd_link_relocatable (flinfo->info))
11434 {
11435 bfd_vma val;
11436 bfd_vma dot = (rel->r_offset
11437 + o->output_offset + o->output_section->vma);
11438 #ifdef DEBUG
11439 printf ("Encountered a complex symbol!");
11440 printf (" (input_bfd %s, section %s, reloc %ld\n",
11441 bfd_get_filename (input_bfd), o->name,
11442 (long) (rel - internal_relocs));
11443 printf (" symbol: idx %8.8lx, name %s\n",
11444 r_symndx, sym_name);
11445 printf (" reloc : info %8.8lx, addr %8.8lx\n",
11446 (unsigned long) rel->r_info,
11447 (unsigned long) rel->r_offset);
11448 #endif
11449 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
11450 isymbuf, locsymcount, s_type == STT_SRELC))
11451 return false;
11452
11453 /* Symbol evaluated OK. Update to absolute value. */
11454 set_symbol_value (input_bfd, isymbuf, locsymcount,
11455 r_symndx, val);
11456 continue;
11457 }
11458
11459 if (action_discarded != -1 && ps != NULL)
11460 {
11461 /* Complain if the definition comes from a
11462 discarded section. */
11463 if ((sec = *ps) != NULL && discarded_section (sec))
11464 {
11465 BFD_ASSERT (r_symndx != STN_UNDEF);
11466 if (action_discarded & COMPLAIN)
11467 (*flinfo->info->callbacks->einfo)
11468 /* xgettext:c-format */
11469 (_("%X`%s' referenced in section `%pA' of %pB: "
11470 "defined in discarded section `%pA' of %pB\n"),
11471 sym_name, o, input_bfd, sec, sec->owner);
11472
11473 /* Try to do the best we can to support buggy old
11474 versions of gcc. Pretend that the symbol is
11475 really defined in the kept linkonce section.
11476 FIXME: This is quite broken. Modifying the
11477 symbol here means we will be changing all later
11478 uses of the symbol, not just in this section. */
11479 if (action_discarded & PRETEND)
11480 {
11481 asection *kept;
11482
11483 kept = _bfd_elf_check_kept_section (sec,
11484 flinfo->info);
11485 if (kept != NULL)
11486 {
11487 *ps = kept;
11488 continue;
11489 }
11490 }
11491 }
11492 }
11493 }
11494
11495 /* Relocate the section by invoking a back end routine.
11496
11497 The back end routine is responsible for adjusting the
11498 section contents as necessary, and (if using Rela relocs
11499 and generating a relocatable output file) adjusting the
11500 reloc addend as necessary.
11501
11502 The back end routine does not have to worry about setting
11503 the reloc address or the reloc symbol index.
11504
11505 The back end routine is given a pointer to the swapped in
11506 internal symbols, and can access the hash table entries
11507 for the external symbols via elf_sym_hashes (input_bfd).
11508
11509 When generating relocatable output, the back end routine
11510 must handle STB_LOCAL/STT_SECTION symbols specially. The
11511 output symbol is going to be a section symbol
11512 corresponding to the output section, which will require
11513 the addend to be adjusted. */
11514
11515 ret = (*relocate_section) (output_bfd, flinfo->info,
11516 input_bfd, o, contents,
11517 internal_relocs,
11518 isymbuf,
11519 flinfo->sections);
11520 if (!ret)
11521 return false;
11522
11523 if (ret == 2
11524 || bfd_link_relocatable (flinfo->info)
11525 || flinfo->info->emitrelocations)
11526 {
11527 Elf_Internal_Rela *irela;
11528 Elf_Internal_Rela *irelaend, *irelamid;
11529 bfd_vma last_offset;
11530 struct elf_link_hash_entry **rel_hash;
11531 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
11532 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
11533 unsigned int next_erel;
11534 bool rela_normal;
11535 struct bfd_elf_section_data *esdi, *esdo;
11536
11537 esdi = elf_section_data (o);
11538 esdo = elf_section_data (o->output_section);
11539 rela_normal = false;
11540
11541 /* Adjust the reloc addresses and symbol indices. */
11542
11543 irela = internal_relocs;
11544 irelaend = irela + o->reloc_count;
11545 rel_hash = PTR_ADD (esdo->rel.hashes, esdo->rel.count);
11546 /* We start processing the REL relocs, if any. When we reach
11547 IRELAMID in the loop, we switch to the RELA relocs. */
11548 irelamid = irela;
11549 if (esdi->rel.hdr != NULL)
11550 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
11551 * bed->s->int_rels_per_ext_rel);
11552 rel_hash_list = rel_hash;
11553 rela_hash_list = NULL;
11554 last_offset = o->output_offset;
11555 if (!bfd_link_relocatable (flinfo->info))
11556 last_offset += o->output_section->vma;
11557 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
11558 {
11559 unsigned long r_symndx;
11560 asection *sec;
11561 Elf_Internal_Sym sym;
11562
11563 if (next_erel == bed->s->int_rels_per_ext_rel)
11564 {
11565 rel_hash++;
11566 next_erel = 0;
11567 }
11568
11569 if (irela == irelamid)
11570 {
11571 rel_hash = PTR_ADD (esdo->rela.hashes, esdo->rela.count);
11572 rela_hash_list = rel_hash;
11573 rela_normal = bed->rela_normal;
11574 }
11575
11576 irela->r_offset = _bfd_elf_section_offset (output_bfd,
11577 flinfo->info, o,
11578 irela->r_offset);
11579 if (irela->r_offset >= (bfd_vma) -2)
11580 {
11581 /* This is a reloc for a deleted entry or somesuch.
11582 Turn it into an R_*_NONE reloc, at the same
11583 offset as the last reloc. elf_eh_frame.c and
11584 bfd_elf_discard_info rely on reloc offsets
11585 being ordered. */
11586 irela->r_offset = last_offset;
11587 irela->r_info = 0;
11588 irela->r_addend = 0;
11589 continue;
11590 }
11591
11592 irela->r_offset += o->output_offset;
11593
11594 /* Relocs in an executable have to be virtual addresses. */
11595 if (!bfd_link_relocatable (flinfo->info))
11596 irela->r_offset += o->output_section->vma;
11597
11598 last_offset = irela->r_offset;
11599
11600 r_symndx = irela->r_info >> r_sym_shift;
11601 if (r_symndx == STN_UNDEF)
11602 continue;
11603
11604 if (r_symndx >= locsymcount
11605 || (elf_bad_symtab (input_bfd)
11606 && flinfo->sections[r_symndx] == NULL))
11607 {
11608 struct elf_link_hash_entry *rh;
11609 unsigned long indx;
11610
11611 /* This is a reloc against a global symbol. We
11612 have not yet output all the local symbols, so
11613 we do not know the symbol index of any global
11614 symbol. We set the rel_hash entry for this
11615 reloc to point to the global hash table entry
11616 for this symbol. The symbol index is then
11617 set at the end of bfd_elf_final_link. */
11618 indx = r_symndx - extsymoff;
11619 rh = elf_sym_hashes (input_bfd)[indx];
11620 while (rh->root.type == bfd_link_hash_indirect
11621 || rh->root.type == bfd_link_hash_warning)
11622 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
11623
11624 /* Setting the index to -2 tells
11625 elf_link_output_extsym that this symbol is
11626 used by a reloc. */
11627 BFD_ASSERT (rh->indx < 0);
11628 rh->indx = -2;
11629 *rel_hash = rh;
11630
11631 continue;
11632 }
11633
11634 /* This is a reloc against a local symbol. */
11635
11636 *rel_hash = NULL;
11637 sym = isymbuf[r_symndx];
11638 sec = flinfo->sections[r_symndx];
11639 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
11640 {
11641 /* I suppose the backend ought to fill in the
11642 section of any STT_SECTION symbol against a
11643 processor specific section. */
11644 r_symndx = STN_UNDEF;
11645 if (bfd_is_abs_section (sec))
11646 ;
11647 else if (sec == NULL || sec->owner == NULL)
11648 {
11649 bfd_set_error (bfd_error_bad_value);
11650 return false;
11651 }
11652 else
11653 {
11654 asection *osec = sec->output_section;
11655
11656 /* If we have discarded a section, the output
11657 section will be the absolute section. In
11658 case of discarded SEC_MERGE sections, use
11659 the kept section. relocate_section should
11660 have already handled discarded linkonce
11661 sections. */
11662 if (bfd_is_abs_section (osec)
11663 && sec->kept_section != NULL
11664 && sec->kept_section->output_section != NULL)
11665 {
11666 osec = sec->kept_section->output_section;
11667 irela->r_addend -= osec->vma;
11668 }
11669
11670 if (!bfd_is_abs_section (osec))
11671 {
11672 r_symndx = osec->target_index;
11673 if (r_symndx == STN_UNDEF)
11674 {
11675 irela->r_addend += osec->vma;
11676 osec = _bfd_nearby_section (output_bfd, osec,
11677 osec->vma);
11678 irela->r_addend -= osec->vma;
11679 r_symndx = osec->target_index;
11680 }
11681 }
11682 }
11683
11684 /* Adjust the addend according to where the
11685 section winds up in the output section. */
11686 if (rela_normal)
11687 irela->r_addend += sec->output_offset;
11688 }
11689 else
11690 {
11691 if (flinfo->indices[r_symndx] == -1)
11692 {
11693 unsigned long shlink;
11694 const char *name;
11695 asection *osec;
11696 long indx;
11697
11698 if (flinfo->info->strip == strip_all)
11699 {
11700 /* You can't do ld -r -s. */
11701 bfd_set_error (bfd_error_invalid_operation);
11702 return false;
11703 }
11704
11705 /* This symbol was skipped earlier, but
11706 since it is needed by a reloc, we
11707 must output it now. */
11708 shlink = symtab_hdr->sh_link;
11709 name = (bfd_elf_string_from_elf_section
11710 (input_bfd, shlink, sym.st_name));
11711 if (name == NULL)
11712 return false;
11713
11714 osec = sec->output_section;
11715 sym.st_shndx =
11716 _bfd_elf_section_from_bfd_section (output_bfd,
11717 osec);
11718 if (sym.st_shndx == SHN_BAD)
11719 return false;
11720
11721 sym.st_value += sec->output_offset;
11722 if (!bfd_link_relocatable (flinfo->info))
11723 {
11724 sym.st_value += osec->vma;
11725 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
11726 {
11727 struct elf_link_hash_table *htab
11728 = elf_hash_table (flinfo->info);
11729
11730 /* STT_TLS symbols are relative to PT_TLS
11731 segment base. */
11732 if (htab->tls_sec != NULL)
11733 sym.st_value -= htab->tls_sec->vma;
11734 else
11735 sym.st_info
11736 = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
11737 STT_NOTYPE);
11738 }
11739 }
11740
11741 indx = bfd_get_symcount (output_bfd);
11742 ret = elf_link_output_symstrtab (flinfo, name,
11743 &sym, sec,
11744 NULL);
11745 if (ret == 0)
11746 return false;
11747 else if (ret == 1)
11748 flinfo->indices[r_symndx] = indx;
11749 else
11750 abort ();
11751 }
11752
11753 r_symndx = flinfo->indices[r_symndx];
11754 }
11755
11756 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
11757 | (irela->r_info & r_type_mask));
11758 }
11759
11760 /* Swap out the relocs. */
11761 input_rel_hdr = esdi->rel.hdr;
11762 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
11763 {
11764 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11765 input_rel_hdr,
11766 internal_relocs,
11767 rel_hash_list))
11768 return false;
11769 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
11770 * bed->s->int_rels_per_ext_rel);
11771 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
11772 }
11773
11774 input_rela_hdr = esdi->rela.hdr;
11775 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
11776 {
11777 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11778 input_rela_hdr,
11779 internal_relocs,
11780 rela_hash_list))
11781 return false;
11782 }
11783 }
11784 }
11785
11786 /* Write out the modified section contents. */
11787 if (bed->elf_backend_write_section
11788 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
11789 contents))
11790 {
11791 /* Section written out. */
11792 }
11793 else switch (o->sec_info_type)
11794 {
11795 case SEC_INFO_TYPE_STABS:
11796 if (! (_bfd_write_section_stabs
11797 (output_bfd,
11798 &elf_hash_table (flinfo->info)->stab_info,
11799 o, &elf_section_data (o)->sec_info, contents)))
11800 return false;
11801 break;
11802 case SEC_INFO_TYPE_MERGE:
11803 if (! _bfd_write_merged_section (output_bfd, o,
11804 elf_section_data (o)->sec_info))
11805 return false;
11806 break;
11807 case SEC_INFO_TYPE_EH_FRAME:
11808 {
11809 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
11810 o, contents))
11811 return false;
11812 }
11813 break;
11814 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11815 {
11816 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11817 flinfo->info,
11818 o, contents))
11819 return false;
11820 }
11821 break;
11822 default:
11823 {
11824 if (! (o->flags & SEC_EXCLUDE))
11825 {
11826 file_ptr offset = (file_ptr) o->output_offset;
11827 bfd_size_type todo = o->size;
11828
11829 offset *= bfd_octets_per_byte (output_bfd, o);
11830
11831 if ((o->flags & SEC_ELF_REVERSE_COPY)
11832 && o->size > address_size)
11833 {
11834 /* Reverse-copy input section to output. */
11835
11836 if ((o->size & (address_size - 1)) != 0
11837 || (o->reloc_count != 0
11838 && (o->size * bed->s->int_rels_per_ext_rel
11839 != o->reloc_count * address_size)))
11840 {
11841 _bfd_error_handler
11842 /* xgettext:c-format */
11843 (_("error: %pB: size of section %pA is not "
11844 "multiple of address size"),
11845 input_bfd, o);
11846 bfd_set_error (bfd_error_bad_value);
11847 return false;
11848 }
11849
11850 do
11851 {
11852 todo -= address_size;
11853 if (! bfd_set_section_contents (output_bfd,
11854 o->output_section,
11855 contents + todo,
11856 offset,
11857 address_size))
11858 return false;
11859 if (todo == 0)
11860 break;
11861 offset += address_size;
11862 }
11863 while (1);
11864 }
11865 else if (! bfd_set_section_contents (output_bfd,
11866 o->output_section,
11867 contents,
11868 offset, todo))
11869 return false;
11870 }
11871 }
11872 break;
11873 }
11874 }
11875
11876 return true;
11877 }
11878
11879 /* Generate a reloc when linking an ELF file. This is a reloc
11880 requested by the linker, and does not come from any input file. This
11881 is used to build constructor and destructor tables when linking
11882 with -Ur. */
11883
11884 static bool
11885 elf_reloc_link_order (bfd *output_bfd,
11886 struct bfd_link_info *info,
11887 asection *output_section,
11888 struct bfd_link_order *link_order)
11889 {
11890 reloc_howto_type *howto;
11891 long indx;
11892 bfd_vma offset;
11893 bfd_vma addend;
11894 struct bfd_elf_section_reloc_data *reldata;
11895 struct elf_link_hash_entry **rel_hash_ptr;
11896 Elf_Internal_Shdr *rel_hdr;
11897 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11898 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11899 bfd_byte *erel;
11900 unsigned int i;
11901 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
11902
11903 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11904 if (howto == NULL)
11905 {
11906 bfd_set_error (bfd_error_bad_value);
11907 return false;
11908 }
11909
11910 addend = link_order->u.reloc.p->addend;
11911
11912 if (esdo->rel.hdr)
11913 reldata = &esdo->rel;
11914 else if (esdo->rela.hdr)
11915 reldata = &esdo->rela;
11916 else
11917 {
11918 reldata = NULL;
11919 BFD_ASSERT (0);
11920 }
11921
11922 /* Figure out the symbol index. */
11923 rel_hash_ptr = reldata->hashes + reldata->count;
11924 if (link_order->type == bfd_section_reloc_link_order)
11925 {
11926 indx = link_order->u.reloc.p->u.section->target_index;
11927 BFD_ASSERT (indx != 0);
11928 *rel_hash_ptr = NULL;
11929 }
11930 else
11931 {
11932 struct elf_link_hash_entry *h;
11933
11934 /* Treat a reloc against a defined symbol as though it were
11935 actually against the section. */
11936 h = ((struct elf_link_hash_entry *)
11937 bfd_wrapped_link_hash_lookup (output_bfd, info,
11938 link_order->u.reloc.p->u.name,
11939 false, false, true));
11940 if (h != NULL
11941 && (h->root.type == bfd_link_hash_defined
11942 || h->root.type == bfd_link_hash_defweak))
11943 {
11944 asection *section;
11945
11946 section = h->root.u.def.section;
11947 indx = section->output_section->target_index;
11948 *rel_hash_ptr = NULL;
11949 /* It seems that we ought to add the symbol value to the
11950 addend here, but in practice it has already been added
11951 because it was passed to constructor_callback. */
11952 addend += section->output_section->vma + section->output_offset;
11953 }
11954 else if (h != NULL)
11955 {
11956 /* Setting the index to -2 tells elf_link_output_extsym that
11957 this symbol is used by a reloc. */
11958 h->indx = -2;
11959 *rel_hash_ptr = h;
11960 indx = 0;
11961 }
11962 else
11963 {
11964 (*info->callbacks->unattached_reloc)
11965 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
11966 indx = 0;
11967 }
11968 }
11969
11970 /* If this is an inplace reloc, we must write the addend into the
11971 object file. */
11972 if (howto->partial_inplace && addend != 0)
11973 {
11974 bfd_size_type size;
11975 bfd_reloc_status_type rstat;
11976 bfd_byte *buf;
11977 bool ok;
11978 const char *sym_name;
11979 bfd_size_type octets;
11980
11981 size = (bfd_size_type) bfd_get_reloc_size (howto);
11982 buf = (bfd_byte *) bfd_zmalloc (size);
11983 if (buf == NULL && size != 0)
11984 return false;
11985 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11986 switch (rstat)
11987 {
11988 case bfd_reloc_ok:
11989 break;
11990
11991 default:
11992 case bfd_reloc_outofrange:
11993 abort ();
11994
11995 case bfd_reloc_overflow:
11996 if (link_order->type == bfd_section_reloc_link_order)
11997 sym_name = bfd_section_name (link_order->u.reloc.p->u.section);
11998 else
11999 sym_name = link_order->u.reloc.p->u.name;
12000 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
12001 howto->name, addend, NULL, NULL,
12002 (bfd_vma) 0);
12003 break;
12004 }
12005
12006 octets = link_order->offset * bfd_octets_per_byte (output_bfd,
12007 output_section);
12008 ok = bfd_set_section_contents (output_bfd, output_section, buf,
12009 octets, size);
12010 free (buf);
12011 if (! ok)
12012 return false;
12013 }
12014
12015 /* The address of a reloc is relative to the section in a
12016 relocatable file, and is a virtual address in an executable
12017 file. */
12018 offset = link_order->offset;
12019 if (! bfd_link_relocatable (info))
12020 offset += output_section->vma;
12021
12022 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
12023 {
12024 irel[i].r_offset = offset;
12025 irel[i].r_info = 0;
12026 irel[i].r_addend = 0;
12027 }
12028 if (bed->s->arch_size == 32)
12029 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
12030 else
12031 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
12032
12033 rel_hdr = reldata->hdr;
12034 erel = rel_hdr->contents;
12035 if (rel_hdr->sh_type == SHT_REL)
12036 {
12037 erel += reldata->count * bed->s->sizeof_rel;
12038 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
12039 }
12040 else
12041 {
12042 irel[0].r_addend = addend;
12043 erel += reldata->count * bed->s->sizeof_rela;
12044 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
12045 }
12046
12047 ++reldata->count;
12048
12049 return true;
12050 }
12051
12052 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
12053 Returns TRUE upon success, FALSE otherwise. */
12054
12055 static bool
12056 elf_output_implib (bfd *abfd, struct bfd_link_info *info)
12057 {
12058 bool ret = false;
12059 bfd *implib_bfd;
12060 const struct elf_backend_data *bed;
12061 flagword flags;
12062 enum bfd_architecture arch;
12063 unsigned int mach;
12064 asymbol **sympp = NULL;
12065 long symsize;
12066 long symcount;
12067 long src_count;
12068 elf_symbol_type *osymbuf;
12069 size_t amt;
12070
12071 implib_bfd = info->out_implib_bfd;
12072 bed = get_elf_backend_data (abfd);
12073
12074 if (!bfd_set_format (implib_bfd, bfd_object))
12075 return false;
12076
12077 /* Use flag from executable but make it a relocatable object. */
12078 flags = bfd_get_file_flags (abfd);
12079 flags &= ~HAS_RELOC;
12080 if (!bfd_set_start_address (implib_bfd, 0)
12081 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
12082 return false;
12083
12084 /* Copy architecture of output file to import library file. */
12085 arch = bfd_get_arch (abfd);
12086 mach = bfd_get_mach (abfd);
12087 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
12088 && (abfd->target_defaulted
12089 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
12090 return false;
12091
12092 /* Get symbol table size. */
12093 symsize = bfd_get_symtab_upper_bound (abfd);
12094 if (symsize < 0)
12095 return false;
12096
12097 /* Read in the symbol table. */
12098 sympp = (asymbol **) bfd_malloc (symsize);
12099 if (sympp == NULL)
12100 return false;
12101
12102 symcount = bfd_canonicalize_symtab (abfd, sympp);
12103 if (symcount < 0)
12104 goto free_sym_buf;
12105
12106 /* Allow the BFD backend to copy any private header data it
12107 understands from the output BFD to the import library BFD. */
12108 if (! bfd_copy_private_header_data (abfd, implib_bfd))
12109 goto free_sym_buf;
12110
12111 /* Filter symbols to appear in the import library. */
12112 if (bed->elf_backend_filter_implib_symbols)
12113 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
12114 symcount);
12115 else
12116 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
12117 if (symcount == 0)
12118 {
12119 bfd_set_error (bfd_error_no_symbols);
12120 _bfd_error_handler (_("%pB: no symbol found for import library"),
12121 implib_bfd);
12122 goto free_sym_buf;
12123 }
12124
12125
12126 /* Make symbols absolute. */
12127 amt = symcount * sizeof (*osymbuf);
12128 osymbuf = (elf_symbol_type *) bfd_alloc (implib_bfd, amt);
12129 if (osymbuf == NULL)
12130 goto free_sym_buf;
12131
12132 for (src_count = 0; src_count < symcount; src_count++)
12133 {
12134 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
12135 sizeof (*osymbuf));
12136 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
12137 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
12138 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
12139 osymbuf[src_count].internal_elf_sym.st_value =
12140 osymbuf[src_count].symbol.value;
12141 sympp[src_count] = &osymbuf[src_count].symbol;
12142 }
12143
12144 bfd_set_symtab (implib_bfd, sympp, symcount);
12145
12146 /* Allow the BFD backend to copy any private data it understands
12147 from the output BFD to the import library BFD. This is done last
12148 to permit the routine to look at the filtered symbol table. */
12149 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
12150 goto free_sym_buf;
12151
12152 if (!bfd_close (implib_bfd))
12153 goto free_sym_buf;
12154
12155 ret = true;
12156
12157 free_sym_buf:
12158 free (sympp);
12159 return ret;
12160 }
12161
12162 static void
12163 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
12164 {
12165 asection *o;
12166
12167 if (flinfo->symstrtab != NULL)
12168 _bfd_elf_strtab_free (flinfo->symstrtab);
12169 free (flinfo->contents);
12170 free (flinfo->external_relocs);
12171 free (flinfo->internal_relocs);
12172 free (flinfo->external_syms);
12173 free (flinfo->locsym_shndx);
12174 free (flinfo->internal_syms);
12175 free (flinfo->indices);
12176 free (flinfo->sections);
12177 if (flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1)
12178 free (flinfo->symshndxbuf);
12179 for (o = obfd->sections; o != NULL; o = o->next)
12180 {
12181 struct bfd_elf_section_data *esdo = elf_section_data (o);
12182 free (esdo->rel.hashes);
12183 free (esdo->rela.hashes);
12184 }
12185 }
12186
12187 /* Do the final step of an ELF link. */
12188
12189 bool
12190 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
12191 {
12192 bool dynamic;
12193 bool emit_relocs;
12194 bfd *dynobj;
12195 struct elf_final_link_info flinfo;
12196 asection *o;
12197 struct bfd_link_order *p;
12198 bfd *sub;
12199 bfd_size_type max_contents_size;
12200 bfd_size_type max_external_reloc_size;
12201 bfd_size_type max_internal_reloc_count;
12202 bfd_size_type max_sym_count;
12203 bfd_size_type max_sym_shndx_count;
12204 Elf_Internal_Sym elfsym;
12205 unsigned int i;
12206 Elf_Internal_Shdr *symtab_hdr;
12207 Elf_Internal_Shdr *symtab_shndx_hdr;
12208 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12209 struct elf_outext_info eoinfo;
12210 bool merged;
12211 size_t relativecount;
12212 size_t relr_entsize;
12213 asection *reldyn = 0;
12214 bfd_size_type amt;
12215 asection *attr_section = NULL;
12216 bfd_vma attr_size = 0;
12217 const char *std_attrs_section;
12218 struct elf_link_hash_table *htab = elf_hash_table (info);
12219 bool sections_removed;
12220 bool ret;
12221
12222 if (!is_elf_hash_table (&htab->root))
12223 return false;
12224
12225 if (bfd_link_pic (info))
12226 abfd->flags |= DYNAMIC;
12227
12228 dynamic = htab->dynamic_sections_created;
12229 dynobj = htab->dynobj;
12230
12231 emit_relocs = (bfd_link_relocatable (info)
12232 || info->emitrelocations);
12233
12234 memset (&flinfo, 0, sizeof (flinfo));
12235 flinfo.info = info;
12236 flinfo.output_bfd = abfd;
12237 flinfo.symstrtab = _bfd_elf_strtab_init ();
12238 if (flinfo.symstrtab == NULL)
12239 return false;
12240
12241 if (! dynamic)
12242 {
12243 flinfo.hash_sec = NULL;
12244 flinfo.symver_sec = NULL;
12245 }
12246 else
12247 {
12248 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
12249 /* Note that dynsym_sec can be NULL (on VMS). */
12250 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
12251 /* Note that it is OK if symver_sec is NULL. */
12252 }
12253
12254 if (info->unique_symbol
12255 && !bfd_hash_table_init (&flinfo.local_hash_table,
12256 local_hash_newfunc,
12257 sizeof (struct local_hash_entry)))
12258 return false;
12259
12260 /* The object attributes have been merged. Remove the input
12261 sections from the link, and set the contents of the output
12262 section. */
12263 sections_removed = false;
12264 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
12265 for (o = abfd->sections; o != NULL; o = o->next)
12266 {
12267 bool remove_section = false;
12268
12269 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
12270 || strcmp (o->name, ".gnu.attributes") == 0)
12271 {
12272 for (p = o->map_head.link_order; p != NULL; p = p->next)
12273 {
12274 asection *input_section;
12275
12276 if (p->type != bfd_indirect_link_order)
12277 continue;
12278 input_section = p->u.indirect.section;
12279 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12280 elf_link_input_bfd ignores this section. */
12281 input_section->flags &= ~SEC_HAS_CONTENTS;
12282 }
12283
12284 attr_size = bfd_elf_obj_attr_size (abfd);
12285 bfd_set_section_size (o, attr_size);
12286 /* Skip this section later on. */
12287 o->map_head.link_order = NULL;
12288 if (attr_size)
12289 attr_section = o;
12290 else
12291 remove_section = true;
12292 }
12293 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
12294 {
12295 /* Remove empty group section from linker output. */
12296 remove_section = true;
12297 }
12298 if (remove_section)
12299 {
12300 o->flags |= SEC_EXCLUDE;
12301 bfd_section_list_remove (abfd, o);
12302 abfd->section_count--;
12303 sections_removed = true;
12304 }
12305 }
12306 if (sections_removed)
12307 _bfd_fix_excluded_sec_syms (abfd, info);
12308
12309 /* Count up the number of relocations we will output for each output
12310 section, so that we know the sizes of the reloc sections. We
12311 also figure out some maximum sizes. */
12312 max_contents_size = 0;
12313 max_external_reloc_size = 0;
12314 max_internal_reloc_count = 0;
12315 max_sym_count = 0;
12316 max_sym_shndx_count = 0;
12317 merged = false;
12318 for (o = abfd->sections; o != NULL; o = o->next)
12319 {
12320 struct bfd_elf_section_data *esdo = elf_section_data (o);
12321 o->reloc_count = 0;
12322
12323 for (p = o->map_head.link_order; p != NULL; p = p->next)
12324 {
12325 unsigned int reloc_count = 0;
12326 unsigned int additional_reloc_count = 0;
12327 struct bfd_elf_section_data *esdi = NULL;
12328
12329 if (p->type == bfd_section_reloc_link_order
12330 || p->type == bfd_symbol_reloc_link_order)
12331 reloc_count = 1;
12332 else if (p->type == bfd_indirect_link_order)
12333 {
12334 asection *sec;
12335
12336 sec = p->u.indirect.section;
12337
12338 /* Mark all sections which are to be included in the
12339 link. This will normally be every section. We need
12340 to do this so that we can identify any sections which
12341 the linker has decided to not include. */
12342 sec->linker_mark = true;
12343
12344 if (sec->flags & SEC_MERGE)
12345 merged = true;
12346
12347 if (sec->rawsize > max_contents_size)
12348 max_contents_size = sec->rawsize;
12349 if (sec->size > max_contents_size)
12350 max_contents_size = sec->size;
12351
12352 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
12353 && (sec->owner->flags & DYNAMIC) == 0)
12354 {
12355 size_t sym_count;
12356
12357 /* We are interested in just local symbols, not all
12358 symbols. */
12359 if (elf_bad_symtab (sec->owner))
12360 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
12361 / bed->s->sizeof_sym);
12362 else
12363 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
12364
12365 if (sym_count > max_sym_count)
12366 max_sym_count = sym_count;
12367
12368 if (sym_count > max_sym_shndx_count
12369 && elf_symtab_shndx_list (sec->owner) != NULL)
12370 max_sym_shndx_count = sym_count;
12371
12372 if (esdo->this_hdr.sh_type == SHT_REL
12373 || esdo->this_hdr.sh_type == SHT_RELA)
12374 /* Some backends use reloc_count in relocation sections
12375 to count particular types of relocs. Of course,
12376 reloc sections themselves can't have relocations. */
12377 ;
12378 else if (emit_relocs)
12379 {
12380 reloc_count = sec->reloc_count;
12381 if (bed->elf_backend_count_additional_relocs)
12382 {
12383 int c;
12384 c = (*bed->elf_backend_count_additional_relocs) (sec);
12385 additional_reloc_count += c;
12386 }
12387 }
12388 else if (bed->elf_backend_count_relocs)
12389 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
12390
12391 esdi = elf_section_data (sec);
12392
12393 if ((sec->flags & SEC_RELOC) != 0)
12394 {
12395 size_t ext_size = 0;
12396
12397 if (esdi->rel.hdr != NULL)
12398 ext_size = esdi->rel.hdr->sh_size;
12399 if (esdi->rela.hdr != NULL)
12400 ext_size += esdi->rela.hdr->sh_size;
12401
12402 if (ext_size > max_external_reloc_size)
12403 max_external_reloc_size = ext_size;
12404 if (sec->reloc_count > max_internal_reloc_count)
12405 max_internal_reloc_count = sec->reloc_count;
12406 }
12407 }
12408 }
12409
12410 if (reloc_count == 0)
12411 continue;
12412
12413 reloc_count += additional_reloc_count;
12414 o->reloc_count += reloc_count;
12415
12416 if (p->type == bfd_indirect_link_order && emit_relocs)
12417 {
12418 if (esdi->rel.hdr)
12419 {
12420 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
12421 esdo->rel.count += additional_reloc_count;
12422 }
12423 if (esdi->rela.hdr)
12424 {
12425 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
12426 esdo->rela.count += additional_reloc_count;
12427 }
12428 }
12429 else
12430 {
12431 if (o->use_rela_p)
12432 esdo->rela.count += reloc_count;
12433 else
12434 esdo->rel.count += reloc_count;
12435 }
12436 }
12437
12438 if (o->reloc_count > 0)
12439 o->flags |= SEC_RELOC;
12440 else
12441 {
12442 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12443 set it (this is probably a bug) and if it is set
12444 assign_section_numbers will create a reloc section. */
12445 o->flags &=~ SEC_RELOC;
12446 }
12447
12448 /* If the SEC_ALLOC flag is not set, force the section VMA to
12449 zero. This is done in elf_fake_sections as well, but forcing
12450 the VMA to 0 here will ensure that relocs against these
12451 sections are handled correctly. */
12452 if ((o->flags & SEC_ALLOC) == 0
12453 && ! o->user_set_vma)
12454 o->vma = 0;
12455 }
12456
12457 if (! bfd_link_relocatable (info) && merged)
12458 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
12459
12460 /* Figure out the file positions for everything but the symbol table
12461 and the relocs. We set symcount to force assign_section_numbers
12462 to create a symbol table. */
12463 abfd->symcount = info->strip != strip_all || emit_relocs;
12464 BFD_ASSERT (! abfd->output_has_begun);
12465 if (! _bfd_elf_compute_section_file_positions (abfd, info))
12466 goto error_return;
12467
12468 /* Set sizes, and assign file positions for reloc sections. */
12469 for (o = abfd->sections; o != NULL; o = o->next)
12470 {
12471 struct bfd_elf_section_data *esdo = elf_section_data (o);
12472 if ((o->flags & SEC_RELOC) != 0)
12473 {
12474 if (esdo->rel.hdr
12475 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
12476 goto error_return;
12477
12478 if (esdo->rela.hdr
12479 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
12480 goto error_return;
12481 }
12482
12483 /* _bfd_elf_compute_section_file_positions makes temporary use
12484 of target_index. Reset it. */
12485 o->target_index = 0;
12486
12487 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12488 to count upwards while actually outputting the relocations. */
12489 esdo->rel.count = 0;
12490 esdo->rela.count = 0;
12491
12492 if ((esdo->this_hdr.sh_offset == (file_ptr) -1)
12493 && !bfd_section_is_ctf (o))
12494 {
12495 /* Cache the section contents so that they can be compressed
12496 later. Use bfd_malloc since it will be freed by
12497 bfd_compress_section_contents. */
12498 unsigned char *contents = esdo->this_hdr.contents;
12499 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
12500 abort ();
12501 contents
12502 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
12503 if (contents == NULL)
12504 goto error_return;
12505 esdo->this_hdr.contents = contents;
12506 }
12507 }
12508
12509 /* We have now assigned file positions for all the sections except .symtab,
12510 .strtab, and non-loaded reloc and compressed debugging sections. We start
12511 the .symtab section at the current file position, and write directly to it.
12512 We build the .strtab section in memory. */
12513 abfd->symcount = 0;
12514 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12515 /* sh_name is set in prep_headers. */
12516 symtab_hdr->sh_type = SHT_SYMTAB;
12517 /* sh_flags, sh_addr and sh_size all start off zero. */
12518 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
12519 /* sh_link is set in assign_section_numbers. */
12520 /* sh_info is set below. */
12521 /* sh_offset is set just below. */
12522 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
12523
12524 if (max_sym_count < 20)
12525 max_sym_count = 20;
12526 htab->strtabsize = max_sym_count;
12527 amt = max_sym_count * sizeof (struct elf_sym_strtab);
12528 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
12529 if (htab->strtab == NULL)
12530 goto error_return;
12531 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12532 flinfo.symshndxbuf
12533 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
12534 ? (Elf_External_Sym_Shndx *) -1 : NULL);
12535
12536 if (info->strip != strip_all || emit_relocs)
12537 {
12538 file_ptr off = elf_next_file_pos (abfd);
12539
12540 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, true);
12541
12542 /* Note that at this point elf_next_file_pos (abfd) is
12543 incorrect. We do not yet know the size of the .symtab section.
12544 We correct next_file_pos below, after we do know the size. */
12545
12546 /* Start writing out the symbol table. The first symbol is always a
12547 dummy symbol. */
12548 elfsym.st_value = 0;
12549 elfsym.st_size = 0;
12550 elfsym.st_info = 0;
12551 elfsym.st_other = 0;
12552 elfsym.st_shndx = SHN_UNDEF;
12553 elfsym.st_target_internal = 0;
12554 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
12555 bfd_und_section_ptr, NULL) != 1)
12556 goto error_return;
12557
12558 /* Output a symbol for each section if asked or they are used for
12559 relocs. These symbols usually have no names. We store the
12560 index of each one in the index field of the section, so that
12561 we can find it again when outputting relocs. */
12562
12563 if (bfd_keep_unused_section_symbols (abfd) || emit_relocs)
12564 {
12565 bool name_local_sections
12566 = (bed->elf_backend_name_local_section_symbols
12567 && bed->elf_backend_name_local_section_symbols (abfd));
12568 const char *name = NULL;
12569
12570 elfsym.st_size = 0;
12571 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12572 elfsym.st_other = 0;
12573 elfsym.st_value = 0;
12574 elfsym.st_target_internal = 0;
12575 for (i = 1; i < elf_numsections (abfd); i++)
12576 {
12577 o = bfd_section_from_elf_index (abfd, i);
12578 if (o != NULL)
12579 {
12580 o->target_index = bfd_get_symcount (abfd);
12581 elfsym.st_shndx = i;
12582 if (!bfd_link_relocatable (info))
12583 elfsym.st_value = o->vma;
12584 if (name_local_sections)
12585 name = o->name;
12586 if (elf_link_output_symstrtab (&flinfo, name, &elfsym, o,
12587 NULL) != 1)
12588 goto error_return;
12589 }
12590 }
12591 }
12592 }
12593
12594 /* On some targets like Irix 5 the symbol split between local and global
12595 ones recorded in the sh_info field needs to be done between section
12596 and all other symbols. */
12597 if (bed->elf_backend_elfsym_local_is_section
12598 && bed->elf_backend_elfsym_local_is_section (abfd))
12599 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12600
12601 /* Allocate some memory to hold information read in from the input
12602 files. */
12603 if (max_contents_size != 0)
12604 {
12605 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12606 if (flinfo.contents == NULL)
12607 goto error_return;
12608 }
12609
12610 if (max_external_reloc_size != 0)
12611 {
12612 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
12613 if (flinfo.external_relocs == NULL)
12614 goto error_return;
12615 }
12616
12617 if (max_internal_reloc_count != 0)
12618 {
12619 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
12620 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
12621 if (flinfo.internal_relocs == NULL)
12622 goto error_return;
12623 }
12624
12625 if (max_sym_count != 0)
12626 {
12627 amt = max_sym_count * bed->s->sizeof_sym;
12628 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
12629 if (flinfo.external_syms == NULL)
12630 goto error_return;
12631
12632 amt = max_sym_count * sizeof (Elf_Internal_Sym);
12633 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
12634 if (flinfo.internal_syms == NULL)
12635 goto error_return;
12636
12637 amt = max_sym_count * sizeof (long);
12638 flinfo.indices = (long int *) bfd_malloc (amt);
12639 if (flinfo.indices == NULL)
12640 goto error_return;
12641
12642 amt = max_sym_count * sizeof (asection *);
12643 flinfo.sections = (asection **) bfd_malloc (amt);
12644 if (flinfo.sections == NULL)
12645 goto error_return;
12646 }
12647
12648 if (max_sym_shndx_count != 0)
12649 {
12650 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
12651 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
12652 if (flinfo.locsym_shndx == NULL)
12653 goto error_return;
12654 }
12655
12656 if (htab->tls_sec)
12657 {
12658 bfd_vma base, end = 0; /* Both bytes. */
12659 asection *sec;
12660
12661 for (sec = htab->tls_sec;
12662 sec && (sec->flags & SEC_THREAD_LOCAL);
12663 sec = sec->next)
12664 {
12665 bfd_size_type size = sec->size;
12666 unsigned int opb = bfd_octets_per_byte (abfd, sec);
12667
12668 if (size == 0
12669 && (sec->flags & SEC_HAS_CONTENTS) == 0)
12670 {
12671 struct bfd_link_order *ord = sec->map_tail.link_order;
12672
12673 if (ord != NULL)
12674 size = ord->offset * opb + ord->size;
12675 }
12676 end = sec->vma + size / opb;
12677 }
12678 base = htab->tls_sec->vma;
12679 /* Only align end of TLS section if static TLS doesn't have special
12680 alignment requirements. */
12681 if (bed->static_tls_alignment == 1)
12682 end = align_power (end, htab->tls_sec->alignment_power);
12683 htab->tls_size = end - base;
12684 }
12685
12686 if (!_bfd_elf_fixup_eh_frame_hdr (info))
12687 return false;
12688
12689 /* Finish relative relocations here after regular symbol processing
12690 is finished if DT_RELR is enabled. */
12691 if (info->enable_dt_relr
12692 && bed->finish_relative_relocs
12693 && !bed->finish_relative_relocs (info))
12694 info->callbacks->einfo
12695 (_("%F%P: %pB: failed to finish relative relocations\n"), abfd);
12696
12697 /* Since ELF permits relocations to be against local symbols, we
12698 must have the local symbols available when we do the relocations.
12699 Since we would rather only read the local symbols once, and we
12700 would rather not keep them in memory, we handle all the
12701 relocations for a single input file at the same time.
12702
12703 Unfortunately, there is no way to know the total number of local
12704 symbols until we have seen all of them, and the local symbol
12705 indices precede the global symbol indices. This means that when
12706 we are generating relocatable output, and we see a reloc against
12707 a global symbol, we can not know the symbol index until we have
12708 finished examining all the local symbols to see which ones we are
12709 going to output. To deal with this, we keep the relocations in
12710 memory, and don't output them until the end of the link. This is
12711 an unfortunate waste of memory, but I don't see a good way around
12712 it. Fortunately, it only happens when performing a relocatable
12713 link, which is not the common case. FIXME: If keep_memory is set
12714 we could write the relocs out and then read them again; I don't
12715 know how bad the memory loss will be. */
12716
12717 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12718 sub->output_has_begun = false;
12719 for (o = abfd->sections; o != NULL; o = o->next)
12720 {
12721 for (p = o->map_head.link_order; p != NULL; p = p->next)
12722 {
12723 if (p->type == bfd_indirect_link_order
12724 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12725 == bfd_target_elf_flavour)
12726 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12727 {
12728 if (! sub->output_has_begun)
12729 {
12730 if (! elf_link_input_bfd (&flinfo, sub))
12731 goto error_return;
12732 sub->output_has_begun = true;
12733 }
12734 }
12735 else if (p->type == bfd_section_reloc_link_order
12736 || p->type == bfd_symbol_reloc_link_order)
12737 {
12738 if (! elf_reloc_link_order (abfd, info, o, p))
12739 goto error_return;
12740 }
12741 else
12742 {
12743 if (! _bfd_default_link_order (abfd, info, o, p))
12744 {
12745 if (p->type == bfd_indirect_link_order
12746 && (bfd_get_flavour (sub)
12747 == bfd_target_elf_flavour)
12748 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12749 != bed->s->elfclass))
12750 {
12751 const char *iclass, *oclass;
12752
12753 switch (bed->s->elfclass)
12754 {
12755 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12756 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12757 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12758 default: abort ();
12759 }
12760
12761 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
12762 {
12763 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12764 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12765 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12766 default: abort ();
12767 }
12768
12769 bfd_set_error (bfd_error_wrong_format);
12770 _bfd_error_handler
12771 /* xgettext:c-format */
12772 (_("%pB: file class %s incompatible with %s"),
12773 sub, iclass, oclass);
12774 }
12775
12776 goto error_return;
12777 }
12778 }
12779 }
12780 }
12781
12782 /* Free symbol buffer if needed. */
12783 if (!info->reduce_memory_overheads)
12784 {
12785 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12786 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
12787 {
12788 free (elf_tdata (sub)->symbuf);
12789 elf_tdata (sub)->symbuf = NULL;
12790 }
12791 }
12792
12793 ret = true;
12794
12795 /* Output any global symbols that got converted to local in a
12796 version script or due to symbol visibility. We do this in a
12797 separate step since ELF requires all local symbols to appear
12798 prior to any global symbols. FIXME: We should only do this if
12799 some global symbols were, in fact, converted to become local.
12800 FIXME: Will this work correctly with the Irix 5 linker? */
12801 eoinfo.failed = false;
12802 eoinfo.flinfo = &flinfo;
12803 eoinfo.localsyms = true;
12804 eoinfo.file_sym_done = false;
12805 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12806 if (eoinfo.failed)
12807 {
12808 ret = false;
12809 goto return_local_hash_table;
12810 }
12811
12812 /* If backend needs to output some local symbols not present in the hash
12813 table, do it now. */
12814 if (bed->elf_backend_output_arch_local_syms
12815 && (info->strip != strip_all || emit_relocs))
12816 {
12817 if (! ((*bed->elf_backend_output_arch_local_syms)
12818 (abfd, info, &flinfo, elf_link_output_symstrtab)))
12819 {
12820 ret = false;
12821 goto return_local_hash_table;
12822 }
12823 }
12824
12825 /* That wrote out all the local symbols. Finish up the symbol table
12826 with the global symbols. Even if we want to strip everything we
12827 can, we still need to deal with those global symbols that got
12828 converted to local in a version script. */
12829
12830 /* The sh_info field records the index of the first non local symbol. */
12831 if (!symtab_hdr->sh_info)
12832 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12833
12834 if (dynamic
12835 && htab->dynsym != NULL
12836 && htab->dynsym->output_section != bfd_abs_section_ptr)
12837 {
12838 Elf_Internal_Sym sym;
12839 bfd_byte *dynsym = htab->dynsym->contents;
12840
12841 o = htab->dynsym->output_section;
12842 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
12843
12844 /* Write out the section symbols for the output sections. */
12845 if (bfd_link_pic (info)
12846 || htab->is_relocatable_executable)
12847 {
12848 asection *s;
12849
12850 sym.st_size = 0;
12851 sym.st_name = 0;
12852 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12853 sym.st_other = 0;
12854 sym.st_target_internal = 0;
12855
12856 for (s = abfd->sections; s != NULL; s = s->next)
12857 {
12858 int indx;
12859 bfd_byte *dest;
12860 long dynindx;
12861
12862 dynindx = elf_section_data (s)->dynindx;
12863 if (dynindx <= 0)
12864 continue;
12865 indx = elf_section_data (s)->this_idx;
12866 BFD_ASSERT (indx > 0);
12867 sym.st_shndx = indx;
12868 if (! check_dynsym (abfd, &sym))
12869 {
12870 ret = false;
12871 goto return_local_hash_table;
12872 }
12873 sym.st_value = s->vma;
12874 dest = dynsym + dynindx * bed->s->sizeof_sym;
12875
12876 /* Inform the linker of the addition of this symbol. */
12877
12878 if (info->callbacks->ctf_new_dynsym)
12879 info->callbacks->ctf_new_dynsym (dynindx, &sym);
12880
12881 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12882 }
12883 }
12884
12885 /* Write out the local dynsyms. */
12886 if (htab->dynlocal)
12887 {
12888 struct elf_link_local_dynamic_entry *e;
12889 for (e = htab->dynlocal; e ; e = e->next)
12890 {
12891 asection *s;
12892 bfd_byte *dest;
12893
12894 /* Copy the internal symbol and turn off visibility.
12895 Note that we saved a word of storage and overwrote
12896 the original st_name with the dynstr_index. */
12897 sym = e->isym;
12898 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
12899 sym.st_shndx = SHN_UNDEF;
12900
12901 s = bfd_section_from_elf_index (e->input_bfd,
12902 e->isym.st_shndx);
12903 if (s != NULL
12904 && s->output_section != NULL
12905 && elf_section_data (s->output_section) != NULL)
12906 {
12907 sym.st_shndx =
12908 elf_section_data (s->output_section)->this_idx;
12909 if (! check_dynsym (abfd, &sym))
12910 {
12911 ret = false;
12912 goto return_local_hash_table;
12913 }
12914 sym.st_value = (s->output_section->vma
12915 + s->output_offset
12916 + e->isym.st_value);
12917 }
12918
12919 /* Inform the linker of the addition of this symbol. */
12920
12921 if (info->callbacks->ctf_new_dynsym)
12922 info->callbacks->ctf_new_dynsym (e->dynindx, &sym);
12923
12924 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12925 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12926 }
12927 }
12928 }
12929
12930 /* We get the global symbols from the hash table. */
12931 eoinfo.failed = false;
12932 eoinfo.localsyms = false;
12933 eoinfo.flinfo = &flinfo;
12934 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12935 if (eoinfo.failed)
12936 {
12937 ret = false;
12938 goto return_local_hash_table;
12939 }
12940
12941 /* If backend needs to output some symbols not present in the hash
12942 table, do it now. */
12943 if (bed->elf_backend_output_arch_syms
12944 && (info->strip != strip_all || emit_relocs))
12945 {
12946 if (! ((*bed->elf_backend_output_arch_syms)
12947 (abfd, info, &flinfo, elf_link_output_symstrtab)))
12948 {
12949 ret = false;
12950 goto return_local_hash_table;
12951 }
12952 }
12953
12954 /* Finalize the .strtab section. */
12955 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12956
12957 /* Swap out the .strtab section. */
12958 if (!elf_link_swap_symbols_out (&flinfo))
12959 {
12960 ret = false;
12961 goto return_local_hash_table;
12962 }
12963
12964 /* Now we know the size of the symtab section. */
12965 if (bfd_get_symcount (abfd) > 0)
12966 {
12967 /* Finish up and write out the symbol string table (.strtab)
12968 section. */
12969 Elf_Internal_Shdr *symstrtab_hdr = NULL;
12970 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12971
12972 if (elf_symtab_shndx_list (abfd))
12973 {
12974 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
12975
12976 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12977 {
12978 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12979 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12980 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12981 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12982 symtab_shndx_hdr->sh_size = amt;
12983
12984 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12985 off, true);
12986
12987 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12988 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12989 {
12990 ret = false;
12991 goto return_local_hash_table;
12992 }
12993 }
12994 }
12995
12996 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12997 /* sh_name was set in prep_headers. */
12998 symstrtab_hdr->sh_type = SHT_STRTAB;
12999 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
13000 symstrtab_hdr->sh_addr = 0;
13001 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
13002 symstrtab_hdr->sh_entsize = 0;
13003 symstrtab_hdr->sh_link = 0;
13004 symstrtab_hdr->sh_info = 0;
13005 /* sh_offset is set just below. */
13006 symstrtab_hdr->sh_addralign = 1;
13007
13008 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
13009 off, true);
13010 elf_next_file_pos (abfd) = off;
13011
13012 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
13013 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
13014 {
13015 ret = false;
13016 goto return_local_hash_table;
13017 }
13018 }
13019
13020 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
13021 {
13022 _bfd_error_handler (_("%pB: failed to generate import library"),
13023 info->out_implib_bfd);
13024 ret = false;
13025 goto return_local_hash_table;
13026 }
13027
13028 /* Adjust the relocs to have the correct symbol indices. */
13029 for (o = abfd->sections; o != NULL; o = o->next)
13030 {
13031 struct bfd_elf_section_data *esdo = elf_section_data (o);
13032 bool sort;
13033
13034 if ((o->flags & SEC_RELOC) == 0)
13035 continue;
13036
13037 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
13038 if (esdo->rel.hdr != NULL
13039 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
13040 {
13041 ret = false;
13042 goto return_local_hash_table;
13043 }
13044 if (esdo->rela.hdr != NULL
13045 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
13046 {
13047 ret = false;
13048 goto return_local_hash_table;
13049 }
13050
13051 /* Set the reloc_count field to 0 to prevent write_relocs from
13052 trying to swap the relocs out itself. */
13053 o->reloc_count = 0;
13054 }
13055
13056 relativecount = 0;
13057 if (dynamic && info->combreloc && dynobj != NULL)
13058 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
13059
13060 relr_entsize = 0;
13061 if (htab->srelrdyn != NULL
13062 && htab->srelrdyn->output_section != NULL
13063 && htab->srelrdyn->size != 0)
13064 {
13065 asection *s = htab->srelrdyn->output_section;
13066 relr_entsize = elf_section_data (s)->this_hdr.sh_entsize;
13067 if (relr_entsize == 0)
13068 {
13069 relr_entsize = bed->s->arch_size / 8;
13070 elf_section_data (s)->this_hdr.sh_entsize = relr_entsize;
13071 }
13072 }
13073
13074 /* If we are linking against a dynamic object, or generating a
13075 shared library, finish up the dynamic linking information. */
13076 if (dynamic)
13077 {
13078 bfd_byte *dyncon, *dynconend;
13079
13080 /* Fix up .dynamic entries. */
13081 o = bfd_get_linker_section (dynobj, ".dynamic");
13082 BFD_ASSERT (o != NULL);
13083
13084 dyncon = o->contents;
13085 dynconend = PTR_ADD (o->contents, o->size);
13086 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13087 {
13088 Elf_Internal_Dyn dyn;
13089 const char *name;
13090 unsigned int type;
13091 bfd_size_type sh_size;
13092 bfd_vma sh_addr;
13093
13094 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13095
13096 switch (dyn.d_tag)
13097 {
13098 default:
13099 continue;
13100 case DT_NULL:
13101 if (relativecount != 0)
13102 {
13103 switch (elf_section_data (reldyn)->this_hdr.sh_type)
13104 {
13105 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
13106 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
13107 }
13108 if (dyn.d_tag != DT_NULL
13109 && dynconend - dyncon >= bed->s->sizeof_dyn)
13110 {
13111 dyn.d_un.d_val = relativecount;
13112 relativecount = 0;
13113 break;
13114 }
13115 relativecount = 0;
13116 }
13117 if (relr_entsize != 0)
13118 {
13119 if (dynconend - dyncon >= 3 * bed->s->sizeof_dyn)
13120 {
13121 asection *s = htab->srelrdyn;
13122 dyn.d_tag = DT_RELR;
13123 dyn.d_un.d_ptr
13124 = s->output_section->vma + s->output_offset;
13125 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13126 dyncon += bed->s->sizeof_dyn;
13127
13128 dyn.d_tag = DT_RELRSZ;
13129 dyn.d_un.d_val = s->size;
13130 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13131 dyncon += bed->s->sizeof_dyn;
13132
13133 dyn.d_tag = DT_RELRENT;
13134 dyn.d_un.d_val = relr_entsize;
13135 relr_entsize = 0;
13136 break;
13137 }
13138 relr_entsize = 0;
13139 }
13140 continue;
13141
13142 case DT_INIT:
13143 name = info->init_function;
13144 goto get_sym;
13145 case DT_FINI:
13146 name = info->fini_function;
13147 get_sym:
13148 {
13149 struct elf_link_hash_entry *h;
13150
13151 h = elf_link_hash_lookup (htab, name, false, false, true);
13152 if (h != NULL
13153 && (h->root.type == bfd_link_hash_defined
13154 || h->root.type == bfd_link_hash_defweak))
13155 {
13156 dyn.d_un.d_ptr = h->root.u.def.value;
13157 o = h->root.u.def.section;
13158 if (o->output_section != NULL)
13159 dyn.d_un.d_ptr += (o->output_section->vma
13160 + o->output_offset);
13161 else
13162 {
13163 /* The symbol is imported from another shared
13164 library and does not apply to this one. */
13165 dyn.d_un.d_ptr = 0;
13166 }
13167 break;
13168 }
13169 }
13170 continue;
13171
13172 case DT_PREINIT_ARRAYSZ:
13173 name = ".preinit_array";
13174 goto get_out_size;
13175 case DT_INIT_ARRAYSZ:
13176 name = ".init_array";
13177 goto get_out_size;
13178 case DT_FINI_ARRAYSZ:
13179 name = ".fini_array";
13180 get_out_size:
13181 o = bfd_get_section_by_name (abfd, name);
13182 if (o == NULL)
13183 {
13184 _bfd_error_handler
13185 (_("could not find section %s"), name);
13186 goto error_return;
13187 }
13188 if (o->size == 0)
13189 _bfd_error_handler
13190 (_("warning: %s section has zero size"), name);
13191 dyn.d_un.d_val = o->size;
13192 break;
13193
13194 case DT_PREINIT_ARRAY:
13195 name = ".preinit_array";
13196 goto get_out_vma;
13197 case DT_INIT_ARRAY:
13198 name = ".init_array";
13199 goto get_out_vma;
13200 case DT_FINI_ARRAY:
13201 name = ".fini_array";
13202 get_out_vma:
13203 o = bfd_get_section_by_name (abfd, name);
13204 goto do_vma;
13205
13206 case DT_HASH:
13207 name = ".hash";
13208 goto get_vma;
13209 case DT_GNU_HASH:
13210 name = ".gnu.hash";
13211 goto get_vma;
13212 case DT_STRTAB:
13213 name = ".dynstr";
13214 goto get_vma;
13215 case DT_SYMTAB:
13216 name = ".dynsym";
13217 goto get_vma;
13218 case DT_VERDEF:
13219 name = ".gnu.version_d";
13220 goto get_vma;
13221 case DT_VERNEED:
13222 name = ".gnu.version_r";
13223 goto get_vma;
13224 case DT_VERSYM:
13225 name = ".gnu.version";
13226 get_vma:
13227 o = bfd_get_linker_section (dynobj, name);
13228 do_vma:
13229 if (o == NULL || bfd_is_abs_section (o->output_section))
13230 {
13231 _bfd_error_handler
13232 (_("could not find section %s"), name);
13233 goto error_return;
13234 }
13235 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
13236 {
13237 _bfd_error_handler
13238 (_("warning: section '%s' is being made into a note"), name);
13239 bfd_set_error (bfd_error_nonrepresentable_section);
13240 goto error_return;
13241 }
13242 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
13243 break;
13244
13245 case DT_REL:
13246 case DT_RELA:
13247 case DT_RELSZ:
13248 case DT_RELASZ:
13249 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
13250 type = SHT_REL;
13251 else
13252 type = SHT_RELA;
13253 sh_size = 0;
13254 sh_addr = 0;
13255 for (i = 1; i < elf_numsections (abfd); i++)
13256 {
13257 Elf_Internal_Shdr *hdr;
13258
13259 hdr = elf_elfsections (abfd)[i];
13260 if (hdr->sh_type == type
13261 && (hdr->sh_flags & SHF_ALLOC) != 0)
13262 {
13263 sh_size += hdr->sh_size;
13264 if (sh_addr == 0
13265 || sh_addr > hdr->sh_addr)
13266 sh_addr = hdr->sh_addr;
13267 }
13268 }
13269
13270 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
13271 {
13272 unsigned int opb = bfd_octets_per_byte (abfd, o);
13273
13274 /* Don't count procedure linkage table relocs in the
13275 overall reloc count. */
13276 sh_size -= htab->srelplt->size;
13277 if (sh_size == 0)
13278 /* If the size is zero, make the address zero too.
13279 This is to avoid a glibc bug. If the backend
13280 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
13281 zero, then we'll put DT_RELA at the end of
13282 DT_JMPREL. glibc will interpret the end of
13283 DT_RELA matching the end of DT_JMPREL as the
13284 case where DT_RELA includes DT_JMPREL, and for
13285 LD_BIND_NOW will decide that processing DT_RELA
13286 will process the PLT relocs too. Net result:
13287 No PLT relocs applied. */
13288 sh_addr = 0;
13289
13290 /* If .rela.plt is the first .rela section, exclude
13291 it from DT_RELA. */
13292 else if (sh_addr == (htab->srelplt->output_section->vma
13293 + htab->srelplt->output_offset) * opb)
13294 sh_addr += htab->srelplt->size;
13295 }
13296
13297 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
13298 dyn.d_un.d_val = sh_size;
13299 else
13300 dyn.d_un.d_ptr = sh_addr;
13301 break;
13302 }
13303 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13304 }
13305 }
13306
13307 /* If we have created any dynamic sections, then output them. */
13308 if (dynobj != NULL)
13309 {
13310 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
13311 goto error_return;
13312
13313 /* Check for DT_TEXTREL (late, in case the backend removes it). */
13314 if (bfd_link_textrel_check (info)
13315 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL
13316 && o->size != 0)
13317 {
13318 bfd_byte *dyncon, *dynconend;
13319
13320 dyncon = o->contents;
13321 dynconend = o->contents + o->size;
13322 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13323 {
13324 Elf_Internal_Dyn dyn;
13325
13326 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13327
13328 if (dyn.d_tag == DT_TEXTREL)
13329 {
13330 if (info->textrel_check == textrel_check_error)
13331 info->callbacks->einfo
13332 (_("%P%X: read-only segment has dynamic relocations\n"));
13333 else if (bfd_link_dll (info))
13334 info->callbacks->einfo
13335 (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
13336 else if (bfd_link_pde (info))
13337 info->callbacks->einfo
13338 (_("%P: warning: creating DT_TEXTREL in a PDE\n"));
13339 else
13340 info->callbacks->einfo
13341 (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
13342 break;
13343 }
13344 }
13345 }
13346
13347 for (o = dynobj->sections; o != NULL; o = o->next)
13348 {
13349 if ((o->flags & SEC_HAS_CONTENTS) == 0
13350 || o->size == 0
13351 || o->output_section == bfd_abs_section_ptr)
13352 continue;
13353 if ((o->flags & SEC_LINKER_CREATED) == 0)
13354 {
13355 /* At this point, we are only interested in sections
13356 created by _bfd_elf_link_create_dynamic_sections. */
13357 continue;
13358 }
13359 if (htab->stab_info.stabstr == o)
13360 continue;
13361 if (htab->eh_info.hdr_sec == o)
13362 continue;
13363 if (strcmp (o->name, ".dynstr") != 0)
13364 {
13365 bfd_size_type octets = ((file_ptr) o->output_offset
13366 * bfd_octets_per_byte (abfd, o));
13367 if (!bfd_set_section_contents (abfd, o->output_section,
13368 o->contents, octets, o->size))
13369 goto error_return;
13370 }
13371 else
13372 {
13373 /* The contents of the .dynstr section are actually in a
13374 stringtab. */
13375 file_ptr off;
13376
13377 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
13378 if (bfd_seek (abfd, off, SEEK_SET) != 0
13379 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
13380 goto error_return;
13381 }
13382 }
13383 }
13384
13385 if (!info->resolve_section_groups)
13386 {
13387 bool failed = false;
13388
13389 BFD_ASSERT (bfd_link_relocatable (info));
13390 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
13391 if (failed)
13392 goto error_return;
13393 }
13394
13395 /* If we have optimized stabs strings, output them. */
13396 if (htab->stab_info.stabstr != NULL)
13397 {
13398 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
13399 goto error_return;
13400 }
13401
13402 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
13403 goto error_return;
13404
13405 if (info->callbacks->emit_ctf)
13406 info->callbacks->emit_ctf ();
13407
13408 elf_final_link_free (abfd, &flinfo);
13409
13410 if (attr_section)
13411 {
13412 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
13413 if (contents == NULL)
13414 {
13415 /* Bail out and fail. */
13416 ret = false;
13417 goto return_local_hash_table;
13418 }
13419 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
13420 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
13421 free (contents);
13422 }
13423
13424 return_local_hash_table:
13425 if (info->unique_symbol)
13426 bfd_hash_table_free (&flinfo.local_hash_table);
13427 return ret;
13428
13429 error_return:
13430 elf_final_link_free (abfd, &flinfo);
13431 ret = false;
13432 goto return_local_hash_table;
13433 }
13434 \f
13435 /* Initialize COOKIE for input bfd ABFD. */
13436
13437 static bool
13438 init_reloc_cookie (struct elf_reloc_cookie *cookie,
13439 struct bfd_link_info *info, bfd *abfd)
13440 {
13441 Elf_Internal_Shdr *symtab_hdr;
13442 const struct elf_backend_data *bed;
13443
13444 bed = get_elf_backend_data (abfd);
13445 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13446
13447 cookie->abfd = abfd;
13448 cookie->sym_hashes = elf_sym_hashes (abfd);
13449 cookie->bad_symtab = elf_bad_symtab (abfd);
13450 if (cookie->bad_symtab)
13451 {
13452 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13453 cookie->extsymoff = 0;
13454 }
13455 else
13456 {
13457 cookie->locsymcount = symtab_hdr->sh_info;
13458 cookie->extsymoff = symtab_hdr->sh_info;
13459 }
13460
13461 if (bed->s->arch_size == 32)
13462 cookie->r_sym_shift = 8;
13463 else
13464 cookie->r_sym_shift = 32;
13465
13466 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
13467 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
13468 {
13469 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
13470 cookie->locsymcount, 0,
13471 NULL, NULL, NULL);
13472 if (cookie->locsyms == NULL)
13473 {
13474 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
13475 return false;
13476 }
13477 if (_bfd_link_keep_memory (info) )
13478 {
13479 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
13480 info->cache_size += (cookie->locsymcount
13481 * sizeof (Elf_External_Sym_Shndx));
13482 }
13483 }
13484 return true;
13485 }
13486
13487 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
13488
13489 static void
13490 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
13491 {
13492 Elf_Internal_Shdr *symtab_hdr;
13493
13494 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13495 if (symtab_hdr->contents != (unsigned char *) cookie->locsyms)
13496 free (cookie->locsyms);
13497 }
13498
13499 /* Initialize the relocation information in COOKIE for input section SEC
13500 of input bfd ABFD. */
13501
13502 static bool
13503 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13504 struct bfd_link_info *info, bfd *abfd,
13505 asection *sec)
13506 {
13507 if (sec->reloc_count == 0)
13508 {
13509 cookie->rels = NULL;
13510 cookie->relend = NULL;
13511 }
13512 else
13513 {
13514 cookie->rels = _bfd_elf_link_info_read_relocs (abfd, info, sec,
13515 NULL, NULL,
13516 _bfd_link_keep_memory (info));
13517 if (cookie->rels == NULL)
13518 return false;
13519 cookie->rel = cookie->rels;
13520 cookie->relend = cookie->rels + sec->reloc_count;
13521 }
13522 cookie->rel = cookie->rels;
13523 return true;
13524 }
13525
13526 /* Free the memory allocated by init_reloc_cookie_rels,
13527 if appropriate. */
13528
13529 static void
13530 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13531 asection *sec)
13532 {
13533 if (elf_section_data (sec)->relocs != cookie->rels)
13534 free (cookie->rels);
13535 }
13536
13537 /* Initialize the whole of COOKIE for input section SEC. */
13538
13539 static bool
13540 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13541 struct bfd_link_info *info,
13542 asection *sec)
13543 {
13544 if (!init_reloc_cookie (cookie, info, sec->owner))
13545 goto error1;
13546 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
13547 goto error2;
13548 return true;
13549
13550 error2:
13551 fini_reloc_cookie (cookie, sec->owner);
13552 error1:
13553 return false;
13554 }
13555
13556 /* Free the memory allocated by init_reloc_cookie_for_section,
13557 if appropriate. */
13558
13559 static void
13560 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13561 asection *sec)
13562 {
13563 fini_reloc_cookie_rels (cookie, sec);
13564 fini_reloc_cookie (cookie, sec->owner);
13565 }
13566 \f
13567 /* Garbage collect unused sections. */
13568
13569 /* Default gc_mark_hook. */
13570
13571 asection *
13572 _bfd_elf_gc_mark_hook (asection *sec,
13573 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13574 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13575 struct elf_link_hash_entry *h,
13576 Elf_Internal_Sym *sym)
13577 {
13578 if (h != NULL)
13579 {
13580 switch (h->root.type)
13581 {
13582 case bfd_link_hash_defined:
13583 case bfd_link_hash_defweak:
13584 return h->root.u.def.section;
13585
13586 case bfd_link_hash_common:
13587 return h->root.u.c.p->section;
13588
13589 default:
13590 break;
13591 }
13592 }
13593 else
13594 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
13595
13596 return NULL;
13597 }
13598
13599 /* Return the debug definition section. */
13600
13601 static asection *
13602 elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
13603 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13604 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13605 struct elf_link_hash_entry *h,
13606 Elf_Internal_Sym *sym)
13607 {
13608 if (h != NULL)
13609 {
13610 /* Return the global debug definition section. */
13611 if ((h->root.type == bfd_link_hash_defined
13612 || h->root.type == bfd_link_hash_defweak)
13613 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
13614 return h->root.u.def.section;
13615 }
13616 else
13617 {
13618 /* Return the local debug definition section. */
13619 asection *isec = bfd_section_from_elf_index (sec->owner,
13620 sym->st_shndx);
13621 if ((isec->flags & SEC_DEBUGGING) != 0)
13622 return isec;
13623 }
13624
13625 return NULL;
13626 }
13627
13628 /* COOKIE->rel describes a relocation against section SEC, which is
13629 a section we've decided to keep. Return the section that contains
13630 the relocation symbol, or NULL if no section contains it. */
13631
13632 asection *
13633 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
13634 elf_gc_mark_hook_fn gc_mark_hook,
13635 struct elf_reloc_cookie *cookie,
13636 bool *start_stop)
13637 {
13638 unsigned long r_symndx;
13639 struct elf_link_hash_entry *h, *hw;
13640
13641 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
13642 if (r_symndx == STN_UNDEF)
13643 return NULL;
13644
13645 if (r_symndx >= cookie->locsymcount
13646 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13647 {
13648 bool was_marked;
13649
13650 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
13651 if (h == NULL)
13652 {
13653 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
13654 sec->owner);
13655 return NULL;
13656 }
13657 while (h->root.type == bfd_link_hash_indirect
13658 || h->root.type == bfd_link_hash_warning)
13659 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13660
13661 was_marked = h->mark;
13662 h->mark = 1;
13663 /* Keep all aliases of the symbol too. If an object symbol
13664 needs to be copied into .dynbss then all of its aliases
13665 should be present as dynamic symbols, not just the one used
13666 on the copy relocation. */
13667 hw = h;
13668 while (hw->is_weakalias)
13669 {
13670 hw = hw->u.alias;
13671 hw->mark = 1;
13672 }
13673
13674 if (!was_marked && h->start_stop && !h->root.ldscript_def)
13675 {
13676 if (info->start_stop_gc)
13677 return NULL;
13678
13679 /* To work around a glibc bug, mark XXX input sections
13680 when there is a reference to __start_XXX or __stop_XXX
13681 symbols. */
13682 else if (start_stop != NULL)
13683 {
13684 asection *s = h->u2.start_stop_section;
13685 *start_stop = true;
13686 return s;
13687 }
13688 }
13689
13690 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
13691 }
13692
13693 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
13694 &cookie->locsyms[r_symndx]);
13695 }
13696
13697 /* COOKIE->rel describes a relocation against section SEC, which is
13698 a section we've decided to keep. Mark the section that contains
13699 the relocation symbol. */
13700
13701 bool
13702 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
13703 asection *sec,
13704 elf_gc_mark_hook_fn gc_mark_hook,
13705 struct elf_reloc_cookie *cookie)
13706 {
13707 asection *rsec;
13708 bool start_stop = false;
13709
13710 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
13711 while (rsec != NULL)
13712 {
13713 if (!rsec->gc_mark)
13714 {
13715 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
13716 || (rsec->owner->flags & DYNAMIC) != 0)
13717 rsec->gc_mark = 1;
13718 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
13719 return false;
13720 }
13721 if (!start_stop)
13722 break;
13723 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
13724 }
13725 return true;
13726 }
13727
13728 /* The mark phase of garbage collection. For a given section, mark
13729 it and any sections in this section's group, and all the sections
13730 which define symbols to which it refers. */
13731
13732 bool
13733 _bfd_elf_gc_mark (struct bfd_link_info *info,
13734 asection *sec,
13735 elf_gc_mark_hook_fn gc_mark_hook)
13736 {
13737 bool ret;
13738 asection *group_sec, *eh_frame;
13739
13740 sec->gc_mark = 1;
13741
13742 /* Mark all the sections in the group. */
13743 group_sec = elf_section_data (sec)->next_in_group;
13744 if (group_sec && !group_sec->gc_mark)
13745 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
13746 return false;
13747
13748 /* Look through the section relocs. */
13749 ret = true;
13750 eh_frame = elf_eh_frame_section (sec->owner);
13751 if ((sec->flags & SEC_RELOC) != 0
13752 && sec->reloc_count > 0
13753 && sec != eh_frame)
13754 {
13755 struct elf_reloc_cookie cookie;
13756
13757 if (!init_reloc_cookie_for_section (&cookie, info, sec))
13758 ret = false;
13759 else
13760 {
13761 for (; cookie.rel < cookie.relend; cookie.rel++)
13762 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
13763 {
13764 ret = false;
13765 break;
13766 }
13767 fini_reloc_cookie_for_section (&cookie, sec);
13768 }
13769 }
13770
13771 if (ret && eh_frame && elf_fde_list (sec))
13772 {
13773 struct elf_reloc_cookie cookie;
13774
13775 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
13776 ret = false;
13777 else
13778 {
13779 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
13780 gc_mark_hook, &cookie))
13781 ret = false;
13782 fini_reloc_cookie_for_section (&cookie, eh_frame);
13783 }
13784 }
13785
13786 eh_frame = elf_section_eh_frame_entry (sec);
13787 if (ret && eh_frame && !eh_frame->gc_mark)
13788 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
13789 ret = false;
13790
13791 return ret;
13792 }
13793
13794 /* Scan and mark sections in a special or debug section group. */
13795
13796 static void
13797 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
13798 {
13799 /* Point to first section of section group. */
13800 asection *ssec;
13801 /* Used to iterate the section group. */
13802 asection *msec;
13803
13804 bool is_special_grp = true;
13805 bool is_debug_grp = true;
13806
13807 /* First scan to see if group contains any section other than debug
13808 and special section. */
13809 ssec = msec = elf_next_in_group (grp);
13810 do
13811 {
13812 if ((msec->flags & SEC_DEBUGGING) == 0)
13813 is_debug_grp = false;
13814
13815 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
13816 is_special_grp = false;
13817
13818 msec = elf_next_in_group (msec);
13819 }
13820 while (msec != ssec);
13821
13822 /* If this is a pure debug section group or pure special section group,
13823 keep all sections in this group. */
13824 if (is_debug_grp || is_special_grp)
13825 {
13826 do
13827 {
13828 msec->gc_mark = 1;
13829 msec = elf_next_in_group (msec);
13830 }
13831 while (msec != ssec);
13832 }
13833 }
13834
13835 /* Keep debug and special sections. */
13836
13837 bool
13838 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
13839 elf_gc_mark_hook_fn mark_hook)
13840 {
13841 bfd *ibfd;
13842
13843 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13844 {
13845 asection *isec;
13846 bool some_kept;
13847 bool debug_frag_seen;
13848 bool has_kept_debug_info;
13849
13850 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13851 continue;
13852 isec = ibfd->sections;
13853 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13854 continue;
13855
13856 /* Ensure all linker created sections are kept,
13857 see if any other section is already marked,
13858 and note if we have any fragmented debug sections. */
13859 debug_frag_seen = some_kept = has_kept_debug_info = false;
13860 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13861 {
13862 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13863 isec->gc_mark = 1;
13864 else if (isec->gc_mark
13865 && (isec->flags & SEC_ALLOC) != 0
13866 && elf_section_type (isec) != SHT_NOTE)
13867 some_kept = true;
13868 else
13869 {
13870 /* Since all sections, except for backend specific ones,
13871 have been garbage collected, call mark_hook on this
13872 section if any of its linked-to sections is marked. */
13873 asection *linked_to_sec;
13874 for (linked_to_sec = elf_linked_to_section (isec);
13875 linked_to_sec != NULL && !linked_to_sec->linker_mark;
13876 linked_to_sec = elf_linked_to_section (linked_to_sec))
13877 {
13878 if (linked_to_sec->gc_mark)
13879 {
13880 if (!_bfd_elf_gc_mark (info, isec, mark_hook))
13881 return false;
13882 break;
13883 }
13884 linked_to_sec->linker_mark = 1;
13885 }
13886 for (linked_to_sec = elf_linked_to_section (isec);
13887 linked_to_sec != NULL && linked_to_sec->linker_mark;
13888 linked_to_sec = elf_linked_to_section (linked_to_sec))
13889 linked_to_sec->linker_mark = 0;
13890 }
13891
13892 if (!debug_frag_seen
13893 && (isec->flags & SEC_DEBUGGING)
13894 && startswith (isec->name, ".debug_line."))
13895 debug_frag_seen = true;
13896 else if (strcmp (bfd_section_name (isec),
13897 "__patchable_function_entries") == 0
13898 && elf_linked_to_section (isec) == NULL)
13899 info->callbacks->einfo (_("%F%P: %pB(%pA): error: "
13900 "need linked-to section "
13901 "for --gc-sections\n"),
13902 isec->owner, isec);
13903 }
13904
13905 /* If no non-note alloc section in this file will be kept, then
13906 we can toss out the debug and special sections. */
13907 if (!some_kept)
13908 continue;
13909
13910 /* Keep debug and special sections like .comment when they are
13911 not part of a group. Also keep section groups that contain
13912 just debug sections or special sections. NB: Sections with
13913 linked-to section has been handled above. */
13914 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13915 {
13916 if ((isec->flags & SEC_GROUP) != 0)
13917 _bfd_elf_gc_mark_debug_special_section_group (isec);
13918 else if (((isec->flags & SEC_DEBUGGING) != 0
13919 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
13920 && elf_next_in_group (isec) == NULL
13921 && elf_linked_to_section (isec) == NULL)
13922 isec->gc_mark = 1;
13923 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
13924 has_kept_debug_info = true;
13925 }
13926
13927 /* Look for CODE sections which are going to be discarded,
13928 and find and discard any fragmented debug sections which
13929 are associated with that code section. */
13930 if (debug_frag_seen)
13931 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13932 if ((isec->flags & SEC_CODE) != 0
13933 && isec->gc_mark == 0)
13934 {
13935 unsigned int ilen;
13936 asection *dsec;
13937
13938 ilen = strlen (isec->name);
13939
13940 /* Association is determined by the name of the debug
13941 section containing the name of the code section as
13942 a suffix. For example .debug_line.text.foo is a
13943 debug section associated with .text.foo. */
13944 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
13945 {
13946 unsigned int dlen;
13947
13948 if (dsec->gc_mark == 0
13949 || (dsec->flags & SEC_DEBUGGING) == 0)
13950 continue;
13951
13952 dlen = strlen (dsec->name);
13953
13954 if (dlen > ilen
13955 && strncmp (dsec->name + (dlen - ilen),
13956 isec->name, ilen) == 0)
13957 dsec->gc_mark = 0;
13958 }
13959 }
13960
13961 /* Mark debug sections referenced by kept debug sections. */
13962 if (has_kept_debug_info)
13963 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13964 if (isec->gc_mark
13965 && (isec->flags & SEC_DEBUGGING) != 0)
13966 if (!_bfd_elf_gc_mark (info, isec,
13967 elf_gc_mark_debug_section))
13968 return false;
13969 }
13970 return true;
13971 }
13972
13973 static bool
13974 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
13975 {
13976 bfd *sub;
13977 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13978
13979 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13980 {
13981 asection *o;
13982
13983 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13984 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
13985 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13986 continue;
13987 o = sub->sections;
13988 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13989 continue;
13990
13991 for (o = sub->sections; o != NULL; o = o->next)
13992 {
13993 /* When any section in a section group is kept, we keep all
13994 sections in the section group. If the first member of
13995 the section group is excluded, we will also exclude the
13996 group section. */
13997 if (o->flags & SEC_GROUP)
13998 {
13999 asection *first = elf_next_in_group (o);
14000 o->gc_mark = first->gc_mark;
14001 }
14002
14003 if (o->gc_mark)
14004 continue;
14005
14006 /* Skip sweeping sections already excluded. */
14007 if (o->flags & SEC_EXCLUDE)
14008 continue;
14009
14010 /* Since this is early in the link process, it is simple
14011 to remove a section from the output. */
14012 o->flags |= SEC_EXCLUDE;
14013
14014 if (info->print_gc_sections && o->size != 0)
14015 /* xgettext:c-format */
14016 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
14017 o, sub);
14018 }
14019 }
14020
14021 return true;
14022 }
14023
14024 /* Propagate collected vtable information. This is called through
14025 elf_link_hash_traverse. */
14026
14027 static bool
14028 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
14029 {
14030 /* Those that are not vtables. */
14031 if (h->start_stop
14032 || h->u2.vtable == NULL
14033 || h->u2.vtable->parent == NULL)
14034 return true;
14035
14036 /* Those vtables that do not have parents, we cannot merge. */
14037 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
14038 return true;
14039
14040 /* If we've already been done, exit. */
14041 if (h->u2.vtable->used && h->u2.vtable->used[-1])
14042 return true;
14043
14044 /* Make sure the parent's table is up to date. */
14045 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
14046
14047 if (h->u2.vtable->used == NULL)
14048 {
14049 /* None of this table's entries were referenced. Re-use the
14050 parent's table. */
14051 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
14052 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
14053 }
14054 else
14055 {
14056 size_t n;
14057 bool *cu, *pu;
14058
14059 /* Or the parent's entries into ours. */
14060 cu = h->u2.vtable->used;
14061 cu[-1] = true;
14062 pu = h->u2.vtable->parent->u2.vtable->used;
14063 if (pu != NULL)
14064 {
14065 const struct elf_backend_data *bed;
14066 unsigned int log_file_align;
14067
14068 bed = get_elf_backend_data (h->root.u.def.section->owner);
14069 log_file_align = bed->s->log_file_align;
14070 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
14071 while (n--)
14072 {
14073 if (*pu)
14074 *cu = true;
14075 pu++;
14076 cu++;
14077 }
14078 }
14079 }
14080
14081 return true;
14082 }
14083
14084 struct link_info_ok
14085 {
14086 struct bfd_link_info *info;
14087 bool ok;
14088 };
14089
14090 static bool
14091 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h,
14092 void *ptr)
14093 {
14094 asection *sec;
14095 bfd_vma hstart, hend;
14096 Elf_Internal_Rela *relstart, *relend, *rel;
14097 const struct elf_backend_data *bed;
14098 unsigned int log_file_align;
14099 struct link_info_ok *info = (struct link_info_ok *) ptr;
14100
14101 /* Take care of both those symbols that do not describe vtables as
14102 well as those that are not loaded. */
14103 if (h->start_stop
14104 || h->u2.vtable == NULL
14105 || h->u2.vtable->parent == NULL)
14106 return true;
14107
14108 BFD_ASSERT (h->root.type == bfd_link_hash_defined
14109 || h->root.type == bfd_link_hash_defweak);
14110
14111 sec = h->root.u.def.section;
14112 hstart = h->root.u.def.value;
14113 hend = hstart + h->size;
14114
14115 relstart = _bfd_elf_link_info_read_relocs (sec->owner, info->info,
14116 sec, NULL, NULL, true);
14117 if (!relstart)
14118 return info->ok = false;
14119 bed = get_elf_backend_data (sec->owner);
14120 log_file_align = bed->s->log_file_align;
14121
14122 relend = relstart + sec->reloc_count;
14123
14124 for (rel = relstart; rel < relend; ++rel)
14125 if (rel->r_offset >= hstart && rel->r_offset < hend)
14126 {
14127 /* If the entry is in use, do nothing. */
14128 if (h->u2.vtable->used
14129 && (rel->r_offset - hstart) < h->u2.vtable->size)
14130 {
14131 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
14132 if (h->u2.vtable->used[entry])
14133 continue;
14134 }
14135 /* Otherwise, kill it. */
14136 rel->r_offset = rel->r_info = rel->r_addend = 0;
14137 }
14138
14139 return true;
14140 }
14141
14142 /* Mark sections containing dynamically referenced symbols. When
14143 building shared libraries, we must assume that any visible symbol is
14144 referenced. */
14145
14146 bool
14147 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
14148 {
14149 struct bfd_link_info *info = (struct bfd_link_info *) inf;
14150 struct bfd_elf_dynamic_list *d = info->dynamic_list;
14151
14152 if ((h->root.type == bfd_link_hash_defined
14153 || h->root.type == bfd_link_hash_defweak)
14154 && (!h->start_stop
14155 || h->root.ldscript_def
14156 || !info->start_stop_gc)
14157 && ((h->ref_dynamic && !h->forced_local)
14158 || ((h->def_regular || ELF_COMMON_DEF_P (h))
14159 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
14160 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
14161 && (!bfd_link_executable (info)
14162 || info->gc_keep_exported
14163 || info->export_dynamic
14164 || (h->dynamic
14165 && d != NULL
14166 && (*d->match) (&d->head, NULL, h->root.root.string)))
14167 && (h->versioned >= versioned
14168 || !bfd_hide_sym_by_version (info->version_info,
14169 h->root.root.string)))))
14170 h->root.u.def.section->flags |= SEC_KEEP;
14171
14172 return true;
14173 }
14174
14175 /* Keep all sections containing symbols undefined on the command-line,
14176 and the section containing the entry symbol. */
14177
14178 void
14179 _bfd_elf_gc_keep (struct bfd_link_info *info)
14180 {
14181 struct bfd_sym_chain *sym;
14182
14183 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
14184 {
14185 struct elf_link_hash_entry *h;
14186
14187 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
14188 false, false, false);
14189
14190 if (h != NULL
14191 && (h->root.type == bfd_link_hash_defined
14192 || h->root.type == bfd_link_hash_defweak)
14193 && !bfd_is_const_section (h->root.u.def.section))
14194 h->root.u.def.section->flags |= SEC_KEEP;
14195 }
14196 }
14197
14198 bool
14199 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
14200 struct bfd_link_info *info)
14201 {
14202 bfd *ibfd = info->input_bfds;
14203
14204 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14205 {
14206 asection *sec;
14207 struct elf_reloc_cookie cookie;
14208
14209 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
14210 continue;
14211 sec = ibfd->sections;
14212 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14213 continue;
14214
14215 if (!init_reloc_cookie (&cookie, info, ibfd))
14216 return false;
14217
14218 for (sec = ibfd->sections; sec; sec = sec->next)
14219 {
14220 if (startswith (bfd_section_name (sec), ".eh_frame_entry")
14221 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
14222 {
14223 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
14224 fini_reloc_cookie_rels (&cookie, sec);
14225 }
14226 }
14227 }
14228 return true;
14229 }
14230
14231 /* Do mark and sweep of unused sections. */
14232
14233 bool
14234 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
14235 {
14236 bool ok = true;
14237 bfd *sub;
14238 elf_gc_mark_hook_fn gc_mark_hook;
14239 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14240 struct elf_link_hash_table *htab;
14241 struct link_info_ok info_ok;
14242
14243 if (!bed->can_gc_sections
14244 || !is_elf_hash_table (info->hash))
14245 {
14246 _bfd_error_handler(_("warning: gc-sections option ignored"));
14247 return true;
14248 }
14249
14250 bed->gc_keep (info);
14251 htab = elf_hash_table (info);
14252
14253 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
14254 at the .eh_frame section if we can mark the FDEs individually. */
14255 for (sub = info->input_bfds;
14256 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
14257 sub = sub->link.next)
14258 {
14259 asection *sec;
14260 struct elf_reloc_cookie cookie;
14261
14262 sec = sub->sections;
14263 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14264 continue;
14265 sec = bfd_get_section_by_name (sub, ".eh_frame");
14266 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
14267 {
14268 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
14269 if (elf_section_data (sec)->sec_info
14270 && (sec->flags & SEC_LINKER_CREATED) == 0)
14271 elf_eh_frame_section (sub) = sec;
14272 fini_reloc_cookie_for_section (&cookie, sec);
14273 sec = bfd_get_next_section_by_name (NULL, sec);
14274 }
14275 }
14276
14277 /* Apply transitive closure to the vtable entry usage info. */
14278 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
14279 if (!ok)
14280 return false;
14281
14282 /* Kill the vtable relocations that were not used. */
14283 info_ok.info = info;
14284 info_ok.ok = true;
14285 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &info_ok);
14286 if (!info_ok.ok)
14287 return false;
14288
14289 /* Mark dynamically referenced symbols. */
14290 if (htab->dynamic_sections_created || info->gc_keep_exported)
14291 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
14292
14293 /* Grovel through relocs to find out who stays ... */
14294 gc_mark_hook = bed->gc_mark_hook;
14295 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
14296 {
14297 asection *o;
14298
14299 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
14300 || elf_object_id (sub) != elf_hash_table_id (htab)
14301 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
14302 continue;
14303
14304 o = sub->sections;
14305 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14306 continue;
14307
14308 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
14309 Also treat note sections as a root, if the section is not part
14310 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
14311 well as FINI_ARRAY sections for ld -r. */
14312 for (o = sub->sections; o != NULL; o = o->next)
14313 if (!o->gc_mark
14314 && (o->flags & SEC_EXCLUDE) == 0
14315 && ((o->flags & SEC_KEEP) != 0
14316 || (bfd_link_relocatable (info)
14317 && ((elf_section_data (o)->this_hdr.sh_type
14318 == SHT_PREINIT_ARRAY)
14319 || (elf_section_data (o)->this_hdr.sh_type
14320 == SHT_INIT_ARRAY)
14321 || (elf_section_data (o)->this_hdr.sh_type
14322 == SHT_FINI_ARRAY)))
14323 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
14324 && elf_next_in_group (o) == NULL
14325 && elf_linked_to_section (o) == NULL)
14326 || ((elf_tdata (sub)->has_gnu_osabi & elf_gnu_osabi_retain)
14327 && (elf_section_flags (o) & SHF_GNU_RETAIN))))
14328 {
14329 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
14330 return false;
14331 }
14332 }
14333
14334 /* Allow the backend to mark additional target specific sections. */
14335 bed->gc_mark_extra_sections (info, gc_mark_hook);
14336
14337 /* ... and mark SEC_EXCLUDE for those that go. */
14338 return elf_gc_sweep (abfd, info);
14339 }
14340 \f
14341 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
14342
14343 bool
14344 bfd_elf_gc_record_vtinherit (bfd *abfd,
14345 asection *sec,
14346 struct elf_link_hash_entry *h,
14347 bfd_vma offset)
14348 {
14349 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
14350 struct elf_link_hash_entry **search, *child;
14351 size_t extsymcount;
14352 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14353
14354 /* The sh_info field of the symtab header tells us where the
14355 external symbols start. We don't care about the local symbols at
14356 this point. */
14357 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
14358 if (!elf_bad_symtab (abfd))
14359 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
14360
14361 sym_hashes = elf_sym_hashes (abfd);
14362 sym_hashes_end = PTR_ADD (sym_hashes, extsymcount);
14363
14364 /* Hunt down the child symbol, which is in this section at the same
14365 offset as the relocation. */
14366 for (search = sym_hashes; search != sym_hashes_end; ++search)
14367 {
14368 if ((child = *search) != NULL
14369 && (child->root.type == bfd_link_hash_defined
14370 || child->root.type == bfd_link_hash_defweak)
14371 && child->root.u.def.section == sec
14372 && child->root.u.def.value == offset)
14373 goto win;
14374 }
14375
14376 /* xgettext:c-format */
14377 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
14378 abfd, sec, (uint64_t) offset);
14379 bfd_set_error (bfd_error_invalid_operation);
14380 return false;
14381
14382 win:
14383 if (!child->u2.vtable)
14384 {
14385 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
14386 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
14387 if (!child->u2.vtable)
14388 return false;
14389 }
14390 if (!h)
14391 {
14392 /* This *should* only be the absolute section. It could potentially
14393 be that someone has defined a non-global vtable though, which
14394 would be bad. It isn't worth paging in the local symbols to be
14395 sure though; that case should simply be handled by the assembler. */
14396
14397 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
14398 }
14399 else
14400 child->u2.vtable->parent = h;
14401
14402 return true;
14403 }
14404
14405 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
14406
14407 bool
14408 bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec,
14409 struct elf_link_hash_entry *h,
14410 bfd_vma addend)
14411 {
14412 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14413 unsigned int log_file_align = bed->s->log_file_align;
14414
14415 if (!h)
14416 {
14417 /* xgettext:c-format */
14418 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
14419 abfd, sec);
14420 bfd_set_error (bfd_error_bad_value);
14421 return false;
14422 }
14423
14424 if (!h->u2.vtable)
14425 {
14426 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
14427 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
14428 if (!h->u2.vtable)
14429 return false;
14430 }
14431
14432 if (addend >= h->u2.vtable->size)
14433 {
14434 size_t size, bytes, file_align;
14435 bool *ptr = h->u2.vtable->used;
14436
14437 /* While the symbol is undefined, we have to be prepared to handle
14438 a zero size. */
14439 file_align = 1 << log_file_align;
14440 if (h->root.type == bfd_link_hash_undefined)
14441 size = addend + file_align;
14442 else
14443 {
14444 size = h->size;
14445 if (addend >= size)
14446 {
14447 /* Oops! We've got a reference past the defined end of
14448 the table. This is probably a bug -- shall we warn? */
14449 size = addend + file_align;
14450 }
14451 }
14452 size = (size + file_align - 1) & -file_align;
14453
14454 /* Allocate one extra entry for use as a "done" flag for the
14455 consolidation pass. */
14456 bytes = ((size >> log_file_align) + 1) * sizeof (bool);
14457
14458 if (ptr)
14459 {
14460 ptr = (bool *) bfd_realloc (ptr - 1, bytes);
14461
14462 if (ptr != NULL)
14463 {
14464 size_t oldbytes;
14465
14466 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
14467 * sizeof (bool));
14468 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
14469 }
14470 }
14471 else
14472 ptr = (bool *) bfd_zmalloc (bytes);
14473
14474 if (ptr == NULL)
14475 return false;
14476
14477 /* And arrange for that done flag to be at index -1. */
14478 h->u2.vtable->used = ptr + 1;
14479 h->u2.vtable->size = size;
14480 }
14481
14482 h->u2.vtable->used[addend >> log_file_align] = true;
14483
14484 return true;
14485 }
14486
14487 /* Map an ELF section header flag to its corresponding string. */
14488 typedef struct
14489 {
14490 char *flag_name;
14491 flagword flag_value;
14492 } elf_flags_to_name_table;
14493
14494 static const elf_flags_to_name_table elf_flags_to_names [] =
14495 {
14496 { "SHF_WRITE", SHF_WRITE },
14497 { "SHF_ALLOC", SHF_ALLOC },
14498 { "SHF_EXECINSTR", SHF_EXECINSTR },
14499 { "SHF_MERGE", SHF_MERGE },
14500 { "SHF_STRINGS", SHF_STRINGS },
14501 { "SHF_INFO_LINK", SHF_INFO_LINK},
14502 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
14503 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
14504 { "SHF_GROUP", SHF_GROUP },
14505 { "SHF_TLS", SHF_TLS },
14506 { "SHF_MASKOS", SHF_MASKOS },
14507 { "SHF_EXCLUDE", SHF_EXCLUDE },
14508 };
14509
14510 /* Returns TRUE if the section is to be included, otherwise FALSE. */
14511 bool
14512 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
14513 struct flag_info *flaginfo,
14514 asection *section)
14515 {
14516 const bfd_vma sh_flags = elf_section_flags (section);
14517
14518 if (!flaginfo->flags_initialized)
14519 {
14520 bfd *obfd = info->output_bfd;
14521 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14522 struct flag_info_list *tf = flaginfo->flag_list;
14523 int with_hex = 0;
14524 int without_hex = 0;
14525
14526 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
14527 {
14528 unsigned i;
14529 flagword (*lookup) (char *);
14530
14531 lookup = bed->elf_backend_lookup_section_flags_hook;
14532 if (lookup != NULL)
14533 {
14534 flagword hexval = (*lookup) ((char *) tf->name);
14535
14536 if (hexval != 0)
14537 {
14538 if (tf->with == with_flags)
14539 with_hex |= hexval;
14540 else if (tf->with == without_flags)
14541 without_hex |= hexval;
14542 tf->valid = true;
14543 continue;
14544 }
14545 }
14546 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
14547 {
14548 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
14549 {
14550 if (tf->with == with_flags)
14551 with_hex |= elf_flags_to_names[i].flag_value;
14552 else if (tf->with == without_flags)
14553 without_hex |= elf_flags_to_names[i].flag_value;
14554 tf->valid = true;
14555 break;
14556 }
14557 }
14558 if (!tf->valid)
14559 {
14560 info->callbacks->einfo
14561 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
14562 return false;
14563 }
14564 }
14565 flaginfo->flags_initialized = true;
14566 flaginfo->only_with_flags |= with_hex;
14567 flaginfo->not_with_flags |= without_hex;
14568 }
14569
14570 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
14571 return false;
14572
14573 if ((flaginfo->not_with_flags & sh_flags) != 0)
14574 return false;
14575
14576 return true;
14577 }
14578
14579 struct alloc_got_off_arg {
14580 bfd_vma gotoff;
14581 struct bfd_link_info *info;
14582 };
14583
14584 /* We need a special top-level link routine to convert got reference counts
14585 to real got offsets. */
14586
14587 static bool
14588 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
14589 {
14590 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
14591 bfd *obfd = gofarg->info->output_bfd;
14592 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14593
14594 if (h->got.refcount > 0)
14595 {
14596 h->got.offset = gofarg->gotoff;
14597 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
14598 }
14599 else
14600 h->got.offset = (bfd_vma) -1;
14601
14602 return true;
14603 }
14604
14605 /* And an accompanying bit to work out final got entry offsets once
14606 we're done. Should be called from final_link. */
14607
14608 bool
14609 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
14610 struct bfd_link_info *info)
14611 {
14612 bfd *i;
14613 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14614 bfd_vma gotoff;
14615 struct alloc_got_off_arg gofarg;
14616
14617 BFD_ASSERT (abfd == info->output_bfd);
14618
14619 if (! is_elf_hash_table (info->hash))
14620 return false;
14621
14622 /* The GOT offset is relative to the .got section, but the GOT header is
14623 put into the .got.plt section, if the backend uses it. */
14624 if (bed->want_got_plt)
14625 gotoff = 0;
14626 else
14627 gotoff = bed->got_header_size;
14628
14629 /* Do the local .got entries first. */
14630 for (i = info->input_bfds; i; i = i->link.next)
14631 {
14632 bfd_signed_vma *local_got;
14633 size_t j, locsymcount;
14634 Elf_Internal_Shdr *symtab_hdr;
14635
14636 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
14637 continue;
14638
14639 local_got = elf_local_got_refcounts (i);
14640 if (!local_got)
14641 continue;
14642
14643 symtab_hdr = &elf_tdata (i)->symtab_hdr;
14644 if (elf_bad_symtab (i))
14645 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
14646 else
14647 locsymcount = symtab_hdr->sh_info;
14648
14649 for (j = 0; j < locsymcount; ++j)
14650 {
14651 if (local_got[j] > 0)
14652 {
14653 local_got[j] = gotoff;
14654 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
14655 }
14656 else
14657 local_got[j] = (bfd_vma) -1;
14658 }
14659 }
14660
14661 /* Then the global .got entries. .plt refcounts are handled by
14662 adjust_dynamic_symbol */
14663 gofarg.gotoff = gotoff;
14664 gofarg.info = info;
14665 elf_link_hash_traverse (elf_hash_table (info),
14666 elf_gc_allocate_got_offsets,
14667 &gofarg);
14668 return true;
14669 }
14670
14671 /* Many folk need no more in the way of final link than this, once
14672 got entry reference counting is enabled. */
14673
14674 bool
14675 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
14676 {
14677 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
14678 return false;
14679
14680 /* Invoke the regular ELF backend linker to do all the work. */
14681 return bfd_elf_final_link (abfd, info);
14682 }
14683
14684 bool
14685 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
14686 {
14687 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
14688
14689 if (rcookie->bad_symtab)
14690 rcookie->rel = rcookie->rels;
14691
14692 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
14693 {
14694 unsigned long r_symndx;
14695
14696 if (! rcookie->bad_symtab)
14697 if (rcookie->rel->r_offset > offset)
14698 return false;
14699 if (rcookie->rel->r_offset != offset)
14700 continue;
14701
14702 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
14703 if (r_symndx == STN_UNDEF)
14704 return true;
14705
14706 if (r_symndx >= rcookie->locsymcount
14707 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
14708 {
14709 struct elf_link_hash_entry *h;
14710
14711 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
14712
14713 while (h->root.type == bfd_link_hash_indirect
14714 || h->root.type == bfd_link_hash_warning)
14715 h = (struct elf_link_hash_entry *) h->root.u.i.link;
14716
14717 if ((h->root.type == bfd_link_hash_defined
14718 || h->root.type == bfd_link_hash_defweak)
14719 && (h->root.u.def.section->owner != rcookie->abfd
14720 || h->root.u.def.section->kept_section != NULL
14721 || discarded_section (h->root.u.def.section)))
14722 return true;
14723 }
14724 else
14725 {
14726 /* It's not a relocation against a global symbol,
14727 but it could be a relocation against a local
14728 symbol for a discarded section. */
14729 asection *isec;
14730 Elf_Internal_Sym *isym;
14731
14732 /* Need to: get the symbol; get the section. */
14733 isym = &rcookie->locsyms[r_symndx];
14734 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
14735 if (isec != NULL
14736 && (isec->kept_section != NULL
14737 || discarded_section (isec)))
14738 return true;
14739 }
14740 return false;
14741 }
14742 return false;
14743 }
14744
14745 /* Discard unneeded references to discarded sections.
14746 Returns -1 on error, 1 if any section's size was changed, 0 if
14747 nothing changed. This function assumes that the relocations are in
14748 sorted order, which is true for all known assemblers. */
14749
14750 int
14751 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
14752 {
14753 struct elf_reloc_cookie cookie;
14754 asection *o;
14755 bfd *abfd;
14756 int changed = 0;
14757
14758 if (info->traditional_format
14759 || !is_elf_hash_table (info->hash))
14760 return 0;
14761
14762 o = bfd_get_section_by_name (output_bfd, ".stab");
14763 if (o != NULL)
14764 {
14765 asection *i;
14766
14767 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14768 {
14769 if (i->size == 0
14770 || i->reloc_count == 0
14771 || i->sec_info_type != SEC_INFO_TYPE_STABS)
14772 continue;
14773
14774 abfd = i->owner;
14775 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14776 continue;
14777
14778 if (!init_reloc_cookie_for_section (&cookie, info, i))
14779 return -1;
14780
14781 if (_bfd_discard_section_stabs (abfd, i,
14782 elf_section_data (i)->sec_info,
14783 bfd_elf_reloc_symbol_deleted_p,
14784 &cookie))
14785 changed = 1;
14786
14787 fini_reloc_cookie_for_section (&cookie, i);
14788 }
14789 }
14790
14791 o = NULL;
14792 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
14793 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
14794 if (o != NULL)
14795 {
14796 asection *i;
14797 int eh_changed = 0;
14798 unsigned int eh_alignment; /* Octets. */
14799
14800 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14801 {
14802 if (i->size == 0)
14803 continue;
14804
14805 abfd = i->owner;
14806 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14807 continue;
14808
14809 if (!init_reloc_cookie_for_section (&cookie, info, i))
14810 return -1;
14811
14812 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
14813 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
14814 bfd_elf_reloc_symbol_deleted_p,
14815 &cookie))
14816 {
14817 eh_changed = 1;
14818 if (i->size != i->rawsize)
14819 changed = 1;
14820 }
14821
14822 fini_reloc_cookie_for_section (&cookie, i);
14823 }
14824
14825 eh_alignment = ((1 << o->alignment_power)
14826 * bfd_octets_per_byte (output_bfd, o));
14827 /* Skip over zero terminator, and prevent empty sections from
14828 adding alignment padding at the end. */
14829 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
14830 if (i->size == 0)
14831 i->flags |= SEC_EXCLUDE;
14832 else if (i->size > 4)
14833 break;
14834 /* The last non-empty eh_frame section doesn't need padding. */
14835 if (i != NULL)
14836 i = i->map_tail.s;
14837 /* Any prior sections must pad the last FDE out to the output
14838 section alignment. Otherwise we might have zero padding
14839 between sections, which would be seen as a terminator. */
14840 for (; i != NULL; i = i->map_tail.s)
14841 if (i->size == 4)
14842 /* All but the last zero terminator should have been removed. */
14843 BFD_FAIL ();
14844 else
14845 {
14846 bfd_size_type size
14847 = (i->size + eh_alignment - 1) & -eh_alignment;
14848 if (i->size != size)
14849 {
14850 i->size = size;
14851 changed = 1;
14852 eh_changed = 1;
14853 }
14854 }
14855 if (eh_changed)
14856 elf_link_hash_traverse (elf_hash_table (info),
14857 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
14858 }
14859
14860 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
14861 {
14862 const struct elf_backend_data *bed;
14863 asection *s;
14864
14865 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14866 continue;
14867 s = abfd->sections;
14868 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14869 continue;
14870
14871 bed = get_elf_backend_data (abfd);
14872
14873 if (bed->elf_backend_discard_info != NULL)
14874 {
14875 if (!init_reloc_cookie (&cookie, info, abfd))
14876 return -1;
14877
14878 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
14879 changed = 1;
14880
14881 fini_reloc_cookie (&cookie, abfd);
14882 }
14883 }
14884
14885 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
14886 _bfd_elf_end_eh_frame_parsing (info);
14887
14888 if (info->eh_frame_hdr_type
14889 && !bfd_link_relocatable (info)
14890 && _bfd_elf_discard_section_eh_frame_hdr (info))
14891 changed = 1;
14892
14893 return changed;
14894 }
14895
14896 bool
14897 _bfd_elf_section_already_linked (bfd *abfd,
14898 asection *sec,
14899 struct bfd_link_info *info)
14900 {
14901 flagword flags;
14902 const char *name, *key;
14903 struct bfd_section_already_linked *l;
14904 struct bfd_section_already_linked_hash_entry *already_linked_list;
14905
14906 if (sec->output_section == bfd_abs_section_ptr)
14907 return false;
14908
14909 flags = sec->flags;
14910
14911 /* Return if it isn't a linkonce section. A comdat group section
14912 also has SEC_LINK_ONCE set. */
14913 if ((flags & SEC_LINK_ONCE) == 0)
14914 return false;
14915
14916 /* Don't put group member sections on our list of already linked
14917 sections. They are handled as a group via their group section. */
14918 if (elf_sec_group (sec) != NULL)
14919 return false;
14920
14921 /* For a SHT_GROUP section, use the group signature as the key. */
14922 name = sec->name;
14923 if ((flags & SEC_GROUP) != 0
14924 && elf_next_in_group (sec) != NULL
14925 && elf_group_name (elf_next_in_group (sec)) != NULL)
14926 key = elf_group_name (elf_next_in_group (sec));
14927 else
14928 {
14929 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
14930 if (startswith (name, ".gnu.linkonce.")
14931 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
14932 key++;
14933 else
14934 /* Must be a user linkonce section that doesn't follow gcc's
14935 naming convention. In this case we won't be matching
14936 single member groups. */
14937 key = name;
14938 }
14939
14940 already_linked_list = bfd_section_already_linked_table_lookup (key);
14941
14942 for (l = already_linked_list->entry; l != NULL; l = l->next)
14943 {
14944 /* We may have 2 different types of sections on the list: group
14945 sections with a signature of <key> (<key> is some string),
14946 and linkonce sections named .gnu.linkonce.<type>.<key>.
14947 Match like sections. LTO plugin sections are an exception.
14948 They are always named .gnu.linkonce.t.<key> and match either
14949 type of section. */
14950 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
14951 && ((flags & SEC_GROUP) != 0
14952 || strcmp (name, l->sec->name) == 0))
14953 || (l->sec->owner->flags & BFD_PLUGIN) != 0
14954 || (sec->owner->flags & BFD_PLUGIN) != 0)
14955 {
14956 /* The section has already been linked. See if we should
14957 issue a warning. */
14958 if (!_bfd_handle_already_linked (sec, l, info))
14959 return false;
14960
14961 if (flags & SEC_GROUP)
14962 {
14963 asection *first = elf_next_in_group (sec);
14964 asection *s = first;
14965
14966 while (s != NULL)
14967 {
14968 s->output_section = bfd_abs_section_ptr;
14969 /* Record which group discards it. */
14970 s->kept_section = l->sec;
14971 s = elf_next_in_group (s);
14972 /* These lists are circular. */
14973 if (s == first)
14974 break;
14975 }
14976 }
14977
14978 return true;
14979 }
14980 }
14981
14982 /* A single member comdat group section may be discarded by a
14983 linkonce section and vice versa. */
14984 if ((flags & SEC_GROUP) != 0)
14985 {
14986 asection *first = elf_next_in_group (sec);
14987
14988 if (first != NULL && elf_next_in_group (first) == first)
14989 /* Check this single member group against linkonce sections. */
14990 for (l = already_linked_list->entry; l != NULL; l = l->next)
14991 if ((l->sec->flags & SEC_GROUP) == 0
14992 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14993 {
14994 first->output_section = bfd_abs_section_ptr;
14995 first->kept_section = l->sec;
14996 sec->output_section = bfd_abs_section_ptr;
14997 break;
14998 }
14999 }
15000 else
15001 /* Check this linkonce section against single member groups. */
15002 for (l = already_linked_list->entry; l != NULL; l = l->next)
15003 if (l->sec->flags & SEC_GROUP)
15004 {
15005 asection *first = elf_next_in_group (l->sec);
15006
15007 if (first != NULL
15008 && elf_next_in_group (first) == first
15009 && bfd_elf_match_symbols_in_sections (first, sec, info))
15010 {
15011 sec->output_section = bfd_abs_section_ptr;
15012 sec->kept_section = first;
15013 break;
15014 }
15015 }
15016
15017 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
15018 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
15019 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
15020 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
15021 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
15022 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
15023 `.gnu.linkonce.t.F' section from a different bfd not requiring any
15024 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
15025 The reverse order cannot happen as there is never a bfd with only the
15026 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
15027 matter as here were are looking only for cross-bfd sections. */
15028
15029 if ((flags & SEC_GROUP) == 0 && startswith (name, ".gnu.linkonce.r."))
15030 for (l = already_linked_list->entry; l != NULL; l = l->next)
15031 if ((l->sec->flags & SEC_GROUP) == 0
15032 && startswith (l->sec->name, ".gnu.linkonce.t."))
15033 {
15034 if (abfd != l->sec->owner)
15035 sec->output_section = bfd_abs_section_ptr;
15036 break;
15037 }
15038
15039 /* This is the first section with this name. Record it. */
15040 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
15041 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
15042 return sec->output_section == bfd_abs_section_ptr;
15043 }
15044
15045 bool
15046 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
15047 {
15048 return sym->st_shndx == SHN_COMMON;
15049 }
15050
15051 unsigned int
15052 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
15053 {
15054 return SHN_COMMON;
15055 }
15056
15057 asection *
15058 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
15059 {
15060 return bfd_com_section_ptr;
15061 }
15062
15063 bfd_vma
15064 _bfd_elf_default_got_elt_size (bfd *abfd,
15065 struct bfd_link_info *info ATTRIBUTE_UNUSED,
15066 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
15067 bfd *ibfd ATTRIBUTE_UNUSED,
15068 unsigned long symndx ATTRIBUTE_UNUSED)
15069 {
15070 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15071 return bed->s->arch_size / 8;
15072 }
15073
15074 /* Routines to support the creation of dynamic relocs. */
15075
15076 /* Returns the name of the dynamic reloc section associated with SEC. */
15077
15078 static const char *
15079 get_dynamic_reloc_section_name (bfd * abfd,
15080 asection * sec,
15081 bool is_rela)
15082 {
15083 char *name;
15084 const char *old_name = bfd_section_name (sec);
15085 const char *prefix = is_rela ? ".rela" : ".rel";
15086
15087 if (old_name == NULL)
15088 return NULL;
15089
15090 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
15091 sprintf (name, "%s%s", prefix, old_name);
15092
15093 return name;
15094 }
15095
15096 /* Returns the dynamic reloc section associated with SEC.
15097 If necessary compute the name of the dynamic reloc section based
15098 on SEC's name (looked up in ABFD's string table) and the setting
15099 of IS_RELA. */
15100
15101 asection *
15102 _bfd_elf_get_dynamic_reloc_section (bfd *abfd,
15103 asection *sec,
15104 bool is_rela)
15105 {
15106 asection *reloc_sec = elf_section_data (sec)->sreloc;
15107
15108 if (reloc_sec == NULL)
15109 {
15110 const char *name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
15111
15112 if (name != NULL)
15113 {
15114 reloc_sec = bfd_get_linker_section (abfd, name);
15115
15116 if (reloc_sec != NULL)
15117 elf_section_data (sec)->sreloc = reloc_sec;
15118 }
15119 }
15120
15121 return reloc_sec;
15122 }
15123
15124 /* Returns the dynamic reloc section associated with SEC. If the
15125 section does not exist it is created and attached to the DYNOBJ
15126 bfd and stored in the SRELOC field of SEC's elf_section_data
15127 structure.
15128
15129 ALIGNMENT is the alignment for the newly created section and
15130 IS_RELA defines whether the name should be .rela.<SEC's name>
15131 or .rel.<SEC's name>. The section name is looked up in the
15132 string table associated with ABFD. */
15133
15134 asection *
15135 _bfd_elf_make_dynamic_reloc_section (asection *sec,
15136 bfd *dynobj,
15137 unsigned int alignment,
15138 bfd *abfd,
15139 bool is_rela)
15140 {
15141 asection * reloc_sec = elf_section_data (sec)->sreloc;
15142
15143 if (reloc_sec == NULL)
15144 {
15145 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
15146
15147 if (name == NULL)
15148 return NULL;
15149
15150 reloc_sec = bfd_get_linker_section (dynobj, name);
15151
15152 if (reloc_sec == NULL)
15153 {
15154 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
15155 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
15156 if ((sec->flags & SEC_ALLOC) != 0)
15157 flags |= SEC_ALLOC | SEC_LOAD;
15158
15159 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
15160 if (reloc_sec != NULL)
15161 {
15162 /* _bfd_elf_get_sec_type_attr chooses a section type by
15163 name. Override as it may be wrong, eg. for a user
15164 section named "auto" we'll get ".relauto" which is
15165 seen to be a .rela section. */
15166 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
15167 if (!bfd_set_section_alignment (reloc_sec, alignment))
15168 reloc_sec = NULL;
15169 }
15170 }
15171
15172 elf_section_data (sec)->sreloc = reloc_sec;
15173 }
15174
15175 return reloc_sec;
15176 }
15177
15178 /* Copy the ELF symbol type and other attributes for a linker script
15179 assignment from HSRC to HDEST. Generally this should be treated as
15180 if we found a strong non-dynamic definition for HDEST (except that
15181 ld ignores multiple definition errors). */
15182 void
15183 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
15184 struct bfd_link_hash_entry *hdest,
15185 struct bfd_link_hash_entry *hsrc)
15186 {
15187 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
15188 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
15189 Elf_Internal_Sym isym;
15190
15191 ehdest->type = ehsrc->type;
15192 ehdest->target_internal = ehsrc->target_internal;
15193
15194 isym.st_other = ehsrc->other;
15195 elf_merge_st_other (abfd, ehdest, isym.st_other, NULL, true, false);
15196 }
15197
15198 /* Append a RELA relocation REL to section S in BFD. */
15199
15200 void
15201 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15202 {
15203 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15204 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
15205 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
15206 bed->s->swap_reloca_out (abfd, rel, loc);
15207 }
15208
15209 /* Append a REL relocation REL to section S in BFD. */
15210
15211 void
15212 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15213 {
15214 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15215 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
15216 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
15217 bed->s->swap_reloc_out (abfd, rel, loc);
15218 }
15219
15220 /* Define __start, __stop, .startof. or .sizeof. symbol. */
15221
15222 struct bfd_link_hash_entry *
15223 bfd_elf_define_start_stop (struct bfd_link_info *info,
15224 const char *symbol, asection *sec)
15225 {
15226 struct elf_link_hash_entry *h;
15227
15228 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
15229 false, false, true);
15230 /* NB: Common symbols will be turned into definition later. */
15231 if (h != NULL
15232 && !h->root.ldscript_def
15233 && (h->root.type == bfd_link_hash_undefined
15234 || h->root.type == bfd_link_hash_undefweak
15235 || ((h->ref_regular || h->def_dynamic)
15236 && !h->def_regular
15237 && h->root.type != bfd_link_hash_common)))
15238 {
15239 bool was_dynamic = h->ref_dynamic || h->def_dynamic;
15240 h->verinfo.verdef = NULL;
15241 h->root.type = bfd_link_hash_defined;
15242 h->root.u.def.section = sec;
15243 h->root.u.def.value = 0;
15244 h->def_regular = 1;
15245 h->def_dynamic = 0;
15246 h->start_stop = 1;
15247 h->u2.start_stop_section = sec;
15248 if (symbol[0] == '.')
15249 {
15250 /* .startof. and .sizeof. symbols are local. */
15251 const struct elf_backend_data *bed;
15252 bed = get_elf_backend_data (info->output_bfd);
15253 (*bed->elf_backend_hide_symbol) (info, h, true);
15254 }
15255 else
15256 {
15257 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
15258 h->other = ((h->other & ~ELF_ST_VISIBILITY (-1))
15259 | info->start_stop_visibility);
15260 if (was_dynamic)
15261 bfd_elf_link_record_dynamic_symbol (info, h);
15262 }
15263 return &h->root;
15264 }
15265 return NULL;
15266 }
15267
15268 /* Find dynamic relocs for H that apply to read-only sections. */
15269
15270 asection *
15271 _bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry *h)
15272 {
15273 struct elf_dyn_relocs *p;
15274
15275 for (p = h->dyn_relocs; p != NULL; p = p->next)
15276 {
15277 asection *s = p->sec->output_section;
15278
15279 if (s != NULL && (s->flags & SEC_READONLY) != 0)
15280 return p->sec;
15281 }
15282 return NULL;
15283 }
15284
15285 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
15286 read-only sections. */
15287
15288 bool
15289 _bfd_elf_maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
15290 {
15291 asection *sec;
15292
15293 if (h->root.type == bfd_link_hash_indirect)
15294 return true;
15295
15296 sec = _bfd_elf_readonly_dynrelocs (h);
15297 if (sec != NULL)
15298 {
15299 struct bfd_link_info *info = (struct bfd_link_info *) inf;
15300
15301 info->flags |= DF_TEXTREL;
15302 /* xgettext:c-format */
15303 info->callbacks->minfo (_("%pB: dynamic relocation against `%pT' "
15304 "in read-only section `%pA'\n"),
15305 sec->owner, h->root.root.string, sec);
15306
15307 if (bfd_link_textrel_check (info))
15308 /* xgettext:c-format */
15309 info->callbacks->einfo (_("%P: %pB: warning: relocation against `%s' "
15310 "in read-only section `%pA'\n"),
15311 sec->owner, h->root.root.string, sec);
15312
15313 /* Not an error, just cut short the traversal. */
15314 return false;
15315 }
15316 return true;
15317 }
15318
15319 /* Add dynamic tags. */
15320
15321 bool
15322 _bfd_elf_add_dynamic_tags (bfd *output_bfd, struct bfd_link_info *info,
15323 bool need_dynamic_reloc)
15324 {
15325 struct elf_link_hash_table *htab = elf_hash_table (info);
15326
15327 if (htab->dynamic_sections_created)
15328 {
15329 /* Add some entries to the .dynamic section. We fill in the
15330 values later, in finish_dynamic_sections, but we must add
15331 the entries now so that we get the correct size for the
15332 .dynamic section. The DT_DEBUG entry is filled in by the
15333 dynamic linker and used by the debugger. */
15334 #define add_dynamic_entry(TAG, VAL) \
15335 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
15336
15337 const struct elf_backend_data *bed
15338 = get_elf_backend_data (output_bfd);
15339
15340 if (bfd_link_executable (info))
15341 {
15342 if (!add_dynamic_entry (DT_DEBUG, 0))
15343 return false;
15344 }
15345
15346 if (htab->dt_pltgot_required || htab->splt->size != 0)
15347 {
15348 /* DT_PLTGOT is used by prelink even if there is no PLT
15349 relocation. */
15350 if (!add_dynamic_entry (DT_PLTGOT, 0))
15351 return false;
15352 }
15353
15354 if (htab->dt_jmprel_required || htab->srelplt->size != 0)
15355 {
15356 if (!add_dynamic_entry (DT_PLTRELSZ, 0)
15357 || !add_dynamic_entry (DT_PLTREL,
15358 (bed->rela_plts_and_copies_p
15359 ? DT_RELA : DT_REL))
15360 || !add_dynamic_entry (DT_JMPREL, 0))
15361 return false;
15362 }
15363
15364 if (htab->tlsdesc_plt
15365 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
15366 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
15367 return false;
15368
15369 if (need_dynamic_reloc)
15370 {
15371 if (bed->rela_plts_and_copies_p)
15372 {
15373 if (!add_dynamic_entry (DT_RELA, 0)
15374 || !add_dynamic_entry (DT_RELASZ, 0)
15375 || !add_dynamic_entry (DT_RELAENT,
15376 bed->s->sizeof_rela))
15377 return false;
15378 }
15379 else
15380 {
15381 if (!add_dynamic_entry (DT_REL, 0)
15382 || !add_dynamic_entry (DT_RELSZ, 0)
15383 || !add_dynamic_entry (DT_RELENT,
15384 bed->s->sizeof_rel))
15385 return false;
15386 }
15387
15388 /* If any dynamic relocs apply to a read-only section,
15389 then we need a DT_TEXTREL entry. */
15390 if ((info->flags & DF_TEXTREL) == 0)
15391 elf_link_hash_traverse (htab, _bfd_elf_maybe_set_textrel,
15392 info);
15393
15394 if ((info->flags & DF_TEXTREL) != 0)
15395 {
15396 if (htab->ifunc_resolvers)
15397 info->callbacks->einfo
15398 (_("%P: warning: GNU indirect functions with DT_TEXTREL "
15399 "may result in a segfault at runtime; recompile with %s\n"),
15400 bfd_link_dll (info) ? "-fPIC" : "-fPIE");
15401
15402 if (!add_dynamic_entry (DT_TEXTREL, 0))
15403 return false;
15404 }
15405 }
15406 }
15407 #undef add_dynamic_entry
15408
15409 return true;
15410 }