elflink.c revision 1.1.1.11 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
208 /* Create a strtab to hold the dynamic symbol names. */
210 static bool
211 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
212 {
213 struct elf_link_hash_table *hash_table;
214
215 hash_table = elf_hash_table (info);
216 if (hash_table->dynobj == NULL)
217 {
218 /* We may not set dynobj, an input file holding linker created
219 dynamic sections to abfd, which may be a dynamic object with
220 its own dynamic sections. We need to find a normal input file
221 to hold linker created sections if possible. */
222 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
223 {
224 bfd *ibfd;
225 asection *s;
226 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
227 if ((ibfd->flags
228 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
229 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
230 && elf_object_id (ibfd) == elf_hash_table_id (hash_table)
231 && !((s = ibfd->sections) != NULL
232 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS))
233 {
234 abfd = ibfd;
235 break;
236 }
237 }
238 hash_table->dynobj = abfd;
239 }
240
241 if (hash_table->dynstr == NULL)
242 {
243 hash_table->dynstr = _bfd_elf_strtab_init ();
244 if (hash_table->dynstr == NULL)
245 return false;
246 }
247 return true;
248 }
249
250 /* Create some sections which will be filled in with dynamic linking
251 information. ABFD is an input file which requires dynamic sections
252 to be created. The dynamic sections take up virtual memory space
253 when the final executable is run, so we need to create them before
254 addresses are assigned to the output sections. We work out the
255 actual contents and size of these sections later. */
256
257 bool
258 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
259 {
260 flagword flags;
261 asection *s;
262 const struct elf_backend_data *bed;
263 struct elf_link_hash_entry *h;
264
265 if (! is_elf_hash_table (info->hash))
266 return false;
267
268 if (elf_hash_table (info)->dynamic_sections_created)
269 return true;
270
271 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
272 return false;
273
274 abfd = elf_hash_table (info)->dynobj;
275 bed = get_elf_backend_data (abfd);
276
277 flags = bed->dynamic_sec_flags;
278
279 /* A dynamically linked executable has a .interp section, but a
280 shared library does not. */
281 if (bfd_link_executable (info) && !info->nointerp)
282 {
283 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
284 flags | SEC_READONLY);
285 if (s == NULL)
286 return false;
287 }
288
289 /* Create sections to hold version informations. These are removed
290 if they are not needed. */
291 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
292 flags | SEC_READONLY);
293 if (s == NULL
294 || !bfd_set_section_alignment (s, bed->s->log_file_align))
295 return false;
296
297 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
298 flags | SEC_READONLY);
299 if (s == NULL
300 || !bfd_set_section_alignment (s, 1))
301 return false;
302
303 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
304 flags | SEC_READONLY);
305 if (s == NULL
306 || !bfd_set_section_alignment (s, bed->s->log_file_align))
307 return false;
308
309 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
310 flags | SEC_READONLY);
311 if (s == NULL
312 || !bfd_set_section_alignment (s, bed->s->log_file_align))
313 return false;
314 elf_hash_table (info)->dynsym = s;
315
316 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
317 flags | SEC_READONLY);
318 if (s == NULL)
319 return false;
320
321 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
322 if (s == NULL
323 || !bfd_set_section_alignment (s, bed->s->log_file_align))
324 return false;
325
326 /* The special symbol _DYNAMIC is always set to the start of the
327 .dynamic section. We could set _DYNAMIC in a linker script, but we
328 only want to define it if we are, in fact, creating a .dynamic
329 section. We don't want to define it if there is no .dynamic
330 section, since on some ELF platforms the start up code examines it
331 to decide how to initialize the process. */
332 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
333 elf_hash_table (info)->hdynamic = h;
334 if (h == NULL)
335 return false;
336
337 if (info->emit_hash)
338 {
339 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
340 flags | SEC_READONLY);
341 if (s == NULL
342 || !bfd_set_section_alignment (s, bed->s->log_file_align))
343 return false;
344 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
345 }
346
347 if (info->emit_gnu_hash && bed->record_xhash_symbol == NULL)
348 {
349 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
350 flags | SEC_READONLY);
351 if (s == NULL
352 || !bfd_set_section_alignment (s, bed->s->log_file_align))
353 return false;
354 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
355 4 32-bit words followed by variable count of 64-bit words, then
356 variable count of 32-bit words. */
357 if (bed->s->arch_size == 64)
358 elf_section_data (s)->this_hdr.sh_entsize = 0;
359 else
360 elf_section_data (s)->this_hdr.sh_entsize = 4;
361 }
362
363 if (info->enable_dt_relr)
364 {
365 s = bfd_make_section_anyway_with_flags (abfd, ".relr.dyn",
366 (bed->dynamic_sec_flags
367 | SEC_READONLY));
368 if (s == NULL
369 || !bfd_set_section_alignment (s, bed->s->log_file_align))
370 return false;
371 elf_hash_table (info)->srelrdyn = s;
372 }
373
374 /* Let the backend create the rest of the sections. This lets the
375 backend set the right flags. The backend will normally create
376 the .got and .plt sections. */
377 if (bed->elf_backend_create_dynamic_sections == NULL
378 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
379 return false;
380
381 elf_hash_table (info)->dynamic_sections_created = true;
382
383 return true;
384 }
385
386 /* Create dynamic sections when linking against a dynamic object. */
387
388 bool
389 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
390 {
391 flagword flags, pltflags;
392 struct elf_link_hash_entry *h;
393 asection *s;
394 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
395 struct elf_link_hash_table *htab = elf_hash_table (info);
396
397 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
398 .rel[a].bss sections. */
399 flags = bed->dynamic_sec_flags;
400
401 pltflags = flags;
402 if (bed->plt_not_loaded)
403 /* We do not clear SEC_ALLOC here because we still want the OS to
404 allocate space for the section; it's just that there's nothing
405 to read in from the object file. */
406 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
407 else
408 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
409 if (bed->plt_readonly)
410 pltflags |= SEC_READONLY;
411
412 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
413 if (s == NULL
414 || !bfd_set_section_alignment (s, bed->plt_alignment))
415 return false;
416 htab->splt = s;
417
418 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
419 .plt section. */
420 if (bed->want_plt_sym)
421 {
422 h = _bfd_elf_define_linkage_sym (abfd, info, s,
423 "_PROCEDURE_LINKAGE_TABLE_");
424 elf_hash_table (info)->hplt = h;
425 if (h == NULL)
426 return false;
427 }
428
429 s = bfd_make_section_anyway_with_flags (abfd,
430 (bed->rela_plts_and_copies_p
431 ? ".rela.plt" : ".rel.plt"),
432 flags | SEC_READONLY);
433 if (s == NULL
434 || !bfd_set_section_alignment (s, bed->s->log_file_align))
435 return false;
436 htab->srelplt = s;
437
438 if (! _bfd_elf_create_got_section (abfd, info))
439 return false;
440
441 if (bed->want_dynbss)
442 {
443 /* The .dynbss section is a place to put symbols which are defined
444 by dynamic objects, are referenced by regular objects, and are
445 not functions. We must allocate space for them in the process
446 image and use a R_*_COPY reloc to tell the dynamic linker to
447 initialize them at run time. The linker script puts the .dynbss
448 section into the .bss section of the final image. */
449 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
450 SEC_ALLOC | SEC_LINKER_CREATED);
451 if (s == NULL)
452 return false;
453 htab->sdynbss = s;
454
455 if (bed->want_dynrelro)
456 {
457 /* Similarly, but for symbols that were originally in read-only
458 sections. This section doesn't really need to have contents,
459 but make it like other .data.rel.ro sections. */
460 s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
461 flags);
462 if (s == NULL)
463 return false;
464 htab->sdynrelro = s;
465 }
466
467 /* The .rel[a].bss section holds copy relocs. This section is not
468 normally needed. We need to create it here, though, so that the
469 linker will map it to an output section. We can't just create it
470 only if we need it, because we will not know whether we need it
471 until we have seen all the input files, and the first time the
472 main linker code calls BFD after examining all the input files
473 (size_dynamic_sections) the input sections have already been
474 mapped to the output sections. If the section turns out not to
475 be needed, we can discard it later. We will never need this
476 section when generating a shared object, since they do not use
477 copy relocs. */
478 if (bfd_link_executable (info))
479 {
480 s = bfd_make_section_anyway_with_flags (abfd,
481 (bed->rela_plts_and_copies_p
482 ? ".rela.bss" : ".rel.bss"),
483 flags | SEC_READONLY);
484 if (s == NULL
485 || !bfd_set_section_alignment (s, bed->s->log_file_align))
486 return false;
487 htab->srelbss = s;
488
489 if (bed->want_dynrelro)
490 {
491 s = (bfd_make_section_anyway_with_flags
492 (abfd, (bed->rela_plts_and_copies_p
493 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
494 flags | SEC_READONLY));
495 if (s == NULL
496 || !bfd_set_section_alignment (s, bed->s->log_file_align))
497 return false;
498 htab->sreldynrelro = s;
499 }
500 }
501 }
502
503 return true;
504 }
505
506 /* Record a new dynamic symbol. We record the dynamic symbols as we
508 read the input files, since we need to have a list of all of them
509 before we can determine the final sizes of the output sections.
510 Note that we may actually call this function even though we are not
511 going to output any dynamic symbols; in some cases we know that a
512 symbol should be in the dynamic symbol table, but only if there is
513 one. */
514
515 bool
516 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
517 struct elf_link_hash_entry *h)
518 {
519 if (h->dynindx == -1)
520 {
521 struct elf_strtab_hash *dynstr;
522 char *p;
523 const char *name;
524 size_t indx;
525
526 if (h->root.type == bfd_link_hash_defined
527 || h->root.type == bfd_link_hash_defweak)
528 {
529 /* An IR symbol should not be made dynamic. */
530 if (h->root.u.def.section != NULL
531 && h->root.u.def.section->owner != NULL
532 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)
533 return true;
534 }
535
536 /* XXX: The ABI draft says the linker must turn hidden and
537 internal symbols into STB_LOCAL symbols when producing the
538 DSO. However, if ld.so honors st_other in the dynamic table,
539 this would not be necessary. */
540 switch (ELF_ST_VISIBILITY (h->other))
541 {
542 case STV_INTERNAL:
543 case STV_HIDDEN:
544 if (h->root.type != bfd_link_hash_undefined
545 && h->root.type != bfd_link_hash_undefweak)
546 {
547 h->forced_local = 1;
548 if (!elf_hash_table (info)->is_relocatable_executable
549 || ((h->root.type == bfd_link_hash_defined
550 || h->root.type == bfd_link_hash_defweak)
551 && h->root.u.def.section->owner != NULL
552 && h->root.u.def.section->owner->no_export)
553 || (h->root.type == bfd_link_hash_common
554 && h->root.u.c.p->section->owner != NULL
555 && h->root.u.c.p->section->owner->no_export))
556 return true;
557 }
558
559 default:
560 break;
561 }
562
563 h->dynindx = elf_hash_table (info)->dynsymcount;
564 ++elf_hash_table (info)->dynsymcount;
565
566 dynstr = elf_hash_table (info)->dynstr;
567 if (dynstr == NULL)
568 {
569 /* Create a strtab to hold the dynamic symbol names. */
570 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
571 if (dynstr == NULL)
572 return false;
573 }
574
575 /* We don't put any version information in the dynamic string
576 table. */
577 name = h->root.root.string;
578 p = strchr (name, ELF_VER_CHR);
579 if (p != NULL)
580 /* We know that the p points into writable memory. In fact,
581 there are only a few symbols that have read-only names, being
582 those like _GLOBAL_OFFSET_TABLE_ that are created specially
583 by the backends. Most symbols will have names pointing into
584 an ELF string table read from a file, or to objalloc memory. */
585 *p = 0;
586
587 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
588
589 if (p != NULL)
590 *p = ELF_VER_CHR;
591
592 if (indx == (size_t) -1)
593 return false;
594 h->dynstr_index = indx;
595 }
596
597 return true;
598 }
599
600 /* Mark a symbol dynamic. */
602
603 static void
604 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
605 struct elf_link_hash_entry *h,
606 Elf_Internal_Sym *sym)
607 {
608 struct bfd_elf_dynamic_list *d = info->dynamic_list;
609
610 /* It may be called more than once on the same H. */
611 if(h->dynamic || bfd_link_relocatable (info))
612 return;
613
614 if ((info->dynamic_data
615 && (h->type == STT_OBJECT
616 || h->type == STT_COMMON
617 || (sym != NULL
618 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
619 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
620 || (d != NULL
621 && h->non_elf
622 && (*d->match) (&d->head, NULL, h->root.root.string)))
623 {
624 h->dynamic = 1;
625 /* NB: If a symbol is made dynamic by --dynamic-list, it has
626 non-IR reference. */
627 h->root.non_ir_ref_dynamic = 1;
628 }
629 }
630
631 /* Record an assignment to a symbol made by a linker script. We need
632 this in case some dynamic object refers to this symbol. */
633
634 bool
635 bfd_elf_record_link_assignment (bfd *output_bfd,
636 struct bfd_link_info *info,
637 const char *name,
638 bool provide,
639 bool hidden)
640 {
641 struct elf_link_hash_entry *h, *hv;
642 struct elf_link_hash_table *htab;
643 const struct elf_backend_data *bed;
644
645 if (!is_elf_hash_table (info->hash))
646 return true;
647
648 htab = elf_hash_table (info);
649 h = elf_link_hash_lookup (htab, name, !provide, true, false);
650 if (h == NULL)
651 return provide;
652
653 if (h->root.type == bfd_link_hash_warning)
654 h = (struct elf_link_hash_entry *) h->root.u.i.link;
655
656 if (h->versioned == unknown)
657 {
658 /* Set versioned if symbol version is unknown. */
659 char *version = strrchr (name, ELF_VER_CHR);
660 if (version)
661 {
662 if (version > name && version[-1] != ELF_VER_CHR)
663 h->versioned = versioned_hidden;
664 else
665 h->versioned = versioned;
666 }
667 }
668
669 /* Symbols defined in a linker script but not referenced anywhere
670 else will have non_elf set. */
671 if (h->non_elf)
672 {
673 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
674 h->non_elf = 0;
675 }
676
677 switch (h->root.type)
678 {
679 case bfd_link_hash_defined:
680 case bfd_link_hash_defweak:
681 case bfd_link_hash_common:
682 break;
683 case bfd_link_hash_undefweak:
684 case bfd_link_hash_undefined:
685 /* Since we're defining the symbol, don't let it seem to have not
686 been defined. record_dynamic_symbol and size_dynamic_sections
687 may depend on this. */
688 h->root.type = bfd_link_hash_new;
689 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
690 bfd_link_repair_undef_list (&htab->root);
691 break;
692 case bfd_link_hash_new:
693 break;
694 case bfd_link_hash_indirect:
695 /* We had a versioned symbol in a dynamic library. We make the
696 the versioned symbol point to this one. */
697 bed = get_elf_backend_data (output_bfd);
698 hv = h;
699 while (hv->root.type == bfd_link_hash_indirect
700 || hv->root.type == bfd_link_hash_warning)
701 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
702 /* We don't need to update h->root.u since linker will set them
703 later. */
704 h->root.type = bfd_link_hash_undefined;
705 hv->root.type = bfd_link_hash_indirect;
706 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
707 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
708 break;
709 default:
710 BFD_FAIL ();
711 return false;
712 }
713
714 /* If this symbol is being provided by the linker script, and it is
715 currently defined by a dynamic object, but not by a regular
716 object, then mark it as undefined so that the generic linker will
717 force the correct value. */
718 if (provide
719 && h->def_dynamic
720 && !h->def_regular)
721 h->root.type = bfd_link_hash_undefined;
722
723 /* If this symbol is currently defined by a dynamic object, but not
724 by a regular object, then clear out any version information because
725 the symbol will not be associated with the dynamic object any
726 more. */
727 if (h->def_dynamic && !h->def_regular)
728 h->verinfo.verdef = NULL;
729
730 /* Make sure this symbol is not garbage collected. */
731 h->mark = 1;
732
733 h->def_regular = 1;
734
735 if (hidden)
736 {
737 bed = get_elf_backend_data (output_bfd);
738 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
739 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
740 (*bed->elf_backend_hide_symbol) (info, h, true);
741 }
742
743 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
744 and executables. */
745 if (!bfd_link_relocatable (info)
746 && h->dynindx != -1
747 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
748 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
749 h->forced_local = 1;
750
751 if ((h->def_dynamic
752 || h->ref_dynamic
753 || bfd_link_dll (info)
754 || elf_hash_table (info)->is_relocatable_executable)
755 && !h->forced_local
756 && h->dynindx == -1)
757 {
758 if (! bfd_elf_link_record_dynamic_symbol (info, h))
759 return false;
760
761 /* If this is a weak defined symbol, and we know a corresponding
762 real symbol from the same dynamic object, make sure the real
763 symbol is also made into a dynamic symbol. */
764 if (h->is_weakalias)
765 {
766 struct elf_link_hash_entry *def = weakdef (h);
767
768 if (def->dynindx == -1
769 && !bfd_elf_link_record_dynamic_symbol (info, def))
770 return false;
771 }
772 }
773
774 return true;
775 }
776
777 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
778 success, and 2 on a failure caused by attempting to record a symbol
779 in a discarded section, eg. a discarded link-once section symbol. */
780
781 int
782 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
783 bfd *input_bfd,
784 long input_indx)
785 {
786 size_t amt;
787 struct elf_link_local_dynamic_entry *entry;
788 struct elf_link_hash_table *eht;
789 struct elf_strtab_hash *dynstr;
790 size_t dynstr_index;
791 char *name;
792 Elf_External_Sym_Shndx eshndx;
793 char esym[sizeof (Elf64_External_Sym)];
794
795 if (! is_elf_hash_table (info->hash))
796 return 0;
797
798 /* See if the entry exists already. */
799 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
800 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
801 return 1;
802
803 amt = sizeof (*entry);
804 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
805 if (entry == NULL)
806 return 0;
807
808 /* Go find the symbol, so that we can find it's name. */
809 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
810 1, input_indx, &entry->isym, esym, &eshndx))
811 {
812 bfd_release (input_bfd, entry);
813 return 0;
814 }
815
816 if (entry->isym.st_shndx != SHN_UNDEF
817 && entry->isym.st_shndx < SHN_LORESERVE)
818 {
819 asection *s;
820
821 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
822 if (s == NULL || bfd_is_abs_section (s->output_section))
823 {
824 /* We can still bfd_release here as nothing has done another
825 bfd_alloc. We can't do this later in this function. */
826 bfd_release (input_bfd, entry);
827 return 2;
828 }
829 }
830
831 name = (bfd_elf_string_from_elf_section
832 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
833 entry->isym.st_name));
834
835 dynstr = elf_hash_table (info)->dynstr;
836 if (dynstr == NULL)
837 {
838 /* Create a strtab to hold the dynamic symbol names. */
839 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
840 if (dynstr == NULL)
841 return 0;
842 }
843
844 dynstr_index = _bfd_elf_strtab_add (dynstr, name, false);
845 if (dynstr_index == (size_t) -1)
846 return 0;
847 entry->isym.st_name = dynstr_index;
848
849 eht = elf_hash_table (info);
850
851 entry->next = eht->dynlocal;
852 eht->dynlocal = entry;
853 entry->input_bfd = input_bfd;
854 entry->input_indx = input_indx;
855 eht->dynsymcount++;
856
857 /* Whatever binding the symbol had before, it's now local. */
858 entry->isym.st_info
859 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
860
861 /* The dynindx will be set at the end of size_dynamic_sections. */
862
863 return 1;
864 }
865
866 /* Return the dynindex of a local dynamic symbol. */
867
868 long
869 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
870 bfd *input_bfd,
871 long input_indx)
872 {
873 struct elf_link_local_dynamic_entry *e;
874
875 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
876 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
877 return e->dynindx;
878 return -1;
879 }
880
881 /* This function is used to renumber the dynamic symbols, if some of
882 them are removed because they are marked as local. This is called
883 via elf_link_hash_traverse. */
884
885 static bool
886 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
887 void *data)
888 {
889 size_t *count = (size_t *) data;
890
891 if (h->forced_local)
892 return true;
893
894 if (h->dynindx != -1)
895 h->dynindx = ++(*count);
896
897 return true;
898 }
899
900
901 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
902 STB_LOCAL binding. */
903
904 static bool
905 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
906 void *data)
907 {
908 size_t *count = (size_t *) data;
909
910 if (!h->forced_local)
911 return true;
912
913 if (h->dynindx != -1)
914 h->dynindx = ++(*count);
915
916 return true;
917 }
918
919 /* Return true if the dynamic symbol for a given section should be
920 omitted when creating a shared library. */
921 bool
922 _bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED,
923 struct bfd_link_info *info,
924 asection *p)
925 {
926 struct elf_link_hash_table *htab;
927 asection *ip;
928
929 switch (elf_section_data (p)->this_hdr.sh_type)
930 {
931 case SHT_PROGBITS:
932 case SHT_NOBITS:
933 /* If sh_type is yet undecided, assume it could be
934 SHT_PROGBITS/SHT_NOBITS. */
935 case SHT_NULL:
936 htab = elf_hash_table (info);
937 if (htab->text_index_section != NULL)
938 return p != htab->text_index_section && p != htab->data_index_section;
939
940 return (htab->dynobj != NULL
941 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
942 && ip->output_section == p);
943
944 /* There shouldn't be section relative relocations
945 against any other section. */
946 default:
947 return true;
948 }
949 }
950
951 bool
952 _bfd_elf_omit_section_dynsym_all
953 (bfd *output_bfd ATTRIBUTE_UNUSED,
954 struct bfd_link_info *info ATTRIBUTE_UNUSED,
955 asection *p ATTRIBUTE_UNUSED)
956 {
957 return true;
958 }
959
960 /* Assign dynsym indices. In a shared library we generate a section
961 symbol for each output section, which come first. Next come symbols
962 which have been forced to local binding. Then all of the back-end
963 allocated local dynamic syms, followed by the rest of the global
964 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
965 (This prevents the early call before elf_backend_init_index_section
966 and strip_excluded_output_sections setting dynindx for sections
967 that are stripped.) */
968
969 static unsigned long
970 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
971 struct bfd_link_info *info,
972 unsigned long *section_sym_count)
973 {
974 unsigned long dynsymcount = 0;
975 bool do_sec = section_sym_count != NULL;
976
977 if (bfd_link_pic (info)
978 || elf_hash_table (info)->is_relocatable_executable)
979 {
980 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
981 asection *p;
982 for (p = output_bfd->sections; p ; p = p->next)
983 if ((p->flags & SEC_EXCLUDE) == 0
984 && (p->flags & SEC_ALLOC) != 0
985 && elf_hash_table (info)->dynamic_relocs
986 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
987 {
988 ++dynsymcount;
989 if (do_sec)
990 elf_section_data (p)->dynindx = dynsymcount;
991 }
992 else if (do_sec)
993 elf_section_data (p)->dynindx = 0;
994 }
995 if (do_sec)
996 *section_sym_count = dynsymcount;
997
998 elf_link_hash_traverse (elf_hash_table (info),
999 elf_link_renumber_local_hash_table_dynsyms,
1000 &dynsymcount);
1001
1002 if (elf_hash_table (info)->dynlocal)
1003 {
1004 struct elf_link_local_dynamic_entry *p;
1005 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
1006 p->dynindx = ++dynsymcount;
1007 }
1008 elf_hash_table (info)->local_dynsymcount = dynsymcount;
1009
1010 elf_link_hash_traverse (elf_hash_table (info),
1011 elf_link_renumber_hash_table_dynsyms,
1012 &dynsymcount);
1013
1014 /* There is an unused NULL entry at the head of the table which we
1015 must account for in our count even if the table is empty since it
1016 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
1017 .dynamic section. */
1018 dynsymcount++;
1019
1020 elf_hash_table (info)->dynsymcount = dynsymcount;
1021 return dynsymcount;
1022 }
1023
1024 /* Merge st_other field. */
1025
1026 static void
1027 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
1028 unsigned int st_other, asection *sec,
1029 bool definition, bool dynamic)
1030 {
1031 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1032
1033 /* If st_other has a processor-specific meaning, specific
1034 code might be needed here. */
1035 if (bed->elf_backend_merge_symbol_attribute)
1036 (*bed->elf_backend_merge_symbol_attribute) (h, st_other, definition,
1037 dynamic);
1038
1039 if (!dynamic)
1040 {
1041 unsigned symvis = ELF_ST_VISIBILITY (st_other);
1042 unsigned hvis = ELF_ST_VISIBILITY (h->other);
1043
1044 /* Keep the most constraining visibility. Leave the remainder
1045 of the st_other field to elf_backend_merge_symbol_attribute. */
1046 if (symvis - 1 < hvis - 1)
1047 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
1048 }
1049 else if (definition
1050 && ELF_ST_VISIBILITY (st_other) != STV_DEFAULT
1051 && (sec->flags & SEC_READONLY) == 0)
1052 h->protected_def = 1;
1053 }
1054
1055 /* This function is called when we want to merge a new symbol with an
1056 existing symbol. It handles the various cases which arise when we
1057 find a definition in a dynamic object, or when there is already a
1058 definition in a dynamic object. The new symbol is described by
1059 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1060 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1061 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1062 of an old common symbol. We set OVERRIDE if the old symbol is
1063 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1064 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1065 to change. By OK to change, we mean that we shouldn't warn if the
1066 type or size does change. */
1067
1068 static bool
1069 _bfd_elf_merge_symbol (bfd *abfd,
1070 struct bfd_link_info *info,
1071 const char *name,
1072 Elf_Internal_Sym *sym,
1073 asection **psec,
1074 bfd_vma *pvalue,
1075 struct elf_link_hash_entry **sym_hash,
1076 bfd **poldbfd,
1077 bool *pold_weak,
1078 unsigned int *pold_alignment,
1079 bool *skip,
1080 bfd **override,
1081 bool *type_change_ok,
1082 bool *size_change_ok,
1083 bool *matched)
1084 {
1085 asection *sec, *oldsec;
1086 struct elf_link_hash_entry *h;
1087 struct elf_link_hash_entry *hi;
1088 struct elf_link_hash_entry *flip;
1089 int bind;
1090 bfd *oldbfd;
1091 bool newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
1092 bool newweak, oldweak, newfunc, oldfunc;
1093 const struct elf_backend_data *bed;
1094 char *new_version;
1095 bool default_sym = *matched;
1096 struct elf_link_hash_table *htab;
1097
1098 *skip = false;
1099 *override = NULL;
1100
1101 sec = *psec;
1102 bind = ELF_ST_BIND (sym->st_info);
1103
1104 if (! bfd_is_und_section (sec))
1105 h = elf_link_hash_lookup (elf_hash_table (info), name, true, false, false);
1106 else
1107 h = ((struct elf_link_hash_entry *)
1108 bfd_wrapped_link_hash_lookup (abfd, info, name, true, false, false));
1109 if (h == NULL)
1110 return false;
1111 *sym_hash = h;
1112
1113 bed = get_elf_backend_data (abfd);
1114
1115 /* NEW_VERSION is the symbol version of the new symbol. */
1116 if (h->versioned != unversioned)
1117 {
1118 /* Symbol version is unknown or versioned. */
1119 new_version = strrchr (name, ELF_VER_CHR);
1120 if (new_version)
1121 {
1122 if (h->versioned == unknown)
1123 {
1124 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1125 h->versioned = versioned_hidden;
1126 else
1127 h->versioned = versioned;
1128 }
1129 new_version += 1;
1130 if (new_version[0] == '\0')
1131 new_version = NULL;
1132 }
1133 else
1134 h->versioned = unversioned;
1135 }
1136 else
1137 new_version = NULL;
1138
1139 /* For merging, we only care about real symbols. But we need to make
1140 sure that indirect symbol dynamic flags are updated. */
1141 hi = h;
1142 while (h->root.type == bfd_link_hash_indirect
1143 || h->root.type == bfd_link_hash_warning)
1144 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1145
1146 if (!*matched)
1147 {
1148 if (hi == h || h->root.type == bfd_link_hash_new)
1149 *matched = true;
1150 else
1151 {
1152 /* OLD_HIDDEN is true if the existing symbol is only visible
1153 to the symbol with the same symbol version. NEW_HIDDEN is
1154 true if the new symbol is only visible to the symbol with
1155 the same symbol version. */
1156 bool old_hidden = h->versioned == versioned_hidden;
1157 bool new_hidden = hi->versioned == versioned_hidden;
1158 if (!old_hidden && !new_hidden)
1159 /* The new symbol matches the existing symbol if both
1160 aren't hidden. */
1161 *matched = true;
1162 else
1163 {
1164 /* OLD_VERSION is the symbol version of the existing
1165 symbol. */
1166 char *old_version;
1167
1168 if (h->versioned >= versioned)
1169 old_version = strrchr (h->root.root.string,
1170 ELF_VER_CHR) + 1;
1171 else
1172 old_version = NULL;
1173
1174 /* The new symbol matches the existing symbol if they
1175 have the same symbol version. */
1176 *matched = (old_version == new_version
1177 || (old_version != NULL
1178 && new_version != NULL
1179 && strcmp (old_version, new_version) == 0));
1180 }
1181 }
1182 }
1183
1184 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1185 existing symbol. */
1186
1187 oldbfd = NULL;
1188 oldsec = NULL;
1189 switch (h->root.type)
1190 {
1191 default:
1192 break;
1193
1194 case bfd_link_hash_undefined:
1195 case bfd_link_hash_undefweak:
1196 oldbfd = h->root.u.undef.abfd;
1197 break;
1198
1199 case bfd_link_hash_defined:
1200 case bfd_link_hash_defweak:
1201 oldbfd = h->root.u.def.section->owner;
1202 oldsec = h->root.u.def.section;
1203 break;
1204
1205 case bfd_link_hash_common:
1206 oldbfd = h->root.u.c.p->section->owner;
1207 oldsec = h->root.u.c.p->section;
1208 if (pold_alignment)
1209 *pold_alignment = h->root.u.c.p->alignment_power;
1210 break;
1211 }
1212 if (poldbfd && *poldbfd == NULL)
1213 *poldbfd = oldbfd;
1214
1215 /* Differentiate strong and weak symbols. */
1216 newweak = bind == STB_WEAK;
1217 oldweak = (h->root.type == bfd_link_hash_defweak
1218 || h->root.type == bfd_link_hash_undefweak);
1219 if (pold_weak)
1220 *pold_weak = oldweak;
1221
1222 /* We have to check it for every instance since the first few may be
1223 references and not all compilers emit symbol type for undefined
1224 symbols. */
1225 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1226
1227 htab = elf_hash_table (info);
1228
1229 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1230 respectively, is from a dynamic object. */
1231
1232 newdyn = (abfd->flags & DYNAMIC) != 0;
1233
1234 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1235 syms and defined syms in dynamic libraries respectively.
1236 ref_dynamic on the other hand can be set for a symbol defined in
1237 a dynamic library, and def_dynamic may not be set; When the
1238 definition in a dynamic lib is overridden by a definition in the
1239 executable use of the symbol in the dynamic lib becomes a
1240 reference to the executable symbol. */
1241 if (newdyn)
1242 {
1243 if (bfd_is_und_section (sec))
1244 {
1245 if (bind != STB_WEAK)
1246 {
1247 h->ref_dynamic_nonweak = 1;
1248 hi->ref_dynamic_nonweak = 1;
1249 }
1250 }
1251 else
1252 {
1253 /* Update the existing symbol only if they match. */
1254 if (*matched)
1255 h->dynamic_def = 1;
1256 hi->dynamic_def = 1;
1257 }
1258 }
1259
1260 /* If we just created the symbol, mark it as being an ELF symbol.
1261 Other than that, there is nothing to do--there is no merge issue
1262 with a newly defined symbol--so we just return. */
1263
1264 if (h->root.type == bfd_link_hash_new)
1265 {
1266 h->non_elf = 0;
1267 return true;
1268 }
1269
1270 /* In cases involving weak versioned symbols, we may wind up trying
1271 to merge a symbol with itself. Catch that here, to avoid the
1272 confusion that results if we try to override a symbol with
1273 itself. The additional tests catch cases like
1274 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1275 dynamic object, which we do want to handle here. */
1276 if (abfd == oldbfd
1277 && (newweak || oldweak)
1278 && ((abfd->flags & DYNAMIC) == 0
1279 || !h->def_regular))
1280 return true;
1281
1282 olddyn = false;
1283 if (oldbfd != NULL)
1284 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1285 else if (oldsec != NULL)
1286 {
1287 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1288 indices used by MIPS ELF. */
1289 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1290 }
1291
1292 /* Set non_ir_ref_dynamic only when not handling DT_NEEDED entries. */
1293 if (!htab->handling_dt_needed
1294 && oldbfd != NULL
1295 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN))
1296 {
1297 if (newdyn != olddyn)
1298 {
1299 /* Handle a case where plugin_notice won't be called and thus
1300 won't set the non_ir_ref flags on the first pass over
1301 symbols. */
1302 h->root.non_ir_ref_dynamic = true;
1303 hi->root.non_ir_ref_dynamic = true;
1304 }
1305 else if ((oldbfd->flags & BFD_PLUGIN) != 0
1306 && hi->root.type == bfd_link_hash_indirect)
1307 {
1308 /* Change indirect symbol from IR to undefined. */
1309 hi->root.type = bfd_link_hash_undefined;
1310 hi->root.u.undef.abfd = oldbfd;
1311 }
1312 }
1313
1314 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1315 respectively, appear to be a definition rather than reference. */
1316
1317 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1318
1319 olddef = (h->root.type != bfd_link_hash_undefined
1320 && h->root.type != bfd_link_hash_undefweak
1321 && h->root.type != bfd_link_hash_common);
1322
1323 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1324 respectively, appear to be a function. */
1325
1326 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1327 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1328
1329 oldfunc = (h->type != STT_NOTYPE
1330 && bed->is_function_type (h->type));
1331
1332 if (!(newfunc && oldfunc)
1333 && ELF_ST_TYPE (sym->st_info) != h->type
1334 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1335 && h->type != STT_NOTYPE
1336 && (newdef || bfd_is_com_section (sec))
1337 && (olddef || h->root.type == bfd_link_hash_common))
1338 {
1339 /* If creating a default indirect symbol ("foo" or "foo@") from
1340 a dynamic versioned definition ("foo@@") skip doing so if
1341 there is an existing regular definition with a different
1342 type. We don't want, for example, a "time" variable in the
1343 executable overriding a "time" function in a shared library. */
1344 if (newdyn
1345 && !olddyn)
1346 {
1347 *skip = true;
1348 return true;
1349 }
1350
1351 /* When adding a symbol from a regular object file after we have
1352 created indirect symbols, undo the indirection and any
1353 dynamic state. */
1354 if (hi != h
1355 && !newdyn
1356 && olddyn)
1357 {
1358 h = hi;
1359 (*bed->elf_backend_hide_symbol) (info, h, true);
1360 h->forced_local = 0;
1361 h->ref_dynamic = 0;
1362 h->def_dynamic = 0;
1363 h->dynamic_def = 0;
1364 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1365 {
1366 h->root.type = bfd_link_hash_undefined;
1367 h->root.u.undef.abfd = abfd;
1368 }
1369 else
1370 {
1371 h->root.type = bfd_link_hash_new;
1372 h->root.u.undef.abfd = NULL;
1373 }
1374 return true;
1375 }
1376 }
1377
1378 /* Check TLS symbols. We don't check undefined symbols introduced
1379 by "ld -u" which have no type (and oldbfd NULL), and we don't
1380 check symbols from plugins because they also have no type. */
1381 if (oldbfd != NULL
1382 && (oldbfd->flags & BFD_PLUGIN) == 0
1383 && (abfd->flags & BFD_PLUGIN) == 0
1384 && ELF_ST_TYPE (sym->st_info) != h->type
1385 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1386 {
1387 bfd *ntbfd, *tbfd;
1388 bool ntdef, tdef;
1389 asection *ntsec, *tsec;
1390
1391 if (h->type == STT_TLS)
1392 {
1393 ntbfd = abfd;
1394 ntsec = sec;
1395 ntdef = newdef;
1396 tbfd = oldbfd;
1397 tsec = oldsec;
1398 tdef = olddef;
1399 }
1400 else
1401 {
1402 ntbfd = oldbfd;
1403 ntsec = oldsec;
1404 ntdef = olddef;
1405 tbfd = abfd;
1406 tsec = sec;
1407 tdef = newdef;
1408 }
1409
1410 if (tdef && ntdef)
1411 _bfd_error_handler
1412 /* xgettext:c-format */
1413 (_("%s: TLS definition in %pB section %pA "
1414 "mismatches non-TLS definition in %pB section %pA"),
1415 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
1416 else if (!tdef && !ntdef)
1417 _bfd_error_handler
1418 /* xgettext:c-format */
1419 (_("%s: TLS reference in %pB "
1420 "mismatches non-TLS reference in %pB"),
1421 h->root.root.string, tbfd, ntbfd);
1422 else if (tdef)
1423 _bfd_error_handler
1424 /* xgettext:c-format */
1425 (_("%s: TLS definition in %pB section %pA "
1426 "mismatches non-TLS reference in %pB"),
1427 h->root.root.string, tbfd, tsec, ntbfd);
1428 else
1429 _bfd_error_handler
1430 /* xgettext:c-format */
1431 (_("%s: TLS reference in %pB "
1432 "mismatches non-TLS definition in %pB section %pA"),
1433 h->root.root.string, tbfd, ntbfd, ntsec);
1434
1435 bfd_set_error (bfd_error_bad_value);
1436 return false;
1437 }
1438
1439 /* If the old symbol has non-default visibility, we ignore the new
1440 definition from a dynamic object. */
1441 if (newdyn
1442 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1443 && !bfd_is_und_section (sec))
1444 {
1445 *skip = true;
1446 /* Make sure this symbol is dynamic. */
1447 h->ref_dynamic = 1;
1448 hi->ref_dynamic = 1;
1449 /* A protected symbol has external availability. Make sure it is
1450 recorded as dynamic.
1451
1452 FIXME: Should we check type and size for protected symbol? */
1453 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1454 return bfd_elf_link_record_dynamic_symbol (info, h);
1455 else
1456 return true;
1457 }
1458 else if (!newdyn
1459 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1460 && h->def_dynamic)
1461 {
1462 /* If the new symbol with non-default visibility comes from a
1463 relocatable file and the old definition comes from a dynamic
1464 object, we remove the old definition. */
1465 if (hi->root.type == bfd_link_hash_indirect)
1466 {
1467 /* Handle the case where the old dynamic definition is
1468 default versioned. We need to copy the symbol info from
1469 the symbol with default version to the normal one if it
1470 was referenced before. */
1471 if (h->ref_regular)
1472 {
1473 hi->root.type = h->root.type;
1474 h->root.type = bfd_link_hash_indirect;
1475 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1476
1477 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1478 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1479 {
1480 /* If the new symbol is hidden or internal, completely undo
1481 any dynamic link state. */
1482 (*bed->elf_backend_hide_symbol) (info, h, true);
1483 h->forced_local = 0;
1484 h->ref_dynamic = 0;
1485 }
1486 else
1487 h->ref_dynamic = 1;
1488
1489 h->def_dynamic = 0;
1490 /* FIXME: Should we check type and size for protected symbol? */
1491 h->size = 0;
1492 h->type = 0;
1493
1494 h = hi;
1495 }
1496 else
1497 h = hi;
1498 }
1499
1500 /* If the old symbol was undefined before, then it will still be
1501 on the undefs list. If the new symbol is undefined or
1502 common, we can't make it bfd_link_hash_new here, because new
1503 undefined or common symbols will be added to the undefs list
1504 by _bfd_generic_link_add_one_symbol. Symbols may not be
1505 added twice to the undefs list. Also, if the new symbol is
1506 undefweak then we don't want to lose the strong undef. */
1507 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1508 {
1509 h->root.type = bfd_link_hash_undefined;
1510 h->root.u.undef.abfd = abfd;
1511 }
1512 else
1513 {
1514 h->root.type = bfd_link_hash_new;
1515 h->root.u.undef.abfd = NULL;
1516 }
1517
1518 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1519 {
1520 /* If the new symbol is hidden or internal, completely undo
1521 any dynamic link state. */
1522 (*bed->elf_backend_hide_symbol) (info, h, true);
1523 h->forced_local = 0;
1524 h->ref_dynamic = 0;
1525 }
1526 else
1527 h->ref_dynamic = 1;
1528 h->def_dynamic = 0;
1529 /* FIXME: Should we check type and size for protected symbol? */
1530 h->size = 0;
1531 h->type = 0;
1532 return true;
1533 }
1534
1535 /* If a new weak symbol definition comes from a regular file and the
1536 old symbol comes from a dynamic library, we treat the new one as
1537 strong. Similarly, an old weak symbol definition from a regular
1538 file is treated as strong when the new symbol comes from a dynamic
1539 library. Further, an old weak symbol from a dynamic library is
1540 treated as strong if the new symbol is from a dynamic library.
1541 This reflects the way glibc's ld.so works.
1542
1543 Also allow a weak symbol to override a linker script symbol
1544 defined by an early pass over the script. This is done so the
1545 linker knows the symbol is defined in an object file, for the
1546 DEFINED script function.
1547
1548 Do this before setting *type_change_ok or *size_change_ok so that
1549 we warn properly when dynamic library symbols are overridden. */
1550
1551 if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
1552 newweak = false;
1553 if (olddef && newdyn)
1554 oldweak = false;
1555
1556 /* Allow changes between different types of function symbol. */
1557 if (newfunc && oldfunc)
1558 *type_change_ok = true;
1559
1560 /* It's OK to change the type if either the existing symbol or the
1561 new symbol is weak. A type change is also OK if the old symbol
1562 is undefined and the new symbol is defined. */
1563
1564 if (oldweak
1565 || newweak
1566 || (newdef
1567 && h->root.type == bfd_link_hash_undefined))
1568 *type_change_ok = true;
1569
1570 /* It's OK to change the size if either the existing symbol or the
1571 new symbol is weak, or if the old symbol is undefined. */
1572
1573 if (*type_change_ok
1574 || h->root.type == bfd_link_hash_undefined)
1575 *size_change_ok = true;
1576
1577 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1578 symbol, respectively, appears to be a common symbol in a dynamic
1579 object. If a symbol appears in an uninitialized section, and is
1580 not weak, and is not a function, then it may be a common symbol
1581 which was resolved when the dynamic object was created. We want
1582 to treat such symbols specially, because they raise special
1583 considerations when setting the symbol size: if the symbol
1584 appears as a common symbol in a regular object, and the size in
1585 the regular object is larger, we must make sure that we use the
1586 larger size. This problematic case can always be avoided in C,
1587 but it must be handled correctly when using Fortran shared
1588 libraries.
1589
1590 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1591 likewise for OLDDYNCOMMON and OLDDEF.
1592
1593 Note that this test is just a heuristic, and that it is quite
1594 possible to have an uninitialized symbol in a shared object which
1595 is really a definition, rather than a common symbol. This could
1596 lead to some minor confusion when the symbol really is a common
1597 symbol in some regular object. However, I think it will be
1598 harmless. */
1599
1600 if (newdyn
1601 && newdef
1602 && !newweak
1603 && (sec->flags & SEC_ALLOC) != 0
1604 && (sec->flags & SEC_LOAD) == 0
1605 && sym->st_size > 0
1606 && !newfunc)
1607 newdyncommon = true;
1608 else
1609 newdyncommon = false;
1610
1611 if (olddyn
1612 && olddef
1613 && h->root.type == bfd_link_hash_defined
1614 && h->def_dynamic
1615 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1616 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1617 && h->size > 0
1618 && !oldfunc)
1619 olddyncommon = true;
1620 else
1621 olddyncommon = false;
1622
1623 /* We now know everything about the old and new symbols. We ask the
1624 backend to check if we can merge them. */
1625 if (bed->merge_symbol != NULL)
1626 {
1627 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1628 return false;
1629 sec = *psec;
1630 }
1631
1632 /* There are multiple definitions of a normal symbol. Skip the
1633 default symbol as well as definition from an IR object. */
1634 if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
1635 && !default_sym && h->def_regular
1636 && !(oldbfd != NULL
1637 && (oldbfd->flags & BFD_PLUGIN) != 0
1638 && (abfd->flags & BFD_PLUGIN) == 0))
1639 {
1640 /* Handle a multiple definition. */
1641 (*info->callbacks->multiple_definition) (info, &h->root,
1642 abfd, sec, *pvalue);
1643 *skip = true;
1644 return true;
1645 }
1646
1647 /* If both the old and the new symbols look like common symbols in a
1648 dynamic object, set the size of the symbol to the larger of the
1649 two. */
1650
1651 if (olddyncommon
1652 && newdyncommon
1653 && sym->st_size != h->size)
1654 {
1655 /* Since we think we have two common symbols, issue a multiple
1656 common warning if desired. Note that we only warn if the
1657 size is different. If the size is the same, we simply let
1658 the old symbol override the new one as normally happens with
1659 symbols defined in dynamic objects. */
1660
1661 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1662 bfd_link_hash_common, sym->st_size);
1663 if (sym->st_size > h->size)
1664 h->size = sym->st_size;
1665
1666 *size_change_ok = true;
1667 }
1668
1669 /* If we are looking at a dynamic object, and we have found a
1670 definition, we need to see if the symbol was already defined by
1671 some other object. If so, we want to use the existing
1672 definition, and we do not want to report a multiple symbol
1673 definition error; we do this by clobbering *PSEC to be
1674 bfd_und_section_ptr.
1675
1676 We treat a common symbol as a definition if the symbol in the
1677 shared library is a function, since common symbols always
1678 represent variables; this can cause confusion in principle, but
1679 any such confusion would seem to indicate an erroneous program or
1680 shared library. We also permit a common symbol in a regular
1681 object to override a weak symbol in a shared object. */
1682
1683 if (newdyn
1684 && newdef
1685 && (olddef
1686 || (h->root.type == bfd_link_hash_common
1687 && (newweak || newfunc))))
1688 {
1689 *override = abfd;
1690 newdef = false;
1691 newdyncommon = false;
1692
1693 *psec = sec = bfd_und_section_ptr;
1694 *size_change_ok = true;
1695
1696 /* If we get here when the old symbol is a common symbol, then
1697 we are explicitly letting it override a weak symbol or
1698 function in a dynamic object, and we don't want to warn about
1699 a type change. If the old symbol is a defined symbol, a type
1700 change warning may still be appropriate. */
1701
1702 if (h->root.type == bfd_link_hash_common)
1703 *type_change_ok = true;
1704 }
1705
1706 /* Handle the special case of an old common symbol merging with a
1707 new symbol which looks like a common symbol in a shared object.
1708 We change *PSEC and *PVALUE to make the new symbol look like a
1709 common symbol, and let _bfd_generic_link_add_one_symbol do the
1710 right thing. */
1711
1712 if (newdyncommon
1713 && h->root.type == bfd_link_hash_common)
1714 {
1715 *override = oldbfd;
1716 newdef = false;
1717 newdyncommon = false;
1718 *pvalue = sym->st_size;
1719 *psec = sec = bed->common_section (oldsec);
1720 *size_change_ok = true;
1721 }
1722
1723 /* Skip weak definitions of symbols that are already defined. */
1724 if (newdef && olddef && newweak)
1725 {
1726 /* Don't skip new non-IR weak syms. */
1727 if (!(oldbfd != NULL
1728 && (oldbfd->flags & BFD_PLUGIN) != 0
1729 && (abfd->flags & BFD_PLUGIN) == 0))
1730 {
1731 newdef = false;
1732 *skip = true;
1733 }
1734
1735 /* Merge st_other. If the symbol already has a dynamic index,
1736 but visibility says it should not be visible, turn it into a
1737 local symbol. */
1738 elf_merge_st_other (abfd, h, sym->st_other, sec, newdef, newdyn);
1739 if (h->dynindx != -1)
1740 switch (ELF_ST_VISIBILITY (h->other))
1741 {
1742 case STV_INTERNAL:
1743 case STV_HIDDEN:
1744 (*bed->elf_backend_hide_symbol) (info, h, true);
1745 break;
1746 }
1747 }
1748
1749 /* If the old symbol is from a dynamic object, and the new symbol is
1750 a definition which is not from a dynamic object, then the new
1751 symbol overrides the old symbol. Symbols from regular files
1752 always take precedence over symbols from dynamic objects, even if
1753 they are defined after the dynamic object in the link.
1754
1755 As above, we again permit a common symbol in a regular object to
1756 override a definition in a shared object if the shared object
1757 symbol is a function or is weak. */
1758
1759 flip = NULL;
1760 if (!newdyn
1761 && (newdef
1762 || (bfd_is_com_section (sec)
1763 && (oldweak || oldfunc)))
1764 && olddyn
1765 && olddef
1766 && h->def_dynamic)
1767 {
1768 /* Change the hash table entry to undefined, and let
1769 _bfd_generic_link_add_one_symbol do the right thing with the
1770 new definition. */
1771
1772 h->root.type = bfd_link_hash_undefined;
1773 h->root.u.undef.abfd = h->root.u.def.section->owner;
1774 *size_change_ok = true;
1775
1776 olddef = false;
1777 olddyncommon = false;
1778
1779 /* We again permit a type change when a common symbol may be
1780 overriding a function. */
1781
1782 if (bfd_is_com_section (sec))
1783 {
1784 if (oldfunc)
1785 {
1786 /* If a common symbol overrides a function, make sure
1787 that it isn't defined dynamically nor has type
1788 function. */
1789 h->def_dynamic = 0;
1790 h->type = STT_NOTYPE;
1791 }
1792 *type_change_ok = true;
1793 }
1794
1795 if (hi->root.type == bfd_link_hash_indirect)
1796 flip = hi;
1797 else
1798 /* This union may have been set to be non-NULL when this symbol
1799 was seen in a dynamic object. We must force the union to be
1800 NULL, so that it is correct for a regular symbol. */
1801 h->verinfo.vertree = NULL;
1802 }
1803
1804 /* Handle the special case of a new common symbol merging with an
1805 old symbol that looks like it might be a common symbol defined in
1806 a shared object. Note that we have already handled the case in
1807 which a new common symbol should simply override the definition
1808 in the shared library. */
1809
1810 if (! newdyn
1811 && bfd_is_com_section (sec)
1812 && olddyncommon)
1813 {
1814 /* It would be best if we could set the hash table entry to a
1815 common symbol, but we don't know what to use for the section
1816 or the alignment. */
1817 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1818 bfd_link_hash_common, sym->st_size);
1819
1820 /* If the presumed common symbol in the dynamic object is
1821 larger, pretend that the new symbol has its size. */
1822
1823 if (h->size > *pvalue)
1824 *pvalue = h->size;
1825
1826 /* We need to remember the alignment required by the symbol
1827 in the dynamic object. */
1828 BFD_ASSERT (pold_alignment);
1829 *pold_alignment = h->root.u.def.section->alignment_power;
1830
1831 olddef = false;
1832 olddyncommon = false;
1833
1834 h->root.type = bfd_link_hash_undefined;
1835 h->root.u.undef.abfd = h->root.u.def.section->owner;
1836
1837 *size_change_ok = true;
1838 *type_change_ok = true;
1839
1840 if (hi->root.type == bfd_link_hash_indirect)
1841 flip = hi;
1842 else
1843 h->verinfo.vertree = NULL;
1844 }
1845
1846 if (flip != NULL)
1847 {
1848 /* Handle the case where we had a versioned symbol in a dynamic
1849 library and now find a definition in a normal object. In this
1850 case, we make the versioned symbol point to the normal one. */
1851 flip->root.type = h->root.type;
1852 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1853 h->root.type = bfd_link_hash_indirect;
1854 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1855 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1856 if (h->def_dynamic)
1857 {
1858 h->def_dynamic = 0;
1859 flip->ref_dynamic = 1;
1860 }
1861 }
1862
1863 return true;
1864 }
1865
1866 /* This function is called to create an indirect symbol from the
1867 default for the symbol with the default version if needed. The
1868 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1869 set DYNSYM if the new indirect symbol is dynamic. */
1870
1871 static bool
1872 _bfd_elf_add_default_symbol (bfd *abfd,
1873 struct bfd_link_info *info,
1874 struct elf_link_hash_entry *h,
1875 const char *name,
1876 Elf_Internal_Sym *sym,
1877 asection *sec,
1878 bfd_vma value,
1879 bfd **poldbfd,
1880 bool *dynsym)
1881 {
1882 bool type_change_ok;
1883 bool size_change_ok;
1884 bool skip;
1885 char *shortname;
1886 struct elf_link_hash_entry *hi;
1887 struct bfd_link_hash_entry *bh;
1888 const struct elf_backend_data *bed;
1889 bool collect;
1890 bool dynamic;
1891 bfd *override;
1892 char *p;
1893 size_t len, shortlen;
1894 asection *tmp_sec;
1895 bool matched;
1896
1897 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1898 return true;
1899
1900 /* If this symbol has a version, and it is the default version, we
1901 create an indirect symbol from the default name to the fully
1902 decorated name. This will cause external references which do not
1903 specify a version to be bound to this version of the symbol. */
1904 p = strchr (name, ELF_VER_CHR);
1905 if (h->versioned == unknown)
1906 {
1907 if (p == NULL)
1908 {
1909 h->versioned = unversioned;
1910 return true;
1911 }
1912 else
1913 {
1914 if (p[1] != ELF_VER_CHR)
1915 {
1916 h->versioned = versioned_hidden;
1917 return true;
1918 }
1919 else
1920 h->versioned = versioned;
1921 }
1922 }
1923 else
1924 {
1925 /* PR ld/19073: We may see an unversioned definition after the
1926 default version. */
1927 if (p == NULL)
1928 return true;
1929 }
1930
1931 bed = get_elf_backend_data (abfd);
1932 collect = bed->collect;
1933 dynamic = (abfd->flags & DYNAMIC) != 0;
1934
1935 shortlen = p - name;
1936 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1937 if (shortname == NULL)
1938 return false;
1939 memcpy (shortname, name, shortlen);
1940 shortname[shortlen] = '\0';
1941
1942 /* We are going to create a new symbol. Merge it with any existing
1943 symbol with this name. For the purposes of the merge, act as
1944 though we were defining the symbol we just defined, although we
1945 actually going to define an indirect symbol. */
1946 type_change_ok = false;
1947 size_change_ok = false;
1948 matched = true;
1949 tmp_sec = sec;
1950 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1951 &hi, poldbfd, NULL, NULL, &skip, &override,
1952 &type_change_ok, &size_change_ok, &matched))
1953 return false;
1954
1955 if (skip)
1956 goto nondefault;
1957
1958 if (hi->def_regular || ELF_COMMON_DEF_P (hi))
1959 {
1960 /* If the undecorated symbol will have a version added by a
1961 script different to H, then don't indirect to/from the
1962 undecorated symbol. This isn't ideal because we may not yet
1963 have seen symbol versions, if given by a script on the
1964 command line rather than via --version-script. */
1965 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1966 {
1967 bool hide;
1968
1969 hi->verinfo.vertree
1970 = bfd_find_version_for_sym (info->version_info,
1971 hi->root.root.string, &hide);
1972 if (hi->verinfo.vertree != NULL && hide)
1973 {
1974 (*bed->elf_backend_hide_symbol) (info, hi, true);
1975 goto nondefault;
1976 }
1977 }
1978 if (hi->verinfo.vertree != NULL
1979 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1980 goto nondefault;
1981 }
1982
1983 if (! override)
1984 {
1985 /* Add the default symbol if not performing a relocatable link. */
1986 if (! bfd_link_relocatable (info))
1987 {
1988 bh = &hi->root;
1989 if (bh->type == bfd_link_hash_defined
1990 && bh->u.def.section->owner != NULL
1991 && (bh->u.def.section->owner->flags & BFD_PLUGIN) != 0)
1992 {
1993 /* Mark the previous definition from IR object as
1994 undefined so that the generic linker will override
1995 it. */
1996 bh->type = bfd_link_hash_undefined;
1997 bh->u.undef.abfd = bh->u.def.section->owner;
1998 }
1999 if (! (_bfd_generic_link_add_one_symbol
2000 (info, abfd, shortname, BSF_INDIRECT,
2001 bfd_ind_section_ptr,
2002 0, name, false, collect, &bh)))
2003 return false;
2004 hi = (struct elf_link_hash_entry *) bh;
2005 }
2006 }
2007 else
2008 {
2009 /* In this case the symbol named SHORTNAME is overriding the
2010 indirect symbol we want to add. We were planning on making
2011 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
2012 is the name without a version. NAME is the fully versioned
2013 name, and it is the default version.
2014
2015 Overriding means that we already saw a definition for the
2016 symbol SHORTNAME in a regular object, and it is overriding
2017 the symbol defined in the dynamic object.
2018
2019 When this happens, we actually want to change NAME, the
2020 symbol we just added, to refer to SHORTNAME. This will cause
2021 references to NAME in the shared object to become references
2022 to SHORTNAME in the regular object. This is what we expect
2023 when we override a function in a shared object: that the
2024 references in the shared object will be mapped to the
2025 definition in the regular object. */
2026
2027 while (hi->root.type == bfd_link_hash_indirect
2028 || hi->root.type == bfd_link_hash_warning)
2029 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2030
2031 h->root.type = bfd_link_hash_indirect;
2032 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
2033 if (h->def_dynamic)
2034 {
2035 h->def_dynamic = 0;
2036 hi->ref_dynamic = 1;
2037 if (hi->ref_regular
2038 || hi->def_regular)
2039 {
2040 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
2041 return false;
2042 }
2043 }
2044
2045 /* Now set HI to H, so that the following code will set the
2046 other fields correctly. */
2047 hi = h;
2048 }
2049
2050 /* Check if HI is a warning symbol. */
2051 if (hi->root.type == bfd_link_hash_warning)
2052 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2053
2054 /* If there is a duplicate definition somewhere, then HI may not
2055 point to an indirect symbol. We will have reported an error to
2056 the user in that case. */
2057
2058 if (hi->root.type == bfd_link_hash_indirect)
2059 {
2060 struct elf_link_hash_entry *ht;
2061
2062 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
2063 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
2064
2065 /* If we first saw a reference to SHORTNAME with non-default
2066 visibility, merge that visibility to the @@VER symbol. */
2067 elf_merge_st_other (abfd, ht, hi->other, sec, true, dynamic);
2068
2069 /* A reference to the SHORTNAME symbol from a dynamic library
2070 will be satisfied by the versioned symbol at runtime. In
2071 effect, we have a reference to the versioned symbol. */
2072 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2073 hi->dynamic_def |= ht->dynamic_def;
2074
2075 /* See if the new flags lead us to realize that the symbol must
2076 be dynamic. */
2077 if (! *dynsym)
2078 {
2079 if (! dynamic)
2080 {
2081 if (! bfd_link_executable (info)
2082 || hi->def_dynamic
2083 || hi->ref_dynamic)
2084 *dynsym = true;
2085 }
2086 else
2087 {
2088 if (hi->ref_regular)
2089 *dynsym = true;
2090 }
2091 }
2092 }
2093
2094 /* We also need to define an indirection from the nondefault version
2095 of the symbol. */
2096
2097 nondefault:
2098 len = strlen (name);
2099 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
2100 if (shortname == NULL)
2101 return false;
2102 memcpy (shortname, name, shortlen);
2103 memcpy (shortname + shortlen, p + 1, len - shortlen);
2104
2105 /* Once again, merge with any existing symbol. */
2106 type_change_ok = false;
2107 size_change_ok = false;
2108 tmp_sec = sec;
2109 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
2110 &hi, poldbfd, NULL, NULL, &skip, &override,
2111 &type_change_ok, &size_change_ok, &matched))
2112 return false;
2113
2114 if (skip)
2115 {
2116 if (!dynamic
2117 && h->root.type == bfd_link_hash_defweak
2118 && hi->root.type == bfd_link_hash_defined)
2119 {
2120 /* We are handling a weak sym@@ver and attempting to define
2121 a weak sym@ver, but _bfd_elf_merge_symbol said to skip the
2122 new weak sym@ver because there is already a strong sym@ver.
2123 However, sym@ver and sym@@ver are really the same symbol.
2124 The existing strong sym@ver ought to override sym@@ver. */
2125 h->root.type = bfd_link_hash_defined;
2126 h->root.u.def.section = hi->root.u.def.section;
2127 h->root.u.def.value = hi->root.u.def.value;
2128 hi->root.type = bfd_link_hash_indirect;
2129 hi->root.u.i.link = &h->root;
2130 }
2131 else
2132 return true;
2133 }
2134 else if (override)
2135 {
2136 /* Here SHORTNAME is a versioned name, so we don't expect to see
2137 the type of override we do in the case above unless it is
2138 overridden by a versioned definition. */
2139 if (hi->root.type != bfd_link_hash_defined
2140 && hi->root.type != bfd_link_hash_defweak)
2141 _bfd_error_handler
2142 /* xgettext:c-format */
2143 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
2144 abfd, shortname);
2145 return true;
2146 }
2147 else
2148 {
2149 bh = &hi->root;
2150 if (! (_bfd_generic_link_add_one_symbol
2151 (info, abfd, shortname, BSF_INDIRECT,
2152 bfd_ind_section_ptr, 0, name, false, collect, &bh)))
2153 return false;
2154 hi = (struct elf_link_hash_entry *) bh;
2155 }
2156
2157 /* If there is a duplicate definition somewhere, then HI may not
2158 point to an indirect symbol. We will have reported an error
2159 to the user in that case. */
2160 if (hi->root.type == bfd_link_hash_indirect)
2161 {
2162 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
2163 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2164 hi->dynamic_def |= h->dynamic_def;
2165
2166 /* If we first saw a reference to @VER symbol with
2167 non-default visibility, merge that visibility to the
2168 @@VER symbol. */
2169 elf_merge_st_other (abfd, h, hi->other, sec, true, dynamic);
2170
2171 /* See if the new flags lead us to realize that the symbol
2172 must be dynamic. */
2173 if (! *dynsym)
2174 {
2175 if (! dynamic)
2176 {
2177 if (! bfd_link_executable (info)
2178 || hi->ref_dynamic)
2179 *dynsym = true;
2180 }
2181 else
2182 {
2183 if (hi->ref_regular)
2184 *dynsym = true;
2185 }
2186 }
2187 }
2188
2189 return true;
2190 }
2191
2192 /* This routine is used to export all defined symbols into the dynamic
2194 symbol table. It is called via elf_link_hash_traverse. */
2195
2196 static bool
2197 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
2198 {
2199 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2200
2201 /* Ignore indirect symbols. These are added by the versioning code. */
2202 if (h->root.type == bfd_link_hash_indirect)
2203 return true;
2204
2205 /* Ignore this if we won't export it. */
2206 if (!eif->info->export_dynamic && !h->dynamic)
2207 return true;
2208
2209 if (h->dynindx == -1
2210 && (h->def_regular || h->ref_regular)
2211 && ! bfd_hide_sym_by_version (eif->info->version_info,
2212 h->root.root.string))
2213 {
2214 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2215 {
2216 eif->failed = true;
2217 return false;
2218 }
2219 }
2220
2221 return true;
2222 }
2223
2224 /* Return true if GLIBC_ABI_DT_RELR is added to the list of version
2226 dependencies successfully. GLIBC_ABI_DT_RELR will be put into the
2227 .gnu.version_r section. */
2228
2229 static bool
2230 elf_link_add_dt_relr_dependency (struct elf_find_verdep_info *rinfo)
2231 {
2232 bfd *glibc_bfd = NULL;
2233 Elf_Internal_Verneed *t;
2234 Elf_Internal_Vernaux *a;
2235 size_t amt;
2236 const char *relr = "GLIBC_ABI_DT_RELR";
2237
2238 /* See if we already know about GLIBC_PRIVATE_DT_RELR. */
2239 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2240 t != NULL;
2241 t = t->vn_nextref)
2242 {
2243 const char *soname = bfd_elf_get_dt_soname (t->vn_bfd);
2244 /* Skip the shared library if it isn't libc.so. */
2245 if (!soname || !startswith (soname, "libc.so."))
2246 continue;
2247
2248 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2249 {
2250 /* Return if GLIBC_PRIVATE_DT_RELR dependency has been
2251 added. */
2252 if (a->vna_nodename == relr
2253 || strcmp (a->vna_nodename, relr) == 0)
2254 return true;
2255
2256 /* Check if libc.so provides GLIBC_2.XX version. */
2257 if (!glibc_bfd && startswith (a->vna_nodename, "GLIBC_2."))
2258 glibc_bfd = t->vn_bfd;
2259 }
2260
2261 break;
2262 }
2263
2264 /* Skip if it isn't linked against glibc. */
2265 if (glibc_bfd == NULL)
2266 return true;
2267
2268 /* This is a new version. Add it to tree we are building. */
2269 if (t == NULL)
2270 {
2271 amt = sizeof *t;
2272 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd,
2273 amt);
2274 if (t == NULL)
2275 {
2276 rinfo->failed = true;
2277 return false;
2278 }
2279
2280 t->vn_bfd = glibc_bfd;
2281 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2282 elf_tdata (rinfo->info->output_bfd)->verref = t;
2283 }
2284
2285 amt = sizeof *a;
2286 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2287 if (a == NULL)
2288 {
2289 rinfo->failed = true;
2290 return false;
2291 }
2292
2293 a->vna_nodename = relr;
2294 a->vna_flags = 0;
2295 a->vna_nextptr = t->vn_auxptr;
2296 a->vna_other = rinfo->vers + 1;
2297 ++rinfo->vers;
2298
2299 t->vn_auxptr = a;
2300
2301 return true;
2302 }
2303
2304 /* Look through the symbols which are defined in other shared
2305 libraries and referenced here. Update the list of version
2306 dependencies. This will be put into the .gnu.version_r section.
2307 This function is called via elf_link_hash_traverse. */
2308
2309 static bool
2310 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2311 void *data)
2312 {
2313 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2314 Elf_Internal_Verneed *t;
2315 Elf_Internal_Vernaux *a;
2316 size_t amt;
2317
2318 /* We only care about symbols defined in shared objects with version
2319 information. */
2320 if (!h->def_dynamic
2321 || h->def_regular
2322 || h->dynindx == -1
2323 || h->verinfo.verdef == NULL
2324 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2325 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2326 return true;
2327
2328 /* See if we already know about this version. */
2329 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2330 t != NULL;
2331 t = t->vn_nextref)
2332 {
2333 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2334 continue;
2335
2336 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2337 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2338 return true;
2339
2340 break;
2341 }
2342
2343 /* This is a new version. Add it to tree we are building. */
2344
2345 if (t == NULL)
2346 {
2347 amt = sizeof *t;
2348 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2349 if (t == NULL)
2350 {
2351 rinfo->failed = true;
2352 return false;
2353 }
2354
2355 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2356 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2357 elf_tdata (rinfo->info->output_bfd)->verref = t;
2358 }
2359
2360 amt = sizeof *a;
2361 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2362 if (a == NULL)
2363 {
2364 rinfo->failed = true;
2365 return false;
2366 }
2367
2368 /* Note that we are copying a string pointer here, and testing it
2369 above. If bfd_elf_string_from_elf_section is ever changed to
2370 discard the string data when low in memory, this will have to be
2371 fixed. */
2372 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2373
2374 a->vna_flags = h->verinfo.verdef->vd_flags;
2375 a->vna_nextptr = t->vn_auxptr;
2376
2377 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2378 ++rinfo->vers;
2379
2380 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2381
2382 t->vn_auxptr = a;
2383
2384 return true;
2385 }
2386
2387 /* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2388 hidden. Set *T_P to NULL if there is no match. */
2389
2390 static bool
2391 _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
2392 struct elf_link_hash_entry *h,
2393 const char *version_p,
2394 struct bfd_elf_version_tree **t_p,
2395 bool *hide)
2396 {
2397 struct bfd_elf_version_tree *t;
2398
2399 /* Look for the version. If we find it, it is no longer weak. */
2400 for (t = info->version_info; t != NULL; t = t->next)
2401 {
2402 if (strcmp (t->name, version_p) == 0)
2403 {
2404 size_t len;
2405 char *alc;
2406 struct bfd_elf_version_expr *d;
2407
2408 len = version_p - h->root.root.string;
2409 alc = (char *) bfd_malloc (len);
2410 if (alc == NULL)
2411 return false;
2412 memcpy (alc, h->root.root.string, len - 1);
2413 alc[len - 1] = '\0';
2414 if (alc[len - 2] == ELF_VER_CHR)
2415 alc[len - 2] = '\0';
2416
2417 h->verinfo.vertree = t;
2418 t->used = true;
2419 d = NULL;
2420
2421 if (t->globals.list != NULL)
2422 d = (*t->match) (&t->globals, NULL, alc);
2423
2424 /* See if there is anything to force this symbol to
2425 local scope. */
2426 if (d == NULL && t->locals.list != NULL)
2427 {
2428 d = (*t->match) (&t->locals, NULL, alc);
2429 if (d != NULL
2430 && h->dynindx != -1
2431 && ! info->export_dynamic)
2432 *hide = true;
2433 }
2434
2435 free (alc);
2436 break;
2437 }
2438 }
2439
2440 *t_p = t;
2441
2442 return true;
2443 }
2444
2445 /* Return TRUE if the symbol H is hidden by version script. */
2446
2447 bool
2448 _bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
2449 struct elf_link_hash_entry *h)
2450 {
2451 const char *p;
2452 bool hide = false;
2453 const struct elf_backend_data *bed
2454 = get_elf_backend_data (info->output_bfd);
2455
2456 /* Version script only hides symbols defined in regular objects. */
2457 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2458 return true;
2459
2460 p = strchr (h->root.root.string, ELF_VER_CHR);
2461 if (p != NULL && h->verinfo.vertree == NULL)
2462 {
2463 struct bfd_elf_version_tree *t;
2464
2465 ++p;
2466 if (*p == ELF_VER_CHR)
2467 ++p;
2468
2469 if (*p != '\0'
2470 && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
2471 && hide)
2472 {
2473 if (hide)
2474 (*bed->elf_backend_hide_symbol) (info, h, true);
2475 return true;
2476 }
2477 }
2478
2479 /* If we don't have a version for this symbol, see if we can find
2480 something. */
2481 if (h->verinfo.vertree == NULL && info->version_info != NULL)
2482 {
2483 h->verinfo.vertree
2484 = bfd_find_version_for_sym (info->version_info,
2485 h->root.root.string, &hide);
2486 if (h->verinfo.vertree != NULL && hide)
2487 {
2488 (*bed->elf_backend_hide_symbol) (info, h, true);
2489 return true;
2490 }
2491 }
2492
2493 return false;
2494 }
2495
2496 /* Figure out appropriate versions for all the symbols. We may not
2497 have the version number script until we have read all of the input
2498 files, so until that point we don't know which symbols should be
2499 local. This function is called via elf_link_hash_traverse. */
2500
2501 static bool
2502 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2503 {
2504 struct elf_info_failed *sinfo;
2505 struct bfd_link_info *info;
2506 const struct elf_backend_data *bed;
2507 struct elf_info_failed eif;
2508 char *p;
2509 bool hide;
2510
2511 sinfo = (struct elf_info_failed *) data;
2512 info = sinfo->info;
2513
2514 /* Fix the symbol flags. */
2515 eif.failed = false;
2516 eif.info = info;
2517 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2518 {
2519 if (eif.failed)
2520 sinfo->failed = true;
2521 return false;
2522 }
2523
2524 bed = get_elf_backend_data (info->output_bfd);
2525
2526 /* We only need version numbers for symbols defined in regular
2527 objects. */
2528 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2529 {
2530 /* Hide symbols defined in discarded input sections. */
2531 if ((h->root.type == bfd_link_hash_defined
2532 || h->root.type == bfd_link_hash_defweak)
2533 && discarded_section (h->root.u.def.section))
2534 (*bed->elf_backend_hide_symbol) (info, h, true);
2535 return true;
2536 }
2537
2538 hide = false;
2539 p = strchr (h->root.root.string, ELF_VER_CHR);
2540 if (p != NULL && h->verinfo.vertree == NULL)
2541 {
2542 struct bfd_elf_version_tree *t;
2543
2544 ++p;
2545 if (*p == ELF_VER_CHR)
2546 ++p;
2547
2548 /* If there is no version string, we can just return out. */
2549 if (*p == '\0')
2550 return true;
2551
2552 if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
2553 {
2554 sinfo->failed = true;
2555 return false;
2556 }
2557
2558 if (hide)
2559 (*bed->elf_backend_hide_symbol) (info, h, true);
2560
2561 /* If we are building an application, we need to create a
2562 version node for this version. */
2563 if (t == NULL && bfd_link_executable (info))
2564 {
2565 struct bfd_elf_version_tree **pp;
2566 int version_index;
2567
2568 /* If we aren't going to export this symbol, we don't need
2569 to worry about it. */
2570 if (h->dynindx == -1)
2571 return true;
2572
2573 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2574 sizeof *t);
2575 if (t == NULL)
2576 {
2577 sinfo->failed = true;
2578 return false;
2579 }
2580
2581 t->name = p;
2582 t->name_indx = (unsigned int) -1;
2583 t->used = true;
2584
2585 version_index = 1;
2586 /* Don't count anonymous version tag. */
2587 if (sinfo->info->version_info != NULL
2588 && sinfo->info->version_info->vernum == 0)
2589 version_index = 0;
2590 for (pp = &sinfo->info->version_info;
2591 *pp != NULL;
2592 pp = &(*pp)->next)
2593 ++version_index;
2594 t->vernum = version_index;
2595
2596 *pp = t;
2597
2598 h->verinfo.vertree = t;
2599 }
2600 else if (t == NULL)
2601 {
2602 /* We could not find the version for a symbol when
2603 generating a shared archive. Return an error. */
2604 _bfd_error_handler
2605 /* xgettext:c-format */
2606 (_("%pB: version node not found for symbol %s"),
2607 info->output_bfd, h->root.root.string);
2608 bfd_set_error (bfd_error_bad_value);
2609 sinfo->failed = true;
2610 return false;
2611 }
2612 }
2613
2614 /* If we don't have a version for this symbol, see if we can find
2615 something. */
2616 if (!hide
2617 && h->verinfo.vertree == NULL
2618 && sinfo->info->version_info != NULL)
2619 {
2620 h->verinfo.vertree
2621 = bfd_find_version_for_sym (sinfo->info->version_info,
2622 h->root.root.string, &hide);
2623 if (h->verinfo.vertree != NULL && hide)
2624 (*bed->elf_backend_hide_symbol) (info, h, true);
2625 }
2626
2627 return true;
2628 }
2629
2630 /* Read and swap the relocs from the section indicated by SHDR. This
2632 may be either a REL or a RELA section. The relocations are
2633 translated into RELA relocations and stored in INTERNAL_RELOCS,
2634 which should have already been allocated to contain enough space.
2635 The EXTERNAL_RELOCS are a buffer where the external form of the
2636 relocations should be stored.
2637
2638 Returns FALSE if something goes wrong. */
2639
2640 static bool
2641 elf_link_read_relocs_from_section (bfd *abfd,
2642 asection *sec,
2643 Elf_Internal_Shdr *shdr,
2644 void *external_relocs,
2645 Elf_Internal_Rela *internal_relocs)
2646 {
2647 const struct elf_backend_data *bed;
2648 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2649 const bfd_byte *erela;
2650 const bfd_byte *erelaend;
2651 Elf_Internal_Rela *irela;
2652 Elf_Internal_Shdr *symtab_hdr;
2653 size_t nsyms;
2654
2655 /* Position ourselves at the start of the section. */
2656 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2657 return false;
2658
2659 /* Read the relocations. */
2660 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2661 return false;
2662
2663 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2664 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2665
2666 bed = get_elf_backend_data (abfd);
2667
2668 /* Convert the external relocations to the internal format. */
2669 if (shdr->sh_entsize == bed->s->sizeof_rel)
2670 swap_in = bed->s->swap_reloc_in;
2671 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2672 swap_in = bed->s->swap_reloca_in;
2673 else
2674 {
2675 bfd_set_error (bfd_error_wrong_format);
2676 return false;
2677 }
2678
2679 erela = (const bfd_byte *) external_relocs;
2680 /* Setting erelaend like this and comparing with <= handles case of
2681 a fuzzed object with sh_size not a multiple of sh_entsize. */
2682 erelaend = erela + shdr->sh_size - shdr->sh_entsize;
2683 irela = internal_relocs;
2684 while (erela <= erelaend)
2685 {
2686 bfd_vma r_symndx;
2687
2688 (*swap_in) (abfd, erela, irela);
2689 r_symndx = ELF32_R_SYM (irela->r_info);
2690 if (bed->s->arch_size == 64)
2691 r_symndx >>= 24;
2692 if (nsyms > 0)
2693 {
2694 if ((size_t) r_symndx >= nsyms)
2695 {
2696 _bfd_error_handler
2697 /* xgettext:c-format */
2698 (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
2699 " for offset %#" PRIx64 " in section `%pA'"),
2700 abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
2701 (uint64_t) irela->r_offset, sec);
2702 bfd_set_error (bfd_error_bad_value);
2703 return false;
2704 }
2705 }
2706 else if (r_symndx != STN_UNDEF)
2707 {
2708 _bfd_error_handler
2709 /* xgettext:c-format */
2710 (_("%pB: non-zero symbol index (%#" PRIx64 ")"
2711 " for offset %#" PRIx64 " in section `%pA'"
2712 " when the object file has no symbol table"),
2713 abfd, (uint64_t) r_symndx,
2714 (uint64_t) irela->r_offset, sec);
2715 bfd_set_error (bfd_error_bad_value);
2716 return false;
2717 }
2718 irela += bed->s->int_rels_per_ext_rel;
2719 erela += shdr->sh_entsize;
2720 }
2721
2722 return true;
2723 }
2724
2725 /* Read and swap the relocs for a section O. They may have been
2726 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2727 not NULL, they are used as buffers to read into. They are known to
2728 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2729 the return value is allocated using either malloc or bfd_alloc,
2730 according to the KEEP_MEMORY argument. If O has two relocation
2731 sections (both REL and RELA relocations), then the REL_HDR
2732 relocations will appear first in INTERNAL_RELOCS, followed by the
2733 RELA_HDR relocations. If INFO isn't NULL and KEEP_MEMORY is true,
2734 update cache_size. */
2735
2736 Elf_Internal_Rela *
2737 _bfd_elf_link_info_read_relocs (bfd *abfd,
2738 struct bfd_link_info *info,
2739 asection *o,
2740 void *external_relocs,
2741 Elf_Internal_Rela *internal_relocs,
2742 bool keep_memory)
2743 {
2744 void *alloc1 = NULL;
2745 Elf_Internal_Rela *alloc2 = NULL;
2746 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2747 struct bfd_elf_section_data *esdo = elf_section_data (o);
2748 Elf_Internal_Rela *internal_rela_relocs;
2749
2750 if (esdo->relocs != NULL)
2751 return esdo->relocs;
2752
2753 if (o->reloc_count == 0)
2754 return NULL;
2755
2756 if (internal_relocs == NULL)
2757 {
2758 bfd_size_type size;
2759
2760 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
2761 if (keep_memory)
2762 {
2763 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2764 if (info)
2765 info->cache_size += size;
2766 }
2767 else
2768 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2769 if (internal_relocs == NULL)
2770 goto error_return;
2771 }
2772
2773 if (external_relocs == NULL)
2774 {
2775 bfd_size_type size = 0;
2776
2777 if (esdo->rel.hdr)
2778 size += esdo->rel.hdr->sh_size;
2779 if (esdo->rela.hdr)
2780 size += esdo->rela.hdr->sh_size;
2781
2782 alloc1 = bfd_malloc (size);
2783 if (alloc1 == NULL)
2784 goto error_return;
2785 external_relocs = alloc1;
2786 }
2787
2788 internal_rela_relocs = internal_relocs;
2789 if (esdo->rel.hdr)
2790 {
2791 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2792 external_relocs,
2793 internal_relocs))
2794 goto error_return;
2795 external_relocs = (((bfd_byte *) external_relocs)
2796 + esdo->rel.hdr->sh_size);
2797 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2798 * bed->s->int_rels_per_ext_rel);
2799 }
2800
2801 if (esdo->rela.hdr
2802 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2803 external_relocs,
2804 internal_rela_relocs)))
2805 goto error_return;
2806
2807 /* Cache the results for next time, if we can. */
2808 if (keep_memory)
2809 esdo->relocs = internal_relocs;
2810
2811 free (alloc1);
2812
2813 /* Don't free alloc2, since if it was allocated we are passing it
2814 back (under the name of internal_relocs). */
2815
2816 return internal_relocs;
2817
2818 error_return:
2819 free (alloc1);
2820 if (alloc2 != NULL)
2821 {
2822 if (keep_memory)
2823 bfd_release (abfd, alloc2);
2824 else
2825 free (alloc2);
2826 }
2827 return NULL;
2828 }
2829
2830 /* This is similar to _bfd_elf_link_info_read_relocs, except for that
2831 NULL is passed to _bfd_elf_link_info_read_relocs for pointer to
2832 struct bfd_link_info. */
2833
2834 Elf_Internal_Rela *
2835 _bfd_elf_link_read_relocs (bfd *abfd,
2836 asection *o,
2837 void *external_relocs,
2838 Elf_Internal_Rela *internal_relocs,
2839 bool keep_memory)
2840 {
2841 return _bfd_elf_link_info_read_relocs (abfd, NULL, o, external_relocs,
2842 internal_relocs, keep_memory);
2843
2844 }
2845
2846 /* Compute the size of, and allocate space for, REL_HDR which is the
2847 section header for a section containing relocations for O. */
2848
2849 static bool
2850 _bfd_elf_link_size_reloc_section (bfd *abfd,
2851 struct bfd_elf_section_reloc_data *reldata)
2852 {
2853 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2854
2855 /* That allows us to calculate the size of the section. */
2856 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2857
2858 /* The contents field must last into write_object_contents, so we
2859 allocate it with bfd_alloc rather than malloc. Also since we
2860 cannot be sure that the contents will actually be filled in,
2861 we zero the allocated space. */
2862 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2863 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2864 return false;
2865
2866 if (reldata->hashes == NULL && reldata->count)
2867 {
2868 struct elf_link_hash_entry **p;
2869
2870 p = ((struct elf_link_hash_entry **)
2871 bfd_zmalloc (reldata->count * sizeof (*p)));
2872 if (p == NULL)
2873 return false;
2874
2875 reldata->hashes = p;
2876 }
2877
2878 return true;
2879 }
2880
2881 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2882 originated from the section given by INPUT_REL_HDR) to the
2883 OUTPUT_BFD. */
2884
2885 bool
2886 _bfd_elf_link_output_relocs (bfd *output_bfd,
2887 asection *input_section,
2888 Elf_Internal_Shdr *input_rel_hdr,
2889 Elf_Internal_Rela *internal_relocs,
2890 struct elf_link_hash_entry **rel_hash
2891 ATTRIBUTE_UNUSED)
2892 {
2893 Elf_Internal_Rela *irela;
2894 Elf_Internal_Rela *irelaend;
2895 bfd_byte *erel;
2896 struct bfd_elf_section_reloc_data *output_reldata;
2897 asection *output_section;
2898 const struct elf_backend_data *bed;
2899 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2900 struct bfd_elf_section_data *esdo;
2901
2902 output_section = input_section->output_section;
2903
2904 bed = get_elf_backend_data (output_bfd);
2905 esdo = elf_section_data (output_section);
2906 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2907 {
2908 output_reldata = &esdo->rel;
2909 swap_out = bed->s->swap_reloc_out;
2910 }
2911 else if (esdo->rela.hdr
2912 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2913 {
2914 output_reldata = &esdo->rela;
2915 swap_out = bed->s->swap_reloca_out;
2916 }
2917 else
2918 {
2919 _bfd_error_handler
2920 /* xgettext:c-format */
2921 (_("%pB: relocation size mismatch in %pB section %pA"),
2922 output_bfd, input_section->owner, input_section);
2923 bfd_set_error (bfd_error_wrong_format);
2924 return false;
2925 }
2926
2927 erel = output_reldata->hdr->contents;
2928 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2929 irela = internal_relocs;
2930 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2931 * bed->s->int_rels_per_ext_rel);
2932 while (irela < irelaend)
2933 {
2934 (*swap_out) (output_bfd, irela, erel);
2935 irela += bed->s->int_rels_per_ext_rel;
2936 erel += input_rel_hdr->sh_entsize;
2937 }
2938
2939 /* Bump the counter, so that we know where to add the next set of
2940 relocations. */
2941 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2942
2943 return true;
2944 }
2945
2946 /* Make weak undefined symbols in PIE dynamic. */
2948
2949 bool
2950 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2951 struct elf_link_hash_entry *h)
2952 {
2953 if (bfd_link_pie (info)
2954 && h->dynindx == -1
2955 && h->root.type == bfd_link_hash_undefweak)
2956 return bfd_elf_link_record_dynamic_symbol (info, h);
2957
2958 return true;
2959 }
2960
2961 /* Fix up the flags for a symbol. This handles various cases which
2962 can only be fixed after all the input files are seen. This is
2963 currently called by both adjust_dynamic_symbol and
2964 assign_sym_version, which is unnecessary but perhaps more robust in
2965 the face of future changes. */
2966
2967 static bool
2968 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2969 struct elf_info_failed *eif)
2970 {
2971 const struct elf_backend_data *bed;
2972
2973 /* If this symbol was mentioned in a non-ELF file, try to set
2974 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2975 permit a non-ELF file to correctly refer to a symbol defined in
2976 an ELF dynamic object. */
2977 if (h->non_elf)
2978 {
2979 while (h->root.type == bfd_link_hash_indirect)
2980 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2981
2982 if (h->root.type != bfd_link_hash_defined
2983 && h->root.type != bfd_link_hash_defweak)
2984 {
2985 h->ref_regular = 1;
2986 h->ref_regular_nonweak = 1;
2987 }
2988 else
2989 {
2990 if (h->root.u.def.section->owner != NULL
2991 && (bfd_get_flavour (h->root.u.def.section->owner)
2992 == bfd_target_elf_flavour))
2993 {
2994 h->ref_regular = 1;
2995 h->ref_regular_nonweak = 1;
2996 }
2997 else
2998 h->def_regular = 1;
2999 }
3000
3001 if (h->dynindx == -1
3002 && (h->def_dynamic
3003 || h->ref_dynamic))
3004 {
3005 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
3006 {
3007 eif->failed = true;
3008 return false;
3009 }
3010 }
3011 }
3012 else
3013 {
3014 /* Unfortunately, NON_ELF is only correct if the symbol
3015 was first seen in a non-ELF file. Fortunately, if the symbol
3016 was first seen in an ELF file, we're probably OK unless the
3017 symbol was defined in a non-ELF file. Catch that case here.
3018 FIXME: We're still in trouble if the symbol was first seen in
3019 a dynamic object, and then later in a non-ELF regular object. */
3020 if ((h->root.type == bfd_link_hash_defined
3021 || h->root.type == bfd_link_hash_defweak)
3022 && !h->def_regular
3023 && (h->root.u.def.section->owner != NULL
3024 ? (bfd_get_flavour (h->root.u.def.section->owner)
3025 != bfd_target_elf_flavour)
3026 : (bfd_is_abs_section (h->root.u.def.section)
3027 && !h->def_dynamic)))
3028 h->def_regular = 1;
3029 }
3030
3031 /* Backend specific symbol fixup. */
3032 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
3033 if (bed->elf_backend_fixup_symbol
3034 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
3035 return false;
3036
3037 /* If this is a final link, and the symbol was defined as a common
3038 symbol in a regular object file, and there was no definition in
3039 any dynamic object, then the linker will have allocated space for
3040 the symbol in a common section but the DEF_REGULAR
3041 flag will not have been set. */
3042 if (h->root.type == bfd_link_hash_defined
3043 && !h->def_regular
3044 && h->ref_regular
3045 && !h->def_dynamic
3046 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
3047 h->def_regular = 1;
3048
3049 /* Symbols defined in discarded sections shouldn't be dynamic. */
3050 if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
3051 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3052
3053 /* If a weak undefined symbol has non-default visibility, we also
3054 hide it from the dynamic linker. */
3055 else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3056 && h->root.type == bfd_link_hash_undefweak)
3057 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3058
3059 /* A hidden versioned symbol in executable should be forced local if
3060 it is is locally defined, not referenced by shared library and not
3061 exported. */
3062 else if (bfd_link_executable (eif->info)
3063 && h->versioned == versioned_hidden
3064 && !eif->info->export_dynamic
3065 && !h->dynamic
3066 && !h->ref_dynamic
3067 && h->def_regular)
3068 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3069
3070 /* If -Bsymbolic was used (which means to bind references to global
3071 symbols to the definition within the shared object), and this
3072 symbol was defined in a regular object, then it actually doesn't
3073 need a PLT entry. Likewise, if the symbol has non-default
3074 visibility. If the symbol has hidden or internal visibility, we
3075 will force it local. */
3076 else if (h->needs_plt
3077 && bfd_link_pic (eif->info)
3078 && is_elf_hash_table (eif->info->hash)
3079 && (SYMBOLIC_BIND (eif->info, h)
3080 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
3081 && h->def_regular)
3082 {
3083 bool force_local;
3084
3085 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
3086 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
3087 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
3088 }
3089
3090 /* If this is a weak defined symbol in a dynamic object, and we know
3091 the real definition in the dynamic object, copy interesting flags
3092 over to the real definition. */
3093 if (h->is_weakalias)
3094 {
3095 struct elf_link_hash_entry *def = weakdef (h);
3096
3097 /* If the real definition is defined by a regular object file,
3098 don't do anything special. See the longer description in
3099 _bfd_elf_adjust_dynamic_symbol, below. If the def is not
3100 bfd_link_hash_defined as it was when put on the alias list
3101 then it must have originally been a versioned symbol (for
3102 which a non-versioned indirect symbol is created) and later
3103 a definition for the non-versioned symbol is found. In that
3104 case the indirection is flipped with the versioned symbol
3105 becoming an indirect pointing at the non-versioned symbol.
3106 Thus, not an alias any more. */
3107 if (def->def_regular
3108 || def->root.type != bfd_link_hash_defined)
3109 {
3110 h = def;
3111 while ((h = h->u.alias) != def)
3112 h->is_weakalias = 0;
3113 }
3114 else
3115 {
3116 while (h->root.type == bfd_link_hash_indirect)
3117 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3118 BFD_ASSERT (h->root.type == bfd_link_hash_defined
3119 || h->root.type == bfd_link_hash_defweak);
3120 BFD_ASSERT (def->def_dynamic);
3121 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
3122 }
3123 }
3124
3125 return true;
3126 }
3127
3128 /* Make the backend pick a good value for a dynamic symbol. This is
3129 called via elf_link_hash_traverse, and also calls itself
3130 recursively. */
3131
3132 static bool
3133 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
3134 {
3135 struct elf_info_failed *eif = (struct elf_info_failed *) data;
3136 struct elf_link_hash_table *htab;
3137 const struct elf_backend_data *bed;
3138
3139 if (! is_elf_hash_table (eif->info->hash))
3140 return false;
3141
3142 /* Ignore indirect symbols. These are added by the versioning code. */
3143 if (h->root.type == bfd_link_hash_indirect)
3144 return true;
3145
3146 /* Fix the symbol flags. */
3147 if (! _bfd_elf_fix_symbol_flags (h, eif))
3148 return false;
3149
3150 htab = elf_hash_table (eif->info);
3151 bed = get_elf_backend_data (htab->dynobj);
3152
3153 if (h->root.type == bfd_link_hash_undefweak)
3154 {
3155 if (eif->info->dynamic_undefined_weak == 0)
3156 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3157 else if (eif->info->dynamic_undefined_weak > 0
3158 && h->ref_regular
3159 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3160 && !bfd_hide_sym_by_version (eif->info->version_info,
3161 h->root.root.string))
3162 {
3163 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
3164 {
3165 eif->failed = true;
3166 return false;
3167 }
3168 }
3169 }
3170
3171 /* If this symbol does not require a PLT entry, and it is not
3172 defined by a dynamic object, or is not referenced by a regular
3173 object, ignore it. We do have to handle a weak defined symbol,
3174 even if no regular object refers to it, if we decided to add it
3175 to the dynamic symbol table. FIXME: Do we normally need to worry
3176 about symbols which are defined by one dynamic object and
3177 referenced by another one? */
3178 if (!h->needs_plt
3179 && h->type != STT_GNU_IFUNC
3180 && (h->def_regular
3181 || !h->def_dynamic
3182 || (!h->ref_regular
3183 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
3184 {
3185 h->plt = elf_hash_table (eif->info)->init_plt_offset;
3186 return true;
3187 }
3188
3189 /* If we've already adjusted this symbol, don't do it again. This
3190 can happen via a recursive call. */
3191 if (h->dynamic_adjusted)
3192 return true;
3193
3194 /* Don't look at this symbol again. Note that we must set this
3195 after checking the above conditions, because we may look at a
3196 symbol once, decide not to do anything, and then get called
3197 recursively later after REF_REGULAR is set below. */
3198 h->dynamic_adjusted = 1;
3199
3200 /* If this is a weak definition, and we know a real definition, and
3201 the real symbol is not itself defined by a regular object file,
3202 then get a good value for the real definition. We handle the
3203 real symbol first, for the convenience of the backend routine.
3204
3205 Note that there is a confusing case here. If the real definition
3206 is defined by a regular object file, we don't get the real symbol
3207 from the dynamic object, but we do get the weak symbol. If the
3208 processor backend uses a COPY reloc, then if some routine in the
3209 dynamic object changes the real symbol, we will not see that
3210 change in the corresponding weak symbol. This is the way other
3211 ELF linkers work as well, and seems to be a result of the shared
3212 library model.
3213
3214 I will clarify this issue. Most SVR4 shared libraries define the
3215 variable _timezone and define timezone as a weak synonym. The
3216 tzset call changes _timezone. If you write
3217 extern int timezone;
3218 int _timezone = 5;
3219 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3220 you might expect that, since timezone is a synonym for _timezone,
3221 the same number will print both times. However, if the processor
3222 backend uses a COPY reloc, then actually timezone will be copied
3223 into your process image, and, since you define _timezone
3224 yourself, _timezone will not. Thus timezone and _timezone will
3225 wind up at different memory locations. The tzset call will set
3226 _timezone, leaving timezone unchanged. */
3227
3228 if (h->is_weakalias)
3229 {
3230 struct elf_link_hash_entry *def = weakdef (h);
3231
3232 /* If we get to this point, there is an implicit reference to
3233 the alias by a regular object file via the weak symbol H. */
3234 def->ref_regular = 1;
3235
3236 /* Ensure that the backend adjust_dynamic_symbol function sees
3237 the strong alias before H by recursively calling ourselves. */
3238 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
3239 return false;
3240 }
3241
3242 /* If a symbol has no type and no size and does not require a PLT
3243 entry, then we are probably about to do the wrong thing here: we
3244 are probably going to create a COPY reloc for an empty object.
3245 This case can arise when a shared object is built with assembly
3246 code, and the assembly code fails to set the symbol type. */
3247 if (h->size == 0
3248 && h->type == STT_NOTYPE
3249 && !h->needs_plt)
3250 _bfd_error_handler
3251 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3252 h->root.root.string);
3253
3254 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3255 {
3256 eif->failed = true;
3257 return false;
3258 }
3259
3260 return true;
3261 }
3262
3263 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3264 DYNBSS. */
3265
3266 bool
3267 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3268 struct elf_link_hash_entry *h,
3269 asection *dynbss)
3270 {
3271 unsigned int power_of_two;
3272 bfd_vma mask;
3273 asection *sec = h->root.u.def.section;
3274
3275 /* The section alignment of the definition is the maximum alignment
3276 requirement of symbols defined in the section. Since we don't
3277 know the symbol alignment requirement, we start with the
3278 maximum alignment and check low bits of the symbol address
3279 for the minimum alignment. */
3280 power_of_two = bfd_section_alignment (sec);
3281 mask = ((bfd_vma) 1 << power_of_two) - 1;
3282 while ((h->root.u.def.value & mask) != 0)
3283 {
3284 mask >>= 1;
3285 --power_of_two;
3286 }
3287
3288 if (power_of_two > bfd_section_alignment (dynbss))
3289 {
3290 /* Adjust the section alignment if needed. */
3291 if (!bfd_set_section_alignment (dynbss, power_of_two))
3292 return false;
3293 }
3294
3295 /* We make sure that the symbol will be aligned properly. */
3296 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3297
3298 /* Define the symbol as being at this point in DYNBSS. */
3299 h->root.u.def.section = dynbss;
3300 h->root.u.def.value = dynbss->size;
3301
3302 /* Increment the size of DYNBSS to make room for the symbol. */
3303 dynbss->size += h->size;
3304
3305 /* No error if extern_protected_data is true. */
3306 if (h->protected_def
3307 && (!info->extern_protected_data
3308 || (info->extern_protected_data < 0
3309 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
3310 info->callbacks->einfo
3311 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3312 h->root.root.string);
3313
3314 return true;
3315 }
3316
3317 /* Adjust all external symbols pointing into SEC_MERGE sections
3318 to reflect the object merging within the sections. */
3319
3320 static bool
3321 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
3322 {
3323 asection *sec;
3324
3325 if ((h->root.type == bfd_link_hash_defined
3326 || h->root.type == bfd_link_hash_defweak)
3327 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
3328 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3329 {
3330 bfd *output_bfd = (bfd *) data;
3331
3332 h->root.u.def.value =
3333 _bfd_merged_section_offset (output_bfd,
3334 &h->root.u.def.section,
3335 elf_section_data (sec)->sec_info,
3336 h->root.u.def.value);
3337 }
3338
3339 return true;
3340 }
3341
3342 /* Returns false if the symbol referred to by H should be considered
3343 to resolve local to the current module, and true if it should be
3344 considered to bind dynamically. */
3345
3346 bool
3347 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3348 struct bfd_link_info *info,
3349 bool not_local_protected)
3350 {
3351 bool binding_stays_local_p;
3352 const struct elf_backend_data *bed;
3353 struct elf_link_hash_table *hash_table;
3354
3355 if (h == NULL)
3356 return false;
3357
3358 while (h->root.type == bfd_link_hash_indirect
3359 || h->root.type == bfd_link_hash_warning)
3360 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3361
3362 /* If it was forced local, then clearly it's not dynamic. */
3363 if (h->dynindx == -1)
3364 return false;
3365 if (h->forced_local)
3366 return false;
3367
3368 /* Identify the cases where name binding rules say that a
3369 visible symbol resolves locally. */
3370 binding_stays_local_p = (bfd_link_executable (info)
3371 || SYMBOLIC_BIND (info, h));
3372
3373 switch (ELF_ST_VISIBILITY (h->other))
3374 {
3375 case STV_INTERNAL:
3376 case STV_HIDDEN:
3377 return false;
3378
3379 case STV_PROTECTED:
3380 hash_table = elf_hash_table (info);
3381 if (!is_elf_hash_table (&hash_table->root))
3382 return false;
3383
3384 bed = get_elf_backend_data (hash_table->dynobj);
3385
3386 /* Proper resolution for function pointer equality may require
3387 that these symbols perhaps be resolved dynamically, even though
3388 we should be resolving them to the current module. */
3389 if (!not_local_protected || !bed->is_function_type (h->type))
3390 binding_stays_local_p = true;
3391 break;
3392
3393 default:
3394 break;
3395 }
3396
3397 /* If it isn't defined locally, then clearly it's dynamic. */
3398 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
3399 return true;
3400
3401 /* Otherwise, the symbol is dynamic if binding rules don't tell
3402 us that it remains local. */
3403 return !binding_stays_local_p;
3404 }
3405
3406 /* Return true if the symbol referred to by H should be considered
3407 to resolve local to the current module, and false otherwise. Differs
3408 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3409 undefined symbols. The two functions are virtually identical except
3410 for the place where dynindx == -1 is tested. If that test is true,
3411 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3412 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3413 defined symbols.
3414 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3415 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3416 treatment of undefined weak symbols. For those that do not make
3417 undefined weak symbols dynamic, both functions may return false. */
3418
3419 bool
3420 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3421 struct bfd_link_info *info,
3422 bool local_protected)
3423 {
3424 const struct elf_backend_data *bed;
3425 struct elf_link_hash_table *hash_table;
3426
3427 /* If it's a local sym, of course we resolve locally. */
3428 if (h == NULL)
3429 return true;
3430
3431 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3432 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3433 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3434 return true;
3435
3436 /* Forced local symbols resolve locally. */
3437 if (h->forced_local)
3438 return true;
3439
3440 /* Common symbols that become definitions don't get the DEF_REGULAR
3441 flag set, so test it first, and don't bail out. */
3442 if (ELF_COMMON_DEF_P (h))
3443 /* Do nothing. */;
3444 /* If we don't have a definition in a regular file, then we can't
3445 resolve locally. The sym is either undefined or dynamic. */
3446 else if (!h->def_regular)
3447 return false;
3448
3449 /* Non-dynamic symbols resolve locally. */
3450 if (h->dynindx == -1)
3451 return true;
3452
3453 /* At this point, we know the symbol is defined and dynamic. In an
3454 executable it must resolve locally, likewise when building symbolic
3455 shared libraries. */
3456 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3457 return true;
3458
3459 /* Now deal with defined dynamic symbols in shared libraries. Ones
3460 with default visibility might not resolve locally. */
3461 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3462 return false;
3463
3464 hash_table = elf_hash_table (info);
3465 if (!is_elf_hash_table (&hash_table->root))
3466 return true;
3467
3468 /* STV_PROTECTED symbols with indirect external access are local. */
3469 if (info->indirect_extern_access > 0)
3470 return true;
3471
3472 bed = get_elf_backend_data (hash_table->dynobj);
3473
3474 /* If extern_protected_data is false, STV_PROTECTED non-function
3475 symbols are local. */
3476 if ((!info->extern_protected_data
3477 || (info->extern_protected_data < 0
3478 && !bed->extern_protected_data))
3479 && !bed->is_function_type (h->type))
3480 return true;
3481
3482 /* Function pointer equality tests may require that STV_PROTECTED
3483 symbols be treated as dynamic symbols. If the address of a
3484 function not defined in an executable is set to that function's
3485 plt entry in the executable, then the address of the function in
3486 a shared library must also be the plt entry in the executable. */
3487 return local_protected;
3488 }
3489
3490 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3491 aligned. Returns the first TLS output section. */
3492
3493 struct bfd_section *
3494 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3495 {
3496 struct bfd_section *sec, *tls;
3497 unsigned int align = 0;
3498
3499 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3500 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3501 break;
3502 tls = sec;
3503
3504 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3505 if (sec->alignment_power > align)
3506 align = sec->alignment_power;
3507
3508 elf_hash_table (info)->tls_sec = tls;
3509
3510 /* Ensure the alignment of the first section (usually .tdata) is the largest
3511 alignment, so that the tls segment starts aligned. */
3512 if (tls != NULL)
3513 tls->alignment_power = align;
3514
3515 return tls;
3516 }
3517
3518 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3519 static bool
3520 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3521 Elf_Internal_Sym *sym)
3522 {
3523 const struct elf_backend_data *bed;
3524
3525 /* Local symbols do not count, but target specific ones might. */
3526 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3527 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3528 return false;
3529
3530 bed = get_elf_backend_data (abfd);
3531 /* Function symbols do not count. */
3532 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3533 return false;
3534
3535 /* If the section is undefined, then so is the symbol. */
3536 if (sym->st_shndx == SHN_UNDEF)
3537 return false;
3538
3539 /* If the symbol is defined in the common section, then
3540 it is a common definition and so does not count. */
3541 if (bed->common_definition (sym))
3542 return false;
3543
3544 /* If the symbol is in a target specific section then we
3545 must rely upon the backend to tell us what it is. */
3546 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3547 /* FIXME - this function is not coded yet:
3548
3549 return _bfd_is_global_symbol_definition (abfd, sym);
3550
3551 Instead for now assume that the definition is not global,
3552 Even if this is wrong, at least the linker will behave
3553 in the same way that it used to do. */
3554 return false;
3555
3556 return true;
3557 }
3558
3559 /* Search the symbol table of the archive element of the archive ABFD
3560 whose archive map contains a mention of SYMDEF, and determine if
3561 the symbol is defined in this element. */
3562 static bool
3563 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3564 {
3565 Elf_Internal_Shdr * hdr;
3566 size_t symcount;
3567 size_t extsymcount;
3568 size_t extsymoff;
3569 Elf_Internal_Sym *isymbuf;
3570 Elf_Internal_Sym *isym;
3571 Elf_Internal_Sym *isymend;
3572 bool result;
3573
3574 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset, NULL);
3575 if (abfd == NULL)
3576 return false;
3577
3578 if (! bfd_check_format (abfd, bfd_object))
3579 return false;
3580
3581 /* Select the appropriate symbol table. If we don't know if the
3582 object file is an IR object, give linker LTO plugin a chance to
3583 get the correct symbol table. */
3584 if (abfd->plugin_format == bfd_plugin_yes
3585 #if BFD_SUPPORTS_PLUGINS
3586 || (abfd->plugin_format == bfd_plugin_unknown
3587 && bfd_link_plugin_object_p (abfd))
3588 #endif
3589 )
3590 {
3591 /* Use the IR symbol table if the object has been claimed by
3592 plugin. */
3593 abfd = abfd->plugin_dummy_bfd;
3594 hdr = &elf_tdata (abfd)->symtab_hdr;
3595 }
3596 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3597 hdr = &elf_tdata (abfd)->symtab_hdr;
3598 else
3599 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3600
3601 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3602
3603 /* The sh_info field of the symtab header tells us where the
3604 external symbols start. We don't care about the local symbols. */
3605 if (elf_bad_symtab (abfd))
3606 {
3607 extsymcount = symcount;
3608 extsymoff = 0;
3609 }
3610 else
3611 {
3612 extsymcount = symcount - hdr->sh_info;
3613 extsymoff = hdr->sh_info;
3614 }
3615
3616 if (extsymcount == 0)
3617 return false;
3618
3619 /* Read in the symbol table. */
3620 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3621 NULL, NULL, NULL);
3622 if (isymbuf == NULL)
3623 return false;
3624
3625 /* Scan the symbol table looking for SYMDEF. */
3626 result = false;
3627 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3628 {
3629 const char *name;
3630
3631 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3632 isym->st_name);
3633 if (name == NULL)
3634 break;
3635
3636 if (strcmp (name, symdef->name) == 0)
3637 {
3638 result = is_global_data_symbol_definition (abfd, isym);
3639 break;
3640 }
3641 }
3642
3643 free (isymbuf);
3644
3645 return result;
3646 }
3647
3648 /* Add an entry to the .dynamic table. */
3650
3651 bool
3652 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3653 bfd_vma tag,
3654 bfd_vma val)
3655 {
3656 struct elf_link_hash_table *hash_table;
3657 const struct elf_backend_data *bed;
3658 asection *s;
3659 bfd_size_type newsize;
3660 bfd_byte *newcontents;
3661 Elf_Internal_Dyn dyn;
3662
3663 hash_table = elf_hash_table (info);
3664 if (! is_elf_hash_table (&hash_table->root))
3665 return false;
3666
3667 if (tag == DT_RELA || tag == DT_REL)
3668 hash_table->dynamic_relocs = true;
3669
3670 bed = get_elf_backend_data (hash_table->dynobj);
3671 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3672 BFD_ASSERT (s != NULL);
3673
3674 newsize = s->size + bed->s->sizeof_dyn;
3675 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3676 if (newcontents == NULL)
3677 return false;
3678
3679 dyn.d_tag = tag;
3680 dyn.d_un.d_val = val;
3681 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3682
3683 s->size = newsize;
3684 s->contents = newcontents;
3685
3686 return true;
3687 }
3688
3689 /* Strip zero-sized dynamic sections. */
3690
3691 bool
3692 _bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info *info)
3693 {
3694 struct elf_link_hash_table *hash_table;
3695 const struct elf_backend_data *bed;
3696 asection *s, *sdynamic, **pp;
3697 asection *rela_dyn, *rel_dyn;
3698 Elf_Internal_Dyn dyn;
3699 bfd_byte *extdyn, *next;
3700 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3701 bool strip_zero_sized;
3702 bool strip_zero_sized_plt;
3703
3704 if (bfd_link_relocatable (info))
3705 return true;
3706
3707 hash_table = elf_hash_table (info);
3708 if (!is_elf_hash_table (&hash_table->root))
3709 return false;
3710
3711 if (!hash_table->dynobj)
3712 return true;
3713
3714 sdynamic= bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3715 if (!sdynamic)
3716 return true;
3717
3718 bed = get_elf_backend_data (hash_table->dynobj);
3719 swap_dyn_in = bed->s->swap_dyn_in;
3720
3721 strip_zero_sized = false;
3722 strip_zero_sized_plt = false;
3723
3724 /* Strip zero-sized dynamic sections. */
3725 rela_dyn = bfd_get_section_by_name (info->output_bfd, ".rela.dyn");
3726 rel_dyn = bfd_get_section_by_name (info->output_bfd, ".rel.dyn");
3727 for (pp = &info->output_bfd->sections; (s = *pp) != NULL;)
3728 if (s->size == 0
3729 && (s == rela_dyn
3730 || s == rel_dyn
3731 || s == hash_table->srelplt->output_section
3732 || s == hash_table->splt->output_section))
3733 {
3734 *pp = s->next;
3735 info->output_bfd->section_count--;
3736 strip_zero_sized = true;
3737 if (s == rela_dyn)
3738 s = rela_dyn;
3739 if (s == rel_dyn)
3740 s = rel_dyn;
3741 else if (s == hash_table->splt->output_section)
3742 {
3743 s = hash_table->splt;
3744 strip_zero_sized_plt = true;
3745 }
3746 else
3747 s = hash_table->srelplt;
3748 s->flags |= SEC_EXCLUDE;
3749 s->output_section = bfd_abs_section_ptr;
3750 }
3751 else
3752 pp = &s->next;
3753
3754 if (strip_zero_sized_plt && sdynamic->size != 0)
3755 for (extdyn = sdynamic->contents;
3756 extdyn < sdynamic->contents + sdynamic->size;
3757 extdyn = next)
3758 {
3759 next = extdyn + bed->s->sizeof_dyn;
3760 swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3761 switch (dyn.d_tag)
3762 {
3763 default:
3764 break;
3765 case DT_JMPREL:
3766 case DT_PLTRELSZ:
3767 case DT_PLTREL:
3768 /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
3769 the procedure linkage table (the .plt section) has been
3770 removed. */
3771 memmove (extdyn, next,
3772 sdynamic->size - (next - sdynamic->contents));
3773 next = extdyn;
3774 }
3775 }
3776
3777 if (strip_zero_sized)
3778 {
3779 /* Regenerate program headers. */
3780 elf_seg_map (info->output_bfd) = NULL;
3781 return _bfd_elf_map_sections_to_segments (info->output_bfd, info,
3782 NULL);
3783 }
3784
3785 return true;
3786 }
3787
3788 /* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error,
3789 1 if a DT_NEEDED tag already exists, and 0 on success. */
3790
3791 int
3792 bfd_elf_add_dt_needed_tag (bfd *abfd, struct bfd_link_info *info)
3793 {
3794 struct elf_link_hash_table *hash_table;
3795 size_t strindex;
3796 const char *soname;
3797
3798 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3799 return -1;
3800
3801 hash_table = elf_hash_table (info);
3802 soname = elf_dt_name (abfd);
3803 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, false);
3804 if (strindex == (size_t) -1)
3805 return -1;
3806
3807 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3808 {
3809 asection *sdyn;
3810 const struct elf_backend_data *bed;
3811 bfd_byte *extdyn;
3812
3813 bed = get_elf_backend_data (hash_table->dynobj);
3814 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3815 if (sdyn != NULL && sdyn->size != 0)
3816 for (extdyn = sdyn->contents;
3817 extdyn < sdyn->contents + sdyn->size;
3818 extdyn += bed->s->sizeof_dyn)
3819 {
3820 Elf_Internal_Dyn dyn;
3821
3822 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3823 if (dyn.d_tag == DT_NEEDED
3824 && dyn.d_un.d_val == strindex)
3825 {
3826 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3827 return 1;
3828 }
3829 }
3830 }
3831
3832 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3833 return -1;
3834
3835 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3836 return -1;
3837
3838 return 0;
3839 }
3840
3841 /* Return true if SONAME is on the needed list between NEEDED and STOP
3842 (or the end of list if STOP is NULL), and needed by a library that
3843 will be loaded. */
3844
3845 static bool
3846 on_needed_list (const char *soname,
3847 struct bfd_link_needed_list *needed,
3848 struct bfd_link_needed_list *stop)
3849 {
3850 struct bfd_link_needed_list *look;
3851 for (look = needed; look != stop; look = look->next)
3852 if (strcmp (soname, look->name) == 0
3853 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3854 /* If needed by a library that itself is not directly
3855 needed, recursively check whether that library is
3856 indirectly needed. Since we add DT_NEEDED entries to
3857 the end of the list, library dependencies appear after
3858 the library. Therefore search prior to the current
3859 LOOK, preventing possible infinite recursion. */
3860 || on_needed_list (elf_dt_name (look->by), needed, look)))
3861 return true;
3862
3863 return false;
3864 }
3865
3866 /* Sort symbol by value, section, size, and type. */
3867 static int
3868 elf_sort_symbol (const void *arg1, const void *arg2)
3869 {
3870 const struct elf_link_hash_entry *h1;
3871 const struct elf_link_hash_entry *h2;
3872 bfd_signed_vma vdiff;
3873 int sdiff;
3874 const char *n1;
3875 const char *n2;
3876
3877 h1 = *(const struct elf_link_hash_entry **) arg1;
3878 h2 = *(const struct elf_link_hash_entry **) arg2;
3879 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3880 if (vdiff != 0)
3881 return vdiff > 0 ? 1 : -1;
3882
3883 sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3884 if (sdiff != 0)
3885 return sdiff;
3886
3887 /* Sort so that sized symbols are selected over zero size symbols. */
3888 vdiff = h1->size - h2->size;
3889 if (vdiff != 0)
3890 return vdiff > 0 ? 1 : -1;
3891
3892 /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */
3893 if (h1->type != h2->type)
3894 return h1->type - h2->type;
3895
3896 /* If symbols are properly sized and typed, and multiple strong
3897 aliases are not defined in a shared library by the user we
3898 shouldn't get here. Unfortunately linker script symbols like
3899 __bss_start sometimes match a user symbol defined at the start of
3900 .bss without proper size and type. We'd like to preference the
3901 user symbol over reserved system symbols. Sort on leading
3902 underscores. */
3903 n1 = h1->root.root.string;
3904 n2 = h2->root.root.string;
3905 while (*n1 == *n2)
3906 {
3907 if (*n1 == 0)
3908 break;
3909 ++n1;
3910 ++n2;
3911 }
3912 if (*n1 == '_')
3913 return -1;
3914 if (*n2 == '_')
3915 return 1;
3916
3917 /* Final sort on name selects user symbols like '_u' over reserved
3918 system symbols like '_Z' and also will avoid qsort instability. */
3919 return *n1 - *n2;
3920 }
3921
3922 /* This function is used to adjust offsets into .dynstr for
3923 dynamic symbols. This is called via elf_link_hash_traverse. */
3924
3925 static bool
3926 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3927 {
3928 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3929
3930 if (h->dynindx != -1)
3931 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3932 return true;
3933 }
3934
3935 /* Assign string offsets in .dynstr, update all structures referencing
3936 them. */
3937
3938 static bool
3939 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3940 {
3941 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3942 struct elf_link_local_dynamic_entry *entry;
3943 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3944 bfd *dynobj = hash_table->dynobj;
3945 asection *sdyn;
3946 bfd_size_type size;
3947 const struct elf_backend_data *bed;
3948 bfd_byte *extdyn;
3949
3950 _bfd_elf_strtab_finalize (dynstr);
3951 size = _bfd_elf_strtab_size (dynstr);
3952
3953 /* Allow the linker to examine the dynsymtab now it's fully populated. */
3954
3955 if (info->callbacks->examine_strtab)
3956 info->callbacks->examine_strtab (dynstr);
3957
3958 bed = get_elf_backend_data (dynobj);
3959 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3960 BFD_ASSERT (sdyn != NULL);
3961
3962 /* Update all .dynamic entries referencing .dynstr strings. */
3963 for (extdyn = sdyn->contents;
3964 extdyn < PTR_ADD (sdyn->contents, sdyn->size);
3965 extdyn += bed->s->sizeof_dyn)
3966 {
3967 Elf_Internal_Dyn dyn;
3968
3969 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3970 switch (dyn.d_tag)
3971 {
3972 case DT_STRSZ:
3973 dyn.d_un.d_val = size;
3974 break;
3975 case DT_NEEDED:
3976 case DT_SONAME:
3977 case DT_RPATH:
3978 case DT_RUNPATH:
3979 case DT_FILTER:
3980 case DT_AUXILIARY:
3981 case DT_AUDIT:
3982 case DT_DEPAUDIT:
3983 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3984 break;
3985 default:
3986 continue;
3987 }
3988 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3989 }
3990
3991 /* Now update local dynamic symbols. */
3992 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3993 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3994 entry->isym.st_name);
3995
3996 /* And the rest of dynamic symbols. */
3997 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3998
3999 /* Adjust version definitions. */
4000 if (elf_tdata (output_bfd)->cverdefs)
4001 {
4002 asection *s;
4003 bfd_byte *p;
4004 size_t i;
4005 Elf_Internal_Verdef def;
4006 Elf_Internal_Verdaux defaux;
4007
4008 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
4009 p = s->contents;
4010 do
4011 {
4012 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
4013 &def);
4014 p += sizeof (Elf_External_Verdef);
4015 if (def.vd_aux != sizeof (Elf_External_Verdef))
4016 continue;
4017 for (i = 0; i < def.vd_cnt; ++i)
4018 {
4019 _bfd_elf_swap_verdaux_in (output_bfd,
4020 (Elf_External_Verdaux *) p, &defaux);
4021 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
4022 defaux.vda_name);
4023 _bfd_elf_swap_verdaux_out (output_bfd,
4024 &defaux, (Elf_External_Verdaux *) p);
4025 p += sizeof (Elf_External_Verdaux);
4026 }
4027 }
4028 while (def.vd_next);
4029 }
4030
4031 /* Adjust version references. */
4032 if (elf_tdata (output_bfd)->verref)
4033 {
4034 asection *s;
4035 bfd_byte *p;
4036 size_t i;
4037 Elf_Internal_Verneed need;
4038 Elf_Internal_Vernaux needaux;
4039
4040 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
4041 p = s->contents;
4042 do
4043 {
4044 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
4045 &need);
4046 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
4047 _bfd_elf_swap_verneed_out (output_bfd, &need,
4048 (Elf_External_Verneed *) p);
4049 p += sizeof (Elf_External_Verneed);
4050 for (i = 0; i < need.vn_cnt; ++i)
4051 {
4052 _bfd_elf_swap_vernaux_in (output_bfd,
4053 (Elf_External_Vernaux *) p, &needaux);
4054 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
4055 needaux.vna_name);
4056 _bfd_elf_swap_vernaux_out (output_bfd,
4057 &needaux,
4058 (Elf_External_Vernaux *) p);
4059 p += sizeof (Elf_External_Vernaux);
4060 }
4061 }
4062 while (need.vn_next);
4063 }
4064
4065 return true;
4066 }
4067
4068 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4070 The default is to only match when the INPUT and OUTPUT are exactly
4071 the same target. */
4072
4073 bool
4074 _bfd_elf_default_relocs_compatible (const bfd_target *input,
4075 const bfd_target *output)
4076 {
4077 return input == output;
4078 }
4079
4080 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4081 This version is used when different targets for the same architecture
4082 are virtually identical. */
4083
4084 bool
4085 _bfd_elf_relocs_compatible (const bfd_target *input,
4086 const bfd_target *output)
4087 {
4088 const struct elf_backend_data *obed, *ibed;
4089
4090 if (input == output)
4091 return true;
4092
4093 ibed = xvec_get_elf_backend_data (input);
4094 obed = xvec_get_elf_backend_data (output);
4095
4096 if (ibed->arch != obed->arch)
4097 return false;
4098
4099 /* If both backends are using this function, deem them compatible. */
4100 return ibed->relocs_compatible == obed->relocs_compatible;
4101 }
4102
4103 /* Make a special call to the linker "notice" function to tell it that
4104 we are about to handle an as-needed lib, or have finished
4105 processing the lib. */
4106
4107 bool
4108 _bfd_elf_notice_as_needed (bfd *ibfd,
4109 struct bfd_link_info *info,
4110 enum notice_asneeded_action act)
4111 {
4112 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
4113 }
4114
4115 /* Call ACTION on each relocation in an ELF object file. */
4116
4117 bool
4118 _bfd_elf_link_iterate_on_relocs
4119 (bfd *abfd, struct bfd_link_info *info,
4120 bool (*action) (bfd *, struct bfd_link_info *, asection *,
4121 const Elf_Internal_Rela *))
4122 {
4123 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4124 struct elf_link_hash_table *htab = elf_hash_table (info);
4125
4126 /* If this object is the same format as the output object, and it is
4127 not a shared library, then let the backend look through the
4128 relocs.
4129
4130 This is required to build global offset table entries and to
4131 arrange for dynamic relocs. It is not required for the
4132 particular common case of linking non PIC code, even when linking
4133 against shared libraries, but unfortunately there is no way of
4134 knowing whether an object file has been compiled PIC or not.
4135 Looking through the relocs is not particularly time consuming.
4136 The problem is that we must either (1) keep the relocs in memory,
4137 which causes the linker to require additional runtime memory or
4138 (2) read the relocs twice from the input file, which wastes time.
4139 This would be a good case for using mmap.
4140
4141 I have no idea how to handle linking PIC code into a file of a
4142 different format. It probably can't be done. */
4143 if ((abfd->flags & DYNAMIC) == 0
4144 && is_elf_hash_table (&htab->root)
4145 && elf_object_id (abfd) == elf_hash_table_id (htab)
4146 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4147 {
4148 asection *o;
4149
4150 for (o = abfd->sections; o != NULL; o = o->next)
4151 {
4152 Elf_Internal_Rela *internal_relocs;
4153 bool ok;
4154
4155 /* Don't check relocations in excluded sections. Don't do
4156 anything special with non-loaded, non-alloced sections.
4157 In particular, any relocs in such sections should not
4158 affect GOT and PLT reference counting (ie. we don't
4159 allow them to create GOT or PLT entries), there's no
4160 possibility or desire to optimize TLS relocs, and
4161 there's not much point in propagating relocs to shared
4162 libs that the dynamic linker won't relocate. */
4163 if ((o->flags & SEC_ALLOC) == 0
4164 || (o->flags & SEC_RELOC) == 0
4165 || (o->flags & SEC_EXCLUDE) != 0
4166 || o->reloc_count == 0
4167 || ((info->strip == strip_all || info->strip == strip_debugger)
4168 && (o->flags & SEC_DEBUGGING) != 0)
4169 || bfd_is_abs_section (o->output_section))
4170 continue;
4171
4172 internal_relocs = _bfd_elf_link_info_read_relocs (abfd, info,
4173 o, NULL,
4174 NULL,
4175 _bfd_link_keep_memory (info));
4176 if (internal_relocs == NULL)
4177 return false;
4178
4179 ok = action (abfd, info, o, internal_relocs);
4180
4181 if (elf_section_data (o)->relocs != internal_relocs)
4182 free (internal_relocs);
4183
4184 if (! ok)
4185 return false;
4186 }
4187 }
4188
4189 return true;
4190 }
4191
4192 /* Check relocations in an ELF object file. This is called after
4193 all input files have been opened. */
4194
4195 bool
4196 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
4197 {
4198 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4199 if (bed->check_relocs != NULL)
4200 return _bfd_elf_link_iterate_on_relocs (abfd, info,
4201 bed->check_relocs);
4202 return true;
4203 }
4204
4205 /* Add symbols from an ELF object file to the linker hash table. */
4206
4207 static bool
4208 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
4209 {
4210 Elf_Internal_Ehdr *ehdr;
4211 Elf_Internal_Shdr *hdr;
4212 size_t symcount;
4213 size_t extsymcount;
4214 size_t extsymoff;
4215 struct elf_link_hash_entry **sym_hash;
4216 bool dynamic;
4217 Elf_External_Versym *extversym = NULL;
4218 Elf_External_Versym *extversym_end = NULL;
4219 Elf_External_Versym *ever;
4220 struct elf_link_hash_entry *weaks;
4221 struct elf_link_hash_entry **nondeflt_vers = NULL;
4222 size_t nondeflt_vers_cnt = 0;
4223 Elf_Internal_Sym *isymbuf = NULL;
4224 Elf_Internal_Sym *isym;
4225 Elf_Internal_Sym *isymend;
4226 const struct elf_backend_data *bed;
4227 bool add_needed;
4228 struct elf_link_hash_table *htab;
4229 void *alloc_mark = NULL;
4230 struct bfd_hash_entry **old_table = NULL;
4231 unsigned int old_size = 0;
4232 unsigned int old_count = 0;
4233 void *old_tab = NULL;
4234 void *old_ent;
4235 struct bfd_link_hash_entry *old_undefs = NULL;
4236 struct bfd_link_hash_entry *old_undefs_tail = NULL;
4237 void *old_strtab = NULL;
4238 size_t tabsize = 0;
4239 asection *s;
4240 bool just_syms;
4241
4242 htab = elf_hash_table (info);
4243 bed = get_elf_backend_data (abfd);
4244
4245 if ((abfd->flags & DYNAMIC) == 0)
4246 dynamic = false;
4247 else
4248 {
4249 dynamic = true;
4250
4251 /* You can't use -r against a dynamic object. Also, there's no
4252 hope of using a dynamic object which does not exactly match
4253 the format of the output file. */
4254 if (bfd_link_relocatable (info)
4255 || !is_elf_hash_table (&htab->root)
4256 || info->output_bfd->xvec != abfd->xvec)
4257 {
4258 if (bfd_link_relocatable (info))
4259 bfd_set_error (bfd_error_invalid_operation);
4260 else
4261 bfd_set_error (bfd_error_wrong_format);
4262 goto error_return;
4263 }
4264 }
4265
4266 ehdr = elf_elfheader (abfd);
4267 if (info->warn_alternate_em
4268 && bed->elf_machine_code != ehdr->e_machine
4269 && ((bed->elf_machine_alt1 != 0
4270 && ehdr->e_machine == bed->elf_machine_alt1)
4271 || (bed->elf_machine_alt2 != 0
4272 && ehdr->e_machine == bed->elf_machine_alt2)))
4273 _bfd_error_handler
4274 /* xgettext:c-format */
4275 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
4276 ehdr->e_machine, abfd, bed->elf_machine_code);
4277
4278 /* As a GNU extension, any input sections which are named
4279 .gnu.warning.SYMBOL are treated as warning symbols for the given
4280 symbol. This differs from .gnu.warning sections, which generate
4281 warnings when they are included in an output file. */
4282 /* PR 12761: Also generate this warning when building shared libraries. */
4283 for (s = abfd->sections; s != NULL; s = s->next)
4284 {
4285 const char *name;
4286
4287 name = bfd_section_name (s);
4288 if (startswith (name, ".gnu.warning."))
4289 {
4290 char *msg;
4291 bfd_size_type sz;
4292
4293 name += sizeof ".gnu.warning." - 1;
4294
4295 /* If this is a shared object, then look up the symbol
4296 in the hash table. If it is there, and it is already
4297 been defined, then we will not be using the entry
4298 from this shared object, so we don't need to warn.
4299 FIXME: If we see the definition in a regular object
4300 later on, we will warn, but we shouldn't. The only
4301 fix is to keep track of what warnings we are supposed
4302 to emit, and then handle them all at the end of the
4303 link. */
4304 if (dynamic)
4305 {
4306 struct elf_link_hash_entry *h;
4307
4308 h = elf_link_hash_lookup (htab, name, false, false, true);
4309
4310 /* FIXME: What about bfd_link_hash_common? */
4311 if (h != NULL
4312 && (h->root.type == bfd_link_hash_defined
4313 || h->root.type == bfd_link_hash_defweak))
4314 continue;
4315 }
4316
4317 sz = s->size;
4318 msg = (char *) bfd_alloc (abfd, sz + 1);
4319 if (msg == NULL)
4320 goto error_return;
4321
4322 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
4323 goto error_return;
4324
4325 msg[sz] = '\0';
4326
4327 if (! (_bfd_generic_link_add_one_symbol
4328 (info, abfd, name, BSF_WARNING, s, 0, msg,
4329 false, bed->collect, NULL)))
4330 goto error_return;
4331
4332 if (bfd_link_executable (info))
4333 {
4334 /* Clobber the section size so that the warning does
4335 not get copied into the output file. */
4336 s->size = 0;
4337
4338 /* Also set SEC_EXCLUDE, so that symbols defined in
4339 the warning section don't get copied to the output. */
4340 s->flags |= SEC_EXCLUDE;
4341 }
4342 }
4343 }
4344
4345 just_syms = ((s = abfd->sections) != NULL
4346 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
4347
4348 add_needed = true;
4349 if (! dynamic)
4350 {
4351 /* If we are creating a shared library, create all the dynamic
4352 sections immediately. We need to attach them to something,
4353 so we attach them to this BFD, provided it is the right
4354 format and is not from ld --just-symbols. Always create the
4355 dynamic sections for -E/--dynamic-list. FIXME: If there
4356 are no input BFD's of the same format as the output, we can't
4357 make a shared library. */
4358 if (!just_syms
4359 && (bfd_link_pic (info)
4360 || (!bfd_link_relocatable (info)
4361 && info->nointerp
4362 && (info->export_dynamic || info->dynamic)))
4363 && is_elf_hash_table (&htab->root)
4364 && info->output_bfd->xvec == abfd->xvec
4365 && !htab->dynamic_sections_created)
4366 {
4367 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4368 goto error_return;
4369 }
4370 }
4371 else if (!is_elf_hash_table (&htab->root))
4372 goto error_return;
4373 else
4374 {
4375 const char *soname = NULL;
4376 char *audit = NULL;
4377 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
4378 const Elf_Internal_Phdr *phdr;
4379 struct elf_link_loaded_list *loaded_lib;
4380
4381 /* ld --just-symbols and dynamic objects don't mix very well.
4382 ld shouldn't allow it. */
4383 if (just_syms)
4384 abort ();
4385
4386 /* If this dynamic lib was specified on the command line with
4387 --as-needed in effect, then we don't want to add a DT_NEEDED
4388 tag unless the lib is actually used. Similary for libs brought
4389 in by another lib's DT_NEEDED. When --no-add-needed is used
4390 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4391 any dynamic library in DT_NEEDED tags in the dynamic lib at
4392 all. */
4393 add_needed = (elf_dyn_lib_class (abfd)
4394 & (DYN_AS_NEEDED | DYN_DT_NEEDED
4395 | DYN_NO_NEEDED)) == 0;
4396
4397 s = bfd_get_section_by_name (abfd, ".dynamic");
4398 if (s != NULL && s->size != 0)
4399 {
4400 bfd_byte *dynbuf;
4401 bfd_byte *extdyn;
4402 unsigned int elfsec;
4403 unsigned long shlink;
4404
4405 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
4406 {
4407 error_free_dyn:
4408 free (dynbuf);
4409 goto error_return;
4410 }
4411
4412 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
4413 if (elfsec == SHN_BAD)
4414 goto error_free_dyn;
4415 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4416
4417 for (extdyn = dynbuf;
4418 extdyn <= dynbuf + s->size - bed->s->sizeof_dyn;
4419 extdyn += bed->s->sizeof_dyn)
4420 {
4421 Elf_Internal_Dyn dyn;
4422
4423 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4424 if (dyn.d_tag == DT_SONAME)
4425 {
4426 unsigned int tagv = dyn.d_un.d_val;
4427 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4428 if (soname == NULL)
4429 goto error_free_dyn;
4430 }
4431 if (dyn.d_tag == DT_NEEDED)
4432 {
4433 struct bfd_link_needed_list *n, **pn;
4434 char *fnm, *anm;
4435 unsigned int tagv = dyn.d_un.d_val;
4436 size_t amt = sizeof (struct bfd_link_needed_list);
4437
4438 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4439 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4440 if (n == NULL || fnm == NULL)
4441 goto error_free_dyn;
4442 amt = strlen (fnm) + 1;
4443 anm = (char *) bfd_alloc (abfd, amt);
4444 if (anm == NULL)
4445 goto error_free_dyn;
4446 memcpy (anm, fnm, amt);
4447 n->name = anm;
4448 n->by = abfd;
4449 n->next = NULL;
4450 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4451 ;
4452 *pn = n;
4453 }
4454 if (dyn.d_tag == DT_RUNPATH)
4455 {
4456 struct bfd_link_needed_list *n, **pn;
4457 char *fnm, *anm;
4458 unsigned int tagv = dyn.d_un.d_val;
4459 size_t amt = sizeof (struct bfd_link_needed_list);
4460
4461 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4462 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4463 if (n == NULL || fnm == NULL)
4464 goto error_free_dyn;
4465 amt = strlen (fnm) + 1;
4466 anm = (char *) bfd_alloc (abfd, amt);
4467 if (anm == NULL)
4468 goto error_free_dyn;
4469 memcpy (anm, fnm, amt);
4470 n->name = anm;
4471 n->by = abfd;
4472 n->next = NULL;
4473 for (pn = & runpath;
4474 *pn != NULL;
4475 pn = &(*pn)->next)
4476 ;
4477 *pn = n;
4478 }
4479 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4480 if (!runpath && dyn.d_tag == DT_RPATH)
4481 {
4482 struct bfd_link_needed_list *n, **pn;
4483 char *fnm, *anm;
4484 unsigned int tagv = dyn.d_un.d_val;
4485 size_t amt = sizeof (struct bfd_link_needed_list);
4486
4487 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4488 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4489 if (n == NULL || fnm == NULL)
4490 goto error_free_dyn;
4491 amt = strlen (fnm) + 1;
4492 anm = (char *) bfd_alloc (abfd, amt);
4493 if (anm == NULL)
4494 goto error_free_dyn;
4495 memcpy (anm, fnm, amt);
4496 n->name = anm;
4497 n->by = abfd;
4498 n->next = NULL;
4499 for (pn = & rpath;
4500 *pn != NULL;
4501 pn = &(*pn)->next)
4502 ;
4503 *pn = n;
4504 }
4505 if (dyn.d_tag == DT_AUDIT)
4506 {
4507 unsigned int tagv = dyn.d_un.d_val;
4508 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4509 }
4510 if (dyn.d_tag == DT_FLAGS_1)
4511 elf_tdata (abfd)->is_pie = (dyn.d_un.d_val & DF_1_PIE) != 0;
4512 }
4513
4514 free (dynbuf);
4515 }
4516
4517 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4518 frees all more recently bfd_alloc'd blocks as well. */
4519 if (runpath)
4520 rpath = runpath;
4521
4522 if (rpath)
4523 {
4524 struct bfd_link_needed_list **pn;
4525 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4526 ;
4527 *pn = rpath;
4528 }
4529
4530 /* If we have a PT_GNU_RELRO program header, mark as read-only
4531 all sections contained fully therein. This makes relro
4532 shared library sections appear as they will at run-time. */
4533 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4534 while (phdr-- > elf_tdata (abfd)->phdr)
4535 if (phdr->p_type == PT_GNU_RELRO)
4536 {
4537 for (s = abfd->sections; s != NULL; s = s->next)
4538 {
4539 unsigned int opb = bfd_octets_per_byte (abfd, s);
4540
4541 if ((s->flags & SEC_ALLOC) != 0
4542 && s->vma * opb >= phdr->p_vaddr
4543 && s->vma * opb + s->size <= phdr->p_vaddr + phdr->p_memsz)
4544 s->flags |= SEC_READONLY;
4545 }
4546 break;
4547 }
4548
4549 /* We do not want to include any of the sections in a dynamic
4550 object in the output file. We hack by simply clobbering the
4551 list of sections in the BFD. This could be handled more
4552 cleanly by, say, a new section flag; the existing
4553 SEC_NEVER_LOAD flag is not the one we want, because that one
4554 still implies that the section takes up space in the output
4555 file. */
4556 bfd_section_list_clear (abfd);
4557
4558 /* Find the name to use in a DT_NEEDED entry that refers to this
4559 object. If the object has a DT_SONAME entry, we use it.
4560 Otherwise, if the generic linker stuck something in
4561 elf_dt_name, we use that. Otherwise, we just use the file
4562 name. */
4563 if (soname == NULL || *soname == '\0')
4564 {
4565 soname = elf_dt_name (abfd);
4566 if (soname == NULL || *soname == '\0')
4567 soname = bfd_get_filename (abfd);
4568 }
4569
4570 /* Save the SONAME because sometimes the linker emulation code
4571 will need to know it. */
4572 elf_dt_name (abfd) = soname;
4573
4574 /* If we have already included this dynamic object in the
4575 link, just ignore it. There is no reason to include a
4576 particular dynamic object more than once. */
4577 for (loaded_lib = htab->dyn_loaded;
4578 loaded_lib != NULL;
4579 loaded_lib = loaded_lib->next)
4580 {
4581 if (strcmp (elf_dt_name (loaded_lib->abfd), soname) == 0)
4582 return true;
4583 }
4584
4585 /* Create dynamic sections for backends that require that be done
4586 before setup_gnu_properties. */
4587 if (add_needed
4588 && !_bfd_elf_link_create_dynamic_sections (abfd, info))
4589 return false;
4590
4591 /* Save the DT_AUDIT entry for the linker emulation code. */
4592 elf_dt_audit (abfd) = audit;
4593 }
4594
4595 /* If this is a dynamic object, we always link against the .dynsym
4596 symbol table, not the .symtab symbol table. The dynamic linker
4597 will only see the .dynsym symbol table, so there is no reason to
4598 look at .symtab for a dynamic object. */
4599
4600 if (! dynamic || elf_dynsymtab (abfd) == 0)
4601 hdr = &elf_tdata (abfd)->symtab_hdr;
4602 else
4603 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4604
4605 symcount = hdr->sh_size / bed->s->sizeof_sym;
4606
4607 /* The sh_info field of the symtab header tells us where the
4608 external symbols start. We don't care about the local symbols at
4609 this point. */
4610 if (elf_bad_symtab (abfd))
4611 {
4612 extsymcount = symcount;
4613 extsymoff = 0;
4614 }
4615 else
4616 {
4617 extsymcount = symcount - hdr->sh_info;
4618 extsymoff = hdr->sh_info;
4619 }
4620
4621 sym_hash = elf_sym_hashes (abfd);
4622 if (extsymcount != 0)
4623 {
4624 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4625 NULL, NULL, NULL);
4626 if (isymbuf == NULL)
4627 goto error_return;
4628
4629 if (sym_hash == NULL)
4630 {
4631 /* We store a pointer to the hash table entry for each
4632 external symbol. */
4633 size_t amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4634 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4635 if (sym_hash == NULL)
4636 goto error_free_sym;
4637 elf_sym_hashes (abfd) = sym_hash;
4638 }
4639 }
4640
4641 if (dynamic)
4642 {
4643 /* Read in any version definitions. */
4644 if (!_bfd_elf_slurp_version_tables (abfd,
4645 info->default_imported_symver))
4646 goto error_free_sym;
4647
4648 /* Read in the symbol versions, but don't bother to convert them
4649 to internal format. */
4650 if (elf_dynversym (abfd) != 0)
4651 {
4652 Elf_Internal_Shdr *versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4653 bfd_size_type amt = versymhdr->sh_size;
4654
4655 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0)
4656 goto error_free_sym;
4657 extversym = (Elf_External_Versym *)
4658 _bfd_malloc_and_read (abfd, amt, amt);
4659 if (extversym == NULL)
4660 goto error_free_sym;
4661 extversym_end = extversym + amt / sizeof (*extversym);
4662 }
4663 }
4664
4665 /* If we are loading an as-needed shared lib, save the symbol table
4666 state before we start adding symbols. If the lib turns out
4667 to be unneeded, restore the state. */
4668 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4669 {
4670 unsigned int i;
4671 size_t entsize;
4672
4673 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4674 {
4675 struct bfd_hash_entry *p;
4676 struct elf_link_hash_entry *h;
4677
4678 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4679 {
4680 h = (struct elf_link_hash_entry *) p;
4681 entsize += htab->root.table.entsize;
4682 if (h->root.type == bfd_link_hash_warning)
4683 {
4684 entsize += htab->root.table.entsize;
4685 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4686 }
4687 if (h->root.type == bfd_link_hash_common)
4688 entsize += sizeof (*h->root.u.c.p);
4689 }
4690 }
4691
4692 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4693 old_tab = bfd_malloc (tabsize + entsize);
4694 if (old_tab == NULL)
4695 goto error_free_vers;
4696
4697 /* Remember the current objalloc pointer, so that all mem for
4698 symbols added can later be reclaimed. */
4699 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4700 if (alloc_mark == NULL)
4701 goto error_free_vers;
4702
4703 /* Make a special call to the linker "notice" function to
4704 tell it that we are about to handle an as-needed lib. */
4705 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4706 goto error_free_vers;
4707
4708 /* Clone the symbol table. Remember some pointers into the
4709 symbol table, and dynamic symbol count. */
4710 old_ent = (char *) old_tab + tabsize;
4711 memcpy (old_tab, htab->root.table.table, tabsize);
4712 old_undefs = htab->root.undefs;
4713 old_undefs_tail = htab->root.undefs_tail;
4714 old_table = htab->root.table.table;
4715 old_size = htab->root.table.size;
4716 old_count = htab->root.table.count;
4717 old_strtab = NULL;
4718 if (htab->dynstr != NULL)
4719 {
4720 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4721 if (old_strtab == NULL)
4722 goto error_free_vers;
4723 }
4724
4725 for (i = 0; i < htab->root.table.size; i++)
4726 {
4727 struct bfd_hash_entry *p;
4728 struct elf_link_hash_entry *h;
4729
4730 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4731 {
4732 h = (struct elf_link_hash_entry *) p;
4733 memcpy (old_ent, h, htab->root.table.entsize);
4734 old_ent = (char *) old_ent + htab->root.table.entsize;
4735 if (h->root.type == bfd_link_hash_warning)
4736 {
4737 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4738 memcpy (old_ent, h, htab->root.table.entsize);
4739 old_ent = (char *) old_ent + htab->root.table.entsize;
4740 }
4741 if (h->root.type == bfd_link_hash_common)
4742 {
4743 memcpy (old_ent, h->root.u.c.p, sizeof (*h->root.u.c.p));
4744 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
4745 }
4746 }
4747 }
4748 }
4749
4750 weaks = NULL;
4751 if (extversym == NULL)
4752 ever = NULL;
4753 else if (extversym + extsymoff < extversym_end)
4754 ever = extversym + extsymoff;
4755 else
4756 {
4757 /* xgettext:c-format */
4758 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4759 abfd, (long) extsymoff,
4760 (long) (extversym_end - extversym) / sizeof (* extversym));
4761 bfd_set_error (bfd_error_bad_value);
4762 goto error_free_vers;
4763 }
4764
4765 if (!bfd_link_relocatable (info)
4766 && abfd->lto_slim_object)
4767 {
4768 _bfd_error_handler
4769 (_("%pB: plugin needed to handle lto object"), abfd);
4770 }
4771
4772 for (isym = isymbuf, isymend = PTR_ADD (isymbuf, extsymcount);
4773 isym < isymend;
4774 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4775 {
4776 int bind;
4777 bfd_vma value;
4778 asection *sec, *new_sec;
4779 flagword flags;
4780 const char *name;
4781 struct elf_link_hash_entry *h;
4782 struct elf_link_hash_entry *hi;
4783 bool definition;
4784 bool size_change_ok;
4785 bool type_change_ok;
4786 bool new_weak;
4787 bool old_weak;
4788 bfd *override;
4789 bool common;
4790 bool discarded;
4791 unsigned int old_alignment;
4792 unsigned int shindex;
4793 bfd *old_bfd;
4794 bool matched;
4795
4796 override = NULL;
4797
4798 flags = BSF_NO_FLAGS;
4799 sec = NULL;
4800 value = isym->st_value;
4801 common = bed->common_definition (isym);
4802 if (common && info->inhibit_common_definition)
4803 {
4804 /* Treat common symbol as undefined for --no-define-common. */
4805 isym->st_shndx = SHN_UNDEF;
4806 common = false;
4807 }
4808 discarded = false;
4809
4810 bind = ELF_ST_BIND (isym->st_info);
4811 switch (bind)
4812 {
4813 case STB_LOCAL:
4814 /* This should be impossible, since ELF requires that all
4815 global symbols follow all local symbols, and that sh_info
4816 point to the first global symbol. Unfortunately, Irix 5
4817 screws this up. */
4818 if (elf_bad_symtab (abfd))
4819 continue;
4820
4821 /* If we aren't prepared to handle locals within the globals
4822 then we'll likely segfault on a NULL symbol hash if the
4823 symbol is ever referenced in relocations. */
4824 shindex = elf_elfheader (abfd)->e_shstrndx;
4825 name = bfd_elf_string_from_elf_section (abfd, shindex, hdr->sh_name);
4826 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4827 " (>= sh_info of %lu)"),
4828 abfd, name, (long) (isym - isymbuf + extsymoff),
4829 (long) extsymoff);
4830
4831 /* Dynamic object relocations are not processed by ld, so
4832 ld won't run into the problem mentioned above. */
4833 if (dynamic)
4834 continue;
4835 bfd_set_error (bfd_error_bad_value);
4836 goto error_free_vers;
4837
4838 case STB_GLOBAL:
4839 if (isym->st_shndx != SHN_UNDEF && !common)
4840 flags = BSF_GLOBAL;
4841 break;
4842
4843 case STB_WEAK:
4844 flags = BSF_WEAK;
4845 break;
4846
4847 case STB_GNU_UNIQUE:
4848 flags = BSF_GNU_UNIQUE;
4849 break;
4850
4851 default:
4852 /* Leave it up to the processor backend. */
4853 break;
4854 }
4855
4856 if (isym->st_shndx == SHN_UNDEF)
4857 sec = bfd_und_section_ptr;
4858 else if (isym->st_shndx == SHN_ABS)
4859 sec = bfd_abs_section_ptr;
4860 else if (isym->st_shndx == SHN_COMMON)
4861 {
4862 sec = bfd_com_section_ptr;
4863 /* What ELF calls the size we call the value. What ELF
4864 calls the value we call the alignment. */
4865 value = isym->st_size;
4866 }
4867 else
4868 {
4869 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4870 if (sec == NULL)
4871 sec = bfd_abs_section_ptr;
4872 else if (discarded_section (sec))
4873 {
4874 /* Symbols from discarded section are undefined. We keep
4875 its visibility. */
4876 sec = bfd_und_section_ptr;
4877 discarded = true;
4878 isym->st_shndx = SHN_UNDEF;
4879 }
4880 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4881 value -= sec->vma;
4882 }
4883
4884 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4885 isym->st_name);
4886 if (name == NULL)
4887 goto error_free_vers;
4888
4889 if (isym->st_shndx == SHN_COMMON
4890 && (abfd->flags & BFD_PLUGIN) != 0)
4891 {
4892 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4893
4894 if (xc == NULL)
4895 {
4896 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4897 | SEC_EXCLUDE);
4898 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4899 if (xc == NULL)
4900 goto error_free_vers;
4901 }
4902 sec = xc;
4903 }
4904 else if (isym->st_shndx == SHN_COMMON
4905 && ELF_ST_TYPE (isym->st_info) == STT_TLS
4906 && !bfd_link_relocatable (info))
4907 {
4908 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4909
4910 if (tcomm == NULL)
4911 {
4912 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4913 | SEC_LINKER_CREATED);
4914 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4915 if (tcomm == NULL)
4916 goto error_free_vers;
4917 }
4918 sec = tcomm;
4919 }
4920 else if (bed->elf_add_symbol_hook)
4921 {
4922 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4923 &sec, &value))
4924 goto error_free_vers;
4925
4926 /* The hook function sets the name to NULL if this symbol
4927 should be skipped for some reason. */
4928 if (name == NULL)
4929 continue;
4930 }
4931
4932 /* Sanity check that all possibilities were handled. */
4933 if (sec == NULL)
4934 abort ();
4935
4936 /* Silently discard TLS symbols from --just-syms. There's
4937 no way to combine a static TLS block with a new TLS block
4938 for this executable. */
4939 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4940 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4941 continue;
4942
4943 if (bfd_is_und_section (sec)
4944 || bfd_is_com_section (sec))
4945 definition = false;
4946 else
4947 definition = true;
4948
4949 size_change_ok = false;
4950 type_change_ok = bed->type_change_ok;
4951 old_weak = false;
4952 matched = false;
4953 old_alignment = 0;
4954 old_bfd = NULL;
4955 new_sec = sec;
4956
4957 if (is_elf_hash_table (&htab->root))
4958 {
4959 Elf_Internal_Versym iver;
4960 unsigned int vernum = 0;
4961 bool skip;
4962
4963 if (ever == NULL)
4964 {
4965 if (info->default_imported_symver)
4966 /* Use the default symbol version created earlier. */
4967 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4968 else
4969 iver.vs_vers = 0;
4970 }
4971 else if (ever >= extversym_end)
4972 {
4973 /* xgettext:c-format */
4974 _bfd_error_handler (_("%pB: not enough version information"),
4975 abfd);
4976 bfd_set_error (bfd_error_bad_value);
4977 goto error_free_vers;
4978 }
4979 else
4980 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4981
4982 vernum = iver.vs_vers & VERSYM_VERSION;
4983
4984 /* If this is a hidden symbol, or if it is not version
4985 1, we append the version name to the symbol name.
4986 However, we do not modify a non-hidden absolute symbol
4987 if it is not a function, because it might be the version
4988 symbol itself. FIXME: What if it isn't? */
4989 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4990 || (vernum > 1
4991 && (!bfd_is_abs_section (sec)
4992 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4993 {
4994 const char *verstr;
4995 size_t namelen, verlen, newlen;
4996 char *newname, *p;
4997
4998 if (isym->st_shndx != SHN_UNDEF)
4999 {
5000 if (vernum > elf_tdata (abfd)->cverdefs)
5001 verstr = NULL;
5002 else if (vernum > 1)
5003 verstr =
5004 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
5005 else
5006 verstr = "";
5007
5008 if (verstr == NULL)
5009 {
5010 _bfd_error_handler
5011 /* xgettext:c-format */
5012 (_("%pB: %s: invalid version %u (max %d)"),
5013 abfd, name, vernum,
5014 elf_tdata (abfd)->cverdefs);
5015 bfd_set_error (bfd_error_bad_value);
5016 goto error_free_vers;
5017 }
5018 }
5019 else
5020 {
5021 /* We cannot simply test for the number of
5022 entries in the VERNEED section since the
5023 numbers for the needed versions do not start
5024 at 0. */
5025 Elf_Internal_Verneed *t;
5026
5027 verstr = NULL;
5028 for (t = elf_tdata (abfd)->verref;
5029 t != NULL;
5030 t = t->vn_nextref)
5031 {
5032 Elf_Internal_Vernaux *a;
5033
5034 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5035 {
5036 if (a->vna_other == vernum)
5037 {
5038 verstr = a->vna_nodename;
5039 break;
5040 }
5041 }
5042 if (a != NULL)
5043 break;
5044 }
5045 if (verstr == NULL)
5046 {
5047 _bfd_error_handler
5048 /* xgettext:c-format */
5049 (_("%pB: %s: invalid needed version %d"),
5050 abfd, name, vernum);
5051 bfd_set_error (bfd_error_bad_value);
5052 goto error_free_vers;
5053 }
5054 }
5055
5056 namelen = strlen (name);
5057 verlen = strlen (verstr);
5058 newlen = namelen + verlen + 2;
5059 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
5060 && isym->st_shndx != SHN_UNDEF)
5061 ++newlen;
5062
5063 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
5064 if (newname == NULL)
5065 goto error_free_vers;
5066 memcpy (newname, name, namelen);
5067 p = newname + namelen;
5068 *p++ = ELF_VER_CHR;
5069 /* If this is a defined non-hidden version symbol,
5070 we add another @ to the name. This indicates the
5071 default version of the symbol. */
5072 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
5073 && isym->st_shndx != SHN_UNDEF)
5074 *p++ = ELF_VER_CHR;
5075 memcpy (p, verstr, verlen + 1);
5076
5077 name = newname;
5078 }
5079
5080 /* If this symbol has default visibility and the user has
5081 requested we not re-export it, then mark it as hidden. */
5082 if (!bfd_is_und_section (sec)
5083 && !dynamic
5084 && abfd->no_export
5085 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
5086 isym->st_other = (STV_HIDDEN
5087 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
5088
5089 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
5090 sym_hash, &old_bfd, &old_weak,
5091 &old_alignment, &skip, &override,
5092 &type_change_ok, &size_change_ok,
5093 &matched))
5094 goto error_free_vers;
5095
5096 if (skip)
5097 continue;
5098
5099 /* Override a definition only if the new symbol matches the
5100 existing one. */
5101 if (override && matched)
5102 definition = false;
5103
5104 h = *sym_hash;
5105 while (h->root.type == bfd_link_hash_indirect
5106 || h->root.type == bfd_link_hash_warning)
5107 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5108
5109 if (h->versioned != unversioned
5110 && elf_tdata (abfd)->verdef != NULL
5111 && vernum > 1
5112 && definition)
5113 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
5114 }
5115
5116 if (! (_bfd_generic_link_add_one_symbol
5117 (info, override ? override : abfd, name, flags, sec, value,
5118 NULL, false, bed->collect,
5119 (struct bfd_link_hash_entry **) sym_hash)))
5120 goto error_free_vers;
5121
5122 h = *sym_hash;
5123 /* We need to make sure that indirect symbol dynamic flags are
5124 updated. */
5125 hi = h;
5126 while (h->root.type == bfd_link_hash_indirect
5127 || h->root.type == bfd_link_hash_warning)
5128 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5129
5130 *sym_hash = h;
5131
5132 /* Setting the index to -3 tells elf_link_output_extsym that
5133 this symbol is defined in a discarded section. */
5134 if (discarded && is_elf_hash_table (&htab->root))
5135 h->indx = -3;
5136
5137 new_weak = (flags & BSF_WEAK) != 0;
5138 if (dynamic
5139 && definition
5140 && new_weak
5141 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
5142 && is_elf_hash_table (&htab->root)
5143 && h->u.alias == NULL)
5144 {
5145 /* Keep a list of all weak defined non function symbols from
5146 a dynamic object, using the alias field. Later in this
5147 function we will set the alias field to the correct
5148 value. We only put non-function symbols from dynamic
5149 objects on this list, because that happens to be the only
5150 time we need to know the normal symbol corresponding to a
5151 weak symbol, and the information is time consuming to
5152 figure out. If the alias field is not already NULL,
5153 then this symbol was already defined by some previous
5154 dynamic object, and we will be using that previous
5155 definition anyhow. */
5156
5157 h->u.alias = weaks;
5158 weaks = h;
5159 }
5160
5161 /* Set the alignment of a common symbol. */
5162 if ((common || bfd_is_com_section (sec))
5163 && h->root.type == bfd_link_hash_common)
5164 {
5165 unsigned int align;
5166
5167 if (common)
5168 align = bfd_log2 (isym->st_value);
5169 else
5170 {
5171 /* The new symbol is a common symbol in a shared object.
5172 We need to get the alignment from the section. */
5173 align = new_sec->alignment_power;
5174 }
5175 if (align > old_alignment)
5176 h->root.u.c.p->alignment_power = align;
5177 else
5178 h->root.u.c.p->alignment_power = old_alignment;
5179 }
5180
5181 if (is_elf_hash_table (&htab->root))
5182 {
5183 /* Set a flag in the hash table entry indicating the type of
5184 reference or definition we just found. A dynamic symbol
5185 is one which is referenced or defined by both a regular
5186 object and a shared object. */
5187 bool dynsym = false;
5188
5189 /* Plugin symbols aren't normal. Don't set def/ref flags. */
5190 if ((abfd->flags & BFD_PLUGIN) != 0)
5191 {
5192 /* Except for this flag to track nonweak references. */
5193 if (!definition
5194 && bind != STB_WEAK)
5195 h->ref_ir_nonweak = 1;
5196 }
5197 else if (!dynamic)
5198 {
5199 if (! definition)
5200 {
5201 h->ref_regular = 1;
5202 if (bind != STB_WEAK)
5203 h->ref_regular_nonweak = 1;
5204 }
5205 else
5206 {
5207 h->def_regular = 1;
5208 if (h->def_dynamic)
5209 {
5210 h->def_dynamic = 0;
5211 h->ref_dynamic = 1;
5212 }
5213 }
5214 }
5215 else
5216 {
5217 if (! definition)
5218 {
5219 h->ref_dynamic = 1;
5220 hi->ref_dynamic = 1;
5221 }
5222 else
5223 {
5224 h->def_dynamic = 1;
5225 hi->def_dynamic = 1;
5226 }
5227 }
5228
5229 /* If an indirect symbol has been forced local, don't
5230 make the real symbol dynamic. */
5231 if (h != hi && hi->forced_local)
5232 ;
5233 else if (!dynamic)
5234 {
5235 if (bfd_link_dll (info)
5236 || h->def_dynamic
5237 || h->ref_dynamic)
5238 dynsym = true;
5239 }
5240 else
5241 {
5242 if (h->def_regular
5243 || h->ref_regular
5244 || (h->is_weakalias
5245 && weakdef (h)->dynindx != -1))
5246 dynsym = true;
5247 }
5248
5249 /* Check to see if we need to add an indirect symbol for
5250 the default name. */
5251 if ((definition
5252 || (!override && h->root.type == bfd_link_hash_common))
5253 && !(hi != h
5254 && hi->versioned == versioned_hidden))
5255 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
5256 sec, value, &old_bfd, &dynsym))
5257 goto error_free_vers;
5258
5259 /* Check the alignment when a common symbol is involved. This
5260 can change when a common symbol is overridden by a normal
5261 definition or a common symbol is ignored due to the old
5262 normal definition. We need to make sure the maximum
5263 alignment is maintained. */
5264 if ((old_alignment || common)
5265 && h->root.type != bfd_link_hash_common)
5266 {
5267 unsigned int common_align;
5268 unsigned int normal_align;
5269 unsigned int symbol_align;
5270 bfd *normal_bfd;
5271 bfd *common_bfd;
5272
5273 BFD_ASSERT (h->root.type == bfd_link_hash_defined
5274 || h->root.type == bfd_link_hash_defweak);
5275
5276 symbol_align = ffs (h->root.u.def.value) - 1;
5277 if (h->root.u.def.section->owner != NULL
5278 && (h->root.u.def.section->owner->flags
5279 & (DYNAMIC | BFD_PLUGIN)) == 0)
5280 {
5281 normal_align = h->root.u.def.section->alignment_power;
5282 if (normal_align > symbol_align)
5283 normal_align = symbol_align;
5284 }
5285 else
5286 normal_align = symbol_align;
5287
5288 if (old_alignment)
5289 {
5290 common_align = old_alignment;
5291 common_bfd = old_bfd;
5292 normal_bfd = abfd;
5293 }
5294 else
5295 {
5296 common_align = bfd_log2 (isym->st_value);
5297 common_bfd = abfd;
5298 normal_bfd = old_bfd;
5299 }
5300
5301 if (normal_align < common_align)
5302 {
5303 /* PR binutils/2735 */
5304 if (normal_bfd == NULL)
5305 _bfd_error_handler
5306 /* xgettext:c-format */
5307 (_("warning: alignment %u of common symbol `%s' in %pB is"
5308 " greater than the alignment (%u) of its section %pA"),
5309 1 << common_align, name, common_bfd,
5310 1 << normal_align, h->root.u.def.section);
5311 else
5312 _bfd_error_handler
5313 /* xgettext:c-format */
5314 (_("warning: alignment %u of symbol `%s' in %pB"
5315 " is smaller than %u in %pB"),
5316 1 << normal_align, name, normal_bfd,
5317 1 << common_align, common_bfd);
5318 }
5319 }
5320
5321 /* Remember the symbol size if it isn't undefined. */
5322 if (isym->st_size != 0
5323 && isym->st_shndx != SHN_UNDEF
5324 && (definition || h->size == 0))
5325 {
5326 if (h->size != 0
5327 && h->size != isym->st_size
5328 && ! size_change_ok)
5329 _bfd_error_handler
5330 /* xgettext:c-format */
5331 (_("warning: size of symbol `%s' changed"
5332 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
5333 name, (uint64_t) h->size, old_bfd,
5334 (uint64_t) isym->st_size, abfd);
5335
5336 h->size = isym->st_size;
5337 }
5338
5339 /* If this is a common symbol, then we always want H->SIZE
5340 to be the size of the common symbol. The code just above
5341 won't fix the size if a common symbol becomes larger. We
5342 don't warn about a size change here, because that is
5343 covered by --warn-common. Allow changes between different
5344 function types. */
5345 if (h->root.type == bfd_link_hash_common)
5346 h->size = h->root.u.c.size;
5347
5348 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
5349 && ((definition && !new_weak)
5350 || (old_weak && h->root.type == bfd_link_hash_common)
5351 || h->type == STT_NOTYPE))
5352 {
5353 unsigned int type = ELF_ST_TYPE (isym->st_info);
5354
5355 /* Turn an IFUNC symbol from a DSO into a normal FUNC
5356 symbol. */
5357 if (type == STT_GNU_IFUNC
5358 && (abfd->flags & DYNAMIC) != 0)
5359 type = STT_FUNC;
5360
5361 if (h->type != type)
5362 {
5363 if (h->type != STT_NOTYPE && ! type_change_ok)
5364 /* xgettext:c-format */
5365 _bfd_error_handler
5366 (_("warning: type of symbol `%s' changed"
5367 " from %d to %d in %pB"),
5368 name, h->type, type, abfd);
5369
5370 h->type = type;
5371 }
5372 }
5373
5374 /* Merge st_other field. */
5375 elf_merge_st_other (abfd, h, isym->st_other, sec,
5376 definition, dynamic);
5377
5378 /* We don't want to make debug symbol dynamic. */
5379 if (definition
5380 && (sec->flags & SEC_DEBUGGING)
5381 && !bfd_link_relocatable (info))
5382 dynsym = false;
5383
5384 /* Nor should we make plugin symbols dynamic. */
5385 if ((abfd->flags & BFD_PLUGIN) != 0)
5386 dynsym = false;
5387
5388 if (definition)
5389 {
5390 h->target_internal = isym->st_target_internal;
5391 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5392 }
5393
5394 if (definition && !dynamic)
5395 {
5396 char *p = strchr (name, ELF_VER_CHR);
5397 if (p != NULL && p[1] != ELF_VER_CHR)
5398 {
5399 /* Queue non-default versions so that .symver x, x@FOO
5400 aliases can be checked. */
5401 if (!nondeflt_vers)
5402 {
5403 size_t amt = ((isymend - isym + 1)
5404 * sizeof (struct elf_link_hash_entry *));
5405 nondeflt_vers
5406 = (struct elf_link_hash_entry **) bfd_malloc (amt);
5407 if (!nondeflt_vers)
5408 goto error_free_vers;
5409 }
5410 nondeflt_vers[nondeflt_vers_cnt++] = h;
5411 }
5412 }
5413
5414 if (dynsym && h->dynindx == -1)
5415 {
5416 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5417 goto error_free_vers;
5418 if (h->is_weakalias
5419 && weakdef (h)->dynindx == -1)
5420 {
5421 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
5422 goto error_free_vers;
5423 }
5424 }
5425 else if (h->dynindx != -1)
5426 /* If the symbol already has a dynamic index, but
5427 visibility says it should not be visible, turn it into
5428 a local symbol. */
5429 switch (ELF_ST_VISIBILITY (h->other))
5430 {
5431 case STV_INTERNAL:
5432 case STV_HIDDEN:
5433 (*bed->elf_backend_hide_symbol) (info, h, true);
5434 dynsym = false;
5435 break;
5436 }
5437
5438 if (!add_needed
5439 && matched
5440 && definition
5441 && h->root.type != bfd_link_hash_indirect
5442 && ((dynsym
5443 && h->ref_regular_nonweak)
5444 || (old_bfd != NULL
5445 && (old_bfd->flags & BFD_PLUGIN) != 0
5446 && h->ref_ir_nonweak
5447 && !info->lto_all_symbols_read)
5448 || (h->ref_dynamic_nonweak
5449 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5450 && !on_needed_list (elf_dt_name (abfd),
5451 htab->needed, NULL))))
5452 {
5453 const char *soname = elf_dt_name (abfd);
5454
5455 info->callbacks->minfo ("%!", soname, old_bfd,
5456 h->root.root.string);
5457
5458 /* A symbol from a library loaded via DT_NEEDED of some
5459 other library is referenced by a regular object.
5460 Add a DT_NEEDED entry for it. Issue an error if
5461 --no-add-needed is used and the reference was not
5462 a weak one. */
5463 if (old_bfd != NULL
5464 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
5465 {
5466 _bfd_error_handler
5467 /* xgettext:c-format */
5468 (_("%pB: undefined reference to symbol '%s'"),
5469 old_bfd, name);
5470 bfd_set_error (bfd_error_missing_dso);
5471 goto error_free_vers;
5472 }
5473
5474 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
5475 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
5476
5477 /* Create dynamic sections for backends that require
5478 that be done before setup_gnu_properties. */
5479 if (!_bfd_elf_link_create_dynamic_sections (abfd, info))
5480 return false;
5481 add_needed = true;
5482 }
5483 }
5484 }
5485
5486 if (info->lto_plugin_active
5487 && !bfd_link_relocatable (info)
5488 && (abfd->flags & BFD_PLUGIN) == 0
5489 && !just_syms
5490 && extsymcount)
5491 {
5492 int r_sym_shift;
5493
5494 if (bed->s->arch_size == 32)
5495 r_sym_shift = 8;
5496 else
5497 r_sym_shift = 32;
5498
5499 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5500 referenced in regular objects so that linker plugin will get
5501 the correct symbol resolution. */
5502
5503 sym_hash = elf_sym_hashes (abfd);
5504 for (s = abfd->sections; s != NULL; s = s->next)
5505 {
5506 Elf_Internal_Rela *internal_relocs;
5507 Elf_Internal_Rela *rel, *relend;
5508
5509 /* Don't check relocations in excluded sections. */
5510 if ((s->flags & SEC_RELOC) == 0
5511 || s->reloc_count == 0
5512 || (s->flags & SEC_EXCLUDE) != 0
5513 || ((info->strip == strip_all
5514 || info->strip == strip_debugger)
5515 && (s->flags & SEC_DEBUGGING) != 0))
5516 continue;
5517
5518 internal_relocs = _bfd_elf_link_info_read_relocs (abfd, info,
5519 s, NULL,
5520 NULL,
5521 _bfd_link_keep_memory (info));
5522 if (internal_relocs == NULL)
5523 goto error_free_vers;
5524
5525 rel = internal_relocs;
5526 relend = rel + s->reloc_count;
5527 for ( ; rel < relend; rel++)
5528 {
5529 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5530 struct elf_link_hash_entry *h;
5531
5532 /* Skip local symbols. */
5533 if (r_symndx < extsymoff)
5534 continue;
5535
5536 h = sym_hash[r_symndx - extsymoff];
5537 if (h != NULL)
5538 h->root.non_ir_ref_regular = 1;
5539 }
5540
5541 if (elf_section_data (s)->relocs != internal_relocs)
5542 free (internal_relocs);
5543 }
5544 }
5545
5546 free (extversym);
5547 extversym = NULL;
5548 free (isymbuf);
5549 isymbuf = NULL;
5550
5551 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5552 {
5553 unsigned int i;
5554
5555 /* Restore the symbol table. */
5556 old_ent = (char *) old_tab + tabsize;
5557 memset (elf_sym_hashes (abfd), 0,
5558 extsymcount * sizeof (struct elf_link_hash_entry *));
5559 htab->root.table.table = old_table;
5560 htab->root.table.size = old_size;
5561 htab->root.table.count = old_count;
5562 memcpy (htab->root.table.table, old_tab, tabsize);
5563 htab->root.undefs = old_undefs;
5564 htab->root.undefs_tail = old_undefs_tail;
5565 if (htab->dynstr != NULL)
5566 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5567 free (old_strtab);
5568 old_strtab = NULL;
5569 for (i = 0; i < htab->root.table.size; i++)
5570 {
5571 struct bfd_hash_entry *p;
5572 struct elf_link_hash_entry *h;
5573 unsigned int non_ir_ref_dynamic;
5574
5575 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5576 {
5577 /* Preserve non_ir_ref_dynamic so that this symbol
5578 will be exported when the dynamic lib becomes needed
5579 in the second pass. */
5580 h = (struct elf_link_hash_entry *) p;
5581 if (h->root.type == bfd_link_hash_warning)
5582 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5583 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
5584
5585 h = (struct elf_link_hash_entry *) p;
5586 memcpy (h, old_ent, htab->root.table.entsize);
5587 old_ent = (char *) old_ent + htab->root.table.entsize;
5588 if (h->root.type == bfd_link_hash_warning)
5589 {
5590 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5591 memcpy (h, old_ent, htab->root.table.entsize);
5592 old_ent = (char *) old_ent + htab->root.table.entsize;
5593 }
5594 if (h->root.type == bfd_link_hash_common)
5595 {
5596 memcpy (h->root.u.c.p, old_ent, sizeof (*h->root.u.c.p));
5597 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
5598 }
5599 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
5600 }
5601 }
5602
5603 /* Make a special call to the linker "notice" function to
5604 tell it that symbols added for crefs may need to be removed. */
5605 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
5606 goto error_free_vers;
5607
5608 free (old_tab);
5609 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5610 alloc_mark);
5611 free (nondeflt_vers);
5612 return true;
5613 }
5614
5615 if (old_tab != NULL)
5616 {
5617 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
5618 goto error_free_vers;
5619 free (old_tab);
5620 old_tab = NULL;
5621 }
5622
5623 /* Now that all the symbols from this input file are created, if
5624 not performing a relocatable link, handle .symver foo, foo@BAR
5625 such that any relocs against foo become foo@BAR. */
5626 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
5627 {
5628 size_t cnt, symidx;
5629
5630 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5631 {
5632 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5633 char *shortname, *p;
5634 size_t amt;
5635
5636 p = strchr (h->root.root.string, ELF_VER_CHR);
5637 if (p == NULL
5638 || (h->root.type != bfd_link_hash_defined
5639 && h->root.type != bfd_link_hash_defweak))
5640 continue;
5641
5642 amt = p - h->root.root.string;
5643 shortname = (char *) bfd_malloc (amt + 1);
5644 if (!shortname)
5645 goto error_free_vers;
5646 memcpy (shortname, h->root.root.string, amt);
5647 shortname[amt] = '\0';
5648
5649 hi = (struct elf_link_hash_entry *)
5650 bfd_link_hash_lookup (&htab->root, shortname,
5651 false, false, false);
5652 if (hi != NULL
5653 && hi->root.type == h->root.type
5654 && hi->root.u.def.value == h->root.u.def.value
5655 && hi->root.u.def.section == h->root.u.def.section)
5656 {
5657 (*bed->elf_backend_hide_symbol) (info, hi, true);
5658 hi->root.type = bfd_link_hash_indirect;
5659 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
5660 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
5661 sym_hash = elf_sym_hashes (abfd);
5662 if (sym_hash)
5663 for (symidx = 0; symidx < extsymcount; ++symidx)
5664 if (sym_hash[symidx] == hi)
5665 {
5666 sym_hash[symidx] = h;
5667 break;
5668 }
5669 }
5670 free (shortname);
5671 }
5672 free (nondeflt_vers);
5673 nondeflt_vers = NULL;
5674 }
5675
5676 /* Now set the alias field correctly for all the weak defined
5677 symbols we found. The only way to do this is to search all the
5678 symbols. Since we only need the information for non functions in
5679 dynamic objects, that's the only time we actually put anything on
5680 the list WEAKS. We need this information so that if a regular
5681 object refers to a symbol defined weakly in a dynamic object, the
5682 real symbol in the dynamic object is also put in the dynamic
5683 symbols; we also must arrange for both symbols to point to the
5684 same memory location. We could handle the general case of symbol
5685 aliasing, but a general symbol alias can only be generated in
5686 assembler code, handling it correctly would be very time
5687 consuming, and other ELF linkers don't handle general aliasing
5688 either. */
5689 if (weaks != NULL)
5690 {
5691 struct elf_link_hash_entry **hpp;
5692 struct elf_link_hash_entry **hppend;
5693 struct elf_link_hash_entry **sorted_sym_hash;
5694 struct elf_link_hash_entry *h;
5695 size_t sym_count, amt;
5696
5697 /* Since we have to search the whole symbol list for each weak
5698 defined symbol, search time for N weak defined symbols will be
5699 O(N^2). Binary search will cut it down to O(NlogN). */
5700 amt = extsymcount * sizeof (*sorted_sym_hash);
5701 sorted_sym_hash = bfd_malloc (amt);
5702 if (sorted_sym_hash == NULL)
5703 goto error_return;
5704 sym_hash = sorted_sym_hash;
5705 hpp = elf_sym_hashes (abfd);
5706 hppend = hpp + extsymcount;
5707 sym_count = 0;
5708 for (; hpp < hppend; hpp++)
5709 {
5710 h = *hpp;
5711 if (h != NULL
5712 && h->root.type == bfd_link_hash_defined
5713 && !bed->is_function_type (h->type))
5714 {
5715 *sym_hash = h;
5716 sym_hash++;
5717 sym_count++;
5718 }
5719 }
5720
5721 qsort (sorted_sym_hash, sym_count, sizeof (*sorted_sym_hash),
5722 elf_sort_symbol);
5723
5724 while (weaks != NULL)
5725 {
5726 struct elf_link_hash_entry *hlook;
5727 asection *slook;
5728 bfd_vma vlook;
5729 size_t i, j, idx = 0;
5730
5731 hlook = weaks;
5732 weaks = hlook->u.alias;
5733 hlook->u.alias = NULL;
5734
5735 if (hlook->root.type != bfd_link_hash_defined
5736 && hlook->root.type != bfd_link_hash_defweak)
5737 continue;
5738
5739 slook = hlook->root.u.def.section;
5740 vlook = hlook->root.u.def.value;
5741
5742 i = 0;
5743 j = sym_count;
5744 while (i != j)
5745 {
5746 bfd_signed_vma vdiff;
5747 idx = (i + j) / 2;
5748 h = sorted_sym_hash[idx];
5749 vdiff = vlook - h->root.u.def.value;
5750 if (vdiff < 0)
5751 j = idx;
5752 else if (vdiff > 0)
5753 i = idx + 1;
5754 else
5755 {
5756 int sdiff = slook->id - h->root.u.def.section->id;
5757 if (sdiff < 0)
5758 j = idx;
5759 else if (sdiff > 0)
5760 i = idx + 1;
5761 else
5762 break;
5763 }
5764 }
5765
5766 /* We didn't find a value/section match. */
5767 if (i == j)
5768 continue;
5769
5770 /* With multiple aliases, or when the weak symbol is already
5771 strongly defined, we have multiple matching symbols and
5772 the binary search above may land on any of them. Step
5773 one past the matching symbol(s). */
5774 while (++idx != j)
5775 {
5776 h = sorted_sym_hash[idx];
5777 if (h->root.u.def.section != slook
5778 || h->root.u.def.value != vlook)
5779 break;
5780 }
5781
5782 /* Now look back over the aliases. Since we sorted by size
5783 as well as value and section, we'll choose the one with
5784 the largest size. */
5785 while (idx-- != i)
5786 {
5787 h = sorted_sym_hash[idx];
5788
5789 /* Stop if value or section doesn't match. */
5790 if (h->root.u.def.section != slook
5791 || h->root.u.def.value != vlook)
5792 break;
5793 else if (h != hlook)
5794 {
5795 struct elf_link_hash_entry *t;
5796
5797 hlook->u.alias = h;
5798 hlook->is_weakalias = 1;
5799 t = h;
5800 if (t->u.alias != NULL)
5801 while (t->u.alias != h)
5802 t = t->u.alias;
5803 t->u.alias = hlook;
5804
5805 /* If the weak definition is in the list of dynamic
5806 symbols, make sure the real definition is put
5807 there as well. */
5808 if (hlook->dynindx != -1 && h->dynindx == -1)
5809 {
5810 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5811 {
5812 err_free_sym_hash:
5813 free (sorted_sym_hash);
5814 goto error_return;
5815 }
5816 }
5817
5818 /* If the real definition is in the list of dynamic
5819 symbols, make sure the weak definition is put
5820 there as well. If we don't do this, then the
5821 dynamic loader might not merge the entries for the
5822 real definition and the weak definition. */
5823 if (h->dynindx != -1 && hlook->dynindx == -1)
5824 {
5825 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
5826 goto err_free_sym_hash;
5827 }
5828 break;
5829 }
5830 }
5831 }
5832
5833 free (sorted_sym_hash);
5834 }
5835
5836 if (bed->check_directives
5837 && !(*bed->check_directives) (abfd, info))
5838 return false;
5839
5840 /* If this is a non-traditional link, try to optimize the handling
5841 of the .stab/.stabstr sections. */
5842 if (! dynamic
5843 && ! info->traditional_format
5844 && is_elf_hash_table (&htab->root)
5845 && (info->strip != strip_all && info->strip != strip_debugger))
5846 {
5847 asection *stabstr;
5848
5849 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5850 if (stabstr != NULL)
5851 {
5852 bfd_size_type string_offset = 0;
5853 asection *stab;
5854
5855 for (stab = abfd->sections; stab; stab = stab->next)
5856 if (startswith (stab->name, ".stab")
5857 && (!stab->name[5] ||
5858 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5859 && (stab->flags & SEC_MERGE) == 0
5860 && !bfd_is_abs_section (stab->output_section))
5861 {
5862 struct bfd_elf_section_data *secdata;
5863
5864 secdata = elf_section_data (stab);
5865 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5866 stabstr, &secdata->sec_info,
5867 &string_offset))
5868 goto error_return;
5869 if (secdata->sec_info)
5870 stab->sec_info_type = SEC_INFO_TYPE_STABS;
5871 }
5872 }
5873 }
5874
5875 if (dynamic && add_needed)
5876 {
5877 /* Add this bfd to the loaded list. */
5878 struct elf_link_loaded_list *n;
5879
5880 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5881 if (n == NULL)
5882 goto error_return;
5883 n->abfd = abfd;
5884 n->next = htab->dyn_loaded;
5885 htab->dyn_loaded = n;
5886 }
5887 if (dynamic && !add_needed
5888 && (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) != 0)
5889 elf_dyn_lib_class (abfd) |= DYN_NO_NEEDED;
5890
5891 return true;
5892
5893 error_free_vers:
5894 free (old_tab);
5895 free (old_strtab);
5896 free (nondeflt_vers);
5897 free (extversym);
5898 error_free_sym:
5899 free (isymbuf);
5900 error_return:
5901 return false;
5902 }
5903
5904 /* Return the linker hash table entry of a symbol that might be
5905 satisfied by an archive symbol. Return -1 on error. */
5906
5907 struct bfd_link_hash_entry *
5908 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5909 struct bfd_link_info *info,
5910 const char *name)
5911 {
5912 struct bfd_link_hash_entry *h;
5913 char *p, *copy;
5914 size_t len, first;
5915
5916 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
5917 if (h != NULL)
5918 return h;
5919
5920 /* If this is a default version (the name contains @@), look up the
5921 symbol again with only one `@' as well as without the version.
5922 The effect is that references to the symbol with and without the
5923 version will be matched by the default symbol in the archive. */
5924
5925 p = strchr (name, ELF_VER_CHR);
5926 if (p == NULL || p[1] != ELF_VER_CHR)
5927 return h;
5928
5929 /* First check with only one `@'. */
5930 len = strlen (name);
5931 copy = (char *) bfd_alloc (abfd, len);
5932 if (copy == NULL)
5933 return (struct bfd_link_hash_entry *) -1;
5934
5935 first = p - name + 1;
5936 memcpy (copy, name, first);
5937 memcpy (copy + first, name + first + 1, len - first);
5938
5939 h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
5940 if (h == NULL)
5941 {
5942 /* We also need to check references to the symbol without the
5943 version. */
5944 copy[first - 1] = '\0';
5945 h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
5946 }
5947
5948 bfd_release (abfd, copy);
5949 return h;
5950 }
5951
5952 /* Add symbols from an ELF archive file to the linker hash table. We
5953 don't use _bfd_generic_link_add_archive_symbols because we need to
5954 handle versioned symbols.
5955
5956 Fortunately, ELF archive handling is simpler than that done by
5957 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5958 oddities. In ELF, if we find a symbol in the archive map, and the
5959 symbol is currently undefined, we know that we must pull in that
5960 object file.
5961
5962 Unfortunately, we do have to make multiple passes over the symbol
5963 table until nothing further is resolved. */
5964
5965 static bool
5966 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5967 {
5968 symindex c;
5969 unsigned char *included = NULL;
5970 carsym *symdefs;
5971 bool loop;
5972 size_t amt;
5973 const struct elf_backend_data *bed;
5974 struct bfd_link_hash_entry * (*archive_symbol_lookup)
5975 (bfd *, struct bfd_link_info *, const char *);
5976
5977 if (! bfd_has_map (abfd))
5978 {
5979 /* An empty archive is a special case. */
5980 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5981 return true;
5982 bfd_set_error (bfd_error_no_armap);
5983 return false;
5984 }
5985
5986 /* Keep track of all symbols we know to be already defined, and all
5987 files we know to be already included. This is to speed up the
5988 second and subsequent passes. */
5989 c = bfd_ardata (abfd)->symdef_count;
5990 if (c == 0)
5991 return true;
5992 amt = c * sizeof (*included);
5993 included = (unsigned char *) bfd_zmalloc (amt);
5994 if (included == NULL)
5995 return false;
5996
5997 symdefs = bfd_ardata (abfd)->symdefs;
5998 bed = get_elf_backend_data (abfd);
5999 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
6000
6001 do
6002 {
6003 file_ptr last;
6004 symindex i;
6005 carsym *symdef;
6006 carsym *symdefend;
6007
6008 loop = false;
6009 last = -1;
6010
6011 symdef = symdefs;
6012 symdefend = symdef + c;
6013 for (i = 0; symdef < symdefend; symdef++, i++)
6014 {
6015 struct bfd_link_hash_entry *h;
6016 bfd *element;
6017 struct bfd_link_hash_entry *undefs_tail;
6018 symindex mark;
6019
6020 if (included[i])
6021 continue;
6022 if (symdef->file_offset == last)
6023 {
6024 included[i] = true;
6025 continue;
6026 }
6027
6028 h = archive_symbol_lookup (abfd, info, symdef->name);
6029 if (h == (struct bfd_link_hash_entry *) -1)
6030 goto error_return;
6031
6032 if (h == NULL)
6033 continue;
6034
6035 if (h->type == bfd_link_hash_undefined)
6036 {
6037 /* If the archive element has already been loaded then one
6038 of the symbols defined by that element might have been
6039 made undefined due to being in a discarded section. */
6040 if (is_elf_hash_table (info->hash)
6041 && ((struct elf_link_hash_entry *) h)->indx == -3)
6042 continue;
6043 }
6044 else if (h->type == bfd_link_hash_common)
6045 {
6046 /* We currently have a common symbol. The archive map contains
6047 a reference to this symbol, so we may want to include it. We
6048 only want to include it however, if this archive element
6049 contains a definition of the symbol, not just another common
6050 declaration of it.
6051
6052 Unfortunately some archivers (including GNU ar) will put
6053 declarations of common symbols into their archive maps, as
6054 well as real definitions, so we cannot just go by the archive
6055 map alone. Instead we must read in the element's symbol
6056 table and check that to see what kind of symbol definition
6057 this is. */
6058 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
6059 continue;
6060 }
6061 else
6062 {
6063 if (h->type != bfd_link_hash_undefweak)
6064 /* Symbol must be defined. Don't check it again. */
6065 included[i] = true;
6066 continue;
6067 }
6068
6069 /* We need to include this archive member. */
6070 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset,
6071 info);
6072 if (element == NULL)
6073 goto error_return;
6074
6075 if (! bfd_check_format (element, bfd_object))
6076 goto error_return;
6077
6078 undefs_tail = info->hash->undefs_tail;
6079
6080 if (!(*info->callbacks
6081 ->add_archive_element) (info, element, symdef->name, &element))
6082 continue;
6083 if (!bfd_link_add_symbols (element, info))
6084 goto error_return;
6085
6086 /* If there are any new undefined symbols, we need to make
6087 another pass through the archive in order to see whether
6088 they can be defined. FIXME: This isn't perfect, because
6089 common symbols wind up on undefs_tail and because an
6090 undefined symbol which is defined later on in this pass
6091 does not require another pass. This isn't a bug, but it
6092 does make the code less efficient than it could be. */
6093 if (undefs_tail != info->hash->undefs_tail)
6094 loop = true;
6095
6096 /* Look backward to mark all symbols from this object file
6097 which we have already seen in this pass. */
6098 mark = i;
6099 do
6100 {
6101 included[mark] = true;
6102 if (mark == 0)
6103 break;
6104 --mark;
6105 }
6106 while (symdefs[mark].file_offset == symdef->file_offset);
6107
6108 /* We mark subsequent symbols from this object file as we go
6109 on through the loop. */
6110 last = symdef->file_offset;
6111 }
6112 }
6113 while (loop);
6114
6115 free (included);
6116 return true;
6117
6118 error_return:
6119 free (included);
6120 return false;
6121 }
6122
6123 /* Given an ELF BFD, add symbols to the global hash table as
6124 appropriate. */
6125
6126 bool
6127 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
6128 {
6129 switch (bfd_get_format (abfd))
6130 {
6131 case bfd_object:
6132 return elf_link_add_object_symbols (abfd, info);
6133 case bfd_archive:
6134 return elf_link_add_archive_symbols (abfd, info);
6135 default:
6136 bfd_set_error (bfd_error_wrong_format);
6137 return false;
6138 }
6139 }
6140
6141 struct hash_codes_info
6143 {
6144 unsigned long *hashcodes;
6145 bool error;
6146 };
6147
6148 /* This function will be called though elf_link_hash_traverse to store
6149 all hash value of the exported symbols in an array. */
6150
6151 static bool
6152 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
6153 {
6154 struct hash_codes_info *inf = (struct hash_codes_info *) data;
6155 const char *name;
6156 unsigned long ha;
6157 char *alc = NULL;
6158
6159 /* Ignore indirect symbols. These are added by the versioning code. */
6160 if (h->dynindx == -1)
6161 return true;
6162
6163 name = h->root.root.string;
6164 if (h->versioned >= versioned)
6165 {
6166 char *p = strchr (name, ELF_VER_CHR);
6167 if (p != NULL)
6168 {
6169 alc = (char *) bfd_malloc (p - name + 1);
6170 if (alc == NULL)
6171 {
6172 inf->error = true;
6173 return false;
6174 }
6175 memcpy (alc, name, p - name);
6176 alc[p - name] = '\0';
6177 name = alc;
6178 }
6179 }
6180
6181 /* Compute the hash value. */
6182 ha = bfd_elf_hash (name);
6183
6184 /* Store the found hash value in the array given as the argument. */
6185 *(inf->hashcodes)++ = ha;
6186
6187 /* And store it in the struct so that we can put it in the hash table
6188 later. */
6189 h->u.elf_hash_value = ha;
6190
6191 free (alc);
6192 return true;
6193 }
6194
6195 struct collect_gnu_hash_codes
6196 {
6197 bfd *output_bfd;
6198 const struct elf_backend_data *bed;
6199 unsigned long int nsyms;
6200 unsigned long int maskbits;
6201 unsigned long int *hashcodes;
6202 unsigned long int *hashval;
6203 unsigned long int *indx;
6204 unsigned long int *counts;
6205 bfd_vma *bitmask;
6206 bfd_byte *contents;
6207 bfd_size_type xlat;
6208 long int min_dynindx;
6209 unsigned long int bucketcount;
6210 unsigned long int symindx;
6211 long int local_indx;
6212 long int shift1, shift2;
6213 unsigned long int mask;
6214 bool error;
6215 };
6216
6217 /* This function will be called though elf_link_hash_traverse to store
6218 all hash value of the exported symbols in an array. */
6219
6220 static bool
6221 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
6222 {
6223 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6224 const char *name;
6225 unsigned long ha;
6226 char *alc = NULL;
6227
6228 /* Ignore indirect symbols. These are added by the versioning code. */
6229 if (h->dynindx == -1)
6230 return true;
6231
6232 /* Ignore also local symbols and undefined symbols. */
6233 if (! (*s->bed->elf_hash_symbol) (h))
6234 return true;
6235
6236 name = h->root.root.string;
6237 if (h->versioned >= versioned)
6238 {
6239 char *p = strchr (name, ELF_VER_CHR);
6240 if (p != NULL)
6241 {
6242 alc = (char *) bfd_malloc (p - name + 1);
6243 if (alc == NULL)
6244 {
6245 s->error = true;
6246 return false;
6247 }
6248 memcpy (alc, name, p - name);
6249 alc[p - name] = '\0';
6250 name = alc;
6251 }
6252 }
6253
6254 /* Compute the hash value. */
6255 ha = bfd_elf_gnu_hash (name);
6256
6257 /* Store the found hash value in the array for compute_bucket_count,
6258 and also for .dynsym reordering purposes. */
6259 s->hashcodes[s->nsyms] = ha;
6260 s->hashval[h->dynindx] = ha;
6261 ++s->nsyms;
6262 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
6263 s->min_dynindx = h->dynindx;
6264
6265 free (alc);
6266 return true;
6267 }
6268
6269 /* This function will be called though elf_link_hash_traverse to do
6270 final dynamic symbol renumbering in case of .gnu.hash.
6271 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6272 to the translation table. */
6273
6274 static bool
6275 elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data)
6276 {
6277 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6278 unsigned long int bucket;
6279 unsigned long int val;
6280
6281 /* Ignore indirect symbols. */
6282 if (h->dynindx == -1)
6283 return true;
6284
6285 /* Ignore also local symbols and undefined symbols. */
6286 if (! (*s->bed->elf_hash_symbol) (h))
6287 {
6288 if (h->dynindx >= s->min_dynindx)
6289 {
6290 if (s->bed->record_xhash_symbol != NULL)
6291 {
6292 (*s->bed->record_xhash_symbol) (h, 0);
6293 s->local_indx++;
6294 }
6295 else
6296 h->dynindx = s->local_indx++;
6297 }
6298 return true;
6299 }
6300
6301 bucket = s->hashval[h->dynindx] % s->bucketcount;
6302 val = (s->hashval[h->dynindx] >> s->shift1)
6303 & ((s->maskbits >> s->shift1) - 1);
6304 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
6305 s->bitmask[val]
6306 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
6307 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
6308 if (s->counts[bucket] == 1)
6309 /* Last element terminates the chain. */
6310 val |= 1;
6311 bfd_put_32 (s->output_bfd, val,
6312 s->contents + (s->indx[bucket] - s->symindx) * 4);
6313 --s->counts[bucket];
6314 if (s->bed->record_xhash_symbol != NULL)
6315 {
6316 bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4;
6317
6318 (*s->bed->record_xhash_symbol) (h, xlat_loc);
6319 }
6320 else
6321 h->dynindx = s->indx[bucket]++;
6322 return true;
6323 }
6324
6325 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6326
6327 bool
6328 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
6329 {
6330 return !(h->forced_local
6331 || h->root.type == bfd_link_hash_undefined
6332 || h->root.type == bfd_link_hash_undefweak
6333 || ((h->root.type == bfd_link_hash_defined
6334 || h->root.type == bfd_link_hash_defweak)
6335 && h->root.u.def.section->output_section == NULL));
6336 }
6337
6338 /* Array used to determine the number of hash table buckets to use
6339 based on the number of symbols there are. If there are fewer than
6340 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6341 fewer than 37 we use 17 buckets, and so forth. We never use more
6342 than 32771 buckets. */
6343
6344 static const size_t elf_buckets[] =
6345 {
6346 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6347 16411, 32771, 0
6348 };
6349
6350 /* Compute bucket count for hashing table. We do not use a static set
6351 of possible tables sizes anymore. Instead we determine for all
6352 possible reasonable sizes of the table the outcome (i.e., the
6353 number of collisions etc) and choose the best solution. The
6354 weighting functions are not too simple to allow the table to grow
6355 without bounds. Instead one of the weighting factors is the size.
6356 Therefore the result is always a good payoff between few collisions
6357 (= short chain lengths) and table size. */
6358 static size_t
6359 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
6360 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
6361 unsigned long int nsyms,
6362 int gnu_hash)
6363 {
6364 size_t best_size = 0;
6365 unsigned long int i;
6366
6367 if (info->optimize)
6368 {
6369 size_t minsize;
6370 size_t maxsize;
6371 uint64_t best_chlen = ~((uint64_t) 0);
6372 bfd *dynobj = elf_hash_table (info)->dynobj;
6373 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
6374 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
6375 unsigned long int *counts;
6376 bfd_size_type amt;
6377 unsigned int no_improvement_count = 0;
6378
6379 /* Possible optimization parameters: if we have NSYMS symbols we say
6380 that the hashing table must at least have NSYMS/4 and at most
6381 2*NSYMS buckets. */
6382 minsize = nsyms / 4;
6383 if (minsize == 0)
6384 minsize = 1;
6385 best_size = maxsize = nsyms * 2;
6386 if (gnu_hash)
6387 {
6388 if (minsize < 2)
6389 minsize = 2;
6390 if ((best_size & 31) == 0)
6391 ++best_size;
6392 }
6393
6394 /* Create array where we count the collisions in. We must use bfd_malloc
6395 since the size could be large. */
6396 amt = maxsize;
6397 amt *= sizeof (unsigned long int);
6398 counts = (unsigned long int *) bfd_malloc (amt);
6399 if (counts == NULL)
6400 return 0;
6401
6402 /* Compute the "optimal" size for the hash table. The criteria is a
6403 minimal chain length. The minor criteria is (of course) the size
6404 of the table. */
6405 for (i = minsize; i < maxsize; ++i)
6406 {
6407 /* Walk through the array of hashcodes and count the collisions. */
6408 uint64_t max;
6409 unsigned long int j;
6410 unsigned long int fact;
6411
6412 if (gnu_hash && (i & 31) == 0)
6413 continue;
6414
6415 memset (counts, '\0', i * sizeof (unsigned long int));
6416
6417 /* Determine how often each hash bucket is used. */
6418 for (j = 0; j < nsyms; ++j)
6419 ++counts[hashcodes[j] % i];
6420
6421 /* For the weight function we need some information about the
6422 pagesize on the target. This is information need not be 100%
6423 accurate. Since this information is not available (so far) we
6424 define it here to a reasonable default value. If it is crucial
6425 to have a better value some day simply define this value. */
6426 # ifndef BFD_TARGET_PAGESIZE
6427 # define BFD_TARGET_PAGESIZE (4096)
6428 # endif
6429
6430 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6431 and the chains. */
6432 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
6433
6434 # if 1
6435 /* Variant 1: optimize for short chains. We add the squares
6436 of all the chain lengths (which favors many small chain
6437 over a few long chains). */
6438 for (j = 0; j < i; ++j)
6439 max += counts[j] * counts[j];
6440
6441 /* This adds penalties for the overall size of the table. */
6442 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6443 max *= fact * fact;
6444 # else
6445 /* Variant 2: Optimize a lot more for small table. Here we
6446 also add squares of the size but we also add penalties for
6447 empty slots (the +1 term). */
6448 for (j = 0; j < i; ++j)
6449 max += (1 + counts[j]) * (1 + counts[j]);
6450
6451 /* The overall size of the table is considered, but not as
6452 strong as in variant 1, where it is squared. */
6453 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6454 max *= fact;
6455 # endif
6456
6457 /* Compare with current best results. */
6458 if (max < best_chlen)
6459 {
6460 best_chlen = max;
6461 best_size = i;
6462 no_improvement_count = 0;
6463 }
6464 /* PR 11843: Avoid futile long searches for the best bucket size
6465 when there are a large number of symbols. */
6466 else if (++no_improvement_count == 100)
6467 break;
6468 }
6469
6470 free (counts);
6471 }
6472 else
6473 {
6474 for (i = 0; elf_buckets[i] != 0; i++)
6475 {
6476 best_size = elf_buckets[i];
6477 if (nsyms < elf_buckets[i + 1])
6478 break;
6479 }
6480 if (gnu_hash && best_size < 2)
6481 best_size = 2;
6482 }
6483
6484 return best_size;
6485 }
6486
6487 /* Size any SHT_GROUP section for ld -r. */
6488
6489 bool
6490 _bfd_elf_size_group_sections (struct bfd_link_info *info)
6491 {
6492 bfd *ibfd;
6493 asection *s;
6494
6495 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6496 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6497 && (s = ibfd->sections) != NULL
6498 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
6499 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6500 return false;
6501 return true;
6502 }
6503
6504 /* Set a default stack segment size. The value in INFO wins. If it
6505 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6506 undefined it is initialized. */
6507
6508 bool
6509 bfd_elf_stack_segment_size (bfd *output_bfd,
6510 struct bfd_link_info *info,
6511 const char *legacy_symbol,
6512 bfd_vma default_size)
6513 {
6514 struct elf_link_hash_entry *h = NULL;
6515
6516 /* Look for legacy symbol. */
6517 if (legacy_symbol)
6518 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6519 false, false, false);
6520 if (h && (h->root.type == bfd_link_hash_defined
6521 || h->root.type == bfd_link_hash_defweak)
6522 && h->def_regular
6523 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6524 {
6525 /* The symbol has no type if specified on the command line. */
6526 h->type = STT_OBJECT;
6527 if (info->stacksize)
6528 /* xgettext:c-format */
6529 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6530 output_bfd, legacy_symbol);
6531 else if (h->root.u.def.section != bfd_abs_section_ptr)
6532 /* xgettext:c-format */
6533 _bfd_error_handler (_("%pB: %s not absolute"),
6534 output_bfd, legacy_symbol);
6535 else
6536 info->stacksize = h->root.u.def.value;
6537 }
6538
6539 if (!info->stacksize)
6540 /* If the user didn't set a size, or explicitly inhibit the
6541 size, set it now. */
6542 info->stacksize = default_size;
6543
6544 /* Provide the legacy symbol, if it is referenced. */
6545 if (h && (h->root.type == bfd_link_hash_undefined
6546 || h->root.type == bfd_link_hash_undefweak))
6547 {
6548 struct bfd_link_hash_entry *bh = NULL;
6549
6550 if (!(_bfd_generic_link_add_one_symbol
6551 (info, output_bfd, legacy_symbol,
6552 BSF_GLOBAL, bfd_abs_section_ptr,
6553 info->stacksize >= 0 ? info->stacksize : 0,
6554 NULL, false, get_elf_backend_data (output_bfd)->collect, &bh)))
6555 return false;
6556
6557 h = (struct elf_link_hash_entry *) bh;
6558 h->def_regular = 1;
6559 h->type = STT_OBJECT;
6560 }
6561
6562 return true;
6563 }
6564
6565 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6566
6567 struct elf_gc_sweep_symbol_info
6568 {
6569 struct bfd_link_info *info;
6570 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6571 bool);
6572 };
6573
6574 static bool
6575 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6576 {
6577 if (!h->mark
6578 && (((h->root.type == bfd_link_hash_defined
6579 || h->root.type == bfd_link_hash_defweak)
6580 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6581 && h->root.u.def.section->gc_mark))
6582 || h->root.type == bfd_link_hash_undefined
6583 || h->root.type == bfd_link_hash_undefweak))
6584 {
6585 struct elf_gc_sweep_symbol_info *inf;
6586
6587 inf = (struct elf_gc_sweep_symbol_info *) data;
6588 (*inf->hide_symbol) (inf->info, h, true);
6589 h->def_regular = 0;
6590 h->ref_regular = 0;
6591 h->ref_regular_nonweak = 0;
6592 }
6593
6594 return true;
6595 }
6596
6597 /* Set up the sizes and contents of the ELF dynamic sections. This is
6598 called by the ELF linker emulation before_allocation routine. We
6599 must set the sizes of the sections before the linker sets the
6600 addresses of the various sections. */
6601
6602 bool
6603 bfd_elf_size_dynamic_sections (bfd *output_bfd,
6604 const char *soname,
6605 const char *rpath,
6606 const char *filter_shlib,
6607 const char *audit,
6608 const char *depaudit,
6609 const char * const *auxiliary_filters,
6610 struct bfd_link_info *info,
6611 asection **sinterpptr)
6612 {
6613 bfd *dynobj;
6614 const struct elf_backend_data *bed;
6615
6616 *sinterpptr = NULL;
6617
6618 if (!is_elf_hash_table (info->hash))
6619 return true;
6620
6621 /* Any syms created from now on start with -1 in
6622 got.refcount/offset and plt.refcount/offset. */
6623 elf_hash_table (info)->init_got_refcount
6624 = elf_hash_table (info)->init_got_offset;
6625 elf_hash_table (info)->init_plt_refcount
6626 = elf_hash_table (info)->init_plt_offset;
6627
6628 bed = get_elf_backend_data (output_bfd);
6629
6630 /* The backend may have to create some sections regardless of whether
6631 we're dynamic or not. */
6632 if (bed->elf_backend_always_size_sections
6633 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6634 return false;
6635
6636 dynobj = elf_hash_table (info)->dynobj;
6637
6638 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6639 {
6640 struct bfd_elf_version_tree *verdefs;
6641 struct elf_info_failed asvinfo;
6642 struct bfd_elf_version_tree *t;
6643 struct bfd_elf_version_expr *d;
6644 asection *s;
6645 size_t soname_indx;
6646
6647 /* If we are supposed to export all symbols into the dynamic symbol
6648 table (this is not the normal case), then do so. */
6649 if (info->export_dynamic
6650 || (bfd_link_executable (info) && info->dynamic))
6651 {
6652 struct elf_info_failed eif;
6653
6654 eif.info = info;
6655 eif.failed = false;
6656 elf_link_hash_traverse (elf_hash_table (info),
6657 _bfd_elf_export_symbol,
6658 &eif);
6659 if (eif.failed)
6660 return false;
6661 }
6662
6663 if (soname != NULL)
6664 {
6665 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6666 soname, true);
6667 if (soname_indx == (size_t) -1
6668 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6669 return false;
6670 }
6671 else
6672 soname_indx = (size_t) -1;
6673
6674 /* Make all global versions with definition. */
6675 for (t = info->version_info; t != NULL; t = t->next)
6676 for (d = t->globals.list; d != NULL; d = d->next)
6677 if (!d->symver && d->literal)
6678 {
6679 const char *verstr, *name;
6680 size_t namelen, verlen, newlen;
6681 char *newname, *p, leading_char;
6682 struct elf_link_hash_entry *newh;
6683
6684 leading_char = bfd_get_symbol_leading_char (output_bfd);
6685 name = d->pattern;
6686 namelen = strlen (name) + (leading_char != '\0');
6687 verstr = t->name;
6688 verlen = strlen (verstr);
6689 newlen = namelen + verlen + 3;
6690
6691 newname = (char *) bfd_malloc (newlen);
6692 if (newname == NULL)
6693 return false;
6694 newname[0] = leading_char;
6695 memcpy (newname + (leading_char != '\0'), name, namelen);
6696
6697 /* Check the hidden versioned definition. */
6698 p = newname + namelen;
6699 *p++ = ELF_VER_CHR;
6700 memcpy (p, verstr, verlen + 1);
6701 newh = elf_link_hash_lookup (elf_hash_table (info),
6702 newname, false, false,
6703 false);
6704 if (newh == NULL
6705 || (newh->root.type != bfd_link_hash_defined
6706 && newh->root.type != bfd_link_hash_defweak))
6707 {
6708 /* Check the default versioned definition. */
6709 *p++ = ELF_VER_CHR;
6710 memcpy (p, verstr, verlen + 1);
6711 newh = elf_link_hash_lookup (elf_hash_table (info),
6712 newname, false, false,
6713 false);
6714 }
6715 free (newname);
6716
6717 /* Mark this version if there is a definition and it is
6718 not defined in a shared object. */
6719 if (newh != NULL
6720 && !newh->def_dynamic
6721 && (newh->root.type == bfd_link_hash_defined
6722 || newh->root.type == bfd_link_hash_defweak))
6723 d->symver = 1;
6724 }
6725
6726 /* Attach all the symbols to their version information. */
6727 asvinfo.info = info;
6728 asvinfo.failed = false;
6729
6730 elf_link_hash_traverse (elf_hash_table (info),
6731 _bfd_elf_link_assign_sym_version,
6732 &asvinfo);
6733 if (asvinfo.failed)
6734 return false;
6735
6736 if (!info->allow_undefined_version)
6737 {
6738 /* Check if all global versions have a definition. */
6739 bool all_defined = true;
6740 for (t = info->version_info; t != NULL; t = t->next)
6741 for (d = t->globals.list; d != NULL; d = d->next)
6742 if (d->literal && !d->symver && !d->script)
6743 {
6744 _bfd_error_handler
6745 (_("%s: undefined version: %s"),
6746 d->pattern, t->name);
6747 all_defined = false;
6748 }
6749
6750 if (!all_defined)
6751 {
6752 bfd_set_error (bfd_error_bad_value);
6753 return false;
6754 }
6755 }
6756
6757 /* Set up the version definition section. */
6758 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6759 BFD_ASSERT (s != NULL);
6760
6761 /* We may have created additional version definitions if we are
6762 just linking a regular application. */
6763 verdefs = info->version_info;
6764
6765 /* Skip anonymous version tag. */
6766 if (verdefs != NULL && verdefs->vernum == 0)
6767 verdefs = verdefs->next;
6768
6769 if (verdefs == NULL && !info->create_default_symver)
6770 s->flags |= SEC_EXCLUDE;
6771 else
6772 {
6773 unsigned int cdefs;
6774 bfd_size_type size;
6775 bfd_byte *p;
6776 Elf_Internal_Verdef def;
6777 Elf_Internal_Verdaux defaux;
6778 struct bfd_link_hash_entry *bh;
6779 struct elf_link_hash_entry *h;
6780 const char *name;
6781
6782 cdefs = 0;
6783 size = 0;
6784
6785 /* Make space for the base version. */
6786 size += sizeof (Elf_External_Verdef);
6787 size += sizeof (Elf_External_Verdaux);
6788 ++cdefs;
6789
6790 /* Make space for the default version. */
6791 if (info->create_default_symver)
6792 {
6793 size += sizeof (Elf_External_Verdef);
6794 ++cdefs;
6795 }
6796
6797 for (t = verdefs; t != NULL; t = t->next)
6798 {
6799 struct bfd_elf_version_deps *n;
6800
6801 /* Don't emit base version twice. */
6802 if (t->vernum == 0)
6803 continue;
6804
6805 size += sizeof (Elf_External_Verdef);
6806 size += sizeof (Elf_External_Verdaux);
6807 ++cdefs;
6808
6809 for (n = t->deps; n != NULL; n = n->next)
6810 size += sizeof (Elf_External_Verdaux);
6811 }
6812
6813 s->size = size;
6814 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6815 if (s->contents == NULL && s->size != 0)
6816 return false;
6817
6818 /* Fill in the version definition section. */
6819
6820 p = s->contents;
6821
6822 def.vd_version = VER_DEF_CURRENT;
6823 def.vd_flags = VER_FLG_BASE;
6824 def.vd_ndx = 1;
6825 def.vd_cnt = 1;
6826 if (info->create_default_symver)
6827 {
6828 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6829 def.vd_next = sizeof (Elf_External_Verdef);
6830 }
6831 else
6832 {
6833 def.vd_aux = sizeof (Elf_External_Verdef);
6834 def.vd_next = (sizeof (Elf_External_Verdef)
6835 + sizeof (Elf_External_Verdaux));
6836 }
6837
6838 if (soname_indx != (size_t) -1)
6839 {
6840 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6841 soname_indx);
6842 def.vd_hash = bfd_elf_hash (soname);
6843 defaux.vda_name = soname_indx;
6844 name = soname;
6845 }
6846 else
6847 {
6848 size_t indx;
6849
6850 name = lbasename (bfd_get_filename (output_bfd));
6851 def.vd_hash = bfd_elf_hash (name);
6852 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6853 name, false);
6854 if (indx == (size_t) -1)
6855 return false;
6856 defaux.vda_name = indx;
6857 }
6858 defaux.vda_next = 0;
6859
6860 _bfd_elf_swap_verdef_out (output_bfd, &def,
6861 (Elf_External_Verdef *) p);
6862 p += sizeof (Elf_External_Verdef);
6863 if (info->create_default_symver)
6864 {
6865 /* Add a symbol representing this version. */
6866 bh = NULL;
6867 if (! (_bfd_generic_link_add_one_symbol
6868 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6869 0, NULL, false,
6870 get_elf_backend_data (dynobj)->collect, &bh)))
6871 return false;
6872 h = (struct elf_link_hash_entry *) bh;
6873 h->non_elf = 0;
6874 h->def_regular = 1;
6875 h->type = STT_OBJECT;
6876 h->verinfo.vertree = NULL;
6877
6878 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6879 return false;
6880
6881 /* Create a duplicate of the base version with the same
6882 aux block, but different flags. */
6883 def.vd_flags = 0;
6884 def.vd_ndx = 2;
6885 def.vd_aux = sizeof (Elf_External_Verdef);
6886 if (verdefs)
6887 def.vd_next = (sizeof (Elf_External_Verdef)
6888 + sizeof (Elf_External_Verdaux));
6889 else
6890 def.vd_next = 0;
6891 _bfd_elf_swap_verdef_out (output_bfd, &def,
6892 (Elf_External_Verdef *) p);
6893 p += sizeof (Elf_External_Verdef);
6894 }
6895 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6896 (Elf_External_Verdaux *) p);
6897 p += sizeof (Elf_External_Verdaux);
6898
6899 for (t = verdefs; t != NULL; t = t->next)
6900 {
6901 unsigned int cdeps;
6902 struct bfd_elf_version_deps *n;
6903
6904 /* Don't emit the base version twice. */
6905 if (t->vernum == 0)
6906 continue;
6907
6908 cdeps = 0;
6909 for (n = t->deps; n != NULL; n = n->next)
6910 ++cdeps;
6911
6912 /* Add a symbol representing this version. */
6913 bh = NULL;
6914 if (! (_bfd_generic_link_add_one_symbol
6915 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6916 0, NULL, false,
6917 get_elf_backend_data (dynobj)->collect, &bh)))
6918 return false;
6919 h = (struct elf_link_hash_entry *) bh;
6920 h->non_elf = 0;
6921 h->def_regular = 1;
6922 h->type = STT_OBJECT;
6923 h->verinfo.vertree = t;
6924
6925 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6926 return false;
6927
6928 def.vd_version = VER_DEF_CURRENT;
6929 def.vd_flags = 0;
6930 if (t->globals.list == NULL
6931 && t->locals.list == NULL
6932 && ! t->used)
6933 def.vd_flags |= VER_FLG_WEAK;
6934 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6935 def.vd_cnt = cdeps + 1;
6936 def.vd_hash = bfd_elf_hash (t->name);
6937 def.vd_aux = sizeof (Elf_External_Verdef);
6938 def.vd_next = 0;
6939
6940 /* If a basever node is next, it *must* be the last node in
6941 the chain, otherwise Verdef construction breaks. */
6942 if (t->next != NULL && t->next->vernum == 0)
6943 BFD_ASSERT (t->next->next == NULL);
6944
6945 if (t->next != NULL && t->next->vernum != 0)
6946 def.vd_next = (sizeof (Elf_External_Verdef)
6947 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6948
6949 _bfd_elf_swap_verdef_out (output_bfd, &def,
6950 (Elf_External_Verdef *) p);
6951 p += sizeof (Elf_External_Verdef);
6952
6953 defaux.vda_name = h->dynstr_index;
6954 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6955 h->dynstr_index);
6956 defaux.vda_next = 0;
6957 if (t->deps != NULL)
6958 defaux.vda_next = sizeof (Elf_External_Verdaux);
6959 t->name_indx = defaux.vda_name;
6960
6961 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6962 (Elf_External_Verdaux *) p);
6963 p += sizeof (Elf_External_Verdaux);
6964
6965 for (n = t->deps; n != NULL; n = n->next)
6966 {
6967 if (n->version_needed == NULL)
6968 {
6969 /* This can happen if there was an error in the
6970 version script. */
6971 defaux.vda_name = 0;
6972 }
6973 else
6974 {
6975 defaux.vda_name = n->version_needed->name_indx;
6976 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6977 defaux.vda_name);
6978 }
6979 if (n->next == NULL)
6980 defaux.vda_next = 0;
6981 else
6982 defaux.vda_next = sizeof (Elf_External_Verdaux);
6983
6984 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6985 (Elf_External_Verdaux *) p);
6986 p += sizeof (Elf_External_Verdaux);
6987 }
6988 }
6989
6990 elf_tdata (output_bfd)->cverdefs = cdefs;
6991 }
6992 }
6993
6994 if (info->gc_sections && bed->can_gc_sections)
6995 {
6996 struct elf_gc_sweep_symbol_info sweep_info;
6997
6998 /* Remove the symbols that were in the swept sections from the
6999 dynamic symbol table. */
7000 sweep_info.info = info;
7001 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
7002 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
7003 &sweep_info);
7004 }
7005
7006 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7007 {
7008 asection *s;
7009 struct elf_find_verdep_info sinfo;
7010
7011 /* Work out the size of the version reference section. */
7012
7013 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
7014 BFD_ASSERT (s != NULL);
7015
7016 sinfo.info = info;
7017 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
7018 if (sinfo.vers == 0)
7019 sinfo.vers = 1;
7020 sinfo.failed = false;
7021
7022 elf_link_hash_traverse (elf_hash_table (info),
7023 _bfd_elf_link_find_version_dependencies,
7024 &sinfo);
7025 if (sinfo.failed)
7026 return false;
7027
7028 if (info->enable_dt_relr)
7029 {
7030 elf_link_add_dt_relr_dependency (&sinfo);
7031 if (sinfo.failed)
7032 return false;
7033 }
7034
7035 if (elf_tdata (output_bfd)->verref == NULL)
7036 s->flags |= SEC_EXCLUDE;
7037 else
7038 {
7039 Elf_Internal_Verneed *vn;
7040 unsigned int size;
7041 unsigned int crefs;
7042 bfd_byte *p;
7043
7044 /* Build the version dependency section. */
7045 size = 0;
7046 crefs = 0;
7047 for (vn = elf_tdata (output_bfd)->verref;
7048 vn != NULL;
7049 vn = vn->vn_nextref)
7050 {
7051 Elf_Internal_Vernaux *a;
7052
7053 size += sizeof (Elf_External_Verneed);
7054 ++crefs;
7055 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7056 size += sizeof (Elf_External_Vernaux);
7057 }
7058
7059 s->size = size;
7060 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7061 if (s->contents == NULL)
7062 return false;
7063
7064 p = s->contents;
7065 for (vn = elf_tdata (output_bfd)->verref;
7066 vn != NULL;
7067 vn = vn->vn_nextref)
7068 {
7069 unsigned int caux;
7070 Elf_Internal_Vernaux *a;
7071 size_t indx;
7072
7073 caux = 0;
7074 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7075 ++caux;
7076
7077 vn->vn_version = VER_NEED_CURRENT;
7078 vn->vn_cnt = caux;
7079 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7080 elf_dt_name (vn->vn_bfd) != NULL
7081 ? elf_dt_name (vn->vn_bfd)
7082 : lbasename (bfd_get_filename
7083 (vn->vn_bfd)),
7084 false);
7085 if (indx == (size_t) -1)
7086 return false;
7087 vn->vn_file = indx;
7088 vn->vn_aux = sizeof (Elf_External_Verneed);
7089 if (vn->vn_nextref == NULL)
7090 vn->vn_next = 0;
7091 else
7092 vn->vn_next = (sizeof (Elf_External_Verneed)
7093 + caux * sizeof (Elf_External_Vernaux));
7094
7095 _bfd_elf_swap_verneed_out (output_bfd, vn,
7096 (Elf_External_Verneed *) p);
7097 p += sizeof (Elf_External_Verneed);
7098
7099 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7100 {
7101 a->vna_hash = bfd_elf_hash (a->vna_nodename);
7102 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7103 a->vna_nodename, false);
7104 if (indx == (size_t) -1)
7105 return false;
7106 a->vna_name = indx;
7107 if (a->vna_nextptr == NULL)
7108 a->vna_next = 0;
7109 else
7110 a->vna_next = sizeof (Elf_External_Vernaux);
7111
7112 _bfd_elf_swap_vernaux_out (output_bfd, a,
7113 (Elf_External_Vernaux *) p);
7114 p += sizeof (Elf_External_Vernaux);
7115 }
7116 }
7117
7118 elf_tdata (output_bfd)->cverrefs = crefs;
7119 }
7120 }
7121
7122 if (bfd_link_relocatable (info)
7123 && !_bfd_elf_size_group_sections (info))
7124 return false;
7125
7126 /* Determine any GNU_STACK segment requirements, after the backend
7127 has had a chance to set a default segment size. */
7128 if (info->execstack)
7129 {
7130 /* If the user has explicitly requested warnings, then generate one even
7131 though the choice is the result of another command line option. */
7132 if (info->warn_execstack == 1)
7133 _bfd_error_handler
7134 (_("\
7135 warning: enabling an executable stack because of -z execstack command line option"));
7136 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
7137 }
7138 else if (info->noexecstack)
7139 elf_stack_flags (output_bfd) = PF_R | PF_W;
7140 else
7141 {
7142 bfd *inputobj;
7143 asection *notesec = NULL;
7144 bfd *noteobj = NULL;
7145 bfd *emptyobj = NULL;
7146 int exec = 0;
7147
7148 for (inputobj = info->input_bfds;
7149 inputobj;
7150 inputobj = inputobj->link.next)
7151 {
7152 asection *s;
7153
7154 if (inputobj->flags
7155 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
7156 continue;
7157 s = inputobj->sections;
7158 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7159 continue;
7160
7161 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
7162 if (s)
7163 {
7164 notesec = s;
7165 if (s->flags & SEC_CODE)
7166 {
7167 noteobj = inputobj;
7168 exec = PF_X;
7169 /* There is no point in scanning the remaining bfds. */
7170 break;
7171 }
7172 }
7173 else if (bed->default_execstack && info->default_execstack)
7174 {
7175 exec = PF_X;
7176 emptyobj = inputobj;
7177 }
7178 }
7179
7180 if (notesec || info->stacksize > 0)
7181 {
7182 if (exec)
7183 {
7184 if (info->warn_execstack != 0)
7185 {
7186 /* PR 29072: Because an executable stack is a serious
7187 security risk, make sure that the user knows that it is
7188 being enabled despite the fact that it was not requested
7189 on the command line. */
7190 if (noteobj)
7191 _bfd_error_handler (_("\
7192 warning: %s: requires executable stack (because the .note.GNU-stack section is executable)"),
7193 bfd_get_filename (noteobj));
7194 else if (emptyobj)
7195 {
7196 _bfd_error_handler (_("\
7197 warning: %s: missing .note.GNU-stack section implies executable stack"),
7198 bfd_get_filename (emptyobj));
7199 _bfd_error_handler (_("\
7200 NOTE: This behaviour is deprecated and will be removed in a future version of the linker"));
7201 }
7202 }
7203 }
7204 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
7205 }
7206
7207 if (notesec && exec && bfd_link_relocatable (info)
7208 && notesec->output_section != bfd_abs_section_ptr)
7209 notesec->output_section->flags |= SEC_CODE;
7210 }
7211
7212 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7213 {
7214 struct elf_info_failed eif;
7215 struct elf_link_hash_entry *h;
7216 asection *dynstr;
7217 asection *s;
7218
7219 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
7220 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
7221
7222 if (info->symbolic)
7223 {
7224 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
7225 return false;
7226 info->flags |= DF_SYMBOLIC;
7227 }
7228
7229 if (rpath != NULL)
7230 {
7231 size_t indx;
7232 bfd_vma tag;
7233
7234 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
7235 true);
7236 if (indx == (size_t) -1)
7237 return false;
7238
7239 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
7240 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
7241 return false;
7242 }
7243
7244 if (filter_shlib != NULL)
7245 {
7246 size_t indx;
7247
7248 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7249 filter_shlib, true);
7250 if (indx == (size_t) -1
7251 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
7252 return false;
7253 }
7254
7255 if (auxiliary_filters != NULL)
7256 {
7257 const char * const *p;
7258
7259 for (p = auxiliary_filters; *p != NULL; p++)
7260 {
7261 size_t indx;
7262
7263 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7264 *p, true);
7265 if (indx == (size_t) -1
7266 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
7267 return false;
7268 }
7269 }
7270
7271 if (audit != NULL)
7272 {
7273 size_t indx;
7274
7275 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
7276 true);
7277 if (indx == (size_t) -1
7278 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
7279 return false;
7280 }
7281
7282 if (depaudit != NULL)
7283 {
7284 size_t indx;
7285
7286 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
7287 true);
7288 if (indx == (size_t) -1
7289 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
7290 return false;
7291 }
7292
7293 eif.info = info;
7294 eif.failed = false;
7295
7296 /* Find all symbols which were defined in a dynamic object and make
7297 the backend pick a reasonable value for them. */
7298 elf_link_hash_traverse (elf_hash_table (info),
7299 _bfd_elf_adjust_dynamic_symbol,
7300 &eif);
7301 if (eif.failed)
7302 return false;
7303
7304 /* Add some entries to the .dynamic section. We fill in some of the
7305 values later, in bfd_elf_final_link, but we must add the entries
7306 now so that we know the final size of the .dynamic section. */
7307
7308 /* If there are initialization and/or finalization functions to
7309 call then add the corresponding DT_INIT/DT_FINI entries. */
7310 h = (info->init_function
7311 ? elf_link_hash_lookup (elf_hash_table (info),
7312 info->init_function, false,
7313 false, false)
7314 : NULL);
7315 if (h != NULL
7316 && (h->ref_regular
7317 || h->def_regular))
7318 {
7319 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
7320 return false;
7321 }
7322 h = (info->fini_function
7323 ? elf_link_hash_lookup (elf_hash_table (info),
7324 info->fini_function, false,
7325 false, false)
7326 : NULL);
7327 if (h != NULL
7328 && (h->ref_regular
7329 || h->def_regular))
7330 {
7331 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
7332 return false;
7333 }
7334
7335 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
7336 if (s != NULL && s->linker_has_input)
7337 {
7338 /* DT_PREINIT_ARRAY is not allowed in shared library. */
7339 if (! bfd_link_executable (info))
7340 {
7341 bfd *sub;
7342 asection *o;
7343
7344 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
7345 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
7346 && (o = sub->sections) != NULL
7347 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
7348 for (o = sub->sections; o != NULL; o = o->next)
7349 if (elf_section_data (o)->this_hdr.sh_type
7350 == SHT_PREINIT_ARRAY)
7351 {
7352 _bfd_error_handler
7353 (_("%pB: .preinit_array section is not allowed in DSO"),
7354 sub);
7355 break;
7356 }
7357
7358 bfd_set_error (bfd_error_nonrepresentable_section);
7359 return false;
7360 }
7361
7362 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
7363 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
7364 return false;
7365 }
7366 s = bfd_get_section_by_name (output_bfd, ".init_array");
7367 if (s != NULL && s->linker_has_input)
7368 {
7369 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
7370 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
7371 return false;
7372 }
7373 s = bfd_get_section_by_name (output_bfd, ".fini_array");
7374 if (s != NULL && s->linker_has_input)
7375 {
7376 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
7377 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
7378 return false;
7379 }
7380
7381 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
7382 /* If .dynstr is excluded from the link, we don't want any of
7383 these tags. Strictly, we should be checking each section
7384 individually; This quick check covers for the case where
7385 someone does a /DISCARD/ : { *(*) }. */
7386 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
7387 {
7388 bfd_size_type strsize;
7389
7390 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7391 if ((info->emit_hash
7392 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
7393 || (info->emit_gnu_hash
7394 && (bed->record_xhash_symbol == NULL
7395 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)))
7396 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
7397 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
7398 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
7399 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
7400 bed->s->sizeof_sym)
7401 || (info->gnu_flags_1
7402 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_FLAGS_1,
7403 info->gnu_flags_1)))
7404 return false;
7405 }
7406 }
7407
7408 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
7409 return false;
7410
7411 /* The backend must work out the sizes of all the other dynamic
7412 sections. */
7413 if (dynobj != NULL
7414 && bed->elf_backend_size_dynamic_sections != NULL
7415 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
7416 return false;
7417
7418 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7419 {
7420 if (elf_tdata (output_bfd)->cverdefs)
7421 {
7422 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
7423
7424 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
7425 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
7426 return false;
7427 }
7428
7429 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
7430 {
7431 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
7432 return false;
7433 }
7434 else if (info->flags & DF_BIND_NOW)
7435 {
7436 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
7437 return false;
7438 }
7439
7440 if (info->flags_1)
7441 {
7442 if (bfd_link_executable (info))
7443 info->flags_1 &= ~ (DF_1_INITFIRST
7444 | DF_1_NODELETE
7445 | DF_1_NOOPEN);
7446 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
7447 return false;
7448 }
7449
7450 if (elf_tdata (output_bfd)->cverrefs)
7451 {
7452 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
7453
7454 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
7455 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
7456 return false;
7457 }
7458
7459 if ((elf_tdata (output_bfd)->cverrefs == 0
7460 && elf_tdata (output_bfd)->cverdefs == 0)
7461 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
7462 {
7463 asection *s;
7464
7465 s = bfd_get_linker_section (dynobj, ".gnu.version");
7466 s->flags |= SEC_EXCLUDE;
7467 }
7468 }
7469 return true;
7470 }
7471
7472 /* Find the first non-excluded output section. We'll use its
7473 section symbol for some emitted relocs. */
7474 void
7475 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7476 {
7477 asection *s;
7478 asection *found = NULL;
7479
7480 for (s = output_bfd->sections; s != NULL; s = s->next)
7481 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7482 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7483 {
7484 found = s;
7485 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7486 break;
7487 }
7488 elf_hash_table (info)->text_index_section = found;
7489 }
7490
7491 /* Find two non-excluded output sections, one for code, one for data.
7492 We'll use their section symbols for some emitted relocs. */
7493 void
7494 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7495 {
7496 asection *s;
7497 asection *found = NULL;
7498
7499 /* Data first, since setting text_index_section changes
7500 _bfd_elf_omit_section_dynsym_default. */
7501 for (s = output_bfd->sections; s != NULL; s = s->next)
7502 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7503 && !(s->flags & SEC_READONLY)
7504 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7505 {
7506 found = s;
7507 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7508 break;
7509 }
7510 elf_hash_table (info)->data_index_section = found;
7511
7512 for (s = output_bfd->sections; s != NULL; s = s->next)
7513 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7514 && (s->flags & SEC_READONLY)
7515 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7516 {
7517 found = s;
7518 break;
7519 }
7520 elf_hash_table (info)->text_index_section = found;
7521 }
7522
7523 #define GNU_HASH_SECTION_NAME(bed) \
7524 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7525
7526 bool
7527 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7528 {
7529 const struct elf_backend_data *bed;
7530 unsigned long section_sym_count;
7531 bfd_size_type dynsymcount = 0;
7532
7533 if (!is_elf_hash_table (info->hash))
7534 return true;
7535
7536 bed = get_elf_backend_data (output_bfd);
7537 (*bed->elf_backend_init_index_section) (output_bfd, info);
7538
7539 /* Assign dynsym indices. In a shared library we generate a section
7540 symbol for each output section, which come first. Next come all
7541 of the back-end allocated local dynamic syms, followed by the rest
7542 of the global symbols.
7543
7544 This is usually not needed for static binaries, however backends
7545 can request to always do it, e.g. the MIPS backend uses dynamic
7546 symbol counts to lay out GOT, which will be produced in the
7547 presence of GOT relocations even in static binaries (holding fixed
7548 data in that case, to satisfy those relocations). */
7549
7550 if (elf_hash_table (info)->dynamic_sections_created
7551 || bed->always_renumber_dynsyms)
7552 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7553 §ion_sym_count);
7554
7555 if (elf_hash_table (info)->dynamic_sections_created)
7556 {
7557 bfd *dynobj;
7558 asection *s;
7559 unsigned int dtagcount;
7560
7561 dynobj = elf_hash_table (info)->dynobj;
7562
7563 /* Work out the size of the symbol version section. */
7564 s = bfd_get_linker_section (dynobj, ".gnu.version");
7565 BFD_ASSERT (s != NULL);
7566 if ((s->flags & SEC_EXCLUDE) == 0)
7567 {
7568 s->size = dynsymcount * sizeof (Elf_External_Versym);
7569 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7570 if (s->contents == NULL)
7571 return false;
7572
7573 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7574 return false;
7575 }
7576
7577 /* Set the size of the .dynsym and .hash sections. We counted
7578 the number of dynamic symbols in elf_link_add_object_symbols.
7579 We will build the contents of .dynsym and .hash when we build
7580 the final symbol table, because until then we do not know the
7581 correct value to give the symbols. We built the .dynstr
7582 section as we went along in elf_link_add_object_symbols. */
7583 s = elf_hash_table (info)->dynsym;
7584 BFD_ASSERT (s != NULL);
7585 s->size = dynsymcount * bed->s->sizeof_sym;
7586
7587 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7588 if (s->contents == NULL)
7589 return false;
7590
7591 /* The first entry in .dynsym is a dummy symbol. Clear all the
7592 section syms, in case we don't output them all. */
7593 ++section_sym_count;
7594 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
7595
7596 elf_hash_table (info)->bucketcount = 0;
7597
7598 /* Compute the size of the hashing table. As a side effect this
7599 computes the hash values for all the names we export. */
7600 if (info->emit_hash)
7601 {
7602 unsigned long int *hashcodes;
7603 struct hash_codes_info hashinf;
7604 bfd_size_type amt;
7605 unsigned long int nsyms;
7606 size_t bucketcount;
7607 size_t hash_entry_size;
7608
7609 /* Compute the hash values for all exported symbols. At the same
7610 time store the values in an array so that we could use them for
7611 optimizations. */
7612 amt = dynsymcount * sizeof (unsigned long int);
7613 hashcodes = (unsigned long int *) bfd_malloc (amt);
7614 if (hashcodes == NULL)
7615 return false;
7616 hashinf.hashcodes = hashcodes;
7617 hashinf.error = false;
7618
7619 /* Put all hash values in HASHCODES. */
7620 elf_link_hash_traverse (elf_hash_table (info),
7621 elf_collect_hash_codes, &hashinf);
7622 if (hashinf.error)
7623 {
7624 free (hashcodes);
7625 return false;
7626 }
7627
7628 nsyms = hashinf.hashcodes - hashcodes;
7629 bucketcount
7630 = compute_bucket_count (info, hashcodes, nsyms, 0);
7631 free (hashcodes);
7632
7633 if (bucketcount == 0 && nsyms > 0)
7634 return false;
7635
7636 elf_hash_table (info)->bucketcount = bucketcount;
7637
7638 s = bfd_get_linker_section (dynobj, ".hash");
7639 BFD_ASSERT (s != NULL);
7640 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7641 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
7642 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7643 if (s->contents == NULL)
7644 return false;
7645
7646 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7647 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7648 s->contents + hash_entry_size);
7649 }
7650
7651 if (info->emit_gnu_hash)
7652 {
7653 size_t i, cnt;
7654 unsigned char *contents;
7655 struct collect_gnu_hash_codes cinfo;
7656 bfd_size_type amt;
7657 size_t bucketcount;
7658
7659 memset (&cinfo, 0, sizeof (cinfo));
7660
7661 /* Compute the hash values for all exported symbols. At the same
7662 time store the values in an array so that we could use them for
7663 optimizations. */
7664 amt = dynsymcount * 2 * sizeof (unsigned long int);
7665 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
7666 if (cinfo.hashcodes == NULL)
7667 return false;
7668
7669 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7670 cinfo.min_dynindx = -1;
7671 cinfo.output_bfd = output_bfd;
7672 cinfo.bed = bed;
7673
7674 /* Put all hash values in HASHCODES. */
7675 elf_link_hash_traverse (elf_hash_table (info),
7676 elf_collect_gnu_hash_codes, &cinfo);
7677 if (cinfo.error)
7678 {
7679 free (cinfo.hashcodes);
7680 return false;
7681 }
7682
7683 bucketcount
7684 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7685
7686 if (bucketcount == 0)
7687 {
7688 free (cinfo.hashcodes);
7689 return false;
7690 }
7691
7692 s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (bed));
7693 BFD_ASSERT (s != NULL);
7694
7695 if (cinfo.nsyms == 0)
7696 {
7697 /* Empty .gnu.hash or .MIPS.xhash section is special. */
7698 BFD_ASSERT (cinfo.min_dynindx == -1);
7699 free (cinfo.hashcodes);
7700 s->size = 5 * 4 + bed->s->arch_size / 8;
7701 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7702 if (contents == NULL)
7703 return false;
7704 s->contents = contents;
7705 /* 1 empty bucket. */
7706 bfd_put_32 (output_bfd, 1, contents);
7707 /* SYMIDX above the special symbol 0. */
7708 bfd_put_32 (output_bfd, 1, contents + 4);
7709 /* Just one word for bitmask. */
7710 bfd_put_32 (output_bfd, 1, contents + 8);
7711 /* Only hash fn bloom filter. */
7712 bfd_put_32 (output_bfd, 0, contents + 12);
7713 /* No hashes are valid - empty bitmask. */
7714 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7715 /* No hashes in the only bucket. */
7716 bfd_put_32 (output_bfd, 0,
7717 contents + 16 + bed->s->arch_size / 8);
7718 }
7719 else
7720 {
7721 unsigned long int maskwords, maskbitslog2, x;
7722 BFD_ASSERT (cinfo.min_dynindx != -1);
7723
7724 x = cinfo.nsyms;
7725 maskbitslog2 = 1;
7726 while ((x >>= 1) != 0)
7727 ++maskbitslog2;
7728 if (maskbitslog2 < 3)
7729 maskbitslog2 = 5;
7730 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7731 maskbitslog2 = maskbitslog2 + 3;
7732 else
7733 maskbitslog2 = maskbitslog2 + 2;
7734 if (bed->s->arch_size == 64)
7735 {
7736 if (maskbitslog2 == 5)
7737 maskbitslog2 = 6;
7738 cinfo.shift1 = 6;
7739 }
7740 else
7741 cinfo.shift1 = 5;
7742 cinfo.mask = (1 << cinfo.shift1) - 1;
7743 cinfo.shift2 = maskbitslog2;
7744 cinfo.maskbits = 1 << maskbitslog2;
7745 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7746 amt = bucketcount * sizeof (unsigned long int) * 2;
7747 amt += maskwords * sizeof (bfd_vma);
7748 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
7749 if (cinfo.bitmask == NULL)
7750 {
7751 free (cinfo.hashcodes);
7752 return false;
7753 }
7754
7755 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
7756 cinfo.indx = cinfo.counts + bucketcount;
7757 cinfo.symindx = dynsymcount - cinfo.nsyms;
7758 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7759
7760 /* Determine how often each hash bucket is used. */
7761 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7762 for (i = 0; i < cinfo.nsyms; ++i)
7763 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7764
7765 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7766 if (cinfo.counts[i] != 0)
7767 {
7768 cinfo.indx[i] = cnt;
7769 cnt += cinfo.counts[i];
7770 }
7771 BFD_ASSERT (cnt == dynsymcount);
7772 cinfo.bucketcount = bucketcount;
7773 cinfo.local_indx = cinfo.min_dynindx;
7774
7775 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7776 s->size += cinfo.maskbits / 8;
7777 if (bed->record_xhash_symbol != NULL)
7778 s->size += cinfo.nsyms * 4;
7779 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7780 if (contents == NULL)
7781 {
7782 free (cinfo.bitmask);
7783 free (cinfo.hashcodes);
7784 return false;
7785 }
7786
7787 s->contents = contents;
7788 bfd_put_32 (output_bfd, bucketcount, contents);
7789 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7790 bfd_put_32 (output_bfd, maskwords, contents + 8);
7791 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7792 contents += 16 + cinfo.maskbits / 8;
7793
7794 for (i = 0; i < bucketcount; ++i)
7795 {
7796 if (cinfo.counts[i] == 0)
7797 bfd_put_32 (output_bfd, 0, contents);
7798 else
7799 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7800 contents += 4;
7801 }
7802
7803 cinfo.contents = contents;
7804
7805 cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents;
7806 /* Renumber dynamic symbols, if populating .gnu.hash section.
7807 If using .MIPS.xhash, populate the translation table. */
7808 elf_link_hash_traverse (elf_hash_table (info),
7809 elf_gnu_hash_process_symidx, &cinfo);
7810
7811 contents = s->contents + 16;
7812 for (i = 0; i < maskwords; ++i)
7813 {
7814 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7815 contents);
7816 contents += bed->s->arch_size / 8;
7817 }
7818
7819 free (cinfo.bitmask);
7820 free (cinfo.hashcodes);
7821 }
7822 }
7823
7824 s = bfd_get_linker_section (dynobj, ".dynstr");
7825 BFD_ASSERT (s != NULL);
7826
7827 elf_finalize_dynstr (output_bfd, info);
7828
7829 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7830
7831 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7832 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7833 return false;
7834 }
7835
7836 return true;
7837 }
7838
7839 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7841
7842 static void
7843 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7844 asection *sec)
7845 {
7846 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7847 sec->sec_info_type = SEC_INFO_TYPE_NONE;
7848 }
7849
7850 /* Finish SHF_MERGE section merging. */
7851
7852 bool
7853 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
7854 {
7855 bfd *ibfd;
7856 asection *sec;
7857
7858 if (!is_elf_hash_table (info->hash))
7859 return false;
7860
7861 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7862 if ((ibfd->flags & DYNAMIC) == 0
7863 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
7864 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7865 == get_elf_backend_data (obfd)->s->elfclass))
7866 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7867 if ((sec->flags & SEC_MERGE) != 0
7868 && !bfd_is_abs_section (sec->output_section))
7869 {
7870 struct bfd_elf_section_data *secdata;
7871
7872 secdata = elf_section_data (sec);
7873 if (! _bfd_add_merge_section (obfd,
7874 &elf_hash_table (info)->merge_info,
7875 sec, &secdata->sec_info))
7876 return false;
7877 else if (secdata->sec_info)
7878 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
7879 }
7880
7881 if (elf_hash_table (info)->merge_info != NULL)
7882 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
7883 merge_sections_remove_hook);
7884 return true;
7885 }
7886
7887 /* Create an entry in an ELF linker hash table. */
7888
7889 struct bfd_hash_entry *
7890 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7891 struct bfd_hash_table *table,
7892 const char *string)
7893 {
7894 /* Allocate the structure if it has not already been allocated by a
7895 subclass. */
7896 if (entry == NULL)
7897 {
7898 entry = (struct bfd_hash_entry *)
7899 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
7900 if (entry == NULL)
7901 return entry;
7902 }
7903
7904 /* Call the allocation method of the superclass. */
7905 entry = _bfd_link_hash_newfunc (entry, table, string);
7906 if (entry != NULL)
7907 {
7908 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7909 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7910
7911 /* Set local fields. */
7912 ret->indx = -1;
7913 ret->dynindx = -1;
7914 ret->got = htab->init_got_refcount;
7915 ret->plt = htab->init_plt_refcount;
7916 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7917 - offsetof (struct elf_link_hash_entry, size)));
7918 /* Assume that we have been called by a non-ELF symbol reader.
7919 This flag is then reset by the code which reads an ELF input
7920 file. This ensures that a symbol created by a non-ELF symbol
7921 reader will have the flag set correctly. */
7922 ret->non_elf = 1;
7923 }
7924
7925 return entry;
7926 }
7927
7928 /* Copy data from an indirect symbol to its direct symbol, hiding the
7929 old indirect symbol. Also used for copying flags to a weakdef. */
7930
7931 void
7932 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7933 struct elf_link_hash_entry *dir,
7934 struct elf_link_hash_entry *ind)
7935 {
7936 struct elf_link_hash_table *htab;
7937
7938 if (ind->dyn_relocs != NULL)
7939 {
7940 if (dir->dyn_relocs != NULL)
7941 {
7942 struct elf_dyn_relocs **pp;
7943 struct elf_dyn_relocs *p;
7944
7945 /* Add reloc counts against the indirect sym to the direct sym
7946 list. Merge any entries against the same section. */
7947 for (pp = &ind->dyn_relocs; (p = *pp) != NULL; )
7948 {
7949 struct elf_dyn_relocs *q;
7950
7951 for (q = dir->dyn_relocs; q != NULL; q = q->next)
7952 if (q->sec == p->sec)
7953 {
7954 q->pc_count += p->pc_count;
7955 q->count += p->count;
7956 *pp = p->next;
7957 break;
7958 }
7959 if (q == NULL)
7960 pp = &p->next;
7961 }
7962 *pp = dir->dyn_relocs;
7963 }
7964
7965 dir->dyn_relocs = ind->dyn_relocs;
7966 ind->dyn_relocs = NULL;
7967 }
7968
7969 /* Copy down any references that we may have already seen to the
7970 symbol which just became indirect. */
7971
7972 if (dir->versioned != versioned_hidden)
7973 dir->ref_dynamic |= ind->ref_dynamic;
7974 dir->ref_regular |= ind->ref_regular;
7975 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7976 dir->non_got_ref |= ind->non_got_ref;
7977 dir->needs_plt |= ind->needs_plt;
7978 dir->pointer_equality_needed |= ind->pointer_equality_needed;
7979
7980 if (ind->root.type != bfd_link_hash_indirect)
7981 return;
7982
7983 /* Copy over the global and procedure linkage table refcount entries.
7984 These may have been already set up by a check_relocs routine. */
7985 htab = elf_hash_table (info);
7986 if (ind->got.refcount > htab->init_got_refcount.refcount)
7987 {
7988 if (dir->got.refcount < 0)
7989 dir->got.refcount = 0;
7990 dir->got.refcount += ind->got.refcount;
7991 ind->got.refcount = htab->init_got_refcount.refcount;
7992 }
7993
7994 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7995 {
7996 if (dir->plt.refcount < 0)
7997 dir->plt.refcount = 0;
7998 dir->plt.refcount += ind->plt.refcount;
7999 ind->plt.refcount = htab->init_plt_refcount.refcount;
8000 }
8001
8002 if (ind->dynindx != -1)
8003 {
8004 if (dir->dynindx != -1)
8005 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
8006 dir->dynindx = ind->dynindx;
8007 dir->dynstr_index = ind->dynstr_index;
8008 ind->dynindx = -1;
8009 ind->dynstr_index = 0;
8010 }
8011 }
8012
8013 void
8014 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
8015 struct elf_link_hash_entry *h,
8016 bool force_local)
8017 {
8018 /* STT_GNU_IFUNC symbol must go through PLT. */
8019 if (h->type != STT_GNU_IFUNC)
8020 {
8021 h->plt = elf_hash_table (info)->init_plt_offset;
8022 h->needs_plt = 0;
8023 }
8024 if (force_local)
8025 {
8026 h->forced_local = 1;
8027 if (h->dynindx != -1)
8028 {
8029 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8030 h->dynstr_index);
8031 h->dynindx = -1;
8032 h->dynstr_index = 0;
8033 }
8034 }
8035 }
8036
8037 /* Hide a symbol. */
8038
8039 void
8040 _bfd_elf_link_hide_symbol (bfd *output_bfd,
8041 struct bfd_link_info *info,
8042 struct bfd_link_hash_entry *h)
8043 {
8044 if (is_elf_hash_table (info->hash))
8045 {
8046 const struct elf_backend_data *bed
8047 = get_elf_backend_data (output_bfd);
8048 struct elf_link_hash_entry *eh
8049 = (struct elf_link_hash_entry *) h;
8050 bed->elf_backend_hide_symbol (info, eh, true);
8051 eh->def_dynamic = 0;
8052 eh->ref_dynamic = 0;
8053 eh->dynamic_def = 0;
8054 }
8055 }
8056
8057 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
8058 caller. */
8059
8060 bool
8061 _bfd_elf_link_hash_table_init
8062 (struct elf_link_hash_table *table,
8063 bfd *abfd,
8064 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
8065 struct bfd_hash_table *,
8066 const char *),
8067 unsigned int entsize,
8068 enum elf_target_id target_id)
8069 {
8070 bool ret;
8071 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
8072
8073 table->init_got_refcount.refcount = can_refcount - 1;
8074 table->init_plt_refcount.refcount = can_refcount - 1;
8075 table->init_got_offset.offset = -(bfd_vma) 1;
8076 table->init_plt_offset.offset = -(bfd_vma) 1;
8077 /* The first dynamic symbol is a dummy. */
8078 table->dynsymcount = 1;
8079
8080 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
8081
8082 table->root.type = bfd_link_elf_hash_table;
8083 table->hash_table_id = target_id;
8084 table->target_os = get_elf_backend_data (abfd)->target_os;
8085
8086 return ret;
8087 }
8088
8089 /* Create an ELF linker hash table. */
8090
8091 struct bfd_link_hash_table *
8092 _bfd_elf_link_hash_table_create (bfd *abfd)
8093 {
8094 struct elf_link_hash_table *ret;
8095 size_t amt = sizeof (struct elf_link_hash_table);
8096
8097 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
8098 if (ret == NULL)
8099 return NULL;
8100
8101 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
8102 sizeof (struct elf_link_hash_entry),
8103 GENERIC_ELF_DATA))
8104 {
8105 free (ret);
8106 return NULL;
8107 }
8108 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
8109
8110 return &ret->root;
8111 }
8112
8113 /* Destroy an ELF linker hash table. */
8114
8115 void
8116 _bfd_elf_link_hash_table_free (bfd *obfd)
8117 {
8118 struct elf_link_hash_table *htab;
8119
8120 htab = (struct elf_link_hash_table *) obfd->link.hash;
8121 if (htab->dynstr != NULL)
8122 _bfd_elf_strtab_free (htab->dynstr);
8123 _bfd_merge_sections_free (htab->merge_info);
8124 _bfd_generic_link_hash_table_free (obfd);
8125 }
8126
8127 /* This is a hook for the ELF emulation code in the generic linker to
8128 tell the backend linker what file name to use for the DT_NEEDED
8129 entry for a dynamic object. */
8130
8131 void
8132 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
8133 {
8134 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8135 && bfd_get_format (abfd) == bfd_object)
8136 elf_dt_name (abfd) = name;
8137 }
8138
8139 int
8140 bfd_elf_get_dyn_lib_class (bfd *abfd)
8141 {
8142 int lib_class;
8143 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8144 && bfd_get_format (abfd) == bfd_object)
8145 lib_class = elf_dyn_lib_class (abfd);
8146 else
8147 lib_class = 0;
8148 return lib_class;
8149 }
8150
8151 void
8152 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
8153 {
8154 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8155 && bfd_get_format (abfd) == bfd_object)
8156 elf_dyn_lib_class (abfd) = lib_class;
8157 }
8158
8159 /* Get the list of DT_NEEDED entries for a link. This is a hook for
8160 the linker ELF emulation code. */
8161
8162 struct bfd_link_needed_list *
8163 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
8164 struct bfd_link_info *info)
8165 {
8166 if (! is_elf_hash_table (info->hash))
8167 return NULL;
8168 return elf_hash_table (info)->needed;
8169 }
8170
8171 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
8172 hook for the linker ELF emulation code. */
8173
8174 struct bfd_link_needed_list *
8175 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
8176 struct bfd_link_info *info)
8177 {
8178 if (! is_elf_hash_table (info->hash))
8179 return NULL;
8180 return elf_hash_table (info)->runpath;
8181 }
8182
8183 /* Get the name actually used for a dynamic object for a link. This
8184 is the SONAME entry if there is one. Otherwise, it is the string
8185 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
8186
8187 const char *
8188 bfd_elf_get_dt_soname (bfd *abfd)
8189 {
8190 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8191 && bfd_get_format (abfd) == bfd_object)
8192 return elf_dt_name (abfd);
8193 return NULL;
8194 }
8195
8196 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
8197 the ELF linker emulation code. */
8198
8199 bool
8200 bfd_elf_get_bfd_needed_list (bfd *abfd,
8201 struct bfd_link_needed_list **pneeded)
8202 {
8203 asection *s;
8204 bfd_byte *dynbuf = NULL;
8205 unsigned int elfsec;
8206 unsigned long shlink;
8207 bfd_byte *extdyn, *extdynend;
8208 size_t extdynsize;
8209 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
8210
8211 *pneeded = NULL;
8212
8213 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
8214 || bfd_get_format (abfd) != bfd_object)
8215 return true;
8216
8217 s = bfd_get_section_by_name (abfd, ".dynamic");
8218 if (s == NULL || s->size == 0)
8219 return true;
8220
8221 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
8222 goto error_return;
8223
8224 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
8225 if (elfsec == SHN_BAD)
8226 goto error_return;
8227
8228 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
8229
8230 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
8231 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
8232
8233 extdyn = dynbuf;
8234 extdynend = extdyn + s->size;
8235 for (; extdyn < extdynend; extdyn += extdynsize)
8236 {
8237 Elf_Internal_Dyn dyn;
8238
8239 (*swap_dyn_in) (abfd, extdyn, &dyn);
8240
8241 if (dyn.d_tag == DT_NULL)
8242 break;
8243
8244 if (dyn.d_tag == DT_NEEDED)
8245 {
8246 const char *string;
8247 struct bfd_link_needed_list *l;
8248 unsigned int tagv = dyn.d_un.d_val;
8249 size_t amt;
8250
8251 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
8252 if (string == NULL)
8253 goto error_return;
8254
8255 amt = sizeof *l;
8256 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
8257 if (l == NULL)
8258 goto error_return;
8259
8260 l->by = abfd;
8261 l->name = string;
8262 l->next = *pneeded;
8263 *pneeded = l;
8264 }
8265 }
8266
8267 free (dynbuf);
8268
8269 return true;
8270
8271 error_return:
8272 free (dynbuf);
8273 return false;
8274 }
8275
8276 struct elf_symbuf_symbol
8277 {
8278 unsigned long st_name; /* Symbol name, index in string tbl */
8279 unsigned char st_info; /* Type and binding attributes */
8280 unsigned char st_other; /* Visibilty, and target specific */
8281 };
8282
8283 struct elf_symbuf_head
8284 {
8285 struct elf_symbuf_symbol *ssym;
8286 size_t count;
8287 unsigned int st_shndx;
8288 };
8289
8290 struct elf_symbol
8291 {
8292 union
8293 {
8294 Elf_Internal_Sym *isym;
8295 struct elf_symbuf_symbol *ssym;
8296 void *p;
8297 } u;
8298 const char *name;
8299 };
8300
8301 /* Sort references to symbols by ascending section number. */
8302
8303 static int
8304 elf_sort_elf_symbol (const void *arg1, const void *arg2)
8305 {
8306 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
8307 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
8308
8309 if (s1->st_shndx != s2->st_shndx)
8310 return s1->st_shndx > s2->st_shndx ? 1 : -1;
8311 /* Final sort by the address of the sym in the symbuf ensures
8312 a stable sort. */
8313 if (s1 != s2)
8314 return s1 > s2 ? 1 : -1;
8315 return 0;
8316 }
8317
8318 static int
8319 elf_sym_name_compare (const void *arg1, const void *arg2)
8320 {
8321 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
8322 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
8323 int ret = strcmp (s1->name, s2->name);
8324 if (ret != 0)
8325 return ret;
8326 if (s1->u.p != s2->u.p)
8327 return s1->u.p > s2->u.p ? 1 : -1;
8328 return 0;
8329 }
8330
8331 static struct elf_symbuf_head *
8332 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
8333 {
8334 Elf_Internal_Sym **ind, **indbufend, **indbuf;
8335 struct elf_symbuf_symbol *ssym;
8336 struct elf_symbuf_head *ssymbuf, *ssymhead;
8337 size_t i, shndx_count, total_size, amt;
8338
8339 amt = symcount * sizeof (*indbuf);
8340 indbuf = (Elf_Internal_Sym **) bfd_malloc (amt);
8341 if (indbuf == NULL)
8342 return NULL;
8343
8344 for (ind = indbuf, i = 0; i < symcount; i++)
8345 if (isymbuf[i].st_shndx != SHN_UNDEF)
8346 *ind++ = &isymbuf[i];
8347 indbufend = ind;
8348
8349 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
8350 elf_sort_elf_symbol);
8351
8352 shndx_count = 0;
8353 if (indbufend > indbuf)
8354 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
8355 if (ind[0]->st_shndx != ind[1]->st_shndx)
8356 shndx_count++;
8357
8358 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
8359 + (indbufend - indbuf) * sizeof (*ssym));
8360 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
8361 if (ssymbuf == NULL)
8362 {
8363 free (indbuf);
8364 return NULL;
8365 }
8366
8367 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
8368 ssymbuf->ssym = NULL;
8369 ssymbuf->count = shndx_count;
8370 ssymbuf->st_shndx = 0;
8371 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
8372 {
8373 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
8374 {
8375 ssymhead++;
8376 ssymhead->ssym = ssym;
8377 ssymhead->count = 0;
8378 ssymhead->st_shndx = (*ind)->st_shndx;
8379 }
8380 ssym->st_name = (*ind)->st_name;
8381 ssym->st_info = (*ind)->st_info;
8382 ssym->st_other = (*ind)->st_other;
8383 ssymhead->count++;
8384 }
8385 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
8386 && (uintptr_t) ssym - (uintptr_t) ssymbuf == total_size);
8387
8388 free (indbuf);
8389 return ssymbuf;
8390 }
8391
8392 /* Check if 2 sections define the same set of local and global
8393 symbols. */
8394
8395 static bool
8396 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
8397 struct bfd_link_info *info)
8398 {
8399 bfd *bfd1, *bfd2;
8400 const struct elf_backend_data *bed1, *bed2;
8401 Elf_Internal_Shdr *hdr1, *hdr2;
8402 size_t symcount1, symcount2;
8403 Elf_Internal_Sym *isymbuf1, *isymbuf2;
8404 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
8405 Elf_Internal_Sym *isym, *isymend;
8406 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
8407 size_t count1, count2, sec_count1, sec_count2, i;
8408 unsigned int shndx1, shndx2;
8409 bool result;
8410 bool ignore_section_symbol_p;
8411
8412 bfd1 = sec1->owner;
8413 bfd2 = sec2->owner;
8414
8415 /* Both sections have to be in ELF. */
8416 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
8417 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
8418 return false;
8419
8420 if (elf_section_type (sec1) != elf_section_type (sec2))
8421 return false;
8422
8423 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
8424 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
8425 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
8426 return false;
8427
8428 bed1 = get_elf_backend_data (bfd1);
8429 bed2 = get_elf_backend_data (bfd2);
8430 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
8431 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
8432 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
8433 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
8434
8435 if (symcount1 == 0 || symcount2 == 0)
8436 return false;
8437
8438 result = false;
8439 isymbuf1 = NULL;
8440 isymbuf2 = NULL;
8441 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
8442 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
8443
8444 /* Ignore section symbols only when matching non-debugging sections
8445 or linkonce section with comdat section. */
8446 ignore_section_symbol_p
8447 = ((sec1->flags & SEC_DEBUGGING) == 0
8448 || ((elf_section_flags (sec1) & SHF_GROUP)
8449 != (elf_section_flags (sec2) & SHF_GROUP)));
8450
8451 if (ssymbuf1 == NULL)
8452 {
8453 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
8454 NULL, NULL, NULL);
8455 if (isymbuf1 == NULL)
8456 goto done;
8457
8458 if (info != NULL && !info->reduce_memory_overheads)
8459 {
8460 ssymbuf1 = elf_create_symbuf (symcount1, isymbuf1);
8461 elf_tdata (bfd1)->symbuf = ssymbuf1;
8462 }
8463 }
8464
8465 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
8466 {
8467 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
8468 NULL, NULL, NULL);
8469 if (isymbuf2 == NULL)
8470 goto done;
8471
8472 if (ssymbuf1 != NULL && info != NULL && !info->reduce_memory_overheads)
8473 {
8474 ssymbuf2 = elf_create_symbuf (symcount2, isymbuf2);
8475 elf_tdata (bfd2)->symbuf = ssymbuf2;
8476 }
8477 }
8478
8479 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
8480 {
8481 /* Optimized faster version. */
8482 size_t lo, hi, mid;
8483 struct elf_symbol *symp;
8484 struct elf_symbuf_symbol *ssym, *ssymend;
8485
8486 lo = 0;
8487 hi = ssymbuf1->count;
8488 ssymbuf1++;
8489 count1 = 0;
8490 sec_count1 = 0;
8491 while (lo < hi)
8492 {
8493 mid = (lo + hi) / 2;
8494 if (shndx1 < ssymbuf1[mid].st_shndx)
8495 hi = mid;
8496 else if (shndx1 > ssymbuf1[mid].st_shndx)
8497 lo = mid + 1;
8498 else
8499 {
8500 count1 = ssymbuf1[mid].count;
8501 ssymbuf1 += mid;
8502 break;
8503 }
8504 }
8505 if (ignore_section_symbol_p)
8506 {
8507 for (i = 0; i < count1; i++)
8508 if (ELF_ST_TYPE (ssymbuf1->ssym[i].st_info) == STT_SECTION)
8509 sec_count1++;
8510 count1 -= sec_count1;
8511 }
8512
8513 lo = 0;
8514 hi = ssymbuf2->count;
8515 ssymbuf2++;
8516 count2 = 0;
8517 sec_count2 = 0;
8518 while (lo < hi)
8519 {
8520 mid = (lo + hi) / 2;
8521 if (shndx2 < ssymbuf2[mid].st_shndx)
8522 hi = mid;
8523 else if (shndx2 > ssymbuf2[mid].st_shndx)
8524 lo = mid + 1;
8525 else
8526 {
8527 count2 = ssymbuf2[mid].count;
8528 ssymbuf2 += mid;
8529 break;
8530 }
8531 }
8532 if (ignore_section_symbol_p)
8533 {
8534 for (i = 0; i < count2; i++)
8535 if (ELF_ST_TYPE (ssymbuf2->ssym[i].st_info) == STT_SECTION)
8536 sec_count2++;
8537 count2 -= sec_count2;
8538 }
8539
8540 if (count1 == 0 || count2 == 0 || count1 != count2)
8541 goto done;
8542
8543 symtable1
8544 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
8545 symtable2
8546 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
8547 if (symtable1 == NULL || symtable2 == NULL)
8548 goto done;
8549
8550 symp = symtable1;
8551 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1 + sec_count1;
8552 ssym < ssymend; ssym++)
8553 if (sec_count1 == 0
8554 || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
8555 {
8556 symp->u.ssym = ssym;
8557 symp->name = bfd_elf_string_from_elf_section (bfd1,
8558 hdr1->sh_link,
8559 ssym->st_name);
8560 symp++;
8561 }
8562
8563 symp = symtable2;
8564 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2 + sec_count2;
8565 ssym < ssymend; ssym++)
8566 if (sec_count2 == 0
8567 || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
8568 {
8569 symp->u.ssym = ssym;
8570 symp->name = bfd_elf_string_from_elf_section (bfd2,
8571 hdr2->sh_link,
8572 ssym->st_name);
8573 symp++;
8574 }
8575
8576 /* Sort symbol by name. */
8577 qsort (symtable1, count1, sizeof (struct elf_symbol),
8578 elf_sym_name_compare);
8579 qsort (symtable2, count1, sizeof (struct elf_symbol),
8580 elf_sym_name_compare);
8581
8582 for (i = 0; i < count1; i++)
8583 /* Two symbols must have the same binding, type and name. */
8584 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8585 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8586 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8587 goto done;
8588
8589 result = true;
8590 goto done;
8591 }
8592
8593 symtable1 = (struct elf_symbol *)
8594 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8595 symtable2 = (struct elf_symbol *)
8596 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
8597 if (symtable1 == NULL || symtable2 == NULL)
8598 goto done;
8599
8600 /* Count definitions in the section. */
8601 count1 = 0;
8602 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
8603 if (isym->st_shndx == shndx1
8604 && (!ignore_section_symbol_p
8605 || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
8606 symtable1[count1++].u.isym = isym;
8607
8608 count2 = 0;
8609 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
8610 if (isym->st_shndx == shndx2
8611 && (!ignore_section_symbol_p
8612 || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
8613 symtable2[count2++].u.isym = isym;
8614
8615 if (count1 == 0 || count2 == 0 || count1 != count2)
8616 goto done;
8617
8618 for (i = 0; i < count1; i++)
8619 symtable1[i].name
8620 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8621 symtable1[i].u.isym->st_name);
8622
8623 for (i = 0; i < count2; i++)
8624 symtable2[i].name
8625 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8626 symtable2[i].u.isym->st_name);
8627
8628 /* Sort symbol by name. */
8629 qsort (symtable1, count1, sizeof (struct elf_symbol),
8630 elf_sym_name_compare);
8631 qsort (symtable2, count1, sizeof (struct elf_symbol),
8632 elf_sym_name_compare);
8633
8634 for (i = 0; i < count1; i++)
8635 /* Two symbols must have the same binding, type and name. */
8636 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8637 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8638 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8639 goto done;
8640
8641 result = true;
8642
8643 done:
8644 free (symtable1);
8645 free (symtable2);
8646 free (isymbuf1);
8647 free (isymbuf2);
8648
8649 return result;
8650 }
8651
8652 /* Return TRUE if 2 section types are compatible. */
8653
8654 bool
8655 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8656 bfd *bbfd, const asection *bsec)
8657 {
8658 if (asec == NULL
8659 || bsec == NULL
8660 || abfd->xvec->flavour != bfd_target_elf_flavour
8661 || bbfd->xvec->flavour != bfd_target_elf_flavour)
8662 return true;
8663
8664 return elf_section_type (asec) == elf_section_type (bsec);
8665 }
8666
8667 /* Final phase of ELF linker. */
8669
8670 /* A structure we use to avoid passing large numbers of arguments. */
8671
8672 struct elf_final_link_info
8673 {
8674 /* General link information. */
8675 struct bfd_link_info *info;
8676 /* Output BFD. */
8677 bfd *output_bfd;
8678 /* Symbol string table. */
8679 struct elf_strtab_hash *symstrtab;
8680 /* .hash section. */
8681 asection *hash_sec;
8682 /* symbol version section (.gnu.version). */
8683 asection *symver_sec;
8684 /* Buffer large enough to hold contents of any section. */
8685 bfd_byte *contents;
8686 /* Buffer large enough to hold external relocs of any section. */
8687 void *external_relocs;
8688 /* Buffer large enough to hold internal relocs of any section. */
8689 Elf_Internal_Rela *internal_relocs;
8690 /* Buffer large enough to hold external local symbols of any input
8691 BFD. */
8692 bfd_byte *external_syms;
8693 /* And a buffer for symbol section indices. */
8694 Elf_External_Sym_Shndx *locsym_shndx;
8695 /* Buffer large enough to hold internal local symbols of any input
8696 BFD. */
8697 Elf_Internal_Sym *internal_syms;
8698 /* Array large enough to hold a symbol index for each local symbol
8699 of any input BFD. */
8700 long *indices;
8701 /* Array large enough to hold a section pointer for each local
8702 symbol of any input BFD. */
8703 asection **sections;
8704 /* Buffer for SHT_SYMTAB_SHNDX section. */
8705 Elf_External_Sym_Shndx *symshndxbuf;
8706 /* Number of STT_FILE syms seen. */
8707 size_t filesym_count;
8708 /* Local symbol hash table. */
8709 struct bfd_hash_table local_hash_table;
8710 };
8711
8712 struct local_hash_entry
8713 {
8714 /* Base hash table entry structure. */
8715 struct bfd_hash_entry root;
8716 /* Size of the local symbol name. */
8717 size_t size;
8718 /* Number of the duplicated local symbol names. */
8719 long count;
8720 };
8721
8722 /* Create an entry in the local symbol hash table. */
8723
8724 static struct bfd_hash_entry *
8725 local_hash_newfunc (struct bfd_hash_entry *entry,
8726 struct bfd_hash_table *table,
8727 const char *string)
8728 {
8729
8730 /* Allocate the structure if it has not already been allocated by a
8731 subclass. */
8732 if (entry == NULL)
8733 {
8734 entry = bfd_hash_allocate (table,
8735 sizeof (struct local_hash_entry));
8736 if (entry == NULL)
8737 return entry;
8738 }
8739
8740 /* Call the allocation method of the superclass. */
8741 entry = bfd_hash_newfunc (entry, table, string);
8742 if (entry != NULL)
8743 {
8744 ((struct local_hash_entry *) entry)->count = 0;
8745 ((struct local_hash_entry *) entry)->size = 0;
8746 }
8747
8748 return entry;
8749 }
8750
8751 /* This struct is used to pass information to elf_link_output_extsym. */
8752
8753 struct elf_outext_info
8754 {
8755 bool failed;
8756 bool localsyms;
8757 bool file_sym_done;
8758 struct elf_final_link_info *flinfo;
8759 };
8760
8761
8762 /* Support for evaluating a complex relocation.
8763
8764 Complex relocations are generalized, self-describing relocations. The
8765 implementation of them consists of two parts: complex symbols, and the
8766 relocations themselves.
8767
8768 The relocations use a reserved elf-wide relocation type code (R_RELC
8769 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8770 information (start bit, end bit, word width, etc) into the addend. This
8771 information is extracted from CGEN-generated operand tables within gas.
8772
8773 Complex symbols are mangled symbols (STT_RELC external / BSF_RELC
8774 internal) representing prefix-notation expressions, including but not
8775 limited to those sorts of expressions normally encoded as addends in the
8776 addend field. The symbol mangling format is:
8777
8778 <node> := <literal>
8779 | <unary-operator> ':' <node>
8780 | <binary-operator> ':' <node> ':' <node>
8781 ;
8782
8783 <literal> := 's' <digits=N> ':' <N character symbol name>
8784 | 'S' <digits=N> ':' <N character section name>
8785 | '#' <hexdigits>
8786 ;
8787
8788 <binary-operator> := as in C
8789 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8790
8791 static void
8792 set_symbol_value (bfd *bfd_with_globals,
8793 Elf_Internal_Sym *isymbuf,
8794 size_t locsymcount,
8795 size_t symidx,
8796 bfd_vma val)
8797 {
8798 struct elf_link_hash_entry **sym_hashes;
8799 struct elf_link_hash_entry *h;
8800 size_t extsymoff = locsymcount;
8801
8802 if (symidx < locsymcount)
8803 {
8804 Elf_Internal_Sym *sym;
8805
8806 sym = isymbuf + symidx;
8807 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8808 {
8809 /* It is a local symbol: move it to the
8810 "absolute" section and give it a value. */
8811 sym->st_shndx = SHN_ABS;
8812 sym->st_value = val;
8813 return;
8814 }
8815 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8816 extsymoff = 0;
8817 }
8818
8819 /* It is a global symbol: set its link type
8820 to "defined" and give it a value. */
8821
8822 sym_hashes = elf_sym_hashes (bfd_with_globals);
8823 h = sym_hashes [symidx - extsymoff];
8824 while (h->root.type == bfd_link_hash_indirect
8825 || h->root.type == bfd_link_hash_warning)
8826 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8827 h->root.type = bfd_link_hash_defined;
8828 h->root.u.def.value = val;
8829 h->root.u.def.section = bfd_abs_section_ptr;
8830 }
8831
8832 static bool
8833 resolve_symbol (const char *name,
8834 bfd *input_bfd,
8835 struct elf_final_link_info *flinfo,
8836 bfd_vma *result,
8837 Elf_Internal_Sym *isymbuf,
8838 size_t locsymcount)
8839 {
8840 Elf_Internal_Sym *sym;
8841 struct bfd_link_hash_entry *global_entry;
8842 const char *candidate = NULL;
8843 Elf_Internal_Shdr *symtab_hdr;
8844 size_t i;
8845
8846 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8847
8848 for (i = 0; i < locsymcount; ++ i)
8849 {
8850 sym = isymbuf + i;
8851
8852 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8853 continue;
8854
8855 candidate = bfd_elf_string_from_elf_section (input_bfd,
8856 symtab_hdr->sh_link,
8857 sym->st_name);
8858 #ifdef DEBUG
8859 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8860 name, candidate, (unsigned long) sym->st_value);
8861 #endif
8862 if (candidate && strcmp (candidate, name) == 0)
8863 {
8864 asection *sec = flinfo->sections [i];
8865
8866 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8867 *result += sec->output_offset + sec->output_section->vma;
8868 #ifdef DEBUG
8869 printf ("Found symbol with value %8.8lx\n",
8870 (unsigned long) *result);
8871 #endif
8872 return true;
8873 }
8874 }
8875
8876 /* Hmm, haven't found it yet. perhaps it is a global. */
8877 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
8878 false, false, true);
8879 if (!global_entry)
8880 return false;
8881
8882 if (global_entry->type == bfd_link_hash_defined
8883 || global_entry->type == bfd_link_hash_defweak)
8884 {
8885 *result = (global_entry->u.def.value
8886 + global_entry->u.def.section->output_section->vma
8887 + global_entry->u.def.section->output_offset);
8888 #ifdef DEBUG
8889 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8890 global_entry->root.string, (unsigned long) *result);
8891 #endif
8892 return true;
8893 }
8894
8895 return false;
8896 }
8897
8898 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8899 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8900 names like "foo.end" which is the end address of section "foo". */
8901
8902 static bool
8903 resolve_section (const char *name,
8904 asection *sections,
8905 bfd_vma *result,
8906 bfd * abfd)
8907 {
8908 asection *curr;
8909 unsigned int len;
8910
8911 for (curr = sections; curr; curr = curr->next)
8912 if (strcmp (curr->name, name) == 0)
8913 {
8914 *result = curr->vma;
8915 return true;
8916 }
8917
8918 /* Hmm. still haven't found it. try pseudo-section names. */
8919 /* FIXME: This could be coded more efficiently... */
8920 for (curr = sections; curr; curr = curr->next)
8921 {
8922 len = strlen (curr->name);
8923 if (len > strlen (name))
8924 continue;
8925
8926 if (strncmp (curr->name, name, len) == 0)
8927 {
8928 if (startswith (name + len, ".end"))
8929 {
8930 *result = (curr->vma
8931 + curr->size / bfd_octets_per_byte (abfd, curr));
8932 return true;
8933 }
8934
8935 /* Insert more pseudo-section names here, if you like. */
8936 }
8937 }
8938
8939 return false;
8940 }
8941
8942 static void
8943 undefined_reference (const char *reftype, const char *name)
8944 {
8945 /* xgettext:c-format */
8946 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8947 reftype, name);
8948 bfd_set_error (bfd_error_bad_value);
8949 }
8950
8951 static bool
8952 eval_symbol (bfd_vma *result,
8953 const char **symp,
8954 bfd *input_bfd,
8955 struct elf_final_link_info *flinfo,
8956 bfd_vma dot,
8957 Elf_Internal_Sym *isymbuf,
8958 size_t locsymcount,
8959 int signed_p)
8960 {
8961 size_t len;
8962 size_t symlen;
8963 bfd_vma a;
8964 bfd_vma b;
8965 char symbuf[4096];
8966 const char *sym = *symp;
8967 const char *symend;
8968 bool symbol_is_section = false;
8969
8970 len = strlen (sym);
8971 symend = sym + len;
8972
8973 if (len < 1 || len > sizeof (symbuf))
8974 {
8975 bfd_set_error (bfd_error_invalid_operation);
8976 return false;
8977 }
8978
8979 switch (* sym)
8980 {
8981 case '.':
8982 *result = dot;
8983 *symp = sym + 1;
8984 return true;
8985
8986 case '#':
8987 ++sym;
8988 *result = strtoul (sym, (char **) symp, 16);
8989 return true;
8990
8991 case 'S':
8992 symbol_is_section = true;
8993 /* Fall through. */
8994 case 's':
8995 ++sym;
8996 symlen = strtol (sym, (char **) symp, 10);
8997 sym = *symp + 1; /* Skip the trailing ':'. */
8998
8999 if (symend < sym || symlen + 1 > sizeof (symbuf))
9000 {
9001 bfd_set_error (bfd_error_invalid_operation);
9002 return false;
9003 }
9004
9005 memcpy (symbuf, sym, symlen);
9006 symbuf[symlen] = '\0';
9007 *symp = sym + symlen;
9008
9009 /* Is it always possible, with complex symbols, that gas "mis-guessed"
9010 the symbol as a section, or vice-versa. so we're pretty liberal in our
9011 interpretation here; section means "try section first", not "must be a
9012 section", and likewise with symbol. */
9013
9014 if (symbol_is_section)
9015 {
9016 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
9017 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
9018 isymbuf, locsymcount))
9019 {
9020 undefined_reference ("section", symbuf);
9021 return false;
9022 }
9023 }
9024 else
9025 {
9026 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
9027 isymbuf, locsymcount)
9028 && !resolve_section (symbuf, flinfo->output_bfd->sections,
9029 result, input_bfd))
9030 {
9031 undefined_reference ("symbol", symbuf);
9032 return false;
9033 }
9034 }
9035
9036 return true;
9037
9038 /* All that remains are operators. */
9039
9040 #define UNARY_OP(op) \
9041 if (startswith (sym, #op)) \
9042 { \
9043 sym += strlen (#op); \
9044 if (*sym == ':') \
9045 ++sym; \
9046 *symp = sym; \
9047 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
9048 isymbuf, locsymcount, signed_p)) \
9049 return false; \
9050 if (signed_p) \
9051 *result = op ((bfd_signed_vma) a); \
9052 else \
9053 *result = op a; \
9054 return true; \
9055 }
9056
9057 #define BINARY_OP_HEAD(op) \
9058 if (startswith (sym, #op)) \
9059 { \
9060 sym += strlen (#op); \
9061 if (*sym == ':') \
9062 ++sym; \
9063 *symp = sym; \
9064 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
9065 isymbuf, locsymcount, signed_p)) \
9066 return false; \
9067 ++*symp; \
9068 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
9069 isymbuf, locsymcount, signed_p)) \
9070 return false;
9071 #define BINARY_OP_TAIL(op) \
9072 if (signed_p) \
9073 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
9074 else \
9075 *result = a op b; \
9076 return true; \
9077 }
9078 #define BINARY_OP(op) BINARY_OP_HEAD(op) BINARY_OP_TAIL(op)
9079
9080 default:
9081 UNARY_OP (0-);
9082 BINARY_OP_HEAD (<<);
9083 if (b >= sizeof (a) * CHAR_BIT)
9084 {
9085 *result = 0;
9086 return true;
9087 }
9088 signed_p = 0;
9089 BINARY_OP_TAIL (<<);
9090 BINARY_OP_HEAD (>>);
9091 if (b >= sizeof (a) * CHAR_BIT)
9092 {
9093 *result = signed_p && (bfd_signed_vma) a < 0 ? -1 : 0;
9094 return true;
9095 }
9096 BINARY_OP_TAIL (>>);
9097 BINARY_OP (==);
9098 BINARY_OP (!=);
9099 BINARY_OP (<=);
9100 BINARY_OP (>=);
9101 BINARY_OP (&&);
9102 BINARY_OP (||);
9103 UNARY_OP (~);
9104 UNARY_OP (!);
9105 BINARY_OP (*);
9106 BINARY_OP_HEAD (/);
9107 if (b == 0)
9108 {
9109 _bfd_error_handler (_("division by zero"));
9110 bfd_set_error (bfd_error_bad_value);
9111 return false;
9112 }
9113 BINARY_OP_TAIL (/);
9114 BINARY_OP_HEAD (%);
9115 if (b == 0)
9116 {
9117 _bfd_error_handler (_("division by zero"));
9118 bfd_set_error (bfd_error_bad_value);
9119 return false;
9120 }
9121 BINARY_OP_TAIL (%);
9122 BINARY_OP (^);
9123 BINARY_OP (|);
9124 BINARY_OP (&);
9125 BINARY_OP (+);
9126 BINARY_OP (-);
9127 BINARY_OP (<);
9128 BINARY_OP (>);
9129 #undef UNARY_OP
9130 #undef BINARY_OP
9131 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
9132 bfd_set_error (bfd_error_invalid_operation);
9133 return false;
9134 }
9135 }
9136
9137 static void
9138 put_value (bfd_vma size,
9139 unsigned long chunksz,
9140 bfd *input_bfd,
9141 bfd_vma x,
9142 bfd_byte *location)
9143 {
9144 location += (size - chunksz);
9145
9146 for (; size; size -= chunksz, location -= chunksz)
9147 {
9148 switch (chunksz)
9149 {
9150 case 1:
9151 bfd_put_8 (input_bfd, x, location);
9152 x >>= 8;
9153 break;
9154 case 2:
9155 bfd_put_16 (input_bfd, x, location);
9156 x >>= 16;
9157 break;
9158 case 4:
9159 bfd_put_32 (input_bfd, x, location);
9160 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
9161 x >>= 16;
9162 x >>= 16;
9163 break;
9164 #ifdef BFD64
9165 case 8:
9166 bfd_put_64 (input_bfd, x, location);
9167 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
9168 x >>= 32;
9169 x >>= 32;
9170 break;
9171 #endif
9172 default:
9173 abort ();
9174 break;
9175 }
9176 }
9177 }
9178
9179 static bfd_vma
9180 get_value (bfd_vma size,
9181 unsigned long chunksz,
9182 bfd *input_bfd,
9183 bfd_byte *location)
9184 {
9185 int shift;
9186 bfd_vma x = 0;
9187
9188 /* Sanity checks. */
9189 BFD_ASSERT (chunksz <= sizeof (x)
9190 && size >= chunksz
9191 && chunksz != 0
9192 && (size % chunksz) == 0
9193 && input_bfd != NULL
9194 && location != NULL);
9195
9196 if (chunksz == sizeof (x))
9197 {
9198 BFD_ASSERT (size == chunksz);
9199
9200 /* Make sure that we do not perform an undefined shift operation.
9201 We know that size == chunksz so there will only be one iteration
9202 of the loop below. */
9203 shift = 0;
9204 }
9205 else
9206 shift = 8 * chunksz;
9207
9208 for (; size; size -= chunksz, location += chunksz)
9209 {
9210 switch (chunksz)
9211 {
9212 case 1:
9213 x = (x << shift) | bfd_get_8 (input_bfd, location);
9214 break;
9215 case 2:
9216 x = (x << shift) | bfd_get_16 (input_bfd, location);
9217 break;
9218 case 4:
9219 x = (x << shift) | bfd_get_32 (input_bfd, location);
9220 break;
9221 #ifdef BFD64
9222 case 8:
9223 x = (x << shift) | bfd_get_64 (input_bfd, location);
9224 break;
9225 #endif
9226 default:
9227 abort ();
9228 }
9229 }
9230 return x;
9231 }
9232
9233 static void
9234 decode_complex_addend (unsigned long *start, /* in bits */
9235 unsigned long *oplen, /* in bits */
9236 unsigned long *len, /* in bits */
9237 unsigned long *wordsz, /* in bytes */
9238 unsigned long *chunksz, /* in bytes */
9239 unsigned long *lsb0_p,
9240 unsigned long *signed_p,
9241 unsigned long *trunc_p,
9242 unsigned long encoded)
9243 {
9244 * start = encoded & 0x3F;
9245 * len = (encoded >> 6) & 0x3F;
9246 * oplen = (encoded >> 12) & 0x3F;
9247 * wordsz = (encoded >> 18) & 0xF;
9248 * chunksz = (encoded >> 22) & 0xF;
9249 * lsb0_p = (encoded >> 27) & 1;
9250 * signed_p = (encoded >> 28) & 1;
9251 * trunc_p = (encoded >> 29) & 1;
9252 }
9253
9254 bfd_reloc_status_type
9255 bfd_elf_perform_complex_relocation (bfd *input_bfd,
9256 asection *input_section,
9257 bfd_byte *contents,
9258 Elf_Internal_Rela *rel,
9259 bfd_vma relocation)
9260 {
9261 bfd_vma shift, x, mask;
9262 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
9263 bfd_reloc_status_type r;
9264 bfd_size_type octets;
9265
9266 /* Perform this reloc, since it is complex.
9267 (this is not to say that it necessarily refers to a complex
9268 symbol; merely that it is a self-describing CGEN based reloc.
9269 i.e. the addend has the complete reloc information (bit start, end,
9270 word size, etc) encoded within it.). */
9271
9272 decode_complex_addend (&start, &oplen, &len, &wordsz,
9273 &chunksz, &lsb0_p, &signed_p,
9274 &trunc_p, rel->r_addend);
9275
9276 mask = (((1L << (len - 1)) - 1) << 1) | 1;
9277
9278 if (lsb0_p)
9279 shift = (start + 1) - len;
9280 else
9281 shift = (8 * wordsz) - (start + len);
9282
9283 octets = rel->r_offset * bfd_octets_per_byte (input_bfd, input_section);
9284 x = get_value (wordsz, chunksz, input_bfd, contents + octets);
9285
9286 #ifdef DEBUG
9287 printf ("Doing complex reloc: "
9288 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
9289 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
9290 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
9291 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9292 oplen, (unsigned long) x, (unsigned long) mask,
9293 (unsigned long) relocation);
9294 #endif
9295
9296 r = bfd_reloc_ok;
9297 if (! trunc_p)
9298 /* Now do an overflow check. */
9299 r = bfd_check_overflow ((signed_p
9300 ? complain_overflow_signed
9301 : complain_overflow_unsigned),
9302 len, 0, (8 * wordsz),
9303 relocation);
9304
9305 /* Do the deed. */
9306 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
9307
9308 #ifdef DEBUG
9309 printf (" relocation: %8.8lx\n"
9310 " shifted mask: %8.8lx\n"
9311 " shifted/masked reloc: %8.8lx\n"
9312 " result: %8.8lx\n",
9313 (unsigned long) relocation, (unsigned long) (mask << shift),
9314 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
9315 #endif
9316 put_value (wordsz, chunksz, input_bfd, x, contents + octets);
9317 return r;
9318 }
9319
9320 /* Functions to read r_offset from external (target order) reloc
9321 entry. Faster than bfd_getl32 et al, because we let the compiler
9322 know the value is aligned. */
9323
9324 static bfd_vma
9325 ext32l_r_offset (const void *p)
9326 {
9327 union aligned32
9328 {
9329 uint32_t v;
9330 unsigned char c[4];
9331 };
9332 const union aligned32 *a
9333 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9334
9335 uint32_t aval = ( (uint32_t) a->c[0]
9336 | (uint32_t) a->c[1] << 8
9337 | (uint32_t) a->c[2] << 16
9338 | (uint32_t) a->c[3] << 24);
9339 return aval;
9340 }
9341
9342 static bfd_vma
9343 ext32b_r_offset (const void *p)
9344 {
9345 union aligned32
9346 {
9347 uint32_t v;
9348 unsigned char c[4];
9349 };
9350 const union aligned32 *a
9351 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9352
9353 uint32_t aval = ( (uint32_t) a->c[0] << 24
9354 | (uint32_t) a->c[1] << 16
9355 | (uint32_t) a->c[2] << 8
9356 | (uint32_t) a->c[3]);
9357 return aval;
9358 }
9359
9360 static bfd_vma
9361 ext64l_r_offset (const void *p)
9362 {
9363 union aligned64
9364 {
9365 uint64_t v;
9366 unsigned char c[8];
9367 };
9368 const union aligned64 *a
9369 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9370
9371 uint64_t aval = ( (uint64_t) a->c[0]
9372 | (uint64_t) a->c[1] << 8
9373 | (uint64_t) a->c[2] << 16
9374 | (uint64_t) a->c[3] << 24
9375 | (uint64_t) a->c[4] << 32
9376 | (uint64_t) a->c[5] << 40
9377 | (uint64_t) a->c[6] << 48
9378 | (uint64_t) a->c[7] << 56);
9379 return aval;
9380 }
9381
9382 static bfd_vma
9383 ext64b_r_offset (const void *p)
9384 {
9385 union aligned64
9386 {
9387 uint64_t v;
9388 unsigned char c[8];
9389 };
9390 const union aligned64 *a
9391 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9392
9393 uint64_t aval = ( (uint64_t) a->c[0] << 56
9394 | (uint64_t) a->c[1] << 48
9395 | (uint64_t) a->c[2] << 40
9396 | (uint64_t) a->c[3] << 32
9397 | (uint64_t) a->c[4] << 24
9398 | (uint64_t) a->c[5] << 16
9399 | (uint64_t) a->c[6] << 8
9400 | (uint64_t) a->c[7]);
9401 return aval;
9402 }
9403
9404 /* When performing a relocatable link, the input relocations are
9405 preserved. But, if they reference global symbols, the indices
9406 referenced must be updated. Update all the relocations found in
9407 RELDATA. */
9408
9409 static bool
9410 elf_link_adjust_relocs (bfd *abfd,
9411 asection *sec,
9412 struct bfd_elf_section_reloc_data *reldata,
9413 bool sort,
9414 struct bfd_link_info *info)
9415 {
9416 unsigned int i;
9417 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9418 bfd_byte *erela;
9419 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9420 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9421 bfd_vma r_type_mask;
9422 int r_sym_shift;
9423 unsigned int count = reldata->count;
9424 struct elf_link_hash_entry **rel_hash = reldata->hashes;
9425
9426 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
9427 {
9428 swap_in = bed->s->swap_reloc_in;
9429 swap_out = bed->s->swap_reloc_out;
9430 }
9431 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
9432 {
9433 swap_in = bed->s->swap_reloca_in;
9434 swap_out = bed->s->swap_reloca_out;
9435 }
9436 else
9437 abort ();
9438
9439 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
9440 abort ();
9441
9442 if (bed->s->arch_size == 32)
9443 {
9444 r_type_mask = 0xff;
9445 r_sym_shift = 8;
9446 }
9447 else
9448 {
9449 r_type_mask = 0xffffffff;
9450 r_sym_shift = 32;
9451 }
9452
9453 erela = reldata->hdr->contents;
9454 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
9455 {
9456 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
9457 unsigned int j;
9458
9459 if (*rel_hash == NULL)
9460 continue;
9461
9462 if ((*rel_hash)->indx == -2
9463 && info->gc_sections
9464 && ! info->gc_keep_exported)
9465 {
9466 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9467 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
9468 abfd, sec,
9469 (*rel_hash)->root.root.string);
9470 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
9471 abfd, sec);
9472 bfd_set_error (bfd_error_invalid_operation);
9473 return false;
9474 }
9475 BFD_ASSERT ((*rel_hash)->indx >= 0);
9476
9477 (*swap_in) (abfd, erela, irela);
9478 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
9479 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
9480 | (irela[j].r_info & r_type_mask));
9481 (*swap_out) (abfd, irela, erela);
9482 }
9483
9484 if (bed->elf_backend_update_relocs)
9485 (*bed->elf_backend_update_relocs) (sec, reldata);
9486
9487 if (sort && count != 0)
9488 {
9489 bfd_vma (*ext_r_off) (const void *);
9490 bfd_vma r_off;
9491 size_t elt_size;
9492 bfd_byte *base, *end, *p, *loc;
9493 bfd_byte *buf = NULL;
9494
9495 if (bed->s->arch_size == 32)
9496 {
9497 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9498 ext_r_off = ext32l_r_offset;
9499 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9500 ext_r_off = ext32b_r_offset;
9501 else
9502 abort ();
9503 }
9504 else
9505 {
9506 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9507 ext_r_off = ext64l_r_offset;
9508 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9509 ext_r_off = ext64b_r_offset;
9510 else
9511 abort ();
9512 }
9513
9514 /* Must use a stable sort here. A modified insertion sort,
9515 since the relocs are mostly sorted already. */
9516 elt_size = reldata->hdr->sh_entsize;
9517 base = reldata->hdr->contents;
9518 end = base + count * elt_size;
9519 if (elt_size > sizeof (Elf64_External_Rela))
9520 abort ();
9521
9522 /* Ensure the first element is lowest. This acts as a sentinel,
9523 speeding the main loop below. */
9524 r_off = (*ext_r_off) (base);
9525 for (p = loc = base; (p += elt_size) < end; )
9526 {
9527 bfd_vma r_off2 = (*ext_r_off) (p);
9528 if (r_off > r_off2)
9529 {
9530 r_off = r_off2;
9531 loc = p;
9532 }
9533 }
9534 if (loc != base)
9535 {
9536 /* Don't just swap *base and *loc as that changes the order
9537 of the original base[0] and base[1] if they happen to
9538 have the same r_offset. */
9539 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
9540 memcpy (onebuf, loc, elt_size);
9541 memmove (base + elt_size, base, loc - base);
9542 memcpy (base, onebuf, elt_size);
9543 }
9544
9545 for (p = base + elt_size; (p += elt_size) < end; )
9546 {
9547 /* base to p is sorted, *p is next to insert. */
9548 r_off = (*ext_r_off) (p);
9549 /* Search the sorted region for location to insert. */
9550 loc = p - elt_size;
9551 while (r_off < (*ext_r_off) (loc))
9552 loc -= elt_size;
9553 loc += elt_size;
9554 if (loc != p)
9555 {
9556 /* Chances are there is a run of relocs to insert here,
9557 from one of more input files. Files are not always
9558 linked in order due to the way elf_link_input_bfd is
9559 called. See pr17666. */
9560 size_t sortlen = p - loc;
9561 bfd_vma r_off2 = (*ext_r_off) (loc);
9562 size_t runlen = elt_size;
9563 bfd_vma r_off_runend = r_off;
9564 bfd_vma r_off_runend_next;
9565 size_t buf_size = 96 * 1024;
9566 while (p + runlen < end
9567 && (sortlen <= buf_size
9568 || runlen + elt_size <= buf_size)
9569 /* run must not break the ordering of base..loc+1 */
9570 && r_off2 > (r_off_runend_next = (*ext_r_off) (p + runlen))
9571 /* run must be already sorted */
9572 && r_off_runend_next >= r_off_runend)
9573 {
9574 runlen += elt_size;
9575 r_off_runend = r_off_runend_next;
9576 }
9577 if (buf == NULL)
9578 {
9579 buf = bfd_malloc (buf_size);
9580 if (buf == NULL)
9581 return false;
9582 }
9583 if (runlen < sortlen)
9584 {
9585 memcpy (buf, p, runlen);
9586 memmove (loc + runlen, loc, sortlen);
9587 memcpy (loc, buf, runlen);
9588 }
9589 else
9590 {
9591 memcpy (buf, loc, sortlen);
9592 memmove (loc, p, runlen);
9593 memcpy (loc + runlen, buf, sortlen);
9594 }
9595 p += runlen - elt_size;
9596 }
9597 }
9598 /* Hashes are no longer valid. */
9599 free (reldata->hashes);
9600 reldata->hashes = NULL;
9601 free (buf);
9602 }
9603 return true;
9604 }
9605
9606 struct elf_link_sort_rela
9607 {
9608 union {
9609 bfd_vma offset;
9610 bfd_vma sym_mask;
9611 } u;
9612 enum elf_reloc_type_class type;
9613 /* We use this as an array of size int_rels_per_ext_rel. */
9614 Elf_Internal_Rela rela[1];
9615 };
9616
9617 /* qsort stability here and for cmp2 is only an issue if multiple
9618 dynamic relocations are emitted at the same address. But targets
9619 that apply a series of dynamic relocations each operating on the
9620 result of the prior relocation can't use -z combreloc as
9621 implemented anyway. Such schemes tend to be broken by sorting on
9622 symbol index. That leaves dynamic NONE relocs as the only other
9623 case where ld might emit multiple relocs at the same address, and
9624 those are only emitted due to target bugs. */
9625
9626 static int
9627 elf_link_sort_cmp1 (const void *A, const void *B)
9628 {
9629 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9630 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9631 int relativea, relativeb;
9632
9633 relativea = a->type == reloc_class_relative;
9634 relativeb = b->type == reloc_class_relative;
9635
9636 if (relativea < relativeb)
9637 return 1;
9638 if (relativea > relativeb)
9639 return -1;
9640 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
9641 return -1;
9642 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
9643 return 1;
9644 if (a->rela->r_offset < b->rela->r_offset)
9645 return -1;
9646 if (a->rela->r_offset > b->rela->r_offset)
9647 return 1;
9648 return 0;
9649 }
9650
9651 static int
9652 elf_link_sort_cmp2 (const void *A, const void *B)
9653 {
9654 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9655 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9656
9657 if (a->type < b->type)
9658 return -1;
9659 if (a->type > b->type)
9660 return 1;
9661 if (a->u.offset < b->u.offset)
9662 return -1;
9663 if (a->u.offset > b->u.offset)
9664 return 1;
9665 if (a->rela->r_offset < b->rela->r_offset)
9666 return -1;
9667 if (a->rela->r_offset > b->rela->r_offset)
9668 return 1;
9669 return 0;
9670 }
9671
9672 static size_t
9673 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9674 {
9675 asection *dynamic_relocs;
9676 asection *rela_dyn;
9677 asection *rel_dyn;
9678 bfd_size_type count, size;
9679 size_t i, ret, sort_elt, ext_size;
9680 bfd_byte *sort, *s_non_relative, *p;
9681 struct elf_link_sort_rela *sq;
9682 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9683 int i2e = bed->s->int_rels_per_ext_rel;
9684 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
9685 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9686 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9687 struct bfd_link_order *lo;
9688 bfd_vma r_sym_mask;
9689 bool use_rela;
9690
9691 /* Find a dynamic reloc section. */
9692 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9693 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
9694 if (rela_dyn != NULL && rela_dyn->size > 0
9695 && rel_dyn != NULL && rel_dyn->size > 0)
9696 {
9697 bool use_rela_initialised = false;
9698
9699 /* This is just here to stop gcc from complaining.
9700 Its initialization checking code is not perfect. */
9701 use_rela = true;
9702
9703 /* Both sections are present. Examine the sizes
9704 of the indirect sections to help us choose. */
9705 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9706 if (lo->type == bfd_indirect_link_order)
9707 {
9708 asection *o = lo->u.indirect.section;
9709
9710 if ((o->size % bed->s->sizeof_rela) == 0)
9711 {
9712 if ((o->size % bed->s->sizeof_rel) == 0)
9713 /* Section size is divisible by both rel and rela sizes.
9714 It is of no help to us. */
9715 ;
9716 else
9717 {
9718 /* Section size is only divisible by rela. */
9719 if (use_rela_initialised && !use_rela)
9720 {
9721 _bfd_error_handler (_("%pB: unable to sort relocs - "
9722 "they are in more than one size"),
9723 abfd);
9724 bfd_set_error (bfd_error_invalid_operation);
9725 return 0;
9726 }
9727 else
9728 {
9729 use_rela = true;
9730 use_rela_initialised = true;
9731 }
9732 }
9733 }
9734 else if ((o->size % bed->s->sizeof_rel) == 0)
9735 {
9736 /* Section size is only divisible by rel. */
9737 if (use_rela_initialised && use_rela)
9738 {
9739 _bfd_error_handler (_("%pB: unable to sort relocs - "
9740 "they are in more than one size"),
9741 abfd);
9742 bfd_set_error (bfd_error_invalid_operation);
9743 return 0;
9744 }
9745 else
9746 {
9747 use_rela = false;
9748 use_rela_initialised = true;
9749 }
9750 }
9751 else
9752 {
9753 /* The section size is not divisible by either -
9754 something is wrong. */
9755 _bfd_error_handler (_("%pB: unable to sort relocs - "
9756 "they are of an unknown size"), abfd);
9757 bfd_set_error (bfd_error_invalid_operation);
9758 return 0;
9759 }
9760 }
9761
9762 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9763 if (lo->type == bfd_indirect_link_order)
9764 {
9765 asection *o = lo->u.indirect.section;
9766
9767 if ((o->size % bed->s->sizeof_rela) == 0)
9768 {
9769 if ((o->size % bed->s->sizeof_rel) == 0)
9770 /* Section size is divisible by both rel and rela sizes.
9771 It is of no help to us. */
9772 ;
9773 else
9774 {
9775 /* Section size is only divisible by rela. */
9776 if (use_rela_initialised && !use_rela)
9777 {
9778 _bfd_error_handler (_("%pB: unable to sort relocs - "
9779 "they are in more than one size"),
9780 abfd);
9781 bfd_set_error (bfd_error_invalid_operation);
9782 return 0;
9783 }
9784 else
9785 {
9786 use_rela = true;
9787 use_rela_initialised = true;
9788 }
9789 }
9790 }
9791 else if ((o->size % bed->s->sizeof_rel) == 0)
9792 {
9793 /* Section size is only divisible by rel. */
9794 if (use_rela_initialised && use_rela)
9795 {
9796 _bfd_error_handler (_("%pB: unable to sort relocs - "
9797 "they are in more than one size"),
9798 abfd);
9799 bfd_set_error (bfd_error_invalid_operation);
9800 return 0;
9801 }
9802 else
9803 {
9804 use_rela = false;
9805 use_rela_initialised = true;
9806 }
9807 }
9808 else
9809 {
9810 /* The section size is not divisible by either -
9811 something is wrong. */
9812 _bfd_error_handler (_("%pB: unable to sort relocs - "
9813 "they are of an unknown size"), abfd);
9814 bfd_set_error (bfd_error_invalid_operation);
9815 return 0;
9816 }
9817 }
9818
9819 if (! use_rela_initialised)
9820 /* Make a guess. */
9821 use_rela = true;
9822 }
9823 else if (rela_dyn != NULL && rela_dyn->size > 0)
9824 use_rela = true;
9825 else if (rel_dyn != NULL && rel_dyn->size > 0)
9826 use_rela = false;
9827 else
9828 return 0;
9829
9830 if (use_rela)
9831 {
9832 dynamic_relocs = rela_dyn;
9833 ext_size = bed->s->sizeof_rela;
9834 swap_in = bed->s->swap_reloca_in;
9835 swap_out = bed->s->swap_reloca_out;
9836 }
9837 else
9838 {
9839 dynamic_relocs = rel_dyn;
9840 ext_size = bed->s->sizeof_rel;
9841 swap_in = bed->s->swap_reloc_in;
9842 swap_out = bed->s->swap_reloc_out;
9843 }
9844
9845 size = 0;
9846 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9847 if (lo->type == bfd_indirect_link_order)
9848 size += lo->u.indirect.section->size;
9849
9850 if (size != dynamic_relocs->size)
9851 return 0;
9852
9853 sort_elt = (sizeof (struct elf_link_sort_rela)
9854 + (i2e - 1) * sizeof (Elf_Internal_Rela));
9855
9856 count = dynamic_relocs->size / ext_size;
9857 if (count == 0)
9858 return 0;
9859 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
9860
9861 if (sort == NULL)
9862 {
9863 (*info->callbacks->warning)
9864 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
9865 return 0;
9866 }
9867
9868 if (bed->s->arch_size == 32)
9869 r_sym_mask = ~(bfd_vma) 0xff;
9870 else
9871 r_sym_mask = ~(bfd_vma) 0xffffffff;
9872
9873 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9874 if (lo->type == bfd_indirect_link_order)
9875 {
9876 bfd_byte *erel, *erelend;
9877 asection *o = lo->u.indirect.section;
9878
9879 if (o->contents == NULL && o->size != 0)
9880 {
9881 /* This is a reloc section that is being handled as a normal
9882 section. See bfd_section_from_shdr. We can't combine
9883 relocs in this case. */
9884 free (sort);
9885 return 0;
9886 }
9887 erel = o->contents;
9888 erelend = o->contents + o->size;
9889 p = sort + o->output_offset * opb / ext_size * sort_elt;
9890
9891 while (erel < erelend)
9892 {
9893 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9894
9895 (*swap_in) (abfd, erel, s->rela);
9896 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
9897 s->u.sym_mask = r_sym_mask;
9898 p += sort_elt;
9899 erel += ext_size;
9900 }
9901 }
9902
9903 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9904
9905 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9906 {
9907 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9908 if (s->type != reloc_class_relative)
9909 break;
9910 }
9911 ret = i;
9912 s_non_relative = p;
9913
9914 sq = (struct elf_link_sort_rela *) s_non_relative;
9915 for (; i < count; i++, p += sort_elt)
9916 {
9917 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9918 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9919 sq = sp;
9920 sp->u.offset = sq->rela->r_offset;
9921 }
9922
9923 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9924
9925 struct elf_link_hash_table *htab = elf_hash_table (info);
9926 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9927 {
9928 /* We have plt relocs in .rela.dyn. */
9929 sq = (struct elf_link_sort_rela *) sort;
9930 for (i = 0; i < count; i++)
9931 if (sq[count - i - 1].type != reloc_class_plt)
9932 break;
9933 if (i != 0 && htab->srelplt->size == i * ext_size)
9934 {
9935 struct bfd_link_order **plo;
9936 /* Put srelplt link_order last. This is so the output_offset
9937 set in the next loop is correct for DT_JMPREL. */
9938 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9939 if ((*plo)->type == bfd_indirect_link_order
9940 && (*plo)->u.indirect.section == htab->srelplt)
9941 {
9942 lo = *plo;
9943 *plo = lo->next;
9944 }
9945 else
9946 plo = &(*plo)->next;
9947 *plo = lo;
9948 lo->next = NULL;
9949 dynamic_relocs->map_tail.link_order = lo;
9950 }
9951 }
9952
9953 p = sort;
9954 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9955 if (lo->type == bfd_indirect_link_order)
9956 {
9957 bfd_byte *erel, *erelend;
9958 asection *o = lo->u.indirect.section;
9959
9960 erel = o->contents;
9961 erelend = o->contents + o->size;
9962 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
9963 while (erel < erelend)
9964 {
9965 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9966 (*swap_out) (abfd, s->rela, erel);
9967 p += sort_elt;
9968 erel += ext_size;
9969 }
9970 }
9971
9972 free (sort);
9973 *psec = dynamic_relocs;
9974 return ret;
9975 }
9976
9977 /* Add a symbol to the output symbol string table. */
9978
9979 static int
9980 elf_link_output_symstrtab (void *finf,
9981 const char *name,
9982 Elf_Internal_Sym *elfsym,
9983 asection *input_sec,
9984 struct elf_link_hash_entry *h)
9985 {
9986 struct elf_final_link_info *flinfo = finf;
9987 int (*output_symbol_hook)
9988 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9989 struct elf_link_hash_entry *);
9990 struct elf_link_hash_table *hash_table;
9991 const struct elf_backend_data *bed;
9992 bfd_size_type strtabsize;
9993
9994 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9995
9996 bed = get_elf_backend_data (flinfo->output_bfd);
9997 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9998 if (output_symbol_hook != NULL)
9999 {
10000 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
10001 if (ret != 1)
10002 return ret;
10003 }
10004
10005 if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC)
10006 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
10007 if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE)
10008 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique;
10009
10010 if (name == NULL
10011 || *name == '\0'
10012 || (input_sec->flags & SEC_EXCLUDE))
10013 elfsym->st_name = (unsigned long) -1;
10014 else
10015 {
10016 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
10017 to get the final offset for st_name. */
10018 char *versioned_name = (char *) name;
10019 if (h != NULL)
10020 {
10021 if (h->versioned == versioned && h->def_dynamic)
10022 {
10023 /* Keep only one '@' for versioned symbols defined in
10024 shared objects. */
10025 char *version = strrchr (name, ELF_VER_CHR);
10026 char *base_end = strchr (name, ELF_VER_CHR);
10027 if (version != base_end)
10028 {
10029 size_t base_len;
10030 size_t len = strlen (name);
10031 versioned_name = bfd_alloc (flinfo->output_bfd, len);
10032 if (versioned_name == NULL)
10033 return 0;
10034 base_len = base_end - name;
10035 memcpy (versioned_name, name, base_len);
10036 memcpy (versioned_name + base_len, version,
10037 len - base_len);
10038 }
10039 }
10040 }
10041 else if (flinfo->info->unique_symbol
10042 && ELF_ST_BIND (elfsym->st_info) == STB_LOCAL)
10043 {
10044 struct local_hash_entry *lh;
10045 size_t count_len;
10046 size_t base_len;
10047 char buf[30];
10048 switch (ELF_ST_TYPE (elfsym->st_info))
10049 {
10050 case STT_FILE:
10051 case STT_SECTION:
10052 break;
10053 default:
10054 lh = (struct local_hash_entry *) bfd_hash_lookup
10055 (&flinfo->local_hash_table, name, true, false);
10056 if (lh == NULL)
10057 return 0;
10058 /* Always append ".COUNT" to local symbols to avoid
10059 potential conflicts with local symbol "XXX.COUNT". */
10060 sprintf (buf, "%lx", lh->count);
10061 base_len = lh->size;
10062 if (!base_len)
10063 {
10064 base_len = strlen (name);
10065 lh->size = base_len;
10066 }
10067 count_len = strlen (buf);
10068 versioned_name = bfd_alloc (flinfo->output_bfd,
10069 base_len + count_len + 2);
10070 if (versioned_name == NULL)
10071 return 0;
10072 memcpy (versioned_name, name, base_len);
10073 versioned_name[base_len] = '.';
10074 memcpy (versioned_name + base_len + 1, buf,
10075 count_len + 1);
10076 lh->count++;
10077 break;
10078 }
10079 }
10080 elfsym->st_name
10081 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
10082 versioned_name, false);
10083 if (elfsym->st_name == (unsigned long) -1)
10084 return 0;
10085 }
10086
10087 hash_table = elf_hash_table (flinfo->info);
10088 strtabsize = hash_table->strtabsize;
10089 if (strtabsize <= flinfo->output_bfd->symcount)
10090 {
10091 strtabsize += strtabsize;
10092 hash_table->strtabsize = strtabsize;
10093 strtabsize *= sizeof (*hash_table->strtab);
10094 hash_table->strtab
10095 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
10096 strtabsize);
10097 if (hash_table->strtab == NULL)
10098 return 0;
10099 }
10100 hash_table->strtab[flinfo->output_bfd->symcount].sym = *elfsym;
10101 hash_table->strtab[flinfo->output_bfd->symcount].dest_index
10102 = flinfo->output_bfd->symcount;
10103 flinfo->output_bfd->symcount += 1;
10104
10105 return 1;
10106 }
10107
10108 /* Swap symbols out to the symbol table and flush the output symbols to
10109 the file. */
10110
10111 static bool
10112 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
10113 {
10114 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
10115 size_t amt;
10116 size_t i;
10117 const struct elf_backend_data *bed;
10118 bfd_byte *symbuf;
10119 Elf_Internal_Shdr *hdr;
10120 file_ptr pos;
10121 bool ret;
10122
10123 if (flinfo->output_bfd->symcount == 0)
10124 return true;
10125
10126 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
10127
10128 bed = get_elf_backend_data (flinfo->output_bfd);
10129
10130 amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount;
10131 symbuf = (bfd_byte *) bfd_malloc (amt);
10132 if (symbuf == NULL)
10133 return false;
10134
10135 if (flinfo->symshndxbuf)
10136 {
10137 amt = sizeof (Elf_External_Sym_Shndx);
10138 amt *= bfd_get_symcount (flinfo->output_bfd);
10139 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
10140 if (flinfo->symshndxbuf == NULL)
10141 {
10142 free (symbuf);
10143 return false;
10144 }
10145 }
10146
10147 /* Now swap out the symbols. */
10148 for (i = 0; i < flinfo->output_bfd->symcount; i++)
10149 {
10150 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
10151 if (elfsym->sym.st_name == (unsigned long) -1)
10152 elfsym->sym.st_name = 0;
10153 else
10154 elfsym->sym.st_name
10155 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
10156 elfsym->sym.st_name);
10157
10158 /* Inform the linker of the addition of this symbol. */
10159
10160 if (flinfo->info->callbacks->ctf_new_symbol)
10161 flinfo->info->callbacks->ctf_new_symbol (elfsym->dest_index,
10162 &elfsym->sym);
10163
10164 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
10165 ((bfd_byte *) symbuf
10166 + (elfsym->dest_index
10167 * bed->s->sizeof_sym)),
10168 NPTR_ADD (flinfo->symshndxbuf,
10169 elfsym->dest_index));
10170 }
10171
10172 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
10173 pos = hdr->sh_offset + hdr->sh_size;
10174 amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount;
10175 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
10176 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
10177 {
10178 hdr->sh_size += amt;
10179 ret = true;
10180 }
10181 else
10182 ret = false;
10183
10184 free (symbuf);
10185
10186 free (hash_table->strtab);
10187 hash_table->strtab = NULL;
10188
10189 return ret;
10190 }
10191
10192 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
10193
10194 static bool
10195 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
10196 {
10197 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
10198 && sym->st_shndx < SHN_LORESERVE)
10199 {
10200 /* The gABI doesn't support dynamic symbols in output sections
10201 beyond 64k. */
10202 _bfd_error_handler
10203 /* xgettext:c-format */
10204 (_("%pB: too many sections: %d (>= %d)"),
10205 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
10206 bfd_set_error (bfd_error_nonrepresentable_section);
10207 return false;
10208 }
10209 return true;
10210 }
10211
10212 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
10213 allowing an unsatisfied unversioned symbol in the DSO to match a
10214 versioned symbol that would normally require an explicit version.
10215 We also handle the case that a DSO references a hidden symbol
10216 which may be satisfied by a versioned symbol in another DSO. */
10217
10218 static bool
10219 elf_link_check_versioned_symbol (struct bfd_link_info *info,
10220 const struct elf_backend_data *bed,
10221 struct elf_link_hash_entry *h)
10222 {
10223 bfd *abfd;
10224 struct elf_link_loaded_list *loaded;
10225
10226 if (!is_elf_hash_table (info->hash))
10227 return false;
10228
10229 /* Check indirect symbol. */
10230 while (h->root.type == bfd_link_hash_indirect)
10231 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10232
10233 switch (h->root.type)
10234 {
10235 default:
10236 abfd = NULL;
10237 break;
10238
10239 case bfd_link_hash_undefined:
10240 case bfd_link_hash_undefweak:
10241 abfd = h->root.u.undef.abfd;
10242 if (abfd == NULL
10243 || (abfd->flags & DYNAMIC) == 0
10244 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
10245 return false;
10246 break;
10247
10248 case bfd_link_hash_defined:
10249 case bfd_link_hash_defweak:
10250 abfd = h->root.u.def.section->owner;
10251 break;
10252
10253 case bfd_link_hash_common:
10254 abfd = h->root.u.c.p->section->owner;
10255 break;
10256 }
10257 BFD_ASSERT (abfd != NULL);
10258
10259 for (loaded = elf_hash_table (info)->dyn_loaded;
10260 loaded != NULL;
10261 loaded = loaded->next)
10262 {
10263 bfd *input;
10264 Elf_Internal_Shdr *hdr;
10265 size_t symcount;
10266 size_t extsymcount;
10267 size_t extsymoff;
10268 Elf_Internal_Shdr *versymhdr;
10269 Elf_Internal_Sym *isym;
10270 Elf_Internal_Sym *isymend;
10271 Elf_Internal_Sym *isymbuf;
10272 Elf_External_Versym *ever;
10273 Elf_External_Versym *extversym;
10274
10275 input = loaded->abfd;
10276
10277 /* We check each DSO for a possible hidden versioned definition. */
10278 if (input == abfd
10279 || elf_dynversym (input) == 0)
10280 continue;
10281
10282 hdr = &elf_tdata (input)->dynsymtab_hdr;
10283
10284 symcount = hdr->sh_size / bed->s->sizeof_sym;
10285 if (elf_bad_symtab (input))
10286 {
10287 extsymcount = symcount;
10288 extsymoff = 0;
10289 }
10290 else
10291 {
10292 extsymcount = symcount - hdr->sh_info;
10293 extsymoff = hdr->sh_info;
10294 }
10295
10296 if (extsymcount == 0)
10297 continue;
10298
10299 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
10300 NULL, NULL, NULL);
10301 if (isymbuf == NULL)
10302 return false;
10303
10304 /* Read in any version definitions. */
10305 versymhdr = &elf_tdata (input)->dynversym_hdr;
10306 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
10307 || (extversym = (Elf_External_Versym *)
10308 _bfd_malloc_and_read (input, versymhdr->sh_size,
10309 versymhdr->sh_size)) == NULL)
10310 {
10311 free (isymbuf);
10312 return false;
10313 }
10314
10315 ever = extversym + extsymoff;
10316 isymend = isymbuf + extsymcount;
10317 for (isym = isymbuf; isym < isymend; isym++, ever++)
10318 {
10319 const char *name;
10320 Elf_Internal_Versym iver;
10321 unsigned short version_index;
10322
10323 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
10324 || isym->st_shndx == SHN_UNDEF)
10325 continue;
10326
10327 name = bfd_elf_string_from_elf_section (input,
10328 hdr->sh_link,
10329 isym->st_name);
10330 if (strcmp (name, h->root.root.string) != 0)
10331 continue;
10332
10333 _bfd_elf_swap_versym_in (input, ever, &iver);
10334
10335 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
10336 && !(h->def_regular
10337 && h->forced_local))
10338 {
10339 /* If we have a non-hidden versioned sym, then it should
10340 have provided a definition for the undefined sym unless
10341 it is defined in a non-shared object and forced local.
10342 */
10343 abort ();
10344 }
10345
10346 version_index = iver.vs_vers & VERSYM_VERSION;
10347 if (version_index == 1 || version_index == 2)
10348 {
10349 /* This is the base or first version. We can use it. */
10350 free (extversym);
10351 free (isymbuf);
10352 return true;
10353 }
10354 }
10355
10356 free (extversym);
10357 free (isymbuf);
10358 }
10359
10360 return false;
10361 }
10362
10363 /* Convert ELF common symbol TYPE. */
10364
10365 static int
10366 elf_link_convert_common_type (struct bfd_link_info *info, int type)
10367 {
10368 /* Commom symbol can only appear in relocatable link. */
10369 if (!bfd_link_relocatable (info))
10370 abort ();
10371 switch (info->elf_stt_common)
10372 {
10373 case unchanged:
10374 break;
10375 case elf_stt_common:
10376 type = STT_COMMON;
10377 break;
10378 case no_elf_stt_common:
10379 type = STT_OBJECT;
10380 break;
10381 }
10382 return type;
10383 }
10384
10385 /* Add an external symbol to the symbol table. This is called from
10386 the hash table traversal routine. When generating a shared object,
10387 we go through the symbol table twice. The first time we output
10388 anything that might have been forced to local scope in a version
10389 script. The second time we output the symbols that are still
10390 global symbols. */
10391
10392 static bool
10393 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
10394 {
10395 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
10396 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
10397 struct elf_final_link_info *flinfo = eoinfo->flinfo;
10398 bool strip;
10399 Elf_Internal_Sym sym;
10400 asection *input_sec;
10401 const struct elf_backend_data *bed;
10402 long indx;
10403 int ret;
10404 unsigned int type;
10405
10406 if (h->root.type == bfd_link_hash_warning)
10407 {
10408 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10409 if (h->root.type == bfd_link_hash_new)
10410 return true;
10411 }
10412
10413 /* Decide whether to output this symbol in this pass. */
10414 if (eoinfo->localsyms)
10415 {
10416 if (!h->forced_local)
10417 return true;
10418 }
10419 else
10420 {
10421 if (h->forced_local)
10422 return true;
10423 }
10424
10425 bed = get_elf_backend_data (flinfo->output_bfd);
10426
10427 if (h->root.type == bfd_link_hash_undefined)
10428 {
10429 /* If we have an undefined symbol reference here then it must have
10430 come from a shared library that is being linked in. (Undefined
10431 references in regular files have already been handled unless
10432 they are in unreferenced sections which are removed by garbage
10433 collection). */
10434 bool ignore_undef = false;
10435
10436 /* Some symbols may be special in that the fact that they're
10437 undefined can be safely ignored - let backend determine that. */
10438 if (bed->elf_backend_ignore_undef_symbol)
10439 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
10440
10441 /* If we are reporting errors for this situation then do so now. */
10442 if (!ignore_undef
10443 && h->ref_dynamic_nonweak
10444 && (!h->ref_regular || flinfo->info->gc_sections)
10445 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
10446 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
10447 {
10448 flinfo->info->callbacks->undefined_symbol
10449 (flinfo->info, h->root.root.string,
10450 h->ref_regular ? NULL : h->root.u.undef.abfd, NULL, 0,
10451 flinfo->info->unresolved_syms_in_shared_libs == RM_DIAGNOSE
10452 && !flinfo->info->warn_unresolved_syms);
10453 }
10454
10455 /* Strip a global symbol defined in a discarded section. */
10456 if (h->indx == -3)
10457 return true;
10458 }
10459
10460 /* We should also warn if a forced local symbol is referenced from
10461 shared libraries. */
10462 if (bfd_link_executable (flinfo->info)
10463 && h->forced_local
10464 && h->ref_dynamic
10465 && h->def_regular
10466 && !h->dynamic_def
10467 && h->ref_dynamic_nonweak
10468 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
10469 {
10470 bfd *def_bfd;
10471 const char *msg;
10472 struct elf_link_hash_entry *hi = h;
10473
10474 /* Check indirect symbol. */
10475 while (hi->root.type == bfd_link_hash_indirect)
10476 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
10477
10478 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
10479 /* xgettext:c-format */
10480 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
10481 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
10482 /* xgettext:c-format */
10483 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
10484 else
10485 /* xgettext:c-format */
10486 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
10487 def_bfd = flinfo->output_bfd;
10488 if (hi->root.u.def.section != bfd_abs_section_ptr)
10489 def_bfd = hi->root.u.def.section->owner;
10490 _bfd_error_handler (msg, flinfo->output_bfd,
10491 h->root.root.string, def_bfd);
10492 bfd_set_error (bfd_error_bad_value);
10493 eoinfo->failed = true;
10494 return false;
10495 }
10496
10497 /* We don't want to output symbols that have never been mentioned by
10498 a regular file, or that we have been told to strip. However, if
10499 h->indx is set to -2, the symbol is used by a reloc and we must
10500 output it. */
10501 strip = false;
10502 if (h->indx == -2)
10503 ;
10504 else if ((h->def_dynamic
10505 || h->ref_dynamic
10506 || h->root.type == bfd_link_hash_new)
10507 && !h->def_regular
10508 && !h->ref_regular)
10509 strip = true;
10510 else if (flinfo->info->strip == strip_all)
10511 strip = true;
10512 else if (flinfo->info->strip == strip_some
10513 && bfd_hash_lookup (flinfo->info->keep_hash,
10514 h->root.root.string, false, false) == NULL)
10515 strip = true;
10516 else if ((h->root.type == bfd_link_hash_defined
10517 || h->root.type == bfd_link_hash_defweak)
10518 && ((flinfo->info->strip_discarded
10519 && discarded_section (h->root.u.def.section))
10520 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
10521 && h->root.u.def.section->owner != NULL
10522 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
10523 strip = true;
10524 else if ((h->root.type == bfd_link_hash_undefined
10525 || h->root.type == bfd_link_hash_undefweak)
10526 && h->root.u.undef.abfd != NULL
10527 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
10528 strip = true;
10529
10530 type = h->type;
10531
10532 /* If we're stripping it, and it's not a dynamic symbol, there's
10533 nothing else to do. However, if it is a forced local symbol or
10534 an ifunc symbol we need to give the backend finish_dynamic_symbol
10535 function a chance to make it dynamic. */
10536 if (strip
10537 && h->dynindx == -1
10538 && type != STT_GNU_IFUNC
10539 && !h->forced_local)
10540 return true;
10541
10542 sym.st_value = 0;
10543 sym.st_size = h->size;
10544 sym.st_other = h->other;
10545 switch (h->root.type)
10546 {
10547 default:
10548 case bfd_link_hash_new:
10549 case bfd_link_hash_warning:
10550 abort ();
10551 return false;
10552
10553 case bfd_link_hash_undefined:
10554 case bfd_link_hash_undefweak:
10555 input_sec = bfd_und_section_ptr;
10556 sym.st_shndx = SHN_UNDEF;
10557 break;
10558
10559 case bfd_link_hash_defined:
10560 case bfd_link_hash_defweak:
10561 {
10562 input_sec = h->root.u.def.section;
10563 if (input_sec->output_section != NULL)
10564 {
10565 sym.st_shndx =
10566 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
10567 input_sec->output_section);
10568 if (sym.st_shndx == SHN_BAD)
10569 {
10570 _bfd_error_handler
10571 /* xgettext:c-format */
10572 (_("%pB: could not find output section %pA for input section %pA"),
10573 flinfo->output_bfd, input_sec->output_section, input_sec);
10574 bfd_set_error (bfd_error_nonrepresentable_section);
10575 eoinfo->failed = true;
10576 return false;
10577 }
10578
10579 /* ELF symbols in relocatable files are section relative,
10580 but in nonrelocatable files they are virtual
10581 addresses. */
10582 sym.st_value = h->root.u.def.value + input_sec->output_offset;
10583 if (!bfd_link_relocatable (flinfo->info))
10584 {
10585 sym.st_value += input_sec->output_section->vma;
10586 if (h->type == STT_TLS)
10587 {
10588 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
10589 if (tls_sec != NULL)
10590 sym.st_value -= tls_sec->vma;
10591 }
10592 }
10593 }
10594 else
10595 {
10596 BFD_ASSERT (input_sec->owner == NULL
10597 || (input_sec->owner->flags & DYNAMIC) != 0);
10598 sym.st_shndx = SHN_UNDEF;
10599 input_sec = bfd_und_section_ptr;
10600 }
10601 }
10602 break;
10603
10604 case bfd_link_hash_common:
10605 input_sec = h->root.u.c.p->section;
10606 sym.st_shndx = bed->common_section_index (input_sec);
10607 sym.st_value = 1 << h->root.u.c.p->alignment_power;
10608 break;
10609
10610 case bfd_link_hash_indirect:
10611 /* These symbols are created by symbol versioning. They point
10612 to the decorated version of the name. For example, if the
10613 symbol foo@@GNU_1.2 is the default, which should be used when
10614 foo is used with no version, then we add an indirect symbol
10615 foo which points to foo@@GNU_1.2. We ignore these symbols,
10616 since the indirected symbol is already in the hash table. */
10617 return true;
10618 }
10619
10620 if (type == STT_COMMON || type == STT_OBJECT)
10621 switch (h->root.type)
10622 {
10623 case bfd_link_hash_common:
10624 type = elf_link_convert_common_type (flinfo->info, type);
10625 break;
10626 case bfd_link_hash_defined:
10627 case bfd_link_hash_defweak:
10628 if (bed->common_definition (&sym))
10629 type = elf_link_convert_common_type (flinfo->info, type);
10630 else
10631 type = STT_OBJECT;
10632 break;
10633 case bfd_link_hash_undefined:
10634 case bfd_link_hash_undefweak:
10635 break;
10636 default:
10637 abort ();
10638 }
10639
10640 if (h->forced_local)
10641 {
10642 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
10643 /* Turn off visibility on local symbol. */
10644 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
10645 }
10646 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10647 else if (h->unique_global && h->def_regular)
10648 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
10649 else if (h->root.type == bfd_link_hash_undefweak
10650 || h->root.type == bfd_link_hash_defweak)
10651 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
10652 else
10653 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
10654 sym.st_target_internal = h->target_internal;
10655
10656 /* Give the processor backend a chance to tweak the symbol value,
10657 and also to finish up anything that needs to be done for this
10658 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
10659 forced local syms when non-shared is due to a historical quirk.
10660 STT_GNU_IFUNC symbol must go through PLT. */
10661 if ((h->type == STT_GNU_IFUNC
10662 && h->def_regular
10663 && !bfd_link_relocatable (flinfo->info))
10664 || ((h->dynindx != -1
10665 || h->forced_local)
10666 && ((bfd_link_pic (flinfo->info)
10667 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
10668 || h->root.type != bfd_link_hash_undefweak))
10669 || !h->forced_local)
10670 && elf_hash_table (flinfo->info)->dynamic_sections_created))
10671 {
10672 if (! ((*bed->elf_backend_finish_dynamic_symbol)
10673 (flinfo->output_bfd, flinfo->info, h, &sym)))
10674 {
10675 eoinfo->failed = true;
10676 return false;
10677 }
10678 }
10679
10680 /* If we are marking the symbol as undefined, and there are no
10681 non-weak references to this symbol from a regular object, then
10682 mark the symbol as weak undefined; if there are non-weak
10683 references, mark the symbol as strong. We can't do this earlier,
10684 because it might not be marked as undefined until the
10685 finish_dynamic_symbol routine gets through with it. */
10686 if (sym.st_shndx == SHN_UNDEF
10687 && h->ref_regular
10688 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
10689 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
10690 {
10691 int bindtype;
10692 type = ELF_ST_TYPE (sym.st_info);
10693
10694 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10695 if (type == STT_GNU_IFUNC)
10696 type = STT_FUNC;
10697
10698 if (h->ref_regular_nonweak)
10699 bindtype = STB_GLOBAL;
10700 else
10701 bindtype = STB_WEAK;
10702 sym.st_info = ELF_ST_INFO (bindtype, type);
10703 }
10704
10705 /* If this is a symbol defined in a dynamic library, don't use the
10706 symbol size from the dynamic library. Relinking an executable
10707 against a new library may introduce gratuitous changes in the
10708 executable's symbols if we keep the size. */
10709 if (sym.st_shndx == SHN_UNDEF
10710 && !h->def_regular
10711 && h->def_dynamic)
10712 sym.st_size = 0;
10713
10714 /* If a non-weak symbol with non-default visibility is not defined
10715 locally, it is a fatal error. */
10716 if (!bfd_link_relocatable (flinfo->info)
10717 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
10718 && ELF_ST_BIND (sym.st_info) != STB_WEAK
10719 && h->root.type == bfd_link_hash_undefined
10720 && !h->def_regular)
10721 {
10722 const char *msg;
10723
10724 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
10725 /* xgettext:c-format */
10726 msg = _("%pB: protected symbol `%s' isn't defined");
10727 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
10728 /* xgettext:c-format */
10729 msg = _("%pB: internal symbol `%s' isn't defined");
10730 else
10731 /* xgettext:c-format */
10732 msg = _("%pB: hidden symbol `%s' isn't defined");
10733 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
10734 bfd_set_error (bfd_error_bad_value);
10735 eoinfo->failed = true;
10736 return false;
10737 }
10738
10739 /* If this symbol should be put in the .dynsym section, then put it
10740 there now. We already know the symbol index. We also fill in
10741 the entry in the .hash section. */
10742 if (h->dynindx != -1
10743 && elf_hash_table (flinfo->info)->dynamic_sections_created
10744 && elf_hash_table (flinfo->info)->dynsym != NULL
10745 && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
10746 {
10747 bfd_byte *esym;
10748
10749 /* Since there is no version information in the dynamic string,
10750 if there is no version info in symbol version section, we will
10751 have a run-time problem if not linking executable, referenced
10752 by shared library, or not bound locally. */
10753 if (h->verinfo.verdef == NULL
10754 && (!bfd_link_executable (flinfo->info)
10755 || h->ref_dynamic
10756 || !h->def_regular))
10757 {
10758 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
10759
10760 if (p && p [1] != '\0')
10761 {
10762 _bfd_error_handler
10763 /* xgettext:c-format */
10764 (_("%pB: no symbol version section for versioned symbol `%s'"),
10765 flinfo->output_bfd, h->root.root.string);
10766 eoinfo->failed = true;
10767 return false;
10768 }
10769 }
10770
10771 sym.st_name = h->dynstr_index;
10772 esym = (elf_hash_table (flinfo->info)->dynsym->contents
10773 + h->dynindx * bed->s->sizeof_sym);
10774 if (!check_dynsym (flinfo->output_bfd, &sym))
10775 {
10776 eoinfo->failed = true;
10777 return false;
10778 }
10779
10780 /* Inform the linker of the addition of this symbol. */
10781
10782 if (flinfo->info->callbacks->ctf_new_dynsym)
10783 flinfo->info->callbacks->ctf_new_dynsym (h->dynindx, &sym);
10784
10785 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
10786
10787 if (flinfo->hash_sec != NULL)
10788 {
10789 size_t hash_entry_size;
10790 bfd_byte *bucketpos;
10791 bfd_vma chain;
10792 size_t bucketcount;
10793 size_t bucket;
10794
10795 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
10796 bucket = h->u.elf_hash_value % bucketcount;
10797
10798 hash_entry_size
10799 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
10800 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
10801 + (bucket + 2) * hash_entry_size);
10802 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
10803 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
10804 bucketpos);
10805 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
10806 ((bfd_byte *) flinfo->hash_sec->contents
10807 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
10808 }
10809
10810 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
10811 {
10812 Elf_Internal_Versym iversym;
10813 Elf_External_Versym *eversym;
10814
10815 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
10816 {
10817 if (h->verinfo.verdef == NULL
10818 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
10819 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
10820 iversym.vs_vers = 1;
10821 else
10822 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
10823 }
10824 else
10825 {
10826 if (h->verinfo.vertree == NULL)
10827 iversym.vs_vers = 1;
10828 else
10829 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
10830 if (flinfo->info->create_default_symver)
10831 iversym.vs_vers++;
10832 }
10833
10834 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
10835 defined locally. */
10836 if (h->versioned == versioned_hidden && h->def_regular)
10837 iversym.vs_vers |= VERSYM_HIDDEN;
10838
10839 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
10840 eversym += h->dynindx;
10841 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
10842 }
10843 }
10844
10845 /* If the symbol is undefined, and we didn't output it to .dynsym,
10846 strip it from .symtab too. Obviously we can't do this for
10847 relocatable output or when needed for --emit-relocs. */
10848 else if (input_sec == bfd_und_section_ptr
10849 && h->indx != -2
10850 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10851 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
10852 && !bfd_link_relocatable (flinfo->info))
10853 return true;
10854
10855 /* Also strip others that we couldn't earlier due to dynamic symbol
10856 processing. */
10857 if (strip)
10858 return true;
10859 if ((input_sec->flags & SEC_EXCLUDE) != 0)
10860 return true;
10861
10862 /* Output a FILE symbol so that following locals are not associated
10863 with the wrong input file. We need one for forced local symbols
10864 if we've seen more than one FILE symbol or when we have exactly
10865 one FILE symbol but global symbols are present in a file other
10866 than the one with the FILE symbol. We also need one if linker
10867 defined symbols are present. In practice these conditions are
10868 always met, so just emit the FILE symbol unconditionally. */
10869 if (eoinfo->localsyms
10870 && !eoinfo->file_sym_done
10871 && eoinfo->flinfo->filesym_count != 0)
10872 {
10873 Elf_Internal_Sym fsym;
10874
10875 memset (&fsym, 0, sizeof (fsym));
10876 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10877 fsym.st_shndx = SHN_ABS;
10878 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10879 bfd_und_section_ptr, NULL))
10880 return false;
10881
10882 eoinfo->file_sym_done = true;
10883 }
10884
10885 indx = bfd_get_symcount (flinfo->output_bfd);
10886 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10887 input_sec, h);
10888 if (ret == 0)
10889 {
10890 eoinfo->failed = true;
10891 return false;
10892 }
10893 else if (ret == 1)
10894 h->indx = indx;
10895 else if (h->indx == -2)
10896 abort();
10897
10898 return true;
10899 }
10900
10901 /* Return TRUE if special handling is done for relocs in SEC against
10902 symbols defined in discarded sections. */
10903
10904 static bool
10905 elf_section_ignore_discarded_relocs (asection *sec)
10906 {
10907 const struct elf_backend_data *bed;
10908
10909 switch (sec->sec_info_type)
10910 {
10911 case SEC_INFO_TYPE_STABS:
10912 case SEC_INFO_TYPE_EH_FRAME:
10913 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10914 return true;
10915 default:
10916 break;
10917 }
10918
10919 bed = get_elf_backend_data (sec->owner);
10920 if (bed->elf_backend_ignore_discarded_relocs != NULL
10921 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10922 return true;
10923
10924 return false;
10925 }
10926
10927 /* Return a mask saying how ld should treat relocations in SEC against
10928 symbols defined in discarded sections. If this function returns
10929 COMPLAIN set, ld will issue a warning message. If this function
10930 returns PRETEND set, and the discarded section was link-once and the
10931 same size as the kept link-once section, ld will pretend that the
10932 symbol was actually defined in the kept section. Otherwise ld will
10933 zero the reloc (at least that is the intent, but some cooperation by
10934 the target dependent code is needed, particularly for REL targets). */
10935
10936 unsigned int
10937 _bfd_elf_default_action_discarded (asection *sec)
10938 {
10939 if (sec->flags & SEC_DEBUGGING)
10940 return PRETEND;
10941
10942 if (strcmp (".eh_frame", sec->name) == 0)
10943 return 0;
10944
10945 if (strcmp (".gcc_except_table", sec->name) == 0)
10946 return 0;
10947
10948 return COMPLAIN | PRETEND;
10949 }
10950
10951 /* Find a match between a section and a member of a section group. */
10952
10953 static asection *
10954 match_group_member (asection *sec, asection *group,
10955 struct bfd_link_info *info)
10956 {
10957 asection *first = elf_next_in_group (group);
10958 asection *s = first;
10959
10960 while (s != NULL)
10961 {
10962 if (bfd_elf_match_symbols_in_sections (s, sec, info))
10963 return s;
10964
10965 s = elf_next_in_group (s);
10966 if (s == first)
10967 break;
10968 }
10969
10970 return NULL;
10971 }
10972
10973 /* Check if the kept section of a discarded section SEC can be used
10974 to replace it. Return the replacement if it is OK. Otherwise return
10975 NULL. */
10976
10977 asection *
10978 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
10979 {
10980 asection *kept;
10981
10982 kept = sec->kept_section;
10983 if (kept != NULL)
10984 {
10985 if ((kept->flags & SEC_GROUP) != 0)
10986 kept = match_group_member (sec, kept, info);
10987 if (kept != NULL)
10988 {
10989 if ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10990 != (kept->rawsize != 0 ? kept->rawsize : kept->size))
10991 kept = NULL;
10992 else
10993 {
10994 /* Get the real kept section. */
10995 asection *next;
10996 for (next = kept->kept_section;
10997 next != NULL;
10998 next = next->kept_section)
10999 kept = next;
11000 }
11001 }
11002 sec->kept_section = kept;
11003 }
11004 return kept;
11005 }
11006
11007 /* Link an input file into the linker output file. This function
11008 handles all the sections and relocations of the input file at once.
11009 This is so that we only have to read the local symbols once, and
11010 don't have to keep them in memory. */
11011
11012 static bool
11013 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
11014 {
11015 int (*relocate_section)
11016 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
11017 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
11018 bfd *output_bfd;
11019 Elf_Internal_Shdr *symtab_hdr;
11020 size_t locsymcount;
11021 size_t extsymoff;
11022 Elf_Internal_Sym *isymbuf;
11023 Elf_Internal_Sym *isym;
11024 Elf_Internal_Sym *isymend;
11025 long *pindex;
11026 asection **ppsection;
11027 asection *o;
11028 const struct elf_backend_data *bed;
11029 struct elf_link_hash_entry **sym_hashes;
11030 bfd_size_type address_size;
11031 bfd_vma r_type_mask;
11032 int r_sym_shift;
11033 bool have_file_sym = false;
11034
11035 output_bfd = flinfo->output_bfd;
11036 bed = get_elf_backend_data (output_bfd);
11037 relocate_section = bed->elf_backend_relocate_section;
11038
11039 /* If this is a dynamic object, we don't want to do anything here:
11040 we don't want the local symbols, and we don't want the section
11041 contents. */
11042 if ((input_bfd->flags & DYNAMIC) != 0)
11043 return true;
11044
11045 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
11046 if (elf_bad_symtab (input_bfd))
11047 {
11048 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
11049 extsymoff = 0;
11050 }
11051 else
11052 {
11053 locsymcount = symtab_hdr->sh_info;
11054 extsymoff = symtab_hdr->sh_info;
11055 }
11056
11057 /* Enable GNU OSABI features in the output BFD that are used in the input
11058 BFD. */
11059 if (bed->elf_osabi == ELFOSABI_NONE
11060 || bed->elf_osabi == ELFOSABI_GNU
11061 || bed->elf_osabi == ELFOSABI_FREEBSD)
11062 elf_tdata (output_bfd)->has_gnu_osabi
11063 |= (elf_tdata (input_bfd)->has_gnu_osabi
11064 & (bfd_link_relocatable (flinfo->info)
11065 ? -1 : ~elf_gnu_osabi_retain));
11066
11067 /* Read the local symbols. */
11068 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
11069 if (isymbuf == NULL && locsymcount != 0)
11070 {
11071 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
11072 flinfo->internal_syms,
11073 flinfo->external_syms,
11074 flinfo->locsym_shndx);
11075 if (isymbuf == NULL)
11076 return false;
11077 }
11078
11079 /* Find local symbol sections and adjust values of symbols in
11080 SEC_MERGE sections. Write out those local symbols we know are
11081 going into the output file. */
11082 isymend = PTR_ADD (isymbuf, locsymcount);
11083 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
11084 isym < isymend;
11085 isym++, pindex++, ppsection++)
11086 {
11087 asection *isec;
11088 const char *name;
11089 Elf_Internal_Sym osym;
11090 long indx;
11091 int ret;
11092
11093 *pindex = -1;
11094
11095 if (elf_bad_symtab (input_bfd))
11096 {
11097 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
11098 {
11099 *ppsection = NULL;
11100 continue;
11101 }
11102 }
11103
11104 if (isym->st_shndx == SHN_UNDEF)
11105 isec = bfd_und_section_ptr;
11106 else if (isym->st_shndx == SHN_ABS)
11107 isec = bfd_abs_section_ptr;
11108 else if (isym->st_shndx == SHN_COMMON)
11109 isec = bfd_com_section_ptr;
11110 else
11111 {
11112 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
11113 if (isec == NULL)
11114 {
11115 /* Don't attempt to output symbols with st_shnx in the
11116 reserved range other than SHN_ABS and SHN_COMMON. */
11117 isec = bfd_und_section_ptr;
11118 }
11119 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
11120 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
11121 isym->st_value =
11122 _bfd_merged_section_offset (output_bfd, &isec,
11123 elf_section_data (isec)->sec_info,
11124 isym->st_value);
11125 }
11126
11127 *ppsection = isec;
11128
11129 /* Don't output the first, undefined, symbol. In fact, don't
11130 output any undefined local symbol. */
11131 if (isec == bfd_und_section_ptr)
11132 continue;
11133
11134 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
11135 {
11136 /* We never output section symbols. Instead, we use the
11137 section symbol of the corresponding section in the output
11138 file. */
11139 continue;
11140 }
11141
11142 /* If we are stripping all symbols, we don't want to output this
11143 one. */
11144 if (flinfo->info->strip == strip_all)
11145 continue;
11146
11147 /* If we are discarding all local symbols, we don't want to
11148 output this one. If we are generating a relocatable output
11149 file, then some of the local symbols may be required by
11150 relocs; we output them below as we discover that they are
11151 needed. */
11152 if (flinfo->info->discard == discard_all)
11153 continue;
11154
11155 /* If this symbol is defined in a section which we are
11156 discarding, we don't need to keep it. */
11157 if (isym->st_shndx != SHN_UNDEF
11158 && isym->st_shndx < SHN_LORESERVE
11159 && isec->output_section == NULL
11160 && flinfo->info->non_contiguous_regions
11161 && flinfo->info->non_contiguous_regions_warnings)
11162 {
11163 _bfd_error_handler (_("warning: --enable-non-contiguous-regions "
11164 "discards section `%s' from '%s'\n"),
11165 isec->name, bfd_get_filename (isec->owner));
11166 continue;
11167 }
11168
11169 if (isym->st_shndx != SHN_UNDEF
11170 && isym->st_shndx < SHN_LORESERVE
11171 && bfd_section_removed_from_list (output_bfd,
11172 isec->output_section))
11173 continue;
11174
11175 /* Get the name of the symbol. */
11176 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
11177 isym->st_name);
11178 if (name == NULL)
11179 return false;
11180
11181 /* See if we are discarding symbols with this name. */
11182 if ((flinfo->info->strip == strip_some
11183 && (bfd_hash_lookup (flinfo->info->keep_hash, name, false, false)
11184 == NULL))
11185 || (((flinfo->info->discard == discard_sec_merge
11186 && (isec->flags & SEC_MERGE)
11187 && !bfd_link_relocatable (flinfo->info))
11188 || flinfo->info->discard == discard_l)
11189 && bfd_is_local_label_name (input_bfd, name)))
11190 continue;
11191
11192 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
11193 {
11194 if (input_bfd->lto_output)
11195 /* -flto puts a temp file name here. This means builds
11196 are not reproducible. Discard the symbol. */
11197 continue;
11198 have_file_sym = true;
11199 flinfo->filesym_count += 1;
11200 }
11201 if (!have_file_sym)
11202 {
11203 /* In the absence of debug info, bfd_find_nearest_line uses
11204 FILE symbols to determine the source file for local
11205 function symbols. Provide a FILE symbol here if input
11206 files lack such, so that their symbols won't be
11207 associated with a previous input file. It's not the
11208 source file, but the best we can do. */
11209 const char *filename;
11210 have_file_sym = true;
11211 flinfo->filesym_count += 1;
11212 memset (&osym, 0, sizeof (osym));
11213 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
11214 osym.st_shndx = SHN_ABS;
11215 if (input_bfd->lto_output)
11216 filename = NULL;
11217 else
11218 filename = lbasename (bfd_get_filename (input_bfd));
11219 if (!elf_link_output_symstrtab (flinfo, filename, &osym,
11220 bfd_abs_section_ptr, NULL))
11221 return false;
11222 }
11223
11224 osym = *isym;
11225
11226 /* Adjust the section index for the output file. */
11227 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11228 isec->output_section);
11229 if (osym.st_shndx == SHN_BAD)
11230 return false;
11231
11232 /* ELF symbols in relocatable files are section relative, but
11233 in executable files they are virtual addresses. Note that
11234 this code assumes that all ELF sections have an associated
11235 BFD section with a reasonable value for output_offset; below
11236 we assume that they also have a reasonable value for
11237 output_section. Any special sections must be set up to meet
11238 these requirements. */
11239 osym.st_value += isec->output_offset;
11240 if (!bfd_link_relocatable (flinfo->info))
11241 {
11242 osym.st_value += isec->output_section->vma;
11243 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
11244 {
11245 /* STT_TLS symbols are relative to PT_TLS segment base. */
11246 if (elf_hash_table (flinfo->info)->tls_sec != NULL)
11247 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
11248 else
11249 osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
11250 STT_NOTYPE);
11251 }
11252 }
11253
11254 indx = bfd_get_symcount (output_bfd);
11255 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
11256 if (ret == 0)
11257 return false;
11258 else if (ret == 1)
11259 *pindex = indx;
11260 }
11261
11262 if (bed->s->arch_size == 32)
11263 {
11264 r_type_mask = 0xff;
11265 r_sym_shift = 8;
11266 address_size = 4;
11267 }
11268 else
11269 {
11270 r_type_mask = 0xffffffff;
11271 r_sym_shift = 32;
11272 address_size = 8;
11273 }
11274
11275 /* Relocate the contents of each section. */
11276 sym_hashes = elf_sym_hashes (input_bfd);
11277 for (o = input_bfd->sections; o != NULL; o = o->next)
11278 {
11279 bfd_byte *contents;
11280
11281 if (! o->linker_mark)
11282 {
11283 /* This section was omitted from the link. */
11284 continue;
11285 }
11286
11287 if (!flinfo->info->resolve_section_groups
11288 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
11289 {
11290 /* Deal with the group signature symbol. */
11291 struct bfd_elf_section_data *sec_data = elf_section_data (o);
11292 unsigned long symndx = sec_data->this_hdr.sh_info;
11293 asection *osec = o->output_section;
11294
11295 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
11296 if (symndx >= locsymcount
11297 || (elf_bad_symtab (input_bfd)
11298 && flinfo->sections[symndx] == NULL))
11299 {
11300 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
11301 while (h->root.type == bfd_link_hash_indirect
11302 || h->root.type == bfd_link_hash_warning)
11303 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11304 /* Arrange for symbol to be output. */
11305 h->indx = -2;
11306 elf_section_data (osec)->this_hdr.sh_info = -2;
11307 }
11308 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
11309 {
11310 /* We'll use the output section target_index. */
11311 asection *sec = flinfo->sections[symndx]->output_section;
11312 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
11313 }
11314 else
11315 {
11316 if (flinfo->indices[symndx] == -1)
11317 {
11318 /* Otherwise output the local symbol now. */
11319 Elf_Internal_Sym sym = isymbuf[symndx];
11320 asection *sec = flinfo->sections[symndx]->output_section;
11321 const char *name;
11322 long indx;
11323 int ret;
11324
11325 name = bfd_elf_string_from_elf_section (input_bfd,
11326 symtab_hdr->sh_link,
11327 sym.st_name);
11328 if (name == NULL)
11329 return false;
11330
11331 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11332 sec);
11333 if (sym.st_shndx == SHN_BAD)
11334 return false;
11335
11336 sym.st_value += o->output_offset;
11337
11338 indx = bfd_get_symcount (output_bfd);
11339 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
11340 NULL);
11341 if (ret == 0)
11342 return false;
11343 else if (ret == 1)
11344 flinfo->indices[symndx] = indx;
11345 else
11346 abort ();
11347 }
11348 elf_section_data (osec)->this_hdr.sh_info
11349 = flinfo->indices[symndx];
11350 }
11351 }
11352
11353 if ((o->flags & SEC_HAS_CONTENTS) == 0
11354 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
11355 continue;
11356
11357 if ((o->flags & SEC_LINKER_CREATED) != 0)
11358 {
11359 /* Section was created by _bfd_elf_link_create_dynamic_sections
11360 or somesuch. */
11361 continue;
11362 }
11363
11364 /* Get the contents of the section. They have been cached by a
11365 relaxation routine. Note that o is a section in an input
11366 file, so the contents field will not have been set by any of
11367 the routines which work on output files. */
11368 if (elf_section_data (o)->this_hdr.contents != NULL)
11369 {
11370 contents = elf_section_data (o)->this_hdr.contents;
11371 if (bed->caches_rawsize
11372 && o->rawsize != 0
11373 && o->rawsize < o->size)
11374 {
11375 memcpy (flinfo->contents, contents, o->rawsize);
11376 contents = flinfo->contents;
11377 }
11378 }
11379 else
11380 {
11381 contents = flinfo->contents;
11382 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
11383 return false;
11384 }
11385
11386 if ((o->flags & SEC_RELOC) != 0)
11387 {
11388 Elf_Internal_Rela *internal_relocs;
11389 Elf_Internal_Rela *rel, *relend;
11390 int action_discarded;
11391 int ret;
11392
11393 /* Get the swapped relocs. */
11394 internal_relocs
11395 = _bfd_elf_link_info_read_relocs (input_bfd, flinfo->info, o,
11396 flinfo->external_relocs,
11397 flinfo->internal_relocs,
11398 false);
11399 if (internal_relocs == NULL
11400 && o->reloc_count > 0)
11401 return false;
11402
11403 action_discarded = -1;
11404 if (!elf_section_ignore_discarded_relocs (o))
11405 action_discarded = (*bed->action_discarded) (o);
11406
11407 /* Run through the relocs evaluating complex reloc symbols and
11408 looking for relocs against symbols from discarded sections
11409 or section symbols from removed link-once sections.
11410 Complain about relocs against discarded sections. Zero
11411 relocs against removed link-once sections. */
11412
11413 rel = internal_relocs;
11414 relend = rel + o->reloc_count;
11415 for ( ; rel < relend; rel++)
11416 {
11417 unsigned long r_symndx = rel->r_info >> r_sym_shift;
11418 unsigned int s_type;
11419 asection **ps, *sec;
11420 struct elf_link_hash_entry *h = NULL;
11421 const char *sym_name;
11422
11423 if (r_symndx == STN_UNDEF)
11424 continue;
11425
11426 if (r_symndx >= locsymcount
11427 || (elf_bad_symtab (input_bfd)
11428 && flinfo->sections[r_symndx] == NULL))
11429 {
11430 h = sym_hashes[r_symndx - extsymoff];
11431
11432 /* Badly formatted input files can contain relocs that
11433 reference non-existant symbols. Check here so that
11434 we do not seg fault. */
11435 if (h == NULL)
11436 {
11437 _bfd_error_handler
11438 /* xgettext:c-format */
11439 (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
11440 "that references a non-existent global symbol"),
11441 input_bfd, (uint64_t) rel->r_info, o);
11442 bfd_set_error (bfd_error_bad_value);
11443 return false;
11444 }
11445
11446 while (h->root.type == bfd_link_hash_indirect
11447 || h->root.type == bfd_link_hash_warning)
11448 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11449
11450 s_type = h->type;
11451
11452 /* If a plugin symbol is referenced from a non-IR file,
11453 mark the symbol as undefined. Note that the
11454 linker may attach linker created dynamic sections
11455 to the plugin bfd. Symbols defined in linker
11456 created sections are not plugin symbols. */
11457 if ((h->root.non_ir_ref_regular
11458 || h->root.non_ir_ref_dynamic)
11459 && (h->root.type == bfd_link_hash_defined
11460 || h->root.type == bfd_link_hash_defweak)
11461 && (h->root.u.def.section->flags
11462 & SEC_LINKER_CREATED) == 0
11463 && h->root.u.def.section->owner != NULL
11464 && (h->root.u.def.section->owner->flags
11465 & BFD_PLUGIN) != 0)
11466 {
11467 h->root.type = bfd_link_hash_undefined;
11468 h->root.u.undef.abfd = h->root.u.def.section->owner;
11469 }
11470
11471 ps = NULL;
11472 if (h->root.type == bfd_link_hash_defined
11473 || h->root.type == bfd_link_hash_defweak)
11474 ps = &h->root.u.def.section;
11475
11476 sym_name = h->root.root.string;
11477 }
11478 else
11479 {
11480 Elf_Internal_Sym *sym = isymbuf + r_symndx;
11481
11482 s_type = ELF_ST_TYPE (sym->st_info);
11483 ps = &flinfo->sections[r_symndx];
11484 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
11485 sym, *ps);
11486 }
11487
11488 if ((s_type == STT_RELC || s_type == STT_SRELC)
11489 && !bfd_link_relocatable (flinfo->info))
11490 {
11491 bfd_vma val;
11492 bfd_vma dot = (rel->r_offset
11493 + o->output_offset + o->output_section->vma);
11494 #ifdef DEBUG
11495 printf ("Encountered a complex symbol!");
11496 printf (" (input_bfd %s, section %s, reloc %ld\n",
11497 bfd_get_filename (input_bfd), o->name,
11498 (long) (rel - internal_relocs));
11499 printf (" symbol: idx %8.8lx, name %s\n",
11500 r_symndx, sym_name);
11501 printf (" reloc : info %8.8lx, addr %8.8lx\n",
11502 (unsigned long) rel->r_info,
11503 (unsigned long) rel->r_offset);
11504 #endif
11505 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
11506 isymbuf, locsymcount, s_type == STT_SRELC))
11507 return false;
11508
11509 /* Symbol evaluated OK. Update to absolute value. */
11510 set_symbol_value (input_bfd, isymbuf, locsymcount,
11511 r_symndx, val);
11512 continue;
11513 }
11514
11515 if (action_discarded != -1 && ps != NULL)
11516 {
11517 /* Complain if the definition comes from a
11518 discarded section. */
11519 if ((sec = *ps) != NULL && discarded_section (sec))
11520 {
11521 BFD_ASSERT (r_symndx != STN_UNDEF);
11522 if (action_discarded & COMPLAIN)
11523 (*flinfo->info->callbacks->einfo)
11524 /* xgettext:c-format */
11525 (_("%X`%s' referenced in section `%pA' of %pB: "
11526 "defined in discarded section `%pA' of %pB\n"),
11527 sym_name, o, input_bfd, sec, sec->owner);
11528
11529 /* Try to do the best we can to support buggy old
11530 versions of gcc. Pretend that the symbol is
11531 really defined in the kept linkonce section.
11532 FIXME: This is quite broken. Modifying the
11533 symbol here means we will be changing all later
11534 uses of the symbol, not just in this section. */
11535 if (action_discarded & PRETEND)
11536 {
11537 asection *kept;
11538
11539 kept = _bfd_elf_check_kept_section (sec,
11540 flinfo->info);
11541 if (kept != NULL)
11542 {
11543 *ps = kept;
11544 continue;
11545 }
11546 }
11547 }
11548 }
11549 }
11550
11551 /* Relocate the section by invoking a back end routine.
11552
11553 The back end routine is responsible for adjusting the
11554 section contents as necessary, and (if using Rela relocs
11555 and generating a relocatable output file) adjusting the
11556 reloc addend as necessary.
11557
11558 The back end routine does not have to worry about setting
11559 the reloc address or the reloc symbol index.
11560
11561 The back end routine is given a pointer to the swapped in
11562 internal symbols, and can access the hash table entries
11563 for the external symbols via elf_sym_hashes (input_bfd).
11564
11565 When generating relocatable output, the back end routine
11566 must handle STB_LOCAL/STT_SECTION symbols specially. The
11567 output symbol is going to be a section symbol
11568 corresponding to the output section, which will require
11569 the addend to be adjusted. */
11570
11571 ret = (*relocate_section) (output_bfd, flinfo->info,
11572 input_bfd, o, contents,
11573 internal_relocs,
11574 isymbuf,
11575 flinfo->sections);
11576 if (!ret)
11577 return false;
11578
11579 if (ret == 2
11580 || bfd_link_relocatable (flinfo->info)
11581 || flinfo->info->emitrelocations)
11582 {
11583 Elf_Internal_Rela *irela;
11584 Elf_Internal_Rela *irelaend, *irelamid;
11585 bfd_vma last_offset;
11586 struct elf_link_hash_entry **rel_hash;
11587 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
11588 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
11589 unsigned int next_erel;
11590 bool rela_normal;
11591 struct bfd_elf_section_data *esdi, *esdo;
11592
11593 esdi = elf_section_data (o);
11594 esdo = elf_section_data (o->output_section);
11595 rela_normal = false;
11596
11597 /* Adjust the reloc addresses and symbol indices. */
11598
11599 irela = internal_relocs;
11600 irelaend = irela + o->reloc_count;
11601 rel_hash = PTR_ADD (esdo->rel.hashes, esdo->rel.count);
11602 /* We start processing the REL relocs, if any. When we reach
11603 IRELAMID in the loop, we switch to the RELA relocs. */
11604 irelamid = irela;
11605 if (esdi->rel.hdr != NULL)
11606 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
11607 * bed->s->int_rels_per_ext_rel);
11608 rel_hash_list = rel_hash;
11609 rela_hash_list = NULL;
11610 last_offset = o->output_offset;
11611 if (!bfd_link_relocatable (flinfo->info))
11612 last_offset += o->output_section->vma;
11613 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
11614 {
11615 unsigned long r_symndx;
11616 asection *sec;
11617 Elf_Internal_Sym sym;
11618
11619 if (next_erel == bed->s->int_rels_per_ext_rel)
11620 {
11621 rel_hash++;
11622 next_erel = 0;
11623 }
11624
11625 if (irela == irelamid)
11626 {
11627 rel_hash = PTR_ADD (esdo->rela.hashes, esdo->rela.count);
11628 rela_hash_list = rel_hash;
11629 rela_normal = bed->rela_normal;
11630 }
11631
11632 irela->r_offset = _bfd_elf_section_offset (output_bfd,
11633 flinfo->info, o,
11634 irela->r_offset);
11635 if (irela->r_offset >= (bfd_vma) -2)
11636 {
11637 /* This is a reloc for a deleted entry or somesuch.
11638 Turn it into an R_*_NONE reloc, at the same
11639 offset as the last reloc. elf_eh_frame.c and
11640 bfd_elf_discard_info rely on reloc offsets
11641 being ordered. */
11642 irela->r_offset = last_offset;
11643 irela->r_info = 0;
11644 irela->r_addend = 0;
11645 continue;
11646 }
11647
11648 irela->r_offset += o->output_offset;
11649
11650 /* Relocs in an executable have to be virtual addresses. */
11651 if (!bfd_link_relocatable (flinfo->info))
11652 irela->r_offset += o->output_section->vma;
11653
11654 last_offset = irela->r_offset;
11655
11656 r_symndx = irela->r_info >> r_sym_shift;
11657 if (r_symndx == STN_UNDEF)
11658 continue;
11659
11660 if (r_symndx >= locsymcount
11661 || (elf_bad_symtab (input_bfd)
11662 && flinfo->sections[r_symndx] == NULL))
11663 {
11664 struct elf_link_hash_entry *rh;
11665 unsigned long indx;
11666
11667 /* This is a reloc against a global symbol. We
11668 have not yet output all the local symbols, so
11669 we do not know the symbol index of any global
11670 symbol. We set the rel_hash entry for this
11671 reloc to point to the global hash table entry
11672 for this symbol. The symbol index is then
11673 set at the end of bfd_elf_final_link. */
11674 indx = r_symndx - extsymoff;
11675 rh = elf_sym_hashes (input_bfd)[indx];
11676 while (rh->root.type == bfd_link_hash_indirect
11677 || rh->root.type == bfd_link_hash_warning)
11678 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
11679
11680 /* Setting the index to -2 tells
11681 elf_link_output_extsym that this symbol is
11682 used by a reloc. */
11683 BFD_ASSERT (rh->indx < 0);
11684 rh->indx = -2;
11685 *rel_hash = rh;
11686
11687 continue;
11688 }
11689
11690 /* This is a reloc against a local symbol. */
11691
11692 *rel_hash = NULL;
11693 sym = isymbuf[r_symndx];
11694 sec = flinfo->sections[r_symndx];
11695 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
11696 {
11697 /* I suppose the backend ought to fill in the
11698 section of any STT_SECTION symbol against a
11699 processor specific section. */
11700 r_symndx = STN_UNDEF;
11701 if (bfd_is_abs_section (sec))
11702 ;
11703 else if (sec == NULL || sec->owner == NULL)
11704 {
11705 bfd_set_error (bfd_error_bad_value);
11706 return false;
11707 }
11708 else
11709 {
11710 asection *osec = sec->output_section;
11711
11712 /* If we have discarded a section, the output
11713 section will be the absolute section. In
11714 case of discarded SEC_MERGE sections, use
11715 the kept section. relocate_section should
11716 have already handled discarded linkonce
11717 sections. */
11718 if (bfd_is_abs_section (osec)
11719 && sec->kept_section != NULL
11720 && sec->kept_section->output_section != NULL)
11721 {
11722 osec = sec->kept_section->output_section;
11723 irela->r_addend -= osec->vma;
11724 }
11725
11726 if (!bfd_is_abs_section (osec))
11727 {
11728 r_symndx = osec->target_index;
11729 if (r_symndx == STN_UNDEF)
11730 {
11731 irela->r_addend += osec->vma;
11732 osec = _bfd_nearby_section (output_bfd, osec,
11733 osec->vma);
11734 irela->r_addend -= osec->vma;
11735 r_symndx = osec->target_index;
11736 }
11737 }
11738 }
11739
11740 /* Adjust the addend according to where the
11741 section winds up in the output section. */
11742 if (rela_normal)
11743 irela->r_addend += sec->output_offset;
11744 }
11745 else
11746 {
11747 if (flinfo->indices[r_symndx] == -1)
11748 {
11749 unsigned long shlink;
11750 const char *name;
11751 asection *osec;
11752 long indx;
11753
11754 if (flinfo->info->strip == strip_all)
11755 {
11756 /* You can't do ld -r -s. */
11757 bfd_set_error (bfd_error_invalid_operation);
11758 return false;
11759 }
11760
11761 /* This symbol was skipped earlier, but
11762 since it is needed by a reloc, we
11763 must output it now. */
11764 shlink = symtab_hdr->sh_link;
11765 name = (bfd_elf_string_from_elf_section
11766 (input_bfd, shlink, sym.st_name));
11767 if (name == NULL)
11768 return false;
11769
11770 osec = sec->output_section;
11771 sym.st_shndx =
11772 _bfd_elf_section_from_bfd_section (output_bfd,
11773 osec);
11774 if (sym.st_shndx == SHN_BAD)
11775 return false;
11776
11777 sym.st_value += sec->output_offset;
11778 if (!bfd_link_relocatable (flinfo->info))
11779 {
11780 sym.st_value += osec->vma;
11781 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
11782 {
11783 struct elf_link_hash_table *htab
11784 = elf_hash_table (flinfo->info);
11785
11786 /* STT_TLS symbols are relative to PT_TLS
11787 segment base. */
11788 if (htab->tls_sec != NULL)
11789 sym.st_value -= htab->tls_sec->vma;
11790 else
11791 sym.st_info
11792 = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
11793 STT_NOTYPE);
11794 }
11795 }
11796
11797 indx = bfd_get_symcount (output_bfd);
11798 ret = elf_link_output_symstrtab (flinfo, name,
11799 &sym, sec,
11800 NULL);
11801 if (ret == 0)
11802 return false;
11803 else if (ret == 1)
11804 flinfo->indices[r_symndx] = indx;
11805 else
11806 abort ();
11807 }
11808
11809 r_symndx = flinfo->indices[r_symndx];
11810 }
11811
11812 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
11813 | (irela->r_info & r_type_mask));
11814 }
11815
11816 /* Swap out the relocs. */
11817 input_rel_hdr = esdi->rel.hdr;
11818 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
11819 {
11820 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11821 input_rel_hdr,
11822 internal_relocs,
11823 rel_hash_list))
11824 return false;
11825 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
11826 * bed->s->int_rels_per_ext_rel);
11827 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
11828 }
11829
11830 input_rela_hdr = esdi->rela.hdr;
11831 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
11832 {
11833 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11834 input_rela_hdr,
11835 internal_relocs,
11836 rela_hash_list))
11837 return false;
11838 }
11839 }
11840 }
11841
11842 /* Write out the modified section contents. */
11843 if (bed->elf_backend_write_section
11844 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
11845 contents))
11846 {
11847 /* Section written out. */
11848 }
11849 else switch (o->sec_info_type)
11850 {
11851 case SEC_INFO_TYPE_STABS:
11852 if (! (_bfd_write_section_stabs
11853 (output_bfd,
11854 &elf_hash_table (flinfo->info)->stab_info,
11855 o, &elf_section_data (o)->sec_info, contents)))
11856 return false;
11857 break;
11858 case SEC_INFO_TYPE_MERGE:
11859 if (! _bfd_write_merged_section (output_bfd, o,
11860 elf_section_data (o)->sec_info))
11861 return false;
11862 break;
11863 case SEC_INFO_TYPE_EH_FRAME:
11864 {
11865 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
11866 o, contents))
11867 return false;
11868 }
11869 break;
11870 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11871 {
11872 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11873 flinfo->info,
11874 o, contents))
11875 return false;
11876 }
11877 break;
11878 default:
11879 {
11880 if (! (o->flags & SEC_EXCLUDE))
11881 {
11882 file_ptr offset = (file_ptr) o->output_offset;
11883 bfd_size_type todo = o->size;
11884
11885 offset *= bfd_octets_per_byte (output_bfd, o);
11886
11887 if ((o->flags & SEC_ELF_REVERSE_COPY)
11888 && o->size > address_size)
11889 {
11890 /* Reverse-copy input section to output. */
11891
11892 if ((o->size & (address_size - 1)) != 0
11893 || (o->reloc_count != 0
11894 && (o->size * bed->s->int_rels_per_ext_rel
11895 != o->reloc_count * address_size)))
11896 {
11897 _bfd_error_handler
11898 /* xgettext:c-format */
11899 (_("error: %pB: size of section %pA is not "
11900 "multiple of address size"),
11901 input_bfd, o);
11902 bfd_set_error (bfd_error_bad_value);
11903 return false;
11904 }
11905
11906 do
11907 {
11908 todo -= address_size;
11909 if (! bfd_set_section_contents (output_bfd,
11910 o->output_section,
11911 contents + todo,
11912 offset,
11913 address_size))
11914 return false;
11915 if (todo == 0)
11916 break;
11917 offset += address_size;
11918 }
11919 while (1);
11920 }
11921 else if (! bfd_set_section_contents (output_bfd,
11922 o->output_section,
11923 contents,
11924 offset, todo))
11925 return false;
11926 }
11927 }
11928 break;
11929 }
11930 }
11931
11932 return true;
11933 }
11934
11935 /* Generate a reloc when linking an ELF file. This is a reloc
11936 requested by the linker, and does not come from any input file. This
11937 is used to build constructor and destructor tables when linking
11938 with -Ur. */
11939
11940 static bool
11941 elf_reloc_link_order (bfd *output_bfd,
11942 struct bfd_link_info *info,
11943 asection *output_section,
11944 struct bfd_link_order *link_order)
11945 {
11946 reloc_howto_type *howto;
11947 long indx;
11948 bfd_vma offset;
11949 bfd_vma addend;
11950 struct bfd_elf_section_reloc_data *reldata;
11951 struct elf_link_hash_entry **rel_hash_ptr;
11952 Elf_Internal_Shdr *rel_hdr;
11953 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11954 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11955 bfd_byte *erel;
11956 unsigned int i;
11957 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
11958
11959 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11960 if (howto == NULL)
11961 {
11962 bfd_set_error (bfd_error_bad_value);
11963 return false;
11964 }
11965
11966 addend = link_order->u.reloc.p->addend;
11967
11968 if (esdo->rel.hdr)
11969 reldata = &esdo->rel;
11970 else if (esdo->rela.hdr)
11971 reldata = &esdo->rela;
11972 else
11973 {
11974 reldata = NULL;
11975 BFD_ASSERT (0);
11976 }
11977
11978 /* Figure out the symbol index. */
11979 rel_hash_ptr = reldata->hashes + reldata->count;
11980 if (link_order->type == bfd_section_reloc_link_order)
11981 {
11982 indx = link_order->u.reloc.p->u.section->target_index;
11983 BFD_ASSERT (indx != 0);
11984 *rel_hash_ptr = NULL;
11985 }
11986 else
11987 {
11988 struct elf_link_hash_entry *h;
11989
11990 /* Treat a reloc against a defined symbol as though it were
11991 actually against the section. */
11992 h = ((struct elf_link_hash_entry *)
11993 bfd_wrapped_link_hash_lookup (output_bfd, info,
11994 link_order->u.reloc.p->u.name,
11995 false, false, true));
11996 if (h != NULL
11997 && (h->root.type == bfd_link_hash_defined
11998 || h->root.type == bfd_link_hash_defweak))
11999 {
12000 asection *section;
12001
12002 section = h->root.u.def.section;
12003 indx = section->output_section->target_index;
12004 *rel_hash_ptr = NULL;
12005 /* It seems that we ought to add the symbol value to the
12006 addend here, but in practice it has already been added
12007 because it was passed to constructor_callback. */
12008 addend += section->output_section->vma + section->output_offset;
12009 }
12010 else if (h != NULL)
12011 {
12012 /* Setting the index to -2 tells elf_link_output_extsym that
12013 this symbol is used by a reloc. */
12014 h->indx = -2;
12015 *rel_hash_ptr = h;
12016 indx = 0;
12017 }
12018 else
12019 {
12020 (*info->callbacks->unattached_reloc)
12021 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
12022 indx = 0;
12023 }
12024 }
12025
12026 /* If this is an inplace reloc, we must write the addend into the
12027 object file. */
12028 if (howto->partial_inplace && addend != 0)
12029 {
12030 bfd_size_type size;
12031 bfd_reloc_status_type rstat;
12032 bfd_byte *buf;
12033 bool ok;
12034 const char *sym_name;
12035 bfd_size_type octets;
12036
12037 size = (bfd_size_type) bfd_get_reloc_size (howto);
12038 buf = (bfd_byte *) bfd_zmalloc (size);
12039 if (buf == NULL && size != 0)
12040 return false;
12041 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
12042 switch (rstat)
12043 {
12044 case bfd_reloc_ok:
12045 break;
12046
12047 default:
12048 case bfd_reloc_outofrange:
12049 abort ();
12050
12051 case bfd_reloc_overflow:
12052 if (link_order->type == bfd_section_reloc_link_order)
12053 sym_name = bfd_section_name (link_order->u.reloc.p->u.section);
12054 else
12055 sym_name = link_order->u.reloc.p->u.name;
12056 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
12057 howto->name, addend, NULL, NULL,
12058 (bfd_vma) 0);
12059 break;
12060 }
12061
12062 octets = link_order->offset * bfd_octets_per_byte (output_bfd,
12063 output_section);
12064 ok = bfd_set_section_contents (output_bfd, output_section, buf,
12065 octets, size);
12066 free (buf);
12067 if (! ok)
12068 return false;
12069 }
12070
12071 /* The address of a reloc is relative to the section in a
12072 relocatable file, and is a virtual address in an executable
12073 file. */
12074 offset = link_order->offset;
12075 if (! bfd_link_relocatable (info))
12076 offset += output_section->vma;
12077
12078 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
12079 {
12080 irel[i].r_offset = offset;
12081 irel[i].r_info = 0;
12082 irel[i].r_addend = 0;
12083 }
12084 if (bed->s->arch_size == 32)
12085 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
12086 else
12087 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
12088
12089 rel_hdr = reldata->hdr;
12090 erel = rel_hdr->contents;
12091 if (rel_hdr->sh_type == SHT_REL)
12092 {
12093 erel += reldata->count * bed->s->sizeof_rel;
12094 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
12095 }
12096 else
12097 {
12098 irel[0].r_addend = addend;
12099 erel += reldata->count * bed->s->sizeof_rela;
12100 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
12101 }
12102
12103 ++reldata->count;
12104
12105 return true;
12106 }
12107
12108 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
12109 Returns TRUE upon success, FALSE otherwise. */
12110
12111 static bool
12112 elf_output_implib (bfd *abfd, struct bfd_link_info *info)
12113 {
12114 bool ret = false;
12115 bfd *implib_bfd;
12116 const struct elf_backend_data *bed;
12117 flagword flags;
12118 enum bfd_architecture arch;
12119 unsigned int mach;
12120 asymbol **sympp = NULL;
12121 long symsize;
12122 long symcount;
12123 long src_count;
12124 elf_symbol_type *osymbuf;
12125 size_t amt;
12126
12127 implib_bfd = info->out_implib_bfd;
12128 bed = get_elf_backend_data (abfd);
12129
12130 if (!bfd_set_format (implib_bfd, bfd_object))
12131 return false;
12132
12133 /* Use flag from executable but make it a relocatable object. */
12134 flags = bfd_get_file_flags (abfd);
12135 flags &= ~HAS_RELOC;
12136 if (!bfd_set_start_address (implib_bfd, 0)
12137 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
12138 return false;
12139
12140 /* Copy architecture of output file to import library file. */
12141 arch = bfd_get_arch (abfd);
12142 mach = bfd_get_mach (abfd);
12143 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
12144 && (abfd->target_defaulted
12145 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
12146 return false;
12147
12148 /* Get symbol table size. */
12149 symsize = bfd_get_symtab_upper_bound (abfd);
12150 if (symsize < 0)
12151 return false;
12152
12153 /* Read in the symbol table. */
12154 sympp = (asymbol **) bfd_malloc (symsize);
12155 if (sympp == NULL)
12156 return false;
12157
12158 symcount = bfd_canonicalize_symtab (abfd, sympp);
12159 if (symcount < 0)
12160 goto free_sym_buf;
12161
12162 /* Allow the BFD backend to copy any private header data it
12163 understands from the output BFD to the import library BFD. */
12164 if (! bfd_copy_private_header_data (abfd, implib_bfd))
12165 goto free_sym_buf;
12166
12167 /* Filter symbols to appear in the import library. */
12168 if (bed->elf_backend_filter_implib_symbols)
12169 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
12170 symcount);
12171 else
12172 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
12173 if (symcount == 0)
12174 {
12175 bfd_set_error (bfd_error_no_symbols);
12176 _bfd_error_handler (_("%pB: no symbol found for import library"),
12177 implib_bfd);
12178 goto free_sym_buf;
12179 }
12180
12181
12182 /* Make symbols absolute. */
12183 amt = symcount * sizeof (*osymbuf);
12184 osymbuf = (elf_symbol_type *) bfd_alloc (implib_bfd, amt);
12185 if (osymbuf == NULL)
12186 goto free_sym_buf;
12187
12188 for (src_count = 0; src_count < symcount; src_count++)
12189 {
12190 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
12191 sizeof (*osymbuf));
12192 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
12193 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
12194 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
12195 osymbuf[src_count].internal_elf_sym.st_value =
12196 osymbuf[src_count].symbol.value;
12197 sympp[src_count] = &osymbuf[src_count].symbol;
12198 }
12199
12200 bfd_set_symtab (implib_bfd, sympp, symcount);
12201
12202 /* Allow the BFD backend to copy any private data it understands
12203 from the output BFD to the import library BFD. This is done last
12204 to permit the routine to look at the filtered symbol table. */
12205 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
12206 goto free_sym_buf;
12207
12208 if (!bfd_close (implib_bfd))
12209 goto free_sym_buf;
12210
12211 ret = true;
12212
12213 free_sym_buf:
12214 free (sympp);
12215 return ret;
12216 }
12217
12218 static void
12219 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
12220 {
12221 asection *o;
12222
12223 if (flinfo->symstrtab != NULL)
12224 _bfd_elf_strtab_free (flinfo->symstrtab);
12225 free (flinfo->contents);
12226 free (flinfo->external_relocs);
12227 free (flinfo->internal_relocs);
12228 free (flinfo->external_syms);
12229 free (flinfo->locsym_shndx);
12230 free (flinfo->internal_syms);
12231 free (flinfo->indices);
12232 free (flinfo->sections);
12233 if (flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1)
12234 free (flinfo->symshndxbuf);
12235 for (o = obfd->sections; o != NULL; o = o->next)
12236 {
12237 struct bfd_elf_section_data *esdo = elf_section_data (o);
12238 free (esdo->rel.hashes);
12239 free (esdo->rela.hashes);
12240 }
12241 }
12242
12243 /* Do the final step of an ELF link. */
12244
12245 bool
12246 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
12247 {
12248 bool dynamic;
12249 bool emit_relocs;
12250 bfd *dynobj;
12251 struct elf_final_link_info flinfo;
12252 asection *o;
12253 struct bfd_link_order *p;
12254 bfd *sub;
12255 bfd_size_type max_contents_size;
12256 bfd_size_type max_external_reloc_size;
12257 bfd_size_type max_internal_reloc_count;
12258 bfd_size_type max_sym_count;
12259 bfd_size_type max_sym_shndx_count;
12260 Elf_Internal_Sym elfsym;
12261 unsigned int i;
12262 Elf_Internal_Shdr *symtab_hdr;
12263 Elf_Internal_Shdr *symtab_shndx_hdr;
12264 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12265 struct elf_outext_info eoinfo;
12266 bool merged;
12267 size_t relativecount;
12268 size_t relr_entsize;
12269 asection *reldyn = 0;
12270 bfd_size_type amt;
12271 asection *attr_section = NULL;
12272 bfd_vma attr_size = 0;
12273 const char *std_attrs_section;
12274 struct elf_link_hash_table *htab = elf_hash_table (info);
12275 bool sections_removed;
12276 bool ret;
12277
12278 if (!is_elf_hash_table (&htab->root))
12279 return false;
12280
12281 if (bfd_link_pic (info))
12282 abfd->flags |= DYNAMIC;
12283
12284 dynamic = htab->dynamic_sections_created;
12285 dynobj = htab->dynobj;
12286
12287 emit_relocs = (bfd_link_relocatable (info)
12288 || info->emitrelocations);
12289
12290 memset (&flinfo, 0, sizeof (flinfo));
12291 flinfo.info = info;
12292 flinfo.output_bfd = abfd;
12293 flinfo.symstrtab = _bfd_elf_strtab_init ();
12294 if (flinfo.symstrtab == NULL)
12295 return false;
12296
12297 if (! dynamic)
12298 {
12299 flinfo.hash_sec = NULL;
12300 flinfo.symver_sec = NULL;
12301 }
12302 else
12303 {
12304 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
12305 /* Note that dynsym_sec can be NULL (on VMS). */
12306 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
12307 /* Note that it is OK if symver_sec is NULL. */
12308 }
12309
12310 if (info->unique_symbol
12311 && !bfd_hash_table_init (&flinfo.local_hash_table,
12312 local_hash_newfunc,
12313 sizeof (struct local_hash_entry)))
12314 return false;
12315
12316 /* The object attributes have been merged. Remove the input
12317 sections from the link, and set the contents of the output
12318 section. */
12319 sections_removed = false;
12320 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
12321 for (o = abfd->sections; o != NULL; o = o->next)
12322 {
12323 bool remove_section = false;
12324
12325 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
12326 || strcmp (o->name, ".gnu.attributes") == 0)
12327 {
12328 for (p = o->map_head.link_order; p != NULL; p = p->next)
12329 {
12330 asection *input_section;
12331
12332 if (p->type != bfd_indirect_link_order)
12333 continue;
12334 input_section = p->u.indirect.section;
12335 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12336 elf_link_input_bfd ignores this section. */
12337 input_section->flags &= ~SEC_HAS_CONTENTS;
12338 }
12339
12340 attr_size = bfd_elf_obj_attr_size (abfd);
12341 bfd_set_section_size (o, attr_size);
12342 /* Skip this section later on. */
12343 o->map_head.link_order = NULL;
12344 if (attr_size)
12345 attr_section = o;
12346 else
12347 remove_section = true;
12348 }
12349 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
12350 {
12351 /* Remove empty group section from linker output. */
12352 remove_section = true;
12353 }
12354 if (remove_section)
12355 {
12356 o->flags |= SEC_EXCLUDE;
12357 bfd_section_list_remove (abfd, o);
12358 abfd->section_count--;
12359 sections_removed = true;
12360 }
12361 }
12362 if (sections_removed)
12363 _bfd_fix_excluded_sec_syms (abfd, info);
12364
12365 /* Count up the number of relocations we will output for each output
12366 section, so that we know the sizes of the reloc sections. We
12367 also figure out some maximum sizes. */
12368 max_contents_size = 0;
12369 max_external_reloc_size = 0;
12370 max_internal_reloc_count = 0;
12371 max_sym_count = 0;
12372 max_sym_shndx_count = 0;
12373 merged = false;
12374 for (o = abfd->sections; o != NULL; o = o->next)
12375 {
12376 struct bfd_elf_section_data *esdo = elf_section_data (o);
12377 o->reloc_count = 0;
12378
12379 for (p = o->map_head.link_order; p != NULL; p = p->next)
12380 {
12381 unsigned int reloc_count = 0;
12382 unsigned int additional_reloc_count = 0;
12383 struct bfd_elf_section_data *esdi = NULL;
12384
12385 if (p->type == bfd_section_reloc_link_order
12386 || p->type == bfd_symbol_reloc_link_order)
12387 reloc_count = 1;
12388 else if (p->type == bfd_indirect_link_order)
12389 {
12390 asection *sec;
12391
12392 sec = p->u.indirect.section;
12393
12394 /* Mark all sections which are to be included in the
12395 link. This will normally be every section. We need
12396 to do this so that we can identify any sections which
12397 the linker has decided to not include. */
12398 sec->linker_mark = true;
12399
12400 if (sec->flags & SEC_MERGE)
12401 merged = true;
12402
12403 if (sec->rawsize > max_contents_size)
12404 max_contents_size = sec->rawsize;
12405 if (sec->size > max_contents_size)
12406 max_contents_size = sec->size;
12407
12408 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
12409 && (sec->owner->flags & DYNAMIC) == 0)
12410 {
12411 size_t sym_count;
12412
12413 /* We are interested in just local symbols, not all
12414 symbols. */
12415 if (elf_bad_symtab (sec->owner))
12416 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
12417 / bed->s->sizeof_sym);
12418 else
12419 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
12420
12421 if (sym_count > max_sym_count)
12422 max_sym_count = sym_count;
12423
12424 if (sym_count > max_sym_shndx_count
12425 && elf_symtab_shndx_list (sec->owner) != NULL)
12426 max_sym_shndx_count = sym_count;
12427
12428 esdi = elf_section_data (sec);
12429
12430 if (esdi->this_hdr.sh_type == SHT_REL
12431 || esdi->this_hdr.sh_type == SHT_RELA)
12432 /* Some backends use reloc_count in relocation sections
12433 to count particular types of relocs. Of course,
12434 reloc sections themselves can't have relocations. */
12435 ;
12436 else if (emit_relocs)
12437 {
12438 reloc_count = sec->reloc_count;
12439 if (bed->elf_backend_count_additional_relocs)
12440 {
12441 int c;
12442 c = (*bed->elf_backend_count_additional_relocs) (sec);
12443 additional_reloc_count += c;
12444 }
12445 }
12446 else if (bed->elf_backend_count_relocs)
12447 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
12448
12449 if ((sec->flags & SEC_RELOC) != 0)
12450 {
12451 size_t ext_size = 0;
12452
12453 if (esdi->rel.hdr != NULL)
12454 ext_size = esdi->rel.hdr->sh_size;
12455 if (esdi->rela.hdr != NULL)
12456 ext_size += esdi->rela.hdr->sh_size;
12457
12458 if (ext_size > max_external_reloc_size)
12459 max_external_reloc_size = ext_size;
12460 if (sec->reloc_count > max_internal_reloc_count)
12461 max_internal_reloc_count = sec->reloc_count;
12462 }
12463 }
12464 }
12465
12466 if (reloc_count == 0)
12467 continue;
12468
12469 reloc_count += additional_reloc_count;
12470 o->reloc_count += reloc_count;
12471
12472 if (p->type == bfd_indirect_link_order && emit_relocs)
12473 {
12474 if (esdi->rel.hdr)
12475 {
12476 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
12477 esdo->rel.count += additional_reloc_count;
12478 }
12479 if (esdi->rela.hdr)
12480 {
12481 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
12482 esdo->rela.count += additional_reloc_count;
12483 }
12484 }
12485 else
12486 {
12487 if (o->use_rela_p)
12488 esdo->rela.count += reloc_count;
12489 else
12490 esdo->rel.count += reloc_count;
12491 }
12492 }
12493
12494 if (o->reloc_count > 0)
12495 o->flags |= SEC_RELOC;
12496 else
12497 {
12498 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12499 set it (this is probably a bug) and if it is set
12500 assign_section_numbers will create a reloc section. */
12501 o->flags &=~ SEC_RELOC;
12502 }
12503
12504 /* If the SEC_ALLOC flag is not set, force the section VMA to
12505 zero. This is done in elf_fake_sections as well, but forcing
12506 the VMA to 0 here will ensure that relocs against these
12507 sections are handled correctly. */
12508 if ((o->flags & SEC_ALLOC) == 0
12509 && ! o->user_set_vma)
12510 o->vma = 0;
12511 }
12512
12513 if (! bfd_link_relocatable (info) && merged)
12514 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
12515
12516 /* Figure out the file positions for everything but the symbol table
12517 and the relocs. We set symcount to force assign_section_numbers
12518 to create a symbol table. */
12519 abfd->symcount = info->strip != strip_all || emit_relocs;
12520 BFD_ASSERT (! abfd->output_has_begun);
12521 if (! _bfd_elf_compute_section_file_positions (abfd, info))
12522 goto error_return;
12523
12524 /* Set sizes, and assign file positions for reloc sections. */
12525 for (o = abfd->sections; o != NULL; o = o->next)
12526 {
12527 struct bfd_elf_section_data *esdo = elf_section_data (o);
12528 if ((o->flags & SEC_RELOC) != 0)
12529 {
12530 if (esdo->rel.hdr
12531 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
12532 goto error_return;
12533
12534 if (esdo->rela.hdr
12535 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
12536 goto error_return;
12537 }
12538
12539 /* _bfd_elf_compute_section_file_positions makes temporary use
12540 of target_index. Reset it. */
12541 o->target_index = 0;
12542
12543 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12544 to count upwards while actually outputting the relocations. */
12545 esdo->rel.count = 0;
12546 esdo->rela.count = 0;
12547
12548 if ((esdo->this_hdr.sh_offset == (file_ptr) -1)
12549 && !bfd_section_is_ctf (o))
12550 {
12551 /* Cache the section contents so that they can be compressed
12552 later. Use bfd_malloc since it will be freed by
12553 bfd_compress_section_contents. */
12554 unsigned char *contents = esdo->this_hdr.contents;
12555 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
12556 abort ();
12557 contents
12558 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
12559 if (contents == NULL)
12560 goto error_return;
12561 esdo->this_hdr.contents = contents;
12562 }
12563 }
12564
12565 /* We have now assigned file positions for all the sections except .symtab,
12566 .strtab, and non-loaded reloc and compressed debugging sections. We start
12567 the .symtab section at the current file position, and write directly to it.
12568 We build the .strtab section in memory. */
12569 abfd->symcount = 0;
12570 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12571 /* sh_name is set in prep_headers. */
12572 symtab_hdr->sh_type = SHT_SYMTAB;
12573 /* sh_flags, sh_addr and sh_size all start off zero. */
12574 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
12575 /* sh_link is set in assign_section_numbers. */
12576 /* sh_info is set below. */
12577 /* sh_offset is set just below. */
12578 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
12579
12580 if (max_sym_count < 20)
12581 max_sym_count = 20;
12582 htab->strtabsize = max_sym_count;
12583 amt = max_sym_count * sizeof (struct elf_sym_strtab);
12584 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
12585 if (htab->strtab == NULL)
12586 goto error_return;
12587 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12588 flinfo.symshndxbuf
12589 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
12590 ? (Elf_External_Sym_Shndx *) -1 : NULL);
12591
12592 if (info->strip != strip_all || emit_relocs)
12593 {
12594 file_ptr off = elf_next_file_pos (abfd);
12595
12596 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, true);
12597
12598 /* Note that at this point elf_next_file_pos (abfd) is
12599 incorrect. We do not yet know the size of the .symtab section.
12600 We correct next_file_pos below, after we do know the size. */
12601
12602 /* Start writing out the symbol table. The first symbol is always a
12603 dummy symbol. */
12604 elfsym.st_value = 0;
12605 elfsym.st_size = 0;
12606 elfsym.st_info = 0;
12607 elfsym.st_other = 0;
12608 elfsym.st_shndx = SHN_UNDEF;
12609 elfsym.st_target_internal = 0;
12610 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
12611 bfd_und_section_ptr, NULL) != 1)
12612 goto error_return;
12613
12614 /* Output a symbol for each section if asked or they are used for
12615 relocs. These symbols usually have no names. We store the
12616 index of each one in the index field of the section, so that
12617 we can find it again when outputting relocs. */
12618
12619 if (bfd_keep_unused_section_symbols (abfd) || emit_relocs)
12620 {
12621 bool name_local_sections
12622 = (bed->elf_backend_name_local_section_symbols
12623 && bed->elf_backend_name_local_section_symbols (abfd));
12624 const char *name = NULL;
12625
12626 elfsym.st_size = 0;
12627 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12628 elfsym.st_other = 0;
12629 elfsym.st_value = 0;
12630 elfsym.st_target_internal = 0;
12631 for (i = 1; i < elf_numsections (abfd); i++)
12632 {
12633 o = bfd_section_from_elf_index (abfd, i);
12634 if (o != NULL)
12635 {
12636 o->target_index = bfd_get_symcount (abfd);
12637 elfsym.st_shndx = i;
12638 if (!bfd_link_relocatable (info))
12639 elfsym.st_value = o->vma;
12640 if (name_local_sections)
12641 name = o->name;
12642 if (elf_link_output_symstrtab (&flinfo, name, &elfsym, o,
12643 NULL) != 1)
12644 goto error_return;
12645 }
12646 }
12647 }
12648 }
12649
12650 /* On some targets like Irix 5 the symbol split between local and global
12651 ones recorded in the sh_info field needs to be done between section
12652 and all other symbols. */
12653 if (bed->elf_backend_elfsym_local_is_section
12654 && bed->elf_backend_elfsym_local_is_section (abfd))
12655 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12656
12657 /* Allocate some memory to hold information read in from the input
12658 files. */
12659 if (max_contents_size != 0)
12660 {
12661 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12662 if (flinfo.contents == NULL)
12663 goto error_return;
12664 }
12665
12666 if (max_external_reloc_size != 0)
12667 {
12668 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
12669 if (flinfo.external_relocs == NULL)
12670 goto error_return;
12671 }
12672
12673 if (max_internal_reloc_count != 0)
12674 {
12675 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
12676 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
12677 if (flinfo.internal_relocs == NULL)
12678 goto error_return;
12679 }
12680
12681 if (max_sym_count != 0)
12682 {
12683 amt = max_sym_count * bed->s->sizeof_sym;
12684 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
12685 if (flinfo.external_syms == NULL)
12686 goto error_return;
12687
12688 amt = max_sym_count * sizeof (Elf_Internal_Sym);
12689 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
12690 if (flinfo.internal_syms == NULL)
12691 goto error_return;
12692
12693 amt = max_sym_count * sizeof (long);
12694 flinfo.indices = (long int *) bfd_malloc (amt);
12695 if (flinfo.indices == NULL)
12696 goto error_return;
12697
12698 amt = max_sym_count * sizeof (asection *);
12699 flinfo.sections = (asection **) bfd_malloc (amt);
12700 if (flinfo.sections == NULL)
12701 goto error_return;
12702 }
12703
12704 if (max_sym_shndx_count != 0)
12705 {
12706 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
12707 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
12708 if (flinfo.locsym_shndx == NULL)
12709 goto error_return;
12710 }
12711
12712 if (htab->tls_sec)
12713 {
12714 bfd_vma base, end = 0; /* Both bytes. */
12715 asection *sec;
12716
12717 for (sec = htab->tls_sec;
12718 sec && (sec->flags & SEC_THREAD_LOCAL);
12719 sec = sec->next)
12720 {
12721 bfd_size_type size = sec->size;
12722 unsigned int opb = bfd_octets_per_byte (abfd, sec);
12723
12724 if (size == 0
12725 && (sec->flags & SEC_HAS_CONTENTS) == 0)
12726 {
12727 struct bfd_link_order *ord = sec->map_tail.link_order;
12728
12729 if (ord != NULL)
12730 size = ord->offset * opb + ord->size;
12731 }
12732 end = sec->vma + size / opb;
12733 }
12734 base = htab->tls_sec->vma;
12735 /* Only align end of TLS section if static TLS doesn't have special
12736 alignment requirements. */
12737 if (bed->static_tls_alignment == 1)
12738 end = align_power (end, htab->tls_sec->alignment_power);
12739 htab->tls_size = end - base;
12740 }
12741
12742 if (!_bfd_elf_fixup_eh_frame_hdr (info))
12743 return false;
12744
12745 /* Finish relative relocations here after regular symbol processing
12746 is finished if DT_RELR is enabled. */
12747 if (info->enable_dt_relr
12748 && bed->finish_relative_relocs
12749 && !bed->finish_relative_relocs (info))
12750 info->callbacks->einfo
12751 (_("%F%P: %pB: failed to finish relative relocations\n"), abfd);
12752
12753 /* Since ELF permits relocations to be against local symbols, we
12754 must have the local symbols available when we do the relocations.
12755 Since we would rather only read the local symbols once, and we
12756 would rather not keep them in memory, we handle all the
12757 relocations for a single input file at the same time.
12758
12759 Unfortunately, there is no way to know the total number of local
12760 symbols until we have seen all of them, and the local symbol
12761 indices precede the global symbol indices. This means that when
12762 we are generating relocatable output, and we see a reloc against
12763 a global symbol, we can not know the symbol index until we have
12764 finished examining all the local symbols to see which ones we are
12765 going to output. To deal with this, we keep the relocations in
12766 memory, and don't output them until the end of the link. This is
12767 an unfortunate waste of memory, but I don't see a good way around
12768 it. Fortunately, it only happens when performing a relocatable
12769 link, which is not the common case. FIXME: If keep_memory is set
12770 we could write the relocs out and then read them again; I don't
12771 know how bad the memory loss will be. */
12772
12773 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12774 sub->output_has_begun = false;
12775 for (o = abfd->sections; o != NULL; o = o->next)
12776 {
12777 for (p = o->map_head.link_order; p != NULL; p = p->next)
12778 {
12779 if (p->type == bfd_indirect_link_order
12780 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12781 == bfd_target_elf_flavour)
12782 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12783 {
12784 if (! sub->output_has_begun)
12785 {
12786 if (! elf_link_input_bfd (&flinfo, sub))
12787 goto error_return;
12788 sub->output_has_begun = true;
12789 }
12790 }
12791 else if (p->type == bfd_section_reloc_link_order
12792 || p->type == bfd_symbol_reloc_link_order)
12793 {
12794 if (! elf_reloc_link_order (abfd, info, o, p))
12795 goto error_return;
12796 }
12797 else
12798 {
12799 if (! _bfd_default_link_order (abfd, info, o, p))
12800 {
12801 if (p->type == bfd_indirect_link_order
12802 && (bfd_get_flavour (sub)
12803 == bfd_target_elf_flavour)
12804 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12805 != bed->s->elfclass))
12806 {
12807 const char *iclass, *oclass;
12808
12809 switch (bed->s->elfclass)
12810 {
12811 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12812 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12813 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12814 default: abort ();
12815 }
12816
12817 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
12818 {
12819 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12820 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12821 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12822 default: abort ();
12823 }
12824
12825 bfd_set_error (bfd_error_wrong_format);
12826 _bfd_error_handler
12827 /* xgettext:c-format */
12828 (_("%pB: file class %s incompatible with %s"),
12829 sub, iclass, oclass);
12830 }
12831
12832 goto error_return;
12833 }
12834 }
12835 }
12836 }
12837
12838 /* Free symbol buffer if needed. */
12839 if (!info->reduce_memory_overheads)
12840 {
12841 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12842 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
12843 {
12844 free (elf_tdata (sub)->symbuf);
12845 elf_tdata (sub)->symbuf = NULL;
12846 }
12847 }
12848
12849 ret = true;
12850
12851 /* Output any global symbols that got converted to local in a
12852 version script or due to symbol visibility. We do this in a
12853 separate step since ELF requires all local symbols to appear
12854 prior to any global symbols. FIXME: We should only do this if
12855 some global symbols were, in fact, converted to become local.
12856 FIXME: Will this work correctly with the Irix 5 linker? */
12857 eoinfo.failed = false;
12858 eoinfo.flinfo = &flinfo;
12859 eoinfo.localsyms = true;
12860 eoinfo.file_sym_done = false;
12861 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12862 if (eoinfo.failed)
12863 {
12864 ret = false;
12865 goto return_local_hash_table;
12866 }
12867
12868 /* If backend needs to output some local symbols not present in the hash
12869 table, do it now. */
12870 if (bed->elf_backend_output_arch_local_syms
12871 && (info->strip != strip_all || emit_relocs))
12872 {
12873 if (! ((*bed->elf_backend_output_arch_local_syms)
12874 (abfd, info, &flinfo, elf_link_output_symstrtab)))
12875 {
12876 ret = false;
12877 goto return_local_hash_table;
12878 }
12879 }
12880
12881 /* That wrote out all the local symbols. Finish up the symbol table
12882 with the global symbols. Even if we want to strip everything we
12883 can, we still need to deal with those global symbols that got
12884 converted to local in a version script. */
12885
12886 /* The sh_info field records the index of the first non local symbol. */
12887 if (!symtab_hdr->sh_info)
12888 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12889
12890 if (dynamic
12891 && htab->dynsym != NULL
12892 && htab->dynsym->output_section != bfd_abs_section_ptr)
12893 {
12894 Elf_Internal_Sym sym;
12895 bfd_byte *dynsym = htab->dynsym->contents;
12896
12897 o = htab->dynsym->output_section;
12898 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
12899
12900 /* Write out the section symbols for the output sections. */
12901 if (bfd_link_pic (info)
12902 || htab->is_relocatable_executable)
12903 {
12904 asection *s;
12905
12906 sym.st_size = 0;
12907 sym.st_name = 0;
12908 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12909 sym.st_other = 0;
12910 sym.st_target_internal = 0;
12911
12912 for (s = abfd->sections; s != NULL; s = s->next)
12913 {
12914 int indx;
12915 bfd_byte *dest;
12916 long dynindx;
12917
12918 dynindx = elf_section_data (s)->dynindx;
12919 if (dynindx <= 0)
12920 continue;
12921 indx = elf_section_data (s)->this_idx;
12922 BFD_ASSERT (indx > 0);
12923 sym.st_shndx = indx;
12924 if (! check_dynsym (abfd, &sym))
12925 {
12926 ret = false;
12927 goto return_local_hash_table;
12928 }
12929 sym.st_value = s->vma;
12930 dest = dynsym + dynindx * bed->s->sizeof_sym;
12931
12932 /* Inform the linker of the addition of this symbol. */
12933
12934 if (info->callbacks->ctf_new_dynsym)
12935 info->callbacks->ctf_new_dynsym (dynindx, &sym);
12936
12937 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12938 }
12939 }
12940
12941 /* Write out the local dynsyms. */
12942 if (htab->dynlocal)
12943 {
12944 struct elf_link_local_dynamic_entry *e;
12945 for (e = htab->dynlocal; e ; e = e->next)
12946 {
12947 asection *s;
12948 bfd_byte *dest;
12949
12950 /* Copy the internal symbol and turn off visibility.
12951 Note that we saved a word of storage and overwrote
12952 the original st_name with the dynstr_index. */
12953 sym = e->isym;
12954 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
12955 sym.st_shndx = SHN_UNDEF;
12956
12957 s = bfd_section_from_elf_index (e->input_bfd,
12958 e->isym.st_shndx);
12959 if (s != NULL
12960 && s->output_section != NULL
12961 && elf_section_data (s->output_section) != NULL)
12962 {
12963 sym.st_shndx =
12964 elf_section_data (s->output_section)->this_idx;
12965 if (! check_dynsym (abfd, &sym))
12966 {
12967 ret = false;
12968 goto return_local_hash_table;
12969 }
12970 sym.st_value = (s->output_section->vma
12971 + s->output_offset
12972 + e->isym.st_value);
12973 }
12974
12975 /* Inform the linker of the addition of this symbol. */
12976
12977 if (info->callbacks->ctf_new_dynsym)
12978 info->callbacks->ctf_new_dynsym (e->dynindx, &sym);
12979
12980 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12981 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12982 }
12983 }
12984 }
12985
12986 /* We get the global symbols from the hash table. */
12987 eoinfo.failed = false;
12988 eoinfo.localsyms = false;
12989 eoinfo.flinfo = &flinfo;
12990 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12991 if (eoinfo.failed)
12992 {
12993 ret = false;
12994 goto return_local_hash_table;
12995 }
12996
12997 /* If backend needs to output some symbols not present in the hash
12998 table, do it now. */
12999 if (bed->elf_backend_output_arch_syms
13000 && (info->strip != strip_all || emit_relocs))
13001 {
13002 if (! ((*bed->elf_backend_output_arch_syms)
13003 (abfd, info, &flinfo, elf_link_output_symstrtab)))
13004 {
13005 ret = false;
13006 goto return_local_hash_table;
13007 }
13008 }
13009
13010 /* Finalize the .strtab section. */
13011 _bfd_elf_strtab_finalize (flinfo.symstrtab);
13012
13013 /* Swap out the .strtab section. */
13014 if (!elf_link_swap_symbols_out (&flinfo))
13015 {
13016 ret = false;
13017 goto return_local_hash_table;
13018 }
13019
13020 /* Now we know the size of the symtab section. */
13021 if (bfd_get_symcount (abfd) > 0)
13022 {
13023 /* Finish up and write out the symbol string table (.strtab)
13024 section. */
13025 Elf_Internal_Shdr *symstrtab_hdr = NULL;
13026 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
13027
13028 if (elf_symtab_shndx_list (abfd))
13029 {
13030 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
13031
13032 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
13033 {
13034 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
13035 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
13036 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
13037 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
13038 symtab_shndx_hdr->sh_size = amt;
13039
13040 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
13041 off, true);
13042
13043 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
13044 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
13045 {
13046 ret = false;
13047 goto return_local_hash_table;
13048 }
13049 }
13050 }
13051
13052 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
13053 /* sh_name was set in prep_headers. */
13054 symstrtab_hdr->sh_type = SHT_STRTAB;
13055 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
13056 symstrtab_hdr->sh_addr = 0;
13057 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
13058 symstrtab_hdr->sh_entsize = 0;
13059 symstrtab_hdr->sh_link = 0;
13060 symstrtab_hdr->sh_info = 0;
13061 /* sh_offset is set just below. */
13062 symstrtab_hdr->sh_addralign = 1;
13063
13064 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
13065 off, true);
13066 elf_next_file_pos (abfd) = off;
13067
13068 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
13069 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
13070 {
13071 ret = false;
13072 goto return_local_hash_table;
13073 }
13074 }
13075
13076 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
13077 {
13078 _bfd_error_handler (_("%pB: failed to generate import library"),
13079 info->out_implib_bfd);
13080 ret = false;
13081 goto return_local_hash_table;
13082 }
13083
13084 /* Adjust the relocs to have the correct symbol indices. */
13085 for (o = abfd->sections; o != NULL; o = o->next)
13086 {
13087 struct bfd_elf_section_data *esdo = elf_section_data (o);
13088 bool sort;
13089
13090 if ((o->flags & SEC_RELOC) == 0)
13091 continue;
13092
13093 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
13094 if (esdo->rel.hdr != NULL
13095 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
13096 {
13097 ret = false;
13098 goto return_local_hash_table;
13099 }
13100 if (esdo->rela.hdr != NULL
13101 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
13102 {
13103 ret = false;
13104 goto return_local_hash_table;
13105 }
13106
13107 /* Set the reloc_count field to 0 to prevent write_relocs from
13108 trying to swap the relocs out itself. */
13109 o->reloc_count = 0;
13110 }
13111
13112 relativecount = 0;
13113 if (dynamic && info->combreloc && dynobj != NULL)
13114 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
13115
13116 relr_entsize = 0;
13117 if (htab->srelrdyn != NULL
13118 && htab->srelrdyn->output_section != NULL
13119 && htab->srelrdyn->size != 0)
13120 {
13121 asection *s = htab->srelrdyn->output_section;
13122 relr_entsize = elf_section_data (s)->this_hdr.sh_entsize;
13123 if (relr_entsize == 0)
13124 {
13125 relr_entsize = bed->s->arch_size / 8;
13126 elf_section_data (s)->this_hdr.sh_entsize = relr_entsize;
13127 }
13128 }
13129
13130 /* If we are linking against a dynamic object, or generating a
13131 shared library, finish up the dynamic linking information. */
13132 if (dynamic)
13133 {
13134 bfd_byte *dyncon, *dynconend;
13135
13136 /* Fix up .dynamic entries. */
13137 o = bfd_get_linker_section (dynobj, ".dynamic");
13138 BFD_ASSERT (o != NULL);
13139
13140 dyncon = o->contents;
13141 dynconend = PTR_ADD (o->contents, o->size);
13142 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13143 {
13144 Elf_Internal_Dyn dyn;
13145 const char *name;
13146 unsigned int type;
13147 bfd_size_type sh_size;
13148 bfd_vma sh_addr;
13149
13150 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13151
13152 switch (dyn.d_tag)
13153 {
13154 default:
13155 continue;
13156 case DT_NULL:
13157 if (relativecount != 0)
13158 {
13159 switch (elf_section_data (reldyn)->this_hdr.sh_type)
13160 {
13161 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
13162 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
13163 }
13164 if (dyn.d_tag != DT_NULL
13165 && dynconend - dyncon >= bed->s->sizeof_dyn)
13166 {
13167 dyn.d_un.d_val = relativecount;
13168 relativecount = 0;
13169 break;
13170 }
13171 relativecount = 0;
13172 }
13173 if (relr_entsize != 0)
13174 {
13175 if (dynconend - dyncon >= 3 * bed->s->sizeof_dyn)
13176 {
13177 asection *s = htab->srelrdyn;
13178 dyn.d_tag = DT_RELR;
13179 dyn.d_un.d_ptr
13180 = s->output_section->vma + s->output_offset;
13181 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13182 dyncon += bed->s->sizeof_dyn;
13183
13184 dyn.d_tag = DT_RELRSZ;
13185 dyn.d_un.d_val = s->size;
13186 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13187 dyncon += bed->s->sizeof_dyn;
13188
13189 dyn.d_tag = DT_RELRENT;
13190 dyn.d_un.d_val = relr_entsize;
13191 relr_entsize = 0;
13192 break;
13193 }
13194 relr_entsize = 0;
13195 }
13196 continue;
13197
13198 case DT_INIT:
13199 name = info->init_function;
13200 goto get_sym;
13201 case DT_FINI:
13202 name = info->fini_function;
13203 get_sym:
13204 {
13205 struct elf_link_hash_entry *h;
13206
13207 h = elf_link_hash_lookup (htab, name, false, false, true);
13208 if (h != NULL
13209 && (h->root.type == bfd_link_hash_defined
13210 || h->root.type == bfd_link_hash_defweak))
13211 {
13212 dyn.d_un.d_ptr = h->root.u.def.value;
13213 o = h->root.u.def.section;
13214 if (o->output_section != NULL)
13215 dyn.d_un.d_ptr += (o->output_section->vma
13216 + o->output_offset);
13217 else
13218 {
13219 /* The symbol is imported from another shared
13220 library and does not apply to this one. */
13221 dyn.d_un.d_ptr = 0;
13222 }
13223 break;
13224 }
13225 }
13226 continue;
13227
13228 case DT_PREINIT_ARRAYSZ:
13229 name = ".preinit_array";
13230 goto get_out_size;
13231 case DT_INIT_ARRAYSZ:
13232 name = ".init_array";
13233 goto get_out_size;
13234 case DT_FINI_ARRAYSZ:
13235 name = ".fini_array";
13236 get_out_size:
13237 o = bfd_get_section_by_name (abfd, name);
13238 if (o == NULL)
13239 {
13240 _bfd_error_handler
13241 (_("could not find section %s"), name);
13242 goto error_return;
13243 }
13244 if (o->size == 0)
13245 _bfd_error_handler
13246 (_("warning: %s section has zero size"), name);
13247 dyn.d_un.d_val = o->size;
13248 break;
13249
13250 case DT_PREINIT_ARRAY:
13251 name = ".preinit_array";
13252 goto get_out_vma;
13253 case DT_INIT_ARRAY:
13254 name = ".init_array";
13255 goto get_out_vma;
13256 case DT_FINI_ARRAY:
13257 name = ".fini_array";
13258 get_out_vma:
13259 o = bfd_get_section_by_name (abfd, name);
13260 goto do_vma;
13261
13262 case DT_HASH:
13263 name = ".hash";
13264 goto get_vma;
13265 case DT_GNU_HASH:
13266 name = ".gnu.hash";
13267 goto get_vma;
13268 case DT_STRTAB:
13269 name = ".dynstr";
13270 goto get_vma;
13271 case DT_SYMTAB:
13272 name = ".dynsym";
13273 goto get_vma;
13274 case DT_VERDEF:
13275 name = ".gnu.version_d";
13276 goto get_vma;
13277 case DT_VERNEED:
13278 name = ".gnu.version_r";
13279 goto get_vma;
13280 case DT_VERSYM:
13281 name = ".gnu.version";
13282 get_vma:
13283 o = bfd_get_linker_section (dynobj, name);
13284 do_vma:
13285 if (o == NULL || bfd_is_abs_section (o->output_section))
13286 {
13287 _bfd_error_handler
13288 (_("could not find section %s"), name);
13289 goto error_return;
13290 }
13291 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
13292 {
13293 _bfd_error_handler
13294 (_("warning: section '%s' is being made into a note"), name);
13295 bfd_set_error (bfd_error_nonrepresentable_section);
13296 goto error_return;
13297 }
13298 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
13299 break;
13300
13301 case DT_REL:
13302 case DT_RELA:
13303 case DT_RELSZ:
13304 case DT_RELASZ:
13305 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
13306 type = SHT_REL;
13307 else
13308 type = SHT_RELA;
13309 sh_size = 0;
13310 sh_addr = 0;
13311 for (i = 1; i < elf_numsections (abfd); i++)
13312 {
13313 Elf_Internal_Shdr *hdr;
13314
13315 hdr = elf_elfsections (abfd)[i];
13316 if (hdr->sh_type == type
13317 && (hdr->sh_flags & SHF_ALLOC) != 0)
13318 {
13319 sh_size += hdr->sh_size;
13320 if (sh_addr == 0
13321 || sh_addr > hdr->sh_addr)
13322 sh_addr = hdr->sh_addr;
13323 }
13324 }
13325
13326 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
13327 {
13328 unsigned int opb = bfd_octets_per_byte (abfd, o);
13329
13330 /* Don't count procedure linkage table relocs in the
13331 overall reloc count. */
13332 sh_size -= htab->srelplt->size;
13333 if (sh_size == 0)
13334 /* If the size is zero, make the address zero too.
13335 This is to avoid a glibc bug. If the backend
13336 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
13337 zero, then we'll put DT_RELA at the end of
13338 DT_JMPREL. glibc will interpret the end of
13339 DT_RELA matching the end of DT_JMPREL as the
13340 case where DT_RELA includes DT_JMPREL, and for
13341 LD_BIND_NOW will decide that processing DT_RELA
13342 will process the PLT relocs too. Net result:
13343 No PLT relocs applied. */
13344 sh_addr = 0;
13345
13346 /* If .rela.plt is the first .rela section, exclude
13347 it from DT_RELA. */
13348 else if (sh_addr == (htab->srelplt->output_section->vma
13349 + htab->srelplt->output_offset) * opb)
13350 sh_addr += htab->srelplt->size;
13351 }
13352
13353 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
13354 dyn.d_un.d_val = sh_size;
13355 else
13356 dyn.d_un.d_ptr = sh_addr;
13357 break;
13358 }
13359 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13360 }
13361 }
13362
13363 /* If we have created any dynamic sections, then output them. */
13364 if (dynobj != NULL)
13365 {
13366 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
13367 goto error_return;
13368
13369 /* Check for DT_TEXTREL (late, in case the backend removes it). */
13370 if (bfd_link_textrel_check (info)
13371 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL
13372 && o->size != 0)
13373 {
13374 bfd_byte *dyncon, *dynconend;
13375
13376 dyncon = o->contents;
13377 dynconend = o->contents + o->size;
13378 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13379 {
13380 Elf_Internal_Dyn dyn;
13381
13382 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13383
13384 if (dyn.d_tag == DT_TEXTREL)
13385 {
13386 if (info->textrel_check == textrel_check_error)
13387 info->callbacks->einfo
13388 (_("%P%X: read-only segment has dynamic relocations\n"));
13389 else if (bfd_link_dll (info))
13390 info->callbacks->einfo
13391 (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
13392 else if (bfd_link_pde (info))
13393 info->callbacks->einfo
13394 (_("%P: warning: creating DT_TEXTREL in a PDE\n"));
13395 else
13396 info->callbacks->einfo
13397 (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
13398 break;
13399 }
13400 }
13401 }
13402
13403 for (o = dynobj->sections; o != NULL; o = o->next)
13404 {
13405 if ((o->flags & SEC_HAS_CONTENTS) == 0
13406 || o->size == 0
13407 || o->output_section == bfd_abs_section_ptr)
13408 continue;
13409 if ((o->flags & SEC_LINKER_CREATED) == 0)
13410 {
13411 /* At this point, we are only interested in sections
13412 created by _bfd_elf_link_create_dynamic_sections. */
13413 continue;
13414 }
13415 if (htab->stab_info.stabstr == o)
13416 continue;
13417 if (htab->eh_info.hdr_sec == o)
13418 continue;
13419 if (strcmp (o->name, ".dynstr") != 0)
13420 {
13421 bfd_size_type octets = ((file_ptr) o->output_offset
13422 * bfd_octets_per_byte (abfd, o));
13423 if (!bfd_set_section_contents (abfd, o->output_section,
13424 o->contents, octets, o->size))
13425 goto error_return;
13426 }
13427 else
13428 {
13429 /* The contents of the .dynstr section are actually in a
13430 stringtab. */
13431 file_ptr off;
13432
13433 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
13434 if (bfd_seek (abfd, off, SEEK_SET) != 0
13435 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
13436 goto error_return;
13437 }
13438 }
13439 }
13440
13441 if (!info->resolve_section_groups)
13442 {
13443 bool failed = false;
13444
13445 BFD_ASSERT (bfd_link_relocatable (info));
13446 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
13447 if (failed)
13448 goto error_return;
13449 }
13450
13451 /* If we have optimized stabs strings, output them. */
13452 if (htab->stab_info.stabstr != NULL)
13453 {
13454 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
13455 goto error_return;
13456 }
13457
13458 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
13459 goto error_return;
13460
13461 if (info->callbacks->emit_ctf)
13462 info->callbacks->emit_ctf ();
13463
13464 elf_final_link_free (abfd, &flinfo);
13465
13466 if (attr_section)
13467 {
13468 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
13469 if (contents == NULL)
13470 {
13471 /* Bail out and fail. */
13472 ret = false;
13473 goto return_local_hash_table;
13474 }
13475 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
13476 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
13477 free (contents);
13478 }
13479
13480 return_local_hash_table:
13481 if (info->unique_symbol)
13482 bfd_hash_table_free (&flinfo.local_hash_table);
13483 return ret;
13484
13485 error_return:
13486 elf_final_link_free (abfd, &flinfo);
13487 ret = false;
13488 goto return_local_hash_table;
13489 }
13490
13491 /* Initialize COOKIE for input bfd ABFD. */
13493
13494 static bool
13495 init_reloc_cookie (struct elf_reloc_cookie *cookie,
13496 struct bfd_link_info *info, bfd *abfd)
13497 {
13498 Elf_Internal_Shdr *symtab_hdr;
13499 const struct elf_backend_data *bed;
13500
13501 bed = get_elf_backend_data (abfd);
13502 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13503
13504 cookie->abfd = abfd;
13505 cookie->sym_hashes = elf_sym_hashes (abfd);
13506 cookie->bad_symtab = elf_bad_symtab (abfd);
13507 if (cookie->bad_symtab)
13508 {
13509 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13510 cookie->extsymoff = 0;
13511 }
13512 else
13513 {
13514 cookie->locsymcount = symtab_hdr->sh_info;
13515 cookie->extsymoff = symtab_hdr->sh_info;
13516 }
13517
13518 if (bed->s->arch_size == 32)
13519 cookie->r_sym_shift = 8;
13520 else
13521 cookie->r_sym_shift = 32;
13522
13523 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
13524 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
13525 {
13526 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
13527 cookie->locsymcount, 0,
13528 NULL, NULL, NULL);
13529 if (cookie->locsyms == NULL)
13530 {
13531 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
13532 return false;
13533 }
13534 if (_bfd_link_keep_memory (info) )
13535 {
13536 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
13537 info->cache_size += (cookie->locsymcount
13538 * sizeof (Elf_External_Sym_Shndx));
13539 }
13540 }
13541 return true;
13542 }
13543
13544 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
13545
13546 static void
13547 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
13548 {
13549 Elf_Internal_Shdr *symtab_hdr;
13550
13551 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13552 if (symtab_hdr->contents != (unsigned char *) cookie->locsyms)
13553 free (cookie->locsyms);
13554 }
13555
13556 /* Initialize the relocation information in COOKIE for input section SEC
13557 of input bfd ABFD. */
13558
13559 static bool
13560 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13561 struct bfd_link_info *info, bfd *abfd,
13562 asection *sec)
13563 {
13564 if (sec->reloc_count == 0)
13565 {
13566 cookie->rels = NULL;
13567 cookie->relend = NULL;
13568 }
13569 else
13570 {
13571 cookie->rels = _bfd_elf_link_info_read_relocs (abfd, info, sec,
13572 NULL, NULL,
13573 _bfd_link_keep_memory (info));
13574 if (cookie->rels == NULL)
13575 return false;
13576 cookie->rel = cookie->rels;
13577 cookie->relend = cookie->rels + sec->reloc_count;
13578 }
13579 cookie->rel = cookie->rels;
13580 return true;
13581 }
13582
13583 /* Free the memory allocated by init_reloc_cookie_rels,
13584 if appropriate. */
13585
13586 static void
13587 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13588 asection *sec)
13589 {
13590 if (elf_section_data (sec)->relocs != cookie->rels)
13591 free (cookie->rels);
13592 }
13593
13594 /* Initialize the whole of COOKIE for input section SEC. */
13595
13596 static bool
13597 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13598 struct bfd_link_info *info,
13599 asection *sec)
13600 {
13601 if (!init_reloc_cookie (cookie, info, sec->owner))
13602 goto error1;
13603 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
13604 goto error2;
13605 return true;
13606
13607 error2:
13608 fini_reloc_cookie (cookie, sec->owner);
13609 error1:
13610 return false;
13611 }
13612
13613 /* Free the memory allocated by init_reloc_cookie_for_section,
13614 if appropriate. */
13615
13616 static void
13617 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13618 asection *sec)
13619 {
13620 fini_reloc_cookie_rels (cookie, sec);
13621 fini_reloc_cookie (cookie, sec->owner);
13622 }
13623
13624 /* Garbage collect unused sections. */
13626
13627 /* Default gc_mark_hook. */
13628
13629 asection *
13630 _bfd_elf_gc_mark_hook (asection *sec,
13631 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13632 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13633 struct elf_link_hash_entry *h,
13634 Elf_Internal_Sym *sym)
13635 {
13636 if (h != NULL)
13637 {
13638 switch (h->root.type)
13639 {
13640 case bfd_link_hash_defined:
13641 case bfd_link_hash_defweak:
13642 return h->root.u.def.section;
13643
13644 case bfd_link_hash_common:
13645 return h->root.u.c.p->section;
13646
13647 default:
13648 break;
13649 }
13650 }
13651 else
13652 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
13653
13654 return NULL;
13655 }
13656
13657 /* Return the debug definition section. */
13658
13659 static asection *
13660 elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
13661 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13662 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13663 struct elf_link_hash_entry *h,
13664 Elf_Internal_Sym *sym)
13665 {
13666 if (h != NULL)
13667 {
13668 /* Return the global debug definition section. */
13669 if ((h->root.type == bfd_link_hash_defined
13670 || h->root.type == bfd_link_hash_defweak)
13671 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
13672 return h->root.u.def.section;
13673 }
13674 else
13675 {
13676 /* Return the local debug definition section. */
13677 asection *isec = bfd_section_from_elf_index (sec->owner,
13678 sym->st_shndx);
13679 if ((isec->flags & SEC_DEBUGGING) != 0)
13680 return isec;
13681 }
13682
13683 return NULL;
13684 }
13685
13686 /* COOKIE->rel describes a relocation against section SEC, which is
13687 a section we've decided to keep. Return the section that contains
13688 the relocation symbol, or NULL if no section contains it. */
13689
13690 asection *
13691 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
13692 elf_gc_mark_hook_fn gc_mark_hook,
13693 struct elf_reloc_cookie *cookie,
13694 bool *start_stop)
13695 {
13696 unsigned long r_symndx;
13697 struct elf_link_hash_entry *h, *hw;
13698
13699 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
13700 if (r_symndx == STN_UNDEF)
13701 return NULL;
13702
13703 if (r_symndx >= cookie->locsymcount
13704 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13705 {
13706 bool was_marked;
13707
13708 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
13709 if (h == NULL)
13710 {
13711 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
13712 sec->owner);
13713 return NULL;
13714 }
13715 while (h->root.type == bfd_link_hash_indirect
13716 || h->root.type == bfd_link_hash_warning)
13717 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13718
13719 was_marked = h->mark;
13720 h->mark = 1;
13721 /* Keep all aliases of the symbol too. If an object symbol
13722 needs to be copied into .dynbss then all of its aliases
13723 should be present as dynamic symbols, not just the one used
13724 on the copy relocation. */
13725 hw = h;
13726 while (hw->is_weakalias)
13727 {
13728 hw = hw->u.alias;
13729 hw->mark = 1;
13730 }
13731
13732 if (!was_marked && h->start_stop && !h->root.ldscript_def)
13733 {
13734 if (info->start_stop_gc)
13735 return NULL;
13736
13737 /* To work around a glibc bug, mark XXX input sections
13738 when there is a reference to __start_XXX or __stop_XXX
13739 symbols. */
13740 else if (start_stop != NULL)
13741 {
13742 asection *s = h->u2.start_stop_section;
13743 *start_stop = true;
13744 return s;
13745 }
13746 }
13747
13748 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
13749 }
13750
13751 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
13752 &cookie->locsyms[r_symndx]);
13753 }
13754
13755 /* COOKIE->rel describes a relocation against section SEC, which is
13756 a section we've decided to keep. Mark the section that contains
13757 the relocation symbol. */
13758
13759 bool
13760 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
13761 asection *sec,
13762 elf_gc_mark_hook_fn gc_mark_hook,
13763 struct elf_reloc_cookie *cookie)
13764 {
13765 asection *rsec;
13766 bool start_stop = false;
13767
13768 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
13769 while (rsec != NULL)
13770 {
13771 if (!rsec->gc_mark)
13772 {
13773 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
13774 || (rsec->owner->flags & DYNAMIC) != 0)
13775 rsec->gc_mark = 1;
13776 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
13777 return false;
13778 }
13779 if (!start_stop)
13780 break;
13781 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
13782 }
13783 return true;
13784 }
13785
13786 /* The mark phase of garbage collection. For a given section, mark
13787 it and any sections in this section's group, and all the sections
13788 which define symbols to which it refers. */
13789
13790 bool
13791 _bfd_elf_gc_mark (struct bfd_link_info *info,
13792 asection *sec,
13793 elf_gc_mark_hook_fn gc_mark_hook)
13794 {
13795 bool ret;
13796 asection *group_sec, *eh_frame;
13797
13798 sec->gc_mark = 1;
13799
13800 /* Mark all the sections in the group. */
13801 group_sec = elf_section_data (sec)->next_in_group;
13802 if (group_sec && !group_sec->gc_mark)
13803 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
13804 return false;
13805
13806 /* Look through the section relocs. */
13807 ret = true;
13808 eh_frame = elf_eh_frame_section (sec->owner);
13809 if ((sec->flags & SEC_RELOC) != 0
13810 && sec->reloc_count > 0
13811 && sec != eh_frame)
13812 {
13813 struct elf_reloc_cookie cookie;
13814
13815 if (!init_reloc_cookie_for_section (&cookie, info, sec))
13816 ret = false;
13817 else
13818 {
13819 for (; cookie.rel < cookie.relend; cookie.rel++)
13820 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
13821 {
13822 ret = false;
13823 break;
13824 }
13825 fini_reloc_cookie_for_section (&cookie, sec);
13826 }
13827 }
13828
13829 if (ret && eh_frame && elf_fde_list (sec))
13830 {
13831 struct elf_reloc_cookie cookie;
13832
13833 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
13834 ret = false;
13835 else
13836 {
13837 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
13838 gc_mark_hook, &cookie))
13839 ret = false;
13840 fini_reloc_cookie_for_section (&cookie, eh_frame);
13841 }
13842 }
13843
13844 eh_frame = elf_section_eh_frame_entry (sec);
13845 if (ret && eh_frame && !eh_frame->gc_mark)
13846 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
13847 ret = false;
13848
13849 return ret;
13850 }
13851
13852 /* Scan and mark sections in a special or debug section group. */
13853
13854 static void
13855 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
13856 {
13857 /* Point to first section of section group. */
13858 asection *ssec;
13859 /* Used to iterate the section group. */
13860 asection *msec;
13861
13862 bool is_special_grp = true;
13863 bool is_debug_grp = true;
13864
13865 /* First scan to see if group contains any section other than debug
13866 and special section. */
13867 ssec = msec = elf_next_in_group (grp);
13868 do
13869 {
13870 if ((msec->flags & SEC_DEBUGGING) == 0)
13871 is_debug_grp = false;
13872
13873 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
13874 is_special_grp = false;
13875
13876 msec = elf_next_in_group (msec);
13877 }
13878 while (msec != ssec);
13879
13880 /* If this is a pure debug section group or pure special section group,
13881 keep all sections in this group. */
13882 if (is_debug_grp || is_special_grp)
13883 {
13884 do
13885 {
13886 msec->gc_mark = 1;
13887 msec = elf_next_in_group (msec);
13888 }
13889 while (msec != ssec);
13890 }
13891 }
13892
13893 /* Keep debug and special sections. */
13894
13895 bool
13896 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
13897 elf_gc_mark_hook_fn mark_hook)
13898 {
13899 bfd *ibfd;
13900
13901 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13902 {
13903 asection *isec;
13904 bool some_kept;
13905 bool debug_frag_seen;
13906 bool has_kept_debug_info;
13907
13908 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13909 continue;
13910 isec = ibfd->sections;
13911 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13912 continue;
13913
13914 /* Ensure all linker created sections are kept,
13915 see if any other section is already marked,
13916 and note if we have any fragmented debug sections. */
13917 debug_frag_seen = some_kept = has_kept_debug_info = false;
13918 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13919 {
13920 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13921 isec->gc_mark = 1;
13922 else if (isec->gc_mark
13923 && (isec->flags & SEC_ALLOC) != 0
13924 && elf_section_type (isec) != SHT_NOTE)
13925 some_kept = true;
13926 else
13927 {
13928 /* Since all sections, except for backend specific ones,
13929 have been garbage collected, call mark_hook on this
13930 section if any of its linked-to sections is marked. */
13931 asection *linked_to_sec;
13932 for (linked_to_sec = elf_linked_to_section (isec);
13933 linked_to_sec != NULL && !linked_to_sec->linker_mark;
13934 linked_to_sec = elf_linked_to_section (linked_to_sec))
13935 {
13936 if (linked_to_sec->gc_mark)
13937 {
13938 if (!_bfd_elf_gc_mark (info, isec, mark_hook))
13939 return false;
13940 break;
13941 }
13942 linked_to_sec->linker_mark = 1;
13943 }
13944 for (linked_to_sec = elf_linked_to_section (isec);
13945 linked_to_sec != NULL && linked_to_sec->linker_mark;
13946 linked_to_sec = elf_linked_to_section (linked_to_sec))
13947 linked_to_sec->linker_mark = 0;
13948 }
13949
13950 if (!debug_frag_seen
13951 && (isec->flags & SEC_DEBUGGING)
13952 && startswith (isec->name, ".debug_line."))
13953 debug_frag_seen = true;
13954 else if (strcmp (bfd_section_name (isec),
13955 "__patchable_function_entries") == 0
13956 && elf_linked_to_section (isec) == NULL)
13957 info->callbacks->einfo (_("%F%P: %pB(%pA): error: "
13958 "need linked-to section "
13959 "for --gc-sections\n"),
13960 isec->owner, isec);
13961 }
13962
13963 /* If no non-note alloc section in this file will be kept, then
13964 we can toss out the debug and special sections. */
13965 if (!some_kept)
13966 continue;
13967
13968 /* Keep debug and special sections like .comment when they are
13969 not part of a group. Also keep section groups that contain
13970 just debug sections or special sections. NB: Sections with
13971 linked-to section has been handled above. */
13972 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13973 {
13974 if ((isec->flags & SEC_GROUP) != 0)
13975 _bfd_elf_gc_mark_debug_special_section_group (isec);
13976 else if (((isec->flags & SEC_DEBUGGING) != 0
13977 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
13978 && elf_next_in_group (isec) == NULL
13979 && elf_linked_to_section (isec) == NULL)
13980 isec->gc_mark = 1;
13981 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
13982 has_kept_debug_info = true;
13983 }
13984
13985 /* Look for CODE sections which are going to be discarded,
13986 and find and discard any fragmented debug sections which
13987 are associated with that code section. */
13988 if (debug_frag_seen)
13989 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13990 if ((isec->flags & SEC_CODE) != 0
13991 && isec->gc_mark == 0)
13992 {
13993 unsigned int ilen;
13994 asection *dsec;
13995
13996 ilen = strlen (isec->name);
13997
13998 /* Association is determined by the name of the debug
13999 section containing the name of the code section as
14000 a suffix. For example .debug_line.text.foo is a
14001 debug section associated with .text.foo. */
14002 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
14003 {
14004 unsigned int dlen;
14005
14006 if (dsec->gc_mark == 0
14007 || (dsec->flags & SEC_DEBUGGING) == 0)
14008 continue;
14009
14010 dlen = strlen (dsec->name);
14011
14012 if (dlen > ilen
14013 && strncmp (dsec->name + (dlen - ilen),
14014 isec->name, ilen) == 0)
14015 dsec->gc_mark = 0;
14016 }
14017 }
14018
14019 /* Mark debug sections referenced by kept debug sections. */
14020 if (has_kept_debug_info)
14021 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14022 if (isec->gc_mark
14023 && (isec->flags & SEC_DEBUGGING) != 0)
14024 if (!_bfd_elf_gc_mark (info, isec,
14025 elf_gc_mark_debug_section))
14026 return false;
14027 }
14028 return true;
14029 }
14030
14031 static bool
14032 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
14033 {
14034 bfd *sub;
14035 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14036
14037 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
14038 {
14039 asection *o;
14040
14041 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
14042 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
14043 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
14044 continue;
14045 o = sub->sections;
14046 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14047 continue;
14048
14049 for (o = sub->sections; o != NULL; o = o->next)
14050 {
14051 /* When any section in a section group is kept, we keep all
14052 sections in the section group. If the first member of
14053 the section group is excluded, we will also exclude the
14054 group section. */
14055 if (o->flags & SEC_GROUP)
14056 {
14057 asection *first = elf_next_in_group (o);
14058 o->gc_mark = first->gc_mark;
14059 }
14060
14061 if (o->gc_mark)
14062 continue;
14063
14064 /* Skip sweeping sections already excluded. */
14065 if (o->flags & SEC_EXCLUDE)
14066 continue;
14067
14068 /* Since this is early in the link process, it is simple
14069 to remove a section from the output. */
14070 o->flags |= SEC_EXCLUDE;
14071
14072 if (info->print_gc_sections && o->size != 0)
14073 /* xgettext:c-format */
14074 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
14075 o, sub);
14076 }
14077 }
14078
14079 return true;
14080 }
14081
14082 /* Propagate collected vtable information. This is called through
14083 elf_link_hash_traverse. */
14084
14085 static bool
14086 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
14087 {
14088 /* Those that are not vtables. */
14089 if (h->start_stop
14090 || h->u2.vtable == NULL
14091 || h->u2.vtable->parent == NULL)
14092 return true;
14093
14094 /* Those vtables that do not have parents, we cannot merge. */
14095 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
14096 return true;
14097
14098 /* If we've already been done, exit. */
14099 if (h->u2.vtable->used && h->u2.vtable->used[-1])
14100 return true;
14101
14102 /* Make sure the parent's table is up to date. */
14103 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
14104
14105 if (h->u2.vtable->used == NULL)
14106 {
14107 /* None of this table's entries were referenced. Re-use the
14108 parent's table. */
14109 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
14110 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
14111 }
14112 else
14113 {
14114 size_t n;
14115 bool *cu, *pu;
14116
14117 /* Or the parent's entries into ours. */
14118 cu = h->u2.vtable->used;
14119 cu[-1] = true;
14120 pu = h->u2.vtable->parent->u2.vtable->used;
14121 if (pu != NULL)
14122 {
14123 const struct elf_backend_data *bed;
14124 unsigned int log_file_align;
14125
14126 bed = get_elf_backend_data (h->root.u.def.section->owner);
14127 log_file_align = bed->s->log_file_align;
14128 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
14129 while (n--)
14130 {
14131 if (*pu)
14132 *cu = true;
14133 pu++;
14134 cu++;
14135 }
14136 }
14137 }
14138
14139 return true;
14140 }
14141
14142 struct link_info_ok
14143 {
14144 struct bfd_link_info *info;
14145 bool ok;
14146 };
14147
14148 static bool
14149 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h,
14150 void *ptr)
14151 {
14152 asection *sec;
14153 bfd_vma hstart, hend;
14154 Elf_Internal_Rela *relstart, *relend, *rel;
14155 const struct elf_backend_data *bed;
14156 unsigned int log_file_align;
14157 struct link_info_ok *info = (struct link_info_ok *) ptr;
14158
14159 /* Take care of both those symbols that do not describe vtables as
14160 well as those that are not loaded. */
14161 if (h->start_stop
14162 || h->u2.vtable == NULL
14163 || h->u2.vtable->parent == NULL)
14164 return true;
14165
14166 BFD_ASSERT (h->root.type == bfd_link_hash_defined
14167 || h->root.type == bfd_link_hash_defweak);
14168
14169 sec = h->root.u.def.section;
14170 hstart = h->root.u.def.value;
14171 hend = hstart + h->size;
14172
14173 relstart = _bfd_elf_link_info_read_relocs (sec->owner, info->info,
14174 sec, NULL, NULL, true);
14175 if (!relstart)
14176 return info->ok = false;
14177 bed = get_elf_backend_data (sec->owner);
14178 log_file_align = bed->s->log_file_align;
14179
14180 relend = relstart + sec->reloc_count;
14181
14182 for (rel = relstart; rel < relend; ++rel)
14183 if (rel->r_offset >= hstart && rel->r_offset < hend)
14184 {
14185 /* If the entry is in use, do nothing. */
14186 if (h->u2.vtable->used
14187 && (rel->r_offset - hstart) < h->u2.vtable->size)
14188 {
14189 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
14190 if (h->u2.vtable->used[entry])
14191 continue;
14192 }
14193 /* Otherwise, kill it. */
14194 rel->r_offset = rel->r_info = rel->r_addend = 0;
14195 }
14196
14197 return true;
14198 }
14199
14200 /* Mark sections containing dynamically referenced symbols. When
14201 building shared libraries, we must assume that any visible symbol is
14202 referenced. */
14203
14204 bool
14205 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
14206 {
14207 struct bfd_link_info *info = (struct bfd_link_info *) inf;
14208 struct bfd_elf_dynamic_list *d = info->dynamic_list;
14209
14210 if ((h->root.type == bfd_link_hash_defined
14211 || h->root.type == bfd_link_hash_defweak)
14212 && (!h->start_stop
14213 || h->root.ldscript_def
14214 || !info->start_stop_gc)
14215 && ((h->ref_dynamic && !h->forced_local)
14216 || ((h->def_regular || ELF_COMMON_DEF_P (h))
14217 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
14218 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
14219 && (!bfd_link_executable (info)
14220 || info->gc_keep_exported
14221 || info->export_dynamic
14222 || (h->dynamic
14223 && d != NULL
14224 && (*d->match) (&d->head, NULL, h->root.root.string)))
14225 && (h->versioned >= versioned
14226 || !bfd_hide_sym_by_version (info->version_info,
14227 h->root.root.string)))))
14228 h->root.u.def.section->flags |= SEC_KEEP;
14229
14230 return true;
14231 }
14232
14233 /* Keep all sections containing symbols undefined on the command-line,
14234 and the section containing the entry symbol. */
14235
14236 void
14237 _bfd_elf_gc_keep (struct bfd_link_info *info)
14238 {
14239 struct bfd_sym_chain *sym;
14240
14241 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
14242 {
14243 struct elf_link_hash_entry *h;
14244
14245 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
14246 false, false, false);
14247
14248 if (h != NULL
14249 && (h->root.type == bfd_link_hash_defined
14250 || h->root.type == bfd_link_hash_defweak)
14251 && !bfd_is_const_section (h->root.u.def.section))
14252 h->root.u.def.section->flags |= SEC_KEEP;
14253 }
14254 }
14255
14256 bool
14257 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
14258 struct bfd_link_info *info)
14259 {
14260 bfd *ibfd = info->input_bfds;
14261
14262 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14263 {
14264 asection *sec;
14265 struct elf_reloc_cookie cookie;
14266
14267 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
14268 continue;
14269 sec = ibfd->sections;
14270 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14271 continue;
14272
14273 if (!init_reloc_cookie (&cookie, info, ibfd))
14274 return false;
14275
14276 for (sec = ibfd->sections; sec; sec = sec->next)
14277 {
14278 if (startswith (bfd_section_name (sec), ".eh_frame_entry")
14279 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
14280 {
14281 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
14282 fini_reloc_cookie_rels (&cookie, sec);
14283 }
14284 }
14285 }
14286 return true;
14287 }
14288
14289 /* Do mark and sweep of unused sections. */
14290
14291 bool
14292 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
14293 {
14294 bool ok = true;
14295 bfd *sub;
14296 elf_gc_mark_hook_fn gc_mark_hook;
14297 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14298 struct elf_link_hash_table *htab;
14299 struct link_info_ok info_ok;
14300
14301 if (!bed->can_gc_sections
14302 || !is_elf_hash_table (info->hash))
14303 {
14304 _bfd_error_handler(_("warning: gc-sections option ignored"));
14305 return true;
14306 }
14307
14308 bed->gc_keep (info);
14309 htab = elf_hash_table (info);
14310
14311 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
14312 at the .eh_frame section if we can mark the FDEs individually. */
14313 for (sub = info->input_bfds;
14314 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
14315 sub = sub->link.next)
14316 {
14317 asection *sec;
14318 struct elf_reloc_cookie cookie;
14319
14320 sec = sub->sections;
14321 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14322 continue;
14323 sec = bfd_get_section_by_name (sub, ".eh_frame");
14324 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
14325 {
14326 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
14327 if (elf_section_data (sec)->sec_info
14328 && (sec->flags & SEC_LINKER_CREATED) == 0)
14329 elf_eh_frame_section (sub) = sec;
14330 fini_reloc_cookie_for_section (&cookie, sec);
14331 sec = bfd_get_next_section_by_name (NULL, sec);
14332 }
14333 }
14334
14335 /* Apply transitive closure to the vtable entry usage info. */
14336 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
14337 if (!ok)
14338 return false;
14339
14340 /* Kill the vtable relocations that were not used. */
14341 info_ok.info = info;
14342 info_ok.ok = true;
14343 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &info_ok);
14344 if (!info_ok.ok)
14345 return false;
14346
14347 /* Mark dynamically referenced symbols. */
14348 if (htab->dynamic_sections_created || info->gc_keep_exported)
14349 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
14350
14351 /* Grovel through relocs to find out who stays ... */
14352 gc_mark_hook = bed->gc_mark_hook;
14353 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
14354 {
14355 asection *o;
14356
14357 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
14358 || elf_object_id (sub) != elf_hash_table_id (htab)
14359 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
14360 continue;
14361
14362 o = sub->sections;
14363 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14364 continue;
14365
14366 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
14367 Also treat note sections as a root, if the section is not part
14368 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
14369 well as FINI_ARRAY sections for ld -r. */
14370 for (o = sub->sections; o != NULL; o = o->next)
14371 if (!o->gc_mark
14372 && (o->flags & SEC_EXCLUDE) == 0
14373 && ((o->flags & SEC_KEEP) != 0
14374 || (bfd_link_relocatable (info)
14375 && ((elf_section_data (o)->this_hdr.sh_type
14376 == SHT_PREINIT_ARRAY)
14377 || (elf_section_data (o)->this_hdr.sh_type
14378 == SHT_INIT_ARRAY)
14379 || (elf_section_data (o)->this_hdr.sh_type
14380 == SHT_FINI_ARRAY)))
14381 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
14382 && elf_next_in_group (o) == NULL
14383 && elf_linked_to_section (o) == NULL)
14384 || ((elf_tdata (sub)->has_gnu_osabi & elf_gnu_osabi_retain)
14385 && (elf_section_flags (o) & SHF_GNU_RETAIN))))
14386 {
14387 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
14388 return false;
14389 }
14390 }
14391
14392 /* Allow the backend to mark additional target specific sections. */
14393 bed->gc_mark_extra_sections (info, gc_mark_hook);
14394
14395 /* ... and mark SEC_EXCLUDE for those that go. */
14396 return elf_gc_sweep (abfd, info);
14397 }
14398
14399 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
14401
14402 bool
14403 bfd_elf_gc_record_vtinherit (bfd *abfd,
14404 asection *sec,
14405 struct elf_link_hash_entry *h,
14406 bfd_vma offset)
14407 {
14408 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
14409 struct elf_link_hash_entry **search, *child;
14410 size_t extsymcount;
14411 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14412
14413 /* The sh_info field of the symtab header tells us where the
14414 external symbols start. We don't care about the local symbols at
14415 this point. */
14416 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
14417 if (!elf_bad_symtab (abfd))
14418 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
14419
14420 sym_hashes = elf_sym_hashes (abfd);
14421 sym_hashes_end = PTR_ADD (sym_hashes, extsymcount);
14422
14423 /* Hunt down the child symbol, which is in this section at the same
14424 offset as the relocation. */
14425 for (search = sym_hashes; search != sym_hashes_end; ++search)
14426 {
14427 if ((child = *search) != NULL
14428 && (child->root.type == bfd_link_hash_defined
14429 || child->root.type == bfd_link_hash_defweak)
14430 && child->root.u.def.section == sec
14431 && child->root.u.def.value == offset)
14432 goto win;
14433 }
14434
14435 /* xgettext:c-format */
14436 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
14437 abfd, sec, (uint64_t) offset);
14438 bfd_set_error (bfd_error_invalid_operation);
14439 return false;
14440
14441 win:
14442 if (!child->u2.vtable)
14443 {
14444 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
14445 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
14446 if (!child->u2.vtable)
14447 return false;
14448 }
14449 if (!h)
14450 {
14451 /* This *should* only be the absolute section. It could potentially
14452 be that someone has defined a non-global vtable though, which
14453 would be bad. It isn't worth paging in the local symbols to be
14454 sure though; that case should simply be handled by the assembler. */
14455
14456 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
14457 }
14458 else
14459 child->u2.vtable->parent = h;
14460
14461 return true;
14462 }
14463
14464 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
14465
14466 bool
14467 bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec,
14468 struct elf_link_hash_entry *h,
14469 bfd_vma addend)
14470 {
14471 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14472 unsigned int log_file_align = bed->s->log_file_align;
14473
14474 if (!h)
14475 {
14476 /* xgettext:c-format */
14477 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
14478 abfd, sec);
14479 bfd_set_error (bfd_error_bad_value);
14480 return false;
14481 }
14482
14483 if (!h->u2.vtable)
14484 {
14485 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
14486 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
14487 if (!h->u2.vtable)
14488 return false;
14489 }
14490
14491 if (addend >= h->u2.vtable->size)
14492 {
14493 size_t size, bytes, file_align;
14494 bool *ptr = h->u2.vtable->used;
14495
14496 /* While the symbol is undefined, we have to be prepared to handle
14497 a zero size. */
14498 file_align = 1 << log_file_align;
14499 if (h->root.type == bfd_link_hash_undefined)
14500 size = addend + file_align;
14501 else
14502 {
14503 size = h->size;
14504 if (addend >= size)
14505 {
14506 /* Oops! We've got a reference past the defined end of
14507 the table. This is probably a bug -- shall we warn? */
14508 size = addend + file_align;
14509 }
14510 }
14511 size = (size + file_align - 1) & -file_align;
14512
14513 /* Allocate one extra entry for use as a "done" flag for the
14514 consolidation pass. */
14515 bytes = ((size >> log_file_align) + 1) * sizeof (bool);
14516
14517 if (ptr)
14518 {
14519 ptr = (bool *) bfd_realloc (ptr - 1, bytes);
14520
14521 if (ptr != NULL)
14522 {
14523 size_t oldbytes;
14524
14525 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
14526 * sizeof (bool));
14527 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
14528 }
14529 }
14530 else
14531 ptr = (bool *) bfd_zmalloc (bytes);
14532
14533 if (ptr == NULL)
14534 return false;
14535
14536 /* And arrange for that done flag to be at index -1. */
14537 h->u2.vtable->used = ptr + 1;
14538 h->u2.vtable->size = size;
14539 }
14540
14541 h->u2.vtable->used[addend >> log_file_align] = true;
14542
14543 return true;
14544 }
14545
14546 /* Map an ELF section header flag to its corresponding string. */
14547 typedef struct
14548 {
14549 char *flag_name;
14550 flagword flag_value;
14551 } elf_flags_to_name_table;
14552
14553 static const elf_flags_to_name_table elf_flags_to_names [] =
14554 {
14555 { "SHF_WRITE", SHF_WRITE },
14556 { "SHF_ALLOC", SHF_ALLOC },
14557 { "SHF_EXECINSTR", SHF_EXECINSTR },
14558 { "SHF_MERGE", SHF_MERGE },
14559 { "SHF_STRINGS", SHF_STRINGS },
14560 { "SHF_INFO_LINK", SHF_INFO_LINK},
14561 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
14562 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
14563 { "SHF_GROUP", SHF_GROUP },
14564 { "SHF_TLS", SHF_TLS },
14565 { "SHF_MASKOS", SHF_MASKOS },
14566 { "SHF_EXCLUDE", SHF_EXCLUDE },
14567 };
14568
14569 /* Returns TRUE if the section is to be included, otherwise FALSE. */
14570 bool
14571 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
14572 struct flag_info *flaginfo,
14573 asection *section)
14574 {
14575 const bfd_vma sh_flags = elf_section_flags (section);
14576
14577 if (!flaginfo->flags_initialized)
14578 {
14579 bfd *obfd = info->output_bfd;
14580 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14581 struct flag_info_list *tf = flaginfo->flag_list;
14582 int with_hex = 0;
14583 int without_hex = 0;
14584
14585 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
14586 {
14587 unsigned i;
14588 flagword (*lookup) (char *);
14589
14590 lookup = bed->elf_backend_lookup_section_flags_hook;
14591 if (lookup != NULL)
14592 {
14593 flagword hexval = (*lookup) ((char *) tf->name);
14594
14595 if (hexval != 0)
14596 {
14597 if (tf->with == with_flags)
14598 with_hex |= hexval;
14599 else if (tf->with == without_flags)
14600 without_hex |= hexval;
14601 tf->valid = true;
14602 continue;
14603 }
14604 }
14605 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
14606 {
14607 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
14608 {
14609 if (tf->with == with_flags)
14610 with_hex |= elf_flags_to_names[i].flag_value;
14611 else if (tf->with == without_flags)
14612 without_hex |= elf_flags_to_names[i].flag_value;
14613 tf->valid = true;
14614 break;
14615 }
14616 }
14617 if (!tf->valid)
14618 {
14619 info->callbacks->einfo
14620 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
14621 return false;
14622 }
14623 }
14624 flaginfo->flags_initialized = true;
14625 flaginfo->only_with_flags |= with_hex;
14626 flaginfo->not_with_flags |= without_hex;
14627 }
14628
14629 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
14630 return false;
14631
14632 if ((flaginfo->not_with_flags & sh_flags) != 0)
14633 return false;
14634
14635 return true;
14636 }
14637
14638 struct alloc_got_off_arg {
14639 bfd_vma gotoff;
14640 struct bfd_link_info *info;
14641 };
14642
14643 /* We need a special top-level link routine to convert got reference counts
14644 to real got offsets. */
14645
14646 static bool
14647 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
14648 {
14649 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
14650 bfd *obfd = gofarg->info->output_bfd;
14651 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14652
14653 if (h->got.refcount > 0)
14654 {
14655 h->got.offset = gofarg->gotoff;
14656 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
14657 }
14658 else
14659 h->got.offset = (bfd_vma) -1;
14660
14661 return true;
14662 }
14663
14664 /* And an accompanying bit to work out final got entry offsets once
14665 we're done. Should be called from final_link. */
14666
14667 bool
14668 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
14669 struct bfd_link_info *info)
14670 {
14671 bfd *i;
14672 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14673 bfd_vma gotoff;
14674 struct alloc_got_off_arg gofarg;
14675
14676 BFD_ASSERT (abfd == info->output_bfd);
14677
14678 if (! is_elf_hash_table (info->hash))
14679 return false;
14680
14681 /* The GOT offset is relative to the .got section, but the GOT header is
14682 put into the .got.plt section, if the backend uses it. */
14683 if (bed->want_got_plt)
14684 gotoff = 0;
14685 else
14686 gotoff = bed->got_header_size;
14687
14688 /* Do the local .got entries first. */
14689 for (i = info->input_bfds; i; i = i->link.next)
14690 {
14691 bfd_signed_vma *local_got;
14692 size_t j, locsymcount;
14693 Elf_Internal_Shdr *symtab_hdr;
14694
14695 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
14696 continue;
14697
14698 local_got = elf_local_got_refcounts (i);
14699 if (!local_got)
14700 continue;
14701
14702 symtab_hdr = &elf_tdata (i)->symtab_hdr;
14703 if (elf_bad_symtab (i))
14704 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
14705 else
14706 locsymcount = symtab_hdr->sh_info;
14707
14708 for (j = 0; j < locsymcount; ++j)
14709 {
14710 if (local_got[j] > 0)
14711 {
14712 local_got[j] = gotoff;
14713 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
14714 }
14715 else
14716 local_got[j] = (bfd_vma) -1;
14717 }
14718 }
14719
14720 /* Then the global .got entries. .plt refcounts are handled by
14721 adjust_dynamic_symbol */
14722 gofarg.gotoff = gotoff;
14723 gofarg.info = info;
14724 elf_link_hash_traverse (elf_hash_table (info),
14725 elf_gc_allocate_got_offsets,
14726 &gofarg);
14727 return true;
14728 }
14729
14730 /* Many folk need no more in the way of final link than this, once
14731 got entry reference counting is enabled. */
14732
14733 bool
14734 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
14735 {
14736 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
14737 return false;
14738
14739 /* Invoke the regular ELF backend linker to do all the work. */
14740 return bfd_elf_final_link (abfd, info);
14741 }
14742
14743 bool
14744 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
14745 {
14746 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
14747
14748 if (rcookie->bad_symtab)
14749 rcookie->rel = rcookie->rels;
14750
14751 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
14752 {
14753 unsigned long r_symndx;
14754
14755 if (! rcookie->bad_symtab)
14756 if (rcookie->rel->r_offset > offset)
14757 return false;
14758 if (rcookie->rel->r_offset != offset)
14759 continue;
14760
14761 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
14762 if (r_symndx == STN_UNDEF)
14763 return true;
14764
14765 if (r_symndx >= rcookie->locsymcount
14766 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
14767 {
14768 struct elf_link_hash_entry *h;
14769
14770 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
14771
14772 while (h->root.type == bfd_link_hash_indirect
14773 || h->root.type == bfd_link_hash_warning)
14774 h = (struct elf_link_hash_entry *) h->root.u.i.link;
14775
14776 if ((h->root.type == bfd_link_hash_defined
14777 || h->root.type == bfd_link_hash_defweak)
14778 && (h->root.u.def.section->owner != rcookie->abfd
14779 || h->root.u.def.section->kept_section != NULL
14780 || discarded_section (h->root.u.def.section)))
14781 return true;
14782 }
14783 else
14784 {
14785 /* It's not a relocation against a global symbol,
14786 but it could be a relocation against a local
14787 symbol for a discarded section. */
14788 asection *isec;
14789 Elf_Internal_Sym *isym;
14790
14791 /* Need to: get the symbol; get the section. */
14792 isym = &rcookie->locsyms[r_symndx];
14793 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
14794 if (isec != NULL
14795 && (isec->kept_section != NULL
14796 || discarded_section (isec)))
14797 return true;
14798 }
14799 return false;
14800 }
14801 return false;
14802 }
14803
14804 /* Discard unneeded references to discarded sections.
14805 Returns -1 on error, 1 if any section's size was changed, 0 if
14806 nothing changed. This function assumes that the relocations are in
14807 sorted order, which is true for all known assemblers. */
14808
14809 int
14810 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
14811 {
14812 struct elf_reloc_cookie cookie;
14813 asection *o;
14814 bfd *abfd;
14815 int changed = 0;
14816
14817 if (info->traditional_format
14818 || !is_elf_hash_table (info->hash))
14819 return 0;
14820
14821 o = bfd_get_section_by_name (output_bfd, ".stab");
14822 if (o != NULL)
14823 {
14824 asection *i;
14825
14826 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14827 {
14828 if (i->size == 0
14829 || i->reloc_count == 0
14830 || i->sec_info_type != SEC_INFO_TYPE_STABS)
14831 continue;
14832
14833 abfd = i->owner;
14834 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14835 continue;
14836
14837 if (!init_reloc_cookie_for_section (&cookie, info, i))
14838 return -1;
14839
14840 if (_bfd_discard_section_stabs (abfd, i,
14841 elf_section_data (i)->sec_info,
14842 bfd_elf_reloc_symbol_deleted_p,
14843 &cookie))
14844 changed = 1;
14845
14846 fini_reloc_cookie_for_section (&cookie, i);
14847 }
14848 }
14849
14850 o = NULL;
14851 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
14852 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
14853 if (o != NULL)
14854 {
14855 asection *i;
14856 int eh_changed = 0;
14857 unsigned int eh_alignment; /* Octets. */
14858
14859 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14860 {
14861 if (i->size == 0)
14862 continue;
14863
14864 abfd = i->owner;
14865 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14866 continue;
14867
14868 if (!init_reloc_cookie_for_section (&cookie, info, i))
14869 return -1;
14870
14871 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
14872 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
14873 bfd_elf_reloc_symbol_deleted_p,
14874 &cookie))
14875 {
14876 eh_changed = 1;
14877 if (i->size != i->rawsize)
14878 changed = 1;
14879 }
14880
14881 fini_reloc_cookie_for_section (&cookie, i);
14882 }
14883
14884 eh_alignment = ((1 << o->alignment_power)
14885 * bfd_octets_per_byte (output_bfd, o));
14886 /* Skip over zero terminator, and prevent empty sections from
14887 adding alignment padding at the end. */
14888 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
14889 if (i->size == 0)
14890 i->flags |= SEC_EXCLUDE;
14891 else if (i->size > 4)
14892 break;
14893 /* The last non-empty eh_frame section doesn't need padding. */
14894 if (i != NULL)
14895 i = i->map_tail.s;
14896 /* Any prior sections must pad the last FDE out to the output
14897 section alignment. Otherwise we might have zero padding
14898 between sections, which would be seen as a terminator. */
14899 for (; i != NULL; i = i->map_tail.s)
14900 if (i->size == 4)
14901 /* All but the last zero terminator should have been removed. */
14902 BFD_FAIL ();
14903 else
14904 {
14905 bfd_size_type size
14906 = (i->size + eh_alignment - 1) & -eh_alignment;
14907 if (i->size != size)
14908 {
14909 i->size = size;
14910 changed = 1;
14911 eh_changed = 1;
14912 }
14913 }
14914 if (eh_changed)
14915 elf_link_hash_traverse (elf_hash_table (info),
14916 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
14917 }
14918
14919 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
14920 {
14921 const struct elf_backend_data *bed;
14922 asection *s;
14923
14924 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14925 continue;
14926 s = abfd->sections;
14927 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14928 continue;
14929
14930 bed = get_elf_backend_data (abfd);
14931
14932 if (bed->elf_backend_discard_info != NULL)
14933 {
14934 if (!init_reloc_cookie (&cookie, info, abfd))
14935 return -1;
14936
14937 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
14938 changed = 1;
14939
14940 fini_reloc_cookie (&cookie, abfd);
14941 }
14942 }
14943
14944 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
14945 _bfd_elf_end_eh_frame_parsing (info);
14946
14947 if (info->eh_frame_hdr_type
14948 && !bfd_link_relocatable (info)
14949 && _bfd_elf_discard_section_eh_frame_hdr (info))
14950 changed = 1;
14951
14952 return changed;
14953 }
14954
14955 bool
14956 _bfd_elf_section_already_linked (bfd *abfd,
14957 asection *sec,
14958 struct bfd_link_info *info)
14959 {
14960 flagword flags;
14961 const char *name, *key;
14962 struct bfd_section_already_linked *l;
14963 struct bfd_section_already_linked_hash_entry *already_linked_list;
14964
14965 if (sec->output_section == bfd_abs_section_ptr)
14966 return false;
14967
14968 flags = sec->flags;
14969
14970 /* Return if it isn't a linkonce section. A comdat group section
14971 also has SEC_LINK_ONCE set. */
14972 if ((flags & SEC_LINK_ONCE) == 0)
14973 return false;
14974
14975 /* Don't put group member sections on our list of already linked
14976 sections. They are handled as a group via their group section. */
14977 if (elf_sec_group (sec) != NULL)
14978 return false;
14979
14980 /* For a SHT_GROUP section, use the group signature as the key. */
14981 name = sec->name;
14982 if ((flags & SEC_GROUP) != 0
14983 && elf_next_in_group (sec) != NULL
14984 && elf_group_name (elf_next_in_group (sec)) != NULL)
14985 key = elf_group_name (elf_next_in_group (sec));
14986 else
14987 {
14988 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
14989 if (startswith (name, ".gnu.linkonce.")
14990 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
14991 key++;
14992 else
14993 /* Must be a user linkonce section that doesn't follow gcc's
14994 naming convention. In this case we won't be matching
14995 single member groups. */
14996 key = name;
14997 }
14998
14999 already_linked_list = bfd_section_already_linked_table_lookup (key);
15000
15001 for (l = already_linked_list->entry; l != NULL; l = l->next)
15002 {
15003 /* We may have 2 different types of sections on the list: group
15004 sections with a signature of <key> (<key> is some string),
15005 and linkonce sections named .gnu.linkonce.<type>.<key>.
15006 Match like sections. LTO plugin sections are an exception.
15007 They are always named .gnu.linkonce.t.<key> and match either
15008 type of section. */
15009 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
15010 && ((flags & SEC_GROUP) != 0
15011 || strcmp (name, l->sec->name) == 0))
15012 || (l->sec->owner->flags & BFD_PLUGIN) != 0
15013 || (sec->owner->flags & BFD_PLUGIN) != 0)
15014 {
15015 /* The section has already been linked. See if we should
15016 issue a warning. */
15017 if (!_bfd_handle_already_linked (sec, l, info))
15018 return false;
15019
15020 if (flags & SEC_GROUP)
15021 {
15022 asection *first = elf_next_in_group (sec);
15023 asection *s = first;
15024
15025 while (s != NULL)
15026 {
15027 s->output_section = bfd_abs_section_ptr;
15028 /* Record which group discards it. */
15029 s->kept_section = l->sec;
15030 s = elf_next_in_group (s);
15031 /* These lists are circular. */
15032 if (s == first)
15033 break;
15034 }
15035 }
15036
15037 return true;
15038 }
15039 }
15040
15041 /* A single member comdat group section may be discarded by a
15042 linkonce section and vice versa. */
15043 if ((flags & SEC_GROUP) != 0)
15044 {
15045 asection *first = elf_next_in_group (sec);
15046
15047 if (first != NULL && elf_next_in_group (first) == first)
15048 /* Check this single member group against linkonce sections. */
15049 for (l = already_linked_list->entry; l != NULL; l = l->next)
15050 if ((l->sec->flags & SEC_GROUP) == 0
15051 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
15052 {
15053 first->output_section = bfd_abs_section_ptr;
15054 first->kept_section = l->sec;
15055 sec->output_section = bfd_abs_section_ptr;
15056 break;
15057 }
15058 }
15059 else
15060 /* Check this linkonce section against single member groups. */
15061 for (l = already_linked_list->entry; l != NULL; l = l->next)
15062 if (l->sec->flags & SEC_GROUP)
15063 {
15064 asection *first = elf_next_in_group (l->sec);
15065
15066 if (first != NULL
15067 && elf_next_in_group (first) == first
15068 && bfd_elf_match_symbols_in_sections (first, sec, info))
15069 {
15070 sec->output_section = bfd_abs_section_ptr;
15071 sec->kept_section = first;
15072 break;
15073 }
15074 }
15075
15076 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
15077 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
15078 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
15079 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
15080 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
15081 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
15082 `.gnu.linkonce.t.F' section from a different bfd not requiring any
15083 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
15084 The reverse order cannot happen as there is never a bfd with only the
15085 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
15086 matter as here were are looking only for cross-bfd sections. */
15087
15088 if ((flags & SEC_GROUP) == 0 && startswith (name, ".gnu.linkonce.r."))
15089 for (l = already_linked_list->entry; l != NULL; l = l->next)
15090 if ((l->sec->flags & SEC_GROUP) == 0
15091 && startswith (l->sec->name, ".gnu.linkonce.t."))
15092 {
15093 if (abfd != l->sec->owner)
15094 sec->output_section = bfd_abs_section_ptr;
15095 break;
15096 }
15097
15098 /* This is the first section with this name. Record it. */
15099 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
15100 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
15101 return sec->output_section == bfd_abs_section_ptr;
15102 }
15103
15104 bool
15105 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
15106 {
15107 return sym->st_shndx == SHN_COMMON;
15108 }
15109
15110 unsigned int
15111 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
15112 {
15113 return SHN_COMMON;
15114 }
15115
15116 asection *
15117 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
15118 {
15119 return bfd_com_section_ptr;
15120 }
15121
15122 bfd_vma
15123 _bfd_elf_default_got_elt_size (bfd *abfd,
15124 struct bfd_link_info *info ATTRIBUTE_UNUSED,
15125 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
15126 bfd *ibfd ATTRIBUTE_UNUSED,
15127 unsigned long symndx ATTRIBUTE_UNUSED)
15128 {
15129 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15130 return bed->s->arch_size / 8;
15131 }
15132
15133 /* Routines to support the creation of dynamic relocs. */
15134
15135 /* Returns the name of the dynamic reloc section associated with SEC. */
15136
15137 static const char *
15138 get_dynamic_reloc_section_name (bfd * abfd,
15139 asection * sec,
15140 bool is_rela)
15141 {
15142 char *name;
15143 const char *old_name = bfd_section_name (sec);
15144 const char *prefix = is_rela ? ".rela" : ".rel";
15145
15146 if (old_name == NULL)
15147 return NULL;
15148
15149 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
15150 sprintf (name, "%s%s", prefix, old_name);
15151
15152 return name;
15153 }
15154
15155 /* Returns the dynamic reloc section associated with SEC.
15156 If necessary compute the name of the dynamic reloc section based
15157 on SEC's name (looked up in ABFD's string table) and the setting
15158 of IS_RELA. */
15159
15160 asection *
15161 _bfd_elf_get_dynamic_reloc_section (bfd *abfd,
15162 asection *sec,
15163 bool is_rela)
15164 {
15165 asection *reloc_sec = elf_section_data (sec)->sreloc;
15166
15167 if (reloc_sec == NULL)
15168 {
15169 const char *name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
15170
15171 if (name != NULL)
15172 {
15173 reloc_sec = bfd_get_linker_section (abfd, name);
15174
15175 if (reloc_sec != NULL)
15176 elf_section_data (sec)->sreloc = reloc_sec;
15177 }
15178 }
15179
15180 return reloc_sec;
15181 }
15182
15183 /* Returns the dynamic reloc section associated with SEC. If the
15184 section does not exist it is created and attached to the DYNOBJ
15185 bfd and stored in the SRELOC field of SEC's elf_section_data
15186 structure.
15187
15188 ALIGNMENT is the alignment for the newly created section and
15189 IS_RELA defines whether the name should be .rela.<SEC's name>
15190 or .rel.<SEC's name>. The section name is looked up in the
15191 string table associated with ABFD. */
15192
15193 asection *
15194 _bfd_elf_make_dynamic_reloc_section (asection *sec,
15195 bfd *dynobj,
15196 unsigned int alignment,
15197 bfd *abfd,
15198 bool is_rela)
15199 {
15200 asection * reloc_sec = elf_section_data (sec)->sreloc;
15201
15202 if (reloc_sec == NULL)
15203 {
15204 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
15205
15206 if (name == NULL)
15207 return NULL;
15208
15209 reloc_sec = bfd_get_linker_section (dynobj, name);
15210
15211 if (reloc_sec == NULL)
15212 {
15213 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
15214 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
15215 if ((sec->flags & SEC_ALLOC) != 0)
15216 flags |= SEC_ALLOC | SEC_LOAD;
15217
15218 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
15219 if (reloc_sec != NULL)
15220 {
15221 /* _bfd_elf_get_sec_type_attr chooses a section type by
15222 name. Override as it may be wrong, eg. for a user
15223 section named "auto" we'll get ".relauto" which is
15224 seen to be a .rela section. */
15225 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
15226 if (!bfd_set_section_alignment (reloc_sec, alignment))
15227 reloc_sec = NULL;
15228 }
15229 }
15230
15231 elf_section_data (sec)->sreloc = reloc_sec;
15232 }
15233
15234 return reloc_sec;
15235 }
15236
15237 /* Copy the ELF symbol type and other attributes for a linker script
15238 assignment from HSRC to HDEST. Generally this should be treated as
15239 if we found a strong non-dynamic definition for HDEST (except that
15240 ld ignores multiple definition errors). */
15241 void
15242 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
15243 struct bfd_link_hash_entry *hdest,
15244 struct bfd_link_hash_entry *hsrc)
15245 {
15246 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
15247 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
15248 Elf_Internal_Sym isym;
15249
15250 ehdest->type = ehsrc->type;
15251 ehdest->target_internal = ehsrc->target_internal;
15252
15253 isym.st_other = ehsrc->other;
15254 elf_merge_st_other (abfd, ehdest, isym.st_other, NULL, true, false);
15255 }
15256
15257 /* Append a RELA relocation REL to section S in BFD. */
15258
15259 void
15260 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15261 {
15262 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15263 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
15264 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
15265 bed->s->swap_reloca_out (abfd, rel, loc);
15266 }
15267
15268 /* Append a REL relocation REL to section S in BFD. */
15269
15270 void
15271 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15272 {
15273 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15274 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
15275 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
15276 bed->s->swap_reloc_out (abfd, rel, loc);
15277 }
15278
15279 /* Define __start, __stop, .startof. or .sizeof. symbol. */
15280
15281 struct bfd_link_hash_entry *
15282 bfd_elf_define_start_stop (struct bfd_link_info *info,
15283 const char *symbol, asection *sec)
15284 {
15285 struct elf_link_hash_entry *h;
15286
15287 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
15288 false, false, true);
15289 /* NB: Common symbols will be turned into definition later. */
15290 if (h != NULL
15291 && !h->root.ldscript_def
15292 && (h->root.type == bfd_link_hash_undefined
15293 || h->root.type == bfd_link_hash_undefweak
15294 || ((h->ref_regular || h->def_dynamic)
15295 && !h->def_regular
15296 && h->root.type != bfd_link_hash_common)))
15297 {
15298 bool was_dynamic = h->ref_dynamic || h->def_dynamic;
15299 h->verinfo.verdef = NULL;
15300 h->root.type = bfd_link_hash_defined;
15301 h->root.u.def.section = sec;
15302 h->root.u.def.value = 0;
15303 h->def_regular = 1;
15304 h->def_dynamic = 0;
15305 h->start_stop = 1;
15306 h->u2.start_stop_section = sec;
15307 if (symbol[0] == '.')
15308 {
15309 /* .startof. and .sizeof. symbols are local. */
15310 const struct elf_backend_data *bed;
15311 bed = get_elf_backend_data (info->output_bfd);
15312 (*bed->elf_backend_hide_symbol) (info, h, true);
15313 }
15314 else
15315 {
15316 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
15317 h->other = ((h->other & ~ELF_ST_VISIBILITY (-1))
15318 | info->start_stop_visibility);
15319 if (was_dynamic)
15320 bfd_elf_link_record_dynamic_symbol (info, h);
15321 }
15322 return &h->root;
15323 }
15324 return NULL;
15325 }
15326
15327 /* Find dynamic relocs for H that apply to read-only sections. */
15328
15329 asection *
15330 _bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry *h)
15331 {
15332 struct elf_dyn_relocs *p;
15333
15334 for (p = h->dyn_relocs; p != NULL; p = p->next)
15335 {
15336 asection *s = p->sec->output_section;
15337
15338 if (s != NULL && (s->flags & SEC_READONLY) != 0)
15339 return p->sec;
15340 }
15341 return NULL;
15342 }
15343
15344 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
15345 read-only sections. */
15346
15347 bool
15348 _bfd_elf_maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
15349 {
15350 asection *sec;
15351
15352 if (h->root.type == bfd_link_hash_indirect)
15353 return true;
15354
15355 sec = _bfd_elf_readonly_dynrelocs (h);
15356 if (sec != NULL)
15357 {
15358 struct bfd_link_info *info = (struct bfd_link_info *) inf;
15359
15360 info->flags |= DF_TEXTREL;
15361 /* xgettext:c-format */
15362 info->callbacks->minfo (_("%pB: dynamic relocation against `%pT' "
15363 "in read-only section `%pA'\n"),
15364 sec->owner, h->root.root.string, sec);
15365
15366 if (bfd_link_textrel_check (info))
15367 /* xgettext:c-format */
15368 info->callbacks->einfo (_("%P: %pB: warning: relocation against `%s' "
15369 "in read-only section `%pA'\n"),
15370 sec->owner, h->root.root.string, sec);
15371
15372 /* Not an error, just cut short the traversal. */
15373 return false;
15374 }
15375 return true;
15376 }
15377
15378 /* Add dynamic tags. */
15379
15380 bool
15381 _bfd_elf_add_dynamic_tags (bfd *output_bfd, struct bfd_link_info *info,
15382 bool need_dynamic_reloc)
15383 {
15384 struct elf_link_hash_table *htab = elf_hash_table (info);
15385
15386 if (htab->dynamic_sections_created)
15387 {
15388 /* Add some entries to the .dynamic section. We fill in the
15389 values later, in finish_dynamic_sections, but we must add
15390 the entries now so that we get the correct size for the
15391 .dynamic section. The DT_DEBUG entry is filled in by the
15392 dynamic linker and used by the debugger. */
15393 #define add_dynamic_entry(TAG, VAL) \
15394 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
15395
15396 const struct elf_backend_data *bed
15397 = get_elf_backend_data (output_bfd);
15398
15399 if (bfd_link_executable (info))
15400 {
15401 if (!add_dynamic_entry (DT_DEBUG, 0))
15402 return false;
15403 }
15404
15405 if (htab->dt_pltgot_required || htab->splt->size != 0)
15406 {
15407 /* DT_PLTGOT is used by prelink even if there is no PLT
15408 relocation. */
15409 if (!add_dynamic_entry (DT_PLTGOT, 0))
15410 return false;
15411 }
15412
15413 if (htab->dt_jmprel_required || htab->srelplt->size != 0)
15414 {
15415 if (!add_dynamic_entry (DT_PLTRELSZ, 0)
15416 || !add_dynamic_entry (DT_PLTREL,
15417 (bed->rela_plts_and_copies_p
15418 ? DT_RELA : DT_REL))
15419 || !add_dynamic_entry (DT_JMPREL, 0))
15420 return false;
15421 }
15422
15423 if (htab->tlsdesc_plt
15424 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
15425 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
15426 return false;
15427
15428 if (need_dynamic_reloc)
15429 {
15430 if (bed->rela_plts_and_copies_p)
15431 {
15432 if (!add_dynamic_entry (DT_RELA, 0)
15433 || !add_dynamic_entry (DT_RELASZ, 0)
15434 || !add_dynamic_entry (DT_RELAENT,
15435 bed->s->sizeof_rela))
15436 return false;
15437 }
15438 else
15439 {
15440 if (!add_dynamic_entry (DT_REL, 0)
15441 || !add_dynamic_entry (DT_RELSZ, 0)
15442 || !add_dynamic_entry (DT_RELENT,
15443 bed->s->sizeof_rel))
15444 return false;
15445 }
15446
15447 /* If any dynamic relocs apply to a read-only section,
15448 then we need a DT_TEXTREL entry. */
15449 if ((info->flags & DF_TEXTREL) == 0)
15450 elf_link_hash_traverse (htab, _bfd_elf_maybe_set_textrel,
15451 info);
15452
15453 if ((info->flags & DF_TEXTREL) != 0)
15454 {
15455 if (htab->ifunc_resolvers)
15456 info->callbacks->einfo
15457 (_("%P: warning: GNU indirect functions with DT_TEXTREL "
15458 "may result in a segfault at runtime; recompile with %s\n"),
15459 bfd_link_dll (info) ? "-fPIC" : "-fPIE");
15460
15461 if (!add_dynamic_entry (DT_TEXTREL, 0))
15462 return false;
15463 }
15464 }
15465 }
15466 #undef add_dynamic_entry
15467
15468 return true;
15469 }
15470