Tidy warn-execstack handling
[binutils-gdb.git] / bfd / elflink.c
1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2022 Free Software Foundation, Inc.
3
4 This file is part of BFD, the Binary File Descriptor library.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
20
21 #include "sysdep.h"
22 #include "bfd.h"
23 #include "bfdlink.h"
24 #include "libbfd.h"
25 #define ARCH_SIZE 0
26 #include "elf-bfd.h"
27 #include "safe-ctype.h"
28 #include "libiberty.h"
29 #include "objalloc.h"
30 #if BFD_SUPPORTS_PLUGINS
31 #include "plugin-api.h"
32 #include "plugin.h"
33 #endif
34
35 #include <limits.h>
36 #ifndef CHAR_BIT
37 #define CHAR_BIT 8
38 #endif
39
40 /* This struct is used to pass information to routines called via
41 elf_link_hash_traverse which must return failure. */
42
43 struct elf_info_failed
44 {
45 struct bfd_link_info *info;
46 bool failed;
47 };
48
49 /* This structure is used to pass information to
50 _bfd_elf_link_find_version_dependencies. */
51
52 struct elf_find_verdep_info
53 {
54 /* General link information. */
55 struct bfd_link_info *info;
56 /* The number of dependencies. */
57 unsigned int vers;
58 /* Whether we had a failure. */
59 bool failed;
60 };
61
62 static bool _bfd_elf_fix_symbol_flags
63 (struct elf_link_hash_entry *, struct elf_info_failed *);
64
65 asection *
66 _bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
67 unsigned long r_symndx,
68 bool discard)
69 {
70 if (r_symndx >= cookie->locsymcount
71 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
72 {
73 struct elf_link_hash_entry *h;
74
75 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
76
77 while (h->root.type == bfd_link_hash_indirect
78 || h->root.type == bfd_link_hash_warning)
79 h = (struct elf_link_hash_entry *) h->root.u.i.link;
80
81 if ((h->root.type == bfd_link_hash_defined
82 || h->root.type == bfd_link_hash_defweak)
83 && discarded_section (h->root.u.def.section))
84 return h->root.u.def.section;
85 else
86 return NULL;
87 }
88 else
89 {
90 /* It's not a relocation against a global symbol,
91 but it could be a relocation against a local
92 symbol for a discarded section. */
93 asection *isec;
94 Elf_Internal_Sym *isym;
95
96 /* Need to: get the symbol; get the section. */
97 isym = &cookie->locsyms[r_symndx];
98 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
99 if (isec != NULL
100 && discard ? discarded_section (isec) : 1)
101 return isec;
102 }
103 return NULL;
104 }
105
106 /* Define a symbol in a dynamic linkage section. */
107
108 struct elf_link_hash_entry *
109 _bfd_elf_define_linkage_sym (bfd *abfd,
110 struct bfd_link_info *info,
111 asection *sec,
112 const char *name)
113 {
114 struct elf_link_hash_entry *h;
115 struct bfd_link_hash_entry *bh;
116 const struct elf_backend_data *bed;
117
118 h = elf_link_hash_lookup (elf_hash_table (info), name, false, false, false);
119 if (h != NULL)
120 {
121 /* Zap symbol defined in an as-needed lib that wasn't linked.
122 This is a symptom of a larger problem: Absolute symbols
123 defined in shared libraries can't be overridden, because we
124 lose the link to the bfd which is via the symbol section. */
125 h->root.type = bfd_link_hash_new;
126 bh = &h->root;
127 }
128 else
129 bh = NULL;
130
131 bed = get_elf_backend_data (abfd);
132 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
133 sec, 0, NULL, false, bed->collect,
134 &bh))
135 return NULL;
136 h = (struct elf_link_hash_entry *) bh;
137 BFD_ASSERT (h != NULL);
138 h->def_regular = 1;
139 h->non_elf = 0;
140 h->root.linker_def = 1;
141 h->type = STT_OBJECT;
142 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
143 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
144
145 (*bed->elf_backend_hide_symbol) (info, h, true);
146 return h;
147 }
148
149 bool
150 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
151 {
152 flagword flags;
153 asection *s;
154 struct elf_link_hash_entry *h;
155 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
156 struct elf_link_hash_table *htab = elf_hash_table (info);
157
158 /* This function may be called more than once. */
159 if (htab->sgot != NULL)
160 return true;
161
162 flags = bed->dynamic_sec_flags;
163
164 s = bfd_make_section_anyway_with_flags (abfd,
165 (bed->rela_plts_and_copies_p
166 ? ".rela.got" : ".rel.got"),
167 (bed->dynamic_sec_flags
168 | SEC_READONLY));
169 if (s == NULL
170 || !bfd_set_section_alignment (s, bed->s->log_file_align))
171 return false;
172 htab->srelgot = s;
173
174 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
175 if (s == NULL
176 || !bfd_set_section_alignment (s, bed->s->log_file_align))
177 return false;
178 htab->sgot = s;
179
180 if (bed->want_got_plt)
181 {
182 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
183 if (s == NULL
184 || !bfd_set_section_alignment (s, bed->s->log_file_align))
185 return false;
186 htab->sgotplt = s;
187 }
188
189 /* The first bit of the global offset table is the header. */
190 s->size += bed->got_header_size;
191
192 if (bed->want_got_sym)
193 {
194 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
195 (or .got.plt) section. We don't do this in the linker script
196 because we don't want to define the symbol if we are not creating
197 a global offset table. */
198 h = _bfd_elf_define_linkage_sym (abfd, info, s,
199 "_GLOBAL_OFFSET_TABLE_");
200 elf_hash_table (info)->hgot = h;
201 if (h == NULL)
202 return false;
203 }
204
205 return true;
206 }
207 \f
208 /* Create a strtab to hold the dynamic symbol names. */
209 static bool
210 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
211 {
212 struct elf_link_hash_table *hash_table;
213
214 hash_table = elf_hash_table (info);
215 if (hash_table->dynobj == NULL)
216 {
217 /* We may not set dynobj, an input file holding linker created
218 dynamic sections to abfd, which may be a dynamic object with
219 its own dynamic sections. We need to find a normal input file
220 to hold linker created sections if possible. */
221 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
222 {
223 bfd *ibfd;
224 asection *s;
225 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
226 if ((ibfd->flags
227 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
228 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
229 && elf_object_id (ibfd) == elf_hash_table_id (hash_table)
230 && !((s = ibfd->sections) != NULL
231 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS))
232 {
233 abfd = ibfd;
234 break;
235 }
236 }
237 hash_table->dynobj = abfd;
238 }
239
240 if (hash_table->dynstr == NULL)
241 {
242 hash_table->dynstr = _bfd_elf_strtab_init ();
243 if (hash_table->dynstr == NULL)
244 return false;
245 }
246 return true;
247 }
248
249 /* Create some sections which will be filled in with dynamic linking
250 information. ABFD is an input file which requires dynamic sections
251 to be created. The dynamic sections take up virtual memory space
252 when the final executable is run, so we need to create them before
253 addresses are assigned to the output sections. We work out the
254 actual contents and size of these sections later. */
255
256 bool
257 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
258 {
259 flagword flags;
260 asection *s;
261 const struct elf_backend_data *bed;
262 struct elf_link_hash_entry *h;
263
264 if (! is_elf_hash_table (info->hash))
265 return false;
266
267 if (elf_hash_table (info)->dynamic_sections_created)
268 return true;
269
270 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
271 return false;
272
273 abfd = elf_hash_table (info)->dynobj;
274 bed = get_elf_backend_data (abfd);
275
276 flags = bed->dynamic_sec_flags;
277
278 /* A dynamically linked executable has a .interp section, but a
279 shared library does not. */
280 if (bfd_link_executable (info) && !info->nointerp)
281 {
282 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
283 flags | SEC_READONLY);
284 if (s == NULL)
285 return false;
286 }
287
288 /* Create sections to hold version informations. These are removed
289 if they are not needed. */
290 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
291 flags | SEC_READONLY);
292 if (s == NULL
293 || !bfd_set_section_alignment (s, bed->s->log_file_align))
294 return false;
295
296 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
297 flags | SEC_READONLY);
298 if (s == NULL
299 || !bfd_set_section_alignment (s, 1))
300 return false;
301
302 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
303 flags | SEC_READONLY);
304 if (s == NULL
305 || !bfd_set_section_alignment (s, bed->s->log_file_align))
306 return false;
307
308 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
309 flags | SEC_READONLY);
310 if (s == NULL
311 || !bfd_set_section_alignment (s, bed->s->log_file_align))
312 return false;
313 elf_hash_table (info)->dynsym = s;
314
315 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
316 flags | SEC_READONLY);
317 if (s == NULL)
318 return false;
319
320 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
321 if (s == NULL
322 || !bfd_set_section_alignment (s, bed->s->log_file_align))
323 return false;
324
325 /* The special symbol _DYNAMIC is always set to the start of the
326 .dynamic section. We could set _DYNAMIC in a linker script, but we
327 only want to define it if we are, in fact, creating a .dynamic
328 section. We don't want to define it if there is no .dynamic
329 section, since on some ELF platforms the start up code examines it
330 to decide how to initialize the process. */
331 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
332 elf_hash_table (info)->hdynamic = h;
333 if (h == NULL)
334 return false;
335
336 if (info->emit_hash)
337 {
338 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
339 flags | SEC_READONLY);
340 if (s == NULL
341 || !bfd_set_section_alignment (s, bed->s->log_file_align))
342 return false;
343 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
344 }
345
346 if (info->emit_gnu_hash && bed->record_xhash_symbol == NULL)
347 {
348 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
349 flags | SEC_READONLY);
350 if (s == NULL
351 || !bfd_set_section_alignment (s, bed->s->log_file_align))
352 return false;
353 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
354 4 32-bit words followed by variable count of 64-bit words, then
355 variable count of 32-bit words. */
356 if (bed->s->arch_size == 64)
357 elf_section_data (s)->this_hdr.sh_entsize = 0;
358 else
359 elf_section_data (s)->this_hdr.sh_entsize = 4;
360 }
361
362 if (info->enable_dt_relr)
363 {
364 s = bfd_make_section_anyway_with_flags (abfd, ".relr.dyn",
365 (bed->dynamic_sec_flags
366 | SEC_READONLY));
367 if (s == NULL
368 || !bfd_set_section_alignment (s, bed->s->log_file_align))
369 return false;
370 elf_hash_table (info)->srelrdyn = s;
371 }
372
373 /* Let the backend create the rest of the sections. This lets the
374 backend set the right flags. The backend will normally create
375 the .got and .plt sections. */
376 if (bed->elf_backend_create_dynamic_sections == NULL
377 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
378 return false;
379
380 elf_hash_table (info)->dynamic_sections_created = true;
381
382 return true;
383 }
384
385 /* Create dynamic sections when linking against a dynamic object. */
386
387 bool
388 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
389 {
390 flagword flags, pltflags;
391 struct elf_link_hash_entry *h;
392 asection *s;
393 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
394 struct elf_link_hash_table *htab = elf_hash_table (info);
395
396 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
397 .rel[a].bss sections. */
398 flags = bed->dynamic_sec_flags;
399
400 pltflags = flags;
401 if (bed->plt_not_loaded)
402 /* We do not clear SEC_ALLOC here because we still want the OS to
403 allocate space for the section; it's just that there's nothing
404 to read in from the object file. */
405 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
406 else
407 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
408 if (bed->plt_readonly)
409 pltflags |= SEC_READONLY;
410
411 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
412 if (s == NULL
413 || !bfd_set_section_alignment (s, bed->plt_alignment))
414 return false;
415 htab->splt = s;
416
417 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
418 .plt section. */
419 if (bed->want_plt_sym)
420 {
421 h = _bfd_elf_define_linkage_sym (abfd, info, s,
422 "_PROCEDURE_LINKAGE_TABLE_");
423 elf_hash_table (info)->hplt = h;
424 if (h == NULL)
425 return false;
426 }
427
428 s = bfd_make_section_anyway_with_flags (abfd,
429 (bed->rela_plts_and_copies_p
430 ? ".rela.plt" : ".rel.plt"),
431 flags | SEC_READONLY);
432 if (s == NULL
433 || !bfd_set_section_alignment (s, bed->s->log_file_align))
434 return false;
435 htab->srelplt = s;
436
437 if (! _bfd_elf_create_got_section (abfd, info))
438 return false;
439
440 if (bed->want_dynbss)
441 {
442 /* The .dynbss section is a place to put symbols which are defined
443 by dynamic objects, are referenced by regular objects, and are
444 not functions. We must allocate space for them in the process
445 image and use a R_*_COPY reloc to tell the dynamic linker to
446 initialize them at run time. The linker script puts the .dynbss
447 section into the .bss section of the final image. */
448 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
449 SEC_ALLOC | SEC_LINKER_CREATED);
450 if (s == NULL)
451 return false;
452 htab->sdynbss = s;
453
454 if (bed->want_dynrelro)
455 {
456 /* Similarly, but for symbols that were originally in read-only
457 sections. This section doesn't really need to have contents,
458 but make it like other .data.rel.ro sections. */
459 s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
460 flags);
461 if (s == NULL)
462 return false;
463 htab->sdynrelro = s;
464 }
465
466 /* The .rel[a].bss section holds copy relocs. This section is not
467 normally needed. We need to create it here, though, so that the
468 linker will map it to an output section. We can't just create it
469 only if we need it, because we will not know whether we need it
470 until we have seen all the input files, and the first time the
471 main linker code calls BFD after examining all the input files
472 (size_dynamic_sections) the input sections have already been
473 mapped to the output sections. If the section turns out not to
474 be needed, we can discard it later. We will never need this
475 section when generating a shared object, since they do not use
476 copy relocs. */
477 if (bfd_link_executable (info))
478 {
479 s = bfd_make_section_anyway_with_flags (abfd,
480 (bed->rela_plts_and_copies_p
481 ? ".rela.bss" : ".rel.bss"),
482 flags | SEC_READONLY);
483 if (s == NULL
484 || !bfd_set_section_alignment (s, bed->s->log_file_align))
485 return false;
486 htab->srelbss = s;
487
488 if (bed->want_dynrelro)
489 {
490 s = (bfd_make_section_anyway_with_flags
491 (abfd, (bed->rela_plts_and_copies_p
492 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
493 flags | SEC_READONLY));
494 if (s == NULL
495 || !bfd_set_section_alignment (s, bed->s->log_file_align))
496 return false;
497 htab->sreldynrelro = s;
498 }
499 }
500 }
501
502 return true;
503 }
504 \f
505 /* Record a new dynamic symbol. We record the dynamic symbols as we
506 read the input files, since we need to have a list of all of them
507 before we can determine the final sizes of the output sections.
508 Note that we may actually call this function even though we are not
509 going to output any dynamic symbols; in some cases we know that a
510 symbol should be in the dynamic symbol table, but only if there is
511 one. */
512
513 bool
514 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
515 struct elf_link_hash_entry *h)
516 {
517 if (h->dynindx == -1)
518 {
519 struct elf_strtab_hash *dynstr;
520 char *p;
521 const char *name;
522 size_t indx;
523
524 if (h->root.type == bfd_link_hash_defined
525 || h->root.type == bfd_link_hash_defweak)
526 {
527 /* An IR symbol should not be made dynamic. */
528 if (h->root.u.def.section != NULL
529 && h->root.u.def.section->owner != NULL
530 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)
531 return true;
532 }
533
534 /* XXX: The ABI draft says the linker must turn hidden and
535 internal symbols into STB_LOCAL symbols when producing the
536 DSO. However, if ld.so honors st_other in the dynamic table,
537 this would not be necessary. */
538 switch (ELF_ST_VISIBILITY (h->other))
539 {
540 case STV_INTERNAL:
541 case STV_HIDDEN:
542 if (h->root.type != bfd_link_hash_undefined
543 && h->root.type != bfd_link_hash_undefweak)
544 {
545 h->forced_local = 1;
546 if (!elf_hash_table (info)->is_relocatable_executable
547 || ((h->root.type == bfd_link_hash_defined
548 || h->root.type == bfd_link_hash_defweak)
549 && h->root.u.def.section->owner != NULL
550 && h->root.u.def.section->owner->no_export)
551 || (h->root.type == bfd_link_hash_common
552 && h->root.u.c.p->section->owner != NULL
553 && h->root.u.c.p->section->owner->no_export))
554 return true;
555 }
556
557 default:
558 break;
559 }
560
561 h->dynindx = elf_hash_table (info)->dynsymcount;
562 ++elf_hash_table (info)->dynsymcount;
563
564 dynstr = elf_hash_table (info)->dynstr;
565 if (dynstr == NULL)
566 {
567 /* Create a strtab to hold the dynamic symbol names. */
568 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
569 if (dynstr == NULL)
570 return false;
571 }
572
573 /* We don't put any version information in the dynamic string
574 table. */
575 name = h->root.root.string;
576 p = strchr (name, ELF_VER_CHR);
577 if (p != NULL)
578 /* We know that the p points into writable memory. In fact,
579 there are only a few symbols that have read-only names, being
580 those like _GLOBAL_OFFSET_TABLE_ that are created specially
581 by the backends. Most symbols will have names pointing into
582 an ELF string table read from a file, or to objalloc memory. */
583 *p = 0;
584
585 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
586
587 if (p != NULL)
588 *p = ELF_VER_CHR;
589
590 if (indx == (size_t) -1)
591 return false;
592 h->dynstr_index = indx;
593 }
594
595 return true;
596 }
597 \f
598 /* Mark a symbol dynamic. */
599
600 static void
601 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
602 struct elf_link_hash_entry *h,
603 Elf_Internal_Sym *sym)
604 {
605 struct bfd_elf_dynamic_list *d = info->dynamic_list;
606
607 /* It may be called more than once on the same H. */
608 if(h->dynamic || bfd_link_relocatable (info))
609 return;
610
611 if ((info->dynamic_data
612 && (h->type == STT_OBJECT
613 || h->type == STT_COMMON
614 || (sym != NULL
615 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
616 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
617 || (d != NULL
618 && h->non_elf
619 && (*d->match) (&d->head, NULL, h->root.root.string)))
620 {
621 h->dynamic = 1;
622 /* NB: If a symbol is made dynamic by --dynamic-list, it has
623 non-IR reference. */
624 h->root.non_ir_ref_dynamic = 1;
625 }
626 }
627
628 /* Record an assignment to a symbol made by a linker script. We need
629 this in case some dynamic object refers to this symbol. */
630
631 bool
632 bfd_elf_record_link_assignment (bfd *output_bfd,
633 struct bfd_link_info *info,
634 const char *name,
635 bool provide,
636 bool hidden)
637 {
638 struct elf_link_hash_entry *h, *hv;
639 struct elf_link_hash_table *htab;
640 const struct elf_backend_data *bed;
641
642 if (!is_elf_hash_table (info->hash))
643 return true;
644
645 htab = elf_hash_table (info);
646 h = elf_link_hash_lookup (htab, name, !provide, true, false);
647 if (h == NULL)
648 return provide;
649
650 if (h->root.type == bfd_link_hash_warning)
651 h = (struct elf_link_hash_entry *) h->root.u.i.link;
652
653 if (h->versioned == unknown)
654 {
655 /* Set versioned if symbol version is unknown. */
656 char *version = strrchr (name, ELF_VER_CHR);
657 if (version)
658 {
659 if (version > name && version[-1] != ELF_VER_CHR)
660 h->versioned = versioned_hidden;
661 else
662 h->versioned = versioned;
663 }
664 }
665
666 /* Symbols defined in a linker script but not referenced anywhere
667 else will have non_elf set. */
668 if (h->non_elf)
669 {
670 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
671 h->non_elf = 0;
672 }
673
674 switch (h->root.type)
675 {
676 case bfd_link_hash_defined:
677 case bfd_link_hash_defweak:
678 case bfd_link_hash_common:
679 break;
680 case bfd_link_hash_undefweak:
681 case bfd_link_hash_undefined:
682 /* Since we're defining the symbol, don't let it seem to have not
683 been defined. record_dynamic_symbol and size_dynamic_sections
684 may depend on this. */
685 h->root.type = bfd_link_hash_new;
686 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
687 bfd_link_repair_undef_list (&htab->root);
688 break;
689 case bfd_link_hash_new:
690 break;
691 case bfd_link_hash_indirect:
692 /* We had a versioned symbol in a dynamic library. We make the
693 the versioned symbol point to this one. */
694 bed = get_elf_backend_data (output_bfd);
695 hv = h;
696 while (hv->root.type == bfd_link_hash_indirect
697 || hv->root.type == bfd_link_hash_warning)
698 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
699 /* We don't need to update h->root.u since linker will set them
700 later. */
701 h->root.type = bfd_link_hash_undefined;
702 hv->root.type = bfd_link_hash_indirect;
703 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
704 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
705 break;
706 default:
707 BFD_FAIL ();
708 return false;
709 }
710
711 /* If this symbol is being provided by the linker script, and it is
712 currently defined by a dynamic object, but not by a regular
713 object, then mark it as undefined so that the generic linker will
714 force the correct value. */
715 if (provide
716 && h->def_dynamic
717 && !h->def_regular)
718 h->root.type = bfd_link_hash_undefined;
719
720 /* If this symbol is currently defined by a dynamic object, but not
721 by a regular object, then clear out any version information because
722 the symbol will not be associated with the dynamic object any
723 more. */
724 if (h->def_dynamic && !h->def_regular)
725 h->verinfo.verdef = NULL;
726
727 /* Make sure this symbol is not garbage collected. */
728 h->mark = 1;
729
730 h->def_regular = 1;
731
732 if (hidden)
733 {
734 bed = get_elf_backend_data (output_bfd);
735 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
736 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
737 (*bed->elf_backend_hide_symbol) (info, h, true);
738 }
739
740 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
741 and executables. */
742 if (!bfd_link_relocatable (info)
743 && h->dynindx != -1
744 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
745 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
746 h->forced_local = 1;
747
748 if ((h->def_dynamic
749 || h->ref_dynamic
750 || bfd_link_dll (info)
751 || elf_hash_table (info)->is_relocatable_executable)
752 && !h->forced_local
753 && h->dynindx == -1)
754 {
755 if (! bfd_elf_link_record_dynamic_symbol (info, h))
756 return false;
757
758 /* If this is a weak defined symbol, and we know a corresponding
759 real symbol from the same dynamic object, make sure the real
760 symbol is also made into a dynamic symbol. */
761 if (h->is_weakalias)
762 {
763 struct elf_link_hash_entry *def = weakdef (h);
764
765 if (def->dynindx == -1
766 && !bfd_elf_link_record_dynamic_symbol (info, def))
767 return false;
768 }
769 }
770
771 return true;
772 }
773
774 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
775 success, and 2 on a failure caused by attempting to record a symbol
776 in a discarded section, eg. a discarded link-once section symbol. */
777
778 int
779 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
780 bfd *input_bfd,
781 long input_indx)
782 {
783 size_t amt;
784 struct elf_link_local_dynamic_entry *entry;
785 struct elf_link_hash_table *eht;
786 struct elf_strtab_hash *dynstr;
787 size_t dynstr_index;
788 char *name;
789 Elf_External_Sym_Shndx eshndx;
790 char esym[sizeof (Elf64_External_Sym)];
791
792 if (! is_elf_hash_table (info->hash))
793 return 0;
794
795 /* See if the entry exists already. */
796 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
797 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
798 return 1;
799
800 amt = sizeof (*entry);
801 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
802 if (entry == NULL)
803 return 0;
804
805 /* Go find the symbol, so that we can find it's name. */
806 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
807 1, input_indx, &entry->isym, esym, &eshndx))
808 {
809 bfd_release (input_bfd, entry);
810 return 0;
811 }
812
813 if (entry->isym.st_shndx != SHN_UNDEF
814 && entry->isym.st_shndx < SHN_LORESERVE)
815 {
816 asection *s;
817
818 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
819 if (s == NULL || bfd_is_abs_section (s->output_section))
820 {
821 /* We can still bfd_release here as nothing has done another
822 bfd_alloc. We can't do this later in this function. */
823 bfd_release (input_bfd, entry);
824 return 2;
825 }
826 }
827
828 name = (bfd_elf_string_from_elf_section
829 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
830 entry->isym.st_name));
831
832 dynstr = elf_hash_table (info)->dynstr;
833 if (dynstr == NULL)
834 {
835 /* Create a strtab to hold the dynamic symbol names. */
836 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
837 if (dynstr == NULL)
838 return 0;
839 }
840
841 dynstr_index = _bfd_elf_strtab_add (dynstr, name, false);
842 if (dynstr_index == (size_t) -1)
843 return 0;
844 entry->isym.st_name = dynstr_index;
845
846 eht = elf_hash_table (info);
847
848 entry->next = eht->dynlocal;
849 eht->dynlocal = entry;
850 entry->input_bfd = input_bfd;
851 entry->input_indx = input_indx;
852 eht->dynsymcount++;
853
854 /* Whatever binding the symbol had before, it's now local. */
855 entry->isym.st_info
856 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
857
858 /* The dynindx will be set at the end of size_dynamic_sections. */
859
860 return 1;
861 }
862
863 /* Return the dynindex of a local dynamic symbol. */
864
865 long
866 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
867 bfd *input_bfd,
868 long input_indx)
869 {
870 struct elf_link_local_dynamic_entry *e;
871
872 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
873 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
874 return e->dynindx;
875 return -1;
876 }
877
878 /* This function is used to renumber the dynamic symbols, if some of
879 them are removed because they are marked as local. This is called
880 via elf_link_hash_traverse. */
881
882 static bool
883 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
884 void *data)
885 {
886 size_t *count = (size_t *) data;
887
888 if (h->forced_local)
889 return true;
890
891 if (h->dynindx != -1)
892 h->dynindx = ++(*count);
893
894 return true;
895 }
896
897
898 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
899 STB_LOCAL binding. */
900
901 static bool
902 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
903 void *data)
904 {
905 size_t *count = (size_t *) data;
906
907 if (!h->forced_local)
908 return true;
909
910 if (h->dynindx != -1)
911 h->dynindx = ++(*count);
912
913 return true;
914 }
915
916 /* Return true if the dynamic symbol for a given section should be
917 omitted when creating a shared library. */
918 bool
919 _bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED,
920 struct bfd_link_info *info,
921 asection *p)
922 {
923 struct elf_link_hash_table *htab;
924 asection *ip;
925
926 switch (elf_section_data (p)->this_hdr.sh_type)
927 {
928 case SHT_PROGBITS:
929 case SHT_NOBITS:
930 /* If sh_type is yet undecided, assume it could be
931 SHT_PROGBITS/SHT_NOBITS. */
932 case SHT_NULL:
933 htab = elf_hash_table (info);
934 if (htab->text_index_section != NULL)
935 return p != htab->text_index_section && p != htab->data_index_section;
936
937 return (htab->dynobj != NULL
938 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
939 && ip->output_section == p);
940
941 /* There shouldn't be section relative relocations
942 against any other section. */
943 default:
944 return true;
945 }
946 }
947
948 bool
949 _bfd_elf_omit_section_dynsym_all
950 (bfd *output_bfd ATTRIBUTE_UNUSED,
951 struct bfd_link_info *info ATTRIBUTE_UNUSED,
952 asection *p ATTRIBUTE_UNUSED)
953 {
954 return true;
955 }
956
957 /* Assign dynsym indices. In a shared library we generate a section
958 symbol for each output section, which come first. Next come symbols
959 which have been forced to local binding. Then all of the back-end
960 allocated local dynamic syms, followed by the rest of the global
961 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
962 (This prevents the early call before elf_backend_init_index_section
963 and strip_excluded_output_sections setting dynindx for sections
964 that are stripped.) */
965
966 static unsigned long
967 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
968 struct bfd_link_info *info,
969 unsigned long *section_sym_count)
970 {
971 unsigned long dynsymcount = 0;
972 bool do_sec = section_sym_count != NULL;
973
974 if (bfd_link_pic (info)
975 || elf_hash_table (info)->is_relocatable_executable)
976 {
977 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
978 asection *p;
979 for (p = output_bfd->sections; p ; p = p->next)
980 if ((p->flags & SEC_EXCLUDE) == 0
981 && (p->flags & SEC_ALLOC) != 0
982 && elf_hash_table (info)->dynamic_relocs
983 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
984 {
985 ++dynsymcount;
986 if (do_sec)
987 elf_section_data (p)->dynindx = dynsymcount;
988 }
989 else if (do_sec)
990 elf_section_data (p)->dynindx = 0;
991 }
992 if (do_sec)
993 *section_sym_count = dynsymcount;
994
995 elf_link_hash_traverse (elf_hash_table (info),
996 elf_link_renumber_local_hash_table_dynsyms,
997 &dynsymcount);
998
999 if (elf_hash_table (info)->dynlocal)
1000 {
1001 struct elf_link_local_dynamic_entry *p;
1002 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
1003 p->dynindx = ++dynsymcount;
1004 }
1005 elf_hash_table (info)->local_dynsymcount = dynsymcount;
1006
1007 elf_link_hash_traverse (elf_hash_table (info),
1008 elf_link_renumber_hash_table_dynsyms,
1009 &dynsymcount);
1010
1011 /* There is an unused NULL entry at the head of the table which we
1012 must account for in our count even if the table is empty since it
1013 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
1014 .dynamic section. */
1015 dynsymcount++;
1016
1017 elf_hash_table (info)->dynsymcount = dynsymcount;
1018 return dynsymcount;
1019 }
1020
1021 /* Merge st_other field. */
1022
1023 static void
1024 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
1025 unsigned int st_other, asection *sec,
1026 bool definition, bool dynamic)
1027 {
1028 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1029
1030 /* If st_other has a processor-specific meaning, specific
1031 code might be needed here. */
1032 if (bed->elf_backend_merge_symbol_attribute)
1033 (*bed->elf_backend_merge_symbol_attribute) (h, st_other, definition,
1034 dynamic);
1035
1036 if (!dynamic)
1037 {
1038 unsigned symvis = ELF_ST_VISIBILITY (st_other);
1039 unsigned hvis = ELF_ST_VISIBILITY (h->other);
1040
1041 /* Keep the most constraining visibility. Leave the remainder
1042 of the st_other field to elf_backend_merge_symbol_attribute. */
1043 if (symvis - 1 < hvis - 1)
1044 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
1045 }
1046 else if (definition
1047 && ELF_ST_VISIBILITY (st_other) != STV_DEFAULT
1048 && (sec->flags & SEC_READONLY) == 0)
1049 h->protected_def = 1;
1050 }
1051
1052 /* This function is called when we want to merge a new symbol with an
1053 existing symbol. It handles the various cases which arise when we
1054 find a definition in a dynamic object, or when there is already a
1055 definition in a dynamic object. The new symbol is described by
1056 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1057 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1058 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1059 of an old common symbol. We set OVERRIDE if the old symbol is
1060 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1061 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1062 to change. By OK to change, we mean that we shouldn't warn if the
1063 type or size does change. */
1064
1065 static bool
1066 _bfd_elf_merge_symbol (bfd *abfd,
1067 struct bfd_link_info *info,
1068 const char *name,
1069 Elf_Internal_Sym *sym,
1070 asection **psec,
1071 bfd_vma *pvalue,
1072 struct elf_link_hash_entry **sym_hash,
1073 bfd **poldbfd,
1074 bool *pold_weak,
1075 unsigned int *pold_alignment,
1076 bool *skip,
1077 bfd **override,
1078 bool *type_change_ok,
1079 bool *size_change_ok,
1080 bool *matched)
1081 {
1082 asection *sec, *oldsec;
1083 struct elf_link_hash_entry *h;
1084 struct elf_link_hash_entry *hi;
1085 struct elf_link_hash_entry *flip;
1086 int bind;
1087 bfd *oldbfd;
1088 bool newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
1089 bool newweak, oldweak, newfunc, oldfunc;
1090 const struct elf_backend_data *bed;
1091 char *new_version;
1092 bool default_sym = *matched;
1093 struct elf_link_hash_table *htab;
1094
1095 *skip = false;
1096 *override = NULL;
1097
1098 sec = *psec;
1099 bind = ELF_ST_BIND (sym->st_info);
1100
1101 if (! bfd_is_und_section (sec))
1102 h = elf_link_hash_lookup (elf_hash_table (info), name, true, false, false);
1103 else
1104 h = ((struct elf_link_hash_entry *)
1105 bfd_wrapped_link_hash_lookup (abfd, info, name, true, false, false));
1106 if (h == NULL)
1107 return false;
1108 *sym_hash = h;
1109
1110 bed = get_elf_backend_data (abfd);
1111
1112 /* NEW_VERSION is the symbol version of the new symbol. */
1113 if (h->versioned != unversioned)
1114 {
1115 /* Symbol version is unknown or versioned. */
1116 new_version = strrchr (name, ELF_VER_CHR);
1117 if (new_version)
1118 {
1119 if (h->versioned == unknown)
1120 {
1121 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1122 h->versioned = versioned_hidden;
1123 else
1124 h->versioned = versioned;
1125 }
1126 new_version += 1;
1127 if (new_version[0] == '\0')
1128 new_version = NULL;
1129 }
1130 else
1131 h->versioned = unversioned;
1132 }
1133 else
1134 new_version = NULL;
1135
1136 /* For merging, we only care about real symbols. But we need to make
1137 sure that indirect symbol dynamic flags are updated. */
1138 hi = h;
1139 while (h->root.type == bfd_link_hash_indirect
1140 || h->root.type == bfd_link_hash_warning)
1141 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1142
1143 if (!*matched)
1144 {
1145 if (hi == h || h->root.type == bfd_link_hash_new)
1146 *matched = true;
1147 else
1148 {
1149 /* OLD_HIDDEN is true if the existing symbol is only visible
1150 to the symbol with the same symbol version. NEW_HIDDEN is
1151 true if the new symbol is only visible to the symbol with
1152 the same symbol version. */
1153 bool old_hidden = h->versioned == versioned_hidden;
1154 bool new_hidden = hi->versioned == versioned_hidden;
1155 if (!old_hidden && !new_hidden)
1156 /* The new symbol matches the existing symbol if both
1157 aren't hidden. */
1158 *matched = true;
1159 else
1160 {
1161 /* OLD_VERSION is the symbol version of the existing
1162 symbol. */
1163 char *old_version;
1164
1165 if (h->versioned >= versioned)
1166 old_version = strrchr (h->root.root.string,
1167 ELF_VER_CHR) + 1;
1168 else
1169 old_version = NULL;
1170
1171 /* The new symbol matches the existing symbol if they
1172 have the same symbol version. */
1173 *matched = (old_version == new_version
1174 || (old_version != NULL
1175 && new_version != NULL
1176 && strcmp (old_version, new_version) == 0));
1177 }
1178 }
1179 }
1180
1181 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1182 existing symbol. */
1183
1184 oldbfd = NULL;
1185 oldsec = NULL;
1186 switch (h->root.type)
1187 {
1188 default:
1189 break;
1190
1191 case bfd_link_hash_undefined:
1192 case bfd_link_hash_undefweak:
1193 oldbfd = h->root.u.undef.abfd;
1194 break;
1195
1196 case bfd_link_hash_defined:
1197 case bfd_link_hash_defweak:
1198 oldbfd = h->root.u.def.section->owner;
1199 oldsec = h->root.u.def.section;
1200 break;
1201
1202 case bfd_link_hash_common:
1203 oldbfd = h->root.u.c.p->section->owner;
1204 oldsec = h->root.u.c.p->section;
1205 if (pold_alignment)
1206 *pold_alignment = h->root.u.c.p->alignment_power;
1207 break;
1208 }
1209 if (poldbfd && *poldbfd == NULL)
1210 *poldbfd = oldbfd;
1211
1212 /* Differentiate strong and weak symbols. */
1213 newweak = bind == STB_WEAK;
1214 oldweak = (h->root.type == bfd_link_hash_defweak
1215 || h->root.type == bfd_link_hash_undefweak);
1216 if (pold_weak)
1217 *pold_weak = oldweak;
1218
1219 /* We have to check it for every instance since the first few may be
1220 references and not all compilers emit symbol type for undefined
1221 symbols. */
1222 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1223
1224 htab = elf_hash_table (info);
1225
1226 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1227 respectively, is from a dynamic object. */
1228
1229 newdyn = (abfd->flags & DYNAMIC) != 0;
1230
1231 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1232 syms and defined syms in dynamic libraries respectively.
1233 ref_dynamic on the other hand can be set for a symbol defined in
1234 a dynamic library, and def_dynamic may not be set; When the
1235 definition in a dynamic lib is overridden by a definition in the
1236 executable use of the symbol in the dynamic lib becomes a
1237 reference to the executable symbol. */
1238 if (newdyn)
1239 {
1240 if (bfd_is_und_section (sec))
1241 {
1242 if (bind != STB_WEAK)
1243 {
1244 h->ref_dynamic_nonweak = 1;
1245 hi->ref_dynamic_nonweak = 1;
1246 }
1247 }
1248 else
1249 {
1250 /* Update the existing symbol only if they match. */
1251 if (*matched)
1252 h->dynamic_def = 1;
1253 hi->dynamic_def = 1;
1254 }
1255 }
1256
1257 /* If we just created the symbol, mark it as being an ELF symbol.
1258 Other than that, there is nothing to do--there is no merge issue
1259 with a newly defined symbol--so we just return. */
1260
1261 if (h->root.type == bfd_link_hash_new)
1262 {
1263 h->non_elf = 0;
1264 return true;
1265 }
1266
1267 /* In cases involving weak versioned symbols, we may wind up trying
1268 to merge a symbol with itself. Catch that here, to avoid the
1269 confusion that results if we try to override a symbol with
1270 itself. The additional tests catch cases like
1271 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1272 dynamic object, which we do want to handle here. */
1273 if (abfd == oldbfd
1274 && (newweak || oldweak)
1275 && ((abfd->flags & DYNAMIC) == 0
1276 || !h->def_regular))
1277 return true;
1278
1279 olddyn = false;
1280 if (oldbfd != NULL)
1281 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1282 else if (oldsec != NULL)
1283 {
1284 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1285 indices used by MIPS ELF. */
1286 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1287 }
1288
1289 /* Set non_ir_ref_dynamic only when not handling DT_NEEDED entries. */
1290 if (!htab->handling_dt_needed
1291 && oldbfd != NULL
1292 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN))
1293 {
1294 if (newdyn != olddyn)
1295 {
1296 /* Handle a case where plugin_notice won't be called and thus
1297 won't set the non_ir_ref flags on the first pass over
1298 symbols. */
1299 h->root.non_ir_ref_dynamic = true;
1300 hi->root.non_ir_ref_dynamic = true;
1301 }
1302 else if ((oldbfd->flags & BFD_PLUGIN) != 0
1303 && hi->root.type == bfd_link_hash_indirect)
1304 {
1305 /* Change indirect symbol from IR to undefined. */
1306 hi->root.type = bfd_link_hash_undefined;
1307 hi->root.u.undef.abfd = oldbfd;
1308 }
1309 }
1310
1311 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1312 respectively, appear to be a definition rather than reference. */
1313
1314 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1315
1316 olddef = (h->root.type != bfd_link_hash_undefined
1317 && h->root.type != bfd_link_hash_undefweak
1318 && h->root.type != bfd_link_hash_common);
1319
1320 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1321 respectively, appear to be a function. */
1322
1323 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1324 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1325
1326 oldfunc = (h->type != STT_NOTYPE
1327 && bed->is_function_type (h->type));
1328
1329 if (!(newfunc && oldfunc)
1330 && ELF_ST_TYPE (sym->st_info) != h->type
1331 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1332 && h->type != STT_NOTYPE
1333 && (newdef || bfd_is_com_section (sec))
1334 && (olddef || h->root.type == bfd_link_hash_common))
1335 {
1336 /* If creating a default indirect symbol ("foo" or "foo@") from
1337 a dynamic versioned definition ("foo@@") skip doing so if
1338 there is an existing regular definition with a different
1339 type. We don't want, for example, a "time" variable in the
1340 executable overriding a "time" function in a shared library. */
1341 if (newdyn
1342 && !olddyn)
1343 {
1344 *skip = true;
1345 return true;
1346 }
1347
1348 /* When adding a symbol from a regular object file after we have
1349 created indirect symbols, undo the indirection and any
1350 dynamic state. */
1351 if (hi != h
1352 && !newdyn
1353 && olddyn)
1354 {
1355 h = hi;
1356 (*bed->elf_backend_hide_symbol) (info, h, true);
1357 h->forced_local = 0;
1358 h->ref_dynamic = 0;
1359 h->def_dynamic = 0;
1360 h->dynamic_def = 0;
1361 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1362 {
1363 h->root.type = bfd_link_hash_undefined;
1364 h->root.u.undef.abfd = abfd;
1365 }
1366 else
1367 {
1368 h->root.type = bfd_link_hash_new;
1369 h->root.u.undef.abfd = NULL;
1370 }
1371 return true;
1372 }
1373 }
1374
1375 /* Check TLS symbols. We don't check undefined symbols introduced
1376 by "ld -u" which have no type (and oldbfd NULL), and we don't
1377 check symbols from plugins because they also have no type. */
1378 if (oldbfd != NULL
1379 && (oldbfd->flags & BFD_PLUGIN) == 0
1380 && (abfd->flags & BFD_PLUGIN) == 0
1381 && ELF_ST_TYPE (sym->st_info) != h->type
1382 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1383 {
1384 bfd *ntbfd, *tbfd;
1385 bool ntdef, tdef;
1386 asection *ntsec, *tsec;
1387
1388 if (h->type == STT_TLS)
1389 {
1390 ntbfd = abfd;
1391 ntsec = sec;
1392 ntdef = newdef;
1393 tbfd = oldbfd;
1394 tsec = oldsec;
1395 tdef = olddef;
1396 }
1397 else
1398 {
1399 ntbfd = oldbfd;
1400 ntsec = oldsec;
1401 ntdef = olddef;
1402 tbfd = abfd;
1403 tsec = sec;
1404 tdef = newdef;
1405 }
1406
1407 if (tdef && ntdef)
1408 _bfd_error_handler
1409 /* xgettext:c-format */
1410 (_("%s: TLS definition in %pB section %pA "
1411 "mismatches non-TLS definition in %pB section %pA"),
1412 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
1413 else if (!tdef && !ntdef)
1414 _bfd_error_handler
1415 /* xgettext:c-format */
1416 (_("%s: TLS reference in %pB "
1417 "mismatches non-TLS reference in %pB"),
1418 h->root.root.string, tbfd, ntbfd);
1419 else if (tdef)
1420 _bfd_error_handler
1421 /* xgettext:c-format */
1422 (_("%s: TLS definition in %pB section %pA "
1423 "mismatches non-TLS reference in %pB"),
1424 h->root.root.string, tbfd, tsec, ntbfd);
1425 else
1426 _bfd_error_handler
1427 /* xgettext:c-format */
1428 (_("%s: TLS reference in %pB "
1429 "mismatches non-TLS definition in %pB section %pA"),
1430 h->root.root.string, tbfd, ntbfd, ntsec);
1431
1432 bfd_set_error (bfd_error_bad_value);
1433 return false;
1434 }
1435
1436 /* If the old symbol has non-default visibility, we ignore the new
1437 definition from a dynamic object. */
1438 if (newdyn
1439 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1440 && !bfd_is_und_section (sec))
1441 {
1442 *skip = true;
1443 /* Make sure this symbol is dynamic. */
1444 h->ref_dynamic = 1;
1445 hi->ref_dynamic = 1;
1446 /* A protected symbol has external availability. Make sure it is
1447 recorded as dynamic.
1448
1449 FIXME: Should we check type and size for protected symbol? */
1450 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1451 return bfd_elf_link_record_dynamic_symbol (info, h);
1452 else
1453 return true;
1454 }
1455 else if (!newdyn
1456 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1457 && h->def_dynamic)
1458 {
1459 /* If the new symbol with non-default visibility comes from a
1460 relocatable file and the old definition comes from a dynamic
1461 object, we remove the old definition. */
1462 if (hi->root.type == bfd_link_hash_indirect)
1463 {
1464 /* Handle the case where the old dynamic definition is
1465 default versioned. We need to copy the symbol info from
1466 the symbol with default version to the normal one if it
1467 was referenced before. */
1468 if (h->ref_regular)
1469 {
1470 hi->root.type = h->root.type;
1471 h->root.type = bfd_link_hash_indirect;
1472 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1473
1474 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1475 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1476 {
1477 /* If the new symbol is hidden or internal, completely undo
1478 any dynamic link state. */
1479 (*bed->elf_backend_hide_symbol) (info, h, true);
1480 h->forced_local = 0;
1481 h->ref_dynamic = 0;
1482 }
1483 else
1484 h->ref_dynamic = 1;
1485
1486 h->def_dynamic = 0;
1487 /* FIXME: Should we check type and size for protected symbol? */
1488 h->size = 0;
1489 h->type = 0;
1490
1491 h = hi;
1492 }
1493 else
1494 h = hi;
1495 }
1496
1497 /* If the old symbol was undefined before, then it will still be
1498 on the undefs list. If the new symbol is undefined or
1499 common, we can't make it bfd_link_hash_new here, because new
1500 undefined or common symbols will be added to the undefs list
1501 by _bfd_generic_link_add_one_symbol. Symbols may not be
1502 added twice to the undefs list. Also, if the new symbol is
1503 undefweak then we don't want to lose the strong undef. */
1504 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1505 {
1506 h->root.type = bfd_link_hash_undefined;
1507 h->root.u.undef.abfd = abfd;
1508 }
1509 else
1510 {
1511 h->root.type = bfd_link_hash_new;
1512 h->root.u.undef.abfd = NULL;
1513 }
1514
1515 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1516 {
1517 /* If the new symbol is hidden or internal, completely undo
1518 any dynamic link state. */
1519 (*bed->elf_backend_hide_symbol) (info, h, true);
1520 h->forced_local = 0;
1521 h->ref_dynamic = 0;
1522 }
1523 else
1524 h->ref_dynamic = 1;
1525 h->def_dynamic = 0;
1526 /* FIXME: Should we check type and size for protected symbol? */
1527 h->size = 0;
1528 h->type = 0;
1529 return true;
1530 }
1531
1532 /* If a new weak symbol definition comes from a regular file and the
1533 old symbol comes from a dynamic library, we treat the new one as
1534 strong. Similarly, an old weak symbol definition from a regular
1535 file is treated as strong when the new symbol comes from a dynamic
1536 library. Further, an old weak symbol from a dynamic library is
1537 treated as strong if the new symbol is from a dynamic library.
1538 This reflects the way glibc's ld.so works.
1539
1540 Also allow a weak symbol to override a linker script symbol
1541 defined by an early pass over the script. This is done so the
1542 linker knows the symbol is defined in an object file, for the
1543 DEFINED script function.
1544
1545 Do this before setting *type_change_ok or *size_change_ok so that
1546 we warn properly when dynamic library symbols are overridden. */
1547
1548 if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
1549 newweak = false;
1550 if (olddef && newdyn)
1551 oldweak = false;
1552
1553 /* Allow changes between different types of function symbol. */
1554 if (newfunc && oldfunc)
1555 *type_change_ok = true;
1556
1557 /* It's OK to change the type if either the existing symbol or the
1558 new symbol is weak. A type change is also OK if the old symbol
1559 is undefined and the new symbol is defined. */
1560
1561 if (oldweak
1562 || newweak
1563 || (newdef
1564 && h->root.type == bfd_link_hash_undefined))
1565 *type_change_ok = true;
1566
1567 /* It's OK to change the size if either the existing symbol or the
1568 new symbol is weak, or if the old symbol is undefined. */
1569
1570 if (*type_change_ok
1571 || h->root.type == bfd_link_hash_undefined)
1572 *size_change_ok = true;
1573
1574 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1575 symbol, respectively, appears to be a common symbol in a dynamic
1576 object. If a symbol appears in an uninitialized section, and is
1577 not weak, and is not a function, then it may be a common symbol
1578 which was resolved when the dynamic object was created. We want
1579 to treat such symbols specially, because they raise special
1580 considerations when setting the symbol size: if the symbol
1581 appears as a common symbol in a regular object, and the size in
1582 the regular object is larger, we must make sure that we use the
1583 larger size. This problematic case can always be avoided in C,
1584 but it must be handled correctly when using Fortran shared
1585 libraries.
1586
1587 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1588 likewise for OLDDYNCOMMON and OLDDEF.
1589
1590 Note that this test is just a heuristic, and that it is quite
1591 possible to have an uninitialized symbol in a shared object which
1592 is really a definition, rather than a common symbol. This could
1593 lead to some minor confusion when the symbol really is a common
1594 symbol in some regular object. However, I think it will be
1595 harmless. */
1596
1597 if (newdyn
1598 && newdef
1599 && !newweak
1600 && (sec->flags & SEC_ALLOC) != 0
1601 && (sec->flags & SEC_LOAD) == 0
1602 && sym->st_size > 0
1603 && !newfunc)
1604 newdyncommon = true;
1605 else
1606 newdyncommon = false;
1607
1608 if (olddyn
1609 && olddef
1610 && h->root.type == bfd_link_hash_defined
1611 && h->def_dynamic
1612 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1613 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1614 && h->size > 0
1615 && !oldfunc)
1616 olddyncommon = true;
1617 else
1618 olddyncommon = false;
1619
1620 /* We now know everything about the old and new symbols. We ask the
1621 backend to check if we can merge them. */
1622 if (bed->merge_symbol != NULL)
1623 {
1624 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1625 return false;
1626 sec = *psec;
1627 }
1628
1629 /* There are multiple definitions of a normal symbol. Skip the
1630 default symbol as well as definition from an IR object. */
1631 if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
1632 && !default_sym && h->def_regular
1633 && !(oldbfd != NULL
1634 && (oldbfd->flags & BFD_PLUGIN) != 0
1635 && (abfd->flags & BFD_PLUGIN) == 0))
1636 {
1637 /* Handle a multiple definition. */
1638 (*info->callbacks->multiple_definition) (info, &h->root,
1639 abfd, sec, *pvalue);
1640 *skip = true;
1641 return true;
1642 }
1643
1644 /* If both the old and the new symbols look like common symbols in a
1645 dynamic object, set the size of the symbol to the larger of the
1646 two. */
1647
1648 if (olddyncommon
1649 && newdyncommon
1650 && sym->st_size != h->size)
1651 {
1652 /* Since we think we have two common symbols, issue a multiple
1653 common warning if desired. Note that we only warn if the
1654 size is different. If the size is the same, we simply let
1655 the old symbol override the new one as normally happens with
1656 symbols defined in dynamic objects. */
1657
1658 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1659 bfd_link_hash_common, sym->st_size);
1660 if (sym->st_size > h->size)
1661 h->size = sym->st_size;
1662
1663 *size_change_ok = true;
1664 }
1665
1666 /* If we are looking at a dynamic object, and we have found a
1667 definition, we need to see if the symbol was already defined by
1668 some other object. If so, we want to use the existing
1669 definition, and we do not want to report a multiple symbol
1670 definition error; we do this by clobbering *PSEC to be
1671 bfd_und_section_ptr.
1672
1673 We treat a common symbol as a definition if the symbol in the
1674 shared library is a function, since common symbols always
1675 represent variables; this can cause confusion in principle, but
1676 any such confusion would seem to indicate an erroneous program or
1677 shared library. We also permit a common symbol in a regular
1678 object to override a weak symbol in a shared object. */
1679
1680 if (newdyn
1681 && newdef
1682 && (olddef
1683 || (h->root.type == bfd_link_hash_common
1684 && (newweak || newfunc))))
1685 {
1686 *override = abfd;
1687 newdef = false;
1688 newdyncommon = false;
1689
1690 *psec = sec = bfd_und_section_ptr;
1691 *size_change_ok = true;
1692
1693 /* If we get here when the old symbol is a common symbol, then
1694 we are explicitly letting it override a weak symbol or
1695 function in a dynamic object, and we don't want to warn about
1696 a type change. If the old symbol is a defined symbol, a type
1697 change warning may still be appropriate. */
1698
1699 if (h->root.type == bfd_link_hash_common)
1700 *type_change_ok = true;
1701 }
1702
1703 /* Handle the special case of an old common symbol merging with a
1704 new symbol which looks like a common symbol in a shared object.
1705 We change *PSEC and *PVALUE to make the new symbol look like a
1706 common symbol, and let _bfd_generic_link_add_one_symbol do the
1707 right thing. */
1708
1709 if (newdyncommon
1710 && h->root.type == bfd_link_hash_common)
1711 {
1712 *override = oldbfd;
1713 newdef = false;
1714 newdyncommon = false;
1715 *pvalue = sym->st_size;
1716 *psec = sec = bed->common_section (oldsec);
1717 *size_change_ok = true;
1718 }
1719
1720 /* Skip weak definitions of symbols that are already defined. */
1721 if (newdef && olddef && newweak)
1722 {
1723 /* Don't skip new non-IR weak syms. */
1724 if (!(oldbfd != NULL
1725 && (oldbfd->flags & BFD_PLUGIN) != 0
1726 && (abfd->flags & BFD_PLUGIN) == 0))
1727 {
1728 newdef = false;
1729 *skip = true;
1730 }
1731
1732 /* Merge st_other. If the symbol already has a dynamic index,
1733 but visibility says it should not be visible, turn it into a
1734 local symbol. */
1735 elf_merge_st_other (abfd, h, sym->st_other, sec, newdef, newdyn);
1736 if (h->dynindx != -1)
1737 switch (ELF_ST_VISIBILITY (h->other))
1738 {
1739 case STV_INTERNAL:
1740 case STV_HIDDEN:
1741 (*bed->elf_backend_hide_symbol) (info, h, true);
1742 break;
1743 }
1744 }
1745
1746 /* If the old symbol is from a dynamic object, and the new symbol is
1747 a definition which is not from a dynamic object, then the new
1748 symbol overrides the old symbol. Symbols from regular files
1749 always take precedence over symbols from dynamic objects, even if
1750 they are defined after the dynamic object in the link.
1751
1752 As above, we again permit a common symbol in a regular object to
1753 override a definition in a shared object if the shared object
1754 symbol is a function or is weak. */
1755
1756 flip = NULL;
1757 if (!newdyn
1758 && (newdef
1759 || (bfd_is_com_section (sec)
1760 && (oldweak || oldfunc)))
1761 && olddyn
1762 && olddef
1763 && h->def_dynamic)
1764 {
1765 /* Change the hash table entry to undefined, and let
1766 _bfd_generic_link_add_one_symbol do the right thing with the
1767 new definition. */
1768
1769 h->root.type = bfd_link_hash_undefined;
1770 h->root.u.undef.abfd = h->root.u.def.section->owner;
1771 *size_change_ok = true;
1772
1773 olddef = false;
1774 olddyncommon = false;
1775
1776 /* We again permit a type change when a common symbol may be
1777 overriding a function. */
1778
1779 if (bfd_is_com_section (sec))
1780 {
1781 if (oldfunc)
1782 {
1783 /* If a common symbol overrides a function, make sure
1784 that it isn't defined dynamically nor has type
1785 function. */
1786 h->def_dynamic = 0;
1787 h->type = STT_NOTYPE;
1788 }
1789 *type_change_ok = true;
1790 }
1791
1792 if (hi->root.type == bfd_link_hash_indirect)
1793 flip = hi;
1794 else
1795 /* This union may have been set to be non-NULL when this symbol
1796 was seen in a dynamic object. We must force the union to be
1797 NULL, so that it is correct for a regular symbol. */
1798 h->verinfo.vertree = NULL;
1799 }
1800
1801 /* Handle the special case of a new common symbol merging with an
1802 old symbol that looks like it might be a common symbol defined in
1803 a shared object. Note that we have already handled the case in
1804 which a new common symbol should simply override the definition
1805 in the shared library. */
1806
1807 if (! newdyn
1808 && bfd_is_com_section (sec)
1809 && olddyncommon)
1810 {
1811 /* It would be best if we could set the hash table entry to a
1812 common symbol, but we don't know what to use for the section
1813 or the alignment. */
1814 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1815 bfd_link_hash_common, sym->st_size);
1816
1817 /* If the presumed common symbol in the dynamic object is
1818 larger, pretend that the new symbol has its size. */
1819
1820 if (h->size > *pvalue)
1821 *pvalue = h->size;
1822
1823 /* We need to remember the alignment required by the symbol
1824 in the dynamic object. */
1825 BFD_ASSERT (pold_alignment);
1826 *pold_alignment = h->root.u.def.section->alignment_power;
1827
1828 olddef = false;
1829 olddyncommon = false;
1830
1831 h->root.type = bfd_link_hash_undefined;
1832 h->root.u.undef.abfd = h->root.u.def.section->owner;
1833
1834 *size_change_ok = true;
1835 *type_change_ok = true;
1836
1837 if (hi->root.type == bfd_link_hash_indirect)
1838 flip = hi;
1839 else
1840 h->verinfo.vertree = NULL;
1841 }
1842
1843 if (flip != NULL)
1844 {
1845 /* Handle the case where we had a versioned symbol in a dynamic
1846 library and now find a definition in a normal object. In this
1847 case, we make the versioned symbol point to the normal one. */
1848 flip->root.type = h->root.type;
1849 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1850 h->root.type = bfd_link_hash_indirect;
1851 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1852 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1853 if (h->def_dynamic)
1854 {
1855 h->def_dynamic = 0;
1856 flip->ref_dynamic = 1;
1857 }
1858 }
1859
1860 return true;
1861 }
1862
1863 /* This function is called to create an indirect symbol from the
1864 default for the symbol with the default version if needed. The
1865 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1866 set DYNSYM if the new indirect symbol is dynamic. */
1867
1868 static bool
1869 _bfd_elf_add_default_symbol (bfd *abfd,
1870 struct bfd_link_info *info,
1871 struct elf_link_hash_entry *h,
1872 const char *name,
1873 Elf_Internal_Sym *sym,
1874 asection *sec,
1875 bfd_vma value,
1876 bfd **poldbfd,
1877 bool *dynsym)
1878 {
1879 bool type_change_ok;
1880 bool size_change_ok;
1881 bool skip;
1882 char *shortname;
1883 struct elf_link_hash_entry *hi;
1884 struct bfd_link_hash_entry *bh;
1885 const struct elf_backend_data *bed;
1886 bool collect;
1887 bool dynamic;
1888 bfd *override;
1889 char *p;
1890 size_t len, shortlen;
1891 asection *tmp_sec;
1892 bool matched;
1893
1894 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1895 return true;
1896
1897 /* If this symbol has a version, and it is the default version, we
1898 create an indirect symbol from the default name to the fully
1899 decorated name. This will cause external references which do not
1900 specify a version to be bound to this version of the symbol. */
1901 p = strchr (name, ELF_VER_CHR);
1902 if (h->versioned == unknown)
1903 {
1904 if (p == NULL)
1905 {
1906 h->versioned = unversioned;
1907 return true;
1908 }
1909 else
1910 {
1911 if (p[1] != ELF_VER_CHR)
1912 {
1913 h->versioned = versioned_hidden;
1914 return true;
1915 }
1916 else
1917 h->versioned = versioned;
1918 }
1919 }
1920 else
1921 {
1922 /* PR ld/19073: We may see an unversioned definition after the
1923 default version. */
1924 if (p == NULL)
1925 return true;
1926 }
1927
1928 bed = get_elf_backend_data (abfd);
1929 collect = bed->collect;
1930 dynamic = (abfd->flags & DYNAMIC) != 0;
1931
1932 shortlen = p - name;
1933 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1934 if (shortname == NULL)
1935 return false;
1936 memcpy (shortname, name, shortlen);
1937 shortname[shortlen] = '\0';
1938
1939 /* We are going to create a new symbol. Merge it with any existing
1940 symbol with this name. For the purposes of the merge, act as
1941 though we were defining the symbol we just defined, although we
1942 actually going to define an indirect symbol. */
1943 type_change_ok = false;
1944 size_change_ok = false;
1945 matched = true;
1946 tmp_sec = sec;
1947 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1948 &hi, poldbfd, NULL, NULL, &skip, &override,
1949 &type_change_ok, &size_change_ok, &matched))
1950 return false;
1951
1952 if (skip)
1953 goto nondefault;
1954
1955 if (hi->def_regular || ELF_COMMON_DEF_P (hi))
1956 {
1957 /* If the undecorated symbol will have a version added by a
1958 script different to H, then don't indirect to/from the
1959 undecorated symbol. This isn't ideal because we may not yet
1960 have seen symbol versions, if given by a script on the
1961 command line rather than via --version-script. */
1962 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1963 {
1964 bool hide;
1965
1966 hi->verinfo.vertree
1967 = bfd_find_version_for_sym (info->version_info,
1968 hi->root.root.string, &hide);
1969 if (hi->verinfo.vertree != NULL && hide)
1970 {
1971 (*bed->elf_backend_hide_symbol) (info, hi, true);
1972 goto nondefault;
1973 }
1974 }
1975 if (hi->verinfo.vertree != NULL
1976 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1977 goto nondefault;
1978 }
1979
1980 if (! override)
1981 {
1982 /* Add the default symbol if not performing a relocatable link. */
1983 if (! bfd_link_relocatable (info))
1984 {
1985 bh = &hi->root;
1986 if (bh->type == bfd_link_hash_defined
1987 && bh->u.def.section->owner != NULL
1988 && (bh->u.def.section->owner->flags & BFD_PLUGIN) != 0)
1989 {
1990 /* Mark the previous definition from IR object as
1991 undefined so that the generic linker will override
1992 it. */
1993 bh->type = bfd_link_hash_undefined;
1994 bh->u.undef.abfd = bh->u.def.section->owner;
1995 }
1996 if (! (_bfd_generic_link_add_one_symbol
1997 (info, abfd, shortname, BSF_INDIRECT,
1998 bfd_ind_section_ptr,
1999 0, name, false, collect, &bh)))
2000 return false;
2001 hi = (struct elf_link_hash_entry *) bh;
2002 }
2003 }
2004 else
2005 {
2006 /* In this case the symbol named SHORTNAME is overriding the
2007 indirect symbol we want to add. We were planning on making
2008 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
2009 is the name without a version. NAME is the fully versioned
2010 name, and it is the default version.
2011
2012 Overriding means that we already saw a definition for the
2013 symbol SHORTNAME in a regular object, and it is overriding
2014 the symbol defined in the dynamic object.
2015
2016 When this happens, we actually want to change NAME, the
2017 symbol we just added, to refer to SHORTNAME. This will cause
2018 references to NAME in the shared object to become references
2019 to SHORTNAME in the regular object. This is what we expect
2020 when we override a function in a shared object: that the
2021 references in the shared object will be mapped to the
2022 definition in the regular object. */
2023
2024 while (hi->root.type == bfd_link_hash_indirect
2025 || hi->root.type == bfd_link_hash_warning)
2026 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2027
2028 h->root.type = bfd_link_hash_indirect;
2029 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
2030 if (h->def_dynamic)
2031 {
2032 h->def_dynamic = 0;
2033 hi->ref_dynamic = 1;
2034 if (hi->ref_regular
2035 || hi->def_regular)
2036 {
2037 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
2038 return false;
2039 }
2040 }
2041
2042 /* Now set HI to H, so that the following code will set the
2043 other fields correctly. */
2044 hi = h;
2045 }
2046
2047 /* Check if HI is a warning symbol. */
2048 if (hi->root.type == bfd_link_hash_warning)
2049 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2050
2051 /* If there is a duplicate definition somewhere, then HI may not
2052 point to an indirect symbol. We will have reported an error to
2053 the user in that case. */
2054
2055 if (hi->root.type == bfd_link_hash_indirect)
2056 {
2057 struct elf_link_hash_entry *ht;
2058
2059 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
2060 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
2061
2062 /* If we first saw a reference to SHORTNAME with non-default
2063 visibility, merge that visibility to the @@VER symbol. */
2064 elf_merge_st_other (abfd, ht, hi->other, sec, true, dynamic);
2065
2066 /* A reference to the SHORTNAME symbol from a dynamic library
2067 will be satisfied by the versioned symbol at runtime. In
2068 effect, we have a reference to the versioned symbol. */
2069 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2070 hi->dynamic_def |= ht->dynamic_def;
2071
2072 /* See if the new flags lead us to realize that the symbol must
2073 be dynamic. */
2074 if (! *dynsym)
2075 {
2076 if (! dynamic)
2077 {
2078 if (! bfd_link_executable (info)
2079 || hi->def_dynamic
2080 || hi->ref_dynamic)
2081 *dynsym = true;
2082 }
2083 else
2084 {
2085 if (hi->ref_regular)
2086 *dynsym = true;
2087 }
2088 }
2089 }
2090
2091 /* We also need to define an indirection from the nondefault version
2092 of the symbol. */
2093
2094 nondefault:
2095 len = strlen (name);
2096 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
2097 if (shortname == NULL)
2098 return false;
2099 memcpy (shortname, name, shortlen);
2100 memcpy (shortname + shortlen, p + 1, len - shortlen);
2101
2102 /* Once again, merge with any existing symbol. */
2103 type_change_ok = false;
2104 size_change_ok = false;
2105 tmp_sec = sec;
2106 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
2107 &hi, poldbfd, NULL, NULL, &skip, &override,
2108 &type_change_ok, &size_change_ok, &matched))
2109 return false;
2110
2111 if (skip)
2112 {
2113 if (!dynamic
2114 && h->root.type == bfd_link_hash_defweak
2115 && hi->root.type == bfd_link_hash_defined)
2116 {
2117 /* We are handling a weak sym@@ver and attempting to define
2118 a weak sym@ver, but _bfd_elf_merge_symbol said to skip the
2119 new weak sym@ver because there is already a strong sym@ver.
2120 However, sym@ver and sym@@ver are really the same symbol.
2121 The existing strong sym@ver ought to override sym@@ver. */
2122 h->root.type = bfd_link_hash_defined;
2123 h->root.u.def.section = hi->root.u.def.section;
2124 h->root.u.def.value = hi->root.u.def.value;
2125 hi->root.type = bfd_link_hash_indirect;
2126 hi->root.u.i.link = &h->root;
2127 }
2128 else
2129 return true;
2130 }
2131 else if (override)
2132 {
2133 /* Here SHORTNAME is a versioned name, so we don't expect to see
2134 the type of override we do in the case above unless it is
2135 overridden by a versioned definition. */
2136 if (hi->root.type != bfd_link_hash_defined
2137 && hi->root.type != bfd_link_hash_defweak)
2138 _bfd_error_handler
2139 /* xgettext:c-format */
2140 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
2141 abfd, shortname);
2142 return true;
2143 }
2144 else
2145 {
2146 bh = &hi->root;
2147 if (! (_bfd_generic_link_add_one_symbol
2148 (info, abfd, shortname, BSF_INDIRECT,
2149 bfd_ind_section_ptr, 0, name, false, collect, &bh)))
2150 return false;
2151 hi = (struct elf_link_hash_entry *) bh;
2152 }
2153
2154 /* If there is a duplicate definition somewhere, then HI may not
2155 point to an indirect symbol. We will have reported an error
2156 to the user in that case. */
2157 if (hi->root.type == bfd_link_hash_indirect)
2158 {
2159 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
2160 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2161 hi->dynamic_def |= h->dynamic_def;
2162
2163 /* If we first saw a reference to @VER symbol with
2164 non-default visibility, merge that visibility to the
2165 @@VER symbol. */
2166 elf_merge_st_other (abfd, h, hi->other, sec, true, dynamic);
2167
2168 /* See if the new flags lead us to realize that the symbol
2169 must be dynamic. */
2170 if (! *dynsym)
2171 {
2172 if (! dynamic)
2173 {
2174 if (! bfd_link_executable (info)
2175 || hi->ref_dynamic)
2176 *dynsym = true;
2177 }
2178 else
2179 {
2180 if (hi->ref_regular)
2181 *dynsym = true;
2182 }
2183 }
2184 }
2185
2186 return true;
2187 }
2188 \f
2189 /* This routine is used to export all defined symbols into the dynamic
2190 symbol table. It is called via elf_link_hash_traverse. */
2191
2192 static bool
2193 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
2194 {
2195 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2196
2197 /* Ignore indirect symbols. These are added by the versioning code. */
2198 if (h->root.type == bfd_link_hash_indirect)
2199 return true;
2200
2201 /* Ignore this if we won't export it. */
2202 if (!eif->info->export_dynamic && !h->dynamic)
2203 return true;
2204
2205 if (h->dynindx == -1
2206 && (h->def_regular || h->ref_regular)
2207 && ! bfd_hide_sym_by_version (eif->info->version_info,
2208 h->root.root.string))
2209 {
2210 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2211 {
2212 eif->failed = true;
2213 return false;
2214 }
2215 }
2216
2217 return true;
2218 }
2219 \f
2220 /* Return true if GLIBC_ABI_DT_RELR is added to the list of version
2221 dependencies successfully. GLIBC_ABI_DT_RELR will be put into the
2222 .gnu.version_r section. */
2223
2224 static bool
2225 elf_link_add_dt_relr_dependency (struct elf_find_verdep_info *rinfo)
2226 {
2227 bfd *glibc_bfd = NULL;
2228 Elf_Internal_Verneed *t;
2229 Elf_Internal_Vernaux *a;
2230 size_t amt;
2231 const char *relr = "GLIBC_ABI_DT_RELR";
2232
2233 /* See if we already know about GLIBC_PRIVATE_DT_RELR. */
2234 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2235 t != NULL;
2236 t = t->vn_nextref)
2237 {
2238 const char *soname = bfd_elf_get_dt_soname (t->vn_bfd);
2239 /* Skip the shared library if it isn't libc.so. */
2240 if (!soname || !startswith (soname, "libc.so."))
2241 continue;
2242
2243 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2244 {
2245 /* Return if GLIBC_PRIVATE_DT_RELR dependency has been
2246 added. */
2247 if (a->vna_nodename == relr
2248 || strcmp (a->vna_nodename, relr) == 0)
2249 return true;
2250
2251 /* Check if libc.so provides GLIBC_2.XX version. */
2252 if (!glibc_bfd && startswith (a->vna_nodename, "GLIBC_2."))
2253 glibc_bfd = t->vn_bfd;
2254 }
2255
2256 break;
2257 }
2258
2259 /* Skip if it isn't linked against glibc. */
2260 if (glibc_bfd == NULL)
2261 return true;
2262
2263 /* This is a new version. Add it to tree we are building. */
2264 if (t == NULL)
2265 {
2266 amt = sizeof *t;
2267 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd,
2268 amt);
2269 if (t == NULL)
2270 {
2271 rinfo->failed = true;
2272 return false;
2273 }
2274
2275 t->vn_bfd = glibc_bfd;
2276 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2277 elf_tdata (rinfo->info->output_bfd)->verref = t;
2278 }
2279
2280 amt = sizeof *a;
2281 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2282 if (a == NULL)
2283 {
2284 rinfo->failed = true;
2285 return false;
2286 }
2287
2288 a->vna_nodename = relr;
2289 a->vna_flags = 0;
2290 a->vna_nextptr = t->vn_auxptr;
2291 a->vna_other = rinfo->vers + 1;
2292 ++rinfo->vers;
2293
2294 t->vn_auxptr = a;
2295
2296 return true;
2297 }
2298
2299 /* Look through the symbols which are defined in other shared
2300 libraries and referenced here. Update the list of version
2301 dependencies. This will be put into the .gnu.version_r section.
2302 This function is called via elf_link_hash_traverse. */
2303
2304 static bool
2305 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2306 void *data)
2307 {
2308 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2309 Elf_Internal_Verneed *t;
2310 Elf_Internal_Vernaux *a;
2311 size_t amt;
2312
2313 /* We only care about symbols defined in shared objects with version
2314 information. */
2315 if (!h->def_dynamic
2316 || h->def_regular
2317 || h->dynindx == -1
2318 || h->verinfo.verdef == NULL
2319 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2320 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2321 return true;
2322
2323 /* See if we already know about this version. */
2324 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2325 t != NULL;
2326 t = t->vn_nextref)
2327 {
2328 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2329 continue;
2330
2331 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2332 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2333 return true;
2334
2335 break;
2336 }
2337
2338 /* This is a new version. Add it to tree we are building. */
2339
2340 if (t == NULL)
2341 {
2342 amt = sizeof *t;
2343 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2344 if (t == NULL)
2345 {
2346 rinfo->failed = true;
2347 return false;
2348 }
2349
2350 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2351 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2352 elf_tdata (rinfo->info->output_bfd)->verref = t;
2353 }
2354
2355 amt = sizeof *a;
2356 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2357 if (a == NULL)
2358 {
2359 rinfo->failed = true;
2360 return false;
2361 }
2362
2363 /* Note that we are copying a string pointer here, and testing it
2364 above. If bfd_elf_string_from_elf_section is ever changed to
2365 discard the string data when low in memory, this will have to be
2366 fixed. */
2367 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2368
2369 a->vna_flags = h->verinfo.verdef->vd_flags;
2370 a->vna_nextptr = t->vn_auxptr;
2371
2372 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2373 ++rinfo->vers;
2374
2375 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2376
2377 t->vn_auxptr = a;
2378
2379 return true;
2380 }
2381
2382 /* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2383 hidden. Set *T_P to NULL if there is no match. */
2384
2385 static bool
2386 _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
2387 struct elf_link_hash_entry *h,
2388 const char *version_p,
2389 struct bfd_elf_version_tree **t_p,
2390 bool *hide)
2391 {
2392 struct bfd_elf_version_tree *t;
2393
2394 /* Look for the version. If we find it, it is no longer weak. */
2395 for (t = info->version_info; t != NULL; t = t->next)
2396 {
2397 if (strcmp (t->name, version_p) == 0)
2398 {
2399 size_t len;
2400 char *alc;
2401 struct bfd_elf_version_expr *d;
2402
2403 len = version_p - h->root.root.string;
2404 alc = (char *) bfd_malloc (len);
2405 if (alc == NULL)
2406 return false;
2407 memcpy (alc, h->root.root.string, len - 1);
2408 alc[len - 1] = '\0';
2409 if (alc[len - 2] == ELF_VER_CHR)
2410 alc[len - 2] = '\0';
2411
2412 h->verinfo.vertree = t;
2413 t->used = true;
2414 d = NULL;
2415
2416 if (t->globals.list != NULL)
2417 d = (*t->match) (&t->globals, NULL, alc);
2418
2419 /* See if there is anything to force this symbol to
2420 local scope. */
2421 if (d == NULL && t->locals.list != NULL)
2422 {
2423 d = (*t->match) (&t->locals, NULL, alc);
2424 if (d != NULL
2425 && h->dynindx != -1
2426 && ! info->export_dynamic)
2427 *hide = true;
2428 }
2429
2430 free (alc);
2431 break;
2432 }
2433 }
2434
2435 *t_p = t;
2436
2437 return true;
2438 }
2439
2440 /* Return TRUE if the symbol H is hidden by version script. */
2441
2442 bool
2443 _bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
2444 struct elf_link_hash_entry *h)
2445 {
2446 const char *p;
2447 bool hide = false;
2448 const struct elf_backend_data *bed
2449 = get_elf_backend_data (info->output_bfd);
2450
2451 /* Version script only hides symbols defined in regular objects. */
2452 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2453 return true;
2454
2455 p = strchr (h->root.root.string, ELF_VER_CHR);
2456 if (p != NULL && h->verinfo.vertree == NULL)
2457 {
2458 struct bfd_elf_version_tree *t;
2459
2460 ++p;
2461 if (*p == ELF_VER_CHR)
2462 ++p;
2463
2464 if (*p != '\0'
2465 && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
2466 && hide)
2467 {
2468 if (hide)
2469 (*bed->elf_backend_hide_symbol) (info, h, true);
2470 return true;
2471 }
2472 }
2473
2474 /* If we don't have a version for this symbol, see if we can find
2475 something. */
2476 if (h->verinfo.vertree == NULL && info->version_info != NULL)
2477 {
2478 h->verinfo.vertree
2479 = bfd_find_version_for_sym (info->version_info,
2480 h->root.root.string, &hide);
2481 if (h->verinfo.vertree != NULL && hide)
2482 {
2483 (*bed->elf_backend_hide_symbol) (info, h, true);
2484 return true;
2485 }
2486 }
2487
2488 return false;
2489 }
2490
2491 /* Figure out appropriate versions for all the symbols. We may not
2492 have the version number script until we have read all of the input
2493 files, so until that point we don't know which symbols should be
2494 local. This function is called via elf_link_hash_traverse. */
2495
2496 static bool
2497 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2498 {
2499 struct elf_info_failed *sinfo;
2500 struct bfd_link_info *info;
2501 const struct elf_backend_data *bed;
2502 struct elf_info_failed eif;
2503 char *p;
2504 bool hide;
2505
2506 sinfo = (struct elf_info_failed *) data;
2507 info = sinfo->info;
2508
2509 /* Fix the symbol flags. */
2510 eif.failed = false;
2511 eif.info = info;
2512 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2513 {
2514 if (eif.failed)
2515 sinfo->failed = true;
2516 return false;
2517 }
2518
2519 bed = get_elf_backend_data (info->output_bfd);
2520
2521 /* We only need version numbers for symbols defined in regular
2522 objects. */
2523 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2524 {
2525 /* Hide symbols defined in discarded input sections. */
2526 if ((h->root.type == bfd_link_hash_defined
2527 || h->root.type == bfd_link_hash_defweak)
2528 && discarded_section (h->root.u.def.section))
2529 (*bed->elf_backend_hide_symbol) (info, h, true);
2530 return true;
2531 }
2532
2533 hide = false;
2534 p = strchr (h->root.root.string, ELF_VER_CHR);
2535 if (p != NULL && h->verinfo.vertree == NULL)
2536 {
2537 struct bfd_elf_version_tree *t;
2538
2539 ++p;
2540 if (*p == ELF_VER_CHR)
2541 ++p;
2542
2543 /* If there is no version string, we can just return out. */
2544 if (*p == '\0')
2545 return true;
2546
2547 if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
2548 {
2549 sinfo->failed = true;
2550 return false;
2551 }
2552
2553 if (hide)
2554 (*bed->elf_backend_hide_symbol) (info, h, true);
2555
2556 /* If we are building an application, we need to create a
2557 version node for this version. */
2558 if (t == NULL && bfd_link_executable (info))
2559 {
2560 struct bfd_elf_version_tree **pp;
2561 int version_index;
2562
2563 /* If we aren't going to export this symbol, we don't need
2564 to worry about it. */
2565 if (h->dynindx == -1)
2566 return true;
2567
2568 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2569 sizeof *t);
2570 if (t == NULL)
2571 {
2572 sinfo->failed = true;
2573 return false;
2574 }
2575
2576 t->name = p;
2577 t->name_indx = (unsigned int) -1;
2578 t->used = true;
2579
2580 version_index = 1;
2581 /* Don't count anonymous version tag. */
2582 if (sinfo->info->version_info != NULL
2583 && sinfo->info->version_info->vernum == 0)
2584 version_index = 0;
2585 for (pp = &sinfo->info->version_info;
2586 *pp != NULL;
2587 pp = &(*pp)->next)
2588 ++version_index;
2589 t->vernum = version_index;
2590
2591 *pp = t;
2592
2593 h->verinfo.vertree = t;
2594 }
2595 else if (t == NULL)
2596 {
2597 /* We could not find the version for a symbol when
2598 generating a shared archive. Return an error. */
2599 _bfd_error_handler
2600 /* xgettext:c-format */
2601 (_("%pB: version node not found for symbol %s"),
2602 info->output_bfd, h->root.root.string);
2603 bfd_set_error (bfd_error_bad_value);
2604 sinfo->failed = true;
2605 return false;
2606 }
2607 }
2608
2609 /* If we don't have a version for this symbol, see if we can find
2610 something. */
2611 if (!hide
2612 && h->verinfo.vertree == NULL
2613 && sinfo->info->version_info != NULL)
2614 {
2615 h->verinfo.vertree
2616 = bfd_find_version_for_sym (sinfo->info->version_info,
2617 h->root.root.string, &hide);
2618 if (h->verinfo.vertree != NULL && hide)
2619 (*bed->elf_backend_hide_symbol) (info, h, true);
2620 }
2621
2622 return true;
2623 }
2624 \f
2625 /* Read and swap the relocs from the section indicated by SHDR. This
2626 may be either a REL or a RELA section. The relocations are
2627 translated into RELA relocations and stored in INTERNAL_RELOCS,
2628 which should have already been allocated to contain enough space.
2629 The EXTERNAL_RELOCS are a buffer where the external form of the
2630 relocations should be stored.
2631
2632 Returns FALSE if something goes wrong. */
2633
2634 static bool
2635 elf_link_read_relocs_from_section (bfd *abfd,
2636 asection *sec,
2637 Elf_Internal_Shdr *shdr,
2638 void *external_relocs,
2639 Elf_Internal_Rela *internal_relocs)
2640 {
2641 const struct elf_backend_data *bed;
2642 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2643 const bfd_byte *erela;
2644 const bfd_byte *erelaend;
2645 Elf_Internal_Rela *irela;
2646 Elf_Internal_Shdr *symtab_hdr;
2647 size_t nsyms;
2648
2649 /* Position ourselves at the start of the section. */
2650 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2651 return false;
2652
2653 /* Read the relocations. */
2654 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2655 return false;
2656
2657 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2658 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2659
2660 bed = get_elf_backend_data (abfd);
2661
2662 /* Convert the external relocations to the internal format. */
2663 if (shdr->sh_entsize == bed->s->sizeof_rel)
2664 swap_in = bed->s->swap_reloc_in;
2665 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2666 swap_in = bed->s->swap_reloca_in;
2667 else
2668 {
2669 bfd_set_error (bfd_error_wrong_format);
2670 return false;
2671 }
2672
2673 erela = (const bfd_byte *) external_relocs;
2674 /* Setting erelaend like this and comparing with <= handles case of
2675 a fuzzed object with sh_size not a multiple of sh_entsize. */
2676 erelaend = erela + shdr->sh_size - shdr->sh_entsize;
2677 irela = internal_relocs;
2678 while (erela <= erelaend)
2679 {
2680 bfd_vma r_symndx;
2681
2682 (*swap_in) (abfd, erela, irela);
2683 r_symndx = ELF32_R_SYM (irela->r_info);
2684 if (bed->s->arch_size == 64)
2685 r_symndx >>= 24;
2686 if (nsyms > 0)
2687 {
2688 if ((size_t) r_symndx >= nsyms)
2689 {
2690 _bfd_error_handler
2691 /* xgettext:c-format */
2692 (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
2693 " for offset %#" PRIx64 " in section `%pA'"),
2694 abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
2695 (uint64_t) irela->r_offset, sec);
2696 bfd_set_error (bfd_error_bad_value);
2697 return false;
2698 }
2699 }
2700 else if (r_symndx != STN_UNDEF)
2701 {
2702 _bfd_error_handler
2703 /* xgettext:c-format */
2704 (_("%pB: non-zero symbol index (%#" PRIx64 ")"
2705 " for offset %#" PRIx64 " in section `%pA'"
2706 " when the object file has no symbol table"),
2707 abfd, (uint64_t) r_symndx,
2708 (uint64_t) irela->r_offset, sec);
2709 bfd_set_error (bfd_error_bad_value);
2710 return false;
2711 }
2712 irela += bed->s->int_rels_per_ext_rel;
2713 erela += shdr->sh_entsize;
2714 }
2715
2716 return true;
2717 }
2718
2719 /* Read and swap the relocs for a section O. They may have been
2720 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2721 not NULL, they are used as buffers to read into. They are known to
2722 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2723 the return value is allocated using either malloc or bfd_alloc,
2724 according to the KEEP_MEMORY argument. If O has two relocation
2725 sections (both REL and RELA relocations), then the REL_HDR
2726 relocations will appear first in INTERNAL_RELOCS, followed by the
2727 RELA_HDR relocations. If INFO isn't NULL and KEEP_MEMORY is true,
2728 update cache_size. */
2729
2730 Elf_Internal_Rela *
2731 _bfd_elf_link_info_read_relocs (bfd *abfd,
2732 struct bfd_link_info *info,
2733 asection *o,
2734 void *external_relocs,
2735 Elf_Internal_Rela *internal_relocs,
2736 bool keep_memory)
2737 {
2738 void *alloc1 = NULL;
2739 Elf_Internal_Rela *alloc2 = NULL;
2740 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2741 struct bfd_elf_section_data *esdo = elf_section_data (o);
2742 Elf_Internal_Rela *internal_rela_relocs;
2743
2744 if (esdo->relocs != NULL)
2745 return esdo->relocs;
2746
2747 if (o->reloc_count == 0)
2748 return NULL;
2749
2750 if (internal_relocs == NULL)
2751 {
2752 bfd_size_type size;
2753
2754 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
2755 if (keep_memory)
2756 {
2757 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2758 if (info)
2759 info->cache_size += size;
2760 }
2761 else
2762 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2763 if (internal_relocs == NULL)
2764 goto error_return;
2765 }
2766
2767 if (external_relocs == NULL)
2768 {
2769 bfd_size_type size = 0;
2770
2771 if (esdo->rel.hdr)
2772 size += esdo->rel.hdr->sh_size;
2773 if (esdo->rela.hdr)
2774 size += esdo->rela.hdr->sh_size;
2775
2776 alloc1 = bfd_malloc (size);
2777 if (alloc1 == NULL)
2778 goto error_return;
2779 external_relocs = alloc1;
2780 }
2781
2782 internal_rela_relocs = internal_relocs;
2783 if (esdo->rel.hdr)
2784 {
2785 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2786 external_relocs,
2787 internal_relocs))
2788 goto error_return;
2789 external_relocs = (((bfd_byte *) external_relocs)
2790 + esdo->rel.hdr->sh_size);
2791 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2792 * bed->s->int_rels_per_ext_rel);
2793 }
2794
2795 if (esdo->rela.hdr
2796 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2797 external_relocs,
2798 internal_rela_relocs)))
2799 goto error_return;
2800
2801 /* Cache the results for next time, if we can. */
2802 if (keep_memory)
2803 esdo->relocs = internal_relocs;
2804
2805 free (alloc1);
2806
2807 /* Don't free alloc2, since if it was allocated we are passing it
2808 back (under the name of internal_relocs). */
2809
2810 return internal_relocs;
2811
2812 error_return:
2813 free (alloc1);
2814 if (alloc2 != NULL)
2815 {
2816 if (keep_memory)
2817 bfd_release (abfd, alloc2);
2818 else
2819 free (alloc2);
2820 }
2821 return NULL;
2822 }
2823
2824 /* This is similar to _bfd_elf_link_info_read_relocs, except for that
2825 NULL is passed to _bfd_elf_link_info_read_relocs for pointer to
2826 struct bfd_link_info. */
2827
2828 Elf_Internal_Rela *
2829 _bfd_elf_link_read_relocs (bfd *abfd,
2830 asection *o,
2831 void *external_relocs,
2832 Elf_Internal_Rela *internal_relocs,
2833 bool keep_memory)
2834 {
2835 return _bfd_elf_link_info_read_relocs (abfd, NULL, o, external_relocs,
2836 internal_relocs, keep_memory);
2837
2838 }
2839
2840 /* Compute the size of, and allocate space for, REL_HDR which is the
2841 section header for a section containing relocations for O. */
2842
2843 static bool
2844 _bfd_elf_link_size_reloc_section (bfd *abfd,
2845 struct bfd_elf_section_reloc_data *reldata)
2846 {
2847 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2848
2849 /* That allows us to calculate the size of the section. */
2850 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2851
2852 /* The contents field must last into write_object_contents, so we
2853 allocate it with bfd_alloc rather than malloc. Also since we
2854 cannot be sure that the contents will actually be filled in,
2855 we zero the allocated space. */
2856 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2857 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2858 return false;
2859
2860 if (reldata->hashes == NULL && reldata->count)
2861 {
2862 struct elf_link_hash_entry **p;
2863
2864 p = ((struct elf_link_hash_entry **)
2865 bfd_zmalloc (reldata->count * sizeof (*p)));
2866 if (p == NULL)
2867 return false;
2868
2869 reldata->hashes = p;
2870 }
2871
2872 return true;
2873 }
2874
2875 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2876 originated from the section given by INPUT_REL_HDR) to the
2877 OUTPUT_BFD. */
2878
2879 bool
2880 _bfd_elf_link_output_relocs (bfd *output_bfd,
2881 asection *input_section,
2882 Elf_Internal_Shdr *input_rel_hdr,
2883 Elf_Internal_Rela *internal_relocs,
2884 struct elf_link_hash_entry **rel_hash
2885 ATTRIBUTE_UNUSED)
2886 {
2887 Elf_Internal_Rela *irela;
2888 Elf_Internal_Rela *irelaend;
2889 bfd_byte *erel;
2890 struct bfd_elf_section_reloc_data *output_reldata;
2891 asection *output_section;
2892 const struct elf_backend_data *bed;
2893 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2894 struct bfd_elf_section_data *esdo;
2895
2896 output_section = input_section->output_section;
2897
2898 bed = get_elf_backend_data (output_bfd);
2899 esdo = elf_section_data (output_section);
2900 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2901 {
2902 output_reldata = &esdo->rel;
2903 swap_out = bed->s->swap_reloc_out;
2904 }
2905 else if (esdo->rela.hdr
2906 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2907 {
2908 output_reldata = &esdo->rela;
2909 swap_out = bed->s->swap_reloca_out;
2910 }
2911 else
2912 {
2913 _bfd_error_handler
2914 /* xgettext:c-format */
2915 (_("%pB: relocation size mismatch in %pB section %pA"),
2916 output_bfd, input_section->owner, input_section);
2917 bfd_set_error (bfd_error_wrong_format);
2918 return false;
2919 }
2920
2921 erel = output_reldata->hdr->contents;
2922 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2923 irela = internal_relocs;
2924 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2925 * bed->s->int_rels_per_ext_rel);
2926 while (irela < irelaend)
2927 {
2928 (*swap_out) (output_bfd, irela, erel);
2929 irela += bed->s->int_rels_per_ext_rel;
2930 erel += input_rel_hdr->sh_entsize;
2931 }
2932
2933 /* Bump the counter, so that we know where to add the next set of
2934 relocations. */
2935 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2936
2937 return true;
2938 }
2939 \f
2940 /* Make weak undefined symbols in PIE dynamic. */
2941
2942 bool
2943 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2944 struct elf_link_hash_entry *h)
2945 {
2946 if (bfd_link_pie (info)
2947 && h->dynindx == -1
2948 && h->root.type == bfd_link_hash_undefweak)
2949 return bfd_elf_link_record_dynamic_symbol (info, h);
2950
2951 return true;
2952 }
2953
2954 /* Fix up the flags for a symbol. This handles various cases which
2955 can only be fixed after all the input files are seen. This is
2956 currently called by both adjust_dynamic_symbol and
2957 assign_sym_version, which is unnecessary but perhaps more robust in
2958 the face of future changes. */
2959
2960 static bool
2961 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2962 struct elf_info_failed *eif)
2963 {
2964 const struct elf_backend_data *bed;
2965
2966 /* If this symbol was mentioned in a non-ELF file, try to set
2967 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2968 permit a non-ELF file to correctly refer to a symbol defined in
2969 an ELF dynamic object. */
2970 if (h->non_elf)
2971 {
2972 while (h->root.type == bfd_link_hash_indirect)
2973 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2974
2975 if (h->root.type != bfd_link_hash_defined
2976 && h->root.type != bfd_link_hash_defweak)
2977 {
2978 h->ref_regular = 1;
2979 h->ref_regular_nonweak = 1;
2980 }
2981 else
2982 {
2983 if (h->root.u.def.section->owner != NULL
2984 && (bfd_get_flavour (h->root.u.def.section->owner)
2985 == bfd_target_elf_flavour))
2986 {
2987 h->ref_regular = 1;
2988 h->ref_regular_nonweak = 1;
2989 }
2990 else
2991 h->def_regular = 1;
2992 }
2993
2994 if (h->dynindx == -1
2995 && (h->def_dynamic
2996 || h->ref_dynamic))
2997 {
2998 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2999 {
3000 eif->failed = true;
3001 return false;
3002 }
3003 }
3004 }
3005 else
3006 {
3007 /* Unfortunately, NON_ELF is only correct if the symbol
3008 was first seen in a non-ELF file. Fortunately, if the symbol
3009 was first seen in an ELF file, we're probably OK unless the
3010 symbol was defined in a non-ELF file. Catch that case here.
3011 FIXME: We're still in trouble if the symbol was first seen in
3012 a dynamic object, and then later in a non-ELF regular object. */
3013 if ((h->root.type == bfd_link_hash_defined
3014 || h->root.type == bfd_link_hash_defweak)
3015 && !h->def_regular
3016 && (h->root.u.def.section->owner != NULL
3017 ? (bfd_get_flavour (h->root.u.def.section->owner)
3018 != bfd_target_elf_flavour)
3019 : (bfd_is_abs_section (h->root.u.def.section)
3020 && !h->def_dynamic)))
3021 h->def_regular = 1;
3022 }
3023
3024 /* Backend specific symbol fixup. */
3025 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
3026 if (bed->elf_backend_fixup_symbol
3027 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
3028 return false;
3029
3030 /* If this is a final link, and the symbol was defined as a common
3031 symbol in a regular object file, and there was no definition in
3032 any dynamic object, then the linker will have allocated space for
3033 the symbol in a common section but the DEF_REGULAR
3034 flag will not have been set. */
3035 if (h->root.type == bfd_link_hash_defined
3036 && !h->def_regular
3037 && h->ref_regular
3038 && !h->def_dynamic
3039 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
3040 h->def_regular = 1;
3041
3042 /* Symbols defined in discarded sections shouldn't be dynamic. */
3043 if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
3044 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3045
3046 /* If a weak undefined symbol has non-default visibility, we also
3047 hide it from the dynamic linker. */
3048 else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3049 && h->root.type == bfd_link_hash_undefweak)
3050 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3051
3052 /* A hidden versioned symbol in executable should be forced local if
3053 it is is locally defined, not referenced by shared library and not
3054 exported. */
3055 else if (bfd_link_executable (eif->info)
3056 && h->versioned == versioned_hidden
3057 && !eif->info->export_dynamic
3058 && !h->dynamic
3059 && !h->ref_dynamic
3060 && h->def_regular)
3061 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3062
3063 /* If -Bsymbolic was used (which means to bind references to global
3064 symbols to the definition within the shared object), and this
3065 symbol was defined in a regular object, then it actually doesn't
3066 need a PLT entry. Likewise, if the symbol has non-default
3067 visibility. If the symbol has hidden or internal visibility, we
3068 will force it local. */
3069 else if (h->needs_plt
3070 && bfd_link_pic (eif->info)
3071 && is_elf_hash_table (eif->info->hash)
3072 && (SYMBOLIC_BIND (eif->info, h)
3073 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
3074 && h->def_regular)
3075 {
3076 bool force_local;
3077
3078 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
3079 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
3080 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
3081 }
3082
3083 /* If this is a weak defined symbol in a dynamic object, and we know
3084 the real definition in the dynamic object, copy interesting flags
3085 over to the real definition. */
3086 if (h->is_weakalias)
3087 {
3088 struct elf_link_hash_entry *def = weakdef (h);
3089
3090 /* If the real definition is defined by a regular object file,
3091 don't do anything special. See the longer description in
3092 _bfd_elf_adjust_dynamic_symbol, below. If the def is not
3093 bfd_link_hash_defined as it was when put on the alias list
3094 then it must have originally been a versioned symbol (for
3095 which a non-versioned indirect symbol is created) and later
3096 a definition for the non-versioned symbol is found. In that
3097 case the indirection is flipped with the versioned symbol
3098 becoming an indirect pointing at the non-versioned symbol.
3099 Thus, not an alias any more. */
3100 if (def->def_regular
3101 || def->root.type != bfd_link_hash_defined)
3102 {
3103 h = def;
3104 while ((h = h->u.alias) != def)
3105 h->is_weakalias = 0;
3106 }
3107 else
3108 {
3109 while (h->root.type == bfd_link_hash_indirect)
3110 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3111 BFD_ASSERT (h->root.type == bfd_link_hash_defined
3112 || h->root.type == bfd_link_hash_defweak);
3113 BFD_ASSERT (def->def_dynamic);
3114 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
3115 }
3116 }
3117
3118 return true;
3119 }
3120
3121 /* Make the backend pick a good value for a dynamic symbol. This is
3122 called via elf_link_hash_traverse, and also calls itself
3123 recursively. */
3124
3125 static bool
3126 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
3127 {
3128 struct elf_info_failed *eif = (struct elf_info_failed *) data;
3129 struct elf_link_hash_table *htab;
3130 const struct elf_backend_data *bed;
3131
3132 if (! is_elf_hash_table (eif->info->hash))
3133 return false;
3134
3135 /* Ignore indirect symbols. These are added by the versioning code. */
3136 if (h->root.type == bfd_link_hash_indirect)
3137 return true;
3138
3139 /* Fix the symbol flags. */
3140 if (! _bfd_elf_fix_symbol_flags (h, eif))
3141 return false;
3142
3143 htab = elf_hash_table (eif->info);
3144 bed = get_elf_backend_data (htab->dynobj);
3145
3146 if (h->root.type == bfd_link_hash_undefweak)
3147 {
3148 if (eif->info->dynamic_undefined_weak == 0)
3149 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3150 else if (eif->info->dynamic_undefined_weak > 0
3151 && h->ref_regular
3152 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3153 && !bfd_hide_sym_by_version (eif->info->version_info,
3154 h->root.root.string))
3155 {
3156 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
3157 {
3158 eif->failed = true;
3159 return false;
3160 }
3161 }
3162 }
3163
3164 /* If this symbol does not require a PLT entry, and it is not
3165 defined by a dynamic object, or is not referenced by a regular
3166 object, ignore it. We do have to handle a weak defined symbol,
3167 even if no regular object refers to it, if we decided to add it
3168 to the dynamic symbol table. FIXME: Do we normally need to worry
3169 about symbols which are defined by one dynamic object and
3170 referenced by another one? */
3171 if (!h->needs_plt
3172 && h->type != STT_GNU_IFUNC
3173 && (h->def_regular
3174 || !h->def_dynamic
3175 || (!h->ref_regular
3176 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
3177 {
3178 h->plt = elf_hash_table (eif->info)->init_plt_offset;
3179 return true;
3180 }
3181
3182 /* If we've already adjusted this symbol, don't do it again. This
3183 can happen via a recursive call. */
3184 if (h->dynamic_adjusted)
3185 return true;
3186
3187 /* Don't look at this symbol again. Note that we must set this
3188 after checking the above conditions, because we may look at a
3189 symbol once, decide not to do anything, and then get called
3190 recursively later after REF_REGULAR is set below. */
3191 h->dynamic_adjusted = 1;
3192
3193 /* If this is a weak definition, and we know a real definition, and
3194 the real symbol is not itself defined by a regular object file,
3195 then get a good value for the real definition. We handle the
3196 real symbol first, for the convenience of the backend routine.
3197
3198 Note that there is a confusing case here. If the real definition
3199 is defined by a regular object file, we don't get the real symbol
3200 from the dynamic object, but we do get the weak symbol. If the
3201 processor backend uses a COPY reloc, then if some routine in the
3202 dynamic object changes the real symbol, we will not see that
3203 change in the corresponding weak symbol. This is the way other
3204 ELF linkers work as well, and seems to be a result of the shared
3205 library model.
3206
3207 I will clarify this issue. Most SVR4 shared libraries define the
3208 variable _timezone and define timezone as a weak synonym. The
3209 tzset call changes _timezone. If you write
3210 extern int timezone;
3211 int _timezone = 5;
3212 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3213 you might expect that, since timezone is a synonym for _timezone,
3214 the same number will print both times. However, if the processor
3215 backend uses a COPY reloc, then actually timezone will be copied
3216 into your process image, and, since you define _timezone
3217 yourself, _timezone will not. Thus timezone and _timezone will
3218 wind up at different memory locations. The tzset call will set
3219 _timezone, leaving timezone unchanged. */
3220
3221 if (h->is_weakalias)
3222 {
3223 struct elf_link_hash_entry *def = weakdef (h);
3224
3225 /* If we get to this point, there is an implicit reference to
3226 the alias by a regular object file via the weak symbol H. */
3227 def->ref_regular = 1;
3228
3229 /* Ensure that the backend adjust_dynamic_symbol function sees
3230 the strong alias before H by recursively calling ourselves. */
3231 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
3232 return false;
3233 }
3234
3235 /* If a symbol has no type and no size and does not require a PLT
3236 entry, then we are probably about to do the wrong thing here: we
3237 are probably going to create a COPY reloc for an empty object.
3238 This case can arise when a shared object is built with assembly
3239 code, and the assembly code fails to set the symbol type. */
3240 if (h->size == 0
3241 && h->type == STT_NOTYPE
3242 && !h->needs_plt)
3243 _bfd_error_handler
3244 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3245 h->root.root.string);
3246
3247 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3248 {
3249 eif->failed = true;
3250 return false;
3251 }
3252
3253 return true;
3254 }
3255
3256 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3257 DYNBSS. */
3258
3259 bool
3260 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3261 struct elf_link_hash_entry *h,
3262 asection *dynbss)
3263 {
3264 unsigned int power_of_two;
3265 bfd_vma mask;
3266 asection *sec = h->root.u.def.section;
3267
3268 /* The section alignment of the definition is the maximum alignment
3269 requirement of symbols defined in the section. Since we don't
3270 know the symbol alignment requirement, we start with the
3271 maximum alignment and check low bits of the symbol address
3272 for the minimum alignment. */
3273 power_of_two = bfd_section_alignment (sec);
3274 mask = ((bfd_vma) 1 << power_of_two) - 1;
3275 while ((h->root.u.def.value & mask) != 0)
3276 {
3277 mask >>= 1;
3278 --power_of_two;
3279 }
3280
3281 if (power_of_two > bfd_section_alignment (dynbss))
3282 {
3283 /* Adjust the section alignment if needed. */
3284 if (!bfd_set_section_alignment (dynbss, power_of_two))
3285 return false;
3286 }
3287
3288 /* We make sure that the symbol will be aligned properly. */
3289 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3290
3291 /* Define the symbol as being at this point in DYNBSS. */
3292 h->root.u.def.section = dynbss;
3293 h->root.u.def.value = dynbss->size;
3294
3295 /* Increment the size of DYNBSS to make room for the symbol. */
3296 dynbss->size += h->size;
3297
3298 /* No error if extern_protected_data is true. */
3299 if (h->protected_def
3300 && (!info->extern_protected_data
3301 || (info->extern_protected_data < 0
3302 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
3303 info->callbacks->einfo
3304 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3305 h->root.root.string);
3306
3307 return true;
3308 }
3309
3310 /* Adjust all external symbols pointing into SEC_MERGE sections
3311 to reflect the object merging within the sections. */
3312
3313 static bool
3314 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
3315 {
3316 asection *sec;
3317
3318 if ((h->root.type == bfd_link_hash_defined
3319 || h->root.type == bfd_link_hash_defweak)
3320 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
3321 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3322 {
3323 bfd *output_bfd = (bfd *) data;
3324
3325 h->root.u.def.value =
3326 _bfd_merged_section_offset (output_bfd,
3327 &h->root.u.def.section,
3328 elf_section_data (sec)->sec_info,
3329 h->root.u.def.value);
3330 }
3331
3332 return true;
3333 }
3334
3335 /* Returns false if the symbol referred to by H should be considered
3336 to resolve local to the current module, and true if it should be
3337 considered to bind dynamically. */
3338
3339 bool
3340 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3341 struct bfd_link_info *info,
3342 bool not_local_protected)
3343 {
3344 bool binding_stays_local_p;
3345 const struct elf_backend_data *bed;
3346 struct elf_link_hash_table *hash_table;
3347
3348 if (h == NULL)
3349 return false;
3350
3351 while (h->root.type == bfd_link_hash_indirect
3352 || h->root.type == bfd_link_hash_warning)
3353 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3354
3355 /* If it was forced local, then clearly it's not dynamic. */
3356 if (h->dynindx == -1)
3357 return false;
3358 if (h->forced_local)
3359 return false;
3360
3361 /* Identify the cases where name binding rules say that a
3362 visible symbol resolves locally. */
3363 binding_stays_local_p = (bfd_link_executable (info)
3364 || SYMBOLIC_BIND (info, h));
3365
3366 switch (ELF_ST_VISIBILITY (h->other))
3367 {
3368 case STV_INTERNAL:
3369 case STV_HIDDEN:
3370 return false;
3371
3372 case STV_PROTECTED:
3373 hash_table = elf_hash_table (info);
3374 if (!is_elf_hash_table (&hash_table->root))
3375 return false;
3376
3377 bed = get_elf_backend_data (hash_table->dynobj);
3378
3379 /* Proper resolution for function pointer equality may require
3380 that these symbols perhaps be resolved dynamically, even though
3381 we should be resolving them to the current module. */
3382 if (!not_local_protected || !bed->is_function_type (h->type))
3383 binding_stays_local_p = true;
3384 break;
3385
3386 default:
3387 break;
3388 }
3389
3390 /* If it isn't defined locally, then clearly it's dynamic. */
3391 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
3392 return true;
3393
3394 /* Otherwise, the symbol is dynamic if binding rules don't tell
3395 us that it remains local. */
3396 return !binding_stays_local_p;
3397 }
3398
3399 /* Return true if the symbol referred to by H should be considered
3400 to resolve local to the current module, and false otherwise. Differs
3401 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3402 undefined symbols. The two functions are virtually identical except
3403 for the place where dynindx == -1 is tested. If that test is true,
3404 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3405 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3406 defined symbols.
3407 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3408 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3409 treatment of undefined weak symbols. For those that do not make
3410 undefined weak symbols dynamic, both functions may return false. */
3411
3412 bool
3413 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3414 struct bfd_link_info *info,
3415 bool local_protected)
3416 {
3417 const struct elf_backend_data *bed;
3418 struct elf_link_hash_table *hash_table;
3419
3420 /* If it's a local sym, of course we resolve locally. */
3421 if (h == NULL)
3422 return true;
3423
3424 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3425 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3426 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3427 return true;
3428
3429 /* Forced local symbols resolve locally. */
3430 if (h->forced_local)
3431 return true;
3432
3433 /* Common symbols that become definitions don't get the DEF_REGULAR
3434 flag set, so test it first, and don't bail out. */
3435 if (ELF_COMMON_DEF_P (h))
3436 /* Do nothing. */;
3437 /* If we don't have a definition in a regular file, then we can't
3438 resolve locally. The sym is either undefined or dynamic. */
3439 else if (!h->def_regular)
3440 return false;
3441
3442 /* Non-dynamic symbols resolve locally. */
3443 if (h->dynindx == -1)
3444 return true;
3445
3446 /* At this point, we know the symbol is defined and dynamic. In an
3447 executable it must resolve locally, likewise when building symbolic
3448 shared libraries. */
3449 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3450 return true;
3451
3452 /* Now deal with defined dynamic symbols in shared libraries. Ones
3453 with default visibility might not resolve locally. */
3454 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3455 return false;
3456
3457 hash_table = elf_hash_table (info);
3458 if (!is_elf_hash_table (&hash_table->root))
3459 return true;
3460
3461 /* STV_PROTECTED symbols with indirect external access are local. */
3462 if (info->indirect_extern_access > 0)
3463 return true;
3464
3465 bed = get_elf_backend_data (hash_table->dynobj);
3466
3467 /* If extern_protected_data is false, STV_PROTECTED non-function
3468 symbols are local. */
3469 if ((!info->extern_protected_data
3470 || (info->extern_protected_data < 0
3471 && !bed->extern_protected_data))
3472 && !bed->is_function_type (h->type))
3473 return true;
3474
3475 /* Function pointer equality tests may require that STV_PROTECTED
3476 symbols be treated as dynamic symbols. If the address of a
3477 function not defined in an executable is set to that function's
3478 plt entry in the executable, then the address of the function in
3479 a shared library must also be the plt entry in the executable. */
3480 return local_protected;
3481 }
3482
3483 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3484 aligned. Returns the first TLS output section. */
3485
3486 struct bfd_section *
3487 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3488 {
3489 struct bfd_section *sec, *tls;
3490 unsigned int align = 0;
3491
3492 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3493 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3494 break;
3495 tls = sec;
3496
3497 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3498 if (sec->alignment_power > align)
3499 align = sec->alignment_power;
3500
3501 elf_hash_table (info)->tls_sec = tls;
3502
3503 /* Ensure the alignment of the first section (usually .tdata) is the largest
3504 alignment, so that the tls segment starts aligned. */
3505 if (tls != NULL)
3506 tls->alignment_power = align;
3507
3508 return tls;
3509 }
3510
3511 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3512 static bool
3513 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3514 Elf_Internal_Sym *sym)
3515 {
3516 const struct elf_backend_data *bed;
3517
3518 /* Local symbols do not count, but target specific ones might. */
3519 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3520 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3521 return false;
3522
3523 bed = get_elf_backend_data (abfd);
3524 /* Function symbols do not count. */
3525 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3526 return false;
3527
3528 /* If the section is undefined, then so is the symbol. */
3529 if (sym->st_shndx == SHN_UNDEF)
3530 return false;
3531
3532 /* If the symbol is defined in the common section, then
3533 it is a common definition and so does not count. */
3534 if (bed->common_definition (sym))
3535 return false;
3536
3537 /* If the symbol is in a target specific section then we
3538 must rely upon the backend to tell us what it is. */
3539 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3540 /* FIXME - this function is not coded yet:
3541
3542 return _bfd_is_global_symbol_definition (abfd, sym);
3543
3544 Instead for now assume that the definition is not global,
3545 Even if this is wrong, at least the linker will behave
3546 in the same way that it used to do. */
3547 return false;
3548
3549 return true;
3550 }
3551
3552 /* Search the symbol table of the archive element of the archive ABFD
3553 whose archive map contains a mention of SYMDEF, and determine if
3554 the symbol is defined in this element. */
3555 static bool
3556 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3557 {
3558 Elf_Internal_Shdr * hdr;
3559 size_t symcount;
3560 size_t extsymcount;
3561 size_t extsymoff;
3562 Elf_Internal_Sym *isymbuf;
3563 Elf_Internal_Sym *isym;
3564 Elf_Internal_Sym *isymend;
3565 bool result;
3566
3567 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset, NULL);
3568 if (abfd == NULL)
3569 return false;
3570
3571 if (! bfd_check_format (abfd, bfd_object))
3572 return false;
3573
3574 /* Select the appropriate symbol table. If we don't know if the
3575 object file is an IR object, give linker LTO plugin a chance to
3576 get the correct symbol table. */
3577 if (abfd->plugin_format == bfd_plugin_yes
3578 #if BFD_SUPPORTS_PLUGINS
3579 || (abfd->plugin_format == bfd_plugin_unknown
3580 && bfd_link_plugin_object_p (abfd))
3581 #endif
3582 )
3583 {
3584 /* Use the IR symbol table if the object has been claimed by
3585 plugin. */
3586 abfd = abfd->plugin_dummy_bfd;
3587 hdr = &elf_tdata (abfd)->symtab_hdr;
3588 }
3589 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3590 hdr = &elf_tdata (abfd)->symtab_hdr;
3591 else
3592 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3593
3594 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3595
3596 /* The sh_info field of the symtab header tells us where the
3597 external symbols start. We don't care about the local symbols. */
3598 if (elf_bad_symtab (abfd))
3599 {
3600 extsymcount = symcount;
3601 extsymoff = 0;
3602 }
3603 else
3604 {
3605 extsymcount = symcount - hdr->sh_info;
3606 extsymoff = hdr->sh_info;
3607 }
3608
3609 if (extsymcount == 0)
3610 return false;
3611
3612 /* Read in the symbol table. */
3613 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3614 NULL, NULL, NULL);
3615 if (isymbuf == NULL)
3616 return false;
3617
3618 /* Scan the symbol table looking for SYMDEF. */
3619 result = false;
3620 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3621 {
3622 const char *name;
3623
3624 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3625 isym->st_name);
3626 if (name == NULL)
3627 break;
3628
3629 if (strcmp (name, symdef->name) == 0)
3630 {
3631 result = is_global_data_symbol_definition (abfd, isym);
3632 break;
3633 }
3634 }
3635
3636 free (isymbuf);
3637
3638 return result;
3639 }
3640 \f
3641 /* Add an entry to the .dynamic table. */
3642
3643 bool
3644 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3645 bfd_vma tag,
3646 bfd_vma val)
3647 {
3648 struct elf_link_hash_table *hash_table;
3649 const struct elf_backend_data *bed;
3650 asection *s;
3651 bfd_size_type newsize;
3652 bfd_byte *newcontents;
3653 Elf_Internal_Dyn dyn;
3654
3655 hash_table = elf_hash_table (info);
3656 if (! is_elf_hash_table (&hash_table->root))
3657 return false;
3658
3659 if (tag == DT_RELA || tag == DT_REL)
3660 hash_table->dynamic_relocs = true;
3661
3662 bed = get_elf_backend_data (hash_table->dynobj);
3663 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3664 BFD_ASSERT (s != NULL);
3665
3666 newsize = s->size + bed->s->sizeof_dyn;
3667 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3668 if (newcontents == NULL)
3669 return false;
3670
3671 dyn.d_tag = tag;
3672 dyn.d_un.d_val = val;
3673 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3674
3675 s->size = newsize;
3676 s->contents = newcontents;
3677
3678 return true;
3679 }
3680
3681 /* Strip zero-sized dynamic sections. */
3682
3683 bool
3684 _bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info *info)
3685 {
3686 struct elf_link_hash_table *hash_table;
3687 const struct elf_backend_data *bed;
3688 asection *s, *sdynamic, **pp;
3689 asection *rela_dyn, *rel_dyn;
3690 Elf_Internal_Dyn dyn;
3691 bfd_byte *extdyn, *next;
3692 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3693 bool strip_zero_sized;
3694 bool strip_zero_sized_plt;
3695
3696 if (bfd_link_relocatable (info))
3697 return true;
3698
3699 hash_table = elf_hash_table (info);
3700 if (!is_elf_hash_table (&hash_table->root))
3701 return false;
3702
3703 if (!hash_table->dynobj)
3704 return true;
3705
3706 sdynamic= bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3707 if (!sdynamic)
3708 return true;
3709
3710 bed = get_elf_backend_data (hash_table->dynobj);
3711 swap_dyn_in = bed->s->swap_dyn_in;
3712
3713 strip_zero_sized = false;
3714 strip_zero_sized_plt = false;
3715
3716 /* Strip zero-sized dynamic sections. */
3717 rela_dyn = bfd_get_section_by_name (info->output_bfd, ".rela.dyn");
3718 rel_dyn = bfd_get_section_by_name (info->output_bfd, ".rel.dyn");
3719 for (pp = &info->output_bfd->sections; (s = *pp) != NULL;)
3720 if (s->size == 0
3721 && (s == rela_dyn
3722 || s == rel_dyn
3723 || s == hash_table->srelplt->output_section
3724 || s == hash_table->splt->output_section))
3725 {
3726 *pp = s->next;
3727 info->output_bfd->section_count--;
3728 strip_zero_sized = true;
3729 if (s == rela_dyn)
3730 s = rela_dyn;
3731 if (s == rel_dyn)
3732 s = rel_dyn;
3733 else if (s == hash_table->splt->output_section)
3734 {
3735 s = hash_table->splt;
3736 strip_zero_sized_plt = true;
3737 }
3738 else
3739 s = hash_table->srelplt;
3740 s->flags |= SEC_EXCLUDE;
3741 s->output_section = bfd_abs_section_ptr;
3742 }
3743 else
3744 pp = &s->next;
3745
3746 if (strip_zero_sized_plt && sdynamic->size != 0)
3747 for (extdyn = sdynamic->contents;
3748 extdyn < sdynamic->contents + sdynamic->size;
3749 extdyn = next)
3750 {
3751 next = extdyn + bed->s->sizeof_dyn;
3752 swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3753 switch (dyn.d_tag)
3754 {
3755 default:
3756 break;
3757 case DT_JMPREL:
3758 case DT_PLTRELSZ:
3759 case DT_PLTREL:
3760 /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
3761 the procedure linkage table (the .plt section) has been
3762 removed. */
3763 memmove (extdyn, next,
3764 sdynamic->size - (next - sdynamic->contents));
3765 next = extdyn;
3766 }
3767 }
3768
3769 if (strip_zero_sized)
3770 {
3771 /* Regenerate program headers. */
3772 elf_seg_map (info->output_bfd) = NULL;
3773 return _bfd_elf_map_sections_to_segments (info->output_bfd, info,
3774 NULL);
3775 }
3776
3777 return true;
3778 }
3779
3780 /* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error,
3781 1 if a DT_NEEDED tag already exists, and 0 on success. */
3782
3783 int
3784 bfd_elf_add_dt_needed_tag (bfd *abfd, struct bfd_link_info *info)
3785 {
3786 struct elf_link_hash_table *hash_table;
3787 size_t strindex;
3788 const char *soname;
3789
3790 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3791 return -1;
3792
3793 hash_table = elf_hash_table (info);
3794 soname = elf_dt_name (abfd);
3795 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, false);
3796 if (strindex == (size_t) -1)
3797 return -1;
3798
3799 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3800 {
3801 asection *sdyn;
3802 const struct elf_backend_data *bed;
3803 bfd_byte *extdyn;
3804
3805 bed = get_elf_backend_data (hash_table->dynobj);
3806 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3807 if (sdyn != NULL && sdyn->size != 0)
3808 for (extdyn = sdyn->contents;
3809 extdyn < sdyn->contents + sdyn->size;
3810 extdyn += bed->s->sizeof_dyn)
3811 {
3812 Elf_Internal_Dyn dyn;
3813
3814 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3815 if (dyn.d_tag == DT_NEEDED
3816 && dyn.d_un.d_val == strindex)
3817 {
3818 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3819 return 1;
3820 }
3821 }
3822 }
3823
3824 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3825 return -1;
3826
3827 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3828 return -1;
3829
3830 return 0;
3831 }
3832
3833 /* Return true if SONAME is on the needed list between NEEDED and STOP
3834 (or the end of list if STOP is NULL), and needed by a library that
3835 will be loaded. */
3836
3837 static bool
3838 on_needed_list (const char *soname,
3839 struct bfd_link_needed_list *needed,
3840 struct bfd_link_needed_list *stop)
3841 {
3842 struct bfd_link_needed_list *look;
3843 for (look = needed; look != stop; look = look->next)
3844 if (strcmp (soname, look->name) == 0
3845 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3846 /* If needed by a library that itself is not directly
3847 needed, recursively check whether that library is
3848 indirectly needed. Since we add DT_NEEDED entries to
3849 the end of the list, library dependencies appear after
3850 the library. Therefore search prior to the current
3851 LOOK, preventing possible infinite recursion. */
3852 || on_needed_list (elf_dt_name (look->by), needed, look)))
3853 return true;
3854
3855 return false;
3856 }
3857
3858 /* Sort symbol by value, section, size, and type. */
3859 static int
3860 elf_sort_symbol (const void *arg1, const void *arg2)
3861 {
3862 const struct elf_link_hash_entry *h1;
3863 const struct elf_link_hash_entry *h2;
3864 bfd_signed_vma vdiff;
3865 int sdiff;
3866 const char *n1;
3867 const char *n2;
3868
3869 h1 = *(const struct elf_link_hash_entry **) arg1;
3870 h2 = *(const struct elf_link_hash_entry **) arg2;
3871 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3872 if (vdiff != 0)
3873 return vdiff > 0 ? 1 : -1;
3874
3875 sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3876 if (sdiff != 0)
3877 return sdiff;
3878
3879 /* Sort so that sized symbols are selected over zero size symbols. */
3880 vdiff = h1->size - h2->size;
3881 if (vdiff != 0)
3882 return vdiff > 0 ? 1 : -1;
3883
3884 /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */
3885 if (h1->type != h2->type)
3886 return h1->type - h2->type;
3887
3888 /* If symbols are properly sized and typed, and multiple strong
3889 aliases are not defined in a shared library by the user we
3890 shouldn't get here. Unfortunately linker script symbols like
3891 __bss_start sometimes match a user symbol defined at the start of
3892 .bss without proper size and type. We'd like to preference the
3893 user symbol over reserved system symbols. Sort on leading
3894 underscores. */
3895 n1 = h1->root.root.string;
3896 n2 = h2->root.root.string;
3897 while (*n1 == *n2)
3898 {
3899 if (*n1 == 0)
3900 break;
3901 ++n1;
3902 ++n2;
3903 }
3904 if (*n1 == '_')
3905 return -1;
3906 if (*n2 == '_')
3907 return 1;
3908
3909 /* Final sort on name selects user symbols like '_u' over reserved
3910 system symbols like '_Z' and also will avoid qsort instability. */
3911 return *n1 - *n2;
3912 }
3913
3914 /* This function is used to adjust offsets into .dynstr for
3915 dynamic symbols. This is called via elf_link_hash_traverse. */
3916
3917 static bool
3918 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3919 {
3920 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3921
3922 if (h->dynindx != -1)
3923 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3924 return true;
3925 }
3926
3927 /* Assign string offsets in .dynstr, update all structures referencing
3928 them. */
3929
3930 static bool
3931 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3932 {
3933 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3934 struct elf_link_local_dynamic_entry *entry;
3935 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3936 bfd *dynobj = hash_table->dynobj;
3937 asection *sdyn;
3938 bfd_size_type size;
3939 const struct elf_backend_data *bed;
3940 bfd_byte *extdyn;
3941
3942 _bfd_elf_strtab_finalize (dynstr);
3943 size = _bfd_elf_strtab_size (dynstr);
3944
3945 /* Allow the linker to examine the dynsymtab now it's fully populated. */
3946
3947 if (info->callbacks->examine_strtab)
3948 info->callbacks->examine_strtab (dynstr);
3949
3950 bed = get_elf_backend_data (dynobj);
3951 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3952 BFD_ASSERT (sdyn != NULL);
3953
3954 /* Update all .dynamic entries referencing .dynstr strings. */
3955 for (extdyn = sdyn->contents;
3956 extdyn < PTR_ADD (sdyn->contents, sdyn->size);
3957 extdyn += bed->s->sizeof_dyn)
3958 {
3959 Elf_Internal_Dyn dyn;
3960
3961 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3962 switch (dyn.d_tag)
3963 {
3964 case DT_STRSZ:
3965 dyn.d_un.d_val = size;
3966 break;
3967 case DT_NEEDED:
3968 case DT_SONAME:
3969 case DT_RPATH:
3970 case DT_RUNPATH:
3971 case DT_FILTER:
3972 case DT_AUXILIARY:
3973 case DT_AUDIT:
3974 case DT_DEPAUDIT:
3975 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3976 break;
3977 default:
3978 continue;
3979 }
3980 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3981 }
3982
3983 /* Now update local dynamic symbols. */
3984 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3985 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3986 entry->isym.st_name);
3987
3988 /* And the rest of dynamic symbols. */
3989 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3990
3991 /* Adjust version definitions. */
3992 if (elf_tdata (output_bfd)->cverdefs)
3993 {
3994 asection *s;
3995 bfd_byte *p;
3996 size_t i;
3997 Elf_Internal_Verdef def;
3998 Elf_Internal_Verdaux defaux;
3999
4000 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
4001 p = s->contents;
4002 do
4003 {
4004 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
4005 &def);
4006 p += sizeof (Elf_External_Verdef);
4007 if (def.vd_aux != sizeof (Elf_External_Verdef))
4008 continue;
4009 for (i = 0; i < def.vd_cnt; ++i)
4010 {
4011 _bfd_elf_swap_verdaux_in (output_bfd,
4012 (Elf_External_Verdaux *) p, &defaux);
4013 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
4014 defaux.vda_name);
4015 _bfd_elf_swap_verdaux_out (output_bfd,
4016 &defaux, (Elf_External_Verdaux *) p);
4017 p += sizeof (Elf_External_Verdaux);
4018 }
4019 }
4020 while (def.vd_next);
4021 }
4022
4023 /* Adjust version references. */
4024 if (elf_tdata (output_bfd)->verref)
4025 {
4026 asection *s;
4027 bfd_byte *p;
4028 size_t i;
4029 Elf_Internal_Verneed need;
4030 Elf_Internal_Vernaux needaux;
4031
4032 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
4033 p = s->contents;
4034 do
4035 {
4036 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
4037 &need);
4038 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
4039 _bfd_elf_swap_verneed_out (output_bfd, &need,
4040 (Elf_External_Verneed *) p);
4041 p += sizeof (Elf_External_Verneed);
4042 for (i = 0; i < need.vn_cnt; ++i)
4043 {
4044 _bfd_elf_swap_vernaux_in (output_bfd,
4045 (Elf_External_Vernaux *) p, &needaux);
4046 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
4047 needaux.vna_name);
4048 _bfd_elf_swap_vernaux_out (output_bfd,
4049 &needaux,
4050 (Elf_External_Vernaux *) p);
4051 p += sizeof (Elf_External_Vernaux);
4052 }
4053 }
4054 while (need.vn_next);
4055 }
4056
4057 return true;
4058 }
4059 \f
4060 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4061 The default is to only match when the INPUT and OUTPUT are exactly
4062 the same target. */
4063
4064 bool
4065 _bfd_elf_default_relocs_compatible (const bfd_target *input,
4066 const bfd_target *output)
4067 {
4068 return input == output;
4069 }
4070
4071 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4072 This version is used when different targets for the same architecture
4073 are virtually identical. */
4074
4075 bool
4076 _bfd_elf_relocs_compatible (const bfd_target *input,
4077 const bfd_target *output)
4078 {
4079 const struct elf_backend_data *obed, *ibed;
4080
4081 if (input == output)
4082 return true;
4083
4084 ibed = xvec_get_elf_backend_data (input);
4085 obed = xvec_get_elf_backend_data (output);
4086
4087 if (ibed->arch != obed->arch)
4088 return false;
4089
4090 /* If both backends are using this function, deem them compatible. */
4091 return ibed->relocs_compatible == obed->relocs_compatible;
4092 }
4093
4094 /* Make a special call to the linker "notice" function to tell it that
4095 we are about to handle an as-needed lib, or have finished
4096 processing the lib. */
4097
4098 bool
4099 _bfd_elf_notice_as_needed (bfd *ibfd,
4100 struct bfd_link_info *info,
4101 enum notice_asneeded_action act)
4102 {
4103 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
4104 }
4105
4106 /* Call ACTION on each relocation in an ELF object file. */
4107
4108 bool
4109 _bfd_elf_link_iterate_on_relocs
4110 (bfd *abfd, struct bfd_link_info *info,
4111 bool (*action) (bfd *, struct bfd_link_info *, asection *,
4112 const Elf_Internal_Rela *))
4113 {
4114 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4115 struct elf_link_hash_table *htab = elf_hash_table (info);
4116
4117 /* If this object is the same format as the output object, and it is
4118 not a shared library, then let the backend look through the
4119 relocs.
4120
4121 This is required to build global offset table entries and to
4122 arrange for dynamic relocs. It is not required for the
4123 particular common case of linking non PIC code, even when linking
4124 against shared libraries, but unfortunately there is no way of
4125 knowing whether an object file has been compiled PIC or not.
4126 Looking through the relocs is not particularly time consuming.
4127 The problem is that we must either (1) keep the relocs in memory,
4128 which causes the linker to require additional runtime memory or
4129 (2) read the relocs twice from the input file, which wastes time.
4130 This would be a good case for using mmap.
4131
4132 I have no idea how to handle linking PIC code into a file of a
4133 different format. It probably can't be done. */
4134 if ((abfd->flags & DYNAMIC) == 0
4135 && is_elf_hash_table (&htab->root)
4136 && elf_object_id (abfd) == elf_hash_table_id (htab)
4137 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4138 {
4139 asection *o;
4140
4141 for (o = abfd->sections; o != NULL; o = o->next)
4142 {
4143 Elf_Internal_Rela *internal_relocs;
4144 bool ok;
4145
4146 /* Don't check relocations in excluded sections. Don't do
4147 anything special with non-loaded, non-alloced sections.
4148 In particular, any relocs in such sections should not
4149 affect GOT and PLT reference counting (ie. we don't
4150 allow them to create GOT or PLT entries), there's no
4151 possibility or desire to optimize TLS relocs, and
4152 there's not much point in propagating relocs to shared
4153 libs that the dynamic linker won't relocate. */
4154 if ((o->flags & SEC_ALLOC) == 0
4155 || (o->flags & SEC_RELOC) == 0
4156 || (o->flags & SEC_EXCLUDE) != 0
4157 || o->reloc_count == 0
4158 || ((info->strip == strip_all || info->strip == strip_debugger)
4159 && (o->flags & SEC_DEBUGGING) != 0)
4160 || bfd_is_abs_section (o->output_section))
4161 continue;
4162
4163 internal_relocs = _bfd_elf_link_info_read_relocs (abfd, info,
4164 o, NULL,
4165 NULL,
4166 _bfd_link_keep_memory (info));
4167 if (internal_relocs == NULL)
4168 return false;
4169
4170 ok = action (abfd, info, o, internal_relocs);
4171
4172 if (elf_section_data (o)->relocs != internal_relocs)
4173 free (internal_relocs);
4174
4175 if (! ok)
4176 return false;
4177 }
4178 }
4179
4180 return true;
4181 }
4182
4183 /* Check relocations in an ELF object file. This is called after
4184 all input files have been opened. */
4185
4186 bool
4187 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
4188 {
4189 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4190 if (bed->check_relocs != NULL)
4191 return _bfd_elf_link_iterate_on_relocs (abfd, info,
4192 bed->check_relocs);
4193 return true;
4194 }
4195
4196 /* Add symbols from an ELF object file to the linker hash table. */
4197
4198 static bool
4199 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
4200 {
4201 Elf_Internal_Ehdr *ehdr;
4202 Elf_Internal_Shdr *hdr;
4203 size_t symcount;
4204 size_t extsymcount;
4205 size_t extsymoff;
4206 struct elf_link_hash_entry **sym_hash;
4207 bool dynamic;
4208 Elf_External_Versym *extversym = NULL;
4209 Elf_External_Versym *extversym_end = NULL;
4210 Elf_External_Versym *ever;
4211 struct elf_link_hash_entry *weaks;
4212 struct elf_link_hash_entry **nondeflt_vers = NULL;
4213 size_t nondeflt_vers_cnt = 0;
4214 Elf_Internal_Sym *isymbuf = NULL;
4215 Elf_Internal_Sym *isym;
4216 Elf_Internal_Sym *isymend;
4217 const struct elf_backend_data *bed;
4218 bool add_needed;
4219 struct elf_link_hash_table *htab;
4220 void *alloc_mark = NULL;
4221 struct bfd_hash_entry **old_table = NULL;
4222 unsigned int old_size = 0;
4223 unsigned int old_count = 0;
4224 void *old_tab = NULL;
4225 void *old_ent;
4226 struct bfd_link_hash_entry *old_undefs = NULL;
4227 struct bfd_link_hash_entry *old_undefs_tail = NULL;
4228 void *old_strtab = NULL;
4229 size_t tabsize = 0;
4230 asection *s;
4231 bool just_syms;
4232
4233 htab = elf_hash_table (info);
4234 bed = get_elf_backend_data (abfd);
4235
4236 if ((abfd->flags & DYNAMIC) == 0)
4237 dynamic = false;
4238 else
4239 {
4240 dynamic = true;
4241
4242 /* You can't use -r against a dynamic object. Also, there's no
4243 hope of using a dynamic object which does not exactly match
4244 the format of the output file. */
4245 if (bfd_link_relocatable (info)
4246 || !is_elf_hash_table (&htab->root)
4247 || info->output_bfd->xvec != abfd->xvec)
4248 {
4249 if (bfd_link_relocatable (info))
4250 bfd_set_error (bfd_error_invalid_operation);
4251 else
4252 bfd_set_error (bfd_error_wrong_format);
4253 goto error_return;
4254 }
4255 }
4256
4257 ehdr = elf_elfheader (abfd);
4258 if (info->warn_alternate_em
4259 && bed->elf_machine_code != ehdr->e_machine
4260 && ((bed->elf_machine_alt1 != 0
4261 && ehdr->e_machine == bed->elf_machine_alt1)
4262 || (bed->elf_machine_alt2 != 0
4263 && ehdr->e_machine == bed->elf_machine_alt2)))
4264 _bfd_error_handler
4265 /* xgettext:c-format */
4266 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
4267 ehdr->e_machine, abfd, bed->elf_machine_code);
4268
4269 /* As a GNU extension, any input sections which are named
4270 .gnu.warning.SYMBOL are treated as warning symbols for the given
4271 symbol. This differs from .gnu.warning sections, which generate
4272 warnings when they are included in an output file. */
4273 /* PR 12761: Also generate this warning when building shared libraries. */
4274 for (s = abfd->sections; s != NULL; s = s->next)
4275 {
4276 const char *name;
4277
4278 name = bfd_section_name (s);
4279 if (startswith (name, ".gnu.warning."))
4280 {
4281 char *msg;
4282 bfd_size_type sz;
4283
4284 name += sizeof ".gnu.warning." - 1;
4285
4286 /* If this is a shared object, then look up the symbol
4287 in the hash table. If it is there, and it is already
4288 been defined, then we will not be using the entry
4289 from this shared object, so we don't need to warn.
4290 FIXME: If we see the definition in a regular object
4291 later on, we will warn, but we shouldn't. The only
4292 fix is to keep track of what warnings we are supposed
4293 to emit, and then handle them all at the end of the
4294 link. */
4295 if (dynamic)
4296 {
4297 struct elf_link_hash_entry *h;
4298
4299 h = elf_link_hash_lookup (htab, name, false, false, true);
4300
4301 /* FIXME: What about bfd_link_hash_common? */
4302 if (h != NULL
4303 && (h->root.type == bfd_link_hash_defined
4304 || h->root.type == bfd_link_hash_defweak))
4305 continue;
4306 }
4307
4308 sz = s->size;
4309 msg = (char *) bfd_alloc (abfd, sz + 1);
4310 if (msg == NULL)
4311 goto error_return;
4312
4313 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
4314 goto error_return;
4315
4316 msg[sz] = '\0';
4317
4318 if (! (_bfd_generic_link_add_one_symbol
4319 (info, abfd, name, BSF_WARNING, s, 0, msg,
4320 false, bed->collect, NULL)))
4321 goto error_return;
4322
4323 if (bfd_link_executable (info))
4324 {
4325 /* Clobber the section size so that the warning does
4326 not get copied into the output file. */
4327 s->size = 0;
4328
4329 /* Also set SEC_EXCLUDE, so that symbols defined in
4330 the warning section don't get copied to the output. */
4331 s->flags |= SEC_EXCLUDE;
4332 }
4333 }
4334 }
4335
4336 just_syms = ((s = abfd->sections) != NULL
4337 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
4338
4339 add_needed = true;
4340 if (! dynamic)
4341 {
4342 /* If we are creating a shared library, create all the dynamic
4343 sections immediately. We need to attach them to something,
4344 so we attach them to this BFD, provided it is the right
4345 format and is not from ld --just-symbols. Always create the
4346 dynamic sections for -E/--dynamic-list. FIXME: If there
4347 are no input BFD's of the same format as the output, we can't
4348 make a shared library. */
4349 if (!just_syms
4350 && (bfd_link_pic (info)
4351 || (!bfd_link_relocatable (info)
4352 && info->nointerp
4353 && (info->export_dynamic || info->dynamic)))
4354 && is_elf_hash_table (&htab->root)
4355 && info->output_bfd->xvec == abfd->xvec
4356 && !htab->dynamic_sections_created)
4357 {
4358 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4359 goto error_return;
4360 }
4361 }
4362 else if (!is_elf_hash_table (&htab->root))
4363 goto error_return;
4364 else
4365 {
4366 const char *soname = NULL;
4367 char *audit = NULL;
4368 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
4369 const Elf_Internal_Phdr *phdr;
4370 struct elf_link_loaded_list *loaded_lib;
4371
4372 /* ld --just-symbols and dynamic objects don't mix very well.
4373 ld shouldn't allow it. */
4374 if (just_syms)
4375 abort ();
4376
4377 /* If this dynamic lib was specified on the command line with
4378 --as-needed in effect, then we don't want to add a DT_NEEDED
4379 tag unless the lib is actually used. Similary for libs brought
4380 in by another lib's DT_NEEDED. When --no-add-needed is used
4381 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4382 any dynamic library in DT_NEEDED tags in the dynamic lib at
4383 all. */
4384 add_needed = (elf_dyn_lib_class (abfd)
4385 & (DYN_AS_NEEDED | DYN_DT_NEEDED
4386 | DYN_NO_NEEDED)) == 0;
4387
4388 s = bfd_get_section_by_name (abfd, ".dynamic");
4389 if (s != NULL && s->size != 0)
4390 {
4391 bfd_byte *dynbuf;
4392 bfd_byte *extdyn;
4393 unsigned int elfsec;
4394 unsigned long shlink;
4395
4396 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
4397 {
4398 error_free_dyn:
4399 free (dynbuf);
4400 goto error_return;
4401 }
4402
4403 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
4404 if (elfsec == SHN_BAD)
4405 goto error_free_dyn;
4406 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4407
4408 for (extdyn = dynbuf;
4409 extdyn <= dynbuf + s->size - bed->s->sizeof_dyn;
4410 extdyn += bed->s->sizeof_dyn)
4411 {
4412 Elf_Internal_Dyn dyn;
4413
4414 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4415 if (dyn.d_tag == DT_SONAME)
4416 {
4417 unsigned int tagv = dyn.d_un.d_val;
4418 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4419 if (soname == NULL)
4420 goto error_free_dyn;
4421 }
4422 if (dyn.d_tag == DT_NEEDED)
4423 {
4424 struct bfd_link_needed_list *n, **pn;
4425 char *fnm, *anm;
4426 unsigned int tagv = dyn.d_un.d_val;
4427 size_t amt = sizeof (struct bfd_link_needed_list);
4428
4429 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4430 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4431 if (n == NULL || fnm == NULL)
4432 goto error_free_dyn;
4433 amt = strlen (fnm) + 1;
4434 anm = (char *) bfd_alloc (abfd, amt);
4435 if (anm == NULL)
4436 goto error_free_dyn;
4437 memcpy (anm, fnm, amt);
4438 n->name = anm;
4439 n->by = abfd;
4440 n->next = NULL;
4441 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4442 ;
4443 *pn = n;
4444 }
4445 if (dyn.d_tag == DT_RUNPATH)
4446 {
4447 struct bfd_link_needed_list *n, **pn;
4448 char *fnm, *anm;
4449 unsigned int tagv = dyn.d_un.d_val;
4450 size_t amt = sizeof (struct bfd_link_needed_list);
4451
4452 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4453 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4454 if (n == NULL || fnm == NULL)
4455 goto error_free_dyn;
4456 amt = strlen (fnm) + 1;
4457 anm = (char *) bfd_alloc (abfd, amt);
4458 if (anm == NULL)
4459 goto error_free_dyn;
4460 memcpy (anm, fnm, amt);
4461 n->name = anm;
4462 n->by = abfd;
4463 n->next = NULL;
4464 for (pn = & runpath;
4465 *pn != NULL;
4466 pn = &(*pn)->next)
4467 ;
4468 *pn = n;
4469 }
4470 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4471 if (!runpath && dyn.d_tag == DT_RPATH)
4472 {
4473 struct bfd_link_needed_list *n, **pn;
4474 char *fnm, *anm;
4475 unsigned int tagv = dyn.d_un.d_val;
4476 size_t amt = sizeof (struct bfd_link_needed_list);
4477
4478 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4479 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4480 if (n == NULL || fnm == NULL)
4481 goto error_free_dyn;
4482 amt = strlen (fnm) + 1;
4483 anm = (char *) bfd_alloc (abfd, amt);
4484 if (anm == NULL)
4485 goto error_free_dyn;
4486 memcpy (anm, fnm, amt);
4487 n->name = anm;
4488 n->by = abfd;
4489 n->next = NULL;
4490 for (pn = & rpath;
4491 *pn != NULL;
4492 pn = &(*pn)->next)
4493 ;
4494 *pn = n;
4495 }
4496 if (dyn.d_tag == DT_AUDIT)
4497 {
4498 unsigned int tagv = dyn.d_un.d_val;
4499 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4500 }
4501 if (dyn.d_tag == DT_FLAGS_1)
4502 elf_tdata (abfd)->is_pie = (dyn.d_un.d_val & DF_1_PIE) != 0;
4503 }
4504
4505 free (dynbuf);
4506 }
4507
4508 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4509 frees all more recently bfd_alloc'd blocks as well. */
4510 if (runpath)
4511 rpath = runpath;
4512
4513 if (rpath)
4514 {
4515 struct bfd_link_needed_list **pn;
4516 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4517 ;
4518 *pn = rpath;
4519 }
4520
4521 /* If we have a PT_GNU_RELRO program header, mark as read-only
4522 all sections contained fully therein. This makes relro
4523 shared library sections appear as they will at run-time. */
4524 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4525 while (phdr-- > elf_tdata (abfd)->phdr)
4526 if (phdr->p_type == PT_GNU_RELRO)
4527 {
4528 for (s = abfd->sections; s != NULL; s = s->next)
4529 {
4530 unsigned int opb = bfd_octets_per_byte (abfd, s);
4531
4532 if ((s->flags & SEC_ALLOC) != 0
4533 && s->vma * opb >= phdr->p_vaddr
4534 && s->vma * opb + s->size <= phdr->p_vaddr + phdr->p_memsz)
4535 s->flags |= SEC_READONLY;
4536 }
4537 break;
4538 }
4539
4540 /* We do not want to include any of the sections in a dynamic
4541 object in the output file. We hack by simply clobbering the
4542 list of sections in the BFD. This could be handled more
4543 cleanly by, say, a new section flag; the existing
4544 SEC_NEVER_LOAD flag is not the one we want, because that one
4545 still implies that the section takes up space in the output
4546 file. */
4547 bfd_section_list_clear (abfd);
4548
4549 /* Find the name to use in a DT_NEEDED entry that refers to this
4550 object. If the object has a DT_SONAME entry, we use it.
4551 Otherwise, if the generic linker stuck something in
4552 elf_dt_name, we use that. Otherwise, we just use the file
4553 name. */
4554 if (soname == NULL || *soname == '\0')
4555 {
4556 soname = elf_dt_name (abfd);
4557 if (soname == NULL || *soname == '\0')
4558 soname = bfd_get_filename (abfd);
4559 }
4560
4561 /* Save the SONAME because sometimes the linker emulation code
4562 will need to know it. */
4563 elf_dt_name (abfd) = soname;
4564
4565 /* If we have already included this dynamic object in the
4566 link, just ignore it. There is no reason to include a
4567 particular dynamic object more than once. */
4568 for (loaded_lib = htab->dyn_loaded;
4569 loaded_lib != NULL;
4570 loaded_lib = loaded_lib->next)
4571 {
4572 if (strcmp (elf_dt_name (loaded_lib->abfd), soname) == 0)
4573 return true;
4574 }
4575
4576 /* Create dynamic sections for backends that require that be done
4577 before setup_gnu_properties. */
4578 if (add_needed
4579 && !_bfd_elf_link_create_dynamic_sections (abfd, info))
4580 return false;
4581
4582 /* Save the DT_AUDIT entry for the linker emulation code. */
4583 elf_dt_audit (abfd) = audit;
4584 }
4585
4586 /* If this is a dynamic object, we always link against the .dynsym
4587 symbol table, not the .symtab symbol table. The dynamic linker
4588 will only see the .dynsym symbol table, so there is no reason to
4589 look at .symtab for a dynamic object. */
4590
4591 if (! dynamic || elf_dynsymtab (abfd) == 0)
4592 hdr = &elf_tdata (abfd)->symtab_hdr;
4593 else
4594 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4595
4596 symcount = hdr->sh_size / bed->s->sizeof_sym;
4597
4598 /* The sh_info field of the symtab header tells us where the
4599 external symbols start. We don't care about the local symbols at
4600 this point. */
4601 if (elf_bad_symtab (abfd))
4602 {
4603 extsymcount = symcount;
4604 extsymoff = 0;
4605 }
4606 else
4607 {
4608 extsymcount = symcount - hdr->sh_info;
4609 extsymoff = hdr->sh_info;
4610 }
4611
4612 sym_hash = elf_sym_hashes (abfd);
4613 if (extsymcount != 0)
4614 {
4615 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4616 NULL, NULL, NULL);
4617 if (isymbuf == NULL)
4618 goto error_return;
4619
4620 if (sym_hash == NULL)
4621 {
4622 /* We store a pointer to the hash table entry for each
4623 external symbol. */
4624 size_t amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4625 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4626 if (sym_hash == NULL)
4627 goto error_free_sym;
4628 elf_sym_hashes (abfd) = sym_hash;
4629 }
4630 }
4631
4632 if (dynamic)
4633 {
4634 /* Read in any version definitions. */
4635 if (!_bfd_elf_slurp_version_tables (abfd,
4636 info->default_imported_symver))
4637 goto error_free_sym;
4638
4639 /* Read in the symbol versions, but don't bother to convert them
4640 to internal format. */
4641 if (elf_dynversym (abfd) != 0)
4642 {
4643 Elf_Internal_Shdr *versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4644 bfd_size_type amt = versymhdr->sh_size;
4645
4646 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0)
4647 goto error_free_sym;
4648 extversym = (Elf_External_Versym *)
4649 _bfd_malloc_and_read (abfd, amt, amt);
4650 if (extversym == NULL)
4651 goto error_free_sym;
4652 extversym_end = extversym + amt / sizeof (*extversym);
4653 }
4654 }
4655
4656 /* If we are loading an as-needed shared lib, save the symbol table
4657 state before we start adding symbols. If the lib turns out
4658 to be unneeded, restore the state. */
4659 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4660 {
4661 unsigned int i;
4662 size_t entsize;
4663
4664 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4665 {
4666 struct bfd_hash_entry *p;
4667 struct elf_link_hash_entry *h;
4668
4669 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4670 {
4671 h = (struct elf_link_hash_entry *) p;
4672 entsize += htab->root.table.entsize;
4673 if (h->root.type == bfd_link_hash_warning)
4674 {
4675 entsize += htab->root.table.entsize;
4676 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4677 }
4678 if (h->root.type == bfd_link_hash_common)
4679 entsize += sizeof (*h->root.u.c.p);
4680 }
4681 }
4682
4683 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4684 old_tab = bfd_malloc (tabsize + entsize);
4685 if (old_tab == NULL)
4686 goto error_free_vers;
4687
4688 /* Remember the current objalloc pointer, so that all mem for
4689 symbols added can later be reclaimed. */
4690 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4691 if (alloc_mark == NULL)
4692 goto error_free_vers;
4693
4694 /* Make a special call to the linker "notice" function to
4695 tell it that we are about to handle an as-needed lib. */
4696 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4697 goto error_free_vers;
4698
4699 /* Clone the symbol table. Remember some pointers into the
4700 symbol table, and dynamic symbol count. */
4701 old_ent = (char *) old_tab + tabsize;
4702 memcpy (old_tab, htab->root.table.table, tabsize);
4703 old_undefs = htab->root.undefs;
4704 old_undefs_tail = htab->root.undefs_tail;
4705 old_table = htab->root.table.table;
4706 old_size = htab->root.table.size;
4707 old_count = htab->root.table.count;
4708 old_strtab = NULL;
4709 if (htab->dynstr != NULL)
4710 {
4711 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4712 if (old_strtab == NULL)
4713 goto error_free_vers;
4714 }
4715
4716 for (i = 0; i < htab->root.table.size; i++)
4717 {
4718 struct bfd_hash_entry *p;
4719 struct elf_link_hash_entry *h;
4720
4721 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4722 {
4723 h = (struct elf_link_hash_entry *) p;
4724 memcpy (old_ent, h, htab->root.table.entsize);
4725 old_ent = (char *) old_ent + htab->root.table.entsize;
4726 if (h->root.type == bfd_link_hash_warning)
4727 {
4728 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4729 memcpy (old_ent, h, htab->root.table.entsize);
4730 old_ent = (char *) old_ent + htab->root.table.entsize;
4731 }
4732 if (h->root.type == bfd_link_hash_common)
4733 {
4734 memcpy (old_ent, h->root.u.c.p, sizeof (*h->root.u.c.p));
4735 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
4736 }
4737 }
4738 }
4739 }
4740
4741 weaks = NULL;
4742 if (extversym == NULL)
4743 ever = NULL;
4744 else if (extversym + extsymoff < extversym_end)
4745 ever = extversym + extsymoff;
4746 else
4747 {
4748 /* xgettext:c-format */
4749 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4750 abfd, (long) extsymoff,
4751 (long) (extversym_end - extversym) / sizeof (* extversym));
4752 bfd_set_error (bfd_error_bad_value);
4753 goto error_free_vers;
4754 }
4755
4756 if (!bfd_link_relocatable (info)
4757 && abfd->lto_slim_object)
4758 {
4759 _bfd_error_handler
4760 (_("%pB: plugin needed to handle lto object"), abfd);
4761 }
4762
4763 for (isym = isymbuf, isymend = PTR_ADD (isymbuf, extsymcount);
4764 isym < isymend;
4765 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4766 {
4767 int bind;
4768 bfd_vma value;
4769 asection *sec, *new_sec;
4770 flagword flags;
4771 const char *name;
4772 struct elf_link_hash_entry *h;
4773 struct elf_link_hash_entry *hi;
4774 bool definition;
4775 bool size_change_ok;
4776 bool type_change_ok;
4777 bool new_weak;
4778 bool old_weak;
4779 bfd *override;
4780 bool common;
4781 bool discarded;
4782 unsigned int old_alignment;
4783 unsigned int shindex;
4784 bfd *old_bfd;
4785 bool matched;
4786
4787 override = NULL;
4788
4789 flags = BSF_NO_FLAGS;
4790 sec = NULL;
4791 value = isym->st_value;
4792 common = bed->common_definition (isym);
4793 if (common && info->inhibit_common_definition)
4794 {
4795 /* Treat common symbol as undefined for --no-define-common. */
4796 isym->st_shndx = SHN_UNDEF;
4797 common = false;
4798 }
4799 discarded = false;
4800
4801 bind = ELF_ST_BIND (isym->st_info);
4802 switch (bind)
4803 {
4804 case STB_LOCAL:
4805 /* This should be impossible, since ELF requires that all
4806 global symbols follow all local symbols, and that sh_info
4807 point to the first global symbol. Unfortunately, Irix 5
4808 screws this up. */
4809 if (elf_bad_symtab (abfd))
4810 continue;
4811
4812 /* If we aren't prepared to handle locals within the globals
4813 then we'll likely segfault on a NULL symbol hash if the
4814 symbol is ever referenced in relocations. */
4815 shindex = elf_elfheader (abfd)->e_shstrndx;
4816 name = bfd_elf_string_from_elf_section (abfd, shindex, hdr->sh_name);
4817 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4818 " (>= sh_info of %lu)"),
4819 abfd, name, (long) (isym - isymbuf + extsymoff),
4820 (long) extsymoff);
4821
4822 /* Dynamic object relocations are not processed by ld, so
4823 ld won't run into the problem mentioned above. */
4824 if (dynamic)
4825 continue;
4826 bfd_set_error (bfd_error_bad_value);
4827 goto error_free_vers;
4828
4829 case STB_GLOBAL:
4830 if (isym->st_shndx != SHN_UNDEF && !common)
4831 flags = BSF_GLOBAL;
4832 break;
4833
4834 case STB_WEAK:
4835 flags = BSF_WEAK;
4836 break;
4837
4838 case STB_GNU_UNIQUE:
4839 flags = BSF_GNU_UNIQUE;
4840 break;
4841
4842 default:
4843 /* Leave it up to the processor backend. */
4844 break;
4845 }
4846
4847 if (isym->st_shndx == SHN_UNDEF)
4848 sec = bfd_und_section_ptr;
4849 else if (isym->st_shndx == SHN_ABS)
4850 sec = bfd_abs_section_ptr;
4851 else if (isym->st_shndx == SHN_COMMON)
4852 {
4853 sec = bfd_com_section_ptr;
4854 /* What ELF calls the size we call the value. What ELF
4855 calls the value we call the alignment. */
4856 value = isym->st_size;
4857 }
4858 else
4859 {
4860 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4861 if (sec == NULL)
4862 sec = bfd_abs_section_ptr;
4863 else if (discarded_section (sec))
4864 {
4865 /* Symbols from discarded section are undefined. We keep
4866 its visibility. */
4867 sec = bfd_und_section_ptr;
4868 discarded = true;
4869 isym->st_shndx = SHN_UNDEF;
4870 }
4871 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4872 value -= sec->vma;
4873 }
4874
4875 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4876 isym->st_name);
4877 if (name == NULL)
4878 goto error_free_vers;
4879
4880 if (isym->st_shndx == SHN_COMMON
4881 && (abfd->flags & BFD_PLUGIN) != 0)
4882 {
4883 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4884
4885 if (xc == NULL)
4886 {
4887 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4888 | SEC_EXCLUDE);
4889 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4890 if (xc == NULL)
4891 goto error_free_vers;
4892 }
4893 sec = xc;
4894 }
4895 else if (isym->st_shndx == SHN_COMMON
4896 && ELF_ST_TYPE (isym->st_info) == STT_TLS
4897 && !bfd_link_relocatable (info))
4898 {
4899 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4900
4901 if (tcomm == NULL)
4902 {
4903 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4904 | SEC_LINKER_CREATED);
4905 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4906 if (tcomm == NULL)
4907 goto error_free_vers;
4908 }
4909 sec = tcomm;
4910 }
4911 else if (bed->elf_add_symbol_hook)
4912 {
4913 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4914 &sec, &value))
4915 goto error_free_vers;
4916
4917 /* The hook function sets the name to NULL if this symbol
4918 should be skipped for some reason. */
4919 if (name == NULL)
4920 continue;
4921 }
4922
4923 /* Sanity check that all possibilities were handled. */
4924 if (sec == NULL)
4925 abort ();
4926
4927 /* Silently discard TLS symbols from --just-syms. There's
4928 no way to combine a static TLS block with a new TLS block
4929 for this executable. */
4930 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4931 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4932 continue;
4933
4934 if (bfd_is_und_section (sec)
4935 || bfd_is_com_section (sec))
4936 definition = false;
4937 else
4938 definition = true;
4939
4940 size_change_ok = false;
4941 type_change_ok = bed->type_change_ok;
4942 old_weak = false;
4943 matched = false;
4944 old_alignment = 0;
4945 old_bfd = NULL;
4946 new_sec = sec;
4947
4948 if (is_elf_hash_table (&htab->root))
4949 {
4950 Elf_Internal_Versym iver;
4951 unsigned int vernum = 0;
4952 bool skip;
4953
4954 if (ever == NULL)
4955 {
4956 if (info->default_imported_symver)
4957 /* Use the default symbol version created earlier. */
4958 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4959 else
4960 iver.vs_vers = 0;
4961 }
4962 else if (ever >= extversym_end)
4963 {
4964 /* xgettext:c-format */
4965 _bfd_error_handler (_("%pB: not enough version information"),
4966 abfd);
4967 bfd_set_error (bfd_error_bad_value);
4968 goto error_free_vers;
4969 }
4970 else
4971 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4972
4973 vernum = iver.vs_vers & VERSYM_VERSION;
4974
4975 /* If this is a hidden symbol, or if it is not version
4976 1, we append the version name to the symbol name.
4977 However, we do not modify a non-hidden absolute symbol
4978 if it is not a function, because it might be the version
4979 symbol itself. FIXME: What if it isn't? */
4980 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4981 || (vernum > 1
4982 && (!bfd_is_abs_section (sec)
4983 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4984 {
4985 const char *verstr;
4986 size_t namelen, verlen, newlen;
4987 char *newname, *p;
4988
4989 if (isym->st_shndx != SHN_UNDEF)
4990 {
4991 if (vernum > elf_tdata (abfd)->cverdefs)
4992 verstr = NULL;
4993 else if (vernum > 1)
4994 verstr =
4995 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4996 else
4997 verstr = "";
4998
4999 if (verstr == NULL)
5000 {
5001 _bfd_error_handler
5002 /* xgettext:c-format */
5003 (_("%pB: %s: invalid version %u (max %d)"),
5004 abfd, name, vernum,
5005 elf_tdata (abfd)->cverdefs);
5006 bfd_set_error (bfd_error_bad_value);
5007 goto error_free_vers;
5008 }
5009 }
5010 else
5011 {
5012 /* We cannot simply test for the number of
5013 entries in the VERNEED section since the
5014 numbers for the needed versions do not start
5015 at 0. */
5016 Elf_Internal_Verneed *t;
5017
5018 verstr = NULL;
5019 for (t = elf_tdata (abfd)->verref;
5020 t != NULL;
5021 t = t->vn_nextref)
5022 {
5023 Elf_Internal_Vernaux *a;
5024
5025 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5026 {
5027 if (a->vna_other == vernum)
5028 {
5029 verstr = a->vna_nodename;
5030 break;
5031 }
5032 }
5033 if (a != NULL)
5034 break;
5035 }
5036 if (verstr == NULL)
5037 {
5038 _bfd_error_handler
5039 /* xgettext:c-format */
5040 (_("%pB: %s: invalid needed version %d"),
5041 abfd, name, vernum);
5042 bfd_set_error (bfd_error_bad_value);
5043 goto error_free_vers;
5044 }
5045 }
5046
5047 namelen = strlen (name);
5048 verlen = strlen (verstr);
5049 newlen = namelen + verlen + 2;
5050 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
5051 && isym->st_shndx != SHN_UNDEF)
5052 ++newlen;
5053
5054 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
5055 if (newname == NULL)
5056 goto error_free_vers;
5057 memcpy (newname, name, namelen);
5058 p = newname + namelen;
5059 *p++ = ELF_VER_CHR;
5060 /* If this is a defined non-hidden version symbol,
5061 we add another @ to the name. This indicates the
5062 default version of the symbol. */
5063 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
5064 && isym->st_shndx != SHN_UNDEF)
5065 *p++ = ELF_VER_CHR;
5066 memcpy (p, verstr, verlen + 1);
5067
5068 name = newname;
5069 }
5070
5071 /* If this symbol has default visibility and the user has
5072 requested we not re-export it, then mark it as hidden. */
5073 if (!bfd_is_und_section (sec)
5074 && !dynamic
5075 && abfd->no_export
5076 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
5077 isym->st_other = (STV_HIDDEN
5078 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
5079
5080 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
5081 sym_hash, &old_bfd, &old_weak,
5082 &old_alignment, &skip, &override,
5083 &type_change_ok, &size_change_ok,
5084 &matched))
5085 goto error_free_vers;
5086
5087 if (skip)
5088 continue;
5089
5090 /* Override a definition only if the new symbol matches the
5091 existing one. */
5092 if (override && matched)
5093 definition = false;
5094
5095 h = *sym_hash;
5096 while (h->root.type == bfd_link_hash_indirect
5097 || h->root.type == bfd_link_hash_warning)
5098 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5099
5100 if (h->versioned != unversioned
5101 && elf_tdata (abfd)->verdef != NULL
5102 && vernum > 1
5103 && definition)
5104 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
5105 }
5106
5107 if (! (_bfd_generic_link_add_one_symbol
5108 (info, override ? override : abfd, name, flags, sec, value,
5109 NULL, false, bed->collect,
5110 (struct bfd_link_hash_entry **) sym_hash)))
5111 goto error_free_vers;
5112
5113 h = *sym_hash;
5114 /* We need to make sure that indirect symbol dynamic flags are
5115 updated. */
5116 hi = h;
5117 while (h->root.type == bfd_link_hash_indirect
5118 || h->root.type == bfd_link_hash_warning)
5119 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5120
5121 *sym_hash = h;
5122
5123 /* Setting the index to -3 tells elf_link_output_extsym that
5124 this symbol is defined in a discarded section. */
5125 if (discarded && is_elf_hash_table (&htab->root))
5126 h->indx = -3;
5127
5128 new_weak = (flags & BSF_WEAK) != 0;
5129 if (dynamic
5130 && definition
5131 && new_weak
5132 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
5133 && is_elf_hash_table (&htab->root)
5134 && h->u.alias == NULL)
5135 {
5136 /* Keep a list of all weak defined non function symbols from
5137 a dynamic object, using the alias field. Later in this
5138 function we will set the alias field to the correct
5139 value. We only put non-function symbols from dynamic
5140 objects on this list, because that happens to be the only
5141 time we need to know the normal symbol corresponding to a
5142 weak symbol, and the information is time consuming to
5143 figure out. If the alias field is not already NULL,
5144 then this symbol was already defined by some previous
5145 dynamic object, and we will be using that previous
5146 definition anyhow. */
5147
5148 h->u.alias = weaks;
5149 weaks = h;
5150 }
5151
5152 /* Set the alignment of a common symbol. */
5153 if ((common || bfd_is_com_section (sec))
5154 && h->root.type == bfd_link_hash_common)
5155 {
5156 unsigned int align;
5157
5158 if (common)
5159 align = bfd_log2 (isym->st_value);
5160 else
5161 {
5162 /* The new symbol is a common symbol in a shared object.
5163 We need to get the alignment from the section. */
5164 align = new_sec->alignment_power;
5165 }
5166 if (align > old_alignment)
5167 h->root.u.c.p->alignment_power = align;
5168 else
5169 h->root.u.c.p->alignment_power = old_alignment;
5170 }
5171
5172 if (is_elf_hash_table (&htab->root))
5173 {
5174 /* Set a flag in the hash table entry indicating the type of
5175 reference or definition we just found. A dynamic symbol
5176 is one which is referenced or defined by both a regular
5177 object and a shared object. */
5178 bool dynsym = false;
5179
5180 /* Plugin symbols aren't normal. Don't set def/ref flags. */
5181 if ((abfd->flags & BFD_PLUGIN) != 0)
5182 {
5183 /* Except for this flag to track nonweak references. */
5184 if (!definition
5185 && bind != STB_WEAK)
5186 h->ref_ir_nonweak = 1;
5187 }
5188 else if (!dynamic)
5189 {
5190 if (! definition)
5191 {
5192 h->ref_regular = 1;
5193 if (bind != STB_WEAK)
5194 h->ref_regular_nonweak = 1;
5195 }
5196 else
5197 {
5198 h->def_regular = 1;
5199 if (h->def_dynamic)
5200 {
5201 h->def_dynamic = 0;
5202 h->ref_dynamic = 1;
5203 }
5204 }
5205 }
5206 else
5207 {
5208 if (! definition)
5209 {
5210 h->ref_dynamic = 1;
5211 hi->ref_dynamic = 1;
5212 }
5213 else
5214 {
5215 h->def_dynamic = 1;
5216 hi->def_dynamic = 1;
5217 }
5218 }
5219
5220 /* If an indirect symbol has been forced local, don't
5221 make the real symbol dynamic. */
5222 if (h != hi && hi->forced_local)
5223 ;
5224 else if (!dynamic)
5225 {
5226 if (bfd_link_dll (info)
5227 || h->def_dynamic
5228 || h->ref_dynamic)
5229 dynsym = true;
5230 }
5231 else
5232 {
5233 if (h->def_regular
5234 || h->ref_regular
5235 || (h->is_weakalias
5236 && weakdef (h)->dynindx != -1))
5237 dynsym = true;
5238 }
5239
5240 /* Check to see if we need to add an indirect symbol for
5241 the default name. */
5242 if ((definition
5243 || (!override && h->root.type == bfd_link_hash_common))
5244 && !(hi != h
5245 && hi->versioned == versioned_hidden))
5246 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
5247 sec, value, &old_bfd, &dynsym))
5248 goto error_free_vers;
5249
5250 /* Check the alignment when a common symbol is involved. This
5251 can change when a common symbol is overridden by a normal
5252 definition or a common symbol is ignored due to the old
5253 normal definition. We need to make sure the maximum
5254 alignment is maintained. */
5255 if ((old_alignment || common)
5256 && h->root.type != bfd_link_hash_common)
5257 {
5258 unsigned int common_align;
5259 unsigned int normal_align;
5260 unsigned int symbol_align;
5261 bfd *normal_bfd;
5262 bfd *common_bfd;
5263
5264 BFD_ASSERT (h->root.type == bfd_link_hash_defined
5265 || h->root.type == bfd_link_hash_defweak);
5266
5267 symbol_align = ffs (h->root.u.def.value) - 1;
5268 if (h->root.u.def.section->owner != NULL
5269 && (h->root.u.def.section->owner->flags
5270 & (DYNAMIC | BFD_PLUGIN)) == 0)
5271 {
5272 normal_align = h->root.u.def.section->alignment_power;
5273 if (normal_align > symbol_align)
5274 normal_align = symbol_align;
5275 }
5276 else
5277 normal_align = symbol_align;
5278
5279 if (old_alignment)
5280 {
5281 common_align = old_alignment;
5282 common_bfd = old_bfd;
5283 normal_bfd = abfd;
5284 }
5285 else
5286 {
5287 common_align = bfd_log2 (isym->st_value);
5288 common_bfd = abfd;
5289 normal_bfd = old_bfd;
5290 }
5291
5292 if (normal_align < common_align)
5293 {
5294 /* PR binutils/2735 */
5295 if (normal_bfd == NULL)
5296 _bfd_error_handler
5297 /* xgettext:c-format */
5298 (_("warning: alignment %u of common symbol `%s' in %pB is"
5299 " greater than the alignment (%u) of its section %pA"),
5300 1 << common_align, name, common_bfd,
5301 1 << normal_align, h->root.u.def.section);
5302 else
5303 _bfd_error_handler
5304 /* xgettext:c-format */
5305 (_("warning: alignment %u of symbol `%s' in %pB"
5306 " is smaller than %u in %pB"),
5307 1 << normal_align, name, normal_bfd,
5308 1 << common_align, common_bfd);
5309 }
5310 }
5311
5312 /* Remember the symbol size if it isn't undefined. */
5313 if (isym->st_size != 0
5314 && isym->st_shndx != SHN_UNDEF
5315 && (definition || h->size == 0))
5316 {
5317 if (h->size != 0
5318 && h->size != isym->st_size
5319 && ! size_change_ok)
5320 _bfd_error_handler
5321 /* xgettext:c-format */
5322 (_("warning: size of symbol `%s' changed"
5323 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
5324 name, (uint64_t) h->size, old_bfd,
5325 (uint64_t) isym->st_size, abfd);
5326
5327 h->size = isym->st_size;
5328 }
5329
5330 /* If this is a common symbol, then we always want H->SIZE
5331 to be the size of the common symbol. The code just above
5332 won't fix the size if a common symbol becomes larger. We
5333 don't warn about a size change here, because that is
5334 covered by --warn-common. Allow changes between different
5335 function types. */
5336 if (h->root.type == bfd_link_hash_common)
5337 h->size = h->root.u.c.size;
5338
5339 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
5340 && ((definition && !new_weak)
5341 || (old_weak && h->root.type == bfd_link_hash_common)
5342 || h->type == STT_NOTYPE))
5343 {
5344 unsigned int type = ELF_ST_TYPE (isym->st_info);
5345
5346 /* Turn an IFUNC symbol from a DSO into a normal FUNC
5347 symbol. */
5348 if (type == STT_GNU_IFUNC
5349 && (abfd->flags & DYNAMIC) != 0)
5350 type = STT_FUNC;
5351
5352 if (h->type != type)
5353 {
5354 if (h->type != STT_NOTYPE && ! type_change_ok)
5355 /* xgettext:c-format */
5356 _bfd_error_handler
5357 (_("warning: type of symbol `%s' changed"
5358 " from %d to %d in %pB"),
5359 name, h->type, type, abfd);
5360
5361 h->type = type;
5362 }
5363 }
5364
5365 /* Merge st_other field. */
5366 elf_merge_st_other (abfd, h, isym->st_other, sec,
5367 definition, dynamic);
5368
5369 /* We don't want to make debug symbol dynamic. */
5370 if (definition
5371 && (sec->flags & SEC_DEBUGGING)
5372 && !bfd_link_relocatable (info))
5373 dynsym = false;
5374
5375 /* Nor should we make plugin symbols dynamic. */
5376 if ((abfd->flags & BFD_PLUGIN) != 0)
5377 dynsym = false;
5378
5379 if (definition)
5380 {
5381 h->target_internal = isym->st_target_internal;
5382 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5383 }
5384
5385 if (definition && !dynamic)
5386 {
5387 char *p = strchr (name, ELF_VER_CHR);
5388 if (p != NULL && p[1] != ELF_VER_CHR)
5389 {
5390 /* Queue non-default versions so that .symver x, x@FOO
5391 aliases can be checked. */
5392 if (!nondeflt_vers)
5393 {
5394 size_t amt = ((isymend - isym + 1)
5395 * sizeof (struct elf_link_hash_entry *));
5396 nondeflt_vers
5397 = (struct elf_link_hash_entry **) bfd_malloc (amt);
5398 if (!nondeflt_vers)
5399 goto error_free_vers;
5400 }
5401 nondeflt_vers[nondeflt_vers_cnt++] = h;
5402 }
5403 }
5404
5405 if (dynsym && h->dynindx == -1)
5406 {
5407 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5408 goto error_free_vers;
5409 if (h->is_weakalias
5410 && weakdef (h)->dynindx == -1)
5411 {
5412 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
5413 goto error_free_vers;
5414 }
5415 }
5416 else if (h->dynindx != -1)
5417 /* If the symbol already has a dynamic index, but
5418 visibility says it should not be visible, turn it into
5419 a local symbol. */
5420 switch (ELF_ST_VISIBILITY (h->other))
5421 {
5422 case STV_INTERNAL:
5423 case STV_HIDDEN:
5424 (*bed->elf_backend_hide_symbol) (info, h, true);
5425 dynsym = false;
5426 break;
5427 }
5428
5429 if (!add_needed
5430 && matched
5431 && definition
5432 && h->root.type != bfd_link_hash_indirect
5433 && ((dynsym
5434 && h->ref_regular_nonweak)
5435 || (old_bfd != NULL
5436 && (old_bfd->flags & BFD_PLUGIN) != 0
5437 && h->ref_ir_nonweak
5438 && !info->lto_all_symbols_read)
5439 || (h->ref_dynamic_nonweak
5440 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5441 && !on_needed_list (elf_dt_name (abfd),
5442 htab->needed, NULL))))
5443 {
5444 const char *soname = elf_dt_name (abfd);
5445
5446 info->callbacks->minfo ("%!", soname, old_bfd,
5447 h->root.root.string);
5448
5449 /* A symbol from a library loaded via DT_NEEDED of some
5450 other library is referenced by a regular object.
5451 Add a DT_NEEDED entry for it. Issue an error if
5452 --no-add-needed is used and the reference was not
5453 a weak one. */
5454 if (old_bfd != NULL
5455 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
5456 {
5457 _bfd_error_handler
5458 /* xgettext:c-format */
5459 (_("%pB: undefined reference to symbol '%s'"),
5460 old_bfd, name);
5461 bfd_set_error (bfd_error_missing_dso);
5462 goto error_free_vers;
5463 }
5464
5465 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
5466 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
5467
5468 /* Create dynamic sections for backends that require
5469 that be done before setup_gnu_properties. */
5470 if (!_bfd_elf_link_create_dynamic_sections (abfd, info))
5471 return false;
5472 add_needed = true;
5473 }
5474 }
5475 }
5476
5477 if (info->lto_plugin_active
5478 && !bfd_link_relocatable (info)
5479 && (abfd->flags & BFD_PLUGIN) == 0
5480 && !just_syms
5481 && extsymcount)
5482 {
5483 int r_sym_shift;
5484
5485 if (bed->s->arch_size == 32)
5486 r_sym_shift = 8;
5487 else
5488 r_sym_shift = 32;
5489
5490 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5491 referenced in regular objects so that linker plugin will get
5492 the correct symbol resolution. */
5493
5494 sym_hash = elf_sym_hashes (abfd);
5495 for (s = abfd->sections; s != NULL; s = s->next)
5496 {
5497 Elf_Internal_Rela *internal_relocs;
5498 Elf_Internal_Rela *rel, *relend;
5499
5500 /* Don't check relocations in excluded sections. */
5501 if ((s->flags & SEC_RELOC) == 0
5502 || s->reloc_count == 0
5503 || (s->flags & SEC_EXCLUDE) != 0
5504 || ((info->strip == strip_all
5505 || info->strip == strip_debugger)
5506 && (s->flags & SEC_DEBUGGING) != 0))
5507 continue;
5508
5509 internal_relocs = _bfd_elf_link_info_read_relocs (abfd, info,
5510 s, NULL,
5511 NULL,
5512 _bfd_link_keep_memory (info));
5513 if (internal_relocs == NULL)
5514 goto error_free_vers;
5515
5516 rel = internal_relocs;
5517 relend = rel + s->reloc_count;
5518 for ( ; rel < relend; rel++)
5519 {
5520 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5521 struct elf_link_hash_entry *h;
5522
5523 /* Skip local symbols. */
5524 if (r_symndx < extsymoff)
5525 continue;
5526
5527 h = sym_hash[r_symndx - extsymoff];
5528 if (h != NULL)
5529 h->root.non_ir_ref_regular = 1;
5530 }
5531
5532 if (elf_section_data (s)->relocs != internal_relocs)
5533 free (internal_relocs);
5534 }
5535 }
5536
5537 free (extversym);
5538 extversym = NULL;
5539 free (isymbuf);
5540 isymbuf = NULL;
5541
5542 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5543 {
5544 unsigned int i;
5545
5546 /* Restore the symbol table. */
5547 old_ent = (char *) old_tab + tabsize;
5548 memset (elf_sym_hashes (abfd), 0,
5549 extsymcount * sizeof (struct elf_link_hash_entry *));
5550 htab->root.table.table = old_table;
5551 htab->root.table.size = old_size;
5552 htab->root.table.count = old_count;
5553 memcpy (htab->root.table.table, old_tab, tabsize);
5554 htab->root.undefs = old_undefs;
5555 htab->root.undefs_tail = old_undefs_tail;
5556 if (htab->dynstr != NULL)
5557 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5558 free (old_strtab);
5559 old_strtab = NULL;
5560 for (i = 0; i < htab->root.table.size; i++)
5561 {
5562 struct bfd_hash_entry *p;
5563 struct elf_link_hash_entry *h;
5564 unsigned int non_ir_ref_dynamic;
5565
5566 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5567 {
5568 /* Preserve non_ir_ref_dynamic so that this symbol
5569 will be exported when the dynamic lib becomes needed
5570 in the second pass. */
5571 h = (struct elf_link_hash_entry *) p;
5572 if (h->root.type == bfd_link_hash_warning)
5573 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5574 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
5575
5576 h = (struct elf_link_hash_entry *) p;
5577 memcpy (h, old_ent, htab->root.table.entsize);
5578 old_ent = (char *) old_ent + htab->root.table.entsize;
5579 if (h->root.type == bfd_link_hash_warning)
5580 {
5581 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5582 memcpy (h, old_ent, htab->root.table.entsize);
5583 old_ent = (char *) old_ent + htab->root.table.entsize;
5584 }
5585 if (h->root.type == bfd_link_hash_common)
5586 {
5587 memcpy (h->root.u.c.p, old_ent, sizeof (*h->root.u.c.p));
5588 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
5589 }
5590 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
5591 }
5592 }
5593
5594 /* Make a special call to the linker "notice" function to
5595 tell it that symbols added for crefs may need to be removed. */
5596 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
5597 goto error_free_vers;
5598
5599 free (old_tab);
5600 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5601 alloc_mark);
5602 free (nondeflt_vers);
5603 return true;
5604 }
5605
5606 if (old_tab != NULL)
5607 {
5608 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
5609 goto error_free_vers;
5610 free (old_tab);
5611 old_tab = NULL;
5612 }
5613
5614 /* Now that all the symbols from this input file are created, if
5615 not performing a relocatable link, handle .symver foo, foo@BAR
5616 such that any relocs against foo become foo@BAR. */
5617 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
5618 {
5619 size_t cnt, symidx;
5620
5621 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5622 {
5623 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5624 char *shortname, *p;
5625 size_t amt;
5626
5627 p = strchr (h->root.root.string, ELF_VER_CHR);
5628 if (p == NULL
5629 || (h->root.type != bfd_link_hash_defined
5630 && h->root.type != bfd_link_hash_defweak))
5631 continue;
5632
5633 amt = p - h->root.root.string;
5634 shortname = (char *) bfd_malloc (amt + 1);
5635 if (!shortname)
5636 goto error_free_vers;
5637 memcpy (shortname, h->root.root.string, amt);
5638 shortname[amt] = '\0';
5639
5640 hi = (struct elf_link_hash_entry *)
5641 bfd_link_hash_lookup (&htab->root, shortname,
5642 false, false, false);
5643 if (hi != NULL
5644 && hi->root.type == h->root.type
5645 && hi->root.u.def.value == h->root.u.def.value
5646 && hi->root.u.def.section == h->root.u.def.section)
5647 {
5648 (*bed->elf_backend_hide_symbol) (info, hi, true);
5649 hi->root.type = bfd_link_hash_indirect;
5650 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
5651 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
5652 sym_hash = elf_sym_hashes (abfd);
5653 if (sym_hash)
5654 for (symidx = 0; symidx < extsymcount; ++symidx)
5655 if (sym_hash[symidx] == hi)
5656 {
5657 sym_hash[symidx] = h;
5658 break;
5659 }
5660 }
5661 free (shortname);
5662 }
5663 free (nondeflt_vers);
5664 nondeflt_vers = NULL;
5665 }
5666
5667 /* Now set the alias field correctly for all the weak defined
5668 symbols we found. The only way to do this is to search all the
5669 symbols. Since we only need the information for non functions in
5670 dynamic objects, that's the only time we actually put anything on
5671 the list WEAKS. We need this information so that if a regular
5672 object refers to a symbol defined weakly in a dynamic object, the
5673 real symbol in the dynamic object is also put in the dynamic
5674 symbols; we also must arrange for both symbols to point to the
5675 same memory location. We could handle the general case of symbol
5676 aliasing, but a general symbol alias can only be generated in
5677 assembler code, handling it correctly would be very time
5678 consuming, and other ELF linkers don't handle general aliasing
5679 either. */
5680 if (weaks != NULL)
5681 {
5682 struct elf_link_hash_entry **hpp;
5683 struct elf_link_hash_entry **hppend;
5684 struct elf_link_hash_entry **sorted_sym_hash;
5685 struct elf_link_hash_entry *h;
5686 size_t sym_count, amt;
5687
5688 /* Since we have to search the whole symbol list for each weak
5689 defined symbol, search time for N weak defined symbols will be
5690 O(N^2). Binary search will cut it down to O(NlogN). */
5691 amt = extsymcount * sizeof (*sorted_sym_hash);
5692 sorted_sym_hash = bfd_malloc (amt);
5693 if (sorted_sym_hash == NULL)
5694 goto error_return;
5695 sym_hash = sorted_sym_hash;
5696 hpp = elf_sym_hashes (abfd);
5697 hppend = hpp + extsymcount;
5698 sym_count = 0;
5699 for (; hpp < hppend; hpp++)
5700 {
5701 h = *hpp;
5702 if (h != NULL
5703 && h->root.type == bfd_link_hash_defined
5704 && !bed->is_function_type (h->type))
5705 {
5706 *sym_hash = h;
5707 sym_hash++;
5708 sym_count++;
5709 }
5710 }
5711
5712 qsort (sorted_sym_hash, sym_count, sizeof (*sorted_sym_hash),
5713 elf_sort_symbol);
5714
5715 while (weaks != NULL)
5716 {
5717 struct elf_link_hash_entry *hlook;
5718 asection *slook;
5719 bfd_vma vlook;
5720 size_t i, j, idx = 0;
5721
5722 hlook = weaks;
5723 weaks = hlook->u.alias;
5724 hlook->u.alias = NULL;
5725
5726 if (hlook->root.type != bfd_link_hash_defined
5727 && hlook->root.type != bfd_link_hash_defweak)
5728 continue;
5729
5730 slook = hlook->root.u.def.section;
5731 vlook = hlook->root.u.def.value;
5732
5733 i = 0;
5734 j = sym_count;
5735 while (i != j)
5736 {
5737 bfd_signed_vma vdiff;
5738 idx = (i + j) / 2;
5739 h = sorted_sym_hash[idx];
5740 vdiff = vlook - h->root.u.def.value;
5741 if (vdiff < 0)
5742 j = idx;
5743 else if (vdiff > 0)
5744 i = idx + 1;
5745 else
5746 {
5747 int sdiff = slook->id - h->root.u.def.section->id;
5748 if (sdiff < 0)
5749 j = idx;
5750 else if (sdiff > 0)
5751 i = idx + 1;
5752 else
5753 break;
5754 }
5755 }
5756
5757 /* We didn't find a value/section match. */
5758 if (i == j)
5759 continue;
5760
5761 /* With multiple aliases, or when the weak symbol is already
5762 strongly defined, we have multiple matching symbols and
5763 the binary search above may land on any of them. Step
5764 one past the matching symbol(s). */
5765 while (++idx != j)
5766 {
5767 h = sorted_sym_hash[idx];
5768 if (h->root.u.def.section != slook
5769 || h->root.u.def.value != vlook)
5770 break;
5771 }
5772
5773 /* Now look back over the aliases. Since we sorted by size
5774 as well as value and section, we'll choose the one with
5775 the largest size. */
5776 while (idx-- != i)
5777 {
5778 h = sorted_sym_hash[idx];
5779
5780 /* Stop if value or section doesn't match. */
5781 if (h->root.u.def.section != slook
5782 || h->root.u.def.value != vlook)
5783 break;
5784 else if (h != hlook)
5785 {
5786 struct elf_link_hash_entry *t;
5787
5788 hlook->u.alias = h;
5789 hlook->is_weakalias = 1;
5790 t = h;
5791 if (t->u.alias != NULL)
5792 while (t->u.alias != h)
5793 t = t->u.alias;
5794 t->u.alias = hlook;
5795
5796 /* If the weak definition is in the list of dynamic
5797 symbols, make sure the real definition is put
5798 there as well. */
5799 if (hlook->dynindx != -1 && h->dynindx == -1)
5800 {
5801 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5802 {
5803 err_free_sym_hash:
5804 free (sorted_sym_hash);
5805 goto error_return;
5806 }
5807 }
5808
5809 /* If the real definition is in the list of dynamic
5810 symbols, make sure the weak definition is put
5811 there as well. If we don't do this, then the
5812 dynamic loader might not merge the entries for the
5813 real definition and the weak definition. */
5814 if (h->dynindx != -1 && hlook->dynindx == -1)
5815 {
5816 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
5817 goto err_free_sym_hash;
5818 }
5819 break;
5820 }
5821 }
5822 }
5823
5824 free (sorted_sym_hash);
5825 }
5826
5827 if (bed->check_directives
5828 && !(*bed->check_directives) (abfd, info))
5829 return false;
5830
5831 /* If this is a non-traditional link, try to optimize the handling
5832 of the .stab/.stabstr sections. */
5833 if (! dynamic
5834 && ! info->traditional_format
5835 && is_elf_hash_table (&htab->root)
5836 && (info->strip != strip_all && info->strip != strip_debugger))
5837 {
5838 asection *stabstr;
5839
5840 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5841 if (stabstr != NULL)
5842 {
5843 bfd_size_type string_offset = 0;
5844 asection *stab;
5845
5846 for (stab = abfd->sections; stab; stab = stab->next)
5847 if (startswith (stab->name, ".stab")
5848 && (!stab->name[5] ||
5849 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5850 && (stab->flags & SEC_MERGE) == 0
5851 && !bfd_is_abs_section (stab->output_section))
5852 {
5853 struct bfd_elf_section_data *secdata;
5854
5855 secdata = elf_section_data (stab);
5856 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5857 stabstr, &secdata->sec_info,
5858 &string_offset))
5859 goto error_return;
5860 if (secdata->sec_info)
5861 stab->sec_info_type = SEC_INFO_TYPE_STABS;
5862 }
5863 }
5864 }
5865
5866 if (dynamic && add_needed)
5867 {
5868 /* Add this bfd to the loaded list. */
5869 struct elf_link_loaded_list *n;
5870
5871 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5872 if (n == NULL)
5873 goto error_return;
5874 n->abfd = abfd;
5875 n->next = htab->dyn_loaded;
5876 htab->dyn_loaded = n;
5877 }
5878 if (dynamic && !add_needed
5879 && (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) != 0)
5880 elf_dyn_lib_class (abfd) |= DYN_NO_NEEDED;
5881
5882 return true;
5883
5884 error_free_vers:
5885 free (old_tab);
5886 free (old_strtab);
5887 free (nondeflt_vers);
5888 free (extversym);
5889 error_free_sym:
5890 free (isymbuf);
5891 error_return:
5892 return false;
5893 }
5894
5895 /* Return the linker hash table entry of a symbol that might be
5896 satisfied by an archive symbol. Return -1 on error. */
5897
5898 struct bfd_link_hash_entry *
5899 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5900 struct bfd_link_info *info,
5901 const char *name)
5902 {
5903 struct bfd_link_hash_entry *h;
5904 char *p, *copy;
5905 size_t len, first;
5906
5907 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
5908 if (h != NULL)
5909 return h;
5910
5911 /* If this is a default version (the name contains @@), look up the
5912 symbol again with only one `@' as well as without the version.
5913 The effect is that references to the symbol with and without the
5914 version will be matched by the default symbol in the archive. */
5915
5916 p = strchr (name, ELF_VER_CHR);
5917 if (p == NULL || p[1] != ELF_VER_CHR)
5918 return h;
5919
5920 /* First check with only one `@'. */
5921 len = strlen (name);
5922 copy = (char *) bfd_alloc (abfd, len);
5923 if (copy == NULL)
5924 return (struct bfd_link_hash_entry *) -1;
5925
5926 first = p - name + 1;
5927 memcpy (copy, name, first);
5928 memcpy (copy + first, name + first + 1, len - first);
5929
5930 h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
5931 if (h == NULL)
5932 {
5933 /* We also need to check references to the symbol without the
5934 version. */
5935 copy[first - 1] = '\0';
5936 h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
5937 }
5938
5939 bfd_release (abfd, copy);
5940 return h;
5941 }
5942
5943 /* Add symbols from an ELF archive file to the linker hash table. We
5944 don't use _bfd_generic_link_add_archive_symbols because we need to
5945 handle versioned symbols.
5946
5947 Fortunately, ELF archive handling is simpler than that done by
5948 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5949 oddities. In ELF, if we find a symbol in the archive map, and the
5950 symbol is currently undefined, we know that we must pull in that
5951 object file.
5952
5953 Unfortunately, we do have to make multiple passes over the symbol
5954 table until nothing further is resolved. */
5955
5956 static bool
5957 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5958 {
5959 symindex c;
5960 unsigned char *included = NULL;
5961 carsym *symdefs;
5962 bool loop;
5963 size_t amt;
5964 const struct elf_backend_data *bed;
5965 struct bfd_link_hash_entry * (*archive_symbol_lookup)
5966 (bfd *, struct bfd_link_info *, const char *);
5967
5968 if (! bfd_has_map (abfd))
5969 {
5970 /* An empty archive is a special case. */
5971 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5972 return true;
5973 bfd_set_error (bfd_error_no_armap);
5974 return false;
5975 }
5976
5977 /* Keep track of all symbols we know to be already defined, and all
5978 files we know to be already included. This is to speed up the
5979 second and subsequent passes. */
5980 c = bfd_ardata (abfd)->symdef_count;
5981 if (c == 0)
5982 return true;
5983 amt = c * sizeof (*included);
5984 included = (unsigned char *) bfd_zmalloc (amt);
5985 if (included == NULL)
5986 return false;
5987
5988 symdefs = bfd_ardata (abfd)->symdefs;
5989 bed = get_elf_backend_data (abfd);
5990 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5991
5992 do
5993 {
5994 file_ptr last;
5995 symindex i;
5996 carsym *symdef;
5997 carsym *symdefend;
5998
5999 loop = false;
6000 last = -1;
6001
6002 symdef = symdefs;
6003 symdefend = symdef + c;
6004 for (i = 0; symdef < symdefend; symdef++, i++)
6005 {
6006 struct bfd_link_hash_entry *h;
6007 bfd *element;
6008 struct bfd_link_hash_entry *undefs_tail;
6009 symindex mark;
6010
6011 if (included[i])
6012 continue;
6013 if (symdef->file_offset == last)
6014 {
6015 included[i] = true;
6016 continue;
6017 }
6018
6019 h = archive_symbol_lookup (abfd, info, symdef->name);
6020 if (h == (struct bfd_link_hash_entry *) -1)
6021 goto error_return;
6022
6023 if (h == NULL)
6024 continue;
6025
6026 if (h->type == bfd_link_hash_undefined)
6027 {
6028 /* If the archive element has already been loaded then one
6029 of the symbols defined by that element might have been
6030 made undefined due to being in a discarded section. */
6031 if (is_elf_hash_table (info->hash)
6032 && ((struct elf_link_hash_entry *) h)->indx == -3)
6033 continue;
6034 }
6035 else if (h->type == bfd_link_hash_common)
6036 {
6037 /* We currently have a common symbol. The archive map contains
6038 a reference to this symbol, so we may want to include it. We
6039 only want to include it however, if this archive element
6040 contains a definition of the symbol, not just another common
6041 declaration of it.
6042
6043 Unfortunately some archivers (including GNU ar) will put
6044 declarations of common symbols into their archive maps, as
6045 well as real definitions, so we cannot just go by the archive
6046 map alone. Instead we must read in the element's symbol
6047 table and check that to see what kind of symbol definition
6048 this is. */
6049 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
6050 continue;
6051 }
6052 else
6053 {
6054 if (h->type != bfd_link_hash_undefweak)
6055 /* Symbol must be defined. Don't check it again. */
6056 included[i] = true;
6057 continue;
6058 }
6059
6060 /* We need to include this archive member. */
6061 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset,
6062 info);
6063 if (element == NULL)
6064 goto error_return;
6065
6066 if (! bfd_check_format (element, bfd_object))
6067 goto error_return;
6068
6069 undefs_tail = info->hash->undefs_tail;
6070
6071 if (!(*info->callbacks
6072 ->add_archive_element) (info, element, symdef->name, &element))
6073 continue;
6074 if (!bfd_link_add_symbols (element, info))
6075 goto error_return;
6076
6077 /* If there are any new undefined symbols, we need to make
6078 another pass through the archive in order to see whether
6079 they can be defined. FIXME: This isn't perfect, because
6080 common symbols wind up on undefs_tail and because an
6081 undefined symbol which is defined later on in this pass
6082 does not require another pass. This isn't a bug, but it
6083 does make the code less efficient than it could be. */
6084 if (undefs_tail != info->hash->undefs_tail)
6085 loop = true;
6086
6087 /* Look backward to mark all symbols from this object file
6088 which we have already seen in this pass. */
6089 mark = i;
6090 do
6091 {
6092 included[mark] = true;
6093 if (mark == 0)
6094 break;
6095 --mark;
6096 }
6097 while (symdefs[mark].file_offset == symdef->file_offset);
6098
6099 /* We mark subsequent symbols from this object file as we go
6100 on through the loop. */
6101 last = symdef->file_offset;
6102 }
6103 }
6104 while (loop);
6105
6106 free (included);
6107 return true;
6108
6109 error_return:
6110 free (included);
6111 return false;
6112 }
6113
6114 /* Given an ELF BFD, add symbols to the global hash table as
6115 appropriate. */
6116
6117 bool
6118 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
6119 {
6120 switch (bfd_get_format (abfd))
6121 {
6122 case bfd_object:
6123 return elf_link_add_object_symbols (abfd, info);
6124 case bfd_archive:
6125 return elf_link_add_archive_symbols (abfd, info);
6126 default:
6127 bfd_set_error (bfd_error_wrong_format);
6128 return false;
6129 }
6130 }
6131 \f
6132 struct hash_codes_info
6133 {
6134 unsigned long *hashcodes;
6135 bool error;
6136 };
6137
6138 /* This function will be called though elf_link_hash_traverse to store
6139 all hash value of the exported symbols in an array. */
6140
6141 static bool
6142 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
6143 {
6144 struct hash_codes_info *inf = (struct hash_codes_info *) data;
6145 const char *name;
6146 unsigned long ha;
6147 char *alc = NULL;
6148
6149 /* Ignore indirect symbols. These are added by the versioning code. */
6150 if (h->dynindx == -1)
6151 return true;
6152
6153 name = h->root.root.string;
6154 if (h->versioned >= versioned)
6155 {
6156 char *p = strchr (name, ELF_VER_CHR);
6157 if (p != NULL)
6158 {
6159 alc = (char *) bfd_malloc (p - name + 1);
6160 if (alc == NULL)
6161 {
6162 inf->error = true;
6163 return false;
6164 }
6165 memcpy (alc, name, p - name);
6166 alc[p - name] = '\0';
6167 name = alc;
6168 }
6169 }
6170
6171 /* Compute the hash value. */
6172 ha = bfd_elf_hash (name);
6173
6174 /* Store the found hash value in the array given as the argument. */
6175 *(inf->hashcodes)++ = ha;
6176
6177 /* And store it in the struct so that we can put it in the hash table
6178 later. */
6179 h->u.elf_hash_value = ha;
6180
6181 free (alc);
6182 return true;
6183 }
6184
6185 struct collect_gnu_hash_codes
6186 {
6187 bfd *output_bfd;
6188 const struct elf_backend_data *bed;
6189 unsigned long int nsyms;
6190 unsigned long int maskbits;
6191 unsigned long int *hashcodes;
6192 unsigned long int *hashval;
6193 unsigned long int *indx;
6194 unsigned long int *counts;
6195 bfd_vma *bitmask;
6196 bfd_byte *contents;
6197 bfd_size_type xlat;
6198 long int min_dynindx;
6199 unsigned long int bucketcount;
6200 unsigned long int symindx;
6201 long int local_indx;
6202 long int shift1, shift2;
6203 unsigned long int mask;
6204 bool error;
6205 };
6206
6207 /* This function will be called though elf_link_hash_traverse to store
6208 all hash value of the exported symbols in an array. */
6209
6210 static bool
6211 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
6212 {
6213 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6214 const char *name;
6215 unsigned long ha;
6216 char *alc = NULL;
6217
6218 /* Ignore indirect symbols. These are added by the versioning code. */
6219 if (h->dynindx == -1)
6220 return true;
6221
6222 /* Ignore also local symbols and undefined symbols. */
6223 if (! (*s->bed->elf_hash_symbol) (h))
6224 return true;
6225
6226 name = h->root.root.string;
6227 if (h->versioned >= versioned)
6228 {
6229 char *p = strchr (name, ELF_VER_CHR);
6230 if (p != NULL)
6231 {
6232 alc = (char *) bfd_malloc (p - name + 1);
6233 if (alc == NULL)
6234 {
6235 s->error = true;
6236 return false;
6237 }
6238 memcpy (alc, name, p - name);
6239 alc[p - name] = '\0';
6240 name = alc;
6241 }
6242 }
6243
6244 /* Compute the hash value. */
6245 ha = bfd_elf_gnu_hash (name);
6246
6247 /* Store the found hash value in the array for compute_bucket_count,
6248 and also for .dynsym reordering purposes. */
6249 s->hashcodes[s->nsyms] = ha;
6250 s->hashval[h->dynindx] = ha;
6251 ++s->nsyms;
6252 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
6253 s->min_dynindx = h->dynindx;
6254
6255 free (alc);
6256 return true;
6257 }
6258
6259 /* This function will be called though elf_link_hash_traverse to do
6260 final dynamic symbol renumbering in case of .gnu.hash.
6261 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6262 to the translation table. */
6263
6264 static bool
6265 elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data)
6266 {
6267 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6268 unsigned long int bucket;
6269 unsigned long int val;
6270
6271 /* Ignore indirect symbols. */
6272 if (h->dynindx == -1)
6273 return true;
6274
6275 /* Ignore also local symbols and undefined symbols. */
6276 if (! (*s->bed->elf_hash_symbol) (h))
6277 {
6278 if (h->dynindx >= s->min_dynindx)
6279 {
6280 if (s->bed->record_xhash_symbol != NULL)
6281 {
6282 (*s->bed->record_xhash_symbol) (h, 0);
6283 s->local_indx++;
6284 }
6285 else
6286 h->dynindx = s->local_indx++;
6287 }
6288 return true;
6289 }
6290
6291 bucket = s->hashval[h->dynindx] % s->bucketcount;
6292 val = (s->hashval[h->dynindx] >> s->shift1)
6293 & ((s->maskbits >> s->shift1) - 1);
6294 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
6295 s->bitmask[val]
6296 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
6297 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
6298 if (s->counts[bucket] == 1)
6299 /* Last element terminates the chain. */
6300 val |= 1;
6301 bfd_put_32 (s->output_bfd, val,
6302 s->contents + (s->indx[bucket] - s->symindx) * 4);
6303 --s->counts[bucket];
6304 if (s->bed->record_xhash_symbol != NULL)
6305 {
6306 bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4;
6307
6308 (*s->bed->record_xhash_symbol) (h, xlat_loc);
6309 }
6310 else
6311 h->dynindx = s->indx[bucket]++;
6312 return true;
6313 }
6314
6315 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6316
6317 bool
6318 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
6319 {
6320 return !(h->forced_local
6321 || h->root.type == bfd_link_hash_undefined
6322 || h->root.type == bfd_link_hash_undefweak
6323 || ((h->root.type == bfd_link_hash_defined
6324 || h->root.type == bfd_link_hash_defweak)
6325 && h->root.u.def.section->output_section == NULL));
6326 }
6327
6328 /* Array used to determine the number of hash table buckets to use
6329 based on the number of symbols there are. If there are fewer than
6330 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6331 fewer than 37 we use 17 buckets, and so forth. We never use more
6332 than 32771 buckets. */
6333
6334 static const size_t elf_buckets[] =
6335 {
6336 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6337 16411, 32771, 0
6338 };
6339
6340 /* Compute bucket count for hashing table. We do not use a static set
6341 of possible tables sizes anymore. Instead we determine for all
6342 possible reasonable sizes of the table the outcome (i.e., the
6343 number of collisions etc) and choose the best solution. The
6344 weighting functions are not too simple to allow the table to grow
6345 without bounds. Instead one of the weighting factors is the size.
6346 Therefore the result is always a good payoff between few collisions
6347 (= short chain lengths) and table size. */
6348 static size_t
6349 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
6350 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
6351 unsigned long int nsyms,
6352 int gnu_hash)
6353 {
6354 size_t best_size = 0;
6355 unsigned long int i;
6356
6357 /* We have a problem here. The following code to optimize the table
6358 size requires an integer type with more the 32 bits. If
6359 BFD_HOST_U_64_BIT is set we know about such a type. */
6360 #ifdef BFD_HOST_U_64_BIT
6361 if (info->optimize)
6362 {
6363 size_t minsize;
6364 size_t maxsize;
6365 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
6366 bfd *dynobj = elf_hash_table (info)->dynobj;
6367 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
6368 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
6369 unsigned long int *counts;
6370 bfd_size_type amt;
6371 unsigned int no_improvement_count = 0;
6372
6373 /* Possible optimization parameters: if we have NSYMS symbols we say
6374 that the hashing table must at least have NSYMS/4 and at most
6375 2*NSYMS buckets. */
6376 minsize = nsyms / 4;
6377 if (minsize == 0)
6378 minsize = 1;
6379 best_size = maxsize = nsyms * 2;
6380 if (gnu_hash)
6381 {
6382 if (minsize < 2)
6383 minsize = 2;
6384 if ((best_size & 31) == 0)
6385 ++best_size;
6386 }
6387
6388 /* Create array where we count the collisions in. We must use bfd_malloc
6389 since the size could be large. */
6390 amt = maxsize;
6391 amt *= sizeof (unsigned long int);
6392 counts = (unsigned long int *) bfd_malloc (amt);
6393 if (counts == NULL)
6394 return 0;
6395
6396 /* Compute the "optimal" size for the hash table. The criteria is a
6397 minimal chain length. The minor criteria is (of course) the size
6398 of the table. */
6399 for (i = minsize; i < maxsize; ++i)
6400 {
6401 /* Walk through the array of hashcodes and count the collisions. */
6402 BFD_HOST_U_64_BIT max;
6403 unsigned long int j;
6404 unsigned long int fact;
6405
6406 if (gnu_hash && (i & 31) == 0)
6407 continue;
6408
6409 memset (counts, '\0', i * sizeof (unsigned long int));
6410
6411 /* Determine how often each hash bucket is used. */
6412 for (j = 0; j < nsyms; ++j)
6413 ++counts[hashcodes[j] % i];
6414
6415 /* For the weight function we need some information about the
6416 pagesize on the target. This is information need not be 100%
6417 accurate. Since this information is not available (so far) we
6418 define it here to a reasonable default value. If it is crucial
6419 to have a better value some day simply define this value. */
6420 # ifndef BFD_TARGET_PAGESIZE
6421 # define BFD_TARGET_PAGESIZE (4096)
6422 # endif
6423
6424 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6425 and the chains. */
6426 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
6427
6428 # if 1
6429 /* Variant 1: optimize for short chains. We add the squares
6430 of all the chain lengths (which favors many small chain
6431 over a few long chains). */
6432 for (j = 0; j < i; ++j)
6433 max += counts[j] * counts[j];
6434
6435 /* This adds penalties for the overall size of the table. */
6436 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6437 max *= fact * fact;
6438 # else
6439 /* Variant 2: Optimize a lot more for small table. Here we
6440 also add squares of the size but we also add penalties for
6441 empty slots (the +1 term). */
6442 for (j = 0; j < i; ++j)
6443 max += (1 + counts[j]) * (1 + counts[j]);
6444
6445 /* The overall size of the table is considered, but not as
6446 strong as in variant 1, where it is squared. */
6447 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6448 max *= fact;
6449 # endif
6450
6451 /* Compare with current best results. */
6452 if (max < best_chlen)
6453 {
6454 best_chlen = max;
6455 best_size = i;
6456 no_improvement_count = 0;
6457 }
6458 /* PR 11843: Avoid futile long searches for the best bucket size
6459 when there are a large number of symbols. */
6460 else if (++no_improvement_count == 100)
6461 break;
6462 }
6463
6464 free (counts);
6465 }
6466 else
6467 #endif /* defined (BFD_HOST_U_64_BIT) */
6468 {
6469 /* This is the fallback solution if no 64bit type is available or if we
6470 are not supposed to spend much time on optimizations. We select the
6471 bucket count using a fixed set of numbers. */
6472 for (i = 0; elf_buckets[i] != 0; i++)
6473 {
6474 best_size = elf_buckets[i];
6475 if (nsyms < elf_buckets[i + 1])
6476 break;
6477 }
6478 if (gnu_hash && best_size < 2)
6479 best_size = 2;
6480 }
6481
6482 return best_size;
6483 }
6484
6485 /* Size any SHT_GROUP section for ld -r. */
6486
6487 bool
6488 _bfd_elf_size_group_sections (struct bfd_link_info *info)
6489 {
6490 bfd *ibfd;
6491 asection *s;
6492
6493 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6494 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6495 && (s = ibfd->sections) != NULL
6496 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
6497 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6498 return false;
6499 return true;
6500 }
6501
6502 /* Set a default stack segment size. The value in INFO wins. If it
6503 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6504 undefined it is initialized. */
6505
6506 bool
6507 bfd_elf_stack_segment_size (bfd *output_bfd,
6508 struct bfd_link_info *info,
6509 const char *legacy_symbol,
6510 bfd_vma default_size)
6511 {
6512 struct elf_link_hash_entry *h = NULL;
6513
6514 /* Look for legacy symbol. */
6515 if (legacy_symbol)
6516 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6517 false, false, false);
6518 if (h && (h->root.type == bfd_link_hash_defined
6519 || h->root.type == bfd_link_hash_defweak)
6520 && h->def_regular
6521 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6522 {
6523 /* The symbol has no type if specified on the command line. */
6524 h->type = STT_OBJECT;
6525 if (info->stacksize)
6526 /* xgettext:c-format */
6527 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6528 output_bfd, legacy_symbol);
6529 else if (h->root.u.def.section != bfd_abs_section_ptr)
6530 /* xgettext:c-format */
6531 _bfd_error_handler (_("%pB: %s not absolute"),
6532 output_bfd, legacy_symbol);
6533 else
6534 info->stacksize = h->root.u.def.value;
6535 }
6536
6537 if (!info->stacksize)
6538 /* If the user didn't set a size, or explicitly inhibit the
6539 size, set it now. */
6540 info->stacksize = default_size;
6541
6542 /* Provide the legacy symbol, if it is referenced. */
6543 if (h && (h->root.type == bfd_link_hash_undefined
6544 || h->root.type == bfd_link_hash_undefweak))
6545 {
6546 struct bfd_link_hash_entry *bh = NULL;
6547
6548 if (!(_bfd_generic_link_add_one_symbol
6549 (info, output_bfd, legacy_symbol,
6550 BSF_GLOBAL, bfd_abs_section_ptr,
6551 info->stacksize >= 0 ? info->stacksize : 0,
6552 NULL, false, get_elf_backend_data (output_bfd)->collect, &bh)))
6553 return false;
6554
6555 h = (struct elf_link_hash_entry *) bh;
6556 h->def_regular = 1;
6557 h->type = STT_OBJECT;
6558 }
6559
6560 return true;
6561 }
6562
6563 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6564
6565 struct elf_gc_sweep_symbol_info
6566 {
6567 struct bfd_link_info *info;
6568 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6569 bool);
6570 };
6571
6572 static bool
6573 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6574 {
6575 if (!h->mark
6576 && (((h->root.type == bfd_link_hash_defined
6577 || h->root.type == bfd_link_hash_defweak)
6578 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6579 && h->root.u.def.section->gc_mark))
6580 || h->root.type == bfd_link_hash_undefined
6581 || h->root.type == bfd_link_hash_undefweak))
6582 {
6583 struct elf_gc_sweep_symbol_info *inf;
6584
6585 inf = (struct elf_gc_sweep_symbol_info *) data;
6586 (*inf->hide_symbol) (inf->info, h, true);
6587 h->def_regular = 0;
6588 h->ref_regular = 0;
6589 h->ref_regular_nonweak = 0;
6590 }
6591
6592 return true;
6593 }
6594
6595 /* Set up the sizes and contents of the ELF dynamic sections. This is
6596 called by the ELF linker emulation before_allocation routine. We
6597 must set the sizes of the sections before the linker sets the
6598 addresses of the various sections. */
6599
6600 bool
6601 bfd_elf_size_dynamic_sections (bfd *output_bfd,
6602 const char *soname,
6603 const char *rpath,
6604 const char *filter_shlib,
6605 const char *audit,
6606 const char *depaudit,
6607 const char * const *auxiliary_filters,
6608 struct bfd_link_info *info,
6609 asection **sinterpptr)
6610 {
6611 bfd *dynobj;
6612 const struct elf_backend_data *bed;
6613
6614 *sinterpptr = NULL;
6615
6616 if (!is_elf_hash_table (info->hash))
6617 return true;
6618
6619 /* Any syms created from now on start with -1 in
6620 got.refcount/offset and plt.refcount/offset. */
6621 elf_hash_table (info)->init_got_refcount
6622 = elf_hash_table (info)->init_got_offset;
6623 elf_hash_table (info)->init_plt_refcount
6624 = elf_hash_table (info)->init_plt_offset;
6625
6626 bed = get_elf_backend_data (output_bfd);
6627
6628 /* The backend may have to create some sections regardless of whether
6629 we're dynamic or not. */
6630 if (bed->elf_backend_always_size_sections
6631 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6632 return false;
6633
6634 dynobj = elf_hash_table (info)->dynobj;
6635
6636 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6637 {
6638 struct bfd_elf_version_tree *verdefs;
6639 struct elf_info_failed asvinfo;
6640 struct bfd_elf_version_tree *t;
6641 struct bfd_elf_version_expr *d;
6642 asection *s;
6643 size_t soname_indx;
6644
6645 /* If we are supposed to export all symbols into the dynamic symbol
6646 table (this is not the normal case), then do so. */
6647 if (info->export_dynamic
6648 || (bfd_link_executable (info) && info->dynamic))
6649 {
6650 struct elf_info_failed eif;
6651
6652 eif.info = info;
6653 eif.failed = false;
6654 elf_link_hash_traverse (elf_hash_table (info),
6655 _bfd_elf_export_symbol,
6656 &eif);
6657 if (eif.failed)
6658 return false;
6659 }
6660
6661 if (soname != NULL)
6662 {
6663 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6664 soname, true);
6665 if (soname_indx == (size_t) -1
6666 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6667 return false;
6668 }
6669 else
6670 soname_indx = (size_t) -1;
6671
6672 /* Make all global versions with definition. */
6673 for (t = info->version_info; t != NULL; t = t->next)
6674 for (d = t->globals.list; d != NULL; d = d->next)
6675 if (!d->symver && d->literal)
6676 {
6677 const char *verstr, *name;
6678 size_t namelen, verlen, newlen;
6679 char *newname, *p, leading_char;
6680 struct elf_link_hash_entry *newh;
6681
6682 leading_char = bfd_get_symbol_leading_char (output_bfd);
6683 name = d->pattern;
6684 namelen = strlen (name) + (leading_char != '\0');
6685 verstr = t->name;
6686 verlen = strlen (verstr);
6687 newlen = namelen + verlen + 3;
6688
6689 newname = (char *) bfd_malloc (newlen);
6690 if (newname == NULL)
6691 return false;
6692 newname[0] = leading_char;
6693 memcpy (newname + (leading_char != '\0'), name, namelen);
6694
6695 /* Check the hidden versioned definition. */
6696 p = newname + namelen;
6697 *p++ = ELF_VER_CHR;
6698 memcpy (p, verstr, verlen + 1);
6699 newh = elf_link_hash_lookup (elf_hash_table (info),
6700 newname, false, false,
6701 false);
6702 if (newh == NULL
6703 || (newh->root.type != bfd_link_hash_defined
6704 && newh->root.type != bfd_link_hash_defweak))
6705 {
6706 /* Check the default versioned definition. */
6707 *p++ = ELF_VER_CHR;
6708 memcpy (p, verstr, verlen + 1);
6709 newh = elf_link_hash_lookup (elf_hash_table (info),
6710 newname, false, false,
6711 false);
6712 }
6713 free (newname);
6714
6715 /* Mark this version if there is a definition and it is
6716 not defined in a shared object. */
6717 if (newh != NULL
6718 && !newh->def_dynamic
6719 && (newh->root.type == bfd_link_hash_defined
6720 || newh->root.type == bfd_link_hash_defweak))
6721 d->symver = 1;
6722 }
6723
6724 /* Attach all the symbols to their version information. */
6725 asvinfo.info = info;
6726 asvinfo.failed = false;
6727
6728 elf_link_hash_traverse (elf_hash_table (info),
6729 _bfd_elf_link_assign_sym_version,
6730 &asvinfo);
6731 if (asvinfo.failed)
6732 return false;
6733
6734 if (!info->allow_undefined_version)
6735 {
6736 /* Check if all global versions have a definition. */
6737 bool all_defined = true;
6738 for (t = info->version_info; t != NULL; t = t->next)
6739 for (d = t->globals.list; d != NULL; d = d->next)
6740 if (d->literal && !d->symver && !d->script)
6741 {
6742 _bfd_error_handler
6743 (_("%s: undefined version: %s"),
6744 d->pattern, t->name);
6745 all_defined = false;
6746 }
6747
6748 if (!all_defined)
6749 {
6750 bfd_set_error (bfd_error_bad_value);
6751 return false;
6752 }
6753 }
6754
6755 /* Set up the version definition section. */
6756 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6757 BFD_ASSERT (s != NULL);
6758
6759 /* We may have created additional version definitions if we are
6760 just linking a regular application. */
6761 verdefs = info->version_info;
6762
6763 /* Skip anonymous version tag. */
6764 if (verdefs != NULL && verdefs->vernum == 0)
6765 verdefs = verdefs->next;
6766
6767 if (verdefs == NULL && !info->create_default_symver)
6768 s->flags |= SEC_EXCLUDE;
6769 else
6770 {
6771 unsigned int cdefs;
6772 bfd_size_type size;
6773 bfd_byte *p;
6774 Elf_Internal_Verdef def;
6775 Elf_Internal_Verdaux defaux;
6776 struct bfd_link_hash_entry *bh;
6777 struct elf_link_hash_entry *h;
6778 const char *name;
6779
6780 cdefs = 0;
6781 size = 0;
6782
6783 /* Make space for the base version. */
6784 size += sizeof (Elf_External_Verdef);
6785 size += sizeof (Elf_External_Verdaux);
6786 ++cdefs;
6787
6788 /* Make space for the default version. */
6789 if (info->create_default_symver)
6790 {
6791 size += sizeof (Elf_External_Verdef);
6792 ++cdefs;
6793 }
6794
6795 for (t = verdefs; t != NULL; t = t->next)
6796 {
6797 struct bfd_elf_version_deps *n;
6798
6799 /* Don't emit base version twice. */
6800 if (t->vernum == 0)
6801 continue;
6802
6803 size += sizeof (Elf_External_Verdef);
6804 size += sizeof (Elf_External_Verdaux);
6805 ++cdefs;
6806
6807 for (n = t->deps; n != NULL; n = n->next)
6808 size += sizeof (Elf_External_Verdaux);
6809 }
6810
6811 s->size = size;
6812 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6813 if (s->contents == NULL && s->size != 0)
6814 return false;
6815
6816 /* Fill in the version definition section. */
6817
6818 p = s->contents;
6819
6820 def.vd_version = VER_DEF_CURRENT;
6821 def.vd_flags = VER_FLG_BASE;
6822 def.vd_ndx = 1;
6823 def.vd_cnt = 1;
6824 if (info->create_default_symver)
6825 {
6826 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6827 def.vd_next = sizeof (Elf_External_Verdef);
6828 }
6829 else
6830 {
6831 def.vd_aux = sizeof (Elf_External_Verdef);
6832 def.vd_next = (sizeof (Elf_External_Verdef)
6833 + sizeof (Elf_External_Verdaux));
6834 }
6835
6836 if (soname_indx != (size_t) -1)
6837 {
6838 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6839 soname_indx);
6840 def.vd_hash = bfd_elf_hash (soname);
6841 defaux.vda_name = soname_indx;
6842 name = soname;
6843 }
6844 else
6845 {
6846 size_t indx;
6847
6848 name = lbasename (bfd_get_filename (output_bfd));
6849 def.vd_hash = bfd_elf_hash (name);
6850 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6851 name, false);
6852 if (indx == (size_t) -1)
6853 return false;
6854 defaux.vda_name = indx;
6855 }
6856 defaux.vda_next = 0;
6857
6858 _bfd_elf_swap_verdef_out (output_bfd, &def,
6859 (Elf_External_Verdef *) p);
6860 p += sizeof (Elf_External_Verdef);
6861 if (info->create_default_symver)
6862 {
6863 /* Add a symbol representing this version. */
6864 bh = NULL;
6865 if (! (_bfd_generic_link_add_one_symbol
6866 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6867 0, NULL, false,
6868 get_elf_backend_data (dynobj)->collect, &bh)))
6869 return false;
6870 h = (struct elf_link_hash_entry *) bh;
6871 h->non_elf = 0;
6872 h->def_regular = 1;
6873 h->type = STT_OBJECT;
6874 h->verinfo.vertree = NULL;
6875
6876 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6877 return false;
6878
6879 /* Create a duplicate of the base version with the same
6880 aux block, but different flags. */
6881 def.vd_flags = 0;
6882 def.vd_ndx = 2;
6883 def.vd_aux = sizeof (Elf_External_Verdef);
6884 if (verdefs)
6885 def.vd_next = (sizeof (Elf_External_Verdef)
6886 + sizeof (Elf_External_Verdaux));
6887 else
6888 def.vd_next = 0;
6889 _bfd_elf_swap_verdef_out (output_bfd, &def,
6890 (Elf_External_Verdef *) p);
6891 p += sizeof (Elf_External_Verdef);
6892 }
6893 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6894 (Elf_External_Verdaux *) p);
6895 p += sizeof (Elf_External_Verdaux);
6896
6897 for (t = verdefs; t != NULL; t = t->next)
6898 {
6899 unsigned int cdeps;
6900 struct bfd_elf_version_deps *n;
6901
6902 /* Don't emit the base version twice. */
6903 if (t->vernum == 0)
6904 continue;
6905
6906 cdeps = 0;
6907 for (n = t->deps; n != NULL; n = n->next)
6908 ++cdeps;
6909
6910 /* Add a symbol representing this version. */
6911 bh = NULL;
6912 if (! (_bfd_generic_link_add_one_symbol
6913 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6914 0, NULL, false,
6915 get_elf_backend_data (dynobj)->collect, &bh)))
6916 return false;
6917 h = (struct elf_link_hash_entry *) bh;
6918 h->non_elf = 0;
6919 h->def_regular = 1;
6920 h->type = STT_OBJECT;
6921 h->verinfo.vertree = t;
6922
6923 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6924 return false;
6925
6926 def.vd_version = VER_DEF_CURRENT;
6927 def.vd_flags = 0;
6928 if (t->globals.list == NULL
6929 && t->locals.list == NULL
6930 && ! t->used)
6931 def.vd_flags |= VER_FLG_WEAK;
6932 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6933 def.vd_cnt = cdeps + 1;
6934 def.vd_hash = bfd_elf_hash (t->name);
6935 def.vd_aux = sizeof (Elf_External_Verdef);
6936 def.vd_next = 0;
6937
6938 /* If a basever node is next, it *must* be the last node in
6939 the chain, otherwise Verdef construction breaks. */
6940 if (t->next != NULL && t->next->vernum == 0)
6941 BFD_ASSERT (t->next->next == NULL);
6942
6943 if (t->next != NULL && t->next->vernum != 0)
6944 def.vd_next = (sizeof (Elf_External_Verdef)
6945 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6946
6947 _bfd_elf_swap_verdef_out (output_bfd, &def,
6948 (Elf_External_Verdef *) p);
6949 p += sizeof (Elf_External_Verdef);
6950
6951 defaux.vda_name = h->dynstr_index;
6952 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6953 h->dynstr_index);
6954 defaux.vda_next = 0;
6955 if (t->deps != NULL)
6956 defaux.vda_next = sizeof (Elf_External_Verdaux);
6957 t->name_indx = defaux.vda_name;
6958
6959 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6960 (Elf_External_Verdaux *) p);
6961 p += sizeof (Elf_External_Verdaux);
6962
6963 for (n = t->deps; n != NULL; n = n->next)
6964 {
6965 if (n->version_needed == NULL)
6966 {
6967 /* This can happen if there was an error in the
6968 version script. */
6969 defaux.vda_name = 0;
6970 }
6971 else
6972 {
6973 defaux.vda_name = n->version_needed->name_indx;
6974 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6975 defaux.vda_name);
6976 }
6977 if (n->next == NULL)
6978 defaux.vda_next = 0;
6979 else
6980 defaux.vda_next = sizeof (Elf_External_Verdaux);
6981
6982 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6983 (Elf_External_Verdaux *) p);
6984 p += sizeof (Elf_External_Verdaux);
6985 }
6986 }
6987
6988 elf_tdata (output_bfd)->cverdefs = cdefs;
6989 }
6990 }
6991
6992 if (info->gc_sections && bed->can_gc_sections)
6993 {
6994 struct elf_gc_sweep_symbol_info sweep_info;
6995
6996 /* Remove the symbols that were in the swept sections from the
6997 dynamic symbol table. */
6998 sweep_info.info = info;
6999 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
7000 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
7001 &sweep_info);
7002 }
7003
7004 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7005 {
7006 asection *s;
7007 struct elf_find_verdep_info sinfo;
7008
7009 /* Work out the size of the version reference section. */
7010
7011 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
7012 BFD_ASSERT (s != NULL);
7013
7014 sinfo.info = info;
7015 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
7016 if (sinfo.vers == 0)
7017 sinfo.vers = 1;
7018 sinfo.failed = false;
7019
7020 elf_link_hash_traverse (elf_hash_table (info),
7021 _bfd_elf_link_find_version_dependencies,
7022 &sinfo);
7023 if (sinfo.failed)
7024 return false;
7025
7026 if (info->enable_dt_relr)
7027 {
7028 elf_link_add_dt_relr_dependency (&sinfo);
7029 if (sinfo.failed)
7030 return false;
7031 }
7032
7033 if (elf_tdata (output_bfd)->verref == NULL)
7034 s->flags |= SEC_EXCLUDE;
7035 else
7036 {
7037 Elf_Internal_Verneed *vn;
7038 unsigned int size;
7039 unsigned int crefs;
7040 bfd_byte *p;
7041
7042 /* Build the version dependency section. */
7043 size = 0;
7044 crefs = 0;
7045 for (vn = elf_tdata (output_bfd)->verref;
7046 vn != NULL;
7047 vn = vn->vn_nextref)
7048 {
7049 Elf_Internal_Vernaux *a;
7050
7051 size += sizeof (Elf_External_Verneed);
7052 ++crefs;
7053 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7054 size += sizeof (Elf_External_Vernaux);
7055 }
7056
7057 s->size = size;
7058 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7059 if (s->contents == NULL)
7060 return false;
7061
7062 p = s->contents;
7063 for (vn = elf_tdata (output_bfd)->verref;
7064 vn != NULL;
7065 vn = vn->vn_nextref)
7066 {
7067 unsigned int caux;
7068 Elf_Internal_Vernaux *a;
7069 size_t indx;
7070
7071 caux = 0;
7072 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7073 ++caux;
7074
7075 vn->vn_version = VER_NEED_CURRENT;
7076 vn->vn_cnt = caux;
7077 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7078 elf_dt_name (vn->vn_bfd) != NULL
7079 ? elf_dt_name (vn->vn_bfd)
7080 : lbasename (bfd_get_filename
7081 (vn->vn_bfd)),
7082 false);
7083 if (indx == (size_t) -1)
7084 return false;
7085 vn->vn_file = indx;
7086 vn->vn_aux = sizeof (Elf_External_Verneed);
7087 if (vn->vn_nextref == NULL)
7088 vn->vn_next = 0;
7089 else
7090 vn->vn_next = (sizeof (Elf_External_Verneed)
7091 + caux * sizeof (Elf_External_Vernaux));
7092
7093 _bfd_elf_swap_verneed_out (output_bfd, vn,
7094 (Elf_External_Verneed *) p);
7095 p += sizeof (Elf_External_Verneed);
7096
7097 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7098 {
7099 a->vna_hash = bfd_elf_hash (a->vna_nodename);
7100 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7101 a->vna_nodename, false);
7102 if (indx == (size_t) -1)
7103 return false;
7104 a->vna_name = indx;
7105 if (a->vna_nextptr == NULL)
7106 a->vna_next = 0;
7107 else
7108 a->vna_next = sizeof (Elf_External_Vernaux);
7109
7110 _bfd_elf_swap_vernaux_out (output_bfd, a,
7111 (Elf_External_Vernaux *) p);
7112 p += sizeof (Elf_External_Vernaux);
7113 }
7114 }
7115
7116 elf_tdata (output_bfd)->cverrefs = crefs;
7117 }
7118 }
7119
7120 if (bfd_link_relocatable (info)
7121 && !_bfd_elf_size_group_sections (info))
7122 return false;
7123
7124 /* Determine any GNU_STACK segment requirements, after the backend
7125 has had a chance to set a default segment size. */
7126 if (info->execstack)
7127 {
7128 /* If the user has explicitly requested warnings, then generate one even
7129 though the choice is the result of another command line option. */
7130 if (info->warn_execstack == 1)
7131 _bfd_error_handler
7132 (_("\
7133 warning: enabling an executable stack because of -z execstack command line option"));
7134 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
7135 }
7136 else if (info->noexecstack)
7137 elf_stack_flags (output_bfd) = PF_R | PF_W;
7138 else
7139 {
7140 bfd *inputobj;
7141 asection *notesec = NULL;
7142 bfd *noteobj = NULL;
7143 bfd *emptyobj = NULL;
7144 int exec = 0;
7145
7146 for (inputobj = info->input_bfds;
7147 inputobj;
7148 inputobj = inputobj->link.next)
7149 {
7150 asection *s;
7151
7152 if (inputobj->flags
7153 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
7154 continue;
7155 s = inputobj->sections;
7156 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7157 continue;
7158
7159 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
7160 if (s)
7161 {
7162 notesec = s;
7163 if (s->flags & SEC_CODE)
7164 {
7165 noteobj = inputobj;
7166 exec = PF_X;
7167 /* There is no point in scanning the remaining bfds. */
7168 break;
7169 }
7170 }
7171 else if (bed->default_execstack && info->default_execstack)
7172 {
7173 exec = PF_X;
7174 emptyobj = inputobj;
7175 }
7176 }
7177
7178 if (notesec || info->stacksize > 0)
7179 {
7180 if (exec)
7181 {
7182 if (info->warn_execstack != 0)
7183 {
7184 /* PR 29072: Because an executable stack is a serious
7185 security risk, make sure that the user knows that it is
7186 being enabled despite the fact that it was not requested
7187 on the command line. */
7188 if (noteobj)
7189 _bfd_error_handler (_("\
7190 warning: %s: requires executable stack (because the .note.GNU-stack section is executable)"),
7191 bfd_get_filename (noteobj));
7192 else if (emptyobj)
7193 {
7194 _bfd_error_handler (_("\
7195 warning: %s: missing .note.GNU-stack section implies executable stack"),
7196 bfd_get_filename (emptyobj));
7197 _bfd_error_handler (_("\
7198 NOTE: This behaviour is deprecated and will be removed in a future version of the linker"));
7199 }
7200 }
7201 }
7202 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
7203 }
7204
7205 if (notesec && exec && bfd_link_relocatable (info)
7206 && notesec->output_section != bfd_abs_section_ptr)
7207 notesec->output_section->flags |= SEC_CODE;
7208 }
7209
7210 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7211 {
7212 struct elf_info_failed eif;
7213 struct elf_link_hash_entry *h;
7214 asection *dynstr;
7215 asection *s;
7216
7217 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
7218 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
7219
7220 if (info->symbolic)
7221 {
7222 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
7223 return false;
7224 info->flags |= DF_SYMBOLIC;
7225 }
7226
7227 if (rpath != NULL)
7228 {
7229 size_t indx;
7230 bfd_vma tag;
7231
7232 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
7233 true);
7234 if (indx == (size_t) -1)
7235 return false;
7236
7237 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
7238 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
7239 return false;
7240 }
7241
7242 if (filter_shlib != NULL)
7243 {
7244 size_t indx;
7245
7246 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7247 filter_shlib, true);
7248 if (indx == (size_t) -1
7249 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
7250 return false;
7251 }
7252
7253 if (auxiliary_filters != NULL)
7254 {
7255 const char * const *p;
7256
7257 for (p = auxiliary_filters; *p != NULL; p++)
7258 {
7259 size_t indx;
7260
7261 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7262 *p, true);
7263 if (indx == (size_t) -1
7264 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
7265 return false;
7266 }
7267 }
7268
7269 if (audit != NULL)
7270 {
7271 size_t indx;
7272
7273 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
7274 true);
7275 if (indx == (size_t) -1
7276 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
7277 return false;
7278 }
7279
7280 if (depaudit != NULL)
7281 {
7282 size_t indx;
7283
7284 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
7285 true);
7286 if (indx == (size_t) -1
7287 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
7288 return false;
7289 }
7290
7291 eif.info = info;
7292 eif.failed = false;
7293
7294 /* Find all symbols which were defined in a dynamic object and make
7295 the backend pick a reasonable value for them. */
7296 elf_link_hash_traverse (elf_hash_table (info),
7297 _bfd_elf_adjust_dynamic_symbol,
7298 &eif);
7299 if (eif.failed)
7300 return false;
7301
7302 /* Add some entries to the .dynamic section. We fill in some of the
7303 values later, in bfd_elf_final_link, but we must add the entries
7304 now so that we know the final size of the .dynamic section. */
7305
7306 /* If there are initialization and/or finalization functions to
7307 call then add the corresponding DT_INIT/DT_FINI entries. */
7308 h = (info->init_function
7309 ? elf_link_hash_lookup (elf_hash_table (info),
7310 info->init_function, false,
7311 false, false)
7312 : NULL);
7313 if (h != NULL
7314 && (h->ref_regular
7315 || h->def_regular))
7316 {
7317 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
7318 return false;
7319 }
7320 h = (info->fini_function
7321 ? elf_link_hash_lookup (elf_hash_table (info),
7322 info->fini_function, false,
7323 false, false)
7324 : NULL);
7325 if (h != NULL
7326 && (h->ref_regular
7327 || h->def_regular))
7328 {
7329 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
7330 return false;
7331 }
7332
7333 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
7334 if (s != NULL && s->linker_has_input)
7335 {
7336 /* DT_PREINIT_ARRAY is not allowed in shared library. */
7337 if (! bfd_link_executable (info))
7338 {
7339 bfd *sub;
7340 asection *o;
7341
7342 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
7343 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
7344 && (o = sub->sections) != NULL
7345 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
7346 for (o = sub->sections; o != NULL; o = o->next)
7347 if (elf_section_data (o)->this_hdr.sh_type
7348 == SHT_PREINIT_ARRAY)
7349 {
7350 _bfd_error_handler
7351 (_("%pB: .preinit_array section is not allowed in DSO"),
7352 sub);
7353 break;
7354 }
7355
7356 bfd_set_error (bfd_error_nonrepresentable_section);
7357 return false;
7358 }
7359
7360 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
7361 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
7362 return false;
7363 }
7364 s = bfd_get_section_by_name (output_bfd, ".init_array");
7365 if (s != NULL && s->linker_has_input)
7366 {
7367 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
7368 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
7369 return false;
7370 }
7371 s = bfd_get_section_by_name (output_bfd, ".fini_array");
7372 if (s != NULL && s->linker_has_input)
7373 {
7374 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
7375 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
7376 return false;
7377 }
7378
7379 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
7380 /* If .dynstr is excluded from the link, we don't want any of
7381 these tags. Strictly, we should be checking each section
7382 individually; This quick check covers for the case where
7383 someone does a /DISCARD/ : { *(*) }. */
7384 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
7385 {
7386 bfd_size_type strsize;
7387
7388 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7389 if ((info->emit_hash
7390 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
7391 || (info->emit_gnu_hash
7392 && (bed->record_xhash_symbol == NULL
7393 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)))
7394 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
7395 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
7396 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
7397 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
7398 bed->s->sizeof_sym)
7399 || (info->gnu_flags_1
7400 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_FLAGS_1,
7401 info->gnu_flags_1)))
7402 return false;
7403 }
7404 }
7405
7406 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
7407 return false;
7408
7409 /* The backend must work out the sizes of all the other dynamic
7410 sections. */
7411 if (dynobj != NULL
7412 && bed->elf_backend_size_dynamic_sections != NULL
7413 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
7414 return false;
7415
7416 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7417 {
7418 if (elf_tdata (output_bfd)->cverdefs)
7419 {
7420 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
7421
7422 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
7423 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
7424 return false;
7425 }
7426
7427 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
7428 {
7429 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
7430 return false;
7431 }
7432 else if (info->flags & DF_BIND_NOW)
7433 {
7434 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
7435 return false;
7436 }
7437
7438 if (info->flags_1)
7439 {
7440 if (bfd_link_executable (info))
7441 info->flags_1 &= ~ (DF_1_INITFIRST
7442 | DF_1_NODELETE
7443 | DF_1_NOOPEN);
7444 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
7445 return false;
7446 }
7447
7448 if (elf_tdata (output_bfd)->cverrefs)
7449 {
7450 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
7451
7452 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
7453 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
7454 return false;
7455 }
7456
7457 if ((elf_tdata (output_bfd)->cverrefs == 0
7458 && elf_tdata (output_bfd)->cverdefs == 0)
7459 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
7460 {
7461 asection *s;
7462
7463 s = bfd_get_linker_section (dynobj, ".gnu.version");
7464 s->flags |= SEC_EXCLUDE;
7465 }
7466 }
7467 return true;
7468 }
7469
7470 /* Find the first non-excluded output section. We'll use its
7471 section symbol for some emitted relocs. */
7472 void
7473 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7474 {
7475 asection *s;
7476 asection *found = NULL;
7477
7478 for (s = output_bfd->sections; s != NULL; s = s->next)
7479 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7480 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7481 {
7482 found = s;
7483 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7484 break;
7485 }
7486 elf_hash_table (info)->text_index_section = found;
7487 }
7488
7489 /* Find two non-excluded output sections, one for code, one for data.
7490 We'll use their section symbols for some emitted relocs. */
7491 void
7492 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7493 {
7494 asection *s;
7495 asection *found = NULL;
7496
7497 /* Data first, since setting text_index_section changes
7498 _bfd_elf_omit_section_dynsym_default. */
7499 for (s = output_bfd->sections; s != NULL; s = s->next)
7500 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7501 && !(s->flags & SEC_READONLY)
7502 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7503 {
7504 found = s;
7505 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7506 break;
7507 }
7508 elf_hash_table (info)->data_index_section = found;
7509
7510 for (s = output_bfd->sections; s != NULL; s = s->next)
7511 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7512 && (s->flags & SEC_READONLY)
7513 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7514 {
7515 found = s;
7516 break;
7517 }
7518 elf_hash_table (info)->text_index_section = found;
7519 }
7520
7521 #define GNU_HASH_SECTION_NAME(bed) \
7522 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7523
7524 bool
7525 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7526 {
7527 const struct elf_backend_data *bed;
7528 unsigned long section_sym_count;
7529 bfd_size_type dynsymcount = 0;
7530
7531 if (!is_elf_hash_table (info->hash))
7532 return true;
7533
7534 bed = get_elf_backend_data (output_bfd);
7535 (*bed->elf_backend_init_index_section) (output_bfd, info);
7536
7537 /* Assign dynsym indices. In a shared library we generate a section
7538 symbol for each output section, which come first. Next come all
7539 of the back-end allocated local dynamic syms, followed by the rest
7540 of the global symbols.
7541
7542 This is usually not needed for static binaries, however backends
7543 can request to always do it, e.g. the MIPS backend uses dynamic
7544 symbol counts to lay out GOT, which will be produced in the
7545 presence of GOT relocations even in static binaries (holding fixed
7546 data in that case, to satisfy those relocations). */
7547
7548 if (elf_hash_table (info)->dynamic_sections_created
7549 || bed->always_renumber_dynsyms)
7550 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7551 &section_sym_count);
7552
7553 if (elf_hash_table (info)->dynamic_sections_created)
7554 {
7555 bfd *dynobj;
7556 asection *s;
7557 unsigned int dtagcount;
7558
7559 dynobj = elf_hash_table (info)->dynobj;
7560
7561 /* Work out the size of the symbol version section. */
7562 s = bfd_get_linker_section (dynobj, ".gnu.version");
7563 BFD_ASSERT (s != NULL);
7564 if ((s->flags & SEC_EXCLUDE) == 0)
7565 {
7566 s->size = dynsymcount * sizeof (Elf_External_Versym);
7567 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7568 if (s->contents == NULL)
7569 return false;
7570
7571 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7572 return false;
7573 }
7574
7575 /* Set the size of the .dynsym and .hash sections. We counted
7576 the number of dynamic symbols in elf_link_add_object_symbols.
7577 We will build the contents of .dynsym and .hash when we build
7578 the final symbol table, because until then we do not know the
7579 correct value to give the symbols. We built the .dynstr
7580 section as we went along in elf_link_add_object_symbols. */
7581 s = elf_hash_table (info)->dynsym;
7582 BFD_ASSERT (s != NULL);
7583 s->size = dynsymcount * bed->s->sizeof_sym;
7584
7585 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7586 if (s->contents == NULL)
7587 return false;
7588
7589 /* The first entry in .dynsym is a dummy symbol. Clear all the
7590 section syms, in case we don't output them all. */
7591 ++section_sym_count;
7592 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
7593
7594 elf_hash_table (info)->bucketcount = 0;
7595
7596 /* Compute the size of the hashing table. As a side effect this
7597 computes the hash values for all the names we export. */
7598 if (info->emit_hash)
7599 {
7600 unsigned long int *hashcodes;
7601 struct hash_codes_info hashinf;
7602 bfd_size_type amt;
7603 unsigned long int nsyms;
7604 size_t bucketcount;
7605 size_t hash_entry_size;
7606
7607 /* Compute the hash values for all exported symbols. At the same
7608 time store the values in an array so that we could use them for
7609 optimizations. */
7610 amt = dynsymcount * sizeof (unsigned long int);
7611 hashcodes = (unsigned long int *) bfd_malloc (amt);
7612 if (hashcodes == NULL)
7613 return false;
7614 hashinf.hashcodes = hashcodes;
7615 hashinf.error = false;
7616
7617 /* Put all hash values in HASHCODES. */
7618 elf_link_hash_traverse (elf_hash_table (info),
7619 elf_collect_hash_codes, &hashinf);
7620 if (hashinf.error)
7621 {
7622 free (hashcodes);
7623 return false;
7624 }
7625
7626 nsyms = hashinf.hashcodes - hashcodes;
7627 bucketcount
7628 = compute_bucket_count (info, hashcodes, nsyms, 0);
7629 free (hashcodes);
7630
7631 if (bucketcount == 0 && nsyms > 0)
7632 return false;
7633
7634 elf_hash_table (info)->bucketcount = bucketcount;
7635
7636 s = bfd_get_linker_section (dynobj, ".hash");
7637 BFD_ASSERT (s != NULL);
7638 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7639 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
7640 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7641 if (s->contents == NULL)
7642 return false;
7643
7644 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7645 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7646 s->contents + hash_entry_size);
7647 }
7648
7649 if (info->emit_gnu_hash)
7650 {
7651 size_t i, cnt;
7652 unsigned char *contents;
7653 struct collect_gnu_hash_codes cinfo;
7654 bfd_size_type amt;
7655 size_t bucketcount;
7656
7657 memset (&cinfo, 0, sizeof (cinfo));
7658
7659 /* Compute the hash values for all exported symbols. At the same
7660 time store the values in an array so that we could use them for
7661 optimizations. */
7662 amt = dynsymcount * 2 * sizeof (unsigned long int);
7663 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
7664 if (cinfo.hashcodes == NULL)
7665 return false;
7666
7667 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7668 cinfo.min_dynindx = -1;
7669 cinfo.output_bfd = output_bfd;
7670 cinfo.bed = bed;
7671
7672 /* Put all hash values in HASHCODES. */
7673 elf_link_hash_traverse (elf_hash_table (info),
7674 elf_collect_gnu_hash_codes, &cinfo);
7675 if (cinfo.error)
7676 {
7677 free (cinfo.hashcodes);
7678 return false;
7679 }
7680
7681 bucketcount
7682 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7683
7684 if (bucketcount == 0)
7685 {
7686 free (cinfo.hashcodes);
7687 return false;
7688 }
7689
7690 s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (bed));
7691 BFD_ASSERT (s != NULL);
7692
7693 if (cinfo.nsyms == 0)
7694 {
7695 /* Empty .gnu.hash or .MIPS.xhash section is special. */
7696 BFD_ASSERT (cinfo.min_dynindx == -1);
7697 free (cinfo.hashcodes);
7698 s->size = 5 * 4 + bed->s->arch_size / 8;
7699 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7700 if (contents == NULL)
7701 return false;
7702 s->contents = contents;
7703 /* 1 empty bucket. */
7704 bfd_put_32 (output_bfd, 1, contents);
7705 /* SYMIDX above the special symbol 0. */
7706 bfd_put_32 (output_bfd, 1, contents + 4);
7707 /* Just one word for bitmask. */
7708 bfd_put_32 (output_bfd, 1, contents + 8);
7709 /* Only hash fn bloom filter. */
7710 bfd_put_32 (output_bfd, 0, contents + 12);
7711 /* No hashes are valid - empty bitmask. */
7712 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7713 /* No hashes in the only bucket. */
7714 bfd_put_32 (output_bfd, 0,
7715 contents + 16 + bed->s->arch_size / 8);
7716 }
7717 else
7718 {
7719 unsigned long int maskwords, maskbitslog2, x;
7720 BFD_ASSERT (cinfo.min_dynindx != -1);
7721
7722 x = cinfo.nsyms;
7723 maskbitslog2 = 1;
7724 while ((x >>= 1) != 0)
7725 ++maskbitslog2;
7726 if (maskbitslog2 < 3)
7727 maskbitslog2 = 5;
7728 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7729 maskbitslog2 = maskbitslog2 + 3;
7730 else
7731 maskbitslog2 = maskbitslog2 + 2;
7732 if (bed->s->arch_size == 64)
7733 {
7734 if (maskbitslog2 == 5)
7735 maskbitslog2 = 6;
7736 cinfo.shift1 = 6;
7737 }
7738 else
7739 cinfo.shift1 = 5;
7740 cinfo.mask = (1 << cinfo.shift1) - 1;
7741 cinfo.shift2 = maskbitslog2;
7742 cinfo.maskbits = 1 << maskbitslog2;
7743 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7744 amt = bucketcount * sizeof (unsigned long int) * 2;
7745 amt += maskwords * sizeof (bfd_vma);
7746 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
7747 if (cinfo.bitmask == NULL)
7748 {
7749 free (cinfo.hashcodes);
7750 return false;
7751 }
7752
7753 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
7754 cinfo.indx = cinfo.counts + bucketcount;
7755 cinfo.symindx = dynsymcount - cinfo.nsyms;
7756 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7757
7758 /* Determine how often each hash bucket is used. */
7759 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7760 for (i = 0; i < cinfo.nsyms; ++i)
7761 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7762
7763 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7764 if (cinfo.counts[i] != 0)
7765 {
7766 cinfo.indx[i] = cnt;
7767 cnt += cinfo.counts[i];
7768 }
7769 BFD_ASSERT (cnt == dynsymcount);
7770 cinfo.bucketcount = bucketcount;
7771 cinfo.local_indx = cinfo.min_dynindx;
7772
7773 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7774 s->size += cinfo.maskbits / 8;
7775 if (bed->record_xhash_symbol != NULL)
7776 s->size += cinfo.nsyms * 4;
7777 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7778 if (contents == NULL)
7779 {
7780 free (cinfo.bitmask);
7781 free (cinfo.hashcodes);
7782 return false;
7783 }
7784
7785 s->contents = contents;
7786 bfd_put_32 (output_bfd, bucketcount, contents);
7787 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7788 bfd_put_32 (output_bfd, maskwords, contents + 8);
7789 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7790 contents += 16 + cinfo.maskbits / 8;
7791
7792 for (i = 0; i < bucketcount; ++i)
7793 {
7794 if (cinfo.counts[i] == 0)
7795 bfd_put_32 (output_bfd, 0, contents);
7796 else
7797 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7798 contents += 4;
7799 }
7800
7801 cinfo.contents = contents;
7802
7803 cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents;
7804 /* Renumber dynamic symbols, if populating .gnu.hash section.
7805 If using .MIPS.xhash, populate the translation table. */
7806 elf_link_hash_traverse (elf_hash_table (info),
7807 elf_gnu_hash_process_symidx, &cinfo);
7808
7809 contents = s->contents + 16;
7810 for (i = 0; i < maskwords; ++i)
7811 {
7812 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7813 contents);
7814 contents += bed->s->arch_size / 8;
7815 }
7816
7817 free (cinfo.bitmask);
7818 free (cinfo.hashcodes);
7819 }
7820 }
7821
7822 s = bfd_get_linker_section (dynobj, ".dynstr");
7823 BFD_ASSERT (s != NULL);
7824
7825 elf_finalize_dynstr (output_bfd, info);
7826
7827 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7828
7829 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7830 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7831 return false;
7832 }
7833
7834 return true;
7835 }
7836 \f
7837 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7838
7839 static void
7840 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7841 asection *sec)
7842 {
7843 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7844 sec->sec_info_type = SEC_INFO_TYPE_NONE;
7845 }
7846
7847 /* Finish SHF_MERGE section merging. */
7848
7849 bool
7850 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
7851 {
7852 bfd *ibfd;
7853 asection *sec;
7854
7855 if (!is_elf_hash_table (info->hash))
7856 return false;
7857
7858 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7859 if ((ibfd->flags & DYNAMIC) == 0
7860 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
7861 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7862 == get_elf_backend_data (obfd)->s->elfclass))
7863 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7864 if ((sec->flags & SEC_MERGE) != 0
7865 && !bfd_is_abs_section (sec->output_section))
7866 {
7867 struct bfd_elf_section_data *secdata;
7868
7869 secdata = elf_section_data (sec);
7870 if (! _bfd_add_merge_section (obfd,
7871 &elf_hash_table (info)->merge_info,
7872 sec, &secdata->sec_info))
7873 return false;
7874 else if (secdata->sec_info)
7875 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
7876 }
7877
7878 if (elf_hash_table (info)->merge_info != NULL)
7879 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
7880 merge_sections_remove_hook);
7881 return true;
7882 }
7883
7884 /* Create an entry in an ELF linker hash table. */
7885
7886 struct bfd_hash_entry *
7887 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7888 struct bfd_hash_table *table,
7889 const char *string)
7890 {
7891 /* Allocate the structure if it has not already been allocated by a
7892 subclass. */
7893 if (entry == NULL)
7894 {
7895 entry = (struct bfd_hash_entry *)
7896 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
7897 if (entry == NULL)
7898 return entry;
7899 }
7900
7901 /* Call the allocation method of the superclass. */
7902 entry = _bfd_link_hash_newfunc (entry, table, string);
7903 if (entry != NULL)
7904 {
7905 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7906 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7907
7908 /* Set local fields. */
7909 ret->indx = -1;
7910 ret->dynindx = -1;
7911 ret->got = htab->init_got_refcount;
7912 ret->plt = htab->init_plt_refcount;
7913 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7914 - offsetof (struct elf_link_hash_entry, size)));
7915 /* Assume that we have been called by a non-ELF symbol reader.
7916 This flag is then reset by the code which reads an ELF input
7917 file. This ensures that a symbol created by a non-ELF symbol
7918 reader will have the flag set correctly. */
7919 ret->non_elf = 1;
7920 }
7921
7922 return entry;
7923 }
7924
7925 /* Copy data from an indirect symbol to its direct symbol, hiding the
7926 old indirect symbol. Also used for copying flags to a weakdef. */
7927
7928 void
7929 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7930 struct elf_link_hash_entry *dir,
7931 struct elf_link_hash_entry *ind)
7932 {
7933 struct elf_link_hash_table *htab;
7934
7935 if (ind->dyn_relocs != NULL)
7936 {
7937 if (dir->dyn_relocs != NULL)
7938 {
7939 struct elf_dyn_relocs **pp;
7940 struct elf_dyn_relocs *p;
7941
7942 /* Add reloc counts against the indirect sym to the direct sym
7943 list. Merge any entries against the same section. */
7944 for (pp = &ind->dyn_relocs; (p = *pp) != NULL; )
7945 {
7946 struct elf_dyn_relocs *q;
7947
7948 for (q = dir->dyn_relocs; q != NULL; q = q->next)
7949 if (q->sec == p->sec)
7950 {
7951 q->pc_count += p->pc_count;
7952 q->count += p->count;
7953 *pp = p->next;
7954 break;
7955 }
7956 if (q == NULL)
7957 pp = &p->next;
7958 }
7959 *pp = dir->dyn_relocs;
7960 }
7961
7962 dir->dyn_relocs = ind->dyn_relocs;
7963 ind->dyn_relocs = NULL;
7964 }
7965
7966 /* Copy down any references that we may have already seen to the
7967 symbol which just became indirect. */
7968
7969 if (dir->versioned != versioned_hidden)
7970 dir->ref_dynamic |= ind->ref_dynamic;
7971 dir->ref_regular |= ind->ref_regular;
7972 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7973 dir->non_got_ref |= ind->non_got_ref;
7974 dir->needs_plt |= ind->needs_plt;
7975 dir->pointer_equality_needed |= ind->pointer_equality_needed;
7976
7977 if (ind->root.type != bfd_link_hash_indirect)
7978 return;
7979
7980 /* Copy over the global and procedure linkage table refcount entries.
7981 These may have been already set up by a check_relocs routine. */
7982 htab = elf_hash_table (info);
7983 if (ind->got.refcount > htab->init_got_refcount.refcount)
7984 {
7985 if (dir->got.refcount < 0)
7986 dir->got.refcount = 0;
7987 dir->got.refcount += ind->got.refcount;
7988 ind->got.refcount = htab->init_got_refcount.refcount;
7989 }
7990
7991 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7992 {
7993 if (dir->plt.refcount < 0)
7994 dir->plt.refcount = 0;
7995 dir->plt.refcount += ind->plt.refcount;
7996 ind->plt.refcount = htab->init_plt_refcount.refcount;
7997 }
7998
7999 if (ind->dynindx != -1)
8000 {
8001 if (dir->dynindx != -1)
8002 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
8003 dir->dynindx = ind->dynindx;
8004 dir->dynstr_index = ind->dynstr_index;
8005 ind->dynindx = -1;
8006 ind->dynstr_index = 0;
8007 }
8008 }
8009
8010 void
8011 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
8012 struct elf_link_hash_entry *h,
8013 bool force_local)
8014 {
8015 /* STT_GNU_IFUNC symbol must go through PLT. */
8016 if (h->type != STT_GNU_IFUNC)
8017 {
8018 h->plt = elf_hash_table (info)->init_plt_offset;
8019 h->needs_plt = 0;
8020 }
8021 if (force_local)
8022 {
8023 h->forced_local = 1;
8024 if (h->dynindx != -1)
8025 {
8026 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8027 h->dynstr_index);
8028 h->dynindx = -1;
8029 h->dynstr_index = 0;
8030 }
8031 }
8032 }
8033
8034 /* Hide a symbol. */
8035
8036 void
8037 _bfd_elf_link_hide_symbol (bfd *output_bfd,
8038 struct bfd_link_info *info,
8039 struct bfd_link_hash_entry *h)
8040 {
8041 if (is_elf_hash_table (info->hash))
8042 {
8043 const struct elf_backend_data *bed
8044 = get_elf_backend_data (output_bfd);
8045 struct elf_link_hash_entry *eh
8046 = (struct elf_link_hash_entry *) h;
8047 bed->elf_backend_hide_symbol (info, eh, true);
8048 eh->def_dynamic = 0;
8049 eh->ref_dynamic = 0;
8050 eh->dynamic_def = 0;
8051 }
8052 }
8053
8054 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
8055 caller. */
8056
8057 bool
8058 _bfd_elf_link_hash_table_init
8059 (struct elf_link_hash_table *table,
8060 bfd *abfd,
8061 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
8062 struct bfd_hash_table *,
8063 const char *),
8064 unsigned int entsize,
8065 enum elf_target_id target_id)
8066 {
8067 bool ret;
8068 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
8069
8070 table->init_got_refcount.refcount = can_refcount - 1;
8071 table->init_plt_refcount.refcount = can_refcount - 1;
8072 table->init_got_offset.offset = -(bfd_vma) 1;
8073 table->init_plt_offset.offset = -(bfd_vma) 1;
8074 /* The first dynamic symbol is a dummy. */
8075 table->dynsymcount = 1;
8076
8077 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
8078
8079 table->root.type = bfd_link_elf_hash_table;
8080 table->hash_table_id = target_id;
8081 table->target_os = get_elf_backend_data (abfd)->target_os;
8082
8083 return ret;
8084 }
8085
8086 /* Create an ELF linker hash table. */
8087
8088 struct bfd_link_hash_table *
8089 _bfd_elf_link_hash_table_create (bfd *abfd)
8090 {
8091 struct elf_link_hash_table *ret;
8092 size_t amt = sizeof (struct elf_link_hash_table);
8093
8094 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
8095 if (ret == NULL)
8096 return NULL;
8097
8098 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
8099 sizeof (struct elf_link_hash_entry),
8100 GENERIC_ELF_DATA))
8101 {
8102 free (ret);
8103 return NULL;
8104 }
8105 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
8106
8107 return &ret->root;
8108 }
8109
8110 /* Destroy an ELF linker hash table. */
8111
8112 void
8113 _bfd_elf_link_hash_table_free (bfd *obfd)
8114 {
8115 struct elf_link_hash_table *htab;
8116
8117 htab = (struct elf_link_hash_table *) obfd->link.hash;
8118 if (htab->dynstr != NULL)
8119 _bfd_elf_strtab_free (htab->dynstr);
8120 _bfd_merge_sections_free (htab->merge_info);
8121 _bfd_generic_link_hash_table_free (obfd);
8122 }
8123
8124 /* This is a hook for the ELF emulation code in the generic linker to
8125 tell the backend linker what file name to use for the DT_NEEDED
8126 entry for a dynamic object. */
8127
8128 void
8129 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
8130 {
8131 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8132 && bfd_get_format (abfd) == bfd_object)
8133 elf_dt_name (abfd) = name;
8134 }
8135
8136 int
8137 bfd_elf_get_dyn_lib_class (bfd *abfd)
8138 {
8139 int lib_class;
8140 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8141 && bfd_get_format (abfd) == bfd_object)
8142 lib_class = elf_dyn_lib_class (abfd);
8143 else
8144 lib_class = 0;
8145 return lib_class;
8146 }
8147
8148 void
8149 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
8150 {
8151 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8152 && bfd_get_format (abfd) == bfd_object)
8153 elf_dyn_lib_class (abfd) = lib_class;
8154 }
8155
8156 /* Get the list of DT_NEEDED entries for a link. This is a hook for
8157 the linker ELF emulation code. */
8158
8159 struct bfd_link_needed_list *
8160 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
8161 struct bfd_link_info *info)
8162 {
8163 if (! is_elf_hash_table (info->hash))
8164 return NULL;
8165 return elf_hash_table (info)->needed;
8166 }
8167
8168 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
8169 hook for the linker ELF emulation code. */
8170
8171 struct bfd_link_needed_list *
8172 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
8173 struct bfd_link_info *info)
8174 {
8175 if (! is_elf_hash_table (info->hash))
8176 return NULL;
8177 return elf_hash_table (info)->runpath;
8178 }
8179
8180 /* Get the name actually used for a dynamic object for a link. This
8181 is the SONAME entry if there is one. Otherwise, it is the string
8182 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
8183
8184 const char *
8185 bfd_elf_get_dt_soname (bfd *abfd)
8186 {
8187 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8188 && bfd_get_format (abfd) == bfd_object)
8189 return elf_dt_name (abfd);
8190 return NULL;
8191 }
8192
8193 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
8194 the ELF linker emulation code. */
8195
8196 bool
8197 bfd_elf_get_bfd_needed_list (bfd *abfd,
8198 struct bfd_link_needed_list **pneeded)
8199 {
8200 asection *s;
8201 bfd_byte *dynbuf = NULL;
8202 unsigned int elfsec;
8203 unsigned long shlink;
8204 bfd_byte *extdyn, *extdynend;
8205 size_t extdynsize;
8206 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
8207
8208 *pneeded = NULL;
8209
8210 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
8211 || bfd_get_format (abfd) != bfd_object)
8212 return true;
8213
8214 s = bfd_get_section_by_name (abfd, ".dynamic");
8215 if (s == NULL || s->size == 0)
8216 return true;
8217
8218 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
8219 goto error_return;
8220
8221 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
8222 if (elfsec == SHN_BAD)
8223 goto error_return;
8224
8225 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
8226
8227 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
8228 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
8229
8230 extdyn = dynbuf;
8231 extdynend = extdyn + s->size;
8232 for (; extdyn < extdynend; extdyn += extdynsize)
8233 {
8234 Elf_Internal_Dyn dyn;
8235
8236 (*swap_dyn_in) (abfd, extdyn, &dyn);
8237
8238 if (dyn.d_tag == DT_NULL)
8239 break;
8240
8241 if (dyn.d_tag == DT_NEEDED)
8242 {
8243 const char *string;
8244 struct bfd_link_needed_list *l;
8245 unsigned int tagv = dyn.d_un.d_val;
8246 size_t amt;
8247
8248 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
8249 if (string == NULL)
8250 goto error_return;
8251
8252 amt = sizeof *l;
8253 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
8254 if (l == NULL)
8255 goto error_return;
8256
8257 l->by = abfd;
8258 l->name = string;
8259 l->next = *pneeded;
8260 *pneeded = l;
8261 }
8262 }
8263
8264 free (dynbuf);
8265
8266 return true;
8267
8268 error_return:
8269 free (dynbuf);
8270 return false;
8271 }
8272
8273 struct elf_symbuf_symbol
8274 {
8275 unsigned long st_name; /* Symbol name, index in string tbl */
8276 unsigned char st_info; /* Type and binding attributes */
8277 unsigned char st_other; /* Visibilty, and target specific */
8278 };
8279
8280 struct elf_symbuf_head
8281 {
8282 struct elf_symbuf_symbol *ssym;
8283 size_t count;
8284 unsigned int st_shndx;
8285 };
8286
8287 struct elf_symbol
8288 {
8289 union
8290 {
8291 Elf_Internal_Sym *isym;
8292 struct elf_symbuf_symbol *ssym;
8293 void *p;
8294 } u;
8295 const char *name;
8296 };
8297
8298 /* Sort references to symbols by ascending section number. */
8299
8300 static int
8301 elf_sort_elf_symbol (const void *arg1, const void *arg2)
8302 {
8303 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
8304 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
8305
8306 if (s1->st_shndx != s2->st_shndx)
8307 return s1->st_shndx > s2->st_shndx ? 1 : -1;
8308 /* Final sort by the address of the sym in the symbuf ensures
8309 a stable sort. */
8310 if (s1 != s2)
8311 return s1 > s2 ? 1 : -1;
8312 return 0;
8313 }
8314
8315 static int
8316 elf_sym_name_compare (const void *arg1, const void *arg2)
8317 {
8318 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
8319 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
8320 int ret = strcmp (s1->name, s2->name);
8321 if (ret != 0)
8322 return ret;
8323 if (s1->u.p != s2->u.p)
8324 return s1->u.p > s2->u.p ? 1 : -1;
8325 return 0;
8326 }
8327
8328 static struct elf_symbuf_head *
8329 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
8330 {
8331 Elf_Internal_Sym **ind, **indbufend, **indbuf;
8332 struct elf_symbuf_symbol *ssym;
8333 struct elf_symbuf_head *ssymbuf, *ssymhead;
8334 size_t i, shndx_count, total_size, amt;
8335
8336 amt = symcount * sizeof (*indbuf);
8337 indbuf = (Elf_Internal_Sym **) bfd_malloc (amt);
8338 if (indbuf == NULL)
8339 return NULL;
8340
8341 for (ind = indbuf, i = 0; i < symcount; i++)
8342 if (isymbuf[i].st_shndx != SHN_UNDEF)
8343 *ind++ = &isymbuf[i];
8344 indbufend = ind;
8345
8346 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
8347 elf_sort_elf_symbol);
8348
8349 shndx_count = 0;
8350 if (indbufend > indbuf)
8351 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
8352 if (ind[0]->st_shndx != ind[1]->st_shndx)
8353 shndx_count++;
8354
8355 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
8356 + (indbufend - indbuf) * sizeof (*ssym));
8357 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
8358 if (ssymbuf == NULL)
8359 {
8360 free (indbuf);
8361 return NULL;
8362 }
8363
8364 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
8365 ssymbuf->ssym = NULL;
8366 ssymbuf->count = shndx_count;
8367 ssymbuf->st_shndx = 0;
8368 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
8369 {
8370 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
8371 {
8372 ssymhead++;
8373 ssymhead->ssym = ssym;
8374 ssymhead->count = 0;
8375 ssymhead->st_shndx = (*ind)->st_shndx;
8376 }
8377 ssym->st_name = (*ind)->st_name;
8378 ssym->st_info = (*ind)->st_info;
8379 ssym->st_other = (*ind)->st_other;
8380 ssymhead->count++;
8381 }
8382 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
8383 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
8384 == total_size));
8385
8386 free (indbuf);
8387 return ssymbuf;
8388 }
8389
8390 /* Check if 2 sections define the same set of local and global
8391 symbols. */
8392
8393 static bool
8394 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
8395 struct bfd_link_info *info)
8396 {
8397 bfd *bfd1, *bfd2;
8398 const struct elf_backend_data *bed1, *bed2;
8399 Elf_Internal_Shdr *hdr1, *hdr2;
8400 size_t symcount1, symcount2;
8401 Elf_Internal_Sym *isymbuf1, *isymbuf2;
8402 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
8403 Elf_Internal_Sym *isym, *isymend;
8404 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
8405 size_t count1, count2, sec_count1, sec_count2, i;
8406 unsigned int shndx1, shndx2;
8407 bool result;
8408 bool ignore_section_symbol_p;
8409
8410 bfd1 = sec1->owner;
8411 bfd2 = sec2->owner;
8412
8413 /* Both sections have to be in ELF. */
8414 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
8415 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
8416 return false;
8417
8418 if (elf_section_type (sec1) != elf_section_type (sec2))
8419 return false;
8420
8421 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
8422 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
8423 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
8424 return false;
8425
8426 bed1 = get_elf_backend_data (bfd1);
8427 bed2 = get_elf_backend_data (bfd2);
8428 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
8429 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
8430 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
8431 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
8432
8433 if (symcount1 == 0 || symcount2 == 0)
8434 return false;
8435
8436 result = false;
8437 isymbuf1 = NULL;
8438 isymbuf2 = NULL;
8439 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
8440 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
8441
8442 /* Ignore section symbols only when matching non-debugging sections
8443 or linkonce section with comdat section. */
8444 ignore_section_symbol_p
8445 = ((sec1->flags & SEC_DEBUGGING) == 0
8446 || ((elf_section_flags (sec1) & SHF_GROUP)
8447 != (elf_section_flags (sec2) & SHF_GROUP)));
8448
8449 if (ssymbuf1 == NULL)
8450 {
8451 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
8452 NULL, NULL, NULL);
8453 if (isymbuf1 == NULL)
8454 goto done;
8455
8456 if (info != NULL && !info->reduce_memory_overheads)
8457 {
8458 ssymbuf1 = elf_create_symbuf (symcount1, isymbuf1);
8459 elf_tdata (bfd1)->symbuf = ssymbuf1;
8460 }
8461 }
8462
8463 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
8464 {
8465 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
8466 NULL, NULL, NULL);
8467 if (isymbuf2 == NULL)
8468 goto done;
8469
8470 if (ssymbuf1 != NULL && info != NULL && !info->reduce_memory_overheads)
8471 {
8472 ssymbuf2 = elf_create_symbuf (symcount2, isymbuf2);
8473 elf_tdata (bfd2)->symbuf = ssymbuf2;
8474 }
8475 }
8476
8477 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
8478 {
8479 /* Optimized faster version. */
8480 size_t lo, hi, mid;
8481 struct elf_symbol *symp;
8482 struct elf_symbuf_symbol *ssym, *ssymend;
8483
8484 lo = 0;
8485 hi = ssymbuf1->count;
8486 ssymbuf1++;
8487 count1 = 0;
8488 sec_count1 = 0;
8489 while (lo < hi)
8490 {
8491 mid = (lo + hi) / 2;
8492 if (shndx1 < ssymbuf1[mid].st_shndx)
8493 hi = mid;
8494 else if (shndx1 > ssymbuf1[mid].st_shndx)
8495 lo = mid + 1;
8496 else
8497 {
8498 count1 = ssymbuf1[mid].count;
8499 ssymbuf1 += mid;
8500 break;
8501 }
8502 }
8503 if (ignore_section_symbol_p)
8504 {
8505 for (i = 0; i < count1; i++)
8506 if (ELF_ST_TYPE (ssymbuf1->ssym[i].st_info) == STT_SECTION)
8507 sec_count1++;
8508 count1 -= sec_count1;
8509 }
8510
8511 lo = 0;
8512 hi = ssymbuf2->count;
8513 ssymbuf2++;
8514 count2 = 0;
8515 sec_count2 = 0;
8516 while (lo < hi)
8517 {
8518 mid = (lo + hi) / 2;
8519 if (shndx2 < ssymbuf2[mid].st_shndx)
8520 hi = mid;
8521 else if (shndx2 > ssymbuf2[mid].st_shndx)
8522 lo = mid + 1;
8523 else
8524 {
8525 count2 = ssymbuf2[mid].count;
8526 ssymbuf2 += mid;
8527 break;
8528 }
8529 }
8530 if (ignore_section_symbol_p)
8531 {
8532 for (i = 0; i < count2; i++)
8533 if (ELF_ST_TYPE (ssymbuf2->ssym[i].st_info) == STT_SECTION)
8534 sec_count2++;
8535 count2 -= sec_count2;
8536 }
8537
8538 if (count1 == 0 || count2 == 0 || count1 != count2)
8539 goto done;
8540
8541 symtable1
8542 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
8543 symtable2
8544 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
8545 if (symtable1 == NULL || symtable2 == NULL)
8546 goto done;
8547
8548 symp = symtable1;
8549 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1 + sec_count1;
8550 ssym < ssymend; ssym++)
8551 if (sec_count1 == 0
8552 || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
8553 {
8554 symp->u.ssym = ssym;
8555 symp->name = bfd_elf_string_from_elf_section (bfd1,
8556 hdr1->sh_link,
8557 ssym->st_name);
8558 symp++;
8559 }
8560
8561 symp = symtable2;
8562 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2 + sec_count2;
8563 ssym < ssymend; ssym++)
8564 if (sec_count2 == 0
8565 || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
8566 {
8567 symp->u.ssym = ssym;
8568 symp->name = bfd_elf_string_from_elf_section (bfd2,
8569 hdr2->sh_link,
8570 ssym->st_name);
8571 symp++;
8572 }
8573
8574 /* Sort symbol by name. */
8575 qsort (symtable1, count1, sizeof (struct elf_symbol),
8576 elf_sym_name_compare);
8577 qsort (symtable2, count1, sizeof (struct elf_symbol),
8578 elf_sym_name_compare);
8579
8580 for (i = 0; i < count1; i++)
8581 /* Two symbols must have the same binding, type and name. */
8582 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8583 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8584 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8585 goto done;
8586
8587 result = true;
8588 goto done;
8589 }
8590
8591 symtable1 = (struct elf_symbol *)
8592 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8593 symtable2 = (struct elf_symbol *)
8594 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
8595 if (symtable1 == NULL || symtable2 == NULL)
8596 goto done;
8597
8598 /* Count definitions in the section. */
8599 count1 = 0;
8600 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
8601 if (isym->st_shndx == shndx1
8602 && (!ignore_section_symbol_p
8603 || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
8604 symtable1[count1++].u.isym = isym;
8605
8606 count2 = 0;
8607 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
8608 if (isym->st_shndx == shndx2
8609 && (!ignore_section_symbol_p
8610 || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
8611 symtable2[count2++].u.isym = isym;
8612
8613 if (count1 == 0 || count2 == 0 || count1 != count2)
8614 goto done;
8615
8616 for (i = 0; i < count1; i++)
8617 symtable1[i].name
8618 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8619 symtable1[i].u.isym->st_name);
8620
8621 for (i = 0; i < count2; i++)
8622 symtable2[i].name
8623 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8624 symtable2[i].u.isym->st_name);
8625
8626 /* Sort symbol by name. */
8627 qsort (symtable1, count1, sizeof (struct elf_symbol),
8628 elf_sym_name_compare);
8629 qsort (symtable2, count1, sizeof (struct elf_symbol),
8630 elf_sym_name_compare);
8631
8632 for (i = 0; i < count1; i++)
8633 /* Two symbols must have the same binding, type and name. */
8634 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8635 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8636 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8637 goto done;
8638
8639 result = true;
8640
8641 done:
8642 free (symtable1);
8643 free (symtable2);
8644 free (isymbuf1);
8645 free (isymbuf2);
8646
8647 return result;
8648 }
8649
8650 /* Return TRUE if 2 section types are compatible. */
8651
8652 bool
8653 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8654 bfd *bbfd, const asection *bsec)
8655 {
8656 if (asec == NULL
8657 || bsec == NULL
8658 || abfd->xvec->flavour != bfd_target_elf_flavour
8659 || bbfd->xvec->flavour != bfd_target_elf_flavour)
8660 return true;
8661
8662 return elf_section_type (asec) == elf_section_type (bsec);
8663 }
8664 \f
8665 /* Final phase of ELF linker. */
8666
8667 /* A structure we use to avoid passing large numbers of arguments. */
8668
8669 struct elf_final_link_info
8670 {
8671 /* General link information. */
8672 struct bfd_link_info *info;
8673 /* Output BFD. */
8674 bfd *output_bfd;
8675 /* Symbol string table. */
8676 struct elf_strtab_hash *symstrtab;
8677 /* .hash section. */
8678 asection *hash_sec;
8679 /* symbol version section (.gnu.version). */
8680 asection *symver_sec;
8681 /* Buffer large enough to hold contents of any section. */
8682 bfd_byte *contents;
8683 /* Buffer large enough to hold external relocs of any section. */
8684 void *external_relocs;
8685 /* Buffer large enough to hold internal relocs of any section. */
8686 Elf_Internal_Rela *internal_relocs;
8687 /* Buffer large enough to hold external local symbols of any input
8688 BFD. */
8689 bfd_byte *external_syms;
8690 /* And a buffer for symbol section indices. */
8691 Elf_External_Sym_Shndx *locsym_shndx;
8692 /* Buffer large enough to hold internal local symbols of any input
8693 BFD. */
8694 Elf_Internal_Sym *internal_syms;
8695 /* Array large enough to hold a symbol index for each local symbol
8696 of any input BFD. */
8697 long *indices;
8698 /* Array large enough to hold a section pointer for each local
8699 symbol of any input BFD. */
8700 asection **sections;
8701 /* Buffer for SHT_SYMTAB_SHNDX section. */
8702 Elf_External_Sym_Shndx *symshndxbuf;
8703 /* Number of STT_FILE syms seen. */
8704 size_t filesym_count;
8705 /* Local symbol hash table. */
8706 struct bfd_hash_table local_hash_table;
8707 };
8708
8709 struct local_hash_entry
8710 {
8711 /* Base hash table entry structure. */
8712 struct bfd_hash_entry root;
8713 /* Size of the local symbol name. */
8714 size_t size;
8715 /* Number of the duplicated local symbol names. */
8716 long count;
8717 };
8718
8719 /* Create an entry in the local symbol hash table. */
8720
8721 static struct bfd_hash_entry *
8722 local_hash_newfunc (struct bfd_hash_entry *entry,
8723 struct bfd_hash_table *table,
8724 const char *string)
8725 {
8726
8727 /* Allocate the structure if it has not already been allocated by a
8728 subclass. */
8729 if (entry == NULL)
8730 {
8731 entry = bfd_hash_allocate (table,
8732 sizeof (struct local_hash_entry));
8733 if (entry == NULL)
8734 return entry;
8735 }
8736
8737 /* Call the allocation method of the superclass. */
8738 entry = bfd_hash_newfunc (entry, table, string);
8739 if (entry != NULL)
8740 {
8741 ((struct local_hash_entry *) entry)->count = 0;
8742 ((struct local_hash_entry *) entry)->size = 0;
8743 }
8744
8745 return entry;
8746 }
8747
8748 /* This struct is used to pass information to elf_link_output_extsym. */
8749
8750 struct elf_outext_info
8751 {
8752 bool failed;
8753 bool localsyms;
8754 bool file_sym_done;
8755 struct elf_final_link_info *flinfo;
8756 };
8757
8758
8759 /* Support for evaluating a complex relocation.
8760
8761 Complex relocations are generalized, self-describing relocations. The
8762 implementation of them consists of two parts: complex symbols, and the
8763 relocations themselves.
8764
8765 The relocations use a reserved elf-wide relocation type code (R_RELC
8766 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8767 information (start bit, end bit, word width, etc) into the addend. This
8768 information is extracted from CGEN-generated operand tables within gas.
8769
8770 Complex symbols are mangled symbols (STT_RELC external / BSF_RELC
8771 internal) representing prefix-notation expressions, including but not
8772 limited to those sorts of expressions normally encoded as addends in the
8773 addend field. The symbol mangling format is:
8774
8775 <node> := <literal>
8776 | <unary-operator> ':' <node>
8777 | <binary-operator> ':' <node> ':' <node>
8778 ;
8779
8780 <literal> := 's' <digits=N> ':' <N character symbol name>
8781 | 'S' <digits=N> ':' <N character section name>
8782 | '#' <hexdigits>
8783 ;
8784
8785 <binary-operator> := as in C
8786 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8787
8788 static void
8789 set_symbol_value (bfd *bfd_with_globals,
8790 Elf_Internal_Sym *isymbuf,
8791 size_t locsymcount,
8792 size_t symidx,
8793 bfd_vma val)
8794 {
8795 struct elf_link_hash_entry **sym_hashes;
8796 struct elf_link_hash_entry *h;
8797 size_t extsymoff = locsymcount;
8798
8799 if (symidx < locsymcount)
8800 {
8801 Elf_Internal_Sym *sym;
8802
8803 sym = isymbuf + symidx;
8804 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8805 {
8806 /* It is a local symbol: move it to the
8807 "absolute" section and give it a value. */
8808 sym->st_shndx = SHN_ABS;
8809 sym->st_value = val;
8810 return;
8811 }
8812 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8813 extsymoff = 0;
8814 }
8815
8816 /* It is a global symbol: set its link type
8817 to "defined" and give it a value. */
8818
8819 sym_hashes = elf_sym_hashes (bfd_with_globals);
8820 h = sym_hashes [symidx - extsymoff];
8821 while (h->root.type == bfd_link_hash_indirect
8822 || h->root.type == bfd_link_hash_warning)
8823 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8824 h->root.type = bfd_link_hash_defined;
8825 h->root.u.def.value = val;
8826 h->root.u.def.section = bfd_abs_section_ptr;
8827 }
8828
8829 static bool
8830 resolve_symbol (const char *name,
8831 bfd *input_bfd,
8832 struct elf_final_link_info *flinfo,
8833 bfd_vma *result,
8834 Elf_Internal_Sym *isymbuf,
8835 size_t locsymcount)
8836 {
8837 Elf_Internal_Sym *sym;
8838 struct bfd_link_hash_entry *global_entry;
8839 const char *candidate = NULL;
8840 Elf_Internal_Shdr *symtab_hdr;
8841 size_t i;
8842
8843 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8844
8845 for (i = 0; i < locsymcount; ++ i)
8846 {
8847 sym = isymbuf + i;
8848
8849 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8850 continue;
8851
8852 candidate = bfd_elf_string_from_elf_section (input_bfd,
8853 symtab_hdr->sh_link,
8854 sym->st_name);
8855 #ifdef DEBUG
8856 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8857 name, candidate, (unsigned long) sym->st_value);
8858 #endif
8859 if (candidate && strcmp (candidate, name) == 0)
8860 {
8861 asection *sec = flinfo->sections [i];
8862
8863 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8864 *result += sec->output_offset + sec->output_section->vma;
8865 #ifdef DEBUG
8866 printf ("Found symbol with value %8.8lx\n",
8867 (unsigned long) *result);
8868 #endif
8869 return true;
8870 }
8871 }
8872
8873 /* Hmm, haven't found it yet. perhaps it is a global. */
8874 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
8875 false, false, true);
8876 if (!global_entry)
8877 return false;
8878
8879 if (global_entry->type == bfd_link_hash_defined
8880 || global_entry->type == bfd_link_hash_defweak)
8881 {
8882 *result = (global_entry->u.def.value
8883 + global_entry->u.def.section->output_section->vma
8884 + global_entry->u.def.section->output_offset);
8885 #ifdef DEBUG
8886 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8887 global_entry->root.string, (unsigned long) *result);
8888 #endif
8889 return true;
8890 }
8891
8892 return false;
8893 }
8894
8895 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8896 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8897 names like "foo.end" which is the end address of section "foo". */
8898
8899 static bool
8900 resolve_section (const char *name,
8901 asection *sections,
8902 bfd_vma *result,
8903 bfd * abfd)
8904 {
8905 asection *curr;
8906 unsigned int len;
8907
8908 for (curr = sections; curr; curr = curr->next)
8909 if (strcmp (curr->name, name) == 0)
8910 {
8911 *result = curr->vma;
8912 return true;
8913 }
8914
8915 /* Hmm. still haven't found it. try pseudo-section names. */
8916 /* FIXME: This could be coded more efficiently... */
8917 for (curr = sections; curr; curr = curr->next)
8918 {
8919 len = strlen (curr->name);
8920 if (len > strlen (name))
8921 continue;
8922
8923 if (strncmp (curr->name, name, len) == 0)
8924 {
8925 if (startswith (name + len, ".end"))
8926 {
8927 *result = (curr->vma
8928 + curr->size / bfd_octets_per_byte (abfd, curr));
8929 return true;
8930 }
8931
8932 /* Insert more pseudo-section names here, if you like. */
8933 }
8934 }
8935
8936 return false;
8937 }
8938
8939 static void
8940 undefined_reference (const char *reftype, const char *name)
8941 {
8942 /* xgettext:c-format */
8943 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8944 reftype, name);
8945 bfd_set_error (bfd_error_bad_value);
8946 }
8947
8948 static bool
8949 eval_symbol (bfd_vma *result,
8950 const char **symp,
8951 bfd *input_bfd,
8952 struct elf_final_link_info *flinfo,
8953 bfd_vma dot,
8954 Elf_Internal_Sym *isymbuf,
8955 size_t locsymcount,
8956 int signed_p)
8957 {
8958 size_t len;
8959 size_t symlen;
8960 bfd_vma a;
8961 bfd_vma b;
8962 char symbuf[4096];
8963 const char *sym = *symp;
8964 const char *symend;
8965 bool symbol_is_section = false;
8966
8967 len = strlen (sym);
8968 symend = sym + len;
8969
8970 if (len < 1 || len > sizeof (symbuf))
8971 {
8972 bfd_set_error (bfd_error_invalid_operation);
8973 return false;
8974 }
8975
8976 switch (* sym)
8977 {
8978 case '.':
8979 *result = dot;
8980 *symp = sym + 1;
8981 return true;
8982
8983 case '#':
8984 ++sym;
8985 *result = strtoul (sym, (char **) symp, 16);
8986 return true;
8987
8988 case 'S':
8989 symbol_is_section = true;
8990 /* Fall through. */
8991 case 's':
8992 ++sym;
8993 symlen = strtol (sym, (char **) symp, 10);
8994 sym = *symp + 1; /* Skip the trailing ':'. */
8995
8996 if (symend < sym || symlen + 1 > sizeof (symbuf))
8997 {
8998 bfd_set_error (bfd_error_invalid_operation);
8999 return false;
9000 }
9001
9002 memcpy (symbuf, sym, symlen);
9003 symbuf[symlen] = '\0';
9004 *symp = sym + symlen;
9005
9006 /* Is it always possible, with complex symbols, that gas "mis-guessed"
9007 the symbol as a section, or vice-versa. so we're pretty liberal in our
9008 interpretation here; section means "try section first", not "must be a
9009 section", and likewise with symbol. */
9010
9011 if (symbol_is_section)
9012 {
9013 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
9014 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
9015 isymbuf, locsymcount))
9016 {
9017 undefined_reference ("section", symbuf);
9018 return false;
9019 }
9020 }
9021 else
9022 {
9023 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
9024 isymbuf, locsymcount)
9025 && !resolve_section (symbuf, flinfo->output_bfd->sections,
9026 result, input_bfd))
9027 {
9028 undefined_reference ("symbol", symbuf);
9029 return false;
9030 }
9031 }
9032
9033 return true;
9034
9035 /* All that remains are operators. */
9036
9037 #define UNARY_OP(op) \
9038 if (startswith (sym, #op)) \
9039 { \
9040 sym += strlen (#op); \
9041 if (*sym == ':') \
9042 ++sym; \
9043 *symp = sym; \
9044 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
9045 isymbuf, locsymcount, signed_p)) \
9046 return false; \
9047 if (signed_p) \
9048 *result = op ((bfd_signed_vma) a); \
9049 else \
9050 *result = op a; \
9051 return true; \
9052 }
9053
9054 #define BINARY_OP_HEAD(op) \
9055 if (startswith (sym, #op)) \
9056 { \
9057 sym += strlen (#op); \
9058 if (*sym == ':') \
9059 ++sym; \
9060 *symp = sym; \
9061 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
9062 isymbuf, locsymcount, signed_p)) \
9063 return false; \
9064 ++*symp; \
9065 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
9066 isymbuf, locsymcount, signed_p)) \
9067 return false;
9068 #define BINARY_OP_TAIL(op) \
9069 if (signed_p) \
9070 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
9071 else \
9072 *result = a op b; \
9073 return true; \
9074 }
9075 #define BINARY_OP(op) BINARY_OP_HEAD(op) BINARY_OP_TAIL(op)
9076
9077 default:
9078 UNARY_OP (0-);
9079 BINARY_OP_HEAD (<<);
9080 if (b >= sizeof (a) * CHAR_BIT)
9081 {
9082 *result = 0;
9083 return true;
9084 }
9085 signed_p = 0;
9086 BINARY_OP_TAIL (<<);
9087 BINARY_OP_HEAD (>>);
9088 if (b >= sizeof (a) * CHAR_BIT)
9089 {
9090 *result = signed_p && (bfd_signed_vma) a < 0 ? -1 : 0;
9091 return true;
9092 }
9093 BINARY_OP_TAIL (>>);
9094 BINARY_OP (==);
9095 BINARY_OP (!=);
9096 BINARY_OP (<=);
9097 BINARY_OP (>=);
9098 BINARY_OP (&&);
9099 BINARY_OP (||);
9100 UNARY_OP (~);
9101 UNARY_OP (!);
9102 BINARY_OP (*);
9103 BINARY_OP_HEAD (/);
9104 if (b == 0)
9105 {
9106 _bfd_error_handler (_("division by zero"));
9107 bfd_set_error (bfd_error_bad_value);
9108 return false;
9109 }
9110 BINARY_OP_TAIL (/);
9111 BINARY_OP_HEAD (%);
9112 if (b == 0)
9113 {
9114 _bfd_error_handler (_("division by zero"));
9115 bfd_set_error (bfd_error_bad_value);
9116 return false;
9117 }
9118 BINARY_OP_TAIL (%);
9119 BINARY_OP (^);
9120 BINARY_OP (|);
9121 BINARY_OP (&);
9122 BINARY_OP (+);
9123 BINARY_OP (-);
9124 BINARY_OP (<);
9125 BINARY_OP (>);
9126 #undef UNARY_OP
9127 #undef BINARY_OP
9128 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
9129 bfd_set_error (bfd_error_invalid_operation);
9130 return false;
9131 }
9132 }
9133
9134 static void
9135 put_value (bfd_vma size,
9136 unsigned long chunksz,
9137 bfd *input_bfd,
9138 bfd_vma x,
9139 bfd_byte *location)
9140 {
9141 location += (size - chunksz);
9142
9143 for (; size; size -= chunksz, location -= chunksz)
9144 {
9145 switch (chunksz)
9146 {
9147 case 1:
9148 bfd_put_8 (input_bfd, x, location);
9149 x >>= 8;
9150 break;
9151 case 2:
9152 bfd_put_16 (input_bfd, x, location);
9153 x >>= 16;
9154 break;
9155 case 4:
9156 bfd_put_32 (input_bfd, x, location);
9157 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
9158 x >>= 16;
9159 x >>= 16;
9160 break;
9161 #ifdef BFD64
9162 case 8:
9163 bfd_put_64 (input_bfd, x, location);
9164 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
9165 x >>= 32;
9166 x >>= 32;
9167 break;
9168 #endif
9169 default:
9170 abort ();
9171 break;
9172 }
9173 }
9174 }
9175
9176 static bfd_vma
9177 get_value (bfd_vma size,
9178 unsigned long chunksz,
9179 bfd *input_bfd,
9180 bfd_byte *location)
9181 {
9182 int shift;
9183 bfd_vma x = 0;
9184
9185 /* Sanity checks. */
9186 BFD_ASSERT (chunksz <= sizeof (x)
9187 && size >= chunksz
9188 && chunksz != 0
9189 && (size % chunksz) == 0
9190 && input_bfd != NULL
9191 && location != NULL);
9192
9193 if (chunksz == sizeof (x))
9194 {
9195 BFD_ASSERT (size == chunksz);
9196
9197 /* Make sure that we do not perform an undefined shift operation.
9198 We know that size == chunksz so there will only be one iteration
9199 of the loop below. */
9200 shift = 0;
9201 }
9202 else
9203 shift = 8 * chunksz;
9204
9205 for (; size; size -= chunksz, location += chunksz)
9206 {
9207 switch (chunksz)
9208 {
9209 case 1:
9210 x = (x << shift) | bfd_get_8 (input_bfd, location);
9211 break;
9212 case 2:
9213 x = (x << shift) | bfd_get_16 (input_bfd, location);
9214 break;
9215 case 4:
9216 x = (x << shift) | bfd_get_32 (input_bfd, location);
9217 break;
9218 #ifdef BFD64
9219 case 8:
9220 x = (x << shift) | bfd_get_64 (input_bfd, location);
9221 break;
9222 #endif
9223 default:
9224 abort ();
9225 }
9226 }
9227 return x;
9228 }
9229
9230 static void
9231 decode_complex_addend (unsigned long *start, /* in bits */
9232 unsigned long *oplen, /* in bits */
9233 unsigned long *len, /* in bits */
9234 unsigned long *wordsz, /* in bytes */
9235 unsigned long *chunksz, /* in bytes */
9236 unsigned long *lsb0_p,
9237 unsigned long *signed_p,
9238 unsigned long *trunc_p,
9239 unsigned long encoded)
9240 {
9241 * start = encoded & 0x3F;
9242 * len = (encoded >> 6) & 0x3F;
9243 * oplen = (encoded >> 12) & 0x3F;
9244 * wordsz = (encoded >> 18) & 0xF;
9245 * chunksz = (encoded >> 22) & 0xF;
9246 * lsb0_p = (encoded >> 27) & 1;
9247 * signed_p = (encoded >> 28) & 1;
9248 * trunc_p = (encoded >> 29) & 1;
9249 }
9250
9251 bfd_reloc_status_type
9252 bfd_elf_perform_complex_relocation (bfd *input_bfd,
9253 asection *input_section,
9254 bfd_byte *contents,
9255 Elf_Internal_Rela *rel,
9256 bfd_vma relocation)
9257 {
9258 bfd_vma shift, x, mask;
9259 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
9260 bfd_reloc_status_type r;
9261 bfd_size_type octets;
9262
9263 /* Perform this reloc, since it is complex.
9264 (this is not to say that it necessarily refers to a complex
9265 symbol; merely that it is a self-describing CGEN based reloc.
9266 i.e. the addend has the complete reloc information (bit start, end,
9267 word size, etc) encoded within it.). */
9268
9269 decode_complex_addend (&start, &oplen, &len, &wordsz,
9270 &chunksz, &lsb0_p, &signed_p,
9271 &trunc_p, rel->r_addend);
9272
9273 mask = (((1L << (len - 1)) - 1) << 1) | 1;
9274
9275 if (lsb0_p)
9276 shift = (start + 1) - len;
9277 else
9278 shift = (8 * wordsz) - (start + len);
9279
9280 octets = rel->r_offset * bfd_octets_per_byte (input_bfd, input_section);
9281 x = get_value (wordsz, chunksz, input_bfd, contents + octets);
9282
9283 #ifdef DEBUG
9284 printf ("Doing complex reloc: "
9285 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
9286 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
9287 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
9288 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9289 oplen, (unsigned long) x, (unsigned long) mask,
9290 (unsigned long) relocation);
9291 #endif
9292
9293 r = bfd_reloc_ok;
9294 if (! trunc_p)
9295 /* Now do an overflow check. */
9296 r = bfd_check_overflow ((signed_p
9297 ? complain_overflow_signed
9298 : complain_overflow_unsigned),
9299 len, 0, (8 * wordsz),
9300 relocation);
9301
9302 /* Do the deed. */
9303 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
9304
9305 #ifdef DEBUG
9306 printf (" relocation: %8.8lx\n"
9307 " shifted mask: %8.8lx\n"
9308 " shifted/masked reloc: %8.8lx\n"
9309 " result: %8.8lx\n",
9310 (unsigned long) relocation, (unsigned long) (mask << shift),
9311 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
9312 #endif
9313 put_value (wordsz, chunksz, input_bfd, x, contents + octets);
9314 return r;
9315 }
9316
9317 /* Functions to read r_offset from external (target order) reloc
9318 entry. Faster than bfd_getl32 et al, because we let the compiler
9319 know the value is aligned. */
9320
9321 static bfd_vma
9322 ext32l_r_offset (const void *p)
9323 {
9324 union aligned32
9325 {
9326 uint32_t v;
9327 unsigned char c[4];
9328 };
9329 const union aligned32 *a
9330 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9331
9332 uint32_t aval = ( (uint32_t) a->c[0]
9333 | (uint32_t) a->c[1] << 8
9334 | (uint32_t) a->c[2] << 16
9335 | (uint32_t) a->c[3] << 24);
9336 return aval;
9337 }
9338
9339 static bfd_vma
9340 ext32b_r_offset (const void *p)
9341 {
9342 union aligned32
9343 {
9344 uint32_t v;
9345 unsigned char c[4];
9346 };
9347 const union aligned32 *a
9348 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9349
9350 uint32_t aval = ( (uint32_t) a->c[0] << 24
9351 | (uint32_t) a->c[1] << 16
9352 | (uint32_t) a->c[2] << 8
9353 | (uint32_t) a->c[3]);
9354 return aval;
9355 }
9356
9357 #ifdef BFD_HOST_64_BIT
9358 static bfd_vma
9359 ext64l_r_offset (const void *p)
9360 {
9361 union aligned64
9362 {
9363 uint64_t v;
9364 unsigned char c[8];
9365 };
9366 const union aligned64 *a
9367 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9368
9369 uint64_t aval = ( (uint64_t) a->c[0]
9370 | (uint64_t) a->c[1] << 8
9371 | (uint64_t) a->c[2] << 16
9372 | (uint64_t) a->c[3] << 24
9373 | (uint64_t) a->c[4] << 32
9374 | (uint64_t) a->c[5] << 40
9375 | (uint64_t) a->c[6] << 48
9376 | (uint64_t) a->c[7] << 56);
9377 return aval;
9378 }
9379
9380 static bfd_vma
9381 ext64b_r_offset (const void *p)
9382 {
9383 union aligned64
9384 {
9385 uint64_t v;
9386 unsigned char c[8];
9387 };
9388 const union aligned64 *a
9389 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9390
9391 uint64_t aval = ( (uint64_t) a->c[0] << 56
9392 | (uint64_t) a->c[1] << 48
9393 | (uint64_t) a->c[2] << 40
9394 | (uint64_t) a->c[3] << 32
9395 | (uint64_t) a->c[4] << 24
9396 | (uint64_t) a->c[5] << 16
9397 | (uint64_t) a->c[6] << 8
9398 | (uint64_t) a->c[7]);
9399 return aval;
9400 }
9401 #endif
9402
9403 /* When performing a relocatable link, the input relocations are
9404 preserved. But, if they reference global symbols, the indices
9405 referenced must be updated. Update all the relocations found in
9406 RELDATA. */
9407
9408 static bool
9409 elf_link_adjust_relocs (bfd *abfd,
9410 asection *sec,
9411 struct bfd_elf_section_reloc_data *reldata,
9412 bool sort,
9413 struct bfd_link_info *info)
9414 {
9415 unsigned int i;
9416 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9417 bfd_byte *erela;
9418 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9419 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9420 bfd_vma r_type_mask;
9421 int r_sym_shift;
9422 unsigned int count = reldata->count;
9423 struct elf_link_hash_entry **rel_hash = reldata->hashes;
9424
9425 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
9426 {
9427 swap_in = bed->s->swap_reloc_in;
9428 swap_out = bed->s->swap_reloc_out;
9429 }
9430 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
9431 {
9432 swap_in = bed->s->swap_reloca_in;
9433 swap_out = bed->s->swap_reloca_out;
9434 }
9435 else
9436 abort ();
9437
9438 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
9439 abort ();
9440
9441 if (bed->s->arch_size == 32)
9442 {
9443 r_type_mask = 0xff;
9444 r_sym_shift = 8;
9445 }
9446 else
9447 {
9448 r_type_mask = 0xffffffff;
9449 r_sym_shift = 32;
9450 }
9451
9452 erela = reldata->hdr->contents;
9453 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
9454 {
9455 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
9456 unsigned int j;
9457
9458 if (*rel_hash == NULL)
9459 continue;
9460
9461 if ((*rel_hash)->indx == -2
9462 && info->gc_sections
9463 && ! info->gc_keep_exported)
9464 {
9465 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9466 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
9467 abfd, sec,
9468 (*rel_hash)->root.root.string);
9469 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
9470 abfd, sec);
9471 bfd_set_error (bfd_error_invalid_operation);
9472 return false;
9473 }
9474 BFD_ASSERT ((*rel_hash)->indx >= 0);
9475
9476 (*swap_in) (abfd, erela, irela);
9477 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
9478 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
9479 | (irela[j].r_info & r_type_mask));
9480 (*swap_out) (abfd, irela, erela);
9481 }
9482
9483 if (bed->elf_backend_update_relocs)
9484 (*bed->elf_backend_update_relocs) (sec, reldata);
9485
9486 if (sort && count != 0)
9487 {
9488 bfd_vma (*ext_r_off) (const void *);
9489 bfd_vma r_off;
9490 size_t elt_size;
9491 bfd_byte *base, *end, *p, *loc;
9492 bfd_byte *buf = NULL;
9493
9494 if (bed->s->arch_size == 32)
9495 {
9496 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9497 ext_r_off = ext32l_r_offset;
9498 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9499 ext_r_off = ext32b_r_offset;
9500 else
9501 abort ();
9502 }
9503 else
9504 {
9505 #ifdef BFD_HOST_64_BIT
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 #endif
9512 abort ();
9513 }
9514
9515 /* Must use a stable sort here. A modified insertion sort,
9516 since the relocs are mostly sorted already. */
9517 elt_size = reldata->hdr->sh_entsize;
9518 base = reldata->hdr->contents;
9519 end = base + count * elt_size;
9520 if (elt_size > sizeof (Elf64_External_Rela))
9521 abort ();
9522
9523 /* Ensure the first element is lowest. This acts as a sentinel,
9524 speeding the main loop below. */
9525 r_off = (*ext_r_off) (base);
9526 for (p = loc = base; (p += elt_size) < end; )
9527 {
9528 bfd_vma r_off2 = (*ext_r_off) (p);
9529 if (r_off > r_off2)
9530 {
9531 r_off = r_off2;
9532 loc = p;
9533 }
9534 }
9535 if (loc != base)
9536 {
9537 /* Don't just swap *base and *loc as that changes the order
9538 of the original base[0] and base[1] if they happen to
9539 have the same r_offset. */
9540 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
9541 memcpy (onebuf, loc, elt_size);
9542 memmove (base + elt_size, base, loc - base);
9543 memcpy (base, onebuf, elt_size);
9544 }
9545
9546 for (p = base + elt_size; (p += elt_size) < end; )
9547 {
9548 /* base to p is sorted, *p is next to insert. */
9549 r_off = (*ext_r_off) (p);
9550 /* Search the sorted region for location to insert. */
9551 loc = p - elt_size;
9552 while (r_off < (*ext_r_off) (loc))
9553 loc -= elt_size;
9554 loc += elt_size;
9555 if (loc != p)
9556 {
9557 /* Chances are there is a run of relocs to insert here,
9558 from one of more input files. Files are not always
9559 linked in order due to the way elf_link_input_bfd is
9560 called. See pr17666. */
9561 size_t sortlen = p - loc;
9562 bfd_vma r_off2 = (*ext_r_off) (loc);
9563 size_t runlen = elt_size;
9564 size_t buf_size = 96 * 1024;
9565 while (p + runlen < end
9566 && (sortlen <= buf_size
9567 || runlen + elt_size <= buf_size)
9568 && r_off2 > (*ext_r_off) (p + runlen))
9569 runlen += elt_size;
9570 if (buf == NULL)
9571 {
9572 buf = bfd_malloc (buf_size);
9573 if (buf == NULL)
9574 return false;
9575 }
9576 if (runlen < sortlen)
9577 {
9578 memcpy (buf, p, runlen);
9579 memmove (loc + runlen, loc, sortlen);
9580 memcpy (loc, buf, runlen);
9581 }
9582 else
9583 {
9584 memcpy (buf, loc, sortlen);
9585 memmove (loc, p, runlen);
9586 memcpy (loc + runlen, buf, sortlen);
9587 }
9588 p += runlen - elt_size;
9589 }
9590 }
9591 /* Hashes are no longer valid. */
9592 free (reldata->hashes);
9593 reldata->hashes = NULL;
9594 free (buf);
9595 }
9596 return true;
9597 }
9598
9599 struct elf_link_sort_rela
9600 {
9601 union {
9602 bfd_vma offset;
9603 bfd_vma sym_mask;
9604 } u;
9605 enum elf_reloc_type_class type;
9606 /* We use this as an array of size int_rels_per_ext_rel. */
9607 Elf_Internal_Rela rela[1];
9608 };
9609
9610 /* qsort stability here and for cmp2 is only an issue if multiple
9611 dynamic relocations are emitted at the same address. But targets
9612 that apply a series of dynamic relocations each operating on the
9613 result of the prior relocation can't use -z combreloc as
9614 implemented anyway. Such schemes tend to be broken by sorting on
9615 symbol index. That leaves dynamic NONE relocs as the only other
9616 case where ld might emit multiple relocs at the same address, and
9617 those are only emitted due to target bugs. */
9618
9619 static int
9620 elf_link_sort_cmp1 (const void *A, const void *B)
9621 {
9622 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9623 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9624 int relativea, relativeb;
9625
9626 relativea = a->type == reloc_class_relative;
9627 relativeb = b->type == reloc_class_relative;
9628
9629 if (relativea < relativeb)
9630 return 1;
9631 if (relativea > relativeb)
9632 return -1;
9633 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
9634 return -1;
9635 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
9636 return 1;
9637 if (a->rela->r_offset < b->rela->r_offset)
9638 return -1;
9639 if (a->rela->r_offset > b->rela->r_offset)
9640 return 1;
9641 return 0;
9642 }
9643
9644 static int
9645 elf_link_sort_cmp2 (const void *A, const void *B)
9646 {
9647 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9648 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9649
9650 if (a->type < b->type)
9651 return -1;
9652 if (a->type > b->type)
9653 return 1;
9654 if (a->u.offset < b->u.offset)
9655 return -1;
9656 if (a->u.offset > b->u.offset)
9657 return 1;
9658 if (a->rela->r_offset < b->rela->r_offset)
9659 return -1;
9660 if (a->rela->r_offset > b->rela->r_offset)
9661 return 1;
9662 return 0;
9663 }
9664
9665 static size_t
9666 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9667 {
9668 asection *dynamic_relocs;
9669 asection *rela_dyn;
9670 asection *rel_dyn;
9671 bfd_size_type count, size;
9672 size_t i, ret, sort_elt, ext_size;
9673 bfd_byte *sort, *s_non_relative, *p;
9674 struct elf_link_sort_rela *sq;
9675 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9676 int i2e = bed->s->int_rels_per_ext_rel;
9677 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
9678 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9679 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9680 struct bfd_link_order *lo;
9681 bfd_vma r_sym_mask;
9682 bool use_rela;
9683
9684 /* Find a dynamic reloc section. */
9685 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9686 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
9687 if (rela_dyn != NULL && rela_dyn->size > 0
9688 && rel_dyn != NULL && rel_dyn->size > 0)
9689 {
9690 bool use_rela_initialised = false;
9691
9692 /* This is just here to stop gcc from complaining.
9693 Its initialization checking code is not perfect. */
9694 use_rela = true;
9695
9696 /* Both sections are present. Examine the sizes
9697 of the indirect sections to help us choose. */
9698 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9699 if (lo->type == bfd_indirect_link_order)
9700 {
9701 asection *o = lo->u.indirect.section;
9702
9703 if ((o->size % bed->s->sizeof_rela) == 0)
9704 {
9705 if ((o->size % bed->s->sizeof_rel) == 0)
9706 /* Section size is divisible by both rel and rela sizes.
9707 It is of no help to us. */
9708 ;
9709 else
9710 {
9711 /* Section size is only divisible by rela. */
9712 if (use_rela_initialised && !use_rela)
9713 {
9714 _bfd_error_handler (_("%pB: unable to sort relocs - "
9715 "they are in more than one size"),
9716 abfd);
9717 bfd_set_error (bfd_error_invalid_operation);
9718 return 0;
9719 }
9720 else
9721 {
9722 use_rela = true;
9723 use_rela_initialised = true;
9724 }
9725 }
9726 }
9727 else if ((o->size % bed->s->sizeof_rel) == 0)
9728 {
9729 /* Section size is only divisible by rel. */
9730 if (use_rela_initialised && use_rela)
9731 {
9732 _bfd_error_handler (_("%pB: unable to sort relocs - "
9733 "they are in more than one size"),
9734 abfd);
9735 bfd_set_error (bfd_error_invalid_operation);
9736 return 0;
9737 }
9738 else
9739 {
9740 use_rela = false;
9741 use_rela_initialised = true;
9742 }
9743 }
9744 else
9745 {
9746 /* The section size is not divisible by either -
9747 something is wrong. */
9748 _bfd_error_handler (_("%pB: unable to sort relocs - "
9749 "they are of an unknown size"), abfd);
9750 bfd_set_error (bfd_error_invalid_operation);
9751 return 0;
9752 }
9753 }
9754
9755 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9756 if (lo->type == bfd_indirect_link_order)
9757 {
9758 asection *o = lo->u.indirect.section;
9759
9760 if ((o->size % bed->s->sizeof_rela) == 0)
9761 {
9762 if ((o->size % bed->s->sizeof_rel) == 0)
9763 /* Section size is divisible by both rel and rela sizes.
9764 It is of no help to us. */
9765 ;
9766 else
9767 {
9768 /* Section size is only divisible by rela. */
9769 if (use_rela_initialised && !use_rela)
9770 {
9771 _bfd_error_handler (_("%pB: unable to sort relocs - "
9772 "they are in more than one size"),
9773 abfd);
9774 bfd_set_error (bfd_error_invalid_operation);
9775 return 0;
9776 }
9777 else
9778 {
9779 use_rela = true;
9780 use_rela_initialised = true;
9781 }
9782 }
9783 }
9784 else if ((o->size % bed->s->sizeof_rel) == 0)
9785 {
9786 /* Section size is only divisible by rel. */
9787 if (use_rela_initialised && use_rela)
9788 {
9789 _bfd_error_handler (_("%pB: unable to sort relocs - "
9790 "they are in more than one size"),
9791 abfd);
9792 bfd_set_error (bfd_error_invalid_operation);
9793 return 0;
9794 }
9795 else
9796 {
9797 use_rela = false;
9798 use_rela_initialised = true;
9799 }
9800 }
9801 else
9802 {
9803 /* The section size is not divisible by either -
9804 something is wrong. */
9805 _bfd_error_handler (_("%pB: unable to sort relocs - "
9806 "they are of an unknown size"), abfd);
9807 bfd_set_error (bfd_error_invalid_operation);
9808 return 0;
9809 }
9810 }
9811
9812 if (! use_rela_initialised)
9813 /* Make a guess. */
9814 use_rela = true;
9815 }
9816 else if (rela_dyn != NULL && rela_dyn->size > 0)
9817 use_rela = true;
9818 else if (rel_dyn != NULL && rel_dyn->size > 0)
9819 use_rela = false;
9820 else
9821 return 0;
9822
9823 if (use_rela)
9824 {
9825 dynamic_relocs = rela_dyn;
9826 ext_size = bed->s->sizeof_rela;
9827 swap_in = bed->s->swap_reloca_in;
9828 swap_out = bed->s->swap_reloca_out;
9829 }
9830 else
9831 {
9832 dynamic_relocs = rel_dyn;
9833 ext_size = bed->s->sizeof_rel;
9834 swap_in = bed->s->swap_reloc_in;
9835 swap_out = bed->s->swap_reloc_out;
9836 }
9837
9838 size = 0;
9839 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9840 if (lo->type == bfd_indirect_link_order)
9841 size += lo->u.indirect.section->size;
9842
9843 if (size != dynamic_relocs->size)
9844 return 0;
9845
9846 sort_elt = (sizeof (struct elf_link_sort_rela)
9847 + (i2e - 1) * sizeof (Elf_Internal_Rela));
9848
9849 count = dynamic_relocs->size / ext_size;
9850 if (count == 0)
9851 return 0;
9852 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
9853
9854 if (sort == NULL)
9855 {
9856 (*info->callbacks->warning)
9857 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
9858 return 0;
9859 }
9860
9861 if (bed->s->arch_size == 32)
9862 r_sym_mask = ~(bfd_vma) 0xff;
9863 else
9864 r_sym_mask = ~(bfd_vma) 0xffffffff;
9865
9866 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9867 if (lo->type == bfd_indirect_link_order)
9868 {
9869 bfd_byte *erel, *erelend;
9870 asection *o = lo->u.indirect.section;
9871
9872 if (o->contents == NULL && o->size != 0)
9873 {
9874 /* This is a reloc section that is being handled as a normal
9875 section. See bfd_section_from_shdr. We can't combine
9876 relocs in this case. */
9877 free (sort);
9878 return 0;
9879 }
9880 erel = o->contents;
9881 erelend = o->contents + o->size;
9882 p = sort + o->output_offset * opb / ext_size * sort_elt;
9883
9884 while (erel < erelend)
9885 {
9886 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9887
9888 (*swap_in) (abfd, erel, s->rela);
9889 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
9890 s->u.sym_mask = r_sym_mask;
9891 p += sort_elt;
9892 erel += ext_size;
9893 }
9894 }
9895
9896 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9897
9898 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9899 {
9900 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9901 if (s->type != reloc_class_relative)
9902 break;
9903 }
9904 ret = i;
9905 s_non_relative = p;
9906
9907 sq = (struct elf_link_sort_rela *) s_non_relative;
9908 for (; i < count; i++, p += sort_elt)
9909 {
9910 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9911 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9912 sq = sp;
9913 sp->u.offset = sq->rela->r_offset;
9914 }
9915
9916 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9917
9918 struct elf_link_hash_table *htab = elf_hash_table (info);
9919 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9920 {
9921 /* We have plt relocs in .rela.dyn. */
9922 sq = (struct elf_link_sort_rela *) sort;
9923 for (i = 0; i < count; i++)
9924 if (sq[count - i - 1].type != reloc_class_plt)
9925 break;
9926 if (i != 0 && htab->srelplt->size == i * ext_size)
9927 {
9928 struct bfd_link_order **plo;
9929 /* Put srelplt link_order last. This is so the output_offset
9930 set in the next loop is correct for DT_JMPREL. */
9931 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9932 if ((*plo)->type == bfd_indirect_link_order
9933 && (*plo)->u.indirect.section == htab->srelplt)
9934 {
9935 lo = *plo;
9936 *plo = lo->next;
9937 }
9938 else
9939 plo = &(*plo)->next;
9940 *plo = lo;
9941 lo->next = NULL;
9942 dynamic_relocs->map_tail.link_order = lo;
9943 }
9944 }
9945
9946 p = sort;
9947 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9948 if (lo->type == bfd_indirect_link_order)
9949 {
9950 bfd_byte *erel, *erelend;
9951 asection *o = lo->u.indirect.section;
9952
9953 erel = o->contents;
9954 erelend = o->contents + o->size;
9955 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
9956 while (erel < erelend)
9957 {
9958 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9959 (*swap_out) (abfd, s->rela, erel);
9960 p += sort_elt;
9961 erel += ext_size;
9962 }
9963 }
9964
9965 free (sort);
9966 *psec = dynamic_relocs;
9967 return ret;
9968 }
9969
9970 /* Add a symbol to the output symbol string table. */
9971
9972 static int
9973 elf_link_output_symstrtab (void *finf,
9974 const char *name,
9975 Elf_Internal_Sym *elfsym,
9976 asection *input_sec,
9977 struct elf_link_hash_entry *h)
9978 {
9979 struct elf_final_link_info *flinfo = finf;
9980 int (*output_symbol_hook)
9981 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9982 struct elf_link_hash_entry *);
9983 struct elf_link_hash_table *hash_table;
9984 const struct elf_backend_data *bed;
9985 bfd_size_type strtabsize;
9986
9987 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9988
9989 bed = get_elf_backend_data (flinfo->output_bfd);
9990 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9991 if (output_symbol_hook != NULL)
9992 {
9993 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
9994 if (ret != 1)
9995 return ret;
9996 }
9997
9998 if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC)
9999 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
10000 if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE)
10001 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique;
10002
10003 if (name == NULL
10004 || *name == '\0'
10005 || (input_sec->flags & SEC_EXCLUDE))
10006 elfsym->st_name = (unsigned long) -1;
10007 else
10008 {
10009 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
10010 to get the final offset for st_name. */
10011 char *versioned_name = (char *) name;
10012 if (h != NULL)
10013 {
10014 if (h->versioned == versioned && h->def_dynamic)
10015 {
10016 /* Keep only one '@' for versioned symbols defined in
10017 shared objects. */
10018 char *version = strrchr (name, ELF_VER_CHR);
10019 char *base_end = strchr (name, ELF_VER_CHR);
10020 if (version != base_end)
10021 {
10022 size_t base_len;
10023 size_t len = strlen (name);
10024 versioned_name = bfd_alloc (flinfo->output_bfd, len);
10025 if (versioned_name == NULL)
10026 return 0;
10027 base_len = base_end - name;
10028 memcpy (versioned_name, name, base_len);
10029 memcpy (versioned_name + base_len, version,
10030 len - base_len);
10031 }
10032 }
10033 }
10034 else if (flinfo->info->unique_symbol
10035 && ELF_ST_BIND (elfsym->st_info) == STB_LOCAL)
10036 {
10037 struct local_hash_entry *lh;
10038 size_t count_len;
10039 size_t base_len;
10040 char buf[30];
10041 switch (ELF_ST_TYPE (elfsym->st_info))
10042 {
10043 case STT_FILE:
10044 case STT_SECTION:
10045 break;
10046 default:
10047 lh = (struct local_hash_entry *) bfd_hash_lookup
10048 (&flinfo->local_hash_table, name, true, false);
10049 if (lh == NULL)
10050 return 0;
10051 /* Always append ".COUNT" to local symbols to avoid
10052 potential conflicts with local symbol "XXX.COUNT". */
10053 sprintf (buf, "%lx", lh->count);
10054 base_len = lh->size;
10055 if (!base_len)
10056 {
10057 base_len = strlen (name);
10058 lh->size = base_len;
10059 }
10060 count_len = strlen (buf);
10061 versioned_name = bfd_alloc (flinfo->output_bfd,
10062 base_len + count_len + 2);
10063 if (versioned_name == NULL)
10064 return 0;
10065 memcpy (versioned_name, name, base_len);
10066 versioned_name[base_len] = '.';
10067 memcpy (versioned_name + base_len + 1, buf,
10068 count_len + 1);
10069 lh->count++;
10070 break;
10071 }
10072 }
10073 elfsym->st_name
10074 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
10075 versioned_name, false);
10076 if (elfsym->st_name == (unsigned long) -1)
10077 return 0;
10078 }
10079
10080 hash_table = elf_hash_table (flinfo->info);
10081 strtabsize = hash_table->strtabsize;
10082 if (strtabsize <= flinfo->output_bfd->symcount)
10083 {
10084 strtabsize += strtabsize;
10085 hash_table->strtabsize = strtabsize;
10086 strtabsize *= sizeof (*hash_table->strtab);
10087 hash_table->strtab
10088 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
10089 strtabsize);
10090 if (hash_table->strtab == NULL)
10091 return 0;
10092 }
10093 hash_table->strtab[flinfo->output_bfd->symcount].sym = *elfsym;
10094 hash_table->strtab[flinfo->output_bfd->symcount].dest_index
10095 = flinfo->output_bfd->symcount;
10096 flinfo->output_bfd->symcount += 1;
10097
10098 return 1;
10099 }
10100
10101 /* Swap symbols out to the symbol table and flush the output symbols to
10102 the file. */
10103
10104 static bool
10105 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
10106 {
10107 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
10108 size_t amt;
10109 size_t i;
10110 const struct elf_backend_data *bed;
10111 bfd_byte *symbuf;
10112 Elf_Internal_Shdr *hdr;
10113 file_ptr pos;
10114 bool ret;
10115
10116 if (flinfo->output_bfd->symcount == 0)
10117 return true;
10118
10119 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
10120
10121 bed = get_elf_backend_data (flinfo->output_bfd);
10122
10123 amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount;
10124 symbuf = (bfd_byte *) bfd_malloc (amt);
10125 if (symbuf == NULL)
10126 return false;
10127
10128 if (flinfo->symshndxbuf)
10129 {
10130 amt = sizeof (Elf_External_Sym_Shndx);
10131 amt *= bfd_get_symcount (flinfo->output_bfd);
10132 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
10133 if (flinfo->symshndxbuf == NULL)
10134 {
10135 free (symbuf);
10136 return false;
10137 }
10138 }
10139
10140 /* Now swap out the symbols. */
10141 for (i = 0; i < flinfo->output_bfd->symcount; i++)
10142 {
10143 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
10144 if (elfsym->sym.st_name == (unsigned long) -1)
10145 elfsym->sym.st_name = 0;
10146 else
10147 elfsym->sym.st_name
10148 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
10149 elfsym->sym.st_name);
10150
10151 /* Inform the linker of the addition of this symbol. */
10152
10153 if (flinfo->info->callbacks->ctf_new_symbol)
10154 flinfo->info->callbacks->ctf_new_symbol (elfsym->dest_index,
10155 &elfsym->sym);
10156
10157 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
10158 ((bfd_byte *) symbuf
10159 + (elfsym->dest_index
10160 * bed->s->sizeof_sym)),
10161 NPTR_ADD (flinfo->symshndxbuf,
10162 elfsym->dest_index));
10163 }
10164
10165 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
10166 pos = hdr->sh_offset + hdr->sh_size;
10167 amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount;
10168 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
10169 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
10170 {
10171 hdr->sh_size += amt;
10172 ret = true;
10173 }
10174 else
10175 ret = false;
10176
10177 free (symbuf);
10178
10179 free (hash_table->strtab);
10180 hash_table->strtab = NULL;
10181
10182 return ret;
10183 }
10184
10185 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
10186
10187 static bool
10188 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
10189 {
10190 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
10191 && sym->st_shndx < SHN_LORESERVE)
10192 {
10193 /* The gABI doesn't support dynamic symbols in output sections
10194 beyond 64k. */
10195 _bfd_error_handler
10196 /* xgettext:c-format */
10197 (_("%pB: too many sections: %d (>= %d)"),
10198 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
10199 bfd_set_error (bfd_error_nonrepresentable_section);
10200 return false;
10201 }
10202 return true;
10203 }
10204
10205 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
10206 allowing an unsatisfied unversioned symbol in the DSO to match a
10207 versioned symbol that would normally require an explicit version.
10208 We also handle the case that a DSO references a hidden symbol
10209 which may be satisfied by a versioned symbol in another DSO. */
10210
10211 static bool
10212 elf_link_check_versioned_symbol (struct bfd_link_info *info,
10213 const struct elf_backend_data *bed,
10214 struct elf_link_hash_entry *h)
10215 {
10216 bfd *abfd;
10217 struct elf_link_loaded_list *loaded;
10218
10219 if (!is_elf_hash_table (info->hash))
10220 return false;
10221
10222 /* Check indirect symbol. */
10223 while (h->root.type == bfd_link_hash_indirect)
10224 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10225
10226 switch (h->root.type)
10227 {
10228 default:
10229 abfd = NULL;
10230 break;
10231
10232 case bfd_link_hash_undefined:
10233 case bfd_link_hash_undefweak:
10234 abfd = h->root.u.undef.abfd;
10235 if (abfd == NULL
10236 || (abfd->flags & DYNAMIC) == 0
10237 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
10238 return false;
10239 break;
10240
10241 case bfd_link_hash_defined:
10242 case bfd_link_hash_defweak:
10243 abfd = h->root.u.def.section->owner;
10244 break;
10245
10246 case bfd_link_hash_common:
10247 abfd = h->root.u.c.p->section->owner;
10248 break;
10249 }
10250 BFD_ASSERT (abfd != NULL);
10251
10252 for (loaded = elf_hash_table (info)->dyn_loaded;
10253 loaded != NULL;
10254 loaded = loaded->next)
10255 {
10256 bfd *input;
10257 Elf_Internal_Shdr *hdr;
10258 size_t symcount;
10259 size_t extsymcount;
10260 size_t extsymoff;
10261 Elf_Internal_Shdr *versymhdr;
10262 Elf_Internal_Sym *isym;
10263 Elf_Internal_Sym *isymend;
10264 Elf_Internal_Sym *isymbuf;
10265 Elf_External_Versym *ever;
10266 Elf_External_Versym *extversym;
10267
10268 input = loaded->abfd;
10269
10270 /* We check each DSO for a possible hidden versioned definition. */
10271 if (input == abfd
10272 || elf_dynversym (input) == 0)
10273 continue;
10274
10275 hdr = &elf_tdata (input)->dynsymtab_hdr;
10276
10277 symcount = hdr->sh_size / bed->s->sizeof_sym;
10278 if (elf_bad_symtab (input))
10279 {
10280 extsymcount = symcount;
10281 extsymoff = 0;
10282 }
10283 else
10284 {
10285 extsymcount = symcount - hdr->sh_info;
10286 extsymoff = hdr->sh_info;
10287 }
10288
10289 if (extsymcount == 0)
10290 continue;
10291
10292 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
10293 NULL, NULL, NULL);
10294 if (isymbuf == NULL)
10295 return false;
10296
10297 /* Read in any version definitions. */
10298 versymhdr = &elf_tdata (input)->dynversym_hdr;
10299 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
10300 || (extversym = (Elf_External_Versym *)
10301 _bfd_malloc_and_read (input, versymhdr->sh_size,
10302 versymhdr->sh_size)) == NULL)
10303 {
10304 free (isymbuf);
10305 return false;
10306 }
10307
10308 ever = extversym + extsymoff;
10309 isymend = isymbuf + extsymcount;
10310 for (isym = isymbuf; isym < isymend; isym++, ever++)
10311 {
10312 const char *name;
10313 Elf_Internal_Versym iver;
10314 unsigned short version_index;
10315
10316 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
10317 || isym->st_shndx == SHN_UNDEF)
10318 continue;
10319
10320 name = bfd_elf_string_from_elf_section (input,
10321 hdr->sh_link,
10322 isym->st_name);
10323 if (strcmp (name, h->root.root.string) != 0)
10324 continue;
10325
10326 _bfd_elf_swap_versym_in (input, ever, &iver);
10327
10328 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
10329 && !(h->def_regular
10330 && h->forced_local))
10331 {
10332 /* If we have a non-hidden versioned sym, then it should
10333 have provided a definition for the undefined sym unless
10334 it is defined in a non-shared object and forced local.
10335 */
10336 abort ();
10337 }
10338
10339 version_index = iver.vs_vers & VERSYM_VERSION;
10340 if (version_index == 1 || version_index == 2)
10341 {
10342 /* This is the base or first version. We can use it. */
10343 free (extversym);
10344 free (isymbuf);
10345 return true;
10346 }
10347 }
10348
10349 free (extversym);
10350 free (isymbuf);
10351 }
10352
10353 return false;
10354 }
10355
10356 /* Convert ELF common symbol TYPE. */
10357
10358 static int
10359 elf_link_convert_common_type (struct bfd_link_info *info, int type)
10360 {
10361 /* Commom symbol can only appear in relocatable link. */
10362 if (!bfd_link_relocatable (info))
10363 abort ();
10364 switch (info->elf_stt_common)
10365 {
10366 case unchanged:
10367 break;
10368 case elf_stt_common:
10369 type = STT_COMMON;
10370 break;
10371 case no_elf_stt_common:
10372 type = STT_OBJECT;
10373 break;
10374 }
10375 return type;
10376 }
10377
10378 /* Add an external symbol to the symbol table. This is called from
10379 the hash table traversal routine. When generating a shared object,
10380 we go through the symbol table twice. The first time we output
10381 anything that might have been forced to local scope in a version
10382 script. The second time we output the symbols that are still
10383 global symbols. */
10384
10385 static bool
10386 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
10387 {
10388 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
10389 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
10390 struct elf_final_link_info *flinfo = eoinfo->flinfo;
10391 bool strip;
10392 Elf_Internal_Sym sym;
10393 asection *input_sec;
10394 const struct elf_backend_data *bed;
10395 long indx;
10396 int ret;
10397 unsigned int type;
10398
10399 if (h->root.type == bfd_link_hash_warning)
10400 {
10401 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10402 if (h->root.type == bfd_link_hash_new)
10403 return true;
10404 }
10405
10406 /* Decide whether to output this symbol in this pass. */
10407 if (eoinfo->localsyms)
10408 {
10409 if (!h->forced_local)
10410 return true;
10411 }
10412 else
10413 {
10414 if (h->forced_local)
10415 return true;
10416 }
10417
10418 bed = get_elf_backend_data (flinfo->output_bfd);
10419
10420 if (h->root.type == bfd_link_hash_undefined)
10421 {
10422 /* If we have an undefined symbol reference here then it must have
10423 come from a shared library that is being linked in. (Undefined
10424 references in regular files have already been handled unless
10425 they are in unreferenced sections which are removed by garbage
10426 collection). */
10427 bool ignore_undef = false;
10428
10429 /* Some symbols may be special in that the fact that they're
10430 undefined can be safely ignored - let backend determine that. */
10431 if (bed->elf_backend_ignore_undef_symbol)
10432 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
10433
10434 /* If we are reporting errors for this situation then do so now. */
10435 if (!ignore_undef
10436 && h->ref_dynamic_nonweak
10437 && (!h->ref_regular || flinfo->info->gc_sections)
10438 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
10439 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
10440 {
10441 flinfo->info->callbacks->undefined_symbol
10442 (flinfo->info, h->root.root.string,
10443 h->ref_regular ? NULL : h->root.u.undef.abfd, NULL, 0,
10444 flinfo->info->unresolved_syms_in_shared_libs == RM_DIAGNOSE
10445 && !flinfo->info->warn_unresolved_syms);
10446 }
10447
10448 /* Strip a global symbol defined in a discarded section. */
10449 if (h->indx == -3)
10450 return true;
10451 }
10452
10453 /* We should also warn if a forced local symbol is referenced from
10454 shared libraries. */
10455 if (bfd_link_executable (flinfo->info)
10456 && h->forced_local
10457 && h->ref_dynamic
10458 && h->def_regular
10459 && !h->dynamic_def
10460 && h->ref_dynamic_nonweak
10461 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
10462 {
10463 bfd *def_bfd;
10464 const char *msg;
10465 struct elf_link_hash_entry *hi = h;
10466
10467 /* Check indirect symbol. */
10468 while (hi->root.type == bfd_link_hash_indirect)
10469 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
10470
10471 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
10472 /* xgettext:c-format */
10473 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
10474 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
10475 /* xgettext:c-format */
10476 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
10477 else
10478 /* xgettext:c-format */
10479 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
10480 def_bfd = flinfo->output_bfd;
10481 if (hi->root.u.def.section != bfd_abs_section_ptr)
10482 def_bfd = hi->root.u.def.section->owner;
10483 _bfd_error_handler (msg, flinfo->output_bfd,
10484 h->root.root.string, def_bfd);
10485 bfd_set_error (bfd_error_bad_value);
10486 eoinfo->failed = true;
10487 return false;
10488 }
10489
10490 /* We don't want to output symbols that have never been mentioned by
10491 a regular file, or that we have been told to strip. However, if
10492 h->indx is set to -2, the symbol is used by a reloc and we must
10493 output it. */
10494 strip = false;
10495 if (h->indx == -2)
10496 ;
10497 else if ((h->def_dynamic
10498 || h->ref_dynamic
10499 || h->root.type == bfd_link_hash_new)
10500 && !h->def_regular
10501 && !h->ref_regular)
10502 strip = true;
10503 else if (flinfo->info->strip == strip_all)
10504 strip = true;
10505 else if (flinfo->info->strip == strip_some
10506 && bfd_hash_lookup (flinfo->info->keep_hash,
10507 h->root.root.string, false, false) == NULL)
10508 strip = true;
10509 else if ((h->root.type == bfd_link_hash_defined
10510 || h->root.type == bfd_link_hash_defweak)
10511 && ((flinfo->info->strip_discarded
10512 && discarded_section (h->root.u.def.section))
10513 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
10514 && h->root.u.def.section->owner != NULL
10515 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
10516 strip = true;
10517 else if ((h->root.type == bfd_link_hash_undefined
10518 || h->root.type == bfd_link_hash_undefweak)
10519 && h->root.u.undef.abfd != NULL
10520 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
10521 strip = true;
10522
10523 type = h->type;
10524
10525 /* If we're stripping it, and it's not a dynamic symbol, there's
10526 nothing else to do. However, if it is a forced local symbol or
10527 an ifunc symbol we need to give the backend finish_dynamic_symbol
10528 function a chance to make it dynamic. */
10529 if (strip
10530 && h->dynindx == -1
10531 && type != STT_GNU_IFUNC
10532 && !h->forced_local)
10533 return true;
10534
10535 sym.st_value = 0;
10536 sym.st_size = h->size;
10537 sym.st_other = h->other;
10538 switch (h->root.type)
10539 {
10540 default:
10541 case bfd_link_hash_new:
10542 case bfd_link_hash_warning:
10543 abort ();
10544 return false;
10545
10546 case bfd_link_hash_undefined:
10547 case bfd_link_hash_undefweak:
10548 input_sec = bfd_und_section_ptr;
10549 sym.st_shndx = SHN_UNDEF;
10550 break;
10551
10552 case bfd_link_hash_defined:
10553 case bfd_link_hash_defweak:
10554 {
10555 input_sec = h->root.u.def.section;
10556 if (input_sec->output_section != NULL)
10557 {
10558 sym.st_shndx =
10559 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
10560 input_sec->output_section);
10561 if (sym.st_shndx == SHN_BAD)
10562 {
10563 _bfd_error_handler
10564 /* xgettext:c-format */
10565 (_("%pB: could not find output section %pA for input section %pA"),
10566 flinfo->output_bfd, input_sec->output_section, input_sec);
10567 bfd_set_error (bfd_error_nonrepresentable_section);
10568 eoinfo->failed = true;
10569 return false;
10570 }
10571
10572 /* ELF symbols in relocatable files are section relative,
10573 but in nonrelocatable files they are virtual
10574 addresses. */
10575 sym.st_value = h->root.u.def.value + input_sec->output_offset;
10576 if (!bfd_link_relocatable (flinfo->info))
10577 {
10578 sym.st_value += input_sec->output_section->vma;
10579 if (h->type == STT_TLS)
10580 {
10581 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
10582 if (tls_sec != NULL)
10583 sym.st_value -= tls_sec->vma;
10584 }
10585 }
10586 }
10587 else
10588 {
10589 BFD_ASSERT (input_sec->owner == NULL
10590 || (input_sec->owner->flags & DYNAMIC) != 0);
10591 sym.st_shndx = SHN_UNDEF;
10592 input_sec = bfd_und_section_ptr;
10593 }
10594 }
10595 break;
10596
10597 case bfd_link_hash_common:
10598 input_sec = h->root.u.c.p->section;
10599 sym.st_shndx = bed->common_section_index (input_sec);
10600 sym.st_value = 1 << h->root.u.c.p->alignment_power;
10601 break;
10602
10603 case bfd_link_hash_indirect:
10604 /* These symbols are created by symbol versioning. They point
10605 to the decorated version of the name. For example, if the
10606 symbol foo@@GNU_1.2 is the default, which should be used when
10607 foo is used with no version, then we add an indirect symbol
10608 foo which points to foo@@GNU_1.2. We ignore these symbols,
10609 since the indirected symbol is already in the hash table. */
10610 return true;
10611 }
10612
10613 if (type == STT_COMMON || type == STT_OBJECT)
10614 switch (h->root.type)
10615 {
10616 case bfd_link_hash_common:
10617 type = elf_link_convert_common_type (flinfo->info, type);
10618 break;
10619 case bfd_link_hash_defined:
10620 case bfd_link_hash_defweak:
10621 if (bed->common_definition (&sym))
10622 type = elf_link_convert_common_type (flinfo->info, type);
10623 else
10624 type = STT_OBJECT;
10625 break;
10626 case bfd_link_hash_undefined:
10627 case bfd_link_hash_undefweak:
10628 break;
10629 default:
10630 abort ();
10631 }
10632
10633 if (h->forced_local)
10634 {
10635 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
10636 /* Turn off visibility on local symbol. */
10637 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
10638 }
10639 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10640 else if (h->unique_global && h->def_regular)
10641 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
10642 else if (h->root.type == bfd_link_hash_undefweak
10643 || h->root.type == bfd_link_hash_defweak)
10644 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
10645 else
10646 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
10647 sym.st_target_internal = h->target_internal;
10648
10649 /* Give the processor backend a chance to tweak the symbol value,
10650 and also to finish up anything that needs to be done for this
10651 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
10652 forced local syms when non-shared is due to a historical quirk.
10653 STT_GNU_IFUNC symbol must go through PLT. */
10654 if ((h->type == STT_GNU_IFUNC
10655 && h->def_regular
10656 && !bfd_link_relocatable (flinfo->info))
10657 || ((h->dynindx != -1
10658 || h->forced_local)
10659 && ((bfd_link_pic (flinfo->info)
10660 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
10661 || h->root.type != bfd_link_hash_undefweak))
10662 || !h->forced_local)
10663 && elf_hash_table (flinfo->info)->dynamic_sections_created))
10664 {
10665 if (! ((*bed->elf_backend_finish_dynamic_symbol)
10666 (flinfo->output_bfd, flinfo->info, h, &sym)))
10667 {
10668 eoinfo->failed = true;
10669 return false;
10670 }
10671 }
10672
10673 /* If we are marking the symbol as undefined, and there are no
10674 non-weak references to this symbol from a regular object, then
10675 mark the symbol as weak undefined; if there are non-weak
10676 references, mark the symbol as strong. We can't do this earlier,
10677 because it might not be marked as undefined until the
10678 finish_dynamic_symbol routine gets through with it. */
10679 if (sym.st_shndx == SHN_UNDEF
10680 && h->ref_regular
10681 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
10682 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
10683 {
10684 int bindtype;
10685 type = ELF_ST_TYPE (sym.st_info);
10686
10687 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10688 if (type == STT_GNU_IFUNC)
10689 type = STT_FUNC;
10690
10691 if (h->ref_regular_nonweak)
10692 bindtype = STB_GLOBAL;
10693 else
10694 bindtype = STB_WEAK;
10695 sym.st_info = ELF_ST_INFO (bindtype, type);
10696 }
10697
10698 /* If this is a symbol defined in a dynamic library, don't use the
10699 symbol size from the dynamic library. Relinking an executable
10700 against a new library may introduce gratuitous changes in the
10701 executable's symbols if we keep the size. */
10702 if (sym.st_shndx == SHN_UNDEF
10703 && !h->def_regular
10704 && h->def_dynamic)
10705 sym.st_size = 0;
10706
10707 /* If a non-weak symbol with non-default visibility is not defined
10708 locally, it is a fatal error. */
10709 if (!bfd_link_relocatable (flinfo->info)
10710 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
10711 && ELF_ST_BIND (sym.st_info) != STB_WEAK
10712 && h->root.type == bfd_link_hash_undefined
10713 && !h->def_regular)
10714 {
10715 const char *msg;
10716
10717 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
10718 /* xgettext:c-format */
10719 msg = _("%pB: protected symbol `%s' isn't defined");
10720 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
10721 /* xgettext:c-format */
10722 msg = _("%pB: internal symbol `%s' isn't defined");
10723 else
10724 /* xgettext:c-format */
10725 msg = _("%pB: hidden symbol `%s' isn't defined");
10726 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
10727 bfd_set_error (bfd_error_bad_value);
10728 eoinfo->failed = true;
10729 return false;
10730 }
10731
10732 /* If this symbol should be put in the .dynsym section, then put it
10733 there now. We already know the symbol index. We also fill in
10734 the entry in the .hash section. */
10735 if (h->dynindx != -1
10736 && elf_hash_table (flinfo->info)->dynamic_sections_created
10737 && elf_hash_table (flinfo->info)->dynsym != NULL
10738 && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
10739 {
10740 bfd_byte *esym;
10741
10742 /* Since there is no version information in the dynamic string,
10743 if there is no version info in symbol version section, we will
10744 have a run-time problem if not linking executable, referenced
10745 by shared library, or not bound locally. */
10746 if (h->verinfo.verdef == NULL
10747 && (!bfd_link_executable (flinfo->info)
10748 || h->ref_dynamic
10749 || !h->def_regular))
10750 {
10751 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
10752
10753 if (p && p [1] != '\0')
10754 {
10755 _bfd_error_handler
10756 /* xgettext:c-format */
10757 (_("%pB: no symbol version section for versioned symbol `%s'"),
10758 flinfo->output_bfd, h->root.root.string);
10759 eoinfo->failed = true;
10760 return false;
10761 }
10762 }
10763
10764 sym.st_name = h->dynstr_index;
10765 esym = (elf_hash_table (flinfo->info)->dynsym->contents
10766 + h->dynindx * bed->s->sizeof_sym);
10767 if (!check_dynsym (flinfo->output_bfd, &sym))
10768 {
10769 eoinfo->failed = true;
10770 return false;
10771 }
10772
10773 /* Inform the linker of the addition of this symbol. */
10774
10775 if (flinfo->info->callbacks->ctf_new_dynsym)
10776 flinfo->info->callbacks->ctf_new_dynsym (h->dynindx, &sym);
10777
10778 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
10779
10780 if (flinfo->hash_sec != NULL)
10781 {
10782 size_t hash_entry_size;
10783 bfd_byte *bucketpos;
10784 bfd_vma chain;
10785 size_t bucketcount;
10786 size_t bucket;
10787
10788 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
10789 bucket = h->u.elf_hash_value % bucketcount;
10790
10791 hash_entry_size
10792 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
10793 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
10794 + (bucket + 2) * hash_entry_size);
10795 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
10796 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
10797 bucketpos);
10798 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
10799 ((bfd_byte *) flinfo->hash_sec->contents
10800 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
10801 }
10802
10803 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
10804 {
10805 Elf_Internal_Versym iversym;
10806 Elf_External_Versym *eversym;
10807
10808 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
10809 {
10810 if (h->verinfo.verdef == NULL
10811 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
10812 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
10813 iversym.vs_vers = 1;
10814 else
10815 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
10816 }
10817 else
10818 {
10819 if (h->verinfo.vertree == NULL)
10820 iversym.vs_vers = 1;
10821 else
10822 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
10823 if (flinfo->info->create_default_symver)
10824 iversym.vs_vers++;
10825 }
10826
10827 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
10828 defined locally. */
10829 if (h->versioned == versioned_hidden && h->def_regular)
10830 iversym.vs_vers |= VERSYM_HIDDEN;
10831
10832 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
10833 eversym += h->dynindx;
10834 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
10835 }
10836 }
10837
10838 /* If the symbol is undefined, and we didn't output it to .dynsym,
10839 strip it from .symtab too. Obviously we can't do this for
10840 relocatable output or when needed for --emit-relocs. */
10841 else if (input_sec == bfd_und_section_ptr
10842 && h->indx != -2
10843 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10844 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
10845 && !bfd_link_relocatable (flinfo->info))
10846 return true;
10847
10848 /* Also strip others that we couldn't earlier due to dynamic symbol
10849 processing. */
10850 if (strip)
10851 return true;
10852 if ((input_sec->flags & SEC_EXCLUDE) != 0)
10853 return true;
10854
10855 /* Output a FILE symbol so that following locals are not associated
10856 with the wrong input file. We need one for forced local symbols
10857 if we've seen more than one FILE symbol or when we have exactly
10858 one FILE symbol but global symbols are present in a file other
10859 than the one with the FILE symbol. We also need one if linker
10860 defined symbols are present. In practice these conditions are
10861 always met, so just emit the FILE symbol unconditionally. */
10862 if (eoinfo->localsyms
10863 && !eoinfo->file_sym_done
10864 && eoinfo->flinfo->filesym_count != 0)
10865 {
10866 Elf_Internal_Sym fsym;
10867
10868 memset (&fsym, 0, sizeof (fsym));
10869 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10870 fsym.st_shndx = SHN_ABS;
10871 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10872 bfd_und_section_ptr, NULL))
10873 return false;
10874
10875 eoinfo->file_sym_done = true;
10876 }
10877
10878 indx = bfd_get_symcount (flinfo->output_bfd);
10879 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10880 input_sec, h);
10881 if (ret == 0)
10882 {
10883 eoinfo->failed = true;
10884 return false;
10885 }
10886 else if (ret == 1)
10887 h->indx = indx;
10888 else if (h->indx == -2)
10889 abort();
10890
10891 return true;
10892 }
10893
10894 /* Return TRUE if special handling is done for relocs in SEC against
10895 symbols defined in discarded sections. */
10896
10897 static bool
10898 elf_section_ignore_discarded_relocs (asection *sec)
10899 {
10900 const struct elf_backend_data *bed;
10901
10902 switch (sec->sec_info_type)
10903 {
10904 case SEC_INFO_TYPE_STABS:
10905 case SEC_INFO_TYPE_EH_FRAME:
10906 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10907 return true;
10908 default:
10909 break;
10910 }
10911
10912 bed = get_elf_backend_data (sec->owner);
10913 if (bed->elf_backend_ignore_discarded_relocs != NULL
10914 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10915 return true;
10916
10917 return false;
10918 }
10919
10920 /* Return a mask saying how ld should treat relocations in SEC against
10921 symbols defined in discarded sections. If this function returns
10922 COMPLAIN set, ld will issue a warning message. If this function
10923 returns PRETEND set, and the discarded section was link-once and the
10924 same size as the kept link-once section, ld will pretend that the
10925 symbol was actually defined in the kept section. Otherwise ld will
10926 zero the reloc (at least that is the intent, but some cooperation by
10927 the target dependent code is needed, particularly for REL targets). */
10928
10929 unsigned int
10930 _bfd_elf_default_action_discarded (asection *sec)
10931 {
10932 if (sec->flags & SEC_DEBUGGING)
10933 return PRETEND;
10934
10935 if (strcmp (".eh_frame", sec->name) == 0)
10936 return 0;
10937
10938 if (strcmp (".gcc_except_table", sec->name) == 0)
10939 return 0;
10940
10941 return COMPLAIN | PRETEND;
10942 }
10943
10944 /* Find a match between a section and a member of a section group. */
10945
10946 static asection *
10947 match_group_member (asection *sec, asection *group,
10948 struct bfd_link_info *info)
10949 {
10950 asection *first = elf_next_in_group (group);
10951 asection *s = first;
10952
10953 while (s != NULL)
10954 {
10955 if (bfd_elf_match_symbols_in_sections (s, sec, info))
10956 return s;
10957
10958 s = elf_next_in_group (s);
10959 if (s == first)
10960 break;
10961 }
10962
10963 return NULL;
10964 }
10965
10966 /* Check if the kept section of a discarded section SEC can be used
10967 to replace it. Return the replacement if it is OK. Otherwise return
10968 NULL. */
10969
10970 asection *
10971 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
10972 {
10973 asection *kept;
10974
10975 kept = sec->kept_section;
10976 if (kept != NULL)
10977 {
10978 if ((kept->flags & SEC_GROUP) != 0)
10979 kept = match_group_member (sec, kept, info);
10980 if (kept != NULL)
10981 {
10982 if ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10983 != (kept->rawsize != 0 ? kept->rawsize : kept->size))
10984 kept = NULL;
10985 else
10986 {
10987 /* Get the real kept section. */
10988 asection *next;
10989 for (next = kept->kept_section;
10990 next != NULL;
10991 next = next->kept_section)
10992 kept = next;
10993 }
10994 }
10995 sec->kept_section = kept;
10996 }
10997 return kept;
10998 }
10999
11000 /* Link an input file into the linker output file. This function
11001 handles all the sections and relocations of the input file at once.
11002 This is so that we only have to read the local symbols once, and
11003 don't have to keep them in memory. */
11004
11005 static bool
11006 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
11007 {
11008 int (*relocate_section)
11009 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
11010 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
11011 bfd *output_bfd;
11012 Elf_Internal_Shdr *symtab_hdr;
11013 size_t locsymcount;
11014 size_t extsymoff;
11015 Elf_Internal_Sym *isymbuf;
11016 Elf_Internal_Sym *isym;
11017 Elf_Internal_Sym *isymend;
11018 long *pindex;
11019 asection **ppsection;
11020 asection *o;
11021 const struct elf_backend_data *bed;
11022 struct elf_link_hash_entry **sym_hashes;
11023 bfd_size_type address_size;
11024 bfd_vma r_type_mask;
11025 int r_sym_shift;
11026 bool have_file_sym = false;
11027
11028 output_bfd = flinfo->output_bfd;
11029 bed = get_elf_backend_data (output_bfd);
11030 relocate_section = bed->elf_backend_relocate_section;
11031
11032 /* If this is a dynamic object, we don't want to do anything here:
11033 we don't want the local symbols, and we don't want the section
11034 contents. */
11035 if ((input_bfd->flags & DYNAMIC) != 0)
11036 return true;
11037
11038 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
11039 if (elf_bad_symtab (input_bfd))
11040 {
11041 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
11042 extsymoff = 0;
11043 }
11044 else
11045 {
11046 locsymcount = symtab_hdr->sh_info;
11047 extsymoff = symtab_hdr->sh_info;
11048 }
11049
11050 /* Enable GNU OSABI features in the output BFD that are used in the input
11051 BFD. */
11052 if (bed->elf_osabi == ELFOSABI_NONE
11053 || bed->elf_osabi == ELFOSABI_GNU
11054 || bed->elf_osabi == ELFOSABI_FREEBSD)
11055 elf_tdata (output_bfd)->has_gnu_osabi
11056 |= (elf_tdata (input_bfd)->has_gnu_osabi
11057 & (bfd_link_relocatable (flinfo->info)
11058 ? -1 : ~elf_gnu_osabi_retain));
11059
11060 /* Read the local symbols. */
11061 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
11062 if (isymbuf == NULL && locsymcount != 0)
11063 {
11064 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
11065 flinfo->internal_syms,
11066 flinfo->external_syms,
11067 flinfo->locsym_shndx);
11068 if (isymbuf == NULL)
11069 return false;
11070 }
11071
11072 /* Find local symbol sections and adjust values of symbols in
11073 SEC_MERGE sections. Write out those local symbols we know are
11074 going into the output file. */
11075 isymend = PTR_ADD (isymbuf, locsymcount);
11076 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
11077 isym < isymend;
11078 isym++, pindex++, ppsection++)
11079 {
11080 asection *isec;
11081 const char *name;
11082 Elf_Internal_Sym osym;
11083 long indx;
11084 int ret;
11085
11086 *pindex = -1;
11087
11088 if (elf_bad_symtab (input_bfd))
11089 {
11090 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
11091 {
11092 *ppsection = NULL;
11093 continue;
11094 }
11095 }
11096
11097 if (isym->st_shndx == SHN_UNDEF)
11098 isec = bfd_und_section_ptr;
11099 else if (isym->st_shndx == SHN_ABS)
11100 isec = bfd_abs_section_ptr;
11101 else if (isym->st_shndx == SHN_COMMON)
11102 isec = bfd_com_section_ptr;
11103 else
11104 {
11105 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
11106 if (isec == NULL)
11107 {
11108 /* Don't attempt to output symbols with st_shnx in the
11109 reserved range other than SHN_ABS and SHN_COMMON. */
11110 isec = bfd_und_section_ptr;
11111 }
11112 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
11113 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
11114 isym->st_value =
11115 _bfd_merged_section_offset (output_bfd, &isec,
11116 elf_section_data (isec)->sec_info,
11117 isym->st_value);
11118 }
11119
11120 *ppsection = isec;
11121
11122 /* Don't output the first, undefined, symbol. In fact, don't
11123 output any undefined local symbol. */
11124 if (isec == bfd_und_section_ptr)
11125 continue;
11126
11127 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
11128 {
11129 /* We never output section symbols. Instead, we use the
11130 section symbol of the corresponding section in the output
11131 file. */
11132 continue;
11133 }
11134
11135 /* If we are stripping all symbols, we don't want to output this
11136 one. */
11137 if (flinfo->info->strip == strip_all)
11138 continue;
11139
11140 /* If we are discarding all local symbols, we don't want to
11141 output this one. If we are generating a relocatable output
11142 file, then some of the local symbols may be required by
11143 relocs; we output them below as we discover that they are
11144 needed. */
11145 if (flinfo->info->discard == discard_all)
11146 continue;
11147
11148 /* If this symbol is defined in a section which we are
11149 discarding, we don't need to keep it. */
11150 if (isym->st_shndx != SHN_UNDEF
11151 && isym->st_shndx < SHN_LORESERVE
11152 && isec->output_section == NULL
11153 && flinfo->info->non_contiguous_regions
11154 && flinfo->info->non_contiguous_regions_warnings)
11155 {
11156 _bfd_error_handler (_("warning: --enable-non-contiguous-regions "
11157 "discards section `%s' from '%s'\n"),
11158 isec->name, bfd_get_filename (isec->owner));
11159 continue;
11160 }
11161
11162 if (isym->st_shndx != SHN_UNDEF
11163 && isym->st_shndx < SHN_LORESERVE
11164 && bfd_section_removed_from_list (output_bfd,
11165 isec->output_section))
11166 continue;
11167
11168 /* Get the name of the symbol. */
11169 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
11170 isym->st_name);
11171 if (name == NULL)
11172 return false;
11173
11174 /* See if we are discarding symbols with this name. */
11175 if ((flinfo->info->strip == strip_some
11176 && (bfd_hash_lookup (flinfo->info->keep_hash, name, false, false)
11177 == NULL))
11178 || (((flinfo->info->discard == discard_sec_merge
11179 && (isec->flags & SEC_MERGE)
11180 && !bfd_link_relocatable (flinfo->info))
11181 || flinfo->info->discard == discard_l)
11182 && bfd_is_local_label_name (input_bfd, name)))
11183 continue;
11184
11185 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
11186 {
11187 if (input_bfd->lto_output)
11188 /* -flto puts a temp file name here. This means builds
11189 are not reproducible. Discard the symbol. */
11190 continue;
11191 have_file_sym = true;
11192 flinfo->filesym_count += 1;
11193 }
11194 if (!have_file_sym)
11195 {
11196 /* In the absence of debug info, bfd_find_nearest_line uses
11197 FILE symbols to determine the source file for local
11198 function symbols. Provide a FILE symbol here if input
11199 files lack such, so that their symbols won't be
11200 associated with a previous input file. It's not the
11201 source file, but the best we can do. */
11202 const char *filename;
11203 have_file_sym = true;
11204 flinfo->filesym_count += 1;
11205 memset (&osym, 0, sizeof (osym));
11206 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
11207 osym.st_shndx = SHN_ABS;
11208 if (input_bfd->lto_output)
11209 filename = NULL;
11210 else
11211 filename = lbasename (bfd_get_filename (input_bfd));
11212 if (!elf_link_output_symstrtab (flinfo, filename, &osym,
11213 bfd_abs_section_ptr, NULL))
11214 return false;
11215 }
11216
11217 osym = *isym;
11218
11219 /* Adjust the section index for the output file. */
11220 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11221 isec->output_section);
11222 if (osym.st_shndx == SHN_BAD)
11223 return false;
11224
11225 /* ELF symbols in relocatable files are section relative, but
11226 in executable files they are virtual addresses. Note that
11227 this code assumes that all ELF sections have an associated
11228 BFD section with a reasonable value for output_offset; below
11229 we assume that they also have a reasonable value for
11230 output_section. Any special sections must be set up to meet
11231 these requirements. */
11232 osym.st_value += isec->output_offset;
11233 if (!bfd_link_relocatable (flinfo->info))
11234 {
11235 osym.st_value += isec->output_section->vma;
11236 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
11237 {
11238 /* STT_TLS symbols are relative to PT_TLS segment base. */
11239 if (elf_hash_table (flinfo->info)->tls_sec != NULL)
11240 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
11241 else
11242 osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
11243 STT_NOTYPE);
11244 }
11245 }
11246
11247 indx = bfd_get_symcount (output_bfd);
11248 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
11249 if (ret == 0)
11250 return false;
11251 else if (ret == 1)
11252 *pindex = indx;
11253 }
11254
11255 if (bed->s->arch_size == 32)
11256 {
11257 r_type_mask = 0xff;
11258 r_sym_shift = 8;
11259 address_size = 4;
11260 }
11261 else
11262 {
11263 r_type_mask = 0xffffffff;
11264 r_sym_shift = 32;
11265 address_size = 8;
11266 }
11267
11268 /* Relocate the contents of each section. */
11269 sym_hashes = elf_sym_hashes (input_bfd);
11270 for (o = input_bfd->sections; o != NULL; o = o->next)
11271 {
11272 bfd_byte *contents;
11273
11274 if (! o->linker_mark)
11275 {
11276 /* This section was omitted from the link. */
11277 continue;
11278 }
11279
11280 if (!flinfo->info->resolve_section_groups
11281 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
11282 {
11283 /* Deal with the group signature symbol. */
11284 struct bfd_elf_section_data *sec_data = elf_section_data (o);
11285 unsigned long symndx = sec_data->this_hdr.sh_info;
11286 asection *osec = o->output_section;
11287
11288 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
11289 if (symndx >= locsymcount
11290 || (elf_bad_symtab (input_bfd)
11291 && flinfo->sections[symndx] == NULL))
11292 {
11293 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
11294 while (h->root.type == bfd_link_hash_indirect
11295 || h->root.type == bfd_link_hash_warning)
11296 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11297 /* Arrange for symbol to be output. */
11298 h->indx = -2;
11299 elf_section_data (osec)->this_hdr.sh_info = -2;
11300 }
11301 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
11302 {
11303 /* We'll use the output section target_index. */
11304 asection *sec = flinfo->sections[symndx]->output_section;
11305 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
11306 }
11307 else
11308 {
11309 if (flinfo->indices[symndx] == -1)
11310 {
11311 /* Otherwise output the local symbol now. */
11312 Elf_Internal_Sym sym = isymbuf[symndx];
11313 asection *sec = flinfo->sections[symndx]->output_section;
11314 const char *name;
11315 long indx;
11316 int ret;
11317
11318 name = bfd_elf_string_from_elf_section (input_bfd,
11319 symtab_hdr->sh_link,
11320 sym.st_name);
11321 if (name == NULL)
11322 return false;
11323
11324 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11325 sec);
11326 if (sym.st_shndx == SHN_BAD)
11327 return false;
11328
11329 sym.st_value += o->output_offset;
11330
11331 indx = bfd_get_symcount (output_bfd);
11332 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
11333 NULL);
11334 if (ret == 0)
11335 return false;
11336 else if (ret == 1)
11337 flinfo->indices[symndx] = indx;
11338 else
11339 abort ();
11340 }
11341 elf_section_data (osec)->this_hdr.sh_info
11342 = flinfo->indices[symndx];
11343 }
11344 }
11345
11346 if ((o->flags & SEC_HAS_CONTENTS) == 0
11347 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
11348 continue;
11349
11350 if ((o->flags & SEC_LINKER_CREATED) != 0)
11351 {
11352 /* Section was created by _bfd_elf_link_create_dynamic_sections
11353 or somesuch. */
11354 continue;
11355 }
11356
11357 /* Get the contents of the section. They have been cached by a
11358 relaxation routine. Note that o is a section in an input
11359 file, so the contents field will not have been set by any of
11360 the routines which work on output files. */
11361 if (elf_section_data (o)->this_hdr.contents != NULL)
11362 {
11363 contents = elf_section_data (o)->this_hdr.contents;
11364 if (bed->caches_rawsize
11365 && o->rawsize != 0
11366 && o->rawsize < o->size)
11367 {
11368 memcpy (flinfo->contents, contents, o->rawsize);
11369 contents = flinfo->contents;
11370 }
11371 }
11372 else
11373 {
11374 contents = flinfo->contents;
11375 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
11376 return false;
11377 }
11378
11379 if ((o->flags & SEC_RELOC) != 0)
11380 {
11381 Elf_Internal_Rela *internal_relocs;
11382 Elf_Internal_Rela *rel, *relend;
11383 int action_discarded;
11384 int ret;
11385
11386 /* Get the swapped relocs. */
11387 internal_relocs
11388 = _bfd_elf_link_info_read_relocs (input_bfd, flinfo->info, o,
11389 flinfo->external_relocs,
11390 flinfo->internal_relocs,
11391 false);
11392 if (internal_relocs == NULL
11393 && o->reloc_count > 0)
11394 return false;
11395
11396 action_discarded = -1;
11397 if (!elf_section_ignore_discarded_relocs (o))
11398 action_discarded = (*bed->action_discarded) (o);
11399
11400 /* Run through the relocs evaluating complex reloc symbols and
11401 looking for relocs against symbols from discarded sections
11402 or section symbols from removed link-once sections.
11403 Complain about relocs against discarded sections. Zero
11404 relocs against removed link-once sections. */
11405
11406 rel = internal_relocs;
11407 relend = rel + o->reloc_count;
11408 for ( ; rel < relend; rel++)
11409 {
11410 unsigned long r_symndx = rel->r_info >> r_sym_shift;
11411 unsigned int s_type;
11412 asection **ps, *sec;
11413 struct elf_link_hash_entry *h = NULL;
11414 const char *sym_name;
11415
11416 if (r_symndx == STN_UNDEF)
11417 continue;
11418
11419 if (r_symndx >= locsymcount
11420 || (elf_bad_symtab (input_bfd)
11421 && flinfo->sections[r_symndx] == NULL))
11422 {
11423 h = sym_hashes[r_symndx - extsymoff];
11424
11425 /* Badly formatted input files can contain relocs that
11426 reference non-existant symbols. Check here so that
11427 we do not seg fault. */
11428 if (h == NULL)
11429 {
11430 _bfd_error_handler
11431 /* xgettext:c-format */
11432 (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
11433 "that references a non-existent global symbol"),
11434 input_bfd, (uint64_t) rel->r_info, o);
11435 bfd_set_error (bfd_error_bad_value);
11436 return false;
11437 }
11438
11439 while (h->root.type == bfd_link_hash_indirect
11440 || h->root.type == bfd_link_hash_warning)
11441 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11442
11443 s_type = h->type;
11444
11445 /* If a plugin symbol is referenced from a non-IR file,
11446 mark the symbol as undefined. Note that the
11447 linker may attach linker created dynamic sections
11448 to the plugin bfd. Symbols defined in linker
11449 created sections are not plugin symbols. */
11450 if ((h->root.non_ir_ref_regular
11451 || h->root.non_ir_ref_dynamic)
11452 && (h->root.type == bfd_link_hash_defined
11453 || h->root.type == bfd_link_hash_defweak)
11454 && (h->root.u.def.section->flags
11455 & SEC_LINKER_CREATED) == 0
11456 && h->root.u.def.section->owner != NULL
11457 && (h->root.u.def.section->owner->flags
11458 & BFD_PLUGIN) != 0)
11459 {
11460 h->root.type = bfd_link_hash_undefined;
11461 h->root.u.undef.abfd = h->root.u.def.section->owner;
11462 }
11463
11464 ps = NULL;
11465 if (h->root.type == bfd_link_hash_defined
11466 || h->root.type == bfd_link_hash_defweak)
11467 ps = &h->root.u.def.section;
11468
11469 sym_name = h->root.root.string;
11470 }
11471 else
11472 {
11473 Elf_Internal_Sym *sym = isymbuf + r_symndx;
11474
11475 s_type = ELF_ST_TYPE (sym->st_info);
11476 ps = &flinfo->sections[r_symndx];
11477 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
11478 sym, *ps);
11479 }
11480
11481 if ((s_type == STT_RELC || s_type == STT_SRELC)
11482 && !bfd_link_relocatable (flinfo->info))
11483 {
11484 bfd_vma val;
11485 bfd_vma dot = (rel->r_offset
11486 + o->output_offset + o->output_section->vma);
11487 #ifdef DEBUG
11488 printf ("Encountered a complex symbol!");
11489 printf (" (input_bfd %s, section %s, reloc %ld\n",
11490 bfd_get_filename (input_bfd), o->name,
11491 (long) (rel - internal_relocs));
11492 printf (" symbol: idx %8.8lx, name %s\n",
11493 r_symndx, sym_name);
11494 printf (" reloc : info %8.8lx, addr %8.8lx\n",
11495 (unsigned long) rel->r_info,
11496 (unsigned long) rel->r_offset);
11497 #endif
11498 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
11499 isymbuf, locsymcount, s_type == STT_SRELC))
11500 return false;
11501
11502 /* Symbol evaluated OK. Update to absolute value. */
11503 set_symbol_value (input_bfd, isymbuf, locsymcount,
11504 r_symndx, val);
11505 continue;
11506 }
11507
11508 if (action_discarded != -1 && ps != NULL)
11509 {
11510 /* Complain if the definition comes from a
11511 discarded section. */
11512 if ((sec = *ps) != NULL && discarded_section (sec))
11513 {
11514 BFD_ASSERT (r_symndx != STN_UNDEF);
11515 if (action_discarded & COMPLAIN)
11516 (*flinfo->info->callbacks->einfo)
11517 /* xgettext:c-format */
11518 (_("%X`%s' referenced in section `%pA' of %pB: "
11519 "defined in discarded section `%pA' of %pB\n"),
11520 sym_name, o, input_bfd, sec, sec->owner);
11521
11522 /* Try to do the best we can to support buggy old
11523 versions of gcc. Pretend that the symbol is
11524 really defined in the kept linkonce section.
11525 FIXME: This is quite broken. Modifying the
11526 symbol here means we will be changing all later
11527 uses of the symbol, not just in this section. */
11528 if (action_discarded & PRETEND)
11529 {
11530 asection *kept;
11531
11532 kept = _bfd_elf_check_kept_section (sec,
11533 flinfo->info);
11534 if (kept != NULL)
11535 {
11536 *ps = kept;
11537 continue;
11538 }
11539 }
11540 }
11541 }
11542 }
11543
11544 /* Relocate the section by invoking a back end routine.
11545
11546 The back end routine is responsible for adjusting the
11547 section contents as necessary, and (if using Rela relocs
11548 and generating a relocatable output file) adjusting the
11549 reloc addend as necessary.
11550
11551 The back end routine does not have to worry about setting
11552 the reloc address or the reloc symbol index.
11553
11554 The back end routine is given a pointer to the swapped in
11555 internal symbols, and can access the hash table entries
11556 for the external symbols via elf_sym_hashes (input_bfd).
11557
11558 When generating relocatable output, the back end routine
11559 must handle STB_LOCAL/STT_SECTION symbols specially. The
11560 output symbol is going to be a section symbol
11561 corresponding to the output section, which will require
11562 the addend to be adjusted. */
11563
11564 ret = (*relocate_section) (output_bfd, flinfo->info,
11565 input_bfd, o, contents,
11566 internal_relocs,
11567 isymbuf,
11568 flinfo->sections);
11569 if (!ret)
11570 return false;
11571
11572 if (ret == 2
11573 || bfd_link_relocatable (flinfo->info)
11574 || flinfo->info->emitrelocations)
11575 {
11576 Elf_Internal_Rela *irela;
11577 Elf_Internal_Rela *irelaend, *irelamid;
11578 bfd_vma last_offset;
11579 struct elf_link_hash_entry **rel_hash;
11580 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
11581 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
11582 unsigned int next_erel;
11583 bool rela_normal;
11584 struct bfd_elf_section_data *esdi, *esdo;
11585
11586 esdi = elf_section_data (o);
11587 esdo = elf_section_data (o->output_section);
11588 rela_normal = false;
11589
11590 /* Adjust the reloc addresses and symbol indices. */
11591
11592 irela = internal_relocs;
11593 irelaend = irela + o->reloc_count;
11594 rel_hash = PTR_ADD (esdo->rel.hashes, esdo->rel.count);
11595 /* We start processing the REL relocs, if any. When we reach
11596 IRELAMID in the loop, we switch to the RELA relocs. */
11597 irelamid = irela;
11598 if (esdi->rel.hdr != NULL)
11599 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
11600 * bed->s->int_rels_per_ext_rel);
11601 rel_hash_list = rel_hash;
11602 rela_hash_list = NULL;
11603 last_offset = o->output_offset;
11604 if (!bfd_link_relocatable (flinfo->info))
11605 last_offset += o->output_section->vma;
11606 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
11607 {
11608 unsigned long r_symndx;
11609 asection *sec;
11610 Elf_Internal_Sym sym;
11611
11612 if (next_erel == bed->s->int_rels_per_ext_rel)
11613 {
11614 rel_hash++;
11615 next_erel = 0;
11616 }
11617
11618 if (irela == irelamid)
11619 {
11620 rel_hash = PTR_ADD (esdo->rela.hashes, esdo->rela.count);
11621 rela_hash_list = rel_hash;
11622 rela_normal = bed->rela_normal;
11623 }
11624
11625 irela->r_offset = _bfd_elf_section_offset (output_bfd,
11626 flinfo->info, o,
11627 irela->r_offset);
11628 if (irela->r_offset >= (bfd_vma) -2)
11629 {
11630 /* This is a reloc for a deleted entry or somesuch.
11631 Turn it into an R_*_NONE reloc, at the same
11632 offset as the last reloc. elf_eh_frame.c and
11633 bfd_elf_discard_info rely on reloc offsets
11634 being ordered. */
11635 irela->r_offset = last_offset;
11636 irela->r_info = 0;
11637 irela->r_addend = 0;
11638 continue;
11639 }
11640
11641 irela->r_offset += o->output_offset;
11642
11643 /* Relocs in an executable have to be virtual addresses. */
11644 if (!bfd_link_relocatable (flinfo->info))
11645 irela->r_offset += o->output_section->vma;
11646
11647 last_offset = irela->r_offset;
11648
11649 r_symndx = irela->r_info >> r_sym_shift;
11650 if (r_symndx == STN_UNDEF)
11651 continue;
11652
11653 if (r_symndx >= locsymcount
11654 || (elf_bad_symtab (input_bfd)
11655 && flinfo->sections[r_symndx] == NULL))
11656 {
11657 struct elf_link_hash_entry *rh;
11658 unsigned long indx;
11659
11660 /* This is a reloc against a global symbol. We
11661 have not yet output all the local symbols, so
11662 we do not know the symbol index of any global
11663 symbol. We set the rel_hash entry for this
11664 reloc to point to the global hash table entry
11665 for this symbol. The symbol index is then
11666 set at the end of bfd_elf_final_link. */
11667 indx = r_symndx - extsymoff;
11668 rh = elf_sym_hashes (input_bfd)[indx];
11669 while (rh->root.type == bfd_link_hash_indirect
11670 || rh->root.type == bfd_link_hash_warning)
11671 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
11672
11673 /* Setting the index to -2 tells
11674 elf_link_output_extsym that this symbol is
11675 used by a reloc. */
11676 BFD_ASSERT (rh->indx < 0);
11677 rh->indx = -2;
11678 *rel_hash = rh;
11679
11680 continue;
11681 }
11682
11683 /* This is a reloc against a local symbol. */
11684
11685 *rel_hash = NULL;
11686 sym = isymbuf[r_symndx];
11687 sec = flinfo->sections[r_symndx];
11688 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
11689 {
11690 /* I suppose the backend ought to fill in the
11691 section of any STT_SECTION symbol against a
11692 processor specific section. */
11693 r_symndx = STN_UNDEF;
11694 if (bfd_is_abs_section (sec))
11695 ;
11696 else if (sec == NULL || sec->owner == NULL)
11697 {
11698 bfd_set_error (bfd_error_bad_value);
11699 return false;
11700 }
11701 else
11702 {
11703 asection *osec = sec->output_section;
11704
11705 /* If we have discarded a section, the output
11706 section will be the absolute section. In
11707 case of discarded SEC_MERGE sections, use
11708 the kept section. relocate_section should
11709 have already handled discarded linkonce
11710 sections. */
11711 if (bfd_is_abs_section (osec)
11712 && sec->kept_section != NULL
11713 && sec->kept_section->output_section != NULL)
11714 {
11715 osec = sec->kept_section->output_section;
11716 irela->r_addend -= osec->vma;
11717 }
11718
11719 if (!bfd_is_abs_section (osec))
11720 {
11721 r_symndx = osec->target_index;
11722 if (r_symndx == STN_UNDEF)
11723 {
11724 irela->r_addend += osec->vma;
11725 osec = _bfd_nearby_section (output_bfd, osec,
11726 osec->vma);
11727 irela->r_addend -= osec->vma;
11728 r_symndx = osec->target_index;
11729 }
11730 }
11731 }
11732
11733 /* Adjust the addend according to where the
11734 section winds up in the output section. */
11735 if (rela_normal)
11736 irela->r_addend += sec->output_offset;
11737 }
11738 else
11739 {
11740 if (flinfo->indices[r_symndx] == -1)
11741 {
11742 unsigned long shlink;
11743 const char *name;
11744 asection *osec;
11745 long indx;
11746
11747 if (flinfo->info->strip == strip_all)
11748 {
11749 /* You can't do ld -r -s. */
11750 bfd_set_error (bfd_error_invalid_operation);
11751 return false;
11752 }
11753
11754 /* This symbol was skipped earlier, but
11755 since it is needed by a reloc, we
11756 must output it now. */
11757 shlink = symtab_hdr->sh_link;
11758 name = (bfd_elf_string_from_elf_section
11759 (input_bfd, shlink, sym.st_name));
11760 if (name == NULL)
11761 return false;
11762
11763 osec = sec->output_section;
11764 sym.st_shndx =
11765 _bfd_elf_section_from_bfd_section (output_bfd,
11766 osec);
11767 if (sym.st_shndx == SHN_BAD)
11768 return false;
11769
11770 sym.st_value += sec->output_offset;
11771 if (!bfd_link_relocatable (flinfo->info))
11772 {
11773 sym.st_value += osec->vma;
11774 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
11775 {
11776 struct elf_link_hash_table *htab
11777 = elf_hash_table (flinfo->info);
11778
11779 /* STT_TLS symbols are relative to PT_TLS
11780 segment base. */
11781 if (htab->tls_sec != NULL)
11782 sym.st_value -= htab->tls_sec->vma;
11783 else
11784 sym.st_info
11785 = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
11786 STT_NOTYPE);
11787 }
11788 }
11789
11790 indx = bfd_get_symcount (output_bfd);
11791 ret = elf_link_output_symstrtab (flinfo, name,
11792 &sym, sec,
11793 NULL);
11794 if (ret == 0)
11795 return false;
11796 else if (ret == 1)
11797 flinfo->indices[r_symndx] = indx;
11798 else
11799 abort ();
11800 }
11801
11802 r_symndx = flinfo->indices[r_symndx];
11803 }
11804
11805 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
11806 | (irela->r_info & r_type_mask));
11807 }
11808
11809 /* Swap out the relocs. */
11810 input_rel_hdr = esdi->rel.hdr;
11811 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
11812 {
11813 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11814 input_rel_hdr,
11815 internal_relocs,
11816 rel_hash_list))
11817 return false;
11818 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
11819 * bed->s->int_rels_per_ext_rel);
11820 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
11821 }
11822
11823 input_rela_hdr = esdi->rela.hdr;
11824 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
11825 {
11826 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11827 input_rela_hdr,
11828 internal_relocs,
11829 rela_hash_list))
11830 return false;
11831 }
11832 }
11833 }
11834
11835 /* Write out the modified section contents. */
11836 if (bed->elf_backend_write_section
11837 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
11838 contents))
11839 {
11840 /* Section written out. */
11841 }
11842 else switch (o->sec_info_type)
11843 {
11844 case SEC_INFO_TYPE_STABS:
11845 if (! (_bfd_write_section_stabs
11846 (output_bfd,
11847 &elf_hash_table (flinfo->info)->stab_info,
11848 o, &elf_section_data (o)->sec_info, contents)))
11849 return false;
11850 break;
11851 case SEC_INFO_TYPE_MERGE:
11852 if (! _bfd_write_merged_section (output_bfd, o,
11853 elf_section_data (o)->sec_info))
11854 return false;
11855 break;
11856 case SEC_INFO_TYPE_EH_FRAME:
11857 {
11858 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
11859 o, contents))
11860 return false;
11861 }
11862 break;
11863 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11864 {
11865 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11866 flinfo->info,
11867 o, contents))
11868 return false;
11869 }
11870 break;
11871 default:
11872 {
11873 if (! (o->flags & SEC_EXCLUDE))
11874 {
11875 file_ptr offset = (file_ptr) o->output_offset;
11876 bfd_size_type todo = o->size;
11877
11878 offset *= bfd_octets_per_byte (output_bfd, o);
11879
11880 if ((o->flags & SEC_ELF_REVERSE_COPY)
11881 && o->size > address_size)
11882 {
11883 /* Reverse-copy input section to output. */
11884
11885 if ((o->size & (address_size - 1)) != 0
11886 || (o->reloc_count != 0
11887 && (o->size * bed->s->int_rels_per_ext_rel
11888 != o->reloc_count * address_size)))
11889 {
11890 _bfd_error_handler
11891 /* xgettext:c-format */
11892 (_("error: %pB: size of section %pA is not "
11893 "multiple of address size"),
11894 input_bfd, o);
11895 bfd_set_error (bfd_error_bad_value);
11896 return false;
11897 }
11898
11899 do
11900 {
11901 todo -= address_size;
11902 if (! bfd_set_section_contents (output_bfd,
11903 o->output_section,
11904 contents + todo,
11905 offset,
11906 address_size))
11907 return false;
11908 if (todo == 0)
11909 break;
11910 offset += address_size;
11911 }
11912 while (1);
11913 }
11914 else if (! bfd_set_section_contents (output_bfd,
11915 o->output_section,
11916 contents,
11917 offset, todo))
11918 return false;
11919 }
11920 }
11921 break;
11922 }
11923 }
11924
11925 return true;
11926 }
11927
11928 /* Generate a reloc when linking an ELF file. This is a reloc
11929 requested by the linker, and does not come from any input file. This
11930 is used to build constructor and destructor tables when linking
11931 with -Ur. */
11932
11933 static bool
11934 elf_reloc_link_order (bfd *output_bfd,
11935 struct bfd_link_info *info,
11936 asection *output_section,
11937 struct bfd_link_order *link_order)
11938 {
11939 reloc_howto_type *howto;
11940 long indx;
11941 bfd_vma offset;
11942 bfd_vma addend;
11943 struct bfd_elf_section_reloc_data *reldata;
11944 struct elf_link_hash_entry **rel_hash_ptr;
11945 Elf_Internal_Shdr *rel_hdr;
11946 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11947 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11948 bfd_byte *erel;
11949 unsigned int i;
11950 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
11951
11952 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11953 if (howto == NULL)
11954 {
11955 bfd_set_error (bfd_error_bad_value);
11956 return false;
11957 }
11958
11959 addend = link_order->u.reloc.p->addend;
11960
11961 if (esdo->rel.hdr)
11962 reldata = &esdo->rel;
11963 else if (esdo->rela.hdr)
11964 reldata = &esdo->rela;
11965 else
11966 {
11967 reldata = NULL;
11968 BFD_ASSERT (0);
11969 }
11970
11971 /* Figure out the symbol index. */
11972 rel_hash_ptr = reldata->hashes + reldata->count;
11973 if (link_order->type == bfd_section_reloc_link_order)
11974 {
11975 indx = link_order->u.reloc.p->u.section->target_index;
11976 BFD_ASSERT (indx != 0);
11977 *rel_hash_ptr = NULL;
11978 }
11979 else
11980 {
11981 struct elf_link_hash_entry *h;
11982
11983 /* Treat a reloc against a defined symbol as though it were
11984 actually against the section. */
11985 h = ((struct elf_link_hash_entry *)
11986 bfd_wrapped_link_hash_lookup (output_bfd, info,
11987 link_order->u.reloc.p->u.name,
11988 false, false, true));
11989 if (h != NULL
11990 && (h->root.type == bfd_link_hash_defined
11991 || h->root.type == bfd_link_hash_defweak))
11992 {
11993 asection *section;
11994
11995 section = h->root.u.def.section;
11996 indx = section->output_section->target_index;
11997 *rel_hash_ptr = NULL;
11998 /* It seems that we ought to add the symbol value to the
11999 addend here, but in practice it has already been added
12000 because it was passed to constructor_callback. */
12001 addend += section->output_section->vma + section->output_offset;
12002 }
12003 else if (h != NULL)
12004 {
12005 /* Setting the index to -2 tells elf_link_output_extsym that
12006 this symbol is used by a reloc. */
12007 h->indx = -2;
12008 *rel_hash_ptr = h;
12009 indx = 0;
12010 }
12011 else
12012 {
12013 (*info->callbacks->unattached_reloc)
12014 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
12015 indx = 0;
12016 }
12017 }
12018
12019 /* If this is an inplace reloc, we must write the addend into the
12020 object file. */
12021 if (howto->partial_inplace && addend != 0)
12022 {
12023 bfd_size_type size;
12024 bfd_reloc_status_type rstat;
12025 bfd_byte *buf;
12026 bool ok;
12027 const char *sym_name;
12028 bfd_size_type octets;
12029
12030 size = (bfd_size_type) bfd_get_reloc_size (howto);
12031 buf = (bfd_byte *) bfd_zmalloc (size);
12032 if (buf == NULL && size != 0)
12033 return false;
12034 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
12035 switch (rstat)
12036 {
12037 case bfd_reloc_ok:
12038 break;
12039
12040 default:
12041 case bfd_reloc_outofrange:
12042 abort ();
12043
12044 case bfd_reloc_overflow:
12045 if (link_order->type == bfd_section_reloc_link_order)
12046 sym_name = bfd_section_name (link_order->u.reloc.p->u.section);
12047 else
12048 sym_name = link_order->u.reloc.p->u.name;
12049 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
12050 howto->name, addend, NULL, NULL,
12051 (bfd_vma) 0);
12052 break;
12053 }
12054
12055 octets = link_order->offset * bfd_octets_per_byte (output_bfd,
12056 output_section);
12057 ok = bfd_set_section_contents (output_bfd, output_section, buf,
12058 octets, size);
12059 free (buf);
12060 if (! ok)
12061 return false;
12062 }
12063
12064 /* The address of a reloc is relative to the section in a
12065 relocatable file, and is a virtual address in an executable
12066 file. */
12067 offset = link_order->offset;
12068 if (! bfd_link_relocatable (info))
12069 offset += output_section->vma;
12070
12071 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
12072 {
12073 irel[i].r_offset = offset;
12074 irel[i].r_info = 0;
12075 irel[i].r_addend = 0;
12076 }
12077 if (bed->s->arch_size == 32)
12078 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
12079 else
12080 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
12081
12082 rel_hdr = reldata->hdr;
12083 erel = rel_hdr->contents;
12084 if (rel_hdr->sh_type == SHT_REL)
12085 {
12086 erel += reldata->count * bed->s->sizeof_rel;
12087 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
12088 }
12089 else
12090 {
12091 irel[0].r_addend = addend;
12092 erel += reldata->count * bed->s->sizeof_rela;
12093 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
12094 }
12095
12096 ++reldata->count;
12097
12098 return true;
12099 }
12100
12101 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
12102 Returns TRUE upon success, FALSE otherwise. */
12103
12104 static bool
12105 elf_output_implib (bfd *abfd, struct bfd_link_info *info)
12106 {
12107 bool ret = false;
12108 bfd *implib_bfd;
12109 const struct elf_backend_data *bed;
12110 flagword flags;
12111 enum bfd_architecture arch;
12112 unsigned int mach;
12113 asymbol **sympp = NULL;
12114 long symsize;
12115 long symcount;
12116 long src_count;
12117 elf_symbol_type *osymbuf;
12118 size_t amt;
12119
12120 implib_bfd = info->out_implib_bfd;
12121 bed = get_elf_backend_data (abfd);
12122
12123 if (!bfd_set_format (implib_bfd, bfd_object))
12124 return false;
12125
12126 /* Use flag from executable but make it a relocatable object. */
12127 flags = bfd_get_file_flags (abfd);
12128 flags &= ~HAS_RELOC;
12129 if (!bfd_set_start_address (implib_bfd, 0)
12130 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
12131 return false;
12132
12133 /* Copy architecture of output file to import library file. */
12134 arch = bfd_get_arch (abfd);
12135 mach = bfd_get_mach (abfd);
12136 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
12137 && (abfd->target_defaulted
12138 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
12139 return false;
12140
12141 /* Get symbol table size. */
12142 symsize = bfd_get_symtab_upper_bound (abfd);
12143 if (symsize < 0)
12144 return false;
12145
12146 /* Read in the symbol table. */
12147 sympp = (asymbol **) bfd_malloc (symsize);
12148 if (sympp == NULL)
12149 return false;
12150
12151 symcount = bfd_canonicalize_symtab (abfd, sympp);
12152 if (symcount < 0)
12153 goto free_sym_buf;
12154
12155 /* Allow the BFD backend to copy any private header data it
12156 understands from the output BFD to the import library BFD. */
12157 if (! bfd_copy_private_header_data (abfd, implib_bfd))
12158 goto free_sym_buf;
12159
12160 /* Filter symbols to appear in the import library. */
12161 if (bed->elf_backend_filter_implib_symbols)
12162 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
12163 symcount);
12164 else
12165 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
12166 if (symcount == 0)
12167 {
12168 bfd_set_error (bfd_error_no_symbols);
12169 _bfd_error_handler (_("%pB: no symbol found for import library"),
12170 implib_bfd);
12171 goto free_sym_buf;
12172 }
12173
12174
12175 /* Make symbols absolute. */
12176 amt = symcount * sizeof (*osymbuf);
12177 osymbuf = (elf_symbol_type *) bfd_alloc (implib_bfd, amt);
12178 if (osymbuf == NULL)
12179 goto free_sym_buf;
12180
12181 for (src_count = 0; src_count < symcount; src_count++)
12182 {
12183 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
12184 sizeof (*osymbuf));
12185 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
12186 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
12187 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
12188 osymbuf[src_count].internal_elf_sym.st_value =
12189 osymbuf[src_count].symbol.value;
12190 sympp[src_count] = &osymbuf[src_count].symbol;
12191 }
12192
12193 bfd_set_symtab (implib_bfd, sympp, symcount);
12194
12195 /* Allow the BFD backend to copy any private data it understands
12196 from the output BFD to the import library BFD. This is done last
12197 to permit the routine to look at the filtered symbol table. */
12198 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
12199 goto free_sym_buf;
12200
12201 if (!bfd_close (implib_bfd))
12202 goto free_sym_buf;
12203
12204 ret = true;
12205
12206 free_sym_buf:
12207 free (sympp);
12208 return ret;
12209 }
12210
12211 static void
12212 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
12213 {
12214 asection *o;
12215
12216 if (flinfo->symstrtab != NULL)
12217 _bfd_elf_strtab_free (flinfo->symstrtab);
12218 free (flinfo->contents);
12219 free (flinfo->external_relocs);
12220 free (flinfo->internal_relocs);
12221 free (flinfo->external_syms);
12222 free (flinfo->locsym_shndx);
12223 free (flinfo->internal_syms);
12224 free (flinfo->indices);
12225 free (flinfo->sections);
12226 if (flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1)
12227 free (flinfo->symshndxbuf);
12228 for (o = obfd->sections; o != NULL; o = o->next)
12229 {
12230 struct bfd_elf_section_data *esdo = elf_section_data (o);
12231 free (esdo->rel.hashes);
12232 free (esdo->rela.hashes);
12233 }
12234 }
12235
12236 /* Do the final step of an ELF link. */
12237
12238 bool
12239 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
12240 {
12241 bool dynamic;
12242 bool emit_relocs;
12243 bfd *dynobj;
12244 struct elf_final_link_info flinfo;
12245 asection *o;
12246 struct bfd_link_order *p;
12247 bfd *sub;
12248 bfd_size_type max_contents_size;
12249 bfd_size_type max_external_reloc_size;
12250 bfd_size_type max_internal_reloc_count;
12251 bfd_size_type max_sym_count;
12252 bfd_size_type max_sym_shndx_count;
12253 Elf_Internal_Sym elfsym;
12254 unsigned int i;
12255 Elf_Internal_Shdr *symtab_hdr;
12256 Elf_Internal_Shdr *symtab_shndx_hdr;
12257 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12258 struct elf_outext_info eoinfo;
12259 bool merged;
12260 size_t relativecount;
12261 size_t relr_entsize;
12262 asection *reldyn = 0;
12263 bfd_size_type amt;
12264 asection *attr_section = NULL;
12265 bfd_vma attr_size = 0;
12266 const char *std_attrs_section;
12267 struct elf_link_hash_table *htab = elf_hash_table (info);
12268 bool sections_removed;
12269 bool ret;
12270
12271 if (!is_elf_hash_table (&htab->root))
12272 return false;
12273
12274 if (bfd_link_pic (info))
12275 abfd->flags |= DYNAMIC;
12276
12277 dynamic = htab->dynamic_sections_created;
12278 dynobj = htab->dynobj;
12279
12280 emit_relocs = (bfd_link_relocatable (info)
12281 || info->emitrelocations);
12282
12283 memset (&flinfo, 0, sizeof (flinfo));
12284 flinfo.info = info;
12285 flinfo.output_bfd = abfd;
12286 flinfo.symstrtab = _bfd_elf_strtab_init ();
12287 if (flinfo.symstrtab == NULL)
12288 return false;
12289
12290 if (! dynamic)
12291 {
12292 flinfo.hash_sec = NULL;
12293 flinfo.symver_sec = NULL;
12294 }
12295 else
12296 {
12297 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
12298 /* Note that dynsym_sec can be NULL (on VMS). */
12299 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
12300 /* Note that it is OK if symver_sec is NULL. */
12301 }
12302
12303 if (info->unique_symbol
12304 && !bfd_hash_table_init (&flinfo.local_hash_table,
12305 local_hash_newfunc,
12306 sizeof (struct local_hash_entry)))
12307 return false;
12308
12309 /* The object attributes have been merged. Remove the input
12310 sections from the link, and set the contents of the output
12311 section. */
12312 sections_removed = false;
12313 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
12314 for (o = abfd->sections; o != NULL; o = o->next)
12315 {
12316 bool remove_section = false;
12317
12318 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
12319 || strcmp (o->name, ".gnu.attributes") == 0)
12320 {
12321 for (p = o->map_head.link_order; p != NULL; p = p->next)
12322 {
12323 asection *input_section;
12324
12325 if (p->type != bfd_indirect_link_order)
12326 continue;
12327 input_section = p->u.indirect.section;
12328 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12329 elf_link_input_bfd ignores this section. */
12330 input_section->flags &= ~SEC_HAS_CONTENTS;
12331 }
12332
12333 attr_size = bfd_elf_obj_attr_size (abfd);
12334 bfd_set_section_size (o, attr_size);
12335 /* Skip this section later on. */
12336 o->map_head.link_order = NULL;
12337 if (attr_size)
12338 attr_section = o;
12339 else
12340 remove_section = true;
12341 }
12342 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
12343 {
12344 /* Remove empty group section from linker output. */
12345 remove_section = true;
12346 }
12347 if (remove_section)
12348 {
12349 o->flags |= SEC_EXCLUDE;
12350 bfd_section_list_remove (abfd, o);
12351 abfd->section_count--;
12352 sections_removed = true;
12353 }
12354 }
12355 if (sections_removed)
12356 _bfd_fix_excluded_sec_syms (abfd, info);
12357
12358 /* Count up the number of relocations we will output for each output
12359 section, so that we know the sizes of the reloc sections. We
12360 also figure out some maximum sizes. */
12361 max_contents_size = 0;
12362 max_external_reloc_size = 0;
12363 max_internal_reloc_count = 0;
12364 max_sym_count = 0;
12365 max_sym_shndx_count = 0;
12366 merged = false;
12367 for (o = abfd->sections; o != NULL; o = o->next)
12368 {
12369 struct bfd_elf_section_data *esdo = elf_section_data (o);
12370 o->reloc_count = 0;
12371
12372 for (p = o->map_head.link_order; p != NULL; p = p->next)
12373 {
12374 unsigned int reloc_count = 0;
12375 unsigned int additional_reloc_count = 0;
12376 struct bfd_elf_section_data *esdi = NULL;
12377
12378 if (p->type == bfd_section_reloc_link_order
12379 || p->type == bfd_symbol_reloc_link_order)
12380 reloc_count = 1;
12381 else if (p->type == bfd_indirect_link_order)
12382 {
12383 asection *sec;
12384
12385 sec = p->u.indirect.section;
12386
12387 /* Mark all sections which are to be included in the
12388 link. This will normally be every section. We need
12389 to do this so that we can identify any sections which
12390 the linker has decided to not include. */
12391 sec->linker_mark = true;
12392
12393 if (sec->flags & SEC_MERGE)
12394 merged = true;
12395
12396 if (sec->rawsize > max_contents_size)
12397 max_contents_size = sec->rawsize;
12398 if (sec->size > max_contents_size)
12399 max_contents_size = sec->size;
12400
12401 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
12402 && (sec->owner->flags & DYNAMIC) == 0)
12403 {
12404 size_t sym_count;
12405
12406 /* We are interested in just local symbols, not all
12407 symbols. */
12408 if (elf_bad_symtab (sec->owner))
12409 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
12410 / bed->s->sizeof_sym);
12411 else
12412 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
12413
12414 if (sym_count > max_sym_count)
12415 max_sym_count = sym_count;
12416
12417 if (sym_count > max_sym_shndx_count
12418 && elf_symtab_shndx_list (sec->owner) != NULL)
12419 max_sym_shndx_count = sym_count;
12420
12421 if (esdo->this_hdr.sh_type == SHT_REL
12422 || esdo->this_hdr.sh_type == SHT_RELA)
12423 /* Some backends use reloc_count in relocation sections
12424 to count particular types of relocs. Of course,
12425 reloc sections themselves can't have relocations. */
12426 ;
12427 else if (emit_relocs)
12428 {
12429 reloc_count = sec->reloc_count;
12430 if (bed->elf_backend_count_additional_relocs)
12431 {
12432 int c;
12433 c = (*bed->elf_backend_count_additional_relocs) (sec);
12434 additional_reloc_count += c;
12435 }
12436 }
12437 else if (bed->elf_backend_count_relocs)
12438 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
12439
12440 esdi = elf_section_data (sec);
12441
12442 if ((sec->flags & SEC_RELOC) != 0)
12443 {
12444 size_t ext_size = 0;
12445
12446 if (esdi->rel.hdr != NULL)
12447 ext_size = esdi->rel.hdr->sh_size;
12448 if (esdi->rela.hdr != NULL)
12449 ext_size += esdi->rela.hdr->sh_size;
12450
12451 if (ext_size > max_external_reloc_size)
12452 max_external_reloc_size = ext_size;
12453 if (sec->reloc_count > max_internal_reloc_count)
12454 max_internal_reloc_count = sec->reloc_count;
12455 }
12456 }
12457 }
12458
12459 if (reloc_count == 0)
12460 continue;
12461
12462 reloc_count += additional_reloc_count;
12463 o->reloc_count += reloc_count;
12464
12465 if (p->type == bfd_indirect_link_order && emit_relocs)
12466 {
12467 if (esdi->rel.hdr)
12468 {
12469 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
12470 esdo->rel.count += additional_reloc_count;
12471 }
12472 if (esdi->rela.hdr)
12473 {
12474 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
12475 esdo->rela.count += additional_reloc_count;
12476 }
12477 }
12478 else
12479 {
12480 if (o->use_rela_p)
12481 esdo->rela.count += reloc_count;
12482 else
12483 esdo->rel.count += reloc_count;
12484 }
12485 }
12486
12487 if (o->reloc_count > 0)
12488 o->flags |= SEC_RELOC;
12489 else
12490 {
12491 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12492 set it (this is probably a bug) and if it is set
12493 assign_section_numbers will create a reloc section. */
12494 o->flags &=~ SEC_RELOC;
12495 }
12496
12497 /* If the SEC_ALLOC flag is not set, force the section VMA to
12498 zero. This is done in elf_fake_sections as well, but forcing
12499 the VMA to 0 here will ensure that relocs against these
12500 sections are handled correctly. */
12501 if ((o->flags & SEC_ALLOC) == 0
12502 && ! o->user_set_vma)
12503 o->vma = 0;
12504 }
12505
12506 if (! bfd_link_relocatable (info) && merged)
12507 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
12508
12509 /* Figure out the file positions for everything but the symbol table
12510 and the relocs. We set symcount to force assign_section_numbers
12511 to create a symbol table. */
12512 abfd->symcount = info->strip != strip_all || emit_relocs;
12513 BFD_ASSERT (! abfd->output_has_begun);
12514 if (! _bfd_elf_compute_section_file_positions (abfd, info))
12515 goto error_return;
12516
12517 /* Set sizes, and assign file positions for reloc sections. */
12518 for (o = abfd->sections; o != NULL; o = o->next)
12519 {
12520 struct bfd_elf_section_data *esdo = elf_section_data (o);
12521 if ((o->flags & SEC_RELOC) != 0)
12522 {
12523 if (esdo->rel.hdr
12524 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
12525 goto error_return;
12526
12527 if (esdo->rela.hdr
12528 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
12529 goto error_return;
12530 }
12531
12532 /* _bfd_elf_compute_section_file_positions makes temporary use
12533 of target_index. Reset it. */
12534 o->target_index = 0;
12535
12536 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12537 to count upwards while actually outputting the relocations. */
12538 esdo->rel.count = 0;
12539 esdo->rela.count = 0;
12540
12541 if ((esdo->this_hdr.sh_offset == (file_ptr) -1)
12542 && !bfd_section_is_ctf (o))
12543 {
12544 /* Cache the section contents so that they can be compressed
12545 later. Use bfd_malloc since it will be freed by
12546 bfd_compress_section_contents. */
12547 unsigned char *contents = esdo->this_hdr.contents;
12548 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
12549 abort ();
12550 contents
12551 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
12552 if (contents == NULL)
12553 goto error_return;
12554 esdo->this_hdr.contents = contents;
12555 }
12556 }
12557
12558 /* We have now assigned file positions for all the sections except .symtab,
12559 .strtab, and non-loaded reloc and compressed debugging sections. We start
12560 the .symtab section at the current file position, and write directly to it.
12561 We build the .strtab section in memory. */
12562 abfd->symcount = 0;
12563 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12564 /* sh_name is set in prep_headers. */
12565 symtab_hdr->sh_type = SHT_SYMTAB;
12566 /* sh_flags, sh_addr and sh_size all start off zero. */
12567 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
12568 /* sh_link is set in assign_section_numbers. */
12569 /* sh_info is set below. */
12570 /* sh_offset is set just below. */
12571 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
12572
12573 if (max_sym_count < 20)
12574 max_sym_count = 20;
12575 htab->strtabsize = max_sym_count;
12576 amt = max_sym_count * sizeof (struct elf_sym_strtab);
12577 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
12578 if (htab->strtab == NULL)
12579 goto error_return;
12580 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12581 flinfo.symshndxbuf
12582 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
12583 ? (Elf_External_Sym_Shndx *) -1 : NULL);
12584
12585 if (info->strip != strip_all || emit_relocs)
12586 {
12587 file_ptr off = elf_next_file_pos (abfd);
12588
12589 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, true);
12590
12591 /* Note that at this point elf_next_file_pos (abfd) is
12592 incorrect. We do not yet know the size of the .symtab section.
12593 We correct next_file_pos below, after we do know the size. */
12594
12595 /* Start writing out the symbol table. The first symbol is always a
12596 dummy symbol. */
12597 elfsym.st_value = 0;
12598 elfsym.st_size = 0;
12599 elfsym.st_info = 0;
12600 elfsym.st_other = 0;
12601 elfsym.st_shndx = SHN_UNDEF;
12602 elfsym.st_target_internal = 0;
12603 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
12604 bfd_und_section_ptr, NULL) != 1)
12605 goto error_return;
12606
12607 /* Output a symbol for each section if asked or they are used for
12608 relocs. These symbols usually have no names. We store the
12609 index of each one in the index field of the section, so that
12610 we can find it again when outputting relocs. */
12611
12612 if (bfd_keep_unused_section_symbols (abfd) || emit_relocs)
12613 {
12614 bool name_local_sections
12615 = (bed->elf_backend_name_local_section_symbols
12616 && bed->elf_backend_name_local_section_symbols (abfd));
12617 const char *name = NULL;
12618
12619 elfsym.st_size = 0;
12620 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12621 elfsym.st_other = 0;
12622 elfsym.st_value = 0;
12623 elfsym.st_target_internal = 0;
12624 for (i = 1; i < elf_numsections (abfd); i++)
12625 {
12626 o = bfd_section_from_elf_index (abfd, i);
12627 if (o != NULL)
12628 {
12629 o->target_index = bfd_get_symcount (abfd);
12630 elfsym.st_shndx = i;
12631 if (!bfd_link_relocatable (info))
12632 elfsym.st_value = o->vma;
12633 if (name_local_sections)
12634 name = o->name;
12635 if (elf_link_output_symstrtab (&flinfo, name, &elfsym, o,
12636 NULL) != 1)
12637 goto error_return;
12638 }
12639 }
12640 }
12641 }
12642
12643 /* On some targets like Irix 5 the symbol split between local and global
12644 ones recorded in the sh_info field needs to be done between section
12645 and all other symbols. */
12646 if (bed->elf_backend_elfsym_local_is_section
12647 && bed->elf_backend_elfsym_local_is_section (abfd))
12648 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12649
12650 /* Allocate some memory to hold information read in from the input
12651 files. */
12652 if (max_contents_size != 0)
12653 {
12654 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12655 if (flinfo.contents == NULL)
12656 goto error_return;
12657 }
12658
12659 if (max_external_reloc_size != 0)
12660 {
12661 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
12662 if (flinfo.external_relocs == NULL)
12663 goto error_return;
12664 }
12665
12666 if (max_internal_reloc_count != 0)
12667 {
12668 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
12669 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
12670 if (flinfo.internal_relocs == NULL)
12671 goto error_return;
12672 }
12673
12674 if (max_sym_count != 0)
12675 {
12676 amt = max_sym_count * bed->s->sizeof_sym;
12677 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
12678 if (flinfo.external_syms == NULL)
12679 goto error_return;
12680
12681 amt = max_sym_count * sizeof (Elf_Internal_Sym);
12682 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
12683 if (flinfo.internal_syms == NULL)
12684 goto error_return;
12685
12686 amt = max_sym_count * sizeof (long);
12687 flinfo.indices = (long int *) bfd_malloc (amt);
12688 if (flinfo.indices == NULL)
12689 goto error_return;
12690
12691 amt = max_sym_count * sizeof (asection *);
12692 flinfo.sections = (asection **) bfd_malloc (amt);
12693 if (flinfo.sections == NULL)
12694 goto error_return;
12695 }
12696
12697 if (max_sym_shndx_count != 0)
12698 {
12699 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
12700 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
12701 if (flinfo.locsym_shndx == NULL)
12702 goto error_return;
12703 }
12704
12705 if (htab->tls_sec)
12706 {
12707 bfd_vma base, end = 0; /* Both bytes. */
12708 asection *sec;
12709
12710 for (sec = htab->tls_sec;
12711 sec && (sec->flags & SEC_THREAD_LOCAL);
12712 sec = sec->next)
12713 {
12714 bfd_size_type size = sec->size;
12715 unsigned int opb = bfd_octets_per_byte (abfd, sec);
12716
12717 if (size == 0
12718 && (sec->flags & SEC_HAS_CONTENTS) == 0)
12719 {
12720 struct bfd_link_order *ord = sec->map_tail.link_order;
12721
12722 if (ord != NULL)
12723 size = ord->offset * opb + ord->size;
12724 }
12725 end = sec->vma + size / opb;
12726 }
12727 base = htab->tls_sec->vma;
12728 /* Only align end of TLS section if static TLS doesn't have special
12729 alignment requirements. */
12730 if (bed->static_tls_alignment == 1)
12731 end = align_power (end, htab->tls_sec->alignment_power);
12732 htab->tls_size = end - base;
12733 }
12734
12735 if (!_bfd_elf_fixup_eh_frame_hdr (info))
12736 return false;
12737
12738 /* Finish relative relocations here after regular symbol processing
12739 is finished if DT_RELR is enabled. */
12740 if (info->enable_dt_relr
12741 && bed->finish_relative_relocs
12742 && !bed->finish_relative_relocs (info))
12743 info->callbacks->einfo
12744 (_("%F%P: %pB: failed to finish relative relocations\n"), abfd);
12745
12746 /* Since ELF permits relocations to be against local symbols, we
12747 must have the local symbols available when we do the relocations.
12748 Since we would rather only read the local symbols once, and we
12749 would rather not keep them in memory, we handle all the
12750 relocations for a single input file at the same time.
12751
12752 Unfortunately, there is no way to know the total number of local
12753 symbols until we have seen all of them, and the local symbol
12754 indices precede the global symbol indices. This means that when
12755 we are generating relocatable output, and we see a reloc against
12756 a global symbol, we can not know the symbol index until we have
12757 finished examining all the local symbols to see which ones we are
12758 going to output. To deal with this, we keep the relocations in
12759 memory, and don't output them until the end of the link. This is
12760 an unfortunate waste of memory, but I don't see a good way around
12761 it. Fortunately, it only happens when performing a relocatable
12762 link, which is not the common case. FIXME: If keep_memory is set
12763 we could write the relocs out and then read them again; I don't
12764 know how bad the memory loss will be. */
12765
12766 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12767 sub->output_has_begun = false;
12768 for (o = abfd->sections; o != NULL; o = o->next)
12769 {
12770 for (p = o->map_head.link_order; p != NULL; p = p->next)
12771 {
12772 if (p->type == bfd_indirect_link_order
12773 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12774 == bfd_target_elf_flavour)
12775 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12776 {
12777 if (! sub->output_has_begun)
12778 {
12779 if (! elf_link_input_bfd (&flinfo, sub))
12780 goto error_return;
12781 sub->output_has_begun = true;
12782 }
12783 }
12784 else if (p->type == bfd_section_reloc_link_order
12785 || p->type == bfd_symbol_reloc_link_order)
12786 {
12787 if (! elf_reloc_link_order (abfd, info, o, p))
12788 goto error_return;
12789 }
12790 else
12791 {
12792 if (! _bfd_default_link_order (abfd, info, o, p))
12793 {
12794 if (p->type == bfd_indirect_link_order
12795 && (bfd_get_flavour (sub)
12796 == bfd_target_elf_flavour)
12797 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12798 != bed->s->elfclass))
12799 {
12800 const char *iclass, *oclass;
12801
12802 switch (bed->s->elfclass)
12803 {
12804 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12805 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12806 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12807 default: abort ();
12808 }
12809
12810 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
12811 {
12812 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12813 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12814 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12815 default: abort ();
12816 }
12817
12818 bfd_set_error (bfd_error_wrong_format);
12819 _bfd_error_handler
12820 /* xgettext:c-format */
12821 (_("%pB: file class %s incompatible with %s"),
12822 sub, iclass, oclass);
12823 }
12824
12825 goto error_return;
12826 }
12827 }
12828 }
12829 }
12830
12831 /* Free symbol buffer if needed. */
12832 if (!info->reduce_memory_overheads)
12833 {
12834 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12835 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
12836 {
12837 free (elf_tdata (sub)->symbuf);
12838 elf_tdata (sub)->symbuf = NULL;
12839 }
12840 }
12841
12842 ret = true;
12843
12844 /* Output any global symbols that got converted to local in a
12845 version script or due to symbol visibility. We do this in a
12846 separate step since ELF requires all local symbols to appear
12847 prior to any global symbols. FIXME: We should only do this if
12848 some global symbols were, in fact, converted to become local.
12849 FIXME: Will this work correctly with the Irix 5 linker? */
12850 eoinfo.failed = false;
12851 eoinfo.flinfo = &flinfo;
12852 eoinfo.localsyms = true;
12853 eoinfo.file_sym_done = false;
12854 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12855 if (eoinfo.failed)
12856 {
12857 ret = false;
12858 goto return_local_hash_table;
12859 }
12860
12861 /* If backend needs to output some local symbols not present in the hash
12862 table, do it now. */
12863 if (bed->elf_backend_output_arch_local_syms
12864 && (info->strip != strip_all || emit_relocs))
12865 {
12866 if (! ((*bed->elf_backend_output_arch_local_syms)
12867 (abfd, info, &flinfo, elf_link_output_symstrtab)))
12868 {
12869 ret = false;
12870 goto return_local_hash_table;
12871 }
12872 }
12873
12874 /* That wrote out all the local symbols. Finish up the symbol table
12875 with the global symbols. Even if we want to strip everything we
12876 can, we still need to deal with those global symbols that got
12877 converted to local in a version script. */
12878
12879 /* The sh_info field records the index of the first non local symbol. */
12880 if (!symtab_hdr->sh_info)
12881 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12882
12883 if (dynamic
12884 && htab->dynsym != NULL
12885 && htab->dynsym->output_section != bfd_abs_section_ptr)
12886 {
12887 Elf_Internal_Sym sym;
12888 bfd_byte *dynsym = htab->dynsym->contents;
12889
12890 o = htab->dynsym->output_section;
12891 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
12892
12893 /* Write out the section symbols for the output sections. */
12894 if (bfd_link_pic (info)
12895 || htab->is_relocatable_executable)
12896 {
12897 asection *s;
12898
12899 sym.st_size = 0;
12900 sym.st_name = 0;
12901 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12902 sym.st_other = 0;
12903 sym.st_target_internal = 0;
12904
12905 for (s = abfd->sections; s != NULL; s = s->next)
12906 {
12907 int indx;
12908 bfd_byte *dest;
12909 long dynindx;
12910
12911 dynindx = elf_section_data (s)->dynindx;
12912 if (dynindx <= 0)
12913 continue;
12914 indx = elf_section_data (s)->this_idx;
12915 BFD_ASSERT (indx > 0);
12916 sym.st_shndx = indx;
12917 if (! check_dynsym (abfd, &sym))
12918 {
12919 ret = false;
12920 goto return_local_hash_table;
12921 }
12922 sym.st_value = s->vma;
12923 dest = dynsym + dynindx * bed->s->sizeof_sym;
12924
12925 /* Inform the linker of the addition of this symbol. */
12926
12927 if (info->callbacks->ctf_new_dynsym)
12928 info->callbacks->ctf_new_dynsym (dynindx, &sym);
12929
12930 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12931 }
12932 }
12933
12934 /* Write out the local dynsyms. */
12935 if (htab->dynlocal)
12936 {
12937 struct elf_link_local_dynamic_entry *e;
12938 for (e = htab->dynlocal; e ; e = e->next)
12939 {
12940 asection *s;
12941 bfd_byte *dest;
12942
12943 /* Copy the internal symbol and turn off visibility.
12944 Note that we saved a word of storage and overwrote
12945 the original st_name with the dynstr_index. */
12946 sym = e->isym;
12947 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
12948 sym.st_shndx = SHN_UNDEF;
12949
12950 s = bfd_section_from_elf_index (e->input_bfd,
12951 e->isym.st_shndx);
12952 if (s != NULL
12953 && s->output_section != NULL
12954 && elf_section_data (s->output_section) != NULL)
12955 {
12956 sym.st_shndx =
12957 elf_section_data (s->output_section)->this_idx;
12958 if (! check_dynsym (abfd, &sym))
12959 {
12960 ret = false;
12961 goto return_local_hash_table;
12962 }
12963 sym.st_value = (s->output_section->vma
12964 + s->output_offset
12965 + e->isym.st_value);
12966 }
12967
12968 /* Inform the linker of the addition of this symbol. */
12969
12970 if (info->callbacks->ctf_new_dynsym)
12971 info->callbacks->ctf_new_dynsym (e->dynindx, &sym);
12972
12973 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12974 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12975 }
12976 }
12977 }
12978
12979 /* We get the global symbols from the hash table. */
12980 eoinfo.failed = false;
12981 eoinfo.localsyms = false;
12982 eoinfo.flinfo = &flinfo;
12983 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12984 if (eoinfo.failed)
12985 {
12986 ret = false;
12987 goto return_local_hash_table;
12988 }
12989
12990 /* If backend needs to output some symbols not present in the hash
12991 table, do it now. */
12992 if (bed->elf_backend_output_arch_syms
12993 && (info->strip != strip_all || emit_relocs))
12994 {
12995 if (! ((*bed->elf_backend_output_arch_syms)
12996 (abfd, info, &flinfo, elf_link_output_symstrtab)))
12997 {
12998 ret = false;
12999 goto return_local_hash_table;
13000 }
13001 }
13002
13003 /* Finalize the .strtab section. */
13004 _bfd_elf_strtab_finalize (flinfo.symstrtab);
13005
13006 /* Swap out the .strtab section. */
13007 if (!elf_link_swap_symbols_out (&flinfo))
13008 {
13009 ret = false;
13010 goto return_local_hash_table;
13011 }
13012
13013 /* Now we know the size of the symtab section. */
13014 if (bfd_get_symcount (abfd) > 0)
13015 {
13016 /* Finish up and write out the symbol string table (.strtab)
13017 section. */
13018 Elf_Internal_Shdr *symstrtab_hdr = NULL;
13019 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
13020
13021 if (elf_symtab_shndx_list (abfd))
13022 {
13023 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
13024
13025 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
13026 {
13027 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
13028 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
13029 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
13030 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
13031 symtab_shndx_hdr->sh_size = amt;
13032
13033 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
13034 off, true);
13035
13036 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
13037 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
13038 {
13039 ret = false;
13040 goto return_local_hash_table;
13041 }
13042 }
13043 }
13044
13045 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
13046 /* sh_name was set in prep_headers. */
13047 symstrtab_hdr->sh_type = SHT_STRTAB;
13048 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
13049 symstrtab_hdr->sh_addr = 0;
13050 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
13051 symstrtab_hdr->sh_entsize = 0;
13052 symstrtab_hdr->sh_link = 0;
13053 symstrtab_hdr->sh_info = 0;
13054 /* sh_offset is set just below. */
13055 symstrtab_hdr->sh_addralign = 1;
13056
13057 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
13058 off, true);
13059 elf_next_file_pos (abfd) = off;
13060
13061 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
13062 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
13063 {
13064 ret = false;
13065 goto return_local_hash_table;
13066 }
13067 }
13068
13069 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
13070 {
13071 _bfd_error_handler (_("%pB: failed to generate import library"),
13072 info->out_implib_bfd);
13073 ret = false;
13074 goto return_local_hash_table;
13075 }
13076
13077 /* Adjust the relocs to have the correct symbol indices. */
13078 for (o = abfd->sections; o != NULL; o = o->next)
13079 {
13080 struct bfd_elf_section_data *esdo = elf_section_data (o);
13081 bool sort;
13082
13083 if ((o->flags & SEC_RELOC) == 0)
13084 continue;
13085
13086 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
13087 if (esdo->rel.hdr != NULL
13088 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
13089 {
13090 ret = false;
13091 goto return_local_hash_table;
13092 }
13093 if (esdo->rela.hdr != NULL
13094 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
13095 {
13096 ret = false;
13097 goto return_local_hash_table;
13098 }
13099
13100 /* Set the reloc_count field to 0 to prevent write_relocs from
13101 trying to swap the relocs out itself. */
13102 o->reloc_count = 0;
13103 }
13104
13105 relativecount = 0;
13106 if (dynamic && info->combreloc && dynobj != NULL)
13107 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
13108
13109 relr_entsize = 0;
13110 if (htab->srelrdyn != NULL
13111 && htab->srelrdyn->output_section != NULL
13112 && htab->srelrdyn->size != 0)
13113 {
13114 asection *s = htab->srelrdyn->output_section;
13115 relr_entsize = elf_section_data (s)->this_hdr.sh_entsize;
13116 if (relr_entsize == 0)
13117 {
13118 relr_entsize = bed->s->arch_size / 8;
13119 elf_section_data (s)->this_hdr.sh_entsize = relr_entsize;
13120 }
13121 }
13122
13123 /* If we are linking against a dynamic object, or generating a
13124 shared library, finish up the dynamic linking information. */
13125 if (dynamic)
13126 {
13127 bfd_byte *dyncon, *dynconend;
13128
13129 /* Fix up .dynamic entries. */
13130 o = bfd_get_linker_section (dynobj, ".dynamic");
13131 BFD_ASSERT (o != NULL);
13132
13133 dyncon = o->contents;
13134 dynconend = PTR_ADD (o->contents, o->size);
13135 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13136 {
13137 Elf_Internal_Dyn dyn;
13138 const char *name;
13139 unsigned int type;
13140 bfd_size_type sh_size;
13141 bfd_vma sh_addr;
13142
13143 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13144
13145 switch (dyn.d_tag)
13146 {
13147 default:
13148 continue;
13149 case DT_NULL:
13150 if (relativecount != 0)
13151 {
13152 switch (elf_section_data (reldyn)->this_hdr.sh_type)
13153 {
13154 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
13155 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
13156 }
13157 if (dyn.d_tag != DT_NULL
13158 && dynconend - dyncon >= bed->s->sizeof_dyn)
13159 {
13160 dyn.d_un.d_val = relativecount;
13161 relativecount = 0;
13162 break;
13163 }
13164 relativecount = 0;
13165 }
13166 if (relr_entsize != 0)
13167 {
13168 if (dynconend - dyncon >= 3 * bed->s->sizeof_dyn)
13169 {
13170 asection *s = htab->srelrdyn;
13171 dyn.d_tag = DT_RELR;
13172 dyn.d_un.d_ptr
13173 = s->output_section->vma + s->output_offset;
13174 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13175 dyncon += bed->s->sizeof_dyn;
13176
13177 dyn.d_tag = DT_RELRSZ;
13178 dyn.d_un.d_val = s->size;
13179 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13180 dyncon += bed->s->sizeof_dyn;
13181
13182 dyn.d_tag = DT_RELRENT;
13183 dyn.d_un.d_val = relr_entsize;
13184 relr_entsize = 0;
13185 break;
13186 }
13187 relr_entsize = 0;
13188 }
13189 continue;
13190
13191 case DT_INIT:
13192 name = info->init_function;
13193 goto get_sym;
13194 case DT_FINI:
13195 name = info->fini_function;
13196 get_sym:
13197 {
13198 struct elf_link_hash_entry *h;
13199
13200 h = elf_link_hash_lookup (htab, name, false, false, true);
13201 if (h != NULL
13202 && (h->root.type == bfd_link_hash_defined
13203 || h->root.type == bfd_link_hash_defweak))
13204 {
13205 dyn.d_un.d_ptr = h->root.u.def.value;
13206 o = h->root.u.def.section;
13207 if (o->output_section != NULL)
13208 dyn.d_un.d_ptr += (o->output_section->vma
13209 + o->output_offset);
13210 else
13211 {
13212 /* The symbol is imported from another shared
13213 library and does not apply to this one. */
13214 dyn.d_un.d_ptr = 0;
13215 }
13216 break;
13217 }
13218 }
13219 continue;
13220
13221 case DT_PREINIT_ARRAYSZ:
13222 name = ".preinit_array";
13223 goto get_out_size;
13224 case DT_INIT_ARRAYSZ:
13225 name = ".init_array";
13226 goto get_out_size;
13227 case DT_FINI_ARRAYSZ:
13228 name = ".fini_array";
13229 get_out_size:
13230 o = bfd_get_section_by_name (abfd, name);
13231 if (o == NULL)
13232 {
13233 _bfd_error_handler
13234 (_("could not find section %s"), name);
13235 goto error_return;
13236 }
13237 if (o->size == 0)
13238 _bfd_error_handler
13239 (_("warning: %s section has zero size"), name);
13240 dyn.d_un.d_val = o->size;
13241 break;
13242
13243 case DT_PREINIT_ARRAY:
13244 name = ".preinit_array";
13245 goto get_out_vma;
13246 case DT_INIT_ARRAY:
13247 name = ".init_array";
13248 goto get_out_vma;
13249 case DT_FINI_ARRAY:
13250 name = ".fini_array";
13251 get_out_vma:
13252 o = bfd_get_section_by_name (abfd, name);
13253 goto do_vma;
13254
13255 case DT_HASH:
13256 name = ".hash";
13257 goto get_vma;
13258 case DT_GNU_HASH:
13259 name = ".gnu.hash";
13260 goto get_vma;
13261 case DT_STRTAB:
13262 name = ".dynstr";
13263 goto get_vma;
13264 case DT_SYMTAB:
13265 name = ".dynsym";
13266 goto get_vma;
13267 case DT_VERDEF:
13268 name = ".gnu.version_d";
13269 goto get_vma;
13270 case DT_VERNEED:
13271 name = ".gnu.version_r";
13272 goto get_vma;
13273 case DT_VERSYM:
13274 name = ".gnu.version";
13275 get_vma:
13276 o = bfd_get_linker_section (dynobj, name);
13277 do_vma:
13278 if (o == NULL || bfd_is_abs_section (o->output_section))
13279 {
13280 _bfd_error_handler
13281 (_("could not find section %s"), name);
13282 goto error_return;
13283 }
13284 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
13285 {
13286 _bfd_error_handler
13287 (_("warning: section '%s' is being made into a note"), name);
13288 bfd_set_error (bfd_error_nonrepresentable_section);
13289 goto error_return;
13290 }
13291 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
13292 break;
13293
13294 case DT_REL:
13295 case DT_RELA:
13296 case DT_RELSZ:
13297 case DT_RELASZ:
13298 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
13299 type = SHT_REL;
13300 else
13301 type = SHT_RELA;
13302 sh_size = 0;
13303 sh_addr = 0;
13304 for (i = 1; i < elf_numsections (abfd); i++)
13305 {
13306 Elf_Internal_Shdr *hdr;
13307
13308 hdr = elf_elfsections (abfd)[i];
13309 if (hdr->sh_type == type
13310 && (hdr->sh_flags & SHF_ALLOC) != 0)
13311 {
13312 sh_size += hdr->sh_size;
13313 if (sh_addr == 0
13314 || sh_addr > hdr->sh_addr)
13315 sh_addr = hdr->sh_addr;
13316 }
13317 }
13318
13319 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
13320 {
13321 unsigned int opb = bfd_octets_per_byte (abfd, o);
13322
13323 /* Don't count procedure linkage table relocs in the
13324 overall reloc count. */
13325 sh_size -= htab->srelplt->size;
13326 if (sh_size == 0)
13327 /* If the size is zero, make the address zero too.
13328 This is to avoid a glibc bug. If the backend
13329 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
13330 zero, then we'll put DT_RELA at the end of
13331 DT_JMPREL. glibc will interpret the end of
13332 DT_RELA matching the end of DT_JMPREL as the
13333 case where DT_RELA includes DT_JMPREL, and for
13334 LD_BIND_NOW will decide that processing DT_RELA
13335 will process the PLT relocs too. Net result:
13336 No PLT relocs applied. */
13337 sh_addr = 0;
13338
13339 /* If .rela.plt is the first .rela section, exclude
13340 it from DT_RELA. */
13341 else if (sh_addr == (htab->srelplt->output_section->vma
13342 + htab->srelplt->output_offset) * opb)
13343 sh_addr += htab->srelplt->size;
13344 }
13345
13346 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
13347 dyn.d_un.d_val = sh_size;
13348 else
13349 dyn.d_un.d_ptr = sh_addr;
13350 break;
13351 }
13352 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13353 }
13354 }
13355
13356 /* If we have created any dynamic sections, then output them. */
13357 if (dynobj != NULL)
13358 {
13359 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
13360 goto error_return;
13361
13362 /* Check for DT_TEXTREL (late, in case the backend removes it). */
13363 if (bfd_link_textrel_check (info)
13364 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL
13365 && o->size != 0)
13366 {
13367 bfd_byte *dyncon, *dynconend;
13368
13369 dyncon = o->contents;
13370 dynconend = o->contents + o->size;
13371 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13372 {
13373 Elf_Internal_Dyn dyn;
13374
13375 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13376
13377 if (dyn.d_tag == DT_TEXTREL)
13378 {
13379 if (info->textrel_check == textrel_check_error)
13380 info->callbacks->einfo
13381 (_("%P%X: read-only segment has dynamic relocations\n"));
13382 else if (bfd_link_dll (info))
13383 info->callbacks->einfo
13384 (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
13385 else if (bfd_link_pde (info))
13386 info->callbacks->einfo
13387 (_("%P: warning: creating DT_TEXTREL in a PDE\n"));
13388 else
13389 info->callbacks->einfo
13390 (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
13391 break;
13392 }
13393 }
13394 }
13395
13396 for (o = dynobj->sections; o != NULL; o = o->next)
13397 {
13398 if ((o->flags & SEC_HAS_CONTENTS) == 0
13399 || o->size == 0
13400 || o->output_section == bfd_abs_section_ptr)
13401 continue;
13402 if ((o->flags & SEC_LINKER_CREATED) == 0)
13403 {
13404 /* At this point, we are only interested in sections
13405 created by _bfd_elf_link_create_dynamic_sections. */
13406 continue;
13407 }
13408 if (htab->stab_info.stabstr == o)
13409 continue;
13410 if (htab->eh_info.hdr_sec == o)
13411 continue;
13412 if (strcmp (o->name, ".dynstr") != 0)
13413 {
13414 bfd_size_type octets = ((file_ptr) o->output_offset
13415 * bfd_octets_per_byte (abfd, o));
13416 if (!bfd_set_section_contents (abfd, o->output_section,
13417 o->contents, octets, o->size))
13418 goto error_return;
13419 }
13420 else
13421 {
13422 /* The contents of the .dynstr section are actually in a
13423 stringtab. */
13424 file_ptr off;
13425
13426 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
13427 if (bfd_seek (abfd, off, SEEK_SET) != 0
13428 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
13429 goto error_return;
13430 }
13431 }
13432 }
13433
13434 if (!info->resolve_section_groups)
13435 {
13436 bool failed = false;
13437
13438 BFD_ASSERT (bfd_link_relocatable (info));
13439 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
13440 if (failed)
13441 goto error_return;
13442 }
13443
13444 /* If we have optimized stabs strings, output them. */
13445 if (htab->stab_info.stabstr != NULL)
13446 {
13447 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
13448 goto error_return;
13449 }
13450
13451 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
13452 goto error_return;
13453
13454 if (info->callbacks->emit_ctf)
13455 info->callbacks->emit_ctf ();
13456
13457 elf_final_link_free (abfd, &flinfo);
13458
13459 if (attr_section)
13460 {
13461 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
13462 if (contents == NULL)
13463 {
13464 /* Bail out and fail. */
13465 ret = false;
13466 goto return_local_hash_table;
13467 }
13468 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
13469 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
13470 free (contents);
13471 }
13472
13473 return_local_hash_table:
13474 if (info->unique_symbol)
13475 bfd_hash_table_free (&flinfo.local_hash_table);
13476 return ret;
13477
13478 error_return:
13479 elf_final_link_free (abfd, &flinfo);
13480 ret = false;
13481 goto return_local_hash_table;
13482 }
13483 \f
13484 /* Initialize COOKIE for input bfd ABFD. */
13485
13486 static bool
13487 init_reloc_cookie (struct elf_reloc_cookie *cookie,
13488 struct bfd_link_info *info, bfd *abfd)
13489 {
13490 Elf_Internal_Shdr *symtab_hdr;
13491 const struct elf_backend_data *bed;
13492
13493 bed = get_elf_backend_data (abfd);
13494 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13495
13496 cookie->abfd = abfd;
13497 cookie->sym_hashes = elf_sym_hashes (abfd);
13498 cookie->bad_symtab = elf_bad_symtab (abfd);
13499 if (cookie->bad_symtab)
13500 {
13501 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13502 cookie->extsymoff = 0;
13503 }
13504 else
13505 {
13506 cookie->locsymcount = symtab_hdr->sh_info;
13507 cookie->extsymoff = symtab_hdr->sh_info;
13508 }
13509
13510 if (bed->s->arch_size == 32)
13511 cookie->r_sym_shift = 8;
13512 else
13513 cookie->r_sym_shift = 32;
13514
13515 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
13516 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
13517 {
13518 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
13519 cookie->locsymcount, 0,
13520 NULL, NULL, NULL);
13521 if (cookie->locsyms == NULL)
13522 {
13523 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
13524 return false;
13525 }
13526 if (_bfd_link_keep_memory (info) )
13527 {
13528 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
13529 info->cache_size += (cookie->locsymcount
13530 * sizeof (Elf_External_Sym_Shndx));
13531 }
13532 }
13533 return true;
13534 }
13535
13536 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
13537
13538 static void
13539 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
13540 {
13541 Elf_Internal_Shdr *symtab_hdr;
13542
13543 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13544 if (symtab_hdr->contents != (unsigned char *) cookie->locsyms)
13545 free (cookie->locsyms);
13546 }
13547
13548 /* Initialize the relocation information in COOKIE for input section SEC
13549 of input bfd ABFD. */
13550
13551 static bool
13552 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13553 struct bfd_link_info *info, bfd *abfd,
13554 asection *sec)
13555 {
13556 if (sec->reloc_count == 0)
13557 {
13558 cookie->rels = NULL;
13559 cookie->relend = NULL;
13560 }
13561 else
13562 {
13563 cookie->rels = _bfd_elf_link_info_read_relocs (abfd, info, sec,
13564 NULL, NULL,
13565 _bfd_link_keep_memory (info));
13566 if (cookie->rels == NULL)
13567 return false;
13568 cookie->rel = cookie->rels;
13569 cookie->relend = cookie->rels + sec->reloc_count;
13570 }
13571 cookie->rel = cookie->rels;
13572 return true;
13573 }
13574
13575 /* Free the memory allocated by init_reloc_cookie_rels,
13576 if appropriate. */
13577
13578 static void
13579 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13580 asection *sec)
13581 {
13582 if (elf_section_data (sec)->relocs != cookie->rels)
13583 free (cookie->rels);
13584 }
13585
13586 /* Initialize the whole of COOKIE for input section SEC. */
13587
13588 static bool
13589 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13590 struct bfd_link_info *info,
13591 asection *sec)
13592 {
13593 if (!init_reloc_cookie (cookie, info, sec->owner))
13594 goto error1;
13595 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
13596 goto error2;
13597 return true;
13598
13599 error2:
13600 fini_reloc_cookie (cookie, sec->owner);
13601 error1:
13602 return false;
13603 }
13604
13605 /* Free the memory allocated by init_reloc_cookie_for_section,
13606 if appropriate. */
13607
13608 static void
13609 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13610 asection *sec)
13611 {
13612 fini_reloc_cookie_rels (cookie, sec);
13613 fini_reloc_cookie (cookie, sec->owner);
13614 }
13615 \f
13616 /* Garbage collect unused sections. */
13617
13618 /* Default gc_mark_hook. */
13619
13620 asection *
13621 _bfd_elf_gc_mark_hook (asection *sec,
13622 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13623 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13624 struct elf_link_hash_entry *h,
13625 Elf_Internal_Sym *sym)
13626 {
13627 if (h != NULL)
13628 {
13629 switch (h->root.type)
13630 {
13631 case bfd_link_hash_defined:
13632 case bfd_link_hash_defweak:
13633 return h->root.u.def.section;
13634
13635 case bfd_link_hash_common:
13636 return h->root.u.c.p->section;
13637
13638 default:
13639 break;
13640 }
13641 }
13642 else
13643 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
13644
13645 return NULL;
13646 }
13647
13648 /* Return the debug definition section. */
13649
13650 static asection *
13651 elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
13652 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13653 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13654 struct elf_link_hash_entry *h,
13655 Elf_Internal_Sym *sym)
13656 {
13657 if (h != NULL)
13658 {
13659 /* Return the global debug definition section. */
13660 if ((h->root.type == bfd_link_hash_defined
13661 || h->root.type == bfd_link_hash_defweak)
13662 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
13663 return h->root.u.def.section;
13664 }
13665 else
13666 {
13667 /* Return the local debug definition section. */
13668 asection *isec = bfd_section_from_elf_index (sec->owner,
13669 sym->st_shndx);
13670 if ((isec->flags & SEC_DEBUGGING) != 0)
13671 return isec;
13672 }
13673
13674 return NULL;
13675 }
13676
13677 /* COOKIE->rel describes a relocation against section SEC, which is
13678 a section we've decided to keep. Return the section that contains
13679 the relocation symbol, or NULL if no section contains it. */
13680
13681 asection *
13682 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
13683 elf_gc_mark_hook_fn gc_mark_hook,
13684 struct elf_reloc_cookie *cookie,
13685 bool *start_stop)
13686 {
13687 unsigned long r_symndx;
13688 struct elf_link_hash_entry *h, *hw;
13689
13690 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
13691 if (r_symndx == STN_UNDEF)
13692 return NULL;
13693
13694 if (r_symndx >= cookie->locsymcount
13695 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13696 {
13697 bool was_marked;
13698
13699 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
13700 if (h == NULL)
13701 {
13702 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
13703 sec->owner);
13704 return NULL;
13705 }
13706 while (h->root.type == bfd_link_hash_indirect
13707 || h->root.type == bfd_link_hash_warning)
13708 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13709
13710 was_marked = h->mark;
13711 h->mark = 1;
13712 /* Keep all aliases of the symbol too. If an object symbol
13713 needs to be copied into .dynbss then all of its aliases
13714 should be present as dynamic symbols, not just the one used
13715 on the copy relocation. */
13716 hw = h;
13717 while (hw->is_weakalias)
13718 {
13719 hw = hw->u.alias;
13720 hw->mark = 1;
13721 }
13722
13723 if (!was_marked && h->start_stop && !h->root.ldscript_def)
13724 {
13725 if (info->start_stop_gc)
13726 return NULL;
13727
13728 /* To work around a glibc bug, mark XXX input sections
13729 when there is a reference to __start_XXX or __stop_XXX
13730 symbols. */
13731 else if (start_stop != NULL)
13732 {
13733 asection *s = h->u2.start_stop_section;
13734 *start_stop = true;
13735 return s;
13736 }
13737 }
13738
13739 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
13740 }
13741
13742 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
13743 &cookie->locsyms[r_symndx]);
13744 }
13745
13746 /* COOKIE->rel describes a relocation against section SEC, which is
13747 a section we've decided to keep. Mark the section that contains
13748 the relocation symbol. */
13749
13750 bool
13751 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
13752 asection *sec,
13753 elf_gc_mark_hook_fn gc_mark_hook,
13754 struct elf_reloc_cookie *cookie)
13755 {
13756 asection *rsec;
13757 bool start_stop = false;
13758
13759 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
13760 while (rsec != NULL)
13761 {
13762 if (!rsec->gc_mark)
13763 {
13764 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
13765 || (rsec->owner->flags & DYNAMIC) != 0)
13766 rsec->gc_mark = 1;
13767 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
13768 return false;
13769 }
13770 if (!start_stop)
13771 break;
13772 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
13773 }
13774 return true;
13775 }
13776
13777 /* The mark phase of garbage collection. For a given section, mark
13778 it and any sections in this section's group, and all the sections
13779 which define symbols to which it refers. */
13780
13781 bool
13782 _bfd_elf_gc_mark (struct bfd_link_info *info,
13783 asection *sec,
13784 elf_gc_mark_hook_fn gc_mark_hook)
13785 {
13786 bool ret;
13787 asection *group_sec, *eh_frame;
13788
13789 sec->gc_mark = 1;
13790
13791 /* Mark all the sections in the group. */
13792 group_sec = elf_section_data (sec)->next_in_group;
13793 if (group_sec && !group_sec->gc_mark)
13794 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
13795 return false;
13796
13797 /* Look through the section relocs. */
13798 ret = true;
13799 eh_frame = elf_eh_frame_section (sec->owner);
13800 if ((sec->flags & SEC_RELOC) != 0
13801 && sec->reloc_count > 0
13802 && sec != eh_frame)
13803 {
13804 struct elf_reloc_cookie cookie;
13805
13806 if (!init_reloc_cookie_for_section (&cookie, info, sec))
13807 ret = false;
13808 else
13809 {
13810 for (; cookie.rel < cookie.relend; cookie.rel++)
13811 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
13812 {
13813 ret = false;
13814 break;
13815 }
13816 fini_reloc_cookie_for_section (&cookie, sec);
13817 }
13818 }
13819
13820 if (ret && eh_frame && elf_fde_list (sec))
13821 {
13822 struct elf_reloc_cookie cookie;
13823
13824 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
13825 ret = false;
13826 else
13827 {
13828 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
13829 gc_mark_hook, &cookie))
13830 ret = false;
13831 fini_reloc_cookie_for_section (&cookie, eh_frame);
13832 }
13833 }
13834
13835 eh_frame = elf_section_eh_frame_entry (sec);
13836 if (ret && eh_frame && !eh_frame->gc_mark)
13837 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
13838 ret = false;
13839
13840 return ret;
13841 }
13842
13843 /* Scan and mark sections in a special or debug section group. */
13844
13845 static void
13846 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
13847 {
13848 /* Point to first section of section group. */
13849 asection *ssec;
13850 /* Used to iterate the section group. */
13851 asection *msec;
13852
13853 bool is_special_grp = true;
13854 bool is_debug_grp = true;
13855
13856 /* First scan to see if group contains any section other than debug
13857 and special section. */
13858 ssec = msec = elf_next_in_group (grp);
13859 do
13860 {
13861 if ((msec->flags & SEC_DEBUGGING) == 0)
13862 is_debug_grp = false;
13863
13864 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
13865 is_special_grp = false;
13866
13867 msec = elf_next_in_group (msec);
13868 }
13869 while (msec != ssec);
13870
13871 /* If this is a pure debug section group or pure special section group,
13872 keep all sections in this group. */
13873 if (is_debug_grp || is_special_grp)
13874 {
13875 do
13876 {
13877 msec->gc_mark = 1;
13878 msec = elf_next_in_group (msec);
13879 }
13880 while (msec != ssec);
13881 }
13882 }
13883
13884 /* Keep debug and special sections. */
13885
13886 bool
13887 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
13888 elf_gc_mark_hook_fn mark_hook)
13889 {
13890 bfd *ibfd;
13891
13892 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13893 {
13894 asection *isec;
13895 bool some_kept;
13896 bool debug_frag_seen;
13897 bool has_kept_debug_info;
13898
13899 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13900 continue;
13901 isec = ibfd->sections;
13902 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13903 continue;
13904
13905 /* Ensure all linker created sections are kept,
13906 see if any other section is already marked,
13907 and note if we have any fragmented debug sections. */
13908 debug_frag_seen = some_kept = has_kept_debug_info = false;
13909 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13910 {
13911 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13912 isec->gc_mark = 1;
13913 else if (isec->gc_mark
13914 && (isec->flags & SEC_ALLOC) != 0
13915 && elf_section_type (isec) != SHT_NOTE)
13916 some_kept = true;
13917 else
13918 {
13919 /* Since all sections, except for backend specific ones,
13920 have been garbage collected, call mark_hook on this
13921 section if any of its linked-to sections is marked. */
13922 asection *linked_to_sec;
13923 for (linked_to_sec = elf_linked_to_section (isec);
13924 linked_to_sec != NULL && !linked_to_sec->linker_mark;
13925 linked_to_sec = elf_linked_to_section (linked_to_sec))
13926 {
13927 if (linked_to_sec->gc_mark)
13928 {
13929 if (!_bfd_elf_gc_mark (info, isec, mark_hook))
13930 return false;
13931 break;
13932 }
13933 linked_to_sec->linker_mark = 1;
13934 }
13935 for (linked_to_sec = elf_linked_to_section (isec);
13936 linked_to_sec != NULL && linked_to_sec->linker_mark;
13937 linked_to_sec = elf_linked_to_section (linked_to_sec))
13938 linked_to_sec->linker_mark = 0;
13939 }
13940
13941 if (!debug_frag_seen
13942 && (isec->flags & SEC_DEBUGGING)
13943 && startswith (isec->name, ".debug_line."))
13944 debug_frag_seen = true;
13945 else if (strcmp (bfd_section_name (isec),
13946 "__patchable_function_entries") == 0
13947 && elf_linked_to_section (isec) == NULL)
13948 info->callbacks->einfo (_("%F%P: %pB(%pA): error: "
13949 "need linked-to section "
13950 "for --gc-sections\n"),
13951 isec->owner, isec);
13952 }
13953
13954 /* If no non-note alloc section in this file will be kept, then
13955 we can toss out the debug and special sections. */
13956 if (!some_kept)
13957 continue;
13958
13959 /* Keep debug and special sections like .comment when they are
13960 not part of a group. Also keep section groups that contain
13961 just debug sections or special sections. NB: Sections with
13962 linked-to section has been handled above. */
13963 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13964 {
13965 if ((isec->flags & SEC_GROUP) != 0)
13966 _bfd_elf_gc_mark_debug_special_section_group (isec);
13967 else if (((isec->flags & SEC_DEBUGGING) != 0
13968 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
13969 && elf_next_in_group (isec) == NULL
13970 && elf_linked_to_section (isec) == NULL)
13971 isec->gc_mark = 1;
13972 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
13973 has_kept_debug_info = true;
13974 }
13975
13976 /* Look for CODE sections which are going to be discarded,
13977 and find and discard any fragmented debug sections which
13978 are associated with that code section. */
13979 if (debug_frag_seen)
13980 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13981 if ((isec->flags & SEC_CODE) != 0
13982 && isec->gc_mark == 0)
13983 {
13984 unsigned int ilen;
13985 asection *dsec;
13986
13987 ilen = strlen (isec->name);
13988
13989 /* Association is determined by the name of the debug
13990 section containing the name of the code section as
13991 a suffix. For example .debug_line.text.foo is a
13992 debug section associated with .text.foo. */
13993 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
13994 {
13995 unsigned int dlen;
13996
13997 if (dsec->gc_mark == 0
13998 || (dsec->flags & SEC_DEBUGGING) == 0)
13999 continue;
14000
14001 dlen = strlen (dsec->name);
14002
14003 if (dlen > ilen
14004 && strncmp (dsec->name + (dlen - ilen),
14005 isec->name, ilen) == 0)
14006 dsec->gc_mark = 0;
14007 }
14008 }
14009
14010 /* Mark debug sections referenced by kept debug sections. */
14011 if (has_kept_debug_info)
14012 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14013 if (isec->gc_mark
14014 && (isec->flags & SEC_DEBUGGING) != 0)
14015 if (!_bfd_elf_gc_mark (info, isec,
14016 elf_gc_mark_debug_section))
14017 return false;
14018 }
14019 return true;
14020 }
14021
14022 static bool
14023 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
14024 {
14025 bfd *sub;
14026 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14027
14028 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
14029 {
14030 asection *o;
14031
14032 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
14033 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
14034 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
14035 continue;
14036 o = sub->sections;
14037 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14038 continue;
14039
14040 for (o = sub->sections; o != NULL; o = o->next)
14041 {
14042 /* When any section in a section group is kept, we keep all
14043 sections in the section group. If the first member of
14044 the section group is excluded, we will also exclude the
14045 group section. */
14046 if (o->flags & SEC_GROUP)
14047 {
14048 asection *first = elf_next_in_group (o);
14049 o->gc_mark = first->gc_mark;
14050 }
14051
14052 if (o->gc_mark)
14053 continue;
14054
14055 /* Skip sweeping sections already excluded. */
14056 if (o->flags & SEC_EXCLUDE)
14057 continue;
14058
14059 /* Since this is early in the link process, it is simple
14060 to remove a section from the output. */
14061 o->flags |= SEC_EXCLUDE;
14062
14063 if (info->print_gc_sections && o->size != 0)
14064 /* xgettext:c-format */
14065 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
14066 o, sub);
14067 }
14068 }
14069
14070 return true;
14071 }
14072
14073 /* Propagate collected vtable information. This is called through
14074 elf_link_hash_traverse. */
14075
14076 static bool
14077 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
14078 {
14079 /* Those that are not vtables. */
14080 if (h->start_stop
14081 || h->u2.vtable == NULL
14082 || h->u2.vtable->parent == NULL)
14083 return true;
14084
14085 /* Those vtables that do not have parents, we cannot merge. */
14086 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
14087 return true;
14088
14089 /* If we've already been done, exit. */
14090 if (h->u2.vtable->used && h->u2.vtable->used[-1])
14091 return true;
14092
14093 /* Make sure the parent's table is up to date. */
14094 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
14095
14096 if (h->u2.vtable->used == NULL)
14097 {
14098 /* None of this table's entries were referenced. Re-use the
14099 parent's table. */
14100 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
14101 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
14102 }
14103 else
14104 {
14105 size_t n;
14106 bool *cu, *pu;
14107
14108 /* Or the parent's entries into ours. */
14109 cu = h->u2.vtable->used;
14110 cu[-1] = true;
14111 pu = h->u2.vtable->parent->u2.vtable->used;
14112 if (pu != NULL)
14113 {
14114 const struct elf_backend_data *bed;
14115 unsigned int log_file_align;
14116
14117 bed = get_elf_backend_data (h->root.u.def.section->owner);
14118 log_file_align = bed->s->log_file_align;
14119 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
14120 while (n--)
14121 {
14122 if (*pu)
14123 *cu = true;
14124 pu++;
14125 cu++;
14126 }
14127 }
14128 }
14129
14130 return true;
14131 }
14132
14133 struct link_info_ok
14134 {
14135 struct bfd_link_info *info;
14136 bool ok;
14137 };
14138
14139 static bool
14140 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h,
14141 void *ptr)
14142 {
14143 asection *sec;
14144 bfd_vma hstart, hend;
14145 Elf_Internal_Rela *relstart, *relend, *rel;
14146 const struct elf_backend_data *bed;
14147 unsigned int log_file_align;
14148 struct link_info_ok *info = (struct link_info_ok *) ptr;
14149
14150 /* Take care of both those symbols that do not describe vtables as
14151 well as those that are not loaded. */
14152 if (h->start_stop
14153 || h->u2.vtable == NULL
14154 || h->u2.vtable->parent == NULL)
14155 return true;
14156
14157 BFD_ASSERT (h->root.type == bfd_link_hash_defined
14158 || h->root.type == bfd_link_hash_defweak);
14159
14160 sec = h->root.u.def.section;
14161 hstart = h->root.u.def.value;
14162 hend = hstart + h->size;
14163
14164 relstart = _bfd_elf_link_info_read_relocs (sec->owner, info->info,
14165 sec, NULL, NULL, true);
14166 if (!relstart)
14167 return info->ok = false;
14168 bed = get_elf_backend_data (sec->owner);
14169 log_file_align = bed->s->log_file_align;
14170
14171 relend = relstart + sec->reloc_count;
14172
14173 for (rel = relstart; rel < relend; ++rel)
14174 if (rel->r_offset >= hstart && rel->r_offset < hend)
14175 {
14176 /* If the entry is in use, do nothing. */
14177 if (h->u2.vtable->used
14178 && (rel->r_offset - hstart) < h->u2.vtable->size)
14179 {
14180 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
14181 if (h->u2.vtable->used[entry])
14182 continue;
14183 }
14184 /* Otherwise, kill it. */
14185 rel->r_offset = rel->r_info = rel->r_addend = 0;
14186 }
14187
14188 return true;
14189 }
14190
14191 /* Mark sections containing dynamically referenced symbols. When
14192 building shared libraries, we must assume that any visible symbol is
14193 referenced. */
14194
14195 bool
14196 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
14197 {
14198 struct bfd_link_info *info = (struct bfd_link_info *) inf;
14199 struct bfd_elf_dynamic_list *d = info->dynamic_list;
14200
14201 if ((h->root.type == bfd_link_hash_defined
14202 || h->root.type == bfd_link_hash_defweak)
14203 && (!h->start_stop
14204 || h->root.ldscript_def
14205 || !info->start_stop_gc)
14206 && ((h->ref_dynamic && !h->forced_local)
14207 || ((h->def_regular || ELF_COMMON_DEF_P (h))
14208 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
14209 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
14210 && (!bfd_link_executable (info)
14211 || info->gc_keep_exported
14212 || info->export_dynamic
14213 || (h->dynamic
14214 && d != NULL
14215 && (*d->match) (&d->head, NULL, h->root.root.string)))
14216 && (h->versioned >= versioned
14217 || !bfd_hide_sym_by_version (info->version_info,
14218 h->root.root.string)))))
14219 h->root.u.def.section->flags |= SEC_KEEP;
14220
14221 return true;
14222 }
14223
14224 /* Keep all sections containing symbols undefined on the command-line,
14225 and the section containing the entry symbol. */
14226
14227 void
14228 _bfd_elf_gc_keep (struct bfd_link_info *info)
14229 {
14230 struct bfd_sym_chain *sym;
14231
14232 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
14233 {
14234 struct elf_link_hash_entry *h;
14235
14236 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
14237 false, false, false);
14238
14239 if (h != NULL
14240 && (h->root.type == bfd_link_hash_defined
14241 || h->root.type == bfd_link_hash_defweak)
14242 && !bfd_is_const_section (h->root.u.def.section))
14243 h->root.u.def.section->flags |= SEC_KEEP;
14244 }
14245 }
14246
14247 bool
14248 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
14249 struct bfd_link_info *info)
14250 {
14251 bfd *ibfd = info->input_bfds;
14252
14253 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14254 {
14255 asection *sec;
14256 struct elf_reloc_cookie cookie;
14257
14258 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
14259 continue;
14260 sec = ibfd->sections;
14261 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14262 continue;
14263
14264 if (!init_reloc_cookie (&cookie, info, ibfd))
14265 return false;
14266
14267 for (sec = ibfd->sections; sec; sec = sec->next)
14268 {
14269 if (startswith (bfd_section_name (sec), ".eh_frame_entry")
14270 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
14271 {
14272 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
14273 fini_reloc_cookie_rels (&cookie, sec);
14274 }
14275 }
14276 }
14277 return true;
14278 }
14279
14280 /* Do mark and sweep of unused sections. */
14281
14282 bool
14283 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
14284 {
14285 bool ok = true;
14286 bfd *sub;
14287 elf_gc_mark_hook_fn gc_mark_hook;
14288 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14289 struct elf_link_hash_table *htab;
14290 struct link_info_ok info_ok;
14291
14292 if (!bed->can_gc_sections
14293 || !is_elf_hash_table (info->hash))
14294 {
14295 _bfd_error_handler(_("warning: gc-sections option ignored"));
14296 return true;
14297 }
14298
14299 bed->gc_keep (info);
14300 htab = elf_hash_table (info);
14301
14302 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
14303 at the .eh_frame section if we can mark the FDEs individually. */
14304 for (sub = info->input_bfds;
14305 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
14306 sub = sub->link.next)
14307 {
14308 asection *sec;
14309 struct elf_reloc_cookie cookie;
14310
14311 sec = sub->sections;
14312 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14313 continue;
14314 sec = bfd_get_section_by_name (sub, ".eh_frame");
14315 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
14316 {
14317 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
14318 if (elf_section_data (sec)->sec_info
14319 && (sec->flags & SEC_LINKER_CREATED) == 0)
14320 elf_eh_frame_section (sub) = sec;
14321 fini_reloc_cookie_for_section (&cookie, sec);
14322 sec = bfd_get_next_section_by_name (NULL, sec);
14323 }
14324 }
14325
14326 /* Apply transitive closure to the vtable entry usage info. */
14327 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
14328 if (!ok)
14329 return false;
14330
14331 /* Kill the vtable relocations that were not used. */
14332 info_ok.info = info;
14333 info_ok.ok = true;
14334 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &info_ok);
14335 if (!info_ok.ok)
14336 return false;
14337
14338 /* Mark dynamically referenced symbols. */
14339 if (htab->dynamic_sections_created || info->gc_keep_exported)
14340 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
14341
14342 /* Grovel through relocs to find out who stays ... */
14343 gc_mark_hook = bed->gc_mark_hook;
14344 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
14345 {
14346 asection *o;
14347
14348 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
14349 || elf_object_id (sub) != elf_hash_table_id (htab)
14350 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
14351 continue;
14352
14353 o = sub->sections;
14354 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14355 continue;
14356
14357 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
14358 Also treat note sections as a root, if the section is not part
14359 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
14360 well as FINI_ARRAY sections for ld -r. */
14361 for (o = sub->sections; o != NULL; o = o->next)
14362 if (!o->gc_mark
14363 && (o->flags & SEC_EXCLUDE) == 0
14364 && ((o->flags & SEC_KEEP) != 0
14365 || (bfd_link_relocatable (info)
14366 && ((elf_section_data (o)->this_hdr.sh_type
14367 == SHT_PREINIT_ARRAY)
14368 || (elf_section_data (o)->this_hdr.sh_type
14369 == SHT_INIT_ARRAY)
14370 || (elf_section_data (o)->this_hdr.sh_type
14371 == SHT_FINI_ARRAY)))
14372 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
14373 && elf_next_in_group (o) == NULL
14374 && elf_linked_to_section (o) == NULL)
14375 || ((elf_tdata (sub)->has_gnu_osabi & elf_gnu_osabi_retain)
14376 && (elf_section_flags (o) & SHF_GNU_RETAIN))))
14377 {
14378 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
14379 return false;
14380 }
14381 }
14382
14383 /* Allow the backend to mark additional target specific sections. */
14384 bed->gc_mark_extra_sections (info, gc_mark_hook);
14385
14386 /* ... and mark SEC_EXCLUDE for those that go. */
14387 return elf_gc_sweep (abfd, info);
14388 }
14389 \f
14390 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
14391
14392 bool
14393 bfd_elf_gc_record_vtinherit (bfd *abfd,
14394 asection *sec,
14395 struct elf_link_hash_entry *h,
14396 bfd_vma offset)
14397 {
14398 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
14399 struct elf_link_hash_entry **search, *child;
14400 size_t extsymcount;
14401 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14402
14403 /* The sh_info field of the symtab header tells us where the
14404 external symbols start. We don't care about the local symbols at
14405 this point. */
14406 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
14407 if (!elf_bad_symtab (abfd))
14408 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
14409
14410 sym_hashes = elf_sym_hashes (abfd);
14411 sym_hashes_end = PTR_ADD (sym_hashes, extsymcount);
14412
14413 /* Hunt down the child symbol, which is in this section at the same
14414 offset as the relocation. */
14415 for (search = sym_hashes; search != sym_hashes_end; ++search)
14416 {
14417 if ((child = *search) != NULL
14418 && (child->root.type == bfd_link_hash_defined
14419 || child->root.type == bfd_link_hash_defweak)
14420 && child->root.u.def.section == sec
14421 && child->root.u.def.value == offset)
14422 goto win;
14423 }
14424
14425 /* xgettext:c-format */
14426 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
14427 abfd, sec, (uint64_t) offset);
14428 bfd_set_error (bfd_error_invalid_operation);
14429 return false;
14430
14431 win:
14432 if (!child->u2.vtable)
14433 {
14434 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
14435 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
14436 if (!child->u2.vtable)
14437 return false;
14438 }
14439 if (!h)
14440 {
14441 /* This *should* only be the absolute section. It could potentially
14442 be that someone has defined a non-global vtable though, which
14443 would be bad. It isn't worth paging in the local symbols to be
14444 sure though; that case should simply be handled by the assembler. */
14445
14446 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
14447 }
14448 else
14449 child->u2.vtable->parent = h;
14450
14451 return true;
14452 }
14453
14454 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
14455
14456 bool
14457 bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec,
14458 struct elf_link_hash_entry *h,
14459 bfd_vma addend)
14460 {
14461 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14462 unsigned int log_file_align = bed->s->log_file_align;
14463
14464 if (!h)
14465 {
14466 /* xgettext:c-format */
14467 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
14468 abfd, sec);
14469 bfd_set_error (bfd_error_bad_value);
14470 return false;
14471 }
14472
14473 if (!h->u2.vtable)
14474 {
14475 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
14476 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
14477 if (!h->u2.vtable)
14478 return false;
14479 }
14480
14481 if (addend >= h->u2.vtable->size)
14482 {
14483 size_t size, bytes, file_align;
14484 bool *ptr = h->u2.vtable->used;
14485
14486 /* While the symbol is undefined, we have to be prepared to handle
14487 a zero size. */
14488 file_align = 1 << log_file_align;
14489 if (h->root.type == bfd_link_hash_undefined)
14490 size = addend + file_align;
14491 else
14492 {
14493 size = h->size;
14494 if (addend >= size)
14495 {
14496 /* Oops! We've got a reference past the defined end of
14497 the table. This is probably a bug -- shall we warn? */
14498 size = addend + file_align;
14499 }
14500 }
14501 size = (size + file_align - 1) & -file_align;
14502
14503 /* Allocate one extra entry for use as a "done" flag for the
14504 consolidation pass. */
14505 bytes = ((size >> log_file_align) + 1) * sizeof (bool);
14506
14507 if (ptr)
14508 {
14509 ptr = (bool *) bfd_realloc (ptr - 1, bytes);
14510
14511 if (ptr != NULL)
14512 {
14513 size_t oldbytes;
14514
14515 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
14516 * sizeof (bool));
14517 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
14518 }
14519 }
14520 else
14521 ptr = (bool *) bfd_zmalloc (bytes);
14522
14523 if (ptr == NULL)
14524 return false;
14525
14526 /* And arrange for that done flag to be at index -1. */
14527 h->u2.vtable->used = ptr + 1;
14528 h->u2.vtable->size = size;
14529 }
14530
14531 h->u2.vtable->used[addend >> log_file_align] = true;
14532
14533 return true;
14534 }
14535
14536 /* Map an ELF section header flag to its corresponding string. */
14537 typedef struct
14538 {
14539 char *flag_name;
14540 flagword flag_value;
14541 } elf_flags_to_name_table;
14542
14543 static const elf_flags_to_name_table elf_flags_to_names [] =
14544 {
14545 { "SHF_WRITE", SHF_WRITE },
14546 { "SHF_ALLOC", SHF_ALLOC },
14547 { "SHF_EXECINSTR", SHF_EXECINSTR },
14548 { "SHF_MERGE", SHF_MERGE },
14549 { "SHF_STRINGS", SHF_STRINGS },
14550 { "SHF_INFO_LINK", SHF_INFO_LINK},
14551 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
14552 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
14553 { "SHF_GROUP", SHF_GROUP },
14554 { "SHF_TLS", SHF_TLS },
14555 { "SHF_MASKOS", SHF_MASKOS },
14556 { "SHF_EXCLUDE", SHF_EXCLUDE },
14557 };
14558
14559 /* Returns TRUE if the section is to be included, otherwise FALSE. */
14560 bool
14561 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
14562 struct flag_info *flaginfo,
14563 asection *section)
14564 {
14565 const bfd_vma sh_flags = elf_section_flags (section);
14566
14567 if (!flaginfo->flags_initialized)
14568 {
14569 bfd *obfd = info->output_bfd;
14570 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14571 struct flag_info_list *tf = flaginfo->flag_list;
14572 int with_hex = 0;
14573 int without_hex = 0;
14574
14575 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
14576 {
14577 unsigned i;
14578 flagword (*lookup) (char *);
14579
14580 lookup = bed->elf_backend_lookup_section_flags_hook;
14581 if (lookup != NULL)
14582 {
14583 flagword hexval = (*lookup) ((char *) tf->name);
14584
14585 if (hexval != 0)
14586 {
14587 if (tf->with == with_flags)
14588 with_hex |= hexval;
14589 else if (tf->with == without_flags)
14590 without_hex |= hexval;
14591 tf->valid = true;
14592 continue;
14593 }
14594 }
14595 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
14596 {
14597 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
14598 {
14599 if (tf->with == with_flags)
14600 with_hex |= elf_flags_to_names[i].flag_value;
14601 else if (tf->with == without_flags)
14602 without_hex |= elf_flags_to_names[i].flag_value;
14603 tf->valid = true;
14604 break;
14605 }
14606 }
14607 if (!tf->valid)
14608 {
14609 info->callbacks->einfo
14610 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
14611 return false;
14612 }
14613 }
14614 flaginfo->flags_initialized = true;
14615 flaginfo->only_with_flags |= with_hex;
14616 flaginfo->not_with_flags |= without_hex;
14617 }
14618
14619 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
14620 return false;
14621
14622 if ((flaginfo->not_with_flags & sh_flags) != 0)
14623 return false;
14624
14625 return true;
14626 }
14627
14628 struct alloc_got_off_arg {
14629 bfd_vma gotoff;
14630 struct bfd_link_info *info;
14631 };
14632
14633 /* We need a special top-level link routine to convert got reference counts
14634 to real got offsets. */
14635
14636 static bool
14637 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
14638 {
14639 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
14640 bfd *obfd = gofarg->info->output_bfd;
14641 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14642
14643 if (h->got.refcount > 0)
14644 {
14645 h->got.offset = gofarg->gotoff;
14646 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
14647 }
14648 else
14649 h->got.offset = (bfd_vma) -1;
14650
14651 return true;
14652 }
14653
14654 /* And an accompanying bit to work out final got entry offsets once
14655 we're done. Should be called from final_link. */
14656
14657 bool
14658 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
14659 struct bfd_link_info *info)
14660 {
14661 bfd *i;
14662 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14663 bfd_vma gotoff;
14664 struct alloc_got_off_arg gofarg;
14665
14666 BFD_ASSERT (abfd == info->output_bfd);
14667
14668 if (! is_elf_hash_table (info->hash))
14669 return false;
14670
14671 /* The GOT offset is relative to the .got section, but the GOT header is
14672 put into the .got.plt section, if the backend uses it. */
14673 if (bed->want_got_plt)
14674 gotoff = 0;
14675 else
14676 gotoff = bed->got_header_size;
14677
14678 /* Do the local .got entries first. */
14679 for (i = info->input_bfds; i; i = i->link.next)
14680 {
14681 bfd_signed_vma *local_got;
14682 size_t j, locsymcount;
14683 Elf_Internal_Shdr *symtab_hdr;
14684
14685 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
14686 continue;
14687
14688 local_got = elf_local_got_refcounts (i);
14689 if (!local_got)
14690 continue;
14691
14692 symtab_hdr = &elf_tdata (i)->symtab_hdr;
14693 if (elf_bad_symtab (i))
14694 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
14695 else
14696 locsymcount = symtab_hdr->sh_info;
14697
14698 for (j = 0; j < locsymcount; ++j)
14699 {
14700 if (local_got[j] > 0)
14701 {
14702 local_got[j] = gotoff;
14703 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
14704 }
14705 else
14706 local_got[j] = (bfd_vma) -1;
14707 }
14708 }
14709
14710 /* Then the global .got entries. .plt refcounts are handled by
14711 adjust_dynamic_symbol */
14712 gofarg.gotoff = gotoff;
14713 gofarg.info = info;
14714 elf_link_hash_traverse (elf_hash_table (info),
14715 elf_gc_allocate_got_offsets,
14716 &gofarg);
14717 return true;
14718 }
14719
14720 /* Many folk need no more in the way of final link than this, once
14721 got entry reference counting is enabled. */
14722
14723 bool
14724 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
14725 {
14726 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
14727 return false;
14728
14729 /* Invoke the regular ELF backend linker to do all the work. */
14730 return bfd_elf_final_link (abfd, info);
14731 }
14732
14733 bool
14734 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
14735 {
14736 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
14737
14738 if (rcookie->bad_symtab)
14739 rcookie->rel = rcookie->rels;
14740
14741 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
14742 {
14743 unsigned long r_symndx;
14744
14745 if (! rcookie->bad_symtab)
14746 if (rcookie->rel->r_offset > offset)
14747 return false;
14748 if (rcookie->rel->r_offset != offset)
14749 continue;
14750
14751 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
14752 if (r_symndx == STN_UNDEF)
14753 return true;
14754
14755 if (r_symndx >= rcookie->locsymcount
14756 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
14757 {
14758 struct elf_link_hash_entry *h;
14759
14760 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
14761
14762 while (h->root.type == bfd_link_hash_indirect
14763 || h->root.type == bfd_link_hash_warning)
14764 h = (struct elf_link_hash_entry *) h->root.u.i.link;
14765
14766 if ((h->root.type == bfd_link_hash_defined
14767 || h->root.type == bfd_link_hash_defweak)
14768 && (h->root.u.def.section->owner != rcookie->abfd
14769 || h->root.u.def.section->kept_section != NULL
14770 || discarded_section (h->root.u.def.section)))
14771 return true;
14772 }
14773 else
14774 {
14775 /* It's not a relocation against a global symbol,
14776 but it could be a relocation against a local
14777 symbol for a discarded section. */
14778 asection *isec;
14779 Elf_Internal_Sym *isym;
14780
14781 /* Need to: get the symbol; get the section. */
14782 isym = &rcookie->locsyms[r_symndx];
14783 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
14784 if (isec != NULL
14785 && (isec->kept_section != NULL
14786 || discarded_section (isec)))
14787 return true;
14788 }
14789 return false;
14790 }
14791 return false;
14792 }
14793
14794 /* Discard unneeded references to discarded sections.
14795 Returns -1 on error, 1 if any section's size was changed, 0 if
14796 nothing changed. This function assumes that the relocations are in
14797 sorted order, which is true for all known assemblers. */
14798
14799 int
14800 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
14801 {
14802 struct elf_reloc_cookie cookie;
14803 asection *o;
14804 bfd *abfd;
14805 int changed = 0;
14806
14807 if (info->traditional_format
14808 || !is_elf_hash_table (info->hash))
14809 return 0;
14810
14811 o = bfd_get_section_by_name (output_bfd, ".stab");
14812 if (o != NULL)
14813 {
14814 asection *i;
14815
14816 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14817 {
14818 if (i->size == 0
14819 || i->reloc_count == 0
14820 || i->sec_info_type != SEC_INFO_TYPE_STABS)
14821 continue;
14822
14823 abfd = i->owner;
14824 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14825 continue;
14826
14827 if (!init_reloc_cookie_for_section (&cookie, info, i))
14828 return -1;
14829
14830 if (_bfd_discard_section_stabs (abfd, i,
14831 elf_section_data (i)->sec_info,
14832 bfd_elf_reloc_symbol_deleted_p,
14833 &cookie))
14834 changed = 1;
14835
14836 fini_reloc_cookie_for_section (&cookie, i);
14837 }
14838 }
14839
14840 o = NULL;
14841 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
14842 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
14843 if (o != NULL)
14844 {
14845 asection *i;
14846 int eh_changed = 0;
14847 unsigned int eh_alignment; /* Octets. */
14848
14849 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14850 {
14851 if (i->size == 0)
14852 continue;
14853
14854 abfd = i->owner;
14855 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14856 continue;
14857
14858 if (!init_reloc_cookie_for_section (&cookie, info, i))
14859 return -1;
14860
14861 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
14862 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
14863 bfd_elf_reloc_symbol_deleted_p,
14864 &cookie))
14865 {
14866 eh_changed = 1;
14867 if (i->size != i->rawsize)
14868 changed = 1;
14869 }
14870
14871 fini_reloc_cookie_for_section (&cookie, i);
14872 }
14873
14874 eh_alignment = ((1 << o->alignment_power)
14875 * bfd_octets_per_byte (output_bfd, o));
14876 /* Skip over zero terminator, and prevent empty sections from
14877 adding alignment padding at the end. */
14878 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
14879 if (i->size == 0)
14880 i->flags |= SEC_EXCLUDE;
14881 else if (i->size > 4)
14882 break;
14883 /* The last non-empty eh_frame section doesn't need padding. */
14884 if (i != NULL)
14885 i = i->map_tail.s;
14886 /* Any prior sections must pad the last FDE out to the output
14887 section alignment. Otherwise we might have zero padding
14888 between sections, which would be seen as a terminator. */
14889 for (; i != NULL; i = i->map_tail.s)
14890 if (i->size == 4)
14891 /* All but the last zero terminator should have been removed. */
14892 BFD_FAIL ();
14893 else
14894 {
14895 bfd_size_type size
14896 = (i->size + eh_alignment - 1) & -eh_alignment;
14897 if (i->size != size)
14898 {
14899 i->size = size;
14900 changed = 1;
14901 eh_changed = 1;
14902 }
14903 }
14904 if (eh_changed)
14905 elf_link_hash_traverse (elf_hash_table (info),
14906 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
14907 }
14908
14909 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
14910 {
14911 const struct elf_backend_data *bed;
14912 asection *s;
14913
14914 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14915 continue;
14916 s = abfd->sections;
14917 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14918 continue;
14919
14920 bed = get_elf_backend_data (abfd);
14921
14922 if (bed->elf_backend_discard_info != NULL)
14923 {
14924 if (!init_reloc_cookie (&cookie, info, abfd))
14925 return -1;
14926
14927 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
14928 changed = 1;
14929
14930 fini_reloc_cookie (&cookie, abfd);
14931 }
14932 }
14933
14934 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
14935 _bfd_elf_end_eh_frame_parsing (info);
14936
14937 if (info->eh_frame_hdr_type
14938 && !bfd_link_relocatable (info)
14939 && _bfd_elf_discard_section_eh_frame_hdr (info))
14940 changed = 1;
14941
14942 return changed;
14943 }
14944
14945 bool
14946 _bfd_elf_section_already_linked (bfd *abfd,
14947 asection *sec,
14948 struct bfd_link_info *info)
14949 {
14950 flagword flags;
14951 const char *name, *key;
14952 struct bfd_section_already_linked *l;
14953 struct bfd_section_already_linked_hash_entry *already_linked_list;
14954
14955 if (sec->output_section == bfd_abs_section_ptr)
14956 return false;
14957
14958 flags = sec->flags;
14959
14960 /* Return if it isn't a linkonce section. A comdat group section
14961 also has SEC_LINK_ONCE set. */
14962 if ((flags & SEC_LINK_ONCE) == 0)
14963 return false;
14964
14965 /* Don't put group member sections on our list of already linked
14966 sections. They are handled as a group via their group section. */
14967 if (elf_sec_group (sec) != NULL)
14968 return false;
14969
14970 /* For a SHT_GROUP section, use the group signature as the key. */
14971 name = sec->name;
14972 if ((flags & SEC_GROUP) != 0
14973 && elf_next_in_group (sec) != NULL
14974 && elf_group_name (elf_next_in_group (sec)) != NULL)
14975 key = elf_group_name (elf_next_in_group (sec));
14976 else
14977 {
14978 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
14979 if (startswith (name, ".gnu.linkonce.")
14980 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
14981 key++;
14982 else
14983 /* Must be a user linkonce section that doesn't follow gcc's
14984 naming convention. In this case we won't be matching
14985 single member groups. */
14986 key = name;
14987 }
14988
14989 already_linked_list = bfd_section_already_linked_table_lookup (key);
14990
14991 for (l = already_linked_list->entry; l != NULL; l = l->next)
14992 {
14993 /* We may have 2 different types of sections on the list: group
14994 sections with a signature of <key> (<key> is some string),
14995 and linkonce sections named .gnu.linkonce.<type>.<key>.
14996 Match like sections. LTO plugin sections are an exception.
14997 They are always named .gnu.linkonce.t.<key> and match either
14998 type of section. */
14999 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
15000 && ((flags & SEC_GROUP) != 0
15001 || strcmp (name, l->sec->name) == 0))
15002 || (l->sec->owner->flags & BFD_PLUGIN) != 0
15003 || (sec->owner->flags & BFD_PLUGIN) != 0)
15004 {
15005 /* The section has already been linked. See if we should
15006 issue a warning. */
15007 if (!_bfd_handle_already_linked (sec, l, info))
15008 return false;
15009
15010 if (flags & SEC_GROUP)
15011 {
15012 asection *first = elf_next_in_group (sec);
15013 asection *s = first;
15014
15015 while (s != NULL)
15016 {
15017 s->output_section = bfd_abs_section_ptr;
15018 /* Record which group discards it. */
15019 s->kept_section = l->sec;
15020 s = elf_next_in_group (s);
15021 /* These lists are circular. */
15022 if (s == first)
15023 break;
15024 }
15025 }
15026
15027 return true;
15028 }
15029 }
15030
15031 /* A single member comdat group section may be discarded by a
15032 linkonce section and vice versa. */
15033 if ((flags & SEC_GROUP) != 0)
15034 {
15035 asection *first = elf_next_in_group (sec);
15036
15037 if (first != NULL && elf_next_in_group (first) == first)
15038 /* Check this single member group against linkonce sections. */
15039 for (l = already_linked_list->entry; l != NULL; l = l->next)
15040 if ((l->sec->flags & SEC_GROUP) == 0
15041 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
15042 {
15043 first->output_section = bfd_abs_section_ptr;
15044 first->kept_section = l->sec;
15045 sec->output_section = bfd_abs_section_ptr;
15046 break;
15047 }
15048 }
15049 else
15050 /* Check this linkonce section against single member groups. */
15051 for (l = already_linked_list->entry; l != NULL; l = l->next)
15052 if (l->sec->flags & SEC_GROUP)
15053 {
15054 asection *first = elf_next_in_group (l->sec);
15055
15056 if (first != NULL
15057 && elf_next_in_group (first) == first
15058 && bfd_elf_match_symbols_in_sections (first, sec, info))
15059 {
15060 sec->output_section = bfd_abs_section_ptr;
15061 sec->kept_section = first;
15062 break;
15063 }
15064 }
15065
15066 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
15067 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
15068 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
15069 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
15070 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
15071 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
15072 `.gnu.linkonce.t.F' section from a different bfd not requiring any
15073 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
15074 The reverse order cannot happen as there is never a bfd with only the
15075 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
15076 matter as here were are looking only for cross-bfd sections. */
15077
15078 if ((flags & SEC_GROUP) == 0 && startswith (name, ".gnu.linkonce.r."))
15079 for (l = already_linked_list->entry; l != NULL; l = l->next)
15080 if ((l->sec->flags & SEC_GROUP) == 0
15081 && startswith (l->sec->name, ".gnu.linkonce.t."))
15082 {
15083 if (abfd != l->sec->owner)
15084 sec->output_section = bfd_abs_section_ptr;
15085 break;
15086 }
15087
15088 /* This is the first section with this name. Record it. */
15089 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
15090 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
15091 return sec->output_section == bfd_abs_section_ptr;
15092 }
15093
15094 bool
15095 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
15096 {
15097 return sym->st_shndx == SHN_COMMON;
15098 }
15099
15100 unsigned int
15101 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
15102 {
15103 return SHN_COMMON;
15104 }
15105
15106 asection *
15107 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
15108 {
15109 return bfd_com_section_ptr;
15110 }
15111
15112 bfd_vma
15113 _bfd_elf_default_got_elt_size (bfd *abfd,
15114 struct bfd_link_info *info ATTRIBUTE_UNUSED,
15115 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
15116 bfd *ibfd ATTRIBUTE_UNUSED,
15117 unsigned long symndx ATTRIBUTE_UNUSED)
15118 {
15119 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15120 return bed->s->arch_size / 8;
15121 }
15122
15123 /* Routines to support the creation of dynamic relocs. */
15124
15125 /* Returns the name of the dynamic reloc section associated with SEC. */
15126
15127 static const char *
15128 get_dynamic_reloc_section_name (bfd * abfd,
15129 asection * sec,
15130 bool is_rela)
15131 {
15132 char *name;
15133 const char *old_name = bfd_section_name (sec);
15134 const char *prefix = is_rela ? ".rela" : ".rel";
15135
15136 if (old_name == NULL)
15137 return NULL;
15138
15139 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
15140 sprintf (name, "%s%s", prefix, old_name);
15141
15142 return name;
15143 }
15144
15145 /* Returns the dynamic reloc section associated with SEC.
15146 If necessary compute the name of the dynamic reloc section based
15147 on SEC's name (looked up in ABFD's string table) and the setting
15148 of IS_RELA. */
15149
15150 asection *
15151 _bfd_elf_get_dynamic_reloc_section (bfd *abfd,
15152 asection *sec,
15153 bool is_rela)
15154 {
15155 asection *reloc_sec = elf_section_data (sec)->sreloc;
15156
15157 if (reloc_sec == NULL)
15158 {
15159 const char *name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
15160
15161 if (name != NULL)
15162 {
15163 reloc_sec = bfd_get_linker_section (abfd, name);
15164
15165 if (reloc_sec != NULL)
15166 elf_section_data (sec)->sreloc = reloc_sec;
15167 }
15168 }
15169
15170 return reloc_sec;
15171 }
15172
15173 /* Returns the dynamic reloc section associated with SEC. If the
15174 section does not exist it is created and attached to the DYNOBJ
15175 bfd and stored in the SRELOC field of SEC's elf_section_data
15176 structure.
15177
15178 ALIGNMENT is the alignment for the newly created section and
15179 IS_RELA defines whether the name should be .rela.<SEC's name>
15180 or .rel.<SEC's name>. The section name is looked up in the
15181 string table associated with ABFD. */
15182
15183 asection *
15184 _bfd_elf_make_dynamic_reloc_section (asection *sec,
15185 bfd *dynobj,
15186 unsigned int alignment,
15187 bfd *abfd,
15188 bool is_rela)
15189 {
15190 asection * reloc_sec = elf_section_data (sec)->sreloc;
15191
15192 if (reloc_sec == NULL)
15193 {
15194 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
15195
15196 if (name == NULL)
15197 return NULL;
15198
15199 reloc_sec = bfd_get_linker_section (dynobj, name);
15200
15201 if (reloc_sec == NULL)
15202 {
15203 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
15204 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
15205 if ((sec->flags & SEC_ALLOC) != 0)
15206 flags |= SEC_ALLOC | SEC_LOAD;
15207
15208 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
15209 if (reloc_sec != NULL)
15210 {
15211 /* _bfd_elf_get_sec_type_attr chooses a section type by
15212 name. Override as it may be wrong, eg. for a user
15213 section named "auto" we'll get ".relauto" which is
15214 seen to be a .rela section. */
15215 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
15216 if (!bfd_set_section_alignment (reloc_sec, alignment))
15217 reloc_sec = NULL;
15218 }
15219 }
15220
15221 elf_section_data (sec)->sreloc = reloc_sec;
15222 }
15223
15224 return reloc_sec;
15225 }
15226
15227 /* Copy the ELF symbol type and other attributes for a linker script
15228 assignment from HSRC to HDEST. Generally this should be treated as
15229 if we found a strong non-dynamic definition for HDEST (except that
15230 ld ignores multiple definition errors). */
15231 void
15232 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
15233 struct bfd_link_hash_entry *hdest,
15234 struct bfd_link_hash_entry *hsrc)
15235 {
15236 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
15237 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
15238 Elf_Internal_Sym isym;
15239
15240 ehdest->type = ehsrc->type;
15241 ehdest->target_internal = ehsrc->target_internal;
15242
15243 isym.st_other = ehsrc->other;
15244 elf_merge_st_other (abfd, ehdest, isym.st_other, NULL, true, false);
15245 }
15246
15247 /* Append a RELA relocation REL to section S in BFD. */
15248
15249 void
15250 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15251 {
15252 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15253 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
15254 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
15255 bed->s->swap_reloca_out (abfd, rel, loc);
15256 }
15257
15258 /* Append a REL relocation REL to section S in BFD. */
15259
15260 void
15261 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15262 {
15263 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15264 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
15265 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
15266 bed->s->swap_reloc_out (abfd, rel, loc);
15267 }
15268
15269 /* Define __start, __stop, .startof. or .sizeof. symbol. */
15270
15271 struct bfd_link_hash_entry *
15272 bfd_elf_define_start_stop (struct bfd_link_info *info,
15273 const char *symbol, asection *sec)
15274 {
15275 struct elf_link_hash_entry *h;
15276
15277 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
15278 false, false, true);
15279 /* NB: Common symbols will be turned into definition later. */
15280 if (h != NULL
15281 && !h->root.ldscript_def
15282 && (h->root.type == bfd_link_hash_undefined
15283 || h->root.type == bfd_link_hash_undefweak
15284 || ((h->ref_regular || h->def_dynamic)
15285 && !h->def_regular
15286 && h->root.type != bfd_link_hash_common)))
15287 {
15288 bool was_dynamic = h->ref_dynamic || h->def_dynamic;
15289 h->verinfo.verdef = NULL;
15290 h->root.type = bfd_link_hash_defined;
15291 h->root.u.def.section = sec;
15292 h->root.u.def.value = 0;
15293 h->def_regular = 1;
15294 h->def_dynamic = 0;
15295 h->start_stop = 1;
15296 h->u2.start_stop_section = sec;
15297 if (symbol[0] == '.')
15298 {
15299 /* .startof. and .sizeof. symbols are local. */
15300 const struct elf_backend_data *bed;
15301 bed = get_elf_backend_data (info->output_bfd);
15302 (*bed->elf_backend_hide_symbol) (info, h, true);
15303 }
15304 else
15305 {
15306 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
15307 h->other = ((h->other & ~ELF_ST_VISIBILITY (-1))
15308 | info->start_stop_visibility);
15309 if (was_dynamic)
15310 bfd_elf_link_record_dynamic_symbol (info, h);
15311 }
15312 return &h->root;
15313 }
15314 return NULL;
15315 }
15316
15317 /* Find dynamic relocs for H that apply to read-only sections. */
15318
15319 asection *
15320 _bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry *h)
15321 {
15322 struct elf_dyn_relocs *p;
15323
15324 for (p = h->dyn_relocs; p != NULL; p = p->next)
15325 {
15326 asection *s = p->sec->output_section;
15327
15328 if (s != NULL && (s->flags & SEC_READONLY) != 0)
15329 return p->sec;
15330 }
15331 return NULL;
15332 }
15333
15334 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
15335 read-only sections. */
15336
15337 bool
15338 _bfd_elf_maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
15339 {
15340 asection *sec;
15341
15342 if (h->root.type == bfd_link_hash_indirect)
15343 return true;
15344
15345 sec = _bfd_elf_readonly_dynrelocs (h);
15346 if (sec != NULL)
15347 {
15348 struct bfd_link_info *info = (struct bfd_link_info *) inf;
15349
15350 info->flags |= DF_TEXTREL;
15351 /* xgettext:c-format */
15352 info->callbacks->minfo (_("%pB: dynamic relocation against `%pT' "
15353 "in read-only section `%pA'\n"),
15354 sec->owner, h->root.root.string, sec);
15355
15356 if (bfd_link_textrel_check (info))
15357 /* xgettext:c-format */
15358 info->callbacks->einfo (_("%P: %pB: warning: relocation against `%s' "
15359 "in read-only section `%pA'\n"),
15360 sec->owner, h->root.root.string, sec);
15361
15362 /* Not an error, just cut short the traversal. */
15363 return false;
15364 }
15365 return true;
15366 }
15367
15368 /* Add dynamic tags. */
15369
15370 bool
15371 _bfd_elf_add_dynamic_tags (bfd *output_bfd, struct bfd_link_info *info,
15372 bool need_dynamic_reloc)
15373 {
15374 struct elf_link_hash_table *htab = elf_hash_table (info);
15375
15376 if (htab->dynamic_sections_created)
15377 {
15378 /* Add some entries to the .dynamic section. We fill in the
15379 values later, in finish_dynamic_sections, but we must add
15380 the entries now so that we get the correct size for the
15381 .dynamic section. The DT_DEBUG entry is filled in by the
15382 dynamic linker and used by the debugger. */
15383 #define add_dynamic_entry(TAG, VAL) \
15384 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
15385
15386 const struct elf_backend_data *bed
15387 = get_elf_backend_data (output_bfd);
15388
15389 if (bfd_link_executable (info))
15390 {
15391 if (!add_dynamic_entry (DT_DEBUG, 0))
15392 return false;
15393 }
15394
15395 if (htab->dt_pltgot_required || htab->splt->size != 0)
15396 {
15397 /* DT_PLTGOT is used by prelink even if there is no PLT
15398 relocation. */
15399 if (!add_dynamic_entry (DT_PLTGOT, 0))
15400 return false;
15401 }
15402
15403 if (htab->dt_jmprel_required || htab->srelplt->size != 0)
15404 {
15405 if (!add_dynamic_entry (DT_PLTRELSZ, 0)
15406 || !add_dynamic_entry (DT_PLTREL,
15407 (bed->rela_plts_and_copies_p
15408 ? DT_RELA : DT_REL))
15409 || !add_dynamic_entry (DT_JMPREL, 0))
15410 return false;
15411 }
15412
15413 if (htab->tlsdesc_plt
15414 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
15415 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
15416 return false;
15417
15418 if (need_dynamic_reloc)
15419 {
15420 if (bed->rela_plts_and_copies_p)
15421 {
15422 if (!add_dynamic_entry (DT_RELA, 0)
15423 || !add_dynamic_entry (DT_RELASZ, 0)
15424 || !add_dynamic_entry (DT_RELAENT,
15425 bed->s->sizeof_rela))
15426 return false;
15427 }
15428 else
15429 {
15430 if (!add_dynamic_entry (DT_REL, 0)
15431 || !add_dynamic_entry (DT_RELSZ, 0)
15432 || !add_dynamic_entry (DT_RELENT,
15433 bed->s->sizeof_rel))
15434 return false;
15435 }
15436
15437 /* If any dynamic relocs apply to a read-only section,
15438 then we need a DT_TEXTREL entry. */
15439 if ((info->flags & DF_TEXTREL) == 0)
15440 elf_link_hash_traverse (htab, _bfd_elf_maybe_set_textrel,
15441 info);
15442
15443 if ((info->flags & DF_TEXTREL) != 0)
15444 {
15445 if (htab->ifunc_resolvers)
15446 info->callbacks->einfo
15447 (_("%P: warning: GNU indirect functions with DT_TEXTREL "
15448 "may result in a segfault at runtime; recompile with %s\n"),
15449 bfd_link_dll (info) ? "-fPIC" : "-fPIE");
15450
15451 if (!add_dynamic_entry (DT_TEXTREL, 0))
15452 return false;
15453 }
15454 }
15455 }
15456 #undef add_dynamic_entry
15457
15458 return true;
15459 }