Detect .eh_frame_hdr earlier for SIZEOF_HEADERS
[binutils-gdb.git] / bfd / elflink.c
1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2022 Free Software Foundation, Inc.
3
4 This file is part of BFD, the Binary File Descriptor library.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
20
21 #include "sysdep.h"
22 #include "bfd.h"
23 #include "bfdlink.h"
24 #include "libbfd.h"
25 #define ARCH_SIZE 0
26 #include "elf-bfd.h"
27 #include "safe-ctype.h"
28 #include "libiberty.h"
29 #include "objalloc.h"
30 #if BFD_SUPPORTS_PLUGINS
31 #include "plugin-api.h"
32 #include "plugin.h"
33 #endif
34
35 #include <limits.h>
36 #ifndef CHAR_BIT
37 #define CHAR_BIT 8
38 #endif
39
40 /* This struct is used to pass information to routines called via
41 elf_link_hash_traverse which must return failure. */
42
43 struct elf_info_failed
44 {
45 struct bfd_link_info *info;
46 bool failed;
47 };
48
49 /* This structure is used to pass information to
50 _bfd_elf_link_find_version_dependencies. */
51
52 struct elf_find_verdep_info
53 {
54 /* General link information. */
55 struct bfd_link_info *info;
56 /* The number of dependencies. */
57 unsigned int vers;
58 /* Whether we had a failure. */
59 bool failed;
60 };
61
62 static bool _bfd_elf_fix_symbol_flags
63 (struct elf_link_hash_entry *, struct elf_info_failed *);
64
65 asection *
66 _bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
67 unsigned long r_symndx,
68 bool discard)
69 {
70 if (r_symndx >= cookie->locsymcount
71 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
72 {
73 struct elf_link_hash_entry *h;
74
75 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
76
77 while (h->root.type == bfd_link_hash_indirect
78 || h->root.type == bfd_link_hash_warning)
79 h = (struct elf_link_hash_entry *) h->root.u.i.link;
80
81 if ((h->root.type == bfd_link_hash_defined
82 || h->root.type == bfd_link_hash_defweak)
83 && discarded_section (h->root.u.def.section))
84 return h->root.u.def.section;
85 else
86 return NULL;
87 }
88 else
89 {
90 /* It's not a relocation against a global symbol,
91 but it could be a relocation against a local
92 symbol for a discarded section. */
93 asection *isec;
94 Elf_Internal_Sym *isym;
95
96 /* Need to: get the symbol; get the section. */
97 isym = &cookie->locsyms[r_symndx];
98 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
99 if (isec != NULL
100 && discard ? discarded_section (isec) : 1)
101 return isec;
102 }
103 return NULL;
104 }
105
106 /* Define a symbol in a dynamic linkage section. */
107
108 struct elf_link_hash_entry *
109 _bfd_elf_define_linkage_sym (bfd *abfd,
110 struct bfd_link_info *info,
111 asection *sec,
112 const char *name)
113 {
114 struct elf_link_hash_entry *h;
115 struct bfd_link_hash_entry *bh;
116 const struct elf_backend_data *bed;
117
118 h = elf_link_hash_lookup (elf_hash_table (info), name, false, false, false);
119 if (h != NULL)
120 {
121 /* Zap symbol defined in an as-needed lib that wasn't linked.
122 This is a symptom of a larger problem: Absolute symbols
123 defined in shared libraries can't be overridden, because we
124 lose the link to the bfd which is via the symbol section. */
125 h->root.type = bfd_link_hash_new;
126 bh = &h->root;
127 }
128 else
129 bh = NULL;
130
131 bed = get_elf_backend_data (abfd);
132 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
133 sec, 0, NULL, false, bed->collect,
134 &bh))
135 return NULL;
136 h = (struct elf_link_hash_entry *) bh;
137 BFD_ASSERT (h != NULL);
138 h->def_regular = 1;
139 h->non_elf = 0;
140 h->root.linker_def = 1;
141 h->type = STT_OBJECT;
142 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
143 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
144
145 (*bed->elf_backend_hide_symbol) (info, h, true);
146 return h;
147 }
148
149 bool
150 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
151 {
152 flagword flags;
153 asection *s;
154 struct elf_link_hash_entry *h;
155 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
156 struct elf_link_hash_table *htab = elf_hash_table (info);
157
158 /* This function may be called more than once. */
159 if (htab->sgot != NULL)
160 return true;
161
162 flags = bed->dynamic_sec_flags;
163
164 s = bfd_make_section_anyway_with_flags (abfd,
165 (bed->rela_plts_and_copies_p
166 ? ".rela.got" : ".rel.got"),
167 (bed->dynamic_sec_flags
168 | SEC_READONLY));
169 if (s == NULL
170 || !bfd_set_section_alignment (s, bed->s->log_file_align))
171 return false;
172 htab->srelgot = s;
173
174 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
175 if (s == NULL
176 || !bfd_set_section_alignment (s, bed->s->log_file_align))
177 return false;
178 htab->sgot = s;
179
180 if (bed->want_got_plt)
181 {
182 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
183 if (s == NULL
184 || !bfd_set_section_alignment (s, bed->s->log_file_align))
185 return false;
186 htab->sgotplt = s;
187 }
188
189 /* The first bit of the global offset table is the header. */
190 s->size += bed->got_header_size;
191
192 if (bed->want_got_sym)
193 {
194 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
195 (or .got.plt) section. We don't do this in the linker script
196 because we don't want to define the symbol if we are not creating
197 a global offset table. */
198 h = _bfd_elf_define_linkage_sym (abfd, info, s,
199 "_GLOBAL_OFFSET_TABLE_");
200 elf_hash_table (info)->hgot = h;
201 if (h == NULL)
202 return false;
203 }
204
205 return true;
206 }
207 \f
208 /* Create a strtab to hold the dynamic symbol names. */
209 static bool
210 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
211 {
212 struct elf_link_hash_table *hash_table;
213
214 hash_table = elf_hash_table (info);
215 if (hash_table->dynobj == NULL)
216 {
217 /* We may not set dynobj, an input file holding linker created
218 dynamic sections to abfd, which may be a dynamic object with
219 its own dynamic sections. We need to find a normal input file
220 to hold linker created sections if possible. */
221 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
222 {
223 bfd *ibfd;
224 asection *s;
225 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
226 if ((ibfd->flags
227 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
228 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
229 && elf_object_id (ibfd) == elf_hash_table_id (hash_table)
230 && !((s = ibfd->sections) != NULL
231 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS))
232 {
233 abfd = ibfd;
234 break;
235 }
236 }
237 hash_table->dynobj = abfd;
238 }
239
240 if (hash_table->dynstr == NULL)
241 {
242 hash_table->dynstr = _bfd_elf_strtab_init ();
243 if (hash_table->dynstr == NULL)
244 return false;
245 }
246 return true;
247 }
248
249 /* Create some sections which will be filled in with dynamic linking
250 information. ABFD is an input file which requires dynamic sections
251 to be created. The dynamic sections take up virtual memory space
252 when the final executable is run, so we need to create them before
253 addresses are assigned to the output sections. We work out the
254 actual contents and size of these sections later. */
255
256 bool
257 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
258 {
259 flagword flags;
260 asection *s;
261 const struct elf_backend_data *bed;
262 struct elf_link_hash_entry *h;
263
264 if (! is_elf_hash_table (info->hash))
265 return false;
266
267 if (elf_hash_table (info)->dynamic_sections_created)
268 return true;
269
270 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
271 return false;
272
273 abfd = elf_hash_table (info)->dynobj;
274 bed = get_elf_backend_data (abfd);
275
276 flags = bed->dynamic_sec_flags;
277
278 /* A dynamically linked executable has a .interp section, but a
279 shared library does not. */
280 if (bfd_link_executable (info) && !info->nointerp)
281 {
282 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
283 flags | SEC_READONLY);
284 if (s == NULL)
285 return false;
286 }
287
288 /* Create sections to hold version informations. These are removed
289 if they are not needed. */
290 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
291 flags | SEC_READONLY);
292 if (s == NULL
293 || !bfd_set_section_alignment (s, bed->s->log_file_align))
294 return false;
295
296 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
297 flags | SEC_READONLY);
298 if (s == NULL
299 || !bfd_set_section_alignment (s, 1))
300 return false;
301
302 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
303 flags | SEC_READONLY);
304 if (s == NULL
305 || !bfd_set_section_alignment (s, bed->s->log_file_align))
306 return false;
307
308 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
309 flags | SEC_READONLY);
310 if (s == NULL
311 || !bfd_set_section_alignment (s, bed->s->log_file_align))
312 return false;
313 elf_hash_table (info)->dynsym = s;
314
315 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
316 flags | SEC_READONLY);
317 if (s == NULL)
318 return false;
319
320 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
321 if (s == NULL
322 || !bfd_set_section_alignment (s, bed->s->log_file_align))
323 return false;
324
325 /* The special symbol _DYNAMIC is always set to the start of the
326 .dynamic section. We could set _DYNAMIC in a linker script, but we
327 only want to define it if we are, in fact, creating a .dynamic
328 section. We don't want to define it if there is no .dynamic
329 section, since on some ELF platforms the start up code examines it
330 to decide how to initialize the process. */
331 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
332 elf_hash_table (info)->hdynamic = h;
333 if (h == NULL)
334 return false;
335
336 if (info->emit_hash)
337 {
338 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
339 flags | SEC_READONLY);
340 if (s == NULL
341 || !bfd_set_section_alignment (s, bed->s->log_file_align))
342 return false;
343 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
344 }
345
346 if (info->emit_gnu_hash && bed->record_xhash_symbol == NULL)
347 {
348 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
349 flags | SEC_READONLY);
350 if (s == NULL
351 || !bfd_set_section_alignment (s, bed->s->log_file_align))
352 return false;
353 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
354 4 32-bit words followed by variable count of 64-bit words, then
355 variable count of 32-bit words. */
356 if (bed->s->arch_size == 64)
357 elf_section_data (s)->this_hdr.sh_entsize = 0;
358 else
359 elf_section_data (s)->this_hdr.sh_entsize = 4;
360 }
361
362 if (info->enable_dt_relr)
363 {
364 s = bfd_make_section_anyway_with_flags (abfd, ".relr.dyn",
365 (bed->dynamic_sec_flags
366 | SEC_READONLY));
367 if (s == NULL
368 || !bfd_set_section_alignment (s, bed->s->log_file_align))
369 return false;
370 elf_hash_table (info)->srelrdyn = s;
371 }
372
373 /* Let the backend create the rest of the sections. This lets the
374 backend set the right flags. The backend will normally create
375 the .got and .plt sections. */
376 if (bed->elf_backend_create_dynamic_sections == NULL
377 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
378 return false;
379
380 elf_hash_table (info)->dynamic_sections_created = true;
381
382 return true;
383 }
384
385 /* Create dynamic sections when linking against a dynamic object. */
386
387 bool
388 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
389 {
390 flagword flags, pltflags;
391 struct elf_link_hash_entry *h;
392 asection *s;
393 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
394 struct elf_link_hash_table *htab = elf_hash_table (info);
395
396 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
397 .rel[a].bss sections. */
398 flags = bed->dynamic_sec_flags;
399
400 pltflags = flags;
401 if (bed->plt_not_loaded)
402 /* We do not clear SEC_ALLOC here because we still want the OS to
403 allocate space for the section; it's just that there's nothing
404 to read in from the object file. */
405 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
406 else
407 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
408 if (bed->plt_readonly)
409 pltflags |= SEC_READONLY;
410
411 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
412 if (s == NULL
413 || !bfd_set_section_alignment (s, bed->plt_alignment))
414 return false;
415 htab->splt = s;
416
417 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
418 .plt section. */
419 if (bed->want_plt_sym)
420 {
421 h = _bfd_elf_define_linkage_sym (abfd, info, s,
422 "_PROCEDURE_LINKAGE_TABLE_");
423 elf_hash_table (info)->hplt = h;
424 if (h == NULL)
425 return false;
426 }
427
428 s = bfd_make_section_anyway_with_flags (abfd,
429 (bed->rela_plts_and_copies_p
430 ? ".rela.plt" : ".rel.plt"),
431 flags | SEC_READONLY);
432 if (s == NULL
433 || !bfd_set_section_alignment (s, bed->s->log_file_align))
434 return false;
435 htab->srelplt = s;
436
437 if (! _bfd_elf_create_got_section (abfd, info))
438 return false;
439
440 if (bed->want_dynbss)
441 {
442 /* The .dynbss section is a place to put symbols which are defined
443 by dynamic objects, are referenced by regular objects, and are
444 not functions. We must allocate space for them in the process
445 image and use a R_*_COPY reloc to tell the dynamic linker to
446 initialize them at run time. The linker script puts the .dynbss
447 section into the .bss section of the final image. */
448 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
449 SEC_ALLOC | SEC_LINKER_CREATED);
450 if (s == NULL)
451 return false;
452 htab->sdynbss = s;
453
454 if (bed->want_dynrelro)
455 {
456 /* Similarly, but for symbols that were originally in read-only
457 sections. This section doesn't really need to have contents,
458 but make it like other .data.rel.ro sections. */
459 s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
460 flags);
461 if (s == NULL)
462 return false;
463 htab->sdynrelro = s;
464 }
465
466 /* The .rel[a].bss section holds copy relocs. This section is not
467 normally needed. We need to create it here, though, so that the
468 linker will map it to an output section. We can't just create it
469 only if we need it, because we will not know whether we need it
470 until we have seen all the input files, and the first time the
471 main linker code calls BFD after examining all the input files
472 (size_dynamic_sections) the input sections have already been
473 mapped to the output sections. If the section turns out not to
474 be needed, we can discard it later. We will never need this
475 section when generating a shared object, since they do not use
476 copy relocs. */
477 if (bfd_link_executable (info))
478 {
479 s = bfd_make_section_anyway_with_flags (abfd,
480 (bed->rela_plts_and_copies_p
481 ? ".rela.bss" : ".rel.bss"),
482 flags | SEC_READONLY);
483 if (s == NULL
484 || !bfd_set_section_alignment (s, bed->s->log_file_align))
485 return false;
486 htab->srelbss = s;
487
488 if (bed->want_dynrelro)
489 {
490 s = (bfd_make_section_anyway_with_flags
491 (abfd, (bed->rela_plts_and_copies_p
492 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
493 flags | SEC_READONLY));
494 if (s == NULL
495 || !bfd_set_section_alignment (s, bed->s->log_file_align))
496 return false;
497 htab->sreldynrelro = s;
498 }
499 }
500 }
501
502 return true;
503 }
504 \f
505 /* Record a new dynamic symbol. We record the dynamic symbols as we
506 read the input files, since we need to have a list of all of them
507 before we can determine the final sizes of the output sections.
508 Note that we may actually call this function even though we are not
509 going to output any dynamic symbols; in some cases we know that a
510 symbol should be in the dynamic symbol table, but only if there is
511 one. */
512
513 bool
514 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
515 struct elf_link_hash_entry *h)
516 {
517 if (h->dynindx == -1)
518 {
519 struct elf_strtab_hash *dynstr;
520 char *p;
521 const char *name;
522 size_t indx;
523
524 if (h->root.type == bfd_link_hash_defined
525 || h->root.type == bfd_link_hash_defweak)
526 {
527 /* An IR symbol should not be made dynamic. */
528 if (h->root.u.def.section != NULL
529 && h->root.u.def.section->owner != NULL
530 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)
531 return true;
532 }
533
534 /* XXX: The ABI draft says the linker must turn hidden and
535 internal symbols into STB_LOCAL symbols when producing the
536 DSO. However, if ld.so honors st_other in the dynamic table,
537 this would not be necessary. */
538 switch (ELF_ST_VISIBILITY (h->other))
539 {
540 case STV_INTERNAL:
541 case STV_HIDDEN:
542 if (h->root.type != bfd_link_hash_undefined
543 && h->root.type != bfd_link_hash_undefweak)
544 {
545 h->forced_local = 1;
546 if (!elf_hash_table (info)->is_relocatable_executable
547 || ((h->root.type == bfd_link_hash_defined
548 || h->root.type == bfd_link_hash_defweak)
549 && h->root.u.def.section->owner != NULL
550 && h->root.u.def.section->owner->no_export)
551 || (h->root.type == bfd_link_hash_common
552 && h->root.u.c.p->section->owner != NULL
553 && h->root.u.c.p->section->owner->no_export))
554 return true;
555 }
556
557 default:
558 break;
559 }
560
561 h->dynindx = elf_hash_table (info)->dynsymcount;
562 ++elf_hash_table (info)->dynsymcount;
563
564 dynstr = elf_hash_table (info)->dynstr;
565 if (dynstr == NULL)
566 {
567 /* Create a strtab to hold the dynamic symbol names. */
568 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
569 if (dynstr == NULL)
570 return false;
571 }
572
573 /* We don't put any version information in the dynamic string
574 table. */
575 name = h->root.root.string;
576 p = strchr (name, ELF_VER_CHR);
577 if (p != NULL)
578 /* We know that the p points into writable memory. In fact,
579 there are only a few symbols that have read-only names, being
580 those like _GLOBAL_OFFSET_TABLE_ that are created specially
581 by the backends. Most symbols will have names pointing into
582 an ELF string table read from a file, or to objalloc memory. */
583 *p = 0;
584
585 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
586
587 if (p != NULL)
588 *p = ELF_VER_CHR;
589
590 if (indx == (size_t) -1)
591 return false;
592 h->dynstr_index = indx;
593 }
594
595 return true;
596 }
597 \f
598 /* Mark a symbol dynamic. */
599
600 static void
601 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
602 struct elf_link_hash_entry *h,
603 Elf_Internal_Sym *sym)
604 {
605 struct bfd_elf_dynamic_list *d = info->dynamic_list;
606
607 /* It may be called more than once on the same H. */
608 if(h->dynamic || bfd_link_relocatable (info))
609 return;
610
611 if ((info->dynamic_data
612 && (h->type == STT_OBJECT
613 || h->type == STT_COMMON
614 || (sym != NULL
615 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
616 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
617 || (d != NULL
618 && h->non_elf
619 && (*d->match) (&d->head, NULL, h->root.root.string)))
620 {
621 h->dynamic = 1;
622 /* NB: If a symbol is made dynamic by --dynamic-list, it has
623 non-IR reference. */
624 h->root.non_ir_ref_dynamic = 1;
625 }
626 }
627
628 /* Record an assignment to a symbol made by a linker script. We need
629 this in case some dynamic object refers to this symbol. */
630
631 bool
632 bfd_elf_record_link_assignment (bfd *output_bfd,
633 struct bfd_link_info *info,
634 const char *name,
635 bool provide,
636 bool hidden)
637 {
638 struct elf_link_hash_entry *h, *hv;
639 struct elf_link_hash_table *htab;
640 const struct elf_backend_data *bed;
641
642 if (!is_elf_hash_table (info->hash))
643 return true;
644
645 htab = elf_hash_table (info);
646 h = elf_link_hash_lookup (htab, name, !provide, true, false);
647 if (h == NULL)
648 return provide;
649
650 if (h->root.type == bfd_link_hash_warning)
651 h = (struct elf_link_hash_entry *) h->root.u.i.link;
652
653 if (h->versioned == unknown)
654 {
655 /* Set versioned if symbol version is unknown. */
656 char *version = strrchr (name, ELF_VER_CHR);
657 if (version)
658 {
659 if (version > name && version[-1] != ELF_VER_CHR)
660 h->versioned = versioned_hidden;
661 else
662 h->versioned = versioned;
663 }
664 }
665
666 /* Symbols defined in a linker script but not referenced anywhere
667 else will have non_elf set. */
668 if (h->non_elf)
669 {
670 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
671 h->non_elf = 0;
672 }
673
674 switch (h->root.type)
675 {
676 case bfd_link_hash_defined:
677 case bfd_link_hash_defweak:
678 case bfd_link_hash_common:
679 break;
680 case bfd_link_hash_undefweak:
681 case bfd_link_hash_undefined:
682 /* Since we're defining the symbol, don't let it seem to have not
683 been defined. record_dynamic_symbol and size_dynamic_sections
684 may depend on this. */
685 h->root.type = bfd_link_hash_new;
686 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
687 bfd_link_repair_undef_list (&htab->root);
688 break;
689 case bfd_link_hash_new:
690 break;
691 case bfd_link_hash_indirect:
692 /* We had a versioned symbol in a dynamic library. We make the
693 the versioned symbol point to this one. */
694 bed = get_elf_backend_data (output_bfd);
695 hv = h;
696 while (hv->root.type == bfd_link_hash_indirect
697 || hv->root.type == bfd_link_hash_warning)
698 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
699 /* We don't need to update h->root.u since linker will set them
700 later. */
701 h->root.type = bfd_link_hash_undefined;
702 hv->root.type = bfd_link_hash_indirect;
703 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
704 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
705 break;
706 default:
707 BFD_FAIL ();
708 return false;
709 }
710
711 /* If this symbol is being provided by the linker script, and it is
712 currently defined by a dynamic object, but not by a regular
713 object, then mark it as undefined so that the generic linker will
714 force the correct value. */
715 if (provide
716 && h->def_dynamic
717 && !h->def_regular)
718 h->root.type = bfd_link_hash_undefined;
719
720 /* If this symbol is currently defined by a dynamic object, but not
721 by a regular object, then clear out any version information because
722 the symbol will not be associated with the dynamic object any
723 more. */
724 if (h->def_dynamic && !h->def_regular)
725 h->verinfo.verdef = NULL;
726
727 /* Make sure this symbol is not garbage collected. */
728 h->mark = 1;
729
730 h->def_regular = 1;
731
732 if (hidden)
733 {
734 bed = get_elf_backend_data (output_bfd);
735 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
736 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
737 (*bed->elf_backend_hide_symbol) (info, h, true);
738 }
739
740 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
741 and executables. */
742 if (!bfd_link_relocatable (info)
743 && h->dynindx != -1
744 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
745 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
746 h->forced_local = 1;
747
748 if ((h->def_dynamic
749 || h->ref_dynamic
750 || bfd_link_dll (info)
751 || elf_hash_table (info)->is_relocatable_executable)
752 && !h->forced_local
753 && h->dynindx == -1)
754 {
755 if (! bfd_elf_link_record_dynamic_symbol (info, h))
756 return false;
757
758 /* If this is a weak defined symbol, and we know a corresponding
759 real symbol from the same dynamic object, make sure the real
760 symbol is also made into a dynamic symbol. */
761 if (h->is_weakalias)
762 {
763 struct elf_link_hash_entry *def = weakdef (h);
764
765 if (def->dynindx == -1
766 && !bfd_elf_link_record_dynamic_symbol (info, def))
767 return false;
768 }
769 }
770
771 return true;
772 }
773
774 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
775 success, and 2 on a failure caused by attempting to record a symbol
776 in a discarded section, eg. a discarded link-once section symbol. */
777
778 int
779 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
780 bfd *input_bfd,
781 long input_indx)
782 {
783 size_t amt;
784 struct elf_link_local_dynamic_entry *entry;
785 struct elf_link_hash_table *eht;
786 struct elf_strtab_hash *dynstr;
787 size_t dynstr_index;
788 char *name;
789 Elf_External_Sym_Shndx eshndx;
790 char esym[sizeof (Elf64_External_Sym)];
791
792 if (! is_elf_hash_table (info->hash))
793 return 0;
794
795 /* See if the entry exists already. */
796 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
797 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
798 return 1;
799
800 amt = sizeof (*entry);
801 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
802 if (entry == NULL)
803 return 0;
804
805 /* Go find the symbol, so that we can find it's name. */
806 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
807 1, input_indx, &entry->isym, esym, &eshndx))
808 {
809 bfd_release (input_bfd, entry);
810 return 0;
811 }
812
813 if (entry->isym.st_shndx != SHN_UNDEF
814 && entry->isym.st_shndx < SHN_LORESERVE)
815 {
816 asection *s;
817
818 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
819 if (s == NULL || bfd_is_abs_section (s->output_section))
820 {
821 /* We can still bfd_release here as nothing has done another
822 bfd_alloc. We can't do this later in this function. */
823 bfd_release (input_bfd, entry);
824 return 2;
825 }
826 }
827
828 name = (bfd_elf_string_from_elf_section
829 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
830 entry->isym.st_name));
831
832 dynstr = elf_hash_table (info)->dynstr;
833 if (dynstr == NULL)
834 {
835 /* Create a strtab to hold the dynamic symbol names. */
836 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
837 if (dynstr == NULL)
838 return 0;
839 }
840
841 dynstr_index = _bfd_elf_strtab_add (dynstr, name, false);
842 if (dynstr_index == (size_t) -1)
843 return 0;
844 entry->isym.st_name = dynstr_index;
845
846 eht = elf_hash_table (info);
847
848 entry->next = eht->dynlocal;
849 eht->dynlocal = entry;
850 entry->input_bfd = input_bfd;
851 entry->input_indx = input_indx;
852 eht->dynsymcount++;
853
854 /* Whatever binding the symbol had before, it's now local. */
855 entry->isym.st_info
856 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
857
858 /* The dynindx will be set at the end of size_dynamic_sections. */
859
860 return 1;
861 }
862
863 /* Return the dynindex of a local dynamic symbol. */
864
865 long
866 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
867 bfd *input_bfd,
868 long input_indx)
869 {
870 struct elf_link_local_dynamic_entry *e;
871
872 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
873 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
874 return e->dynindx;
875 return -1;
876 }
877
878 /* This function is used to renumber the dynamic symbols, if some of
879 them are removed because they are marked as local. This is called
880 via elf_link_hash_traverse. */
881
882 static bool
883 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
884 void *data)
885 {
886 size_t *count = (size_t *) data;
887
888 if (h->forced_local)
889 return true;
890
891 if (h->dynindx != -1)
892 h->dynindx = ++(*count);
893
894 return true;
895 }
896
897
898 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
899 STB_LOCAL binding. */
900
901 static bool
902 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
903 void *data)
904 {
905 size_t *count = (size_t *) data;
906
907 if (!h->forced_local)
908 return true;
909
910 if (h->dynindx != -1)
911 h->dynindx = ++(*count);
912
913 return true;
914 }
915
916 /* Return true if the dynamic symbol for a given section should be
917 omitted when creating a shared library. */
918 bool
919 _bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED,
920 struct bfd_link_info *info,
921 asection *p)
922 {
923 struct elf_link_hash_table *htab;
924 asection *ip;
925
926 switch (elf_section_data (p)->this_hdr.sh_type)
927 {
928 case SHT_PROGBITS:
929 case SHT_NOBITS:
930 /* If sh_type is yet undecided, assume it could be
931 SHT_PROGBITS/SHT_NOBITS. */
932 case SHT_NULL:
933 htab = elf_hash_table (info);
934 if (htab->text_index_section != NULL)
935 return p != htab->text_index_section && p != htab->data_index_section;
936
937 return (htab->dynobj != NULL
938 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
939 && ip->output_section == p);
940
941 /* There shouldn't be section relative relocations
942 against any other section. */
943 default:
944 return true;
945 }
946 }
947
948 bool
949 _bfd_elf_omit_section_dynsym_all
950 (bfd *output_bfd ATTRIBUTE_UNUSED,
951 struct bfd_link_info *info ATTRIBUTE_UNUSED,
952 asection *p ATTRIBUTE_UNUSED)
953 {
954 return true;
955 }
956
957 /* Assign dynsym indices. In a shared library we generate a section
958 symbol for each output section, which come first. Next come symbols
959 which have been forced to local binding. Then all of the back-end
960 allocated local dynamic syms, followed by the rest of the global
961 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
962 (This prevents the early call before elf_backend_init_index_section
963 and strip_excluded_output_sections setting dynindx for sections
964 that are stripped.) */
965
966 static unsigned long
967 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
968 struct bfd_link_info *info,
969 unsigned long *section_sym_count)
970 {
971 unsigned long dynsymcount = 0;
972 bool do_sec = section_sym_count != NULL;
973
974 if (bfd_link_pic (info)
975 || elf_hash_table (info)->is_relocatable_executable)
976 {
977 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
978 asection *p;
979 for (p = output_bfd->sections; p ; p = p->next)
980 if ((p->flags & SEC_EXCLUDE) == 0
981 && (p->flags & SEC_ALLOC) != 0
982 && elf_hash_table (info)->dynamic_relocs
983 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
984 {
985 ++dynsymcount;
986 if (do_sec)
987 elf_section_data (p)->dynindx = dynsymcount;
988 }
989 else if (do_sec)
990 elf_section_data (p)->dynindx = 0;
991 }
992 if (do_sec)
993 *section_sym_count = dynsymcount;
994
995 elf_link_hash_traverse (elf_hash_table (info),
996 elf_link_renumber_local_hash_table_dynsyms,
997 &dynsymcount);
998
999 if (elf_hash_table (info)->dynlocal)
1000 {
1001 struct elf_link_local_dynamic_entry *p;
1002 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
1003 p->dynindx = ++dynsymcount;
1004 }
1005 elf_hash_table (info)->local_dynsymcount = dynsymcount;
1006
1007 elf_link_hash_traverse (elf_hash_table (info),
1008 elf_link_renumber_hash_table_dynsyms,
1009 &dynsymcount);
1010
1011 /* There is an unused NULL entry at the head of the table which we
1012 must account for in our count even if the table is empty since it
1013 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
1014 .dynamic section. */
1015 dynsymcount++;
1016
1017 elf_hash_table (info)->dynsymcount = dynsymcount;
1018 return dynsymcount;
1019 }
1020
1021 /* Merge st_other field. */
1022
1023 static void
1024 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
1025 unsigned int st_other, asection *sec,
1026 bool definition, bool dynamic)
1027 {
1028 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1029
1030 /* If st_other has a processor-specific meaning, specific
1031 code might be needed here. */
1032 if (bed->elf_backend_merge_symbol_attribute)
1033 (*bed->elf_backend_merge_symbol_attribute) (h, st_other, definition,
1034 dynamic);
1035
1036 if (!dynamic)
1037 {
1038 unsigned symvis = ELF_ST_VISIBILITY (st_other);
1039 unsigned hvis = ELF_ST_VISIBILITY (h->other);
1040
1041 /* Keep the most constraining visibility. Leave the remainder
1042 of the st_other field to elf_backend_merge_symbol_attribute. */
1043 if (symvis - 1 < hvis - 1)
1044 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
1045 }
1046 else if (definition
1047 && ELF_ST_VISIBILITY (st_other) != STV_DEFAULT
1048 && (sec->flags & SEC_READONLY) == 0)
1049 h->protected_def = 1;
1050 }
1051
1052 /* This function is called when we want to merge a new symbol with an
1053 existing symbol. It handles the various cases which arise when we
1054 find a definition in a dynamic object, or when there is already a
1055 definition in a dynamic object. The new symbol is described by
1056 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1057 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1058 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1059 of an old common symbol. We set OVERRIDE if the old symbol is
1060 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1061 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1062 to change. By OK to change, we mean that we shouldn't warn if the
1063 type or size does change. */
1064
1065 static bool
1066 _bfd_elf_merge_symbol (bfd *abfd,
1067 struct bfd_link_info *info,
1068 const char *name,
1069 Elf_Internal_Sym *sym,
1070 asection **psec,
1071 bfd_vma *pvalue,
1072 struct elf_link_hash_entry **sym_hash,
1073 bfd **poldbfd,
1074 bool *pold_weak,
1075 unsigned int *pold_alignment,
1076 bool *skip,
1077 bfd **override,
1078 bool *type_change_ok,
1079 bool *size_change_ok,
1080 bool *matched)
1081 {
1082 asection *sec, *oldsec;
1083 struct elf_link_hash_entry *h;
1084 struct elf_link_hash_entry *hi;
1085 struct elf_link_hash_entry *flip;
1086 int bind;
1087 bfd *oldbfd;
1088 bool newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
1089 bool newweak, oldweak, newfunc, oldfunc;
1090 const struct elf_backend_data *bed;
1091 char *new_version;
1092 bool default_sym = *matched;
1093
1094 *skip = false;
1095 *override = NULL;
1096
1097 sec = *psec;
1098 bind = ELF_ST_BIND (sym->st_info);
1099
1100 if (! bfd_is_und_section (sec))
1101 h = elf_link_hash_lookup (elf_hash_table (info), name, true, false, false);
1102 else
1103 h = ((struct elf_link_hash_entry *)
1104 bfd_wrapped_link_hash_lookup (abfd, info, name, true, false, false));
1105 if (h == NULL)
1106 return false;
1107 *sym_hash = h;
1108
1109 bed = get_elf_backend_data (abfd);
1110
1111 /* NEW_VERSION is the symbol version of the new symbol. */
1112 if (h->versioned != unversioned)
1113 {
1114 /* Symbol version is unknown or versioned. */
1115 new_version = strrchr (name, ELF_VER_CHR);
1116 if (new_version)
1117 {
1118 if (h->versioned == unknown)
1119 {
1120 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1121 h->versioned = versioned_hidden;
1122 else
1123 h->versioned = versioned;
1124 }
1125 new_version += 1;
1126 if (new_version[0] == '\0')
1127 new_version = NULL;
1128 }
1129 else
1130 h->versioned = unversioned;
1131 }
1132 else
1133 new_version = NULL;
1134
1135 /* For merging, we only care about real symbols. But we need to make
1136 sure that indirect symbol dynamic flags are updated. */
1137 hi = h;
1138 while (h->root.type == bfd_link_hash_indirect
1139 || h->root.type == bfd_link_hash_warning)
1140 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1141
1142 if (!*matched)
1143 {
1144 if (hi == h || h->root.type == bfd_link_hash_new)
1145 *matched = true;
1146 else
1147 {
1148 /* OLD_HIDDEN is true if the existing symbol is only visible
1149 to the symbol with the same symbol version. NEW_HIDDEN is
1150 true if the new symbol is only visible to the symbol with
1151 the same symbol version. */
1152 bool old_hidden = h->versioned == versioned_hidden;
1153 bool new_hidden = hi->versioned == versioned_hidden;
1154 if (!old_hidden && !new_hidden)
1155 /* The new symbol matches the existing symbol if both
1156 aren't hidden. */
1157 *matched = true;
1158 else
1159 {
1160 /* OLD_VERSION is the symbol version of the existing
1161 symbol. */
1162 char *old_version;
1163
1164 if (h->versioned >= versioned)
1165 old_version = strrchr (h->root.root.string,
1166 ELF_VER_CHR) + 1;
1167 else
1168 old_version = NULL;
1169
1170 /* The new symbol matches the existing symbol if they
1171 have the same symbol version. */
1172 *matched = (old_version == new_version
1173 || (old_version != NULL
1174 && new_version != NULL
1175 && strcmp (old_version, new_version) == 0));
1176 }
1177 }
1178 }
1179
1180 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1181 existing symbol. */
1182
1183 oldbfd = NULL;
1184 oldsec = NULL;
1185 switch (h->root.type)
1186 {
1187 default:
1188 break;
1189
1190 case bfd_link_hash_undefined:
1191 case bfd_link_hash_undefweak:
1192 oldbfd = h->root.u.undef.abfd;
1193 break;
1194
1195 case bfd_link_hash_defined:
1196 case bfd_link_hash_defweak:
1197 oldbfd = h->root.u.def.section->owner;
1198 oldsec = h->root.u.def.section;
1199 break;
1200
1201 case bfd_link_hash_common:
1202 oldbfd = h->root.u.c.p->section->owner;
1203 oldsec = h->root.u.c.p->section;
1204 if (pold_alignment)
1205 *pold_alignment = h->root.u.c.p->alignment_power;
1206 break;
1207 }
1208 if (poldbfd && *poldbfd == NULL)
1209 *poldbfd = oldbfd;
1210
1211 /* Differentiate strong and weak symbols. */
1212 newweak = bind == STB_WEAK;
1213 oldweak = (h->root.type == bfd_link_hash_defweak
1214 || h->root.type == bfd_link_hash_undefweak);
1215 if (pold_weak)
1216 *pold_weak = oldweak;
1217
1218 /* We have to check it for every instance since the first few may be
1219 references and not all compilers emit symbol type for undefined
1220 symbols. */
1221 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1222
1223 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1224 respectively, is from a dynamic object. */
1225
1226 newdyn = (abfd->flags & DYNAMIC) != 0;
1227
1228 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1229 syms and defined syms in dynamic libraries respectively.
1230 ref_dynamic on the other hand can be set for a symbol defined in
1231 a dynamic library, and def_dynamic may not be set; When the
1232 definition in a dynamic lib is overridden by a definition in the
1233 executable use of the symbol in the dynamic lib becomes a
1234 reference to the executable symbol. */
1235 if (newdyn)
1236 {
1237 if (bfd_is_und_section (sec))
1238 {
1239 if (bind != STB_WEAK)
1240 {
1241 h->ref_dynamic_nonweak = 1;
1242 hi->ref_dynamic_nonweak = 1;
1243 }
1244 }
1245 else
1246 {
1247 /* Update the existing symbol only if they match. */
1248 if (*matched)
1249 h->dynamic_def = 1;
1250 hi->dynamic_def = 1;
1251 }
1252 }
1253
1254 /* If we just created the symbol, mark it as being an ELF symbol.
1255 Other than that, there is nothing to do--there is no merge issue
1256 with a newly defined symbol--so we just return. */
1257
1258 if (h->root.type == bfd_link_hash_new)
1259 {
1260 h->non_elf = 0;
1261 return true;
1262 }
1263
1264 /* In cases involving weak versioned symbols, we may wind up trying
1265 to merge a symbol with itself. Catch that here, to avoid the
1266 confusion that results if we try to override a symbol with
1267 itself. The additional tests catch cases like
1268 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1269 dynamic object, which we do want to handle here. */
1270 if (abfd == oldbfd
1271 && (newweak || oldweak)
1272 && ((abfd->flags & DYNAMIC) == 0
1273 || !h->def_regular))
1274 return true;
1275
1276 olddyn = false;
1277 if (oldbfd != NULL)
1278 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1279 else if (oldsec != NULL)
1280 {
1281 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1282 indices used by MIPS ELF. */
1283 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1284 }
1285
1286 if (oldbfd != NULL
1287 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN))
1288 {
1289 if (newdyn != olddyn)
1290 {
1291 /* Handle a case where plugin_notice won't be called and thus
1292 won't set the non_ir_ref flags on the first pass over
1293 symbols. */
1294 h->root.non_ir_ref_dynamic = true;
1295 hi->root.non_ir_ref_dynamic = true;
1296 }
1297
1298 if ((oldbfd->flags & BFD_PLUGIN) != 0
1299 && hi->root.type == bfd_link_hash_indirect)
1300 {
1301 /* Change indirect symbol from IR to undefined. */
1302 hi->root.type = bfd_link_hash_undefined;
1303 hi->root.u.undef.abfd = oldbfd;
1304 }
1305 }
1306
1307 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1308 respectively, appear to be a definition rather than reference. */
1309
1310 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1311
1312 olddef = (h->root.type != bfd_link_hash_undefined
1313 && h->root.type != bfd_link_hash_undefweak
1314 && h->root.type != bfd_link_hash_common);
1315
1316 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1317 respectively, appear to be a function. */
1318
1319 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1320 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1321
1322 oldfunc = (h->type != STT_NOTYPE
1323 && bed->is_function_type (h->type));
1324
1325 if (!(newfunc && oldfunc)
1326 && ELF_ST_TYPE (sym->st_info) != h->type
1327 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1328 && h->type != STT_NOTYPE
1329 && (newdef || bfd_is_com_section (sec))
1330 && (olddef || h->root.type == bfd_link_hash_common))
1331 {
1332 /* If creating a default indirect symbol ("foo" or "foo@") from
1333 a dynamic versioned definition ("foo@@") skip doing so if
1334 there is an existing regular definition with a different
1335 type. We don't want, for example, a "time" variable in the
1336 executable overriding a "time" function in a shared library. */
1337 if (newdyn
1338 && !olddyn)
1339 {
1340 *skip = true;
1341 return true;
1342 }
1343
1344 /* When adding a symbol from a regular object file after we have
1345 created indirect symbols, undo the indirection and any
1346 dynamic state. */
1347 if (hi != h
1348 && !newdyn
1349 && olddyn)
1350 {
1351 h = hi;
1352 (*bed->elf_backend_hide_symbol) (info, h, true);
1353 h->forced_local = 0;
1354 h->ref_dynamic = 0;
1355 h->def_dynamic = 0;
1356 h->dynamic_def = 0;
1357 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1358 {
1359 h->root.type = bfd_link_hash_undefined;
1360 h->root.u.undef.abfd = abfd;
1361 }
1362 else
1363 {
1364 h->root.type = bfd_link_hash_new;
1365 h->root.u.undef.abfd = NULL;
1366 }
1367 return true;
1368 }
1369 }
1370
1371 /* Check TLS symbols. We don't check undefined symbols introduced
1372 by "ld -u" which have no type (and oldbfd NULL), and we don't
1373 check symbols from plugins because they also have no type. */
1374 if (oldbfd != NULL
1375 && (oldbfd->flags & BFD_PLUGIN) == 0
1376 && (abfd->flags & BFD_PLUGIN) == 0
1377 && ELF_ST_TYPE (sym->st_info) != h->type
1378 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1379 {
1380 bfd *ntbfd, *tbfd;
1381 bool ntdef, tdef;
1382 asection *ntsec, *tsec;
1383
1384 if (h->type == STT_TLS)
1385 {
1386 ntbfd = abfd;
1387 ntsec = sec;
1388 ntdef = newdef;
1389 tbfd = oldbfd;
1390 tsec = oldsec;
1391 tdef = olddef;
1392 }
1393 else
1394 {
1395 ntbfd = oldbfd;
1396 ntsec = oldsec;
1397 ntdef = olddef;
1398 tbfd = abfd;
1399 tsec = sec;
1400 tdef = newdef;
1401 }
1402
1403 if (tdef && ntdef)
1404 _bfd_error_handler
1405 /* xgettext:c-format */
1406 (_("%s: TLS definition in %pB section %pA "
1407 "mismatches non-TLS definition in %pB section %pA"),
1408 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
1409 else if (!tdef && !ntdef)
1410 _bfd_error_handler
1411 /* xgettext:c-format */
1412 (_("%s: TLS reference in %pB "
1413 "mismatches non-TLS reference in %pB"),
1414 h->root.root.string, tbfd, ntbfd);
1415 else if (tdef)
1416 _bfd_error_handler
1417 /* xgettext:c-format */
1418 (_("%s: TLS definition in %pB section %pA "
1419 "mismatches non-TLS reference in %pB"),
1420 h->root.root.string, tbfd, tsec, ntbfd);
1421 else
1422 _bfd_error_handler
1423 /* xgettext:c-format */
1424 (_("%s: TLS reference in %pB "
1425 "mismatches non-TLS definition in %pB section %pA"),
1426 h->root.root.string, tbfd, ntbfd, ntsec);
1427
1428 bfd_set_error (bfd_error_bad_value);
1429 return false;
1430 }
1431
1432 /* If the old symbol has non-default visibility, we ignore the new
1433 definition from a dynamic object. */
1434 if (newdyn
1435 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1436 && !bfd_is_und_section (sec))
1437 {
1438 *skip = true;
1439 /* Make sure this symbol is dynamic. */
1440 h->ref_dynamic = 1;
1441 hi->ref_dynamic = 1;
1442 /* A protected symbol has external availability. Make sure it is
1443 recorded as dynamic.
1444
1445 FIXME: Should we check type and size for protected symbol? */
1446 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1447 return bfd_elf_link_record_dynamic_symbol (info, h);
1448 else
1449 return true;
1450 }
1451 else if (!newdyn
1452 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1453 && h->def_dynamic)
1454 {
1455 /* If the new symbol with non-default visibility comes from a
1456 relocatable file and the old definition comes from a dynamic
1457 object, we remove the old definition. */
1458 if (hi->root.type == bfd_link_hash_indirect)
1459 {
1460 /* Handle the case where the old dynamic definition is
1461 default versioned. We need to copy the symbol info from
1462 the symbol with default version to the normal one if it
1463 was referenced before. */
1464 if (h->ref_regular)
1465 {
1466 hi->root.type = h->root.type;
1467 h->root.type = bfd_link_hash_indirect;
1468 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1469
1470 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1471 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1472 {
1473 /* If the new symbol is hidden or internal, completely undo
1474 any dynamic link state. */
1475 (*bed->elf_backend_hide_symbol) (info, h, true);
1476 h->forced_local = 0;
1477 h->ref_dynamic = 0;
1478 }
1479 else
1480 h->ref_dynamic = 1;
1481
1482 h->def_dynamic = 0;
1483 /* FIXME: Should we check type and size for protected symbol? */
1484 h->size = 0;
1485 h->type = 0;
1486
1487 h = hi;
1488 }
1489 else
1490 h = hi;
1491 }
1492
1493 /* If the old symbol was undefined before, then it will still be
1494 on the undefs list. If the new symbol is undefined or
1495 common, we can't make it bfd_link_hash_new here, because new
1496 undefined or common symbols will be added to the undefs list
1497 by _bfd_generic_link_add_one_symbol. Symbols may not be
1498 added twice to the undefs list. Also, if the new symbol is
1499 undefweak then we don't want to lose the strong undef. */
1500 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1501 {
1502 h->root.type = bfd_link_hash_undefined;
1503 h->root.u.undef.abfd = abfd;
1504 }
1505 else
1506 {
1507 h->root.type = bfd_link_hash_new;
1508 h->root.u.undef.abfd = NULL;
1509 }
1510
1511 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1512 {
1513 /* If the new symbol is hidden or internal, completely undo
1514 any dynamic link state. */
1515 (*bed->elf_backend_hide_symbol) (info, h, true);
1516 h->forced_local = 0;
1517 h->ref_dynamic = 0;
1518 }
1519 else
1520 h->ref_dynamic = 1;
1521 h->def_dynamic = 0;
1522 /* FIXME: Should we check type and size for protected symbol? */
1523 h->size = 0;
1524 h->type = 0;
1525 return true;
1526 }
1527
1528 /* If a new weak symbol definition comes from a regular file and the
1529 old symbol comes from a dynamic library, we treat the new one as
1530 strong. Similarly, an old weak symbol definition from a regular
1531 file is treated as strong when the new symbol comes from a dynamic
1532 library. Further, an old weak symbol from a dynamic library is
1533 treated as strong if the new symbol is from a dynamic library.
1534 This reflects the way glibc's ld.so works.
1535
1536 Also allow a weak symbol to override a linker script symbol
1537 defined by an early pass over the script. This is done so the
1538 linker knows the symbol is defined in an object file, for the
1539 DEFINED script function.
1540
1541 Do this before setting *type_change_ok or *size_change_ok so that
1542 we warn properly when dynamic library symbols are overridden. */
1543
1544 if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
1545 newweak = false;
1546 if (olddef && newdyn)
1547 oldweak = false;
1548
1549 /* Allow changes between different types of function symbol. */
1550 if (newfunc && oldfunc)
1551 *type_change_ok = true;
1552
1553 /* It's OK to change the type if either the existing symbol or the
1554 new symbol is weak. A type change is also OK if the old symbol
1555 is undefined and the new symbol is defined. */
1556
1557 if (oldweak
1558 || newweak
1559 || (newdef
1560 && h->root.type == bfd_link_hash_undefined))
1561 *type_change_ok = true;
1562
1563 /* It's OK to change the size if either the existing symbol or the
1564 new symbol is weak, or if the old symbol is undefined. */
1565
1566 if (*type_change_ok
1567 || h->root.type == bfd_link_hash_undefined)
1568 *size_change_ok = true;
1569
1570 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1571 symbol, respectively, appears to be a common symbol in a dynamic
1572 object. If a symbol appears in an uninitialized section, and is
1573 not weak, and is not a function, then it may be a common symbol
1574 which was resolved when the dynamic object was created. We want
1575 to treat such symbols specially, because they raise special
1576 considerations when setting the symbol size: if the symbol
1577 appears as a common symbol in a regular object, and the size in
1578 the regular object is larger, we must make sure that we use the
1579 larger size. This problematic case can always be avoided in C,
1580 but it must be handled correctly when using Fortran shared
1581 libraries.
1582
1583 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1584 likewise for OLDDYNCOMMON and OLDDEF.
1585
1586 Note that this test is just a heuristic, and that it is quite
1587 possible to have an uninitialized symbol in a shared object which
1588 is really a definition, rather than a common symbol. This could
1589 lead to some minor confusion when the symbol really is a common
1590 symbol in some regular object. However, I think it will be
1591 harmless. */
1592
1593 if (newdyn
1594 && newdef
1595 && !newweak
1596 && (sec->flags & SEC_ALLOC) != 0
1597 && (sec->flags & SEC_LOAD) == 0
1598 && sym->st_size > 0
1599 && !newfunc)
1600 newdyncommon = true;
1601 else
1602 newdyncommon = false;
1603
1604 if (olddyn
1605 && olddef
1606 && h->root.type == bfd_link_hash_defined
1607 && h->def_dynamic
1608 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1609 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1610 && h->size > 0
1611 && !oldfunc)
1612 olddyncommon = true;
1613 else
1614 olddyncommon = false;
1615
1616 /* We now know everything about the old and new symbols. We ask the
1617 backend to check if we can merge them. */
1618 if (bed->merge_symbol != NULL)
1619 {
1620 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1621 return false;
1622 sec = *psec;
1623 }
1624
1625 /* There are multiple definitions of a normal symbol. Skip the
1626 default symbol as well as definition from an IR object. */
1627 if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
1628 && !default_sym && h->def_regular
1629 && !(oldbfd != NULL
1630 && (oldbfd->flags & BFD_PLUGIN) != 0
1631 && (abfd->flags & BFD_PLUGIN) == 0))
1632 {
1633 /* Handle a multiple definition. */
1634 (*info->callbacks->multiple_definition) (info, &h->root,
1635 abfd, sec, *pvalue);
1636 *skip = true;
1637 return true;
1638 }
1639
1640 /* If both the old and the new symbols look like common symbols in a
1641 dynamic object, set the size of the symbol to the larger of the
1642 two. */
1643
1644 if (olddyncommon
1645 && newdyncommon
1646 && sym->st_size != h->size)
1647 {
1648 /* Since we think we have two common symbols, issue a multiple
1649 common warning if desired. Note that we only warn if the
1650 size is different. If the size is the same, we simply let
1651 the old symbol override the new one as normally happens with
1652 symbols defined in dynamic objects. */
1653
1654 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1655 bfd_link_hash_common, sym->st_size);
1656 if (sym->st_size > h->size)
1657 h->size = sym->st_size;
1658
1659 *size_change_ok = true;
1660 }
1661
1662 /* If we are looking at a dynamic object, and we have found a
1663 definition, we need to see if the symbol was already defined by
1664 some other object. If so, we want to use the existing
1665 definition, and we do not want to report a multiple symbol
1666 definition error; we do this by clobbering *PSEC to be
1667 bfd_und_section_ptr.
1668
1669 We treat a common symbol as a definition if the symbol in the
1670 shared library is a function, since common symbols always
1671 represent variables; this can cause confusion in principle, but
1672 any such confusion would seem to indicate an erroneous program or
1673 shared library. We also permit a common symbol in a regular
1674 object to override a weak symbol in a shared object. */
1675
1676 if (newdyn
1677 && newdef
1678 && (olddef
1679 || (h->root.type == bfd_link_hash_common
1680 && (newweak || newfunc))))
1681 {
1682 *override = abfd;
1683 newdef = false;
1684 newdyncommon = false;
1685
1686 *psec = sec = bfd_und_section_ptr;
1687 *size_change_ok = true;
1688
1689 /* If we get here when the old symbol is a common symbol, then
1690 we are explicitly letting it override a weak symbol or
1691 function in a dynamic object, and we don't want to warn about
1692 a type change. If the old symbol is a defined symbol, a type
1693 change warning may still be appropriate. */
1694
1695 if (h->root.type == bfd_link_hash_common)
1696 *type_change_ok = true;
1697 }
1698
1699 /* Handle the special case of an old common symbol merging with a
1700 new symbol which looks like a common symbol in a shared object.
1701 We change *PSEC and *PVALUE to make the new symbol look like a
1702 common symbol, and let _bfd_generic_link_add_one_symbol do the
1703 right thing. */
1704
1705 if (newdyncommon
1706 && h->root.type == bfd_link_hash_common)
1707 {
1708 *override = oldbfd;
1709 newdef = false;
1710 newdyncommon = false;
1711 *pvalue = sym->st_size;
1712 *psec = sec = bed->common_section (oldsec);
1713 *size_change_ok = true;
1714 }
1715
1716 /* Skip weak definitions of symbols that are already defined. */
1717 if (newdef && olddef && newweak)
1718 {
1719 /* Don't skip new non-IR weak syms. */
1720 if (!(oldbfd != NULL
1721 && (oldbfd->flags & BFD_PLUGIN) != 0
1722 && (abfd->flags & BFD_PLUGIN) == 0))
1723 {
1724 newdef = false;
1725 *skip = true;
1726 }
1727
1728 /* Merge st_other. If the symbol already has a dynamic index,
1729 but visibility says it should not be visible, turn it into a
1730 local symbol. */
1731 elf_merge_st_other (abfd, h, sym->st_other, sec, newdef, newdyn);
1732 if (h->dynindx != -1)
1733 switch (ELF_ST_VISIBILITY (h->other))
1734 {
1735 case STV_INTERNAL:
1736 case STV_HIDDEN:
1737 (*bed->elf_backend_hide_symbol) (info, h, true);
1738 break;
1739 }
1740 }
1741
1742 /* If the old symbol is from a dynamic object, and the new symbol is
1743 a definition which is not from a dynamic object, then the new
1744 symbol overrides the old symbol. Symbols from regular files
1745 always take precedence over symbols from dynamic objects, even if
1746 they are defined after the dynamic object in the link.
1747
1748 As above, we again permit a common symbol in a regular object to
1749 override a definition in a shared object if the shared object
1750 symbol is a function or is weak. */
1751
1752 flip = NULL;
1753 if (!newdyn
1754 && (newdef
1755 || (bfd_is_com_section (sec)
1756 && (oldweak || oldfunc)))
1757 && olddyn
1758 && olddef
1759 && h->def_dynamic)
1760 {
1761 /* Change the hash table entry to undefined, and let
1762 _bfd_generic_link_add_one_symbol do the right thing with the
1763 new definition. */
1764
1765 h->root.type = bfd_link_hash_undefined;
1766 h->root.u.undef.abfd = h->root.u.def.section->owner;
1767 *size_change_ok = true;
1768
1769 olddef = false;
1770 olddyncommon = false;
1771
1772 /* We again permit a type change when a common symbol may be
1773 overriding a function. */
1774
1775 if (bfd_is_com_section (sec))
1776 {
1777 if (oldfunc)
1778 {
1779 /* If a common symbol overrides a function, make sure
1780 that it isn't defined dynamically nor has type
1781 function. */
1782 h->def_dynamic = 0;
1783 h->type = STT_NOTYPE;
1784 }
1785 *type_change_ok = true;
1786 }
1787
1788 if (hi->root.type == bfd_link_hash_indirect)
1789 flip = hi;
1790 else
1791 /* This union may have been set to be non-NULL when this symbol
1792 was seen in a dynamic object. We must force the union to be
1793 NULL, so that it is correct for a regular symbol. */
1794 h->verinfo.vertree = NULL;
1795 }
1796
1797 /* Handle the special case of a new common symbol merging with an
1798 old symbol that looks like it might be a common symbol defined in
1799 a shared object. Note that we have already handled the case in
1800 which a new common symbol should simply override the definition
1801 in the shared library. */
1802
1803 if (! newdyn
1804 && bfd_is_com_section (sec)
1805 && olddyncommon)
1806 {
1807 /* It would be best if we could set the hash table entry to a
1808 common symbol, but we don't know what to use for the section
1809 or the alignment. */
1810 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1811 bfd_link_hash_common, sym->st_size);
1812
1813 /* If the presumed common symbol in the dynamic object is
1814 larger, pretend that the new symbol has its size. */
1815
1816 if (h->size > *pvalue)
1817 *pvalue = h->size;
1818
1819 /* We need to remember the alignment required by the symbol
1820 in the dynamic object. */
1821 BFD_ASSERT (pold_alignment);
1822 *pold_alignment = h->root.u.def.section->alignment_power;
1823
1824 olddef = false;
1825 olddyncommon = false;
1826
1827 h->root.type = bfd_link_hash_undefined;
1828 h->root.u.undef.abfd = h->root.u.def.section->owner;
1829
1830 *size_change_ok = true;
1831 *type_change_ok = true;
1832
1833 if (hi->root.type == bfd_link_hash_indirect)
1834 flip = hi;
1835 else
1836 h->verinfo.vertree = NULL;
1837 }
1838
1839 if (flip != NULL)
1840 {
1841 /* Handle the case where we had a versioned symbol in a dynamic
1842 library and now find a definition in a normal object. In this
1843 case, we make the versioned symbol point to the normal one. */
1844 flip->root.type = h->root.type;
1845 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1846 h->root.type = bfd_link_hash_indirect;
1847 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1848 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1849 if (h->def_dynamic)
1850 {
1851 h->def_dynamic = 0;
1852 flip->ref_dynamic = 1;
1853 }
1854 }
1855
1856 return true;
1857 }
1858
1859 /* This function is called to create an indirect symbol from the
1860 default for the symbol with the default version if needed. The
1861 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1862 set DYNSYM if the new indirect symbol is dynamic. */
1863
1864 static bool
1865 _bfd_elf_add_default_symbol (bfd *abfd,
1866 struct bfd_link_info *info,
1867 struct elf_link_hash_entry *h,
1868 const char *name,
1869 Elf_Internal_Sym *sym,
1870 asection *sec,
1871 bfd_vma value,
1872 bfd **poldbfd,
1873 bool *dynsym)
1874 {
1875 bool type_change_ok;
1876 bool size_change_ok;
1877 bool skip;
1878 char *shortname;
1879 struct elf_link_hash_entry *hi;
1880 struct bfd_link_hash_entry *bh;
1881 const struct elf_backend_data *bed;
1882 bool collect;
1883 bool dynamic;
1884 bfd *override;
1885 char *p;
1886 size_t len, shortlen;
1887 asection *tmp_sec;
1888 bool matched;
1889
1890 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1891 return true;
1892
1893 /* If this symbol has a version, and it is the default version, we
1894 create an indirect symbol from the default name to the fully
1895 decorated name. This will cause external references which do not
1896 specify a version to be bound to this version of the symbol. */
1897 p = strchr (name, ELF_VER_CHR);
1898 if (h->versioned == unknown)
1899 {
1900 if (p == NULL)
1901 {
1902 h->versioned = unversioned;
1903 return true;
1904 }
1905 else
1906 {
1907 if (p[1] != ELF_VER_CHR)
1908 {
1909 h->versioned = versioned_hidden;
1910 return true;
1911 }
1912 else
1913 h->versioned = versioned;
1914 }
1915 }
1916 else
1917 {
1918 /* PR ld/19073: We may see an unversioned definition after the
1919 default version. */
1920 if (p == NULL)
1921 return true;
1922 }
1923
1924 bed = get_elf_backend_data (abfd);
1925 collect = bed->collect;
1926 dynamic = (abfd->flags & DYNAMIC) != 0;
1927
1928 shortlen = p - name;
1929 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1930 if (shortname == NULL)
1931 return false;
1932 memcpy (shortname, name, shortlen);
1933 shortname[shortlen] = '\0';
1934
1935 /* We are going to create a new symbol. Merge it with any existing
1936 symbol with this name. For the purposes of the merge, act as
1937 though we were defining the symbol we just defined, although we
1938 actually going to define an indirect symbol. */
1939 type_change_ok = false;
1940 size_change_ok = false;
1941 matched = true;
1942 tmp_sec = sec;
1943 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1944 &hi, poldbfd, NULL, NULL, &skip, &override,
1945 &type_change_ok, &size_change_ok, &matched))
1946 return false;
1947
1948 if (skip)
1949 goto nondefault;
1950
1951 if (hi->def_regular || ELF_COMMON_DEF_P (hi))
1952 {
1953 /* If the undecorated symbol will have a version added by a
1954 script different to H, then don't indirect to/from the
1955 undecorated symbol. This isn't ideal because we may not yet
1956 have seen symbol versions, if given by a script on the
1957 command line rather than via --version-script. */
1958 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1959 {
1960 bool hide;
1961
1962 hi->verinfo.vertree
1963 = bfd_find_version_for_sym (info->version_info,
1964 hi->root.root.string, &hide);
1965 if (hi->verinfo.vertree != NULL && hide)
1966 {
1967 (*bed->elf_backend_hide_symbol) (info, hi, true);
1968 goto nondefault;
1969 }
1970 }
1971 if (hi->verinfo.vertree != NULL
1972 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1973 goto nondefault;
1974 }
1975
1976 if (! override)
1977 {
1978 /* Add the default symbol if not performing a relocatable link. */
1979 if (! bfd_link_relocatable (info))
1980 {
1981 bh = &hi->root;
1982 if (bh->type == bfd_link_hash_defined
1983 && bh->u.def.section->owner != NULL
1984 && (bh->u.def.section->owner->flags & BFD_PLUGIN) != 0)
1985 {
1986 /* Mark the previous definition from IR object as
1987 undefined so that the generic linker will override
1988 it. */
1989 bh->type = bfd_link_hash_undefined;
1990 bh->u.undef.abfd = bh->u.def.section->owner;
1991 }
1992 if (! (_bfd_generic_link_add_one_symbol
1993 (info, abfd, shortname, BSF_INDIRECT,
1994 bfd_ind_section_ptr,
1995 0, name, false, collect, &bh)))
1996 return false;
1997 hi = (struct elf_link_hash_entry *) bh;
1998 }
1999 }
2000 else
2001 {
2002 /* In this case the symbol named SHORTNAME is overriding the
2003 indirect symbol we want to add. We were planning on making
2004 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
2005 is the name without a version. NAME is the fully versioned
2006 name, and it is the default version.
2007
2008 Overriding means that we already saw a definition for the
2009 symbol SHORTNAME in a regular object, and it is overriding
2010 the symbol defined in the dynamic object.
2011
2012 When this happens, we actually want to change NAME, the
2013 symbol we just added, to refer to SHORTNAME. This will cause
2014 references to NAME in the shared object to become references
2015 to SHORTNAME in the regular object. This is what we expect
2016 when we override a function in a shared object: that the
2017 references in the shared object will be mapped to the
2018 definition in the regular object. */
2019
2020 while (hi->root.type == bfd_link_hash_indirect
2021 || hi->root.type == bfd_link_hash_warning)
2022 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2023
2024 h->root.type = bfd_link_hash_indirect;
2025 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
2026 if (h->def_dynamic)
2027 {
2028 h->def_dynamic = 0;
2029 hi->ref_dynamic = 1;
2030 if (hi->ref_regular
2031 || hi->def_regular)
2032 {
2033 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
2034 return false;
2035 }
2036 }
2037
2038 /* Now set HI to H, so that the following code will set the
2039 other fields correctly. */
2040 hi = h;
2041 }
2042
2043 /* Check if HI is a warning symbol. */
2044 if (hi->root.type == bfd_link_hash_warning)
2045 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2046
2047 /* If there is a duplicate definition somewhere, then HI may not
2048 point to an indirect symbol. We will have reported an error to
2049 the user in that case. */
2050
2051 if (hi->root.type == bfd_link_hash_indirect)
2052 {
2053 struct elf_link_hash_entry *ht;
2054
2055 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
2056 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
2057
2058 /* If we first saw a reference to SHORTNAME with non-default
2059 visibility, merge that visibility to the @@VER symbol. */
2060 elf_merge_st_other (abfd, ht, hi->other, sec, true, dynamic);
2061
2062 /* A reference to the SHORTNAME symbol from a dynamic library
2063 will be satisfied by the versioned symbol at runtime. In
2064 effect, we have a reference to the versioned symbol. */
2065 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2066 hi->dynamic_def |= ht->dynamic_def;
2067
2068 /* See if the new flags lead us to realize that the symbol must
2069 be dynamic. */
2070 if (! *dynsym)
2071 {
2072 if (! dynamic)
2073 {
2074 if (! bfd_link_executable (info)
2075 || hi->def_dynamic
2076 || hi->ref_dynamic)
2077 *dynsym = true;
2078 }
2079 else
2080 {
2081 if (hi->ref_regular)
2082 *dynsym = true;
2083 }
2084 }
2085 }
2086
2087 /* We also need to define an indirection from the nondefault version
2088 of the symbol. */
2089
2090 nondefault:
2091 len = strlen (name);
2092 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
2093 if (shortname == NULL)
2094 return false;
2095 memcpy (shortname, name, shortlen);
2096 memcpy (shortname + shortlen, p + 1, len - shortlen);
2097
2098 /* Once again, merge with any existing symbol. */
2099 type_change_ok = false;
2100 size_change_ok = false;
2101 tmp_sec = sec;
2102 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
2103 &hi, poldbfd, NULL, NULL, &skip, &override,
2104 &type_change_ok, &size_change_ok, &matched))
2105 return false;
2106
2107 if (skip)
2108 {
2109 if (!dynamic
2110 && h->root.type == bfd_link_hash_defweak
2111 && hi->root.type == bfd_link_hash_defined)
2112 {
2113 /* We are handling a weak sym@@ver and attempting to define
2114 a weak sym@ver, but _bfd_elf_merge_symbol said to skip the
2115 new weak sym@ver because there is already a strong sym@ver.
2116 However, sym@ver and sym@@ver are really the same symbol.
2117 The existing strong sym@ver ought to override sym@@ver. */
2118 h->root.type = bfd_link_hash_defined;
2119 h->root.u.def.section = hi->root.u.def.section;
2120 h->root.u.def.value = hi->root.u.def.value;
2121 hi->root.type = bfd_link_hash_indirect;
2122 hi->root.u.i.link = &h->root;
2123 }
2124 else
2125 return true;
2126 }
2127 else if (override)
2128 {
2129 /* Here SHORTNAME is a versioned name, so we don't expect to see
2130 the type of override we do in the case above unless it is
2131 overridden by a versioned definition. */
2132 if (hi->root.type != bfd_link_hash_defined
2133 && hi->root.type != bfd_link_hash_defweak)
2134 _bfd_error_handler
2135 /* xgettext:c-format */
2136 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
2137 abfd, shortname);
2138 return true;
2139 }
2140 else
2141 {
2142 bh = &hi->root;
2143 if (! (_bfd_generic_link_add_one_symbol
2144 (info, abfd, shortname, BSF_INDIRECT,
2145 bfd_ind_section_ptr, 0, name, false, collect, &bh)))
2146 return false;
2147 hi = (struct elf_link_hash_entry *) bh;
2148 }
2149
2150 /* If there is a duplicate definition somewhere, then HI may not
2151 point to an indirect symbol. We will have reported an error
2152 to the user in that case. */
2153 if (hi->root.type == bfd_link_hash_indirect)
2154 {
2155 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
2156 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2157 hi->dynamic_def |= h->dynamic_def;
2158
2159 /* If we first saw a reference to @VER symbol with
2160 non-default visibility, merge that visibility to the
2161 @@VER symbol. */
2162 elf_merge_st_other (abfd, h, hi->other, sec, true, dynamic);
2163
2164 /* See if the new flags lead us to realize that the symbol
2165 must be dynamic. */
2166 if (! *dynsym)
2167 {
2168 if (! dynamic)
2169 {
2170 if (! bfd_link_executable (info)
2171 || hi->ref_dynamic)
2172 *dynsym = true;
2173 }
2174 else
2175 {
2176 if (hi->ref_regular)
2177 *dynsym = true;
2178 }
2179 }
2180 }
2181
2182 return true;
2183 }
2184 \f
2185 /* This routine is used to export all defined symbols into the dynamic
2186 symbol table. It is called via elf_link_hash_traverse. */
2187
2188 static bool
2189 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
2190 {
2191 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2192
2193 /* Ignore indirect symbols. These are added by the versioning code. */
2194 if (h->root.type == bfd_link_hash_indirect)
2195 return true;
2196
2197 /* Ignore this if we won't export it. */
2198 if (!eif->info->export_dynamic && !h->dynamic)
2199 return true;
2200
2201 if (h->dynindx == -1
2202 && (h->def_regular || h->ref_regular)
2203 && ! bfd_hide_sym_by_version (eif->info->version_info,
2204 h->root.root.string))
2205 {
2206 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2207 {
2208 eif->failed = true;
2209 return false;
2210 }
2211 }
2212
2213 return true;
2214 }
2215 \f
2216 /* Return true if GLIBC_ABI_DT_RELR is added to the list of version
2217 dependencies successfully. GLIBC_ABI_DT_RELR will be put into the
2218 .gnu.version_r section. */
2219
2220 static bool
2221 elf_link_add_dt_relr_dependency (struct elf_find_verdep_info *rinfo)
2222 {
2223 bfd *glibc_bfd = NULL;
2224 Elf_Internal_Verneed *t;
2225 Elf_Internal_Vernaux *a;
2226 size_t amt;
2227 const char *relr = "GLIBC_ABI_DT_RELR";
2228
2229 /* See if we already know about GLIBC_PRIVATE_DT_RELR. */
2230 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2231 t != NULL;
2232 t = t->vn_nextref)
2233 {
2234 const char *soname = bfd_elf_get_dt_soname (t->vn_bfd);
2235 /* Skip the shared library if it isn't libc.so. */
2236 if (!soname || !startswith (soname, "libc.so."))
2237 continue;
2238
2239 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2240 {
2241 /* Return if GLIBC_PRIVATE_DT_RELR dependency has been
2242 added. */
2243 if (a->vna_nodename == relr
2244 || strcmp (a->vna_nodename, relr) == 0)
2245 return true;
2246
2247 /* Check if libc.so provides GLIBC_2.XX version. */
2248 if (!glibc_bfd && startswith (a->vna_nodename, "GLIBC_2."))
2249 glibc_bfd = t->vn_bfd;
2250 }
2251
2252 break;
2253 }
2254
2255 /* Skip if it isn't linked against glibc. */
2256 if (glibc_bfd == NULL)
2257 return true;
2258
2259 /* This is a new version. Add it to tree we are building. */
2260 if (t == NULL)
2261 {
2262 amt = sizeof *t;
2263 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd,
2264 amt);
2265 if (t == NULL)
2266 {
2267 rinfo->failed = true;
2268 return false;
2269 }
2270
2271 t->vn_bfd = glibc_bfd;
2272 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2273 elf_tdata (rinfo->info->output_bfd)->verref = t;
2274 }
2275
2276 amt = sizeof *a;
2277 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2278 if (a == NULL)
2279 {
2280 rinfo->failed = true;
2281 return false;
2282 }
2283
2284 a->vna_nodename = relr;
2285 a->vna_flags = 0;
2286 a->vna_nextptr = t->vn_auxptr;
2287 a->vna_other = rinfo->vers + 1;
2288 ++rinfo->vers;
2289
2290 t->vn_auxptr = a;
2291
2292 return true;
2293 }
2294
2295 /* Look through the symbols which are defined in other shared
2296 libraries and referenced here. Update the list of version
2297 dependencies. This will be put into the .gnu.version_r section.
2298 This function is called via elf_link_hash_traverse. */
2299
2300 static bool
2301 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2302 void *data)
2303 {
2304 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2305 Elf_Internal_Verneed *t;
2306 Elf_Internal_Vernaux *a;
2307 size_t amt;
2308
2309 /* We only care about symbols defined in shared objects with version
2310 information. */
2311 if (!h->def_dynamic
2312 || h->def_regular
2313 || h->dynindx == -1
2314 || h->verinfo.verdef == NULL
2315 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2316 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2317 return true;
2318
2319 /* See if we already know about this version. */
2320 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2321 t != NULL;
2322 t = t->vn_nextref)
2323 {
2324 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2325 continue;
2326
2327 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2328 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2329 return true;
2330
2331 break;
2332 }
2333
2334 /* This is a new version. Add it to tree we are building. */
2335
2336 if (t == NULL)
2337 {
2338 amt = sizeof *t;
2339 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2340 if (t == NULL)
2341 {
2342 rinfo->failed = true;
2343 return false;
2344 }
2345
2346 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2347 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2348 elf_tdata (rinfo->info->output_bfd)->verref = t;
2349 }
2350
2351 amt = sizeof *a;
2352 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2353 if (a == NULL)
2354 {
2355 rinfo->failed = true;
2356 return false;
2357 }
2358
2359 /* Note that we are copying a string pointer here, and testing it
2360 above. If bfd_elf_string_from_elf_section is ever changed to
2361 discard the string data when low in memory, this will have to be
2362 fixed. */
2363 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2364
2365 a->vna_flags = h->verinfo.verdef->vd_flags;
2366 a->vna_nextptr = t->vn_auxptr;
2367
2368 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2369 ++rinfo->vers;
2370
2371 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2372
2373 t->vn_auxptr = a;
2374
2375 return true;
2376 }
2377
2378 /* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2379 hidden. Set *T_P to NULL if there is no match. */
2380
2381 static bool
2382 _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
2383 struct elf_link_hash_entry *h,
2384 const char *version_p,
2385 struct bfd_elf_version_tree **t_p,
2386 bool *hide)
2387 {
2388 struct bfd_elf_version_tree *t;
2389
2390 /* Look for the version. If we find it, it is no longer weak. */
2391 for (t = info->version_info; t != NULL; t = t->next)
2392 {
2393 if (strcmp (t->name, version_p) == 0)
2394 {
2395 size_t len;
2396 char *alc;
2397 struct bfd_elf_version_expr *d;
2398
2399 len = version_p - h->root.root.string;
2400 alc = (char *) bfd_malloc (len);
2401 if (alc == NULL)
2402 return false;
2403 memcpy (alc, h->root.root.string, len - 1);
2404 alc[len - 1] = '\0';
2405 if (alc[len - 2] == ELF_VER_CHR)
2406 alc[len - 2] = '\0';
2407
2408 h->verinfo.vertree = t;
2409 t->used = true;
2410 d = NULL;
2411
2412 if (t->globals.list != NULL)
2413 d = (*t->match) (&t->globals, NULL, alc);
2414
2415 /* See if there is anything to force this symbol to
2416 local scope. */
2417 if (d == NULL && t->locals.list != NULL)
2418 {
2419 d = (*t->match) (&t->locals, NULL, alc);
2420 if (d != NULL
2421 && h->dynindx != -1
2422 && ! info->export_dynamic)
2423 *hide = true;
2424 }
2425
2426 free (alc);
2427 break;
2428 }
2429 }
2430
2431 *t_p = t;
2432
2433 return true;
2434 }
2435
2436 /* Return TRUE if the symbol H is hidden by version script. */
2437
2438 bool
2439 _bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
2440 struct elf_link_hash_entry *h)
2441 {
2442 const char *p;
2443 bool hide = false;
2444 const struct elf_backend_data *bed
2445 = get_elf_backend_data (info->output_bfd);
2446
2447 /* Version script only hides symbols defined in regular objects. */
2448 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2449 return true;
2450
2451 p = strchr (h->root.root.string, ELF_VER_CHR);
2452 if (p != NULL && h->verinfo.vertree == NULL)
2453 {
2454 struct bfd_elf_version_tree *t;
2455
2456 ++p;
2457 if (*p == ELF_VER_CHR)
2458 ++p;
2459
2460 if (*p != '\0'
2461 && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
2462 && hide)
2463 {
2464 if (hide)
2465 (*bed->elf_backend_hide_symbol) (info, h, true);
2466 return true;
2467 }
2468 }
2469
2470 /* If we don't have a version for this symbol, see if we can find
2471 something. */
2472 if (h->verinfo.vertree == NULL && info->version_info != NULL)
2473 {
2474 h->verinfo.vertree
2475 = bfd_find_version_for_sym (info->version_info,
2476 h->root.root.string, &hide);
2477 if (h->verinfo.vertree != NULL && hide)
2478 {
2479 (*bed->elf_backend_hide_symbol) (info, h, true);
2480 return true;
2481 }
2482 }
2483
2484 return false;
2485 }
2486
2487 /* Figure out appropriate versions for all the symbols. We may not
2488 have the version number script until we have read all of the input
2489 files, so until that point we don't know which symbols should be
2490 local. This function is called via elf_link_hash_traverse. */
2491
2492 static bool
2493 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2494 {
2495 struct elf_info_failed *sinfo;
2496 struct bfd_link_info *info;
2497 const struct elf_backend_data *bed;
2498 struct elf_info_failed eif;
2499 char *p;
2500 bool hide;
2501
2502 sinfo = (struct elf_info_failed *) data;
2503 info = sinfo->info;
2504
2505 /* Fix the symbol flags. */
2506 eif.failed = false;
2507 eif.info = info;
2508 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2509 {
2510 if (eif.failed)
2511 sinfo->failed = true;
2512 return false;
2513 }
2514
2515 bed = get_elf_backend_data (info->output_bfd);
2516
2517 /* We only need version numbers for symbols defined in regular
2518 objects. */
2519 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2520 {
2521 /* Hide symbols defined in discarded input sections. */
2522 if ((h->root.type == bfd_link_hash_defined
2523 || h->root.type == bfd_link_hash_defweak)
2524 && discarded_section (h->root.u.def.section))
2525 (*bed->elf_backend_hide_symbol) (info, h, true);
2526 return true;
2527 }
2528
2529 hide = false;
2530 p = strchr (h->root.root.string, ELF_VER_CHR);
2531 if (p != NULL && h->verinfo.vertree == NULL)
2532 {
2533 struct bfd_elf_version_tree *t;
2534
2535 ++p;
2536 if (*p == ELF_VER_CHR)
2537 ++p;
2538
2539 /* If there is no version string, we can just return out. */
2540 if (*p == '\0')
2541 return true;
2542
2543 if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
2544 {
2545 sinfo->failed = true;
2546 return false;
2547 }
2548
2549 if (hide)
2550 (*bed->elf_backend_hide_symbol) (info, h, true);
2551
2552 /* If we are building an application, we need to create a
2553 version node for this version. */
2554 if (t == NULL && bfd_link_executable (info))
2555 {
2556 struct bfd_elf_version_tree **pp;
2557 int version_index;
2558
2559 /* If we aren't going to export this symbol, we don't need
2560 to worry about it. */
2561 if (h->dynindx == -1)
2562 return true;
2563
2564 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2565 sizeof *t);
2566 if (t == NULL)
2567 {
2568 sinfo->failed = true;
2569 return false;
2570 }
2571
2572 t->name = p;
2573 t->name_indx = (unsigned int) -1;
2574 t->used = true;
2575
2576 version_index = 1;
2577 /* Don't count anonymous version tag. */
2578 if (sinfo->info->version_info != NULL
2579 && sinfo->info->version_info->vernum == 0)
2580 version_index = 0;
2581 for (pp = &sinfo->info->version_info;
2582 *pp != NULL;
2583 pp = &(*pp)->next)
2584 ++version_index;
2585 t->vernum = version_index;
2586
2587 *pp = t;
2588
2589 h->verinfo.vertree = t;
2590 }
2591 else if (t == NULL)
2592 {
2593 /* We could not find the version for a symbol when
2594 generating a shared archive. Return an error. */
2595 _bfd_error_handler
2596 /* xgettext:c-format */
2597 (_("%pB: version node not found for symbol %s"),
2598 info->output_bfd, h->root.root.string);
2599 bfd_set_error (bfd_error_bad_value);
2600 sinfo->failed = true;
2601 return false;
2602 }
2603 }
2604
2605 /* If we don't have a version for this symbol, see if we can find
2606 something. */
2607 if (!hide
2608 && h->verinfo.vertree == NULL
2609 && sinfo->info->version_info != NULL)
2610 {
2611 h->verinfo.vertree
2612 = bfd_find_version_for_sym (sinfo->info->version_info,
2613 h->root.root.string, &hide);
2614 if (h->verinfo.vertree != NULL && hide)
2615 (*bed->elf_backend_hide_symbol) (info, h, true);
2616 }
2617
2618 return true;
2619 }
2620 \f
2621 /* Read and swap the relocs from the section indicated by SHDR. This
2622 may be either a REL or a RELA section. The relocations are
2623 translated into RELA relocations and stored in INTERNAL_RELOCS,
2624 which should have already been allocated to contain enough space.
2625 The EXTERNAL_RELOCS are a buffer where the external form of the
2626 relocations should be stored.
2627
2628 Returns FALSE if something goes wrong. */
2629
2630 static bool
2631 elf_link_read_relocs_from_section (bfd *abfd,
2632 asection *sec,
2633 Elf_Internal_Shdr *shdr,
2634 void *external_relocs,
2635 Elf_Internal_Rela *internal_relocs)
2636 {
2637 const struct elf_backend_data *bed;
2638 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2639 const bfd_byte *erela;
2640 const bfd_byte *erelaend;
2641 Elf_Internal_Rela *irela;
2642 Elf_Internal_Shdr *symtab_hdr;
2643 size_t nsyms;
2644
2645 /* Position ourselves at the start of the section. */
2646 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2647 return false;
2648
2649 /* Read the relocations. */
2650 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2651 return false;
2652
2653 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2654 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2655
2656 bed = get_elf_backend_data (abfd);
2657
2658 /* Convert the external relocations to the internal format. */
2659 if (shdr->sh_entsize == bed->s->sizeof_rel)
2660 swap_in = bed->s->swap_reloc_in;
2661 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2662 swap_in = bed->s->swap_reloca_in;
2663 else
2664 {
2665 bfd_set_error (bfd_error_wrong_format);
2666 return false;
2667 }
2668
2669 erela = (const bfd_byte *) external_relocs;
2670 /* Setting erelaend like this and comparing with <= handles case of
2671 a fuzzed object with sh_size not a multiple of sh_entsize. */
2672 erelaend = erela + shdr->sh_size - shdr->sh_entsize;
2673 irela = internal_relocs;
2674 while (erela <= erelaend)
2675 {
2676 bfd_vma r_symndx;
2677
2678 (*swap_in) (abfd, erela, irela);
2679 r_symndx = ELF32_R_SYM (irela->r_info);
2680 if (bed->s->arch_size == 64)
2681 r_symndx >>= 24;
2682 if (nsyms > 0)
2683 {
2684 if ((size_t) r_symndx >= nsyms)
2685 {
2686 _bfd_error_handler
2687 /* xgettext:c-format */
2688 (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
2689 " for offset %#" PRIx64 " in section `%pA'"),
2690 abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
2691 (uint64_t) irela->r_offset, sec);
2692 bfd_set_error (bfd_error_bad_value);
2693 return false;
2694 }
2695 }
2696 else if (r_symndx != STN_UNDEF)
2697 {
2698 _bfd_error_handler
2699 /* xgettext:c-format */
2700 (_("%pB: non-zero symbol index (%#" PRIx64 ")"
2701 " for offset %#" PRIx64 " in section `%pA'"
2702 " when the object file has no symbol table"),
2703 abfd, (uint64_t) r_symndx,
2704 (uint64_t) irela->r_offset, sec);
2705 bfd_set_error (bfd_error_bad_value);
2706 return false;
2707 }
2708 irela += bed->s->int_rels_per_ext_rel;
2709 erela += shdr->sh_entsize;
2710 }
2711
2712 return true;
2713 }
2714
2715 /* Read and swap the relocs for a section O. They may have been
2716 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2717 not NULL, they are used as buffers to read into. They are known to
2718 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2719 the return value is allocated using either malloc or bfd_alloc,
2720 according to the KEEP_MEMORY argument. If O has two relocation
2721 sections (both REL and RELA relocations), then the REL_HDR
2722 relocations will appear first in INTERNAL_RELOCS, followed by the
2723 RELA_HDR relocations. If INFO isn't NULL and KEEP_MEMORY is true,
2724 update cache_size. */
2725
2726 Elf_Internal_Rela *
2727 _bfd_elf_link_info_read_relocs (bfd *abfd,
2728 struct bfd_link_info *info,
2729 asection *o,
2730 void *external_relocs,
2731 Elf_Internal_Rela *internal_relocs,
2732 bool keep_memory)
2733 {
2734 void *alloc1 = NULL;
2735 Elf_Internal_Rela *alloc2 = NULL;
2736 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2737 struct bfd_elf_section_data *esdo = elf_section_data (o);
2738 Elf_Internal_Rela *internal_rela_relocs;
2739
2740 if (esdo->relocs != NULL)
2741 return esdo->relocs;
2742
2743 if (o->reloc_count == 0)
2744 return NULL;
2745
2746 if (internal_relocs == NULL)
2747 {
2748 bfd_size_type size;
2749
2750 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
2751 if (keep_memory)
2752 {
2753 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2754 if (info)
2755 info->cache_size += size;
2756 }
2757 else
2758 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2759 if (internal_relocs == NULL)
2760 goto error_return;
2761 }
2762
2763 if (external_relocs == NULL)
2764 {
2765 bfd_size_type size = 0;
2766
2767 if (esdo->rel.hdr)
2768 size += esdo->rel.hdr->sh_size;
2769 if (esdo->rela.hdr)
2770 size += esdo->rela.hdr->sh_size;
2771
2772 alloc1 = bfd_malloc (size);
2773 if (alloc1 == NULL)
2774 goto error_return;
2775 external_relocs = alloc1;
2776 }
2777
2778 internal_rela_relocs = internal_relocs;
2779 if (esdo->rel.hdr)
2780 {
2781 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2782 external_relocs,
2783 internal_relocs))
2784 goto error_return;
2785 external_relocs = (((bfd_byte *) external_relocs)
2786 + esdo->rel.hdr->sh_size);
2787 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2788 * bed->s->int_rels_per_ext_rel);
2789 }
2790
2791 if (esdo->rela.hdr
2792 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2793 external_relocs,
2794 internal_rela_relocs)))
2795 goto error_return;
2796
2797 /* Cache the results for next time, if we can. */
2798 if (keep_memory)
2799 esdo->relocs = internal_relocs;
2800
2801 free (alloc1);
2802
2803 /* Don't free alloc2, since if it was allocated we are passing it
2804 back (under the name of internal_relocs). */
2805
2806 return internal_relocs;
2807
2808 error_return:
2809 free (alloc1);
2810 if (alloc2 != NULL)
2811 {
2812 if (keep_memory)
2813 bfd_release (abfd, alloc2);
2814 else
2815 free (alloc2);
2816 }
2817 return NULL;
2818 }
2819
2820 /* This is similar to _bfd_elf_link_info_read_relocs, except for that
2821 NULL is passed to _bfd_elf_link_info_read_relocs for pointer to
2822 struct bfd_link_info. */
2823
2824 Elf_Internal_Rela *
2825 _bfd_elf_link_read_relocs (bfd *abfd,
2826 asection *o,
2827 void *external_relocs,
2828 Elf_Internal_Rela *internal_relocs,
2829 bool keep_memory)
2830 {
2831 return _bfd_elf_link_info_read_relocs (abfd, NULL, o, external_relocs,
2832 internal_relocs, keep_memory);
2833
2834 }
2835
2836 /* Compute the size of, and allocate space for, REL_HDR which is the
2837 section header for a section containing relocations for O. */
2838
2839 static bool
2840 _bfd_elf_link_size_reloc_section (bfd *abfd,
2841 struct bfd_elf_section_reloc_data *reldata)
2842 {
2843 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2844
2845 /* That allows us to calculate the size of the section. */
2846 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2847
2848 /* The contents field must last into write_object_contents, so we
2849 allocate it with bfd_alloc rather than malloc. Also since we
2850 cannot be sure that the contents will actually be filled in,
2851 we zero the allocated space. */
2852 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2853 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2854 return false;
2855
2856 if (reldata->hashes == NULL && reldata->count)
2857 {
2858 struct elf_link_hash_entry **p;
2859
2860 p = ((struct elf_link_hash_entry **)
2861 bfd_zmalloc (reldata->count * sizeof (*p)));
2862 if (p == NULL)
2863 return false;
2864
2865 reldata->hashes = p;
2866 }
2867
2868 return true;
2869 }
2870
2871 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2872 originated from the section given by INPUT_REL_HDR) to the
2873 OUTPUT_BFD. */
2874
2875 bool
2876 _bfd_elf_link_output_relocs (bfd *output_bfd,
2877 asection *input_section,
2878 Elf_Internal_Shdr *input_rel_hdr,
2879 Elf_Internal_Rela *internal_relocs,
2880 struct elf_link_hash_entry **rel_hash
2881 ATTRIBUTE_UNUSED)
2882 {
2883 Elf_Internal_Rela *irela;
2884 Elf_Internal_Rela *irelaend;
2885 bfd_byte *erel;
2886 struct bfd_elf_section_reloc_data *output_reldata;
2887 asection *output_section;
2888 const struct elf_backend_data *bed;
2889 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2890 struct bfd_elf_section_data *esdo;
2891
2892 output_section = input_section->output_section;
2893
2894 bed = get_elf_backend_data (output_bfd);
2895 esdo = elf_section_data (output_section);
2896 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2897 {
2898 output_reldata = &esdo->rel;
2899 swap_out = bed->s->swap_reloc_out;
2900 }
2901 else if (esdo->rela.hdr
2902 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2903 {
2904 output_reldata = &esdo->rela;
2905 swap_out = bed->s->swap_reloca_out;
2906 }
2907 else
2908 {
2909 _bfd_error_handler
2910 /* xgettext:c-format */
2911 (_("%pB: relocation size mismatch in %pB section %pA"),
2912 output_bfd, input_section->owner, input_section);
2913 bfd_set_error (bfd_error_wrong_format);
2914 return false;
2915 }
2916
2917 erel = output_reldata->hdr->contents;
2918 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2919 irela = internal_relocs;
2920 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2921 * bed->s->int_rels_per_ext_rel);
2922 while (irela < irelaend)
2923 {
2924 (*swap_out) (output_bfd, irela, erel);
2925 irela += bed->s->int_rels_per_ext_rel;
2926 erel += input_rel_hdr->sh_entsize;
2927 }
2928
2929 /* Bump the counter, so that we know where to add the next set of
2930 relocations. */
2931 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2932
2933 return true;
2934 }
2935 \f
2936 /* Make weak undefined symbols in PIE dynamic. */
2937
2938 bool
2939 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2940 struct elf_link_hash_entry *h)
2941 {
2942 if (bfd_link_pie (info)
2943 && h->dynindx == -1
2944 && h->root.type == bfd_link_hash_undefweak)
2945 return bfd_elf_link_record_dynamic_symbol (info, h);
2946
2947 return true;
2948 }
2949
2950 /* Fix up the flags for a symbol. This handles various cases which
2951 can only be fixed after all the input files are seen. This is
2952 currently called by both adjust_dynamic_symbol and
2953 assign_sym_version, which is unnecessary but perhaps more robust in
2954 the face of future changes. */
2955
2956 static bool
2957 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2958 struct elf_info_failed *eif)
2959 {
2960 const struct elf_backend_data *bed;
2961
2962 /* If this symbol was mentioned in a non-ELF file, try to set
2963 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2964 permit a non-ELF file to correctly refer to a symbol defined in
2965 an ELF dynamic object. */
2966 if (h->non_elf)
2967 {
2968 while (h->root.type == bfd_link_hash_indirect)
2969 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2970
2971 if (h->root.type != bfd_link_hash_defined
2972 && h->root.type != bfd_link_hash_defweak)
2973 {
2974 h->ref_regular = 1;
2975 h->ref_regular_nonweak = 1;
2976 }
2977 else
2978 {
2979 if (h->root.u.def.section->owner != NULL
2980 && (bfd_get_flavour (h->root.u.def.section->owner)
2981 == bfd_target_elf_flavour))
2982 {
2983 h->ref_regular = 1;
2984 h->ref_regular_nonweak = 1;
2985 }
2986 else
2987 h->def_regular = 1;
2988 }
2989
2990 if (h->dynindx == -1
2991 && (h->def_dynamic
2992 || h->ref_dynamic))
2993 {
2994 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2995 {
2996 eif->failed = true;
2997 return false;
2998 }
2999 }
3000 }
3001 else
3002 {
3003 /* Unfortunately, NON_ELF is only correct if the symbol
3004 was first seen in a non-ELF file. Fortunately, if the symbol
3005 was first seen in an ELF file, we're probably OK unless the
3006 symbol was defined in a non-ELF file. Catch that case here.
3007 FIXME: We're still in trouble if the symbol was first seen in
3008 a dynamic object, and then later in a non-ELF regular object. */
3009 if ((h->root.type == bfd_link_hash_defined
3010 || h->root.type == bfd_link_hash_defweak)
3011 && !h->def_regular
3012 && (h->root.u.def.section->owner != NULL
3013 ? (bfd_get_flavour (h->root.u.def.section->owner)
3014 != bfd_target_elf_flavour)
3015 : (bfd_is_abs_section (h->root.u.def.section)
3016 && !h->def_dynamic)))
3017 h->def_regular = 1;
3018 }
3019
3020 /* Backend specific symbol fixup. */
3021 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
3022 if (bed->elf_backend_fixup_symbol
3023 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
3024 return false;
3025
3026 /* If this is a final link, and the symbol was defined as a common
3027 symbol in a regular object file, and there was no definition in
3028 any dynamic object, then the linker will have allocated space for
3029 the symbol in a common section but the DEF_REGULAR
3030 flag will not have been set. */
3031 if (h->root.type == bfd_link_hash_defined
3032 && !h->def_regular
3033 && h->ref_regular
3034 && !h->def_dynamic
3035 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
3036 h->def_regular = 1;
3037
3038 /* Symbols defined in discarded sections shouldn't be dynamic. */
3039 if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
3040 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3041
3042 /* If a weak undefined symbol has non-default visibility, we also
3043 hide it from the dynamic linker. */
3044 else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3045 && h->root.type == bfd_link_hash_undefweak)
3046 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3047
3048 /* A hidden versioned symbol in executable should be forced local if
3049 it is is locally defined, not referenced by shared library and not
3050 exported. */
3051 else if (bfd_link_executable (eif->info)
3052 && h->versioned == versioned_hidden
3053 && !eif->info->export_dynamic
3054 && !h->dynamic
3055 && !h->ref_dynamic
3056 && h->def_regular)
3057 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3058
3059 /* If -Bsymbolic was used (which means to bind references to global
3060 symbols to the definition within the shared object), and this
3061 symbol was defined in a regular object, then it actually doesn't
3062 need a PLT entry. Likewise, if the symbol has non-default
3063 visibility. If the symbol has hidden or internal visibility, we
3064 will force it local. */
3065 else if (h->needs_plt
3066 && bfd_link_pic (eif->info)
3067 && is_elf_hash_table (eif->info->hash)
3068 && (SYMBOLIC_BIND (eif->info, h)
3069 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
3070 && h->def_regular)
3071 {
3072 bool force_local;
3073
3074 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
3075 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
3076 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
3077 }
3078
3079 /* If this is a weak defined symbol in a dynamic object, and we know
3080 the real definition in the dynamic object, copy interesting flags
3081 over to the real definition. */
3082 if (h->is_weakalias)
3083 {
3084 struct elf_link_hash_entry *def = weakdef (h);
3085
3086 /* If the real definition is defined by a regular object file,
3087 don't do anything special. See the longer description in
3088 _bfd_elf_adjust_dynamic_symbol, below. If the def is not
3089 bfd_link_hash_defined as it was when put on the alias list
3090 then it must have originally been a versioned symbol (for
3091 which a non-versioned indirect symbol is created) and later
3092 a definition for the non-versioned symbol is found. In that
3093 case the indirection is flipped with the versioned symbol
3094 becoming an indirect pointing at the non-versioned symbol.
3095 Thus, not an alias any more. */
3096 if (def->def_regular
3097 || def->root.type != bfd_link_hash_defined)
3098 {
3099 h = def;
3100 while ((h = h->u.alias) != def)
3101 h->is_weakalias = 0;
3102 }
3103 else
3104 {
3105 while (h->root.type == bfd_link_hash_indirect)
3106 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3107 BFD_ASSERT (h->root.type == bfd_link_hash_defined
3108 || h->root.type == bfd_link_hash_defweak);
3109 BFD_ASSERT (def->def_dynamic);
3110 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
3111 }
3112 }
3113
3114 return true;
3115 }
3116
3117 /* Make the backend pick a good value for a dynamic symbol. This is
3118 called via elf_link_hash_traverse, and also calls itself
3119 recursively. */
3120
3121 static bool
3122 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
3123 {
3124 struct elf_info_failed *eif = (struct elf_info_failed *) data;
3125 struct elf_link_hash_table *htab;
3126 const struct elf_backend_data *bed;
3127
3128 if (! is_elf_hash_table (eif->info->hash))
3129 return false;
3130
3131 /* Ignore indirect symbols. These are added by the versioning code. */
3132 if (h->root.type == bfd_link_hash_indirect)
3133 return true;
3134
3135 /* Fix the symbol flags. */
3136 if (! _bfd_elf_fix_symbol_flags (h, eif))
3137 return false;
3138
3139 htab = elf_hash_table (eif->info);
3140 bed = get_elf_backend_data (htab->dynobj);
3141
3142 if (h->root.type == bfd_link_hash_undefweak)
3143 {
3144 if (eif->info->dynamic_undefined_weak == 0)
3145 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3146 else if (eif->info->dynamic_undefined_weak > 0
3147 && h->ref_regular
3148 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3149 && !bfd_hide_sym_by_version (eif->info->version_info,
3150 h->root.root.string))
3151 {
3152 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
3153 {
3154 eif->failed = true;
3155 return false;
3156 }
3157 }
3158 }
3159
3160 /* If this symbol does not require a PLT entry, and it is not
3161 defined by a dynamic object, or is not referenced by a regular
3162 object, ignore it. We do have to handle a weak defined symbol,
3163 even if no regular object refers to it, if we decided to add it
3164 to the dynamic symbol table. FIXME: Do we normally need to worry
3165 about symbols which are defined by one dynamic object and
3166 referenced by another one? */
3167 if (!h->needs_plt
3168 && h->type != STT_GNU_IFUNC
3169 && (h->def_regular
3170 || !h->def_dynamic
3171 || (!h->ref_regular
3172 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
3173 {
3174 h->plt = elf_hash_table (eif->info)->init_plt_offset;
3175 return true;
3176 }
3177
3178 /* If we've already adjusted this symbol, don't do it again. This
3179 can happen via a recursive call. */
3180 if (h->dynamic_adjusted)
3181 return true;
3182
3183 /* Don't look at this symbol again. Note that we must set this
3184 after checking the above conditions, because we may look at a
3185 symbol once, decide not to do anything, and then get called
3186 recursively later after REF_REGULAR is set below. */
3187 h->dynamic_adjusted = 1;
3188
3189 /* If this is a weak definition, and we know a real definition, and
3190 the real symbol is not itself defined by a regular object file,
3191 then get a good value for the real definition. We handle the
3192 real symbol first, for the convenience of the backend routine.
3193
3194 Note that there is a confusing case here. If the real definition
3195 is defined by a regular object file, we don't get the real symbol
3196 from the dynamic object, but we do get the weak symbol. If the
3197 processor backend uses a COPY reloc, then if some routine in the
3198 dynamic object changes the real symbol, we will not see that
3199 change in the corresponding weak symbol. This is the way other
3200 ELF linkers work as well, and seems to be a result of the shared
3201 library model.
3202
3203 I will clarify this issue. Most SVR4 shared libraries define the
3204 variable _timezone and define timezone as a weak synonym. The
3205 tzset call changes _timezone. If you write
3206 extern int timezone;
3207 int _timezone = 5;
3208 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3209 you might expect that, since timezone is a synonym for _timezone,
3210 the same number will print both times. However, if the processor
3211 backend uses a COPY reloc, then actually timezone will be copied
3212 into your process image, and, since you define _timezone
3213 yourself, _timezone will not. Thus timezone and _timezone will
3214 wind up at different memory locations. The tzset call will set
3215 _timezone, leaving timezone unchanged. */
3216
3217 if (h->is_weakalias)
3218 {
3219 struct elf_link_hash_entry *def = weakdef (h);
3220
3221 /* If we get to this point, there is an implicit reference to
3222 the alias by a regular object file via the weak symbol H. */
3223 def->ref_regular = 1;
3224
3225 /* Ensure that the backend adjust_dynamic_symbol function sees
3226 the strong alias before H by recursively calling ourselves. */
3227 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
3228 return false;
3229 }
3230
3231 /* If a symbol has no type and no size and does not require a PLT
3232 entry, then we are probably about to do the wrong thing here: we
3233 are probably going to create a COPY reloc for an empty object.
3234 This case can arise when a shared object is built with assembly
3235 code, and the assembly code fails to set the symbol type. */
3236 if (h->size == 0
3237 && h->type == STT_NOTYPE
3238 && !h->needs_plt)
3239 _bfd_error_handler
3240 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3241 h->root.root.string);
3242
3243 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3244 {
3245 eif->failed = true;
3246 return false;
3247 }
3248
3249 return true;
3250 }
3251
3252 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3253 DYNBSS. */
3254
3255 bool
3256 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3257 struct elf_link_hash_entry *h,
3258 asection *dynbss)
3259 {
3260 unsigned int power_of_two;
3261 bfd_vma mask;
3262 asection *sec = h->root.u.def.section;
3263
3264 /* The section alignment of the definition is the maximum alignment
3265 requirement of symbols defined in the section. Since we don't
3266 know the symbol alignment requirement, we start with the
3267 maximum alignment and check low bits of the symbol address
3268 for the minimum alignment. */
3269 power_of_two = bfd_section_alignment (sec);
3270 mask = ((bfd_vma) 1 << power_of_two) - 1;
3271 while ((h->root.u.def.value & mask) != 0)
3272 {
3273 mask >>= 1;
3274 --power_of_two;
3275 }
3276
3277 if (power_of_two > bfd_section_alignment (dynbss))
3278 {
3279 /* Adjust the section alignment if needed. */
3280 if (!bfd_set_section_alignment (dynbss, power_of_two))
3281 return false;
3282 }
3283
3284 /* We make sure that the symbol will be aligned properly. */
3285 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3286
3287 /* Define the symbol as being at this point in DYNBSS. */
3288 h->root.u.def.section = dynbss;
3289 h->root.u.def.value = dynbss->size;
3290
3291 /* Increment the size of DYNBSS to make room for the symbol. */
3292 dynbss->size += h->size;
3293
3294 /* No error if extern_protected_data is true. */
3295 if (h->protected_def
3296 && (!info->extern_protected_data
3297 || (info->extern_protected_data < 0
3298 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
3299 info->callbacks->einfo
3300 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3301 h->root.root.string);
3302
3303 return true;
3304 }
3305
3306 /* Adjust all external symbols pointing into SEC_MERGE sections
3307 to reflect the object merging within the sections. */
3308
3309 static bool
3310 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
3311 {
3312 asection *sec;
3313
3314 if ((h->root.type == bfd_link_hash_defined
3315 || h->root.type == bfd_link_hash_defweak)
3316 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
3317 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3318 {
3319 bfd *output_bfd = (bfd *) data;
3320
3321 h->root.u.def.value =
3322 _bfd_merged_section_offset (output_bfd,
3323 &h->root.u.def.section,
3324 elf_section_data (sec)->sec_info,
3325 h->root.u.def.value);
3326 }
3327
3328 return true;
3329 }
3330
3331 /* Returns false if the symbol referred to by H should be considered
3332 to resolve local to the current module, and true if it should be
3333 considered to bind dynamically. */
3334
3335 bool
3336 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3337 struct bfd_link_info *info,
3338 bool not_local_protected)
3339 {
3340 bool binding_stays_local_p;
3341 const struct elf_backend_data *bed;
3342 struct elf_link_hash_table *hash_table;
3343
3344 if (h == NULL)
3345 return false;
3346
3347 while (h->root.type == bfd_link_hash_indirect
3348 || h->root.type == bfd_link_hash_warning)
3349 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3350
3351 /* If it was forced local, then clearly it's not dynamic. */
3352 if (h->dynindx == -1)
3353 return false;
3354 if (h->forced_local)
3355 return false;
3356
3357 /* Identify the cases where name binding rules say that a
3358 visible symbol resolves locally. */
3359 binding_stays_local_p = (bfd_link_executable (info)
3360 || SYMBOLIC_BIND (info, h));
3361
3362 switch (ELF_ST_VISIBILITY (h->other))
3363 {
3364 case STV_INTERNAL:
3365 case STV_HIDDEN:
3366 return false;
3367
3368 case STV_PROTECTED:
3369 hash_table = elf_hash_table (info);
3370 if (!is_elf_hash_table (&hash_table->root))
3371 return false;
3372
3373 bed = get_elf_backend_data (hash_table->dynobj);
3374
3375 /* Proper resolution for function pointer equality may require
3376 that these symbols perhaps be resolved dynamically, even though
3377 we should be resolving them to the current module. */
3378 if (!not_local_protected || !bed->is_function_type (h->type))
3379 binding_stays_local_p = true;
3380 break;
3381
3382 default:
3383 break;
3384 }
3385
3386 /* If it isn't defined locally, then clearly it's dynamic. */
3387 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
3388 return true;
3389
3390 /* Otherwise, the symbol is dynamic if binding rules don't tell
3391 us that it remains local. */
3392 return !binding_stays_local_p;
3393 }
3394
3395 /* Return true if the symbol referred to by H should be considered
3396 to resolve local to the current module, and false otherwise. Differs
3397 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3398 undefined symbols. The two functions are virtually identical except
3399 for the place where dynindx == -1 is tested. If that test is true,
3400 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3401 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3402 defined symbols.
3403 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3404 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3405 treatment of undefined weak symbols. For those that do not make
3406 undefined weak symbols dynamic, both functions may return false. */
3407
3408 bool
3409 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3410 struct bfd_link_info *info,
3411 bool local_protected)
3412 {
3413 const struct elf_backend_data *bed;
3414 struct elf_link_hash_table *hash_table;
3415
3416 /* If it's a local sym, of course we resolve locally. */
3417 if (h == NULL)
3418 return true;
3419
3420 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3421 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3422 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3423 return true;
3424
3425 /* Forced local symbols resolve locally. */
3426 if (h->forced_local)
3427 return true;
3428
3429 /* Common symbols that become definitions don't get the DEF_REGULAR
3430 flag set, so test it first, and don't bail out. */
3431 if (ELF_COMMON_DEF_P (h))
3432 /* Do nothing. */;
3433 /* If we don't have a definition in a regular file, then we can't
3434 resolve locally. The sym is either undefined or dynamic. */
3435 else if (!h->def_regular)
3436 return false;
3437
3438 /* Non-dynamic symbols resolve locally. */
3439 if (h->dynindx == -1)
3440 return true;
3441
3442 /* At this point, we know the symbol is defined and dynamic. In an
3443 executable it must resolve locally, likewise when building symbolic
3444 shared libraries. */
3445 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3446 return true;
3447
3448 /* Now deal with defined dynamic symbols in shared libraries. Ones
3449 with default visibility might not resolve locally. */
3450 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3451 return false;
3452
3453 hash_table = elf_hash_table (info);
3454 if (!is_elf_hash_table (&hash_table->root))
3455 return true;
3456
3457 /* STV_PROTECTED symbols with indirect external access are local. */
3458 if (info->indirect_extern_access > 0)
3459 return true;
3460
3461 bed = get_elf_backend_data (hash_table->dynobj);
3462
3463 /* If extern_protected_data is false, STV_PROTECTED non-function
3464 symbols are local. */
3465 if ((!info->extern_protected_data
3466 || (info->extern_protected_data < 0
3467 && !bed->extern_protected_data))
3468 && !bed->is_function_type (h->type))
3469 return true;
3470
3471 /* Function pointer equality tests may require that STV_PROTECTED
3472 symbols be treated as dynamic symbols. If the address of a
3473 function not defined in an executable is set to that function's
3474 plt entry in the executable, then the address of the function in
3475 a shared library must also be the plt entry in the executable. */
3476 return local_protected;
3477 }
3478
3479 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3480 aligned. Returns the first TLS output section. */
3481
3482 struct bfd_section *
3483 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3484 {
3485 struct bfd_section *sec, *tls;
3486 unsigned int align = 0;
3487
3488 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3489 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3490 break;
3491 tls = sec;
3492
3493 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3494 if (sec->alignment_power > align)
3495 align = sec->alignment_power;
3496
3497 elf_hash_table (info)->tls_sec = tls;
3498
3499 /* Ensure the alignment of the first section (usually .tdata) is the largest
3500 alignment, so that the tls segment starts aligned. */
3501 if (tls != NULL)
3502 tls->alignment_power = align;
3503
3504 return tls;
3505 }
3506
3507 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3508 static bool
3509 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3510 Elf_Internal_Sym *sym)
3511 {
3512 const struct elf_backend_data *bed;
3513
3514 /* Local symbols do not count, but target specific ones might. */
3515 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3516 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3517 return false;
3518
3519 bed = get_elf_backend_data (abfd);
3520 /* Function symbols do not count. */
3521 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3522 return false;
3523
3524 /* If the section is undefined, then so is the symbol. */
3525 if (sym->st_shndx == SHN_UNDEF)
3526 return false;
3527
3528 /* If the symbol is defined in the common section, then
3529 it is a common definition and so does not count. */
3530 if (bed->common_definition (sym))
3531 return false;
3532
3533 /* If the symbol is in a target specific section then we
3534 must rely upon the backend to tell us what it is. */
3535 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3536 /* FIXME - this function is not coded yet:
3537
3538 return _bfd_is_global_symbol_definition (abfd, sym);
3539
3540 Instead for now assume that the definition is not global,
3541 Even if this is wrong, at least the linker will behave
3542 in the same way that it used to do. */
3543 return false;
3544
3545 return true;
3546 }
3547
3548 /* Search the symbol table of the archive element of the archive ABFD
3549 whose archive map contains a mention of SYMDEF, and determine if
3550 the symbol is defined in this element. */
3551 static bool
3552 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3553 {
3554 Elf_Internal_Shdr * hdr;
3555 size_t symcount;
3556 size_t extsymcount;
3557 size_t extsymoff;
3558 Elf_Internal_Sym *isymbuf;
3559 Elf_Internal_Sym *isym;
3560 Elf_Internal_Sym *isymend;
3561 bool result;
3562
3563 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset, NULL);
3564 if (abfd == NULL)
3565 return false;
3566
3567 if (! bfd_check_format (abfd, bfd_object))
3568 return false;
3569
3570 /* Select the appropriate symbol table. If we don't know if the
3571 object file is an IR object, give linker LTO plugin a chance to
3572 get the correct symbol table. */
3573 if (abfd->plugin_format == bfd_plugin_yes
3574 #if BFD_SUPPORTS_PLUGINS
3575 || (abfd->plugin_format == bfd_plugin_unknown
3576 && bfd_link_plugin_object_p (abfd))
3577 #endif
3578 )
3579 {
3580 /* Use the IR symbol table if the object has been claimed by
3581 plugin. */
3582 abfd = abfd->plugin_dummy_bfd;
3583 hdr = &elf_tdata (abfd)->symtab_hdr;
3584 }
3585 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3586 hdr = &elf_tdata (abfd)->symtab_hdr;
3587 else
3588 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3589
3590 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3591
3592 /* The sh_info field of the symtab header tells us where the
3593 external symbols start. We don't care about the local symbols. */
3594 if (elf_bad_symtab (abfd))
3595 {
3596 extsymcount = symcount;
3597 extsymoff = 0;
3598 }
3599 else
3600 {
3601 extsymcount = symcount - hdr->sh_info;
3602 extsymoff = hdr->sh_info;
3603 }
3604
3605 if (extsymcount == 0)
3606 return false;
3607
3608 /* Read in the symbol table. */
3609 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3610 NULL, NULL, NULL);
3611 if (isymbuf == NULL)
3612 return false;
3613
3614 /* Scan the symbol table looking for SYMDEF. */
3615 result = false;
3616 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3617 {
3618 const char *name;
3619
3620 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3621 isym->st_name);
3622 if (name == NULL)
3623 break;
3624
3625 if (strcmp (name, symdef->name) == 0)
3626 {
3627 result = is_global_data_symbol_definition (abfd, isym);
3628 break;
3629 }
3630 }
3631
3632 free (isymbuf);
3633
3634 return result;
3635 }
3636 \f
3637 /* Add an entry to the .dynamic table. */
3638
3639 bool
3640 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3641 bfd_vma tag,
3642 bfd_vma val)
3643 {
3644 struct elf_link_hash_table *hash_table;
3645 const struct elf_backend_data *bed;
3646 asection *s;
3647 bfd_size_type newsize;
3648 bfd_byte *newcontents;
3649 Elf_Internal_Dyn dyn;
3650
3651 hash_table = elf_hash_table (info);
3652 if (! is_elf_hash_table (&hash_table->root))
3653 return false;
3654
3655 if (tag == DT_RELA || tag == DT_REL)
3656 hash_table->dynamic_relocs = true;
3657
3658 bed = get_elf_backend_data (hash_table->dynobj);
3659 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3660 BFD_ASSERT (s != NULL);
3661
3662 newsize = s->size + bed->s->sizeof_dyn;
3663 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3664 if (newcontents == NULL)
3665 return false;
3666
3667 dyn.d_tag = tag;
3668 dyn.d_un.d_val = val;
3669 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3670
3671 s->size = newsize;
3672 s->contents = newcontents;
3673
3674 return true;
3675 }
3676
3677 /* Strip zero-sized dynamic sections. */
3678
3679 bool
3680 _bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info *info)
3681 {
3682 struct elf_link_hash_table *hash_table;
3683 const struct elf_backend_data *bed;
3684 asection *s, *sdynamic, **pp;
3685 asection *rela_dyn, *rel_dyn;
3686 Elf_Internal_Dyn dyn;
3687 bfd_byte *extdyn, *next;
3688 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3689 bool strip_zero_sized;
3690 bool strip_zero_sized_plt;
3691
3692 if (bfd_link_relocatable (info))
3693 return true;
3694
3695 hash_table = elf_hash_table (info);
3696 if (!is_elf_hash_table (&hash_table->root))
3697 return false;
3698
3699 if (!hash_table->dynobj)
3700 return true;
3701
3702 sdynamic= bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3703 if (!sdynamic)
3704 return true;
3705
3706 bed = get_elf_backend_data (hash_table->dynobj);
3707 swap_dyn_in = bed->s->swap_dyn_in;
3708
3709 strip_zero_sized = false;
3710 strip_zero_sized_plt = false;
3711
3712 /* Strip zero-sized dynamic sections. */
3713 rela_dyn = bfd_get_section_by_name (info->output_bfd, ".rela.dyn");
3714 rel_dyn = bfd_get_section_by_name (info->output_bfd, ".rel.dyn");
3715 for (pp = &info->output_bfd->sections; (s = *pp) != NULL;)
3716 if (s->size == 0
3717 && (s == rela_dyn
3718 || s == rel_dyn
3719 || s == hash_table->srelplt->output_section
3720 || s == hash_table->splt->output_section))
3721 {
3722 *pp = s->next;
3723 info->output_bfd->section_count--;
3724 strip_zero_sized = true;
3725 if (s == rela_dyn)
3726 s = rela_dyn;
3727 if (s == rel_dyn)
3728 s = rel_dyn;
3729 else if (s == hash_table->splt->output_section)
3730 {
3731 s = hash_table->splt;
3732 strip_zero_sized_plt = true;
3733 }
3734 else
3735 s = hash_table->srelplt;
3736 s->flags |= SEC_EXCLUDE;
3737 s->output_section = bfd_abs_section_ptr;
3738 }
3739 else
3740 pp = &s->next;
3741
3742 if (strip_zero_sized_plt && sdynamic->size != 0)
3743 for (extdyn = sdynamic->contents;
3744 extdyn < sdynamic->contents + sdynamic->size;
3745 extdyn = next)
3746 {
3747 next = extdyn + bed->s->sizeof_dyn;
3748 swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3749 switch (dyn.d_tag)
3750 {
3751 default:
3752 break;
3753 case DT_JMPREL:
3754 case DT_PLTRELSZ:
3755 case DT_PLTREL:
3756 /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
3757 the procedure linkage table (the .plt section) has been
3758 removed. */
3759 memmove (extdyn, next,
3760 sdynamic->size - (next - sdynamic->contents));
3761 next = extdyn;
3762 }
3763 }
3764
3765 if (strip_zero_sized)
3766 {
3767 /* Regenerate program headers. */
3768 elf_seg_map (info->output_bfd) = NULL;
3769 return _bfd_elf_map_sections_to_segments (info->output_bfd, info,
3770 NULL);
3771 }
3772
3773 return true;
3774 }
3775
3776 /* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error,
3777 1 if a DT_NEEDED tag already exists, and 0 on success. */
3778
3779 int
3780 bfd_elf_add_dt_needed_tag (bfd *abfd, struct bfd_link_info *info)
3781 {
3782 struct elf_link_hash_table *hash_table;
3783 size_t strindex;
3784 const char *soname;
3785
3786 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3787 return -1;
3788
3789 hash_table = elf_hash_table (info);
3790 soname = elf_dt_name (abfd);
3791 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, false);
3792 if (strindex == (size_t) -1)
3793 return -1;
3794
3795 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3796 {
3797 asection *sdyn;
3798 const struct elf_backend_data *bed;
3799 bfd_byte *extdyn;
3800
3801 bed = get_elf_backend_data (hash_table->dynobj);
3802 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3803 if (sdyn != NULL && sdyn->size != 0)
3804 for (extdyn = sdyn->contents;
3805 extdyn < sdyn->contents + sdyn->size;
3806 extdyn += bed->s->sizeof_dyn)
3807 {
3808 Elf_Internal_Dyn dyn;
3809
3810 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3811 if (dyn.d_tag == DT_NEEDED
3812 && dyn.d_un.d_val == strindex)
3813 {
3814 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3815 return 1;
3816 }
3817 }
3818 }
3819
3820 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3821 return -1;
3822
3823 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3824 return -1;
3825
3826 return 0;
3827 }
3828
3829 /* Return true if SONAME is on the needed list between NEEDED and STOP
3830 (or the end of list if STOP is NULL), and needed by a library that
3831 will be loaded. */
3832
3833 static bool
3834 on_needed_list (const char *soname,
3835 struct bfd_link_needed_list *needed,
3836 struct bfd_link_needed_list *stop)
3837 {
3838 struct bfd_link_needed_list *look;
3839 for (look = needed; look != stop; look = look->next)
3840 if (strcmp (soname, look->name) == 0
3841 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3842 /* If needed by a library that itself is not directly
3843 needed, recursively check whether that library is
3844 indirectly needed. Since we add DT_NEEDED entries to
3845 the end of the list, library dependencies appear after
3846 the library. Therefore search prior to the current
3847 LOOK, preventing possible infinite recursion. */
3848 || on_needed_list (elf_dt_name (look->by), needed, look)))
3849 return true;
3850
3851 return false;
3852 }
3853
3854 /* Sort symbol by value, section, size, and type. */
3855 static int
3856 elf_sort_symbol (const void *arg1, const void *arg2)
3857 {
3858 const struct elf_link_hash_entry *h1;
3859 const struct elf_link_hash_entry *h2;
3860 bfd_signed_vma vdiff;
3861 int sdiff;
3862 const char *n1;
3863 const char *n2;
3864
3865 h1 = *(const struct elf_link_hash_entry **) arg1;
3866 h2 = *(const struct elf_link_hash_entry **) arg2;
3867 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3868 if (vdiff != 0)
3869 return vdiff > 0 ? 1 : -1;
3870
3871 sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3872 if (sdiff != 0)
3873 return sdiff;
3874
3875 /* Sort so that sized symbols are selected over zero size symbols. */
3876 vdiff = h1->size - h2->size;
3877 if (vdiff != 0)
3878 return vdiff > 0 ? 1 : -1;
3879
3880 /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */
3881 if (h1->type != h2->type)
3882 return h1->type - h2->type;
3883
3884 /* If symbols are properly sized and typed, and multiple strong
3885 aliases are not defined in a shared library by the user we
3886 shouldn't get here. Unfortunately linker script symbols like
3887 __bss_start sometimes match a user symbol defined at the start of
3888 .bss without proper size and type. We'd like to preference the
3889 user symbol over reserved system symbols. Sort on leading
3890 underscores. */
3891 n1 = h1->root.root.string;
3892 n2 = h2->root.root.string;
3893 while (*n1 == *n2)
3894 {
3895 if (*n1 == 0)
3896 break;
3897 ++n1;
3898 ++n2;
3899 }
3900 if (*n1 == '_')
3901 return -1;
3902 if (*n2 == '_')
3903 return 1;
3904
3905 /* Final sort on name selects user symbols like '_u' over reserved
3906 system symbols like '_Z' and also will avoid qsort instability. */
3907 return *n1 - *n2;
3908 }
3909
3910 /* This function is used to adjust offsets into .dynstr for
3911 dynamic symbols. This is called via elf_link_hash_traverse. */
3912
3913 static bool
3914 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3915 {
3916 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3917
3918 if (h->dynindx != -1)
3919 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3920 return true;
3921 }
3922
3923 /* Assign string offsets in .dynstr, update all structures referencing
3924 them. */
3925
3926 static bool
3927 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3928 {
3929 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3930 struct elf_link_local_dynamic_entry *entry;
3931 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3932 bfd *dynobj = hash_table->dynobj;
3933 asection *sdyn;
3934 bfd_size_type size;
3935 const struct elf_backend_data *bed;
3936 bfd_byte *extdyn;
3937
3938 _bfd_elf_strtab_finalize (dynstr);
3939 size = _bfd_elf_strtab_size (dynstr);
3940
3941 /* Allow the linker to examine the dynsymtab now it's fully populated. */
3942
3943 if (info->callbacks->examine_strtab)
3944 info->callbacks->examine_strtab (dynstr);
3945
3946 bed = get_elf_backend_data (dynobj);
3947 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3948 BFD_ASSERT (sdyn != NULL);
3949
3950 /* Update all .dynamic entries referencing .dynstr strings. */
3951 for (extdyn = sdyn->contents;
3952 extdyn < PTR_ADD (sdyn->contents, sdyn->size);
3953 extdyn += bed->s->sizeof_dyn)
3954 {
3955 Elf_Internal_Dyn dyn;
3956
3957 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3958 switch (dyn.d_tag)
3959 {
3960 case DT_STRSZ:
3961 dyn.d_un.d_val = size;
3962 break;
3963 case DT_NEEDED:
3964 case DT_SONAME:
3965 case DT_RPATH:
3966 case DT_RUNPATH:
3967 case DT_FILTER:
3968 case DT_AUXILIARY:
3969 case DT_AUDIT:
3970 case DT_DEPAUDIT:
3971 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3972 break;
3973 default:
3974 continue;
3975 }
3976 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3977 }
3978
3979 /* Now update local dynamic symbols. */
3980 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3981 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3982 entry->isym.st_name);
3983
3984 /* And the rest of dynamic symbols. */
3985 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3986
3987 /* Adjust version definitions. */
3988 if (elf_tdata (output_bfd)->cverdefs)
3989 {
3990 asection *s;
3991 bfd_byte *p;
3992 size_t i;
3993 Elf_Internal_Verdef def;
3994 Elf_Internal_Verdaux defaux;
3995
3996 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3997 p = s->contents;
3998 do
3999 {
4000 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
4001 &def);
4002 p += sizeof (Elf_External_Verdef);
4003 if (def.vd_aux != sizeof (Elf_External_Verdef))
4004 continue;
4005 for (i = 0; i < def.vd_cnt; ++i)
4006 {
4007 _bfd_elf_swap_verdaux_in (output_bfd,
4008 (Elf_External_Verdaux *) p, &defaux);
4009 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
4010 defaux.vda_name);
4011 _bfd_elf_swap_verdaux_out (output_bfd,
4012 &defaux, (Elf_External_Verdaux *) p);
4013 p += sizeof (Elf_External_Verdaux);
4014 }
4015 }
4016 while (def.vd_next);
4017 }
4018
4019 /* Adjust version references. */
4020 if (elf_tdata (output_bfd)->verref)
4021 {
4022 asection *s;
4023 bfd_byte *p;
4024 size_t i;
4025 Elf_Internal_Verneed need;
4026 Elf_Internal_Vernaux needaux;
4027
4028 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
4029 p = s->contents;
4030 do
4031 {
4032 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
4033 &need);
4034 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
4035 _bfd_elf_swap_verneed_out (output_bfd, &need,
4036 (Elf_External_Verneed *) p);
4037 p += sizeof (Elf_External_Verneed);
4038 for (i = 0; i < need.vn_cnt; ++i)
4039 {
4040 _bfd_elf_swap_vernaux_in (output_bfd,
4041 (Elf_External_Vernaux *) p, &needaux);
4042 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
4043 needaux.vna_name);
4044 _bfd_elf_swap_vernaux_out (output_bfd,
4045 &needaux,
4046 (Elf_External_Vernaux *) p);
4047 p += sizeof (Elf_External_Vernaux);
4048 }
4049 }
4050 while (need.vn_next);
4051 }
4052
4053 return true;
4054 }
4055 \f
4056 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4057 The default is to only match when the INPUT and OUTPUT are exactly
4058 the same target. */
4059
4060 bool
4061 _bfd_elf_default_relocs_compatible (const bfd_target *input,
4062 const bfd_target *output)
4063 {
4064 return input == output;
4065 }
4066
4067 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4068 This version is used when different targets for the same architecture
4069 are virtually identical. */
4070
4071 bool
4072 _bfd_elf_relocs_compatible (const bfd_target *input,
4073 const bfd_target *output)
4074 {
4075 const struct elf_backend_data *obed, *ibed;
4076
4077 if (input == output)
4078 return true;
4079
4080 ibed = xvec_get_elf_backend_data (input);
4081 obed = xvec_get_elf_backend_data (output);
4082
4083 if (ibed->arch != obed->arch)
4084 return false;
4085
4086 /* If both backends are using this function, deem them compatible. */
4087 return ibed->relocs_compatible == obed->relocs_compatible;
4088 }
4089
4090 /* Make a special call to the linker "notice" function to tell it that
4091 we are about to handle an as-needed lib, or have finished
4092 processing the lib. */
4093
4094 bool
4095 _bfd_elf_notice_as_needed (bfd *ibfd,
4096 struct bfd_link_info *info,
4097 enum notice_asneeded_action act)
4098 {
4099 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
4100 }
4101
4102 /* Call ACTION on each relocation in an ELF object file. */
4103
4104 bool
4105 _bfd_elf_link_iterate_on_relocs
4106 (bfd *abfd, struct bfd_link_info *info,
4107 bool (*action) (bfd *, struct bfd_link_info *, asection *,
4108 const Elf_Internal_Rela *))
4109 {
4110 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4111 struct elf_link_hash_table *htab = elf_hash_table (info);
4112
4113 /* If this object is the same format as the output object, and it is
4114 not a shared library, then let the backend look through the
4115 relocs.
4116
4117 This is required to build global offset table entries and to
4118 arrange for dynamic relocs. It is not required for the
4119 particular common case of linking non PIC code, even when linking
4120 against shared libraries, but unfortunately there is no way of
4121 knowing whether an object file has been compiled PIC or not.
4122 Looking through the relocs is not particularly time consuming.
4123 The problem is that we must either (1) keep the relocs in memory,
4124 which causes the linker to require additional runtime memory or
4125 (2) read the relocs twice from the input file, which wastes time.
4126 This would be a good case for using mmap.
4127
4128 I have no idea how to handle linking PIC code into a file of a
4129 different format. It probably can't be done. */
4130 if ((abfd->flags & DYNAMIC) == 0
4131 && is_elf_hash_table (&htab->root)
4132 && elf_object_id (abfd) == elf_hash_table_id (htab)
4133 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4134 {
4135 asection *o;
4136
4137 for (o = abfd->sections; o != NULL; o = o->next)
4138 {
4139 Elf_Internal_Rela *internal_relocs;
4140 bool ok;
4141
4142 /* Don't check relocations in excluded sections. Don't do
4143 anything special with non-loaded, non-alloced sections.
4144 In particular, any relocs in such sections should not
4145 affect GOT and PLT reference counting (ie. we don't
4146 allow them to create GOT or PLT entries), there's no
4147 possibility or desire to optimize TLS relocs, and
4148 there's not much point in propagating relocs to shared
4149 libs that the dynamic linker won't relocate. */
4150 if ((o->flags & SEC_ALLOC) == 0
4151 || (o->flags & SEC_RELOC) == 0
4152 || (o->flags & SEC_EXCLUDE) != 0
4153 || o->reloc_count == 0
4154 || ((info->strip == strip_all || info->strip == strip_debugger)
4155 && (o->flags & SEC_DEBUGGING) != 0)
4156 || bfd_is_abs_section (o->output_section))
4157 continue;
4158
4159 internal_relocs = _bfd_elf_link_info_read_relocs (abfd, info,
4160 o, NULL,
4161 NULL,
4162 _bfd_link_keep_memory (info));
4163 if (internal_relocs == NULL)
4164 return false;
4165
4166 ok = action (abfd, info, o, internal_relocs);
4167
4168 if (elf_section_data (o)->relocs != internal_relocs)
4169 free (internal_relocs);
4170
4171 if (! ok)
4172 return false;
4173 }
4174 }
4175
4176 return true;
4177 }
4178
4179 /* Check relocations in an ELF object file. This is called after
4180 all input files have been opened. */
4181
4182 bool
4183 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
4184 {
4185 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4186 if (bed->check_relocs != NULL)
4187 return _bfd_elf_link_iterate_on_relocs (abfd, info,
4188 bed->check_relocs);
4189 return true;
4190 }
4191
4192 /* Add symbols from an ELF object file to the linker hash table. */
4193
4194 static bool
4195 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
4196 {
4197 Elf_Internal_Ehdr *ehdr;
4198 Elf_Internal_Shdr *hdr;
4199 size_t symcount;
4200 size_t extsymcount;
4201 size_t extsymoff;
4202 struct elf_link_hash_entry **sym_hash;
4203 bool dynamic;
4204 Elf_External_Versym *extversym = NULL;
4205 Elf_External_Versym *extversym_end = NULL;
4206 Elf_External_Versym *ever;
4207 struct elf_link_hash_entry *weaks;
4208 struct elf_link_hash_entry **nondeflt_vers = NULL;
4209 size_t nondeflt_vers_cnt = 0;
4210 Elf_Internal_Sym *isymbuf = NULL;
4211 Elf_Internal_Sym *isym;
4212 Elf_Internal_Sym *isymend;
4213 const struct elf_backend_data *bed;
4214 bool add_needed;
4215 struct elf_link_hash_table *htab;
4216 void *alloc_mark = NULL;
4217 struct bfd_hash_entry **old_table = NULL;
4218 unsigned int old_size = 0;
4219 unsigned int old_count = 0;
4220 void *old_tab = NULL;
4221 void *old_ent;
4222 struct bfd_link_hash_entry *old_undefs = NULL;
4223 struct bfd_link_hash_entry *old_undefs_tail = NULL;
4224 void *old_strtab = NULL;
4225 size_t tabsize = 0;
4226 asection *s;
4227 bool just_syms;
4228
4229 htab = elf_hash_table (info);
4230 bed = get_elf_backend_data (abfd);
4231
4232 if ((abfd->flags & DYNAMIC) == 0)
4233 dynamic = false;
4234 else
4235 {
4236 dynamic = true;
4237
4238 /* You can't use -r against a dynamic object. Also, there's no
4239 hope of using a dynamic object which does not exactly match
4240 the format of the output file. */
4241 if (bfd_link_relocatable (info)
4242 || !is_elf_hash_table (&htab->root)
4243 || info->output_bfd->xvec != abfd->xvec)
4244 {
4245 if (bfd_link_relocatable (info))
4246 bfd_set_error (bfd_error_invalid_operation);
4247 else
4248 bfd_set_error (bfd_error_wrong_format);
4249 goto error_return;
4250 }
4251 }
4252
4253 ehdr = elf_elfheader (abfd);
4254 if (info->warn_alternate_em
4255 && bed->elf_machine_code != ehdr->e_machine
4256 && ((bed->elf_machine_alt1 != 0
4257 && ehdr->e_machine == bed->elf_machine_alt1)
4258 || (bed->elf_machine_alt2 != 0
4259 && ehdr->e_machine == bed->elf_machine_alt2)))
4260 _bfd_error_handler
4261 /* xgettext:c-format */
4262 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
4263 ehdr->e_machine, abfd, bed->elf_machine_code);
4264
4265 /* As a GNU extension, any input sections which are named
4266 .gnu.warning.SYMBOL are treated as warning symbols for the given
4267 symbol. This differs from .gnu.warning sections, which generate
4268 warnings when they are included in an output file. */
4269 /* PR 12761: Also generate this warning when building shared libraries. */
4270 for (s = abfd->sections; s != NULL; s = s->next)
4271 {
4272 const char *name;
4273
4274 name = bfd_section_name (s);
4275 if (startswith (name, ".gnu.warning."))
4276 {
4277 char *msg;
4278 bfd_size_type sz;
4279
4280 name += sizeof ".gnu.warning." - 1;
4281
4282 /* If this is a shared object, then look up the symbol
4283 in the hash table. If it is there, and it is already
4284 been defined, then we will not be using the entry
4285 from this shared object, so we don't need to warn.
4286 FIXME: If we see the definition in a regular object
4287 later on, we will warn, but we shouldn't. The only
4288 fix is to keep track of what warnings we are supposed
4289 to emit, and then handle them all at the end of the
4290 link. */
4291 if (dynamic)
4292 {
4293 struct elf_link_hash_entry *h;
4294
4295 h = elf_link_hash_lookup (htab, name, false, false, true);
4296
4297 /* FIXME: What about bfd_link_hash_common? */
4298 if (h != NULL
4299 && (h->root.type == bfd_link_hash_defined
4300 || h->root.type == bfd_link_hash_defweak))
4301 continue;
4302 }
4303
4304 sz = s->size;
4305 msg = (char *) bfd_alloc (abfd, sz + 1);
4306 if (msg == NULL)
4307 goto error_return;
4308
4309 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
4310 goto error_return;
4311
4312 msg[sz] = '\0';
4313
4314 if (! (_bfd_generic_link_add_one_symbol
4315 (info, abfd, name, BSF_WARNING, s, 0, msg,
4316 false, bed->collect, NULL)))
4317 goto error_return;
4318
4319 if (bfd_link_executable (info))
4320 {
4321 /* Clobber the section size so that the warning does
4322 not get copied into the output file. */
4323 s->size = 0;
4324
4325 /* Also set SEC_EXCLUDE, so that symbols defined in
4326 the warning section don't get copied to the output. */
4327 s->flags |= SEC_EXCLUDE;
4328 }
4329 }
4330 }
4331
4332 just_syms = ((s = abfd->sections) != NULL
4333 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
4334
4335 add_needed = true;
4336 if (! dynamic)
4337 {
4338 /* If we are creating a shared library, create all the dynamic
4339 sections immediately. We need to attach them to something,
4340 so we attach them to this BFD, provided it is the right
4341 format and is not from ld --just-symbols. Always create the
4342 dynamic sections for -E/--dynamic-list. FIXME: If there
4343 are no input BFD's of the same format as the output, we can't
4344 make a shared library. */
4345 if (!just_syms
4346 && (bfd_link_pic (info)
4347 || (!bfd_link_relocatable (info)
4348 && info->nointerp
4349 && (info->export_dynamic || info->dynamic)))
4350 && is_elf_hash_table (&htab->root)
4351 && info->output_bfd->xvec == abfd->xvec
4352 && !htab->dynamic_sections_created)
4353 {
4354 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4355 goto error_return;
4356 }
4357 }
4358 else if (!is_elf_hash_table (&htab->root))
4359 goto error_return;
4360 else
4361 {
4362 const char *soname = NULL;
4363 char *audit = NULL;
4364 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
4365 const Elf_Internal_Phdr *phdr;
4366 struct elf_link_loaded_list *loaded_lib;
4367
4368 /* ld --just-symbols and dynamic objects don't mix very well.
4369 ld shouldn't allow it. */
4370 if (just_syms)
4371 abort ();
4372
4373 /* If this dynamic lib was specified on the command line with
4374 --as-needed in effect, then we don't want to add a DT_NEEDED
4375 tag unless the lib is actually used. Similary for libs brought
4376 in by another lib's DT_NEEDED. When --no-add-needed is used
4377 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4378 any dynamic library in DT_NEEDED tags in the dynamic lib at
4379 all. */
4380 add_needed = (elf_dyn_lib_class (abfd)
4381 & (DYN_AS_NEEDED | DYN_DT_NEEDED
4382 | DYN_NO_NEEDED)) == 0;
4383
4384 s = bfd_get_section_by_name (abfd, ".dynamic");
4385 if (s != NULL && s->size != 0)
4386 {
4387 bfd_byte *dynbuf;
4388 bfd_byte *extdyn;
4389 unsigned int elfsec;
4390 unsigned long shlink;
4391
4392 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
4393 {
4394 error_free_dyn:
4395 free (dynbuf);
4396 goto error_return;
4397 }
4398
4399 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
4400 if (elfsec == SHN_BAD)
4401 goto error_free_dyn;
4402 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4403
4404 for (extdyn = dynbuf;
4405 extdyn <= dynbuf + s->size - bed->s->sizeof_dyn;
4406 extdyn += bed->s->sizeof_dyn)
4407 {
4408 Elf_Internal_Dyn dyn;
4409
4410 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4411 if (dyn.d_tag == DT_SONAME)
4412 {
4413 unsigned int tagv = dyn.d_un.d_val;
4414 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4415 if (soname == NULL)
4416 goto error_free_dyn;
4417 }
4418 if (dyn.d_tag == DT_NEEDED)
4419 {
4420 struct bfd_link_needed_list *n, **pn;
4421 char *fnm, *anm;
4422 unsigned int tagv = dyn.d_un.d_val;
4423 size_t amt = sizeof (struct bfd_link_needed_list);
4424
4425 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4426 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4427 if (n == NULL || fnm == NULL)
4428 goto error_free_dyn;
4429 amt = strlen (fnm) + 1;
4430 anm = (char *) bfd_alloc (abfd, amt);
4431 if (anm == NULL)
4432 goto error_free_dyn;
4433 memcpy (anm, fnm, amt);
4434 n->name = anm;
4435 n->by = abfd;
4436 n->next = NULL;
4437 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4438 ;
4439 *pn = n;
4440 }
4441 if (dyn.d_tag == DT_RUNPATH)
4442 {
4443 struct bfd_link_needed_list *n, **pn;
4444 char *fnm, *anm;
4445 unsigned int tagv = dyn.d_un.d_val;
4446 size_t amt = sizeof (struct bfd_link_needed_list);
4447
4448 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4449 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4450 if (n == NULL || fnm == NULL)
4451 goto error_free_dyn;
4452 amt = strlen (fnm) + 1;
4453 anm = (char *) bfd_alloc (abfd, amt);
4454 if (anm == NULL)
4455 goto error_free_dyn;
4456 memcpy (anm, fnm, amt);
4457 n->name = anm;
4458 n->by = abfd;
4459 n->next = NULL;
4460 for (pn = & runpath;
4461 *pn != NULL;
4462 pn = &(*pn)->next)
4463 ;
4464 *pn = n;
4465 }
4466 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4467 if (!runpath && dyn.d_tag == DT_RPATH)
4468 {
4469 struct bfd_link_needed_list *n, **pn;
4470 char *fnm, *anm;
4471 unsigned int tagv = dyn.d_un.d_val;
4472 size_t amt = sizeof (struct bfd_link_needed_list);
4473
4474 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4475 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4476 if (n == NULL || fnm == NULL)
4477 goto error_free_dyn;
4478 amt = strlen (fnm) + 1;
4479 anm = (char *) bfd_alloc (abfd, amt);
4480 if (anm == NULL)
4481 goto error_free_dyn;
4482 memcpy (anm, fnm, amt);
4483 n->name = anm;
4484 n->by = abfd;
4485 n->next = NULL;
4486 for (pn = & rpath;
4487 *pn != NULL;
4488 pn = &(*pn)->next)
4489 ;
4490 *pn = n;
4491 }
4492 if (dyn.d_tag == DT_AUDIT)
4493 {
4494 unsigned int tagv = dyn.d_un.d_val;
4495 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4496 }
4497 if (dyn.d_tag == DT_FLAGS_1)
4498 elf_tdata (abfd)->is_pie = (dyn.d_un.d_val & DF_1_PIE) != 0;
4499 }
4500
4501 free (dynbuf);
4502 }
4503
4504 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4505 frees all more recently bfd_alloc'd blocks as well. */
4506 if (runpath)
4507 rpath = runpath;
4508
4509 if (rpath)
4510 {
4511 struct bfd_link_needed_list **pn;
4512 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4513 ;
4514 *pn = rpath;
4515 }
4516
4517 /* If we have a PT_GNU_RELRO program header, mark as read-only
4518 all sections contained fully therein. This makes relro
4519 shared library sections appear as they will at run-time. */
4520 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4521 while (phdr-- > elf_tdata (abfd)->phdr)
4522 if (phdr->p_type == PT_GNU_RELRO)
4523 {
4524 for (s = abfd->sections; s != NULL; s = s->next)
4525 {
4526 unsigned int opb = bfd_octets_per_byte (abfd, s);
4527
4528 if ((s->flags & SEC_ALLOC) != 0
4529 && s->vma * opb >= phdr->p_vaddr
4530 && s->vma * opb + s->size <= phdr->p_vaddr + phdr->p_memsz)
4531 s->flags |= SEC_READONLY;
4532 }
4533 break;
4534 }
4535
4536 /* We do not want to include any of the sections in a dynamic
4537 object in the output file. We hack by simply clobbering the
4538 list of sections in the BFD. This could be handled more
4539 cleanly by, say, a new section flag; the existing
4540 SEC_NEVER_LOAD flag is not the one we want, because that one
4541 still implies that the section takes up space in the output
4542 file. */
4543 bfd_section_list_clear (abfd);
4544
4545 /* Find the name to use in a DT_NEEDED entry that refers to this
4546 object. If the object has a DT_SONAME entry, we use it.
4547 Otherwise, if the generic linker stuck something in
4548 elf_dt_name, we use that. Otherwise, we just use the file
4549 name. */
4550 if (soname == NULL || *soname == '\0')
4551 {
4552 soname = elf_dt_name (abfd);
4553 if (soname == NULL || *soname == '\0')
4554 soname = bfd_get_filename (abfd);
4555 }
4556
4557 /* Save the SONAME because sometimes the linker emulation code
4558 will need to know it. */
4559 elf_dt_name (abfd) = soname;
4560
4561 /* If we have already included this dynamic object in the
4562 link, just ignore it. There is no reason to include a
4563 particular dynamic object more than once. */
4564 for (loaded_lib = htab->dyn_loaded;
4565 loaded_lib != NULL;
4566 loaded_lib = loaded_lib->next)
4567 {
4568 if (strcmp (elf_dt_name (loaded_lib->abfd), soname) == 0)
4569 return true;
4570 }
4571
4572 /* Create dynamic sections for backends that require that be done
4573 before setup_gnu_properties. */
4574 if (add_needed
4575 && !_bfd_elf_link_create_dynamic_sections (abfd, info))
4576 return false;
4577
4578 /* Save the DT_AUDIT entry for the linker emulation code. */
4579 elf_dt_audit (abfd) = audit;
4580 }
4581
4582 /* If this is a dynamic object, we always link against the .dynsym
4583 symbol table, not the .symtab symbol table. The dynamic linker
4584 will only see the .dynsym symbol table, so there is no reason to
4585 look at .symtab for a dynamic object. */
4586
4587 if (! dynamic || elf_dynsymtab (abfd) == 0)
4588 hdr = &elf_tdata (abfd)->symtab_hdr;
4589 else
4590 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4591
4592 symcount = hdr->sh_size / bed->s->sizeof_sym;
4593
4594 /* The sh_info field of the symtab header tells us where the
4595 external symbols start. We don't care about the local symbols at
4596 this point. */
4597 if (elf_bad_symtab (abfd))
4598 {
4599 extsymcount = symcount;
4600 extsymoff = 0;
4601 }
4602 else
4603 {
4604 extsymcount = symcount - hdr->sh_info;
4605 extsymoff = hdr->sh_info;
4606 }
4607
4608 sym_hash = elf_sym_hashes (abfd);
4609 if (extsymcount != 0)
4610 {
4611 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4612 NULL, NULL, NULL);
4613 if (isymbuf == NULL)
4614 goto error_return;
4615
4616 if (sym_hash == NULL)
4617 {
4618 /* We store a pointer to the hash table entry for each
4619 external symbol. */
4620 size_t amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4621 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4622 if (sym_hash == NULL)
4623 goto error_free_sym;
4624 elf_sym_hashes (abfd) = sym_hash;
4625 }
4626 }
4627
4628 if (dynamic)
4629 {
4630 /* Read in any version definitions. */
4631 if (!_bfd_elf_slurp_version_tables (abfd,
4632 info->default_imported_symver))
4633 goto error_free_sym;
4634
4635 /* Read in the symbol versions, but don't bother to convert them
4636 to internal format. */
4637 if (elf_dynversym (abfd) != 0)
4638 {
4639 Elf_Internal_Shdr *versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4640 bfd_size_type amt = versymhdr->sh_size;
4641
4642 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0)
4643 goto error_free_sym;
4644 extversym = (Elf_External_Versym *)
4645 _bfd_malloc_and_read (abfd, amt, amt);
4646 if (extversym == NULL)
4647 goto error_free_sym;
4648 extversym_end = extversym + amt / sizeof (*extversym);
4649 }
4650 }
4651
4652 /* If we are loading an as-needed shared lib, save the symbol table
4653 state before we start adding symbols. If the lib turns out
4654 to be unneeded, restore the state. */
4655 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4656 {
4657 unsigned int i;
4658 size_t entsize;
4659
4660 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4661 {
4662 struct bfd_hash_entry *p;
4663 struct elf_link_hash_entry *h;
4664
4665 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4666 {
4667 h = (struct elf_link_hash_entry *) p;
4668 entsize += htab->root.table.entsize;
4669 if (h->root.type == bfd_link_hash_warning)
4670 {
4671 entsize += htab->root.table.entsize;
4672 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4673 }
4674 if (h->root.type == bfd_link_hash_common)
4675 entsize += sizeof (*h->root.u.c.p);
4676 }
4677 }
4678
4679 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4680 old_tab = bfd_malloc (tabsize + entsize);
4681 if (old_tab == NULL)
4682 goto error_free_vers;
4683
4684 /* Remember the current objalloc pointer, so that all mem for
4685 symbols added can later be reclaimed. */
4686 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4687 if (alloc_mark == NULL)
4688 goto error_free_vers;
4689
4690 /* Make a special call to the linker "notice" function to
4691 tell it that we are about to handle an as-needed lib. */
4692 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4693 goto error_free_vers;
4694
4695 /* Clone the symbol table. Remember some pointers into the
4696 symbol table, and dynamic symbol count. */
4697 old_ent = (char *) old_tab + tabsize;
4698 memcpy (old_tab, htab->root.table.table, tabsize);
4699 old_undefs = htab->root.undefs;
4700 old_undefs_tail = htab->root.undefs_tail;
4701 old_table = htab->root.table.table;
4702 old_size = htab->root.table.size;
4703 old_count = htab->root.table.count;
4704 old_strtab = NULL;
4705 if (htab->dynstr != NULL)
4706 {
4707 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4708 if (old_strtab == NULL)
4709 goto error_free_vers;
4710 }
4711
4712 for (i = 0; i < htab->root.table.size; i++)
4713 {
4714 struct bfd_hash_entry *p;
4715 struct elf_link_hash_entry *h;
4716
4717 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4718 {
4719 h = (struct elf_link_hash_entry *) p;
4720 memcpy (old_ent, h, htab->root.table.entsize);
4721 old_ent = (char *) old_ent + htab->root.table.entsize;
4722 if (h->root.type == bfd_link_hash_warning)
4723 {
4724 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4725 memcpy (old_ent, h, htab->root.table.entsize);
4726 old_ent = (char *) old_ent + htab->root.table.entsize;
4727 }
4728 if (h->root.type == bfd_link_hash_common)
4729 {
4730 memcpy (old_ent, h->root.u.c.p, sizeof (*h->root.u.c.p));
4731 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
4732 }
4733 }
4734 }
4735 }
4736
4737 weaks = NULL;
4738 if (extversym == NULL)
4739 ever = NULL;
4740 else if (extversym + extsymoff < extversym_end)
4741 ever = extversym + extsymoff;
4742 else
4743 {
4744 /* xgettext:c-format */
4745 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4746 abfd, (long) extsymoff,
4747 (long) (extversym_end - extversym) / sizeof (* extversym));
4748 bfd_set_error (bfd_error_bad_value);
4749 goto error_free_vers;
4750 }
4751
4752 if (!bfd_link_relocatable (info)
4753 && abfd->lto_slim_object)
4754 {
4755 _bfd_error_handler
4756 (_("%pB: plugin needed to handle lto object"), abfd);
4757 }
4758
4759 for (isym = isymbuf, isymend = PTR_ADD (isymbuf, extsymcount);
4760 isym < isymend;
4761 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4762 {
4763 int bind;
4764 bfd_vma value;
4765 asection *sec, *new_sec;
4766 flagword flags;
4767 const char *name;
4768 struct elf_link_hash_entry *h;
4769 struct elf_link_hash_entry *hi;
4770 bool definition;
4771 bool size_change_ok;
4772 bool type_change_ok;
4773 bool new_weak;
4774 bool old_weak;
4775 bfd *override;
4776 bool common;
4777 bool discarded;
4778 unsigned int old_alignment;
4779 unsigned int shindex;
4780 bfd *old_bfd;
4781 bool matched;
4782
4783 override = NULL;
4784
4785 flags = BSF_NO_FLAGS;
4786 sec = NULL;
4787 value = isym->st_value;
4788 common = bed->common_definition (isym);
4789 if (common && info->inhibit_common_definition)
4790 {
4791 /* Treat common symbol as undefined for --no-define-common. */
4792 isym->st_shndx = SHN_UNDEF;
4793 common = false;
4794 }
4795 discarded = false;
4796
4797 bind = ELF_ST_BIND (isym->st_info);
4798 switch (bind)
4799 {
4800 case STB_LOCAL:
4801 /* This should be impossible, since ELF requires that all
4802 global symbols follow all local symbols, and that sh_info
4803 point to the first global symbol. Unfortunately, Irix 5
4804 screws this up. */
4805 if (elf_bad_symtab (abfd))
4806 continue;
4807
4808 /* If we aren't prepared to handle locals within the globals
4809 then we'll likely segfault on a NULL symbol hash if the
4810 symbol is ever referenced in relocations. */
4811 shindex = elf_elfheader (abfd)->e_shstrndx;
4812 name = bfd_elf_string_from_elf_section (abfd, shindex, hdr->sh_name);
4813 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4814 " (>= sh_info of %lu)"),
4815 abfd, name, (long) (isym - isymbuf + extsymoff),
4816 (long) extsymoff);
4817
4818 /* Dynamic object relocations are not processed by ld, so
4819 ld won't run into the problem mentioned above. */
4820 if (dynamic)
4821 continue;
4822 bfd_set_error (bfd_error_bad_value);
4823 goto error_free_vers;
4824
4825 case STB_GLOBAL:
4826 if (isym->st_shndx != SHN_UNDEF && !common)
4827 flags = BSF_GLOBAL;
4828 break;
4829
4830 case STB_WEAK:
4831 flags = BSF_WEAK;
4832 break;
4833
4834 case STB_GNU_UNIQUE:
4835 flags = BSF_GNU_UNIQUE;
4836 break;
4837
4838 default:
4839 /* Leave it up to the processor backend. */
4840 break;
4841 }
4842
4843 if (isym->st_shndx == SHN_UNDEF)
4844 sec = bfd_und_section_ptr;
4845 else if (isym->st_shndx == SHN_ABS)
4846 sec = bfd_abs_section_ptr;
4847 else if (isym->st_shndx == SHN_COMMON)
4848 {
4849 sec = bfd_com_section_ptr;
4850 /* What ELF calls the size we call the value. What ELF
4851 calls the value we call the alignment. */
4852 value = isym->st_size;
4853 }
4854 else
4855 {
4856 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4857 if (sec == NULL)
4858 sec = bfd_abs_section_ptr;
4859 else if (discarded_section (sec))
4860 {
4861 /* Symbols from discarded section are undefined. We keep
4862 its visibility. */
4863 sec = bfd_und_section_ptr;
4864 discarded = true;
4865 isym->st_shndx = SHN_UNDEF;
4866 }
4867 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4868 value -= sec->vma;
4869 }
4870
4871 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4872 isym->st_name);
4873 if (name == NULL)
4874 goto error_free_vers;
4875
4876 if (isym->st_shndx == SHN_COMMON
4877 && (abfd->flags & BFD_PLUGIN) != 0)
4878 {
4879 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4880
4881 if (xc == NULL)
4882 {
4883 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4884 | SEC_EXCLUDE);
4885 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4886 if (xc == NULL)
4887 goto error_free_vers;
4888 }
4889 sec = xc;
4890 }
4891 else if (isym->st_shndx == SHN_COMMON
4892 && ELF_ST_TYPE (isym->st_info) == STT_TLS
4893 && !bfd_link_relocatable (info))
4894 {
4895 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4896
4897 if (tcomm == NULL)
4898 {
4899 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4900 | SEC_LINKER_CREATED);
4901 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4902 if (tcomm == NULL)
4903 goto error_free_vers;
4904 }
4905 sec = tcomm;
4906 }
4907 else if (bed->elf_add_symbol_hook)
4908 {
4909 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4910 &sec, &value))
4911 goto error_free_vers;
4912
4913 /* The hook function sets the name to NULL if this symbol
4914 should be skipped for some reason. */
4915 if (name == NULL)
4916 continue;
4917 }
4918
4919 /* Sanity check that all possibilities were handled. */
4920 if (sec == NULL)
4921 abort ();
4922
4923 /* Silently discard TLS symbols from --just-syms. There's
4924 no way to combine a static TLS block with a new TLS block
4925 for this executable. */
4926 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4927 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4928 continue;
4929
4930 if (bfd_is_und_section (sec)
4931 || bfd_is_com_section (sec))
4932 definition = false;
4933 else
4934 definition = true;
4935
4936 size_change_ok = false;
4937 type_change_ok = bed->type_change_ok;
4938 old_weak = false;
4939 matched = false;
4940 old_alignment = 0;
4941 old_bfd = NULL;
4942 new_sec = sec;
4943
4944 if (is_elf_hash_table (&htab->root))
4945 {
4946 Elf_Internal_Versym iver;
4947 unsigned int vernum = 0;
4948 bool skip;
4949
4950 if (ever == NULL)
4951 {
4952 if (info->default_imported_symver)
4953 /* Use the default symbol version created earlier. */
4954 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4955 else
4956 iver.vs_vers = 0;
4957 }
4958 else if (ever >= extversym_end)
4959 {
4960 /* xgettext:c-format */
4961 _bfd_error_handler (_("%pB: not enough version information"),
4962 abfd);
4963 bfd_set_error (bfd_error_bad_value);
4964 goto error_free_vers;
4965 }
4966 else
4967 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4968
4969 vernum = iver.vs_vers & VERSYM_VERSION;
4970
4971 /* If this is a hidden symbol, or if it is not version
4972 1, we append the version name to the symbol name.
4973 However, we do not modify a non-hidden absolute symbol
4974 if it is not a function, because it might be the version
4975 symbol itself. FIXME: What if it isn't? */
4976 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4977 || (vernum > 1
4978 && (!bfd_is_abs_section (sec)
4979 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4980 {
4981 const char *verstr;
4982 size_t namelen, verlen, newlen;
4983 char *newname, *p;
4984
4985 if (isym->st_shndx != SHN_UNDEF)
4986 {
4987 if (vernum > elf_tdata (abfd)->cverdefs)
4988 verstr = NULL;
4989 else if (vernum > 1)
4990 verstr =
4991 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4992 else
4993 verstr = "";
4994
4995 if (verstr == NULL)
4996 {
4997 _bfd_error_handler
4998 /* xgettext:c-format */
4999 (_("%pB: %s: invalid version %u (max %d)"),
5000 abfd, name, vernum,
5001 elf_tdata (abfd)->cverdefs);
5002 bfd_set_error (bfd_error_bad_value);
5003 goto error_free_vers;
5004 }
5005 }
5006 else
5007 {
5008 /* We cannot simply test for the number of
5009 entries in the VERNEED section since the
5010 numbers for the needed versions do not start
5011 at 0. */
5012 Elf_Internal_Verneed *t;
5013
5014 verstr = NULL;
5015 for (t = elf_tdata (abfd)->verref;
5016 t != NULL;
5017 t = t->vn_nextref)
5018 {
5019 Elf_Internal_Vernaux *a;
5020
5021 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5022 {
5023 if (a->vna_other == vernum)
5024 {
5025 verstr = a->vna_nodename;
5026 break;
5027 }
5028 }
5029 if (a != NULL)
5030 break;
5031 }
5032 if (verstr == NULL)
5033 {
5034 _bfd_error_handler
5035 /* xgettext:c-format */
5036 (_("%pB: %s: invalid needed version %d"),
5037 abfd, name, vernum);
5038 bfd_set_error (bfd_error_bad_value);
5039 goto error_free_vers;
5040 }
5041 }
5042
5043 namelen = strlen (name);
5044 verlen = strlen (verstr);
5045 newlen = namelen + verlen + 2;
5046 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
5047 && isym->st_shndx != SHN_UNDEF)
5048 ++newlen;
5049
5050 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
5051 if (newname == NULL)
5052 goto error_free_vers;
5053 memcpy (newname, name, namelen);
5054 p = newname + namelen;
5055 *p++ = ELF_VER_CHR;
5056 /* If this is a defined non-hidden version symbol,
5057 we add another @ to the name. This indicates the
5058 default version of the symbol. */
5059 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
5060 && isym->st_shndx != SHN_UNDEF)
5061 *p++ = ELF_VER_CHR;
5062 memcpy (p, verstr, verlen + 1);
5063
5064 name = newname;
5065 }
5066
5067 /* If this symbol has default visibility and the user has
5068 requested we not re-export it, then mark it as hidden. */
5069 if (!bfd_is_und_section (sec)
5070 && !dynamic
5071 && abfd->no_export
5072 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
5073 isym->st_other = (STV_HIDDEN
5074 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
5075
5076 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
5077 sym_hash, &old_bfd, &old_weak,
5078 &old_alignment, &skip, &override,
5079 &type_change_ok, &size_change_ok,
5080 &matched))
5081 goto error_free_vers;
5082
5083 if (skip)
5084 continue;
5085
5086 /* Override a definition only if the new symbol matches the
5087 existing one. */
5088 if (override && matched)
5089 definition = false;
5090
5091 h = *sym_hash;
5092 while (h->root.type == bfd_link_hash_indirect
5093 || h->root.type == bfd_link_hash_warning)
5094 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5095
5096 if (h->versioned != unversioned
5097 && elf_tdata (abfd)->verdef != NULL
5098 && vernum > 1
5099 && definition)
5100 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
5101 }
5102
5103 if (! (_bfd_generic_link_add_one_symbol
5104 (info, override ? override : abfd, name, flags, sec, value,
5105 NULL, false, bed->collect,
5106 (struct bfd_link_hash_entry **) sym_hash)))
5107 goto error_free_vers;
5108
5109 h = *sym_hash;
5110 /* We need to make sure that indirect symbol dynamic flags are
5111 updated. */
5112 hi = h;
5113 while (h->root.type == bfd_link_hash_indirect
5114 || h->root.type == bfd_link_hash_warning)
5115 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5116
5117 *sym_hash = h;
5118
5119 /* Setting the index to -3 tells elf_link_output_extsym that
5120 this symbol is defined in a discarded section. */
5121 if (discarded && is_elf_hash_table (&htab->root))
5122 h->indx = -3;
5123
5124 new_weak = (flags & BSF_WEAK) != 0;
5125 if (dynamic
5126 && definition
5127 && new_weak
5128 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
5129 && is_elf_hash_table (&htab->root)
5130 && h->u.alias == NULL)
5131 {
5132 /* Keep a list of all weak defined non function symbols from
5133 a dynamic object, using the alias field. Later in this
5134 function we will set the alias field to the correct
5135 value. We only put non-function symbols from dynamic
5136 objects on this list, because that happens to be the only
5137 time we need to know the normal symbol corresponding to a
5138 weak symbol, and the information is time consuming to
5139 figure out. If the alias field is not already NULL,
5140 then this symbol was already defined by some previous
5141 dynamic object, and we will be using that previous
5142 definition anyhow. */
5143
5144 h->u.alias = weaks;
5145 weaks = h;
5146 }
5147
5148 /* Set the alignment of a common symbol. */
5149 if ((common || bfd_is_com_section (sec))
5150 && h->root.type == bfd_link_hash_common)
5151 {
5152 unsigned int align;
5153
5154 if (common)
5155 align = bfd_log2 (isym->st_value);
5156 else
5157 {
5158 /* The new symbol is a common symbol in a shared object.
5159 We need to get the alignment from the section. */
5160 align = new_sec->alignment_power;
5161 }
5162 if (align > old_alignment)
5163 h->root.u.c.p->alignment_power = align;
5164 else
5165 h->root.u.c.p->alignment_power = old_alignment;
5166 }
5167
5168 if (is_elf_hash_table (&htab->root))
5169 {
5170 /* Set a flag in the hash table entry indicating the type of
5171 reference or definition we just found. A dynamic symbol
5172 is one which is referenced or defined by both a regular
5173 object and a shared object. */
5174 bool dynsym = false;
5175
5176 /* Plugin symbols aren't normal. Don't set def/ref flags. */
5177 if ((abfd->flags & BFD_PLUGIN) != 0)
5178 {
5179 /* Except for this flag to track nonweak references. */
5180 if (!definition
5181 && bind != STB_WEAK)
5182 h->ref_ir_nonweak = 1;
5183 }
5184 else if (!dynamic)
5185 {
5186 if (! definition)
5187 {
5188 h->ref_regular = 1;
5189 if (bind != STB_WEAK)
5190 h->ref_regular_nonweak = 1;
5191 }
5192 else
5193 {
5194 h->def_regular = 1;
5195 if (h->def_dynamic)
5196 {
5197 h->def_dynamic = 0;
5198 h->ref_dynamic = 1;
5199 }
5200 }
5201 }
5202 else
5203 {
5204 if (! definition)
5205 {
5206 h->ref_dynamic = 1;
5207 hi->ref_dynamic = 1;
5208 }
5209 else
5210 {
5211 h->def_dynamic = 1;
5212 hi->def_dynamic = 1;
5213 }
5214 }
5215
5216 /* If an indirect symbol has been forced local, don't
5217 make the real symbol dynamic. */
5218 if (h != hi && hi->forced_local)
5219 ;
5220 else if (!dynamic)
5221 {
5222 if (bfd_link_dll (info)
5223 || h->def_dynamic
5224 || h->ref_dynamic)
5225 dynsym = true;
5226 }
5227 else
5228 {
5229 if (h->def_regular
5230 || h->ref_regular
5231 || (h->is_weakalias
5232 && weakdef (h)->dynindx != -1))
5233 dynsym = true;
5234 }
5235
5236 /* Check to see if we need to add an indirect symbol for
5237 the default name. */
5238 if ((definition
5239 || (!override && h->root.type == bfd_link_hash_common))
5240 && !(hi != h
5241 && hi->versioned == versioned_hidden))
5242 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
5243 sec, value, &old_bfd, &dynsym))
5244 goto error_free_vers;
5245
5246 /* Check the alignment when a common symbol is involved. This
5247 can change when a common symbol is overridden by a normal
5248 definition or a common symbol is ignored due to the old
5249 normal definition. We need to make sure the maximum
5250 alignment is maintained. */
5251 if ((old_alignment || common)
5252 && h->root.type != bfd_link_hash_common)
5253 {
5254 unsigned int common_align;
5255 unsigned int normal_align;
5256 unsigned int symbol_align;
5257 bfd *normal_bfd;
5258 bfd *common_bfd;
5259
5260 BFD_ASSERT (h->root.type == bfd_link_hash_defined
5261 || h->root.type == bfd_link_hash_defweak);
5262
5263 symbol_align = ffs (h->root.u.def.value) - 1;
5264 if (h->root.u.def.section->owner != NULL
5265 && (h->root.u.def.section->owner->flags
5266 & (DYNAMIC | BFD_PLUGIN)) == 0)
5267 {
5268 normal_align = h->root.u.def.section->alignment_power;
5269 if (normal_align > symbol_align)
5270 normal_align = symbol_align;
5271 }
5272 else
5273 normal_align = symbol_align;
5274
5275 if (old_alignment)
5276 {
5277 common_align = old_alignment;
5278 common_bfd = old_bfd;
5279 normal_bfd = abfd;
5280 }
5281 else
5282 {
5283 common_align = bfd_log2 (isym->st_value);
5284 common_bfd = abfd;
5285 normal_bfd = old_bfd;
5286 }
5287
5288 if (normal_align < common_align)
5289 {
5290 /* PR binutils/2735 */
5291 if (normal_bfd == NULL)
5292 _bfd_error_handler
5293 /* xgettext:c-format */
5294 (_("warning: alignment %u of common symbol `%s' in %pB is"
5295 " greater than the alignment (%u) of its section %pA"),
5296 1 << common_align, name, common_bfd,
5297 1 << normal_align, h->root.u.def.section);
5298 else
5299 _bfd_error_handler
5300 /* xgettext:c-format */
5301 (_("warning: alignment %u of symbol `%s' in %pB"
5302 " is smaller than %u in %pB"),
5303 1 << normal_align, name, normal_bfd,
5304 1 << common_align, common_bfd);
5305 }
5306 }
5307
5308 /* Remember the symbol size if it isn't undefined. */
5309 if (isym->st_size != 0
5310 && isym->st_shndx != SHN_UNDEF
5311 && (definition || h->size == 0))
5312 {
5313 if (h->size != 0
5314 && h->size != isym->st_size
5315 && ! size_change_ok)
5316 _bfd_error_handler
5317 /* xgettext:c-format */
5318 (_("warning: size of symbol `%s' changed"
5319 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
5320 name, (uint64_t) h->size, old_bfd,
5321 (uint64_t) isym->st_size, abfd);
5322
5323 h->size = isym->st_size;
5324 }
5325
5326 /* If this is a common symbol, then we always want H->SIZE
5327 to be the size of the common symbol. The code just above
5328 won't fix the size if a common symbol becomes larger. We
5329 don't warn about a size change here, because that is
5330 covered by --warn-common. Allow changes between different
5331 function types. */
5332 if (h->root.type == bfd_link_hash_common)
5333 h->size = h->root.u.c.size;
5334
5335 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
5336 && ((definition && !new_weak)
5337 || (old_weak && h->root.type == bfd_link_hash_common)
5338 || h->type == STT_NOTYPE))
5339 {
5340 unsigned int type = ELF_ST_TYPE (isym->st_info);
5341
5342 /* Turn an IFUNC symbol from a DSO into a normal FUNC
5343 symbol. */
5344 if (type == STT_GNU_IFUNC
5345 && (abfd->flags & DYNAMIC) != 0)
5346 type = STT_FUNC;
5347
5348 if (h->type != type)
5349 {
5350 if (h->type != STT_NOTYPE && ! type_change_ok)
5351 /* xgettext:c-format */
5352 _bfd_error_handler
5353 (_("warning: type of symbol `%s' changed"
5354 " from %d to %d in %pB"),
5355 name, h->type, type, abfd);
5356
5357 h->type = type;
5358 }
5359 }
5360
5361 /* Merge st_other field. */
5362 elf_merge_st_other (abfd, h, isym->st_other, sec,
5363 definition, dynamic);
5364
5365 /* We don't want to make debug symbol dynamic. */
5366 if (definition
5367 && (sec->flags & SEC_DEBUGGING)
5368 && !bfd_link_relocatable (info))
5369 dynsym = false;
5370
5371 /* Nor should we make plugin symbols dynamic. */
5372 if ((abfd->flags & BFD_PLUGIN) != 0)
5373 dynsym = false;
5374
5375 if (definition)
5376 {
5377 h->target_internal = isym->st_target_internal;
5378 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5379 }
5380
5381 if (definition && !dynamic)
5382 {
5383 char *p = strchr (name, ELF_VER_CHR);
5384 if (p != NULL && p[1] != ELF_VER_CHR)
5385 {
5386 /* Queue non-default versions so that .symver x, x@FOO
5387 aliases can be checked. */
5388 if (!nondeflt_vers)
5389 {
5390 size_t amt = ((isymend - isym + 1)
5391 * sizeof (struct elf_link_hash_entry *));
5392 nondeflt_vers
5393 = (struct elf_link_hash_entry **) bfd_malloc (amt);
5394 if (!nondeflt_vers)
5395 goto error_free_vers;
5396 }
5397 nondeflt_vers[nondeflt_vers_cnt++] = h;
5398 }
5399 }
5400
5401 if (dynsym && h->dynindx == -1)
5402 {
5403 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5404 goto error_free_vers;
5405 if (h->is_weakalias
5406 && weakdef (h)->dynindx == -1)
5407 {
5408 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
5409 goto error_free_vers;
5410 }
5411 }
5412 else if (h->dynindx != -1)
5413 /* If the symbol already has a dynamic index, but
5414 visibility says it should not be visible, turn it into
5415 a local symbol. */
5416 switch (ELF_ST_VISIBILITY (h->other))
5417 {
5418 case STV_INTERNAL:
5419 case STV_HIDDEN:
5420 (*bed->elf_backend_hide_symbol) (info, h, true);
5421 dynsym = false;
5422 break;
5423 }
5424
5425 if (!add_needed
5426 && matched
5427 && definition
5428 && h->root.type != bfd_link_hash_indirect
5429 && ((dynsym
5430 && h->ref_regular_nonweak)
5431 || (old_bfd != NULL
5432 && (old_bfd->flags & BFD_PLUGIN) != 0
5433 && h->ref_ir_nonweak
5434 && !info->lto_all_symbols_read)
5435 || (h->ref_dynamic_nonweak
5436 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5437 && !on_needed_list (elf_dt_name (abfd),
5438 htab->needed, NULL))))
5439 {
5440 const char *soname = elf_dt_name (abfd);
5441
5442 info->callbacks->minfo ("%!", soname, old_bfd,
5443 h->root.root.string);
5444
5445 /* A symbol from a library loaded via DT_NEEDED of some
5446 other library is referenced by a regular object.
5447 Add a DT_NEEDED entry for it. Issue an error if
5448 --no-add-needed is used and the reference was not
5449 a weak one. */
5450 if (old_bfd != NULL
5451 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
5452 {
5453 _bfd_error_handler
5454 /* xgettext:c-format */
5455 (_("%pB: undefined reference to symbol '%s'"),
5456 old_bfd, name);
5457 bfd_set_error (bfd_error_missing_dso);
5458 goto error_free_vers;
5459 }
5460
5461 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
5462 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
5463
5464 /* Create dynamic sections for backends that require
5465 that be done before setup_gnu_properties. */
5466 if (!_bfd_elf_link_create_dynamic_sections (abfd, info))
5467 return false;
5468 add_needed = true;
5469 }
5470 }
5471 }
5472
5473 if (info->lto_plugin_active
5474 && !bfd_link_relocatable (info)
5475 && (abfd->flags & BFD_PLUGIN) == 0
5476 && !just_syms
5477 && extsymcount)
5478 {
5479 int r_sym_shift;
5480
5481 if (bed->s->arch_size == 32)
5482 r_sym_shift = 8;
5483 else
5484 r_sym_shift = 32;
5485
5486 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5487 referenced in regular objects so that linker plugin will get
5488 the correct symbol resolution. */
5489
5490 sym_hash = elf_sym_hashes (abfd);
5491 for (s = abfd->sections; s != NULL; s = s->next)
5492 {
5493 Elf_Internal_Rela *internal_relocs;
5494 Elf_Internal_Rela *rel, *relend;
5495
5496 /* Don't check relocations in excluded sections. */
5497 if ((s->flags & SEC_RELOC) == 0
5498 || s->reloc_count == 0
5499 || (s->flags & SEC_EXCLUDE) != 0
5500 || ((info->strip == strip_all
5501 || info->strip == strip_debugger)
5502 && (s->flags & SEC_DEBUGGING) != 0))
5503 continue;
5504
5505 internal_relocs = _bfd_elf_link_info_read_relocs (abfd, info,
5506 s, NULL,
5507 NULL,
5508 _bfd_link_keep_memory (info));
5509 if (internal_relocs == NULL)
5510 goto error_free_vers;
5511
5512 rel = internal_relocs;
5513 relend = rel + s->reloc_count;
5514 for ( ; rel < relend; rel++)
5515 {
5516 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5517 struct elf_link_hash_entry *h;
5518
5519 /* Skip local symbols. */
5520 if (r_symndx < extsymoff)
5521 continue;
5522
5523 h = sym_hash[r_symndx - extsymoff];
5524 if (h != NULL)
5525 h->root.non_ir_ref_regular = 1;
5526 }
5527
5528 if (elf_section_data (s)->relocs != internal_relocs)
5529 free (internal_relocs);
5530 }
5531 }
5532
5533 free (extversym);
5534 extversym = NULL;
5535 free (isymbuf);
5536 isymbuf = NULL;
5537
5538 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5539 {
5540 unsigned int i;
5541
5542 /* Restore the symbol table. */
5543 old_ent = (char *) old_tab + tabsize;
5544 memset (elf_sym_hashes (abfd), 0,
5545 extsymcount * sizeof (struct elf_link_hash_entry *));
5546 htab->root.table.table = old_table;
5547 htab->root.table.size = old_size;
5548 htab->root.table.count = old_count;
5549 memcpy (htab->root.table.table, old_tab, tabsize);
5550 htab->root.undefs = old_undefs;
5551 htab->root.undefs_tail = old_undefs_tail;
5552 if (htab->dynstr != NULL)
5553 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5554 free (old_strtab);
5555 old_strtab = NULL;
5556 for (i = 0; i < htab->root.table.size; i++)
5557 {
5558 struct bfd_hash_entry *p;
5559 struct elf_link_hash_entry *h;
5560 unsigned int non_ir_ref_dynamic;
5561
5562 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5563 {
5564 /* Preserve non_ir_ref_dynamic so that this symbol
5565 will be exported when the dynamic lib becomes needed
5566 in the second pass. */
5567 h = (struct elf_link_hash_entry *) p;
5568 if (h->root.type == bfd_link_hash_warning)
5569 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5570 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
5571
5572 h = (struct elf_link_hash_entry *) p;
5573 memcpy (h, old_ent, htab->root.table.entsize);
5574 old_ent = (char *) old_ent + htab->root.table.entsize;
5575 if (h->root.type == bfd_link_hash_warning)
5576 {
5577 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5578 memcpy (h, old_ent, htab->root.table.entsize);
5579 old_ent = (char *) old_ent + htab->root.table.entsize;
5580 }
5581 if (h->root.type == bfd_link_hash_common)
5582 {
5583 memcpy (h->root.u.c.p, old_ent, sizeof (*h->root.u.c.p));
5584 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
5585 }
5586 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
5587 }
5588 }
5589
5590 /* Make a special call to the linker "notice" function to
5591 tell it that symbols added for crefs may need to be removed. */
5592 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
5593 goto error_free_vers;
5594
5595 free (old_tab);
5596 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5597 alloc_mark);
5598 free (nondeflt_vers);
5599 return true;
5600 }
5601
5602 if (old_tab != NULL)
5603 {
5604 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
5605 goto error_free_vers;
5606 free (old_tab);
5607 old_tab = NULL;
5608 }
5609
5610 /* Now that all the symbols from this input file are created, if
5611 not performing a relocatable link, handle .symver foo, foo@BAR
5612 such that any relocs against foo become foo@BAR. */
5613 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
5614 {
5615 size_t cnt, symidx;
5616
5617 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5618 {
5619 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5620 char *shortname, *p;
5621 size_t amt;
5622
5623 p = strchr (h->root.root.string, ELF_VER_CHR);
5624 if (p == NULL
5625 || (h->root.type != bfd_link_hash_defined
5626 && h->root.type != bfd_link_hash_defweak))
5627 continue;
5628
5629 amt = p - h->root.root.string;
5630 shortname = (char *) bfd_malloc (amt + 1);
5631 if (!shortname)
5632 goto error_free_vers;
5633 memcpy (shortname, h->root.root.string, amt);
5634 shortname[amt] = '\0';
5635
5636 hi = (struct elf_link_hash_entry *)
5637 bfd_link_hash_lookup (&htab->root, shortname,
5638 false, false, false);
5639 if (hi != NULL
5640 && hi->root.type == h->root.type
5641 && hi->root.u.def.value == h->root.u.def.value
5642 && hi->root.u.def.section == h->root.u.def.section)
5643 {
5644 (*bed->elf_backend_hide_symbol) (info, hi, true);
5645 hi->root.type = bfd_link_hash_indirect;
5646 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
5647 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
5648 sym_hash = elf_sym_hashes (abfd);
5649 if (sym_hash)
5650 for (symidx = 0; symidx < extsymcount; ++symidx)
5651 if (sym_hash[symidx] == hi)
5652 {
5653 sym_hash[symidx] = h;
5654 break;
5655 }
5656 }
5657 free (shortname);
5658 }
5659 free (nondeflt_vers);
5660 nondeflt_vers = NULL;
5661 }
5662
5663 /* Now set the alias field correctly for all the weak defined
5664 symbols we found. The only way to do this is to search all the
5665 symbols. Since we only need the information for non functions in
5666 dynamic objects, that's the only time we actually put anything on
5667 the list WEAKS. We need this information so that if a regular
5668 object refers to a symbol defined weakly in a dynamic object, the
5669 real symbol in the dynamic object is also put in the dynamic
5670 symbols; we also must arrange for both symbols to point to the
5671 same memory location. We could handle the general case of symbol
5672 aliasing, but a general symbol alias can only be generated in
5673 assembler code, handling it correctly would be very time
5674 consuming, and other ELF linkers don't handle general aliasing
5675 either. */
5676 if (weaks != NULL)
5677 {
5678 struct elf_link_hash_entry **hpp;
5679 struct elf_link_hash_entry **hppend;
5680 struct elf_link_hash_entry **sorted_sym_hash;
5681 struct elf_link_hash_entry *h;
5682 size_t sym_count, amt;
5683
5684 /* Since we have to search the whole symbol list for each weak
5685 defined symbol, search time for N weak defined symbols will be
5686 O(N^2). Binary search will cut it down to O(NlogN). */
5687 amt = extsymcount * sizeof (*sorted_sym_hash);
5688 sorted_sym_hash = bfd_malloc (amt);
5689 if (sorted_sym_hash == NULL)
5690 goto error_return;
5691 sym_hash = sorted_sym_hash;
5692 hpp = elf_sym_hashes (abfd);
5693 hppend = hpp + extsymcount;
5694 sym_count = 0;
5695 for (; hpp < hppend; hpp++)
5696 {
5697 h = *hpp;
5698 if (h != NULL
5699 && h->root.type == bfd_link_hash_defined
5700 && !bed->is_function_type (h->type))
5701 {
5702 *sym_hash = h;
5703 sym_hash++;
5704 sym_count++;
5705 }
5706 }
5707
5708 qsort (sorted_sym_hash, sym_count, sizeof (*sorted_sym_hash),
5709 elf_sort_symbol);
5710
5711 while (weaks != NULL)
5712 {
5713 struct elf_link_hash_entry *hlook;
5714 asection *slook;
5715 bfd_vma vlook;
5716 size_t i, j, idx = 0;
5717
5718 hlook = weaks;
5719 weaks = hlook->u.alias;
5720 hlook->u.alias = NULL;
5721
5722 if (hlook->root.type != bfd_link_hash_defined
5723 && hlook->root.type != bfd_link_hash_defweak)
5724 continue;
5725
5726 slook = hlook->root.u.def.section;
5727 vlook = hlook->root.u.def.value;
5728
5729 i = 0;
5730 j = sym_count;
5731 while (i != j)
5732 {
5733 bfd_signed_vma vdiff;
5734 idx = (i + j) / 2;
5735 h = sorted_sym_hash[idx];
5736 vdiff = vlook - h->root.u.def.value;
5737 if (vdiff < 0)
5738 j = idx;
5739 else if (vdiff > 0)
5740 i = idx + 1;
5741 else
5742 {
5743 int sdiff = slook->id - h->root.u.def.section->id;
5744 if (sdiff < 0)
5745 j = idx;
5746 else if (sdiff > 0)
5747 i = idx + 1;
5748 else
5749 break;
5750 }
5751 }
5752
5753 /* We didn't find a value/section match. */
5754 if (i == j)
5755 continue;
5756
5757 /* With multiple aliases, or when the weak symbol is already
5758 strongly defined, we have multiple matching symbols and
5759 the binary search above may land on any of them. Step
5760 one past the matching symbol(s). */
5761 while (++idx != j)
5762 {
5763 h = sorted_sym_hash[idx];
5764 if (h->root.u.def.section != slook
5765 || h->root.u.def.value != vlook)
5766 break;
5767 }
5768
5769 /* Now look back over the aliases. Since we sorted by size
5770 as well as value and section, we'll choose the one with
5771 the largest size. */
5772 while (idx-- != i)
5773 {
5774 h = sorted_sym_hash[idx];
5775
5776 /* Stop if value or section doesn't match. */
5777 if (h->root.u.def.section != slook
5778 || h->root.u.def.value != vlook)
5779 break;
5780 else if (h != hlook)
5781 {
5782 struct elf_link_hash_entry *t;
5783
5784 hlook->u.alias = h;
5785 hlook->is_weakalias = 1;
5786 t = h;
5787 if (t->u.alias != NULL)
5788 while (t->u.alias != h)
5789 t = t->u.alias;
5790 t->u.alias = hlook;
5791
5792 /* If the weak definition is in the list of dynamic
5793 symbols, make sure the real definition is put
5794 there as well. */
5795 if (hlook->dynindx != -1 && h->dynindx == -1)
5796 {
5797 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5798 {
5799 err_free_sym_hash:
5800 free (sorted_sym_hash);
5801 goto error_return;
5802 }
5803 }
5804
5805 /* If the real definition is in the list of dynamic
5806 symbols, make sure the weak definition is put
5807 there as well. If we don't do this, then the
5808 dynamic loader might not merge the entries for the
5809 real definition and the weak definition. */
5810 if (h->dynindx != -1 && hlook->dynindx == -1)
5811 {
5812 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
5813 goto err_free_sym_hash;
5814 }
5815 break;
5816 }
5817 }
5818 }
5819
5820 free (sorted_sym_hash);
5821 }
5822
5823 if (bed->check_directives
5824 && !(*bed->check_directives) (abfd, info))
5825 return false;
5826
5827 /* If this is a non-traditional link, try to optimize the handling
5828 of the .stab/.stabstr sections. */
5829 if (! dynamic
5830 && ! info->traditional_format
5831 && is_elf_hash_table (&htab->root)
5832 && (info->strip != strip_all && info->strip != strip_debugger))
5833 {
5834 asection *stabstr;
5835
5836 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5837 if (stabstr != NULL)
5838 {
5839 bfd_size_type string_offset = 0;
5840 asection *stab;
5841
5842 for (stab = abfd->sections; stab; stab = stab->next)
5843 if (startswith (stab->name, ".stab")
5844 && (!stab->name[5] ||
5845 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5846 && (stab->flags & SEC_MERGE) == 0
5847 && !bfd_is_abs_section (stab->output_section))
5848 {
5849 struct bfd_elf_section_data *secdata;
5850
5851 secdata = elf_section_data (stab);
5852 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5853 stabstr, &secdata->sec_info,
5854 &string_offset))
5855 goto error_return;
5856 if (secdata->sec_info)
5857 stab->sec_info_type = SEC_INFO_TYPE_STABS;
5858 }
5859 }
5860 }
5861
5862 if (dynamic && add_needed)
5863 {
5864 /* Add this bfd to the loaded list. */
5865 struct elf_link_loaded_list *n;
5866
5867 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5868 if (n == NULL)
5869 goto error_return;
5870 n->abfd = abfd;
5871 n->next = htab->dyn_loaded;
5872 htab->dyn_loaded = n;
5873 }
5874 if (dynamic && !add_needed
5875 && (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) != 0)
5876 elf_dyn_lib_class (abfd) |= DYN_NO_NEEDED;
5877
5878 return true;
5879
5880 error_free_vers:
5881 free (old_tab);
5882 free (old_strtab);
5883 free (nondeflt_vers);
5884 free (extversym);
5885 error_free_sym:
5886 free (isymbuf);
5887 error_return:
5888 return false;
5889 }
5890
5891 /* Return the linker hash table entry of a symbol that might be
5892 satisfied by an archive symbol. Return -1 on error. */
5893
5894 struct bfd_link_hash_entry *
5895 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5896 struct bfd_link_info *info,
5897 const char *name)
5898 {
5899 struct bfd_link_hash_entry *h;
5900 char *p, *copy;
5901 size_t len, first;
5902
5903 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
5904 if (h != NULL)
5905 return h;
5906
5907 /* If this is a default version (the name contains @@), look up the
5908 symbol again with only one `@' as well as without the version.
5909 The effect is that references to the symbol with and without the
5910 version will be matched by the default symbol in the archive. */
5911
5912 p = strchr (name, ELF_VER_CHR);
5913 if (p == NULL || p[1] != ELF_VER_CHR)
5914 return h;
5915
5916 /* First check with only one `@'. */
5917 len = strlen (name);
5918 copy = (char *) bfd_alloc (abfd, len);
5919 if (copy == NULL)
5920 return (struct bfd_link_hash_entry *) -1;
5921
5922 first = p - name + 1;
5923 memcpy (copy, name, first);
5924 memcpy (copy + first, name + first + 1, len - first);
5925
5926 h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
5927 if (h == NULL)
5928 {
5929 /* We also need to check references to the symbol without the
5930 version. */
5931 copy[first - 1] = '\0';
5932 h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
5933 }
5934
5935 bfd_release (abfd, copy);
5936 return h;
5937 }
5938
5939 /* Add symbols from an ELF archive file to the linker hash table. We
5940 don't use _bfd_generic_link_add_archive_symbols because we need to
5941 handle versioned symbols.
5942
5943 Fortunately, ELF archive handling is simpler than that done by
5944 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5945 oddities. In ELF, if we find a symbol in the archive map, and the
5946 symbol is currently undefined, we know that we must pull in that
5947 object file.
5948
5949 Unfortunately, we do have to make multiple passes over the symbol
5950 table until nothing further is resolved. */
5951
5952 static bool
5953 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5954 {
5955 symindex c;
5956 unsigned char *included = NULL;
5957 carsym *symdefs;
5958 bool loop;
5959 size_t amt;
5960 const struct elf_backend_data *bed;
5961 struct bfd_link_hash_entry * (*archive_symbol_lookup)
5962 (bfd *, struct bfd_link_info *, const char *);
5963
5964 if (! bfd_has_map (abfd))
5965 {
5966 /* An empty archive is a special case. */
5967 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5968 return true;
5969 bfd_set_error (bfd_error_no_armap);
5970 return false;
5971 }
5972
5973 /* Keep track of all symbols we know to be already defined, and all
5974 files we know to be already included. This is to speed up the
5975 second and subsequent passes. */
5976 c = bfd_ardata (abfd)->symdef_count;
5977 if (c == 0)
5978 return true;
5979 amt = c * sizeof (*included);
5980 included = (unsigned char *) bfd_zmalloc (amt);
5981 if (included == NULL)
5982 return false;
5983
5984 symdefs = bfd_ardata (abfd)->symdefs;
5985 bed = get_elf_backend_data (abfd);
5986 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5987
5988 do
5989 {
5990 file_ptr last;
5991 symindex i;
5992 carsym *symdef;
5993 carsym *symdefend;
5994
5995 loop = false;
5996 last = -1;
5997
5998 symdef = symdefs;
5999 symdefend = symdef + c;
6000 for (i = 0; symdef < symdefend; symdef++, i++)
6001 {
6002 struct bfd_link_hash_entry *h;
6003 bfd *element;
6004 struct bfd_link_hash_entry *undefs_tail;
6005 symindex mark;
6006
6007 if (included[i])
6008 continue;
6009 if (symdef->file_offset == last)
6010 {
6011 included[i] = true;
6012 continue;
6013 }
6014
6015 h = archive_symbol_lookup (abfd, info, symdef->name);
6016 if (h == (struct bfd_link_hash_entry *) -1)
6017 goto error_return;
6018
6019 if (h == NULL)
6020 continue;
6021
6022 if (h->type == bfd_link_hash_undefined)
6023 {
6024 /* If the archive element has already been loaded then one
6025 of the symbols defined by that element might have been
6026 made undefined due to being in a discarded section. */
6027 if (is_elf_hash_table (info->hash)
6028 && ((struct elf_link_hash_entry *) h)->indx == -3)
6029 continue;
6030 }
6031 else if (h->type == bfd_link_hash_common)
6032 {
6033 /* We currently have a common symbol. The archive map contains
6034 a reference to this symbol, so we may want to include it. We
6035 only want to include it however, if this archive element
6036 contains a definition of the symbol, not just another common
6037 declaration of it.
6038
6039 Unfortunately some archivers (including GNU ar) will put
6040 declarations of common symbols into their archive maps, as
6041 well as real definitions, so we cannot just go by the archive
6042 map alone. Instead we must read in the element's symbol
6043 table and check that to see what kind of symbol definition
6044 this is. */
6045 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
6046 continue;
6047 }
6048 else
6049 {
6050 if (h->type != bfd_link_hash_undefweak)
6051 /* Symbol must be defined. Don't check it again. */
6052 included[i] = true;
6053 continue;
6054 }
6055
6056 /* We need to include this archive member. */
6057 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset,
6058 info);
6059 if (element == NULL)
6060 goto error_return;
6061
6062 if (! bfd_check_format (element, bfd_object))
6063 goto error_return;
6064
6065 undefs_tail = info->hash->undefs_tail;
6066
6067 if (!(*info->callbacks
6068 ->add_archive_element) (info, element, symdef->name, &element))
6069 continue;
6070 if (!bfd_link_add_symbols (element, info))
6071 goto error_return;
6072
6073 /* If there are any new undefined symbols, we need to make
6074 another pass through the archive in order to see whether
6075 they can be defined. FIXME: This isn't perfect, because
6076 common symbols wind up on undefs_tail and because an
6077 undefined symbol which is defined later on in this pass
6078 does not require another pass. This isn't a bug, but it
6079 does make the code less efficient than it could be. */
6080 if (undefs_tail != info->hash->undefs_tail)
6081 loop = true;
6082
6083 /* Look backward to mark all symbols from this object file
6084 which we have already seen in this pass. */
6085 mark = i;
6086 do
6087 {
6088 included[mark] = true;
6089 if (mark == 0)
6090 break;
6091 --mark;
6092 }
6093 while (symdefs[mark].file_offset == symdef->file_offset);
6094
6095 /* We mark subsequent symbols from this object file as we go
6096 on through the loop. */
6097 last = symdef->file_offset;
6098 }
6099 }
6100 while (loop);
6101
6102 free (included);
6103 return true;
6104
6105 error_return:
6106 free (included);
6107 return false;
6108 }
6109
6110 /* Given an ELF BFD, add symbols to the global hash table as
6111 appropriate. */
6112
6113 bool
6114 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
6115 {
6116 switch (bfd_get_format (abfd))
6117 {
6118 case bfd_object:
6119 return elf_link_add_object_symbols (abfd, info);
6120 case bfd_archive:
6121 return elf_link_add_archive_symbols (abfd, info);
6122 default:
6123 bfd_set_error (bfd_error_wrong_format);
6124 return false;
6125 }
6126 }
6127 \f
6128 struct hash_codes_info
6129 {
6130 unsigned long *hashcodes;
6131 bool error;
6132 };
6133
6134 /* This function will be called though elf_link_hash_traverse to store
6135 all hash value of the exported symbols in an array. */
6136
6137 static bool
6138 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
6139 {
6140 struct hash_codes_info *inf = (struct hash_codes_info *) data;
6141 const char *name;
6142 unsigned long ha;
6143 char *alc = NULL;
6144
6145 /* Ignore indirect symbols. These are added by the versioning code. */
6146 if (h->dynindx == -1)
6147 return true;
6148
6149 name = h->root.root.string;
6150 if (h->versioned >= versioned)
6151 {
6152 char *p = strchr (name, ELF_VER_CHR);
6153 if (p != NULL)
6154 {
6155 alc = (char *) bfd_malloc (p - name + 1);
6156 if (alc == NULL)
6157 {
6158 inf->error = true;
6159 return false;
6160 }
6161 memcpy (alc, name, p - name);
6162 alc[p - name] = '\0';
6163 name = alc;
6164 }
6165 }
6166
6167 /* Compute the hash value. */
6168 ha = bfd_elf_hash (name);
6169
6170 /* Store the found hash value in the array given as the argument. */
6171 *(inf->hashcodes)++ = ha;
6172
6173 /* And store it in the struct so that we can put it in the hash table
6174 later. */
6175 h->u.elf_hash_value = ha;
6176
6177 free (alc);
6178 return true;
6179 }
6180
6181 struct collect_gnu_hash_codes
6182 {
6183 bfd *output_bfd;
6184 const struct elf_backend_data *bed;
6185 unsigned long int nsyms;
6186 unsigned long int maskbits;
6187 unsigned long int *hashcodes;
6188 unsigned long int *hashval;
6189 unsigned long int *indx;
6190 unsigned long int *counts;
6191 bfd_vma *bitmask;
6192 bfd_byte *contents;
6193 bfd_size_type xlat;
6194 long int min_dynindx;
6195 unsigned long int bucketcount;
6196 unsigned long int symindx;
6197 long int local_indx;
6198 long int shift1, shift2;
6199 unsigned long int mask;
6200 bool error;
6201 };
6202
6203 /* This function will be called though elf_link_hash_traverse to store
6204 all hash value of the exported symbols in an array. */
6205
6206 static bool
6207 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
6208 {
6209 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6210 const char *name;
6211 unsigned long ha;
6212 char *alc = NULL;
6213
6214 /* Ignore indirect symbols. These are added by the versioning code. */
6215 if (h->dynindx == -1)
6216 return true;
6217
6218 /* Ignore also local symbols and undefined symbols. */
6219 if (! (*s->bed->elf_hash_symbol) (h))
6220 return true;
6221
6222 name = h->root.root.string;
6223 if (h->versioned >= versioned)
6224 {
6225 char *p = strchr (name, ELF_VER_CHR);
6226 if (p != NULL)
6227 {
6228 alc = (char *) bfd_malloc (p - name + 1);
6229 if (alc == NULL)
6230 {
6231 s->error = true;
6232 return false;
6233 }
6234 memcpy (alc, name, p - name);
6235 alc[p - name] = '\0';
6236 name = alc;
6237 }
6238 }
6239
6240 /* Compute the hash value. */
6241 ha = bfd_elf_gnu_hash (name);
6242
6243 /* Store the found hash value in the array for compute_bucket_count,
6244 and also for .dynsym reordering purposes. */
6245 s->hashcodes[s->nsyms] = ha;
6246 s->hashval[h->dynindx] = ha;
6247 ++s->nsyms;
6248 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
6249 s->min_dynindx = h->dynindx;
6250
6251 free (alc);
6252 return true;
6253 }
6254
6255 /* This function will be called though elf_link_hash_traverse to do
6256 final dynamic symbol renumbering in case of .gnu.hash.
6257 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6258 to the translation table. */
6259
6260 static bool
6261 elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data)
6262 {
6263 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6264 unsigned long int bucket;
6265 unsigned long int val;
6266
6267 /* Ignore indirect symbols. */
6268 if (h->dynindx == -1)
6269 return true;
6270
6271 /* Ignore also local symbols and undefined symbols. */
6272 if (! (*s->bed->elf_hash_symbol) (h))
6273 {
6274 if (h->dynindx >= s->min_dynindx)
6275 {
6276 if (s->bed->record_xhash_symbol != NULL)
6277 {
6278 (*s->bed->record_xhash_symbol) (h, 0);
6279 s->local_indx++;
6280 }
6281 else
6282 h->dynindx = s->local_indx++;
6283 }
6284 return true;
6285 }
6286
6287 bucket = s->hashval[h->dynindx] % s->bucketcount;
6288 val = (s->hashval[h->dynindx] >> s->shift1)
6289 & ((s->maskbits >> s->shift1) - 1);
6290 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
6291 s->bitmask[val]
6292 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
6293 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
6294 if (s->counts[bucket] == 1)
6295 /* Last element terminates the chain. */
6296 val |= 1;
6297 bfd_put_32 (s->output_bfd, val,
6298 s->contents + (s->indx[bucket] - s->symindx) * 4);
6299 --s->counts[bucket];
6300 if (s->bed->record_xhash_symbol != NULL)
6301 {
6302 bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4;
6303
6304 (*s->bed->record_xhash_symbol) (h, xlat_loc);
6305 }
6306 else
6307 h->dynindx = s->indx[bucket]++;
6308 return true;
6309 }
6310
6311 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6312
6313 bool
6314 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
6315 {
6316 return !(h->forced_local
6317 || h->root.type == bfd_link_hash_undefined
6318 || h->root.type == bfd_link_hash_undefweak
6319 || ((h->root.type == bfd_link_hash_defined
6320 || h->root.type == bfd_link_hash_defweak)
6321 && h->root.u.def.section->output_section == NULL));
6322 }
6323
6324 /* Array used to determine the number of hash table buckets to use
6325 based on the number of symbols there are. If there are fewer than
6326 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6327 fewer than 37 we use 17 buckets, and so forth. We never use more
6328 than 32771 buckets. */
6329
6330 static const size_t elf_buckets[] =
6331 {
6332 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6333 16411, 32771, 0
6334 };
6335
6336 /* Compute bucket count for hashing table. We do not use a static set
6337 of possible tables sizes anymore. Instead we determine for all
6338 possible reasonable sizes of the table the outcome (i.e., the
6339 number of collisions etc) and choose the best solution. The
6340 weighting functions are not too simple to allow the table to grow
6341 without bounds. Instead one of the weighting factors is the size.
6342 Therefore the result is always a good payoff between few collisions
6343 (= short chain lengths) and table size. */
6344 static size_t
6345 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
6346 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
6347 unsigned long int nsyms,
6348 int gnu_hash)
6349 {
6350 size_t best_size = 0;
6351 unsigned long int i;
6352
6353 /* We have a problem here. The following code to optimize the table
6354 size requires an integer type with more the 32 bits. If
6355 BFD_HOST_U_64_BIT is set we know about such a type. */
6356 #ifdef BFD_HOST_U_64_BIT
6357 if (info->optimize)
6358 {
6359 size_t minsize;
6360 size_t maxsize;
6361 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
6362 bfd *dynobj = elf_hash_table (info)->dynobj;
6363 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
6364 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
6365 unsigned long int *counts;
6366 bfd_size_type amt;
6367 unsigned int no_improvement_count = 0;
6368
6369 /* Possible optimization parameters: if we have NSYMS symbols we say
6370 that the hashing table must at least have NSYMS/4 and at most
6371 2*NSYMS buckets. */
6372 minsize = nsyms / 4;
6373 if (minsize == 0)
6374 minsize = 1;
6375 best_size = maxsize = nsyms * 2;
6376 if (gnu_hash)
6377 {
6378 if (minsize < 2)
6379 minsize = 2;
6380 if ((best_size & 31) == 0)
6381 ++best_size;
6382 }
6383
6384 /* Create array where we count the collisions in. We must use bfd_malloc
6385 since the size could be large. */
6386 amt = maxsize;
6387 amt *= sizeof (unsigned long int);
6388 counts = (unsigned long int *) bfd_malloc (amt);
6389 if (counts == NULL)
6390 return 0;
6391
6392 /* Compute the "optimal" size for the hash table. The criteria is a
6393 minimal chain length. The minor criteria is (of course) the size
6394 of the table. */
6395 for (i = minsize; i < maxsize; ++i)
6396 {
6397 /* Walk through the array of hashcodes and count the collisions. */
6398 BFD_HOST_U_64_BIT max;
6399 unsigned long int j;
6400 unsigned long int fact;
6401
6402 if (gnu_hash && (i & 31) == 0)
6403 continue;
6404
6405 memset (counts, '\0', i * sizeof (unsigned long int));
6406
6407 /* Determine how often each hash bucket is used. */
6408 for (j = 0; j < nsyms; ++j)
6409 ++counts[hashcodes[j] % i];
6410
6411 /* For the weight function we need some information about the
6412 pagesize on the target. This is information need not be 100%
6413 accurate. Since this information is not available (so far) we
6414 define it here to a reasonable default value. If it is crucial
6415 to have a better value some day simply define this value. */
6416 # ifndef BFD_TARGET_PAGESIZE
6417 # define BFD_TARGET_PAGESIZE (4096)
6418 # endif
6419
6420 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6421 and the chains. */
6422 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
6423
6424 # if 1
6425 /* Variant 1: optimize for short chains. We add the squares
6426 of all the chain lengths (which favors many small chain
6427 over a few long chains). */
6428 for (j = 0; j < i; ++j)
6429 max += counts[j] * counts[j];
6430
6431 /* This adds penalties for the overall size of the table. */
6432 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6433 max *= fact * fact;
6434 # else
6435 /* Variant 2: Optimize a lot more for small table. Here we
6436 also add squares of the size but we also add penalties for
6437 empty slots (the +1 term). */
6438 for (j = 0; j < i; ++j)
6439 max += (1 + counts[j]) * (1 + counts[j]);
6440
6441 /* The overall size of the table is considered, but not as
6442 strong as in variant 1, where it is squared. */
6443 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6444 max *= fact;
6445 # endif
6446
6447 /* Compare with current best results. */
6448 if (max < best_chlen)
6449 {
6450 best_chlen = max;
6451 best_size = i;
6452 no_improvement_count = 0;
6453 }
6454 /* PR 11843: Avoid futile long searches for the best bucket size
6455 when there are a large number of symbols. */
6456 else if (++no_improvement_count == 100)
6457 break;
6458 }
6459
6460 free (counts);
6461 }
6462 else
6463 #endif /* defined (BFD_HOST_U_64_BIT) */
6464 {
6465 /* This is the fallback solution if no 64bit type is available or if we
6466 are not supposed to spend much time on optimizations. We select the
6467 bucket count using a fixed set of numbers. */
6468 for (i = 0; elf_buckets[i] != 0; i++)
6469 {
6470 best_size = elf_buckets[i];
6471 if (nsyms < elf_buckets[i + 1])
6472 break;
6473 }
6474 if (gnu_hash && best_size < 2)
6475 best_size = 2;
6476 }
6477
6478 return best_size;
6479 }
6480
6481 /* Size any SHT_GROUP section for ld -r. */
6482
6483 bool
6484 _bfd_elf_size_group_sections (struct bfd_link_info *info)
6485 {
6486 bfd *ibfd;
6487 asection *s;
6488
6489 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6490 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6491 && (s = ibfd->sections) != NULL
6492 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
6493 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6494 return false;
6495 return true;
6496 }
6497
6498 /* Set a default stack segment size. The value in INFO wins. If it
6499 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6500 undefined it is initialized. */
6501
6502 bool
6503 bfd_elf_stack_segment_size (bfd *output_bfd,
6504 struct bfd_link_info *info,
6505 const char *legacy_symbol,
6506 bfd_vma default_size)
6507 {
6508 struct elf_link_hash_entry *h = NULL;
6509
6510 /* Look for legacy symbol. */
6511 if (legacy_symbol)
6512 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6513 false, false, false);
6514 if (h && (h->root.type == bfd_link_hash_defined
6515 || h->root.type == bfd_link_hash_defweak)
6516 && h->def_regular
6517 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6518 {
6519 /* The symbol has no type if specified on the command line. */
6520 h->type = STT_OBJECT;
6521 if (info->stacksize)
6522 /* xgettext:c-format */
6523 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6524 output_bfd, legacy_symbol);
6525 else if (h->root.u.def.section != bfd_abs_section_ptr)
6526 /* xgettext:c-format */
6527 _bfd_error_handler (_("%pB: %s not absolute"),
6528 output_bfd, legacy_symbol);
6529 else
6530 info->stacksize = h->root.u.def.value;
6531 }
6532
6533 if (!info->stacksize)
6534 /* If the user didn't set a size, or explicitly inhibit the
6535 size, set it now. */
6536 info->stacksize = default_size;
6537
6538 /* Provide the legacy symbol, if it is referenced. */
6539 if (h && (h->root.type == bfd_link_hash_undefined
6540 || h->root.type == bfd_link_hash_undefweak))
6541 {
6542 struct bfd_link_hash_entry *bh = NULL;
6543
6544 if (!(_bfd_generic_link_add_one_symbol
6545 (info, output_bfd, legacy_symbol,
6546 BSF_GLOBAL, bfd_abs_section_ptr,
6547 info->stacksize >= 0 ? info->stacksize : 0,
6548 NULL, false, get_elf_backend_data (output_bfd)->collect, &bh)))
6549 return false;
6550
6551 h = (struct elf_link_hash_entry *) bh;
6552 h->def_regular = 1;
6553 h->type = STT_OBJECT;
6554 }
6555
6556 return true;
6557 }
6558
6559 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6560
6561 struct elf_gc_sweep_symbol_info
6562 {
6563 struct bfd_link_info *info;
6564 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6565 bool);
6566 };
6567
6568 static bool
6569 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6570 {
6571 if (!h->mark
6572 && (((h->root.type == bfd_link_hash_defined
6573 || h->root.type == bfd_link_hash_defweak)
6574 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6575 && h->root.u.def.section->gc_mark))
6576 || h->root.type == bfd_link_hash_undefined
6577 || h->root.type == bfd_link_hash_undefweak))
6578 {
6579 struct elf_gc_sweep_symbol_info *inf;
6580
6581 inf = (struct elf_gc_sweep_symbol_info *) data;
6582 (*inf->hide_symbol) (inf->info, h, true);
6583 h->def_regular = 0;
6584 h->ref_regular = 0;
6585 h->ref_regular_nonweak = 0;
6586 }
6587
6588 return true;
6589 }
6590
6591 /* Set up the sizes and contents of the ELF dynamic sections. This is
6592 called by the ELF linker emulation before_allocation routine. We
6593 must set the sizes of the sections before the linker sets the
6594 addresses of the various sections. */
6595
6596 bool
6597 bfd_elf_size_dynamic_sections (bfd *output_bfd,
6598 const char *soname,
6599 const char *rpath,
6600 const char *filter_shlib,
6601 const char *audit,
6602 const char *depaudit,
6603 const char * const *auxiliary_filters,
6604 struct bfd_link_info *info,
6605 asection **sinterpptr)
6606 {
6607 bfd *dynobj;
6608 const struct elf_backend_data *bed;
6609
6610 *sinterpptr = NULL;
6611
6612 if (!is_elf_hash_table (info->hash))
6613 return true;
6614
6615 /* Any syms created from now on start with -1 in
6616 got.refcount/offset and plt.refcount/offset. */
6617 elf_hash_table (info)->init_got_refcount
6618 = elf_hash_table (info)->init_got_offset;
6619 elf_hash_table (info)->init_plt_refcount
6620 = elf_hash_table (info)->init_plt_offset;
6621
6622 bed = get_elf_backend_data (output_bfd);
6623
6624 /* The backend may have to create some sections regardless of whether
6625 we're dynamic or not. */
6626 if (bed->elf_backend_always_size_sections
6627 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6628 return false;
6629
6630 dynobj = elf_hash_table (info)->dynobj;
6631
6632 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6633 {
6634 struct bfd_elf_version_tree *verdefs;
6635 struct elf_info_failed asvinfo;
6636 struct bfd_elf_version_tree *t;
6637 struct bfd_elf_version_expr *d;
6638 asection *s;
6639 size_t soname_indx;
6640
6641 /* If we are supposed to export all symbols into the dynamic symbol
6642 table (this is not the normal case), then do so. */
6643 if (info->export_dynamic
6644 || (bfd_link_executable (info) && info->dynamic))
6645 {
6646 struct elf_info_failed eif;
6647
6648 eif.info = info;
6649 eif.failed = false;
6650 elf_link_hash_traverse (elf_hash_table (info),
6651 _bfd_elf_export_symbol,
6652 &eif);
6653 if (eif.failed)
6654 return false;
6655 }
6656
6657 if (soname != NULL)
6658 {
6659 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6660 soname, true);
6661 if (soname_indx == (size_t) -1
6662 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6663 return false;
6664 }
6665 else
6666 soname_indx = (size_t) -1;
6667
6668 /* Make all global versions with definition. */
6669 for (t = info->version_info; t != NULL; t = t->next)
6670 for (d = t->globals.list; d != NULL; d = d->next)
6671 if (!d->symver && d->literal)
6672 {
6673 const char *verstr, *name;
6674 size_t namelen, verlen, newlen;
6675 char *newname, *p, leading_char;
6676 struct elf_link_hash_entry *newh;
6677
6678 leading_char = bfd_get_symbol_leading_char (output_bfd);
6679 name = d->pattern;
6680 namelen = strlen (name) + (leading_char != '\0');
6681 verstr = t->name;
6682 verlen = strlen (verstr);
6683 newlen = namelen + verlen + 3;
6684
6685 newname = (char *) bfd_malloc (newlen);
6686 if (newname == NULL)
6687 return false;
6688 newname[0] = leading_char;
6689 memcpy (newname + (leading_char != '\0'), name, namelen);
6690
6691 /* Check the hidden versioned definition. */
6692 p = newname + namelen;
6693 *p++ = ELF_VER_CHR;
6694 memcpy (p, verstr, verlen + 1);
6695 newh = elf_link_hash_lookup (elf_hash_table (info),
6696 newname, false, false,
6697 false);
6698 if (newh == NULL
6699 || (newh->root.type != bfd_link_hash_defined
6700 && newh->root.type != bfd_link_hash_defweak))
6701 {
6702 /* Check the default versioned definition. */
6703 *p++ = ELF_VER_CHR;
6704 memcpy (p, verstr, verlen + 1);
6705 newh = elf_link_hash_lookup (elf_hash_table (info),
6706 newname, false, false,
6707 false);
6708 }
6709 free (newname);
6710
6711 /* Mark this version if there is a definition and it is
6712 not defined in a shared object. */
6713 if (newh != NULL
6714 && !newh->def_dynamic
6715 && (newh->root.type == bfd_link_hash_defined
6716 || newh->root.type == bfd_link_hash_defweak))
6717 d->symver = 1;
6718 }
6719
6720 /* Attach all the symbols to their version information. */
6721 asvinfo.info = info;
6722 asvinfo.failed = false;
6723
6724 elf_link_hash_traverse (elf_hash_table (info),
6725 _bfd_elf_link_assign_sym_version,
6726 &asvinfo);
6727 if (asvinfo.failed)
6728 return false;
6729
6730 if (!info->allow_undefined_version)
6731 {
6732 /* Check if all global versions have a definition. */
6733 bool all_defined = true;
6734 for (t = info->version_info; t != NULL; t = t->next)
6735 for (d = t->globals.list; d != NULL; d = d->next)
6736 if (d->literal && !d->symver && !d->script)
6737 {
6738 _bfd_error_handler
6739 (_("%s: undefined version: %s"),
6740 d->pattern, t->name);
6741 all_defined = false;
6742 }
6743
6744 if (!all_defined)
6745 {
6746 bfd_set_error (bfd_error_bad_value);
6747 return false;
6748 }
6749 }
6750
6751 /* Set up the version definition section. */
6752 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6753 BFD_ASSERT (s != NULL);
6754
6755 /* We may have created additional version definitions if we are
6756 just linking a regular application. */
6757 verdefs = info->version_info;
6758
6759 /* Skip anonymous version tag. */
6760 if (verdefs != NULL && verdefs->vernum == 0)
6761 verdefs = verdefs->next;
6762
6763 if (verdefs == NULL && !info->create_default_symver)
6764 s->flags |= SEC_EXCLUDE;
6765 else
6766 {
6767 unsigned int cdefs;
6768 bfd_size_type size;
6769 bfd_byte *p;
6770 Elf_Internal_Verdef def;
6771 Elf_Internal_Verdaux defaux;
6772 struct bfd_link_hash_entry *bh;
6773 struct elf_link_hash_entry *h;
6774 const char *name;
6775
6776 cdefs = 0;
6777 size = 0;
6778
6779 /* Make space for the base version. */
6780 size += sizeof (Elf_External_Verdef);
6781 size += sizeof (Elf_External_Verdaux);
6782 ++cdefs;
6783
6784 /* Make space for the default version. */
6785 if (info->create_default_symver)
6786 {
6787 size += sizeof (Elf_External_Verdef);
6788 ++cdefs;
6789 }
6790
6791 for (t = verdefs; t != NULL; t = t->next)
6792 {
6793 struct bfd_elf_version_deps *n;
6794
6795 /* Don't emit base version twice. */
6796 if (t->vernum == 0)
6797 continue;
6798
6799 size += sizeof (Elf_External_Verdef);
6800 size += sizeof (Elf_External_Verdaux);
6801 ++cdefs;
6802
6803 for (n = t->deps; n != NULL; n = n->next)
6804 size += sizeof (Elf_External_Verdaux);
6805 }
6806
6807 s->size = size;
6808 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6809 if (s->contents == NULL && s->size != 0)
6810 return false;
6811
6812 /* Fill in the version definition section. */
6813
6814 p = s->contents;
6815
6816 def.vd_version = VER_DEF_CURRENT;
6817 def.vd_flags = VER_FLG_BASE;
6818 def.vd_ndx = 1;
6819 def.vd_cnt = 1;
6820 if (info->create_default_symver)
6821 {
6822 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6823 def.vd_next = sizeof (Elf_External_Verdef);
6824 }
6825 else
6826 {
6827 def.vd_aux = sizeof (Elf_External_Verdef);
6828 def.vd_next = (sizeof (Elf_External_Verdef)
6829 + sizeof (Elf_External_Verdaux));
6830 }
6831
6832 if (soname_indx != (size_t) -1)
6833 {
6834 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6835 soname_indx);
6836 def.vd_hash = bfd_elf_hash (soname);
6837 defaux.vda_name = soname_indx;
6838 name = soname;
6839 }
6840 else
6841 {
6842 size_t indx;
6843
6844 name = lbasename (bfd_get_filename (output_bfd));
6845 def.vd_hash = bfd_elf_hash (name);
6846 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6847 name, false);
6848 if (indx == (size_t) -1)
6849 return false;
6850 defaux.vda_name = indx;
6851 }
6852 defaux.vda_next = 0;
6853
6854 _bfd_elf_swap_verdef_out (output_bfd, &def,
6855 (Elf_External_Verdef *) p);
6856 p += sizeof (Elf_External_Verdef);
6857 if (info->create_default_symver)
6858 {
6859 /* Add a symbol representing this version. */
6860 bh = NULL;
6861 if (! (_bfd_generic_link_add_one_symbol
6862 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6863 0, NULL, false,
6864 get_elf_backend_data (dynobj)->collect, &bh)))
6865 return false;
6866 h = (struct elf_link_hash_entry *) bh;
6867 h->non_elf = 0;
6868 h->def_regular = 1;
6869 h->type = STT_OBJECT;
6870 h->verinfo.vertree = NULL;
6871
6872 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6873 return false;
6874
6875 /* Create a duplicate of the base version with the same
6876 aux block, but different flags. */
6877 def.vd_flags = 0;
6878 def.vd_ndx = 2;
6879 def.vd_aux = sizeof (Elf_External_Verdef);
6880 if (verdefs)
6881 def.vd_next = (sizeof (Elf_External_Verdef)
6882 + sizeof (Elf_External_Verdaux));
6883 else
6884 def.vd_next = 0;
6885 _bfd_elf_swap_verdef_out (output_bfd, &def,
6886 (Elf_External_Verdef *) p);
6887 p += sizeof (Elf_External_Verdef);
6888 }
6889 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6890 (Elf_External_Verdaux *) p);
6891 p += sizeof (Elf_External_Verdaux);
6892
6893 for (t = verdefs; t != NULL; t = t->next)
6894 {
6895 unsigned int cdeps;
6896 struct bfd_elf_version_deps *n;
6897
6898 /* Don't emit the base version twice. */
6899 if (t->vernum == 0)
6900 continue;
6901
6902 cdeps = 0;
6903 for (n = t->deps; n != NULL; n = n->next)
6904 ++cdeps;
6905
6906 /* Add a symbol representing this version. */
6907 bh = NULL;
6908 if (! (_bfd_generic_link_add_one_symbol
6909 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6910 0, NULL, false,
6911 get_elf_backend_data (dynobj)->collect, &bh)))
6912 return false;
6913 h = (struct elf_link_hash_entry *) bh;
6914 h->non_elf = 0;
6915 h->def_regular = 1;
6916 h->type = STT_OBJECT;
6917 h->verinfo.vertree = t;
6918
6919 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6920 return false;
6921
6922 def.vd_version = VER_DEF_CURRENT;
6923 def.vd_flags = 0;
6924 if (t->globals.list == NULL
6925 && t->locals.list == NULL
6926 && ! t->used)
6927 def.vd_flags |= VER_FLG_WEAK;
6928 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6929 def.vd_cnt = cdeps + 1;
6930 def.vd_hash = bfd_elf_hash (t->name);
6931 def.vd_aux = sizeof (Elf_External_Verdef);
6932 def.vd_next = 0;
6933
6934 /* If a basever node is next, it *must* be the last node in
6935 the chain, otherwise Verdef construction breaks. */
6936 if (t->next != NULL && t->next->vernum == 0)
6937 BFD_ASSERT (t->next->next == NULL);
6938
6939 if (t->next != NULL && t->next->vernum != 0)
6940 def.vd_next = (sizeof (Elf_External_Verdef)
6941 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6942
6943 _bfd_elf_swap_verdef_out (output_bfd, &def,
6944 (Elf_External_Verdef *) p);
6945 p += sizeof (Elf_External_Verdef);
6946
6947 defaux.vda_name = h->dynstr_index;
6948 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6949 h->dynstr_index);
6950 defaux.vda_next = 0;
6951 if (t->deps != NULL)
6952 defaux.vda_next = sizeof (Elf_External_Verdaux);
6953 t->name_indx = defaux.vda_name;
6954
6955 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6956 (Elf_External_Verdaux *) p);
6957 p += sizeof (Elf_External_Verdaux);
6958
6959 for (n = t->deps; n != NULL; n = n->next)
6960 {
6961 if (n->version_needed == NULL)
6962 {
6963 /* This can happen if there was an error in the
6964 version script. */
6965 defaux.vda_name = 0;
6966 }
6967 else
6968 {
6969 defaux.vda_name = n->version_needed->name_indx;
6970 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6971 defaux.vda_name);
6972 }
6973 if (n->next == NULL)
6974 defaux.vda_next = 0;
6975 else
6976 defaux.vda_next = sizeof (Elf_External_Verdaux);
6977
6978 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6979 (Elf_External_Verdaux *) p);
6980 p += sizeof (Elf_External_Verdaux);
6981 }
6982 }
6983
6984 elf_tdata (output_bfd)->cverdefs = cdefs;
6985 }
6986 }
6987
6988 if (info->gc_sections && bed->can_gc_sections)
6989 {
6990 struct elf_gc_sweep_symbol_info sweep_info;
6991
6992 /* Remove the symbols that were in the swept sections from the
6993 dynamic symbol table. */
6994 sweep_info.info = info;
6995 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6996 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6997 &sweep_info);
6998 }
6999
7000 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7001 {
7002 asection *s;
7003 struct elf_find_verdep_info sinfo;
7004
7005 /* Work out the size of the version reference section. */
7006
7007 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
7008 BFD_ASSERT (s != NULL);
7009
7010 sinfo.info = info;
7011 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
7012 if (sinfo.vers == 0)
7013 sinfo.vers = 1;
7014 sinfo.failed = false;
7015
7016 elf_link_hash_traverse (elf_hash_table (info),
7017 _bfd_elf_link_find_version_dependencies,
7018 &sinfo);
7019 if (sinfo.failed)
7020 return false;
7021
7022 if (info->enable_dt_relr)
7023 {
7024 elf_link_add_dt_relr_dependency (&sinfo);
7025 if (sinfo.failed)
7026 return false;
7027 }
7028
7029 if (elf_tdata (output_bfd)->verref == NULL)
7030 s->flags |= SEC_EXCLUDE;
7031 else
7032 {
7033 Elf_Internal_Verneed *vn;
7034 unsigned int size;
7035 unsigned int crefs;
7036 bfd_byte *p;
7037
7038 /* Build the version dependency section. */
7039 size = 0;
7040 crefs = 0;
7041 for (vn = elf_tdata (output_bfd)->verref;
7042 vn != NULL;
7043 vn = vn->vn_nextref)
7044 {
7045 Elf_Internal_Vernaux *a;
7046
7047 size += sizeof (Elf_External_Verneed);
7048 ++crefs;
7049 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7050 size += sizeof (Elf_External_Vernaux);
7051 }
7052
7053 s->size = size;
7054 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7055 if (s->contents == NULL)
7056 return false;
7057
7058 p = s->contents;
7059 for (vn = elf_tdata (output_bfd)->verref;
7060 vn != NULL;
7061 vn = vn->vn_nextref)
7062 {
7063 unsigned int caux;
7064 Elf_Internal_Vernaux *a;
7065 size_t indx;
7066
7067 caux = 0;
7068 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7069 ++caux;
7070
7071 vn->vn_version = VER_NEED_CURRENT;
7072 vn->vn_cnt = caux;
7073 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7074 elf_dt_name (vn->vn_bfd) != NULL
7075 ? elf_dt_name (vn->vn_bfd)
7076 : lbasename (bfd_get_filename
7077 (vn->vn_bfd)),
7078 false);
7079 if (indx == (size_t) -1)
7080 return false;
7081 vn->vn_file = indx;
7082 vn->vn_aux = sizeof (Elf_External_Verneed);
7083 if (vn->vn_nextref == NULL)
7084 vn->vn_next = 0;
7085 else
7086 vn->vn_next = (sizeof (Elf_External_Verneed)
7087 + caux * sizeof (Elf_External_Vernaux));
7088
7089 _bfd_elf_swap_verneed_out (output_bfd, vn,
7090 (Elf_External_Verneed *) p);
7091 p += sizeof (Elf_External_Verneed);
7092
7093 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7094 {
7095 a->vna_hash = bfd_elf_hash (a->vna_nodename);
7096 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7097 a->vna_nodename, false);
7098 if (indx == (size_t) -1)
7099 return false;
7100 a->vna_name = indx;
7101 if (a->vna_nextptr == NULL)
7102 a->vna_next = 0;
7103 else
7104 a->vna_next = sizeof (Elf_External_Vernaux);
7105
7106 _bfd_elf_swap_vernaux_out (output_bfd, a,
7107 (Elf_External_Vernaux *) p);
7108 p += sizeof (Elf_External_Vernaux);
7109 }
7110 }
7111
7112 elf_tdata (output_bfd)->cverrefs = crefs;
7113 }
7114 }
7115
7116 if (bfd_link_relocatable (info)
7117 && !_bfd_elf_size_group_sections (info))
7118 return false;
7119
7120 /* Determine any GNU_STACK segment requirements, after the backend
7121 has had a chance to set a default segment size. */
7122 if (info->execstack)
7123 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
7124 else if (info->noexecstack)
7125 elf_stack_flags (output_bfd) = PF_R | PF_W;
7126 else
7127 {
7128 bfd *inputobj;
7129 asection *notesec = NULL;
7130 int exec = 0;
7131
7132 for (inputobj = info->input_bfds;
7133 inputobj;
7134 inputobj = inputobj->link.next)
7135 {
7136 asection *s;
7137
7138 if (inputobj->flags
7139 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
7140 continue;
7141 s = inputobj->sections;
7142 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7143 continue;
7144
7145 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
7146 if (s)
7147 {
7148 if (s->flags & SEC_CODE)
7149 exec = PF_X;
7150 notesec = s;
7151 }
7152 else if (bed->default_execstack)
7153 exec = PF_X;
7154 }
7155 if (notesec || info->stacksize > 0)
7156 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
7157 if (notesec && exec && bfd_link_relocatable (info)
7158 && notesec->output_section != bfd_abs_section_ptr)
7159 notesec->output_section->flags |= SEC_CODE;
7160 }
7161
7162 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7163 {
7164 struct elf_info_failed eif;
7165 struct elf_link_hash_entry *h;
7166 asection *dynstr;
7167 asection *s;
7168
7169 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
7170 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
7171
7172 if (info->symbolic)
7173 {
7174 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
7175 return false;
7176 info->flags |= DF_SYMBOLIC;
7177 }
7178
7179 if (rpath != NULL)
7180 {
7181 size_t indx;
7182 bfd_vma tag;
7183
7184 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
7185 true);
7186 if (indx == (size_t) -1)
7187 return false;
7188
7189 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
7190 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
7191 return false;
7192 }
7193
7194 if (filter_shlib != NULL)
7195 {
7196 size_t indx;
7197
7198 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7199 filter_shlib, true);
7200 if (indx == (size_t) -1
7201 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
7202 return false;
7203 }
7204
7205 if (auxiliary_filters != NULL)
7206 {
7207 const char * const *p;
7208
7209 for (p = auxiliary_filters; *p != NULL; p++)
7210 {
7211 size_t indx;
7212
7213 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7214 *p, true);
7215 if (indx == (size_t) -1
7216 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
7217 return false;
7218 }
7219 }
7220
7221 if (audit != NULL)
7222 {
7223 size_t indx;
7224
7225 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
7226 true);
7227 if (indx == (size_t) -1
7228 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
7229 return false;
7230 }
7231
7232 if (depaudit != NULL)
7233 {
7234 size_t indx;
7235
7236 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
7237 true);
7238 if (indx == (size_t) -1
7239 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
7240 return false;
7241 }
7242
7243 eif.info = info;
7244 eif.failed = false;
7245
7246 /* Find all symbols which were defined in a dynamic object and make
7247 the backend pick a reasonable value for them. */
7248 elf_link_hash_traverse (elf_hash_table (info),
7249 _bfd_elf_adjust_dynamic_symbol,
7250 &eif);
7251 if (eif.failed)
7252 return false;
7253
7254 /* Add some entries to the .dynamic section. We fill in some of the
7255 values later, in bfd_elf_final_link, but we must add the entries
7256 now so that we know the final size of the .dynamic section. */
7257
7258 /* If there are initialization and/or finalization functions to
7259 call then add the corresponding DT_INIT/DT_FINI entries. */
7260 h = (info->init_function
7261 ? elf_link_hash_lookup (elf_hash_table (info),
7262 info->init_function, false,
7263 false, false)
7264 : NULL);
7265 if (h != NULL
7266 && (h->ref_regular
7267 || h->def_regular))
7268 {
7269 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
7270 return false;
7271 }
7272 h = (info->fini_function
7273 ? elf_link_hash_lookup (elf_hash_table (info),
7274 info->fini_function, false,
7275 false, false)
7276 : NULL);
7277 if (h != NULL
7278 && (h->ref_regular
7279 || h->def_regular))
7280 {
7281 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
7282 return false;
7283 }
7284
7285 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
7286 if (s != NULL && s->linker_has_input)
7287 {
7288 /* DT_PREINIT_ARRAY is not allowed in shared library. */
7289 if (! bfd_link_executable (info))
7290 {
7291 bfd *sub;
7292 asection *o;
7293
7294 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
7295 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
7296 && (o = sub->sections) != NULL
7297 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
7298 for (o = sub->sections; o != NULL; o = o->next)
7299 if (elf_section_data (o)->this_hdr.sh_type
7300 == SHT_PREINIT_ARRAY)
7301 {
7302 _bfd_error_handler
7303 (_("%pB: .preinit_array section is not allowed in DSO"),
7304 sub);
7305 break;
7306 }
7307
7308 bfd_set_error (bfd_error_nonrepresentable_section);
7309 return false;
7310 }
7311
7312 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
7313 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
7314 return false;
7315 }
7316 s = bfd_get_section_by_name (output_bfd, ".init_array");
7317 if (s != NULL && s->linker_has_input)
7318 {
7319 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
7320 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
7321 return false;
7322 }
7323 s = bfd_get_section_by_name (output_bfd, ".fini_array");
7324 if (s != NULL && s->linker_has_input)
7325 {
7326 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
7327 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
7328 return false;
7329 }
7330
7331 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
7332 /* If .dynstr is excluded from the link, we don't want any of
7333 these tags. Strictly, we should be checking each section
7334 individually; This quick check covers for the case where
7335 someone does a /DISCARD/ : { *(*) }. */
7336 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
7337 {
7338 bfd_size_type strsize;
7339
7340 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7341 if ((info->emit_hash
7342 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
7343 || (info->emit_gnu_hash
7344 && (bed->record_xhash_symbol == NULL
7345 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)))
7346 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
7347 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
7348 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
7349 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
7350 bed->s->sizeof_sym)
7351 || (info->gnu_flags_1
7352 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_FLAGS_1,
7353 info->gnu_flags_1)))
7354 return false;
7355 }
7356 }
7357
7358 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
7359 return false;
7360
7361 /* The backend must work out the sizes of all the other dynamic
7362 sections. */
7363 if (dynobj != NULL
7364 && bed->elf_backend_size_dynamic_sections != NULL
7365 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
7366 return false;
7367
7368 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7369 {
7370 if (elf_tdata (output_bfd)->cverdefs)
7371 {
7372 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
7373
7374 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
7375 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
7376 return false;
7377 }
7378
7379 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
7380 {
7381 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
7382 return false;
7383 }
7384 else if (info->flags & DF_BIND_NOW)
7385 {
7386 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
7387 return false;
7388 }
7389
7390 if (info->flags_1)
7391 {
7392 if (bfd_link_executable (info))
7393 info->flags_1 &= ~ (DF_1_INITFIRST
7394 | DF_1_NODELETE
7395 | DF_1_NOOPEN);
7396 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
7397 return false;
7398 }
7399
7400 if (elf_tdata (output_bfd)->cverrefs)
7401 {
7402 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
7403
7404 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
7405 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
7406 return false;
7407 }
7408
7409 if ((elf_tdata (output_bfd)->cverrefs == 0
7410 && elf_tdata (output_bfd)->cverdefs == 0)
7411 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
7412 {
7413 asection *s;
7414
7415 s = bfd_get_linker_section (dynobj, ".gnu.version");
7416 s->flags |= SEC_EXCLUDE;
7417 }
7418 }
7419 return true;
7420 }
7421
7422 /* Find the first non-excluded output section. We'll use its
7423 section symbol for some emitted relocs. */
7424 void
7425 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7426 {
7427 asection *s;
7428 asection *found = NULL;
7429
7430 for (s = output_bfd->sections; s != NULL; s = s->next)
7431 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7432 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7433 {
7434 found = s;
7435 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7436 break;
7437 }
7438 elf_hash_table (info)->text_index_section = found;
7439 }
7440
7441 /* Find two non-excluded output sections, one for code, one for data.
7442 We'll use their section symbols for some emitted relocs. */
7443 void
7444 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7445 {
7446 asection *s;
7447 asection *found = NULL;
7448
7449 /* Data first, since setting text_index_section changes
7450 _bfd_elf_omit_section_dynsym_default. */
7451 for (s = output_bfd->sections; s != NULL; s = s->next)
7452 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7453 && !(s->flags & SEC_READONLY)
7454 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7455 {
7456 found = s;
7457 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7458 break;
7459 }
7460 elf_hash_table (info)->data_index_section = found;
7461
7462 for (s = output_bfd->sections; s != NULL; s = s->next)
7463 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7464 && (s->flags & SEC_READONLY)
7465 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7466 {
7467 found = s;
7468 break;
7469 }
7470 elf_hash_table (info)->text_index_section = found;
7471 }
7472
7473 #define GNU_HASH_SECTION_NAME(bed) \
7474 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7475
7476 bool
7477 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7478 {
7479 const struct elf_backend_data *bed;
7480 unsigned long section_sym_count;
7481 bfd_size_type dynsymcount = 0;
7482
7483 if (!is_elf_hash_table (info->hash))
7484 return true;
7485
7486 bed = get_elf_backend_data (output_bfd);
7487 (*bed->elf_backend_init_index_section) (output_bfd, info);
7488
7489 /* Assign dynsym indices. In a shared library we generate a section
7490 symbol for each output section, which come first. Next come all
7491 of the back-end allocated local dynamic syms, followed by the rest
7492 of the global symbols.
7493
7494 This is usually not needed for static binaries, however backends
7495 can request to always do it, e.g. the MIPS backend uses dynamic
7496 symbol counts to lay out GOT, which will be produced in the
7497 presence of GOT relocations even in static binaries (holding fixed
7498 data in that case, to satisfy those relocations). */
7499
7500 if (elf_hash_table (info)->dynamic_sections_created
7501 || bed->always_renumber_dynsyms)
7502 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7503 &section_sym_count);
7504
7505 if (elf_hash_table (info)->dynamic_sections_created)
7506 {
7507 bfd *dynobj;
7508 asection *s;
7509 unsigned int dtagcount;
7510
7511 dynobj = elf_hash_table (info)->dynobj;
7512
7513 /* Work out the size of the symbol version section. */
7514 s = bfd_get_linker_section (dynobj, ".gnu.version");
7515 BFD_ASSERT (s != NULL);
7516 if ((s->flags & SEC_EXCLUDE) == 0)
7517 {
7518 s->size = dynsymcount * sizeof (Elf_External_Versym);
7519 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7520 if (s->contents == NULL)
7521 return false;
7522
7523 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7524 return false;
7525 }
7526
7527 /* Set the size of the .dynsym and .hash sections. We counted
7528 the number of dynamic symbols in elf_link_add_object_symbols.
7529 We will build the contents of .dynsym and .hash when we build
7530 the final symbol table, because until then we do not know the
7531 correct value to give the symbols. We built the .dynstr
7532 section as we went along in elf_link_add_object_symbols. */
7533 s = elf_hash_table (info)->dynsym;
7534 BFD_ASSERT (s != NULL);
7535 s->size = dynsymcount * bed->s->sizeof_sym;
7536
7537 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7538 if (s->contents == NULL)
7539 return false;
7540
7541 /* The first entry in .dynsym is a dummy symbol. Clear all the
7542 section syms, in case we don't output them all. */
7543 ++section_sym_count;
7544 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
7545
7546 elf_hash_table (info)->bucketcount = 0;
7547
7548 /* Compute the size of the hashing table. As a side effect this
7549 computes the hash values for all the names we export. */
7550 if (info->emit_hash)
7551 {
7552 unsigned long int *hashcodes;
7553 struct hash_codes_info hashinf;
7554 bfd_size_type amt;
7555 unsigned long int nsyms;
7556 size_t bucketcount;
7557 size_t hash_entry_size;
7558
7559 /* Compute the hash values for all exported symbols. At the same
7560 time store the values in an array so that we could use them for
7561 optimizations. */
7562 amt = dynsymcount * sizeof (unsigned long int);
7563 hashcodes = (unsigned long int *) bfd_malloc (amt);
7564 if (hashcodes == NULL)
7565 return false;
7566 hashinf.hashcodes = hashcodes;
7567 hashinf.error = false;
7568
7569 /* Put all hash values in HASHCODES. */
7570 elf_link_hash_traverse (elf_hash_table (info),
7571 elf_collect_hash_codes, &hashinf);
7572 if (hashinf.error)
7573 {
7574 free (hashcodes);
7575 return false;
7576 }
7577
7578 nsyms = hashinf.hashcodes - hashcodes;
7579 bucketcount
7580 = compute_bucket_count (info, hashcodes, nsyms, 0);
7581 free (hashcodes);
7582
7583 if (bucketcount == 0 && nsyms > 0)
7584 return false;
7585
7586 elf_hash_table (info)->bucketcount = bucketcount;
7587
7588 s = bfd_get_linker_section (dynobj, ".hash");
7589 BFD_ASSERT (s != NULL);
7590 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7591 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
7592 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7593 if (s->contents == NULL)
7594 return false;
7595
7596 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7597 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7598 s->contents + hash_entry_size);
7599 }
7600
7601 if (info->emit_gnu_hash)
7602 {
7603 size_t i, cnt;
7604 unsigned char *contents;
7605 struct collect_gnu_hash_codes cinfo;
7606 bfd_size_type amt;
7607 size_t bucketcount;
7608
7609 memset (&cinfo, 0, sizeof (cinfo));
7610
7611 /* Compute the hash values for all exported symbols. At the same
7612 time store the values in an array so that we could use them for
7613 optimizations. */
7614 amt = dynsymcount * 2 * sizeof (unsigned long int);
7615 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
7616 if (cinfo.hashcodes == NULL)
7617 return false;
7618
7619 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7620 cinfo.min_dynindx = -1;
7621 cinfo.output_bfd = output_bfd;
7622 cinfo.bed = bed;
7623
7624 /* Put all hash values in HASHCODES. */
7625 elf_link_hash_traverse (elf_hash_table (info),
7626 elf_collect_gnu_hash_codes, &cinfo);
7627 if (cinfo.error)
7628 {
7629 free (cinfo.hashcodes);
7630 return false;
7631 }
7632
7633 bucketcount
7634 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7635
7636 if (bucketcount == 0)
7637 {
7638 free (cinfo.hashcodes);
7639 return false;
7640 }
7641
7642 s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (bed));
7643 BFD_ASSERT (s != NULL);
7644
7645 if (cinfo.nsyms == 0)
7646 {
7647 /* Empty .gnu.hash or .MIPS.xhash section is special. */
7648 BFD_ASSERT (cinfo.min_dynindx == -1);
7649 free (cinfo.hashcodes);
7650 s->size = 5 * 4 + bed->s->arch_size / 8;
7651 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7652 if (contents == NULL)
7653 return false;
7654 s->contents = contents;
7655 /* 1 empty bucket. */
7656 bfd_put_32 (output_bfd, 1, contents);
7657 /* SYMIDX above the special symbol 0. */
7658 bfd_put_32 (output_bfd, 1, contents + 4);
7659 /* Just one word for bitmask. */
7660 bfd_put_32 (output_bfd, 1, contents + 8);
7661 /* Only hash fn bloom filter. */
7662 bfd_put_32 (output_bfd, 0, contents + 12);
7663 /* No hashes are valid - empty bitmask. */
7664 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7665 /* No hashes in the only bucket. */
7666 bfd_put_32 (output_bfd, 0,
7667 contents + 16 + bed->s->arch_size / 8);
7668 }
7669 else
7670 {
7671 unsigned long int maskwords, maskbitslog2, x;
7672 BFD_ASSERT (cinfo.min_dynindx != -1);
7673
7674 x = cinfo.nsyms;
7675 maskbitslog2 = 1;
7676 while ((x >>= 1) != 0)
7677 ++maskbitslog2;
7678 if (maskbitslog2 < 3)
7679 maskbitslog2 = 5;
7680 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7681 maskbitslog2 = maskbitslog2 + 3;
7682 else
7683 maskbitslog2 = maskbitslog2 + 2;
7684 if (bed->s->arch_size == 64)
7685 {
7686 if (maskbitslog2 == 5)
7687 maskbitslog2 = 6;
7688 cinfo.shift1 = 6;
7689 }
7690 else
7691 cinfo.shift1 = 5;
7692 cinfo.mask = (1 << cinfo.shift1) - 1;
7693 cinfo.shift2 = maskbitslog2;
7694 cinfo.maskbits = 1 << maskbitslog2;
7695 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7696 amt = bucketcount * sizeof (unsigned long int) * 2;
7697 amt += maskwords * sizeof (bfd_vma);
7698 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
7699 if (cinfo.bitmask == NULL)
7700 {
7701 free (cinfo.hashcodes);
7702 return false;
7703 }
7704
7705 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
7706 cinfo.indx = cinfo.counts + bucketcount;
7707 cinfo.symindx = dynsymcount - cinfo.nsyms;
7708 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7709
7710 /* Determine how often each hash bucket is used. */
7711 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7712 for (i = 0; i < cinfo.nsyms; ++i)
7713 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7714
7715 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7716 if (cinfo.counts[i] != 0)
7717 {
7718 cinfo.indx[i] = cnt;
7719 cnt += cinfo.counts[i];
7720 }
7721 BFD_ASSERT (cnt == dynsymcount);
7722 cinfo.bucketcount = bucketcount;
7723 cinfo.local_indx = cinfo.min_dynindx;
7724
7725 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7726 s->size += cinfo.maskbits / 8;
7727 if (bed->record_xhash_symbol != NULL)
7728 s->size += cinfo.nsyms * 4;
7729 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7730 if (contents == NULL)
7731 {
7732 free (cinfo.bitmask);
7733 free (cinfo.hashcodes);
7734 return false;
7735 }
7736
7737 s->contents = contents;
7738 bfd_put_32 (output_bfd, bucketcount, contents);
7739 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7740 bfd_put_32 (output_bfd, maskwords, contents + 8);
7741 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7742 contents += 16 + cinfo.maskbits / 8;
7743
7744 for (i = 0; i < bucketcount; ++i)
7745 {
7746 if (cinfo.counts[i] == 0)
7747 bfd_put_32 (output_bfd, 0, contents);
7748 else
7749 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7750 contents += 4;
7751 }
7752
7753 cinfo.contents = contents;
7754
7755 cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents;
7756 /* Renumber dynamic symbols, if populating .gnu.hash section.
7757 If using .MIPS.xhash, populate the translation table. */
7758 elf_link_hash_traverse (elf_hash_table (info),
7759 elf_gnu_hash_process_symidx, &cinfo);
7760
7761 contents = s->contents + 16;
7762 for (i = 0; i < maskwords; ++i)
7763 {
7764 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7765 contents);
7766 contents += bed->s->arch_size / 8;
7767 }
7768
7769 free (cinfo.bitmask);
7770 free (cinfo.hashcodes);
7771 }
7772 }
7773
7774 s = bfd_get_linker_section (dynobj, ".dynstr");
7775 BFD_ASSERT (s != NULL);
7776
7777 elf_finalize_dynstr (output_bfd, info);
7778
7779 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7780
7781 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7782 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7783 return false;
7784 }
7785
7786 return true;
7787 }
7788 \f
7789 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7790
7791 static void
7792 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7793 asection *sec)
7794 {
7795 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7796 sec->sec_info_type = SEC_INFO_TYPE_NONE;
7797 }
7798
7799 /* Finish SHF_MERGE section merging. */
7800
7801 bool
7802 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
7803 {
7804 bfd *ibfd;
7805 asection *sec;
7806
7807 if (!is_elf_hash_table (info->hash))
7808 return false;
7809
7810 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7811 if ((ibfd->flags & DYNAMIC) == 0
7812 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
7813 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7814 == get_elf_backend_data (obfd)->s->elfclass))
7815 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7816 if ((sec->flags & SEC_MERGE) != 0
7817 && !bfd_is_abs_section (sec->output_section))
7818 {
7819 struct bfd_elf_section_data *secdata;
7820
7821 secdata = elf_section_data (sec);
7822 if (! _bfd_add_merge_section (obfd,
7823 &elf_hash_table (info)->merge_info,
7824 sec, &secdata->sec_info))
7825 return false;
7826 else if (secdata->sec_info)
7827 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
7828 }
7829
7830 if (elf_hash_table (info)->merge_info != NULL)
7831 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
7832 merge_sections_remove_hook);
7833 return true;
7834 }
7835
7836 /* Create an entry in an ELF linker hash table. */
7837
7838 struct bfd_hash_entry *
7839 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7840 struct bfd_hash_table *table,
7841 const char *string)
7842 {
7843 /* Allocate the structure if it has not already been allocated by a
7844 subclass. */
7845 if (entry == NULL)
7846 {
7847 entry = (struct bfd_hash_entry *)
7848 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
7849 if (entry == NULL)
7850 return entry;
7851 }
7852
7853 /* Call the allocation method of the superclass. */
7854 entry = _bfd_link_hash_newfunc (entry, table, string);
7855 if (entry != NULL)
7856 {
7857 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7858 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7859
7860 /* Set local fields. */
7861 ret->indx = -1;
7862 ret->dynindx = -1;
7863 ret->got = htab->init_got_refcount;
7864 ret->plt = htab->init_plt_refcount;
7865 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7866 - offsetof (struct elf_link_hash_entry, size)));
7867 /* Assume that we have been called by a non-ELF symbol reader.
7868 This flag is then reset by the code which reads an ELF input
7869 file. This ensures that a symbol created by a non-ELF symbol
7870 reader will have the flag set correctly. */
7871 ret->non_elf = 1;
7872 }
7873
7874 return entry;
7875 }
7876
7877 /* Copy data from an indirect symbol to its direct symbol, hiding the
7878 old indirect symbol. Also used for copying flags to a weakdef. */
7879
7880 void
7881 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7882 struct elf_link_hash_entry *dir,
7883 struct elf_link_hash_entry *ind)
7884 {
7885 struct elf_link_hash_table *htab;
7886
7887 if (ind->dyn_relocs != NULL)
7888 {
7889 if (dir->dyn_relocs != NULL)
7890 {
7891 struct elf_dyn_relocs **pp;
7892 struct elf_dyn_relocs *p;
7893
7894 /* Add reloc counts against the indirect sym to the direct sym
7895 list. Merge any entries against the same section. */
7896 for (pp = &ind->dyn_relocs; (p = *pp) != NULL; )
7897 {
7898 struct elf_dyn_relocs *q;
7899
7900 for (q = dir->dyn_relocs; q != NULL; q = q->next)
7901 if (q->sec == p->sec)
7902 {
7903 q->pc_count += p->pc_count;
7904 q->count += p->count;
7905 *pp = p->next;
7906 break;
7907 }
7908 if (q == NULL)
7909 pp = &p->next;
7910 }
7911 *pp = dir->dyn_relocs;
7912 }
7913
7914 dir->dyn_relocs = ind->dyn_relocs;
7915 ind->dyn_relocs = NULL;
7916 }
7917
7918 /* Copy down any references that we may have already seen to the
7919 symbol which just became indirect. */
7920
7921 if (dir->versioned != versioned_hidden)
7922 dir->ref_dynamic |= ind->ref_dynamic;
7923 dir->ref_regular |= ind->ref_regular;
7924 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7925 dir->non_got_ref |= ind->non_got_ref;
7926 dir->needs_plt |= ind->needs_plt;
7927 dir->pointer_equality_needed |= ind->pointer_equality_needed;
7928
7929 if (ind->root.type != bfd_link_hash_indirect)
7930 return;
7931
7932 /* Copy over the global and procedure linkage table refcount entries.
7933 These may have been already set up by a check_relocs routine. */
7934 htab = elf_hash_table (info);
7935 if (ind->got.refcount > htab->init_got_refcount.refcount)
7936 {
7937 if (dir->got.refcount < 0)
7938 dir->got.refcount = 0;
7939 dir->got.refcount += ind->got.refcount;
7940 ind->got.refcount = htab->init_got_refcount.refcount;
7941 }
7942
7943 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7944 {
7945 if (dir->plt.refcount < 0)
7946 dir->plt.refcount = 0;
7947 dir->plt.refcount += ind->plt.refcount;
7948 ind->plt.refcount = htab->init_plt_refcount.refcount;
7949 }
7950
7951 if (ind->dynindx != -1)
7952 {
7953 if (dir->dynindx != -1)
7954 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7955 dir->dynindx = ind->dynindx;
7956 dir->dynstr_index = ind->dynstr_index;
7957 ind->dynindx = -1;
7958 ind->dynstr_index = 0;
7959 }
7960 }
7961
7962 void
7963 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7964 struct elf_link_hash_entry *h,
7965 bool force_local)
7966 {
7967 /* STT_GNU_IFUNC symbol must go through PLT. */
7968 if (h->type != STT_GNU_IFUNC)
7969 {
7970 h->plt = elf_hash_table (info)->init_plt_offset;
7971 h->needs_plt = 0;
7972 }
7973 if (force_local)
7974 {
7975 h->forced_local = 1;
7976 if (h->dynindx != -1)
7977 {
7978 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7979 h->dynstr_index);
7980 h->dynindx = -1;
7981 h->dynstr_index = 0;
7982 }
7983 }
7984 }
7985
7986 /* Hide a symbol. */
7987
7988 void
7989 _bfd_elf_link_hide_symbol (bfd *output_bfd,
7990 struct bfd_link_info *info,
7991 struct bfd_link_hash_entry *h)
7992 {
7993 if (is_elf_hash_table (info->hash))
7994 {
7995 const struct elf_backend_data *bed
7996 = get_elf_backend_data (output_bfd);
7997 struct elf_link_hash_entry *eh
7998 = (struct elf_link_hash_entry *) h;
7999 bed->elf_backend_hide_symbol (info, eh, true);
8000 eh->def_dynamic = 0;
8001 eh->ref_dynamic = 0;
8002 eh->dynamic_def = 0;
8003 }
8004 }
8005
8006 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
8007 caller. */
8008
8009 bool
8010 _bfd_elf_link_hash_table_init
8011 (struct elf_link_hash_table *table,
8012 bfd *abfd,
8013 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
8014 struct bfd_hash_table *,
8015 const char *),
8016 unsigned int entsize,
8017 enum elf_target_id target_id)
8018 {
8019 bool ret;
8020 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
8021
8022 table->init_got_refcount.refcount = can_refcount - 1;
8023 table->init_plt_refcount.refcount = can_refcount - 1;
8024 table->init_got_offset.offset = -(bfd_vma) 1;
8025 table->init_plt_offset.offset = -(bfd_vma) 1;
8026 /* The first dynamic symbol is a dummy. */
8027 table->dynsymcount = 1;
8028
8029 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
8030
8031 table->root.type = bfd_link_elf_hash_table;
8032 table->hash_table_id = target_id;
8033 table->target_os = get_elf_backend_data (abfd)->target_os;
8034
8035 return ret;
8036 }
8037
8038 /* Create an ELF linker hash table. */
8039
8040 struct bfd_link_hash_table *
8041 _bfd_elf_link_hash_table_create (bfd *abfd)
8042 {
8043 struct elf_link_hash_table *ret;
8044 size_t amt = sizeof (struct elf_link_hash_table);
8045
8046 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
8047 if (ret == NULL)
8048 return NULL;
8049
8050 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
8051 sizeof (struct elf_link_hash_entry),
8052 GENERIC_ELF_DATA))
8053 {
8054 free (ret);
8055 return NULL;
8056 }
8057 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
8058
8059 return &ret->root;
8060 }
8061
8062 /* Destroy an ELF linker hash table. */
8063
8064 void
8065 _bfd_elf_link_hash_table_free (bfd *obfd)
8066 {
8067 struct elf_link_hash_table *htab;
8068
8069 htab = (struct elf_link_hash_table *) obfd->link.hash;
8070 if (htab->dynstr != NULL)
8071 _bfd_elf_strtab_free (htab->dynstr);
8072 _bfd_merge_sections_free (htab->merge_info);
8073 _bfd_generic_link_hash_table_free (obfd);
8074 }
8075
8076 /* This is a hook for the ELF emulation code in the generic linker to
8077 tell the backend linker what file name to use for the DT_NEEDED
8078 entry for a dynamic object. */
8079
8080 void
8081 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
8082 {
8083 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8084 && bfd_get_format (abfd) == bfd_object)
8085 elf_dt_name (abfd) = name;
8086 }
8087
8088 int
8089 bfd_elf_get_dyn_lib_class (bfd *abfd)
8090 {
8091 int lib_class;
8092 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8093 && bfd_get_format (abfd) == bfd_object)
8094 lib_class = elf_dyn_lib_class (abfd);
8095 else
8096 lib_class = 0;
8097 return lib_class;
8098 }
8099
8100 void
8101 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
8102 {
8103 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8104 && bfd_get_format (abfd) == bfd_object)
8105 elf_dyn_lib_class (abfd) = lib_class;
8106 }
8107
8108 /* Get the list of DT_NEEDED entries for a link. This is a hook for
8109 the linker ELF emulation code. */
8110
8111 struct bfd_link_needed_list *
8112 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
8113 struct bfd_link_info *info)
8114 {
8115 if (! is_elf_hash_table (info->hash))
8116 return NULL;
8117 return elf_hash_table (info)->needed;
8118 }
8119
8120 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
8121 hook for the linker ELF emulation code. */
8122
8123 struct bfd_link_needed_list *
8124 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
8125 struct bfd_link_info *info)
8126 {
8127 if (! is_elf_hash_table (info->hash))
8128 return NULL;
8129 return elf_hash_table (info)->runpath;
8130 }
8131
8132 /* Get the name actually used for a dynamic object for a link. This
8133 is the SONAME entry if there is one. Otherwise, it is the string
8134 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
8135
8136 const char *
8137 bfd_elf_get_dt_soname (bfd *abfd)
8138 {
8139 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8140 && bfd_get_format (abfd) == bfd_object)
8141 return elf_dt_name (abfd);
8142 return NULL;
8143 }
8144
8145 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
8146 the ELF linker emulation code. */
8147
8148 bool
8149 bfd_elf_get_bfd_needed_list (bfd *abfd,
8150 struct bfd_link_needed_list **pneeded)
8151 {
8152 asection *s;
8153 bfd_byte *dynbuf = NULL;
8154 unsigned int elfsec;
8155 unsigned long shlink;
8156 bfd_byte *extdyn, *extdynend;
8157 size_t extdynsize;
8158 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
8159
8160 *pneeded = NULL;
8161
8162 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
8163 || bfd_get_format (abfd) != bfd_object)
8164 return true;
8165
8166 s = bfd_get_section_by_name (abfd, ".dynamic");
8167 if (s == NULL || s->size == 0)
8168 return true;
8169
8170 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
8171 goto error_return;
8172
8173 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
8174 if (elfsec == SHN_BAD)
8175 goto error_return;
8176
8177 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
8178
8179 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
8180 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
8181
8182 extdyn = dynbuf;
8183 extdynend = extdyn + s->size;
8184 for (; extdyn < extdynend; extdyn += extdynsize)
8185 {
8186 Elf_Internal_Dyn dyn;
8187
8188 (*swap_dyn_in) (abfd, extdyn, &dyn);
8189
8190 if (dyn.d_tag == DT_NULL)
8191 break;
8192
8193 if (dyn.d_tag == DT_NEEDED)
8194 {
8195 const char *string;
8196 struct bfd_link_needed_list *l;
8197 unsigned int tagv = dyn.d_un.d_val;
8198 size_t amt;
8199
8200 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
8201 if (string == NULL)
8202 goto error_return;
8203
8204 amt = sizeof *l;
8205 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
8206 if (l == NULL)
8207 goto error_return;
8208
8209 l->by = abfd;
8210 l->name = string;
8211 l->next = *pneeded;
8212 *pneeded = l;
8213 }
8214 }
8215
8216 free (dynbuf);
8217
8218 return true;
8219
8220 error_return:
8221 free (dynbuf);
8222 return false;
8223 }
8224
8225 struct elf_symbuf_symbol
8226 {
8227 unsigned long st_name; /* Symbol name, index in string tbl */
8228 unsigned char st_info; /* Type and binding attributes */
8229 unsigned char st_other; /* Visibilty, and target specific */
8230 };
8231
8232 struct elf_symbuf_head
8233 {
8234 struct elf_symbuf_symbol *ssym;
8235 size_t count;
8236 unsigned int st_shndx;
8237 };
8238
8239 struct elf_symbol
8240 {
8241 union
8242 {
8243 Elf_Internal_Sym *isym;
8244 struct elf_symbuf_symbol *ssym;
8245 void *p;
8246 } u;
8247 const char *name;
8248 };
8249
8250 /* Sort references to symbols by ascending section number. */
8251
8252 static int
8253 elf_sort_elf_symbol (const void *arg1, const void *arg2)
8254 {
8255 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
8256 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
8257
8258 if (s1->st_shndx != s2->st_shndx)
8259 return s1->st_shndx > s2->st_shndx ? 1 : -1;
8260 /* Final sort by the address of the sym in the symbuf ensures
8261 a stable sort. */
8262 if (s1 != s2)
8263 return s1 > s2 ? 1 : -1;
8264 return 0;
8265 }
8266
8267 static int
8268 elf_sym_name_compare (const void *arg1, const void *arg2)
8269 {
8270 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
8271 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
8272 int ret = strcmp (s1->name, s2->name);
8273 if (ret != 0)
8274 return ret;
8275 if (s1->u.p != s2->u.p)
8276 return s1->u.p > s2->u.p ? 1 : -1;
8277 return 0;
8278 }
8279
8280 static struct elf_symbuf_head *
8281 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
8282 {
8283 Elf_Internal_Sym **ind, **indbufend, **indbuf;
8284 struct elf_symbuf_symbol *ssym;
8285 struct elf_symbuf_head *ssymbuf, *ssymhead;
8286 size_t i, shndx_count, total_size, amt;
8287
8288 amt = symcount * sizeof (*indbuf);
8289 indbuf = (Elf_Internal_Sym **) bfd_malloc (amt);
8290 if (indbuf == NULL)
8291 return NULL;
8292
8293 for (ind = indbuf, i = 0; i < symcount; i++)
8294 if (isymbuf[i].st_shndx != SHN_UNDEF)
8295 *ind++ = &isymbuf[i];
8296 indbufend = ind;
8297
8298 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
8299 elf_sort_elf_symbol);
8300
8301 shndx_count = 0;
8302 if (indbufend > indbuf)
8303 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
8304 if (ind[0]->st_shndx != ind[1]->st_shndx)
8305 shndx_count++;
8306
8307 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
8308 + (indbufend - indbuf) * sizeof (*ssym));
8309 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
8310 if (ssymbuf == NULL)
8311 {
8312 free (indbuf);
8313 return NULL;
8314 }
8315
8316 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
8317 ssymbuf->ssym = NULL;
8318 ssymbuf->count = shndx_count;
8319 ssymbuf->st_shndx = 0;
8320 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
8321 {
8322 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
8323 {
8324 ssymhead++;
8325 ssymhead->ssym = ssym;
8326 ssymhead->count = 0;
8327 ssymhead->st_shndx = (*ind)->st_shndx;
8328 }
8329 ssym->st_name = (*ind)->st_name;
8330 ssym->st_info = (*ind)->st_info;
8331 ssym->st_other = (*ind)->st_other;
8332 ssymhead->count++;
8333 }
8334 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
8335 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
8336 == total_size));
8337
8338 free (indbuf);
8339 return ssymbuf;
8340 }
8341
8342 /* Check if 2 sections define the same set of local and global
8343 symbols. */
8344
8345 static bool
8346 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
8347 struct bfd_link_info *info)
8348 {
8349 bfd *bfd1, *bfd2;
8350 const struct elf_backend_data *bed1, *bed2;
8351 Elf_Internal_Shdr *hdr1, *hdr2;
8352 size_t symcount1, symcount2;
8353 Elf_Internal_Sym *isymbuf1, *isymbuf2;
8354 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
8355 Elf_Internal_Sym *isym, *isymend;
8356 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
8357 size_t count1, count2, sec_count1, sec_count2, i;
8358 unsigned int shndx1, shndx2;
8359 bool result;
8360 bool ignore_section_symbol_p;
8361
8362 bfd1 = sec1->owner;
8363 bfd2 = sec2->owner;
8364
8365 /* Both sections have to be in ELF. */
8366 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
8367 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
8368 return false;
8369
8370 if (elf_section_type (sec1) != elf_section_type (sec2))
8371 return false;
8372
8373 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
8374 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
8375 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
8376 return false;
8377
8378 bed1 = get_elf_backend_data (bfd1);
8379 bed2 = get_elf_backend_data (bfd2);
8380 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
8381 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
8382 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
8383 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
8384
8385 if (symcount1 == 0 || symcount2 == 0)
8386 return false;
8387
8388 result = false;
8389 isymbuf1 = NULL;
8390 isymbuf2 = NULL;
8391 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
8392 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
8393
8394 /* Ignore section symbols only when matching non-debugging sections
8395 or linkonce section with comdat section. */
8396 ignore_section_symbol_p
8397 = ((sec1->flags & SEC_DEBUGGING) == 0
8398 || ((elf_section_flags (sec1) & SHF_GROUP)
8399 != (elf_section_flags (sec2) & SHF_GROUP)));
8400
8401 if (ssymbuf1 == NULL)
8402 {
8403 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
8404 NULL, NULL, NULL);
8405 if (isymbuf1 == NULL)
8406 goto done;
8407
8408 if (info != NULL && !info->reduce_memory_overheads)
8409 {
8410 ssymbuf1 = elf_create_symbuf (symcount1, isymbuf1);
8411 elf_tdata (bfd1)->symbuf = ssymbuf1;
8412 }
8413 }
8414
8415 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
8416 {
8417 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
8418 NULL, NULL, NULL);
8419 if (isymbuf2 == NULL)
8420 goto done;
8421
8422 if (ssymbuf1 != NULL && info != NULL && !info->reduce_memory_overheads)
8423 {
8424 ssymbuf2 = elf_create_symbuf (symcount2, isymbuf2);
8425 elf_tdata (bfd2)->symbuf = ssymbuf2;
8426 }
8427 }
8428
8429 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
8430 {
8431 /* Optimized faster version. */
8432 size_t lo, hi, mid;
8433 struct elf_symbol *symp;
8434 struct elf_symbuf_symbol *ssym, *ssymend;
8435
8436 lo = 0;
8437 hi = ssymbuf1->count;
8438 ssymbuf1++;
8439 count1 = 0;
8440 sec_count1 = 0;
8441 while (lo < hi)
8442 {
8443 mid = (lo + hi) / 2;
8444 if (shndx1 < ssymbuf1[mid].st_shndx)
8445 hi = mid;
8446 else if (shndx1 > ssymbuf1[mid].st_shndx)
8447 lo = mid + 1;
8448 else
8449 {
8450 count1 = ssymbuf1[mid].count;
8451 ssymbuf1 += mid;
8452 break;
8453 }
8454 }
8455 if (ignore_section_symbol_p)
8456 {
8457 for (i = 0; i < count1; i++)
8458 if (ELF_ST_TYPE (ssymbuf1->ssym[i].st_info) == STT_SECTION)
8459 sec_count1++;
8460 count1 -= sec_count1;
8461 }
8462
8463 lo = 0;
8464 hi = ssymbuf2->count;
8465 ssymbuf2++;
8466 count2 = 0;
8467 sec_count2 = 0;
8468 while (lo < hi)
8469 {
8470 mid = (lo + hi) / 2;
8471 if (shndx2 < ssymbuf2[mid].st_shndx)
8472 hi = mid;
8473 else if (shndx2 > ssymbuf2[mid].st_shndx)
8474 lo = mid + 1;
8475 else
8476 {
8477 count2 = ssymbuf2[mid].count;
8478 ssymbuf2 += mid;
8479 break;
8480 }
8481 }
8482 if (ignore_section_symbol_p)
8483 {
8484 for (i = 0; i < count2; i++)
8485 if (ELF_ST_TYPE (ssymbuf2->ssym[i].st_info) == STT_SECTION)
8486 sec_count2++;
8487 count2 -= sec_count2;
8488 }
8489
8490 if (count1 == 0 || count2 == 0 || count1 != count2)
8491 goto done;
8492
8493 symtable1
8494 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
8495 symtable2
8496 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
8497 if (symtable1 == NULL || symtable2 == NULL)
8498 goto done;
8499
8500 symp = symtable1;
8501 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1 + sec_count1;
8502 ssym < ssymend; ssym++)
8503 if (sec_count1 == 0
8504 || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
8505 {
8506 symp->u.ssym = ssym;
8507 symp->name = bfd_elf_string_from_elf_section (bfd1,
8508 hdr1->sh_link,
8509 ssym->st_name);
8510 symp++;
8511 }
8512
8513 symp = symtable2;
8514 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2 + sec_count2;
8515 ssym < ssymend; ssym++)
8516 if (sec_count2 == 0
8517 || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
8518 {
8519 symp->u.ssym = ssym;
8520 symp->name = bfd_elf_string_from_elf_section (bfd2,
8521 hdr2->sh_link,
8522 ssym->st_name);
8523 symp++;
8524 }
8525
8526 /* Sort symbol by name. */
8527 qsort (symtable1, count1, sizeof (struct elf_symbol),
8528 elf_sym_name_compare);
8529 qsort (symtable2, count1, sizeof (struct elf_symbol),
8530 elf_sym_name_compare);
8531
8532 for (i = 0; i < count1; i++)
8533 /* Two symbols must have the same binding, type and name. */
8534 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8535 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8536 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8537 goto done;
8538
8539 result = true;
8540 goto done;
8541 }
8542
8543 symtable1 = (struct elf_symbol *)
8544 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8545 symtable2 = (struct elf_symbol *)
8546 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
8547 if (symtable1 == NULL || symtable2 == NULL)
8548 goto done;
8549
8550 /* Count definitions in the section. */
8551 count1 = 0;
8552 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
8553 if (isym->st_shndx == shndx1
8554 && (!ignore_section_symbol_p
8555 || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
8556 symtable1[count1++].u.isym = isym;
8557
8558 count2 = 0;
8559 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
8560 if (isym->st_shndx == shndx2
8561 && (!ignore_section_symbol_p
8562 || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
8563 symtable2[count2++].u.isym = isym;
8564
8565 if (count1 == 0 || count2 == 0 || count1 != count2)
8566 goto done;
8567
8568 for (i = 0; i < count1; i++)
8569 symtable1[i].name
8570 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8571 symtable1[i].u.isym->st_name);
8572
8573 for (i = 0; i < count2; i++)
8574 symtable2[i].name
8575 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8576 symtable2[i].u.isym->st_name);
8577
8578 /* Sort symbol by name. */
8579 qsort (symtable1, count1, sizeof (struct elf_symbol),
8580 elf_sym_name_compare);
8581 qsort (symtable2, count1, sizeof (struct elf_symbol),
8582 elf_sym_name_compare);
8583
8584 for (i = 0; i < count1; i++)
8585 /* Two symbols must have the same binding, type and name. */
8586 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8587 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8588 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8589 goto done;
8590
8591 result = true;
8592
8593 done:
8594 free (symtable1);
8595 free (symtable2);
8596 free (isymbuf1);
8597 free (isymbuf2);
8598
8599 return result;
8600 }
8601
8602 /* Return TRUE if 2 section types are compatible. */
8603
8604 bool
8605 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8606 bfd *bbfd, const asection *bsec)
8607 {
8608 if (asec == NULL
8609 || bsec == NULL
8610 || abfd->xvec->flavour != bfd_target_elf_flavour
8611 || bbfd->xvec->flavour != bfd_target_elf_flavour)
8612 return true;
8613
8614 return elf_section_type (asec) == elf_section_type (bsec);
8615 }
8616 \f
8617 /* Final phase of ELF linker. */
8618
8619 /* A structure we use to avoid passing large numbers of arguments. */
8620
8621 struct elf_final_link_info
8622 {
8623 /* General link information. */
8624 struct bfd_link_info *info;
8625 /* Output BFD. */
8626 bfd *output_bfd;
8627 /* Symbol string table. */
8628 struct elf_strtab_hash *symstrtab;
8629 /* .hash section. */
8630 asection *hash_sec;
8631 /* symbol version section (.gnu.version). */
8632 asection *symver_sec;
8633 /* Buffer large enough to hold contents of any section. */
8634 bfd_byte *contents;
8635 /* Buffer large enough to hold external relocs of any section. */
8636 void *external_relocs;
8637 /* Buffer large enough to hold internal relocs of any section. */
8638 Elf_Internal_Rela *internal_relocs;
8639 /* Buffer large enough to hold external local symbols of any input
8640 BFD. */
8641 bfd_byte *external_syms;
8642 /* And a buffer for symbol section indices. */
8643 Elf_External_Sym_Shndx *locsym_shndx;
8644 /* Buffer large enough to hold internal local symbols of any input
8645 BFD. */
8646 Elf_Internal_Sym *internal_syms;
8647 /* Array large enough to hold a symbol index for each local symbol
8648 of any input BFD. */
8649 long *indices;
8650 /* Array large enough to hold a section pointer for each local
8651 symbol of any input BFD. */
8652 asection **sections;
8653 /* Buffer for SHT_SYMTAB_SHNDX section. */
8654 Elf_External_Sym_Shndx *symshndxbuf;
8655 /* Number of STT_FILE syms seen. */
8656 size_t filesym_count;
8657 /* Local symbol hash table. */
8658 struct bfd_hash_table local_hash_table;
8659 };
8660
8661 struct local_hash_entry
8662 {
8663 /* Base hash table entry structure. */
8664 struct bfd_hash_entry root;
8665 /* Size of the local symbol name. */
8666 size_t size;
8667 /* Number of the duplicated local symbol names. */
8668 long count;
8669 };
8670
8671 /* Create an entry in the local symbol hash table. */
8672
8673 static struct bfd_hash_entry *
8674 local_hash_newfunc (struct bfd_hash_entry *entry,
8675 struct bfd_hash_table *table,
8676 const char *string)
8677 {
8678
8679 /* Allocate the structure if it has not already been allocated by a
8680 subclass. */
8681 if (entry == NULL)
8682 {
8683 entry = bfd_hash_allocate (table,
8684 sizeof (struct local_hash_entry));
8685 if (entry == NULL)
8686 return entry;
8687 }
8688
8689 /* Call the allocation method of the superclass. */
8690 entry = bfd_hash_newfunc (entry, table, string);
8691 if (entry != NULL)
8692 {
8693 ((struct local_hash_entry *) entry)->count = 0;
8694 ((struct local_hash_entry *) entry)->size = 0;
8695 }
8696
8697 return entry;
8698 }
8699
8700 /* This struct is used to pass information to elf_link_output_extsym. */
8701
8702 struct elf_outext_info
8703 {
8704 bool failed;
8705 bool localsyms;
8706 bool file_sym_done;
8707 struct elf_final_link_info *flinfo;
8708 };
8709
8710
8711 /* Support for evaluating a complex relocation.
8712
8713 Complex relocations are generalized, self-describing relocations. The
8714 implementation of them consists of two parts: complex symbols, and the
8715 relocations themselves.
8716
8717 The relocations use a reserved elf-wide relocation type code (R_RELC
8718 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8719 information (start bit, end bit, word width, etc) into the addend. This
8720 information is extracted from CGEN-generated operand tables within gas.
8721
8722 Complex symbols are mangled symbols (STT_RELC external / BSF_RELC
8723 internal) representing prefix-notation expressions, including but not
8724 limited to those sorts of expressions normally encoded as addends in the
8725 addend field. The symbol mangling format is:
8726
8727 <node> := <literal>
8728 | <unary-operator> ':' <node>
8729 | <binary-operator> ':' <node> ':' <node>
8730 ;
8731
8732 <literal> := 's' <digits=N> ':' <N character symbol name>
8733 | 'S' <digits=N> ':' <N character section name>
8734 | '#' <hexdigits>
8735 ;
8736
8737 <binary-operator> := as in C
8738 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8739
8740 static void
8741 set_symbol_value (bfd *bfd_with_globals,
8742 Elf_Internal_Sym *isymbuf,
8743 size_t locsymcount,
8744 size_t symidx,
8745 bfd_vma val)
8746 {
8747 struct elf_link_hash_entry **sym_hashes;
8748 struct elf_link_hash_entry *h;
8749 size_t extsymoff = locsymcount;
8750
8751 if (symidx < locsymcount)
8752 {
8753 Elf_Internal_Sym *sym;
8754
8755 sym = isymbuf + symidx;
8756 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8757 {
8758 /* It is a local symbol: move it to the
8759 "absolute" section and give it a value. */
8760 sym->st_shndx = SHN_ABS;
8761 sym->st_value = val;
8762 return;
8763 }
8764 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8765 extsymoff = 0;
8766 }
8767
8768 /* It is a global symbol: set its link type
8769 to "defined" and give it a value. */
8770
8771 sym_hashes = elf_sym_hashes (bfd_with_globals);
8772 h = sym_hashes [symidx - extsymoff];
8773 while (h->root.type == bfd_link_hash_indirect
8774 || h->root.type == bfd_link_hash_warning)
8775 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8776 h->root.type = bfd_link_hash_defined;
8777 h->root.u.def.value = val;
8778 h->root.u.def.section = bfd_abs_section_ptr;
8779 }
8780
8781 static bool
8782 resolve_symbol (const char *name,
8783 bfd *input_bfd,
8784 struct elf_final_link_info *flinfo,
8785 bfd_vma *result,
8786 Elf_Internal_Sym *isymbuf,
8787 size_t locsymcount)
8788 {
8789 Elf_Internal_Sym *sym;
8790 struct bfd_link_hash_entry *global_entry;
8791 const char *candidate = NULL;
8792 Elf_Internal_Shdr *symtab_hdr;
8793 size_t i;
8794
8795 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8796
8797 for (i = 0; i < locsymcount; ++ i)
8798 {
8799 sym = isymbuf + i;
8800
8801 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8802 continue;
8803
8804 candidate = bfd_elf_string_from_elf_section (input_bfd,
8805 symtab_hdr->sh_link,
8806 sym->st_name);
8807 #ifdef DEBUG
8808 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8809 name, candidate, (unsigned long) sym->st_value);
8810 #endif
8811 if (candidate && strcmp (candidate, name) == 0)
8812 {
8813 asection *sec = flinfo->sections [i];
8814
8815 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8816 *result += sec->output_offset + sec->output_section->vma;
8817 #ifdef DEBUG
8818 printf ("Found symbol with value %8.8lx\n",
8819 (unsigned long) *result);
8820 #endif
8821 return true;
8822 }
8823 }
8824
8825 /* Hmm, haven't found it yet. perhaps it is a global. */
8826 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
8827 false, false, true);
8828 if (!global_entry)
8829 return false;
8830
8831 if (global_entry->type == bfd_link_hash_defined
8832 || global_entry->type == bfd_link_hash_defweak)
8833 {
8834 *result = (global_entry->u.def.value
8835 + global_entry->u.def.section->output_section->vma
8836 + global_entry->u.def.section->output_offset);
8837 #ifdef DEBUG
8838 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8839 global_entry->root.string, (unsigned long) *result);
8840 #endif
8841 return true;
8842 }
8843
8844 return false;
8845 }
8846
8847 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8848 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8849 names like "foo.end" which is the end address of section "foo". */
8850
8851 static bool
8852 resolve_section (const char *name,
8853 asection *sections,
8854 bfd_vma *result,
8855 bfd * abfd)
8856 {
8857 asection *curr;
8858 unsigned int len;
8859
8860 for (curr = sections; curr; curr = curr->next)
8861 if (strcmp (curr->name, name) == 0)
8862 {
8863 *result = curr->vma;
8864 return true;
8865 }
8866
8867 /* Hmm. still haven't found it. try pseudo-section names. */
8868 /* FIXME: This could be coded more efficiently... */
8869 for (curr = sections; curr; curr = curr->next)
8870 {
8871 len = strlen (curr->name);
8872 if (len > strlen (name))
8873 continue;
8874
8875 if (strncmp (curr->name, name, len) == 0)
8876 {
8877 if (startswith (name + len, ".end"))
8878 {
8879 *result = (curr->vma
8880 + curr->size / bfd_octets_per_byte (abfd, curr));
8881 return true;
8882 }
8883
8884 /* Insert more pseudo-section names here, if you like. */
8885 }
8886 }
8887
8888 return false;
8889 }
8890
8891 static void
8892 undefined_reference (const char *reftype, const char *name)
8893 {
8894 /* xgettext:c-format */
8895 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8896 reftype, name);
8897 bfd_set_error (bfd_error_bad_value);
8898 }
8899
8900 static bool
8901 eval_symbol (bfd_vma *result,
8902 const char **symp,
8903 bfd *input_bfd,
8904 struct elf_final_link_info *flinfo,
8905 bfd_vma dot,
8906 Elf_Internal_Sym *isymbuf,
8907 size_t locsymcount,
8908 int signed_p)
8909 {
8910 size_t len;
8911 size_t symlen;
8912 bfd_vma a;
8913 bfd_vma b;
8914 char symbuf[4096];
8915 const char *sym = *symp;
8916 const char *symend;
8917 bool symbol_is_section = false;
8918
8919 len = strlen (sym);
8920 symend = sym + len;
8921
8922 if (len < 1 || len > sizeof (symbuf))
8923 {
8924 bfd_set_error (bfd_error_invalid_operation);
8925 return false;
8926 }
8927
8928 switch (* sym)
8929 {
8930 case '.':
8931 *result = dot;
8932 *symp = sym + 1;
8933 return true;
8934
8935 case '#':
8936 ++sym;
8937 *result = strtoul (sym, (char **) symp, 16);
8938 return true;
8939
8940 case 'S':
8941 symbol_is_section = true;
8942 /* Fall through. */
8943 case 's':
8944 ++sym;
8945 symlen = strtol (sym, (char **) symp, 10);
8946 sym = *symp + 1; /* Skip the trailing ':'. */
8947
8948 if (symend < sym || symlen + 1 > sizeof (symbuf))
8949 {
8950 bfd_set_error (bfd_error_invalid_operation);
8951 return false;
8952 }
8953
8954 memcpy (symbuf, sym, symlen);
8955 symbuf[symlen] = '\0';
8956 *symp = sym + symlen;
8957
8958 /* Is it always possible, with complex symbols, that gas "mis-guessed"
8959 the symbol as a section, or vice-versa. so we're pretty liberal in our
8960 interpretation here; section means "try section first", not "must be a
8961 section", and likewise with symbol. */
8962
8963 if (symbol_is_section)
8964 {
8965 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8966 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8967 isymbuf, locsymcount))
8968 {
8969 undefined_reference ("section", symbuf);
8970 return false;
8971 }
8972 }
8973 else
8974 {
8975 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8976 isymbuf, locsymcount)
8977 && !resolve_section (symbuf, flinfo->output_bfd->sections,
8978 result, input_bfd))
8979 {
8980 undefined_reference ("symbol", symbuf);
8981 return false;
8982 }
8983 }
8984
8985 return true;
8986
8987 /* All that remains are operators. */
8988
8989 #define UNARY_OP(op) \
8990 if (startswith (sym, #op)) \
8991 { \
8992 sym += strlen (#op); \
8993 if (*sym == ':') \
8994 ++sym; \
8995 *symp = sym; \
8996 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8997 isymbuf, locsymcount, signed_p)) \
8998 return false; \
8999 if (signed_p) \
9000 *result = op ((bfd_signed_vma) a); \
9001 else \
9002 *result = op a; \
9003 return true; \
9004 }
9005
9006 #define BINARY_OP_HEAD(op) \
9007 if (startswith (sym, #op)) \
9008 { \
9009 sym += strlen (#op); \
9010 if (*sym == ':') \
9011 ++sym; \
9012 *symp = sym; \
9013 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
9014 isymbuf, locsymcount, signed_p)) \
9015 return false; \
9016 ++*symp; \
9017 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
9018 isymbuf, locsymcount, signed_p)) \
9019 return false;
9020 #define BINARY_OP_TAIL(op) \
9021 if (signed_p) \
9022 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
9023 else \
9024 *result = a op b; \
9025 return true; \
9026 }
9027 #define BINARY_OP(op) BINARY_OP_HEAD(op) BINARY_OP_TAIL(op)
9028
9029 default:
9030 UNARY_OP (0-);
9031 BINARY_OP_HEAD (<<);
9032 if (b >= sizeof (a) * CHAR_BIT)
9033 {
9034 *result = 0;
9035 return true;
9036 }
9037 signed_p = 0;
9038 BINARY_OP_TAIL (<<);
9039 BINARY_OP_HEAD (>>);
9040 if (b >= sizeof (a) * CHAR_BIT)
9041 {
9042 *result = signed_p && (bfd_signed_vma) a < 0 ? -1 : 0;
9043 return true;
9044 }
9045 BINARY_OP_TAIL (>>);
9046 BINARY_OP (==);
9047 BINARY_OP (!=);
9048 BINARY_OP (<=);
9049 BINARY_OP (>=);
9050 BINARY_OP (&&);
9051 BINARY_OP (||);
9052 UNARY_OP (~);
9053 UNARY_OP (!);
9054 BINARY_OP (*);
9055 BINARY_OP_HEAD (/);
9056 if (b == 0)
9057 {
9058 _bfd_error_handler (_("division by zero"));
9059 bfd_set_error (bfd_error_bad_value);
9060 return false;
9061 }
9062 BINARY_OP_TAIL (/);
9063 BINARY_OP_HEAD (%);
9064 if (b == 0)
9065 {
9066 _bfd_error_handler (_("division by zero"));
9067 bfd_set_error (bfd_error_bad_value);
9068 return false;
9069 }
9070 BINARY_OP_TAIL (%);
9071 BINARY_OP (^);
9072 BINARY_OP (|);
9073 BINARY_OP (&);
9074 BINARY_OP (+);
9075 BINARY_OP (-);
9076 BINARY_OP (<);
9077 BINARY_OP (>);
9078 #undef UNARY_OP
9079 #undef BINARY_OP
9080 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
9081 bfd_set_error (bfd_error_invalid_operation);
9082 return false;
9083 }
9084 }
9085
9086 static void
9087 put_value (bfd_vma size,
9088 unsigned long chunksz,
9089 bfd *input_bfd,
9090 bfd_vma x,
9091 bfd_byte *location)
9092 {
9093 location += (size - chunksz);
9094
9095 for (; size; size -= chunksz, location -= chunksz)
9096 {
9097 switch (chunksz)
9098 {
9099 case 1:
9100 bfd_put_8 (input_bfd, x, location);
9101 x >>= 8;
9102 break;
9103 case 2:
9104 bfd_put_16 (input_bfd, x, location);
9105 x >>= 16;
9106 break;
9107 case 4:
9108 bfd_put_32 (input_bfd, x, location);
9109 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
9110 x >>= 16;
9111 x >>= 16;
9112 break;
9113 #ifdef BFD64
9114 case 8:
9115 bfd_put_64 (input_bfd, x, location);
9116 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
9117 x >>= 32;
9118 x >>= 32;
9119 break;
9120 #endif
9121 default:
9122 abort ();
9123 break;
9124 }
9125 }
9126 }
9127
9128 static bfd_vma
9129 get_value (bfd_vma size,
9130 unsigned long chunksz,
9131 bfd *input_bfd,
9132 bfd_byte *location)
9133 {
9134 int shift;
9135 bfd_vma x = 0;
9136
9137 /* Sanity checks. */
9138 BFD_ASSERT (chunksz <= sizeof (x)
9139 && size >= chunksz
9140 && chunksz != 0
9141 && (size % chunksz) == 0
9142 && input_bfd != NULL
9143 && location != NULL);
9144
9145 if (chunksz == sizeof (x))
9146 {
9147 BFD_ASSERT (size == chunksz);
9148
9149 /* Make sure that we do not perform an undefined shift operation.
9150 We know that size == chunksz so there will only be one iteration
9151 of the loop below. */
9152 shift = 0;
9153 }
9154 else
9155 shift = 8 * chunksz;
9156
9157 for (; size; size -= chunksz, location += chunksz)
9158 {
9159 switch (chunksz)
9160 {
9161 case 1:
9162 x = (x << shift) | bfd_get_8 (input_bfd, location);
9163 break;
9164 case 2:
9165 x = (x << shift) | bfd_get_16 (input_bfd, location);
9166 break;
9167 case 4:
9168 x = (x << shift) | bfd_get_32 (input_bfd, location);
9169 break;
9170 #ifdef BFD64
9171 case 8:
9172 x = (x << shift) | bfd_get_64 (input_bfd, location);
9173 break;
9174 #endif
9175 default:
9176 abort ();
9177 }
9178 }
9179 return x;
9180 }
9181
9182 static void
9183 decode_complex_addend (unsigned long *start, /* in bits */
9184 unsigned long *oplen, /* in bits */
9185 unsigned long *len, /* in bits */
9186 unsigned long *wordsz, /* in bytes */
9187 unsigned long *chunksz, /* in bytes */
9188 unsigned long *lsb0_p,
9189 unsigned long *signed_p,
9190 unsigned long *trunc_p,
9191 unsigned long encoded)
9192 {
9193 * start = encoded & 0x3F;
9194 * len = (encoded >> 6) & 0x3F;
9195 * oplen = (encoded >> 12) & 0x3F;
9196 * wordsz = (encoded >> 18) & 0xF;
9197 * chunksz = (encoded >> 22) & 0xF;
9198 * lsb0_p = (encoded >> 27) & 1;
9199 * signed_p = (encoded >> 28) & 1;
9200 * trunc_p = (encoded >> 29) & 1;
9201 }
9202
9203 bfd_reloc_status_type
9204 bfd_elf_perform_complex_relocation (bfd *input_bfd,
9205 asection *input_section,
9206 bfd_byte *contents,
9207 Elf_Internal_Rela *rel,
9208 bfd_vma relocation)
9209 {
9210 bfd_vma shift, x, mask;
9211 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
9212 bfd_reloc_status_type r;
9213 bfd_size_type octets;
9214
9215 /* Perform this reloc, since it is complex.
9216 (this is not to say that it necessarily refers to a complex
9217 symbol; merely that it is a self-describing CGEN based reloc.
9218 i.e. the addend has the complete reloc information (bit start, end,
9219 word size, etc) encoded within it.). */
9220
9221 decode_complex_addend (&start, &oplen, &len, &wordsz,
9222 &chunksz, &lsb0_p, &signed_p,
9223 &trunc_p, rel->r_addend);
9224
9225 mask = (((1L << (len - 1)) - 1) << 1) | 1;
9226
9227 if (lsb0_p)
9228 shift = (start + 1) - len;
9229 else
9230 shift = (8 * wordsz) - (start + len);
9231
9232 octets = rel->r_offset * bfd_octets_per_byte (input_bfd, input_section);
9233 x = get_value (wordsz, chunksz, input_bfd, contents + octets);
9234
9235 #ifdef DEBUG
9236 printf ("Doing complex reloc: "
9237 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
9238 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
9239 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
9240 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9241 oplen, (unsigned long) x, (unsigned long) mask,
9242 (unsigned long) relocation);
9243 #endif
9244
9245 r = bfd_reloc_ok;
9246 if (! trunc_p)
9247 /* Now do an overflow check. */
9248 r = bfd_check_overflow ((signed_p
9249 ? complain_overflow_signed
9250 : complain_overflow_unsigned),
9251 len, 0, (8 * wordsz),
9252 relocation);
9253
9254 /* Do the deed. */
9255 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
9256
9257 #ifdef DEBUG
9258 printf (" relocation: %8.8lx\n"
9259 " shifted mask: %8.8lx\n"
9260 " shifted/masked reloc: %8.8lx\n"
9261 " result: %8.8lx\n",
9262 (unsigned long) relocation, (unsigned long) (mask << shift),
9263 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
9264 #endif
9265 put_value (wordsz, chunksz, input_bfd, x, contents + octets);
9266 return r;
9267 }
9268
9269 /* Functions to read r_offset from external (target order) reloc
9270 entry. Faster than bfd_getl32 et al, because we let the compiler
9271 know the value is aligned. */
9272
9273 static bfd_vma
9274 ext32l_r_offset (const void *p)
9275 {
9276 union aligned32
9277 {
9278 uint32_t v;
9279 unsigned char c[4];
9280 };
9281 const union aligned32 *a
9282 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9283
9284 uint32_t aval = ( (uint32_t) a->c[0]
9285 | (uint32_t) a->c[1] << 8
9286 | (uint32_t) a->c[2] << 16
9287 | (uint32_t) a->c[3] << 24);
9288 return aval;
9289 }
9290
9291 static bfd_vma
9292 ext32b_r_offset (const void *p)
9293 {
9294 union aligned32
9295 {
9296 uint32_t v;
9297 unsigned char c[4];
9298 };
9299 const union aligned32 *a
9300 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9301
9302 uint32_t aval = ( (uint32_t) a->c[0] << 24
9303 | (uint32_t) a->c[1] << 16
9304 | (uint32_t) a->c[2] << 8
9305 | (uint32_t) a->c[3]);
9306 return aval;
9307 }
9308
9309 #ifdef BFD_HOST_64_BIT
9310 static bfd_vma
9311 ext64l_r_offset (const void *p)
9312 {
9313 union aligned64
9314 {
9315 uint64_t v;
9316 unsigned char c[8];
9317 };
9318 const union aligned64 *a
9319 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9320
9321 uint64_t aval = ( (uint64_t) a->c[0]
9322 | (uint64_t) a->c[1] << 8
9323 | (uint64_t) a->c[2] << 16
9324 | (uint64_t) a->c[3] << 24
9325 | (uint64_t) a->c[4] << 32
9326 | (uint64_t) a->c[5] << 40
9327 | (uint64_t) a->c[6] << 48
9328 | (uint64_t) a->c[7] << 56);
9329 return aval;
9330 }
9331
9332 static bfd_vma
9333 ext64b_r_offset (const void *p)
9334 {
9335 union aligned64
9336 {
9337 uint64_t v;
9338 unsigned char c[8];
9339 };
9340 const union aligned64 *a
9341 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9342
9343 uint64_t aval = ( (uint64_t) a->c[0] << 56
9344 | (uint64_t) a->c[1] << 48
9345 | (uint64_t) a->c[2] << 40
9346 | (uint64_t) a->c[3] << 32
9347 | (uint64_t) a->c[4] << 24
9348 | (uint64_t) a->c[5] << 16
9349 | (uint64_t) a->c[6] << 8
9350 | (uint64_t) a->c[7]);
9351 return aval;
9352 }
9353 #endif
9354
9355 /* When performing a relocatable link, the input relocations are
9356 preserved. But, if they reference global symbols, the indices
9357 referenced must be updated. Update all the relocations found in
9358 RELDATA. */
9359
9360 static bool
9361 elf_link_adjust_relocs (bfd *abfd,
9362 asection *sec,
9363 struct bfd_elf_section_reloc_data *reldata,
9364 bool sort,
9365 struct bfd_link_info *info)
9366 {
9367 unsigned int i;
9368 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9369 bfd_byte *erela;
9370 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9371 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9372 bfd_vma r_type_mask;
9373 int r_sym_shift;
9374 unsigned int count = reldata->count;
9375 struct elf_link_hash_entry **rel_hash = reldata->hashes;
9376
9377 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
9378 {
9379 swap_in = bed->s->swap_reloc_in;
9380 swap_out = bed->s->swap_reloc_out;
9381 }
9382 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
9383 {
9384 swap_in = bed->s->swap_reloca_in;
9385 swap_out = bed->s->swap_reloca_out;
9386 }
9387 else
9388 abort ();
9389
9390 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
9391 abort ();
9392
9393 if (bed->s->arch_size == 32)
9394 {
9395 r_type_mask = 0xff;
9396 r_sym_shift = 8;
9397 }
9398 else
9399 {
9400 r_type_mask = 0xffffffff;
9401 r_sym_shift = 32;
9402 }
9403
9404 erela = reldata->hdr->contents;
9405 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
9406 {
9407 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
9408 unsigned int j;
9409
9410 if (*rel_hash == NULL)
9411 continue;
9412
9413 if ((*rel_hash)->indx == -2
9414 && info->gc_sections
9415 && ! info->gc_keep_exported)
9416 {
9417 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9418 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
9419 abfd, sec,
9420 (*rel_hash)->root.root.string);
9421 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
9422 abfd, sec);
9423 bfd_set_error (bfd_error_invalid_operation);
9424 return false;
9425 }
9426 BFD_ASSERT ((*rel_hash)->indx >= 0);
9427
9428 (*swap_in) (abfd, erela, irela);
9429 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
9430 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
9431 | (irela[j].r_info & r_type_mask));
9432 (*swap_out) (abfd, irela, erela);
9433 }
9434
9435 if (bed->elf_backend_update_relocs)
9436 (*bed->elf_backend_update_relocs) (sec, reldata);
9437
9438 if (sort && count != 0)
9439 {
9440 bfd_vma (*ext_r_off) (const void *);
9441 bfd_vma r_off;
9442 size_t elt_size;
9443 bfd_byte *base, *end, *p, *loc;
9444 bfd_byte *buf = NULL;
9445
9446 if (bed->s->arch_size == 32)
9447 {
9448 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9449 ext_r_off = ext32l_r_offset;
9450 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9451 ext_r_off = ext32b_r_offset;
9452 else
9453 abort ();
9454 }
9455 else
9456 {
9457 #ifdef BFD_HOST_64_BIT
9458 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9459 ext_r_off = ext64l_r_offset;
9460 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9461 ext_r_off = ext64b_r_offset;
9462 else
9463 #endif
9464 abort ();
9465 }
9466
9467 /* Must use a stable sort here. A modified insertion sort,
9468 since the relocs are mostly sorted already. */
9469 elt_size = reldata->hdr->sh_entsize;
9470 base = reldata->hdr->contents;
9471 end = base + count * elt_size;
9472 if (elt_size > sizeof (Elf64_External_Rela))
9473 abort ();
9474
9475 /* Ensure the first element is lowest. This acts as a sentinel,
9476 speeding the main loop below. */
9477 r_off = (*ext_r_off) (base);
9478 for (p = loc = base; (p += elt_size) < end; )
9479 {
9480 bfd_vma r_off2 = (*ext_r_off) (p);
9481 if (r_off > r_off2)
9482 {
9483 r_off = r_off2;
9484 loc = p;
9485 }
9486 }
9487 if (loc != base)
9488 {
9489 /* Don't just swap *base and *loc as that changes the order
9490 of the original base[0] and base[1] if they happen to
9491 have the same r_offset. */
9492 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
9493 memcpy (onebuf, loc, elt_size);
9494 memmove (base + elt_size, base, loc - base);
9495 memcpy (base, onebuf, elt_size);
9496 }
9497
9498 for (p = base + elt_size; (p += elt_size) < end; )
9499 {
9500 /* base to p is sorted, *p is next to insert. */
9501 r_off = (*ext_r_off) (p);
9502 /* Search the sorted region for location to insert. */
9503 loc = p - elt_size;
9504 while (r_off < (*ext_r_off) (loc))
9505 loc -= elt_size;
9506 loc += elt_size;
9507 if (loc != p)
9508 {
9509 /* Chances are there is a run of relocs to insert here,
9510 from one of more input files. Files are not always
9511 linked in order due to the way elf_link_input_bfd is
9512 called. See pr17666. */
9513 size_t sortlen = p - loc;
9514 bfd_vma r_off2 = (*ext_r_off) (loc);
9515 size_t runlen = elt_size;
9516 size_t buf_size = 96 * 1024;
9517 while (p + runlen < end
9518 && (sortlen <= buf_size
9519 || runlen + elt_size <= buf_size)
9520 && r_off2 > (*ext_r_off) (p + runlen))
9521 runlen += elt_size;
9522 if (buf == NULL)
9523 {
9524 buf = bfd_malloc (buf_size);
9525 if (buf == NULL)
9526 return false;
9527 }
9528 if (runlen < sortlen)
9529 {
9530 memcpy (buf, p, runlen);
9531 memmove (loc + runlen, loc, sortlen);
9532 memcpy (loc, buf, runlen);
9533 }
9534 else
9535 {
9536 memcpy (buf, loc, sortlen);
9537 memmove (loc, p, runlen);
9538 memcpy (loc + runlen, buf, sortlen);
9539 }
9540 p += runlen - elt_size;
9541 }
9542 }
9543 /* Hashes are no longer valid. */
9544 free (reldata->hashes);
9545 reldata->hashes = NULL;
9546 free (buf);
9547 }
9548 return true;
9549 }
9550
9551 struct elf_link_sort_rela
9552 {
9553 union {
9554 bfd_vma offset;
9555 bfd_vma sym_mask;
9556 } u;
9557 enum elf_reloc_type_class type;
9558 /* We use this as an array of size int_rels_per_ext_rel. */
9559 Elf_Internal_Rela rela[1];
9560 };
9561
9562 /* qsort stability here and for cmp2 is only an issue if multiple
9563 dynamic relocations are emitted at the same address. But targets
9564 that apply a series of dynamic relocations each operating on the
9565 result of the prior relocation can't use -z combreloc as
9566 implemented anyway. Such schemes tend to be broken by sorting on
9567 symbol index. That leaves dynamic NONE relocs as the only other
9568 case where ld might emit multiple relocs at the same address, and
9569 those are only emitted due to target bugs. */
9570
9571 static int
9572 elf_link_sort_cmp1 (const void *A, const void *B)
9573 {
9574 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9575 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9576 int relativea, relativeb;
9577
9578 relativea = a->type == reloc_class_relative;
9579 relativeb = b->type == reloc_class_relative;
9580
9581 if (relativea < relativeb)
9582 return 1;
9583 if (relativea > relativeb)
9584 return -1;
9585 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
9586 return -1;
9587 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
9588 return 1;
9589 if (a->rela->r_offset < b->rela->r_offset)
9590 return -1;
9591 if (a->rela->r_offset > b->rela->r_offset)
9592 return 1;
9593 return 0;
9594 }
9595
9596 static int
9597 elf_link_sort_cmp2 (const void *A, const void *B)
9598 {
9599 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9600 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9601
9602 if (a->type < b->type)
9603 return -1;
9604 if (a->type > b->type)
9605 return 1;
9606 if (a->u.offset < b->u.offset)
9607 return -1;
9608 if (a->u.offset > b->u.offset)
9609 return 1;
9610 if (a->rela->r_offset < b->rela->r_offset)
9611 return -1;
9612 if (a->rela->r_offset > b->rela->r_offset)
9613 return 1;
9614 return 0;
9615 }
9616
9617 static size_t
9618 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9619 {
9620 asection *dynamic_relocs;
9621 asection *rela_dyn;
9622 asection *rel_dyn;
9623 bfd_size_type count, size;
9624 size_t i, ret, sort_elt, ext_size;
9625 bfd_byte *sort, *s_non_relative, *p;
9626 struct elf_link_sort_rela *sq;
9627 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9628 int i2e = bed->s->int_rels_per_ext_rel;
9629 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
9630 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9631 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9632 struct bfd_link_order *lo;
9633 bfd_vma r_sym_mask;
9634 bool use_rela;
9635
9636 /* Find a dynamic reloc section. */
9637 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9638 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
9639 if (rela_dyn != NULL && rela_dyn->size > 0
9640 && rel_dyn != NULL && rel_dyn->size > 0)
9641 {
9642 bool use_rela_initialised = false;
9643
9644 /* This is just here to stop gcc from complaining.
9645 Its initialization checking code is not perfect. */
9646 use_rela = true;
9647
9648 /* Both sections are present. Examine the sizes
9649 of the indirect sections to help us choose. */
9650 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9651 if (lo->type == bfd_indirect_link_order)
9652 {
9653 asection *o = lo->u.indirect.section;
9654
9655 if ((o->size % bed->s->sizeof_rela) == 0)
9656 {
9657 if ((o->size % bed->s->sizeof_rel) == 0)
9658 /* Section size is divisible by both rel and rela sizes.
9659 It is of no help to us. */
9660 ;
9661 else
9662 {
9663 /* Section size is only divisible by rela. */
9664 if (use_rela_initialised && !use_rela)
9665 {
9666 _bfd_error_handler (_("%pB: unable to sort relocs - "
9667 "they are in more than one size"),
9668 abfd);
9669 bfd_set_error (bfd_error_invalid_operation);
9670 return 0;
9671 }
9672 else
9673 {
9674 use_rela = true;
9675 use_rela_initialised = true;
9676 }
9677 }
9678 }
9679 else if ((o->size % bed->s->sizeof_rel) == 0)
9680 {
9681 /* Section size is only divisible by rel. */
9682 if (use_rela_initialised && use_rela)
9683 {
9684 _bfd_error_handler (_("%pB: unable to sort relocs - "
9685 "they are in more than one size"),
9686 abfd);
9687 bfd_set_error (bfd_error_invalid_operation);
9688 return 0;
9689 }
9690 else
9691 {
9692 use_rela = false;
9693 use_rela_initialised = true;
9694 }
9695 }
9696 else
9697 {
9698 /* The section size is not divisible by either -
9699 something is wrong. */
9700 _bfd_error_handler (_("%pB: unable to sort relocs - "
9701 "they are of an unknown size"), abfd);
9702 bfd_set_error (bfd_error_invalid_operation);
9703 return 0;
9704 }
9705 }
9706
9707 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9708 if (lo->type == bfd_indirect_link_order)
9709 {
9710 asection *o = lo->u.indirect.section;
9711
9712 if ((o->size % bed->s->sizeof_rela) == 0)
9713 {
9714 if ((o->size % bed->s->sizeof_rel) == 0)
9715 /* Section size is divisible by both rel and rela sizes.
9716 It is of no help to us. */
9717 ;
9718 else
9719 {
9720 /* Section size is only divisible by rela. */
9721 if (use_rela_initialised && !use_rela)
9722 {
9723 _bfd_error_handler (_("%pB: unable to sort relocs - "
9724 "they are in more than one size"),
9725 abfd);
9726 bfd_set_error (bfd_error_invalid_operation);
9727 return 0;
9728 }
9729 else
9730 {
9731 use_rela = true;
9732 use_rela_initialised = true;
9733 }
9734 }
9735 }
9736 else if ((o->size % bed->s->sizeof_rel) == 0)
9737 {
9738 /* Section size is only divisible by rel. */
9739 if (use_rela_initialised && use_rela)
9740 {
9741 _bfd_error_handler (_("%pB: unable to sort relocs - "
9742 "they are in more than one size"),
9743 abfd);
9744 bfd_set_error (bfd_error_invalid_operation);
9745 return 0;
9746 }
9747 else
9748 {
9749 use_rela = false;
9750 use_rela_initialised = true;
9751 }
9752 }
9753 else
9754 {
9755 /* The section size is not divisible by either -
9756 something is wrong. */
9757 _bfd_error_handler (_("%pB: unable to sort relocs - "
9758 "they are of an unknown size"), abfd);
9759 bfd_set_error (bfd_error_invalid_operation);
9760 return 0;
9761 }
9762 }
9763
9764 if (! use_rela_initialised)
9765 /* Make a guess. */
9766 use_rela = true;
9767 }
9768 else if (rela_dyn != NULL && rela_dyn->size > 0)
9769 use_rela = true;
9770 else if (rel_dyn != NULL && rel_dyn->size > 0)
9771 use_rela = false;
9772 else
9773 return 0;
9774
9775 if (use_rela)
9776 {
9777 dynamic_relocs = rela_dyn;
9778 ext_size = bed->s->sizeof_rela;
9779 swap_in = bed->s->swap_reloca_in;
9780 swap_out = bed->s->swap_reloca_out;
9781 }
9782 else
9783 {
9784 dynamic_relocs = rel_dyn;
9785 ext_size = bed->s->sizeof_rel;
9786 swap_in = bed->s->swap_reloc_in;
9787 swap_out = bed->s->swap_reloc_out;
9788 }
9789
9790 size = 0;
9791 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9792 if (lo->type == bfd_indirect_link_order)
9793 size += lo->u.indirect.section->size;
9794
9795 if (size != dynamic_relocs->size)
9796 return 0;
9797
9798 sort_elt = (sizeof (struct elf_link_sort_rela)
9799 + (i2e - 1) * sizeof (Elf_Internal_Rela));
9800
9801 count = dynamic_relocs->size / ext_size;
9802 if (count == 0)
9803 return 0;
9804 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
9805
9806 if (sort == NULL)
9807 {
9808 (*info->callbacks->warning)
9809 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
9810 return 0;
9811 }
9812
9813 if (bed->s->arch_size == 32)
9814 r_sym_mask = ~(bfd_vma) 0xff;
9815 else
9816 r_sym_mask = ~(bfd_vma) 0xffffffff;
9817
9818 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9819 if (lo->type == bfd_indirect_link_order)
9820 {
9821 bfd_byte *erel, *erelend;
9822 asection *o = lo->u.indirect.section;
9823
9824 if (o->contents == NULL && o->size != 0)
9825 {
9826 /* This is a reloc section that is being handled as a normal
9827 section. See bfd_section_from_shdr. We can't combine
9828 relocs in this case. */
9829 free (sort);
9830 return 0;
9831 }
9832 erel = o->contents;
9833 erelend = o->contents + o->size;
9834 p = sort + o->output_offset * opb / ext_size * sort_elt;
9835
9836 while (erel < erelend)
9837 {
9838 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9839
9840 (*swap_in) (abfd, erel, s->rela);
9841 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
9842 s->u.sym_mask = r_sym_mask;
9843 p += sort_elt;
9844 erel += ext_size;
9845 }
9846 }
9847
9848 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9849
9850 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9851 {
9852 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9853 if (s->type != reloc_class_relative)
9854 break;
9855 }
9856 ret = i;
9857 s_non_relative = p;
9858
9859 sq = (struct elf_link_sort_rela *) s_non_relative;
9860 for (; i < count; i++, p += sort_elt)
9861 {
9862 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9863 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9864 sq = sp;
9865 sp->u.offset = sq->rela->r_offset;
9866 }
9867
9868 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9869
9870 struct elf_link_hash_table *htab = elf_hash_table (info);
9871 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9872 {
9873 /* We have plt relocs in .rela.dyn. */
9874 sq = (struct elf_link_sort_rela *) sort;
9875 for (i = 0; i < count; i++)
9876 if (sq[count - i - 1].type != reloc_class_plt)
9877 break;
9878 if (i != 0 && htab->srelplt->size == i * ext_size)
9879 {
9880 struct bfd_link_order **plo;
9881 /* Put srelplt link_order last. This is so the output_offset
9882 set in the next loop is correct for DT_JMPREL. */
9883 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9884 if ((*plo)->type == bfd_indirect_link_order
9885 && (*plo)->u.indirect.section == htab->srelplt)
9886 {
9887 lo = *plo;
9888 *plo = lo->next;
9889 }
9890 else
9891 plo = &(*plo)->next;
9892 *plo = lo;
9893 lo->next = NULL;
9894 dynamic_relocs->map_tail.link_order = lo;
9895 }
9896 }
9897
9898 p = sort;
9899 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9900 if (lo->type == bfd_indirect_link_order)
9901 {
9902 bfd_byte *erel, *erelend;
9903 asection *o = lo->u.indirect.section;
9904
9905 erel = o->contents;
9906 erelend = o->contents + o->size;
9907 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
9908 while (erel < erelend)
9909 {
9910 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9911 (*swap_out) (abfd, s->rela, erel);
9912 p += sort_elt;
9913 erel += ext_size;
9914 }
9915 }
9916
9917 free (sort);
9918 *psec = dynamic_relocs;
9919 return ret;
9920 }
9921
9922 /* Add a symbol to the output symbol string table. */
9923
9924 static int
9925 elf_link_output_symstrtab (void *finf,
9926 const char *name,
9927 Elf_Internal_Sym *elfsym,
9928 asection *input_sec,
9929 struct elf_link_hash_entry *h)
9930 {
9931 struct elf_final_link_info *flinfo = finf;
9932 int (*output_symbol_hook)
9933 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9934 struct elf_link_hash_entry *);
9935 struct elf_link_hash_table *hash_table;
9936 const struct elf_backend_data *bed;
9937 bfd_size_type strtabsize;
9938
9939 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9940
9941 bed = get_elf_backend_data (flinfo->output_bfd);
9942 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9943 if (output_symbol_hook != NULL)
9944 {
9945 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
9946 if (ret != 1)
9947 return ret;
9948 }
9949
9950 if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC)
9951 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
9952 if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE)
9953 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique;
9954
9955 if (name == NULL
9956 || *name == '\0'
9957 || (input_sec->flags & SEC_EXCLUDE))
9958 elfsym->st_name = (unsigned long) -1;
9959 else
9960 {
9961 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9962 to get the final offset for st_name. */
9963 char *versioned_name = (char *) name;
9964 if (h != NULL)
9965 {
9966 if (h->versioned == versioned && h->def_dynamic)
9967 {
9968 /* Keep only one '@' for versioned symbols defined in
9969 shared objects. */
9970 char *version = strrchr (name, ELF_VER_CHR);
9971 char *base_end = strchr (name, ELF_VER_CHR);
9972 if (version != base_end)
9973 {
9974 size_t base_len;
9975 size_t len = strlen (name);
9976 versioned_name = bfd_alloc (flinfo->output_bfd, len);
9977 if (versioned_name == NULL)
9978 return 0;
9979 base_len = base_end - name;
9980 memcpy (versioned_name, name, base_len);
9981 memcpy (versioned_name + base_len, version,
9982 len - base_len);
9983 }
9984 }
9985 }
9986 else if (flinfo->info->unique_symbol
9987 && ELF_ST_BIND (elfsym->st_info) == STB_LOCAL)
9988 {
9989 struct local_hash_entry *lh;
9990 size_t count_len;
9991 size_t base_len;
9992 char buf[30];
9993 switch (ELF_ST_TYPE (elfsym->st_info))
9994 {
9995 case STT_FILE:
9996 case STT_SECTION:
9997 break;
9998 default:
9999 lh = (struct local_hash_entry *) bfd_hash_lookup
10000 (&flinfo->local_hash_table, name, true, false);
10001 if (lh == NULL)
10002 return 0;
10003 /* Always append ".COUNT" to local symbols to avoid
10004 potential conflicts with local symbol "XXX.COUNT". */
10005 sprintf (buf, "%lx", lh->count);
10006 base_len = lh->size;
10007 if (!base_len)
10008 {
10009 base_len = strlen (name);
10010 lh->size = base_len;
10011 }
10012 count_len = strlen (buf);
10013 versioned_name = bfd_alloc (flinfo->output_bfd,
10014 base_len + count_len + 2);
10015 if (versioned_name == NULL)
10016 return 0;
10017 memcpy (versioned_name, name, base_len);
10018 versioned_name[base_len] = '.';
10019 memcpy (versioned_name + base_len + 1, buf,
10020 count_len + 1);
10021 lh->count++;
10022 break;
10023 }
10024 }
10025 elfsym->st_name
10026 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
10027 versioned_name, false);
10028 if (elfsym->st_name == (unsigned long) -1)
10029 return 0;
10030 }
10031
10032 hash_table = elf_hash_table (flinfo->info);
10033 strtabsize = hash_table->strtabsize;
10034 if (strtabsize <= flinfo->output_bfd->symcount)
10035 {
10036 strtabsize += strtabsize;
10037 hash_table->strtabsize = strtabsize;
10038 strtabsize *= sizeof (*hash_table->strtab);
10039 hash_table->strtab
10040 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
10041 strtabsize);
10042 if (hash_table->strtab == NULL)
10043 return 0;
10044 }
10045 hash_table->strtab[flinfo->output_bfd->symcount].sym = *elfsym;
10046 hash_table->strtab[flinfo->output_bfd->symcount].dest_index
10047 = flinfo->output_bfd->symcount;
10048 flinfo->output_bfd->symcount += 1;
10049
10050 return 1;
10051 }
10052
10053 /* Swap symbols out to the symbol table and flush the output symbols to
10054 the file. */
10055
10056 static bool
10057 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
10058 {
10059 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
10060 size_t amt;
10061 size_t i;
10062 const struct elf_backend_data *bed;
10063 bfd_byte *symbuf;
10064 Elf_Internal_Shdr *hdr;
10065 file_ptr pos;
10066 bool ret;
10067
10068 if (flinfo->output_bfd->symcount == 0)
10069 return true;
10070
10071 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
10072
10073 bed = get_elf_backend_data (flinfo->output_bfd);
10074
10075 amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount;
10076 symbuf = (bfd_byte *) bfd_malloc (amt);
10077 if (symbuf == NULL)
10078 return false;
10079
10080 if (flinfo->symshndxbuf)
10081 {
10082 amt = sizeof (Elf_External_Sym_Shndx);
10083 amt *= bfd_get_symcount (flinfo->output_bfd);
10084 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
10085 if (flinfo->symshndxbuf == NULL)
10086 {
10087 free (symbuf);
10088 return false;
10089 }
10090 }
10091
10092 /* Now swap out the symbols. */
10093 for (i = 0; i < flinfo->output_bfd->symcount; i++)
10094 {
10095 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
10096 if (elfsym->sym.st_name == (unsigned long) -1)
10097 elfsym->sym.st_name = 0;
10098 else
10099 elfsym->sym.st_name
10100 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
10101 elfsym->sym.st_name);
10102
10103 /* Inform the linker of the addition of this symbol. */
10104
10105 if (flinfo->info->callbacks->ctf_new_symbol)
10106 flinfo->info->callbacks->ctf_new_symbol (elfsym->dest_index,
10107 &elfsym->sym);
10108
10109 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
10110 ((bfd_byte *) symbuf
10111 + (elfsym->dest_index
10112 * bed->s->sizeof_sym)),
10113 NPTR_ADD (flinfo->symshndxbuf,
10114 elfsym->dest_index));
10115 }
10116
10117 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
10118 pos = hdr->sh_offset + hdr->sh_size;
10119 amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount;
10120 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
10121 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
10122 {
10123 hdr->sh_size += amt;
10124 ret = true;
10125 }
10126 else
10127 ret = false;
10128
10129 free (symbuf);
10130
10131 free (hash_table->strtab);
10132 hash_table->strtab = NULL;
10133
10134 return ret;
10135 }
10136
10137 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
10138
10139 static bool
10140 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
10141 {
10142 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
10143 && sym->st_shndx < SHN_LORESERVE)
10144 {
10145 /* The gABI doesn't support dynamic symbols in output sections
10146 beyond 64k. */
10147 _bfd_error_handler
10148 /* xgettext:c-format */
10149 (_("%pB: too many sections: %d (>= %d)"),
10150 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
10151 bfd_set_error (bfd_error_nonrepresentable_section);
10152 return false;
10153 }
10154 return true;
10155 }
10156
10157 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
10158 allowing an unsatisfied unversioned symbol in the DSO to match a
10159 versioned symbol that would normally require an explicit version.
10160 We also handle the case that a DSO references a hidden symbol
10161 which may be satisfied by a versioned symbol in another DSO. */
10162
10163 static bool
10164 elf_link_check_versioned_symbol (struct bfd_link_info *info,
10165 const struct elf_backend_data *bed,
10166 struct elf_link_hash_entry *h)
10167 {
10168 bfd *abfd;
10169 struct elf_link_loaded_list *loaded;
10170
10171 if (!is_elf_hash_table (info->hash))
10172 return false;
10173
10174 /* Check indirect symbol. */
10175 while (h->root.type == bfd_link_hash_indirect)
10176 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10177
10178 switch (h->root.type)
10179 {
10180 default:
10181 abfd = NULL;
10182 break;
10183
10184 case bfd_link_hash_undefined:
10185 case bfd_link_hash_undefweak:
10186 abfd = h->root.u.undef.abfd;
10187 if (abfd == NULL
10188 || (abfd->flags & DYNAMIC) == 0
10189 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
10190 return false;
10191 break;
10192
10193 case bfd_link_hash_defined:
10194 case bfd_link_hash_defweak:
10195 abfd = h->root.u.def.section->owner;
10196 break;
10197
10198 case bfd_link_hash_common:
10199 abfd = h->root.u.c.p->section->owner;
10200 break;
10201 }
10202 BFD_ASSERT (abfd != NULL);
10203
10204 for (loaded = elf_hash_table (info)->dyn_loaded;
10205 loaded != NULL;
10206 loaded = loaded->next)
10207 {
10208 bfd *input;
10209 Elf_Internal_Shdr *hdr;
10210 size_t symcount;
10211 size_t extsymcount;
10212 size_t extsymoff;
10213 Elf_Internal_Shdr *versymhdr;
10214 Elf_Internal_Sym *isym;
10215 Elf_Internal_Sym *isymend;
10216 Elf_Internal_Sym *isymbuf;
10217 Elf_External_Versym *ever;
10218 Elf_External_Versym *extversym;
10219
10220 input = loaded->abfd;
10221
10222 /* We check each DSO for a possible hidden versioned definition. */
10223 if (input == abfd
10224 || elf_dynversym (input) == 0)
10225 continue;
10226
10227 hdr = &elf_tdata (input)->dynsymtab_hdr;
10228
10229 symcount = hdr->sh_size / bed->s->sizeof_sym;
10230 if (elf_bad_symtab (input))
10231 {
10232 extsymcount = symcount;
10233 extsymoff = 0;
10234 }
10235 else
10236 {
10237 extsymcount = symcount - hdr->sh_info;
10238 extsymoff = hdr->sh_info;
10239 }
10240
10241 if (extsymcount == 0)
10242 continue;
10243
10244 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
10245 NULL, NULL, NULL);
10246 if (isymbuf == NULL)
10247 return false;
10248
10249 /* Read in any version definitions. */
10250 versymhdr = &elf_tdata (input)->dynversym_hdr;
10251 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
10252 || (extversym = (Elf_External_Versym *)
10253 _bfd_malloc_and_read (input, versymhdr->sh_size,
10254 versymhdr->sh_size)) == NULL)
10255 {
10256 free (isymbuf);
10257 return false;
10258 }
10259
10260 ever = extversym + extsymoff;
10261 isymend = isymbuf + extsymcount;
10262 for (isym = isymbuf; isym < isymend; isym++, ever++)
10263 {
10264 const char *name;
10265 Elf_Internal_Versym iver;
10266 unsigned short version_index;
10267
10268 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
10269 || isym->st_shndx == SHN_UNDEF)
10270 continue;
10271
10272 name = bfd_elf_string_from_elf_section (input,
10273 hdr->sh_link,
10274 isym->st_name);
10275 if (strcmp (name, h->root.root.string) != 0)
10276 continue;
10277
10278 _bfd_elf_swap_versym_in (input, ever, &iver);
10279
10280 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
10281 && !(h->def_regular
10282 && h->forced_local))
10283 {
10284 /* If we have a non-hidden versioned sym, then it should
10285 have provided a definition for the undefined sym unless
10286 it is defined in a non-shared object and forced local.
10287 */
10288 abort ();
10289 }
10290
10291 version_index = iver.vs_vers & VERSYM_VERSION;
10292 if (version_index == 1 || version_index == 2)
10293 {
10294 /* This is the base or first version. We can use it. */
10295 free (extversym);
10296 free (isymbuf);
10297 return true;
10298 }
10299 }
10300
10301 free (extversym);
10302 free (isymbuf);
10303 }
10304
10305 return false;
10306 }
10307
10308 /* Convert ELF common symbol TYPE. */
10309
10310 static int
10311 elf_link_convert_common_type (struct bfd_link_info *info, int type)
10312 {
10313 /* Commom symbol can only appear in relocatable link. */
10314 if (!bfd_link_relocatable (info))
10315 abort ();
10316 switch (info->elf_stt_common)
10317 {
10318 case unchanged:
10319 break;
10320 case elf_stt_common:
10321 type = STT_COMMON;
10322 break;
10323 case no_elf_stt_common:
10324 type = STT_OBJECT;
10325 break;
10326 }
10327 return type;
10328 }
10329
10330 /* Add an external symbol to the symbol table. This is called from
10331 the hash table traversal routine. When generating a shared object,
10332 we go through the symbol table twice. The first time we output
10333 anything that might have been forced to local scope in a version
10334 script. The second time we output the symbols that are still
10335 global symbols. */
10336
10337 static bool
10338 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
10339 {
10340 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
10341 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
10342 struct elf_final_link_info *flinfo = eoinfo->flinfo;
10343 bool strip;
10344 Elf_Internal_Sym sym;
10345 asection *input_sec;
10346 const struct elf_backend_data *bed;
10347 long indx;
10348 int ret;
10349 unsigned int type;
10350
10351 if (h->root.type == bfd_link_hash_warning)
10352 {
10353 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10354 if (h->root.type == bfd_link_hash_new)
10355 return true;
10356 }
10357
10358 /* Decide whether to output this symbol in this pass. */
10359 if (eoinfo->localsyms)
10360 {
10361 if (!h->forced_local)
10362 return true;
10363 }
10364 else
10365 {
10366 if (h->forced_local)
10367 return true;
10368 }
10369
10370 bed = get_elf_backend_data (flinfo->output_bfd);
10371
10372 if (h->root.type == bfd_link_hash_undefined)
10373 {
10374 /* If we have an undefined symbol reference here then it must have
10375 come from a shared library that is being linked in. (Undefined
10376 references in regular files have already been handled unless
10377 they are in unreferenced sections which are removed by garbage
10378 collection). */
10379 bool ignore_undef = false;
10380
10381 /* Some symbols may be special in that the fact that they're
10382 undefined can be safely ignored - let backend determine that. */
10383 if (bed->elf_backend_ignore_undef_symbol)
10384 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
10385
10386 /* If we are reporting errors for this situation then do so now. */
10387 if (!ignore_undef
10388 && h->ref_dynamic_nonweak
10389 && (!h->ref_regular || flinfo->info->gc_sections)
10390 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
10391 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
10392 {
10393 flinfo->info->callbacks->undefined_symbol
10394 (flinfo->info, h->root.root.string,
10395 h->ref_regular ? NULL : h->root.u.undef.abfd, NULL, 0,
10396 flinfo->info->unresolved_syms_in_shared_libs == RM_DIAGNOSE
10397 && !flinfo->info->warn_unresolved_syms);
10398 }
10399
10400 /* Strip a global symbol defined in a discarded section. */
10401 if (h->indx == -3)
10402 return true;
10403 }
10404
10405 /* We should also warn if a forced local symbol is referenced from
10406 shared libraries. */
10407 if (bfd_link_executable (flinfo->info)
10408 && h->forced_local
10409 && h->ref_dynamic
10410 && h->def_regular
10411 && !h->dynamic_def
10412 && h->ref_dynamic_nonweak
10413 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
10414 {
10415 bfd *def_bfd;
10416 const char *msg;
10417 struct elf_link_hash_entry *hi = h;
10418
10419 /* Check indirect symbol. */
10420 while (hi->root.type == bfd_link_hash_indirect)
10421 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
10422
10423 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
10424 /* xgettext:c-format */
10425 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
10426 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
10427 /* xgettext:c-format */
10428 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
10429 else
10430 /* xgettext:c-format */
10431 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
10432 def_bfd = flinfo->output_bfd;
10433 if (hi->root.u.def.section != bfd_abs_section_ptr)
10434 def_bfd = hi->root.u.def.section->owner;
10435 _bfd_error_handler (msg, flinfo->output_bfd,
10436 h->root.root.string, def_bfd);
10437 bfd_set_error (bfd_error_bad_value);
10438 eoinfo->failed = true;
10439 return false;
10440 }
10441
10442 /* We don't want to output symbols that have never been mentioned by
10443 a regular file, or that we have been told to strip. However, if
10444 h->indx is set to -2, the symbol is used by a reloc and we must
10445 output it. */
10446 strip = false;
10447 if (h->indx == -2)
10448 ;
10449 else if ((h->def_dynamic
10450 || h->ref_dynamic
10451 || h->root.type == bfd_link_hash_new)
10452 && !h->def_regular
10453 && !h->ref_regular)
10454 strip = true;
10455 else if (flinfo->info->strip == strip_all)
10456 strip = true;
10457 else if (flinfo->info->strip == strip_some
10458 && bfd_hash_lookup (flinfo->info->keep_hash,
10459 h->root.root.string, false, false) == NULL)
10460 strip = true;
10461 else if ((h->root.type == bfd_link_hash_defined
10462 || h->root.type == bfd_link_hash_defweak)
10463 && ((flinfo->info->strip_discarded
10464 && discarded_section (h->root.u.def.section))
10465 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
10466 && h->root.u.def.section->owner != NULL
10467 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
10468 strip = true;
10469 else if ((h->root.type == bfd_link_hash_undefined
10470 || h->root.type == bfd_link_hash_undefweak)
10471 && h->root.u.undef.abfd != NULL
10472 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
10473 strip = true;
10474
10475 type = h->type;
10476
10477 /* If we're stripping it, and it's not a dynamic symbol, there's
10478 nothing else to do. However, if it is a forced local symbol or
10479 an ifunc symbol we need to give the backend finish_dynamic_symbol
10480 function a chance to make it dynamic. */
10481 if (strip
10482 && h->dynindx == -1
10483 && type != STT_GNU_IFUNC
10484 && !h->forced_local)
10485 return true;
10486
10487 sym.st_value = 0;
10488 sym.st_size = h->size;
10489 sym.st_other = h->other;
10490 switch (h->root.type)
10491 {
10492 default:
10493 case bfd_link_hash_new:
10494 case bfd_link_hash_warning:
10495 abort ();
10496 return false;
10497
10498 case bfd_link_hash_undefined:
10499 case bfd_link_hash_undefweak:
10500 input_sec = bfd_und_section_ptr;
10501 sym.st_shndx = SHN_UNDEF;
10502 break;
10503
10504 case bfd_link_hash_defined:
10505 case bfd_link_hash_defweak:
10506 {
10507 input_sec = h->root.u.def.section;
10508 if (input_sec->output_section != NULL)
10509 {
10510 sym.st_shndx =
10511 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
10512 input_sec->output_section);
10513 if (sym.st_shndx == SHN_BAD)
10514 {
10515 _bfd_error_handler
10516 /* xgettext:c-format */
10517 (_("%pB: could not find output section %pA for input section %pA"),
10518 flinfo->output_bfd, input_sec->output_section, input_sec);
10519 bfd_set_error (bfd_error_nonrepresentable_section);
10520 eoinfo->failed = true;
10521 return false;
10522 }
10523
10524 /* ELF symbols in relocatable files are section relative,
10525 but in nonrelocatable files they are virtual
10526 addresses. */
10527 sym.st_value = h->root.u.def.value + input_sec->output_offset;
10528 if (!bfd_link_relocatable (flinfo->info))
10529 {
10530 sym.st_value += input_sec->output_section->vma;
10531 if (h->type == STT_TLS)
10532 {
10533 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
10534 if (tls_sec != NULL)
10535 sym.st_value -= tls_sec->vma;
10536 }
10537 }
10538 }
10539 else
10540 {
10541 BFD_ASSERT (input_sec->owner == NULL
10542 || (input_sec->owner->flags & DYNAMIC) != 0);
10543 sym.st_shndx = SHN_UNDEF;
10544 input_sec = bfd_und_section_ptr;
10545 }
10546 }
10547 break;
10548
10549 case bfd_link_hash_common:
10550 input_sec = h->root.u.c.p->section;
10551 sym.st_shndx = bed->common_section_index (input_sec);
10552 sym.st_value = 1 << h->root.u.c.p->alignment_power;
10553 break;
10554
10555 case bfd_link_hash_indirect:
10556 /* These symbols are created by symbol versioning. They point
10557 to the decorated version of the name. For example, if the
10558 symbol foo@@GNU_1.2 is the default, which should be used when
10559 foo is used with no version, then we add an indirect symbol
10560 foo which points to foo@@GNU_1.2. We ignore these symbols,
10561 since the indirected symbol is already in the hash table. */
10562 return true;
10563 }
10564
10565 if (type == STT_COMMON || type == STT_OBJECT)
10566 switch (h->root.type)
10567 {
10568 case bfd_link_hash_common:
10569 type = elf_link_convert_common_type (flinfo->info, type);
10570 break;
10571 case bfd_link_hash_defined:
10572 case bfd_link_hash_defweak:
10573 if (bed->common_definition (&sym))
10574 type = elf_link_convert_common_type (flinfo->info, type);
10575 else
10576 type = STT_OBJECT;
10577 break;
10578 case bfd_link_hash_undefined:
10579 case bfd_link_hash_undefweak:
10580 break;
10581 default:
10582 abort ();
10583 }
10584
10585 if (h->forced_local)
10586 {
10587 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
10588 /* Turn off visibility on local symbol. */
10589 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
10590 }
10591 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10592 else if (h->unique_global && h->def_regular)
10593 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
10594 else if (h->root.type == bfd_link_hash_undefweak
10595 || h->root.type == bfd_link_hash_defweak)
10596 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
10597 else
10598 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
10599 sym.st_target_internal = h->target_internal;
10600
10601 /* Give the processor backend a chance to tweak the symbol value,
10602 and also to finish up anything that needs to be done for this
10603 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
10604 forced local syms when non-shared is due to a historical quirk.
10605 STT_GNU_IFUNC symbol must go through PLT. */
10606 if ((h->type == STT_GNU_IFUNC
10607 && h->def_regular
10608 && !bfd_link_relocatable (flinfo->info))
10609 || ((h->dynindx != -1
10610 || h->forced_local)
10611 && ((bfd_link_pic (flinfo->info)
10612 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
10613 || h->root.type != bfd_link_hash_undefweak))
10614 || !h->forced_local)
10615 && elf_hash_table (flinfo->info)->dynamic_sections_created))
10616 {
10617 if (! ((*bed->elf_backend_finish_dynamic_symbol)
10618 (flinfo->output_bfd, flinfo->info, h, &sym)))
10619 {
10620 eoinfo->failed = true;
10621 return false;
10622 }
10623 }
10624
10625 /* If we are marking the symbol as undefined, and there are no
10626 non-weak references to this symbol from a regular object, then
10627 mark the symbol as weak undefined; if there are non-weak
10628 references, mark the symbol as strong. We can't do this earlier,
10629 because it might not be marked as undefined until the
10630 finish_dynamic_symbol routine gets through with it. */
10631 if (sym.st_shndx == SHN_UNDEF
10632 && h->ref_regular
10633 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
10634 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
10635 {
10636 int bindtype;
10637 type = ELF_ST_TYPE (sym.st_info);
10638
10639 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10640 if (type == STT_GNU_IFUNC)
10641 type = STT_FUNC;
10642
10643 if (h->ref_regular_nonweak)
10644 bindtype = STB_GLOBAL;
10645 else
10646 bindtype = STB_WEAK;
10647 sym.st_info = ELF_ST_INFO (bindtype, type);
10648 }
10649
10650 /* If this is a symbol defined in a dynamic library, don't use the
10651 symbol size from the dynamic library. Relinking an executable
10652 against a new library may introduce gratuitous changes in the
10653 executable's symbols if we keep the size. */
10654 if (sym.st_shndx == SHN_UNDEF
10655 && !h->def_regular
10656 && h->def_dynamic)
10657 sym.st_size = 0;
10658
10659 /* If a non-weak symbol with non-default visibility is not defined
10660 locally, it is a fatal error. */
10661 if (!bfd_link_relocatable (flinfo->info)
10662 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
10663 && ELF_ST_BIND (sym.st_info) != STB_WEAK
10664 && h->root.type == bfd_link_hash_undefined
10665 && !h->def_regular)
10666 {
10667 const char *msg;
10668
10669 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
10670 /* xgettext:c-format */
10671 msg = _("%pB: protected symbol `%s' isn't defined");
10672 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
10673 /* xgettext:c-format */
10674 msg = _("%pB: internal symbol `%s' isn't defined");
10675 else
10676 /* xgettext:c-format */
10677 msg = _("%pB: hidden symbol `%s' isn't defined");
10678 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
10679 bfd_set_error (bfd_error_bad_value);
10680 eoinfo->failed = true;
10681 return false;
10682 }
10683
10684 /* If this symbol should be put in the .dynsym section, then put it
10685 there now. We already know the symbol index. We also fill in
10686 the entry in the .hash section. */
10687 if (h->dynindx != -1
10688 && elf_hash_table (flinfo->info)->dynamic_sections_created
10689 && elf_hash_table (flinfo->info)->dynsym != NULL
10690 && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
10691 {
10692 bfd_byte *esym;
10693
10694 /* Since there is no version information in the dynamic string,
10695 if there is no version info in symbol version section, we will
10696 have a run-time problem if not linking executable, referenced
10697 by shared library, or not bound locally. */
10698 if (h->verinfo.verdef == NULL
10699 && (!bfd_link_executable (flinfo->info)
10700 || h->ref_dynamic
10701 || !h->def_regular))
10702 {
10703 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
10704
10705 if (p && p [1] != '\0')
10706 {
10707 _bfd_error_handler
10708 /* xgettext:c-format */
10709 (_("%pB: no symbol version section for versioned symbol `%s'"),
10710 flinfo->output_bfd, h->root.root.string);
10711 eoinfo->failed = true;
10712 return false;
10713 }
10714 }
10715
10716 sym.st_name = h->dynstr_index;
10717 esym = (elf_hash_table (flinfo->info)->dynsym->contents
10718 + h->dynindx * bed->s->sizeof_sym);
10719 if (!check_dynsym (flinfo->output_bfd, &sym))
10720 {
10721 eoinfo->failed = true;
10722 return false;
10723 }
10724
10725 /* Inform the linker of the addition of this symbol. */
10726
10727 if (flinfo->info->callbacks->ctf_new_dynsym)
10728 flinfo->info->callbacks->ctf_new_dynsym (h->dynindx, &sym);
10729
10730 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
10731
10732 if (flinfo->hash_sec != NULL)
10733 {
10734 size_t hash_entry_size;
10735 bfd_byte *bucketpos;
10736 bfd_vma chain;
10737 size_t bucketcount;
10738 size_t bucket;
10739
10740 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
10741 bucket = h->u.elf_hash_value % bucketcount;
10742
10743 hash_entry_size
10744 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
10745 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
10746 + (bucket + 2) * hash_entry_size);
10747 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
10748 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
10749 bucketpos);
10750 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
10751 ((bfd_byte *) flinfo->hash_sec->contents
10752 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
10753 }
10754
10755 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
10756 {
10757 Elf_Internal_Versym iversym;
10758 Elf_External_Versym *eversym;
10759
10760 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
10761 {
10762 if (h->verinfo.verdef == NULL
10763 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
10764 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
10765 iversym.vs_vers = 1;
10766 else
10767 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
10768 }
10769 else
10770 {
10771 if (h->verinfo.vertree == NULL)
10772 iversym.vs_vers = 1;
10773 else
10774 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
10775 if (flinfo->info->create_default_symver)
10776 iversym.vs_vers++;
10777 }
10778
10779 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
10780 defined locally. */
10781 if (h->versioned == versioned_hidden && h->def_regular)
10782 iversym.vs_vers |= VERSYM_HIDDEN;
10783
10784 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
10785 eversym += h->dynindx;
10786 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
10787 }
10788 }
10789
10790 /* If the symbol is undefined, and we didn't output it to .dynsym,
10791 strip it from .symtab too. Obviously we can't do this for
10792 relocatable output or when needed for --emit-relocs. */
10793 else if (input_sec == bfd_und_section_ptr
10794 && h->indx != -2
10795 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10796 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
10797 && !bfd_link_relocatable (flinfo->info))
10798 return true;
10799
10800 /* Also strip others that we couldn't earlier due to dynamic symbol
10801 processing. */
10802 if (strip)
10803 return true;
10804 if ((input_sec->flags & SEC_EXCLUDE) != 0)
10805 return true;
10806
10807 /* Output a FILE symbol so that following locals are not associated
10808 with the wrong input file. We need one for forced local symbols
10809 if we've seen more than one FILE symbol or when we have exactly
10810 one FILE symbol but global symbols are present in a file other
10811 than the one with the FILE symbol. We also need one if linker
10812 defined symbols are present. In practice these conditions are
10813 always met, so just emit the FILE symbol unconditionally. */
10814 if (eoinfo->localsyms
10815 && !eoinfo->file_sym_done
10816 && eoinfo->flinfo->filesym_count != 0)
10817 {
10818 Elf_Internal_Sym fsym;
10819
10820 memset (&fsym, 0, sizeof (fsym));
10821 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10822 fsym.st_shndx = SHN_ABS;
10823 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10824 bfd_und_section_ptr, NULL))
10825 return false;
10826
10827 eoinfo->file_sym_done = true;
10828 }
10829
10830 indx = bfd_get_symcount (flinfo->output_bfd);
10831 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10832 input_sec, h);
10833 if (ret == 0)
10834 {
10835 eoinfo->failed = true;
10836 return false;
10837 }
10838 else if (ret == 1)
10839 h->indx = indx;
10840 else if (h->indx == -2)
10841 abort();
10842
10843 return true;
10844 }
10845
10846 /* Return TRUE if special handling is done for relocs in SEC against
10847 symbols defined in discarded sections. */
10848
10849 static bool
10850 elf_section_ignore_discarded_relocs (asection *sec)
10851 {
10852 const struct elf_backend_data *bed;
10853
10854 switch (sec->sec_info_type)
10855 {
10856 case SEC_INFO_TYPE_STABS:
10857 case SEC_INFO_TYPE_EH_FRAME:
10858 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10859 return true;
10860 default:
10861 break;
10862 }
10863
10864 bed = get_elf_backend_data (sec->owner);
10865 if (bed->elf_backend_ignore_discarded_relocs != NULL
10866 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10867 return true;
10868
10869 return false;
10870 }
10871
10872 /* Return a mask saying how ld should treat relocations in SEC against
10873 symbols defined in discarded sections. If this function returns
10874 COMPLAIN set, ld will issue a warning message. If this function
10875 returns PRETEND set, and the discarded section was link-once and the
10876 same size as the kept link-once section, ld will pretend that the
10877 symbol was actually defined in the kept section. Otherwise ld will
10878 zero the reloc (at least that is the intent, but some cooperation by
10879 the target dependent code is needed, particularly for REL targets). */
10880
10881 unsigned int
10882 _bfd_elf_default_action_discarded (asection *sec)
10883 {
10884 if (sec->flags & SEC_DEBUGGING)
10885 return PRETEND;
10886
10887 if (strcmp (".eh_frame", sec->name) == 0)
10888 return 0;
10889
10890 if (strcmp (".gcc_except_table", sec->name) == 0)
10891 return 0;
10892
10893 return COMPLAIN | PRETEND;
10894 }
10895
10896 /* Find a match between a section and a member of a section group. */
10897
10898 static asection *
10899 match_group_member (asection *sec, asection *group,
10900 struct bfd_link_info *info)
10901 {
10902 asection *first = elf_next_in_group (group);
10903 asection *s = first;
10904
10905 while (s != NULL)
10906 {
10907 if (bfd_elf_match_symbols_in_sections (s, sec, info))
10908 return s;
10909
10910 s = elf_next_in_group (s);
10911 if (s == first)
10912 break;
10913 }
10914
10915 return NULL;
10916 }
10917
10918 /* Check if the kept section of a discarded section SEC can be used
10919 to replace it. Return the replacement if it is OK. Otherwise return
10920 NULL. */
10921
10922 asection *
10923 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
10924 {
10925 asection *kept;
10926
10927 kept = sec->kept_section;
10928 if (kept != NULL)
10929 {
10930 if ((kept->flags & SEC_GROUP) != 0)
10931 kept = match_group_member (sec, kept, info);
10932 if (kept != NULL)
10933 {
10934 if ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10935 != (kept->rawsize != 0 ? kept->rawsize : kept->size))
10936 kept = NULL;
10937 else
10938 {
10939 /* Get the real kept section. */
10940 asection *next;
10941 for (next = kept->kept_section;
10942 next != NULL;
10943 next = next->kept_section)
10944 kept = next;
10945 }
10946 }
10947 sec->kept_section = kept;
10948 }
10949 return kept;
10950 }
10951
10952 /* Link an input file into the linker output file. This function
10953 handles all the sections and relocations of the input file at once.
10954 This is so that we only have to read the local symbols once, and
10955 don't have to keep them in memory. */
10956
10957 static bool
10958 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
10959 {
10960 int (*relocate_section)
10961 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10962 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10963 bfd *output_bfd;
10964 Elf_Internal_Shdr *symtab_hdr;
10965 size_t locsymcount;
10966 size_t extsymoff;
10967 Elf_Internal_Sym *isymbuf;
10968 Elf_Internal_Sym *isym;
10969 Elf_Internal_Sym *isymend;
10970 long *pindex;
10971 asection **ppsection;
10972 asection *o;
10973 const struct elf_backend_data *bed;
10974 struct elf_link_hash_entry **sym_hashes;
10975 bfd_size_type address_size;
10976 bfd_vma r_type_mask;
10977 int r_sym_shift;
10978 bool have_file_sym = false;
10979
10980 output_bfd = flinfo->output_bfd;
10981 bed = get_elf_backend_data (output_bfd);
10982 relocate_section = bed->elf_backend_relocate_section;
10983
10984 /* If this is a dynamic object, we don't want to do anything here:
10985 we don't want the local symbols, and we don't want the section
10986 contents. */
10987 if ((input_bfd->flags & DYNAMIC) != 0)
10988 return true;
10989
10990 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10991 if (elf_bad_symtab (input_bfd))
10992 {
10993 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10994 extsymoff = 0;
10995 }
10996 else
10997 {
10998 locsymcount = symtab_hdr->sh_info;
10999 extsymoff = symtab_hdr->sh_info;
11000 }
11001
11002 /* Enable GNU OSABI features in the output BFD that are used in the input
11003 BFD. */
11004 if (bed->elf_osabi == ELFOSABI_NONE
11005 || bed->elf_osabi == ELFOSABI_GNU
11006 || bed->elf_osabi == ELFOSABI_FREEBSD)
11007 elf_tdata (output_bfd)->has_gnu_osabi
11008 |= (elf_tdata (input_bfd)->has_gnu_osabi
11009 & (bfd_link_relocatable (flinfo->info)
11010 ? -1 : ~elf_gnu_osabi_retain));
11011
11012 /* Read the local symbols. */
11013 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
11014 if (isymbuf == NULL && locsymcount != 0)
11015 {
11016 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
11017 flinfo->internal_syms,
11018 flinfo->external_syms,
11019 flinfo->locsym_shndx);
11020 if (isymbuf == NULL)
11021 return false;
11022 }
11023
11024 /* Find local symbol sections and adjust values of symbols in
11025 SEC_MERGE sections. Write out those local symbols we know are
11026 going into the output file. */
11027 isymend = PTR_ADD (isymbuf, locsymcount);
11028 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
11029 isym < isymend;
11030 isym++, pindex++, ppsection++)
11031 {
11032 asection *isec;
11033 const char *name;
11034 Elf_Internal_Sym osym;
11035 long indx;
11036 int ret;
11037
11038 *pindex = -1;
11039
11040 if (elf_bad_symtab (input_bfd))
11041 {
11042 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
11043 {
11044 *ppsection = NULL;
11045 continue;
11046 }
11047 }
11048
11049 if (isym->st_shndx == SHN_UNDEF)
11050 isec = bfd_und_section_ptr;
11051 else if (isym->st_shndx == SHN_ABS)
11052 isec = bfd_abs_section_ptr;
11053 else if (isym->st_shndx == SHN_COMMON)
11054 isec = bfd_com_section_ptr;
11055 else
11056 {
11057 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
11058 if (isec == NULL)
11059 {
11060 /* Don't attempt to output symbols with st_shnx in the
11061 reserved range other than SHN_ABS and SHN_COMMON. */
11062 isec = bfd_und_section_ptr;
11063 }
11064 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
11065 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
11066 isym->st_value =
11067 _bfd_merged_section_offset (output_bfd, &isec,
11068 elf_section_data (isec)->sec_info,
11069 isym->st_value);
11070 }
11071
11072 *ppsection = isec;
11073
11074 /* Don't output the first, undefined, symbol. In fact, don't
11075 output any undefined local symbol. */
11076 if (isec == bfd_und_section_ptr)
11077 continue;
11078
11079 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
11080 {
11081 /* We never output section symbols. Instead, we use the
11082 section symbol of the corresponding section in the output
11083 file. */
11084 continue;
11085 }
11086
11087 /* If we are stripping all symbols, we don't want to output this
11088 one. */
11089 if (flinfo->info->strip == strip_all)
11090 continue;
11091
11092 /* If we are discarding all local symbols, we don't want to
11093 output this one. If we are generating a relocatable output
11094 file, then some of the local symbols may be required by
11095 relocs; we output them below as we discover that they are
11096 needed. */
11097 if (flinfo->info->discard == discard_all)
11098 continue;
11099
11100 /* If this symbol is defined in a section which we are
11101 discarding, we don't need to keep it. */
11102 if (isym->st_shndx != SHN_UNDEF
11103 && isym->st_shndx < SHN_LORESERVE
11104 && isec->output_section == NULL
11105 && flinfo->info->non_contiguous_regions
11106 && flinfo->info->non_contiguous_regions_warnings)
11107 {
11108 _bfd_error_handler (_("warning: --enable-non-contiguous-regions "
11109 "discards section `%s' from '%s'\n"),
11110 isec->name, bfd_get_filename (isec->owner));
11111 continue;
11112 }
11113
11114 if (isym->st_shndx != SHN_UNDEF
11115 && isym->st_shndx < SHN_LORESERVE
11116 && bfd_section_removed_from_list (output_bfd,
11117 isec->output_section))
11118 continue;
11119
11120 /* Get the name of the symbol. */
11121 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
11122 isym->st_name);
11123 if (name == NULL)
11124 return false;
11125
11126 /* See if we are discarding symbols with this name. */
11127 if ((flinfo->info->strip == strip_some
11128 && (bfd_hash_lookup (flinfo->info->keep_hash, name, false, false)
11129 == NULL))
11130 || (((flinfo->info->discard == discard_sec_merge
11131 && (isec->flags & SEC_MERGE)
11132 && !bfd_link_relocatable (flinfo->info))
11133 || flinfo->info->discard == discard_l)
11134 && bfd_is_local_label_name (input_bfd, name)))
11135 continue;
11136
11137 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
11138 {
11139 if (input_bfd->lto_output)
11140 /* -flto puts a temp file name here. This means builds
11141 are not reproducible. Discard the symbol. */
11142 continue;
11143 have_file_sym = true;
11144 flinfo->filesym_count += 1;
11145 }
11146 if (!have_file_sym)
11147 {
11148 /* In the absence of debug info, bfd_find_nearest_line uses
11149 FILE symbols to determine the source file for local
11150 function symbols. Provide a FILE symbol here if input
11151 files lack such, so that their symbols won't be
11152 associated with a previous input file. It's not the
11153 source file, but the best we can do. */
11154 const char *filename;
11155 have_file_sym = true;
11156 flinfo->filesym_count += 1;
11157 memset (&osym, 0, sizeof (osym));
11158 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
11159 osym.st_shndx = SHN_ABS;
11160 if (input_bfd->lto_output)
11161 filename = NULL;
11162 else
11163 filename = lbasename (bfd_get_filename (input_bfd));
11164 if (!elf_link_output_symstrtab (flinfo, filename, &osym,
11165 bfd_abs_section_ptr, NULL))
11166 return false;
11167 }
11168
11169 osym = *isym;
11170
11171 /* Adjust the section index for the output file. */
11172 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11173 isec->output_section);
11174 if (osym.st_shndx == SHN_BAD)
11175 return false;
11176
11177 /* ELF symbols in relocatable files are section relative, but
11178 in executable files they are virtual addresses. Note that
11179 this code assumes that all ELF sections have an associated
11180 BFD section with a reasonable value for output_offset; below
11181 we assume that they also have a reasonable value for
11182 output_section. Any special sections must be set up to meet
11183 these requirements. */
11184 osym.st_value += isec->output_offset;
11185 if (!bfd_link_relocatable (flinfo->info))
11186 {
11187 osym.st_value += isec->output_section->vma;
11188 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
11189 {
11190 /* STT_TLS symbols are relative to PT_TLS segment base. */
11191 if (elf_hash_table (flinfo->info)->tls_sec != NULL)
11192 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
11193 else
11194 osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
11195 STT_NOTYPE);
11196 }
11197 }
11198
11199 indx = bfd_get_symcount (output_bfd);
11200 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
11201 if (ret == 0)
11202 return false;
11203 else if (ret == 1)
11204 *pindex = indx;
11205 }
11206
11207 if (bed->s->arch_size == 32)
11208 {
11209 r_type_mask = 0xff;
11210 r_sym_shift = 8;
11211 address_size = 4;
11212 }
11213 else
11214 {
11215 r_type_mask = 0xffffffff;
11216 r_sym_shift = 32;
11217 address_size = 8;
11218 }
11219
11220 /* Relocate the contents of each section. */
11221 sym_hashes = elf_sym_hashes (input_bfd);
11222 for (o = input_bfd->sections; o != NULL; o = o->next)
11223 {
11224 bfd_byte *contents;
11225
11226 if (! o->linker_mark)
11227 {
11228 /* This section was omitted from the link. */
11229 continue;
11230 }
11231
11232 if (!flinfo->info->resolve_section_groups
11233 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
11234 {
11235 /* Deal with the group signature symbol. */
11236 struct bfd_elf_section_data *sec_data = elf_section_data (o);
11237 unsigned long symndx = sec_data->this_hdr.sh_info;
11238 asection *osec = o->output_section;
11239
11240 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
11241 if (symndx >= locsymcount
11242 || (elf_bad_symtab (input_bfd)
11243 && flinfo->sections[symndx] == NULL))
11244 {
11245 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
11246 while (h->root.type == bfd_link_hash_indirect
11247 || h->root.type == bfd_link_hash_warning)
11248 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11249 /* Arrange for symbol to be output. */
11250 h->indx = -2;
11251 elf_section_data (osec)->this_hdr.sh_info = -2;
11252 }
11253 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
11254 {
11255 /* We'll use the output section target_index. */
11256 asection *sec = flinfo->sections[symndx]->output_section;
11257 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
11258 }
11259 else
11260 {
11261 if (flinfo->indices[symndx] == -1)
11262 {
11263 /* Otherwise output the local symbol now. */
11264 Elf_Internal_Sym sym = isymbuf[symndx];
11265 asection *sec = flinfo->sections[symndx]->output_section;
11266 const char *name;
11267 long indx;
11268 int ret;
11269
11270 name = bfd_elf_string_from_elf_section (input_bfd,
11271 symtab_hdr->sh_link,
11272 sym.st_name);
11273 if (name == NULL)
11274 return false;
11275
11276 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11277 sec);
11278 if (sym.st_shndx == SHN_BAD)
11279 return false;
11280
11281 sym.st_value += o->output_offset;
11282
11283 indx = bfd_get_symcount (output_bfd);
11284 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
11285 NULL);
11286 if (ret == 0)
11287 return false;
11288 else if (ret == 1)
11289 flinfo->indices[symndx] = indx;
11290 else
11291 abort ();
11292 }
11293 elf_section_data (osec)->this_hdr.sh_info
11294 = flinfo->indices[symndx];
11295 }
11296 }
11297
11298 if ((o->flags & SEC_HAS_CONTENTS) == 0
11299 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
11300 continue;
11301
11302 if ((o->flags & SEC_LINKER_CREATED) != 0)
11303 {
11304 /* Section was created by _bfd_elf_link_create_dynamic_sections
11305 or somesuch. */
11306 continue;
11307 }
11308
11309 /* Get the contents of the section. They have been cached by a
11310 relaxation routine. Note that o is a section in an input
11311 file, so the contents field will not have been set by any of
11312 the routines which work on output files. */
11313 if (elf_section_data (o)->this_hdr.contents != NULL)
11314 {
11315 contents = elf_section_data (o)->this_hdr.contents;
11316 if (bed->caches_rawsize
11317 && o->rawsize != 0
11318 && o->rawsize < o->size)
11319 {
11320 memcpy (flinfo->contents, contents, o->rawsize);
11321 contents = flinfo->contents;
11322 }
11323 }
11324 else
11325 {
11326 contents = flinfo->contents;
11327 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
11328 return false;
11329 }
11330
11331 if ((o->flags & SEC_RELOC) != 0)
11332 {
11333 Elf_Internal_Rela *internal_relocs;
11334 Elf_Internal_Rela *rel, *relend;
11335 int action_discarded;
11336 int ret;
11337
11338 /* Get the swapped relocs. */
11339 internal_relocs
11340 = _bfd_elf_link_info_read_relocs (input_bfd, flinfo->info, o,
11341 flinfo->external_relocs,
11342 flinfo->internal_relocs,
11343 false);
11344 if (internal_relocs == NULL
11345 && o->reloc_count > 0)
11346 return false;
11347
11348 action_discarded = -1;
11349 if (!elf_section_ignore_discarded_relocs (o))
11350 action_discarded = (*bed->action_discarded) (o);
11351
11352 /* Run through the relocs evaluating complex reloc symbols and
11353 looking for relocs against symbols from discarded sections
11354 or section symbols from removed link-once sections.
11355 Complain about relocs against discarded sections. Zero
11356 relocs against removed link-once sections. */
11357
11358 rel = internal_relocs;
11359 relend = rel + o->reloc_count;
11360 for ( ; rel < relend; rel++)
11361 {
11362 unsigned long r_symndx = rel->r_info >> r_sym_shift;
11363 unsigned int s_type;
11364 asection **ps, *sec;
11365 struct elf_link_hash_entry *h = NULL;
11366 const char *sym_name;
11367
11368 if (r_symndx == STN_UNDEF)
11369 continue;
11370
11371 if (r_symndx >= locsymcount
11372 || (elf_bad_symtab (input_bfd)
11373 && flinfo->sections[r_symndx] == NULL))
11374 {
11375 h = sym_hashes[r_symndx - extsymoff];
11376
11377 /* Badly formatted input files can contain relocs that
11378 reference non-existant symbols. Check here so that
11379 we do not seg fault. */
11380 if (h == NULL)
11381 {
11382 _bfd_error_handler
11383 /* xgettext:c-format */
11384 (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
11385 "that references a non-existent global symbol"),
11386 input_bfd, (uint64_t) rel->r_info, o);
11387 bfd_set_error (bfd_error_bad_value);
11388 return false;
11389 }
11390
11391 while (h->root.type == bfd_link_hash_indirect
11392 || h->root.type == bfd_link_hash_warning)
11393 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11394
11395 s_type = h->type;
11396
11397 /* If a plugin symbol is referenced from a non-IR file,
11398 mark the symbol as undefined. Note that the
11399 linker may attach linker created dynamic sections
11400 to the plugin bfd. Symbols defined in linker
11401 created sections are not plugin symbols. */
11402 if ((h->root.non_ir_ref_regular
11403 || h->root.non_ir_ref_dynamic)
11404 && (h->root.type == bfd_link_hash_defined
11405 || h->root.type == bfd_link_hash_defweak)
11406 && (h->root.u.def.section->flags
11407 & SEC_LINKER_CREATED) == 0
11408 && h->root.u.def.section->owner != NULL
11409 && (h->root.u.def.section->owner->flags
11410 & BFD_PLUGIN) != 0)
11411 {
11412 h->root.type = bfd_link_hash_undefined;
11413 h->root.u.undef.abfd = h->root.u.def.section->owner;
11414 }
11415
11416 ps = NULL;
11417 if (h->root.type == bfd_link_hash_defined
11418 || h->root.type == bfd_link_hash_defweak)
11419 ps = &h->root.u.def.section;
11420
11421 sym_name = h->root.root.string;
11422 }
11423 else
11424 {
11425 Elf_Internal_Sym *sym = isymbuf + r_symndx;
11426
11427 s_type = ELF_ST_TYPE (sym->st_info);
11428 ps = &flinfo->sections[r_symndx];
11429 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
11430 sym, *ps);
11431 }
11432
11433 if ((s_type == STT_RELC || s_type == STT_SRELC)
11434 && !bfd_link_relocatable (flinfo->info))
11435 {
11436 bfd_vma val;
11437 bfd_vma dot = (rel->r_offset
11438 + o->output_offset + o->output_section->vma);
11439 #ifdef DEBUG
11440 printf ("Encountered a complex symbol!");
11441 printf (" (input_bfd %s, section %s, reloc %ld\n",
11442 bfd_get_filename (input_bfd), o->name,
11443 (long) (rel - internal_relocs));
11444 printf (" symbol: idx %8.8lx, name %s\n",
11445 r_symndx, sym_name);
11446 printf (" reloc : info %8.8lx, addr %8.8lx\n",
11447 (unsigned long) rel->r_info,
11448 (unsigned long) rel->r_offset);
11449 #endif
11450 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
11451 isymbuf, locsymcount, s_type == STT_SRELC))
11452 return false;
11453
11454 /* Symbol evaluated OK. Update to absolute value. */
11455 set_symbol_value (input_bfd, isymbuf, locsymcount,
11456 r_symndx, val);
11457 continue;
11458 }
11459
11460 if (action_discarded != -1 && ps != NULL)
11461 {
11462 /* Complain if the definition comes from a
11463 discarded section. */
11464 if ((sec = *ps) != NULL && discarded_section (sec))
11465 {
11466 BFD_ASSERT (r_symndx != STN_UNDEF);
11467 if (action_discarded & COMPLAIN)
11468 (*flinfo->info->callbacks->einfo)
11469 /* xgettext:c-format */
11470 (_("%X`%s' referenced in section `%pA' of %pB: "
11471 "defined in discarded section `%pA' of %pB\n"),
11472 sym_name, o, input_bfd, sec, sec->owner);
11473
11474 /* Try to do the best we can to support buggy old
11475 versions of gcc. Pretend that the symbol is
11476 really defined in the kept linkonce section.
11477 FIXME: This is quite broken. Modifying the
11478 symbol here means we will be changing all later
11479 uses of the symbol, not just in this section. */
11480 if (action_discarded & PRETEND)
11481 {
11482 asection *kept;
11483
11484 kept = _bfd_elf_check_kept_section (sec,
11485 flinfo->info);
11486 if (kept != NULL)
11487 {
11488 *ps = kept;
11489 continue;
11490 }
11491 }
11492 }
11493 }
11494 }
11495
11496 /* Relocate the section by invoking a back end routine.
11497
11498 The back end routine is responsible for adjusting the
11499 section contents as necessary, and (if using Rela relocs
11500 and generating a relocatable output file) adjusting the
11501 reloc addend as necessary.
11502
11503 The back end routine does not have to worry about setting
11504 the reloc address or the reloc symbol index.
11505
11506 The back end routine is given a pointer to the swapped in
11507 internal symbols, and can access the hash table entries
11508 for the external symbols via elf_sym_hashes (input_bfd).
11509
11510 When generating relocatable output, the back end routine
11511 must handle STB_LOCAL/STT_SECTION symbols specially. The
11512 output symbol is going to be a section symbol
11513 corresponding to the output section, which will require
11514 the addend to be adjusted. */
11515
11516 ret = (*relocate_section) (output_bfd, flinfo->info,
11517 input_bfd, o, contents,
11518 internal_relocs,
11519 isymbuf,
11520 flinfo->sections);
11521 if (!ret)
11522 return false;
11523
11524 if (ret == 2
11525 || bfd_link_relocatable (flinfo->info)
11526 || flinfo->info->emitrelocations)
11527 {
11528 Elf_Internal_Rela *irela;
11529 Elf_Internal_Rela *irelaend, *irelamid;
11530 bfd_vma last_offset;
11531 struct elf_link_hash_entry **rel_hash;
11532 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
11533 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
11534 unsigned int next_erel;
11535 bool rela_normal;
11536 struct bfd_elf_section_data *esdi, *esdo;
11537
11538 esdi = elf_section_data (o);
11539 esdo = elf_section_data (o->output_section);
11540 rela_normal = false;
11541
11542 /* Adjust the reloc addresses and symbol indices. */
11543
11544 irela = internal_relocs;
11545 irelaend = irela + o->reloc_count;
11546 rel_hash = PTR_ADD (esdo->rel.hashes, esdo->rel.count);
11547 /* We start processing the REL relocs, if any. When we reach
11548 IRELAMID in the loop, we switch to the RELA relocs. */
11549 irelamid = irela;
11550 if (esdi->rel.hdr != NULL)
11551 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
11552 * bed->s->int_rels_per_ext_rel);
11553 rel_hash_list = rel_hash;
11554 rela_hash_list = NULL;
11555 last_offset = o->output_offset;
11556 if (!bfd_link_relocatable (flinfo->info))
11557 last_offset += o->output_section->vma;
11558 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
11559 {
11560 unsigned long r_symndx;
11561 asection *sec;
11562 Elf_Internal_Sym sym;
11563
11564 if (next_erel == bed->s->int_rels_per_ext_rel)
11565 {
11566 rel_hash++;
11567 next_erel = 0;
11568 }
11569
11570 if (irela == irelamid)
11571 {
11572 rel_hash = PTR_ADD (esdo->rela.hashes, esdo->rela.count);
11573 rela_hash_list = rel_hash;
11574 rela_normal = bed->rela_normal;
11575 }
11576
11577 irela->r_offset = _bfd_elf_section_offset (output_bfd,
11578 flinfo->info, o,
11579 irela->r_offset);
11580 if (irela->r_offset >= (bfd_vma) -2)
11581 {
11582 /* This is a reloc for a deleted entry or somesuch.
11583 Turn it into an R_*_NONE reloc, at the same
11584 offset as the last reloc. elf_eh_frame.c and
11585 bfd_elf_discard_info rely on reloc offsets
11586 being ordered. */
11587 irela->r_offset = last_offset;
11588 irela->r_info = 0;
11589 irela->r_addend = 0;
11590 continue;
11591 }
11592
11593 irela->r_offset += o->output_offset;
11594
11595 /* Relocs in an executable have to be virtual addresses. */
11596 if (!bfd_link_relocatable (flinfo->info))
11597 irela->r_offset += o->output_section->vma;
11598
11599 last_offset = irela->r_offset;
11600
11601 r_symndx = irela->r_info >> r_sym_shift;
11602 if (r_symndx == STN_UNDEF)
11603 continue;
11604
11605 if (r_symndx >= locsymcount
11606 || (elf_bad_symtab (input_bfd)
11607 && flinfo->sections[r_symndx] == NULL))
11608 {
11609 struct elf_link_hash_entry *rh;
11610 unsigned long indx;
11611
11612 /* This is a reloc against a global symbol. We
11613 have not yet output all the local symbols, so
11614 we do not know the symbol index of any global
11615 symbol. We set the rel_hash entry for this
11616 reloc to point to the global hash table entry
11617 for this symbol. The symbol index is then
11618 set at the end of bfd_elf_final_link. */
11619 indx = r_symndx - extsymoff;
11620 rh = elf_sym_hashes (input_bfd)[indx];
11621 while (rh->root.type == bfd_link_hash_indirect
11622 || rh->root.type == bfd_link_hash_warning)
11623 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
11624
11625 /* Setting the index to -2 tells
11626 elf_link_output_extsym that this symbol is
11627 used by a reloc. */
11628 BFD_ASSERT (rh->indx < 0);
11629 rh->indx = -2;
11630 *rel_hash = rh;
11631
11632 continue;
11633 }
11634
11635 /* This is a reloc against a local symbol. */
11636
11637 *rel_hash = NULL;
11638 sym = isymbuf[r_symndx];
11639 sec = flinfo->sections[r_symndx];
11640 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
11641 {
11642 /* I suppose the backend ought to fill in the
11643 section of any STT_SECTION symbol against a
11644 processor specific section. */
11645 r_symndx = STN_UNDEF;
11646 if (bfd_is_abs_section (sec))
11647 ;
11648 else if (sec == NULL || sec->owner == NULL)
11649 {
11650 bfd_set_error (bfd_error_bad_value);
11651 return false;
11652 }
11653 else
11654 {
11655 asection *osec = sec->output_section;
11656
11657 /* If we have discarded a section, the output
11658 section will be the absolute section. In
11659 case of discarded SEC_MERGE sections, use
11660 the kept section. relocate_section should
11661 have already handled discarded linkonce
11662 sections. */
11663 if (bfd_is_abs_section (osec)
11664 && sec->kept_section != NULL
11665 && sec->kept_section->output_section != NULL)
11666 {
11667 osec = sec->kept_section->output_section;
11668 irela->r_addend -= osec->vma;
11669 }
11670
11671 if (!bfd_is_abs_section (osec))
11672 {
11673 r_symndx = osec->target_index;
11674 if (r_symndx == STN_UNDEF)
11675 {
11676 irela->r_addend += osec->vma;
11677 osec = _bfd_nearby_section (output_bfd, osec,
11678 osec->vma);
11679 irela->r_addend -= osec->vma;
11680 r_symndx = osec->target_index;
11681 }
11682 }
11683 }
11684
11685 /* Adjust the addend according to where the
11686 section winds up in the output section. */
11687 if (rela_normal)
11688 irela->r_addend += sec->output_offset;
11689 }
11690 else
11691 {
11692 if (flinfo->indices[r_symndx] == -1)
11693 {
11694 unsigned long shlink;
11695 const char *name;
11696 asection *osec;
11697 long indx;
11698
11699 if (flinfo->info->strip == strip_all)
11700 {
11701 /* You can't do ld -r -s. */
11702 bfd_set_error (bfd_error_invalid_operation);
11703 return false;
11704 }
11705
11706 /* This symbol was skipped earlier, but
11707 since it is needed by a reloc, we
11708 must output it now. */
11709 shlink = symtab_hdr->sh_link;
11710 name = (bfd_elf_string_from_elf_section
11711 (input_bfd, shlink, sym.st_name));
11712 if (name == NULL)
11713 return false;
11714
11715 osec = sec->output_section;
11716 sym.st_shndx =
11717 _bfd_elf_section_from_bfd_section (output_bfd,
11718 osec);
11719 if (sym.st_shndx == SHN_BAD)
11720 return false;
11721
11722 sym.st_value += sec->output_offset;
11723 if (!bfd_link_relocatable (flinfo->info))
11724 {
11725 sym.st_value += osec->vma;
11726 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
11727 {
11728 struct elf_link_hash_table *htab
11729 = elf_hash_table (flinfo->info);
11730
11731 /* STT_TLS symbols are relative to PT_TLS
11732 segment base. */
11733 if (htab->tls_sec != NULL)
11734 sym.st_value -= htab->tls_sec->vma;
11735 else
11736 sym.st_info
11737 = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
11738 STT_NOTYPE);
11739 }
11740 }
11741
11742 indx = bfd_get_symcount (output_bfd);
11743 ret = elf_link_output_symstrtab (flinfo, name,
11744 &sym, sec,
11745 NULL);
11746 if (ret == 0)
11747 return false;
11748 else if (ret == 1)
11749 flinfo->indices[r_symndx] = indx;
11750 else
11751 abort ();
11752 }
11753
11754 r_symndx = flinfo->indices[r_symndx];
11755 }
11756
11757 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
11758 | (irela->r_info & r_type_mask));
11759 }
11760
11761 /* Swap out the relocs. */
11762 input_rel_hdr = esdi->rel.hdr;
11763 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
11764 {
11765 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11766 input_rel_hdr,
11767 internal_relocs,
11768 rel_hash_list))
11769 return false;
11770 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
11771 * bed->s->int_rels_per_ext_rel);
11772 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
11773 }
11774
11775 input_rela_hdr = esdi->rela.hdr;
11776 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
11777 {
11778 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11779 input_rela_hdr,
11780 internal_relocs,
11781 rela_hash_list))
11782 return false;
11783 }
11784 }
11785 }
11786
11787 /* Write out the modified section contents. */
11788 if (bed->elf_backend_write_section
11789 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
11790 contents))
11791 {
11792 /* Section written out. */
11793 }
11794 else switch (o->sec_info_type)
11795 {
11796 case SEC_INFO_TYPE_STABS:
11797 if (! (_bfd_write_section_stabs
11798 (output_bfd,
11799 &elf_hash_table (flinfo->info)->stab_info,
11800 o, &elf_section_data (o)->sec_info, contents)))
11801 return false;
11802 break;
11803 case SEC_INFO_TYPE_MERGE:
11804 if (! _bfd_write_merged_section (output_bfd, o,
11805 elf_section_data (o)->sec_info))
11806 return false;
11807 break;
11808 case SEC_INFO_TYPE_EH_FRAME:
11809 {
11810 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
11811 o, contents))
11812 return false;
11813 }
11814 break;
11815 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11816 {
11817 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11818 flinfo->info,
11819 o, contents))
11820 return false;
11821 }
11822 break;
11823 default:
11824 {
11825 if (! (o->flags & SEC_EXCLUDE))
11826 {
11827 file_ptr offset = (file_ptr) o->output_offset;
11828 bfd_size_type todo = o->size;
11829
11830 offset *= bfd_octets_per_byte (output_bfd, o);
11831
11832 if ((o->flags & SEC_ELF_REVERSE_COPY)
11833 && o->size > address_size)
11834 {
11835 /* Reverse-copy input section to output. */
11836
11837 if ((o->size & (address_size - 1)) != 0
11838 || (o->reloc_count != 0
11839 && (o->size * bed->s->int_rels_per_ext_rel
11840 != o->reloc_count * address_size)))
11841 {
11842 _bfd_error_handler
11843 /* xgettext:c-format */
11844 (_("error: %pB: size of section %pA is not "
11845 "multiple of address size"),
11846 input_bfd, o);
11847 bfd_set_error (bfd_error_bad_value);
11848 return false;
11849 }
11850
11851 do
11852 {
11853 todo -= address_size;
11854 if (! bfd_set_section_contents (output_bfd,
11855 o->output_section,
11856 contents + todo,
11857 offset,
11858 address_size))
11859 return false;
11860 if (todo == 0)
11861 break;
11862 offset += address_size;
11863 }
11864 while (1);
11865 }
11866 else if (! bfd_set_section_contents (output_bfd,
11867 o->output_section,
11868 contents,
11869 offset, todo))
11870 return false;
11871 }
11872 }
11873 break;
11874 }
11875 }
11876
11877 return true;
11878 }
11879
11880 /* Generate a reloc when linking an ELF file. This is a reloc
11881 requested by the linker, and does not come from any input file. This
11882 is used to build constructor and destructor tables when linking
11883 with -Ur. */
11884
11885 static bool
11886 elf_reloc_link_order (bfd *output_bfd,
11887 struct bfd_link_info *info,
11888 asection *output_section,
11889 struct bfd_link_order *link_order)
11890 {
11891 reloc_howto_type *howto;
11892 long indx;
11893 bfd_vma offset;
11894 bfd_vma addend;
11895 struct bfd_elf_section_reloc_data *reldata;
11896 struct elf_link_hash_entry **rel_hash_ptr;
11897 Elf_Internal_Shdr *rel_hdr;
11898 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11899 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11900 bfd_byte *erel;
11901 unsigned int i;
11902 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
11903
11904 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11905 if (howto == NULL)
11906 {
11907 bfd_set_error (bfd_error_bad_value);
11908 return false;
11909 }
11910
11911 addend = link_order->u.reloc.p->addend;
11912
11913 if (esdo->rel.hdr)
11914 reldata = &esdo->rel;
11915 else if (esdo->rela.hdr)
11916 reldata = &esdo->rela;
11917 else
11918 {
11919 reldata = NULL;
11920 BFD_ASSERT (0);
11921 }
11922
11923 /* Figure out the symbol index. */
11924 rel_hash_ptr = reldata->hashes + reldata->count;
11925 if (link_order->type == bfd_section_reloc_link_order)
11926 {
11927 indx = link_order->u.reloc.p->u.section->target_index;
11928 BFD_ASSERT (indx != 0);
11929 *rel_hash_ptr = NULL;
11930 }
11931 else
11932 {
11933 struct elf_link_hash_entry *h;
11934
11935 /* Treat a reloc against a defined symbol as though it were
11936 actually against the section. */
11937 h = ((struct elf_link_hash_entry *)
11938 bfd_wrapped_link_hash_lookup (output_bfd, info,
11939 link_order->u.reloc.p->u.name,
11940 false, false, true));
11941 if (h != NULL
11942 && (h->root.type == bfd_link_hash_defined
11943 || h->root.type == bfd_link_hash_defweak))
11944 {
11945 asection *section;
11946
11947 section = h->root.u.def.section;
11948 indx = section->output_section->target_index;
11949 *rel_hash_ptr = NULL;
11950 /* It seems that we ought to add the symbol value to the
11951 addend here, but in practice it has already been added
11952 because it was passed to constructor_callback. */
11953 addend += section->output_section->vma + section->output_offset;
11954 }
11955 else if (h != NULL)
11956 {
11957 /* Setting the index to -2 tells elf_link_output_extsym that
11958 this symbol is used by a reloc. */
11959 h->indx = -2;
11960 *rel_hash_ptr = h;
11961 indx = 0;
11962 }
11963 else
11964 {
11965 (*info->callbacks->unattached_reloc)
11966 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
11967 indx = 0;
11968 }
11969 }
11970
11971 /* If this is an inplace reloc, we must write the addend into the
11972 object file. */
11973 if (howto->partial_inplace && addend != 0)
11974 {
11975 bfd_size_type size;
11976 bfd_reloc_status_type rstat;
11977 bfd_byte *buf;
11978 bool ok;
11979 const char *sym_name;
11980 bfd_size_type octets;
11981
11982 size = (bfd_size_type) bfd_get_reloc_size (howto);
11983 buf = (bfd_byte *) bfd_zmalloc (size);
11984 if (buf == NULL && size != 0)
11985 return false;
11986 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11987 switch (rstat)
11988 {
11989 case bfd_reloc_ok:
11990 break;
11991
11992 default:
11993 case bfd_reloc_outofrange:
11994 abort ();
11995
11996 case bfd_reloc_overflow:
11997 if (link_order->type == bfd_section_reloc_link_order)
11998 sym_name = bfd_section_name (link_order->u.reloc.p->u.section);
11999 else
12000 sym_name = link_order->u.reloc.p->u.name;
12001 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
12002 howto->name, addend, NULL, NULL,
12003 (bfd_vma) 0);
12004 break;
12005 }
12006
12007 octets = link_order->offset * bfd_octets_per_byte (output_bfd,
12008 output_section);
12009 ok = bfd_set_section_contents (output_bfd, output_section, buf,
12010 octets, size);
12011 free (buf);
12012 if (! ok)
12013 return false;
12014 }
12015
12016 /* The address of a reloc is relative to the section in a
12017 relocatable file, and is a virtual address in an executable
12018 file. */
12019 offset = link_order->offset;
12020 if (! bfd_link_relocatable (info))
12021 offset += output_section->vma;
12022
12023 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
12024 {
12025 irel[i].r_offset = offset;
12026 irel[i].r_info = 0;
12027 irel[i].r_addend = 0;
12028 }
12029 if (bed->s->arch_size == 32)
12030 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
12031 else
12032 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
12033
12034 rel_hdr = reldata->hdr;
12035 erel = rel_hdr->contents;
12036 if (rel_hdr->sh_type == SHT_REL)
12037 {
12038 erel += reldata->count * bed->s->sizeof_rel;
12039 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
12040 }
12041 else
12042 {
12043 irel[0].r_addend = addend;
12044 erel += reldata->count * bed->s->sizeof_rela;
12045 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
12046 }
12047
12048 ++reldata->count;
12049
12050 return true;
12051 }
12052
12053 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
12054 Returns TRUE upon success, FALSE otherwise. */
12055
12056 static bool
12057 elf_output_implib (bfd *abfd, struct bfd_link_info *info)
12058 {
12059 bool ret = false;
12060 bfd *implib_bfd;
12061 const struct elf_backend_data *bed;
12062 flagword flags;
12063 enum bfd_architecture arch;
12064 unsigned int mach;
12065 asymbol **sympp = NULL;
12066 long symsize;
12067 long symcount;
12068 long src_count;
12069 elf_symbol_type *osymbuf;
12070 size_t amt;
12071
12072 implib_bfd = info->out_implib_bfd;
12073 bed = get_elf_backend_data (abfd);
12074
12075 if (!bfd_set_format (implib_bfd, bfd_object))
12076 return false;
12077
12078 /* Use flag from executable but make it a relocatable object. */
12079 flags = bfd_get_file_flags (abfd);
12080 flags &= ~HAS_RELOC;
12081 if (!bfd_set_start_address (implib_bfd, 0)
12082 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
12083 return false;
12084
12085 /* Copy architecture of output file to import library file. */
12086 arch = bfd_get_arch (abfd);
12087 mach = bfd_get_mach (abfd);
12088 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
12089 && (abfd->target_defaulted
12090 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
12091 return false;
12092
12093 /* Get symbol table size. */
12094 symsize = bfd_get_symtab_upper_bound (abfd);
12095 if (symsize < 0)
12096 return false;
12097
12098 /* Read in the symbol table. */
12099 sympp = (asymbol **) bfd_malloc (symsize);
12100 if (sympp == NULL)
12101 return false;
12102
12103 symcount = bfd_canonicalize_symtab (abfd, sympp);
12104 if (symcount < 0)
12105 goto free_sym_buf;
12106
12107 /* Allow the BFD backend to copy any private header data it
12108 understands from the output BFD to the import library BFD. */
12109 if (! bfd_copy_private_header_data (abfd, implib_bfd))
12110 goto free_sym_buf;
12111
12112 /* Filter symbols to appear in the import library. */
12113 if (bed->elf_backend_filter_implib_symbols)
12114 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
12115 symcount);
12116 else
12117 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
12118 if (symcount == 0)
12119 {
12120 bfd_set_error (bfd_error_no_symbols);
12121 _bfd_error_handler (_("%pB: no symbol found for import library"),
12122 implib_bfd);
12123 goto free_sym_buf;
12124 }
12125
12126
12127 /* Make symbols absolute. */
12128 amt = symcount * sizeof (*osymbuf);
12129 osymbuf = (elf_symbol_type *) bfd_alloc (implib_bfd, amt);
12130 if (osymbuf == NULL)
12131 goto free_sym_buf;
12132
12133 for (src_count = 0; src_count < symcount; src_count++)
12134 {
12135 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
12136 sizeof (*osymbuf));
12137 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
12138 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
12139 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
12140 osymbuf[src_count].internal_elf_sym.st_value =
12141 osymbuf[src_count].symbol.value;
12142 sympp[src_count] = &osymbuf[src_count].symbol;
12143 }
12144
12145 bfd_set_symtab (implib_bfd, sympp, symcount);
12146
12147 /* Allow the BFD backend to copy any private data it understands
12148 from the output BFD to the import library BFD. This is done last
12149 to permit the routine to look at the filtered symbol table. */
12150 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
12151 goto free_sym_buf;
12152
12153 if (!bfd_close (implib_bfd))
12154 goto free_sym_buf;
12155
12156 ret = true;
12157
12158 free_sym_buf:
12159 free (sympp);
12160 return ret;
12161 }
12162
12163 static void
12164 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
12165 {
12166 asection *o;
12167
12168 if (flinfo->symstrtab != NULL)
12169 _bfd_elf_strtab_free (flinfo->symstrtab);
12170 free (flinfo->contents);
12171 free (flinfo->external_relocs);
12172 free (flinfo->internal_relocs);
12173 free (flinfo->external_syms);
12174 free (flinfo->locsym_shndx);
12175 free (flinfo->internal_syms);
12176 free (flinfo->indices);
12177 free (flinfo->sections);
12178 if (flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1)
12179 free (flinfo->symshndxbuf);
12180 for (o = obfd->sections; o != NULL; o = o->next)
12181 {
12182 struct bfd_elf_section_data *esdo = elf_section_data (o);
12183 free (esdo->rel.hashes);
12184 free (esdo->rela.hashes);
12185 }
12186 }
12187
12188 /* Do the final step of an ELF link. */
12189
12190 bool
12191 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
12192 {
12193 bool dynamic;
12194 bool emit_relocs;
12195 bfd *dynobj;
12196 struct elf_final_link_info flinfo;
12197 asection *o;
12198 struct bfd_link_order *p;
12199 bfd *sub;
12200 bfd_size_type max_contents_size;
12201 bfd_size_type max_external_reloc_size;
12202 bfd_size_type max_internal_reloc_count;
12203 bfd_size_type max_sym_count;
12204 bfd_size_type max_sym_shndx_count;
12205 Elf_Internal_Sym elfsym;
12206 unsigned int i;
12207 Elf_Internal_Shdr *symtab_hdr;
12208 Elf_Internal_Shdr *symtab_shndx_hdr;
12209 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12210 struct elf_outext_info eoinfo;
12211 bool merged;
12212 size_t relativecount;
12213 size_t relr_entsize;
12214 asection *reldyn = 0;
12215 bfd_size_type amt;
12216 asection *attr_section = NULL;
12217 bfd_vma attr_size = 0;
12218 const char *std_attrs_section;
12219 struct elf_link_hash_table *htab = elf_hash_table (info);
12220 bool sections_removed;
12221 bool ret;
12222
12223 if (!is_elf_hash_table (&htab->root))
12224 return false;
12225
12226 if (bfd_link_pic (info))
12227 abfd->flags |= DYNAMIC;
12228
12229 dynamic = htab->dynamic_sections_created;
12230 dynobj = htab->dynobj;
12231
12232 emit_relocs = (bfd_link_relocatable (info)
12233 || info->emitrelocations);
12234
12235 memset (&flinfo, 0, sizeof (flinfo));
12236 flinfo.info = info;
12237 flinfo.output_bfd = abfd;
12238 flinfo.symstrtab = _bfd_elf_strtab_init ();
12239 if (flinfo.symstrtab == NULL)
12240 return false;
12241
12242 if (! dynamic)
12243 {
12244 flinfo.hash_sec = NULL;
12245 flinfo.symver_sec = NULL;
12246 }
12247 else
12248 {
12249 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
12250 /* Note that dynsym_sec can be NULL (on VMS). */
12251 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
12252 /* Note that it is OK if symver_sec is NULL. */
12253 }
12254
12255 if (info->unique_symbol
12256 && !bfd_hash_table_init (&flinfo.local_hash_table,
12257 local_hash_newfunc,
12258 sizeof (struct local_hash_entry)))
12259 return false;
12260
12261 /* The object attributes have been merged. Remove the input
12262 sections from the link, and set the contents of the output
12263 section. */
12264 sections_removed = false;
12265 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
12266 for (o = abfd->sections; o != NULL; o = o->next)
12267 {
12268 bool remove_section = false;
12269
12270 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
12271 || strcmp (o->name, ".gnu.attributes") == 0)
12272 {
12273 for (p = o->map_head.link_order; p != NULL; p = p->next)
12274 {
12275 asection *input_section;
12276
12277 if (p->type != bfd_indirect_link_order)
12278 continue;
12279 input_section = p->u.indirect.section;
12280 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12281 elf_link_input_bfd ignores this section. */
12282 input_section->flags &= ~SEC_HAS_CONTENTS;
12283 }
12284
12285 attr_size = bfd_elf_obj_attr_size (abfd);
12286 bfd_set_section_size (o, attr_size);
12287 /* Skip this section later on. */
12288 o->map_head.link_order = NULL;
12289 if (attr_size)
12290 attr_section = o;
12291 else
12292 remove_section = true;
12293 }
12294 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
12295 {
12296 /* Remove empty group section from linker output. */
12297 remove_section = true;
12298 }
12299 if (remove_section)
12300 {
12301 o->flags |= SEC_EXCLUDE;
12302 bfd_section_list_remove (abfd, o);
12303 abfd->section_count--;
12304 sections_removed = true;
12305 }
12306 }
12307 if (sections_removed)
12308 _bfd_fix_excluded_sec_syms (abfd, info);
12309
12310 /* Count up the number of relocations we will output for each output
12311 section, so that we know the sizes of the reloc sections. We
12312 also figure out some maximum sizes. */
12313 max_contents_size = 0;
12314 max_external_reloc_size = 0;
12315 max_internal_reloc_count = 0;
12316 max_sym_count = 0;
12317 max_sym_shndx_count = 0;
12318 merged = false;
12319 for (o = abfd->sections; o != NULL; o = o->next)
12320 {
12321 struct bfd_elf_section_data *esdo = elf_section_data (o);
12322 o->reloc_count = 0;
12323
12324 for (p = o->map_head.link_order; p != NULL; p = p->next)
12325 {
12326 unsigned int reloc_count = 0;
12327 unsigned int additional_reloc_count = 0;
12328 struct bfd_elf_section_data *esdi = NULL;
12329
12330 if (p->type == bfd_section_reloc_link_order
12331 || p->type == bfd_symbol_reloc_link_order)
12332 reloc_count = 1;
12333 else if (p->type == bfd_indirect_link_order)
12334 {
12335 asection *sec;
12336
12337 sec = p->u.indirect.section;
12338
12339 /* Mark all sections which are to be included in the
12340 link. This will normally be every section. We need
12341 to do this so that we can identify any sections which
12342 the linker has decided to not include. */
12343 sec->linker_mark = true;
12344
12345 if (sec->flags & SEC_MERGE)
12346 merged = true;
12347
12348 if (sec->rawsize > max_contents_size)
12349 max_contents_size = sec->rawsize;
12350 if (sec->size > max_contents_size)
12351 max_contents_size = sec->size;
12352
12353 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
12354 && (sec->owner->flags & DYNAMIC) == 0)
12355 {
12356 size_t sym_count;
12357
12358 /* We are interested in just local symbols, not all
12359 symbols. */
12360 if (elf_bad_symtab (sec->owner))
12361 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
12362 / bed->s->sizeof_sym);
12363 else
12364 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
12365
12366 if (sym_count > max_sym_count)
12367 max_sym_count = sym_count;
12368
12369 if (sym_count > max_sym_shndx_count
12370 && elf_symtab_shndx_list (sec->owner) != NULL)
12371 max_sym_shndx_count = sym_count;
12372
12373 if (esdo->this_hdr.sh_type == SHT_REL
12374 || esdo->this_hdr.sh_type == SHT_RELA)
12375 /* Some backends use reloc_count in relocation sections
12376 to count particular types of relocs. Of course,
12377 reloc sections themselves can't have relocations. */
12378 ;
12379 else if (emit_relocs)
12380 {
12381 reloc_count = sec->reloc_count;
12382 if (bed->elf_backend_count_additional_relocs)
12383 {
12384 int c;
12385 c = (*bed->elf_backend_count_additional_relocs) (sec);
12386 additional_reloc_count += c;
12387 }
12388 }
12389 else if (bed->elf_backend_count_relocs)
12390 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
12391
12392 esdi = elf_section_data (sec);
12393
12394 if ((sec->flags & SEC_RELOC) != 0)
12395 {
12396 size_t ext_size = 0;
12397
12398 if (esdi->rel.hdr != NULL)
12399 ext_size = esdi->rel.hdr->sh_size;
12400 if (esdi->rela.hdr != NULL)
12401 ext_size += esdi->rela.hdr->sh_size;
12402
12403 if (ext_size > max_external_reloc_size)
12404 max_external_reloc_size = ext_size;
12405 if (sec->reloc_count > max_internal_reloc_count)
12406 max_internal_reloc_count = sec->reloc_count;
12407 }
12408 }
12409 }
12410
12411 if (reloc_count == 0)
12412 continue;
12413
12414 reloc_count += additional_reloc_count;
12415 o->reloc_count += reloc_count;
12416
12417 if (p->type == bfd_indirect_link_order && emit_relocs)
12418 {
12419 if (esdi->rel.hdr)
12420 {
12421 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
12422 esdo->rel.count += additional_reloc_count;
12423 }
12424 if (esdi->rela.hdr)
12425 {
12426 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
12427 esdo->rela.count += additional_reloc_count;
12428 }
12429 }
12430 else
12431 {
12432 if (o->use_rela_p)
12433 esdo->rela.count += reloc_count;
12434 else
12435 esdo->rel.count += reloc_count;
12436 }
12437 }
12438
12439 if (o->reloc_count > 0)
12440 o->flags |= SEC_RELOC;
12441 else
12442 {
12443 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12444 set it (this is probably a bug) and if it is set
12445 assign_section_numbers will create a reloc section. */
12446 o->flags &=~ SEC_RELOC;
12447 }
12448
12449 /* If the SEC_ALLOC flag is not set, force the section VMA to
12450 zero. This is done in elf_fake_sections as well, but forcing
12451 the VMA to 0 here will ensure that relocs against these
12452 sections are handled correctly. */
12453 if ((o->flags & SEC_ALLOC) == 0
12454 && ! o->user_set_vma)
12455 o->vma = 0;
12456 }
12457
12458 if (! bfd_link_relocatable (info) && merged)
12459 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
12460
12461 /* Figure out the file positions for everything but the symbol table
12462 and the relocs. We set symcount to force assign_section_numbers
12463 to create a symbol table. */
12464 abfd->symcount = info->strip != strip_all || emit_relocs;
12465 BFD_ASSERT (! abfd->output_has_begun);
12466 if (! _bfd_elf_compute_section_file_positions (abfd, info))
12467 goto error_return;
12468
12469 /* Set sizes, and assign file positions for reloc sections. */
12470 for (o = abfd->sections; o != NULL; o = o->next)
12471 {
12472 struct bfd_elf_section_data *esdo = elf_section_data (o);
12473 if ((o->flags & SEC_RELOC) != 0)
12474 {
12475 if (esdo->rel.hdr
12476 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
12477 goto error_return;
12478
12479 if (esdo->rela.hdr
12480 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
12481 goto error_return;
12482 }
12483
12484 /* _bfd_elf_compute_section_file_positions makes temporary use
12485 of target_index. Reset it. */
12486 o->target_index = 0;
12487
12488 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12489 to count upwards while actually outputting the relocations. */
12490 esdo->rel.count = 0;
12491 esdo->rela.count = 0;
12492
12493 if ((esdo->this_hdr.sh_offset == (file_ptr) -1)
12494 && !bfd_section_is_ctf (o))
12495 {
12496 /* Cache the section contents so that they can be compressed
12497 later. Use bfd_malloc since it will be freed by
12498 bfd_compress_section_contents. */
12499 unsigned char *contents = esdo->this_hdr.contents;
12500 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
12501 abort ();
12502 contents
12503 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
12504 if (contents == NULL)
12505 goto error_return;
12506 esdo->this_hdr.contents = contents;
12507 }
12508 }
12509
12510 /* We have now assigned file positions for all the sections except .symtab,
12511 .strtab, and non-loaded reloc and compressed debugging sections. We start
12512 the .symtab section at the current file position, and write directly to it.
12513 We build the .strtab section in memory. */
12514 abfd->symcount = 0;
12515 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12516 /* sh_name is set in prep_headers. */
12517 symtab_hdr->sh_type = SHT_SYMTAB;
12518 /* sh_flags, sh_addr and sh_size all start off zero. */
12519 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
12520 /* sh_link is set in assign_section_numbers. */
12521 /* sh_info is set below. */
12522 /* sh_offset is set just below. */
12523 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
12524
12525 if (max_sym_count < 20)
12526 max_sym_count = 20;
12527 htab->strtabsize = max_sym_count;
12528 amt = max_sym_count * sizeof (struct elf_sym_strtab);
12529 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
12530 if (htab->strtab == NULL)
12531 goto error_return;
12532 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12533 flinfo.symshndxbuf
12534 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
12535 ? (Elf_External_Sym_Shndx *) -1 : NULL);
12536
12537 if (info->strip != strip_all || emit_relocs)
12538 {
12539 file_ptr off = elf_next_file_pos (abfd);
12540
12541 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, true);
12542
12543 /* Note that at this point elf_next_file_pos (abfd) is
12544 incorrect. We do not yet know the size of the .symtab section.
12545 We correct next_file_pos below, after we do know the size. */
12546
12547 /* Start writing out the symbol table. The first symbol is always a
12548 dummy symbol. */
12549 elfsym.st_value = 0;
12550 elfsym.st_size = 0;
12551 elfsym.st_info = 0;
12552 elfsym.st_other = 0;
12553 elfsym.st_shndx = SHN_UNDEF;
12554 elfsym.st_target_internal = 0;
12555 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
12556 bfd_und_section_ptr, NULL) != 1)
12557 goto error_return;
12558
12559 /* Output a symbol for each section if asked or they are used for
12560 relocs. These symbols usually have no names. We store the
12561 index of each one in the index field of the section, so that
12562 we can find it again when outputting relocs. */
12563
12564 if (bfd_keep_unused_section_symbols (abfd) || emit_relocs)
12565 {
12566 bool name_local_sections
12567 = (bed->elf_backend_name_local_section_symbols
12568 && bed->elf_backend_name_local_section_symbols (abfd));
12569 const char *name = NULL;
12570
12571 elfsym.st_size = 0;
12572 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12573 elfsym.st_other = 0;
12574 elfsym.st_value = 0;
12575 elfsym.st_target_internal = 0;
12576 for (i = 1; i < elf_numsections (abfd); i++)
12577 {
12578 o = bfd_section_from_elf_index (abfd, i);
12579 if (o != NULL)
12580 {
12581 o->target_index = bfd_get_symcount (abfd);
12582 elfsym.st_shndx = i;
12583 if (!bfd_link_relocatable (info))
12584 elfsym.st_value = o->vma;
12585 if (name_local_sections)
12586 name = o->name;
12587 if (elf_link_output_symstrtab (&flinfo, name, &elfsym, o,
12588 NULL) != 1)
12589 goto error_return;
12590 }
12591 }
12592 }
12593 }
12594
12595 /* On some targets like Irix 5 the symbol split between local and global
12596 ones recorded in the sh_info field needs to be done between section
12597 and all other symbols. */
12598 if (bed->elf_backend_elfsym_local_is_section
12599 && bed->elf_backend_elfsym_local_is_section (abfd))
12600 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12601
12602 /* Allocate some memory to hold information read in from the input
12603 files. */
12604 if (max_contents_size != 0)
12605 {
12606 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12607 if (flinfo.contents == NULL)
12608 goto error_return;
12609 }
12610
12611 if (max_external_reloc_size != 0)
12612 {
12613 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
12614 if (flinfo.external_relocs == NULL)
12615 goto error_return;
12616 }
12617
12618 if (max_internal_reloc_count != 0)
12619 {
12620 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
12621 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
12622 if (flinfo.internal_relocs == NULL)
12623 goto error_return;
12624 }
12625
12626 if (max_sym_count != 0)
12627 {
12628 amt = max_sym_count * bed->s->sizeof_sym;
12629 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
12630 if (flinfo.external_syms == NULL)
12631 goto error_return;
12632
12633 amt = max_sym_count * sizeof (Elf_Internal_Sym);
12634 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
12635 if (flinfo.internal_syms == NULL)
12636 goto error_return;
12637
12638 amt = max_sym_count * sizeof (long);
12639 flinfo.indices = (long int *) bfd_malloc (amt);
12640 if (flinfo.indices == NULL)
12641 goto error_return;
12642
12643 amt = max_sym_count * sizeof (asection *);
12644 flinfo.sections = (asection **) bfd_malloc (amt);
12645 if (flinfo.sections == NULL)
12646 goto error_return;
12647 }
12648
12649 if (max_sym_shndx_count != 0)
12650 {
12651 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
12652 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
12653 if (flinfo.locsym_shndx == NULL)
12654 goto error_return;
12655 }
12656
12657 if (htab->tls_sec)
12658 {
12659 bfd_vma base, end = 0; /* Both bytes. */
12660 asection *sec;
12661
12662 for (sec = htab->tls_sec;
12663 sec && (sec->flags & SEC_THREAD_LOCAL);
12664 sec = sec->next)
12665 {
12666 bfd_size_type size = sec->size;
12667 unsigned int opb = bfd_octets_per_byte (abfd, sec);
12668
12669 if (size == 0
12670 && (sec->flags & SEC_HAS_CONTENTS) == 0)
12671 {
12672 struct bfd_link_order *ord = sec->map_tail.link_order;
12673
12674 if (ord != NULL)
12675 size = ord->offset * opb + ord->size;
12676 }
12677 end = sec->vma + size / opb;
12678 }
12679 base = htab->tls_sec->vma;
12680 /* Only align end of TLS section if static TLS doesn't have special
12681 alignment requirements. */
12682 if (bed->static_tls_alignment == 1)
12683 end = align_power (end, htab->tls_sec->alignment_power);
12684 htab->tls_size = end - base;
12685 }
12686
12687 if (!_bfd_elf_fixup_eh_frame_hdr (info))
12688 return false;
12689
12690 /* Finish relative relocations here after regular symbol processing
12691 is finished if DT_RELR is enabled. */
12692 if (info->enable_dt_relr
12693 && bed->finish_relative_relocs
12694 && !bed->finish_relative_relocs (info))
12695 info->callbacks->einfo
12696 (_("%F%P: %pB: failed to finish relative relocations\n"), abfd);
12697
12698 /* Since ELF permits relocations to be against local symbols, we
12699 must have the local symbols available when we do the relocations.
12700 Since we would rather only read the local symbols once, and we
12701 would rather not keep them in memory, we handle all the
12702 relocations for a single input file at the same time.
12703
12704 Unfortunately, there is no way to know the total number of local
12705 symbols until we have seen all of them, and the local symbol
12706 indices precede the global symbol indices. This means that when
12707 we are generating relocatable output, and we see a reloc against
12708 a global symbol, we can not know the symbol index until we have
12709 finished examining all the local symbols to see which ones we are
12710 going to output. To deal with this, we keep the relocations in
12711 memory, and don't output them until the end of the link. This is
12712 an unfortunate waste of memory, but I don't see a good way around
12713 it. Fortunately, it only happens when performing a relocatable
12714 link, which is not the common case. FIXME: If keep_memory is set
12715 we could write the relocs out and then read them again; I don't
12716 know how bad the memory loss will be. */
12717
12718 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12719 sub->output_has_begun = false;
12720 for (o = abfd->sections; o != NULL; o = o->next)
12721 {
12722 for (p = o->map_head.link_order; p != NULL; p = p->next)
12723 {
12724 if (p->type == bfd_indirect_link_order
12725 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12726 == bfd_target_elf_flavour)
12727 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12728 {
12729 if (! sub->output_has_begun)
12730 {
12731 if (! elf_link_input_bfd (&flinfo, sub))
12732 goto error_return;
12733 sub->output_has_begun = true;
12734 }
12735 }
12736 else if (p->type == bfd_section_reloc_link_order
12737 || p->type == bfd_symbol_reloc_link_order)
12738 {
12739 if (! elf_reloc_link_order (abfd, info, o, p))
12740 goto error_return;
12741 }
12742 else
12743 {
12744 if (! _bfd_default_link_order (abfd, info, o, p))
12745 {
12746 if (p->type == bfd_indirect_link_order
12747 && (bfd_get_flavour (sub)
12748 == bfd_target_elf_flavour)
12749 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12750 != bed->s->elfclass))
12751 {
12752 const char *iclass, *oclass;
12753
12754 switch (bed->s->elfclass)
12755 {
12756 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12757 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12758 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12759 default: abort ();
12760 }
12761
12762 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
12763 {
12764 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12765 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12766 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12767 default: abort ();
12768 }
12769
12770 bfd_set_error (bfd_error_wrong_format);
12771 _bfd_error_handler
12772 /* xgettext:c-format */
12773 (_("%pB: file class %s incompatible with %s"),
12774 sub, iclass, oclass);
12775 }
12776
12777 goto error_return;
12778 }
12779 }
12780 }
12781 }
12782
12783 /* Free symbol buffer if needed. */
12784 if (!info->reduce_memory_overheads)
12785 {
12786 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12787 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
12788 {
12789 free (elf_tdata (sub)->symbuf);
12790 elf_tdata (sub)->symbuf = NULL;
12791 }
12792 }
12793
12794 ret = true;
12795
12796 /* Output any global symbols that got converted to local in a
12797 version script or due to symbol visibility. We do this in a
12798 separate step since ELF requires all local symbols to appear
12799 prior to any global symbols. FIXME: We should only do this if
12800 some global symbols were, in fact, converted to become local.
12801 FIXME: Will this work correctly with the Irix 5 linker? */
12802 eoinfo.failed = false;
12803 eoinfo.flinfo = &flinfo;
12804 eoinfo.localsyms = true;
12805 eoinfo.file_sym_done = false;
12806 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12807 if (eoinfo.failed)
12808 {
12809 ret = false;
12810 goto return_local_hash_table;
12811 }
12812
12813 /* If backend needs to output some local symbols not present in the hash
12814 table, do it now. */
12815 if (bed->elf_backend_output_arch_local_syms
12816 && (info->strip != strip_all || emit_relocs))
12817 {
12818 if (! ((*bed->elf_backend_output_arch_local_syms)
12819 (abfd, info, &flinfo, elf_link_output_symstrtab)))
12820 {
12821 ret = false;
12822 goto return_local_hash_table;
12823 }
12824 }
12825
12826 /* That wrote out all the local symbols. Finish up the symbol table
12827 with the global symbols. Even if we want to strip everything we
12828 can, we still need to deal with those global symbols that got
12829 converted to local in a version script. */
12830
12831 /* The sh_info field records the index of the first non local symbol. */
12832 if (!symtab_hdr->sh_info)
12833 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12834
12835 if (dynamic
12836 && htab->dynsym != NULL
12837 && htab->dynsym->output_section != bfd_abs_section_ptr)
12838 {
12839 Elf_Internal_Sym sym;
12840 bfd_byte *dynsym = htab->dynsym->contents;
12841
12842 o = htab->dynsym->output_section;
12843 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
12844
12845 /* Write out the section symbols for the output sections. */
12846 if (bfd_link_pic (info)
12847 || htab->is_relocatable_executable)
12848 {
12849 asection *s;
12850
12851 sym.st_size = 0;
12852 sym.st_name = 0;
12853 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12854 sym.st_other = 0;
12855 sym.st_target_internal = 0;
12856
12857 for (s = abfd->sections; s != NULL; s = s->next)
12858 {
12859 int indx;
12860 bfd_byte *dest;
12861 long dynindx;
12862
12863 dynindx = elf_section_data (s)->dynindx;
12864 if (dynindx <= 0)
12865 continue;
12866 indx = elf_section_data (s)->this_idx;
12867 BFD_ASSERT (indx > 0);
12868 sym.st_shndx = indx;
12869 if (! check_dynsym (abfd, &sym))
12870 {
12871 ret = false;
12872 goto return_local_hash_table;
12873 }
12874 sym.st_value = s->vma;
12875 dest = dynsym + dynindx * bed->s->sizeof_sym;
12876
12877 /* Inform the linker of the addition of this symbol. */
12878
12879 if (info->callbacks->ctf_new_dynsym)
12880 info->callbacks->ctf_new_dynsym (dynindx, &sym);
12881
12882 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12883 }
12884 }
12885
12886 /* Write out the local dynsyms. */
12887 if (htab->dynlocal)
12888 {
12889 struct elf_link_local_dynamic_entry *e;
12890 for (e = htab->dynlocal; e ; e = e->next)
12891 {
12892 asection *s;
12893 bfd_byte *dest;
12894
12895 /* Copy the internal symbol and turn off visibility.
12896 Note that we saved a word of storage and overwrote
12897 the original st_name with the dynstr_index. */
12898 sym = e->isym;
12899 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
12900 sym.st_shndx = SHN_UNDEF;
12901
12902 s = bfd_section_from_elf_index (e->input_bfd,
12903 e->isym.st_shndx);
12904 if (s != NULL
12905 && s->output_section != NULL
12906 && elf_section_data (s->output_section) != NULL)
12907 {
12908 sym.st_shndx =
12909 elf_section_data (s->output_section)->this_idx;
12910 if (! check_dynsym (abfd, &sym))
12911 {
12912 ret = false;
12913 goto return_local_hash_table;
12914 }
12915 sym.st_value = (s->output_section->vma
12916 + s->output_offset
12917 + e->isym.st_value);
12918 }
12919
12920 /* Inform the linker of the addition of this symbol. */
12921
12922 if (info->callbacks->ctf_new_dynsym)
12923 info->callbacks->ctf_new_dynsym (e->dynindx, &sym);
12924
12925 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12926 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12927 }
12928 }
12929 }
12930
12931 /* We get the global symbols from the hash table. */
12932 eoinfo.failed = false;
12933 eoinfo.localsyms = false;
12934 eoinfo.flinfo = &flinfo;
12935 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12936 if (eoinfo.failed)
12937 {
12938 ret = false;
12939 goto return_local_hash_table;
12940 }
12941
12942 /* If backend needs to output some symbols not present in the hash
12943 table, do it now. */
12944 if (bed->elf_backend_output_arch_syms
12945 && (info->strip != strip_all || emit_relocs))
12946 {
12947 if (! ((*bed->elf_backend_output_arch_syms)
12948 (abfd, info, &flinfo, elf_link_output_symstrtab)))
12949 {
12950 ret = false;
12951 goto return_local_hash_table;
12952 }
12953 }
12954
12955 /* Finalize the .strtab section. */
12956 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12957
12958 /* Swap out the .strtab section. */
12959 if (!elf_link_swap_symbols_out (&flinfo))
12960 {
12961 ret = false;
12962 goto return_local_hash_table;
12963 }
12964
12965 /* Now we know the size of the symtab section. */
12966 if (bfd_get_symcount (abfd) > 0)
12967 {
12968 /* Finish up and write out the symbol string table (.strtab)
12969 section. */
12970 Elf_Internal_Shdr *symstrtab_hdr = NULL;
12971 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12972
12973 if (elf_symtab_shndx_list (abfd))
12974 {
12975 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
12976
12977 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12978 {
12979 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12980 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12981 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12982 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12983 symtab_shndx_hdr->sh_size = amt;
12984
12985 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12986 off, true);
12987
12988 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12989 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12990 {
12991 ret = false;
12992 goto return_local_hash_table;
12993 }
12994 }
12995 }
12996
12997 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12998 /* sh_name was set in prep_headers. */
12999 symstrtab_hdr->sh_type = SHT_STRTAB;
13000 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
13001 symstrtab_hdr->sh_addr = 0;
13002 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
13003 symstrtab_hdr->sh_entsize = 0;
13004 symstrtab_hdr->sh_link = 0;
13005 symstrtab_hdr->sh_info = 0;
13006 /* sh_offset is set just below. */
13007 symstrtab_hdr->sh_addralign = 1;
13008
13009 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
13010 off, true);
13011 elf_next_file_pos (abfd) = off;
13012
13013 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
13014 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
13015 {
13016 ret = false;
13017 goto return_local_hash_table;
13018 }
13019 }
13020
13021 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
13022 {
13023 _bfd_error_handler (_("%pB: failed to generate import library"),
13024 info->out_implib_bfd);
13025 ret = false;
13026 goto return_local_hash_table;
13027 }
13028
13029 /* Adjust the relocs to have the correct symbol indices. */
13030 for (o = abfd->sections; o != NULL; o = o->next)
13031 {
13032 struct bfd_elf_section_data *esdo = elf_section_data (o);
13033 bool sort;
13034
13035 if ((o->flags & SEC_RELOC) == 0)
13036 continue;
13037
13038 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
13039 if (esdo->rel.hdr != NULL
13040 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
13041 {
13042 ret = false;
13043 goto return_local_hash_table;
13044 }
13045 if (esdo->rela.hdr != NULL
13046 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
13047 {
13048 ret = false;
13049 goto return_local_hash_table;
13050 }
13051
13052 /* Set the reloc_count field to 0 to prevent write_relocs from
13053 trying to swap the relocs out itself. */
13054 o->reloc_count = 0;
13055 }
13056
13057 relativecount = 0;
13058 if (dynamic && info->combreloc && dynobj != NULL)
13059 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
13060
13061 relr_entsize = 0;
13062 if (htab->srelrdyn != NULL
13063 && htab->srelrdyn->output_section != NULL
13064 && htab->srelrdyn->size != 0)
13065 {
13066 asection *s = htab->srelrdyn->output_section;
13067 relr_entsize = elf_section_data (s)->this_hdr.sh_entsize;
13068 if (relr_entsize == 0)
13069 {
13070 relr_entsize = bed->s->arch_size / 8;
13071 elf_section_data (s)->this_hdr.sh_entsize = relr_entsize;
13072 }
13073 }
13074
13075 /* If we are linking against a dynamic object, or generating a
13076 shared library, finish up the dynamic linking information. */
13077 if (dynamic)
13078 {
13079 bfd_byte *dyncon, *dynconend;
13080
13081 /* Fix up .dynamic entries. */
13082 o = bfd_get_linker_section (dynobj, ".dynamic");
13083 BFD_ASSERT (o != NULL);
13084
13085 dyncon = o->contents;
13086 dynconend = PTR_ADD (o->contents, o->size);
13087 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13088 {
13089 Elf_Internal_Dyn dyn;
13090 const char *name;
13091 unsigned int type;
13092 bfd_size_type sh_size;
13093 bfd_vma sh_addr;
13094
13095 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13096
13097 switch (dyn.d_tag)
13098 {
13099 default:
13100 continue;
13101 case DT_NULL:
13102 if (relativecount != 0)
13103 {
13104 switch (elf_section_data (reldyn)->this_hdr.sh_type)
13105 {
13106 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
13107 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
13108 }
13109 if (dyn.d_tag != DT_NULL
13110 && dynconend - dyncon >= bed->s->sizeof_dyn)
13111 {
13112 dyn.d_un.d_val = relativecount;
13113 relativecount = 0;
13114 break;
13115 }
13116 relativecount = 0;
13117 }
13118 if (relr_entsize != 0)
13119 {
13120 if (dynconend - dyncon >= 3 * bed->s->sizeof_dyn)
13121 {
13122 asection *s = htab->srelrdyn;
13123 dyn.d_tag = DT_RELR;
13124 dyn.d_un.d_ptr
13125 = s->output_section->vma + s->output_offset;
13126 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13127 dyncon += bed->s->sizeof_dyn;
13128
13129 dyn.d_tag = DT_RELRSZ;
13130 dyn.d_un.d_val = s->size;
13131 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13132 dyncon += bed->s->sizeof_dyn;
13133
13134 dyn.d_tag = DT_RELRENT;
13135 dyn.d_un.d_val = relr_entsize;
13136 relr_entsize = 0;
13137 break;
13138 }
13139 relr_entsize = 0;
13140 }
13141 continue;
13142
13143 case DT_INIT:
13144 name = info->init_function;
13145 goto get_sym;
13146 case DT_FINI:
13147 name = info->fini_function;
13148 get_sym:
13149 {
13150 struct elf_link_hash_entry *h;
13151
13152 h = elf_link_hash_lookup (htab, name, false, false, true);
13153 if (h != NULL
13154 && (h->root.type == bfd_link_hash_defined
13155 || h->root.type == bfd_link_hash_defweak))
13156 {
13157 dyn.d_un.d_ptr = h->root.u.def.value;
13158 o = h->root.u.def.section;
13159 if (o->output_section != NULL)
13160 dyn.d_un.d_ptr += (o->output_section->vma
13161 + o->output_offset);
13162 else
13163 {
13164 /* The symbol is imported from another shared
13165 library and does not apply to this one. */
13166 dyn.d_un.d_ptr = 0;
13167 }
13168 break;
13169 }
13170 }
13171 continue;
13172
13173 case DT_PREINIT_ARRAYSZ:
13174 name = ".preinit_array";
13175 goto get_out_size;
13176 case DT_INIT_ARRAYSZ:
13177 name = ".init_array";
13178 goto get_out_size;
13179 case DT_FINI_ARRAYSZ:
13180 name = ".fini_array";
13181 get_out_size:
13182 o = bfd_get_section_by_name (abfd, name);
13183 if (o == NULL)
13184 {
13185 _bfd_error_handler
13186 (_("could not find section %s"), name);
13187 goto error_return;
13188 }
13189 if (o->size == 0)
13190 _bfd_error_handler
13191 (_("warning: %s section has zero size"), name);
13192 dyn.d_un.d_val = o->size;
13193 break;
13194
13195 case DT_PREINIT_ARRAY:
13196 name = ".preinit_array";
13197 goto get_out_vma;
13198 case DT_INIT_ARRAY:
13199 name = ".init_array";
13200 goto get_out_vma;
13201 case DT_FINI_ARRAY:
13202 name = ".fini_array";
13203 get_out_vma:
13204 o = bfd_get_section_by_name (abfd, name);
13205 goto do_vma;
13206
13207 case DT_HASH:
13208 name = ".hash";
13209 goto get_vma;
13210 case DT_GNU_HASH:
13211 name = ".gnu.hash";
13212 goto get_vma;
13213 case DT_STRTAB:
13214 name = ".dynstr";
13215 goto get_vma;
13216 case DT_SYMTAB:
13217 name = ".dynsym";
13218 goto get_vma;
13219 case DT_VERDEF:
13220 name = ".gnu.version_d";
13221 goto get_vma;
13222 case DT_VERNEED:
13223 name = ".gnu.version_r";
13224 goto get_vma;
13225 case DT_VERSYM:
13226 name = ".gnu.version";
13227 get_vma:
13228 o = bfd_get_linker_section (dynobj, name);
13229 do_vma:
13230 if (o == NULL || bfd_is_abs_section (o->output_section))
13231 {
13232 _bfd_error_handler
13233 (_("could not find section %s"), name);
13234 goto error_return;
13235 }
13236 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
13237 {
13238 _bfd_error_handler
13239 (_("warning: section '%s' is being made into a note"), name);
13240 bfd_set_error (bfd_error_nonrepresentable_section);
13241 goto error_return;
13242 }
13243 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
13244 break;
13245
13246 case DT_REL:
13247 case DT_RELA:
13248 case DT_RELSZ:
13249 case DT_RELASZ:
13250 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
13251 type = SHT_REL;
13252 else
13253 type = SHT_RELA;
13254 sh_size = 0;
13255 sh_addr = 0;
13256 for (i = 1; i < elf_numsections (abfd); i++)
13257 {
13258 Elf_Internal_Shdr *hdr;
13259
13260 hdr = elf_elfsections (abfd)[i];
13261 if (hdr->sh_type == type
13262 && (hdr->sh_flags & SHF_ALLOC) != 0)
13263 {
13264 sh_size += hdr->sh_size;
13265 if (sh_addr == 0
13266 || sh_addr > hdr->sh_addr)
13267 sh_addr = hdr->sh_addr;
13268 }
13269 }
13270
13271 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
13272 {
13273 unsigned int opb = bfd_octets_per_byte (abfd, o);
13274
13275 /* Don't count procedure linkage table relocs in the
13276 overall reloc count. */
13277 sh_size -= htab->srelplt->size;
13278 if (sh_size == 0)
13279 /* If the size is zero, make the address zero too.
13280 This is to avoid a glibc bug. If the backend
13281 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
13282 zero, then we'll put DT_RELA at the end of
13283 DT_JMPREL. glibc will interpret the end of
13284 DT_RELA matching the end of DT_JMPREL as the
13285 case where DT_RELA includes DT_JMPREL, and for
13286 LD_BIND_NOW will decide that processing DT_RELA
13287 will process the PLT relocs too. Net result:
13288 No PLT relocs applied. */
13289 sh_addr = 0;
13290
13291 /* If .rela.plt is the first .rela section, exclude
13292 it from DT_RELA. */
13293 else if (sh_addr == (htab->srelplt->output_section->vma
13294 + htab->srelplt->output_offset) * opb)
13295 sh_addr += htab->srelplt->size;
13296 }
13297
13298 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
13299 dyn.d_un.d_val = sh_size;
13300 else
13301 dyn.d_un.d_ptr = sh_addr;
13302 break;
13303 }
13304 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13305 }
13306 }
13307
13308 /* If we have created any dynamic sections, then output them. */
13309 if (dynobj != NULL)
13310 {
13311 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
13312 goto error_return;
13313
13314 /* Check for DT_TEXTREL (late, in case the backend removes it). */
13315 if (bfd_link_textrel_check (info)
13316 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL
13317 && o->size != 0)
13318 {
13319 bfd_byte *dyncon, *dynconend;
13320
13321 dyncon = o->contents;
13322 dynconend = o->contents + o->size;
13323 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13324 {
13325 Elf_Internal_Dyn dyn;
13326
13327 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13328
13329 if (dyn.d_tag == DT_TEXTREL)
13330 {
13331 if (info->textrel_check == textrel_check_error)
13332 info->callbacks->einfo
13333 (_("%P%X: read-only segment has dynamic relocations\n"));
13334 else if (bfd_link_dll (info))
13335 info->callbacks->einfo
13336 (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
13337 else if (bfd_link_pde (info))
13338 info->callbacks->einfo
13339 (_("%P: warning: creating DT_TEXTREL in a PDE\n"));
13340 else
13341 info->callbacks->einfo
13342 (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
13343 break;
13344 }
13345 }
13346 }
13347
13348 for (o = dynobj->sections; o != NULL; o = o->next)
13349 {
13350 if ((o->flags & SEC_HAS_CONTENTS) == 0
13351 || o->size == 0
13352 || o->output_section == bfd_abs_section_ptr)
13353 continue;
13354 if ((o->flags & SEC_LINKER_CREATED) == 0)
13355 {
13356 /* At this point, we are only interested in sections
13357 created by _bfd_elf_link_create_dynamic_sections. */
13358 continue;
13359 }
13360 if (htab->stab_info.stabstr == o)
13361 continue;
13362 if (htab->eh_info.hdr_sec == o)
13363 continue;
13364 if (strcmp (o->name, ".dynstr") != 0)
13365 {
13366 bfd_size_type octets = ((file_ptr) o->output_offset
13367 * bfd_octets_per_byte (abfd, o));
13368 if (!bfd_set_section_contents (abfd, o->output_section,
13369 o->contents, octets, o->size))
13370 goto error_return;
13371 }
13372 else
13373 {
13374 /* The contents of the .dynstr section are actually in a
13375 stringtab. */
13376 file_ptr off;
13377
13378 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
13379 if (bfd_seek (abfd, off, SEEK_SET) != 0
13380 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
13381 goto error_return;
13382 }
13383 }
13384 }
13385
13386 if (!info->resolve_section_groups)
13387 {
13388 bool failed = false;
13389
13390 BFD_ASSERT (bfd_link_relocatable (info));
13391 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
13392 if (failed)
13393 goto error_return;
13394 }
13395
13396 /* If we have optimized stabs strings, output them. */
13397 if (htab->stab_info.stabstr != NULL)
13398 {
13399 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
13400 goto error_return;
13401 }
13402
13403 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
13404 goto error_return;
13405
13406 if (info->callbacks->emit_ctf)
13407 info->callbacks->emit_ctf ();
13408
13409 elf_final_link_free (abfd, &flinfo);
13410
13411 if (attr_section)
13412 {
13413 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
13414 if (contents == NULL)
13415 {
13416 /* Bail out and fail. */
13417 ret = false;
13418 goto return_local_hash_table;
13419 }
13420 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
13421 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
13422 free (contents);
13423 }
13424
13425 return_local_hash_table:
13426 if (info->unique_symbol)
13427 bfd_hash_table_free (&flinfo.local_hash_table);
13428 return ret;
13429
13430 error_return:
13431 elf_final_link_free (abfd, &flinfo);
13432 ret = false;
13433 goto return_local_hash_table;
13434 }
13435 \f
13436 /* Initialize COOKIE for input bfd ABFD. */
13437
13438 static bool
13439 init_reloc_cookie (struct elf_reloc_cookie *cookie,
13440 struct bfd_link_info *info, bfd *abfd)
13441 {
13442 Elf_Internal_Shdr *symtab_hdr;
13443 const struct elf_backend_data *bed;
13444
13445 bed = get_elf_backend_data (abfd);
13446 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13447
13448 cookie->abfd = abfd;
13449 cookie->sym_hashes = elf_sym_hashes (abfd);
13450 cookie->bad_symtab = elf_bad_symtab (abfd);
13451 if (cookie->bad_symtab)
13452 {
13453 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13454 cookie->extsymoff = 0;
13455 }
13456 else
13457 {
13458 cookie->locsymcount = symtab_hdr->sh_info;
13459 cookie->extsymoff = symtab_hdr->sh_info;
13460 }
13461
13462 if (bed->s->arch_size == 32)
13463 cookie->r_sym_shift = 8;
13464 else
13465 cookie->r_sym_shift = 32;
13466
13467 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
13468 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
13469 {
13470 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
13471 cookie->locsymcount, 0,
13472 NULL, NULL, NULL);
13473 if (cookie->locsyms == NULL)
13474 {
13475 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
13476 return false;
13477 }
13478 if (_bfd_link_keep_memory (info) )
13479 {
13480 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
13481 info->cache_size += (cookie->locsymcount
13482 * sizeof (Elf_External_Sym_Shndx));
13483 }
13484 }
13485 return true;
13486 }
13487
13488 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
13489
13490 static void
13491 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
13492 {
13493 Elf_Internal_Shdr *symtab_hdr;
13494
13495 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13496 if (symtab_hdr->contents != (unsigned char *) cookie->locsyms)
13497 free (cookie->locsyms);
13498 }
13499
13500 /* Initialize the relocation information in COOKIE for input section SEC
13501 of input bfd ABFD. */
13502
13503 static bool
13504 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13505 struct bfd_link_info *info, bfd *abfd,
13506 asection *sec)
13507 {
13508 if (sec->reloc_count == 0)
13509 {
13510 cookie->rels = NULL;
13511 cookie->relend = NULL;
13512 }
13513 else
13514 {
13515 cookie->rels = _bfd_elf_link_info_read_relocs (abfd, info, sec,
13516 NULL, NULL,
13517 _bfd_link_keep_memory (info));
13518 if (cookie->rels == NULL)
13519 return false;
13520 cookie->rel = cookie->rels;
13521 cookie->relend = cookie->rels + sec->reloc_count;
13522 }
13523 cookie->rel = cookie->rels;
13524 return true;
13525 }
13526
13527 /* Free the memory allocated by init_reloc_cookie_rels,
13528 if appropriate. */
13529
13530 static void
13531 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13532 asection *sec)
13533 {
13534 if (elf_section_data (sec)->relocs != cookie->rels)
13535 free (cookie->rels);
13536 }
13537
13538 /* Initialize the whole of COOKIE for input section SEC. */
13539
13540 static bool
13541 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13542 struct bfd_link_info *info,
13543 asection *sec)
13544 {
13545 if (!init_reloc_cookie (cookie, info, sec->owner))
13546 goto error1;
13547 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
13548 goto error2;
13549 return true;
13550
13551 error2:
13552 fini_reloc_cookie (cookie, sec->owner);
13553 error1:
13554 return false;
13555 }
13556
13557 /* Free the memory allocated by init_reloc_cookie_for_section,
13558 if appropriate. */
13559
13560 static void
13561 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13562 asection *sec)
13563 {
13564 fini_reloc_cookie_rels (cookie, sec);
13565 fini_reloc_cookie (cookie, sec->owner);
13566 }
13567 \f
13568 /* Garbage collect unused sections. */
13569
13570 /* Default gc_mark_hook. */
13571
13572 asection *
13573 _bfd_elf_gc_mark_hook (asection *sec,
13574 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13575 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13576 struct elf_link_hash_entry *h,
13577 Elf_Internal_Sym *sym)
13578 {
13579 if (h != NULL)
13580 {
13581 switch (h->root.type)
13582 {
13583 case bfd_link_hash_defined:
13584 case bfd_link_hash_defweak:
13585 return h->root.u.def.section;
13586
13587 case bfd_link_hash_common:
13588 return h->root.u.c.p->section;
13589
13590 default:
13591 break;
13592 }
13593 }
13594 else
13595 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
13596
13597 return NULL;
13598 }
13599
13600 /* Return the debug definition section. */
13601
13602 static asection *
13603 elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
13604 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13605 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13606 struct elf_link_hash_entry *h,
13607 Elf_Internal_Sym *sym)
13608 {
13609 if (h != NULL)
13610 {
13611 /* Return the global debug definition section. */
13612 if ((h->root.type == bfd_link_hash_defined
13613 || h->root.type == bfd_link_hash_defweak)
13614 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
13615 return h->root.u.def.section;
13616 }
13617 else
13618 {
13619 /* Return the local debug definition section. */
13620 asection *isec = bfd_section_from_elf_index (sec->owner,
13621 sym->st_shndx);
13622 if ((isec->flags & SEC_DEBUGGING) != 0)
13623 return isec;
13624 }
13625
13626 return NULL;
13627 }
13628
13629 /* COOKIE->rel describes a relocation against section SEC, which is
13630 a section we've decided to keep. Return the section that contains
13631 the relocation symbol, or NULL if no section contains it. */
13632
13633 asection *
13634 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
13635 elf_gc_mark_hook_fn gc_mark_hook,
13636 struct elf_reloc_cookie *cookie,
13637 bool *start_stop)
13638 {
13639 unsigned long r_symndx;
13640 struct elf_link_hash_entry *h, *hw;
13641
13642 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
13643 if (r_symndx == STN_UNDEF)
13644 return NULL;
13645
13646 if (r_symndx >= cookie->locsymcount
13647 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13648 {
13649 bool was_marked;
13650
13651 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
13652 if (h == NULL)
13653 {
13654 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
13655 sec->owner);
13656 return NULL;
13657 }
13658 while (h->root.type == bfd_link_hash_indirect
13659 || h->root.type == bfd_link_hash_warning)
13660 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13661
13662 was_marked = h->mark;
13663 h->mark = 1;
13664 /* Keep all aliases of the symbol too. If an object symbol
13665 needs to be copied into .dynbss then all of its aliases
13666 should be present as dynamic symbols, not just the one used
13667 on the copy relocation. */
13668 hw = h;
13669 while (hw->is_weakalias)
13670 {
13671 hw = hw->u.alias;
13672 hw->mark = 1;
13673 }
13674
13675 if (!was_marked && h->start_stop && !h->root.ldscript_def)
13676 {
13677 if (info->start_stop_gc)
13678 return NULL;
13679
13680 /* To work around a glibc bug, mark XXX input sections
13681 when there is a reference to __start_XXX or __stop_XXX
13682 symbols. */
13683 else if (start_stop != NULL)
13684 {
13685 asection *s = h->u2.start_stop_section;
13686 *start_stop = true;
13687 return s;
13688 }
13689 }
13690
13691 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
13692 }
13693
13694 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
13695 &cookie->locsyms[r_symndx]);
13696 }
13697
13698 /* COOKIE->rel describes a relocation against section SEC, which is
13699 a section we've decided to keep. Mark the section that contains
13700 the relocation symbol. */
13701
13702 bool
13703 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
13704 asection *sec,
13705 elf_gc_mark_hook_fn gc_mark_hook,
13706 struct elf_reloc_cookie *cookie)
13707 {
13708 asection *rsec;
13709 bool start_stop = false;
13710
13711 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
13712 while (rsec != NULL)
13713 {
13714 if (!rsec->gc_mark)
13715 {
13716 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
13717 || (rsec->owner->flags & DYNAMIC) != 0)
13718 rsec->gc_mark = 1;
13719 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
13720 return false;
13721 }
13722 if (!start_stop)
13723 break;
13724 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
13725 }
13726 return true;
13727 }
13728
13729 /* The mark phase of garbage collection. For a given section, mark
13730 it and any sections in this section's group, and all the sections
13731 which define symbols to which it refers. */
13732
13733 bool
13734 _bfd_elf_gc_mark (struct bfd_link_info *info,
13735 asection *sec,
13736 elf_gc_mark_hook_fn gc_mark_hook)
13737 {
13738 bool ret;
13739 asection *group_sec, *eh_frame;
13740
13741 sec->gc_mark = 1;
13742
13743 /* Mark all the sections in the group. */
13744 group_sec = elf_section_data (sec)->next_in_group;
13745 if (group_sec && !group_sec->gc_mark)
13746 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
13747 return false;
13748
13749 /* Look through the section relocs. */
13750 ret = true;
13751 eh_frame = elf_eh_frame_section (sec->owner);
13752 if ((sec->flags & SEC_RELOC) != 0
13753 && sec->reloc_count > 0
13754 && sec != eh_frame)
13755 {
13756 struct elf_reloc_cookie cookie;
13757
13758 if (!init_reloc_cookie_for_section (&cookie, info, sec))
13759 ret = false;
13760 else
13761 {
13762 for (; cookie.rel < cookie.relend; cookie.rel++)
13763 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
13764 {
13765 ret = false;
13766 break;
13767 }
13768 fini_reloc_cookie_for_section (&cookie, sec);
13769 }
13770 }
13771
13772 if (ret && eh_frame && elf_fde_list (sec))
13773 {
13774 struct elf_reloc_cookie cookie;
13775
13776 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
13777 ret = false;
13778 else
13779 {
13780 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
13781 gc_mark_hook, &cookie))
13782 ret = false;
13783 fini_reloc_cookie_for_section (&cookie, eh_frame);
13784 }
13785 }
13786
13787 eh_frame = elf_section_eh_frame_entry (sec);
13788 if (ret && eh_frame && !eh_frame->gc_mark)
13789 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
13790 ret = false;
13791
13792 return ret;
13793 }
13794
13795 /* Scan and mark sections in a special or debug section group. */
13796
13797 static void
13798 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
13799 {
13800 /* Point to first section of section group. */
13801 asection *ssec;
13802 /* Used to iterate the section group. */
13803 asection *msec;
13804
13805 bool is_special_grp = true;
13806 bool is_debug_grp = true;
13807
13808 /* First scan to see if group contains any section other than debug
13809 and special section. */
13810 ssec = msec = elf_next_in_group (grp);
13811 do
13812 {
13813 if ((msec->flags & SEC_DEBUGGING) == 0)
13814 is_debug_grp = false;
13815
13816 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
13817 is_special_grp = false;
13818
13819 msec = elf_next_in_group (msec);
13820 }
13821 while (msec != ssec);
13822
13823 /* If this is a pure debug section group or pure special section group,
13824 keep all sections in this group. */
13825 if (is_debug_grp || is_special_grp)
13826 {
13827 do
13828 {
13829 msec->gc_mark = 1;
13830 msec = elf_next_in_group (msec);
13831 }
13832 while (msec != ssec);
13833 }
13834 }
13835
13836 /* Keep debug and special sections. */
13837
13838 bool
13839 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
13840 elf_gc_mark_hook_fn mark_hook)
13841 {
13842 bfd *ibfd;
13843
13844 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13845 {
13846 asection *isec;
13847 bool some_kept;
13848 bool debug_frag_seen;
13849 bool has_kept_debug_info;
13850
13851 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13852 continue;
13853 isec = ibfd->sections;
13854 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13855 continue;
13856
13857 /* Ensure all linker created sections are kept,
13858 see if any other section is already marked,
13859 and note if we have any fragmented debug sections. */
13860 debug_frag_seen = some_kept = has_kept_debug_info = false;
13861 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13862 {
13863 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13864 isec->gc_mark = 1;
13865 else if (isec->gc_mark
13866 && (isec->flags & SEC_ALLOC) != 0
13867 && elf_section_type (isec) != SHT_NOTE)
13868 some_kept = true;
13869 else
13870 {
13871 /* Since all sections, except for backend specific ones,
13872 have been garbage collected, call mark_hook on this
13873 section if any of its linked-to sections is marked. */
13874 asection *linked_to_sec;
13875 for (linked_to_sec = elf_linked_to_section (isec);
13876 linked_to_sec != NULL && !linked_to_sec->linker_mark;
13877 linked_to_sec = elf_linked_to_section (linked_to_sec))
13878 {
13879 if (linked_to_sec->gc_mark)
13880 {
13881 if (!_bfd_elf_gc_mark (info, isec, mark_hook))
13882 return false;
13883 break;
13884 }
13885 linked_to_sec->linker_mark = 1;
13886 }
13887 for (linked_to_sec = elf_linked_to_section (isec);
13888 linked_to_sec != NULL && linked_to_sec->linker_mark;
13889 linked_to_sec = elf_linked_to_section (linked_to_sec))
13890 linked_to_sec->linker_mark = 0;
13891 }
13892
13893 if (!debug_frag_seen
13894 && (isec->flags & SEC_DEBUGGING)
13895 && startswith (isec->name, ".debug_line."))
13896 debug_frag_seen = true;
13897 else if (strcmp (bfd_section_name (isec),
13898 "__patchable_function_entries") == 0
13899 && elf_linked_to_section (isec) == NULL)
13900 info->callbacks->einfo (_("%F%P: %pB(%pA): error: "
13901 "need linked-to section "
13902 "for --gc-sections\n"),
13903 isec->owner, isec);
13904 }
13905
13906 /* If no non-note alloc section in this file will be kept, then
13907 we can toss out the debug and special sections. */
13908 if (!some_kept)
13909 continue;
13910
13911 /* Keep debug and special sections like .comment when they are
13912 not part of a group. Also keep section groups that contain
13913 just debug sections or special sections. NB: Sections with
13914 linked-to section has been handled above. */
13915 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13916 {
13917 if ((isec->flags & SEC_GROUP) != 0)
13918 _bfd_elf_gc_mark_debug_special_section_group (isec);
13919 else if (((isec->flags & SEC_DEBUGGING) != 0
13920 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
13921 && elf_next_in_group (isec) == NULL
13922 && elf_linked_to_section (isec) == NULL)
13923 isec->gc_mark = 1;
13924 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
13925 has_kept_debug_info = true;
13926 }
13927
13928 /* Look for CODE sections which are going to be discarded,
13929 and find and discard any fragmented debug sections which
13930 are associated with that code section. */
13931 if (debug_frag_seen)
13932 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13933 if ((isec->flags & SEC_CODE) != 0
13934 && isec->gc_mark == 0)
13935 {
13936 unsigned int ilen;
13937 asection *dsec;
13938
13939 ilen = strlen (isec->name);
13940
13941 /* Association is determined by the name of the debug
13942 section containing the name of the code section as
13943 a suffix. For example .debug_line.text.foo is a
13944 debug section associated with .text.foo. */
13945 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
13946 {
13947 unsigned int dlen;
13948
13949 if (dsec->gc_mark == 0
13950 || (dsec->flags & SEC_DEBUGGING) == 0)
13951 continue;
13952
13953 dlen = strlen (dsec->name);
13954
13955 if (dlen > ilen
13956 && strncmp (dsec->name + (dlen - ilen),
13957 isec->name, ilen) == 0)
13958 dsec->gc_mark = 0;
13959 }
13960 }
13961
13962 /* Mark debug sections referenced by kept debug sections. */
13963 if (has_kept_debug_info)
13964 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13965 if (isec->gc_mark
13966 && (isec->flags & SEC_DEBUGGING) != 0)
13967 if (!_bfd_elf_gc_mark (info, isec,
13968 elf_gc_mark_debug_section))
13969 return false;
13970 }
13971 return true;
13972 }
13973
13974 static bool
13975 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
13976 {
13977 bfd *sub;
13978 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13979
13980 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13981 {
13982 asection *o;
13983
13984 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13985 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
13986 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13987 continue;
13988 o = sub->sections;
13989 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13990 continue;
13991
13992 for (o = sub->sections; o != NULL; o = o->next)
13993 {
13994 /* When any section in a section group is kept, we keep all
13995 sections in the section group. If the first member of
13996 the section group is excluded, we will also exclude the
13997 group section. */
13998 if (o->flags & SEC_GROUP)
13999 {
14000 asection *first = elf_next_in_group (o);
14001 o->gc_mark = first->gc_mark;
14002 }
14003
14004 if (o->gc_mark)
14005 continue;
14006
14007 /* Skip sweeping sections already excluded. */
14008 if (o->flags & SEC_EXCLUDE)
14009 continue;
14010
14011 /* Since this is early in the link process, it is simple
14012 to remove a section from the output. */
14013 o->flags |= SEC_EXCLUDE;
14014
14015 if (info->print_gc_sections && o->size != 0)
14016 /* xgettext:c-format */
14017 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
14018 o, sub);
14019 }
14020 }
14021
14022 return true;
14023 }
14024
14025 /* Propagate collected vtable information. This is called through
14026 elf_link_hash_traverse. */
14027
14028 static bool
14029 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
14030 {
14031 /* Those that are not vtables. */
14032 if (h->start_stop
14033 || h->u2.vtable == NULL
14034 || h->u2.vtable->parent == NULL)
14035 return true;
14036
14037 /* Those vtables that do not have parents, we cannot merge. */
14038 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
14039 return true;
14040
14041 /* If we've already been done, exit. */
14042 if (h->u2.vtable->used && h->u2.vtable->used[-1])
14043 return true;
14044
14045 /* Make sure the parent's table is up to date. */
14046 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
14047
14048 if (h->u2.vtable->used == NULL)
14049 {
14050 /* None of this table's entries were referenced. Re-use the
14051 parent's table. */
14052 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
14053 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
14054 }
14055 else
14056 {
14057 size_t n;
14058 bool *cu, *pu;
14059
14060 /* Or the parent's entries into ours. */
14061 cu = h->u2.vtable->used;
14062 cu[-1] = true;
14063 pu = h->u2.vtable->parent->u2.vtable->used;
14064 if (pu != NULL)
14065 {
14066 const struct elf_backend_data *bed;
14067 unsigned int log_file_align;
14068
14069 bed = get_elf_backend_data (h->root.u.def.section->owner);
14070 log_file_align = bed->s->log_file_align;
14071 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
14072 while (n--)
14073 {
14074 if (*pu)
14075 *cu = true;
14076 pu++;
14077 cu++;
14078 }
14079 }
14080 }
14081
14082 return true;
14083 }
14084
14085 struct link_info_ok
14086 {
14087 struct bfd_link_info *info;
14088 bool ok;
14089 };
14090
14091 static bool
14092 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h,
14093 void *ptr)
14094 {
14095 asection *sec;
14096 bfd_vma hstart, hend;
14097 Elf_Internal_Rela *relstart, *relend, *rel;
14098 const struct elf_backend_data *bed;
14099 unsigned int log_file_align;
14100 struct link_info_ok *info = (struct link_info_ok *) ptr;
14101
14102 /* Take care of both those symbols that do not describe vtables as
14103 well as those that are not loaded. */
14104 if (h->start_stop
14105 || h->u2.vtable == NULL
14106 || h->u2.vtable->parent == NULL)
14107 return true;
14108
14109 BFD_ASSERT (h->root.type == bfd_link_hash_defined
14110 || h->root.type == bfd_link_hash_defweak);
14111
14112 sec = h->root.u.def.section;
14113 hstart = h->root.u.def.value;
14114 hend = hstart + h->size;
14115
14116 relstart = _bfd_elf_link_info_read_relocs (sec->owner, info->info,
14117 sec, NULL, NULL, true);
14118 if (!relstart)
14119 return info->ok = false;
14120 bed = get_elf_backend_data (sec->owner);
14121 log_file_align = bed->s->log_file_align;
14122
14123 relend = relstart + sec->reloc_count;
14124
14125 for (rel = relstart; rel < relend; ++rel)
14126 if (rel->r_offset >= hstart && rel->r_offset < hend)
14127 {
14128 /* If the entry is in use, do nothing. */
14129 if (h->u2.vtable->used
14130 && (rel->r_offset - hstart) < h->u2.vtable->size)
14131 {
14132 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
14133 if (h->u2.vtable->used[entry])
14134 continue;
14135 }
14136 /* Otherwise, kill it. */
14137 rel->r_offset = rel->r_info = rel->r_addend = 0;
14138 }
14139
14140 return true;
14141 }
14142
14143 /* Mark sections containing dynamically referenced symbols. When
14144 building shared libraries, we must assume that any visible symbol is
14145 referenced. */
14146
14147 bool
14148 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
14149 {
14150 struct bfd_link_info *info = (struct bfd_link_info *) inf;
14151 struct bfd_elf_dynamic_list *d = info->dynamic_list;
14152
14153 if ((h->root.type == bfd_link_hash_defined
14154 || h->root.type == bfd_link_hash_defweak)
14155 && (!h->start_stop
14156 || h->root.ldscript_def
14157 || !info->start_stop_gc)
14158 && ((h->ref_dynamic && !h->forced_local)
14159 || ((h->def_regular || ELF_COMMON_DEF_P (h))
14160 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
14161 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
14162 && (!bfd_link_executable (info)
14163 || info->gc_keep_exported
14164 || info->export_dynamic
14165 || (h->dynamic
14166 && d != NULL
14167 && (*d->match) (&d->head, NULL, h->root.root.string)))
14168 && (h->versioned >= versioned
14169 || !bfd_hide_sym_by_version (info->version_info,
14170 h->root.root.string)))))
14171 h->root.u.def.section->flags |= SEC_KEEP;
14172
14173 return true;
14174 }
14175
14176 /* Keep all sections containing symbols undefined on the command-line,
14177 and the section containing the entry symbol. */
14178
14179 void
14180 _bfd_elf_gc_keep (struct bfd_link_info *info)
14181 {
14182 struct bfd_sym_chain *sym;
14183
14184 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
14185 {
14186 struct elf_link_hash_entry *h;
14187
14188 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
14189 false, false, false);
14190
14191 if (h != NULL
14192 && (h->root.type == bfd_link_hash_defined
14193 || h->root.type == bfd_link_hash_defweak)
14194 && !bfd_is_const_section (h->root.u.def.section))
14195 h->root.u.def.section->flags |= SEC_KEEP;
14196 }
14197 }
14198
14199 bool
14200 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
14201 struct bfd_link_info *info)
14202 {
14203 bfd *ibfd = info->input_bfds;
14204
14205 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14206 {
14207 asection *sec;
14208 struct elf_reloc_cookie cookie;
14209
14210 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
14211 continue;
14212 sec = ibfd->sections;
14213 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14214 continue;
14215
14216 if (!init_reloc_cookie (&cookie, info, ibfd))
14217 return false;
14218
14219 for (sec = ibfd->sections; sec; sec = sec->next)
14220 {
14221 if (startswith (bfd_section_name (sec), ".eh_frame_entry")
14222 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
14223 {
14224 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
14225 fini_reloc_cookie_rels (&cookie, sec);
14226 }
14227 }
14228 }
14229 return true;
14230 }
14231
14232 /* Do mark and sweep of unused sections. */
14233
14234 bool
14235 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
14236 {
14237 bool ok = true;
14238 bfd *sub;
14239 elf_gc_mark_hook_fn gc_mark_hook;
14240 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14241 struct elf_link_hash_table *htab;
14242 struct link_info_ok info_ok;
14243
14244 if (!bed->can_gc_sections
14245 || !is_elf_hash_table (info->hash))
14246 {
14247 _bfd_error_handler(_("warning: gc-sections option ignored"));
14248 return true;
14249 }
14250
14251 bed->gc_keep (info);
14252 htab = elf_hash_table (info);
14253
14254 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
14255 at the .eh_frame section if we can mark the FDEs individually. */
14256 for (sub = info->input_bfds;
14257 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
14258 sub = sub->link.next)
14259 {
14260 asection *sec;
14261 struct elf_reloc_cookie cookie;
14262
14263 sec = sub->sections;
14264 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14265 continue;
14266 sec = bfd_get_section_by_name (sub, ".eh_frame");
14267 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
14268 {
14269 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
14270 if (elf_section_data (sec)->sec_info
14271 && (sec->flags & SEC_LINKER_CREATED) == 0)
14272 elf_eh_frame_section (sub) = sec;
14273 fini_reloc_cookie_for_section (&cookie, sec);
14274 sec = bfd_get_next_section_by_name (NULL, sec);
14275 }
14276 }
14277
14278 /* Apply transitive closure to the vtable entry usage info. */
14279 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
14280 if (!ok)
14281 return false;
14282
14283 /* Kill the vtable relocations that were not used. */
14284 info_ok.info = info;
14285 info_ok.ok = true;
14286 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &info_ok);
14287 if (!info_ok.ok)
14288 return false;
14289
14290 /* Mark dynamically referenced symbols. */
14291 if (htab->dynamic_sections_created || info->gc_keep_exported)
14292 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
14293
14294 /* Grovel through relocs to find out who stays ... */
14295 gc_mark_hook = bed->gc_mark_hook;
14296 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
14297 {
14298 asection *o;
14299
14300 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
14301 || elf_object_id (sub) != elf_hash_table_id (htab)
14302 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
14303 continue;
14304
14305 o = sub->sections;
14306 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14307 continue;
14308
14309 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
14310 Also treat note sections as a root, if the section is not part
14311 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
14312 well as FINI_ARRAY sections for ld -r. */
14313 for (o = sub->sections; o != NULL; o = o->next)
14314 if (!o->gc_mark
14315 && (o->flags & SEC_EXCLUDE) == 0
14316 && ((o->flags & SEC_KEEP) != 0
14317 || (bfd_link_relocatable (info)
14318 && ((elf_section_data (o)->this_hdr.sh_type
14319 == SHT_PREINIT_ARRAY)
14320 || (elf_section_data (o)->this_hdr.sh_type
14321 == SHT_INIT_ARRAY)
14322 || (elf_section_data (o)->this_hdr.sh_type
14323 == SHT_FINI_ARRAY)))
14324 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
14325 && elf_next_in_group (o) == NULL
14326 && elf_linked_to_section (o) == NULL)
14327 || ((elf_tdata (sub)->has_gnu_osabi & elf_gnu_osabi_retain)
14328 && (elf_section_flags (o) & SHF_GNU_RETAIN))))
14329 {
14330 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
14331 return false;
14332 }
14333 }
14334
14335 /* Allow the backend to mark additional target specific sections. */
14336 bed->gc_mark_extra_sections (info, gc_mark_hook);
14337
14338 /* ... and mark SEC_EXCLUDE for those that go. */
14339 return elf_gc_sweep (abfd, info);
14340 }
14341 \f
14342 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
14343
14344 bool
14345 bfd_elf_gc_record_vtinherit (bfd *abfd,
14346 asection *sec,
14347 struct elf_link_hash_entry *h,
14348 bfd_vma offset)
14349 {
14350 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
14351 struct elf_link_hash_entry **search, *child;
14352 size_t extsymcount;
14353 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14354
14355 /* The sh_info field of the symtab header tells us where the
14356 external symbols start. We don't care about the local symbols at
14357 this point. */
14358 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
14359 if (!elf_bad_symtab (abfd))
14360 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
14361
14362 sym_hashes = elf_sym_hashes (abfd);
14363 sym_hashes_end = PTR_ADD (sym_hashes, extsymcount);
14364
14365 /* Hunt down the child symbol, which is in this section at the same
14366 offset as the relocation. */
14367 for (search = sym_hashes; search != sym_hashes_end; ++search)
14368 {
14369 if ((child = *search) != NULL
14370 && (child->root.type == bfd_link_hash_defined
14371 || child->root.type == bfd_link_hash_defweak)
14372 && child->root.u.def.section == sec
14373 && child->root.u.def.value == offset)
14374 goto win;
14375 }
14376
14377 /* xgettext:c-format */
14378 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
14379 abfd, sec, (uint64_t) offset);
14380 bfd_set_error (bfd_error_invalid_operation);
14381 return false;
14382
14383 win:
14384 if (!child->u2.vtable)
14385 {
14386 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
14387 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
14388 if (!child->u2.vtable)
14389 return false;
14390 }
14391 if (!h)
14392 {
14393 /* This *should* only be the absolute section. It could potentially
14394 be that someone has defined a non-global vtable though, which
14395 would be bad. It isn't worth paging in the local symbols to be
14396 sure though; that case should simply be handled by the assembler. */
14397
14398 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
14399 }
14400 else
14401 child->u2.vtable->parent = h;
14402
14403 return true;
14404 }
14405
14406 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
14407
14408 bool
14409 bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec,
14410 struct elf_link_hash_entry *h,
14411 bfd_vma addend)
14412 {
14413 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14414 unsigned int log_file_align = bed->s->log_file_align;
14415
14416 if (!h)
14417 {
14418 /* xgettext:c-format */
14419 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
14420 abfd, sec);
14421 bfd_set_error (bfd_error_bad_value);
14422 return false;
14423 }
14424
14425 if (!h->u2.vtable)
14426 {
14427 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
14428 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
14429 if (!h->u2.vtable)
14430 return false;
14431 }
14432
14433 if (addend >= h->u2.vtable->size)
14434 {
14435 size_t size, bytes, file_align;
14436 bool *ptr = h->u2.vtable->used;
14437
14438 /* While the symbol is undefined, we have to be prepared to handle
14439 a zero size. */
14440 file_align = 1 << log_file_align;
14441 if (h->root.type == bfd_link_hash_undefined)
14442 size = addend + file_align;
14443 else
14444 {
14445 size = h->size;
14446 if (addend >= size)
14447 {
14448 /* Oops! We've got a reference past the defined end of
14449 the table. This is probably a bug -- shall we warn? */
14450 size = addend + file_align;
14451 }
14452 }
14453 size = (size + file_align - 1) & -file_align;
14454
14455 /* Allocate one extra entry for use as a "done" flag for the
14456 consolidation pass. */
14457 bytes = ((size >> log_file_align) + 1) * sizeof (bool);
14458
14459 if (ptr)
14460 {
14461 ptr = (bool *) bfd_realloc (ptr - 1, bytes);
14462
14463 if (ptr != NULL)
14464 {
14465 size_t oldbytes;
14466
14467 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
14468 * sizeof (bool));
14469 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
14470 }
14471 }
14472 else
14473 ptr = (bool *) bfd_zmalloc (bytes);
14474
14475 if (ptr == NULL)
14476 return false;
14477
14478 /* And arrange for that done flag to be at index -1. */
14479 h->u2.vtable->used = ptr + 1;
14480 h->u2.vtable->size = size;
14481 }
14482
14483 h->u2.vtable->used[addend >> log_file_align] = true;
14484
14485 return true;
14486 }
14487
14488 /* Map an ELF section header flag to its corresponding string. */
14489 typedef struct
14490 {
14491 char *flag_name;
14492 flagword flag_value;
14493 } elf_flags_to_name_table;
14494
14495 static const elf_flags_to_name_table elf_flags_to_names [] =
14496 {
14497 { "SHF_WRITE", SHF_WRITE },
14498 { "SHF_ALLOC", SHF_ALLOC },
14499 { "SHF_EXECINSTR", SHF_EXECINSTR },
14500 { "SHF_MERGE", SHF_MERGE },
14501 { "SHF_STRINGS", SHF_STRINGS },
14502 { "SHF_INFO_LINK", SHF_INFO_LINK},
14503 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
14504 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
14505 { "SHF_GROUP", SHF_GROUP },
14506 { "SHF_TLS", SHF_TLS },
14507 { "SHF_MASKOS", SHF_MASKOS },
14508 { "SHF_EXCLUDE", SHF_EXCLUDE },
14509 };
14510
14511 /* Returns TRUE if the section is to be included, otherwise FALSE. */
14512 bool
14513 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
14514 struct flag_info *flaginfo,
14515 asection *section)
14516 {
14517 const bfd_vma sh_flags = elf_section_flags (section);
14518
14519 if (!flaginfo->flags_initialized)
14520 {
14521 bfd *obfd = info->output_bfd;
14522 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14523 struct flag_info_list *tf = flaginfo->flag_list;
14524 int with_hex = 0;
14525 int without_hex = 0;
14526
14527 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
14528 {
14529 unsigned i;
14530 flagword (*lookup) (char *);
14531
14532 lookup = bed->elf_backend_lookup_section_flags_hook;
14533 if (lookup != NULL)
14534 {
14535 flagword hexval = (*lookup) ((char *) tf->name);
14536
14537 if (hexval != 0)
14538 {
14539 if (tf->with == with_flags)
14540 with_hex |= hexval;
14541 else if (tf->with == without_flags)
14542 without_hex |= hexval;
14543 tf->valid = true;
14544 continue;
14545 }
14546 }
14547 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
14548 {
14549 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
14550 {
14551 if (tf->with == with_flags)
14552 with_hex |= elf_flags_to_names[i].flag_value;
14553 else if (tf->with == without_flags)
14554 without_hex |= elf_flags_to_names[i].flag_value;
14555 tf->valid = true;
14556 break;
14557 }
14558 }
14559 if (!tf->valid)
14560 {
14561 info->callbacks->einfo
14562 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
14563 return false;
14564 }
14565 }
14566 flaginfo->flags_initialized = true;
14567 flaginfo->only_with_flags |= with_hex;
14568 flaginfo->not_with_flags |= without_hex;
14569 }
14570
14571 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
14572 return false;
14573
14574 if ((flaginfo->not_with_flags & sh_flags) != 0)
14575 return false;
14576
14577 return true;
14578 }
14579
14580 struct alloc_got_off_arg {
14581 bfd_vma gotoff;
14582 struct bfd_link_info *info;
14583 };
14584
14585 /* We need a special top-level link routine to convert got reference counts
14586 to real got offsets. */
14587
14588 static bool
14589 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
14590 {
14591 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
14592 bfd *obfd = gofarg->info->output_bfd;
14593 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14594
14595 if (h->got.refcount > 0)
14596 {
14597 h->got.offset = gofarg->gotoff;
14598 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
14599 }
14600 else
14601 h->got.offset = (bfd_vma) -1;
14602
14603 return true;
14604 }
14605
14606 /* And an accompanying bit to work out final got entry offsets once
14607 we're done. Should be called from final_link. */
14608
14609 bool
14610 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
14611 struct bfd_link_info *info)
14612 {
14613 bfd *i;
14614 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14615 bfd_vma gotoff;
14616 struct alloc_got_off_arg gofarg;
14617
14618 BFD_ASSERT (abfd == info->output_bfd);
14619
14620 if (! is_elf_hash_table (info->hash))
14621 return false;
14622
14623 /* The GOT offset is relative to the .got section, but the GOT header is
14624 put into the .got.plt section, if the backend uses it. */
14625 if (bed->want_got_plt)
14626 gotoff = 0;
14627 else
14628 gotoff = bed->got_header_size;
14629
14630 /* Do the local .got entries first. */
14631 for (i = info->input_bfds; i; i = i->link.next)
14632 {
14633 bfd_signed_vma *local_got;
14634 size_t j, locsymcount;
14635 Elf_Internal_Shdr *symtab_hdr;
14636
14637 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
14638 continue;
14639
14640 local_got = elf_local_got_refcounts (i);
14641 if (!local_got)
14642 continue;
14643
14644 symtab_hdr = &elf_tdata (i)->symtab_hdr;
14645 if (elf_bad_symtab (i))
14646 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
14647 else
14648 locsymcount = symtab_hdr->sh_info;
14649
14650 for (j = 0; j < locsymcount; ++j)
14651 {
14652 if (local_got[j] > 0)
14653 {
14654 local_got[j] = gotoff;
14655 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
14656 }
14657 else
14658 local_got[j] = (bfd_vma) -1;
14659 }
14660 }
14661
14662 /* Then the global .got entries. .plt refcounts are handled by
14663 adjust_dynamic_symbol */
14664 gofarg.gotoff = gotoff;
14665 gofarg.info = info;
14666 elf_link_hash_traverse (elf_hash_table (info),
14667 elf_gc_allocate_got_offsets,
14668 &gofarg);
14669 return true;
14670 }
14671
14672 /* Many folk need no more in the way of final link than this, once
14673 got entry reference counting is enabled. */
14674
14675 bool
14676 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
14677 {
14678 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
14679 return false;
14680
14681 /* Invoke the regular ELF backend linker to do all the work. */
14682 return bfd_elf_final_link (abfd, info);
14683 }
14684
14685 bool
14686 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
14687 {
14688 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
14689
14690 if (rcookie->bad_symtab)
14691 rcookie->rel = rcookie->rels;
14692
14693 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
14694 {
14695 unsigned long r_symndx;
14696
14697 if (! rcookie->bad_symtab)
14698 if (rcookie->rel->r_offset > offset)
14699 return false;
14700 if (rcookie->rel->r_offset != offset)
14701 continue;
14702
14703 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
14704 if (r_symndx == STN_UNDEF)
14705 return true;
14706
14707 if (r_symndx >= rcookie->locsymcount
14708 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
14709 {
14710 struct elf_link_hash_entry *h;
14711
14712 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
14713
14714 while (h->root.type == bfd_link_hash_indirect
14715 || h->root.type == bfd_link_hash_warning)
14716 h = (struct elf_link_hash_entry *) h->root.u.i.link;
14717
14718 if ((h->root.type == bfd_link_hash_defined
14719 || h->root.type == bfd_link_hash_defweak)
14720 && (h->root.u.def.section->owner != rcookie->abfd
14721 || h->root.u.def.section->kept_section != NULL
14722 || discarded_section (h->root.u.def.section)))
14723 return true;
14724 }
14725 else
14726 {
14727 /* It's not a relocation against a global symbol,
14728 but it could be a relocation against a local
14729 symbol for a discarded section. */
14730 asection *isec;
14731 Elf_Internal_Sym *isym;
14732
14733 /* Need to: get the symbol; get the section. */
14734 isym = &rcookie->locsyms[r_symndx];
14735 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
14736 if (isec != NULL
14737 && (isec->kept_section != NULL
14738 || discarded_section (isec)))
14739 return true;
14740 }
14741 return false;
14742 }
14743 return false;
14744 }
14745
14746 /* Discard unneeded references to discarded sections.
14747 Returns -1 on error, 1 if any section's size was changed, 0 if
14748 nothing changed. This function assumes that the relocations are in
14749 sorted order, which is true for all known assemblers. */
14750
14751 int
14752 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
14753 {
14754 struct elf_reloc_cookie cookie;
14755 asection *o;
14756 bfd *abfd;
14757 int changed = 0;
14758
14759 if (info->traditional_format
14760 || !is_elf_hash_table (info->hash))
14761 return 0;
14762
14763 o = bfd_get_section_by_name (output_bfd, ".stab");
14764 if (o != NULL)
14765 {
14766 asection *i;
14767
14768 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14769 {
14770 if (i->size == 0
14771 || i->reloc_count == 0
14772 || i->sec_info_type != SEC_INFO_TYPE_STABS)
14773 continue;
14774
14775 abfd = i->owner;
14776 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14777 continue;
14778
14779 if (!init_reloc_cookie_for_section (&cookie, info, i))
14780 return -1;
14781
14782 if (_bfd_discard_section_stabs (abfd, i,
14783 elf_section_data (i)->sec_info,
14784 bfd_elf_reloc_symbol_deleted_p,
14785 &cookie))
14786 changed = 1;
14787
14788 fini_reloc_cookie_for_section (&cookie, i);
14789 }
14790 }
14791
14792 o = NULL;
14793 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
14794 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
14795 if (o != NULL)
14796 {
14797 asection *i;
14798 int eh_changed = 0;
14799 unsigned int eh_alignment; /* Octets. */
14800
14801 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14802 {
14803 if (i->size == 0)
14804 continue;
14805
14806 abfd = i->owner;
14807 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14808 continue;
14809
14810 if (!init_reloc_cookie_for_section (&cookie, info, i))
14811 return -1;
14812
14813 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
14814 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
14815 bfd_elf_reloc_symbol_deleted_p,
14816 &cookie))
14817 {
14818 eh_changed = 1;
14819 if (i->size != i->rawsize)
14820 changed = 1;
14821 }
14822
14823 fini_reloc_cookie_for_section (&cookie, i);
14824 }
14825
14826 eh_alignment = ((1 << o->alignment_power)
14827 * bfd_octets_per_byte (output_bfd, o));
14828 /* Skip over zero terminator, and prevent empty sections from
14829 adding alignment padding at the end. */
14830 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
14831 if (i->size == 0)
14832 i->flags |= SEC_EXCLUDE;
14833 else if (i->size > 4)
14834 break;
14835 /* The last non-empty eh_frame section doesn't need padding. */
14836 if (i != NULL)
14837 i = i->map_tail.s;
14838 /* Any prior sections must pad the last FDE out to the output
14839 section alignment. Otherwise we might have zero padding
14840 between sections, which would be seen as a terminator. */
14841 for (; i != NULL; i = i->map_tail.s)
14842 if (i->size == 4)
14843 /* All but the last zero terminator should have been removed. */
14844 BFD_FAIL ();
14845 else
14846 {
14847 bfd_size_type size
14848 = (i->size + eh_alignment - 1) & -eh_alignment;
14849 if (i->size != size)
14850 {
14851 i->size = size;
14852 changed = 1;
14853 eh_changed = 1;
14854 }
14855 }
14856 if (eh_changed)
14857 elf_link_hash_traverse (elf_hash_table (info),
14858 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
14859 }
14860
14861 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
14862 {
14863 const struct elf_backend_data *bed;
14864 asection *s;
14865
14866 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14867 continue;
14868 s = abfd->sections;
14869 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14870 continue;
14871
14872 bed = get_elf_backend_data (abfd);
14873
14874 if (bed->elf_backend_discard_info != NULL)
14875 {
14876 if (!init_reloc_cookie (&cookie, info, abfd))
14877 return -1;
14878
14879 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
14880 changed = 1;
14881
14882 fini_reloc_cookie (&cookie, abfd);
14883 }
14884 }
14885
14886 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
14887 _bfd_elf_end_eh_frame_parsing (info);
14888
14889 if (info->eh_frame_hdr_type
14890 && !bfd_link_relocatable (info)
14891 && _bfd_elf_discard_section_eh_frame_hdr (info))
14892 changed = 1;
14893
14894 return changed;
14895 }
14896
14897 bool
14898 _bfd_elf_section_already_linked (bfd *abfd,
14899 asection *sec,
14900 struct bfd_link_info *info)
14901 {
14902 flagword flags;
14903 const char *name, *key;
14904 struct bfd_section_already_linked *l;
14905 struct bfd_section_already_linked_hash_entry *already_linked_list;
14906
14907 if (sec->output_section == bfd_abs_section_ptr)
14908 return false;
14909
14910 flags = sec->flags;
14911
14912 /* Return if it isn't a linkonce section. A comdat group section
14913 also has SEC_LINK_ONCE set. */
14914 if ((flags & SEC_LINK_ONCE) == 0)
14915 return false;
14916
14917 /* Don't put group member sections on our list of already linked
14918 sections. They are handled as a group via their group section. */
14919 if (elf_sec_group (sec) != NULL)
14920 return false;
14921
14922 /* For a SHT_GROUP section, use the group signature as the key. */
14923 name = sec->name;
14924 if ((flags & SEC_GROUP) != 0
14925 && elf_next_in_group (sec) != NULL
14926 && elf_group_name (elf_next_in_group (sec)) != NULL)
14927 key = elf_group_name (elf_next_in_group (sec));
14928 else
14929 {
14930 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
14931 if (startswith (name, ".gnu.linkonce.")
14932 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
14933 key++;
14934 else
14935 /* Must be a user linkonce section that doesn't follow gcc's
14936 naming convention. In this case we won't be matching
14937 single member groups. */
14938 key = name;
14939 }
14940
14941 already_linked_list = bfd_section_already_linked_table_lookup (key);
14942
14943 for (l = already_linked_list->entry; l != NULL; l = l->next)
14944 {
14945 /* We may have 2 different types of sections on the list: group
14946 sections with a signature of <key> (<key> is some string),
14947 and linkonce sections named .gnu.linkonce.<type>.<key>.
14948 Match like sections. LTO plugin sections are an exception.
14949 They are always named .gnu.linkonce.t.<key> and match either
14950 type of section. */
14951 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
14952 && ((flags & SEC_GROUP) != 0
14953 || strcmp (name, l->sec->name) == 0))
14954 || (l->sec->owner->flags & BFD_PLUGIN) != 0
14955 || (sec->owner->flags & BFD_PLUGIN) != 0)
14956 {
14957 /* The section has already been linked. See if we should
14958 issue a warning. */
14959 if (!_bfd_handle_already_linked (sec, l, info))
14960 return false;
14961
14962 if (flags & SEC_GROUP)
14963 {
14964 asection *first = elf_next_in_group (sec);
14965 asection *s = first;
14966
14967 while (s != NULL)
14968 {
14969 s->output_section = bfd_abs_section_ptr;
14970 /* Record which group discards it. */
14971 s->kept_section = l->sec;
14972 s = elf_next_in_group (s);
14973 /* These lists are circular. */
14974 if (s == first)
14975 break;
14976 }
14977 }
14978
14979 return true;
14980 }
14981 }
14982
14983 /* A single member comdat group section may be discarded by a
14984 linkonce section and vice versa. */
14985 if ((flags & SEC_GROUP) != 0)
14986 {
14987 asection *first = elf_next_in_group (sec);
14988
14989 if (first != NULL && elf_next_in_group (first) == first)
14990 /* Check this single member group against linkonce sections. */
14991 for (l = already_linked_list->entry; l != NULL; l = l->next)
14992 if ((l->sec->flags & SEC_GROUP) == 0
14993 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14994 {
14995 first->output_section = bfd_abs_section_ptr;
14996 first->kept_section = l->sec;
14997 sec->output_section = bfd_abs_section_ptr;
14998 break;
14999 }
15000 }
15001 else
15002 /* Check this linkonce section against single member groups. */
15003 for (l = already_linked_list->entry; l != NULL; l = l->next)
15004 if (l->sec->flags & SEC_GROUP)
15005 {
15006 asection *first = elf_next_in_group (l->sec);
15007
15008 if (first != NULL
15009 && elf_next_in_group (first) == first
15010 && bfd_elf_match_symbols_in_sections (first, sec, info))
15011 {
15012 sec->output_section = bfd_abs_section_ptr;
15013 sec->kept_section = first;
15014 break;
15015 }
15016 }
15017
15018 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
15019 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
15020 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
15021 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
15022 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
15023 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
15024 `.gnu.linkonce.t.F' section from a different bfd not requiring any
15025 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
15026 The reverse order cannot happen as there is never a bfd with only the
15027 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
15028 matter as here were are looking only for cross-bfd sections. */
15029
15030 if ((flags & SEC_GROUP) == 0 && startswith (name, ".gnu.linkonce.r."))
15031 for (l = already_linked_list->entry; l != NULL; l = l->next)
15032 if ((l->sec->flags & SEC_GROUP) == 0
15033 && startswith (l->sec->name, ".gnu.linkonce.t."))
15034 {
15035 if (abfd != l->sec->owner)
15036 sec->output_section = bfd_abs_section_ptr;
15037 break;
15038 }
15039
15040 /* This is the first section with this name. Record it. */
15041 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
15042 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
15043 return sec->output_section == bfd_abs_section_ptr;
15044 }
15045
15046 bool
15047 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
15048 {
15049 return sym->st_shndx == SHN_COMMON;
15050 }
15051
15052 unsigned int
15053 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
15054 {
15055 return SHN_COMMON;
15056 }
15057
15058 asection *
15059 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
15060 {
15061 return bfd_com_section_ptr;
15062 }
15063
15064 bfd_vma
15065 _bfd_elf_default_got_elt_size (bfd *abfd,
15066 struct bfd_link_info *info ATTRIBUTE_UNUSED,
15067 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
15068 bfd *ibfd ATTRIBUTE_UNUSED,
15069 unsigned long symndx ATTRIBUTE_UNUSED)
15070 {
15071 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15072 return bed->s->arch_size / 8;
15073 }
15074
15075 /* Routines to support the creation of dynamic relocs. */
15076
15077 /* Returns the name of the dynamic reloc section associated with SEC. */
15078
15079 static const char *
15080 get_dynamic_reloc_section_name (bfd * abfd,
15081 asection * sec,
15082 bool is_rela)
15083 {
15084 char *name;
15085 const char *old_name = bfd_section_name (sec);
15086 const char *prefix = is_rela ? ".rela" : ".rel";
15087
15088 if (old_name == NULL)
15089 return NULL;
15090
15091 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
15092 sprintf (name, "%s%s", prefix, old_name);
15093
15094 return name;
15095 }
15096
15097 /* Returns the dynamic reloc section associated with SEC.
15098 If necessary compute the name of the dynamic reloc section based
15099 on SEC's name (looked up in ABFD's string table) and the setting
15100 of IS_RELA. */
15101
15102 asection *
15103 _bfd_elf_get_dynamic_reloc_section (bfd *abfd,
15104 asection *sec,
15105 bool is_rela)
15106 {
15107 asection *reloc_sec = elf_section_data (sec)->sreloc;
15108
15109 if (reloc_sec == NULL)
15110 {
15111 const char *name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
15112
15113 if (name != NULL)
15114 {
15115 reloc_sec = bfd_get_linker_section (abfd, name);
15116
15117 if (reloc_sec != NULL)
15118 elf_section_data (sec)->sreloc = reloc_sec;
15119 }
15120 }
15121
15122 return reloc_sec;
15123 }
15124
15125 /* Returns the dynamic reloc section associated with SEC. If the
15126 section does not exist it is created and attached to the DYNOBJ
15127 bfd and stored in the SRELOC field of SEC's elf_section_data
15128 structure.
15129
15130 ALIGNMENT is the alignment for the newly created section and
15131 IS_RELA defines whether the name should be .rela.<SEC's name>
15132 or .rel.<SEC's name>. The section name is looked up in the
15133 string table associated with ABFD. */
15134
15135 asection *
15136 _bfd_elf_make_dynamic_reloc_section (asection *sec,
15137 bfd *dynobj,
15138 unsigned int alignment,
15139 bfd *abfd,
15140 bool is_rela)
15141 {
15142 asection * reloc_sec = elf_section_data (sec)->sreloc;
15143
15144 if (reloc_sec == NULL)
15145 {
15146 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
15147
15148 if (name == NULL)
15149 return NULL;
15150
15151 reloc_sec = bfd_get_linker_section (dynobj, name);
15152
15153 if (reloc_sec == NULL)
15154 {
15155 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
15156 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
15157 if ((sec->flags & SEC_ALLOC) != 0)
15158 flags |= SEC_ALLOC | SEC_LOAD;
15159
15160 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
15161 if (reloc_sec != NULL)
15162 {
15163 /* _bfd_elf_get_sec_type_attr chooses a section type by
15164 name. Override as it may be wrong, eg. for a user
15165 section named "auto" we'll get ".relauto" which is
15166 seen to be a .rela section. */
15167 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
15168 if (!bfd_set_section_alignment (reloc_sec, alignment))
15169 reloc_sec = NULL;
15170 }
15171 }
15172
15173 elf_section_data (sec)->sreloc = reloc_sec;
15174 }
15175
15176 return reloc_sec;
15177 }
15178
15179 /* Copy the ELF symbol type and other attributes for a linker script
15180 assignment from HSRC to HDEST. Generally this should be treated as
15181 if we found a strong non-dynamic definition for HDEST (except that
15182 ld ignores multiple definition errors). */
15183 void
15184 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
15185 struct bfd_link_hash_entry *hdest,
15186 struct bfd_link_hash_entry *hsrc)
15187 {
15188 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
15189 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
15190 Elf_Internal_Sym isym;
15191
15192 ehdest->type = ehsrc->type;
15193 ehdest->target_internal = ehsrc->target_internal;
15194
15195 isym.st_other = ehsrc->other;
15196 elf_merge_st_other (abfd, ehdest, isym.st_other, NULL, true, false);
15197 }
15198
15199 /* Append a RELA relocation REL to section S in BFD. */
15200
15201 void
15202 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15203 {
15204 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15205 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
15206 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
15207 bed->s->swap_reloca_out (abfd, rel, loc);
15208 }
15209
15210 /* Append a REL relocation REL to section S in BFD. */
15211
15212 void
15213 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15214 {
15215 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15216 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
15217 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
15218 bed->s->swap_reloc_out (abfd, rel, loc);
15219 }
15220
15221 /* Define __start, __stop, .startof. or .sizeof. symbol. */
15222
15223 struct bfd_link_hash_entry *
15224 bfd_elf_define_start_stop (struct bfd_link_info *info,
15225 const char *symbol, asection *sec)
15226 {
15227 struct elf_link_hash_entry *h;
15228
15229 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
15230 false, false, true);
15231 /* NB: Common symbols will be turned into definition later. */
15232 if (h != NULL
15233 && !h->root.ldscript_def
15234 && (h->root.type == bfd_link_hash_undefined
15235 || h->root.type == bfd_link_hash_undefweak
15236 || ((h->ref_regular || h->def_dynamic)
15237 && !h->def_regular
15238 && h->root.type != bfd_link_hash_common)))
15239 {
15240 bool was_dynamic = h->ref_dynamic || h->def_dynamic;
15241 h->verinfo.verdef = NULL;
15242 h->root.type = bfd_link_hash_defined;
15243 h->root.u.def.section = sec;
15244 h->root.u.def.value = 0;
15245 h->def_regular = 1;
15246 h->def_dynamic = 0;
15247 h->start_stop = 1;
15248 h->u2.start_stop_section = sec;
15249 if (symbol[0] == '.')
15250 {
15251 /* .startof. and .sizeof. symbols are local. */
15252 const struct elf_backend_data *bed;
15253 bed = get_elf_backend_data (info->output_bfd);
15254 (*bed->elf_backend_hide_symbol) (info, h, true);
15255 }
15256 else
15257 {
15258 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
15259 h->other = ((h->other & ~ELF_ST_VISIBILITY (-1))
15260 | info->start_stop_visibility);
15261 if (was_dynamic)
15262 bfd_elf_link_record_dynamic_symbol (info, h);
15263 }
15264 return &h->root;
15265 }
15266 return NULL;
15267 }
15268
15269 /* Find dynamic relocs for H that apply to read-only sections. */
15270
15271 asection *
15272 _bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry *h)
15273 {
15274 struct elf_dyn_relocs *p;
15275
15276 for (p = h->dyn_relocs; p != NULL; p = p->next)
15277 {
15278 asection *s = p->sec->output_section;
15279
15280 if (s != NULL && (s->flags & SEC_READONLY) != 0)
15281 return p->sec;
15282 }
15283 return NULL;
15284 }
15285
15286 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
15287 read-only sections. */
15288
15289 bool
15290 _bfd_elf_maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
15291 {
15292 asection *sec;
15293
15294 if (h->root.type == bfd_link_hash_indirect)
15295 return true;
15296
15297 sec = _bfd_elf_readonly_dynrelocs (h);
15298 if (sec != NULL)
15299 {
15300 struct bfd_link_info *info = (struct bfd_link_info *) inf;
15301
15302 info->flags |= DF_TEXTREL;
15303 /* xgettext:c-format */
15304 info->callbacks->minfo (_("%pB: dynamic relocation against `%pT' "
15305 "in read-only section `%pA'\n"),
15306 sec->owner, h->root.root.string, sec);
15307
15308 if (bfd_link_textrel_check (info))
15309 /* xgettext:c-format */
15310 info->callbacks->einfo (_("%P: %pB: warning: relocation against `%s' "
15311 "in read-only section `%pA'\n"),
15312 sec->owner, h->root.root.string, sec);
15313
15314 /* Not an error, just cut short the traversal. */
15315 return false;
15316 }
15317 return true;
15318 }
15319
15320 /* Add dynamic tags. */
15321
15322 bool
15323 _bfd_elf_add_dynamic_tags (bfd *output_bfd, struct bfd_link_info *info,
15324 bool need_dynamic_reloc)
15325 {
15326 struct elf_link_hash_table *htab = elf_hash_table (info);
15327
15328 if (htab->dynamic_sections_created)
15329 {
15330 /* Add some entries to the .dynamic section. We fill in the
15331 values later, in finish_dynamic_sections, but we must add
15332 the entries now so that we get the correct size for the
15333 .dynamic section. The DT_DEBUG entry is filled in by the
15334 dynamic linker and used by the debugger. */
15335 #define add_dynamic_entry(TAG, VAL) \
15336 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
15337
15338 const struct elf_backend_data *bed
15339 = get_elf_backend_data (output_bfd);
15340
15341 if (bfd_link_executable (info))
15342 {
15343 if (!add_dynamic_entry (DT_DEBUG, 0))
15344 return false;
15345 }
15346
15347 if (htab->dt_pltgot_required || htab->splt->size != 0)
15348 {
15349 /* DT_PLTGOT is used by prelink even if there is no PLT
15350 relocation. */
15351 if (!add_dynamic_entry (DT_PLTGOT, 0))
15352 return false;
15353 }
15354
15355 if (htab->dt_jmprel_required || htab->srelplt->size != 0)
15356 {
15357 if (!add_dynamic_entry (DT_PLTRELSZ, 0)
15358 || !add_dynamic_entry (DT_PLTREL,
15359 (bed->rela_plts_and_copies_p
15360 ? DT_RELA : DT_REL))
15361 || !add_dynamic_entry (DT_JMPREL, 0))
15362 return false;
15363 }
15364
15365 if (htab->tlsdesc_plt
15366 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
15367 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
15368 return false;
15369
15370 if (need_dynamic_reloc)
15371 {
15372 if (bed->rela_plts_and_copies_p)
15373 {
15374 if (!add_dynamic_entry (DT_RELA, 0)
15375 || !add_dynamic_entry (DT_RELASZ, 0)
15376 || !add_dynamic_entry (DT_RELAENT,
15377 bed->s->sizeof_rela))
15378 return false;
15379 }
15380 else
15381 {
15382 if (!add_dynamic_entry (DT_REL, 0)
15383 || !add_dynamic_entry (DT_RELSZ, 0)
15384 || !add_dynamic_entry (DT_RELENT,
15385 bed->s->sizeof_rel))
15386 return false;
15387 }
15388
15389 /* If any dynamic relocs apply to a read-only section,
15390 then we need a DT_TEXTREL entry. */
15391 if ((info->flags & DF_TEXTREL) == 0)
15392 elf_link_hash_traverse (htab, _bfd_elf_maybe_set_textrel,
15393 info);
15394
15395 if ((info->flags & DF_TEXTREL) != 0)
15396 {
15397 if (htab->ifunc_resolvers)
15398 info->callbacks->einfo
15399 (_("%P: warning: GNU indirect functions with DT_TEXTREL "
15400 "may result in a segfault at runtime; recompile with %s\n"),
15401 bfd_link_dll (info) ? "-fPIC" : "-fPIE");
15402
15403 if (!add_dynamic_entry (DT_TEXTREL, 0))
15404 return false;
15405 }
15406 }
15407 }
15408 #undef add_dynamic_entry
15409
15410 return true;
15411 }