]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - bfd/elflink.c
Add extra linker warning message about discrepancies between normal and common symbols.
[thirdparty/binutils-gdb.git] / bfd / elflink.c
1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2023 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 && (s->flags & SEC_HAS_CONTENTS) != 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 (size_t) (dynbuf + s->size - extdyn) >= 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 normal symbol `%s' in %pB"
5306 " is smaller than %u used by the common definition in %pB"),
5307 1 << normal_align, name, normal_bfd,
5308 1 << common_align, common_bfd);
5309
5310 /* PR 30499: make sure that users understand that this warning is serious. */
5311 _bfd_error_handler
5312 (_("warning: NOTE: alignment discrepancies can cause real problems. Investigation is advised."));
5313 }
5314 }
5315
5316 /* Remember the symbol size if it isn't undefined. */
5317 if (isym->st_size != 0
5318 && isym->st_shndx != SHN_UNDEF
5319 && (definition || h->size == 0))
5320 {
5321 if (h->size != 0
5322 && h->size != isym->st_size
5323 && ! size_change_ok)
5324 {
5325 _bfd_error_handler
5326 /* xgettext:c-format */
5327 (_("warning: size of symbol `%s' changed"
5328 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
5329 name, (uint64_t) h->size, old_bfd,
5330 (uint64_t) isym->st_size, abfd);
5331
5332 /* PR 30499: make sure that users understand that this warning is serious. */
5333 _bfd_error_handler
5334 (_("warning: NOTE: size discrepancies can cause real problems. Investigation is advised."));
5335 }
5336
5337 h->size = isym->st_size;
5338 }
5339
5340 /* If this is a common symbol, then we always want H->SIZE
5341 to be the size of the common symbol. The code just above
5342 won't fix the size if a common symbol becomes larger. We
5343 don't warn about a size change here, because that is
5344 covered by --warn-common. Allow changes between different
5345 function types. */
5346 if (h->root.type == bfd_link_hash_common)
5347 h->size = h->root.u.c.size;
5348
5349 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
5350 && ((definition && !new_weak)
5351 || (old_weak && h->root.type == bfd_link_hash_common)
5352 || h->type == STT_NOTYPE))
5353 {
5354 unsigned int type = ELF_ST_TYPE (isym->st_info);
5355
5356 /* Turn an IFUNC symbol from a DSO into a normal FUNC
5357 symbol. */
5358 if (type == STT_GNU_IFUNC
5359 && (abfd->flags & DYNAMIC) != 0)
5360 type = STT_FUNC;
5361
5362 if (h->type != type)
5363 {
5364 if (h->type != STT_NOTYPE && ! type_change_ok)
5365 /* xgettext:c-format */
5366 _bfd_error_handler
5367 (_("warning: type of symbol `%s' changed"
5368 " from %d to %d in %pB"),
5369 name, h->type, type, abfd);
5370
5371 h->type = type;
5372 }
5373 }
5374
5375 /* Merge st_other field. */
5376 elf_merge_st_other (abfd, h, isym->st_other, sec,
5377 definition, dynamic);
5378
5379 /* We don't want to make debug symbol dynamic. */
5380 if (definition
5381 && (sec->flags & SEC_DEBUGGING)
5382 && !bfd_link_relocatable (info))
5383 dynsym = false;
5384
5385 /* Nor should we make plugin symbols dynamic. */
5386 if ((abfd->flags & BFD_PLUGIN) != 0)
5387 dynsym = false;
5388
5389 if (definition)
5390 {
5391 h->target_internal = isym->st_target_internal;
5392 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5393 }
5394
5395 /* Don't add indirect symbols for .symver x, x@FOO aliases
5396 in IR. Since all data or text symbols in IR have the
5397 same type, value and section, we can't tell if a symbol
5398 is an alias of another symbol by their types, values and
5399 sections. */
5400 if (definition
5401 && !dynamic
5402 && (abfd->flags & BFD_PLUGIN) == 0)
5403 {
5404 char *p = strchr (name, ELF_VER_CHR);
5405 if (p != NULL && p[1] != ELF_VER_CHR)
5406 {
5407 /* Queue non-default versions so that .symver x, x@FOO
5408 aliases can be checked. */
5409 if (!nondeflt_vers)
5410 {
5411 size_t amt = ((isymend - isym + 1)
5412 * sizeof (struct elf_link_hash_entry *));
5413 nondeflt_vers
5414 = (struct elf_link_hash_entry **) bfd_malloc (amt);
5415 if (!nondeflt_vers)
5416 goto error_free_vers;
5417 }
5418 nondeflt_vers[nondeflt_vers_cnt++] = h;
5419 }
5420 }
5421
5422 if (dynsym && h->dynindx == -1)
5423 {
5424 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5425 goto error_free_vers;
5426 if (h->is_weakalias
5427 && weakdef (h)->dynindx == -1)
5428 {
5429 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
5430 goto error_free_vers;
5431 }
5432 }
5433 else if (h->dynindx != -1)
5434 /* If the symbol already has a dynamic index, but
5435 visibility says it should not be visible, turn it into
5436 a local symbol. */
5437 switch (ELF_ST_VISIBILITY (h->other))
5438 {
5439 case STV_INTERNAL:
5440 case STV_HIDDEN:
5441 (*bed->elf_backend_hide_symbol) (info, h, true);
5442 dynsym = false;
5443 break;
5444 }
5445
5446 if (!add_needed
5447 && matched
5448 && definition
5449 && h->root.type != bfd_link_hash_indirect
5450 && ((dynsym
5451 && h->ref_regular_nonweak)
5452 || (old_bfd != NULL
5453 && (old_bfd->flags & BFD_PLUGIN) != 0
5454 && h->ref_ir_nonweak
5455 && !info->lto_all_symbols_read)
5456 || (h->ref_dynamic_nonweak
5457 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5458 && !on_needed_list (elf_dt_name (abfd),
5459 htab->needed, NULL))))
5460 {
5461 const char *soname = elf_dt_name (abfd);
5462
5463 info->callbacks->minfo ("%!", soname, old_bfd,
5464 h->root.root.string);
5465
5466 /* A symbol from a library loaded via DT_NEEDED of some
5467 other library is referenced by a regular object.
5468 Add a DT_NEEDED entry for it. Issue an error if
5469 --no-add-needed is used and the reference was not
5470 a weak one. */
5471 if (old_bfd != NULL
5472 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
5473 {
5474 _bfd_error_handler
5475 /* xgettext:c-format */
5476 (_("%pB: undefined reference to symbol '%s'"),
5477 old_bfd, name);
5478 bfd_set_error (bfd_error_missing_dso);
5479 goto error_free_vers;
5480 }
5481
5482 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
5483 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
5484
5485 /* Create dynamic sections for backends that require
5486 that be done before setup_gnu_properties. */
5487 if (!_bfd_elf_link_create_dynamic_sections (abfd, info))
5488 return false;
5489 add_needed = true;
5490 }
5491 }
5492 }
5493
5494 if (info->lto_plugin_active
5495 && !bfd_link_relocatable (info)
5496 && (abfd->flags & BFD_PLUGIN) == 0
5497 && !just_syms
5498 && extsymcount)
5499 {
5500 int r_sym_shift;
5501
5502 if (bed->s->arch_size == 32)
5503 r_sym_shift = 8;
5504 else
5505 r_sym_shift = 32;
5506
5507 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5508 referenced in regular objects so that linker plugin will get
5509 the correct symbol resolution. */
5510
5511 sym_hash = elf_sym_hashes (abfd);
5512 for (s = abfd->sections; s != NULL; s = s->next)
5513 {
5514 Elf_Internal_Rela *internal_relocs;
5515 Elf_Internal_Rela *rel, *relend;
5516
5517 /* Don't check relocations in excluded sections. */
5518 if ((s->flags & SEC_RELOC) == 0
5519 || s->reloc_count == 0
5520 || (s->flags & SEC_EXCLUDE) != 0
5521 || ((info->strip == strip_all
5522 || info->strip == strip_debugger)
5523 && (s->flags & SEC_DEBUGGING) != 0))
5524 continue;
5525
5526 internal_relocs = _bfd_elf_link_info_read_relocs (abfd, info,
5527 s, NULL,
5528 NULL,
5529 _bfd_link_keep_memory (info));
5530 if (internal_relocs == NULL)
5531 goto error_free_vers;
5532
5533 rel = internal_relocs;
5534 relend = rel + s->reloc_count;
5535 for ( ; rel < relend; rel++)
5536 {
5537 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5538 struct elf_link_hash_entry *h;
5539
5540 /* Skip local symbols. */
5541 if (r_symndx < extsymoff)
5542 continue;
5543
5544 h = sym_hash[r_symndx - extsymoff];
5545 if (h != NULL)
5546 h->root.non_ir_ref_regular = 1;
5547 }
5548
5549 if (elf_section_data (s)->relocs != internal_relocs)
5550 free (internal_relocs);
5551 }
5552 }
5553
5554 free (extversym);
5555 extversym = NULL;
5556 free (isymbuf);
5557 isymbuf = NULL;
5558
5559 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5560 {
5561 unsigned int i;
5562
5563 /* Restore the symbol table. */
5564 old_ent = (char *) old_tab + tabsize;
5565 memset (elf_sym_hashes (abfd), 0,
5566 extsymcount * sizeof (struct elf_link_hash_entry *));
5567 htab->root.table.table = old_table;
5568 htab->root.table.size = old_size;
5569 htab->root.table.count = old_count;
5570 memcpy (htab->root.table.table, old_tab, tabsize);
5571 htab->root.undefs = old_undefs;
5572 htab->root.undefs_tail = old_undefs_tail;
5573 if (htab->dynstr != NULL)
5574 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5575 free (old_strtab);
5576 old_strtab = NULL;
5577 for (i = 0; i < htab->root.table.size; i++)
5578 {
5579 struct bfd_hash_entry *p;
5580 struct elf_link_hash_entry *h;
5581 unsigned int non_ir_ref_dynamic;
5582
5583 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5584 {
5585 /* Preserve non_ir_ref_dynamic so that this symbol
5586 will be exported when the dynamic lib becomes needed
5587 in the second pass. */
5588 h = (struct elf_link_hash_entry *) p;
5589 if (h->root.type == bfd_link_hash_warning)
5590 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5591 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
5592
5593 h = (struct elf_link_hash_entry *) p;
5594 memcpy (h, old_ent, htab->root.table.entsize);
5595 old_ent = (char *) old_ent + htab->root.table.entsize;
5596 if (h->root.type == bfd_link_hash_warning)
5597 {
5598 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5599 memcpy (h, old_ent, htab->root.table.entsize);
5600 old_ent = (char *) old_ent + htab->root.table.entsize;
5601 }
5602 if (h->root.type == bfd_link_hash_common)
5603 {
5604 memcpy (h->root.u.c.p, old_ent, sizeof (*h->root.u.c.p));
5605 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
5606 }
5607 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
5608 }
5609 }
5610
5611 /* Make a special call to the linker "notice" function to
5612 tell it that symbols added for crefs may need to be removed. */
5613 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
5614 goto error_free_vers;
5615
5616 free (old_tab);
5617 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5618 alloc_mark);
5619 free (nondeflt_vers);
5620 return true;
5621 }
5622
5623 if (old_tab != NULL)
5624 {
5625 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
5626 goto error_free_vers;
5627 free (old_tab);
5628 old_tab = NULL;
5629 }
5630
5631 /* Now that all the symbols from this input file are created, if
5632 not performing a relocatable link, handle .symver foo, foo@BAR
5633 such that any relocs against foo become foo@BAR. */
5634 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
5635 {
5636 size_t cnt, symidx;
5637
5638 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5639 {
5640 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5641 char *shortname, *p;
5642 size_t amt;
5643
5644 p = strchr (h->root.root.string, ELF_VER_CHR);
5645 if (p == NULL
5646 || (h->root.type != bfd_link_hash_defined
5647 && h->root.type != bfd_link_hash_defweak))
5648 continue;
5649
5650 amt = p - h->root.root.string;
5651 shortname = (char *) bfd_malloc (amt + 1);
5652 if (!shortname)
5653 goto error_free_vers;
5654 memcpy (shortname, h->root.root.string, amt);
5655 shortname[amt] = '\0';
5656
5657 hi = (struct elf_link_hash_entry *)
5658 bfd_link_hash_lookup (&htab->root, shortname,
5659 false, false, false);
5660 if (hi != NULL
5661 && hi->root.type == h->root.type
5662 && hi->root.u.def.value == h->root.u.def.value
5663 && hi->root.u.def.section == h->root.u.def.section)
5664 {
5665 (*bed->elf_backend_hide_symbol) (info, hi, true);
5666 hi->root.type = bfd_link_hash_indirect;
5667 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
5668 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
5669 sym_hash = elf_sym_hashes (abfd);
5670 if (sym_hash)
5671 for (symidx = 0; symidx < extsymcount; ++symidx)
5672 if (sym_hash[symidx] == hi)
5673 {
5674 sym_hash[symidx] = h;
5675 break;
5676 }
5677 }
5678 free (shortname);
5679 }
5680 free (nondeflt_vers);
5681 nondeflt_vers = NULL;
5682 }
5683
5684 /* Now set the alias field correctly for all the weak defined
5685 symbols we found. The only way to do this is to search all the
5686 symbols. Since we only need the information for non functions in
5687 dynamic objects, that's the only time we actually put anything on
5688 the list WEAKS. We need this information so that if a regular
5689 object refers to a symbol defined weakly in a dynamic object, the
5690 real symbol in the dynamic object is also put in the dynamic
5691 symbols; we also must arrange for both symbols to point to the
5692 same memory location. We could handle the general case of symbol
5693 aliasing, but a general symbol alias can only be generated in
5694 assembler code, handling it correctly would be very time
5695 consuming, and other ELF linkers don't handle general aliasing
5696 either. */
5697 if (weaks != NULL)
5698 {
5699 struct elf_link_hash_entry **hpp;
5700 struct elf_link_hash_entry **hppend;
5701 struct elf_link_hash_entry **sorted_sym_hash;
5702 struct elf_link_hash_entry *h;
5703 size_t sym_count, amt;
5704
5705 /* Since we have to search the whole symbol list for each weak
5706 defined symbol, search time for N weak defined symbols will be
5707 O(N^2). Binary search will cut it down to O(NlogN). */
5708 amt = extsymcount * sizeof (*sorted_sym_hash);
5709 sorted_sym_hash = bfd_malloc (amt);
5710 if (sorted_sym_hash == NULL)
5711 goto error_return;
5712 sym_hash = sorted_sym_hash;
5713 hpp = elf_sym_hashes (abfd);
5714 hppend = hpp + extsymcount;
5715 sym_count = 0;
5716 for (; hpp < hppend; hpp++)
5717 {
5718 h = *hpp;
5719 if (h != NULL
5720 && h->root.type == bfd_link_hash_defined
5721 && !bed->is_function_type (h->type))
5722 {
5723 *sym_hash = h;
5724 sym_hash++;
5725 sym_count++;
5726 }
5727 }
5728
5729 qsort (sorted_sym_hash, sym_count, sizeof (*sorted_sym_hash),
5730 elf_sort_symbol);
5731
5732 while (weaks != NULL)
5733 {
5734 struct elf_link_hash_entry *hlook;
5735 asection *slook;
5736 bfd_vma vlook;
5737 size_t i, j, idx = 0;
5738
5739 hlook = weaks;
5740 weaks = hlook->u.alias;
5741 hlook->u.alias = NULL;
5742
5743 if (hlook->root.type != bfd_link_hash_defined
5744 && hlook->root.type != bfd_link_hash_defweak)
5745 continue;
5746
5747 slook = hlook->root.u.def.section;
5748 vlook = hlook->root.u.def.value;
5749
5750 i = 0;
5751 j = sym_count;
5752 while (i != j)
5753 {
5754 bfd_signed_vma vdiff;
5755 idx = (i + j) / 2;
5756 h = sorted_sym_hash[idx];
5757 vdiff = vlook - h->root.u.def.value;
5758 if (vdiff < 0)
5759 j = idx;
5760 else if (vdiff > 0)
5761 i = idx + 1;
5762 else
5763 {
5764 int sdiff = slook->id - h->root.u.def.section->id;
5765 if (sdiff < 0)
5766 j = idx;
5767 else if (sdiff > 0)
5768 i = idx + 1;
5769 else
5770 break;
5771 }
5772 }
5773
5774 /* We didn't find a value/section match. */
5775 if (i == j)
5776 continue;
5777
5778 /* With multiple aliases, or when the weak symbol is already
5779 strongly defined, we have multiple matching symbols and
5780 the binary search above may land on any of them. Step
5781 one past the matching symbol(s). */
5782 while (++idx != j)
5783 {
5784 h = sorted_sym_hash[idx];
5785 if (h->root.u.def.section != slook
5786 || h->root.u.def.value != vlook)
5787 break;
5788 }
5789
5790 /* Now look back over the aliases. Since we sorted by size
5791 as well as value and section, we'll choose the one with
5792 the largest size. */
5793 while (idx-- != i)
5794 {
5795 h = sorted_sym_hash[idx];
5796
5797 /* Stop if value or section doesn't match. */
5798 if (h->root.u.def.section != slook
5799 || h->root.u.def.value != vlook)
5800 break;
5801 else if (h != hlook)
5802 {
5803 struct elf_link_hash_entry *t;
5804
5805 hlook->u.alias = h;
5806 hlook->is_weakalias = 1;
5807 t = h;
5808 if (t->u.alias != NULL)
5809 while (t->u.alias != h)
5810 t = t->u.alias;
5811 t->u.alias = hlook;
5812
5813 /* If the weak definition is in the list of dynamic
5814 symbols, make sure the real definition is put
5815 there as well. */
5816 if (hlook->dynindx != -1 && h->dynindx == -1)
5817 {
5818 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5819 {
5820 err_free_sym_hash:
5821 free (sorted_sym_hash);
5822 goto error_return;
5823 }
5824 }
5825
5826 /* If the real definition is in the list of dynamic
5827 symbols, make sure the weak definition is put
5828 there as well. If we don't do this, then the
5829 dynamic loader might not merge the entries for the
5830 real definition and the weak definition. */
5831 if (h->dynindx != -1 && hlook->dynindx == -1)
5832 {
5833 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
5834 goto err_free_sym_hash;
5835 }
5836 break;
5837 }
5838 }
5839 }
5840
5841 free (sorted_sym_hash);
5842 }
5843
5844 if (bed->check_directives
5845 && !(*bed->check_directives) (abfd, info))
5846 return false;
5847
5848 /* If this is a non-traditional link, try to optimize the handling
5849 of the .stab/.stabstr sections. */
5850 if (! dynamic
5851 && ! info->traditional_format
5852 && is_elf_hash_table (&htab->root)
5853 && (info->strip != strip_all && info->strip != strip_debugger))
5854 {
5855 asection *stabstr;
5856
5857 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5858 if (stabstr != NULL)
5859 {
5860 bfd_size_type string_offset = 0;
5861 asection *stab;
5862
5863 for (stab = abfd->sections; stab; stab = stab->next)
5864 if (startswith (stab->name, ".stab")
5865 && (!stab->name[5] ||
5866 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5867 && (stab->flags & SEC_MERGE) == 0
5868 && !bfd_is_abs_section (stab->output_section))
5869 {
5870 struct bfd_elf_section_data *secdata;
5871
5872 secdata = elf_section_data (stab);
5873 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5874 stabstr, &secdata->sec_info,
5875 &string_offset))
5876 goto error_return;
5877 if (secdata->sec_info)
5878 stab->sec_info_type = SEC_INFO_TYPE_STABS;
5879 }
5880 }
5881 }
5882
5883 if (dynamic && add_needed)
5884 {
5885 /* Add this bfd to the loaded list. */
5886 struct elf_link_loaded_list *n;
5887
5888 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5889 if (n == NULL)
5890 goto error_return;
5891 n->abfd = abfd;
5892 n->next = htab->dyn_loaded;
5893 htab->dyn_loaded = n;
5894 }
5895 if (dynamic && !add_needed
5896 && (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) != 0)
5897 elf_dyn_lib_class (abfd) |= DYN_NO_NEEDED;
5898
5899 return true;
5900
5901 error_free_vers:
5902 free (old_tab);
5903 free (old_strtab);
5904 free (nondeflt_vers);
5905 free (extversym);
5906 error_free_sym:
5907 free (isymbuf);
5908 error_return:
5909 return false;
5910 }
5911
5912 /* Return the linker hash table entry of a symbol that might be
5913 satisfied by an archive symbol. Return -1 on error. */
5914
5915 struct bfd_link_hash_entry *
5916 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5917 struct bfd_link_info *info,
5918 const char *name)
5919 {
5920 struct bfd_link_hash_entry *h;
5921 char *p, *copy;
5922 size_t len, first;
5923
5924 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
5925 if (h != NULL)
5926 return h;
5927
5928 /* If this is a default version (the name contains @@), look up the
5929 symbol again with only one `@' as well as without the version.
5930 The effect is that references to the symbol with and without the
5931 version will be matched by the default symbol in the archive. */
5932
5933 p = strchr (name, ELF_VER_CHR);
5934 if (p == NULL || p[1] != ELF_VER_CHR)
5935 return h;
5936
5937 /* First check with only one `@'. */
5938 len = strlen (name);
5939 copy = (char *) bfd_alloc (abfd, len);
5940 if (copy == NULL)
5941 return (struct bfd_link_hash_entry *) -1;
5942
5943 first = p - name + 1;
5944 memcpy (copy, name, first);
5945 memcpy (copy + first, name + first + 1, len - first);
5946
5947 h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
5948 if (h == NULL)
5949 {
5950 /* We also need to check references to the symbol without the
5951 version. */
5952 copy[first - 1] = '\0';
5953 h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
5954 }
5955
5956 bfd_release (abfd, copy);
5957 return h;
5958 }
5959
5960 /* Add symbols from an ELF archive file to the linker hash table. We
5961 don't use _bfd_generic_link_add_archive_symbols because we need to
5962 handle versioned symbols.
5963
5964 Fortunately, ELF archive handling is simpler than that done by
5965 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5966 oddities. In ELF, if we find a symbol in the archive map, and the
5967 symbol is currently undefined, we know that we must pull in that
5968 object file.
5969
5970 Unfortunately, we do have to make multiple passes over the symbol
5971 table until nothing further is resolved. */
5972
5973 static bool
5974 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5975 {
5976 symindex c;
5977 unsigned char *included = NULL;
5978 carsym *symdefs;
5979 bool loop;
5980 size_t amt;
5981 const struct elf_backend_data *bed;
5982 struct bfd_link_hash_entry * (*archive_symbol_lookup)
5983 (bfd *, struct bfd_link_info *, const char *);
5984
5985 if (! bfd_has_map (abfd))
5986 {
5987 /* An empty archive is a special case. */
5988 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5989 return true;
5990 bfd_set_error (bfd_error_no_armap);
5991 return false;
5992 }
5993
5994 /* Keep track of all symbols we know to be already defined, and all
5995 files we know to be already included. This is to speed up the
5996 second and subsequent passes. */
5997 c = bfd_ardata (abfd)->symdef_count;
5998 if (c == 0)
5999 return true;
6000 amt = c * sizeof (*included);
6001 included = (unsigned char *) bfd_zmalloc (amt);
6002 if (included == NULL)
6003 return false;
6004
6005 symdefs = bfd_ardata (abfd)->symdefs;
6006 bed = get_elf_backend_data (abfd);
6007 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
6008
6009 do
6010 {
6011 file_ptr last;
6012 symindex i;
6013 carsym *symdef;
6014 carsym *symdefend;
6015
6016 loop = false;
6017 last = -1;
6018
6019 symdef = symdefs;
6020 symdefend = symdef + c;
6021 for (i = 0; symdef < symdefend; symdef++, i++)
6022 {
6023 struct bfd_link_hash_entry *h;
6024 bfd *element;
6025 struct bfd_link_hash_entry *undefs_tail;
6026 symindex mark;
6027
6028 if (included[i])
6029 continue;
6030 if (symdef->file_offset == last)
6031 {
6032 included[i] = true;
6033 continue;
6034 }
6035
6036 h = archive_symbol_lookup (abfd, info, symdef->name);
6037 if (h == (struct bfd_link_hash_entry *) -1)
6038 goto error_return;
6039
6040 if (h == NULL)
6041 continue;
6042
6043 if (h->type == bfd_link_hash_undefined)
6044 {
6045 /* If the archive element has already been loaded then one
6046 of the symbols defined by that element might have been
6047 made undefined due to being in a discarded section. */
6048 if (is_elf_hash_table (info->hash)
6049 && ((struct elf_link_hash_entry *) h)->indx == -3)
6050 continue;
6051 }
6052 else if (h->type == bfd_link_hash_common)
6053 {
6054 /* We currently have a common symbol. The archive map contains
6055 a reference to this symbol, so we may want to include it. We
6056 only want to include it however, if this archive element
6057 contains a definition of the symbol, not just another common
6058 declaration of it.
6059
6060 Unfortunately some archivers (including GNU ar) will put
6061 declarations of common symbols into their archive maps, as
6062 well as real definitions, so we cannot just go by the archive
6063 map alone. Instead we must read in the element's symbol
6064 table and check that to see what kind of symbol definition
6065 this is. */
6066 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
6067 continue;
6068 }
6069 else
6070 {
6071 if (h->type != bfd_link_hash_undefweak)
6072 /* Symbol must be defined. Don't check it again. */
6073 included[i] = true;
6074 continue;
6075 }
6076
6077 /* We need to include this archive member. */
6078 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset,
6079 info);
6080 if (element == NULL)
6081 goto error_return;
6082
6083 if (! bfd_check_format (element, bfd_object))
6084 goto error_return;
6085
6086 undefs_tail = info->hash->undefs_tail;
6087
6088 if (!(*info->callbacks
6089 ->add_archive_element) (info, element, symdef->name, &element))
6090 continue;
6091 if (!bfd_link_add_symbols (element, info))
6092 goto error_return;
6093
6094 /* If there are any new undefined symbols, we need to make
6095 another pass through the archive in order to see whether
6096 they can be defined. FIXME: This isn't perfect, because
6097 common symbols wind up on undefs_tail and because an
6098 undefined symbol which is defined later on in this pass
6099 does not require another pass. This isn't a bug, but it
6100 does make the code less efficient than it could be. */
6101 if (undefs_tail != info->hash->undefs_tail)
6102 loop = true;
6103
6104 /* Look backward to mark all symbols from this object file
6105 which we have already seen in this pass. */
6106 mark = i;
6107 do
6108 {
6109 included[mark] = true;
6110 if (mark == 0)
6111 break;
6112 --mark;
6113 }
6114 while (symdefs[mark].file_offset == symdef->file_offset);
6115
6116 /* We mark subsequent symbols from this object file as we go
6117 on through the loop. */
6118 last = symdef->file_offset;
6119 }
6120 }
6121 while (loop);
6122
6123 free (included);
6124 return true;
6125
6126 error_return:
6127 free (included);
6128 return false;
6129 }
6130
6131 /* Given an ELF BFD, add symbols to the global hash table as
6132 appropriate. */
6133
6134 bool
6135 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
6136 {
6137 switch (bfd_get_format (abfd))
6138 {
6139 case bfd_object:
6140 return elf_link_add_object_symbols (abfd, info);
6141 case bfd_archive:
6142 return elf_link_add_archive_symbols (abfd, info);
6143 default:
6144 bfd_set_error (bfd_error_wrong_format);
6145 return false;
6146 }
6147 }
6148 \f
6149 struct hash_codes_info
6150 {
6151 unsigned long *hashcodes;
6152 bool error;
6153 };
6154
6155 /* This function will be called though elf_link_hash_traverse to store
6156 all hash value of the exported symbols in an array. */
6157
6158 static bool
6159 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
6160 {
6161 struct hash_codes_info *inf = (struct hash_codes_info *) data;
6162 const char *name;
6163 unsigned long ha;
6164 char *alc = NULL;
6165
6166 /* Ignore indirect symbols. These are added by the versioning code. */
6167 if (h->dynindx == -1)
6168 return true;
6169
6170 name = h->root.root.string;
6171 if (h->versioned >= versioned)
6172 {
6173 char *p = strchr (name, ELF_VER_CHR);
6174 if (p != NULL)
6175 {
6176 alc = (char *) bfd_malloc (p - name + 1);
6177 if (alc == NULL)
6178 {
6179 inf->error = true;
6180 return false;
6181 }
6182 memcpy (alc, name, p - name);
6183 alc[p - name] = '\0';
6184 name = alc;
6185 }
6186 }
6187
6188 /* Compute the hash value. */
6189 ha = bfd_elf_hash (name);
6190
6191 /* Store the found hash value in the array given as the argument. */
6192 *(inf->hashcodes)++ = ha;
6193
6194 /* And store it in the struct so that we can put it in the hash table
6195 later. */
6196 h->u.elf_hash_value = ha;
6197
6198 free (alc);
6199 return true;
6200 }
6201
6202 struct collect_gnu_hash_codes
6203 {
6204 bfd *output_bfd;
6205 const struct elf_backend_data *bed;
6206 unsigned long int nsyms;
6207 unsigned long int maskbits;
6208 unsigned long int *hashcodes;
6209 unsigned long int *hashval;
6210 unsigned long int *indx;
6211 unsigned long int *counts;
6212 bfd_vma *bitmask;
6213 bfd_byte *contents;
6214 bfd_size_type xlat;
6215 long int min_dynindx;
6216 unsigned long int bucketcount;
6217 unsigned long int symindx;
6218 long int local_indx;
6219 long int shift1, shift2;
6220 unsigned long int mask;
6221 bool error;
6222 };
6223
6224 /* This function will be called though elf_link_hash_traverse to store
6225 all hash value of the exported symbols in an array. */
6226
6227 static bool
6228 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
6229 {
6230 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6231 const char *name;
6232 unsigned long ha;
6233 char *alc = NULL;
6234
6235 /* Ignore indirect symbols. These are added by the versioning code. */
6236 if (h->dynindx == -1)
6237 return true;
6238
6239 /* Ignore also local symbols and undefined symbols. */
6240 if (! (*s->bed->elf_hash_symbol) (h))
6241 return true;
6242
6243 name = h->root.root.string;
6244 if (h->versioned >= versioned)
6245 {
6246 char *p = strchr (name, ELF_VER_CHR);
6247 if (p != NULL)
6248 {
6249 alc = (char *) bfd_malloc (p - name + 1);
6250 if (alc == NULL)
6251 {
6252 s->error = true;
6253 return false;
6254 }
6255 memcpy (alc, name, p - name);
6256 alc[p - name] = '\0';
6257 name = alc;
6258 }
6259 }
6260
6261 /* Compute the hash value. */
6262 ha = bfd_elf_gnu_hash (name);
6263
6264 /* Store the found hash value in the array for compute_bucket_count,
6265 and also for .dynsym reordering purposes. */
6266 s->hashcodes[s->nsyms] = ha;
6267 s->hashval[h->dynindx] = ha;
6268 ++s->nsyms;
6269 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
6270 s->min_dynindx = h->dynindx;
6271
6272 free (alc);
6273 return true;
6274 }
6275
6276 /* This function will be called though elf_link_hash_traverse to do
6277 final dynamic symbol renumbering in case of .gnu.hash.
6278 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6279 to the translation table. */
6280
6281 static bool
6282 elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data)
6283 {
6284 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6285 unsigned long int bucket;
6286 unsigned long int val;
6287
6288 /* Ignore indirect symbols. */
6289 if (h->dynindx == -1)
6290 return true;
6291
6292 /* Ignore also local symbols and undefined symbols. */
6293 if (! (*s->bed->elf_hash_symbol) (h))
6294 {
6295 if (h->dynindx >= s->min_dynindx)
6296 {
6297 if (s->bed->record_xhash_symbol != NULL)
6298 {
6299 (*s->bed->record_xhash_symbol) (h, 0);
6300 s->local_indx++;
6301 }
6302 else
6303 h->dynindx = s->local_indx++;
6304 }
6305 return true;
6306 }
6307
6308 bucket = s->hashval[h->dynindx] % s->bucketcount;
6309 val = (s->hashval[h->dynindx] >> s->shift1)
6310 & ((s->maskbits >> s->shift1) - 1);
6311 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
6312 s->bitmask[val]
6313 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
6314 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
6315 if (s->counts[bucket] == 1)
6316 /* Last element terminates the chain. */
6317 val |= 1;
6318 bfd_put_32 (s->output_bfd, val,
6319 s->contents + (s->indx[bucket] - s->symindx) * 4);
6320 --s->counts[bucket];
6321 if (s->bed->record_xhash_symbol != NULL)
6322 {
6323 bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4;
6324
6325 (*s->bed->record_xhash_symbol) (h, xlat_loc);
6326 }
6327 else
6328 h->dynindx = s->indx[bucket]++;
6329 return true;
6330 }
6331
6332 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6333
6334 bool
6335 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
6336 {
6337 return !(h->forced_local
6338 || h->root.type == bfd_link_hash_undefined
6339 || h->root.type == bfd_link_hash_undefweak
6340 || ((h->root.type == bfd_link_hash_defined
6341 || h->root.type == bfd_link_hash_defweak)
6342 && h->root.u.def.section->output_section == NULL));
6343 }
6344
6345 /* Array used to determine the number of hash table buckets to use
6346 based on the number of symbols there are. If there are fewer than
6347 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6348 fewer than 37 we use 17 buckets, and so forth. We never use more
6349 than 32771 buckets. */
6350
6351 static const size_t elf_buckets[] =
6352 {
6353 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6354 16411, 32771, 0
6355 };
6356
6357 /* Compute bucket count for hashing table. We do not use a static set
6358 of possible tables sizes anymore. Instead we determine for all
6359 possible reasonable sizes of the table the outcome (i.e., the
6360 number of collisions etc) and choose the best solution. The
6361 weighting functions are not too simple to allow the table to grow
6362 without bounds. Instead one of the weighting factors is the size.
6363 Therefore the result is always a good payoff between few collisions
6364 (= short chain lengths) and table size. */
6365 static size_t
6366 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
6367 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
6368 unsigned long int nsyms,
6369 int gnu_hash)
6370 {
6371 size_t best_size = 0;
6372 unsigned long int i;
6373
6374 if (info->optimize)
6375 {
6376 size_t minsize;
6377 size_t maxsize;
6378 uint64_t best_chlen = ~((uint64_t) 0);
6379 bfd *dynobj = elf_hash_table (info)->dynobj;
6380 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
6381 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
6382 unsigned long int *counts;
6383 bfd_size_type amt;
6384 unsigned int no_improvement_count = 0;
6385
6386 /* Possible optimization parameters: if we have NSYMS symbols we say
6387 that the hashing table must at least have NSYMS/4 and at most
6388 2*NSYMS buckets. */
6389 minsize = nsyms / 4;
6390 if (minsize == 0)
6391 minsize = 1;
6392 best_size = maxsize = nsyms * 2;
6393 if (gnu_hash)
6394 {
6395 if (minsize < 2)
6396 minsize = 2;
6397 if ((best_size & 31) == 0)
6398 ++best_size;
6399 }
6400
6401 /* Create array where we count the collisions in. We must use bfd_malloc
6402 since the size could be large. */
6403 amt = maxsize;
6404 amt *= sizeof (unsigned long int);
6405 counts = (unsigned long int *) bfd_malloc (amt);
6406 if (counts == NULL)
6407 return 0;
6408
6409 /* Compute the "optimal" size for the hash table. The criteria is a
6410 minimal chain length. The minor criteria is (of course) the size
6411 of the table. */
6412 for (i = minsize; i < maxsize; ++i)
6413 {
6414 /* Walk through the array of hashcodes and count the collisions. */
6415 uint64_t max;
6416 unsigned long int j;
6417 unsigned long int fact;
6418
6419 if (gnu_hash && (i & 31) == 0)
6420 continue;
6421
6422 memset (counts, '\0', i * sizeof (unsigned long int));
6423
6424 /* Determine how often each hash bucket is used. */
6425 for (j = 0; j < nsyms; ++j)
6426 ++counts[hashcodes[j] % i];
6427
6428 /* For the weight function we need some information about the
6429 pagesize on the target. This is information need not be 100%
6430 accurate. Since this information is not available (so far) we
6431 define it here to a reasonable default value. If it is crucial
6432 to have a better value some day simply define this value. */
6433 # ifndef BFD_TARGET_PAGESIZE
6434 # define BFD_TARGET_PAGESIZE (4096)
6435 # endif
6436
6437 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6438 and the chains. */
6439 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
6440
6441 # if 1
6442 /* Variant 1: optimize for short chains. We add the squares
6443 of all the chain lengths (which favors many small chain
6444 over a few long chains). */
6445 for (j = 0; j < i; ++j)
6446 max += counts[j] * counts[j];
6447
6448 /* This adds penalties for the overall size of the table. */
6449 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6450 max *= fact * fact;
6451 # else
6452 /* Variant 2: Optimize a lot more for small table. Here we
6453 also add squares of the size but we also add penalties for
6454 empty slots (the +1 term). */
6455 for (j = 0; j < i; ++j)
6456 max += (1 + counts[j]) * (1 + counts[j]);
6457
6458 /* The overall size of the table is considered, but not as
6459 strong as in variant 1, where it is squared. */
6460 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6461 max *= fact;
6462 # endif
6463
6464 /* Compare with current best results. */
6465 if (max < best_chlen)
6466 {
6467 best_chlen = max;
6468 best_size = i;
6469 no_improvement_count = 0;
6470 }
6471 /* PR 11843: Avoid futile long searches for the best bucket size
6472 when there are a large number of symbols. */
6473 else if (++no_improvement_count == 100)
6474 break;
6475 }
6476
6477 free (counts);
6478 }
6479 else
6480 {
6481 for (i = 0; elf_buckets[i] != 0; i++)
6482 {
6483 best_size = elf_buckets[i];
6484 if (nsyms < elf_buckets[i + 1])
6485 break;
6486 }
6487 if (gnu_hash && best_size < 2)
6488 best_size = 2;
6489 }
6490
6491 return best_size;
6492 }
6493
6494 /* Size any SHT_GROUP section for ld -r. */
6495
6496 bool
6497 _bfd_elf_size_group_sections (struct bfd_link_info *info)
6498 {
6499 bfd *ibfd;
6500 asection *s;
6501
6502 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6503 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6504 && (s = ibfd->sections) != NULL
6505 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
6506 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6507 return false;
6508 return true;
6509 }
6510
6511 /* Set a default stack segment size. The value in INFO wins. If it
6512 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6513 undefined it is initialized. */
6514
6515 bool
6516 bfd_elf_stack_segment_size (bfd *output_bfd,
6517 struct bfd_link_info *info,
6518 const char *legacy_symbol,
6519 bfd_vma default_size)
6520 {
6521 struct elf_link_hash_entry *h = NULL;
6522
6523 /* Look for legacy symbol. */
6524 if (legacy_symbol)
6525 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6526 false, false, false);
6527 if (h && (h->root.type == bfd_link_hash_defined
6528 || h->root.type == bfd_link_hash_defweak)
6529 && h->def_regular
6530 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6531 {
6532 /* The symbol has no type if specified on the command line. */
6533 h->type = STT_OBJECT;
6534 if (info->stacksize)
6535 /* xgettext:c-format */
6536 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6537 output_bfd, legacy_symbol);
6538 else if (h->root.u.def.section != bfd_abs_section_ptr)
6539 /* xgettext:c-format */
6540 _bfd_error_handler (_("%pB: %s not absolute"),
6541 output_bfd, legacy_symbol);
6542 else
6543 info->stacksize = h->root.u.def.value;
6544 }
6545
6546 if (!info->stacksize)
6547 /* If the user didn't set a size, or explicitly inhibit the
6548 size, set it now. */
6549 info->stacksize = default_size;
6550
6551 /* Provide the legacy symbol, if it is referenced. */
6552 if (h && (h->root.type == bfd_link_hash_undefined
6553 || h->root.type == bfd_link_hash_undefweak))
6554 {
6555 struct bfd_link_hash_entry *bh = NULL;
6556
6557 if (!(_bfd_generic_link_add_one_symbol
6558 (info, output_bfd, legacy_symbol,
6559 BSF_GLOBAL, bfd_abs_section_ptr,
6560 info->stacksize >= 0 ? info->stacksize : 0,
6561 NULL, false, get_elf_backend_data (output_bfd)->collect, &bh)))
6562 return false;
6563
6564 h = (struct elf_link_hash_entry *) bh;
6565 h->def_regular = 1;
6566 h->type = STT_OBJECT;
6567 }
6568
6569 return true;
6570 }
6571
6572 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6573
6574 struct elf_gc_sweep_symbol_info
6575 {
6576 struct bfd_link_info *info;
6577 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6578 bool);
6579 };
6580
6581 static bool
6582 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6583 {
6584 if (!h->mark
6585 && (((h->root.type == bfd_link_hash_defined
6586 || h->root.type == bfd_link_hash_defweak)
6587 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6588 && h->root.u.def.section->gc_mark))
6589 || h->root.type == bfd_link_hash_undefined
6590 || h->root.type == bfd_link_hash_undefweak))
6591 {
6592 struct elf_gc_sweep_symbol_info *inf;
6593
6594 inf = (struct elf_gc_sweep_symbol_info *) data;
6595 (*inf->hide_symbol) (inf->info, h, true);
6596 h->def_regular = 0;
6597 h->ref_regular = 0;
6598 h->ref_regular_nonweak = 0;
6599 }
6600
6601 return true;
6602 }
6603
6604 /* Set up the sizes and contents of the ELF dynamic sections. This is
6605 called by the ELF linker emulation before_allocation routine. We
6606 must set the sizes of the sections before the linker sets the
6607 addresses of the various sections. */
6608
6609 bool
6610 bfd_elf_size_dynamic_sections (bfd *output_bfd,
6611 const char *soname,
6612 const char *rpath,
6613 const char *filter_shlib,
6614 const char *audit,
6615 const char *depaudit,
6616 const char * const *auxiliary_filters,
6617 struct bfd_link_info *info,
6618 asection **sinterpptr)
6619 {
6620 bfd *dynobj;
6621 const struct elf_backend_data *bed;
6622
6623 *sinterpptr = NULL;
6624
6625 if (!is_elf_hash_table (info->hash))
6626 return true;
6627
6628 /* Any syms created from now on start with -1 in
6629 got.refcount/offset and plt.refcount/offset. */
6630 elf_hash_table (info)->init_got_refcount
6631 = elf_hash_table (info)->init_got_offset;
6632 elf_hash_table (info)->init_plt_refcount
6633 = elf_hash_table (info)->init_plt_offset;
6634
6635 bed = get_elf_backend_data (output_bfd);
6636
6637 /* The backend may have to create some sections regardless of whether
6638 we're dynamic or not. */
6639 if (bed->elf_backend_always_size_sections
6640 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6641 return false;
6642
6643 dynobj = elf_hash_table (info)->dynobj;
6644
6645 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6646 {
6647 struct bfd_elf_version_tree *verdefs;
6648 struct elf_info_failed asvinfo;
6649 struct bfd_elf_version_tree *t;
6650 struct bfd_elf_version_expr *d;
6651 asection *s;
6652 size_t soname_indx;
6653
6654 /* If we are supposed to export all symbols into the dynamic symbol
6655 table (this is not the normal case), then do so. */
6656 if (info->export_dynamic
6657 || (bfd_link_executable (info) && info->dynamic))
6658 {
6659 struct elf_info_failed eif;
6660
6661 eif.info = info;
6662 eif.failed = false;
6663 elf_link_hash_traverse (elf_hash_table (info),
6664 _bfd_elf_export_symbol,
6665 &eif);
6666 if (eif.failed)
6667 return false;
6668 }
6669
6670 if (soname != NULL)
6671 {
6672 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6673 soname, true);
6674 if (soname_indx == (size_t) -1
6675 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6676 return false;
6677 }
6678 else
6679 soname_indx = (size_t) -1;
6680
6681 /* Make all global versions with definition. */
6682 for (t = info->version_info; t != NULL; t = t->next)
6683 for (d = t->globals.list; d != NULL; d = d->next)
6684 if (!d->symver && d->literal)
6685 {
6686 const char *verstr, *name;
6687 size_t namelen, verlen, newlen;
6688 char *newname, *p, leading_char;
6689 struct elf_link_hash_entry *newh;
6690
6691 leading_char = bfd_get_symbol_leading_char (output_bfd);
6692 name = d->pattern;
6693 namelen = strlen (name) + (leading_char != '\0');
6694 verstr = t->name;
6695 verlen = strlen (verstr);
6696 newlen = namelen + verlen + 3;
6697
6698 newname = (char *) bfd_malloc (newlen);
6699 if (newname == NULL)
6700 return false;
6701 newname[0] = leading_char;
6702 memcpy (newname + (leading_char != '\0'), name, namelen);
6703
6704 /* Check the hidden versioned definition. */
6705 p = newname + namelen;
6706 *p++ = ELF_VER_CHR;
6707 memcpy (p, verstr, verlen + 1);
6708 newh = elf_link_hash_lookup (elf_hash_table (info),
6709 newname, false, false,
6710 false);
6711 if (newh == NULL
6712 || (newh->root.type != bfd_link_hash_defined
6713 && newh->root.type != bfd_link_hash_defweak))
6714 {
6715 /* Check the default versioned definition. */
6716 *p++ = ELF_VER_CHR;
6717 memcpy (p, verstr, verlen + 1);
6718 newh = elf_link_hash_lookup (elf_hash_table (info),
6719 newname, false, false,
6720 false);
6721 }
6722 free (newname);
6723
6724 /* Mark this version if there is a definition and it is
6725 not defined in a shared object. */
6726 if (newh != NULL
6727 && !newh->def_dynamic
6728 && (newh->root.type == bfd_link_hash_defined
6729 || newh->root.type == bfd_link_hash_defweak))
6730 d->symver = 1;
6731 }
6732
6733 /* Attach all the symbols to their version information. */
6734 asvinfo.info = info;
6735 asvinfo.failed = false;
6736
6737 elf_link_hash_traverse (elf_hash_table (info),
6738 _bfd_elf_link_assign_sym_version,
6739 &asvinfo);
6740 if (asvinfo.failed)
6741 return false;
6742
6743 if (!info->allow_undefined_version)
6744 {
6745 /* Check if all global versions have a definition. */
6746 bool all_defined = true;
6747 for (t = info->version_info; t != NULL; t = t->next)
6748 for (d = t->globals.list; d != NULL; d = d->next)
6749 if (d->literal && !d->symver && !d->script)
6750 {
6751 _bfd_error_handler
6752 (_("%s: undefined version: %s"),
6753 d->pattern, t->name);
6754 all_defined = false;
6755 }
6756
6757 if (!all_defined)
6758 {
6759 bfd_set_error (bfd_error_bad_value);
6760 return false;
6761 }
6762 }
6763
6764 /* Set up the version definition section. */
6765 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6766 BFD_ASSERT (s != NULL);
6767
6768 /* We may have created additional version definitions if we are
6769 just linking a regular application. */
6770 verdefs = info->version_info;
6771
6772 /* Skip anonymous version tag. */
6773 if (verdefs != NULL && verdefs->vernum == 0)
6774 verdefs = verdefs->next;
6775
6776 if (verdefs == NULL && !info->create_default_symver)
6777 s->flags |= SEC_EXCLUDE;
6778 else
6779 {
6780 unsigned int cdefs;
6781 bfd_size_type size;
6782 bfd_byte *p;
6783 Elf_Internal_Verdef def;
6784 Elf_Internal_Verdaux defaux;
6785 struct bfd_link_hash_entry *bh;
6786 struct elf_link_hash_entry *h;
6787 const char *name;
6788
6789 cdefs = 0;
6790 size = 0;
6791
6792 /* Make space for the base version. */
6793 size += sizeof (Elf_External_Verdef);
6794 size += sizeof (Elf_External_Verdaux);
6795 ++cdefs;
6796
6797 /* Make space for the default version. */
6798 if (info->create_default_symver)
6799 {
6800 size += sizeof (Elf_External_Verdef);
6801 ++cdefs;
6802 }
6803
6804 for (t = verdefs; t != NULL; t = t->next)
6805 {
6806 struct bfd_elf_version_deps *n;
6807
6808 /* Don't emit base version twice. */
6809 if (t->vernum == 0)
6810 continue;
6811
6812 size += sizeof (Elf_External_Verdef);
6813 size += sizeof (Elf_External_Verdaux);
6814 ++cdefs;
6815
6816 for (n = t->deps; n != NULL; n = n->next)
6817 size += sizeof (Elf_External_Verdaux);
6818 }
6819
6820 s->size = size;
6821 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6822 if (s->contents == NULL && s->size != 0)
6823 return false;
6824
6825 /* Fill in the version definition section. */
6826
6827 p = s->contents;
6828
6829 def.vd_version = VER_DEF_CURRENT;
6830 def.vd_flags = VER_FLG_BASE;
6831 def.vd_ndx = 1;
6832 def.vd_cnt = 1;
6833 if (info->create_default_symver)
6834 {
6835 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6836 def.vd_next = sizeof (Elf_External_Verdef);
6837 }
6838 else
6839 {
6840 def.vd_aux = sizeof (Elf_External_Verdef);
6841 def.vd_next = (sizeof (Elf_External_Verdef)
6842 + sizeof (Elf_External_Verdaux));
6843 }
6844
6845 if (soname_indx != (size_t) -1)
6846 {
6847 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6848 soname_indx);
6849 def.vd_hash = bfd_elf_hash (soname);
6850 defaux.vda_name = soname_indx;
6851 name = soname;
6852 }
6853 else
6854 {
6855 size_t indx;
6856
6857 name = lbasename (bfd_get_filename (output_bfd));
6858 def.vd_hash = bfd_elf_hash (name);
6859 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6860 name, false);
6861 if (indx == (size_t) -1)
6862 return false;
6863 defaux.vda_name = indx;
6864 }
6865 defaux.vda_next = 0;
6866
6867 _bfd_elf_swap_verdef_out (output_bfd, &def,
6868 (Elf_External_Verdef *) p);
6869 p += sizeof (Elf_External_Verdef);
6870 if (info->create_default_symver)
6871 {
6872 /* Add a symbol representing this version. */
6873 bh = NULL;
6874 if (! (_bfd_generic_link_add_one_symbol
6875 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6876 0, NULL, false,
6877 get_elf_backend_data (dynobj)->collect, &bh)))
6878 return false;
6879 h = (struct elf_link_hash_entry *) bh;
6880 h->non_elf = 0;
6881 h->def_regular = 1;
6882 h->type = STT_OBJECT;
6883 h->verinfo.vertree = NULL;
6884
6885 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6886 return false;
6887
6888 /* Create a duplicate of the base version with the same
6889 aux block, but different flags. */
6890 def.vd_flags = 0;
6891 def.vd_ndx = 2;
6892 def.vd_aux = sizeof (Elf_External_Verdef);
6893 if (verdefs)
6894 def.vd_next = (sizeof (Elf_External_Verdef)
6895 + sizeof (Elf_External_Verdaux));
6896 else
6897 def.vd_next = 0;
6898 _bfd_elf_swap_verdef_out (output_bfd, &def,
6899 (Elf_External_Verdef *) p);
6900 p += sizeof (Elf_External_Verdef);
6901 }
6902 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6903 (Elf_External_Verdaux *) p);
6904 p += sizeof (Elf_External_Verdaux);
6905
6906 for (t = verdefs; t != NULL; t = t->next)
6907 {
6908 unsigned int cdeps;
6909 struct bfd_elf_version_deps *n;
6910
6911 /* Don't emit the base version twice. */
6912 if (t->vernum == 0)
6913 continue;
6914
6915 cdeps = 0;
6916 for (n = t->deps; n != NULL; n = n->next)
6917 ++cdeps;
6918
6919 /* Add a symbol representing this version. */
6920 bh = NULL;
6921 if (! (_bfd_generic_link_add_one_symbol
6922 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6923 0, NULL, false,
6924 get_elf_backend_data (dynobj)->collect, &bh)))
6925 return false;
6926 h = (struct elf_link_hash_entry *) bh;
6927 h->non_elf = 0;
6928 h->def_regular = 1;
6929 h->type = STT_OBJECT;
6930 h->verinfo.vertree = t;
6931
6932 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6933 return false;
6934
6935 def.vd_version = VER_DEF_CURRENT;
6936 def.vd_flags = 0;
6937 if (t->globals.list == NULL
6938 && t->locals.list == NULL
6939 && ! t->used)
6940 def.vd_flags |= VER_FLG_WEAK;
6941 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6942 def.vd_cnt = cdeps + 1;
6943 def.vd_hash = bfd_elf_hash (t->name);
6944 def.vd_aux = sizeof (Elf_External_Verdef);
6945 def.vd_next = 0;
6946
6947 /* If a basever node is next, it *must* be the last node in
6948 the chain, otherwise Verdef construction breaks. */
6949 if (t->next != NULL && t->next->vernum == 0)
6950 BFD_ASSERT (t->next->next == NULL);
6951
6952 if (t->next != NULL && t->next->vernum != 0)
6953 def.vd_next = (sizeof (Elf_External_Verdef)
6954 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6955
6956 _bfd_elf_swap_verdef_out (output_bfd, &def,
6957 (Elf_External_Verdef *) p);
6958 p += sizeof (Elf_External_Verdef);
6959
6960 defaux.vda_name = h->dynstr_index;
6961 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6962 h->dynstr_index);
6963 defaux.vda_next = 0;
6964 if (t->deps != NULL)
6965 defaux.vda_next = sizeof (Elf_External_Verdaux);
6966 t->name_indx = defaux.vda_name;
6967
6968 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6969 (Elf_External_Verdaux *) p);
6970 p += sizeof (Elf_External_Verdaux);
6971
6972 for (n = t->deps; n != NULL; n = n->next)
6973 {
6974 if (n->version_needed == NULL)
6975 {
6976 /* This can happen if there was an error in the
6977 version script. */
6978 defaux.vda_name = 0;
6979 }
6980 else
6981 {
6982 defaux.vda_name = n->version_needed->name_indx;
6983 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6984 defaux.vda_name);
6985 }
6986 if (n->next == NULL)
6987 defaux.vda_next = 0;
6988 else
6989 defaux.vda_next = sizeof (Elf_External_Verdaux);
6990
6991 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6992 (Elf_External_Verdaux *) p);
6993 p += sizeof (Elf_External_Verdaux);
6994 }
6995 }
6996
6997 elf_tdata (output_bfd)->cverdefs = cdefs;
6998 }
6999 }
7000
7001 if (info->gc_sections && bed->can_gc_sections)
7002 {
7003 struct elf_gc_sweep_symbol_info sweep_info;
7004
7005 /* Remove the symbols that were in the swept sections from the
7006 dynamic symbol table. */
7007 sweep_info.info = info;
7008 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
7009 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
7010 &sweep_info);
7011 }
7012
7013 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7014 {
7015 asection *s;
7016 struct elf_find_verdep_info sinfo;
7017
7018 /* Work out the size of the version reference section. */
7019
7020 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
7021 BFD_ASSERT (s != NULL);
7022
7023 sinfo.info = info;
7024 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
7025 if (sinfo.vers == 0)
7026 sinfo.vers = 1;
7027 sinfo.failed = false;
7028
7029 elf_link_hash_traverse (elf_hash_table (info),
7030 _bfd_elf_link_find_version_dependencies,
7031 &sinfo);
7032 if (sinfo.failed)
7033 return false;
7034
7035 if (info->enable_dt_relr)
7036 {
7037 elf_link_add_dt_relr_dependency (&sinfo);
7038 if (sinfo.failed)
7039 return false;
7040 }
7041
7042 if (elf_tdata (output_bfd)->verref == NULL)
7043 s->flags |= SEC_EXCLUDE;
7044 else
7045 {
7046 Elf_Internal_Verneed *vn;
7047 unsigned int size;
7048 unsigned int crefs;
7049 bfd_byte *p;
7050
7051 /* Build the version dependency section. */
7052 size = 0;
7053 crefs = 0;
7054 for (vn = elf_tdata (output_bfd)->verref;
7055 vn != NULL;
7056 vn = vn->vn_nextref)
7057 {
7058 Elf_Internal_Vernaux *a;
7059
7060 size += sizeof (Elf_External_Verneed);
7061 ++crefs;
7062 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7063 size += sizeof (Elf_External_Vernaux);
7064 }
7065
7066 s->size = size;
7067 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7068 if (s->contents == NULL)
7069 return false;
7070
7071 p = s->contents;
7072 for (vn = elf_tdata (output_bfd)->verref;
7073 vn != NULL;
7074 vn = vn->vn_nextref)
7075 {
7076 unsigned int caux;
7077 Elf_Internal_Vernaux *a;
7078 size_t indx;
7079
7080 caux = 0;
7081 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7082 ++caux;
7083
7084 vn->vn_version = VER_NEED_CURRENT;
7085 vn->vn_cnt = caux;
7086 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7087 elf_dt_name (vn->vn_bfd) != NULL
7088 ? elf_dt_name (vn->vn_bfd)
7089 : lbasename (bfd_get_filename
7090 (vn->vn_bfd)),
7091 false);
7092 if (indx == (size_t) -1)
7093 return false;
7094 vn->vn_file = indx;
7095 vn->vn_aux = sizeof (Elf_External_Verneed);
7096 if (vn->vn_nextref == NULL)
7097 vn->vn_next = 0;
7098 else
7099 vn->vn_next = (sizeof (Elf_External_Verneed)
7100 + caux * sizeof (Elf_External_Vernaux));
7101
7102 _bfd_elf_swap_verneed_out (output_bfd, vn,
7103 (Elf_External_Verneed *) p);
7104 p += sizeof (Elf_External_Verneed);
7105
7106 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7107 {
7108 a->vna_hash = bfd_elf_hash (a->vna_nodename);
7109 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7110 a->vna_nodename, false);
7111 if (indx == (size_t) -1)
7112 return false;
7113 a->vna_name = indx;
7114 if (a->vna_nextptr == NULL)
7115 a->vna_next = 0;
7116 else
7117 a->vna_next = sizeof (Elf_External_Vernaux);
7118
7119 _bfd_elf_swap_vernaux_out (output_bfd, a,
7120 (Elf_External_Vernaux *) p);
7121 p += sizeof (Elf_External_Vernaux);
7122 }
7123 }
7124
7125 elf_tdata (output_bfd)->cverrefs = crefs;
7126 }
7127 }
7128
7129 if (bfd_link_relocatable (info)
7130 && !_bfd_elf_size_group_sections (info))
7131 return false;
7132
7133 /* Determine any GNU_STACK segment requirements, after the backend
7134 has had a chance to set a default segment size. */
7135 if (info->execstack)
7136 {
7137 /* If the user has explicitly requested warnings, then generate one even
7138 though the choice is the result of another command line option. */
7139 if (info->warn_execstack == 1)
7140 _bfd_error_handler
7141 (_("\
7142 warning: enabling an executable stack because of -z execstack command line option"));
7143 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
7144 }
7145 else if (info->noexecstack)
7146 elf_stack_flags (output_bfd) = PF_R | PF_W;
7147 else
7148 {
7149 bfd *inputobj;
7150 asection *notesec = NULL;
7151 bfd *noteobj = NULL;
7152 bfd *emptyobj = NULL;
7153 int exec = 0;
7154
7155 for (inputobj = info->input_bfds;
7156 inputobj;
7157 inputobj = inputobj->link.next)
7158 {
7159 asection *s;
7160
7161 if (inputobj->flags
7162 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
7163 continue;
7164 s = inputobj->sections;
7165 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7166 continue;
7167
7168 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
7169 if (s)
7170 {
7171 notesec = s;
7172 if (s->flags & SEC_CODE)
7173 {
7174 noteobj = inputobj;
7175 exec = PF_X;
7176 /* There is no point in scanning the remaining bfds. */
7177 break;
7178 }
7179 }
7180 else if (bed->default_execstack && info->default_execstack)
7181 {
7182 exec = PF_X;
7183 emptyobj = inputobj;
7184 }
7185 }
7186
7187 if (notesec || info->stacksize > 0)
7188 {
7189 if (exec)
7190 {
7191 if (info->warn_execstack != 0)
7192 {
7193 /* PR 29072: Because an executable stack is a serious
7194 security risk, make sure that the user knows that it is
7195 being enabled despite the fact that it was not requested
7196 on the command line. */
7197 if (noteobj)
7198 _bfd_error_handler (_("\
7199 warning: %s: requires executable stack (because the .note.GNU-stack section is executable)"),
7200 bfd_get_filename (noteobj));
7201 else if (emptyobj)
7202 {
7203 _bfd_error_handler (_("\
7204 warning: %s: missing .note.GNU-stack section implies executable stack"),
7205 bfd_get_filename (emptyobj));
7206 _bfd_error_handler (_("\
7207 NOTE: This behaviour is deprecated and will be removed in a future version of the linker"));
7208 }
7209 }
7210 }
7211 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
7212 }
7213
7214 if (notesec && exec && bfd_link_relocatable (info)
7215 && notesec->output_section != bfd_abs_section_ptr)
7216 notesec->output_section->flags |= SEC_CODE;
7217 }
7218
7219 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7220 {
7221 struct elf_info_failed eif;
7222 struct elf_link_hash_entry *h;
7223 asection *dynstr;
7224 asection *s;
7225
7226 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
7227 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
7228
7229 if (info->symbolic)
7230 {
7231 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
7232 return false;
7233 info->flags |= DF_SYMBOLIC;
7234 }
7235
7236 if (rpath != NULL)
7237 {
7238 size_t indx;
7239 bfd_vma tag;
7240
7241 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
7242 true);
7243 if (indx == (size_t) -1)
7244 return false;
7245
7246 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
7247 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
7248 return false;
7249 }
7250
7251 if (filter_shlib != NULL)
7252 {
7253 size_t indx;
7254
7255 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7256 filter_shlib, true);
7257 if (indx == (size_t) -1
7258 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
7259 return false;
7260 }
7261
7262 if (auxiliary_filters != NULL)
7263 {
7264 const char * const *p;
7265
7266 for (p = auxiliary_filters; *p != NULL; p++)
7267 {
7268 size_t indx;
7269
7270 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7271 *p, true);
7272 if (indx == (size_t) -1
7273 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
7274 return false;
7275 }
7276 }
7277
7278 if (audit != NULL)
7279 {
7280 size_t indx;
7281
7282 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
7283 true);
7284 if (indx == (size_t) -1
7285 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
7286 return false;
7287 }
7288
7289 if (depaudit != NULL)
7290 {
7291 size_t indx;
7292
7293 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
7294 true);
7295 if (indx == (size_t) -1
7296 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
7297 return false;
7298 }
7299
7300 eif.info = info;
7301 eif.failed = false;
7302
7303 /* Find all symbols which were defined in a dynamic object and make
7304 the backend pick a reasonable value for them. */
7305 elf_link_hash_traverse (elf_hash_table (info),
7306 _bfd_elf_adjust_dynamic_symbol,
7307 &eif);
7308 if (eif.failed)
7309 return false;
7310
7311 /* Add some entries to the .dynamic section. We fill in some of the
7312 values later, in bfd_elf_final_link, but we must add the entries
7313 now so that we know the final size of the .dynamic section. */
7314
7315 /* If there are initialization and/or finalization functions to
7316 call then add the corresponding DT_INIT/DT_FINI entries. */
7317 h = (info->init_function
7318 ? elf_link_hash_lookup (elf_hash_table (info),
7319 info->init_function, false,
7320 false, false)
7321 : NULL);
7322 if (h != NULL
7323 && (h->ref_regular
7324 || h->def_regular))
7325 {
7326 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
7327 return false;
7328 }
7329 h = (info->fini_function
7330 ? elf_link_hash_lookup (elf_hash_table (info),
7331 info->fini_function, false,
7332 false, false)
7333 : NULL);
7334 if (h != NULL
7335 && (h->ref_regular
7336 || h->def_regular))
7337 {
7338 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
7339 return false;
7340 }
7341
7342 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
7343 if (s != NULL && s->linker_has_input)
7344 {
7345 /* DT_PREINIT_ARRAY is not allowed in shared library. */
7346 if (! bfd_link_executable (info))
7347 {
7348 bfd *sub;
7349 asection *o;
7350
7351 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
7352 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
7353 && (o = sub->sections) != NULL
7354 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
7355 for (o = sub->sections; o != NULL; o = o->next)
7356 if (elf_section_data (o)->this_hdr.sh_type
7357 == SHT_PREINIT_ARRAY)
7358 {
7359 _bfd_error_handler
7360 (_("%pB: .preinit_array section is not allowed in DSO"),
7361 sub);
7362 break;
7363 }
7364
7365 bfd_set_error (bfd_error_nonrepresentable_section);
7366 return false;
7367 }
7368
7369 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
7370 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
7371 return false;
7372 }
7373 s = bfd_get_section_by_name (output_bfd, ".init_array");
7374 if (s != NULL && s->linker_has_input)
7375 {
7376 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
7377 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
7378 return false;
7379 }
7380 s = bfd_get_section_by_name (output_bfd, ".fini_array");
7381 if (s != NULL && s->linker_has_input)
7382 {
7383 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
7384 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
7385 return false;
7386 }
7387
7388 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
7389 /* If .dynstr is excluded from the link, we don't want any of
7390 these tags. Strictly, we should be checking each section
7391 individually; This quick check covers for the case where
7392 someone does a /DISCARD/ : { *(*) }. */
7393 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
7394 {
7395 bfd_size_type strsize;
7396
7397 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7398 if ((info->emit_hash
7399 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
7400 || (info->emit_gnu_hash
7401 && (bed->record_xhash_symbol == NULL
7402 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)))
7403 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
7404 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
7405 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
7406 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
7407 bed->s->sizeof_sym)
7408 || (info->gnu_flags_1
7409 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_FLAGS_1,
7410 info->gnu_flags_1)))
7411 return false;
7412 }
7413 }
7414
7415 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
7416 return false;
7417
7418 /* The backend must work out the sizes of all the other dynamic
7419 sections. */
7420 if (dynobj != NULL
7421 && bed->elf_backend_size_dynamic_sections != NULL
7422 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
7423 return false;
7424
7425 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7426 {
7427 if (elf_tdata (output_bfd)->cverdefs)
7428 {
7429 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
7430
7431 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
7432 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
7433 return false;
7434 }
7435
7436 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
7437 {
7438 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
7439 return false;
7440 }
7441 else if (info->flags & DF_BIND_NOW)
7442 {
7443 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
7444 return false;
7445 }
7446
7447 if (info->flags_1)
7448 {
7449 if (bfd_link_executable (info))
7450 info->flags_1 &= ~ (DF_1_INITFIRST
7451 | DF_1_NODELETE
7452 | DF_1_NOOPEN);
7453 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
7454 return false;
7455 }
7456
7457 if (elf_tdata (output_bfd)->cverrefs)
7458 {
7459 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
7460
7461 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
7462 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
7463 return false;
7464 }
7465
7466 if ((elf_tdata (output_bfd)->cverrefs == 0
7467 && elf_tdata (output_bfd)->cverdefs == 0)
7468 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
7469 {
7470 asection *s;
7471
7472 s = bfd_get_linker_section (dynobj, ".gnu.version");
7473 s->flags |= SEC_EXCLUDE;
7474 }
7475 }
7476 return true;
7477 }
7478
7479 /* Find the first non-excluded output section. We'll use its
7480 section symbol for some emitted relocs. */
7481 void
7482 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7483 {
7484 asection *s;
7485 asection *found = NULL;
7486
7487 for (s = output_bfd->sections; s != NULL; s = s->next)
7488 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7489 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7490 {
7491 found = s;
7492 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7493 break;
7494 }
7495 elf_hash_table (info)->text_index_section = found;
7496 }
7497
7498 /* Find two non-excluded output sections, one for code, one for data.
7499 We'll use their section symbols for some emitted relocs. */
7500 void
7501 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7502 {
7503 asection *s;
7504 asection *found = NULL;
7505
7506 /* Data first, since setting text_index_section changes
7507 _bfd_elf_omit_section_dynsym_default. */
7508 for (s = output_bfd->sections; s != NULL; s = s->next)
7509 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7510 && !(s->flags & SEC_READONLY)
7511 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7512 {
7513 found = s;
7514 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7515 break;
7516 }
7517 elf_hash_table (info)->data_index_section = found;
7518
7519 for (s = output_bfd->sections; s != NULL; s = s->next)
7520 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7521 && (s->flags & SEC_READONLY)
7522 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7523 {
7524 found = s;
7525 break;
7526 }
7527 elf_hash_table (info)->text_index_section = found;
7528 }
7529
7530 #define GNU_HASH_SECTION_NAME(bed) \
7531 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7532
7533 bool
7534 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7535 {
7536 const struct elf_backend_data *bed;
7537 unsigned long section_sym_count;
7538 bfd_size_type dynsymcount = 0;
7539
7540 if (!is_elf_hash_table (info->hash))
7541 return true;
7542
7543 bed = get_elf_backend_data (output_bfd);
7544 (*bed->elf_backend_init_index_section) (output_bfd, info);
7545
7546 /* Assign dynsym indices. In a shared library we generate a section
7547 symbol for each output section, which come first. Next come all
7548 of the back-end allocated local dynamic syms, followed by the rest
7549 of the global symbols.
7550
7551 This is usually not needed for static binaries, however backends
7552 can request to always do it, e.g. the MIPS backend uses dynamic
7553 symbol counts to lay out GOT, which will be produced in the
7554 presence of GOT relocations even in static binaries (holding fixed
7555 data in that case, to satisfy those relocations). */
7556
7557 if (elf_hash_table (info)->dynamic_sections_created
7558 || bed->always_renumber_dynsyms)
7559 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7560 &section_sym_count);
7561
7562 if (elf_hash_table (info)->dynamic_sections_created)
7563 {
7564 bfd *dynobj;
7565 asection *s;
7566 unsigned int dtagcount;
7567
7568 dynobj = elf_hash_table (info)->dynobj;
7569
7570 /* Work out the size of the symbol version section. */
7571 s = bfd_get_linker_section (dynobj, ".gnu.version");
7572 BFD_ASSERT (s != NULL);
7573 if ((s->flags & SEC_EXCLUDE) == 0)
7574 {
7575 s->size = dynsymcount * sizeof (Elf_External_Versym);
7576 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7577 if (s->contents == NULL)
7578 return false;
7579
7580 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7581 return false;
7582 }
7583
7584 /* Set the size of the .dynsym and .hash sections. We counted
7585 the number of dynamic symbols in elf_link_add_object_symbols.
7586 We will build the contents of .dynsym and .hash when we build
7587 the final symbol table, because until then we do not know the
7588 correct value to give the symbols. We built the .dynstr
7589 section as we went along in elf_link_add_object_symbols. */
7590 s = elf_hash_table (info)->dynsym;
7591 BFD_ASSERT (s != NULL);
7592 s->size = dynsymcount * bed->s->sizeof_sym;
7593
7594 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7595 if (s->contents == NULL)
7596 return false;
7597
7598 /* The first entry in .dynsym is a dummy symbol. Clear all the
7599 section syms, in case we don't output them all. */
7600 ++section_sym_count;
7601 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
7602
7603 elf_hash_table (info)->bucketcount = 0;
7604
7605 /* Compute the size of the hashing table. As a side effect this
7606 computes the hash values for all the names we export. */
7607 if (info->emit_hash)
7608 {
7609 unsigned long int *hashcodes;
7610 struct hash_codes_info hashinf;
7611 bfd_size_type amt;
7612 unsigned long int nsyms;
7613 size_t bucketcount;
7614 size_t hash_entry_size;
7615
7616 /* Compute the hash values for all exported symbols. At the same
7617 time store the values in an array so that we could use them for
7618 optimizations. */
7619 amt = dynsymcount * sizeof (unsigned long int);
7620 hashcodes = (unsigned long int *) bfd_malloc (amt);
7621 if (hashcodes == NULL)
7622 return false;
7623 hashinf.hashcodes = hashcodes;
7624 hashinf.error = false;
7625
7626 /* Put all hash values in HASHCODES. */
7627 elf_link_hash_traverse (elf_hash_table (info),
7628 elf_collect_hash_codes, &hashinf);
7629 if (hashinf.error)
7630 {
7631 free (hashcodes);
7632 return false;
7633 }
7634
7635 nsyms = hashinf.hashcodes - hashcodes;
7636 bucketcount
7637 = compute_bucket_count (info, hashcodes, nsyms, 0);
7638 free (hashcodes);
7639
7640 if (bucketcount == 0 && nsyms > 0)
7641 return false;
7642
7643 elf_hash_table (info)->bucketcount = bucketcount;
7644
7645 s = bfd_get_linker_section (dynobj, ".hash");
7646 BFD_ASSERT (s != NULL);
7647 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7648 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
7649 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7650 if (s->contents == NULL)
7651 return false;
7652
7653 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7654 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7655 s->contents + hash_entry_size);
7656 }
7657
7658 if (info->emit_gnu_hash)
7659 {
7660 size_t i, cnt;
7661 unsigned char *contents;
7662 struct collect_gnu_hash_codes cinfo;
7663 bfd_size_type amt;
7664 size_t bucketcount;
7665
7666 memset (&cinfo, 0, sizeof (cinfo));
7667
7668 /* Compute the hash values for all exported symbols. At the same
7669 time store the values in an array so that we could use them for
7670 optimizations. */
7671 amt = dynsymcount * 2 * sizeof (unsigned long int);
7672 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
7673 if (cinfo.hashcodes == NULL)
7674 return false;
7675
7676 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7677 cinfo.min_dynindx = -1;
7678 cinfo.output_bfd = output_bfd;
7679 cinfo.bed = bed;
7680
7681 /* Put all hash values in HASHCODES. */
7682 elf_link_hash_traverse (elf_hash_table (info),
7683 elf_collect_gnu_hash_codes, &cinfo);
7684 if (cinfo.error)
7685 {
7686 free (cinfo.hashcodes);
7687 return false;
7688 }
7689
7690 bucketcount
7691 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7692
7693 if (bucketcount == 0)
7694 {
7695 free (cinfo.hashcodes);
7696 return false;
7697 }
7698
7699 s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (bed));
7700 BFD_ASSERT (s != NULL);
7701
7702 if (cinfo.nsyms == 0)
7703 {
7704 /* Empty .gnu.hash or .MIPS.xhash section is special. */
7705 BFD_ASSERT (cinfo.min_dynindx == -1);
7706 free (cinfo.hashcodes);
7707 s->size = 5 * 4 + bed->s->arch_size / 8;
7708 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7709 if (contents == NULL)
7710 return false;
7711 s->contents = contents;
7712 /* 1 empty bucket. */
7713 bfd_put_32 (output_bfd, 1, contents);
7714 /* SYMIDX above the special symbol 0. */
7715 bfd_put_32 (output_bfd, 1, contents + 4);
7716 /* Just one word for bitmask. */
7717 bfd_put_32 (output_bfd, 1, contents + 8);
7718 /* Only hash fn bloom filter. */
7719 bfd_put_32 (output_bfd, 0, contents + 12);
7720 /* No hashes are valid - empty bitmask. */
7721 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7722 /* No hashes in the only bucket. */
7723 bfd_put_32 (output_bfd, 0,
7724 contents + 16 + bed->s->arch_size / 8);
7725 }
7726 else
7727 {
7728 unsigned long int maskwords, maskbitslog2, x;
7729 BFD_ASSERT (cinfo.min_dynindx != -1);
7730
7731 x = cinfo.nsyms;
7732 maskbitslog2 = 1;
7733 while ((x >>= 1) != 0)
7734 ++maskbitslog2;
7735 if (maskbitslog2 < 3)
7736 maskbitslog2 = 5;
7737 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7738 maskbitslog2 = maskbitslog2 + 3;
7739 else
7740 maskbitslog2 = maskbitslog2 + 2;
7741 if (bed->s->arch_size == 64)
7742 {
7743 if (maskbitslog2 == 5)
7744 maskbitslog2 = 6;
7745 cinfo.shift1 = 6;
7746 }
7747 else
7748 cinfo.shift1 = 5;
7749 cinfo.mask = (1 << cinfo.shift1) - 1;
7750 cinfo.shift2 = maskbitslog2;
7751 cinfo.maskbits = 1 << maskbitslog2;
7752 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7753 amt = bucketcount * sizeof (unsigned long int) * 2;
7754 amt += maskwords * sizeof (bfd_vma);
7755 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
7756 if (cinfo.bitmask == NULL)
7757 {
7758 free (cinfo.hashcodes);
7759 return false;
7760 }
7761
7762 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
7763 cinfo.indx = cinfo.counts + bucketcount;
7764 cinfo.symindx = dynsymcount - cinfo.nsyms;
7765 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7766
7767 /* Determine how often each hash bucket is used. */
7768 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7769 for (i = 0; i < cinfo.nsyms; ++i)
7770 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7771
7772 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7773 if (cinfo.counts[i] != 0)
7774 {
7775 cinfo.indx[i] = cnt;
7776 cnt += cinfo.counts[i];
7777 }
7778 BFD_ASSERT (cnt == dynsymcount);
7779 cinfo.bucketcount = bucketcount;
7780 cinfo.local_indx = cinfo.min_dynindx;
7781
7782 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7783 s->size += cinfo.maskbits / 8;
7784 if (bed->record_xhash_symbol != NULL)
7785 s->size += cinfo.nsyms * 4;
7786 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7787 if (contents == NULL)
7788 {
7789 free (cinfo.bitmask);
7790 free (cinfo.hashcodes);
7791 return false;
7792 }
7793
7794 s->contents = contents;
7795 bfd_put_32 (output_bfd, bucketcount, contents);
7796 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7797 bfd_put_32 (output_bfd, maskwords, contents + 8);
7798 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7799 contents += 16 + cinfo.maskbits / 8;
7800
7801 for (i = 0; i < bucketcount; ++i)
7802 {
7803 if (cinfo.counts[i] == 0)
7804 bfd_put_32 (output_bfd, 0, contents);
7805 else
7806 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7807 contents += 4;
7808 }
7809
7810 cinfo.contents = contents;
7811
7812 cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents;
7813 /* Renumber dynamic symbols, if populating .gnu.hash section.
7814 If using .MIPS.xhash, populate the translation table. */
7815 elf_link_hash_traverse (elf_hash_table (info),
7816 elf_gnu_hash_process_symidx, &cinfo);
7817
7818 contents = s->contents + 16;
7819 for (i = 0; i < maskwords; ++i)
7820 {
7821 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7822 contents);
7823 contents += bed->s->arch_size / 8;
7824 }
7825
7826 free (cinfo.bitmask);
7827 free (cinfo.hashcodes);
7828 }
7829 }
7830
7831 s = bfd_get_linker_section (dynobj, ".dynstr");
7832 BFD_ASSERT (s != NULL);
7833
7834 elf_finalize_dynstr (output_bfd, info);
7835
7836 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7837
7838 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7839 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7840 return false;
7841 }
7842
7843 return true;
7844 }
7845 \f
7846 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7847
7848 static void
7849 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7850 asection *sec)
7851 {
7852 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7853 sec->sec_info_type = SEC_INFO_TYPE_NONE;
7854 }
7855
7856 /* Finish SHF_MERGE section merging. */
7857
7858 bool
7859 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
7860 {
7861 bfd *ibfd;
7862 asection *sec;
7863
7864 if (!is_elf_hash_table (info->hash))
7865 return false;
7866
7867 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7868 if ((ibfd->flags & DYNAMIC) == 0
7869 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
7870 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7871 == get_elf_backend_data (obfd)->s->elfclass))
7872 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7873 if ((sec->flags & SEC_MERGE) != 0
7874 && !bfd_is_abs_section (sec->output_section))
7875 {
7876 struct bfd_elf_section_data *secdata;
7877
7878 secdata = elf_section_data (sec);
7879 if (! _bfd_add_merge_section (obfd,
7880 &elf_hash_table (info)->merge_info,
7881 sec, &secdata->sec_info))
7882 return false;
7883 else if (secdata->sec_info)
7884 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
7885 }
7886
7887 if (elf_hash_table (info)->merge_info != NULL)
7888 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
7889 merge_sections_remove_hook);
7890 return true;
7891 }
7892
7893 /* Create an entry in an ELF linker hash table. */
7894
7895 struct bfd_hash_entry *
7896 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7897 struct bfd_hash_table *table,
7898 const char *string)
7899 {
7900 /* Allocate the structure if it has not already been allocated by a
7901 subclass. */
7902 if (entry == NULL)
7903 {
7904 entry = (struct bfd_hash_entry *)
7905 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
7906 if (entry == NULL)
7907 return entry;
7908 }
7909
7910 /* Call the allocation method of the superclass. */
7911 entry = _bfd_link_hash_newfunc (entry, table, string);
7912 if (entry != NULL)
7913 {
7914 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7915 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7916
7917 /* Set local fields. */
7918 ret->indx = -1;
7919 ret->dynindx = -1;
7920 ret->got = htab->init_got_refcount;
7921 ret->plt = htab->init_plt_refcount;
7922 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7923 - offsetof (struct elf_link_hash_entry, size)));
7924 /* Assume that we have been called by a non-ELF symbol reader.
7925 This flag is then reset by the code which reads an ELF input
7926 file. This ensures that a symbol created by a non-ELF symbol
7927 reader will have the flag set correctly. */
7928 ret->non_elf = 1;
7929 }
7930
7931 return entry;
7932 }
7933
7934 /* Copy data from an indirect symbol to its direct symbol, hiding the
7935 old indirect symbol. Also used for copying flags to a weakdef. */
7936
7937 void
7938 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7939 struct elf_link_hash_entry *dir,
7940 struct elf_link_hash_entry *ind)
7941 {
7942 struct elf_link_hash_table *htab;
7943
7944 if (ind->dyn_relocs != NULL)
7945 {
7946 if (dir->dyn_relocs != NULL)
7947 {
7948 struct elf_dyn_relocs **pp;
7949 struct elf_dyn_relocs *p;
7950
7951 /* Add reloc counts against the indirect sym to the direct sym
7952 list. Merge any entries against the same section. */
7953 for (pp = &ind->dyn_relocs; (p = *pp) != NULL; )
7954 {
7955 struct elf_dyn_relocs *q;
7956
7957 for (q = dir->dyn_relocs; q != NULL; q = q->next)
7958 if (q->sec == p->sec)
7959 {
7960 q->pc_count += p->pc_count;
7961 q->count += p->count;
7962 *pp = p->next;
7963 break;
7964 }
7965 if (q == NULL)
7966 pp = &p->next;
7967 }
7968 *pp = dir->dyn_relocs;
7969 }
7970
7971 dir->dyn_relocs = ind->dyn_relocs;
7972 ind->dyn_relocs = NULL;
7973 }
7974
7975 /* Copy down any references that we may have already seen to the
7976 symbol which just became indirect. */
7977
7978 if (dir->versioned != versioned_hidden)
7979 dir->ref_dynamic |= ind->ref_dynamic;
7980 dir->ref_regular |= ind->ref_regular;
7981 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7982 dir->non_got_ref |= ind->non_got_ref;
7983 dir->needs_plt |= ind->needs_plt;
7984 dir->pointer_equality_needed |= ind->pointer_equality_needed;
7985
7986 if (ind->root.type != bfd_link_hash_indirect)
7987 return;
7988
7989 /* Copy over the global and procedure linkage table refcount entries.
7990 These may have been already set up by a check_relocs routine. */
7991 htab = elf_hash_table (info);
7992 if (ind->got.refcount > htab->init_got_refcount.refcount)
7993 {
7994 if (dir->got.refcount < 0)
7995 dir->got.refcount = 0;
7996 dir->got.refcount += ind->got.refcount;
7997 ind->got.refcount = htab->init_got_refcount.refcount;
7998 }
7999
8000 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
8001 {
8002 if (dir->plt.refcount < 0)
8003 dir->plt.refcount = 0;
8004 dir->plt.refcount += ind->plt.refcount;
8005 ind->plt.refcount = htab->init_plt_refcount.refcount;
8006 }
8007
8008 if (ind->dynindx != -1)
8009 {
8010 if (dir->dynindx != -1)
8011 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
8012 dir->dynindx = ind->dynindx;
8013 dir->dynstr_index = ind->dynstr_index;
8014 ind->dynindx = -1;
8015 ind->dynstr_index = 0;
8016 }
8017 }
8018
8019 void
8020 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
8021 struct elf_link_hash_entry *h,
8022 bool force_local)
8023 {
8024 /* STT_GNU_IFUNC symbol must go through PLT. */
8025 if (h->type != STT_GNU_IFUNC)
8026 {
8027 h->plt = elf_hash_table (info)->init_plt_offset;
8028 h->needs_plt = 0;
8029 }
8030 if (force_local)
8031 {
8032 h->forced_local = 1;
8033 if (h->dynindx != -1)
8034 {
8035 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8036 h->dynstr_index);
8037 h->dynindx = -1;
8038 h->dynstr_index = 0;
8039 }
8040 }
8041 }
8042
8043 /* Hide a symbol. */
8044
8045 void
8046 _bfd_elf_link_hide_symbol (bfd *output_bfd,
8047 struct bfd_link_info *info,
8048 struct bfd_link_hash_entry *h)
8049 {
8050 if (is_elf_hash_table (info->hash))
8051 {
8052 const struct elf_backend_data *bed
8053 = get_elf_backend_data (output_bfd);
8054 struct elf_link_hash_entry *eh
8055 = (struct elf_link_hash_entry *) h;
8056 bed->elf_backend_hide_symbol (info, eh, true);
8057 eh->def_dynamic = 0;
8058 eh->ref_dynamic = 0;
8059 eh->dynamic_def = 0;
8060 }
8061 }
8062
8063 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
8064 caller. */
8065
8066 bool
8067 _bfd_elf_link_hash_table_init
8068 (struct elf_link_hash_table *table,
8069 bfd *abfd,
8070 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
8071 struct bfd_hash_table *,
8072 const char *),
8073 unsigned int entsize,
8074 enum elf_target_id target_id)
8075 {
8076 bool ret;
8077 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
8078
8079 table->init_got_refcount.refcount = can_refcount - 1;
8080 table->init_plt_refcount.refcount = can_refcount - 1;
8081 table->init_got_offset.offset = -(bfd_vma) 1;
8082 table->init_plt_offset.offset = -(bfd_vma) 1;
8083 /* The first dynamic symbol is a dummy. */
8084 table->dynsymcount = 1;
8085
8086 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
8087
8088 table->root.type = bfd_link_elf_hash_table;
8089 table->hash_table_id = target_id;
8090 table->target_os = get_elf_backend_data (abfd)->target_os;
8091
8092 return ret;
8093 }
8094
8095 /* Create an ELF linker hash table. */
8096
8097 struct bfd_link_hash_table *
8098 _bfd_elf_link_hash_table_create (bfd *abfd)
8099 {
8100 struct elf_link_hash_table *ret;
8101 size_t amt = sizeof (struct elf_link_hash_table);
8102
8103 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
8104 if (ret == NULL)
8105 return NULL;
8106
8107 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
8108 sizeof (struct elf_link_hash_entry),
8109 GENERIC_ELF_DATA))
8110 {
8111 free (ret);
8112 return NULL;
8113 }
8114 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
8115
8116 return &ret->root;
8117 }
8118
8119 /* Destroy an ELF linker hash table. */
8120
8121 void
8122 _bfd_elf_link_hash_table_free (bfd *obfd)
8123 {
8124 struct elf_link_hash_table *htab;
8125
8126 htab = (struct elf_link_hash_table *) obfd->link.hash;
8127 if (htab->dynstr != NULL)
8128 _bfd_elf_strtab_free (htab->dynstr);
8129 _bfd_merge_sections_free (htab->merge_info);
8130 _bfd_generic_link_hash_table_free (obfd);
8131 }
8132
8133 /* This is a hook for the ELF emulation code in the generic linker to
8134 tell the backend linker what file name to use for the DT_NEEDED
8135 entry for a dynamic object. */
8136
8137 void
8138 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
8139 {
8140 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8141 && bfd_get_format (abfd) == bfd_object)
8142 elf_dt_name (abfd) = name;
8143 }
8144
8145 int
8146 bfd_elf_get_dyn_lib_class (bfd *abfd)
8147 {
8148 int lib_class;
8149 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8150 && bfd_get_format (abfd) == bfd_object)
8151 lib_class = elf_dyn_lib_class (abfd);
8152 else
8153 lib_class = 0;
8154 return lib_class;
8155 }
8156
8157 void
8158 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
8159 {
8160 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8161 && bfd_get_format (abfd) == bfd_object)
8162 elf_dyn_lib_class (abfd) = lib_class;
8163 }
8164
8165 /* Get the list of DT_NEEDED entries for a link. This is a hook for
8166 the linker ELF emulation code. */
8167
8168 struct bfd_link_needed_list *
8169 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
8170 struct bfd_link_info *info)
8171 {
8172 if (! is_elf_hash_table (info->hash))
8173 return NULL;
8174 return elf_hash_table (info)->needed;
8175 }
8176
8177 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
8178 hook for the linker ELF emulation code. */
8179
8180 struct bfd_link_needed_list *
8181 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
8182 struct bfd_link_info *info)
8183 {
8184 if (! is_elf_hash_table (info->hash))
8185 return NULL;
8186 return elf_hash_table (info)->runpath;
8187 }
8188
8189 /* Get the name actually used for a dynamic object for a link. This
8190 is the SONAME entry if there is one. Otherwise, it is the string
8191 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
8192
8193 const char *
8194 bfd_elf_get_dt_soname (bfd *abfd)
8195 {
8196 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8197 && bfd_get_format (abfd) == bfd_object)
8198 return elf_dt_name (abfd);
8199 return NULL;
8200 }
8201
8202 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
8203 the ELF linker emulation code. */
8204
8205 bool
8206 bfd_elf_get_bfd_needed_list (bfd *abfd,
8207 struct bfd_link_needed_list **pneeded)
8208 {
8209 asection *s;
8210 bfd_byte *dynbuf = NULL;
8211 unsigned int elfsec;
8212 unsigned long shlink;
8213 bfd_byte *extdyn, *extdynend;
8214 size_t extdynsize;
8215 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
8216
8217 *pneeded = NULL;
8218
8219 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
8220 || bfd_get_format (abfd) != bfd_object)
8221 return true;
8222
8223 s = bfd_get_section_by_name (abfd, ".dynamic");
8224 if (s == NULL || s->size == 0 || (s->flags & SEC_HAS_CONTENTS) == 0)
8225 return true;
8226
8227 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
8228 goto error_return;
8229
8230 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
8231 if (elfsec == SHN_BAD)
8232 goto error_return;
8233
8234 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
8235
8236 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
8237 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
8238
8239 for (extdyn = dynbuf, extdynend = dynbuf + s->size;
8240 (size_t) (extdynend - extdyn) >= extdynsize;
8241 extdyn += extdynsize)
8242 {
8243 Elf_Internal_Dyn dyn;
8244
8245 (*swap_dyn_in) (abfd, extdyn, &dyn);
8246
8247 if (dyn.d_tag == DT_NULL)
8248 break;
8249
8250 if (dyn.d_tag == DT_NEEDED)
8251 {
8252 const char *string;
8253 struct bfd_link_needed_list *l;
8254 unsigned int tagv = dyn.d_un.d_val;
8255 size_t amt;
8256
8257 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
8258 if (string == NULL)
8259 goto error_return;
8260
8261 amt = sizeof *l;
8262 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
8263 if (l == NULL)
8264 goto error_return;
8265
8266 l->by = abfd;
8267 l->name = string;
8268 l->next = *pneeded;
8269 *pneeded = l;
8270 }
8271 }
8272
8273 free (dynbuf);
8274
8275 return true;
8276
8277 error_return:
8278 free (dynbuf);
8279 return false;
8280 }
8281
8282 struct elf_symbuf_symbol
8283 {
8284 unsigned long st_name; /* Symbol name, index in string tbl */
8285 unsigned char st_info; /* Type and binding attributes */
8286 unsigned char st_other; /* Visibilty, and target specific */
8287 };
8288
8289 struct elf_symbuf_head
8290 {
8291 struct elf_symbuf_symbol *ssym;
8292 size_t count;
8293 unsigned int st_shndx;
8294 };
8295
8296 struct elf_symbol
8297 {
8298 union
8299 {
8300 Elf_Internal_Sym *isym;
8301 struct elf_symbuf_symbol *ssym;
8302 void *p;
8303 } u;
8304 const char *name;
8305 };
8306
8307 /* Sort references to symbols by ascending section number. */
8308
8309 static int
8310 elf_sort_elf_symbol (const void *arg1, const void *arg2)
8311 {
8312 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
8313 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
8314
8315 if (s1->st_shndx != s2->st_shndx)
8316 return s1->st_shndx > s2->st_shndx ? 1 : -1;
8317 /* Final sort by the address of the sym in the symbuf ensures
8318 a stable sort. */
8319 if (s1 != s2)
8320 return s1 > s2 ? 1 : -1;
8321 return 0;
8322 }
8323
8324 static int
8325 elf_sym_name_compare (const void *arg1, const void *arg2)
8326 {
8327 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
8328 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
8329 int ret = strcmp (s1->name, s2->name);
8330 if (ret != 0)
8331 return ret;
8332 if (s1->u.p != s2->u.p)
8333 return s1->u.p > s2->u.p ? 1 : -1;
8334 return 0;
8335 }
8336
8337 static struct elf_symbuf_head *
8338 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
8339 {
8340 Elf_Internal_Sym **ind, **indbufend, **indbuf;
8341 struct elf_symbuf_symbol *ssym;
8342 struct elf_symbuf_head *ssymbuf, *ssymhead;
8343 size_t i, shndx_count, total_size, amt;
8344
8345 amt = symcount * sizeof (*indbuf);
8346 indbuf = (Elf_Internal_Sym **) bfd_malloc (amt);
8347 if (indbuf == NULL)
8348 return NULL;
8349
8350 for (ind = indbuf, i = 0; i < symcount; i++)
8351 if (isymbuf[i].st_shndx != SHN_UNDEF)
8352 *ind++ = &isymbuf[i];
8353 indbufend = ind;
8354
8355 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
8356 elf_sort_elf_symbol);
8357
8358 shndx_count = 0;
8359 if (indbufend > indbuf)
8360 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
8361 if (ind[0]->st_shndx != ind[1]->st_shndx)
8362 shndx_count++;
8363
8364 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
8365 + (indbufend - indbuf) * sizeof (*ssym));
8366 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
8367 if (ssymbuf == NULL)
8368 {
8369 free (indbuf);
8370 return NULL;
8371 }
8372
8373 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
8374 ssymbuf->ssym = NULL;
8375 ssymbuf->count = shndx_count;
8376 ssymbuf->st_shndx = 0;
8377 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
8378 {
8379 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
8380 {
8381 ssymhead++;
8382 ssymhead->ssym = ssym;
8383 ssymhead->count = 0;
8384 ssymhead->st_shndx = (*ind)->st_shndx;
8385 }
8386 ssym->st_name = (*ind)->st_name;
8387 ssym->st_info = (*ind)->st_info;
8388 ssym->st_other = (*ind)->st_other;
8389 ssymhead->count++;
8390 }
8391 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
8392 && (uintptr_t) ssym - (uintptr_t) ssymbuf == total_size);
8393
8394 free (indbuf);
8395 return ssymbuf;
8396 }
8397
8398 /* Check if 2 sections define the same set of local and global
8399 symbols. */
8400
8401 static bool
8402 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
8403 struct bfd_link_info *info)
8404 {
8405 bfd *bfd1, *bfd2;
8406 const struct elf_backend_data *bed1, *bed2;
8407 Elf_Internal_Shdr *hdr1, *hdr2;
8408 size_t symcount1, symcount2;
8409 Elf_Internal_Sym *isymbuf1, *isymbuf2;
8410 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
8411 Elf_Internal_Sym *isym, *isymend;
8412 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
8413 size_t count1, count2, sec_count1, sec_count2, i;
8414 unsigned int shndx1, shndx2;
8415 bool result;
8416 bool ignore_section_symbol_p;
8417
8418 bfd1 = sec1->owner;
8419 bfd2 = sec2->owner;
8420
8421 /* Both sections have to be in ELF. */
8422 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
8423 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
8424 return false;
8425
8426 if (elf_section_type (sec1) != elf_section_type (sec2))
8427 return false;
8428
8429 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
8430 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
8431 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
8432 return false;
8433
8434 bed1 = get_elf_backend_data (bfd1);
8435 bed2 = get_elf_backend_data (bfd2);
8436 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
8437 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
8438 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
8439 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
8440
8441 if (symcount1 == 0 || symcount2 == 0)
8442 return false;
8443
8444 result = false;
8445 isymbuf1 = NULL;
8446 isymbuf2 = NULL;
8447 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
8448 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
8449
8450 /* Ignore section symbols only when matching non-debugging sections
8451 or linkonce section with comdat section. */
8452 ignore_section_symbol_p
8453 = ((sec1->flags & SEC_DEBUGGING) == 0
8454 || ((elf_section_flags (sec1) & SHF_GROUP)
8455 != (elf_section_flags (sec2) & SHF_GROUP)));
8456
8457 if (ssymbuf1 == NULL)
8458 {
8459 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
8460 NULL, NULL, NULL);
8461 if (isymbuf1 == NULL)
8462 goto done;
8463
8464 if (info != NULL && !info->reduce_memory_overheads)
8465 {
8466 ssymbuf1 = elf_create_symbuf (symcount1, isymbuf1);
8467 elf_tdata (bfd1)->symbuf = ssymbuf1;
8468 }
8469 }
8470
8471 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
8472 {
8473 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
8474 NULL, NULL, NULL);
8475 if (isymbuf2 == NULL)
8476 goto done;
8477
8478 if (ssymbuf1 != NULL && info != NULL && !info->reduce_memory_overheads)
8479 {
8480 ssymbuf2 = elf_create_symbuf (symcount2, isymbuf2);
8481 elf_tdata (bfd2)->symbuf = ssymbuf2;
8482 }
8483 }
8484
8485 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
8486 {
8487 /* Optimized faster version. */
8488 size_t lo, hi, mid;
8489 struct elf_symbol *symp;
8490 struct elf_symbuf_symbol *ssym, *ssymend;
8491
8492 lo = 0;
8493 hi = ssymbuf1->count;
8494 ssymbuf1++;
8495 count1 = 0;
8496 sec_count1 = 0;
8497 while (lo < hi)
8498 {
8499 mid = (lo + hi) / 2;
8500 if (shndx1 < ssymbuf1[mid].st_shndx)
8501 hi = mid;
8502 else if (shndx1 > ssymbuf1[mid].st_shndx)
8503 lo = mid + 1;
8504 else
8505 {
8506 count1 = ssymbuf1[mid].count;
8507 ssymbuf1 += mid;
8508 break;
8509 }
8510 }
8511 if (ignore_section_symbol_p)
8512 {
8513 for (i = 0; i < count1; i++)
8514 if (ELF_ST_TYPE (ssymbuf1->ssym[i].st_info) == STT_SECTION)
8515 sec_count1++;
8516 count1 -= sec_count1;
8517 }
8518
8519 lo = 0;
8520 hi = ssymbuf2->count;
8521 ssymbuf2++;
8522 count2 = 0;
8523 sec_count2 = 0;
8524 while (lo < hi)
8525 {
8526 mid = (lo + hi) / 2;
8527 if (shndx2 < ssymbuf2[mid].st_shndx)
8528 hi = mid;
8529 else if (shndx2 > ssymbuf2[mid].st_shndx)
8530 lo = mid + 1;
8531 else
8532 {
8533 count2 = ssymbuf2[mid].count;
8534 ssymbuf2 += mid;
8535 break;
8536 }
8537 }
8538 if (ignore_section_symbol_p)
8539 {
8540 for (i = 0; i < count2; i++)
8541 if (ELF_ST_TYPE (ssymbuf2->ssym[i].st_info) == STT_SECTION)
8542 sec_count2++;
8543 count2 -= sec_count2;
8544 }
8545
8546 if (count1 == 0 || count2 == 0 || count1 != count2)
8547 goto done;
8548
8549 symtable1
8550 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
8551 symtable2
8552 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
8553 if (symtable1 == NULL || symtable2 == NULL)
8554 goto done;
8555
8556 symp = symtable1;
8557 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1 + sec_count1;
8558 ssym < ssymend; ssym++)
8559 if (sec_count1 == 0
8560 || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
8561 {
8562 symp->u.ssym = ssym;
8563 symp->name = bfd_elf_string_from_elf_section (bfd1,
8564 hdr1->sh_link,
8565 ssym->st_name);
8566 symp++;
8567 }
8568
8569 symp = symtable2;
8570 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2 + sec_count2;
8571 ssym < ssymend; ssym++)
8572 if (sec_count2 == 0
8573 || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
8574 {
8575 symp->u.ssym = ssym;
8576 symp->name = bfd_elf_string_from_elf_section (bfd2,
8577 hdr2->sh_link,
8578 ssym->st_name);
8579 symp++;
8580 }
8581
8582 /* Sort symbol by name. */
8583 qsort (symtable1, count1, sizeof (struct elf_symbol),
8584 elf_sym_name_compare);
8585 qsort (symtable2, count1, sizeof (struct elf_symbol),
8586 elf_sym_name_compare);
8587
8588 for (i = 0; i < count1; i++)
8589 /* Two symbols must have the same binding, type and name. */
8590 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8591 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8592 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8593 goto done;
8594
8595 result = true;
8596 goto done;
8597 }
8598
8599 symtable1 = (struct elf_symbol *)
8600 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8601 symtable2 = (struct elf_symbol *)
8602 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
8603 if (symtable1 == NULL || symtable2 == NULL)
8604 goto done;
8605
8606 /* Count definitions in the section. */
8607 count1 = 0;
8608 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
8609 if (isym->st_shndx == shndx1
8610 && (!ignore_section_symbol_p
8611 || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
8612 symtable1[count1++].u.isym = isym;
8613
8614 count2 = 0;
8615 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
8616 if (isym->st_shndx == shndx2
8617 && (!ignore_section_symbol_p
8618 || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
8619 symtable2[count2++].u.isym = isym;
8620
8621 if (count1 == 0 || count2 == 0 || count1 != count2)
8622 goto done;
8623
8624 for (i = 0; i < count1; i++)
8625 symtable1[i].name
8626 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8627 symtable1[i].u.isym->st_name);
8628
8629 for (i = 0; i < count2; i++)
8630 symtable2[i].name
8631 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8632 symtable2[i].u.isym->st_name);
8633
8634 /* Sort symbol by name. */
8635 qsort (symtable1, count1, sizeof (struct elf_symbol),
8636 elf_sym_name_compare);
8637 qsort (symtable2, count1, sizeof (struct elf_symbol),
8638 elf_sym_name_compare);
8639
8640 for (i = 0; i < count1; i++)
8641 /* Two symbols must have the same binding, type and name. */
8642 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8643 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8644 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8645 goto done;
8646
8647 result = true;
8648
8649 done:
8650 free (symtable1);
8651 free (symtable2);
8652 free (isymbuf1);
8653 free (isymbuf2);
8654
8655 return result;
8656 }
8657
8658 /* Return TRUE if 2 section types are compatible. */
8659
8660 bool
8661 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8662 bfd *bbfd, const asection *bsec)
8663 {
8664 if (asec == NULL
8665 || bsec == NULL
8666 || abfd->xvec->flavour != bfd_target_elf_flavour
8667 || bbfd->xvec->flavour != bfd_target_elf_flavour)
8668 return true;
8669
8670 return elf_section_type (asec) == elf_section_type (bsec);
8671 }
8672 \f
8673 /* Final phase of ELF linker. */
8674
8675 /* A structure we use to avoid passing large numbers of arguments. */
8676
8677 struct elf_final_link_info
8678 {
8679 /* General link information. */
8680 struct bfd_link_info *info;
8681 /* Output BFD. */
8682 bfd *output_bfd;
8683 /* Symbol string table. */
8684 struct elf_strtab_hash *symstrtab;
8685 /* .hash section. */
8686 asection *hash_sec;
8687 /* symbol version section (.gnu.version). */
8688 asection *symver_sec;
8689 /* Buffer large enough to hold contents of any section. */
8690 bfd_byte *contents;
8691 /* Buffer large enough to hold external relocs of any section. */
8692 void *external_relocs;
8693 /* Buffer large enough to hold internal relocs of any section. */
8694 Elf_Internal_Rela *internal_relocs;
8695 /* Buffer large enough to hold external local symbols of any input
8696 BFD. */
8697 bfd_byte *external_syms;
8698 /* And a buffer for symbol section indices. */
8699 Elf_External_Sym_Shndx *locsym_shndx;
8700 /* Buffer large enough to hold internal local symbols of any input
8701 BFD. */
8702 Elf_Internal_Sym *internal_syms;
8703 /* Array large enough to hold a symbol index for each local symbol
8704 of any input BFD. */
8705 long *indices;
8706 /* Array large enough to hold a section pointer for each local
8707 symbol of any input BFD. */
8708 asection **sections;
8709 /* Buffer for SHT_SYMTAB_SHNDX section. */
8710 Elf_External_Sym_Shndx *symshndxbuf;
8711 /* Number of STT_FILE syms seen. */
8712 size_t filesym_count;
8713 /* Local symbol hash table. */
8714 struct bfd_hash_table local_hash_table;
8715 };
8716
8717 struct local_hash_entry
8718 {
8719 /* Base hash table entry structure. */
8720 struct bfd_hash_entry root;
8721 /* Size of the local symbol name. */
8722 size_t size;
8723 /* Number of the duplicated local symbol names. */
8724 long count;
8725 };
8726
8727 /* Create an entry in the local symbol hash table. */
8728
8729 static struct bfd_hash_entry *
8730 local_hash_newfunc (struct bfd_hash_entry *entry,
8731 struct bfd_hash_table *table,
8732 const char *string)
8733 {
8734
8735 /* Allocate the structure if it has not already been allocated by a
8736 subclass. */
8737 if (entry == NULL)
8738 {
8739 entry = bfd_hash_allocate (table,
8740 sizeof (struct local_hash_entry));
8741 if (entry == NULL)
8742 return entry;
8743 }
8744
8745 /* Call the allocation method of the superclass. */
8746 entry = bfd_hash_newfunc (entry, table, string);
8747 if (entry != NULL)
8748 {
8749 ((struct local_hash_entry *) entry)->count = 0;
8750 ((struct local_hash_entry *) entry)->size = 0;
8751 }
8752
8753 return entry;
8754 }
8755
8756 /* This struct is used to pass information to elf_link_output_extsym. */
8757
8758 struct elf_outext_info
8759 {
8760 bool failed;
8761 bool localsyms;
8762 bool file_sym_done;
8763 struct elf_final_link_info *flinfo;
8764 };
8765
8766
8767 /* Support for evaluating a complex relocation.
8768
8769 Complex relocations are generalized, self-describing relocations. The
8770 implementation of them consists of two parts: complex symbols, and the
8771 relocations themselves.
8772
8773 The relocations use a reserved elf-wide relocation type code (R_RELC
8774 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8775 information (start bit, end bit, word width, etc) into the addend. This
8776 information is extracted from CGEN-generated operand tables within gas.
8777
8778 Complex symbols are mangled symbols (STT_RELC external / BSF_RELC
8779 internal) representing prefix-notation expressions, including but not
8780 limited to those sorts of expressions normally encoded as addends in the
8781 addend field. The symbol mangling format is:
8782
8783 <node> := <literal>
8784 | <unary-operator> ':' <node>
8785 | <binary-operator> ':' <node> ':' <node>
8786 ;
8787
8788 <literal> := 's' <digits=N> ':' <N character symbol name>
8789 | 'S' <digits=N> ':' <N character section name>
8790 | '#' <hexdigits>
8791 ;
8792
8793 <binary-operator> := as in C
8794 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8795
8796 static void
8797 set_symbol_value (bfd *bfd_with_globals,
8798 Elf_Internal_Sym *isymbuf,
8799 size_t locsymcount,
8800 size_t symidx,
8801 bfd_vma val)
8802 {
8803 struct elf_link_hash_entry **sym_hashes;
8804 struct elf_link_hash_entry *h;
8805 size_t extsymoff = locsymcount;
8806
8807 if (symidx < locsymcount)
8808 {
8809 Elf_Internal_Sym *sym;
8810
8811 sym = isymbuf + symidx;
8812 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8813 {
8814 /* It is a local symbol: move it to the
8815 "absolute" section and give it a value. */
8816 sym->st_shndx = SHN_ABS;
8817 sym->st_value = val;
8818 return;
8819 }
8820 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8821 extsymoff = 0;
8822 }
8823
8824 /* It is a global symbol: set its link type
8825 to "defined" and give it a value. */
8826
8827 sym_hashes = elf_sym_hashes (bfd_with_globals);
8828 h = sym_hashes [symidx - extsymoff];
8829 while (h->root.type == bfd_link_hash_indirect
8830 || h->root.type == bfd_link_hash_warning)
8831 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8832 h->root.type = bfd_link_hash_defined;
8833 h->root.u.def.value = val;
8834 h->root.u.def.section = bfd_abs_section_ptr;
8835 }
8836
8837 static bool
8838 resolve_symbol (const char *name,
8839 bfd *input_bfd,
8840 struct elf_final_link_info *flinfo,
8841 bfd_vma *result,
8842 Elf_Internal_Sym *isymbuf,
8843 size_t locsymcount)
8844 {
8845 Elf_Internal_Sym *sym;
8846 struct bfd_link_hash_entry *global_entry;
8847 const char *candidate = NULL;
8848 Elf_Internal_Shdr *symtab_hdr;
8849 size_t i;
8850
8851 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8852
8853 for (i = 0; i < locsymcount; ++ i)
8854 {
8855 sym = isymbuf + i;
8856
8857 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8858 continue;
8859
8860 candidate = bfd_elf_string_from_elf_section (input_bfd,
8861 symtab_hdr->sh_link,
8862 sym->st_name);
8863 #ifdef DEBUG
8864 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8865 name, candidate, (unsigned long) sym->st_value);
8866 #endif
8867 if (candidate && strcmp (candidate, name) == 0)
8868 {
8869 asection *sec = flinfo->sections [i];
8870
8871 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8872 *result += sec->output_offset + sec->output_section->vma;
8873 #ifdef DEBUG
8874 printf ("Found symbol with value %8.8lx\n",
8875 (unsigned long) *result);
8876 #endif
8877 return true;
8878 }
8879 }
8880
8881 /* Hmm, haven't found it yet. perhaps it is a global. */
8882 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
8883 false, false, true);
8884 if (!global_entry)
8885 return false;
8886
8887 if (global_entry->type == bfd_link_hash_defined
8888 || global_entry->type == bfd_link_hash_defweak)
8889 {
8890 *result = (global_entry->u.def.value
8891 + global_entry->u.def.section->output_section->vma
8892 + global_entry->u.def.section->output_offset);
8893 #ifdef DEBUG
8894 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8895 global_entry->root.string, (unsigned long) *result);
8896 #endif
8897 return true;
8898 }
8899
8900 return false;
8901 }
8902
8903 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8904 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8905 names like "foo.end" which is the end address of section "foo". */
8906
8907 static bool
8908 resolve_section (const char *name,
8909 asection *sections,
8910 bfd_vma *result,
8911 bfd * abfd)
8912 {
8913 asection *curr;
8914 unsigned int len;
8915
8916 for (curr = sections; curr; curr = curr->next)
8917 if (strcmp (curr->name, name) == 0)
8918 {
8919 *result = curr->vma;
8920 return true;
8921 }
8922
8923 /* Hmm. still haven't found it. try pseudo-section names. */
8924 /* FIXME: This could be coded more efficiently... */
8925 for (curr = sections; curr; curr = curr->next)
8926 {
8927 len = strlen (curr->name);
8928 if (len > strlen (name))
8929 continue;
8930
8931 if (strncmp (curr->name, name, len) == 0)
8932 {
8933 if (startswith (name + len, ".end"))
8934 {
8935 *result = (curr->vma
8936 + curr->size / bfd_octets_per_byte (abfd, curr));
8937 return true;
8938 }
8939
8940 /* Insert more pseudo-section names here, if you like. */
8941 }
8942 }
8943
8944 return false;
8945 }
8946
8947 static void
8948 undefined_reference (const char *reftype, const char *name)
8949 {
8950 /* xgettext:c-format */
8951 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8952 reftype, name);
8953 bfd_set_error (bfd_error_bad_value);
8954 }
8955
8956 static bool
8957 eval_symbol (bfd_vma *result,
8958 const char **symp,
8959 bfd *input_bfd,
8960 struct elf_final_link_info *flinfo,
8961 bfd_vma dot,
8962 Elf_Internal_Sym *isymbuf,
8963 size_t locsymcount,
8964 int signed_p)
8965 {
8966 size_t len;
8967 size_t symlen;
8968 bfd_vma a;
8969 bfd_vma b;
8970 char symbuf[4096];
8971 const char *sym = *symp;
8972 const char *symend;
8973 bool symbol_is_section = false;
8974
8975 len = strlen (sym);
8976 symend = sym + len;
8977
8978 if (len < 1 || len > sizeof (symbuf))
8979 {
8980 bfd_set_error (bfd_error_invalid_operation);
8981 return false;
8982 }
8983
8984 switch (* sym)
8985 {
8986 case '.':
8987 *result = dot;
8988 *symp = sym + 1;
8989 return true;
8990
8991 case '#':
8992 ++sym;
8993 *result = strtoul (sym, (char **) symp, 16);
8994 return true;
8995
8996 case 'S':
8997 symbol_is_section = true;
8998 /* Fall through. */
8999 case 's':
9000 ++sym;
9001 symlen = strtol (sym, (char **) symp, 10);
9002 sym = *symp + 1; /* Skip the trailing ':'. */
9003
9004 if (symend < sym || symlen + 1 > sizeof (symbuf))
9005 {
9006 bfd_set_error (bfd_error_invalid_operation);
9007 return false;
9008 }
9009
9010 memcpy (symbuf, sym, symlen);
9011 symbuf[symlen] = '\0';
9012 *symp = sym + symlen;
9013
9014 /* Is it always possible, with complex symbols, that gas "mis-guessed"
9015 the symbol as a section, or vice-versa. so we're pretty liberal in our
9016 interpretation here; section means "try section first", not "must be a
9017 section", and likewise with symbol. */
9018
9019 if (symbol_is_section)
9020 {
9021 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
9022 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
9023 isymbuf, locsymcount))
9024 {
9025 undefined_reference ("section", symbuf);
9026 return false;
9027 }
9028 }
9029 else
9030 {
9031 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
9032 isymbuf, locsymcount)
9033 && !resolve_section (symbuf, flinfo->output_bfd->sections,
9034 result, input_bfd))
9035 {
9036 undefined_reference ("symbol", symbuf);
9037 return false;
9038 }
9039 }
9040
9041 return true;
9042
9043 /* All that remains are operators. */
9044
9045 #define UNARY_OP(op) \
9046 if (startswith (sym, #op)) \
9047 { \
9048 sym += strlen (#op); \
9049 if (*sym == ':') \
9050 ++sym; \
9051 *symp = sym; \
9052 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
9053 isymbuf, locsymcount, signed_p)) \
9054 return false; \
9055 if (signed_p) \
9056 *result = op ((bfd_signed_vma) a); \
9057 else \
9058 *result = op a; \
9059 return true; \
9060 }
9061
9062 #define BINARY_OP_HEAD(op) \
9063 if (startswith (sym, #op)) \
9064 { \
9065 sym += strlen (#op); \
9066 if (*sym == ':') \
9067 ++sym; \
9068 *symp = sym; \
9069 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
9070 isymbuf, locsymcount, signed_p)) \
9071 return false; \
9072 ++*symp; \
9073 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
9074 isymbuf, locsymcount, signed_p)) \
9075 return false;
9076 #define BINARY_OP_TAIL(op) \
9077 if (signed_p) \
9078 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
9079 else \
9080 *result = a op b; \
9081 return true; \
9082 }
9083 #define BINARY_OP(op) BINARY_OP_HEAD(op) BINARY_OP_TAIL(op)
9084
9085 default:
9086 UNARY_OP (0-);
9087 BINARY_OP_HEAD (<<);
9088 if (b >= sizeof (a) * CHAR_BIT)
9089 {
9090 *result = 0;
9091 return true;
9092 }
9093 signed_p = 0;
9094 BINARY_OP_TAIL (<<);
9095 BINARY_OP_HEAD (>>);
9096 if (b >= sizeof (a) * CHAR_BIT)
9097 {
9098 *result = signed_p && (bfd_signed_vma) a < 0 ? -1 : 0;
9099 return true;
9100 }
9101 BINARY_OP_TAIL (>>);
9102 BINARY_OP (==);
9103 BINARY_OP (!=);
9104 BINARY_OP (<=);
9105 BINARY_OP (>=);
9106 BINARY_OP (&&);
9107 BINARY_OP (||);
9108 UNARY_OP (~);
9109 UNARY_OP (!);
9110 BINARY_OP (*);
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_HEAD (%);
9120 if (b == 0)
9121 {
9122 _bfd_error_handler (_("division by zero"));
9123 bfd_set_error (bfd_error_bad_value);
9124 return false;
9125 }
9126 BINARY_OP_TAIL (%);
9127 BINARY_OP (^);
9128 BINARY_OP (|);
9129 BINARY_OP (&);
9130 BINARY_OP (+);
9131 BINARY_OP (-);
9132 BINARY_OP (<);
9133 BINARY_OP (>);
9134 #undef UNARY_OP
9135 #undef BINARY_OP
9136 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
9137 bfd_set_error (bfd_error_invalid_operation);
9138 return false;
9139 }
9140 }
9141
9142 static void
9143 put_value (bfd_vma size,
9144 unsigned long chunksz,
9145 bfd *input_bfd,
9146 bfd_vma x,
9147 bfd_byte *location)
9148 {
9149 location += (size - chunksz);
9150
9151 for (; size; size -= chunksz, location -= chunksz)
9152 {
9153 switch (chunksz)
9154 {
9155 case 1:
9156 bfd_put_8 (input_bfd, x, location);
9157 x >>= 8;
9158 break;
9159 case 2:
9160 bfd_put_16 (input_bfd, x, location);
9161 x >>= 16;
9162 break;
9163 case 4:
9164 bfd_put_32 (input_bfd, x, location);
9165 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
9166 x >>= 16;
9167 x >>= 16;
9168 break;
9169 #ifdef BFD64
9170 case 8:
9171 bfd_put_64 (input_bfd, x, location);
9172 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
9173 x >>= 32;
9174 x >>= 32;
9175 break;
9176 #endif
9177 default:
9178 abort ();
9179 break;
9180 }
9181 }
9182 }
9183
9184 static bfd_vma
9185 get_value (bfd_vma size,
9186 unsigned long chunksz,
9187 bfd *input_bfd,
9188 bfd_byte *location)
9189 {
9190 int shift;
9191 bfd_vma x = 0;
9192
9193 /* Sanity checks. */
9194 BFD_ASSERT (chunksz <= sizeof (x)
9195 && size >= chunksz
9196 && chunksz != 0
9197 && (size % chunksz) == 0
9198 && input_bfd != NULL
9199 && location != NULL);
9200
9201 if (chunksz == sizeof (x))
9202 {
9203 BFD_ASSERT (size == chunksz);
9204
9205 /* Make sure that we do not perform an undefined shift operation.
9206 We know that size == chunksz so there will only be one iteration
9207 of the loop below. */
9208 shift = 0;
9209 }
9210 else
9211 shift = 8 * chunksz;
9212
9213 for (; size; size -= chunksz, location += chunksz)
9214 {
9215 switch (chunksz)
9216 {
9217 case 1:
9218 x = (x << shift) | bfd_get_8 (input_bfd, location);
9219 break;
9220 case 2:
9221 x = (x << shift) | bfd_get_16 (input_bfd, location);
9222 break;
9223 case 4:
9224 x = (x << shift) | bfd_get_32 (input_bfd, location);
9225 break;
9226 #ifdef BFD64
9227 case 8:
9228 x = (x << shift) | bfd_get_64 (input_bfd, location);
9229 break;
9230 #endif
9231 default:
9232 abort ();
9233 }
9234 }
9235 return x;
9236 }
9237
9238 static void
9239 decode_complex_addend (unsigned long *start, /* in bits */
9240 unsigned long *oplen, /* in bits */
9241 unsigned long *len, /* in bits */
9242 unsigned long *wordsz, /* in bytes */
9243 unsigned long *chunksz, /* in bytes */
9244 unsigned long *lsb0_p,
9245 unsigned long *signed_p,
9246 unsigned long *trunc_p,
9247 unsigned long encoded)
9248 {
9249 * start = encoded & 0x3F;
9250 * len = (encoded >> 6) & 0x3F;
9251 * oplen = (encoded >> 12) & 0x3F;
9252 * wordsz = (encoded >> 18) & 0xF;
9253 * chunksz = (encoded >> 22) & 0xF;
9254 * lsb0_p = (encoded >> 27) & 1;
9255 * signed_p = (encoded >> 28) & 1;
9256 * trunc_p = (encoded >> 29) & 1;
9257 }
9258
9259 bfd_reloc_status_type
9260 bfd_elf_perform_complex_relocation (bfd *input_bfd,
9261 asection *input_section,
9262 bfd_byte *contents,
9263 Elf_Internal_Rela *rel,
9264 bfd_vma relocation)
9265 {
9266 bfd_vma shift, x, mask;
9267 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
9268 bfd_reloc_status_type r;
9269 bfd_size_type octets;
9270
9271 /* Perform this reloc, since it is complex.
9272 (this is not to say that it necessarily refers to a complex
9273 symbol; merely that it is a self-describing CGEN based reloc.
9274 i.e. the addend has the complete reloc information (bit start, end,
9275 word size, etc) encoded within it.). */
9276
9277 decode_complex_addend (&start, &oplen, &len, &wordsz,
9278 &chunksz, &lsb0_p, &signed_p,
9279 &trunc_p, rel->r_addend);
9280
9281 mask = (((1L << (len - 1)) - 1) << 1) | 1;
9282
9283 if (lsb0_p)
9284 shift = (start + 1) - len;
9285 else
9286 shift = (8 * wordsz) - (start + len);
9287
9288 octets = rel->r_offset * bfd_octets_per_byte (input_bfd, input_section);
9289 x = get_value (wordsz, chunksz, input_bfd, contents + octets);
9290
9291 #ifdef DEBUG
9292 printf ("Doing complex reloc: "
9293 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
9294 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
9295 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
9296 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9297 oplen, (unsigned long) x, (unsigned long) mask,
9298 (unsigned long) relocation);
9299 #endif
9300
9301 r = bfd_reloc_ok;
9302 if (! trunc_p)
9303 /* Now do an overflow check. */
9304 r = bfd_check_overflow ((signed_p
9305 ? complain_overflow_signed
9306 : complain_overflow_unsigned),
9307 len, 0, (8 * wordsz),
9308 relocation);
9309
9310 /* Do the deed. */
9311 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
9312
9313 #ifdef DEBUG
9314 printf (" relocation: %8.8lx\n"
9315 " shifted mask: %8.8lx\n"
9316 " shifted/masked reloc: %8.8lx\n"
9317 " result: %8.8lx\n",
9318 (unsigned long) relocation, (unsigned long) (mask << shift),
9319 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
9320 #endif
9321 put_value (wordsz, chunksz, input_bfd, x, contents + octets);
9322 return r;
9323 }
9324
9325 /* Functions to read r_offset from external (target order) reloc
9326 entry. Faster than bfd_getl32 et al, because we let the compiler
9327 know the value is aligned. */
9328
9329 static bfd_vma
9330 ext32l_r_offset (const void *p)
9331 {
9332 union aligned32
9333 {
9334 uint32_t v;
9335 unsigned char c[4];
9336 };
9337 const union aligned32 *a
9338 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9339
9340 uint32_t aval = ( (uint32_t) a->c[0]
9341 | (uint32_t) a->c[1] << 8
9342 | (uint32_t) a->c[2] << 16
9343 | (uint32_t) a->c[3] << 24);
9344 return aval;
9345 }
9346
9347 static bfd_vma
9348 ext32b_r_offset (const void *p)
9349 {
9350 union aligned32
9351 {
9352 uint32_t v;
9353 unsigned char c[4];
9354 };
9355 const union aligned32 *a
9356 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9357
9358 uint32_t aval = ( (uint32_t) a->c[0] << 24
9359 | (uint32_t) a->c[1] << 16
9360 | (uint32_t) a->c[2] << 8
9361 | (uint32_t) a->c[3]);
9362 return aval;
9363 }
9364
9365 static bfd_vma
9366 ext64l_r_offset (const void *p)
9367 {
9368 union aligned64
9369 {
9370 uint64_t v;
9371 unsigned char c[8];
9372 };
9373 const union aligned64 *a
9374 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9375
9376 uint64_t aval = ( (uint64_t) a->c[0]
9377 | (uint64_t) a->c[1] << 8
9378 | (uint64_t) a->c[2] << 16
9379 | (uint64_t) a->c[3] << 24
9380 | (uint64_t) a->c[4] << 32
9381 | (uint64_t) a->c[5] << 40
9382 | (uint64_t) a->c[6] << 48
9383 | (uint64_t) a->c[7] << 56);
9384 return aval;
9385 }
9386
9387 static bfd_vma
9388 ext64b_r_offset (const void *p)
9389 {
9390 union aligned64
9391 {
9392 uint64_t v;
9393 unsigned char c[8];
9394 };
9395 const union aligned64 *a
9396 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9397
9398 uint64_t aval = ( (uint64_t) a->c[0] << 56
9399 | (uint64_t) a->c[1] << 48
9400 | (uint64_t) a->c[2] << 40
9401 | (uint64_t) a->c[3] << 32
9402 | (uint64_t) a->c[4] << 24
9403 | (uint64_t) a->c[5] << 16
9404 | (uint64_t) a->c[6] << 8
9405 | (uint64_t) a->c[7]);
9406 return aval;
9407 }
9408
9409 /* When performing a relocatable link, the input relocations are
9410 preserved. But, if they reference global symbols, the indices
9411 referenced must be updated. Update all the relocations found in
9412 RELDATA. */
9413
9414 static bool
9415 elf_link_adjust_relocs (bfd *abfd,
9416 asection *sec,
9417 struct bfd_elf_section_reloc_data *reldata,
9418 bool sort,
9419 struct bfd_link_info *info)
9420 {
9421 unsigned int i;
9422 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9423 bfd_byte *erela;
9424 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9425 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9426 bfd_vma r_type_mask;
9427 int r_sym_shift;
9428 unsigned int count = reldata->count;
9429 struct elf_link_hash_entry **rel_hash = reldata->hashes;
9430
9431 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
9432 {
9433 swap_in = bed->s->swap_reloc_in;
9434 swap_out = bed->s->swap_reloc_out;
9435 }
9436 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
9437 {
9438 swap_in = bed->s->swap_reloca_in;
9439 swap_out = bed->s->swap_reloca_out;
9440 }
9441 else
9442 abort ();
9443
9444 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
9445 abort ();
9446
9447 if (bed->s->arch_size == 32)
9448 {
9449 r_type_mask = 0xff;
9450 r_sym_shift = 8;
9451 }
9452 else
9453 {
9454 r_type_mask = 0xffffffff;
9455 r_sym_shift = 32;
9456 }
9457
9458 erela = reldata->hdr->contents;
9459 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
9460 {
9461 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
9462 unsigned int j;
9463
9464 if (*rel_hash == NULL)
9465 continue;
9466
9467 if ((*rel_hash)->indx == -2
9468 && info->gc_sections
9469 && ! info->gc_keep_exported)
9470 {
9471 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9472 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
9473 abfd, sec,
9474 (*rel_hash)->root.root.string);
9475 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
9476 abfd, sec);
9477 bfd_set_error (bfd_error_invalid_operation);
9478 return false;
9479 }
9480 BFD_ASSERT ((*rel_hash)->indx >= 0);
9481
9482 (*swap_in) (abfd, erela, irela);
9483 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
9484 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
9485 | (irela[j].r_info & r_type_mask));
9486 (*swap_out) (abfd, irela, erela);
9487 }
9488
9489 if (bed->elf_backend_update_relocs)
9490 (*bed->elf_backend_update_relocs) (sec, reldata);
9491
9492 if (sort && count != 0)
9493 {
9494 bfd_vma (*ext_r_off) (const void *);
9495 bfd_vma r_off;
9496 size_t elt_size;
9497 bfd_byte *base, *end, *p, *loc;
9498 bfd_byte *buf = NULL;
9499
9500 if (bed->s->arch_size == 32)
9501 {
9502 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9503 ext_r_off = ext32l_r_offset;
9504 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9505 ext_r_off = ext32b_r_offset;
9506 else
9507 abort ();
9508 }
9509 else
9510 {
9511 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9512 ext_r_off = ext64l_r_offset;
9513 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9514 ext_r_off = ext64b_r_offset;
9515 else
9516 abort ();
9517 }
9518
9519 /* Must use a stable sort here. A modified insertion sort,
9520 since the relocs are mostly sorted already. */
9521 elt_size = reldata->hdr->sh_entsize;
9522 base = reldata->hdr->contents;
9523 end = base + count * elt_size;
9524 if (elt_size > sizeof (Elf64_External_Rela))
9525 abort ();
9526
9527 /* Ensure the first element is lowest. This acts as a sentinel,
9528 speeding the main loop below. */
9529 r_off = (*ext_r_off) (base);
9530 for (p = loc = base; (p += elt_size) < end; )
9531 {
9532 bfd_vma r_off2 = (*ext_r_off) (p);
9533 if (r_off > r_off2)
9534 {
9535 r_off = r_off2;
9536 loc = p;
9537 }
9538 }
9539 if (loc != base)
9540 {
9541 /* Don't just swap *base and *loc as that changes the order
9542 of the original base[0] and base[1] if they happen to
9543 have the same r_offset. */
9544 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
9545 memcpy (onebuf, loc, elt_size);
9546 memmove (base + elt_size, base, loc - base);
9547 memcpy (base, onebuf, elt_size);
9548 }
9549
9550 for (p = base + elt_size; (p += elt_size) < end; )
9551 {
9552 /* base to p is sorted, *p is next to insert. */
9553 r_off = (*ext_r_off) (p);
9554 /* Search the sorted region for location to insert. */
9555 loc = p - elt_size;
9556 while (r_off < (*ext_r_off) (loc))
9557 loc -= elt_size;
9558 loc += elt_size;
9559 if (loc != p)
9560 {
9561 /* Chances are there is a run of relocs to insert here,
9562 from one of more input files. Files are not always
9563 linked in order due to the way elf_link_input_bfd is
9564 called. See pr17666. */
9565 size_t sortlen = p - loc;
9566 bfd_vma r_off2 = (*ext_r_off) (loc);
9567 size_t runlen = elt_size;
9568 bfd_vma r_off_runend = r_off;
9569 bfd_vma r_off_runend_next;
9570 size_t buf_size = 96 * 1024;
9571 while (p + runlen < end
9572 && (sortlen <= buf_size
9573 || runlen + elt_size <= buf_size)
9574 /* run must not break the ordering of base..loc+1 */
9575 && r_off2 > (r_off_runend_next = (*ext_r_off) (p + runlen))
9576 /* run must be already sorted */
9577 && r_off_runend_next >= r_off_runend)
9578 {
9579 runlen += elt_size;
9580 r_off_runend = r_off_runend_next;
9581 }
9582 if (buf == NULL)
9583 {
9584 buf = bfd_malloc (buf_size);
9585 if (buf == NULL)
9586 return false;
9587 }
9588 if (runlen < sortlen)
9589 {
9590 memcpy (buf, p, runlen);
9591 memmove (loc + runlen, loc, sortlen);
9592 memcpy (loc, buf, runlen);
9593 }
9594 else
9595 {
9596 memcpy (buf, loc, sortlen);
9597 memmove (loc, p, runlen);
9598 memcpy (loc + runlen, buf, sortlen);
9599 }
9600 p += runlen - elt_size;
9601 }
9602 }
9603 /* Hashes are no longer valid. */
9604 free (reldata->hashes);
9605 reldata->hashes = NULL;
9606 free (buf);
9607 }
9608 return true;
9609 }
9610
9611 struct elf_link_sort_rela
9612 {
9613 union {
9614 bfd_vma offset;
9615 bfd_vma sym_mask;
9616 } u;
9617 enum elf_reloc_type_class type;
9618 /* We use this as an array of size int_rels_per_ext_rel. */
9619 Elf_Internal_Rela rela[1];
9620 };
9621
9622 /* qsort stability here and for cmp2 is only an issue if multiple
9623 dynamic relocations are emitted at the same address. But targets
9624 that apply a series of dynamic relocations each operating on the
9625 result of the prior relocation can't use -z combreloc as
9626 implemented anyway. Such schemes tend to be broken by sorting on
9627 symbol index. That leaves dynamic NONE relocs as the only other
9628 case where ld might emit multiple relocs at the same address, and
9629 those are only emitted due to target bugs. */
9630
9631 static int
9632 elf_link_sort_cmp1 (const void *A, const void *B)
9633 {
9634 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9635 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9636 int relativea, relativeb;
9637
9638 relativea = a->type == reloc_class_relative;
9639 relativeb = b->type == reloc_class_relative;
9640
9641 if (relativea < relativeb)
9642 return 1;
9643 if (relativea > relativeb)
9644 return -1;
9645 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
9646 return -1;
9647 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
9648 return 1;
9649 if (a->rela->r_offset < b->rela->r_offset)
9650 return -1;
9651 if (a->rela->r_offset > b->rela->r_offset)
9652 return 1;
9653 return 0;
9654 }
9655
9656 static int
9657 elf_link_sort_cmp2 (const void *A, const void *B)
9658 {
9659 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9660 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9661
9662 if (a->type < b->type)
9663 return -1;
9664 if (a->type > b->type)
9665 return 1;
9666 if (a->u.offset < b->u.offset)
9667 return -1;
9668 if (a->u.offset > b->u.offset)
9669 return 1;
9670 if (a->rela->r_offset < b->rela->r_offset)
9671 return -1;
9672 if (a->rela->r_offset > b->rela->r_offset)
9673 return 1;
9674 return 0;
9675 }
9676
9677 static size_t
9678 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9679 {
9680 asection *dynamic_relocs;
9681 asection *rela_dyn;
9682 asection *rel_dyn;
9683 bfd_size_type count, size;
9684 size_t i, ret, sort_elt, ext_size;
9685 bfd_byte *sort, *s_non_relative, *p;
9686 struct elf_link_sort_rela *sq;
9687 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9688 int i2e = bed->s->int_rels_per_ext_rel;
9689 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
9690 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9691 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9692 struct bfd_link_order *lo;
9693 bfd_vma r_sym_mask;
9694 bool use_rela;
9695
9696 /* Find a dynamic reloc section. */
9697 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9698 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
9699 if (rela_dyn != NULL && rela_dyn->size > 0
9700 && rel_dyn != NULL && rel_dyn->size > 0)
9701 {
9702 bool use_rela_initialised = false;
9703
9704 /* This is just here to stop gcc from complaining.
9705 Its initialization checking code is not perfect. */
9706 use_rela = true;
9707
9708 /* Both sections are present. Examine the sizes
9709 of the indirect sections to help us choose. */
9710 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9711 if (lo->type == bfd_indirect_link_order)
9712 {
9713 asection *o = lo->u.indirect.section;
9714
9715 if ((o->size % bed->s->sizeof_rela) == 0)
9716 {
9717 if ((o->size % bed->s->sizeof_rel) == 0)
9718 /* Section size is divisible by both rel and rela sizes.
9719 It is of no help to us. */
9720 ;
9721 else
9722 {
9723 /* Section size is only divisible by rela. */
9724 if (use_rela_initialised && !use_rela)
9725 {
9726 _bfd_error_handler (_("%pB: unable to sort relocs - "
9727 "they are in more than one size"),
9728 abfd);
9729 bfd_set_error (bfd_error_invalid_operation);
9730 return 0;
9731 }
9732 else
9733 {
9734 use_rela = true;
9735 use_rela_initialised = true;
9736 }
9737 }
9738 }
9739 else if ((o->size % bed->s->sizeof_rel) == 0)
9740 {
9741 /* Section size is only divisible by rel. */
9742 if (use_rela_initialised && use_rela)
9743 {
9744 _bfd_error_handler (_("%pB: unable to sort relocs - "
9745 "they are in more than one size"),
9746 abfd);
9747 bfd_set_error (bfd_error_invalid_operation);
9748 return 0;
9749 }
9750 else
9751 {
9752 use_rela = false;
9753 use_rela_initialised = true;
9754 }
9755 }
9756 else
9757 {
9758 /* The section size is not divisible by either -
9759 something is wrong. */
9760 _bfd_error_handler (_("%pB: unable to sort relocs - "
9761 "they are of an unknown size"), abfd);
9762 bfd_set_error (bfd_error_invalid_operation);
9763 return 0;
9764 }
9765 }
9766
9767 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9768 if (lo->type == bfd_indirect_link_order)
9769 {
9770 asection *o = lo->u.indirect.section;
9771
9772 if ((o->size % bed->s->sizeof_rela) == 0)
9773 {
9774 if ((o->size % bed->s->sizeof_rel) == 0)
9775 /* Section size is divisible by both rel and rela sizes.
9776 It is of no help to us. */
9777 ;
9778 else
9779 {
9780 /* Section size is only divisible by rela. */
9781 if (use_rela_initialised && !use_rela)
9782 {
9783 _bfd_error_handler (_("%pB: unable to sort relocs - "
9784 "they are in more than one size"),
9785 abfd);
9786 bfd_set_error (bfd_error_invalid_operation);
9787 return 0;
9788 }
9789 else
9790 {
9791 use_rela = true;
9792 use_rela_initialised = true;
9793 }
9794 }
9795 }
9796 else if ((o->size % bed->s->sizeof_rel) == 0)
9797 {
9798 /* Section size is only divisible by rel. */
9799 if (use_rela_initialised && use_rela)
9800 {
9801 _bfd_error_handler (_("%pB: unable to sort relocs - "
9802 "they are in more than one size"),
9803 abfd);
9804 bfd_set_error (bfd_error_invalid_operation);
9805 return 0;
9806 }
9807 else
9808 {
9809 use_rela = false;
9810 use_rela_initialised = true;
9811 }
9812 }
9813 else
9814 {
9815 /* The section size is not divisible by either -
9816 something is wrong. */
9817 _bfd_error_handler (_("%pB: unable to sort relocs - "
9818 "they are of an unknown size"), abfd);
9819 bfd_set_error (bfd_error_invalid_operation);
9820 return 0;
9821 }
9822 }
9823
9824 if (! use_rela_initialised)
9825 /* Make a guess. */
9826 use_rela = true;
9827 }
9828 else if (rela_dyn != NULL && rela_dyn->size > 0)
9829 use_rela = true;
9830 else if (rel_dyn != NULL && rel_dyn->size > 0)
9831 use_rela = false;
9832 else
9833 return 0;
9834
9835 if (use_rela)
9836 {
9837 dynamic_relocs = rela_dyn;
9838 ext_size = bed->s->sizeof_rela;
9839 swap_in = bed->s->swap_reloca_in;
9840 swap_out = bed->s->swap_reloca_out;
9841 }
9842 else
9843 {
9844 dynamic_relocs = rel_dyn;
9845 ext_size = bed->s->sizeof_rel;
9846 swap_in = bed->s->swap_reloc_in;
9847 swap_out = bed->s->swap_reloc_out;
9848 }
9849
9850 size = 0;
9851 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9852 if (lo->type == bfd_indirect_link_order)
9853 size += lo->u.indirect.section->size;
9854
9855 if (size != dynamic_relocs->size)
9856 return 0;
9857
9858 sort_elt = (sizeof (struct elf_link_sort_rela)
9859 + (i2e - 1) * sizeof (Elf_Internal_Rela));
9860
9861 count = dynamic_relocs->size / ext_size;
9862 if (count == 0)
9863 return 0;
9864 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
9865
9866 if (sort == NULL)
9867 {
9868 (*info->callbacks->warning)
9869 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
9870 return 0;
9871 }
9872
9873 if (bed->s->arch_size == 32)
9874 r_sym_mask = ~(bfd_vma) 0xff;
9875 else
9876 r_sym_mask = ~(bfd_vma) 0xffffffff;
9877
9878 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9879 if (lo->type == bfd_indirect_link_order)
9880 {
9881 bfd_byte *erel, *erelend;
9882 asection *o = lo->u.indirect.section;
9883
9884 if (o->contents == NULL && o->size != 0)
9885 {
9886 /* This is a reloc section that is being handled as a normal
9887 section. See bfd_section_from_shdr. We can't combine
9888 relocs in this case. */
9889 free (sort);
9890 return 0;
9891 }
9892 erel = o->contents;
9893 erelend = o->contents + o->size;
9894 p = sort + o->output_offset * opb / ext_size * sort_elt;
9895
9896 while (erel < erelend)
9897 {
9898 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9899
9900 (*swap_in) (abfd, erel, s->rela);
9901 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
9902 s->u.sym_mask = r_sym_mask;
9903 p += sort_elt;
9904 erel += ext_size;
9905 }
9906 }
9907
9908 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9909
9910 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9911 {
9912 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9913 if (s->type != reloc_class_relative)
9914 break;
9915 }
9916 ret = i;
9917 s_non_relative = p;
9918
9919 sq = (struct elf_link_sort_rela *) s_non_relative;
9920 for (; i < count; i++, p += sort_elt)
9921 {
9922 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9923 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9924 sq = sp;
9925 sp->u.offset = sq->rela->r_offset;
9926 }
9927
9928 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9929
9930 struct elf_link_hash_table *htab = elf_hash_table (info);
9931 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9932 {
9933 /* We have plt relocs in .rela.dyn. */
9934 sq = (struct elf_link_sort_rela *) sort;
9935 for (i = 0; i < count; i++)
9936 if (sq[count - i - 1].type != reloc_class_plt)
9937 break;
9938 if (i != 0 && htab->srelplt->size == i * ext_size)
9939 {
9940 struct bfd_link_order **plo;
9941 /* Put srelplt link_order last. This is so the output_offset
9942 set in the next loop is correct for DT_JMPREL. */
9943 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9944 if ((*plo)->type == bfd_indirect_link_order
9945 && (*plo)->u.indirect.section == htab->srelplt)
9946 {
9947 lo = *plo;
9948 *plo = lo->next;
9949 }
9950 else
9951 plo = &(*plo)->next;
9952 *plo = lo;
9953 lo->next = NULL;
9954 dynamic_relocs->map_tail.link_order = lo;
9955 }
9956 }
9957
9958 p = sort;
9959 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9960 if (lo->type == bfd_indirect_link_order)
9961 {
9962 bfd_byte *erel, *erelend;
9963 asection *o = lo->u.indirect.section;
9964
9965 erel = o->contents;
9966 erelend = o->contents + o->size;
9967 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
9968 while (erel < erelend)
9969 {
9970 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9971 (*swap_out) (abfd, s->rela, erel);
9972 p += sort_elt;
9973 erel += ext_size;
9974 }
9975 }
9976
9977 free (sort);
9978 *psec = dynamic_relocs;
9979 return ret;
9980 }
9981
9982 /* Add a symbol to the output symbol string table. */
9983
9984 static int
9985 elf_link_output_symstrtab (void *finf,
9986 const char *name,
9987 Elf_Internal_Sym *elfsym,
9988 asection *input_sec,
9989 struct elf_link_hash_entry *h)
9990 {
9991 struct elf_final_link_info *flinfo = finf;
9992 int (*output_symbol_hook)
9993 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9994 struct elf_link_hash_entry *);
9995 struct elf_link_hash_table *hash_table;
9996 const struct elf_backend_data *bed;
9997 bfd_size_type strtabsize;
9998
9999 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
10000
10001 bed = get_elf_backend_data (flinfo->output_bfd);
10002 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
10003 if (output_symbol_hook != NULL)
10004 {
10005 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
10006 if (ret != 1)
10007 return ret;
10008 }
10009
10010 if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC)
10011 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
10012 if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE)
10013 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique;
10014
10015 if (name == NULL || *name == '\0')
10016 elfsym->st_name = (unsigned long) -1;
10017 else
10018 {
10019 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
10020 to get the final offset for st_name. */
10021 char *versioned_name = (char *) name;
10022 if (h != NULL)
10023 {
10024 if (h->versioned == versioned && h->def_dynamic)
10025 {
10026 /* Keep only one '@' for versioned symbols defined in
10027 shared objects. */
10028 char *version = strrchr (name, ELF_VER_CHR);
10029 char *base_end = strchr (name, ELF_VER_CHR);
10030 if (version != base_end)
10031 {
10032 size_t base_len;
10033 size_t len = strlen (name);
10034 versioned_name = bfd_alloc (flinfo->output_bfd, len);
10035 if (versioned_name == NULL)
10036 return 0;
10037 base_len = base_end - name;
10038 memcpy (versioned_name, name, base_len);
10039 memcpy (versioned_name + base_len, version,
10040 len - base_len);
10041 }
10042 }
10043 }
10044 else if (flinfo->info->unique_symbol
10045 && ELF_ST_BIND (elfsym->st_info) == STB_LOCAL)
10046 {
10047 struct local_hash_entry *lh;
10048 size_t count_len;
10049 size_t base_len;
10050 char buf[30];
10051 switch (ELF_ST_TYPE (elfsym->st_info))
10052 {
10053 case STT_FILE:
10054 case STT_SECTION:
10055 break;
10056 default:
10057 lh = (struct local_hash_entry *) bfd_hash_lookup
10058 (&flinfo->local_hash_table, name, true, false);
10059 if (lh == NULL)
10060 return 0;
10061 /* Always append ".COUNT" to local symbols to avoid
10062 potential conflicts with local symbol "XXX.COUNT". */
10063 sprintf (buf, "%lx", lh->count);
10064 base_len = lh->size;
10065 if (!base_len)
10066 {
10067 base_len = strlen (name);
10068 lh->size = base_len;
10069 }
10070 count_len = strlen (buf);
10071 versioned_name = bfd_alloc (flinfo->output_bfd,
10072 base_len + count_len + 2);
10073 if (versioned_name == NULL)
10074 return 0;
10075 memcpy (versioned_name, name, base_len);
10076 versioned_name[base_len] = '.';
10077 memcpy (versioned_name + base_len + 1, buf,
10078 count_len + 1);
10079 lh->count++;
10080 break;
10081 }
10082 }
10083 elfsym->st_name
10084 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
10085 versioned_name, false);
10086 if (elfsym->st_name == (unsigned long) -1)
10087 return 0;
10088 }
10089
10090 hash_table = elf_hash_table (flinfo->info);
10091 strtabsize = hash_table->strtabsize;
10092 if (strtabsize <= flinfo->output_bfd->symcount)
10093 {
10094 strtabsize += strtabsize;
10095 hash_table->strtabsize = strtabsize;
10096 strtabsize *= sizeof (*hash_table->strtab);
10097 hash_table->strtab
10098 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
10099 strtabsize);
10100 if (hash_table->strtab == NULL)
10101 return 0;
10102 }
10103 hash_table->strtab[flinfo->output_bfd->symcount].sym = *elfsym;
10104 hash_table->strtab[flinfo->output_bfd->symcount].dest_index
10105 = flinfo->output_bfd->symcount;
10106 flinfo->output_bfd->symcount += 1;
10107
10108 return 1;
10109 }
10110
10111 /* Swap symbols out to the symbol table and flush the output symbols to
10112 the file. */
10113
10114 static bool
10115 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
10116 {
10117 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
10118 size_t amt;
10119 size_t i;
10120 const struct elf_backend_data *bed;
10121 bfd_byte *symbuf;
10122 Elf_Internal_Shdr *hdr;
10123 file_ptr pos;
10124 bool ret;
10125
10126 if (flinfo->output_bfd->symcount == 0)
10127 return true;
10128
10129 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
10130
10131 bed = get_elf_backend_data (flinfo->output_bfd);
10132
10133 amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount;
10134 symbuf = (bfd_byte *) bfd_malloc (amt);
10135 if (symbuf == NULL)
10136 return false;
10137
10138 if (flinfo->symshndxbuf)
10139 {
10140 amt = sizeof (Elf_External_Sym_Shndx);
10141 amt *= bfd_get_symcount (flinfo->output_bfd);
10142 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
10143 if (flinfo->symshndxbuf == NULL)
10144 {
10145 free (symbuf);
10146 return false;
10147 }
10148 }
10149
10150 /* Now swap out the symbols. */
10151 for (i = 0; i < flinfo->output_bfd->symcount; i++)
10152 {
10153 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
10154 if (elfsym->sym.st_name == (unsigned long) -1)
10155 elfsym->sym.st_name = 0;
10156 else
10157 elfsym->sym.st_name
10158 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
10159 elfsym->sym.st_name);
10160
10161 /* Inform the linker of the addition of this symbol. */
10162
10163 if (flinfo->info->callbacks->ctf_new_symbol)
10164 flinfo->info->callbacks->ctf_new_symbol (elfsym->dest_index,
10165 &elfsym->sym);
10166
10167 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
10168 ((bfd_byte *) symbuf
10169 + (elfsym->dest_index
10170 * bed->s->sizeof_sym)),
10171 NPTR_ADD (flinfo->symshndxbuf,
10172 elfsym->dest_index));
10173 }
10174
10175 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
10176 pos = hdr->sh_offset + hdr->sh_size;
10177 amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount;
10178 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
10179 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
10180 {
10181 hdr->sh_size += amt;
10182 ret = true;
10183 }
10184 else
10185 ret = false;
10186
10187 free (symbuf);
10188
10189 free (hash_table->strtab);
10190 hash_table->strtab = NULL;
10191
10192 return ret;
10193 }
10194
10195 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
10196
10197 static bool
10198 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
10199 {
10200 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
10201 && sym->st_shndx < SHN_LORESERVE)
10202 {
10203 /* The gABI doesn't support dynamic symbols in output sections
10204 beyond 64k. */
10205 _bfd_error_handler
10206 /* xgettext:c-format */
10207 (_("%pB: too many sections: %d (>= %d)"),
10208 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
10209 bfd_set_error (bfd_error_nonrepresentable_section);
10210 return false;
10211 }
10212 return true;
10213 }
10214
10215 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
10216 allowing an unsatisfied unversioned symbol in the DSO to match a
10217 versioned symbol that would normally require an explicit version.
10218 We also handle the case that a DSO references a hidden symbol
10219 which may be satisfied by a versioned symbol in another DSO. */
10220
10221 static bool
10222 elf_link_check_versioned_symbol (struct bfd_link_info *info,
10223 const struct elf_backend_data *bed,
10224 struct elf_link_hash_entry *h)
10225 {
10226 bfd *abfd;
10227 struct elf_link_loaded_list *loaded;
10228
10229 if (!is_elf_hash_table (info->hash))
10230 return false;
10231
10232 /* Check indirect symbol. */
10233 while (h->root.type == bfd_link_hash_indirect)
10234 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10235
10236 switch (h->root.type)
10237 {
10238 default:
10239 abfd = NULL;
10240 break;
10241
10242 case bfd_link_hash_undefined:
10243 case bfd_link_hash_undefweak:
10244 abfd = h->root.u.undef.abfd;
10245 if (abfd == NULL
10246 || (abfd->flags & DYNAMIC) == 0
10247 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
10248 return false;
10249 break;
10250
10251 case bfd_link_hash_defined:
10252 case bfd_link_hash_defweak:
10253 abfd = h->root.u.def.section->owner;
10254 break;
10255
10256 case bfd_link_hash_common:
10257 abfd = h->root.u.c.p->section->owner;
10258 break;
10259 }
10260 BFD_ASSERT (abfd != NULL);
10261
10262 for (loaded = elf_hash_table (info)->dyn_loaded;
10263 loaded != NULL;
10264 loaded = loaded->next)
10265 {
10266 bfd *input;
10267 Elf_Internal_Shdr *hdr;
10268 size_t symcount;
10269 size_t extsymcount;
10270 size_t extsymoff;
10271 Elf_Internal_Shdr *versymhdr;
10272 Elf_Internal_Sym *isym;
10273 Elf_Internal_Sym *isymend;
10274 Elf_Internal_Sym *isymbuf;
10275 Elf_External_Versym *ever;
10276 Elf_External_Versym *extversym;
10277
10278 input = loaded->abfd;
10279
10280 /* We check each DSO for a possible hidden versioned definition. */
10281 if (input == abfd
10282 || elf_dynversym (input) == 0)
10283 continue;
10284
10285 hdr = &elf_tdata (input)->dynsymtab_hdr;
10286
10287 symcount = hdr->sh_size / bed->s->sizeof_sym;
10288 if (elf_bad_symtab (input))
10289 {
10290 extsymcount = symcount;
10291 extsymoff = 0;
10292 }
10293 else
10294 {
10295 extsymcount = symcount - hdr->sh_info;
10296 extsymoff = hdr->sh_info;
10297 }
10298
10299 if (extsymcount == 0)
10300 continue;
10301
10302 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
10303 NULL, NULL, NULL);
10304 if (isymbuf == NULL)
10305 return false;
10306
10307 /* Read in any version definitions. */
10308 versymhdr = &elf_tdata (input)->dynversym_hdr;
10309 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
10310 || (extversym = (Elf_External_Versym *)
10311 _bfd_malloc_and_read (input, versymhdr->sh_size,
10312 versymhdr->sh_size)) == NULL)
10313 {
10314 free (isymbuf);
10315 return false;
10316 }
10317
10318 ever = extversym + extsymoff;
10319 isymend = isymbuf + extsymcount;
10320 for (isym = isymbuf; isym < isymend; isym++, ever++)
10321 {
10322 const char *name;
10323 Elf_Internal_Versym iver;
10324 unsigned short version_index;
10325
10326 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
10327 || isym->st_shndx == SHN_UNDEF)
10328 continue;
10329
10330 name = bfd_elf_string_from_elf_section (input,
10331 hdr->sh_link,
10332 isym->st_name);
10333 if (strcmp (name, h->root.root.string) != 0)
10334 continue;
10335
10336 _bfd_elf_swap_versym_in (input, ever, &iver);
10337
10338 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
10339 && !(h->def_regular
10340 && h->forced_local))
10341 {
10342 /* If we have a non-hidden versioned sym, then it should
10343 have provided a definition for the undefined sym unless
10344 it is defined in a non-shared object and forced local.
10345 */
10346 abort ();
10347 }
10348
10349 version_index = iver.vs_vers & VERSYM_VERSION;
10350 if (version_index == 1 || version_index == 2)
10351 {
10352 /* This is the base or first version. We can use it. */
10353 free (extversym);
10354 free (isymbuf);
10355 return true;
10356 }
10357 }
10358
10359 free (extversym);
10360 free (isymbuf);
10361 }
10362
10363 return false;
10364 }
10365
10366 /* Convert ELF common symbol TYPE. */
10367
10368 static int
10369 elf_link_convert_common_type (struct bfd_link_info *info, int type)
10370 {
10371 /* Commom symbol can only appear in relocatable link. */
10372 if (!bfd_link_relocatable (info))
10373 abort ();
10374 switch (info->elf_stt_common)
10375 {
10376 case unchanged:
10377 break;
10378 case elf_stt_common:
10379 type = STT_COMMON;
10380 break;
10381 case no_elf_stt_common:
10382 type = STT_OBJECT;
10383 break;
10384 }
10385 return type;
10386 }
10387
10388 /* Add an external symbol to the symbol table. This is called from
10389 the hash table traversal routine. When generating a shared object,
10390 we go through the symbol table twice. The first time we output
10391 anything that might have been forced to local scope in a version
10392 script. The second time we output the symbols that are still
10393 global symbols. */
10394
10395 static bool
10396 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
10397 {
10398 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
10399 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
10400 struct elf_final_link_info *flinfo = eoinfo->flinfo;
10401 bool strip;
10402 Elf_Internal_Sym sym;
10403 asection *input_sec;
10404 const struct elf_backend_data *bed;
10405 long indx;
10406 int ret;
10407 unsigned int type;
10408
10409 if (h->root.type == bfd_link_hash_warning)
10410 {
10411 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10412 if (h->root.type == bfd_link_hash_new)
10413 return true;
10414 }
10415
10416 /* Decide whether to output this symbol in this pass. */
10417 if (eoinfo->localsyms)
10418 {
10419 if (!h->forced_local)
10420 return true;
10421 }
10422 else
10423 {
10424 if (h->forced_local)
10425 return true;
10426 }
10427
10428 bed = get_elf_backend_data (flinfo->output_bfd);
10429
10430 if (h->root.type == bfd_link_hash_undefined)
10431 {
10432 /* If we have an undefined symbol reference here then it must have
10433 come from a shared library that is being linked in. (Undefined
10434 references in regular files have already been handled unless
10435 they are in unreferenced sections which are removed by garbage
10436 collection). */
10437 bool ignore_undef = false;
10438
10439 /* Some symbols may be special in that the fact that they're
10440 undefined can be safely ignored - let backend determine that. */
10441 if (bed->elf_backend_ignore_undef_symbol)
10442 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
10443
10444 /* If we are reporting errors for this situation then do so now. */
10445 if (!ignore_undef
10446 && h->ref_dynamic_nonweak
10447 && (!h->ref_regular || flinfo->info->gc_sections)
10448 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
10449 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
10450 {
10451 flinfo->info->callbacks->undefined_symbol
10452 (flinfo->info, h->root.root.string,
10453 h->ref_regular ? NULL : h->root.u.undef.abfd, NULL, 0,
10454 flinfo->info->unresolved_syms_in_shared_libs == RM_DIAGNOSE
10455 && !flinfo->info->warn_unresolved_syms);
10456 }
10457
10458 /* Strip a global symbol defined in a discarded section. */
10459 if (h->indx == -3)
10460 return true;
10461 }
10462
10463 /* We should also warn if a forced local symbol is referenced from
10464 shared libraries. */
10465 if (bfd_link_executable (flinfo->info)
10466 && h->forced_local
10467 && h->ref_dynamic
10468 && h->def_regular
10469 && !h->dynamic_def
10470 && h->ref_dynamic_nonweak
10471 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
10472 {
10473 bfd *def_bfd;
10474 const char *msg;
10475 struct elf_link_hash_entry *hi = h;
10476
10477 /* Check indirect symbol. */
10478 while (hi->root.type == bfd_link_hash_indirect)
10479 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
10480
10481 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
10482 /* xgettext:c-format */
10483 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
10484 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
10485 /* xgettext:c-format */
10486 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
10487 else
10488 /* xgettext:c-format */
10489 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
10490 def_bfd = flinfo->output_bfd;
10491 if (hi->root.u.def.section != bfd_abs_section_ptr)
10492 def_bfd = hi->root.u.def.section->owner;
10493 _bfd_error_handler (msg, flinfo->output_bfd,
10494 h->root.root.string, def_bfd);
10495 bfd_set_error (bfd_error_bad_value);
10496 eoinfo->failed = true;
10497 return false;
10498 }
10499
10500 /* We don't want to output symbols that have never been mentioned by
10501 a regular file, or that we have been told to strip. However, if
10502 h->indx is set to -2, the symbol is used by a reloc and we must
10503 output it. */
10504 strip = false;
10505 if (h->indx == -2)
10506 ;
10507 else if ((h->def_dynamic
10508 || h->ref_dynamic
10509 || h->root.type == bfd_link_hash_new)
10510 && !h->def_regular
10511 && !h->ref_regular)
10512 strip = true;
10513 else if (flinfo->info->strip == strip_all)
10514 strip = true;
10515 else if (flinfo->info->strip == strip_some
10516 && bfd_hash_lookup (flinfo->info->keep_hash,
10517 h->root.root.string, false, false) == NULL)
10518 strip = true;
10519 else if ((h->root.type == bfd_link_hash_defined
10520 || h->root.type == bfd_link_hash_defweak)
10521 && ((flinfo->info->strip_discarded
10522 && discarded_section (h->root.u.def.section))
10523 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
10524 && h->root.u.def.section->owner != NULL
10525 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
10526 strip = true;
10527 else if ((h->root.type == bfd_link_hash_undefined
10528 || h->root.type == bfd_link_hash_undefweak)
10529 && h->root.u.undef.abfd != NULL
10530 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
10531 strip = true;
10532
10533 type = h->type;
10534
10535 /* If we're stripping it, and it's not a dynamic symbol, there's
10536 nothing else to do. However, if it is a forced local symbol or
10537 an ifunc symbol we need to give the backend finish_dynamic_symbol
10538 function a chance to make it dynamic. */
10539 if (strip
10540 && h->dynindx == -1
10541 && type != STT_GNU_IFUNC
10542 && !h->forced_local)
10543 return true;
10544
10545 sym.st_value = 0;
10546 sym.st_size = h->size;
10547 sym.st_other = h->other;
10548 switch (h->root.type)
10549 {
10550 default:
10551 case bfd_link_hash_new:
10552 case bfd_link_hash_warning:
10553 abort ();
10554 return false;
10555
10556 case bfd_link_hash_undefined:
10557 case bfd_link_hash_undefweak:
10558 input_sec = bfd_und_section_ptr;
10559 sym.st_shndx = SHN_UNDEF;
10560 break;
10561
10562 case bfd_link_hash_defined:
10563 case bfd_link_hash_defweak:
10564 {
10565 input_sec = h->root.u.def.section;
10566 if (input_sec->output_section != NULL)
10567 {
10568 sym.st_shndx =
10569 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
10570 input_sec->output_section);
10571 if (sym.st_shndx == SHN_BAD)
10572 {
10573 _bfd_error_handler
10574 /* xgettext:c-format */
10575 (_("%pB: could not find output section %pA for input section %pA"),
10576 flinfo->output_bfd, input_sec->output_section, input_sec);
10577 bfd_set_error (bfd_error_nonrepresentable_section);
10578 eoinfo->failed = true;
10579 return false;
10580 }
10581
10582 /* ELF symbols in relocatable files are section relative,
10583 but in nonrelocatable files they are virtual
10584 addresses. */
10585 sym.st_value = h->root.u.def.value + input_sec->output_offset;
10586 if (!bfd_link_relocatable (flinfo->info))
10587 {
10588 sym.st_value += input_sec->output_section->vma;
10589 if (h->type == STT_TLS)
10590 {
10591 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
10592 if (tls_sec != NULL)
10593 sym.st_value -= tls_sec->vma;
10594 }
10595 }
10596 }
10597 else
10598 {
10599 BFD_ASSERT (input_sec->owner == NULL
10600 || (input_sec->owner->flags & DYNAMIC) != 0);
10601 sym.st_shndx = SHN_UNDEF;
10602 input_sec = bfd_und_section_ptr;
10603 }
10604 }
10605 break;
10606
10607 case bfd_link_hash_common:
10608 input_sec = h->root.u.c.p->section;
10609 sym.st_shndx = bed->common_section_index (input_sec);
10610 sym.st_value = 1 << h->root.u.c.p->alignment_power;
10611 break;
10612
10613 case bfd_link_hash_indirect:
10614 /* These symbols are created by symbol versioning. They point
10615 to the decorated version of the name. For example, if the
10616 symbol foo@@GNU_1.2 is the default, which should be used when
10617 foo is used with no version, then we add an indirect symbol
10618 foo which points to foo@@GNU_1.2. We ignore these symbols,
10619 since the indirected symbol is already in the hash table. */
10620 return true;
10621 }
10622
10623 if (type == STT_COMMON || type == STT_OBJECT)
10624 switch (h->root.type)
10625 {
10626 case bfd_link_hash_common:
10627 type = elf_link_convert_common_type (flinfo->info, type);
10628 break;
10629 case bfd_link_hash_defined:
10630 case bfd_link_hash_defweak:
10631 if (bed->common_definition (&sym))
10632 type = elf_link_convert_common_type (flinfo->info, type);
10633 else
10634 type = STT_OBJECT;
10635 break;
10636 case bfd_link_hash_undefined:
10637 case bfd_link_hash_undefweak:
10638 break;
10639 default:
10640 abort ();
10641 }
10642
10643 if (h->forced_local)
10644 {
10645 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
10646 /* Turn off visibility on local symbol. */
10647 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
10648 }
10649 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10650 else if (h->unique_global && h->def_regular)
10651 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
10652 else if (h->root.type == bfd_link_hash_undefweak
10653 || h->root.type == bfd_link_hash_defweak)
10654 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
10655 else
10656 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
10657 sym.st_target_internal = h->target_internal;
10658
10659 /* Give the processor backend a chance to tweak the symbol value,
10660 and also to finish up anything that needs to be done for this
10661 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
10662 forced local syms when non-shared is due to a historical quirk.
10663 STT_GNU_IFUNC symbol must go through PLT. */
10664 if ((h->type == STT_GNU_IFUNC
10665 && h->def_regular
10666 && !bfd_link_relocatable (flinfo->info))
10667 || ((h->dynindx != -1
10668 || h->forced_local)
10669 && ((bfd_link_pic (flinfo->info)
10670 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
10671 || h->root.type != bfd_link_hash_undefweak))
10672 || !h->forced_local)
10673 && elf_hash_table (flinfo->info)->dynamic_sections_created))
10674 {
10675 if (! ((*bed->elf_backend_finish_dynamic_symbol)
10676 (flinfo->output_bfd, flinfo->info, h, &sym)))
10677 {
10678 eoinfo->failed = true;
10679 return false;
10680 }
10681 }
10682
10683 /* If we are marking the symbol as undefined, and there are no
10684 non-weak references to this symbol from a regular object, then
10685 mark the symbol as weak undefined; if there are non-weak
10686 references, mark the symbol as strong. We can't do this earlier,
10687 because it might not be marked as undefined until the
10688 finish_dynamic_symbol routine gets through with it. */
10689 if (sym.st_shndx == SHN_UNDEF
10690 && h->ref_regular
10691 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
10692 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
10693 {
10694 int bindtype;
10695 type = ELF_ST_TYPE (sym.st_info);
10696
10697 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10698 if (type == STT_GNU_IFUNC)
10699 type = STT_FUNC;
10700
10701 if (h->ref_regular_nonweak)
10702 bindtype = STB_GLOBAL;
10703 else
10704 bindtype = STB_WEAK;
10705 sym.st_info = ELF_ST_INFO (bindtype, type);
10706 }
10707
10708 /* If this is a symbol defined in a dynamic library, don't use the
10709 symbol size from the dynamic library. Relinking an executable
10710 against a new library may introduce gratuitous changes in the
10711 executable's symbols if we keep the size. */
10712 if (sym.st_shndx == SHN_UNDEF
10713 && !h->def_regular
10714 && h->def_dynamic)
10715 sym.st_size = 0;
10716
10717 /* If a non-weak symbol with non-default visibility is not defined
10718 locally, it is a fatal error. */
10719 if (!bfd_link_relocatable (flinfo->info)
10720 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
10721 && ELF_ST_BIND (sym.st_info) != STB_WEAK
10722 && h->root.type == bfd_link_hash_undefined
10723 && !h->def_regular)
10724 {
10725 const char *msg;
10726
10727 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
10728 /* xgettext:c-format */
10729 msg = _("%pB: protected symbol `%s' isn't defined");
10730 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
10731 /* xgettext:c-format */
10732 msg = _("%pB: internal symbol `%s' isn't defined");
10733 else
10734 /* xgettext:c-format */
10735 msg = _("%pB: hidden symbol `%s' isn't defined");
10736 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
10737 bfd_set_error (bfd_error_bad_value);
10738 eoinfo->failed = true;
10739 return false;
10740 }
10741
10742 /* If this symbol should be put in the .dynsym section, then put it
10743 there now. We already know the symbol index. We also fill in
10744 the entry in the .hash section. */
10745 if (h->dynindx != -1
10746 && elf_hash_table (flinfo->info)->dynamic_sections_created
10747 && elf_hash_table (flinfo->info)->dynsym != NULL
10748 && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
10749 {
10750 bfd_byte *esym;
10751
10752 /* Since there is no version information in the dynamic string,
10753 if there is no version info in symbol version section, we will
10754 have a run-time problem if not linking executable, referenced
10755 by shared library, or not bound locally. */
10756 if (h->verinfo.verdef == NULL
10757 && (!bfd_link_executable (flinfo->info)
10758 || h->ref_dynamic
10759 || !h->def_regular))
10760 {
10761 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
10762
10763 if (p && p [1] != '\0')
10764 {
10765 _bfd_error_handler
10766 /* xgettext:c-format */
10767 (_("%pB: no symbol version section for versioned symbol `%s'"),
10768 flinfo->output_bfd, h->root.root.string);
10769 eoinfo->failed = true;
10770 return false;
10771 }
10772 }
10773
10774 sym.st_name = h->dynstr_index;
10775 esym = (elf_hash_table (flinfo->info)->dynsym->contents
10776 + h->dynindx * bed->s->sizeof_sym);
10777 if (!check_dynsym (flinfo->output_bfd, &sym))
10778 {
10779 eoinfo->failed = true;
10780 return false;
10781 }
10782
10783 /* Inform the linker of the addition of this symbol. */
10784
10785 if (flinfo->info->callbacks->ctf_new_dynsym)
10786 flinfo->info->callbacks->ctf_new_dynsym (h->dynindx, &sym);
10787
10788 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
10789
10790 if (flinfo->hash_sec != NULL)
10791 {
10792 size_t hash_entry_size;
10793 bfd_byte *bucketpos;
10794 bfd_vma chain;
10795 size_t bucketcount;
10796 size_t bucket;
10797
10798 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
10799 bucket = h->u.elf_hash_value % bucketcount;
10800
10801 hash_entry_size
10802 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
10803 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
10804 + (bucket + 2) * hash_entry_size);
10805 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
10806 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
10807 bucketpos);
10808 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
10809 ((bfd_byte *) flinfo->hash_sec->contents
10810 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
10811 }
10812
10813 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
10814 {
10815 Elf_Internal_Versym iversym;
10816 Elf_External_Versym *eversym;
10817
10818 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
10819 {
10820 if (h->verinfo.verdef == NULL
10821 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
10822 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
10823 iversym.vs_vers = 1;
10824 else
10825 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
10826 }
10827 else
10828 {
10829 if (h->verinfo.vertree == NULL)
10830 iversym.vs_vers = 1;
10831 else
10832 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
10833 if (flinfo->info->create_default_symver)
10834 iversym.vs_vers++;
10835 }
10836
10837 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
10838 defined locally. */
10839 if (h->versioned == versioned_hidden && h->def_regular)
10840 iversym.vs_vers |= VERSYM_HIDDEN;
10841
10842 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
10843 eversym += h->dynindx;
10844 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
10845 }
10846 }
10847
10848 /* If the symbol is undefined, and we didn't output it to .dynsym,
10849 strip it from .symtab too. Obviously we can't do this for
10850 relocatable output or when needed for --emit-relocs. */
10851 else if (input_sec == bfd_und_section_ptr
10852 && h->indx != -2
10853 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10854 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
10855 && !bfd_link_relocatable (flinfo->info))
10856 return true;
10857
10858 /* Also strip others that we couldn't earlier due to dynamic symbol
10859 processing. */
10860 if (strip)
10861 return true;
10862 if ((input_sec->flags & SEC_EXCLUDE) != 0)
10863 return true;
10864
10865 /* Output a FILE symbol so that following locals are not associated
10866 with the wrong input file. We need one for forced local symbols
10867 if we've seen more than one FILE symbol or when we have exactly
10868 one FILE symbol but global symbols are present in a file other
10869 than the one with the FILE symbol. We also need one if linker
10870 defined symbols are present. In practice these conditions are
10871 always met, so just emit the FILE symbol unconditionally. */
10872 if (eoinfo->localsyms
10873 && !eoinfo->file_sym_done
10874 && eoinfo->flinfo->filesym_count != 0)
10875 {
10876 Elf_Internal_Sym fsym;
10877
10878 memset (&fsym, 0, sizeof (fsym));
10879 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10880 fsym.st_shndx = SHN_ABS;
10881 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10882 bfd_und_section_ptr, NULL))
10883 return false;
10884
10885 eoinfo->file_sym_done = true;
10886 }
10887
10888 indx = bfd_get_symcount (flinfo->output_bfd);
10889 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10890 input_sec, h);
10891 if (ret == 0)
10892 {
10893 eoinfo->failed = true;
10894 return false;
10895 }
10896 else if (ret == 1)
10897 h->indx = indx;
10898 else if (h->indx == -2)
10899 abort();
10900
10901 return true;
10902 }
10903
10904 /* Return TRUE if special handling is done for relocs in SEC against
10905 symbols defined in discarded sections. */
10906
10907 static bool
10908 elf_section_ignore_discarded_relocs (asection *sec)
10909 {
10910 const struct elf_backend_data *bed;
10911
10912 switch (sec->sec_info_type)
10913 {
10914 case SEC_INFO_TYPE_STABS:
10915 case SEC_INFO_TYPE_EH_FRAME:
10916 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10917 case SEC_INFO_TYPE_SFRAME:
10918 return true;
10919 default:
10920 break;
10921 }
10922
10923 bed = get_elf_backend_data (sec->owner);
10924 if (bed->elf_backend_ignore_discarded_relocs != NULL
10925 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10926 return true;
10927
10928 return false;
10929 }
10930
10931 /* Return a mask saying how ld should treat relocations in SEC against
10932 symbols defined in discarded sections. If this function returns
10933 COMPLAIN set, ld will issue a warning message. If this function
10934 returns PRETEND set, and the discarded section was link-once and the
10935 same size as the kept link-once section, ld will pretend that the
10936 symbol was actually defined in the kept section. Otherwise ld will
10937 zero the reloc (at least that is the intent, but some cooperation by
10938 the target dependent code is needed, particularly for REL targets). */
10939
10940 unsigned int
10941 _bfd_elf_default_action_discarded (asection *sec)
10942 {
10943 const struct elf_backend_data *bed;
10944 bed = get_elf_backend_data (sec->owner);
10945
10946 if (sec->flags & SEC_DEBUGGING)
10947 return PRETEND;
10948
10949 if (strcmp (".eh_frame", sec->name) == 0)
10950 return 0;
10951
10952 if (bed->elf_backend_can_make_multiple_eh_frame
10953 && strncmp (sec->name, ".eh_frame.", 10) == 0)
10954 return 0;
10955
10956 if (strcmp (".sframe", sec->name) == 0)
10957 return 0;
10958
10959 if (strcmp (".gcc_except_table", sec->name) == 0)
10960 return 0;
10961
10962 return COMPLAIN | PRETEND;
10963 }
10964
10965 /* Find a match between a section and a member of a section group. */
10966
10967 static asection *
10968 match_group_member (asection *sec, asection *group,
10969 struct bfd_link_info *info)
10970 {
10971 asection *first = elf_next_in_group (group);
10972 asection *s = first;
10973
10974 while (s != NULL)
10975 {
10976 if (bfd_elf_match_symbols_in_sections (s, sec, info))
10977 return s;
10978
10979 s = elf_next_in_group (s);
10980 if (s == first)
10981 break;
10982 }
10983
10984 return NULL;
10985 }
10986
10987 /* Check if the kept section of a discarded section SEC can be used
10988 to replace it. Return the replacement if it is OK. Otherwise return
10989 NULL. */
10990
10991 asection *
10992 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
10993 {
10994 asection *kept;
10995
10996 kept = sec->kept_section;
10997 if (kept != NULL)
10998 {
10999 if ((kept->flags & SEC_GROUP) != 0)
11000 kept = match_group_member (sec, kept, info);
11001 if (kept != NULL)
11002 {
11003 if ((sec->rawsize != 0 ? sec->rawsize : sec->size)
11004 != (kept->rawsize != 0 ? kept->rawsize : kept->size))
11005 kept = NULL;
11006 else
11007 {
11008 /* Get the real kept section. */
11009 asection *next;
11010 for (next = kept->kept_section;
11011 next != NULL;
11012 next = next->kept_section)
11013 kept = next;
11014 }
11015 }
11016 sec->kept_section = kept;
11017 }
11018 return kept;
11019 }
11020
11021 /* Link an input file into the linker output file. This function
11022 handles all the sections and relocations of the input file at once.
11023 This is so that we only have to read the local symbols once, and
11024 don't have to keep them in memory. */
11025
11026 static bool
11027 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
11028 {
11029 int (*relocate_section)
11030 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
11031 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
11032 bfd *output_bfd;
11033 Elf_Internal_Shdr *symtab_hdr;
11034 size_t locsymcount;
11035 size_t extsymoff;
11036 Elf_Internal_Sym *isymbuf;
11037 Elf_Internal_Sym *isym;
11038 Elf_Internal_Sym *isymend;
11039 long *pindex;
11040 asection **ppsection;
11041 asection *o;
11042 const struct elf_backend_data *bed;
11043 struct elf_link_hash_entry **sym_hashes;
11044 bfd_size_type address_size;
11045 bfd_vma r_type_mask;
11046 int r_sym_shift;
11047 bool have_file_sym = false;
11048
11049 output_bfd = flinfo->output_bfd;
11050 bed = get_elf_backend_data (output_bfd);
11051 relocate_section = bed->elf_backend_relocate_section;
11052
11053 /* If this is a dynamic object, we don't want to do anything here:
11054 we don't want the local symbols, and we don't want the section
11055 contents. */
11056 if ((input_bfd->flags & DYNAMIC) != 0)
11057 return true;
11058
11059 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
11060 if (elf_bad_symtab (input_bfd))
11061 {
11062 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
11063 extsymoff = 0;
11064 }
11065 else
11066 {
11067 locsymcount = symtab_hdr->sh_info;
11068 extsymoff = symtab_hdr->sh_info;
11069 }
11070
11071 /* Enable GNU OSABI features in the output BFD that are used in the input
11072 BFD. */
11073 if (bed->elf_osabi == ELFOSABI_NONE
11074 || bed->elf_osabi == ELFOSABI_GNU
11075 || bed->elf_osabi == ELFOSABI_FREEBSD)
11076 elf_tdata (output_bfd)->has_gnu_osabi
11077 |= (elf_tdata (input_bfd)->has_gnu_osabi
11078 & (bfd_link_relocatable (flinfo->info)
11079 ? -1 : ~elf_gnu_osabi_retain));
11080
11081 /* Read the local symbols. */
11082 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
11083 if (isymbuf == NULL && locsymcount != 0)
11084 {
11085 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
11086 flinfo->internal_syms,
11087 flinfo->external_syms,
11088 flinfo->locsym_shndx);
11089 if (isymbuf == NULL)
11090 return false;
11091 }
11092
11093 /* Find local symbol sections and adjust values of symbols in
11094 SEC_MERGE sections. Write out those local symbols we know are
11095 going into the output file. */
11096 isymend = PTR_ADD (isymbuf, locsymcount);
11097 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
11098 isym < isymend;
11099 isym++, pindex++, ppsection++)
11100 {
11101 asection *isec;
11102 const char *name;
11103 Elf_Internal_Sym osym;
11104 long indx;
11105 int ret;
11106
11107 *pindex = -1;
11108
11109 if (elf_bad_symtab (input_bfd))
11110 {
11111 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
11112 {
11113 *ppsection = NULL;
11114 continue;
11115 }
11116 }
11117
11118 if (isym->st_shndx == SHN_UNDEF)
11119 isec = bfd_und_section_ptr;
11120 else if (isym->st_shndx == SHN_ABS)
11121 isec = bfd_abs_section_ptr;
11122 else if (isym->st_shndx == SHN_COMMON)
11123 isec = bfd_com_section_ptr;
11124 else
11125 {
11126 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
11127 if (isec == NULL)
11128 {
11129 /* Don't attempt to output symbols with st_shnx in the
11130 reserved range other than SHN_ABS and SHN_COMMON. */
11131 isec = bfd_und_section_ptr;
11132 }
11133 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
11134 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
11135 isym->st_value =
11136 _bfd_merged_section_offset (output_bfd, &isec,
11137 elf_section_data (isec)->sec_info,
11138 isym->st_value);
11139 }
11140
11141 *ppsection = isec;
11142
11143 /* Don't output the first, undefined, symbol. In fact, don't
11144 output any undefined local symbol. */
11145 if (isec == bfd_und_section_ptr)
11146 continue;
11147
11148 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
11149 {
11150 /* We never output section symbols. Instead, we use the
11151 section symbol of the corresponding section in the output
11152 file. */
11153 continue;
11154 }
11155
11156 /* If we are stripping all symbols, we don't want to output this
11157 one. */
11158 if (flinfo->info->strip == strip_all)
11159 continue;
11160
11161 /* If we are discarding all local symbols, we don't want to
11162 output this one. If we are generating a relocatable output
11163 file, then some of the local symbols may be required by
11164 relocs; we output them below as we discover that they are
11165 needed. */
11166 if (flinfo->info->discard == discard_all)
11167 continue;
11168
11169 /* If this symbol is defined in a section which we are
11170 discarding, we don't need to keep it. */
11171 if (isym->st_shndx < SHN_LORESERVE
11172 && (isec->output_section == NULL
11173 || bfd_section_removed_from_list (output_bfd,
11174 isec->output_section)))
11175 continue;
11176
11177 /* Get the name of the symbol. */
11178 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
11179 isym->st_name);
11180 if (name == NULL)
11181 return false;
11182
11183 /* See if we are discarding symbols with this name. */
11184 if ((flinfo->info->strip == strip_some
11185 && (bfd_hash_lookup (flinfo->info->keep_hash, name, false, false)
11186 == NULL))
11187 || (((flinfo->info->discard == discard_sec_merge
11188 && (isec->flags & SEC_MERGE)
11189 && !bfd_link_relocatable (flinfo->info))
11190 || flinfo->info->discard == discard_l)
11191 && bfd_is_local_label_name (input_bfd, name)))
11192 continue;
11193
11194 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
11195 {
11196 if (input_bfd->lto_output)
11197 /* -flto puts a temp file name here. This means builds
11198 are not reproducible. Discard the symbol. */
11199 continue;
11200 have_file_sym = true;
11201 flinfo->filesym_count += 1;
11202 }
11203 if (!have_file_sym)
11204 {
11205 /* In the absence of debug info, bfd_find_nearest_line uses
11206 FILE symbols to determine the source file for local
11207 function symbols. Provide a FILE symbol here if input
11208 files lack such, so that their symbols won't be
11209 associated with a previous input file. It's not the
11210 source file, but the best we can do. */
11211 const char *filename;
11212 have_file_sym = true;
11213 flinfo->filesym_count += 1;
11214 memset (&osym, 0, sizeof (osym));
11215 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
11216 osym.st_shndx = SHN_ABS;
11217 if (input_bfd->lto_output)
11218 filename = NULL;
11219 else
11220 filename = lbasename (bfd_get_filename (input_bfd));
11221 if (!elf_link_output_symstrtab (flinfo, filename, &osym,
11222 bfd_abs_section_ptr, NULL))
11223 return false;
11224 }
11225
11226 osym = *isym;
11227
11228 /* Adjust the section index for the output file. */
11229 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11230 isec->output_section);
11231 if (osym.st_shndx == SHN_BAD)
11232 return false;
11233
11234 /* ELF symbols in relocatable files are section relative, but
11235 in executable files they are virtual addresses. Note that
11236 this code assumes that all ELF sections have an associated
11237 BFD section with a reasonable value for output_offset; below
11238 we assume that they also have a reasonable value for
11239 output_section. Any special sections must be set up to meet
11240 these requirements. */
11241 osym.st_value += isec->output_offset;
11242 if (!bfd_link_relocatable (flinfo->info))
11243 {
11244 osym.st_value += isec->output_section->vma;
11245 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
11246 {
11247 /* STT_TLS symbols are relative to PT_TLS segment base. */
11248 if (elf_hash_table (flinfo->info)->tls_sec != NULL)
11249 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
11250 else
11251 osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
11252 STT_NOTYPE);
11253 }
11254 }
11255
11256 indx = bfd_get_symcount (output_bfd);
11257 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
11258 if (ret == 0)
11259 return false;
11260 else if (ret == 1)
11261 *pindex = indx;
11262 }
11263
11264 if (bed->s->arch_size == 32)
11265 {
11266 r_type_mask = 0xff;
11267 r_sym_shift = 8;
11268 address_size = 4;
11269 }
11270 else
11271 {
11272 r_type_mask = 0xffffffff;
11273 r_sym_shift = 32;
11274 address_size = 8;
11275 }
11276
11277 /* Relocate the contents of each section. */
11278 sym_hashes = elf_sym_hashes (input_bfd);
11279 for (o = input_bfd->sections; o != NULL; o = o->next)
11280 {
11281 bfd_byte *contents;
11282
11283 if (! o->linker_mark)
11284 {
11285 /* This section was omitted from the link. */
11286 continue;
11287 }
11288
11289 if (!flinfo->info->resolve_section_groups
11290 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
11291 {
11292 /* Deal with the group signature symbol. */
11293 struct bfd_elf_section_data *sec_data = elf_section_data (o);
11294 unsigned long symndx = sec_data->this_hdr.sh_info;
11295 asection *osec = o->output_section;
11296
11297 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
11298 if (symndx >= locsymcount
11299 || (elf_bad_symtab (input_bfd)
11300 && flinfo->sections[symndx] == NULL))
11301 {
11302 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
11303 while (h->root.type == bfd_link_hash_indirect
11304 || h->root.type == bfd_link_hash_warning)
11305 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11306 /* Arrange for symbol to be output. */
11307 h->indx = -2;
11308 elf_section_data (osec)->this_hdr.sh_info = -2;
11309 }
11310 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
11311 {
11312 /* We'll use the output section target_index. */
11313 asection *sec = flinfo->sections[symndx]->output_section;
11314 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
11315 }
11316 else
11317 {
11318 if (flinfo->indices[symndx] == -1)
11319 {
11320 /* Otherwise output the local symbol now. */
11321 Elf_Internal_Sym sym = isymbuf[symndx];
11322 asection *sec = flinfo->sections[symndx]->output_section;
11323 const char *name;
11324 long indx;
11325 int ret;
11326
11327 name = bfd_elf_string_from_elf_section (input_bfd,
11328 symtab_hdr->sh_link,
11329 sym.st_name);
11330 if (name == NULL)
11331 return false;
11332
11333 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11334 sec);
11335 if (sym.st_shndx == SHN_BAD)
11336 return false;
11337
11338 sym.st_value += o->output_offset;
11339
11340 indx = bfd_get_symcount (output_bfd);
11341 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
11342 NULL);
11343 if (ret == 0)
11344 return false;
11345 else if (ret == 1)
11346 flinfo->indices[symndx] = indx;
11347 else
11348 abort ();
11349 }
11350 elf_section_data (osec)->this_hdr.sh_info
11351 = flinfo->indices[symndx];
11352 }
11353 }
11354
11355 if ((o->flags & SEC_HAS_CONTENTS) == 0
11356 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
11357 continue;
11358
11359 if ((o->flags & SEC_LINKER_CREATED) != 0)
11360 {
11361 /* Section was created by _bfd_elf_link_create_dynamic_sections
11362 or somesuch. */
11363 continue;
11364 }
11365
11366 /* Get the contents of the section. They have been cached by a
11367 relaxation routine. Note that o is a section in an input
11368 file, so the contents field will not have been set by any of
11369 the routines which work on output files. */
11370 if (elf_section_data (o)->this_hdr.contents != NULL)
11371 {
11372 contents = elf_section_data (o)->this_hdr.contents;
11373 if (bed->caches_rawsize
11374 && o->rawsize != 0
11375 && o->rawsize < o->size)
11376 {
11377 memcpy (flinfo->contents, contents, o->rawsize);
11378 contents = flinfo->contents;
11379 }
11380 }
11381 else if (!(o->flags & SEC_RELOC)
11382 && !bed->elf_backend_write_section
11383 && o->sec_info_type == SEC_INFO_TYPE_MERGE)
11384 /* A MERGE section that has no relocations doesn't need the
11385 contents anymore, they have been recorded earlier. Except
11386 if the backend has special provisions for writing sections. */
11387 contents = NULL;
11388 else
11389 {
11390 contents = flinfo->contents;
11391 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
11392 return false;
11393 }
11394
11395 if ((o->flags & SEC_RELOC) != 0)
11396 {
11397 Elf_Internal_Rela *internal_relocs;
11398 Elf_Internal_Rela *rel, *relend;
11399 int action_discarded;
11400 int ret;
11401
11402 /* Get the swapped relocs. */
11403 internal_relocs
11404 = _bfd_elf_link_info_read_relocs (input_bfd, flinfo->info, o,
11405 flinfo->external_relocs,
11406 flinfo->internal_relocs,
11407 false);
11408 if (internal_relocs == NULL
11409 && o->reloc_count > 0)
11410 return false;
11411
11412 action_discarded = -1;
11413 if (!elf_section_ignore_discarded_relocs (o))
11414 action_discarded = (*bed->action_discarded) (o);
11415
11416 /* Run through the relocs evaluating complex reloc symbols and
11417 looking for relocs against symbols from discarded sections
11418 or section symbols from removed link-once sections.
11419 Complain about relocs against discarded sections. Zero
11420 relocs against removed link-once sections. */
11421
11422 rel = internal_relocs;
11423 relend = rel + o->reloc_count;
11424 for ( ; rel < relend; rel++)
11425 {
11426 unsigned long r_symndx = rel->r_info >> r_sym_shift;
11427 unsigned int s_type;
11428 asection **ps, *sec;
11429 struct elf_link_hash_entry *h = NULL;
11430 const char *sym_name;
11431
11432 if (r_symndx == STN_UNDEF)
11433 continue;
11434
11435 if (r_symndx >= locsymcount
11436 || (elf_bad_symtab (input_bfd)
11437 && flinfo->sections[r_symndx] == NULL))
11438 {
11439 h = sym_hashes[r_symndx - extsymoff];
11440
11441 /* Badly formatted input files can contain relocs that
11442 reference non-existant symbols. Check here so that
11443 we do not seg fault. */
11444 if (h == NULL)
11445 {
11446 _bfd_error_handler
11447 /* xgettext:c-format */
11448 (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
11449 "that references a non-existent global symbol"),
11450 input_bfd, (uint64_t) rel->r_info, o);
11451 bfd_set_error (bfd_error_bad_value);
11452 return false;
11453 }
11454
11455 while (h->root.type == bfd_link_hash_indirect
11456 || h->root.type == bfd_link_hash_warning)
11457 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11458
11459 s_type = h->type;
11460
11461 /* If a plugin symbol is referenced from a non-IR file,
11462 mark the symbol as undefined. Note that the
11463 linker may attach linker created dynamic sections
11464 to the plugin bfd. Symbols defined in linker
11465 created sections are not plugin symbols. */
11466 if ((h->root.non_ir_ref_regular
11467 || h->root.non_ir_ref_dynamic)
11468 && (h->root.type == bfd_link_hash_defined
11469 || h->root.type == bfd_link_hash_defweak)
11470 && (h->root.u.def.section->flags
11471 & SEC_LINKER_CREATED) == 0
11472 && h->root.u.def.section->owner != NULL
11473 && (h->root.u.def.section->owner->flags
11474 & BFD_PLUGIN) != 0)
11475 {
11476 h->root.type = bfd_link_hash_undefined;
11477 h->root.u.undef.abfd = h->root.u.def.section->owner;
11478 }
11479
11480 ps = NULL;
11481 if (h->root.type == bfd_link_hash_defined
11482 || h->root.type == bfd_link_hash_defweak)
11483 ps = &h->root.u.def.section;
11484
11485 sym_name = h->root.root.string;
11486 }
11487 else
11488 {
11489 Elf_Internal_Sym *sym = isymbuf + r_symndx;
11490
11491 s_type = ELF_ST_TYPE (sym->st_info);
11492 ps = &flinfo->sections[r_symndx];
11493 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
11494 sym, *ps);
11495 }
11496
11497 if ((s_type == STT_RELC || s_type == STT_SRELC)
11498 && !bfd_link_relocatable (flinfo->info))
11499 {
11500 bfd_vma val;
11501 bfd_vma dot = (rel->r_offset
11502 + o->output_offset + o->output_section->vma);
11503 #ifdef DEBUG
11504 printf ("Encountered a complex symbol!");
11505 printf (" (input_bfd %s, section %s, reloc %ld\n",
11506 bfd_get_filename (input_bfd), o->name,
11507 (long) (rel - internal_relocs));
11508 printf (" symbol: idx %8.8lx, name %s\n",
11509 r_symndx, sym_name);
11510 printf (" reloc : info %8.8lx, addr %8.8lx\n",
11511 (unsigned long) rel->r_info,
11512 (unsigned long) rel->r_offset);
11513 #endif
11514 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
11515 isymbuf, locsymcount, s_type == STT_SRELC))
11516 return false;
11517
11518 /* Symbol evaluated OK. Update to absolute value. */
11519 set_symbol_value (input_bfd, isymbuf, locsymcount,
11520 r_symndx, val);
11521 continue;
11522 }
11523
11524 if (action_discarded != -1 && ps != NULL)
11525 {
11526 /* Complain if the definition comes from a
11527 discarded section. */
11528 if ((sec = *ps) != NULL && discarded_section (sec))
11529 {
11530 BFD_ASSERT (r_symndx != STN_UNDEF);
11531 if (action_discarded & COMPLAIN)
11532 (*flinfo->info->callbacks->einfo)
11533 /* xgettext:c-format */
11534 (_("%X`%s' referenced in section `%pA' of %pB: "
11535 "defined in discarded section `%pA' of %pB\n"),
11536 sym_name, o, input_bfd, sec, sec->owner);
11537
11538 /* Try to do the best we can to support buggy old
11539 versions of gcc. Pretend that the symbol is
11540 really defined in the kept linkonce section.
11541 FIXME: This is quite broken. Modifying the
11542 symbol here means we will be changing all later
11543 uses of the symbol, not just in this section. */
11544 if (action_discarded & PRETEND)
11545 {
11546 asection *kept;
11547
11548 kept = _bfd_elf_check_kept_section (sec,
11549 flinfo->info);
11550 if (kept != NULL)
11551 {
11552 *ps = kept;
11553 continue;
11554 }
11555 }
11556 }
11557 }
11558 }
11559
11560 /* Relocate the section by invoking a back end routine.
11561
11562 The back end routine is responsible for adjusting the
11563 section contents as necessary, and (if using Rela relocs
11564 and generating a relocatable output file) adjusting the
11565 reloc addend as necessary.
11566
11567 The back end routine does not have to worry about setting
11568 the reloc address or the reloc symbol index.
11569
11570 The back end routine is given a pointer to the swapped in
11571 internal symbols, and can access the hash table entries
11572 for the external symbols via elf_sym_hashes (input_bfd).
11573
11574 When generating relocatable output, the back end routine
11575 must handle STB_LOCAL/STT_SECTION symbols specially. The
11576 output symbol is going to be a section symbol
11577 corresponding to the output section, which will require
11578 the addend to be adjusted. */
11579
11580 ret = (*relocate_section) (output_bfd, flinfo->info,
11581 input_bfd, o, contents,
11582 internal_relocs,
11583 isymbuf,
11584 flinfo->sections);
11585 if (!ret)
11586 return false;
11587
11588 if (ret == 2
11589 || bfd_link_relocatable (flinfo->info)
11590 || flinfo->info->emitrelocations)
11591 {
11592 Elf_Internal_Rela *irela;
11593 Elf_Internal_Rela *irelaend, *irelamid;
11594 bfd_vma last_offset;
11595 struct elf_link_hash_entry **rel_hash;
11596 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
11597 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
11598 unsigned int next_erel;
11599 bool rela_normal;
11600 struct bfd_elf_section_data *esdi, *esdo;
11601
11602 esdi = elf_section_data (o);
11603 esdo = elf_section_data (o->output_section);
11604 rela_normal = false;
11605
11606 /* Adjust the reloc addresses and symbol indices. */
11607
11608 irela = internal_relocs;
11609 irelaend = irela + o->reloc_count;
11610 rel_hash = PTR_ADD (esdo->rel.hashes, esdo->rel.count);
11611 /* We start processing the REL relocs, if any. When we reach
11612 IRELAMID in the loop, we switch to the RELA relocs. */
11613 irelamid = irela;
11614 if (esdi->rel.hdr != NULL)
11615 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
11616 * bed->s->int_rels_per_ext_rel);
11617 rel_hash_list = rel_hash;
11618 rela_hash_list = NULL;
11619 last_offset = o->output_offset;
11620 if (!bfd_link_relocatable (flinfo->info))
11621 last_offset += o->output_section->vma;
11622 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
11623 {
11624 unsigned long r_symndx;
11625 asection *sec;
11626 Elf_Internal_Sym sym;
11627
11628 if (next_erel == bed->s->int_rels_per_ext_rel)
11629 {
11630 rel_hash++;
11631 next_erel = 0;
11632 }
11633
11634 if (irela == irelamid)
11635 {
11636 rel_hash = PTR_ADD (esdo->rela.hashes, esdo->rela.count);
11637 rela_hash_list = rel_hash;
11638 rela_normal = bed->rela_normal;
11639 }
11640
11641 irela->r_offset = _bfd_elf_section_offset (output_bfd,
11642 flinfo->info, o,
11643 irela->r_offset);
11644 if (irela->r_offset >= (bfd_vma) -2)
11645 {
11646 /* This is a reloc for a deleted entry or somesuch.
11647 Turn it into an R_*_NONE reloc, at the same
11648 offset as the last reloc. elf_eh_frame.c and
11649 bfd_elf_discard_info rely on reloc offsets
11650 being ordered. */
11651 irela->r_offset = last_offset;
11652 irela->r_info = 0;
11653 irela->r_addend = 0;
11654 continue;
11655 }
11656
11657 irela->r_offset += o->output_offset;
11658
11659 /* Relocs in an executable have to be virtual addresses. */
11660 if (!bfd_link_relocatable (flinfo->info))
11661 irela->r_offset += o->output_section->vma;
11662
11663 last_offset = irela->r_offset;
11664
11665 r_symndx = irela->r_info >> r_sym_shift;
11666 if (r_symndx == STN_UNDEF)
11667 continue;
11668
11669 if (r_symndx >= locsymcount
11670 || (elf_bad_symtab (input_bfd)
11671 && flinfo->sections[r_symndx] == NULL))
11672 {
11673 struct elf_link_hash_entry *rh;
11674 unsigned long indx;
11675
11676 /* This is a reloc against a global symbol. We
11677 have not yet output all the local symbols, so
11678 we do not know the symbol index of any global
11679 symbol. We set the rel_hash entry for this
11680 reloc to point to the global hash table entry
11681 for this symbol. The symbol index is then
11682 set at the end of bfd_elf_final_link. */
11683 indx = r_symndx - extsymoff;
11684 rh = elf_sym_hashes (input_bfd)[indx];
11685 while (rh->root.type == bfd_link_hash_indirect
11686 || rh->root.type == bfd_link_hash_warning)
11687 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
11688
11689 /* Setting the index to -2 tells
11690 elf_link_output_extsym that this symbol is
11691 used by a reloc. */
11692 BFD_ASSERT (rh->indx < 0);
11693 rh->indx = -2;
11694 *rel_hash = rh;
11695
11696 continue;
11697 }
11698
11699 /* This is a reloc against a local symbol. */
11700
11701 *rel_hash = NULL;
11702 sym = isymbuf[r_symndx];
11703 sec = flinfo->sections[r_symndx];
11704 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
11705 {
11706 /* I suppose the backend ought to fill in the
11707 section of any STT_SECTION symbol against a
11708 processor specific section. */
11709 r_symndx = STN_UNDEF;
11710 if (bfd_is_abs_section (sec))
11711 ;
11712 else if (sec == NULL || sec->owner == NULL)
11713 {
11714 bfd_set_error (bfd_error_bad_value);
11715 return false;
11716 }
11717 else
11718 {
11719 asection *osec = sec->output_section;
11720
11721 /* If we have discarded a section, the output
11722 section will be the absolute section. In
11723 case of discarded SEC_MERGE sections, use
11724 the kept section. relocate_section should
11725 have already handled discarded linkonce
11726 sections. */
11727 if (bfd_is_abs_section (osec)
11728 && sec->kept_section != NULL
11729 && sec->kept_section->output_section != NULL)
11730 {
11731 osec = sec->kept_section->output_section;
11732 irela->r_addend -= osec->vma;
11733 }
11734
11735 if (!bfd_is_abs_section (osec))
11736 {
11737 r_symndx = osec->target_index;
11738 if (r_symndx == STN_UNDEF)
11739 {
11740 irela->r_addend += osec->vma;
11741 osec = _bfd_nearby_section (output_bfd, osec,
11742 osec->vma);
11743 irela->r_addend -= osec->vma;
11744 r_symndx = osec->target_index;
11745 }
11746 }
11747 }
11748
11749 /* Adjust the addend according to where the
11750 section winds up in the output section. */
11751 if (rela_normal)
11752 irela->r_addend += sec->output_offset;
11753 }
11754 else
11755 {
11756 if (flinfo->indices[r_symndx] == -1)
11757 {
11758 unsigned long shlink;
11759 const char *name;
11760 asection *osec;
11761 long indx;
11762
11763 if (flinfo->info->strip == strip_all)
11764 {
11765 /* You can't do ld -r -s. */
11766 bfd_set_error (bfd_error_invalid_operation);
11767 return false;
11768 }
11769
11770 /* This symbol was skipped earlier, but
11771 since it is needed by a reloc, we
11772 must output it now. */
11773 shlink = symtab_hdr->sh_link;
11774 name = (bfd_elf_string_from_elf_section
11775 (input_bfd, shlink, sym.st_name));
11776 if (name == NULL)
11777 return false;
11778
11779 osec = sec->output_section;
11780 sym.st_shndx =
11781 _bfd_elf_section_from_bfd_section (output_bfd,
11782 osec);
11783 if (sym.st_shndx == SHN_BAD)
11784 return false;
11785
11786 sym.st_value += sec->output_offset;
11787 if (!bfd_link_relocatable (flinfo->info))
11788 {
11789 sym.st_value += osec->vma;
11790 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
11791 {
11792 struct elf_link_hash_table *htab
11793 = elf_hash_table (flinfo->info);
11794
11795 /* STT_TLS symbols are relative to PT_TLS
11796 segment base. */
11797 if (htab->tls_sec != NULL)
11798 sym.st_value -= htab->tls_sec->vma;
11799 else
11800 sym.st_info
11801 = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
11802 STT_NOTYPE);
11803 }
11804 }
11805
11806 indx = bfd_get_symcount (output_bfd);
11807 ret = elf_link_output_symstrtab (flinfo, name,
11808 &sym, sec,
11809 NULL);
11810 if (ret == 0)
11811 return false;
11812 else if (ret == 1)
11813 flinfo->indices[r_symndx] = indx;
11814 else
11815 abort ();
11816 }
11817
11818 r_symndx = flinfo->indices[r_symndx];
11819 }
11820
11821 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
11822 | (irela->r_info & r_type_mask));
11823 }
11824
11825 /* Swap out the relocs. */
11826 input_rel_hdr = esdi->rel.hdr;
11827 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
11828 {
11829 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11830 input_rel_hdr,
11831 internal_relocs,
11832 rel_hash_list))
11833 return false;
11834 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
11835 * bed->s->int_rels_per_ext_rel);
11836 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
11837 }
11838
11839 input_rela_hdr = esdi->rela.hdr;
11840 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
11841 {
11842 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11843 input_rela_hdr,
11844 internal_relocs,
11845 rela_hash_list))
11846 return false;
11847 }
11848 }
11849 }
11850
11851 /* Write out the modified section contents. */
11852 if (bed->elf_backend_write_section
11853 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
11854 contents))
11855 {
11856 /* Section written out. */
11857 }
11858 else switch (o->sec_info_type)
11859 {
11860 case SEC_INFO_TYPE_STABS:
11861 if (! (_bfd_write_section_stabs
11862 (output_bfd,
11863 &elf_hash_table (flinfo->info)->stab_info,
11864 o, &elf_section_data (o)->sec_info, contents)))
11865 return false;
11866 break;
11867 case SEC_INFO_TYPE_MERGE:
11868 if (! _bfd_write_merged_section (output_bfd, o,
11869 elf_section_data (o)->sec_info))
11870 return false;
11871 break;
11872 case SEC_INFO_TYPE_EH_FRAME:
11873 {
11874 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
11875 o, contents))
11876 return false;
11877 }
11878 break;
11879 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11880 {
11881 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11882 flinfo->info,
11883 o, contents))
11884 return false;
11885 }
11886 break;
11887 case SEC_INFO_TYPE_SFRAME:
11888 {
11889 /* Merge .sframe sections into the ctf frame encoder
11890 context of the output_bfd's section. The final .sframe
11891 output section will be written out later. */
11892 if (!_bfd_elf_merge_section_sframe (output_bfd, flinfo->info,
11893 o, contents))
11894 return false;
11895 }
11896 break;
11897 default:
11898 {
11899 if (! (o->flags & SEC_EXCLUDE))
11900 {
11901 file_ptr offset = (file_ptr) o->output_offset;
11902 bfd_size_type todo = o->size;
11903
11904 offset *= bfd_octets_per_byte (output_bfd, o);
11905
11906 if ((o->flags & SEC_ELF_REVERSE_COPY)
11907 && o->size > address_size)
11908 {
11909 /* Reverse-copy input section to output. */
11910
11911 if ((o->size & (address_size - 1)) != 0
11912 || (o->reloc_count != 0
11913 && (o->size * bed->s->int_rels_per_ext_rel
11914 != o->reloc_count * address_size)))
11915 {
11916 _bfd_error_handler
11917 /* xgettext:c-format */
11918 (_("error: %pB: size of section %pA is not "
11919 "multiple of address size"),
11920 input_bfd, o);
11921 bfd_set_error (bfd_error_bad_value);
11922 return false;
11923 }
11924
11925 do
11926 {
11927 todo -= address_size;
11928 if (! bfd_set_section_contents (output_bfd,
11929 o->output_section,
11930 contents + todo,
11931 offset,
11932 address_size))
11933 return false;
11934 if (todo == 0)
11935 break;
11936 offset += address_size;
11937 }
11938 while (1);
11939 }
11940 else if (! bfd_set_section_contents (output_bfd,
11941 o->output_section,
11942 contents,
11943 offset, todo))
11944 return false;
11945 }
11946 }
11947 break;
11948 }
11949 }
11950
11951 return true;
11952 }
11953
11954 /* Generate a reloc when linking an ELF file. This is a reloc
11955 requested by the linker, and does not come from any input file. This
11956 is used to build constructor and destructor tables when linking
11957 with -Ur. */
11958
11959 static bool
11960 elf_reloc_link_order (bfd *output_bfd,
11961 struct bfd_link_info *info,
11962 asection *output_section,
11963 struct bfd_link_order *link_order)
11964 {
11965 reloc_howto_type *howto;
11966 long indx;
11967 bfd_vma offset;
11968 bfd_vma addend;
11969 struct bfd_elf_section_reloc_data *reldata;
11970 struct elf_link_hash_entry **rel_hash_ptr;
11971 Elf_Internal_Shdr *rel_hdr;
11972 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11973 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11974 bfd_byte *erel;
11975 unsigned int i;
11976 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
11977
11978 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11979 if (howto == NULL)
11980 {
11981 bfd_set_error (bfd_error_bad_value);
11982 return false;
11983 }
11984
11985 addend = link_order->u.reloc.p->addend;
11986
11987 if (esdo->rel.hdr)
11988 reldata = &esdo->rel;
11989 else if (esdo->rela.hdr)
11990 reldata = &esdo->rela;
11991 else
11992 {
11993 reldata = NULL;
11994 BFD_ASSERT (0);
11995 }
11996
11997 /* Figure out the symbol index. */
11998 rel_hash_ptr = reldata->hashes + reldata->count;
11999 if (link_order->type == bfd_section_reloc_link_order)
12000 {
12001 indx = link_order->u.reloc.p->u.section->target_index;
12002 BFD_ASSERT (indx != 0);
12003 *rel_hash_ptr = NULL;
12004 }
12005 else
12006 {
12007 struct elf_link_hash_entry *h;
12008
12009 /* Treat a reloc against a defined symbol as though it were
12010 actually against the section. */
12011 h = ((struct elf_link_hash_entry *)
12012 bfd_wrapped_link_hash_lookup (output_bfd, info,
12013 link_order->u.reloc.p->u.name,
12014 false, false, true));
12015 if (h != NULL
12016 && (h->root.type == bfd_link_hash_defined
12017 || h->root.type == bfd_link_hash_defweak))
12018 {
12019 asection *section;
12020
12021 section = h->root.u.def.section;
12022 indx = section->output_section->target_index;
12023 *rel_hash_ptr = NULL;
12024 /* It seems that we ought to add the symbol value to the
12025 addend here, but in practice it has already been added
12026 because it was passed to constructor_callback. */
12027 addend += section->output_section->vma + section->output_offset;
12028 }
12029 else if (h != NULL)
12030 {
12031 /* Setting the index to -2 tells elf_link_output_extsym that
12032 this symbol is used by a reloc. */
12033 h->indx = -2;
12034 *rel_hash_ptr = h;
12035 indx = 0;
12036 }
12037 else
12038 {
12039 (*info->callbacks->unattached_reloc)
12040 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
12041 indx = 0;
12042 }
12043 }
12044
12045 /* If this is an inplace reloc, we must write the addend into the
12046 object file. */
12047 if (howto->partial_inplace && addend != 0)
12048 {
12049 bfd_size_type size;
12050 bfd_reloc_status_type rstat;
12051 bfd_byte *buf;
12052 bool ok;
12053 const char *sym_name;
12054 bfd_size_type octets;
12055
12056 size = (bfd_size_type) bfd_get_reloc_size (howto);
12057 buf = (bfd_byte *) bfd_zmalloc (size);
12058 if (buf == NULL && size != 0)
12059 return false;
12060 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
12061 switch (rstat)
12062 {
12063 case bfd_reloc_ok:
12064 break;
12065
12066 default:
12067 case bfd_reloc_outofrange:
12068 abort ();
12069
12070 case bfd_reloc_overflow:
12071 if (link_order->type == bfd_section_reloc_link_order)
12072 sym_name = bfd_section_name (link_order->u.reloc.p->u.section);
12073 else
12074 sym_name = link_order->u.reloc.p->u.name;
12075 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
12076 howto->name, addend, NULL, NULL,
12077 (bfd_vma) 0);
12078 break;
12079 }
12080
12081 octets = link_order->offset * bfd_octets_per_byte (output_bfd,
12082 output_section);
12083 ok = bfd_set_section_contents (output_bfd, output_section, buf,
12084 octets, size);
12085 free (buf);
12086 if (! ok)
12087 return false;
12088 }
12089
12090 /* The address of a reloc is relative to the section in a
12091 relocatable file, and is a virtual address in an executable
12092 file. */
12093 offset = link_order->offset;
12094 if (! bfd_link_relocatable (info))
12095 offset += output_section->vma;
12096
12097 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
12098 {
12099 irel[i].r_offset = offset;
12100 irel[i].r_info = 0;
12101 irel[i].r_addend = 0;
12102 }
12103 if (bed->s->arch_size == 32)
12104 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
12105 else
12106 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
12107
12108 rel_hdr = reldata->hdr;
12109 erel = rel_hdr->contents;
12110 if (rel_hdr->sh_type == SHT_REL)
12111 {
12112 erel += reldata->count * bed->s->sizeof_rel;
12113 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
12114 }
12115 else
12116 {
12117 irel[0].r_addend = addend;
12118 erel += reldata->count * bed->s->sizeof_rela;
12119 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
12120 }
12121
12122 ++reldata->count;
12123
12124 return true;
12125 }
12126
12127 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
12128 Returns TRUE upon success, FALSE otherwise. */
12129
12130 static bool
12131 elf_output_implib (bfd *abfd, struct bfd_link_info *info)
12132 {
12133 bool ret = false;
12134 bfd *implib_bfd;
12135 const struct elf_backend_data *bed;
12136 flagword flags;
12137 enum bfd_architecture arch;
12138 unsigned int mach;
12139 asymbol **sympp = NULL;
12140 long symsize;
12141 long symcount;
12142 long src_count;
12143 elf_symbol_type *osymbuf;
12144 size_t amt;
12145
12146 implib_bfd = info->out_implib_bfd;
12147 bed = get_elf_backend_data (abfd);
12148
12149 if (!bfd_set_format (implib_bfd, bfd_object))
12150 return false;
12151
12152 /* Use flag from executable but make it a relocatable object. */
12153 flags = bfd_get_file_flags (abfd);
12154 flags &= ~HAS_RELOC;
12155 if (!bfd_set_start_address (implib_bfd, 0)
12156 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
12157 return false;
12158
12159 /* Copy architecture of output file to import library file. */
12160 arch = bfd_get_arch (abfd);
12161 mach = bfd_get_mach (abfd);
12162 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
12163 && (abfd->target_defaulted
12164 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
12165 return false;
12166
12167 /* Get symbol table size. */
12168 symsize = bfd_get_symtab_upper_bound (abfd);
12169 if (symsize < 0)
12170 return false;
12171
12172 /* Read in the symbol table. */
12173 sympp = (asymbol **) bfd_malloc (symsize);
12174 if (sympp == NULL)
12175 return false;
12176
12177 symcount = bfd_canonicalize_symtab (abfd, sympp);
12178 if (symcount < 0)
12179 goto free_sym_buf;
12180
12181 /* Allow the BFD backend to copy any private header data it
12182 understands from the output BFD to the import library BFD. */
12183 if (! bfd_copy_private_header_data (abfd, implib_bfd))
12184 goto free_sym_buf;
12185
12186 /* Filter symbols to appear in the import library. */
12187 if (bed->elf_backend_filter_implib_symbols)
12188 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
12189 symcount);
12190 else
12191 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
12192 if (symcount == 0)
12193 {
12194 bfd_set_error (bfd_error_no_symbols);
12195 _bfd_error_handler (_("%pB: no symbol found for import library"),
12196 implib_bfd);
12197 goto free_sym_buf;
12198 }
12199
12200
12201 /* Make symbols absolute. */
12202 amt = symcount * sizeof (*osymbuf);
12203 osymbuf = (elf_symbol_type *) bfd_alloc (implib_bfd, amt);
12204 if (osymbuf == NULL)
12205 goto free_sym_buf;
12206
12207 for (src_count = 0; src_count < symcount; src_count++)
12208 {
12209 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
12210 sizeof (*osymbuf));
12211 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
12212 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
12213 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
12214 osymbuf[src_count].internal_elf_sym.st_value =
12215 osymbuf[src_count].symbol.value;
12216 sympp[src_count] = &osymbuf[src_count].symbol;
12217 }
12218
12219 bfd_set_symtab (implib_bfd, sympp, symcount);
12220
12221 /* Allow the BFD backend to copy any private data it understands
12222 from the output BFD to the import library BFD. This is done last
12223 to permit the routine to look at the filtered symbol table. */
12224 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
12225 goto free_sym_buf;
12226
12227 if (!bfd_close (implib_bfd))
12228 goto free_sym_buf;
12229
12230 ret = true;
12231
12232 free_sym_buf:
12233 free (sympp);
12234 return ret;
12235 }
12236
12237 static void
12238 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
12239 {
12240 asection *o;
12241
12242 if (flinfo->symstrtab != NULL)
12243 _bfd_elf_strtab_free (flinfo->symstrtab);
12244 free (flinfo->contents);
12245 free (flinfo->external_relocs);
12246 free (flinfo->internal_relocs);
12247 free (flinfo->external_syms);
12248 free (flinfo->locsym_shndx);
12249 free (flinfo->internal_syms);
12250 free (flinfo->indices);
12251 free (flinfo->sections);
12252 if (flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1)
12253 free (flinfo->symshndxbuf);
12254 for (o = obfd->sections; o != NULL; o = o->next)
12255 {
12256 struct bfd_elf_section_data *esdo = elf_section_data (o);
12257 free (esdo->rel.hashes);
12258 free (esdo->rela.hashes);
12259 }
12260 }
12261
12262 /* Do the final step of an ELF link. */
12263
12264 bool
12265 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
12266 {
12267 bool dynamic;
12268 bool emit_relocs;
12269 bfd *dynobj;
12270 struct elf_final_link_info flinfo;
12271 asection *o;
12272 struct bfd_link_order *p;
12273 bfd *sub;
12274 bfd_size_type max_contents_size;
12275 bfd_size_type max_external_reloc_size;
12276 bfd_size_type max_internal_reloc_count;
12277 bfd_size_type max_sym_count;
12278 bfd_size_type max_sym_shndx_count;
12279 Elf_Internal_Sym elfsym;
12280 unsigned int i;
12281 Elf_Internal_Shdr *symtab_hdr;
12282 Elf_Internal_Shdr *symtab_shndx_hdr;
12283 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12284 struct elf_outext_info eoinfo;
12285 bool merged;
12286 size_t relativecount;
12287 size_t relr_entsize;
12288 asection *reldyn = 0;
12289 bfd_size_type amt;
12290 asection *attr_section = NULL;
12291 bfd_vma attr_size = 0;
12292 const char *std_attrs_section;
12293 struct elf_link_hash_table *htab = elf_hash_table (info);
12294 bool sections_removed;
12295 bool ret;
12296
12297 if (!is_elf_hash_table (&htab->root))
12298 return false;
12299
12300 if (bfd_link_pic (info))
12301 abfd->flags |= DYNAMIC;
12302
12303 dynamic = htab->dynamic_sections_created;
12304 dynobj = htab->dynobj;
12305
12306 emit_relocs = (bfd_link_relocatable (info)
12307 || info->emitrelocations);
12308
12309 memset (&flinfo, 0, sizeof (flinfo));
12310 flinfo.info = info;
12311 flinfo.output_bfd = abfd;
12312 flinfo.symstrtab = _bfd_elf_strtab_init ();
12313 if (flinfo.symstrtab == NULL)
12314 return false;
12315
12316 if (! dynamic)
12317 {
12318 flinfo.hash_sec = NULL;
12319 flinfo.symver_sec = NULL;
12320 }
12321 else
12322 {
12323 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
12324 /* Note that dynsym_sec can be NULL (on VMS). */
12325 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
12326 /* Note that it is OK if symver_sec is NULL. */
12327 }
12328
12329 if (info->unique_symbol
12330 && !bfd_hash_table_init (&flinfo.local_hash_table,
12331 local_hash_newfunc,
12332 sizeof (struct local_hash_entry)))
12333 return false;
12334
12335 /* The object attributes have been merged. Remove the input
12336 sections from the link, and set the contents of the output
12337 section. */
12338 sections_removed = false;
12339 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
12340 for (o = abfd->sections; o != NULL; o = o->next)
12341 {
12342 bool remove_section = false;
12343
12344 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
12345 || strcmp (o->name, ".gnu.attributes") == 0)
12346 {
12347 for (p = o->map_head.link_order; p != NULL; p = p->next)
12348 {
12349 asection *input_section;
12350
12351 if (p->type != bfd_indirect_link_order)
12352 continue;
12353 input_section = p->u.indirect.section;
12354 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12355 elf_link_input_bfd ignores this section. */
12356 input_section->flags &= ~SEC_HAS_CONTENTS;
12357 }
12358
12359 attr_size = bfd_elf_obj_attr_size (abfd);
12360 bfd_set_section_size (o, attr_size);
12361 /* Skip this section later on. */
12362 o->map_head.link_order = NULL;
12363 if (attr_size)
12364 attr_section = o;
12365 else
12366 remove_section = true;
12367 }
12368 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
12369 {
12370 /* Remove empty group section from linker output. */
12371 remove_section = true;
12372 }
12373 if (remove_section)
12374 {
12375 o->flags |= SEC_EXCLUDE;
12376 bfd_section_list_remove (abfd, o);
12377 abfd->section_count--;
12378 sections_removed = true;
12379 }
12380 }
12381 if (sections_removed)
12382 _bfd_fix_excluded_sec_syms (abfd, info);
12383
12384 /* Count up the number of relocations we will output for each output
12385 section, so that we know the sizes of the reloc sections. We
12386 also figure out some maximum sizes. */
12387 max_contents_size = 0;
12388 max_external_reloc_size = 0;
12389 max_internal_reloc_count = 0;
12390 max_sym_count = 0;
12391 max_sym_shndx_count = 0;
12392 merged = false;
12393 for (o = abfd->sections; o != NULL; o = o->next)
12394 {
12395 struct bfd_elf_section_data *esdo = elf_section_data (o);
12396 o->reloc_count = 0;
12397
12398 for (p = o->map_head.link_order; p != NULL; p = p->next)
12399 {
12400 unsigned int reloc_count = 0;
12401 unsigned int additional_reloc_count = 0;
12402 struct bfd_elf_section_data *esdi = NULL;
12403
12404 if (p->type == bfd_section_reloc_link_order
12405 || p->type == bfd_symbol_reloc_link_order)
12406 reloc_count = 1;
12407 else if (p->type == bfd_indirect_link_order)
12408 {
12409 asection *sec;
12410
12411 sec = p->u.indirect.section;
12412
12413 /* Mark all sections which are to be included in the
12414 link. This will normally be every section. We need
12415 to do this so that we can identify any sections which
12416 the linker has decided to not include. */
12417 sec->linker_mark = true;
12418
12419 if (sec->flags & SEC_MERGE)
12420 merged = true;
12421
12422 if (sec->rawsize > max_contents_size)
12423 max_contents_size = sec->rawsize;
12424 if (sec->size > max_contents_size)
12425 max_contents_size = sec->size;
12426
12427 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
12428 && (sec->owner->flags & DYNAMIC) == 0)
12429 {
12430 size_t sym_count;
12431
12432 /* We are interested in just local symbols, not all
12433 symbols. */
12434 if (elf_bad_symtab (sec->owner))
12435 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
12436 / bed->s->sizeof_sym);
12437 else
12438 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
12439
12440 if (sym_count > max_sym_count)
12441 max_sym_count = sym_count;
12442
12443 if (sym_count > max_sym_shndx_count
12444 && elf_symtab_shndx_list (sec->owner) != NULL)
12445 max_sym_shndx_count = sym_count;
12446
12447 esdi = elf_section_data (sec);
12448
12449 if (esdi->this_hdr.sh_type == SHT_REL
12450 || esdi->this_hdr.sh_type == SHT_RELA)
12451 /* Some backends use reloc_count in relocation sections
12452 to count particular types of relocs. Of course,
12453 reloc sections themselves can't have relocations. */
12454 ;
12455 else if (emit_relocs)
12456 {
12457 reloc_count = sec->reloc_count;
12458 if (bed->elf_backend_count_additional_relocs)
12459 {
12460 int c;
12461 c = (*bed->elf_backend_count_additional_relocs) (sec);
12462 additional_reloc_count += c;
12463 }
12464 }
12465 else if (bed->elf_backend_count_relocs)
12466 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
12467
12468 if ((sec->flags & SEC_RELOC) != 0)
12469 {
12470 size_t ext_size = 0;
12471
12472 if (esdi->rel.hdr != NULL)
12473 ext_size = esdi->rel.hdr->sh_size;
12474 if (esdi->rela.hdr != NULL)
12475 ext_size += esdi->rela.hdr->sh_size;
12476
12477 if (ext_size > max_external_reloc_size)
12478 max_external_reloc_size = ext_size;
12479 if (sec->reloc_count > max_internal_reloc_count)
12480 max_internal_reloc_count = sec->reloc_count;
12481 }
12482 }
12483 }
12484
12485 if (reloc_count == 0)
12486 continue;
12487
12488 reloc_count += additional_reloc_count;
12489 o->reloc_count += reloc_count;
12490
12491 if (p->type == bfd_indirect_link_order && emit_relocs)
12492 {
12493 if (esdi->rel.hdr)
12494 {
12495 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
12496 esdo->rel.count += additional_reloc_count;
12497 }
12498 if (esdi->rela.hdr)
12499 {
12500 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
12501 esdo->rela.count += additional_reloc_count;
12502 }
12503 }
12504 else
12505 {
12506 if (o->use_rela_p)
12507 esdo->rela.count += reloc_count;
12508 else
12509 esdo->rel.count += reloc_count;
12510 }
12511 }
12512
12513 if (o->reloc_count > 0)
12514 o->flags |= SEC_RELOC;
12515 else
12516 {
12517 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12518 set it (this is probably a bug) and if it is set
12519 assign_section_numbers will create a reloc section. */
12520 o->flags &=~ SEC_RELOC;
12521 }
12522
12523 /* If the SEC_ALLOC flag is not set, force the section VMA to
12524 zero. This is done in elf_fake_sections as well, but forcing
12525 the VMA to 0 here will ensure that relocs against these
12526 sections are handled correctly. */
12527 if ((o->flags & SEC_ALLOC) == 0
12528 && ! o->user_set_vma)
12529 o->vma = 0;
12530 }
12531
12532 if (! bfd_link_relocatable (info) && merged)
12533 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
12534
12535 /* Figure out the file positions for everything but the symbol table
12536 and the relocs. We set symcount to force assign_section_numbers
12537 to create a symbol table. */
12538 abfd->symcount = info->strip != strip_all || emit_relocs;
12539 BFD_ASSERT (! abfd->output_has_begun);
12540 if (! _bfd_elf_compute_section_file_positions (abfd, info))
12541 goto error_return;
12542
12543 /* Set sizes, and assign file positions for reloc sections. */
12544 for (o = abfd->sections; o != NULL; o = o->next)
12545 {
12546 struct bfd_elf_section_data *esdo = elf_section_data (o);
12547 if ((o->flags & SEC_RELOC) != 0)
12548 {
12549 if (esdo->rel.hdr
12550 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
12551 goto error_return;
12552
12553 if (esdo->rela.hdr
12554 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
12555 goto error_return;
12556 }
12557
12558 /* _bfd_elf_compute_section_file_positions makes temporary use
12559 of target_index. Reset it. */
12560 o->target_index = 0;
12561
12562 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12563 to count upwards while actually outputting the relocations. */
12564 esdo->rel.count = 0;
12565 esdo->rela.count = 0;
12566
12567 if ((esdo->this_hdr.sh_offset == (file_ptr) -1)
12568 && !bfd_section_is_ctf (o))
12569 {
12570 /* Cache the section contents so that they can be compressed
12571 later. Use bfd_malloc since it will be freed by
12572 bfd_compress_section_contents. */
12573 unsigned char *contents = esdo->this_hdr.contents;
12574 if (contents != NULL)
12575 abort ();
12576 contents
12577 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
12578 if (contents == NULL)
12579 goto error_return;
12580 esdo->this_hdr.contents = contents;
12581 }
12582 }
12583
12584 /* We have now assigned file positions for all the sections except .symtab,
12585 .strtab, and non-loaded reloc and compressed debugging sections. We start
12586 the .symtab section at the current file position, and write directly to it.
12587 We build the .strtab section in memory. */
12588 abfd->symcount = 0;
12589 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12590 /* sh_name is set in prep_headers. */
12591 symtab_hdr->sh_type = SHT_SYMTAB;
12592 /* sh_flags, sh_addr and sh_size all start off zero. */
12593 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
12594 /* sh_link is set in assign_section_numbers. */
12595 /* sh_info is set below. */
12596 /* sh_offset is set just below. */
12597 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
12598
12599 if (max_sym_count < 20)
12600 max_sym_count = 20;
12601 htab->strtabsize = max_sym_count;
12602 amt = max_sym_count * sizeof (struct elf_sym_strtab);
12603 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
12604 if (htab->strtab == NULL)
12605 goto error_return;
12606 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12607 flinfo.symshndxbuf
12608 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
12609 ? (Elf_External_Sym_Shndx *) -1 : NULL);
12610
12611 if (info->strip != strip_all || emit_relocs)
12612 {
12613 file_ptr off = elf_next_file_pos (abfd);
12614
12615 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, true);
12616
12617 /* Note that at this point elf_next_file_pos (abfd) is
12618 incorrect. We do not yet know the size of the .symtab section.
12619 We correct next_file_pos below, after we do know the size. */
12620
12621 /* Start writing out the symbol table. The first symbol is always a
12622 dummy symbol. */
12623 elfsym.st_value = 0;
12624 elfsym.st_size = 0;
12625 elfsym.st_info = 0;
12626 elfsym.st_other = 0;
12627 elfsym.st_shndx = SHN_UNDEF;
12628 elfsym.st_target_internal = 0;
12629 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
12630 bfd_und_section_ptr, NULL) != 1)
12631 goto error_return;
12632
12633 /* Output a symbol for each section if asked or they are used for
12634 relocs. These symbols usually have no names. We store the
12635 index of each one in the index field of the section, so that
12636 we can find it again when outputting relocs. */
12637
12638 if (bfd_keep_unused_section_symbols (abfd) || emit_relocs)
12639 {
12640 bool name_local_sections
12641 = (bed->elf_backend_name_local_section_symbols
12642 && bed->elf_backend_name_local_section_symbols (abfd));
12643 const char *name = NULL;
12644
12645 elfsym.st_size = 0;
12646 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12647 elfsym.st_other = 0;
12648 elfsym.st_value = 0;
12649 elfsym.st_target_internal = 0;
12650 for (i = 1; i < elf_numsections (abfd); i++)
12651 {
12652 o = bfd_section_from_elf_index (abfd, i);
12653 if (o != NULL)
12654 {
12655 o->target_index = bfd_get_symcount (abfd);
12656 elfsym.st_shndx = i;
12657 if (!bfd_link_relocatable (info))
12658 elfsym.st_value = o->vma;
12659 if (name_local_sections)
12660 name = o->name;
12661 if (elf_link_output_symstrtab (&flinfo, name, &elfsym, o,
12662 NULL) != 1)
12663 goto error_return;
12664 }
12665 }
12666 }
12667 }
12668
12669 /* On some targets like Irix 5 the symbol split between local and global
12670 ones recorded in the sh_info field needs to be done between section
12671 and all other symbols. */
12672 if (bed->elf_backend_elfsym_local_is_section
12673 && bed->elf_backend_elfsym_local_is_section (abfd))
12674 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12675
12676 /* Allocate some memory to hold information read in from the input
12677 files. */
12678 if (max_contents_size != 0)
12679 {
12680 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12681 if (flinfo.contents == NULL)
12682 goto error_return;
12683 }
12684
12685 if (max_external_reloc_size != 0)
12686 {
12687 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
12688 if (flinfo.external_relocs == NULL)
12689 goto error_return;
12690 }
12691
12692 if (max_internal_reloc_count != 0)
12693 {
12694 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
12695 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
12696 if (flinfo.internal_relocs == NULL)
12697 goto error_return;
12698 }
12699
12700 if (max_sym_count != 0)
12701 {
12702 amt = max_sym_count * bed->s->sizeof_sym;
12703 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
12704 if (flinfo.external_syms == NULL)
12705 goto error_return;
12706
12707 amt = max_sym_count * sizeof (Elf_Internal_Sym);
12708 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
12709 if (flinfo.internal_syms == NULL)
12710 goto error_return;
12711
12712 amt = max_sym_count * sizeof (long);
12713 flinfo.indices = (long int *) bfd_malloc (amt);
12714 if (flinfo.indices == NULL)
12715 goto error_return;
12716
12717 amt = max_sym_count * sizeof (asection *);
12718 flinfo.sections = (asection **) bfd_malloc (amt);
12719 if (flinfo.sections == NULL)
12720 goto error_return;
12721 }
12722
12723 if (max_sym_shndx_count != 0)
12724 {
12725 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
12726 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
12727 if (flinfo.locsym_shndx == NULL)
12728 goto error_return;
12729 }
12730
12731 if (htab->tls_sec)
12732 {
12733 bfd_vma base, end = 0; /* Both bytes. */
12734 asection *sec;
12735
12736 for (sec = htab->tls_sec;
12737 sec && (sec->flags & SEC_THREAD_LOCAL);
12738 sec = sec->next)
12739 {
12740 bfd_size_type size = sec->size;
12741 unsigned int opb = bfd_octets_per_byte (abfd, sec);
12742
12743 if (size == 0
12744 && (sec->flags & SEC_HAS_CONTENTS) == 0)
12745 {
12746 struct bfd_link_order *ord = sec->map_tail.link_order;
12747
12748 if (ord != NULL)
12749 size = ord->offset * opb + ord->size;
12750 }
12751 end = sec->vma + size / opb;
12752 }
12753 base = htab->tls_sec->vma;
12754 /* Only align end of TLS section if static TLS doesn't have special
12755 alignment requirements. */
12756 if (bed->static_tls_alignment == 1)
12757 end = align_power (end, htab->tls_sec->alignment_power);
12758 htab->tls_size = end - base;
12759 }
12760
12761 if (!_bfd_elf_fixup_eh_frame_hdr (info))
12762 return false;
12763
12764 /* Finish relative relocations here after regular symbol processing
12765 is finished if DT_RELR is enabled. */
12766 if (info->enable_dt_relr
12767 && bed->finish_relative_relocs
12768 && !bed->finish_relative_relocs (info))
12769 info->callbacks->einfo
12770 (_("%F%P: %pB: failed to finish relative relocations\n"), abfd);
12771
12772 /* Since ELF permits relocations to be against local symbols, we
12773 must have the local symbols available when we do the relocations.
12774 Since we would rather only read the local symbols once, and we
12775 would rather not keep them in memory, we handle all the
12776 relocations for a single input file at the same time.
12777
12778 Unfortunately, there is no way to know the total number of local
12779 symbols until we have seen all of them, and the local symbol
12780 indices precede the global symbol indices. This means that when
12781 we are generating relocatable output, and we see a reloc against
12782 a global symbol, we can not know the symbol index until we have
12783 finished examining all the local symbols to see which ones we are
12784 going to output. To deal with this, we keep the relocations in
12785 memory, and don't output them until the end of the link. This is
12786 an unfortunate waste of memory, but I don't see a good way around
12787 it. Fortunately, it only happens when performing a relocatable
12788 link, which is not the common case. FIXME: If keep_memory is set
12789 we could write the relocs out and then read them again; I don't
12790 know how bad the memory loss will be. */
12791
12792 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12793 sub->output_has_begun = false;
12794 for (o = abfd->sections; o != NULL; o = o->next)
12795 {
12796 for (p = o->map_head.link_order; p != NULL; p = p->next)
12797 {
12798 if (p->type == bfd_indirect_link_order
12799 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12800 == bfd_target_elf_flavour)
12801 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12802 {
12803 if (! sub->output_has_begun)
12804 {
12805 if (! elf_link_input_bfd (&flinfo, sub))
12806 goto error_return;
12807 sub->output_has_begun = true;
12808 }
12809 }
12810 else if (p->type == bfd_section_reloc_link_order
12811 || p->type == bfd_symbol_reloc_link_order)
12812 {
12813 if (! elf_reloc_link_order (abfd, info, o, p))
12814 goto error_return;
12815 }
12816 else
12817 {
12818 if (! _bfd_default_link_order (abfd, info, o, p))
12819 {
12820 if (p->type == bfd_indirect_link_order
12821 && (bfd_get_flavour (sub)
12822 == bfd_target_elf_flavour)
12823 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12824 != bed->s->elfclass))
12825 {
12826 const char *iclass, *oclass;
12827
12828 switch (bed->s->elfclass)
12829 {
12830 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12831 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12832 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12833 default: abort ();
12834 }
12835
12836 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
12837 {
12838 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12839 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12840 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12841 default: abort ();
12842 }
12843
12844 bfd_set_error (bfd_error_wrong_format);
12845 _bfd_error_handler
12846 /* xgettext:c-format */
12847 (_("%pB: file class %s incompatible with %s"),
12848 sub, iclass, oclass);
12849 }
12850
12851 goto error_return;
12852 }
12853 }
12854 }
12855 }
12856
12857 /* Free symbol buffer if needed. */
12858 if (!info->reduce_memory_overheads)
12859 {
12860 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12861 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
12862 {
12863 free (elf_tdata (sub)->symbuf);
12864 elf_tdata (sub)->symbuf = NULL;
12865 }
12866 }
12867
12868 ret = true;
12869
12870 /* Output any global symbols that got converted to local in a
12871 version script or due to symbol visibility. We do this in a
12872 separate step since ELF requires all local symbols to appear
12873 prior to any global symbols. FIXME: We should only do this if
12874 some global symbols were, in fact, converted to become local.
12875 FIXME: Will this work correctly with the Irix 5 linker? */
12876 eoinfo.failed = false;
12877 eoinfo.flinfo = &flinfo;
12878 eoinfo.localsyms = true;
12879 eoinfo.file_sym_done = false;
12880 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12881 if (eoinfo.failed)
12882 {
12883 ret = false;
12884 goto return_local_hash_table;
12885 }
12886
12887 /* If backend needs to output some local symbols not present in the hash
12888 table, do it now. */
12889 if (bed->elf_backend_output_arch_local_syms)
12890 {
12891 if (! ((*bed->elf_backend_output_arch_local_syms)
12892 (abfd, info, &flinfo, elf_link_output_symstrtab)))
12893 {
12894 ret = false;
12895 goto return_local_hash_table;
12896 }
12897 }
12898
12899 /* That wrote out all the local symbols. Finish up the symbol table
12900 with the global symbols. Even if we want to strip everything we
12901 can, we still need to deal with those global symbols that got
12902 converted to local in a version script. */
12903
12904 /* The sh_info field records the index of the first non local symbol. */
12905 if (!symtab_hdr->sh_info)
12906 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12907
12908 if (dynamic
12909 && htab->dynsym != NULL
12910 && htab->dynsym->output_section != bfd_abs_section_ptr)
12911 {
12912 Elf_Internal_Sym sym;
12913 bfd_byte *dynsym = htab->dynsym->contents;
12914
12915 o = htab->dynsym->output_section;
12916 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
12917
12918 /* Write out the section symbols for the output sections. */
12919 if (bfd_link_pic (info)
12920 || htab->is_relocatable_executable)
12921 {
12922 asection *s;
12923
12924 sym.st_size = 0;
12925 sym.st_name = 0;
12926 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12927 sym.st_other = 0;
12928 sym.st_target_internal = 0;
12929
12930 for (s = abfd->sections; s != NULL; s = s->next)
12931 {
12932 int indx;
12933 bfd_byte *dest;
12934 long dynindx;
12935
12936 dynindx = elf_section_data (s)->dynindx;
12937 if (dynindx <= 0)
12938 continue;
12939 indx = elf_section_data (s)->this_idx;
12940 BFD_ASSERT (indx > 0);
12941 sym.st_shndx = indx;
12942 if (! check_dynsym (abfd, &sym))
12943 {
12944 ret = false;
12945 goto return_local_hash_table;
12946 }
12947 sym.st_value = s->vma;
12948 dest = dynsym + dynindx * bed->s->sizeof_sym;
12949
12950 /* Inform the linker of the addition of this symbol. */
12951
12952 if (info->callbacks->ctf_new_dynsym)
12953 info->callbacks->ctf_new_dynsym (dynindx, &sym);
12954
12955 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12956 }
12957 }
12958
12959 /* Write out the local dynsyms. */
12960 if (htab->dynlocal)
12961 {
12962 struct elf_link_local_dynamic_entry *e;
12963 for (e = htab->dynlocal; e ; e = e->next)
12964 {
12965 asection *s;
12966 bfd_byte *dest;
12967
12968 /* Copy the internal symbol and turn off visibility.
12969 Note that we saved a word of storage and overwrote
12970 the original st_name with the dynstr_index. */
12971 sym = e->isym;
12972 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
12973 sym.st_shndx = SHN_UNDEF;
12974
12975 s = bfd_section_from_elf_index (e->input_bfd,
12976 e->isym.st_shndx);
12977 if (s != NULL
12978 && s->output_section != NULL
12979 && elf_section_data (s->output_section) != NULL)
12980 {
12981 sym.st_shndx =
12982 elf_section_data (s->output_section)->this_idx;
12983 if (! check_dynsym (abfd, &sym))
12984 {
12985 ret = false;
12986 goto return_local_hash_table;
12987 }
12988 sym.st_value = (s->output_section->vma
12989 + s->output_offset
12990 + e->isym.st_value);
12991 }
12992
12993 /* Inform the linker of the addition of this symbol. */
12994
12995 if (info->callbacks->ctf_new_dynsym)
12996 info->callbacks->ctf_new_dynsym (e->dynindx, &sym);
12997
12998 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12999 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
13000 }
13001 }
13002 }
13003
13004 /* We get the global symbols from the hash table. */
13005 eoinfo.failed = false;
13006 eoinfo.localsyms = false;
13007 eoinfo.flinfo = &flinfo;
13008 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
13009 if (eoinfo.failed)
13010 {
13011 ret = false;
13012 goto return_local_hash_table;
13013 }
13014
13015 /* If backend needs to output some symbols not present in the hash
13016 table, do it now. */
13017 if (bed->elf_backend_output_arch_syms
13018 && (info->strip != strip_all || emit_relocs))
13019 {
13020 if (! ((*bed->elf_backend_output_arch_syms)
13021 (abfd, info, &flinfo, elf_link_output_symstrtab)))
13022 {
13023 ret = false;
13024 goto return_local_hash_table;
13025 }
13026 }
13027
13028 /* Finalize the .strtab section. */
13029 _bfd_elf_strtab_finalize (flinfo.symstrtab);
13030
13031 /* Swap out the .strtab section. */
13032 if (!elf_link_swap_symbols_out (&flinfo))
13033 {
13034 ret = false;
13035 goto return_local_hash_table;
13036 }
13037
13038 /* Now we know the size of the symtab section. */
13039 if (bfd_get_symcount (abfd) > 0)
13040 {
13041 /* Finish up and write out the symbol string table (.strtab)
13042 section. */
13043 Elf_Internal_Shdr *symstrtab_hdr = NULL;
13044 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
13045
13046 if (elf_symtab_shndx_list (abfd))
13047 {
13048 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
13049
13050 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
13051 {
13052 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
13053 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
13054 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
13055 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
13056 symtab_shndx_hdr->sh_size = amt;
13057
13058 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
13059 off, true);
13060
13061 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
13062 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
13063 {
13064 ret = false;
13065 goto return_local_hash_table;
13066 }
13067 }
13068 }
13069
13070 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
13071 /* sh_name was set in prep_headers. */
13072 symstrtab_hdr->sh_type = SHT_STRTAB;
13073 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
13074 symstrtab_hdr->sh_addr = 0;
13075 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
13076 symstrtab_hdr->sh_entsize = 0;
13077 symstrtab_hdr->sh_link = 0;
13078 symstrtab_hdr->sh_info = 0;
13079 /* sh_offset is set just below. */
13080 symstrtab_hdr->sh_addralign = 1;
13081
13082 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
13083 off, true);
13084 elf_next_file_pos (abfd) = off;
13085
13086 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
13087 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
13088 {
13089 ret = false;
13090 goto return_local_hash_table;
13091 }
13092 }
13093
13094 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
13095 {
13096 _bfd_error_handler (_("%pB: failed to generate import library"),
13097 info->out_implib_bfd);
13098 ret = false;
13099 goto return_local_hash_table;
13100 }
13101
13102 /* Adjust the relocs to have the correct symbol indices. */
13103 for (o = abfd->sections; o != NULL; o = o->next)
13104 {
13105 struct bfd_elf_section_data *esdo = elf_section_data (o);
13106 bool sort;
13107
13108 if ((o->flags & SEC_RELOC) == 0)
13109 continue;
13110
13111 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
13112 if (esdo->rel.hdr != NULL
13113 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
13114 {
13115 ret = false;
13116 goto return_local_hash_table;
13117 }
13118 if (esdo->rela.hdr != NULL
13119 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
13120 {
13121 ret = false;
13122 goto return_local_hash_table;
13123 }
13124
13125 /* Set the reloc_count field to 0 to prevent write_relocs from
13126 trying to swap the relocs out itself. */
13127 o->reloc_count = 0;
13128 }
13129
13130 relativecount = 0;
13131 if (dynamic && info->combreloc && dynobj != NULL)
13132 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
13133
13134 relr_entsize = 0;
13135 if (htab->srelrdyn != NULL
13136 && htab->srelrdyn->output_section != NULL
13137 && htab->srelrdyn->size != 0)
13138 {
13139 asection *s = htab->srelrdyn->output_section;
13140 relr_entsize = elf_section_data (s)->this_hdr.sh_entsize;
13141 if (relr_entsize == 0)
13142 {
13143 relr_entsize = bed->s->arch_size / 8;
13144 elf_section_data (s)->this_hdr.sh_entsize = relr_entsize;
13145 }
13146 }
13147
13148 /* If we are linking against a dynamic object, or generating a
13149 shared library, finish up the dynamic linking information. */
13150 if (dynamic)
13151 {
13152 bfd_byte *dyncon, *dynconend;
13153
13154 /* Fix up .dynamic entries. */
13155 o = bfd_get_linker_section (dynobj, ".dynamic");
13156 BFD_ASSERT (o != NULL);
13157
13158 dyncon = o->contents;
13159 dynconend = PTR_ADD (o->contents, o->size);
13160 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13161 {
13162 Elf_Internal_Dyn dyn;
13163 const char *name;
13164 unsigned int type;
13165 bfd_size_type sh_size;
13166 bfd_vma sh_addr;
13167
13168 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13169
13170 switch (dyn.d_tag)
13171 {
13172 default:
13173 continue;
13174 case DT_NULL:
13175 if (relativecount != 0)
13176 {
13177 switch (elf_section_data (reldyn)->this_hdr.sh_type)
13178 {
13179 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
13180 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
13181 }
13182 if (dyn.d_tag != DT_NULL
13183 && dynconend - dyncon >= bed->s->sizeof_dyn)
13184 {
13185 dyn.d_un.d_val = relativecount;
13186 relativecount = 0;
13187 break;
13188 }
13189 relativecount = 0;
13190 }
13191 if (relr_entsize != 0)
13192 {
13193 if (dynconend - dyncon >= 3 * bed->s->sizeof_dyn)
13194 {
13195 asection *s = htab->srelrdyn;
13196 dyn.d_tag = DT_RELR;
13197 dyn.d_un.d_ptr
13198 = s->output_section->vma + s->output_offset;
13199 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13200 dyncon += bed->s->sizeof_dyn;
13201
13202 dyn.d_tag = DT_RELRSZ;
13203 dyn.d_un.d_val = s->size;
13204 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13205 dyncon += bed->s->sizeof_dyn;
13206
13207 dyn.d_tag = DT_RELRENT;
13208 dyn.d_un.d_val = relr_entsize;
13209 relr_entsize = 0;
13210 break;
13211 }
13212 relr_entsize = 0;
13213 }
13214 continue;
13215
13216 case DT_INIT:
13217 name = info->init_function;
13218 goto get_sym;
13219 case DT_FINI:
13220 name = info->fini_function;
13221 get_sym:
13222 {
13223 struct elf_link_hash_entry *h;
13224
13225 h = elf_link_hash_lookup (htab, name, false, false, true);
13226 if (h != NULL
13227 && (h->root.type == bfd_link_hash_defined
13228 || h->root.type == bfd_link_hash_defweak))
13229 {
13230 dyn.d_un.d_ptr = h->root.u.def.value;
13231 o = h->root.u.def.section;
13232 if (o->output_section != NULL)
13233 dyn.d_un.d_ptr += (o->output_section->vma
13234 + o->output_offset);
13235 else
13236 {
13237 /* The symbol is imported from another shared
13238 library and does not apply to this one. */
13239 dyn.d_un.d_ptr = 0;
13240 }
13241 break;
13242 }
13243 }
13244 continue;
13245
13246 case DT_PREINIT_ARRAYSZ:
13247 name = ".preinit_array";
13248 goto get_out_size;
13249 case DT_INIT_ARRAYSZ:
13250 name = ".init_array";
13251 goto get_out_size;
13252 case DT_FINI_ARRAYSZ:
13253 name = ".fini_array";
13254 get_out_size:
13255 o = bfd_get_section_by_name (abfd, name);
13256 if (o == NULL)
13257 {
13258 _bfd_error_handler
13259 (_("could not find section %s"), name);
13260 goto error_return;
13261 }
13262 if (o->size == 0)
13263 _bfd_error_handler
13264 (_("warning: %s section has zero size"), name);
13265 dyn.d_un.d_val = o->size;
13266 break;
13267
13268 case DT_PREINIT_ARRAY:
13269 name = ".preinit_array";
13270 goto get_out_vma;
13271 case DT_INIT_ARRAY:
13272 name = ".init_array";
13273 goto get_out_vma;
13274 case DT_FINI_ARRAY:
13275 name = ".fini_array";
13276 get_out_vma:
13277 o = bfd_get_section_by_name (abfd, name);
13278 goto do_vma;
13279
13280 case DT_HASH:
13281 name = ".hash";
13282 goto get_vma;
13283 case DT_GNU_HASH:
13284 name = ".gnu.hash";
13285 goto get_vma;
13286 case DT_STRTAB:
13287 name = ".dynstr";
13288 goto get_vma;
13289 case DT_SYMTAB:
13290 name = ".dynsym";
13291 goto get_vma;
13292 case DT_VERDEF:
13293 name = ".gnu.version_d";
13294 goto get_vma;
13295 case DT_VERNEED:
13296 name = ".gnu.version_r";
13297 goto get_vma;
13298 case DT_VERSYM:
13299 name = ".gnu.version";
13300 get_vma:
13301 o = bfd_get_linker_section (dynobj, name);
13302 do_vma:
13303 if (o == NULL || bfd_is_abs_section (o->output_section))
13304 {
13305 _bfd_error_handler
13306 (_("could not find section %s"), name);
13307 goto error_return;
13308 }
13309 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
13310 {
13311 _bfd_error_handler
13312 (_("warning: section '%s' is being made into a note"), name);
13313 bfd_set_error (bfd_error_nonrepresentable_section);
13314 goto error_return;
13315 }
13316 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
13317 break;
13318
13319 case DT_REL:
13320 case DT_RELA:
13321 case DT_RELSZ:
13322 case DT_RELASZ:
13323 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
13324 type = SHT_REL;
13325 else
13326 type = SHT_RELA;
13327 sh_size = 0;
13328 sh_addr = 0;
13329 for (i = 1; i < elf_numsections (abfd); i++)
13330 {
13331 Elf_Internal_Shdr *hdr;
13332
13333 hdr = elf_elfsections (abfd)[i];
13334 if (hdr->sh_type == type
13335 && (hdr->sh_flags & SHF_ALLOC) != 0)
13336 {
13337 sh_size += hdr->sh_size;
13338 if (sh_addr == 0
13339 || sh_addr > hdr->sh_addr)
13340 sh_addr = hdr->sh_addr;
13341 }
13342 }
13343
13344 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
13345 {
13346 unsigned int opb = bfd_octets_per_byte (abfd, o);
13347
13348 /* Don't count procedure linkage table relocs in the
13349 overall reloc count. */
13350 sh_size -= htab->srelplt->size;
13351 if (sh_size == 0)
13352 /* If the size is zero, make the address zero too.
13353 This is to avoid a glibc bug. If the backend
13354 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
13355 zero, then we'll put DT_RELA at the end of
13356 DT_JMPREL. glibc will interpret the end of
13357 DT_RELA matching the end of DT_JMPREL as the
13358 case where DT_RELA includes DT_JMPREL, and for
13359 LD_BIND_NOW will decide that processing DT_RELA
13360 will process the PLT relocs too. Net result:
13361 No PLT relocs applied. */
13362 sh_addr = 0;
13363
13364 /* If .rela.plt is the first .rela section, exclude
13365 it from DT_RELA. */
13366 else if (sh_addr == (htab->srelplt->output_section->vma
13367 + htab->srelplt->output_offset) * opb)
13368 sh_addr += htab->srelplt->size;
13369 }
13370
13371 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
13372 dyn.d_un.d_val = sh_size;
13373 else
13374 dyn.d_un.d_ptr = sh_addr;
13375 break;
13376 }
13377 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13378 }
13379 }
13380
13381 /* If we have created any dynamic sections, then output them. */
13382 if (dynobj != NULL)
13383 {
13384 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
13385 goto error_return;
13386
13387 /* Check for DT_TEXTREL (late, in case the backend removes it). */
13388 if (bfd_link_textrel_check (info)
13389 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL
13390 && o->size != 0)
13391 {
13392 bfd_byte *dyncon, *dynconend;
13393
13394 dyncon = o->contents;
13395 dynconend = o->contents + o->size;
13396 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13397 {
13398 Elf_Internal_Dyn dyn;
13399
13400 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13401
13402 if (dyn.d_tag == DT_TEXTREL)
13403 {
13404 if (info->textrel_check == textrel_check_error)
13405 info->callbacks->einfo
13406 (_("%P%X: read-only segment has dynamic relocations\n"));
13407 else if (bfd_link_dll (info))
13408 info->callbacks->einfo
13409 (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
13410 else if (bfd_link_pde (info))
13411 info->callbacks->einfo
13412 (_("%P: warning: creating DT_TEXTREL in a PDE\n"));
13413 else
13414 info->callbacks->einfo
13415 (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
13416 break;
13417 }
13418 }
13419 }
13420
13421 for (o = dynobj->sections; o != NULL; o = o->next)
13422 {
13423 if ((o->flags & SEC_HAS_CONTENTS) == 0
13424 || o->size == 0
13425 || o->output_section == bfd_abs_section_ptr)
13426 continue;
13427 if ((o->flags & SEC_LINKER_CREATED) == 0)
13428 {
13429 /* At this point, we are only interested in sections
13430 created by _bfd_elf_link_create_dynamic_sections. */
13431 continue;
13432 }
13433 if (htab->stab_info.stabstr == o)
13434 continue;
13435 if (htab->eh_info.hdr_sec == o)
13436 continue;
13437 if (strcmp (o->name, ".dynstr") != 0)
13438 {
13439 bfd_size_type octets = ((file_ptr) o->output_offset
13440 * bfd_octets_per_byte (abfd, o));
13441 if (!bfd_set_section_contents (abfd, o->output_section,
13442 o->contents, octets, o->size))
13443 goto error_return;
13444 }
13445 else
13446 {
13447 /* The contents of the .dynstr section are actually in a
13448 stringtab. */
13449 file_ptr off;
13450
13451 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
13452 if (bfd_seek (abfd, off, SEEK_SET) != 0
13453 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
13454 goto error_return;
13455 }
13456 }
13457 }
13458
13459 if (!info->resolve_section_groups)
13460 {
13461 bool failed = false;
13462
13463 BFD_ASSERT (bfd_link_relocatable (info));
13464 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
13465 if (failed)
13466 goto error_return;
13467 }
13468
13469 /* If we have optimized stabs strings, output them. */
13470 if (htab->stab_info.stabstr != NULL)
13471 {
13472 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
13473 goto error_return;
13474 }
13475
13476 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
13477 goto error_return;
13478
13479 if (! _bfd_elf_write_section_sframe (abfd, info))
13480 goto error_return;
13481
13482 if (info->callbacks->emit_ctf)
13483 info->callbacks->emit_ctf ();
13484
13485 elf_final_link_free (abfd, &flinfo);
13486
13487 if (attr_section)
13488 {
13489 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
13490 if (contents == NULL)
13491 {
13492 /* Bail out and fail. */
13493 ret = false;
13494 goto return_local_hash_table;
13495 }
13496 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
13497 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
13498 free (contents);
13499 }
13500
13501 return_local_hash_table:
13502 if (info->unique_symbol)
13503 bfd_hash_table_free (&flinfo.local_hash_table);
13504 return ret;
13505
13506 error_return:
13507 elf_final_link_free (abfd, &flinfo);
13508 ret = false;
13509 goto return_local_hash_table;
13510 }
13511 \f
13512 /* Initialize COOKIE for input bfd ABFD. */
13513
13514 static bool
13515 init_reloc_cookie (struct elf_reloc_cookie *cookie,
13516 struct bfd_link_info *info, bfd *abfd)
13517 {
13518 Elf_Internal_Shdr *symtab_hdr;
13519 const struct elf_backend_data *bed;
13520
13521 bed = get_elf_backend_data (abfd);
13522 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13523
13524 cookie->abfd = abfd;
13525 cookie->sym_hashes = elf_sym_hashes (abfd);
13526 cookie->bad_symtab = elf_bad_symtab (abfd);
13527 if (cookie->bad_symtab)
13528 {
13529 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13530 cookie->extsymoff = 0;
13531 }
13532 else
13533 {
13534 cookie->locsymcount = symtab_hdr->sh_info;
13535 cookie->extsymoff = symtab_hdr->sh_info;
13536 }
13537
13538 if (bed->s->arch_size == 32)
13539 cookie->r_sym_shift = 8;
13540 else
13541 cookie->r_sym_shift = 32;
13542
13543 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
13544 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
13545 {
13546 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
13547 cookie->locsymcount, 0,
13548 NULL, NULL, NULL);
13549 if (cookie->locsyms == NULL)
13550 {
13551 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
13552 return false;
13553 }
13554 if (_bfd_link_keep_memory (info) )
13555 {
13556 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
13557 info->cache_size += (cookie->locsymcount
13558 * sizeof (Elf_External_Sym_Shndx));
13559 }
13560 }
13561 return true;
13562 }
13563
13564 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
13565
13566 static void
13567 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
13568 {
13569 Elf_Internal_Shdr *symtab_hdr;
13570
13571 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13572 if (symtab_hdr->contents != (unsigned char *) cookie->locsyms)
13573 free (cookie->locsyms);
13574 }
13575
13576 /* Initialize the relocation information in COOKIE for input section SEC
13577 of input bfd ABFD. */
13578
13579 static bool
13580 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13581 struct bfd_link_info *info, bfd *abfd,
13582 asection *sec)
13583 {
13584 if (sec->reloc_count == 0)
13585 {
13586 cookie->rels = NULL;
13587 cookie->relend = NULL;
13588 }
13589 else
13590 {
13591 cookie->rels = _bfd_elf_link_info_read_relocs (abfd, info, sec,
13592 NULL, NULL,
13593 _bfd_link_keep_memory (info));
13594 if (cookie->rels == NULL)
13595 return false;
13596 cookie->rel = cookie->rels;
13597 cookie->relend = cookie->rels + sec->reloc_count;
13598 }
13599 cookie->rel = cookie->rels;
13600 return true;
13601 }
13602
13603 /* Free the memory allocated by init_reloc_cookie_rels,
13604 if appropriate. */
13605
13606 static void
13607 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13608 asection *sec)
13609 {
13610 if (elf_section_data (sec)->relocs != cookie->rels)
13611 free (cookie->rels);
13612 }
13613
13614 /* Initialize the whole of COOKIE for input section SEC. */
13615
13616 static bool
13617 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13618 struct bfd_link_info *info,
13619 asection *sec)
13620 {
13621 if (!init_reloc_cookie (cookie, info, sec->owner))
13622 goto error1;
13623 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
13624 goto error2;
13625 return true;
13626
13627 error2:
13628 fini_reloc_cookie (cookie, sec->owner);
13629 error1:
13630 return false;
13631 }
13632
13633 /* Free the memory allocated by init_reloc_cookie_for_section,
13634 if appropriate. */
13635
13636 static void
13637 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13638 asection *sec)
13639 {
13640 fini_reloc_cookie_rels (cookie, sec);
13641 fini_reloc_cookie (cookie, sec->owner);
13642 }
13643 \f
13644 /* Garbage collect unused sections. */
13645
13646 /* Default gc_mark_hook. */
13647
13648 asection *
13649 _bfd_elf_gc_mark_hook (asection *sec,
13650 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13651 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13652 struct elf_link_hash_entry *h,
13653 Elf_Internal_Sym *sym)
13654 {
13655 if (h != NULL)
13656 {
13657 switch (h->root.type)
13658 {
13659 case bfd_link_hash_defined:
13660 case bfd_link_hash_defweak:
13661 return h->root.u.def.section;
13662
13663 case bfd_link_hash_common:
13664 return h->root.u.c.p->section;
13665
13666 default:
13667 break;
13668 }
13669 }
13670 else
13671 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
13672
13673 return NULL;
13674 }
13675
13676 /* Return the debug definition section. */
13677
13678 static asection *
13679 elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
13680 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13681 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13682 struct elf_link_hash_entry *h,
13683 Elf_Internal_Sym *sym)
13684 {
13685 if (h != NULL)
13686 {
13687 /* Return the global debug definition section. */
13688 if ((h->root.type == bfd_link_hash_defined
13689 || h->root.type == bfd_link_hash_defweak)
13690 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
13691 return h->root.u.def.section;
13692 }
13693 else
13694 {
13695 /* Return the local debug definition section. */
13696 asection *isec = bfd_section_from_elf_index (sec->owner,
13697 sym->st_shndx);
13698 if ((isec->flags & SEC_DEBUGGING) != 0)
13699 return isec;
13700 }
13701
13702 return NULL;
13703 }
13704
13705 /* COOKIE->rel describes a relocation against section SEC, which is
13706 a section we've decided to keep. Return the section that contains
13707 the relocation symbol, or NULL if no section contains it. */
13708
13709 asection *
13710 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
13711 elf_gc_mark_hook_fn gc_mark_hook,
13712 struct elf_reloc_cookie *cookie,
13713 bool *start_stop)
13714 {
13715 unsigned long r_symndx;
13716 struct elf_link_hash_entry *h, *hw;
13717
13718 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
13719 if (r_symndx == STN_UNDEF)
13720 return NULL;
13721
13722 if (r_symndx >= cookie->locsymcount
13723 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13724 {
13725 bool was_marked;
13726
13727 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
13728 if (h == NULL)
13729 {
13730 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
13731 sec->owner);
13732 return NULL;
13733 }
13734 while (h->root.type == bfd_link_hash_indirect
13735 || h->root.type == bfd_link_hash_warning)
13736 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13737
13738 was_marked = h->mark;
13739 h->mark = 1;
13740 /* Keep all aliases of the symbol too. If an object symbol
13741 needs to be copied into .dynbss then all of its aliases
13742 should be present as dynamic symbols, not just the one used
13743 on the copy relocation. */
13744 hw = h;
13745 while (hw->is_weakalias)
13746 {
13747 hw = hw->u.alias;
13748 hw->mark = 1;
13749 }
13750
13751 if (!was_marked && h->start_stop && !h->root.ldscript_def)
13752 {
13753 if (info->start_stop_gc)
13754 return NULL;
13755
13756 /* To work around a glibc bug, mark XXX input sections
13757 when there is a reference to __start_XXX or __stop_XXX
13758 symbols. */
13759 else if (start_stop != NULL)
13760 {
13761 asection *s = h->u2.start_stop_section;
13762 *start_stop = true;
13763 return s;
13764 }
13765 }
13766
13767 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
13768 }
13769
13770 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
13771 &cookie->locsyms[r_symndx]);
13772 }
13773
13774 /* COOKIE->rel describes a relocation against section SEC, which is
13775 a section we've decided to keep. Mark the section that contains
13776 the relocation symbol. */
13777
13778 bool
13779 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
13780 asection *sec,
13781 elf_gc_mark_hook_fn gc_mark_hook,
13782 struct elf_reloc_cookie *cookie)
13783 {
13784 asection *rsec;
13785 bool start_stop = false;
13786
13787 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
13788 while (rsec != NULL)
13789 {
13790 if (!rsec->gc_mark)
13791 {
13792 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
13793 || (rsec->owner->flags & DYNAMIC) != 0)
13794 rsec->gc_mark = 1;
13795 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
13796 return false;
13797 }
13798 if (!start_stop)
13799 break;
13800 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
13801 }
13802 return true;
13803 }
13804
13805 /* The mark phase of garbage collection. For a given section, mark
13806 it and any sections in this section's group, and all the sections
13807 which define symbols to which it refers. */
13808
13809 bool
13810 _bfd_elf_gc_mark (struct bfd_link_info *info,
13811 asection *sec,
13812 elf_gc_mark_hook_fn gc_mark_hook)
13813 {
13814 bool ret;
13815 asection *group_sec, *eh_frame;
13816
13817 sec->gc_mark = 1;
13818
13819 /* Mark all the sections in the group. */
13820 group_sec = elf_section_data (sec)->next_in_group;
13821 if (group_sec && !group_sec->gc_mark)
13822 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
13823 return false;
13824
13825 /* Look through the section relocs. */
13826 ret = true;
13827 eh_frame = elf_eh_frame_section (sec->owner);
13828 if ((sec->flags & SEC_RELOC) != 0
13829 && sec->reloc_count > 0
13830 && sec != eh_frame)
13831 {
13832 struct elf_reloc_cookie cookie;
13833
13834 if (!init_reloc_cookie_for_section (&cookie, info, sec))
13835 ret = false;
13836 else
13837 {
13838 for (; cookie.rel < cookie.relend; cookie.rel++)
13839 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
13840 {
13841 ret = false;
13842 break;
13843 }
13844 fini_reloc_cookie_for_section (&cookie, sec);
13845 }
13846 }
13847
13848 if (ret && eh_frame && elf_fde_list (sec))
13849 {
13850 struct elf_reloc_cookie cookie;
13851
13852 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
13853 ret = false;
13854 else
13855 {
13856 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
13857 gc_mark_hook, &cookie))
13858 ret = false;
13859 fini_reloc_cookie_for_section (&cookie, eh_frame);
13860 }
13861 }
13862
13863 eh_frame = elf_section_eh_frame_entry (sec);
13864 if (ret && eh_frame && !eh_frame->gc_mark)
13865 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
13866 ret = false;
13867
13868 return ret;
13869 }
13870
13871 /* Scan and mark sections in a special or debug section group. */
13872
13873 static void
13874 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
13875 {
13876 /* Point to first section of section group. */
13877 asection *ssec;
13878 /* Used to iterate the section group. */
13879 asection *msec;
13880
13881 bool is_special_grp = true;
13882 bool is_debug_grp = true;
13883
13884 /* First scan to see if group contains any section other than debug
13885 and special section. */
13886 ssec = msec = elf_next_in_group (grp);
13887 do
13888 {
13889 if ((msec->flags & SEC_DEBUGGING) == 0)
13890 is_debug_grp = false;
13891
13892 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
13893 is_special_grp = false;
13894
13895 msec = elf_next_in_group (msec);
13896 }
13897 while (msec != ssec);
13898
13899 /* If this is a pure debug section group or pure special section group,
13900 keep all sections in this group. */
13901 if (is_debug_grp || is_special_grp)
13902 {
13903 do
13904 {
13905 msec->gc_mark = 1;
13906 msec = elf_next_in_group (msec);
13907 }
13908 while (msec != ssec);
13909 }
13910 }
13911
13912 /* Keep debug and special sections. */
13913
13914 bool
13915 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
13916 elf_gc_mark_hook_fn mark_hook)
13917 {
13918 bfd *ibfd;
13919
13920 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13921 {
13922 asection *isec;
13923 bool some_kept;
13924 bool debug_frag_seen;
13925 bool has_kept_debug_info;
13926
13927 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13928 continue;
13929 isec = ibfd->sections;
13930 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13931 continue;
13932
13933 /* Ensure all linker created sections are kept,
13934 see if any other section is already marked,
13935 and note if we have any fragmented debug sections. */
13936 debug_frag_seen = some_kept = has_kept_debug_info = false;
13937 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13938 {
13939 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13940 isec->gc_mark = 1;
13941 else if (isec->gc_mark
13942 && (isec->flags & SEC_ALLOC) != 0
13943 && elf_section_type (isec) != SHT_NOTE)
13944 some_kept = true;
13945 else
13946 {
13947 /* Since all sections, except for backend specific ones,
13948 have been garbage collected, call mark_hook on this
13949 section if any of its linked-to sections is marked. */
13950 asection *linked_to_sec;
13951 for (linked_to_sec = elf_linked_to_section (isec);
13952 linked_to_sec != NULL && !linked_to_sec->linker_mark;
13953 linked_to_sec = elf_linked_to_section (linked_to_sec))
13954 {
13955 if (linked_to_sec->gc_mark)
13956 {
13957 if (!_bfd_elf_gc_mark (info, isec, mark_hook))
13958 return false;
13959 break;
13960 }
13961 linked_to_sec->linker_mark = 1;
13962 }
13963 for (linked_to_sec = elf_linked_to_section (isec);
13964 linked_to_sec != NULL && linked_to_sec->linker_mark;
13965 linked_to_sec = elf_linked_to_section (linked_to_sec))
13966 linked_to_sec->linker_mark = 0;
13967 }
13968
13969 if (!debug_frag_seen
13970 && (isec->flags & SEC_DEBUGGING)
13971 && startswith (isec->name, ".debug_line."))
13972 debug_frag_seen = true;
13973 else if (strcmp (bfd_section_name (isec),
13974 "__patchable_function_entries") == 0
13975 && elf_linked_to_section (isec) == NULL)
13976 info->callbacks->einfo (_("%F%P: %pB(%pA): error: "
13977 "need linked-to section "
13978 "for --gc-sections\n"),
13979 isec->owner, isec);
13980 }
13981
13982 /* If no non-note alloc section in this file will be kept, then
13983 we can toss out the debug and special sections. */
13984 if (!some_kept)
13985 continue;
13986
13987 /* Keep debug and special sections like .comment when they are
13988 not part of a group. Also keep section groups that contain
13989 just debug sections or special sections. NB: Sections with
13990 linked-to section has been handled above. */
13991 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13992 {
13993 if ((isec->flags & SEC_GROUP) != 0)
13994 _bfd_elf_gc_mark_debug_special_section_group (isec);
13995 else if (((isec->flags & SEC_DEBUGGING) != 0
13996 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
13997 && elf_next_in_group (isec) == NULL
13998 && elf_linked_to_section (isec) == NULL)
13999 isec->gc_mark = 1;
14000 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
14001 has_kept_debug_info = true;
14002 }
14003
14004 /* Look for CODE sections which are going to be discarded,
14005 and find and discard any fragmented debug sections which
14006 are associated with that code section. */
14007 if (debug_frag_seen)
14008 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14009 if ((isec->flags & SEC_CODE) != 0
14010 && isec->gc_mark == 0)
14011 {
14012 unsigned int ilen;
14013 asection *dsec;
14014
14015 ilen = strlen (isec->name);
14016
14017 /* Association is determined by the name of the debug
14018 section containing the name of the code section as
14019 a suffix. For example .debug_line.text.foo is a
14020 debug section associated with .text.foo. */
14021 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
14022 {
14023 unsigned int dlen;
14024
14025 if (dsec->gc_mark == 0
14026 || (dsec->flags & SEC_DEBUGGING) == 0)
14027 continue;
14028
14029 dlen = strlen (dsec->name);
14030
14031 if (dlen > ilen
14032 && strncmp (dsec->name + (dlen - ilen),
14033 isec->name, ilen) == 0)
14034 dsec->gc_mark = 0;
14035 }
14036 }
14037
14038 /* Mark debug sections referenced by kept debug sections. */
14039 if (has_kept_debug_info)
14040 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14041 if (isec->gc_mark
14042 && (isec->flags & SEC_DEBUGGING) != 0)
14043 if (!_bfd_elf_gc_mark (info, isec,
14044 elf_gc_mark_debug_section))
14045 return false;
14046 }
14047 return true;
14048 }
14049
14050 static bool
14051 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
14052 {
14053 bfd *sub;
14054 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14055
14056 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
14057 {
14058 asection *o;
14059
14060 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
14061 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
14062 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
14063 continue;
14064 o = sub->sections;
14065 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14066 continue;
14067
14068 for (o = sub->sections; o != NULL; o = o->next)
14069 {
14070 /* When any section in a section group is kept, we keep all
14071 sections in the section group. If the first member of
14072 the section group is excluded, we will also exclude the
14073 group section. */
14074 if (o->flags & SEC_GROUP)
14075 {
14076 asection *first = elf_next_in_group (o);
14077 o->gc_mark = first->gc_mark;
14078 }
14079
14080 if (o->gc_mark)
14081 continue;
14082
14083 /* Skip sweeping sections already excluded. */
14084 if (o->flags & SEC_EXCLUDE)
14085 continue;
14086
14087 /* Since this is early in the link process, it is simple
14088 to remove a section from the output. */
14089 o->flags |= SEC_EXCLUDE;
14090
14091 if (info->print_gc_sections && o->size != 0)
14092 /* xgettext:c-format */
14093 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
14094 o, sub);
14095 }
14096 }
14097
14098 return true;
14099 }
14100
14101 /* Propagate collected vtable information. This is called through
14102 elf_link_hash_traverse. */
14103
14104 static bool
14105 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
14106 {
14107 /* Those that are not vtables. */
14108 if (h->start_stop
14109 || h->u2.vtable == NULL
14110 || h->u2.vtable->parent == NULL)
14111 return true;
14112
14113 /* Those vtables that do not have parents, we cannot merge. */
14114 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
14115 return true;
14116
14117 /* If we've already been done, exit. */
14118 if (h->u2.vtable->used && h->u2.vtable->used[-1])
14119 return true;
14120
14121 /* Make sure the parent's table is up to date. */
14122 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
14123
14124 if (h->u2.vtable->used == NULL)
14125 {
14126 /* None of this table's entries were referenced. Re-use the
14127 parent's table. */
14128 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
14129 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
14130 }
14131 else
14132 {
14133 size_t n;
14134 bool *cu, *pu;
14135
14136 /* Or the parent's entries into ours. */
14137 cu = h->u2.vtable->used;
14138 cu[-1] = true;
14139 pu = h->u2.vtable->parent->u2.vtable->used;
14140 if (pu != NULL)
14141 {
14142 const struct elf_backend_data *bed;
14143 unsigned int log_file_align;
14144
14145 bed = get_elf_backend_data (h->root.u.def.section->owner);
14146 log_file_align = bed->s->log_file_align;
14147 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
14148 while (n--)
14149 {
14150 if (*pu)
14151 *cu = true;
14152 pu++;
14153 cu++;
14154 }
14155 }
14156 }
14157
14158 return true;
14159 }
14160
14161 struct link_info_ok
14162 {
14163 struct bfd_link_info *info;
14164 bool ok;
14165 };
14166
14167 static bool
14168 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h,
14169 void *ptr)
14170 {
14171 asection *sec;
14172 bfd_vma hstart, hend;
14173 Elf_Internal_Rela *relstart, *relend, *rel;
14174 const struct elf_backend_data *bed;
14175 unsigned int log_file_align;
14176 struct link_info_ok *info = (struct link_info_ok *) ptr;
14177
14178 /* Take care of both those symbols that do not describe vtables as
14179 well as those that are not loaded. */
14180 if (h->start_stop
14181 || h->u2.vtable == NULL
14182 || h->u2.vtable->parent == NULL)
14183 return true;
14184
14185 BFD_ASSERT (h->root.type == bfd_link_hash_defined
14186 || h->root.type == bfd_link_hash_defweak);
14187
14188 sec = h->root.u.def.section;
14189 hstart = h->root.u.def.value;
14190 hend = hstart + h->size;
14191
14192 relstart = _bfd_elf_link_info_read_relocs (sec->owner, info->info,
14193 sec, NULL, NULL, true);
14194 if (!relstart)
14195 return info->ok = false;
14196 bed = get_elf_backend_data (sec->owner);
14197 log_file_align = bed->s->log_file_align;
14198
14199 relend = relstart + sec->reloc_count;
14200
14201 for (rel = relstart; rel < relend; ++rel)
14202 if (rel->r_offset >= hstart && rel->r_offset < hend)
14203 {
14204 /* If the entry is in use, do nothing. */
14205 if (h->u2.vtable->used
14206 && (rel->r_offset - hstart) < h->u2.vtable->size)
14207 {
14208 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
14209 if (h->u2.vtable->used[entry])
14210 continue;
14211 }
14212 /* Otherwise, kill it. */
14213 rel->r_offset = rel->r_info = rel->r_addend = 0;
14214 }
14215
14216 return true;
14217 }
14218
14219 /* Mark sections containing dynamically referenced symbols. When
14220 building shared libraries, we must assume that any visible symbol is
14221 referenced. */
14222
14223 bool
14224 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
14225 {
14226 struct bfd_link_info *info = (struct bfd_link_info *) inf;
14227 struct bfd_elf_dynamic_list *d = info->dynamic_list;
14228
14229 if ((h->root.type == bfd_link_hash_defined
14230 || h->root.type == bfd_link_hash_defweak)
14231 && (!h->start_stop
14232 || h->root.ldscript_def
14233 || !info->start_stop_gc)
14234 && ((h->ref_dynamic && !h->forced_local)
14235 || ((h->def_regular || ELF_COMMON_DEF_P (h))
14236 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
14237 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
14238 && (!bfd_link_executable (info)
14239 || info->gc_keep_exported
14240 || info->export_dynamic
14241 || (h->dynamic
14242 && d != NULL
14243 && (*d->match) (&d->head, NULL, h->root.root.string)))
14244 && (h->versioned >= versioned
14245 || !bfd_hide_sym_by_version (info->version_info,
14246 h->root.root.string)))))
14247 h->root.u.def.section->flags |= SEC_KEEP;
14248
14249 return true;
14250 }
14251
14252 /* Keep all sections containing symbols undefined on the command-line,
14253 and the section containing the entry symbol. */
14254
14255 void
14256 _bfd_elf_gc_keep (struct bfd_link_info *info)
14257 {
14258 struct bfd_sym_chain *sym;
14259
14260 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
14261 {
14262 struct elf_link_hash_entry *h;
14263
14264 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
14265 false, false, false);
14266
14267 if (h != NULL
14268 && (h->root.type == bfd_link_hash_defined
14269 || h->root.type == bfd_link_hash_defweak)
14270 && !bfd_is_const_section (h->root.u.def.section))
14271 h->root.u.def.section->flags |= SEC_KEEP;
14272 }
14273 }
14274
14275 bool
14276 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
14277 struct bfd_link_info *info)
14278 {
14279 bfd *ibfd = info->input_bfds;
14280
14281 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14282 {
14283 asection *sec;
14284 struct elf_reloc_cookie cookie;
14285
14286 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
14287 continue;
14288 sec = ibfd->sections;
14289 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14290 continue;
14291
14292 if (!init_reloc_cookie (&cookie, info, ibfd))
14293 return false;
14294
14295 for (sec = ibfd->sections; sec; sec = sec->next)
14296 {
14297 if (startswith (bfd_section_name (sec), ".eh_frame_entry")
14298 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
14299 {
14300 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
14301 fini_reloc_cookie_rels (&cookie, sec);
14302 }
14303 }
14304 }
14305 return true;
14306 }
14307
14308 /* Do mark and sweep of unused sections. */
14309
14310 bool
14311 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
14312 {
14313 bool ok = true;
14314 bfd *sub;
14315 elf_gc_mark_hook_fn gc_mark_hook;
14316 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14317 struct elf_link_hash_table *htab;
14318 struct link_info_ok info_ok;
14319
14320 if (!bed->can_gc_sections
14321 || !is_elf_hash_table (info->hash))
14322 {
14323 _bfd_error_handler(_("warning: gc-sections option ignored"));
14324 return true;
14325 }
14326
14327 bed->gc_keep (info);
14328 htab = elf_hash_table (info);
14329
14330 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
14331 at the .eh_frame section if we can mark the FDEs individually. */
14332 for (sub = info->input_bfds;
14333 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
14334 sub = sub->link.next)
14335 {
14336 asection *sec;
14337 struct elf_reloc_cookie cookie;
14338
14339 sec = sub->sections;
14340 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14341 continue;
14342 sec = bfd_get_section_by_name (sub, ".eh_frame");
14343 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
14344 {
14345 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
14346 if (elf_section_data (sec)->sec_info
14347 && (sec->flags & SEC_LINKER_CREATED) == 0)
14348 elf_eh_frame_section (sub) = sec;
14349 fini_reloc_cookie_for_section (&cookie, sec);
14350 sec = bfd_get_next_section_by_name (NULL, sec);
14351 }
14352 }
14353
14354 /* Apply transitive closure to the vtable entry usage info. */
14355 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
14356 if (!ok)
14357 return false;
14358
14359 /* Kill the vtable relocations that were not used. */
14360 info_ok.info = info;
14361 info_ok.ok = true;
14362 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &info_ok);
14363 if (!info_ok.ok)
14364 return false;
14365
14366 /* Mark dynamically referenced symbols. */
14367 if (htab->dynamic_sections_created || info->gc_keep_exported)
14368 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
14369
14370 /* Grovel through relocs to find out who stays ... */
14371 gc_mark_hook = bed->gc_mark_hook;
14372 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
14373 {
14374 asection *o;
14375
14376 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
14377 || elf_object_id (sub) != elf_hash_table_id (htab)
14378 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
14379 continue;
14380
14381 o = sub->sections;
14382 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14383 continue;
14384
14385 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
14386 Also treat note sections as a root, if the section is not part
14387 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
14388 well as FINI_ARRAY sections for ld -r. */
14389 for (o = sub->sections; o != NULL; o = o->next)
14390 if (!o->gc_mark
14391 && (o->flags & SEC_EXCLUDE) == 0
14392 && ((o->flags & SEC_KEEP) != 0
14393 || (bfd_link_relocatable (info)
14394 && ((elf_section_data (o)->this_hdr.sh_type
14395 == SHT_PREINIT_ARRAY)
14396 || (elf_section_data (o)->this_hdr.sh_type
14397 == SHT_INIT_ARRAY)
14398 || (elf_section_data (o)->this_hdr.sh_type
14399 == SHT_FINI_ARRAY)))
14400 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
14401 && elf_next_in_group (o) == NULL
14402 && elf_linked_to_section (o) == NULL)
14403 || ((elf_tdata (sub)->has_gnu_osabi & elf_gnu_osabi_retain)
14404 && (elf_section_flags (o) & SHF_GNU_RETAIN))))
14405 {
14406 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
14407 return false;
14408 }
14409 }
14410
14411 /* Allow the backend to mark additional target specific sections. */
14412 bed->gc_mark_extra_sections (info, gc_mark_hook);
14413
14414 /* ... and mark SEC_EXCLUDE for those that go. */
14415 return elf_gc_sweep (abfd, info);
14416 }
14417 \f
14418 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
14419
14420 bool
14421 bfd_elf_gc_record_vtinherit (bfd *abfd,
14422 asection *sec,
14423 struct elf_link_hash_entry *h,
14424 bfd_vma offset)
14425 {
14426 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
14427 struct elf_link_hash_entry **search, *child;
14428 size_t extsymcount;
14429 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14430
14431 /* The sh_info field of the symtab header tells us where the
14432 external symbols start. We don't care about the local symbols at
14433 this point. */
14434 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
14435 if (!elf_bad_symtab (abfd))
14436 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
14437
14438 sym_hashes = elf_sym_hashes (abfd);
14439 sym_hashes_end = PTR_ADD (sym_hashes, extsymcount);
14440
14441 /* Hunt down the child symbol, which is in this section at the same
14442 offset as the relocation. */
14443 for (search = sym_hashes; search != sym_hashes_end; ++search)
14444 {
14445 if ((child = *search) != NULL
14446 && (child->root.type == bfd_link_hash_defined
14447 || child->root.type == bfd_link_hash_defweak)
14448 && child->root.u.def.section == sec
14449 && child->root.u.def.value == offset)
14450 goto win;
14451 }
14452
14453 /* xgettext:c-format */
14454 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
14455 abfd, sec, (uint64_t) offset);
14456 bfd_set_error (bfd_error_invalid_operation);
14457 return false;
14458
14459 win:
14460 if (!child->u2.vtable)
14461 {
14462 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
14463 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
14464 if (!child->u2.vtable)
14465 return false;
14466 }
14467 if (!h)
14468 {
14469 /* This *should* only be the absolute section. It could potentially
14470 be that someone has defined a non-global vtable though, which
14471 would be bad. It isn't worth paging in the local symbols to be
14472 sure though; that case should simply be handled by the assembler. */
14473
14474 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
14475 }
14476 else
14477 child->u2.vtable->parent = h;
14478
14479 return true;
14480 }
14481
14482 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
14483
14484 bool
14485 bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec,
14486 struct elf_link_hash_entry *h,
14487 bfd_vma addend)
14488 {
14489 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14490 unsigned int log_file_align = bed->s->log_file_align;
14491
14492 if (!h)
14493 {
14494 /* xgettext:c-format */
14495 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
14496 abfd, sec);
14497 bfd_set_error (bfd_error_bad_value);
14498 return false;
14499 }
14500
14501 if (!h->u2.vtable)
14502 {
14503 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
14504 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
14505 if (!h->u2.vtable)
14506 return false;
14507 }
14508
14509 if (addend >= h->u2.vtable->size)
14510 {
14511 size_t size, bytes, file_align;
14512 bool *ptr = h->u2.vtable->used;
14513
14514 /* While the symbol is undefined, we have to be prepared to handle
14515 a zero size. */
14516 file_align = 1 << log_file_align;
14517 if (h->root.type == bfd_link_hash_undefined)
14518 size = addend + file_align;
14519 else
14520 {
14521 size = h->size;
14522 if (addend >= size)
14523 {
14524 /* Oops! We've got a reference past the defined end of
14525 the table. This is probably a bug -- shall we warn? */
14526 size = addend + file_align;
14527 }
14528 }
14529 size = (size + file_align - 1) & -file_align;
14530
14531 /* Allocate one extra entry for use as a "done" flag for the
14532 consolidation pass. */
14533 bytes = ((size >> log_file_align) + 1) * sizeof (bool);
14534
14535 if (ptr)
14536 {
14537 ptr = (bool *) bfd_realloc (ptr - 1, bytes);
14538
14539 if (ptr != NULL)
14540 {
14541 size_t oldbytes;
14542
14543 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
14544 * sizeof (bool));
14545 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
14546 }
14547 }
14548 else
14549 ptr = (bool *) bfd_zmalloc (bytes);
14550
14551 if (ptr == NULL)
14552 return false;
14553
14554 /* And arrange for that done flag to be at index -1. */
14555 h->u2.vtable->used = ptr + 1;
14556 h->u2.vtable->size = size;
14557 }
14558
14559 h->u2.vtable->used[addend >> log_file_align] = true;
14560
14561 return true;
14562 }
14563
14564 /* Map an ELF section header flag to its corresponding string. */
14565 typedef struct
14566 {
14567 char *flag_name;
14568 flagword flag_value;
14569 } elf_flags_to_name_table;
14570
14571 static const elf_flags_to_name_table elf_flags_to_names [] =
14572 {
14573 { "SHF_WRITE", SHF_WRITE },
14574 { "SHF_ALLOC", SHF_ALLOC },
14575 { "SHF_EXECINSTR", SHF_EXECINSTR },
14576 { "SHF_MERGE", SHF_MERGE },
14577 { "SHF_STRINGS", SHF_STRINGS },
14578 { "SHF_INFO_LINK", SHF_INFO_LINK},
14579 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
14580 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
14581 { "SHF_GROUP", SHF_GROUP },
14582 { "SHF_TLS", SHF_TLS },
14583 { "SHF_MASKOS", SHF_MASKOS },
14584 { "SHF_EXCLUDE", SHF_EXCLUDE },
14585 };
14586
14587 /* Returns TRUE if the section is to be included, otherwise FALSE. */
14588 bool
14589 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
14590 struct flag_info *flaginfo,
14591 asection *section)
14592 {
14593 const bfd_vma sh_flags = elf_section_flags (section);
14594
14595 if (!flaginfo->flags_initialized)
14596 {
14597 bfd *obfd = info->output_bfd;
14598 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14599 struct flag_info_list *tf = flaginfo->flag_list;
14600 int with_hex = 0;
14601 int without_hex = 0;
14602
14603 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
14604 {
14605 unsigned i;
14606 flagword (*lookup) (char *);
14607
14608 lookup = bed->elf_backend_lookup_section_flags_hook;
14609 if (lookup != NULL)
14610 {
14611 flagword hexval = (*lookup) ((char *) tf->name);
14612
14613 if (hexval != 0)
14614 {
14615 if (tf->with == with_flags)
14616 with_hex |= hexval;
14617 else if (tf->with == without_flags)
14618 without_hex |= hexval;
14619 tf->valid = true;
14620 continue;
14621 }
14622 }
14623 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
14624 {
14625 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
14626 {
14627 if (tf->with == with_flags)
14628 with_hex |= elf_flags_to_names[i].flag_value;
14629 else if (tf->with == without_flags)
14630 without_hex |= elf_flags_to_names[i].flag_value;
14631 tf->valid = true;
14632 break;
14633 }
14634 }
14635 if (!tf->valid)
14636 {
14637 info->callbacks->einfo
14638 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
14639 return false;
14640 }
14641 }
14642 flaginfo->flags_initialized = true;
14643 flaginfo->only_with_flags |= with_hex;
14644 flaginfo->not_with_flags |= without_hex;
14645 }
14646
14647 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
14648 return false;
14649
14650 if ((flaginfo->not_with_flags & sh_flags) != 0)
14651 return false;
14652
14653 return true;
14654 }
14655
14656 struct alloc_got_off_arg {
14657 bfd_vma gotoff;
14658 struct bfd_link_info *info;
14659 };
14660
14661 /* We need a special top-level link routine to convert got reference counts
14662 to real got offsets. */
14663
14664 static bool
14665 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
14666 {
14667 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
14668 bfd *obfd = gofarg->info->output_bfd;
14669 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14670
14671 if (h->got.refcount > 0)
14672 {
14673 h->got.offset = gofarg->gotoff;
14674 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
14675 }
14676 else
14677 h->got.offset = (bfd_vma) -1;
14678
14679 return true;
14680 }
14681
14682 /* And an accompanying bit to work out final got entry offsets once
14683 we're done. Should be called from final_link. */
14684
14685 bool
14686 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
14687 struct bfd_link_info *info)
14688 {
14689 bfd *i;
14690 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14691 bfd_vma gotoff;
14692 struct alloc_got_off_arg gofarg;
14693
14694 BFD_ASSERT (abfd == info->output_bfd);
14695
14696 if (! is_elf_hash_table (info->hash))
14697 return false;
14698
14699 /* The GOT offset is relative to the .got section, but the GOT header is
14700 put into the .got.plt section, if the backend uses it. */
14701 if (bed->want_got_plt)
14702 gotoff = 0;
14703 else
14704 gotoff = bed->got_header_size;
14705
14706 /* Do the local .got entries first. */
14707 for (i = info->input_bfds; i; i = i->link.next)
14708 {
14709 bfd_signed_vma *local_got;
14710 size_t j, locsymcount;
14711 Elf_Internal_Shdr *symtab_hdr;
14712
14713 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
14714 continue;
14715
14716 local_got = elf_local_got_refcounts (i);
14717 if (!local_got)
14718 continue;
14719
14720 symtab_hdr = &elf_tdata (i)->symtab_hdr;
14721 if (elf_bad_symtab (i))
14722 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
14723 else
14724 locsymcount = symtab_hdr->sh_info;
14725
14726 for (j = 0; j < locsymcount; ++j)
14727 {
14728 if (local_got[j] > 0)
14729 {
14730 local_got[j] = gotoff;
14731 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
14732 }
14733 else
14734 local_got[j] = (bfd_vma) -1;
14735 }
14736 }
14737
14738 /* Then the global .got entries. .plt refcounts are handled by
14739 adjust_dynamic_symbol */
14740 gofarg.gotoff = gotoff;
14741 gofarg.info = info;
14742 elf_link_hash_traverse (elf_hash_table (info),
14743 elf_gc_allocate_got_offsets,
14744 &gofarg);
14745 return true;
14746 }
14747
14748 /* Many folk need no more in the way of final link than this, once
14749 got entry reference counting is enabled. */
14750
14751 bool
14752 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
14753 {
14754 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
14755 return false;
14756
14757 /* Invoke the regular ELF backend linker to do all the work. */
14758 return bfd_elf_final_link (abfd, info);
14759 }
14760
14761 bool
14762 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
14763 {
14764 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
14765
14766 if (rcookie->bad_symtab)
14767 rcookie->rel = rcookie->rels;
14768
14769 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
14770 {
14771 unsigned long r_symndx;
14772
14773 if (! rcookie->bad_symtab)
14774 if (rcookie->rel->r_offset > offset)
14775 return false;
14776 if (rcookie->rel->r_offset != offset)
14777 continue;
14778
14779 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
14780 if (r_symndx == STN_UNDEF)
14781 return true;
14782
14783 if (r_symndx >= rcookie->locsymcount
14784 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
14785 {
14786 struct elf_link_hash_entry *h;
14787
14788 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
14789
14790 while (h->root.type == bfd_link_hash_indirect
14791 || h->root.type == bfd_link_hash_warning)
14792 h = (struct elf_link_hash_entry *) h->root.u.i.link;
14793
14794 if ((h->root.type == bfd_link_hash_defined
14795 || h->root.type == bfd_link_hash_defweak)
14796 && (h->root.u.def.section->owner != rcookie->abfd
14797 || h->root.u.def.section->kept_section != NULL
14798 || discarded_section (h->root.u.def.section)))
14799 return true;
14800 }
14801 else
14802 {
14803 /* It's not a relocation against a global symbol,
14804 but it could be a relocation against a local
14805 symbol for a discarded section. */
14806 asection *isec;
14807 Elf_Internal_Sym *isym;
14808
14809 /* Need to: get the symbol; get the section. */
14810 isym = &rcookie->locsyms[r_symndx];
14811 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
14812 if (isec != NULL
14813 && (isec->kept_section != NULL
14814 || discarded_section (isec)))
14815 return true;
14816 }
14817 return false;
14818 }
14819 return false;
14820 }
14821
14822 /* Discard unneeded references to discarded sections.
14823 Returns -1 on error, 1 if any section's size was changed, 0 if
14824 nothing changed. This function assumes that the relocations are in
14825 sorted order, which is true for all known assemblers. */
14826
14827 int
14828 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
14829 {
14830 struct elf_reloc_cookie cookie;
14831 asection *o;
14832 bfd *abfd;
14833 int changed = 0;
14834
14835 if (info->traditional_format
14836 || !is_elf_hash_table (info->hash))
14837 return 0;
14838
14839 o = bfd_get_section_by_name (output_bfd, ".stab");
14840 if (o != NULL)
14841 {
14842 asection *i;
14843
14844 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14845 {
14846 if (i->size == 0
14847 || i->reloc_count == 0
14848 || i->sec_info_type != SEC_INFO_TYPE_STABS)
14849 continue;
14850
14851 abfd = i->owner;
14852 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14853 continue;
14854
14855 if (!init_reloc_cookie_for_section (&cookie, info, i))
14856 return -1;
14857
14858 if (_bfd_discard_section_stabs (abfd, i,
14859 elf_section_data (i)->sec_info,
14860 bfd_elf_reloc_symbol_deleted_p,
14861 &cookie))
14862 changed = 1;
14863
14864 fini_reloc_cookie_for_section (&cookie, i);
14865 }
14866 }
14867
14868 o = NULL;
14869 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
14870 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
14871 if (o != NULL)
14872 {
14873 asection *i;
14874 int eh_changed = 0;
14875 unsigned int eh_alignment; /* Octets. */
14876
14877 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14878 {
14879 if (i->size == 0)
14880 continue;
14881
14882 abfd = i->owner;
14883 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14884 continue;
14885
14886 if (!init_reloc_cookie_for_section (&cookie, info, i))
14887 return -1;
14888
14889 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
14890 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
14891 bfd_elf_reloc_symbol_deleted_p,
14892 &cookie))
14893 {
14894 eh_changed = 1;
14895 if (i->size != i->rawsize)
14896 changed = 1;
14897 }
14898
14899 fini_reloc_cookie_for_section (&cookie, i);
14900 }
14901
14902 eh_alignment = ((1 << o->alignment_power)
14903 * bfd_octets_per_byte (output_bfd, o));
14904 /* Skip over zero terminator, and prevent empty sections from
14905 adding alignment padding at the end. */
14906 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
14907 if (i->size == 0)
14908 i->flags |= SEC_EXCLUDE;
14909 else if (i->size > 4)
14910 break;
14911 /* The last non-empty eh_frame section doesn't need padding. */
14912 if (i != NULL)
14913 i = i->map_tail.s;
14914 /* Any prior sections must pad the last FDE out to the output
14915 section alignment. Otherwise we might have zero padding
14916 between sections, which would be seen as a terminator. */
14917 for (; i != NULL; i = i->map_tail.s)
14918 if (i->size == 4)
14919 /* All but the last zero terminator should have been removed. */
14920 BFD_FAIL ();
14921 else
14922 {
14923 bfd_size_type size
14924 = (i->size + eh_alignment - 1) & -eh_alignment;
14925 if (i->size != size)
14926 {
14927 i->size = size;
14928 changed = 1;
14929 eh_changed = 1;
14930 }
14931 }
14932 if (eh_changed)
14933 elf_link_hash_traverse (elf_hash_table (info),
14934 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
14935 }
14936
14937 o = bfd_get_section_by_name (output_bfd, ".sframe");
14938 if (o != NULL)
14939 {
14940 asection *i;
14941
14942 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14943 {
14944 if (i->size == 0)
14945 continue;
14946
14947 abfd = i->owner;
14948 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14949 continue;
14950
14951 if (!init_reloc_cookie_for_section (&cookie, info, i))
14952 return -1;
14953
14954 if (_bfd_elf_parse_sframe (abfd, info, i, &cookie))
14955 {
14956 if (_bfd_elf_discard_section_sframe (i,
14957 bfd_elf_reloc_symbol_deleted_p,
14958 &cookie))
14959 {
14960 if (i->size != i->rawsize)
14961 changed = 1;
14962 }
14963 }
14964 fini_reloc_cookie_for_section (&cookie, i);
14965 }
14966 /* Update the reference to the output .sframe section. Used to
14967 determine later if PT_GNU_SFRAME segment is to be generated. */
14968 if (!_bfd_elf_set_section_sframe (output_bfd, info))
14969 return -1;
14970 }
14971
14972 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
14973 {
14974 const struct elf_backend_data *bed;
14975 asection *s;
14976
14977 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14978 continue;
14979 s = abfd->sections;
14980 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14981 continue;
14982
14983 bed = get_elf_backend_data (abfd);
14984
14985 if (bed->elf_backend_discard_info != NULL)
14986 {
14987 if (!init_reloc_cookie (&cookie, info, abfd))
14988 return -1;
14989
14990 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
14991 changed = 1;
14992
14993 fini_reloc_cookie (&cookie, abfd);
14994 }
14995 }
14996
14997 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
14998 _bfd_elf_end_eh_frame_parsing (info);
14999
15000 if (info->eh_frame_hdr_type
15001 && !bfd_link_relocatable (info)
15002 && _bfd_elf_discard_section_eh_frame_hdr (info))
15003 changed = 1;
15004
15005 return changed;
15006 }
15007
15008 bool
15009 _bfd_elf_section_already_linked (bfd *abfd,
15010 asection *sec,
15011 struct bfd_link_info *info)
15012 {
15013 flagword flags;
15014 const char *name, *key;
15015 struct bfd_section_already_linked *l;
15016 struct bfd_section_already_linked_hash_entry *already_linked_list;
15017
15018 if (sec->output_section == bfd_abs_section_ptr)
15019 return false;
15020
15021 flags = sec->flags;
15022
15023 /* Return if it isn't a linkonce section. A comdat group section
15024 also has SEC_LINK_ONCE set. */
15025 if ((flags & SEC_LINK_ONCE) == 0)
15026 return false;
15027
15028 /* Don't put group member sections on our list of already linked
15029 sections. They are handled as a group via their group section. */
15030 if (elf_sec_group (sec) != NULL)
15031 return false;
15032
15033 /* For a SHT_GROUP section, use the group signature as the key. */
15034 name = sec->name;
15035 if ((flags & SEC_GROUP) != 0
15036 && elf_next_in_group (sec) != NULL
15037 && elf_group_name (elf_next_in_group (sec)) != NULL)
15038 key = elf_group_name (elf_next_in_group (sec));
15039 else
15040 {
15041 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
15042 if (startswith (name, ".gnu.linkonce.")
15043 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
15044 key++;
15045 else
15046 /* Must be a user linkonce section that doesn't follow gcc's
15047 naming convention. In this case we won't be matching
15048 single member groups. */
15049 key = name;
15050 }
15051
15052 already_linked_list = bfd_section_already_linked_table_lookup (key);
15053
15054 for (l = already_linked_list->entry; l != NULL; l = l->next)
15055 {
15056 /* We may have 2 different types of sections on the list: group
15057 sections with a signature of <key> (<key> is some string),
15058 and linkonce sections named .gnu.linkonce.<type>.<key>.
15059 Match like sections. LTO plugin sections are an exception.
15060 They are always named .gnu.linkonce.t.<key> and match either
15061 type of section. */
15062 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
15063 && ((flags & SEC_GROUP) != 0
15064 || strcmp (name, l->sec->name) == 0))
15065 || (l->sec->owner->flags & BFD_PLUGIN) != 0
15066 || (sec->owner->flags & BFD_PLUGIN) != 0)
15067 {
15068 /* The section has already been linked. See if we should
15069 issue a warning. */
15070 if (!_bfd_handle_already_linked (sec, l, info))
15071 return false;
15072
15073 if (flags & SEC_GROUP)
15074 {
15075 asection *first = elf_next_in_group (sec);
15076 asection *s = first;
15077
15078 while (s != NULL)
15079 {
15080 s->output_section = bfd_abs_section_ptr;
15081 /* Record which group discards it. */
15082 s->kept_section = l->sec;
15083 s = elf_next_in_group (s);
15084 /* These lists are circular. */
15085 if (s == first)
15086 break;
15087 }
15088 }
15089
15090 return true;
15091 }
15092 }
15093
15094 /* A single member comdat group section may be discarded by a
15095 linkonce section and vice versa. */
15096 if ((flags & SEC_GROUP) != 0)
15097 {
15098 asection *first = elf_next_in_group (sec);
15099
15100 if (first != NULL && elf_next_in_group (first) == first)
15101 /* Check this single member group against linkonce sections. */
15102 for (l = already_linked_list->entry; l != NULL; l = l->next)
15103 if ((l->sec->flags & SEC_GROUP) == 0
15104 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
15105 {
15106 first->output_section = bfd_abs_section_ptr;
15107 first->kept_section = l->sec;
15108 sec->output_section = bfd_abs_section_ptr;
15109 break;
15110 }
15111 }
15112 else
15113 /* Check this linkonce section against single member groups. */
15114 for (l = already_linked_list->entry; l != NULL; l = l->next)
15115 if (l->sec->flags & SEC_GROUP)
15116 {
15117 asection *first = elf_next_in_group (l->sec);
15118
15119 if (first != NULL
15120 && elf_next_in_group (first) == first
15121 && bfd_elf_match_symbols_in_sections (first, sec, info))
15122 {
15123 sec->output_section = bfd_abs_section_ptr;
15124 sec->kept_section = first;
15125 break;
15126 }
15127 }
15128
15129 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
15130 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
15131 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
15132 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
15133 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
15134 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
15135 `.gnu.linkonce.t.F' section from a different bfd not requiring any
15136 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
15137 The reverse order cannot happen as there is never a bfd with only the
15138 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
15139 matter as here were are looking only for cross-bfd sections. */
15140
15141 if ((flags & SEC_GROUP) == 0 && startswith (name, ".gnu.linkonce.r."))
15142 for (l = already_linked_list->entry; l != NULL; l = l->next)
15143 if ((l->sec->flags & SEC_GROUP) == 0
15144 && startswith (l->sec->name, ".gnu.linkonce.t."))
15145 {
15146 if (abfd != l->sec->owner)
15147 sec->output_section = bfd_abs_section_ptr;
15148 break;
15149 }
15150
15151 /* This is the first section with this name. Record it. */
15152 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
15153 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
15154 return sec->output_section == bfd_abs_section_ptr;
15155 }
15156
15157 bool
15158 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
15159 {
15160 return sym->st_shndx == SHN_COMMON;
15161 }
15162
15163 unsigned int
15164 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
15165 {
15166 return SHN_COMMON;
15167 }
15168
15169 asection *
15170 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
15171 {
15172 return bfd_com_section_ptr;
15173 }
15174
15175 bfd_vma
15176 _bfd_elf_default_got_elt_size (bfd *abfd,
15177 struct bfd_link_info *info ATTRIBUTE_UNUSED,
15178 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
15179 bfd *ibfd ATTRIBUTE_UNUSED,
15180 unsigned long symndx ATTRIBUTE_UNUSED)
15181 {
15182 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15183 return bed->s->arch_size / 8;
15184 }
15185
15186 /* Routines to support the creation of dynamic relocs. */
15187
15188 /* Returns the name of the dynamic reloc section associated with SEC. */
15189
15190 static const char *
15191 get_dynamic_reloc_section_name (bfd * abfd,
15192 asection * sec,
15193 bool is_rela)
15194 {
15195 char *name;
15196 const char *old_name = bfd_section_name (sec);
15197 const char *prefix = is_rela ? ".rela" : ".rel";
15198
15199 if (old_name == NULL)
15200 return NULL;
15201
15202 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
15203 sprintf (name, "%s%s", prefix, old_name);
15204
15205 return name;
15206 }
15207
15208 /* Returns the dynamic reloc section associated with SEC.
15209 If necessary compute the name of the dynamic reloc section based
15210 on SEC's name (looked up in ABFD's string table) and the setting
15211 of IS_RELA. */
15212
15213 asection *
15214 _bfd_elf_get_dynamic_reloc_section (bfd *abfd,
15215 asection *sec,
15216 bool is_rela)
15217 {
15218 asection *reloc_sec = elf_section_data (sec)->sreloc;
15219
15220 if (reloc_sec == NULL)
15221 {
15222 const char *name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
15223
15224 if (name != NULL)
15225 {
15226 reloc_sec = bfd_get_linker_section (abfd, name);
15227
15228 if (reloc_sec != NULL)
15229 elf_section_data (sec)->sreloc = reloc_sec;
15230 }
15231 }
15232
15233 return reloc_sec;
15234 }
15235
15236 /* Returns the dynamic reloc section associated with SEC. If the
15237 section does not exist it is created and attached to the DYNOBJ
15238 bfd and stored in the SRELOC field of SEC's elf_section_data
15239 structure.
15240
15241 ALIGNMENT is the alignment for the newly created section and
15242 IS_RELA defines whether the name should be .rela.<SEC's name>
15243 or .rel.<SEC's name>. The section name is looked up in the
15244 string table associated with ABFD. */
15245
15246 asection *
15247 _bfd_elf_make_dynamic_reloc_section (asection *sec,
15248 bfd *dynobj,
15249 unsigned int alignment,
15250 bfd *abfd,
15251 bool is_rela)
15252 {
15253 asection * reloc_sec = elf_section_data (sec)->sreloc;
15254
15255 if (reloc_sec == NULL)
15256 {
15257 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
15258
15259 if (name == NULL)
15260 return NULL;
15261
15262 reloc_sec = bfd_get_linker_section (dynobj, name);
15263
15264 if (reloc_sec == NULL)
15265 {
15266 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
15267 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
15268 if ((sec->flags & SEC_ALLOC) != 0)
15269 flags |= SEC_ALLOC | SEC_LOAD;
15270
15271 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
15272 if (reloc_sec != NULL)
15273 {
15274 /* _bfd_elf_get_sec_type_attr chooses a section type by
15275 name. Override as it may be wrong, eg. for a user
15276 section named "auto" we'll get ".relauto" which is
15277 seen to be a .rela section. */
15278 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
15279 if (!bfd_set_section_alignment (reloc_sec, alignment))
15280 reloc_sec = NULL;
15281 }
15282 }
15283
15284 elf_section_data (sec)->sreloc = reloc_sec;
15285 }
15286
15287 return reloc_sec;
15288 }
15289
15290 /* Copy the ELF symbol type and other attributes for a linker script
15291 assignment from HSRC to HDEST. Generally this should be treated as
15292 if we found a strong non-dynamic definition for HDEST (except that
15293 ld ignores multiple definition errors). */
15294 void
15295 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
15296 struct bfd_link_hash_entry *hdest,
15297 struct bfd_link_hash_entry *hsrc)
15298 {
15299 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
15300 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
15301 Elf_Internal_Sym isym;
15302
15303 ehdest->type = ehsrc->type;
15304 ehdest->target_internal = ehsrc->target_internal;
15305
15306 isym.st_other = ehsrc->other;
15307 elf_merge_st_other (abfd, ehdest, isym.st_other, NULL, true, false);
15308 }
15309
15310 /* Append a RELA relocation REL to section S in BFD. */
15311
15312 void
15313 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15314 {
15315 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15316 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
15317 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
15318 bed->s->swap_reloca_out (abfd, rel, loc);
15319 }
15320
15321 /* Append a REL relocation REL to section S in BFD. */
15322
15323 void
15324 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15325 {
15326 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15327 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
15328 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
15329 bed->s->swap_reloc_out (abfd, rel, loc);
15330 }
15331
15332 /* Define __start, __stop, .startof. or .sizeof. symbol. */
15333
15334 struct bfd_link_hash_entry *
15335 bfd_elf_define_start_stop (struct bfd_link_info *info,
15336 const char *symbol, asection *sec)
15337 {
15338 struct elf_link_hash_entry *h;
15339
15340 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
15341 false, false, true);
15342 /* NB: Common symbols will be turned into definition later. */
15343 if (h != NULL
15344 && !h->root.ldscript_def
15345 && (h->root.type == bfd_link_hash_undefined
15346 || h->root.type == bfd_link_hash_undefweak
15347 || ((h->ref_regular || h->def_dynamic)
15348 && !h->def_regular
15349 && h->root.type != bfd_link_hash_common)))
15350 {
15351 bool was_dynamic = h->ref_dynamic || h->def_dynamic;
15352 h->verinfo.verdef = NULL;
15353 h->root.type = bfd_link_hash_defined;
15354 h->root.u.def.section = sec;
15355 h->root.u.def.value = 0;
15356 h->def_regular = 1;
15357 h->def_dynamic = 0;
15358 h->start_stop = 1;
15359 h->u2.start_stop_section = sec;
15360 if (symbol[0] == '.')
15361 {
15362 /* .startof. and .sizeof. symbols are local. */
15363 const struct elf_backend_data *bed;
15364 bed = get_elf_backend_data (info->output_bfd);
15365 (*bed->elf_backend_hide_symbol) (info, h, true);
15366 }
15367 else
15368 {
15369 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
15370 h->other = ((h->other & ~ELF_ST_VISIBILITY (-1))
15371 | info->start_stop_visibility);
15372 if (was_dynamic)
15373 bfd_elf_link_record_dynamic_symbol (info, h);
15374 }
15375 return &h->root;
15376 }
15377 return NULL;
15378 }
15379
15380 /* Find dynamic relocs for H that apply to read-only sections. */
15381
15382 asection *
15383 _bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry *h)
15384 {
15385 struct elf_dyn_relocs *p;
15386
15387 for (p = h->dyn_relocs; p != NULL; p = p->next)
15388 {
15389 asection *s = p->sec->output_section;
15390
15391 if (s != NULL && (s->flags & SEC_READONLY) != 0)
15392 return p->sec;
15393 }
15394 return NULL;
15395 }
15396
15397 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
15398 read-only sections. */
15399
15400 bool
15401 _bfd_elf_maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
15402 {
15403 asection *sec;
15404
15405 if (h->root.type == bfd_link_hash_indirect)
15406 return true;
15407
15408 sec = _bfd_elf_readonly_dynrelocs (h);
15409 if (sec != NULL)
15410 {
15411 struct bfd_link_info *info = (struct bfd_link_info *) inf;
15412
15413 info->flags |= DF_TEXTREL;
15414 /* xgettext:c-format */
15415 info->callbacks->minfo (_("%pB: dynamic relocation against `%pT' "
15416 "in read-only section `%pA'\n"),
15417 sec->owner, h->root.root.string, sec);
15418
15419 if (bfd_link_textrel_check (info))
15420 /* xgettext:c-format */
15421 info->callbacks->einfo (_("%P: %pB: warning: relocation against `%s' "
15422 "in read-only section `%pA'\n"),
15423 sec->owner, h->root.root.string, sec);
15424
15425 /* Not an error, just cut short the traversal. */
15426 return false;
15427 }
15428 return true;
15429 }
15430
15431 /* Add dynamic tags. */
15432
15433 bool
15434 _bfd_elf_add_dynamic_tags (bfd *output_bfd, struct bfd_link_info *info,
15435 bool need_dynamic_reloc)
15436 {
15437 struct elf_link_hash_table *htab = elf_hash_table (info);
15438
15439 if (htab->dynamic_sections_created)
15440 {
15441 /* Add some entries to the .dynamic section. We fill in the
15442 values later, in finish_dynamic_sections, but we must add
15443 the entries now so that we get the correct size for the
15444 .dynamic section. The DT_DEBUG entry is filled in by the
15445 dynamic linker and used by the debugger. */
15446 #define add_dynamic_entry(TAG, VAL) \
15447 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
15448
15449 const struct elf_backend_data *bed
15450 = get_elf_backend_data (output_bfd);
15451
15452 if (bfd_link_executable (info))
15453 {
15454 if (!add_dynamic_entry (DT_DEBUG, 0))
15455 return false;
15456 }
15457
15458 if (htab->dt_pltgot_required || htab->splt->size != 0)
15459 {
15460 /* DT_PLTGOT is used by prelink even if there is no PLT
15461 relocation. */
15462 if (!add_dynamic_entry (DT_PLTGOT, 0))
15463 return false;
15464 }
15465
15466 if (htab->dt_jmprel_required || htab->srelplt->size != 0)
15467 {
15468 if (!add_dynamic_entry (DT_PLTRELSZ, 0)
15469 || !add_dynamic_entry (DT_PLTREL,
15470 (bed->rela_plts_and_copies_p
15471 ? DT_RELA : DT_REL))
15472 || !add_dynamic_entry (DT_JMPREL, 0))
15473 return false;
15474 }
15475
15476 if (htab->tlsdesc_plt
15477 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
15478 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
15479 return false;
15480
15481 if (need_dynamic_reloc)
15482 {
15483 if (bed->rela_plts_and_copies_p)
15484 {
15485 if (!add_dynamic_entry (DT_RELA, 0)
15486 || !add_dynamic_entry (DT_RELASZ, 0)
15487 || !add_dynamic_entry (DT_RELAENT,
15488 bed->s->sizeof_rela))
15489 return false;
15490 }
15491 else
15492 {
15493 if (!add_dynamic_entry (DT_REL, 0)
15494 || !add_dynamic_entry (DT_RELSZ, 0)
15495 || !add_dynamic_entry (DT_RELENT,
15496 bed->s->sizeof_rel))
15497 return false;
15498 }
15499
15500 /* If any dynamic relocs apply to a read-only section,
15501 then we need a DT_TEXTREL entry. */
15502 if ((info->flags & DF_TEXTREL) == 0)
15503 elf_link_hash_traverse (htab, _bfd_elf_maybe_set_textrel,
15504 info);
15505
15506 if ((info->flags & DF_TEXTREL) != 0)
15507 {
15508 if (htab->ifunc_resolvers)
15509 info->callbacks->einfo
15510 (_("%P: warning: GNU indirect functions with DT_TEXTREL "
15511 "may result in a segfault at runtime; recompile with %s\n"),
15512 bfd_link_dll (info) ? "-fPIC" : "-fPIE");
15513
15514 if (!add_dynamic_entry (DT_TEXTREL, 0))
15515 return false;
15516 }
15517 }
15518 }
15519 #undef add_dynamic_entry
15520
15521 return true;
15522 }