]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - bfd/elflink.c
ld: Initial DT_RELR support
[thirdparty/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 /* Look through the symbols which are defined in other shared
2217 libraries and referenced here. Update the list of version
2218 dependencies. This will be put into the .gnu.version_r section.
2219 This function is called via elf_link_hash_traverse. */
2220
2221 static bool
2222 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2223 void *data)
2224 {
2225 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2226 Elf_Internal_Verneed *t;
2227 Elf_Internal_Vernaux *a;
2228 size_t amt;
2229
2230 /* We only care about symbols defined in shared objects with version
2231 information. */
2232 if (!h->def_dynamic
2233 || h->def_regular
2234 || h->dynindx == -1
2235 || h->verinfo.verdef == NULL
2236 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2237 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2238 return true;
2239
2240 /* See if we already know about this version. */
2241 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2242 t != NULL;
2243 t = t->vn_nextref)
2244 {
2245 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2246 continue;
2247
2248 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2249 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2250 return true;
2251
2252 break;
2253 }
2254
2255 /* This is a new version. Add it to tree we are building. */
2256
2257 if (t == NULL)
2258 {
2259 amt = sizeof *t;
2260 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2261 if (t == NULL)
2262 {
2263 rinfo->failed = true;
2264 return false;
2265 }
2266
2267 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2268 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2269 elf_tdata (rinfo->info->output_bfd)->verref = t;
2270 }
2271
2272 amt = sizeof *a;
2273 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2274 if (a == NULL)
2275 {
2276 rinfo->failed = true;
2277 return false;
2278 }
2279
2280 /* Note that we are copying a string pointer here, and testing it
2281 above. If bfd_elf_string_from_elf_section is ever changed to
2282 discard the string data when low in memory, this will have to be
2283 fixed. */
2284 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2285
2286 a->vna_flags = h->verinfo.verdef->vd_flags;
2287 a->vna_nextptr = t->vn_auxptr;
2288
2289 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2290 ++rinfo->vers;
2291
2292 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2293
2294 t->vn_auxptr = a;
2295
2296 return true;
2297 }
2298
2299 /* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2300 hidden. Set *T_P to NULL if there is no match. */
2301
2302 static bool
2303 _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
2304 struct elf_link_hash_entry *h,
2305 const char *version_p,
2306 struct bfd_elf_version_tree **t_p,
2307 bool *hide)
2308 {
2309 struct bfd_elf_version_tree *t;
2310
2311 /* Look for the version. If we find it, it is no longer weak. */
2312 for (t = info->version_info; t != NULL; t = t->next)
2313 {
2314 if (strcmp (t->name, version_p) == 0)
2315 {
2316 size_t len;
2317 char *alc;
2318 struct bfd_elf_version_expr *d;
2319
2320 len = version_p - h->root.root.string;
2321 alc = (char *) bfd_malloc (len);
2322 if (alc == NULL)
2323 return false;
2324 memcpy (alc, h->root.root.string, len - 1);
2325 alc[len - 1] = '\0';
2326 if (alc[len - 2] == ELF_VER_CHR)
2327 alc[len - 2] = '\0';
2328
2329 h->verinfo.vertree = t;
2330 t->used = true;
2331 d = NULL;
2332
2333 if (t->globals.list != NULL)
2334 d = (*t->match) (&t->globals, NULL, alc);
2335
2336 /* See if there is anything to force this symbol to
2337 local scope. */
2338 if (d == NULL && t->locals.list != NULL)
2339 {
2340 d = (*t->match) (&t->locals, NULL, alc);
2341 if (d != NULL
2342 && h->dynindx != -1
2343 && ! info->export_dynamic)
2344 *hide = true;
2345 }
2346
2347 free (alc);
2348 break;
2349 }
2350 }
2351
2352 *t_p = t;
2353
2354 return true;
2355 }
2356
2357 /* Return TRUE if the symbol H is hidden by version script. */
2358
2359 bool
2360 _bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
2361 struct elf_link_hash_entry *h)
2362 {
2363 const char *p;
2364 bool hide = false;
2365 const struct elf_backend_data *bed
2366 = get_elf_backend_data (info->output_bfd);
2367
2368 /* Version script only hides symbols defined in regular objects. */
2369 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2370 return true;
2371
2372 p = strchr (h->root.root.string, ELF_VER_CHR);
2373 if (p != NULL && h->verinfo.vertree == NULL)
2374 {
2375 struct bfd_elf_version_tree *t;
2376
2377 ++p;
2378 if (*p == ELF_VER_CHR)
2379 ++p;
2380
2381 if (*p != '\0'
2382 && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
2383 && hide)
2384 {
2385 if (hide)
2386 (*bed->elf_backend_hide_symbol) (info, h, true);
2387 return true;
2388 }
2389 }
2390
2391 /* If we don't have a version for this symbol, see if we can find
2392 something. */
2393 if (h->verinfo.vertree == NULL && info->version_info != NULL)
2394 {
2395 h->verinfo.vertree
2396 = bfd_find_version_for_sym (info->version_info,
2397 h->root.root.string, &hide);
2398 if (h->verinfo.vertree != NULL && hide)
2399 {
2400 (*bed->elf_backend_hide_symbol) (info, h, true);
2401 return true;
2402 }
2403 }
2404
2405 return false;
2406 }
2407
2408 /* Figure out appropriate versions for all the symbols. We may not
2409 have the version number script until we have read all of the input
2410 files, so until that point we don't know which symbols should be
2411 local. This function is called via elf_link_hash_traverse. */
2412
2413 static bool
2414 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2415 {
2416 struct elf_info_failed *sinfo;
2417 struct bfd_link_info *info;
2418 const struct elf_backend_data *bed;
2419 struct elf_info_failed eif;
2420 char *p;
2421 bool hide;
2422
2423 sinfo = (struct elf_info_failed *) data;
2424 info = sinfo->info;
2425
2426 /* Fix the symbol flags. */
2427 eif.failed = false;
2428 eif.info = info;
2429 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2430 {
2431 if (eif.failed)
2432 sinfo->failed = true;
2433 return false;
2434 }
2435
2436 bed = get_elf_backend_data (info->output_bfd);
2437
2438 /* We only need version numbers for symbols defined in regular
2439 objects. */
2440 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2441 {
2442 /* Hide symbols defined in discarded input sections. */
2443 if ((h->root.type == bfd_link_hash_defined
2444 || h->root.type == bfd_link_hash_defweak)
2445 && discarded_section (h->root.u.def.section))
2446 (*bed->elf_backend_hide_symbol) (info, h, true);
2447 return true;
2448 }
2449
2450 hide = false;
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 there is no version string, we can just return out. */
2461 if (*p == '\0')
2462 return true;
2463
2464 if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
2465 {
2466 sinfo->failed = true;
2467 return false;
2468 }
2469
2470 if (hide)
2471 (*bed->elf_backend_hide_symbol) (info, h, true);
2472
2473 /* If we are building an application, we need to create a
2474 version node for this version. */
2475 if (t == NULL && bfd_link_executable (info))
2476 {
2477 struct bfd_elf_version_tree **pp;
2478 int version_index;
2479
2480 /* If we aren't going to export this symbol, we don't need
2481 to worry about it. */
2482 if (h->dynindx == -1)
2483 return true;
2484
2485 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2486 sizeof *t);
2487 if (t == NULL)
2488 {
2489 sinfo->failed = true;
2490 return false;
2491 }
2492
2493 t->name = p;
2494 t->name_indx = (unsigned int) -1;
2495 t->used = true;
2496
2497 version_index = 1;
2498 /* Don't count anonymous version tag. */
2499 if (sinfo->info->version_info != NULL
2500 && sinfo->info->version_info->vernum == 0)
2501 version_index = 0;
2502 for (pp = &sinfo->info->version_info;
2503 *pp != NULL;
2504 pp = &(*pp)->next)
2505 ++version_index;
2506 t->vernum = version_index;
2507
2508 *pp = t;
2509
2510 h->verinfo.vertree = t;
2511 }
2512 else if (t == NULL)
2513 {
2514 /* We could not find the version for a symbol when
2515 generating a shared archive. Return an error. */
2516 _bfd_error_handler
2517 /* xgettext:c-format */
2518 (_("%pB: version node not found for symbol %s"),
2519 info->output_bfd, h->root.root.string);
2520 bfd_set_error (bfd_error_bad_value);
2521 sinfo->failed = true;
2522 return false;
2523 }
2524 }
2525
2526 /* If we don't have a version for this symbol, see if we can find
2527 something. */
2528 if (!hide
2529 && h->verinfo.vertree == NULL
2530 && sinfo->info->version_info != NULL)
2531 {
2532 h->verinfo.vertree
2533 = bfd_find_version_for_sym (sinfo->info->version_info,
2534 h->root.root.string, &hide);
2535 if (h->verinfo.vertree != NULL && hide)
2536 (*bed->elf_backend_hide_symbol) (info, h, true);
2537 }
2538
2539 return true;
2540 }
2541 \f
2542 /* Read and swap the relocs from the section indicated by SHDR. This
2543 may be either a REL or a RELA section. The relocations are
2544 translated into RELA relocations and stored in INTERNAL_RELOCS,
2545 which should have already been allocated to contain enough space.
2546 The EXTERNAL_RELOCS are a buffer where the external form of the
2547 relocations should be stored.
2548
2549 Returns FALSE if something goes wrong. */
2550
2551 static bool
2552 elf_link_read_relocs_from_section (bfd *abfd,
2553 asection *sec,
2554 Elf_Internal_Shdr *shdr,
2555 void *external_relocs,
2556 Elf_Internal_Rela *internal_relocs)
2557 {
2558 const struct elf_backend_data *bed;
2559 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2560 const bfd_byte *erela;
2561 const bfd_byte *erelaend;
2562 Elf_Internal_Rela *irela;
2563 Elf_Internal_Shdr *symtab_hdr;
2564 size_t nsyms;
2565
2566 /* Position ourselves at the start of the section. */
2567 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2568 return false;
2569
2570 /* Read the relocations. */
2571 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2572 return false;
2573
2574 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2575 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2576
2577 bed = get_elf_backend_data (abfd);
2578
2579 /* Convert the external relocations to the internal format. */
2580 if (shdr->sh_entsize == bed->s->sizeof_rel)
2581 swap_in = bed->s->swap_reloc_in;
2582 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2583 swap_in = bed->s->swap_reloca_in;
2584 else
2585 {
2586 bfd_set_error (bfd_error_wrong_format);
2587 return false;
2588 }
2589
2590 erela = (const bfd_byte *) external_relocs;
2591 /* Setting erelaend like this and comparing with <= handles case of
2592 a fuzzed object with sh_size not a multiple of sh_entsize. */
2593 erelaend = erela + shdr->sh_size - shdr->sh_entsize;
2594 irela = internal_relocs;
2595 while (erela <= erelaend)
2596 {
2597 bfd_vma r_symndx;
2598
2599 (*swap_in) (abfd, erela, irela);
2600 r_symndx = ELF32_R_SYM (irela->r_info);
2601 if (bed->s->arch_size == 64)
2602 r_symndx >>= 24;
2603 if (nsyms > 0)
2604 {
2605 if ((size_t) r_symndx >= nsyms)
2606 {
2607 _bfd_error_handler
2608 /* xgettext:c-format */
2609 (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
2610 " for offset %#" PRIx64 " in section `%pA'"),
2611 abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
2612 (uint64_t) irela->r_offset, sec);
2613 bfd_set_error (bfd_error_bad_value);
2614 return false;
2615 }
2616 }
2617 else if (r_symndx != STN_UNDEF)
2618 {
2619 _bfd_error_handler
2620 /* xgettext:c-format */
2621 (_("%pB: non-zero symbol index (%#" PRIx64 ")"
2622 " for offset %#" PRIx64 " in section `%pA'"
2623 " when the object file has no symbol table"),
2624 abfd, (uint64_t) r_symndx,
2625 (uint64_t) irela->r_offset, sec);
2626 bfd_set_error (bfd_error_bad_value);
2627 return false;
2628 }
2629 irela += bed->s->int_rels_per_ext_rel;
2630 erela += shdr->sh_entsize;
2631 }
2632
2633 return true;
2634 }
2635
2636 /* Read and swap the relocs for a section O. They may have been
2637 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2638 not NULL, they are used as buffers to read into. They are known to
2639 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2640 the return value is allocated using either malloc or bfd_alloc,
2641 according to the KEEP_MEMORY argument. If O has two relocation
2642 sections (both REL and RELA relocations), then the REL_HDR
2643 relocations will appear first in INTERNAL_RELOCS, followed by the
2644 RELA_HDR relocations. If INFO isn't NULL and KEEP_MEMORY is true,
2645 update cache_size. */
2646
2647 Elf_Internal_Rela *
2648 _bfd_elf_link_info_read_relocs (bfd *abfd,
2649 struct bfd_link_info *info,
2650 asection *o,
2651 void *external_relocs,
2652 Elf_Internal_Rela *internal_relocs,
2653 bool keep_memory)
2654 {
2655 void *alloc1 = NULL;
2656 Elf_Internal_Rela *alloc2 = NULL;
2657 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2658 struct bfd_elf_section_data *esdo = elf_section_data (o);
2659 Elf_Internal_Rela *internal_rela_relocs;
2660
2661 if (esdo->relocs != NULL)
2662 return esdo->relocs;
2663
2664 if (o->reloc_count == 0)
2665 return NULL;
2666
2667 if (internal_relocs == NULL)
2668 {
2669 bfd_size_type size;
2670
2671 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
2672 if (keep_memory)
2673 {
2674 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2675 if (info)
2676 info->cache_size += size;
2677 }
2678 else
2679 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2680 if (internal_relocs == NULL)
2681 goto error_return;
2682 }
2683
2684 if (external_relocs == NULL)
2685 {
2686 bfd_size_type size = 0;
2687
2688 if (esdo->rel.hdr)
2689 size += esdo->rel.hdr->sh_size;
2690 if (esdo->rela.hdr)
2691 size += esdo->rela.hdr->sh_size;
2692
2693 alloc1 = bfd_malloc (size);
2694 if (alloc1 == NULL)
2695 goto error_return;
2696 external_relocs = alloc1;
2697 }
2698
2699 internal_rela_relocs = internal_relocs;
2700 if (esdo->rel.hdr)
2701 {
2702 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2703 external_relocs,
2704 internal_relocs))
2705 goto error_return;
2706 external_relocs = (((bfd_byte *) external_relocs)
2707 + esdo->rel.hdr->sh_size);
2708 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2709 * bed->s->int_rels_per_ext_rel);
2710 }
2711
2712 if (esdo->rela.hdr
2713 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2714 external_relocs,
2715 internal_rela_relocs)))
2716 goto error_return;
2717
2718 /* Cache the results for next time, if we can. */
2719 if (keep_memory)
2720 esdo->relocs = internal_relocs;
2721
2722 free (alloc1);
2723
2724 /* Don't free alloc2, since if it was allocated we are passing it
2725 back (under the name of internal_relocs). */
2726
2727 return internal_relocs;
2728
2729 error_return:
2730 free (alloc1);
2731 if (alloc2 != NULL)
2732 {
2733 if (keep_memory)
2734 bfd_release (abfd, alloc2);
2735 else
2736 free (alloc2);
2737 }
2738 return NULL;
2739 }
2740
2741 /* This is similar to _bfd_elf_link_info_read_relocs, except for that
2742 NULL is passed to _bfd_elf_link_info_read_relocs for pointer to
2743 struct bfd_link_info. */
2744
2745 Elf_Internal_Rela *
2746 _bfd_elf_link_read_relocs (bfd *abfd,
2747 asection *o,
2748 void *external_relocs,
2749 Elf_Internal_Rela *internal_relocs,
2750 bool keep_memory)
2751 {
2752 return _bfd_elf_link_info_read_relocs (abfd, NULL, o, external_relocs,
2753 internal_relocs, keep_memory);
2754
2755 }
2756
2757 /* Compute the size of, and allocate space for, REL_HDR which is the
2758 section header for a section containing relocations for O. */
2759
2760 static bool
2761 _bfd_elf_link_size_reloc_section (bfd *abfd,
2762 struct bfd_elf_section_reloc_data *reldata)
2763 {
2764 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2765
2766 /* That allows us to calculate the size of the section. */
2767 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2768
2769 /* The contents field must last into write_object_contents, so we
2770 allocate it with bfd_alloc rather than malloc. Also since we
2771 cannot be sure that the contents will actually be filled in,
2772 we zero the allocated space. */
2773 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2774 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2775 return false;
2776
2777 if (reldata->hashes == NULL && reldata->count)
2778 {
2779 struct elf_link_hash_entry **p;
2780
2781 p = ((struct elf_link_hash_entry **)
2782 bfd_zmalloc (reldata->count * sizeof (*p)));
2783 if (p == NULL)
2784 return false;
2785
2786 reldata->hashes = p;
2787 }
2788
2789 return true;
2790 }
2791
2792 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2793 originated from the section given by INPUT_REL_HDR) to the
2794 OUTPUT_BFD. */
2795
2796 bool
2797 _bfd_elf_link_output_relocs (bfd *output_bfd,
2798 asection *input_section,
2799 Elf_Internal_Shdr *input_rel_hdr,
2800 Elf_Internal_Rela *internal_relocs,
2801 struct elf_link_hash_entry **rel_hash
2802 ATTRIBUTE_UNUSED)
2803 {
2804 Elf_Internal_Rela *irela;
2805 Elf_Internal_Rela *irelaend;
2806 bfd_byte *erel;
2807 struct bfd_elf_section_reloc_data *output_reldata;
2808 asection *output_section;
2809 const struct elf_backend_data *bed;
2810 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2811 struct bfd_elf_section_data *esdo;
2812
2813 output_section = input_section->output_section;
2814
2815 bed = get_elf_backend_data (output_bfd);
2816 esdo = elf_section_data (output_section);
2817 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2818 {
2819 output_reldata = &esdo->rel;
2820 swap_out = bed->s->swap_reloc_out;
2821 }
2822 else if (esdo->rela.hdr
2823 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2824 {
2825 output_reldata = &esdo->rela;
2826 swap_out = bed->s->swap_reloca_out;
2827 }
2828 else
2829 {
2830 _bfd_error_handler
2831 /* xgettext:c-format */
2832 (_("%pB: relocation size mismatch in %pB section %pA"),
2833 output_bfd, input_section->owner, input_section);
2834 bfd_set_error (bfd_error_wrong_format);
2835 return false;
2836 }
2837
2838 erel = output_reldata->hdr->contents;
2839 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2840 irela = internal_relocs;
2841 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2842 * bed->s->int_rels_per_ext_rel);
2843 while (irela < irelaend)
2844 {
2845 (*swap_out) (output_bfd, irela, erel);
2846 irela += bed->s->int_rels_per_ext_rel;
2847 erel += input_rel_hdr->sh_entsize;
2848 }
2849
2850 /* Bump the counter, so that we know where to add the next set of
2851 relocations. */
2852 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2853
2854 return true;
2855 }
2856 \f
2857 /* Make weak undefined symbols in PIE dynamic. */
2858
2859 bool
2860 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2861 struct elf_link_hash_entry *h)
2862 {
2863 if (bfd_link_pie (info)
2864 && h->dynindx == -1
2865 && h->root.type == bfd_link_hash_undefweak)
2866 return bfd_elf_link_record_dynamic_symbol (info, h);
2867
2868 return true;
2869 }
2870
2871 /* Fix up the flags for a symbol. This handles various cases which
2872 can only be fixed after all the input files are seen. This is
2873 currently called by both adjust_dynamic_symbol and
2874 assign_sym_version, which is unnecessary but perhaps more robust in
2875 the face of future changes. */
2876
2877 static bool
2878 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2879 struct elf_info_failed *eif)
2880 {
2881 const struct elf_backend_data *bed;
2882
2883 /* If this symbol was mentioned in a non-ELF file, try to set
2884 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2885 permit a non-ELF file to correctly refer to a symbol defined in
2886 an ELF dynamic object. */
2887 if (h->non_elf)
2888 {
2889 while (h->root.type == bfd_link_hash_indirect)
2890 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2891
2892 if (h->root.type != bfd_link_hash_defined
2893 && h->root.type != bfd_link_hash_defweak)
2894 {
2895 h->ref_regular = 1;
2896 h->ref_regular_nonweak = 1;
2897 }
2898 else
2899 {
2900 if (h->root.u.def.section->owner != NULL
2901 && (bfd_get_flavour (h->root.u.def.section->owner)
2902 == bfd_target_elf_flavour))
2903 {
2904 h->ref_regular = 1;
2905 h->ref_regular_nonweak = 1;
2906 }
2907 else
2908 h->def_regular = 1;
2909 }
2910
2911 if (h->dynindx == -1
2912 && (h->def_dynamic
2913 || h->ref_dynamic))
2914 {
2915 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2916 {
2917 eif->failed = true;
2918 return false;
2919 }
2920 }
2921 }
2922 else
2923 {
2924 /* Unfortunately, NON_ELF is only correct if the symbol
2925 was first seen in a non-ELF file. Fortunately, if the symbol
2926 was first seen in an ELF file, we're probably OK unless the
2927 symbol was defined in a non-ELF file. Catch that case here.
2928 FIXME: We're still in trouble if the symbol was first seen in
2929 a dynamic object, and then later in a non-ELF regular object. */
2930 if ((h->root.type == bfd_link_hash_defined
2931 || h->root.type == bfd_link_hash_defweak)
2932 && !h->def_regular
2933 && (h->root.u.def.section->owner != NULL
2934 ? (bfd_get_flavour (h->root.u.def.section->owner)
2935 != bfd_target_elf_flavour)
2936 : (bfd_is_abs_section (h->root.u.def.section)
2937 && !h->def_dynamic)))
2938 h->def_regular = 1;
2939 }
2940
2941 /* Backend specific symbol fixup. */
2942 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2943 if (bed->elf_backend_fixup_symbol
2944 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2945 return false;
2946
2947 /* If this is a final link, and the symbol was defined as a common
2948 symbol in a regular object file, and there was no definition in
2949 any dynamic object, then the linker will have allocated space for
2950 the symbol in a common section but the DEF_REGULAR
2951 flag will not have been set. */
2952 if (h->root.type == bfd_link_hash_defined
2953 && !h->def_regular
2954 && h->ref_regular
2955 && !h->def_dynamic
2956 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
2957 h->def_regular = 1;
2958
2959 /* Symbols defined in discarded sections shouldn't be dynamic. */
2960 if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
2961 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
2962
2963 /* If a weak undefined symbol has non-default visibility, we also
2964 hide it from the dynamic linker. */
2965 else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2966 && h->root.type == bfd_link_hash_undefweak)
2967 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
2968
2969 /* A hidden versioned symbol in executable should be forced local if
2970 it is is locally defined, not referenced by shared library and not
2971 exported. */
2972 else if (bfd_link_executable (eif->info)
2973 && h->versioned == versioned_hidden
2974 && !eif->info->export_dynamic
2975 && !h->dynamic
2976 && !h->ref_dynamic
2977 && h->def_regular)
2978 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
2979
2980 /* If -Bsymbolic was used (which means to bind references to global
2981 symbols to the definition within the shared object), and this
2982 symbol was defined in a regular object, then it actually doesn't
2983 need a PLT entry. Likewise, if the symbol has non-default
2984 visibility. If the symbol has hidden or internal visibility, we
2985 will force it local. */
2986 else if (h->needs_plt
2987 && bfd_link_pic (eif->info)
2988 && is_elf_hash_table (eif->info->hash)
2989 && (SYMBOLIC_BIND (eif->info, h)
2990 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2991 && h->def_regular)
2992 {
2993 bool force_local;
2994
2995 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2996 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2997 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2998 }
2999
3000 /* If this is a weak defined symbol in a dynamic object, and we know
3001 the real definition in the dynamic object, copy interesting flags
3002 over to the real definition. */
3003 if (h->is_weakalias)
3004 {
3005 struct elf_link_hash_entry *def = weakdef (h);
3006
3007 /* If the real definition is defined by a regular object file,
3008 don't do anything special. See the longer description in
3009 _bfd_elf_adjust_dynamic_symbol, below. If the def is not
3010 bfd_link_hash_defined as it was when put on the alias list
3011 then it must have originally been a versioned symbol (for
3012 which a non-versioned indirect symbol is created) and later
3013 a definition for the non-versioned symbol is found. In that
3014 case the indirection is flipped with the versioned symbol
3015 becoming an indirect pointing at the non-versioned symbol.
3016 Thus, not an alias any more. */
3017 if (def->def_regular
3018 || def->root.type != bfd_link_hash_defined)
3019 {
3020 h = def;
3021 while ((h = h->u.alias) != def)
3022 h->is_weakalias = 0;
3023 }
3024 else
3025 {
3026 while (h->root.type == bfd_link_hash_indirect)
3027 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3028 BFD_ASSERT (h->root.type == bfd_link_hash_defined
3029 || h->root.type == bfd_link_hash_defweak);
3030 BFD_ASSERT (def->def_dynamic);
3031 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
3032 }
3033 }
3034
3035 return true;
3036 }
3037
3038 /* Make the backend pick a good value for a dynamic symbol. This is
3039 called via elf_link_hash_traverse, and also calls itself
3040 recursively. */
3041
3042 static bool
3043 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
3044 {
3045 struct elf_info_failed *eif = (struct elf_info_failed *) data;
3046 struct elf_link_hash_table *htab;
3047 const struct elf_backend_data *bed;
3048
3049 if (! is_elf_hash_table (eif->info->hash))
3050 return false;
3051
3052 /* Ignore indirect symbols. These are added by the versioning code. */
3053 if (h->root.type == bfd_link_hash_indirect)
3054 return true;
3055
3056 /* Fix the symbol flags. */
3057 if (! _bfd_elf_fix_symbol_flags (h, eif))
3058 return false;
3059
3060 htab = elf_hash_table (eif->info);
3061 bed = get_elf_backend_data (htab->dynobj);
3062
3063 if (h->root.type == bfd_link_hash_undefweak)
3064 {
3065 if (eif->info->dynamic_undefined_weak == 0)
3066 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3067 else if (eif->info->dynamic_undefined_weak > 0
3068 && h->ref_regular
3069 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3070 && !bfd_hide_sym_by_version (eif->info->version_info,
3071 h->root.root.string))
3072 {
3073 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
3074 {
3075 eif->failed = true;
3076 return false;
3077 }
3078 }
3079 }
3080
3081 /* If this symbol does not require a PLT entry, and it is not
3082 defined by a dynamic object, or is not referenced by a regular
3083 object, ignore it. We do have to handle a weak defined symbol,
3084 even if no regular object refers to it, if we decided to add it
3085 to the dynamic symbol table. FIXME: Do we normally need to worry
3086 about symbols which are defined by one dynamic object and
3087 referenced by another one? */
3088 if (!h->needs_plt
3089 && h->type != STT_GNU_IFUNC
3090 && (h->def_regular
3091 || !h->def_dynamic
3092 || (!h->ref_regular
3093 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
3094 {
3095 h->plt = elf_hash_table (eif->info)->init_plt_offset;
3096 return true;
3097 }
3098
3099 /* If we've already adjusted this symbol, don't do it again. This
3100 can happen via a recursive call. */
3101 if (h->dynamic_adjusted)
3102 return true;
3103
3104 /* Don't look at this symbol again. Note that we must set this
3105 after checking the above conditions, because we may look at a
3106 symbol once, decide not to do anything, and then get called
3107 recursively later after REF_REGULAR is set below. */
3108 h->dynamic_adjusted = 1;
3109
3110 /* If this is a weak definition, and we know a real definition, and
3111 the real symbol is not itself defined by a regular object file,
3112 then get a good value for the real definition. We handle the
3113 real symbol first, for the convenience of the backend routine.
3114
3115 Note that there is a confusing case here. If the real definition
3116 is defined by a regular object file, we don't get the real symbol
3117 from the dynamic object, but we do get the weak symbol. If the
3118 processor backend uses a COPY reloc, then if some routine in the
3119 dynamic object changes the real symbol, we will not see that
3120 change in the corresponding weak symbol. This is the way other
3121 ELF linkers work as well, and seems to be a result of the shared
3122 library model.
3123
3124 I will clarify this issue. Most SVR4 shared libraries define the
3125 variable _timezone and define timezone as a weak synonym. The
3126 tzset call changes _timezone. If you write
3127 extern int timezone;
3128 int _timezone = 5;
3129 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3130 you might expect that, since timezone is a synonym for _timezone,
3131 the same number will print both times. However, if the processor
3132 backend uses a COPY reloc, then actually timezone will be copied
3133 into your process image, and, since you define _timezone
3134 yourself, _timezone will not. Thus timezone and _timezone will
3135 wind up at different memory locations. The tzset call will set
3136 _timezone, leaving timezone unchanged. */
3137
3138 if (h->is_weakalias)
3139 {
3140 struct elf_link_hash_entry *def = weakdef (h);
3141
3142 /* If we get to this point, there is an implicit reference to
3143 the alias by a regular object file via the weak symbol H. */
3144 def->ref_regular = 1;
3145
3146 /* Ensure that the backend adjust_dynamic_symbol function sees
3147 the strong alias before H by recursively calling ourselves. */
3148 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
3149 return false;
3150 }
3151
3152 /* If a symbol has no type and no size and does not require a PLT
3153 entry, then we are probably about to do the wrong thing here: we
3154 are probably going to create a COPY reloc for an empty object.
3155 This case can arise when a shared object is built with assembly
3156 code, and the assembly code fails to set the symbol type. */
3157 if (h->size == 0
3158 && h->type == STT_NOTYPE
3159 && !h->needs_plt)
3160 _bfd_error_handler
3161 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3162 h->root.root.string);
3163
3164 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3165 {
3166 eif->failed = true;
3167 return false;
3168 }
3169
3170 return true;
3171 }
3172
3173 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3174 DYNBSS. */
3175
3176 bool
3177 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3178 struct elf_link_hash_entry *h,
3179 asection *dynbss)
3180 {
3181 unsigned int power_of_two;
3182 bfd_vma mask;
3183 asection *sec = h->root.u.def.section;
3184
3185 /* The section alignment of the definition is the maximum alignment
3186 requirement of symbols defined in the section. Since we don't
3187 know the symbol alignment requirement, we start with the
3188 maximum alignment and check low bits of the symbol address
3189 for the minimum alignment. */
3190 power_of_two = bfd_section_alignment (sec);
3191 mask = ((bfd_vma) 1 << power_of_two) - 1;
3192 while ((h->root.u.def.value & mask) != 0)
3193 {
3194 mask >>= 1;
3195 --power_of_two;
3196 }
3197
3198 if (power_of_two > bfd_section_alignment (dynbss))
3199 {
3200 /* Adjust the section alignment if needed. */
3201 if (!bfd_set_section_alignment (dynbss, power_of_two))
3202 return false;
3203 }
3204
3205 /* We make sure that the symbol will be aligned properly. */
3206 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3207
3208 /* Define the symbol as being at this point in DYNBSS. */
3209 h->root.u.def.section = dynbss;
3210 h->root.u.def.value = dynbss->size;
3211
3212 /* Increment the size of DYNBSS to make room for the symbol. */
3213 dynbss->size += h->size;
3214
3215 /* No error if extern_protected_data is true. */
3216 if (h->protected_def
3217 && (!info->extern_protected_data
3218 || (info->extern_protected_data < 0
3219 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
3220 info->callbacks->einfo
3221 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3222 h->root.root.string);
3223
3224 return true;
3225 }
3226
3227 /* Adjust all external symbols pointing into SEC_MERGE sections
3228 to reflect the object merging within the sections. */
3229
3230 static bool
3231 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
3232 {
3233 asection *sec;
3234
3235 if ((h->root.type == bfd_link_hash_defined
3236 || h->root.type == bfd_link_hash_defweak)
3237 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
3238 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3239 {
3240 bfd *output_bfd = (bfd *) data;
3241
3242 h->root.u.def.value =
3243 _bfd_merged_section_offset (output_bfd,
3244 &h->root.u.def.section,
3245 elf_section_data (sec)->sec_info,
3246 h->root.u.def.value);
3247 }
3248
3249 return true;
3250 }
3251
3252 /* Returns false if the symbol referred to by H should be considered
3253 to resolve local to the current module, and true if it should be
3254 considered to bind dynamically. */
3255
3256 bool
3257 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3258 struct bfd_link_info *info,
3259 bool not_local_protected)
3260 {
3261 bool binding_stays_local_p;
3262 const struct elf_backend_data *bed;
3263 struct elf_link_hash_table *hash_table;
3264
3265 if (h == NULL)
3266 return false;
3267
3268 while (h->root.type == bfd_link_hash_indirect
3269 || h->root.type == bfd_link_hash_warning)
3270 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3271
3272 /* If it was forced local, then clearly it's not dynamic. */
3273 if (h->dynindx == -1)
3274 return false;
3275 if (h->forced_local)
3276 return false;
3277
3278 /* Identify the cases where name binding rules say that a
3279 visible symbol resolves locally. */
3280 binding_stays_local_p = (bfd_link_executable (info)
3281 || SYMBOLIC_BIND (info, h));
3282
3283 switch (ELF_ST_VISIBILITY (h->other))
3284 {
3285 case STV_INTERNAL:
3286 case STV_HIDDEN:
3287 return false;
3288
3289 case STV_PROTECTED:
3290 hash_table = elf_hash_table (info);
3291 if (!is_elf_hash_table (&hash_table->root))
3292 return false;
3293
3294 bed = get_elf_backend_data (hash_table->dynobj);
3295
3296 /* Proper resolution for function pointer equality may require
3297 that these symbols perhaps be resolved dynamically, even though
3298 we should be resolving them to the current module. */
3299 if (!not_local_protected || !bed->is_function_type (h->type))
3300 binding_stays_local_p = true;
3301 break;
3302
3303 default:
3304 break;
3305 }
3306
3307 /* If it isn't defined locally, then clearly it's dynamic. */
3308 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
3309 return true;
3310
3311 /* Otherwise, the symbol is dynamic if binding rules don't tell
3312 us that it remains local. */
3313 return !binding_stays_local_p;
3314 }
3315
3316 /* Return true if the symbol referred to by H should be considered
3317 to resolve local to the current module, and false otherwise. Differs
3318 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3319 undefined symbols. The two functions are virtually identical except
3320 for the place where dynindx == -1 is tested. If that test is true,
3321 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3322 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3323 defined symbols.
3324 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3325 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3326 treatment of undefined weak symbols. For those that do not make
3327 undefined weak symbols dynamic, both functions may return false. */
3328
3329 bool
3330 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3331 struct bfd_link_info *info,
3332 bool local_protected)
3333 {
3334 const struct elf_backend_data *bed;
3335 struct elf_link_hash_table *hash_table;
3336
3337 /* If it's a local sym, of course we resolve locally. */
3338 if (h == NULL)
3339 return true;
3340
3341 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3342 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3343 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3344 return true;
3345
3346 /* Forced local symbols resolve locally. */
3347 if (h->forced_local)
3348 return true;
3349
3350 /* Common symbols that become definitions don't get the DEF_REGULAR
3351 flag set, so test it first, and don't bail out. */
3352 if (ELF_COMMON_DEF_P (h))
3353 /* Do nothing. */;
3354 /* If we don't have a definition in a regular file, then we can't
3355 resolve locally. The sym is either undefined or dynamic. */
3356 else if (!h->def_regular)
3357 return false;
3358
3359 /* Non-dynamic symbols resolve locally. */
3360 if (h->dynindx == -1)
3361 return true;
3362
3363 /* At this point, we know the symbol is defined and dynamic. In an
3364 executable it must resolve locally, likewise when building symbolic
3365 shared libraries. */
3366 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3367 return true;
3368
3369 /* Now deal with defined dynamic symbols in shared libraries. Ones
3370 with default visibility might not resolve locally. */
3371 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3372 return false;
3373
3374 hash_table = elf_hash_table (info);
3375 if (!is_elf_hash_table (&hash_table->root))
3376 return true;
3377
3378 /* STV_PROTECTED symbols with indirect external access are local. */
3379 if (info->indirect_extern_access > 0)
3380 return true;
3381
3382 bed = get_elf_backend_data (hash_table->dynobj);
3383
3384 /* If extern_protected_data is false, STV_PROTECTED non-function
3385 symbols are local. */
3386 if ((!info->extern_protected_data
3387 || (info->extern_protected_data < 0
3388 && !bed->extern_protected_data))
3389 && !bed->is_function_type (h->type))
3390 return true;
3391
3392 /* Function pointer equality tests may require that STV_PROTECTED
3393 symbols be treated as dynamic symbols. If the address of a
3394 function not defined in an executable is set to that function's
3395 plt entry in the executable, then the address of the function in
3396 a shared library must also be the plt entry in the executable. */
3397 return local_protected;
3398 }
3399
3400 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3401 aligned. Returns the first TLS output section. */
3402
3403 struct bfd_section *
3404 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3405 {
3406 struct bfd_section *sec, *tls;
3407 unsigned int align = 0;
3408
3409 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3410 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3411 break;
3412 tls = sec;
3413
3414 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3415 if (sec->alignment_power > align)
3416 align = sec->alignment_power;
3417
3418 elf_hash_table (info)->tls_sec = tls;
3419
3420 /* Ensure the alignment of the first section (usually .tdata) is the largest
3421 alignment, so that the tls segment starts aligned. */
3422 if (tls != NULL)
3423 tls->alignment_power = align;
3424
3425 return tls;
3426 }
3427
3428 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3429 static bool
3430 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3431 Elf_Internal_Sym *sym)
3432 {
3433 const struct elf_backend_data *bed;
3434
3435 /* Local symbols do not count, but target specific ones might. */
3436 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3437 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3438 return false;
3439
3440 bed = get_elf_backend_data (abfd);
3441 /* Function symbols do not count. */
3442 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3443 return false;
3444
3445 /* If the section is undefined, then so is the symbol. */
3446 if (sym->st_shndx == SHN_UNDEF)
3447 return false;
3448
3449 /* If the symbol is defined in the common section, then
3450 it is a common definition and so does not count. */
3451 if (bed->common_definition (sym))
3452 return false;
3453
3454 /* If the symbol is in a target specific section then we
3455 must rely upon the backend to tell us what it is. */
3456 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3457 /* FIXME - this function is not coded yet:
3458
3459 return _bfd_is_global_symbol_definition (abfd, sym);
3460
3461 Instead for now assume that the definition is not global,
3462 Even if this is wrong, at least the linker will behave
3463 in the same way that it used to do. */
3464 return false;
3465
3466 return true;
3467 }
3468
3469 /* Search the symbol table of the archive element of the archive ABFD
3470 whose archive map contains a mention of SYMDEF, and determine if
3471 the symbol is defined in this element. */
3472 static bool
3473 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3474 {
3475 Elf_Internal_Shdr * hdr;
3476 size_t symcount;
3477 size_t extsymcount;
3478 size_t extsymoff;
3479 Elf_Internal_Sym *isymbuf;
3480 Elf_Internal_Sym *isym;
3481 Elf_Internal_Sym *isymend;
3482 bool result;
3483
3484 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset, NULL);
3485 if (abfd == NULL)
3486 return false;
3487
3488 if (! bfd_check_format (abfd, bfd_object))
3489 return false;
3490
3491 /* Select the appropriate symbol table. If we don't know if the
3492 object file is an IR object, give linker LTO plugin a chance to
3493 get the correct symbol table. */
3494 if (abfd->plugin_format == bfd_plugin_yes
3495 #if BFD_SUPPORTS_PLUGINS
3496 || (abfd->plugin_format == bfd_plugin_unknown
3497 && bfd_link_plugin_object_p (abfd))
3498 #endif
3499 )
3500 {
3501 /* Use the IR symbol table if the object has been claimed by
3502 plugin. */
3503 abfd = abfd->plugin_dummy_bfd;
3504 hdr = &elf_tdata (abfd)->symtab_hdr;
3505 }
3506 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3507 hdr = &elf_tdata (abfd)->symtab_hdr;
3508 else
3509 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3510
3511 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3512
3513 /* The sh_info field of the symtab header tells us where the
3514 external symbols start. We don't care about the local symbols. */
3515 if (elf_bad_symtab (abfd))
3516 {
3517 extsymcount = symcount;
3518 extsymoff = 0;
3519 }
3520 else
3521 {
3522 extsymcount = symcount - hdr->sh_info;
3523 extsymoff = hdr->sh_info;
3524 }
3525
3526 if (extsymcount == 0)
3527 return false;
3528
3529 /* Read in the symbol table. */
3530 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3531 NULL, NULL, NULL);
3532 if (isymbuf == NULL)
3533 return false;
3534
3535 /* Scan the symbol table looking for SYMDEF. */
3536 result = false;
3537 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3538 {
3539 const char *name;
3540
3541 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3542 isym->st_name);
3543 if (name == NULL)
3544 break;
3545
3546 if (strcmp (name, symdef->name) == 0)
3547 {
3548 result = is_global_data_symbol_definition (abfd, isym);
3549 break;
3550 }
3551 }
3552
3553 free (isymbuf);
3554
3555 return result;
3556 }
3557 \f
3558 /* Add an entry to the .dynamic table. */
3559
3560 bool
3561 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3562 bfd_vma tag,
3563 bfd_vma val)
3564 {
3565 struct elf_link_hash_table *hash_table;
3566 const struct elf_backend_data *bed;
3567 asection *s;
3568 bfd_size_type newsize;
3569 bfd_byte *newcontents;
3570 Elf_Internal_Dyn dyn;
3571
3572 hash_table = elf_hash_table (info);
3573 if (! is_elf_hash_table (&hash_table->root))
3574 return false;
3575
3576 if (tag == DT_RELA || tag == DT_REL)
3577 hash_table->dynamic_relocs = true;
3578
3579 bed = get_elf_backend_data (hash_table->dynobj);
3580 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3581 BFD_ASSERT (s != NULL);
3582
3583 newsize = s->size + bed->s->sizeof_dyn;
3584 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3585 if (newcontents == NULL)
3586 return false;
3587
3588 dyn.d_tag = tag;
3589 dyn.d_un.d_val = val;
3590 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3591
3592 s->size = newsize;
3593 s->contents = newcontents;
3594
3595 return true;
3596 }
3597
3598 /* Strip zero-sized dynamic sections. */
3599
3600 bool
3601 _bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info *info)
3602 {
3603 struct elf_link_hash_table *hash_table;
3604 const struct elf_backend_data *bed;
3605 asection *s, *sdynamic, **pp;
3606 asection *rela_dyn, *rel_dyn;
3607 Elf_Internal_Dyn dyn;
3608 bfd_byte *extdyn, *next;
3609 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3610 bool strip_zero_sized;
3611 bool strip_zero_sized_plt;
3612
3613 if (bfd_link_relocatable (info))
3614 return true;
3615
3616 hash_table = elf_hash_table (info);
3617 if (!is_elf_hash_table (&hash_table->root))
3618 return false;
3619
3620 if (!hash_table->dynobj)
3621 return true;
3622
3623 sdynamic= bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3624 if (!sdynamic)
3625 return true;
3626
3627 bed = get_elf_backend_data (hash_table->dynobj);
3628 swap_dyn_in = bed->s->swap_dyn_in;
3629
3630 strip_zero_sized = false;
3631 strip_zero_sized_plt = false;
3632
3633 /* Strip zero-sized dynamic sections. */
3634 rela_dyn = bfd_get_section_by_name (info->output_bfd, ".rela.dyn");
3635 rel_dyn = bfd_get_section_by_name (info->output_bfd, ".rel.dyn");
3636 for (pp = &info->output_bfd->sections; (s = *pp) != NULL;)
3637 if (s->size == 0
3638 && (s == rela_dyn
3639 || s == rel_dyn
3640 || s == hash_table->srelplt->output_section
3641 || s == hash_table->splt->output_section))
3642 {
3643 *pp = s->next;
3644 info->output_bfd->section_count--;
3645 strip_zero_sized = true;
3646 if (s == rela_dyn)
3647 s = rela_dyn;
3648 if (s == rel_dyn)
3649 s = rel_dyn;
3650 else if (s == hash_table->splt->output_section)
3651 {
3652 s = hash_table->splt;
3653 strip_zero_sized_plt = true;
3654 }
3655 else
3656 s = hash_table->srelplt;
3657 s->flags |= SEC_EXCLUDE;
3658 s->output_section = bfd_abs_section_ptr;
3659 }
3660 else
3661 pp = &s->next;
3662
3663 if (strip_zero_sized_plt && sdynamic->size != 0)
3664 for (extdyn = sdynamic->contents;
3665 extdyn < sdynamic->contents + sdynamic->size;
3666 extdyn = next)
3667 {
3668 next = extdyn + bed->s->sizeof_dyn;
3669 swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3670 switch (dyn.d_tag)
3671 {
3672 default:
3673 break;
3674 case DT_JMPREL:
3675 case DT_PLTRELSZ:
3676 case DT_PLTREL:
3677 /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
3678 the procedure linkage table (the .plt section) has been
3679 removed. */
3680 memmove (extdyn, next,
3681 sdynamic->size - (next - sdynamic->contents));
3682 next = extdyn;
3683 }
3684 }
3685
3686 if (strip_zero_sized)
3687 {
3688 /* Regenerate program headers. */
3689 elf_seg_map (info->output_bfd) = NULL;
3690 return _bfd_elf_map_sections_to_segments (info->output_bfd, info,
3691 NULL);
3692 }
3693
3694 return true;
3695 }
3696
3697 /* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error,
3698 1 if a DT_NEEDED tag already exists, and 0 on success. */
3699
3700 int
3701 bfd_elf_add_dt_needed_tag (bfd *abfd, struct bfd_link_info *info)
3702 {
3703 struct elf_link_hash_table *hash_table;
3704 size_t strindex;
3705 const char *soname;
3706
3707 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3708 return -1;
3709
3710 hash_table = elf_hash_table (info);
3711 soname = elf_dt_name (abfd);
3712 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, false);
3713 if (strindex == (size_t) -1)
3714 return -1;
3715
3716 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3717 {
3718 asection *sdyn;
3719 const struct elf_backend_data *bed;
3720 bfd_byte *extdyn;
3721
3722 bed = get_elf_backend_data (hash_table->dynobj);
3723 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3724 if (sdyn != NULL && sdyn->size != 0)
3725 for (extdyn = sdyn->contents;
3726 extdyn < sdyn->contents + sdyn->size;
3727 extdyn += bed->s->sizeof_dyn)
3728 {
3729 Elf_Internal_Dyn dyn;
3730
3731 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3732 if (dyn.d_tag == DT_NEEDED
3733 && dyn.d_un.d_val == strindex)
3734 {
3735 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3736 return 1;
3737 }
3738 }
3739 }
3740
3741 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3742 return -1;
3743
3744 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3745 return -1;
3746
3747 return 0;
3748 }
3749
3750 /* Return true if SONAME is on the needed list between NEEDED and STOP
3751 (or the end of list if STOP is NULL), and needed by a library that
3752 will be loaded. */
3753
3754 static bool
3755 on_needed_list (const char *soname,
3756 struct bfd_link_needed_list *needed,
3757 struct bfd_link_needed_list *stop)
3758 {
3759 struct bfd_link_needed_list *look;
3760 for (look = needed; look != stop; look = look->next)
3761 if (strcmp (soname, look->name) == 0
3762 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3763 /* If needed by a library that itself is not directly
3764 needed, recursively check whether that library is
3765 indirectly needed. Since we add DT_NEEDED entries to
3766 the end of the list, library dependencies appear after
3767 the library. Therefore search prior to the current
3768 LOOK, preventing possible infinite recursion. */
3769 || on_needed_list (elf_dt_name (look->by), needed, look)))
3770 return true;
3771
3772 return false;
3773 }
3774
3775 /* Sort symbol by value, section, size, and type. */
3776 static int
3777 elf_sort_symbol (const void *arg1, const void *arg2)
3778 {
3779 const struct elf_link_hash_entry *h1;
3780 const struct elf_link_hash_entry *h2;
3781 bfd_signed_vma vdiff;
3782 int sdiff;
3783 const char *n1;
3784 const char *n2;
3785
3786 h1 = *(const struct elf_link_hash_entry **) arg1;
3787 h2 = *(const struct elf_link_hash_entry **) arg2;
3788 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3789 if (vdiff != 0)
3790 return vdiff > 0 ? 1 : -1;
3791
3792 sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3793 if (sdiff != 0)
3794 return sdiff;
3795
3796 /* Sort so that sized symbols are selected over zero size symbols. */
3797 vdiff = h1->size - h2->size;
3798 if (vdiff != 0)
3799 return vdiff > 0 ? 1 : -1;
3800
3801 /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */
3802 if (h1->type != h2->type)
3803 return h1->type - h2->type;
3804
3805 /* If symbols are properly sized and typed, and multiple strong
3806 aliases are not defined in a shared library by the user we
3807 shouldn't get here. Unfortunately linker script symbols like
3808 __bss_start sometimes match a user symbol defined at the start of
3809 .bss without proper size and type. We'd like to preference the
3810 user symbol over reserved system symbols. Sort on leading
3811 underscores. */
3812 n1 = h1->root.root.string;
3813 n2 = h2->root.root.string;
3814 while (*n1 == *n2)
3815 {
3816 if (*n1 == 0)
3817 break;
3818 ++n1;
3819 ++n2;
3820 }
3821 if (*n1 == '_')
3822 return -1;
3823 if (*n2 == '_')
3824 return 1;
3825
3826 /* Final sort on name selects user symbols like '_u' over reserved
3827 system symbols like '_Z' and also will avoid qsort instability. */
3828 return *n1 - *n2;
3829 }
3830
3831 /* This function is used to adjust offsets into .dynstr for
3832 dynamic symbols. This is called via elf_link_hash_traverse. */
3833
3834 static bool
3835 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3836 {
3837 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3838
3839 if (h->dynindx != -1)
3840 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3841 return true;
3842 }
3843
3844 /* Assign string offsets in .dynstr, update all structures referencing
3845 them. */
3846
3847 static bool
3848 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3849 {
3850 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3851 struct elf_link_local_dynamic_entry *entry;
3852 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3853 bfd *dynobj = hash_table->dynobj;
3854 asection *sdyn;
3855 bfd_size_type size;
3856 const struct elf_backend_data *bed;
3857 bfd_byte *extdyn;
3858
3859 _bfd_elf_strtab_finalize (dynstr);
3860 size = _bfd_elf_strtab_size (dynstr);
3861
3862 /* Allow the linker to examine the dynsymtab now it's fully populated. */
3863
3864 if (info->callbacks->examine_strtab)
3865 info->callbacks->examine_strtab (dynstr);
3866
3867 bed = get_elf_backend_data (dynobj);
3868 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3869 BFD_ASSERT (sdyn != NULL);
3870
3871 /* Update all .dynamic entries referencing .dynstr strings. */
3872 for (extdyn = sdyn->contents;
3873 extdyn < PTR_ADD (sdyn->contents, sdyn->size);
3874 extdyn += bed->s->sizeof_dyn)
3875 {
3876 Elf_Internal_Dyn dyn;
3877
3878 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3879 switch (dyn.d_tag)
3880 {
3881 case DT_STRSZ:
3882 dyn.d_un.d_val = size;
3883 break;
3884 case DT_NEEDED:
3885 case DT_SONAME:
3886 case DT_RPATH:
3887 case DT_RUNPATH:
3888 case DT_FILTER:
3889 case DT_AUXILIARY:
3890 case DT_AUDIT:
3891 case DT_DEPAUDIT:
3892 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3893 break;
3894 default:
3895 continue;
3896 }
3897 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3898 }
3899
3900 /* Now update local dynamic symbols. */
3901 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3902 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3903 entry->isym.st_name);
3904
3905 /* And the rest of dynamic symbols. */
3906 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3907
3908 /* Adjust version definitions. */
3909 if (elf_tdata (output_bfd)->cverdefs)
3910 {
3911 asection *s;
3912 bfd_byte *p;
3913 size_t i;
3914 Elf_Internal_Verdef def;
3915 Elf_Internal_Verdaux defaux;
3916
3917 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3918 p = s->contents;
3919 do
3920 {
3921 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3922 &def);
3923 p += sizeof (Elf_External_Verdef);
3924 if (def.vd_aux != sizeof (Elf_External_Verdef))
3925 continue;
3926 for (i = 0; i < def.vd_cnt; ++i)
3927 {
3928 _bfd_elf_swap_verdaux_in (output_bfd,
3929 (Elf_External_Verdaux *) p, &defaux);
3930 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3931 defaux.vda_name);
3932 _bfd_elf_swap_verdaux_out (output_bfd,
3933 &defaux, (Elf_External_Verdaux *) p);
3934 p += sizeof (Elf_External_Verdaux);
3935 }
3936 }
3937 while (def.vd_next);
3938 }
3939
3940 /* Adjust version references. */
3941 if (elf_tdata (output_bfd)->verref)
3942 {
3943 asection *s;
3944 bfd_byte *p;
3945 size_t i;
3946 Elf_Internal_Verneed need;
3947 Elf_Internal_Vernaux needaux;
3948
3949 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3950 p = s->contents;
3951 do
3952 {
3953 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3954 &need);
3955 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3956 _bfd_elf_swap_verneed_out (output_bfd, &need,
3957 (Elf_External_Verneed *) p);
3958 p += sizeof (Elf_External_Verneed);
3959 for (i = 0; i < need.vn_cnt; ++i)
3960 {
3961 _bfd_elf_swap_vernaux_in (output_bfd,
3962 (Elf_External_Vernaux *) p, &needaux);
3963 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3964 needaux.vna_name);
3965 _bfd_elf_swap_vernaux_out (output_bfd,
3966 &needaux,
3967 (Elf_External_Vernaux *) p);
3968 p += sizeof (Elf_External_Vernaux);
3969 }
3970 }
3971 while (need.vn_next);
3972 }
3973
3974 return true;
3975 }
3976 \f
3977 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3978 The default is to only match when the INPUT and OUTPUT are exactly
3979 the same target. */
3980
3981 bool
3982 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3983 const bfd_target *output)
3984 {
3985 return input == output;
3986 }
3987
3988 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3989 This version is used when different targets for the same architecture
3990 are virtually identical. */
3991
3992 bool
3993 _bfd_elf_relocs_compatible (const bfd_target *input,
3994 const bfd_target *output)
3995 {
3996 const struct elf_backend_data *obed, *ibed;
3997
3998 if (input == output)
3999 return true;
4000
4001 ibed = xvec_get_elf_backend_data (input);
4002 obed = xvec_get_elf_backend_data (output);
4003
4004 if (ibed->arch != obed->arch)
4005 return false;
4006
4007 /* If both backends are using this function, deem them compatible. */
4008 return ibed->relocs_compatible == obed->relocs_compatible;
4009 }
4010
4011 /* Make a special call to the linker "notice" function to tell it that
4012 we are about to handle an as-needed lib, or have finished
4013 processing the lib. */
4014
4015 bool
4016 _bfd_elf_notice_as_needed (bfd *ibfd,
4017 struct bfd_link_info *info,
4018 enum notice_asneeded_action act)
4019 {
4020 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
4021 }
4022
4023 /* Call ACTION on each relocation in an ELF object file. */
4024
4025 bool
4026 _bfd_elf_link_iterate_on_relocs
4027 (bfd *abfd, struct bfd_link_info *info,
4028 bool (*action) (bfd *, struct bfd_link_info *, asection *,
4029 const Elf_Internal_Rela *))
4030 {
4031 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4032 struct elf_link_hash_table *htab = elf_hash_table (info);
4033
4034 /* If this object is the same format as the output object, and it is
4035 not a shared library, then let the backend look through the
4036 relocs.
4037
4038 This is required to build global offset table entries and to
4039 arrange for dynamic relocs. It is not required for the
4040 particular common case of linking non PIC code, even when linking
4041 against shared libraries, but unfortunately there is no way of
4042 knowing whether an object file has been compiled PIC or not.
4043 Looking through the relocs is not particularly time consuming.
4044 The problem is that we must either (1) keep the relocs in memory,
4045 which causes the linker to require additional runtime memory or
4046 (2) read the relocs twice from the input file, which wastes time.
4047 This would be a good case for using mmap.
4048
4049 I have no idea how to handle linking PIC code into a file of a
4050 different format. It probably can't be done. */
4051 if ((abfd->flags & DYNAMIC) == 0
4052 && is_elf_hash_table (&htab->root)
4053 && elf_object_id (abfd) == elf_hash_table_id (htab)
4054 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4055 {
4056 asection *o;
4057
4058 for (o = abfd->sections; o != NULL; o = o->next)
4059 {
4060 Elf_Internal_Rela *internal_relocs;
4061 bool ok;
4062
4063 /* Don't check relocations in excluded sections. Don't do
4064 anything special with non-loaded, non-alloced sections.
4065 In particular, any relocs in such sections should not
4066 affect GOT and PLT reference counting (ie. we don't
4067 allow them to create GOT or PLT entries), there's no
4068 possibility or desire to optimize TLS relocs, and
4069 there's not much point in propagating relocs to shared
4070 libs that the dynamic linker won't relocate. */
4071 if ((o->flags & SEC_ALLOC) == 0
4072 || (o->flags & SEC_RELOC) == 0
4073 || (o->flags & SEC_EXCLUDE) != 0
4074 || o->reloc_count == 0
4075 || ((info->strip == strip_all || info->strip == strip_debugger)
4076 && (o->flags & SEC_DEBUGGING) != 0)
4077 || bfd_is_abs_section (o->output_section))
4078 continue;
4079
4080 internal_relocs = _bfd_elf_link_info_read_relocs (abfd, info,
4081 o, NULL,
4082 NULL,
4083 _bfd_link_keep_memory (info));
4084 if (internal_relocs == NULL)
4085 return false;
4086
4087 ok = action (abfd, info, o, internal_relocs);
4088
4089 if (elf_section_data (o)->relocs != internal_relocs)
4090 free (internal_relocs);
4091
4092 if (! ok)
4093 return false;
4094 }
4095 }
4096
4097 return true;
4098 }
4099
4100 /* Check relocations in an ELF object file. This is called after
4101 all input files have been opened. */
4102
4103 bool
4104 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
4105 {
4106 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4107 if (bed->check_relocs != NULL)
4108 return _bfd_elf_link_iterate_on_relocs (abfd, info,
4109 bed->check_relocs);
4110 return true;
4111 }
4112
4113 /* Add symbols from an ELF object file to the linker hash table. */
4114
4115 static bool
4116 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
4117 {
4118 Elf_Internal_Ehdr *ehdr;
4119 Elf_Internal_Shdr *hdr;
4120 size_t symcount;
4121 size_t extsymcount;
4122 size_t extsymoff;
4123 struct elf_link_hash_entry **sym_hash;
4124 bool dynamic;
4125 Elf_External_Versym *extversym = NULL;
4126 Elf_External_Versym *extversym_end = NULL;
4127 Elf_External_Versym *ever;
4128 struct elf_link_hash_entry *weaks;
4129 struct elf_link_hash_entry **nondeflt_vers = NULL;
4130 size_t nondeflt_vers_cnt = 0;
4131 Elf_Internal_Sym *isymbuf = NULL;
4132 Elf_Internal_Sym *isym;
4133 Elf_Internal_Sym *isymend;
4134 const struct elf_backend_data *bed;
4135 bool add_needed;
4136 struct elf_link_hash_table *htab;
4137 void *alloc_mark = NULL;
4138 struct bfd_hash_entry **old_table = NULL;
4139 unsigned int old_size = 0;
4140 unsigned int old_count = 0;
4141 void *old_tab = NULL;
4142 void *old_ent;
4143 struct bfd_link_hash_entry *old_undefs = NULL;
4144 struct bfd_link_hash_entry *old_undefs_tail = NULL;
4145 void *old_strtab = NULL;
4146 size_t tabsize = 0;
4147 asection *s;
4148 bool just_syms;
4149
4150 htab = elf_hash_table (info);
4151 bed = get_elf_backend_data (abfd);
4152
4153 if ((abfd->flags & DYNAMIC) == 0)
4154 dynamic = false;
4155 else
4156 {
4157 dynamic = true;
4158
4159 /* You can't use -r against a dynamic object. Also, there's no
4160 hope of using a dynamic object which does not exactly match
4161 the format of the output file. */
4162 if (bfd_link_relocatable (info)
4163 || !is_elf_hash_table (&htab->root)
4164 || info->output_bfd->xvec != abfd->xvec)
4165 {
4166 if (bfd_link_relocatable (info))
4167 bfd_set_error (bfd_error_invalid_operation);
4168 else
4169 bfd_set_error (bfd_error_wrong_format);
4170 goto error_return;
4171 }
4172 }
4173
4174 ehdr = elf_elfheader (abfd);
4175 if (info->warn_alternate_em
4176 && bed->elf_machine_code != ehdr->e_machine
4177 && ((bed->elf_machine_alt1 != 0
4178 && ehdr->e_machine == bed->elf_machine_alt1)
4179 || (bed->elf_machine_alt2 != 0
4180 && ehdr->e_machine == bed->elf_machine_alt2)))
4181 _bfd_error_handler
4182 /* xgettext:c-format */
4183 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
4184 ehdr->e_machine, abfd, bed->elf_machine_code);
4185
4186 /* As a GNU extension, any input sections which are named
4187 .gnu.warning.SYMBOL are treated as warning symbols for the given
4188 symbol. This differs from .gnu.warning sections, which generate
4189 warnings when they are included in an output file. */
4190 /* PR 12761: Also generate this warning when building shared libraries. */
4191 for (s = abfd->sections; s != NULL; s = s->next)
4192 {
4193 const char *name;
4194
4195 name = bfd_section_name (s);
4196 if (startswith (name, ".gnu.warning."))
4197 {
4198 char *msg;
4199 bfd_size_type sz;
4200
4201 name += sizeof ".gnu.warning." - 1;
4202
4203 /* If this is a shared object, then look up the symbol
4204 in the hash table. If it is there, and it is already
4205 been defined, then we will not be using the entry
4206 from this shared object, so we don't need to warn.
4207 FIXME: If we see the definition in a regular object
4208 later on, we will warn, but we shouldn't. The only
4209 fix is to keep track of what warnings we are supposed
4210 to emit, and then handle them all at the end of the
4211 link. */
4212 if (dynamic)
4213 {
4214 struct elf_link_hash_entry *h;
4215
4216 h = elf_link_hash_lookup (htab, name, false, false, true);
4217
4218 /* FIXME: What about bfd_link_hash_common? */
4219 if (h != NULL
4220 && (h->root.type == bfd_link_hash_defined
4221 || h->root.type == bfd_link_hash_defweak))
4222 continue;
4223 }
4224
4225 sz = s->size;
4226 msg = (char *) bfd_alloc (abfd, sz + 1);
4227 if (msg == NULL)
4228 goto error_return;
4229
4230 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
4231 goto error_return;
4232
4233 msg[sz] = '\0';
4234
4235 if (! (_bfd_generic_link_add_one_symbol
4236 (info, abfd, name, BSF_WARNING, s, 0, msg,
4237 false, bed->collect, NULL)))
4238 goto error_return;
4239
4240 if (bfd_link_executable (info))
4241 {
4242 /* Clobber the section size so that the warning does
4243 not get copied into the output file. */
4244 s->size = 0;
4245
4246 /* Also set SEC_EXCLUDE, so that symbols defined in
4247 the warning section don't get copied to the output. */
4248 s->flags |= SEC_EXCLUDE;
4249 }
4250 }
4251 }
4252
4253 just_syms = ((s = abfd->sections) != NULL
4254 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
4255
4256 add_needed = true;
4257 if (! dynamic)
4258 {
4259 /* If we are creating a shared library, create all the dynamic
4260 sections immediately. We need to attach them to something,
4261 so we attach them to this BFD, provided it is the right
4262 format and is not from ld --just-symbols. Always create the
4263 dynamic sections for -E/--dynamic-list. FIXME: If there
4264 are no input BFD's of the same format as the output, we can't
4265 make a shared library. */
4266 if (!just_syms
4267 && (bfd_link_pic (info)
4268 || (!bfd_link_relocatable (info)
4269 && info->nointerp
4270 && (info->export_dynamic || info->dynamic)))
4271 && is_elf_hash_table (&htab->root)
4272 && info->output_bfd->xvec == abfd->xvec
4273 && !htab->dynamic_sections_created)
4274 {
4275 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4276 goto error_return;
4277 }
4278 }
4279 else if (!is_elf_hash_table (&htab->root))
4280 goto error_return;
4281 else
4282 {
4283 const char *soname = NULL;
4284 char *audit = NULL;
4285 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
4286 const Elf_Internal_Phdr *phdr;
4287 struct elf_link_loaded_list *loaded_lib;
4288
4289 /* ld --just-symbols and dynamic objects don't mix very well.
4290 ld shouldn't allow it. */
4291 if (just_syms)
4292 abort ();
4293
4294 /* If this dynamic lib was specified on the command line with
4295 --as-needed in effect, then we don't want to add a DT_NEEDED
4296 tag unless the lib is actually used. Similary for libs brought
4297 in by another lib's DT_NEEDED. When --no-add-needed is used
4298 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4299 any dynamic library in DT_NEEDED tags in the dynamic lib at
4300 all. */
4301 add_needed = (elf_dyn_lib_class (abfd)
4302 & (DYN_AS_NEEDED | DYN_DT_NEEDED
4303 | DYN_NO_NEEDED)) == 0;
4304
4305 s = bfd_get_section_by_name (abfd, ".dynamic");
4306 if (s != NULL && s->size != 0)
4307 {
4308 bfd_byte *dynbuf;
4309 bfd_byte *extdyn;
4310 unsigned int elfsec;
4311 unsigned long shlink;
4312
4313 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
4314 {
4315 error_free_dyn:
4316 free (dynbuf);
4317 goto error_return;
4318 }
4319
4320 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
4321 if (elfsec == SHN_BAD)
4322 goto error_free_dyn;
4323 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4324
4325 for (extdyn = dynbuf;
4326 extdyn <= dynbuf + s->size - bed->s->sizeof_dyn;
4327 extdyn += bed->s->sizeof_dyn)
4328 {
4329 Elf_Internal_Dyn dyn;
4330
4331 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4332 if (dyn.d_tag == DT_SONAME)
4333 {
4334 unsigned int tagv = dyn.d_un.d_val;
4335 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4336 if (soname == NULL)
4337 goto error_free_dyn;
4338 }
4339 if (dyn.d_tag == DT_NEEDED)
4340 {
4341 struct bfd_link_needed_list *n, **pn;
4342 char *fnm, *anm;
4343 unsigned int tagv = dyn.d_un.d_val;
4344 size_t amt = sizeof (struct bfd_link_needed_list);
4345
4346 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4347 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4348 if (n == NULL || fnm == NULL)
4349 goto error_free_dyn;
4350 amt = strlen (fnm) + 1;
4351 anm = (char *) bfd_alloc (abfd, amt);
4352 if (anm == NULL)
4353 goto error_free_dyn;
4354 memcpy (anm, fnm, amt);
4355 n->name = anm;
4356 n->by = abfd;
4357 n->next = NULL;
4358 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4359 ;
4360 *pn = n;
4361 }
4362 if (dyn.d_tag == DT_RUNPATH)
4363 {
4364 struct bfd_link_needed_list *n, **pn;
4365 char *fnm, *anm;
4366 unsigned int tagv = dyn.d_un.d_val;
4367 size_t amt = sizeof (struct bfd_link_needed_list);
4368
4369 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4370 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4371 if (n == NULL || fnm == NULL)
4372 goto error_free_dyn;
4373 amt = strlen (fnm) + 1;
4374 anm = (char *) bfd_alloc (abfd, amt);
4375 if (anm == NULL)
4376 goto error_free_dyn;
4377 memcpy (anm, fnm, amt);
4378 n->name = anm;
4379 n->by = abfd;
4380 n->next = NULL;
4381 for (pn = & runpath;
4382 *pn != NULL;
4383 pn = &(*pn)->next)
4384 ;
4385 *pn = n;
4386 }
4387 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4388 if (!runpath && dyn.d_tag == DT_RPATH)
4389 {
4390 struct bfd_link_needed_list *n, **pn;
4391 char *fnm, *anm;
4392 unsigned int tagv = dyn.d_un.d_val;
4393 size_t amt = sizeof (struct bfd_link_needed_list);
4394
4395 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4396 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4397 if (n == NULL || fnm == NULL)
4398 goto error_free_dyn;
4399 amt = strlen (fnm) + 1;
4400 anm = (char *) bfd_alloc (abfd, amt);
4401 if (anm == NULL)
4402 goto error_free_dyn;
4403 memcpy (anm, fnm, amt);
4404 n->name = anm;
4405 n->by = abfd;
4406 n->next = NULL;
4407 for (pn = & rpath;
4408 *pn != NULL;
4409 pn = &(*pn)->next)
4410 ;
4411 *pn = n;
4412 }
4413 if (dyn.d_tag == DT_AUDIT)
4414 {
4415 unsigned int tagv = dyn.d_un.d_val;
4416 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4417 }
4418 if (dyn.d_tag == DT_FLAGS_1)
4419 elf_tdata (abfd)->is_pie = (dyn.d_un.d_val & DF_1_PIE) != 0;
4420 }
4421
4422 free (dynbuf);
4423 }
4424
4425 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4426 frees all more recently bfd_alloc'd blocks as well. */
4427 if (runpath)
4428 rpath = runpath;
4429
4430 if (rpath)
4431 {
4432 struct bfd_link_needed_list **pn;
4433 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4434 ;
4435 *pn = rpath;
4436 }
4437
4438 /* If we have a PT_GNU_RELRO program header, mark as read-only
4439 all sections contained fully therein. This makes relro
4440 shared library sections appear as they will at run-time. */
4441 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4442 while (phdr-- > elf_tdata (abfd)->phdr)
4443 if (phdr->p_type == PT_GNU_RELRO)
4444 {
4445 for (s = abfd->sections; s != NULL; s = s->next)
4446 {
4447 unsigned int opb = bfd_octets_per_byte (abfd, s);
4448
4449 if ((s->flags & SEC_ALLOC) != 0
4450 && s->vma * opb >= phdr->p_vaddr
4451 && s->vma * opb + s->size <= phdr->p_vaddr + phdr->p_memsz)
4452 s->flags |= SEC_READONLY;
4453 }
4454 break;
4455 }
4456
4457 /* We do not want to include any of the sections in a dynamic
4458 object in the output file. We hack by simply clobbering the
4459 list of sections in the BFD. This could be handled more
4460 cleanly by, say, a new section flag; the existing
4461 SEC_NEVER_LOAD flag is not the one we want, because that one
4462 still implies that the section takes up space in the output
4463 file. */
4464 bfd_section_list_clear (abfd);
4465
4466 /* Find the name to use in a DT_NEEDED entry that refers to this
4467 object. If the object has a DT_SONAME entry, we use it.
4468 Otherwise, if the generic linker stuck something in
4469 elf_dt_name, we use that. Otherwise, we just use the file
4470 name. */
4471 if (soname == NULL || *soname == '\0')
4472 {
4473 soname = elf_dt_name (abfd);
4474 if (soname == NULL || *soname == '\0')
4475 soname = bfd_get_filename (abfd);
4476 }
4477
4478 /* Save the SONAME because sometimes the linker emulation code
4479 will need to know it. */
4480 elf_dt_name (abfd) = soname;
4481
4482 /* If we have already included this dynamic object in the
4483 link, just ignore it. There is no reason to include a
4484 particular dynamic object more than once. */
4485 for (loaded_lib = htab->dyn_loaded;
4486 loaded_lib != NULL;
4487 loaded_lib = loaded_lib->next)
4488 {
4489 if (strcmp (elf_dt_name (loaded_lib->abfd), soname) == 0)
4490 return true;
4491 }
4492
4493 /* Create dynamic sections for backends that require that be done
4494 before setup_gnu_properties. */
4495 if (add_needed
4496 && !_bfd_elf_link_create_dynamic_sections (abfd, info))
4497 return false;
4498
4499 /* Save the DT_AUDIT entry for the linker emulation code. */
4500 elf_dt_audit (abfd) = audit;
4501 }
4502
4503 /* If this is a dynamic object, we always link against the .dynsym
4504 symbol table, not the .symtab symbol table. The dynamic linker
4505 will only see the .dynsym symbol table, so there is no reason to
4506 look at .symtab for a dynamic object. */
4507
4508 if (! dynamic || elf_dynsymtab (abfd) == 0)
4509 hdr = &elf_tdata (abfd)->symtab_hdr;
4510 else
4511 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4512
4513 symcount = hdr->sh_size / bed->s->sizeof_sym;
4514
4515 /* The sh_info field of the symtab header tells us where the
4516 external symbols start. We don't care about the local symbols at
4517 this point. */
4518 if (elf_bad_symtab (abfd))
4519 {
4520 extsymcount = symcount;
4521 extsymoff = 0;
4522 }
4523 else
4524 {
4525 extsymcount = symcount - hdr->sh_info;
4526 extsymoff = hdr->sh_info;
4527 }
4528
4529 sym_hash = elf_sym_hashes (abfd);
4530 if (extsymcount != 0)
4531 {
4532 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4533 NULL, NULL, NULL);
4534 if (isymbuf == NULL)
4535 goto error_return;
4536
4537 if (sym_hash == NULL)
4538 {
4539 /* We store a pointer to the hash table entry for each
4540 external symbol. */
4541 size_t amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4542 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4543 if (sym_hash == NULL)
4544 goto error_free_sym;
4545 elf_sym_hashes (abfd) = sym_hash;
4546 }
4547 }
4548
4549 if (dynamic)
4550 {
4551 /* Read in any version definitions. */
4552 if (!_bfd_elf_slurp_version_tables (abfd,
4553 info->default_imported_symver))
4554 goto error_free_sym;
4555
4556 /* Read in the symbol versions, but don't bother to convert them
4557 to internal format. */
4558 if (elf_dynversym (abfd) != 0)
4559 {
4560 Elf_Internal_Shdr *versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4561 bfd_size_type amt = versymhdr->sh_size;
4562
4563 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0)
4564 goto error_free_sym;
4565 extversym = (Elf_External_Versym *)
4566 _bfd_malloc_and_read (abfd, amt, amt);
4567 if (extversym == NULL)
4568 goto error_free_sym;
4569 extversym_end = extversym + amt / sizeof (*extversym);
4570 }
4571 }
4572
4573 /* If we are loading an as-needed shared lib, save the symbol table
4574 state before we start adding symbols. If the lib turns out
4575 to be unneeded, restore the state. */
4576 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4577 {
4578 unsigned int i;
4579 size_t entsize;
4580
4581 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4582 {
4583 struct bfd_hash_entry *p;
4584 struct elf_link_hash_entry *h;
4585
4586 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4587 {
4588 h = (struct elf_link_hash_entry *) p;
4589 entsize += htab->root.table.entsize;
4590 if (h->root.type == bfd_link_hash_warning)
4591 {
4592 entsize += htab->root.table.entsize;
4593 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4594 }
4595 if (h->root.type == bfd_link_hash_common)
4596 entsize += sizeof (*h->root.u.c.p);
4597 }
4598 }
4599
4600 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4601 old_tab = bfd_malloc (tabsize + entsize);
4602 if (old_tab == NULL)
4603 goto error_free_vers;
4604
4605 /* Remember the current objalloc pointer, so that all mem for
4606 symbols added can later be reclaimed. */
4607 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4608 if (alloc_mark == NULL)
4609 goto error_free_vers;
4610
4611 /* Make a special call to the linker "notice" function to
4612 tell it that we are about to handle an as-needed lib. */
4613 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4614 goto error_free_vers;
4615
4616 /* Clone the symbol table. Remember some pointers into the
4617 symbol table, and dynamic symbol count. */
4618 old_ent = (char *) old_tab + tabsize;
4619 memcpy (old_tab, htab->root.table.table, tabsize);
4620 old_undefs = htab->root.undefs;
4621 old_undefs_tail = htab->root.undefs_tail;
4622 old_table = htab->root.table.table;
4623 old_size = htab->root.table.size;
4624 old_count = htab->root.table.count;
4625 old_strtab = NULL;
4626 if (htab->dynstr != NULL)
4627 {
4628 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4629 if (old_strtab == NULL)
4630 goto error_free_vers;
4631 }
4632
4633 for (i = 0; i < htab->root.table.size; i++)
4634 {
4635 struct bfd_hash_entry *p;
4636 struct elf_link_hash_entry *h;
4637
4638 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4639 {
4640 h = (struct elf_link_hash_entry *) p;
4641 memcpy (old_ent, h, htab->root.table.entsize);
4642 old_ent = (char *) old_ent + htab->root.table.entsize;
4643 if (h->root.type == bfd_link_hash_warning)
4644 {
4645 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4646 memcpy (old_ent, h, htab->root.table.entsize);
4647 old_ent = (char *) old_ent + htab->root.table.entsize;
4648 }
4649 if (h->root.type == bfd_link_hash_common)
4650 {
4651 memcpy (old_ent, h->root.u.c.p, sizeof (*h->root.u.c.p));
4652 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
4653 }
4654 }
4655 }
4656 }
4657
4658 weaks = NULL;
4659 if (extversym == NULL)
4660 ever = NULL;
4661 else if (extversym + extsymoff < extversym_end)
4662 ever = extversym + extsymoff;
4663 else
4664 {
4665 /* xgettext:c-format */
4666 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4667 abfd, (long) extsymoff,
4668 (long) (extversym_end - extversym) / sizeof (* extversym));
4669 bfd_set_error (bfd_error_bad_value);
4670 goto error_free_vers;
4671 }
4672
4673 if (!bfd_link_relocatable (info)
4674 && abfd->lto_slim_object)
4675 {
4676 _bfd_error_handler
4677 (_("%pB: plugin needed to handle lto object"), abfd);
4678 }
4679
4680 for (isym = isymbuf, isymend = PTR_ADD (isymbuf, extsymcount);
4681 isym < isymend;
4682 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4683 {
4684 int bind;
4685 bfd_vma value;
4686 asection *sec, *new_sec;
4687 flagword flags;
4688 const char *name;
4689 struct elf_link_hash_entry *h;
4690 struct elf_link_hash_entry *hi;
4691 bool definition;
4692 bool size_change_ok;
4693 bool type_change_ok;
4694 bool new_weak;
4695 bool old_weak;
4696 bfd *override;
4697 bool common;
4698 bool discarded;
4699 unsigned int old_alignment;
4700 unsigned int shindex;
4701 bfd *old_bfd;
4702 bool matched;
4703
4704 override = NULL;
4705
4706 flags = BSF_NO_FLAGS;
4707 sec = NULL;
4708 value = isym->st_value;
4709 common = bed->common_definition (isym);
4710 if (common && info->inhibit_common_definition)
4711 {
4712 /* Treat common symbol as undefined for --no-define-common. */
4713 isym->st_shndx = SHN_UNDEF;
4714 common = false;
4715 }
4716 discarded = false;
4717
4718 bind = ELF_ST_BIND (isym->st_info);
4719 switch (bind)
4720 {
4721 case STB_LOCAL:
4722 /* This should be impossible, since ELF requires that all
4723 global symbols follow all local symbols, and that sh_info
4724 point to the first global symbol. Unfortunately, Irix 5
4725 screws this up. */
4726 if (elf_bad_symtab (abfd))
4727 continue;
4728
4729 /* If we aren't prepared to handle locals within the globals
4730 then we'll likely segfault on a NULL symbol hash if the
4731 symbol is ever referenced in relocations. */
4732 shindex = elf_elfheader (abfd)->e_shstrndx;
4733 name = bfd_elf_string_from_elf_section (abfd, shindex, hdr->sh_name);
4734 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4735 " (>= sh_info of %lu)"),
4736 abfd, name, (long) (isym - isymbuf + extsymoff),
4737 (long) extsymoff);
4738
4739 /* Dynamic object relocations are not processed by ld, so
4740 ld won't run into the problem mentioned above. */
4741 if (dynamic)
4742 continue;
4743 bfd_set_error (bfd_error_bad_value);
4744 goto error_free_vers;
4745
4746 case STB_GLOBAL:
4747 if (isym->st_shndx != SHN_UNDEF && !common)
4748 flags = BSF_GLOBAL;
4749 break;
4750
4751 case STB_WEAK:
4752 flags = BSF_WEAK;
4753 break;
4754
4755 case STB_GNU_UNIQUE:
4756 flags = BSF_GNU_UNIQUE;
4757 break;
4758
4759 default:
4760 /* Leave it up to the processor backend. */
4761 break;
4762 }
4763
4764 if (isym->st_shndx == SHN_UNDEF)
4765 sec = bfd_und_section_ptr;
4766 else if (isym->st_shndx == SHN_ABS)
4767 sec = bfd_abs_section_ptr;
4768 else if (isym->st_shndx == SHN_COMMON)
4769 {
4770 sec = bfd_com_section_ptr;
4771 /* What ELF calls the size we call the value. What ELF
4772 calls the value we call the alignment. */
4773 value = isym->st_size;
4774 }
4775 else
4776 {
4777 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4778 if (sec == NULL)
4779 sec = bfd_abs_section_ptr;
4780 else if (discarded_section (sec))
4781 {
4782 /* Symbols from discarded section are undefined. We keep
4783 its visibility. */
4784 sec = bfd_und_section_ptr;
4785 discarded = true;
4786 isym->st_shndx = SHN_UNDEF;
4787 }
4788 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4789 value -= sec->vma;
4790 }
4791
4792 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4793 isym->st_name);
4794 if (name == NULL)
4795 goto error_free_vers;
4796
4797 if (isym->st_shndx == SHN_COMMON
4798 && (abfd->flags & BFD_PLUGIN) != 0)
4799 {
4800 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4801
4802 if (xc == NULL)
4803 {
4804 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4805 | SEC_EXCLUDE);
4806 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4807 if (xc == NULL)
4808 goto error_free_vers;
4809 }
4810 sec = xc;
4811 }
4812 else if (isym->st_shndx == SHN_COMMON
4813 && ELF_ST_TYPE (isym->st_info) == STT_TLS
4814 && !bfd_link_relocatable (info))
4815 {
4816 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4817
4818 if (tcomm == NULL)
4819 {
4820 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4821 | SEC_LINKER_CREATED);
4822 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4823 if (tcomm == NULL)
4824 goto error_free_vers;
4825 }
4826 sec = tcomm;
4827 }
4828 else if (bed->elf_add_symbol_hook)
4829 {
4830 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4831 &sec, &value))
4832 goto error_free_vers;
4833
4834 /* The hook function sets the name to NULL if this symbol
4835 should be skipped for some reason. */
4836 if (name == NULL)
4837 continue;
4838 }
4839
4840 /* Sanity check that all possibilities were handled. */
4841 if (sec == NULL)
4842 abort ();
4843
4844 /* Silently discard TLS symbols from --just-syms. There's
4845 no way to combine a static TLS block with a new TLS block
4846 for this executable. */
4847 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4848 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4849 continue;
4850
4851 if (bfd_is_und_section (sec)
4852 || bfd_is_com_section (sec))
4853 definition = false;
4854 else
4855 definition = true;
4856
4857 size_change_ok = false;
4858 type_change_ok = bed->type_change_ok;
4859 old_weak = false;
4860 matched = false;
4861 old_alignment = 0;
4862 old_bfd = NULL;
4863 new_sec = sec;
4864
4865 if (is_elf_hash_table (&htab->root))
4866 {
4867 Elf_Internal_Versym iver;
4868 unsigned int vernum = 0;
4869 bool skip;
4870
4871 if (ever == NULL)
4872 {
4873 if (info->default_imported_symver)
4874 /* Use the default symbol version created earlier. */
4875 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4876 else
4877 iver.vs_vers = 0;
4878 }
4879 else if (ever >= extversym_end)
4880 {
4881 /* xgettext:c-format */
4882 _bfd_error_handler (_("%pB: not enough version information"),
4883 abfd);
4884 bfd_set_error (bfd_error_bad_value);
4885 goto error_free_vers;
4886 }
4887 else
4888 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4889
4890 vernum = iver.vs_vers & VERSYM_VERSION;
4891
4892 /* If this is a hidden symbol, or if it is not version
4893 1, we append the version name to the symbol name.
4894 However, we do not modify a non-hidden absolute symbol
4895 if it is not a function, because it might be the version
4896 symbol itself. FIXME: What if it isn't? */
4897 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4898 || (vernum > 1
4899 && (!bfd_is_abs_section (sec)
4900 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4901 {
4902 const char *verstr;
4903 size_t namelen, verlen, newlen;
4904 char *newname, *p;
4905
4906 if (isym->st_shndx != SHN_UNDEF)
4907 {
4908 if (vernum > elf_tdata (abfd)->cverdefs)
4909 verstr = NULL;
4910 else if (vernum > 1)
4911 verstr =
4912 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4913 else
4914 verstr = "";
4915
4916 if (verstr == NULL)
4917 {
4918 _bfd_error_handler
4919 /* xgettext:c-format */
4920 (_("%pB: %s: invalid version %u (max %d)"),
4921 abfd, name, vernum,
4922 elf_tdata (abfd)->cverdefs);
4923 bfd_set_error (bfd_error_bad_value);
4924 goto error_free_vers;
4925 }
4926 }
4927 else
4928 {
4929 /* We cannot simply test for the number of
4930 entries in the VERNEED section since the
4931 numbers for the needed versions do not start
4932 at 0. */
4933 Elf_Internal_Verneed *t;
4934
4935 verstr = NULL;
4936 for (t = elf_tdata (abfd)->verref;
4937 t != NULL;
4938 t = t->vn_nextref)
4939 {
4940 Elf_Internal_Vernaux *a;
4941
4942 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4943 {
4944 if (a->vna_other == vernum)
4945 {
4946 verstr = a->vna_nodename;
4947 break;
4948 }
4949 }
4950 if (a != NULL)
4951 break;
4952 }
4953 if (verstr == NULL)
4954 {
4955 _bfd_error_handler
4956 /* xgettext:c-format */
4957 (_("%pB: %s: invalid needed version %d"),
4958 abfd, name, vernum);
4959 bfd_set_error (bfd_error_bad_value);
4960 goto error_free_vers;
4961 }
4962 }
4963
4964 namelen = strlen (name);
4965 verlen = strlen (verstr);
4966 newlen = namelen + verlen + 2;
4967 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4968 && isym->st_shndx != SHN_UNDEF)
4969 ++newlen;
4970
4971 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4972 if (newname == NULL)
4973 goto error_free_vers;
4974 memcpy (newname, name, namelen);
4975 p = newname + namelen;
4976 *p++ = ELF_VER_CHR;
4977 /* If this is a defined non-hidden version symbol,
4978 we add another @ to the name. This indicates the
4979 default version of the symbol. */
4980 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4981 && isym->st_shndx != SHN_UNDEF)
4982 *p++ = ELF_VER_CHR;
4983 memcpy (p, verstr, verlen + 1);
4984
4985 name = newname;
4986 }
4987
4988 /* If this symbol has default visibility and the user has
4989 requested we not re-export it, then mark it as hidden. */
4990 if (!bfd_is_und_section (sec)
4991 && !dynamic
4992 && abfd->no_export
4993 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4994 isym->st_other = (STV_HIDDEN
4995 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4996
4997 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4998 sym_hash, &old_bfd, &old_weak,
4999 &old_alignment, &skip, &override,
5000 &type_change_ok, &size_change_ok,
5001 &matched))
5002 goto error_free_vers;
5003
5004 if (skip)
5005 continue;
5006
5007 /* Override a definition only if the new symbol matches the
5008 existing one. */
5009 if (override && matched)
5010 definition = false;
5011
5012 h = *sym_hash;
5013 while (h->root.type == bfd_link_hash_indirect
5014 || h->root.type == bfd_link_hash_warning)
5015 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5016
5017 if (h->versioned != unversioned
5018 && elf_tdata (abfd)->verdef != NULL
5019 && vernum > 1
5020 && definition)
5021 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
5022 }
5023
5024 if (! (_bfd_generic_link_add_one_symbol
5025 (info, override ? override : abfd, name, flags, sec, value,
5026 NULL, false, bed->collect,
5027 (struct bfd_link_hash_entry **) sym_hash)))
5028 goto error_free_vers;
5029
5030 h = *sym_hash;
5031 /* We need to make sure that indirect symbol dynamic flags are
5032 updated. */
5033 hi = h;
5034 while (h->root.type == bfd_link_hash_indirect
5035 || h->root.type == bfd_link_hash_warning)
5036 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5037
5038 *sym_hash = h;
5039
5040 /* Setting the index to -3 tells elf_link_output_extsym that
5041 this symbol is defined in a discarded section. */
5042 if (discarded && is_elf_hash_table (&htab->root))
5043 h->indx = -3;
5044
5045 new_weak = (flags & BSF_WEAK) != 0;
5046 if (dynamic
5047 && definition
5048 && new_weak
5049 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
5050 && is_elf_hash_table (&htab->root)
5051 && h->u.alias == NULL)
5052 {
5053 /* Keep a list of all weak defined non function symbols from
5054 a dynamic object, using the alias field. Later in this
5055 function we will set the alias field to the correct
5056 value. We only put non-function symbols from dynamic
5057 objects on this list, because that happens to be the only
5058 time we need to know the normal symbol corresponding to a
5059 weak symbol, and the information is time consuming to
5060 figure out. If the alias field is not already NULL,
5061 then this symbol was already defined by some previous
5062 dynamic object, and we will be using that previous
5063 definition anyhow. */
5064
5065 h->u.alias = weaks;
5066 weaks = h;
5067 }
5068
5069 /* Set the alignment of a common symbol. */
5070 if ((common || bfd_is_com_section (sec))
5071 && h->root.type == bfd_link_hash_common)
5072 {
5073 unsigned int align;
5074
5075 if (common)
5076 align = bfd_log2 (isym->st_value);
5077 else
5078 {
5079 /* The new symbol is a common symbol in a shared object.
5080 We need to get the alignment from the section. */
5081 align = new_sec->alignment_power;
5082 }
5083 if (align > old_alignment)
5084 h->root.u.c.p->alignment_power = align;
5085 else
5086 h->root.u.c.p->alignment_power = old_alignment;
5087 }
5088
5089 if (is_elf_hash_table (&htab->root))
5090 {
5091 /* Set a flag in the hash table entry indicating the type of
5092 reference or definition we just found. A dynamic symbol
5093 is one which is referenced or defined by both a regular
5094 object and a shared object. */
5095 bool dynsym = false;
5096
5097 /* Plugin symbols aren't normal. Don't set def/ref flags. */
5098 if ((abfd->flags & BFD_PLUGIN) != 0)
5099 {
5100 /* Except for this flag to track nonweak references. */
5101 if (!definition
5102 && bind != STB_WEAK)
5103 h->ref_ir_nonweak = 1;
5104 }
5105 else if (!dynamic)
5106 {
5107 if (! definition)
5108 {
5109 h->ref_regular = 1;
5110 if (bind != STB_WEAK)
5111 h->ref_regular_nonweak = 1;
5112 }
5113 else
5114 {
5115 h->def_regular = 1;
5116 if (h->def_dynamic)
5117 {
5118 h->def_dynamic = 0;
5119 h->ref_dynamic = 1;
5120 }
5121 }
5122 }
5123 else
5124 {
5125 if (! definition)
5126 {
5127 h->ref_dynamic = 1;
5128 hi->ref_dynamic = 1;
5129 }
5130 else
5131 {
5132 h->def_dynamic = 1;
5133 hi->def_dynamic = 1;
5134 }
5135 }
5136
5137 /* If an indirect symbol has been forced local, don't
5138 make the real symbol dynamic. */
5139 if (h != hi && hi->forced_local)
5140 ;
5141 else if (!dynamic)
5142 {
5143 if (bfd_link_dll (info)
5144 || h->def_dynamic
5145 || h->ref_dynamic)
5146 dynsym = true;
5147 }
5148 else
5149 {
5150 if (h->def_regular
5151 || h->ref_regular
5152 || (h->is_weakalias
5153 && weakdef (h)->dynindx != -1))
5154 dynsym = true;
5155 }
5156
5157 /* Check to see if we need to add an indirect symbol for
5158 the default name. */
5159 if ((definition
5160 || (!override && h->root.type == bfd_link_hash_common))
5161 && !(hi != h
5162 && hi->versioned == versioned_hidden))
5163 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
5164 sec, value, &old_bfd, &dynsym))
5165 goto error_free_vers;
5166
5167 /* Check the alignment when a common symbol is involved. This
5168 can change when a common symbol is overridden by a normal
5169 definition or a common symbol is ignored due to the old
5170 normal definition. We need to make sure the maximum
5171 alignment is maintained. */
5172 if ((old_alignment || common)
5173 && h->root.type != bfd_link_hash_common)
5174 {
5175 unsigned int common_align;
5176 unsigned int normal_align;
5177 unsigned int symbol_align;
5178 bfd *normal_bfd;
5179 bfd *common_bfd;
5180
5181 BFD_ASSERT (h->root.type == bfd_link_hash_defined
5182 || h->root.type == bfd_link_hash_defweak);
5183
5184 symbol_align = ffs (h->root.u.def.value) - 1;
5185 if (h->root.u.def.section->owner != NULL
5186 && (h->root.u.def.section->owner->flags
5187 & (DYNAMIC | BFD_PLUGIN)) == 0)
5188 {
5189 normal_align = h->root.u.def.section->alignment_power;
5190 if (normal_align > symbol_align)
5191 normal_align = symbol_align;
5192 }
5193 else
5194 normal_align = symbol_align;
5195
5196 if (old_alignment)
5197 {
5198 common_align = old_alignment;
5199 common_bfd = old_bfd;
5200 normal_bfd = abfd;
5201 }
5202 else
5203 {
5204 common_align = bfd_log2 (isym->st_value);
5205 common_bfd = abfd;
5206 normal_bfd = old_bfd;
5207 }
5208
5209 if (normal_align < common_align)
5210 {
5211 /* PR binutils/2735 */
5212 if (normal_bfd == NULL)
5213 _bfd_error_handler
5214 /* xgettext:c-format */
5215 (_("warning: alignment %u of common symbol `%s' in %pB is"
5216 " greater than the alignment (%u) of its section %pA"),
5217 1 << common_align, name, common_bfd,
5218 1 << normal_align, h->root.u.def.section);
5219 else
5220 _bfd_error_handler
5221 /* xgettext:c-format */
5222 (_("warning: alignment %u of symbol `%s' in %pB"
5223 " is smaller than %u in %pB"),
5224 1 << normal_align, name, normal_bfd,
5225 1 << common_align, common_bfd);
5226 }
5227 }
5228
5229 /* Remember the symbol size if it isn't undefined. */
5230 if (isym->st_size != 0
5231 && isym->st_shndx != SHN_UNDEF
5232 && (definition || h->size == 0))
5233 {
5234 if (h->size != 0
5235 && h->size != isym->st_size
5236 && ! size_change_ok)
5237 _bfd_error_handler
5238 /* xgettext:c-format */
5239 (_("warning: size of symbol `%s' changed"
5240 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
5241 name, (uint64_t) h->size, old_bfd,
5242 (uint64_t) isym->st_size, abfd);
5243
5244 h->size = isym->st_size;
5245 }
5246
5247 /* If this is a common symbol, then we always want H->SIZE
5248 to be the size of the common symbol. The code just above
5249 won't fix the size if a common symbol becomes larger. We
5250 don't warn about a size change here, because that is
5251 covered by --warn-common. Allow changes between different
5252 function types. */
5253 if (h->root.type == bfd_link_hash_common)
5254 h->size = h->root.u.c.size;
5255
5256 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
5257 && ((definition && !new_weak)
5258 || (old_weak && h->root.type == bfd_link_hash_common)
5259 || h->type == STT_NOTYPE))
5260 {
5261 unsigned int type = ELF_ST_TYPE (isym->st_info);
5262
5263 /* Turn an IFUNC symbol from a DSO into a normal FUNC
5264 symbol. */
5265 if (type == STT_GNU_IFUNC
5266 && (abfd->flags & DYNAMIC) != 0)
5267 type = STT_FUNC;
5268
5269 if (h->type != type)
5270 {
5271 if (h->type != STT_NOTYPE && ! type_change_ok)
5272 /* xgettext:c-format */
5273 _bfd_error_handler
5274 (_("warning: type of symbol `%s' changed"
5275 " from %d to %d in %pB"),
5276 name, h->type, type, abfd);
5277
5278 h->type = type;
5279 }
5280 }
5281
5282 /* Merge st_other field. */
5283 elf_merge_st_other (abfd, h, isym->st_other, sec,
5284 definition, dynamic);
5285
5286 /* We don't want to make debug symbol dynamic. */
5287 if (definition
5288 && (sec->flags & SEC_DEBUGGING)
5289 && !bfd_link_relocatable (info))
5290 dynsym = false;
5291
5292 /* Nor should we make plugin symbols dynamic. */
5293 if ((abfd->flags & BFD_PLUGIN) != 0)
5294 dynsym = false;
5295
5296 if (definition)
5297 {
5298 h->target_internal = isym->st_target_internal;
5299 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5300 }
5301
5302 if (definition && !dynamic)
5303 {
5304 char *p = strchr (name, ELF_VER_CHR);
5305 if (p != NULL && p[1] != ELF_VER_CHR)
5306 {
5307 /* Queue non-default versions so that .symver x, x@FOO
5308 aliases can be checked. */
5309 if (!nondeflt_vers)
5310 {
5311 size_t amt = ((isymend - isym + 1)
5312 * sizeof (struct elf_link_hash_entry *));
5313 nondeflt_vers
5314 = (struct elf_link_hash_entry **) bfd_malloc (amt);
5315 if (!nondeflt_vers)
5316 goto error_free_vers;
5317 }
5318 nondeflt_vers[nondeflt_vers_cnt++] = h;
5319 }
5320 }
5321
5322 if (dynsym && h->dynindx == -1)
5323 {
5324 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5325 goto error_free_vers;
5326 if (h->is_weakalias
5327 && weakdef (h)->dynindx == -1)
5328 {
5329 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
5330 goto error_free_vers;
5331 }
5332 }
5333 else if (h->dynindx != -1)
5334 /* If the symbol already has a dynamic index, but
5335 visibility says it should not be visible, turn it into
5336 a local symbol. */
5337 switch (ELF_ST_VISIBILITY (h->other))
5338 {
5339 case STV_INTERNAL:
5340 case STV_HIDDEN:
5341 (*bed->elf_backend_hide_symbol) (info, h, true);
5342 dynsym = false;
5343 break;
5344 }
5345
5346 if (!add_needed
5347 && matched
5348 && definition
5349 && h->root.type != bfd_link_hash_indirect
5350 && ((dynsym
5351 && h->ref_regular_nonweak)
5352 || (old_bfd != NULL
5353 && (old_bfd->flags & BFD_PLUGIN) != 0
5354 && h->ref_ir_nonweak
5355 && !info->lto_all_symbols_read)
5356 || (h->ref_dynamic_nonweak
5357 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5358 && !on_needed_list (elf_dt_name (abfd),
5359 htab->needed, NULL))))
5360 {
5361 const char *soname = elf_dt_name (abfd);
5362
5363 info->callbacks->minfo ("%!", soname, old_bfd,
5364 h->root.root.string);
5365
5366 /* A symbol from a library loaded via DT_NEEDED of some
5367 other library is referenced by a regular object.
5368 Add a DT_NEEDED entry for it. Issue an error if
5369 --no-add-needed is used and the reference was not
5370 a weak one. */
5371 if (old_bfd != NULL
5372 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
5373 {
5374 _bfd_error_handler
5375 /* xgettext:c-format */
5376 (_("%pB: undefined reference to symbol '%s'"),
5377 old_bfd, name);
5378 bfd_set_error (bfd_error_missing_dso);
5379 goto error_free_vers;
5380 }
5381
5382 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
5383 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
5384
5385 /* Create dynamic sections for backends that require
5386 that be done before setup_gnu_properties. */
5387 if (!_bfd_elf_link_create_dynamic_sections (abfd, info))
5388 return false;
5389 add_needed = true;
5390 }
5391 }
5392 }
5393
5394 if (info->lto_plugin_active
5395 && !bfd_link_relocatable (info)
5396 && (abfd->flags & BFD_PLUGIN) == 0
5397 && !just_syms
5398 && extsymcount)
5399 {
5400 int r_sym_shift;
5401
5402 if (bed->s->arch_size == 32)
5403 r_sym_shift = 8;
5404 else
5405 r_sym_shift = 32;
5406
5407 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5408 referenced in regular objects so that linker plugin will get
5409 the correct symbol resolution. */
5410
5411 sym_hash = elf_sym_hashes (abfd);
5412 for (s = abfd->sections; s != NULL; s = s->next)
5413 {
5414 Elf_Internal_Rela *internal_relocs;
5415 Elf_Internal_Rela *rel, *relend;
5416
5417 /* Don't check relocations in excluded sections. */
5418 if ((s->flags & SEC_RELOC) == 0
5419 || s->reloc_count == 0
5420 || (s->flags & SEC_EXCLUDE) != 0
5421 || ((info->strip == strip_all
5422 || info->strip == strip_debugger)
5423 && (s->flags & SEC_DEBUGGING) != 0))
5424 continue;
5425
5426 internal_relocs = _bfd_elf_link_info_read_relocs (abfd, info,
5427 s, NULL,
5428 NULL,
5429 _bfd_link_keep_memory (info));
5430 if (internal_relocs == NULL)
5431 goto error_free_vers;
5432
5433 rel = internal_relocs;
5434 relend = rel + s->reloc_count;
5435 for ( ; rel < relend; rel++)
5436 {
5437 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5438 struct elf_link_hash_entry *h;
5439
5440 /* Skip local symbols. */
5441 if (r_symndx < extsymoff)
5442 continue;
5443
5444 h = sym_hash[r_symndx - extsymoff];
5445 if (h != NULL)
5446 h->root.non_ir_ref_regular = 1;
5447 }
5448
5449 if (elf_section_data (s)->relocs != internal_relocs)
5450 free (internal_relocs);
5451 }
5452 }
5453
5454 free (extversym);
5455 extversym = NULL;
5456 free (isymbuf);
5457 isymbuf = NULL;
5458
5459 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5460 {
5461 unsigned int i;
5462
5463 /* Restore the symbol table. */
5464 old_ent = (char *) old_tab + tabsize;
5465 memset (elf_sym_hashes (abfd), 0,
5466 extsymcount * sizeof (struct elf_link_hash_entry *));
5467 htab->root.table.table = old_table;
5468 htab->root.table.size = old_size;
5469 htab->root.table.count = old_count;
5470 memcpy (htab->root.table.table, old_tab, tabsize);
5471 htab->root.undefs = old_undefs;
5472 htab->root.undefs_tail = old_undefs_tail;
5473 if (htab->dynstr != NULL)
5474 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5475 free (old_strtab);
5476 old_strtab = NULL;
5477 for (i = 0; i < htab->root.table.size; i++)
5478 {
5479 struct bfd_hash_entry *p;
5480 struct elf_link_hash_entry *h;
5481 unsigned int non_ir_ref_dynamic;
5482
5483 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5484 {
5485 /* Preserve non_ir_ref_dynamic so that this symbol
5486 will be exported when the dynamic lib becomes needed
5487 in the second pass. */
5488 h = (struct elf_link_hash_entry *) p;
5489 if (h->root.type == bfd_link_hash_warning)
5490 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5491 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
5492
5493 h = (struct elf_link_hash_entry *) p;
5494 memcpy (h, old_ent, htab->root.table.entsize);
5495 old_ent = (char *) old_ent + htab->root.table.entsize;
5496 if (h->root.type == bfd_link_hash_warning)
5497 {
5498 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5499 memcpy (h, old_ent, htab->root.table.entsize);
5500 old_ent = (char *) old_ent + htab->root.table.entsize;
5501 }
5502 if (h->root.type == bfd_link_hash_common)
5503 {
5504 memcpy (h->root.u.c.p, old_ent, sizeof (*h->root.u.c.p));
5505 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
5506 }
5507 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
5508 }
5509 }
5510
5511 /* Make a special call to the linker "notice" function to
5512 tell it that symbols added for crefs may need to be removed. */
5513 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
5514 goto error_free_vers;
5515
5516 free (old_tab);
5517 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5518 alloc_mark);
5519 free (nondeflt_vers);
5520 return true;
5521 }
5522
5523 if (old_tab != NULL)
5524 {
5525 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
5526 goto error_free_vers;
5527 free (old_tab);
5528 old_tab = NULL;
5529 }
5530
5531 /* Now that all the symbols from this input file are created, if
5532 not performing a relocatable link, handle .symver foo, foo@BAR
5533 such that any relocs against foo become foo@BAR. */
5534 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
5535 {
5536 size_t cnt, symidx;
5537
5538 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5539 {
5540 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5541 char *shortname, *p;
5542 size_t amt;
5543
5544 p = strchr (h->root.root.string, ELF_VER_CHR);
5545 if (p == NULL
5546 || (h->root.type != bfd_link_hash_defined
5547 && h->root.type != bfd_link_hash_defweak))
5548 continue;
5549
5550 amt = p - h->root.root.string;
5551 shortname = (char *) bfd_malloc (amt + 1);
5552 if (!shortname)
5553 goto error_free_vers;
5554 memcpy (shortname, h->root.root.string, amt);
5555 shortname[amt] = '\0';
5556
5557 hi = (struct elf_link_hash_entry *)
5558 bfd_link_hash_lookup (&htab->root, shortname,
5559 false, false, false);
5560 if (hi != NULL
5561 && hi->root.type == h->root.type
5562 && hi->root.u.def.value == h->root.u.def.value
5563 && hi->root.u.def.section == h->root.u.def.section)
5564 {
5565 (*bed->elf_backend_hide_symbol) (info, hi, true);
5566 hi->root.type = bfd_link_hash_indirect;
5567 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
5568 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
5569 sym_hash = elf_sym_hashes (abfd);
5570 if (sym_hash)
5571 for (symidx = 0; symidx < extsymcount; ++symidx)
5572 if (sym_hash[symidx] == hi)
5573 {
5574 sym_hash[symidx] = h;
5575 break;
5576 }
5577 }
5578 free (shortname);
5579 }
5580 free (nondeflt_vers);
5581 nondeflt_vers = NULL;
5582 }
5583
5584 /* Now set the alias field correctly for all the weak defined
5585 symbols we found. The only way to do this is to search all the
5586 symbols. Since we only need the information for non functions in
5587 dynamic objects, that's the only time we actually put anything on
5588 the list WEAKS. We need this information so that if a regular
5589 object refers to a symbol defined weakly in a dynamic object, the
5590 real symbol in the dynamic object is also put in the dynamic
5591 symbols; we also must arrange for both symbols to point to the
5592 same memory location. We could handle the general case of symbol
5593 aliasing, but a general symbol alias can only be generated in
5594 assembler code, handling it correctly would be very time
5595 consuming, and other ELF linkers don't handle general aliasing
5596 either. */
5597 if (weaks != NULL)
5598 {
5599 struct elf_link_hash_entry **hpp;
5600 struct elf_link_hash_entry **hppend;
5601 struct elf_link_hash_entry **sorted_sym_hash;
5602 struct elf_link_hash_entry *h;
5603 size_t sym_count, amt;
5604
5605 /* Since we have to search the whole symbol list for each weak
5606 defined symbol, search time for N weak defined symbols will be
5607 O(N^2). Binary search will cut it down to O(NlogN). */
5608 amt = extsymcount * sizeof (*sorted_sym_hash);
5609 sorted_sym_hash = bfd_malloc (amt);
5610 if (sorted_sym_hash == NULL)
5611 goto error_return;
5612 sym_hash = sorted_sym_hash;
5613 hpp = elf_sym_hashes (abfd);
5614 hppend = hpp + extsymcount;
5615 sym_count = 0;
5616 for (; hpp < hppend; hpp++)
5617 {
5618 h = *hpp;
5619 if (h != NULL
5620 && h->root.type == bfd_link_hash_defined
5621 && !bed->is_function_type (h->type))
5622 {
5623 *sym_hash = h;
5624 sym_hash++;
5625 sym_count++;
5626 }
5627 }
5628
5629 qsort (sorted_sym_hash, sym_count, sizeof (*sorted_sym_hash),
5630 elf_sort_symbol);
5631
5632 while (weaks != NULL)
5633 {
5634 struct elf_link_hash_entry *hlook;
5635 asection *slook;
5636 bfd_vma vlook;
5637 size_t i, j, idx = 0;
5638
5639 hlook = weaks;
5640 weaks = hlook->u.alias;
5641 hlook->u.alias = NULL;
5642
5643 if (hlook->root.type != bfd_link_hash_defined
5644 && hlook->root.type != bfd_link_hash_defweak)
5645 continue;
5646
5647 slook = hlook->root.u.def.section;
5648 vlook = hlook->root.u.def.value;
5649
5650 i = 0;
5651 j = sym_count;
5652 while (i != j)
5653 {
5654 bfd_signed_vma vdiff;
5655 idx = (i + j) / 2;
5656 h = sorted_sym_hash[idx];
5657 vdiff = vlook - h->root.u.def.value;
5658 if (vdiff < 0)
5659 j = idx;
5660 else if (vdiff > 0)
5661 i = idx + 1;
5662 else
5663 {
5664 int sdiff = slook->id - h->root.u.def.section->id;
5665 if (sdiff < 0)
5666 j = idx;
5667 else if (sdiff > 0)
5668 i = idx + 1;
5669 else
5670 break;
5671 }
5672 }
5673
5674 /* We didn't find a value/section match. */
5675 if (i == j)
5676 continue;
5677
5678 /* With multiple aliases, or when the weak symbol is already
5679 strongly defined, we have multiple matching symbols and
5680 the binary search above may land on any of them. Step
5681 one past the matching symbol(s). */
5682 while (++idx != j)
5683 {
5684 h = sorted_sym_hash[idx];
5685 if (h->root.u.def.section != slook
5686 || h->root.u.def.value != vlook)
5687 break;
5688 }
5689
5690 /* Now look back over the aliases. Since we sorted by size
5691 as well as value and section, we'll choose the one with
5692 the largest size. */
5693 while (idx-- != i)
5694 {
5695 h = sorted_sym_hash[idx];
5696
5697 /* Stop if value or section doesn't match. */
5698 if (h->root.u.def.section != slook
5699 || h->root.u.def.value != vlook)
5700 break;
5701 else if (h != hlook)
5702 {
5703 struct elf_link_hash_entry *t;
5704
5705 hlook->u.alias = h;
5706 hlook->is_weakalias = 1;
5707 t = h;
5708 if (t->u.alias != NULL)
5709 while (t->u.alias != h)
5710 t = t->u.alias;
5711 t->u.alias = hlook;
5712
5713 /* If the weak definition is in the list of dynamic
5714 symbols, make sure the real definition is put
5715 there as well. */
5716 if (hlook->dynindx != -1 && h->dynindx == -1)
5717 {
5718 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5719 {
5720 err_free_sym_hash:
5721 free (sorted_sym_hash);
5722 goto error_return;
5723 }
5724 }
5725
5726 /* If the real definition is in the list of dynamic
5727 symbols, make sure the weak definition is put
5728 there as well. If we don't do this, then the
5729 dynamic loader might not merge the entries for the
5730 real definition and the weak definition. */
5731 if (h->dynindx != -1 && hlook->dynindx == -1)
5732 {
5733 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
5734 goto err_free_sym_hash;
5735 }
5736 break;
5737 }
5738 }
5739 }
5740
5741 free (sorted_sym_hash);
5742 }
5743
5744 if (bed->check_directives
5745 && !(*bed->check_directives) (abfd, info))
5746 return false;
5747
5748 /* If this is a non-traditional link, try to optimize the handling
5749 of the .stab/.stabstr sections. */
5750 if (! dynamic
5751 && ! info->traditional_format
5752 && is_elf_hash_table (&htab->root)
5753 && (info->strip != strip_all && info->strip != strip_debugger))
5754 {
5755 asection *stabstr;
5756
5757 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5758 if (stabstr != NULL)
5759 {
5760 bfd_size_type string_offset = 0;
5761 asection *stab;
5762
5763 for (stab = abfd->sections; stab; stab = stab->next)
5764 if (startswith (stab->name, ".stab")
5765 && (!stab->name[5] ||
5766 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5767 && (stab->flags & SEC_MERGE) == 0
5768 && !bfd_is_abs_section (stab->output_section))
5769 {
5770 struct bfd_elf_section_data *secdata;
5771
5772 secdata = elf_section_data (stab);
5773 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5774 stabstr, &secdata->sec_info,
5775 &string_offset))
5776 goto error_return;
5777 if (secdata->sec_info)
5778 stab->sec_info_type = SEC_INFO_TYPE_STABS;
5779 }
5780 }
5781 }
5782
5783 if (dynamic && add_needed)
5784 {
5785 /* Add this bfd to the loaded list. */
5786 struct elf_link_loaded_list *n;
5787
5788 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5789 if (n == NULL)
5790 goto error_return;
5791 n->abfd = abfd;
5792 n->next = htab->dyn_loaded;
5793 htab->dyn_loaded = n;
5794 }
5795 if (dynamic && !add_needed
5796 && (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) != 0)
5797 elf_dyn_lib_class (abfd) |= DYN_NO_NEEDED;
5798
5799 return true;
5800
5801 error_free_vers:
5802 free (old_tab);
5803 free (old_strtab);
5804 free (nondeflt_vers);
5805 free (extversym);
5806 error_free_sym:
5807 free (isymbuf);
5808 error_return:
5809 return false;
5810 }
5811
5812 /* Return the linker hash table entry of a symbol that might be
5813 satisfied by an archive symbol. Return -1 on error. */
5814
5815 struct bfd_link_hash_entry *
5816 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5817 struct bfd_link_info *info,
5818 const char *name)
5819 {
5820 struct bfd_link_hash_entry *h;
5821 char *p, *copy;
5822 size_t len, first;
5823
5824 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
5825 if (h != NULL)
5826 return h;
5827
5828 /* If this is a default version (the name contains @@), look up the
5829 symbol again with only one `@' as well as without the version.
5830 The effect is that references to the symbol with and without the
5831 version will be matched by the default symbol in the archive. */
5832
5833 p = strchr (name, ELF_VER_CHR);
5834 if (p == NULL || p[1] != ELF_VER_CHR)
5835 return h;
5836
5837 /* First check with only one `@'. */
5838 len = strlen (name);
5839 copy = (char *) bfd_alloc (abfd, len);
5840 if (copy == NULL)
5841 return (struct bfd_link_hash_entry *) -1;
5842
5843 first = p - name + 1;
5844 memcpy (copy, name, first);
5845 memcpy (copy + first, name + first + 1, len - first);
5846
5847 h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
5848 if (h == NULL)
5849 {
5850 /* We also need to check references to the symbol without the
5851 version. */
5852 copy[first - 1] = '\0';
5853 h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
5854 }
5855
5856 bfd_release (abfd, copy);
5857 return h;
5858 }
5859
5860 /* Add symbols from an ELF archive file to the linker hash table. We
5861 don't use _bfd_generic_link_add_archive_symbols because we need to
5862 handle versioned symbols.
5863
5864 Fortunately, ELF archive handling is simpler than that done by
5865 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5866 oddities. In ELF, if we find a symbol in the archive map, and the
5867 symbol is currently undefined, we know that we must pull in that
5868 object file.
5869
5870 Unfortunately, we do have to make multiple passes over the symbol
5871 table until nothing further is resolved. */
5872
5873 static bool
5874 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5875 {
5876 symindex c;
5877 unsigned char *included = NULL;
5878 carsym *symdefs;
5879 bool loop;
5880 size_t amt;
5881 const struct elf_backend_data *bed;
5882 struct bfd_link_hash_entry * (*archive_symbol_lookup)
5883 (bfd *, struct bfd_link_info *, const char *);
5884
5885 if (! bfd_has_map (abfd))
5886 {
5887 /* An empty archive is a special case. */
5888 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5889 return true;
5890 bfd_set_error (bfd_error_no_armap);
5891 return false;
5892 }
5893
5894 /* Keep track of all symbols we know to be already defined, and all
5895 files we know to be already included. This is to speed up the
5896 second and subsequent passes. */
5897 c = bfd_ardata (abfd)->symdef_count;
5898 if (c == 0)
5899 return true;
5900 amt = c * sizeof (*included);
5901 included = (unsigned char *) bfd_zmalloc (amt);
5902 if (included == NULL)
5903 return false;
5904
5905 symdefs = bfd_ardata (abfd)->symdefs;
5906 bed = get_elf_backend_data (abfd);
5907 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5908
5909 do
5910 {
5911 file_ptr last;
5912 symindex i;
5913 carsym *symdef;
5914 carsym *symdefend;
5915
5916 loop = false;
5917 last = -1;
5918
5919 symdef = symdefs;
5920 symdefend = symdef + c;
5921 for (i = 0; symdef < symdefend; symdef++, i++)
5922 {
5923 struct bfd_link_hash_entry *h;
5924 bfd *element;
5925 struct bfd_link_hash_entry *undefs_tail;
5926 symindex mark;
5927
5928 if (included[i])
5929 continue;
5930 if (symdef->file_offset == last)
5931 {
5932 included[i] = true;
5933 continue;
5934 }
5935
5936 h = archive_symbol_lookup (abfd, info, symdef->name);
5937 if (h == (struct bfd_link_hash_entry *) -1)
5938 goto error_return;
5939
5940 if (h == NULL)
5941 continue;
5942
5943 if (h->type == bfd_link_hash_undefined)
5944 {
5945 /* If the archive element has already been loaded then one
5946 of the symbols defined by that element might have been
5947 made undefined due to being in a discarded section. */
5948 if (is_elf_hash_table (info->hash)
5949 && ((struct elf_link_hash_entry *) h)->indx == -3)
5950 continue;
5951 }
5952 else if (h->type == bfd_link_hash_common)
5953 {
5954 /* We currently have a common symbol. The archive map contains
5955 a reference to this symbol, so we may want to include it. We
5956 only want to include it however, if this archive element
5957 contains a definition of the symbol, not just another common
5958 declaration of it.
5959
5960 Unfortunately some archivers (including GNU ar) will put
5961 declarations of common symbols into their archive maps, as
5962 well as real definitions, so we cannot just go by the archive
5963 map alone. Instead we must read in the element's symbol
5964 table and check that to see what kind of symbol definition
5965 this is. */
5966 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5967 continue;
5968 }
5969 else
5970 {
5971 if (h->type != bfd_link_hash_undefweak)
5972 /* Symbol must be defined. Don't check it again. */
5973 included[i] = true;
5974 continue;
5975 }
5976
5977 /* We need to include this archive member. */
5978 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset,
5979 info);
5980 if (element == NULL)
5981 goto error_return;
5982
5983 if (! bfd_check_format (element, bfd_object))
5984 goto error_return;
5985
5986 undefs_tail = info->hash->undefs_tail;
5987
5988 if (!(*info->callbacks
5989 ->add_archive_element) (info, element, symdef->name, &element))
5990 continue;
5991 if (!bfd_link_add_symbols (element, info))
5992 goto error_return;
5993
5994 /* If there are any new undefined symbols, we need to make
5995 another pass through the archive in order to see whether
5996 they can be defined. FIXME: This isn't perfect, because
5997 common symbols wind up on undefs_tail and because an
5998 undefined symbol which is defined later on in this pass
5999 does not require another pass. This isn't a bug, but it
6000 does make the code less efficient than it could be. */
6001 if (undefs_tail != info->hash->undefs_tail)
6002 loop = true;
6003
6004 /* Look backward to mark all symbols from this object file
6005 which we have already seen in this pass. */
6006 mark = i;
6007 do
6008 {
6009 included[mark] = true;
6010 if (mark == 0)
6011 break;
6012 --mark;
6013 }
6014 while (symdefs[mark].file_offset == symdef->file_offset);
6015
6016 /* We mark subsequent symbols from this object file as we go
6017 on through the loop. */
6018 last = symdef->file_offset;
6019 }
6020 }
6021 while (loop);
6022
6023 free (included);
6024 return true;
6025
6026 error_return:
6027 free (included);
6028 return false;
6029 }
6030
6031 /* Given an ELF BFD, add symbols to the global hash table as
6032 appropriate. */
6033
6034 bool
6035 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
6036 {
6037 switch (bfd_get_format (abfd))
6038 {
6039 case bfd_object:
6040 return elf_link_add_object_symbols (abfd, info);
6041 case bfd_archive:
6042 return elf_link_add_archive_symbols (abfd, info);
6043 default:
6044 bfd_set_error (bfd_error_wrong_format);
6045 return false;
6046 }
6047 }
6048 \f
6049 struct hash_codes_info
6050 {
6051 unsigned long *hashcodes;
6052 bool error;
6053 };
6054
6055 /* This function will be called though elf_link_hash_traverse to store
6056 all hash value of the exported symbols in an array. */
6057
6058 static bool
6059 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
6060 {
6061 struct hash_codes_info *inf = (struct hash_codes_info *) data;
6062 const char *name;
6063 unsigned long ha;
6064 char *alc = NULL;
6065
6066 /* Ignore indirect symbols. These are added by the versioning code. */
6067 if (h->dynindx == -1)
6068 return true;
6069
6070 name = h->root.root.string;
6071 if (h->versioned >= versioned)
6072 {
6073 char *p = strchr (name, ELF_VER_CHR);
6074 if (p != NULL)
6075 {
6076 alc = (char *) bfd_malloc (p - name + 1);
6077 if (alc == NULL)
6078 {
6079 inf->error = true;
6080 return false;
6081 }
6082 memcpy (alc, name, p - name);
6083 alc[p - name] = '\0';
6084 name = alc;
6085 }
6086 }
6087
6088 /* Compute the hash value. */
6089 ha = bfd_elf_hash (name);
6090
6091 /* Store the found hash value in the array given as the argument. */
6092 *(inf->hashcodes)++ = ha;
6093
6094 /* And store it in the struct so that we can put it in the hash table
6095 later. */
6096 h->u.elf_hash_value = ha;
6097
6098 free (alc);
6099 return true;
6100 }
6101
6102 struct collect_gnu_hash_codes
6103 {
6104 bfd *output_bfd;
6105 const struct elf_backend_data *bed;
6106 unsigned long int nsyms;
6107 unsigned long int maskbits;
6108 unsigned long int *hashcodes;
6109 unsigned long int *hashval;
6110 unsigned long int *indx;
6111 unsigned long int *counts;
6112 bfd_vma *bitmask;
6113 bfd_byte *contents;
6114 bfd_size_type xlat;
6115 long int min_dynindx;
6116 unsigned long int bucketcount;
6117 unsigned long int symindx;
6118 long int local_indx;
6119 long int shift1, shift2;
6120 unsigned long int mask;
6121 bool error;
6122 };
6123
6124 /* This function will be called though elf_link_hash_traverse to store
6125 all hash value of the exported symbols in an array. */
6126
6127 static bool
6128 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
6129 {
6130 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6131 const char *name;
6132 unsigned long ha;
6133 char *alc = NULL;
6134
6135 /* Ignore indirect symbols. These are added by the versioning code. */
6136 if (h->dynindx == -1)
6137 return true;
6138
6139 /* Ignore also local symbols and undefined symbols. */
6140 if (! (*s->bed->elf_hash_symbol) (h))
6141 return true;
6142
6143 name = h->root.root.string;
6144 if (h->versioned >= versioned)
6145 {
6146 char *p = strchr (name, ELF_VER_CHR);
6147 if (p != NULL)
6148 {
6149 alc = (char *) bfd_malloc (p - name + 1);
6150 if (alc == NULL)
6151 {
6152 s->error = true;
6153 return false;
6154 }
6155 memcpy (alc, name, p - name);
6156 alc[p - name] = '\0';
6157 name = alc;
6158 }
6159 }
6160
6161 /* Compute the hash value. */
6162 ha = bfd_elf_gnu_hash (name);
6163
6164 /* Store the found hash value in the array for compute_bucket_count,
6165 and also for .dynsym reordering purposes. */
6166 s->hashcodes[s->nsyms] = ha;
6167 s->hashval[h->dynindx] = ha;
6168 ++s->nsyms;
6169 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
6170 s->min_dynindx = h->dynindx;
6171
6172 free (alc);
6173 return true;
6174 }
6175
6176 /* This function will be called though elf_link_hash_traverse to do
6177 final dynamic symbol renumbering in case of .gnu.hash.
6178 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6179 to the translation table. */
6180
6181 static bool
6182 elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data)
6183 {
6184 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6185 unsigned long int bucket;
6186 unsigned long int val;
6187
6188 /* Ignore indirect symbols. */
6189 if (h->dynindx == -1)
6190 return true;
6191
6192 /* Ignore also local symbols and undefined symbols. */
6193 if (! (*s->bed->elf_hash_symbol) (h))
6194 {
6195 if (h->dynindx >= s->min_dynindx)
6196 {
6197 if (s->bed->record_xhash_symbol != NULL)
6198 {
6199 (*s->bed->record_xhash_symbol) (h, 0);
6200 s->local_indx++;
6201 }
6202 else
6203 h->dynindx = s->local_indx++;
6204 }
6205 return true;
6206 }
6207
6208 bucket = s->hashval[h->dynindx] % s->bucketcount;
6209 val = (s->hashval[h->dynindx] >> s->shift1)
6210 & ((s->maskbits >> s->shift1) - 1);
6211 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
6212 s->bitmask[val]
6213 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
6214 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
6215 if (s->counts[bucket] == 1)
6216 /* Last element terminates the chain. */
6217 val |= 1;
6218 bfd_put_32 (s->output_bfd, val,
6219 s->contents + (s->indx[bucket] - s->symindx) * 4);
6220 --s->counts[bucket];
6221 if (s->bed->record_xhash_symbol != NULL)
6222 {
6223 bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4;
6224
6225 (*s->bed->record_xhash_symbol) (h, xlat_loc);
6226 }
6227 else
6228 h->dynindx = s->indx[bucket]++;
6229 return true;
6230 }
6231
6232 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6233
6234 bool
6235 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
6236 {
6237 return !(h->forced_local
6238 || h->root.type == bfd_link_hash_undefined
6239 || h->root.type == bfd_link_hash_undefweak
6240 || ((h->root.type == bfd_link_hash_defined
6241 || h->root.type == bfd_link_hash_defweak)
6242 && h->root.u.def.section->output_section == NULL));
6243 }
6244
6245 /* Array used to determine the number of hash table buckets to use
6246 based on the number of symbols there are. If there are fewer than
6247 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6248 fewer than 37 we use 17 buckets, and so forth. We never use more
6249 than 32771 buckets. */
6250
6251 static const size_t elf_buckets[] =
6252 {
6253 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6254 16411, 32771, 0
6255 };
6256
6257 /* Compute bucket count for hashing table. We do not use a static set
6258 of possible tables sizes anymore. Instead we determine for all
6259 possible reasonable sizes of the table the outcome (i.e., the
6260 number of collisions etc) and choose the best solution. The
6261 weighting functions are not too simple to allow the table to grow
6262 without bounds. Instead one of the weighting factors is the size.
6263 Therefore the result is always a good payoff between few collisions
6264 (= short chain lengths) and table size. */
6265 static size_t
6266 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
6267 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
6268 unsigned long int nsyms,
6269 int gnu_hash)
6270 {
6271 size_t best_size = 0;
6272 unsigned long int i;
6273
6274 /* We have a problem here. The following code to optimize the table
6275 size requires an integer type with more the 32 bits. If
6276 BFD_HOST_U_64_BIT is set we know about such a type. */
6277 #ifdef BFD_HOST_U_64_BIT
6278 if (info->optimize)
6279 {
6280 size_t minsize;
6281 size_t maxsize;
6282 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
6283 bfd *dynobj = elf_hash_table (info)->dynobj;
6284 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
6285 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
6286 unsigned long int *counts;
6287 bfd_size_type amt;
6288 unsigned int no_improvement_count = 0;
6289
6290 /* Possible optimization parameters: if we have NSYMS symbols we say
6291 that the hashing table must at least have NSYMS/4 and at most
6292 2*NSYMS buckets. */
6293 minsize = nsyms / 4;
6294 if (minsize == 0)
6295 minsize = 1;
6296 best_size = maxsize = nsyms * 2;
6297 if (gnu_hash)
6298 {
6299 if (minsize < 2)
6300 minsize = 2;
6301 if ((best_size & 31) == 0)
6302 ++best_size;
6303 }
6304
6305 /* Create array where we count the collisions in. We must use bfd_malloc
6306 since the size could be large. */
6307 amt = maxsize;
6308 amt *= sizeof (unsigned long int);
6309 counts = (unsigned long int *) bfd_malloc (amt);
6310 if (counts == NULL)
6311 return 0;
6312
6313 /* Compute the "optimal" size for the hash table. The criteria is a
6314 minimal chain length. The minor criteria is (of course) the size
6315 of the table. */
6316 for (i = minsize; i < maxsize; ++i)
6317 {
6318 /* Walk through the array of hashcodes and count the collisions. */
6319 BFD_HOST_U_64_BIT max;
6320 unsigned long int j;
6321 unsigned long int fact;
6322
6323 if (gnu_hash && (i & 31) == 0)
6324 continue;
6325
6326 memset (counts, '\0', i * sizeof (unsigned long int));
6327
6328 /* Determine how often each hash bucket is used. */
6329 for (j = 0; j < nsyms; ++j)
6330 ++counts[hashcodes[j] % i];
6331
6332 /* For the weight function we need some information about the
6333 pagesize on the target. This is information need not be 100%
6334 accurate. Since this information is not available (so far) we
6335 define it here to a reasonable default value. If it is crucial
6336 to have a better value some day simply define this value. */
6337 # ifndef BFD_TARGET_PAGESIZE
6338 # define BFD_TARGET_PAGESIZE (4096)
6339 # endif
6340
6341 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6342 and the chains. */
6343 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
6344
6345 # if 1
6346 /* Variant 1: optimize for short chains. We add the squares
6347 of all the chain lengths (which favors many small chain
6348 over a few long chains). */
6349 for (j = 0; j < i; ++j)
6350 max += counts[j] * counts[j];
6351
6352 /* This adds penalties for the overall size of the table. */
6353 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6354 max *= fact * fact;
6355 # else
6356 /* Variant 2: Optimize a lot more for small table. Here we
6357 also add squares of the size but we also add penalties for
6358 empty slots (the +1 term). */
6359 for (j = 0; j < i; ++j)
6360 max += (1 + counts[j]) * (1 + counts[j]);
6361
6362 /* The overall size of the table is considered, but not as
6363 strong as in variant 1, where it is squared. */
6364 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6365 max *= fact;
6366 # endif
6367
6368 /* Compare with current best results. */
6369 if (max < best_chlen)
6370 {
6371 best_chlen = max;
6372 best_size = i;
6373 no_improvement_count = 0;
6374 }
6375 /* PR 11843: Avoid futile long searches for the best bucket size
6376 when there are a large number of symbols. */
6377 else if (++no_improvement_count == 100)
6378 break;
6379 }
6380
6381 free (counts);
6382 }
6383 else
6384 #endif /* defined (BFD_HOST_U_64_BIT) */
6385 {
6386 /* This is the fallback solution if no 64bit type is available or if we
6387 are not supposed to spend much time on optimizations. We select the
6388 bucket count using a fixed set of numbers. */
6389 for (i = 0; elf_buckets[i] != 0; i++)
6390 {
6391 best_size = elf_buckets[i];
6392 if (nsyms < elf_buckets[i + 1])
6393 break;
6394 }
6395 if (gnu_hash && best_size < 2)
6396 best_size = 2;
6397 }
6398
6399 return best_size;
6400 }
6401
6402 /* Size any SHT_GROUP section for ld -r. */
6403
6404 bool
6405 _bfd_elf_size_group_sections (struct bfd_link_info *info)
6406 {
6407 bfd *ibfd;
6408 asection *s;
6409
6410 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6411 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6412 && (s = ibfd->sections) != NULL
6413 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
6414 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6415 return false;
6416 return true;
6417 }
6418
6419 /* Set a default stack segment size. The value in INFO wins. If it
6420 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6421 undefined it is initialized. */
6422
6423 bool
6424 bfd_elf_stack_segment_size (bfd *output_bfd,
6425 struct bfd_link_info *info,
6426 const char *legacy_symbol,
6427 bfd_vma default_size)
6428 {
6429 struct elf_link_hash_entry *h = NULL;
6430
6431 /* Look for legacy symbol. */
6432 if (legacy_symbol)
6433 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6434 false, false, false);
6435 if (h && (h->root.type == bfd_link_hash_defined
6436 || h->root.type == bfd_link_hash_defweak)
6437 && h->def_regular
6438 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6439 {
6440 /* The symbol has no type if specified on the command line. */
6441 h->type = STT_OBJECT;
6442 if (info->stacksize)
6443 /* xgettext:c-format */
6444 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6445 output_bfd, legacy_symbol);
6446 else if (h->root.u.def.section != bfd_abs_section_ptr)
6447 /* xgettext:c-format */
6448 _bfd_error_handler (_("%pB: %s not absolute"),
6449 output_bfd, legacy_symbol);
6450 else
6451 info->stacksize = h->root.u.def.value;
6452 }
6453
6454 if (!info->stacksize)
6455 /* If the user didn't set a size, or explicitly inhibit the
6456 size, set it now. */
6457 info->stacksize = default_size;
6458
6459 /* Provide the legacy symbol, if it is referenced. */
6460 if (h && (h->root.type == bfd_link_hash_undefined
6461 || h->root.type == bfd_link_hash_undefweak))
6462 {
6463 struct bfd_link_hash_entry *bh = NULL;
6464
6465 if (!(_bfd_generic_link_add_one_symbol
6466 (info, output_bfd, legacy_symbol,
6467 BSF_GLOBAL, bfd_abs_section_ptr,
6468 info->stacksize >= 0 ? info->stacksize : 0,
6469 NULL, false, get_elf_backend_data (output_bfd)->collect, &bh)))
6470 return false;
6471
6472 h = (struct elf_link_hash_entry *) bh;
6473 h->def_regular = 1;
6474 h->type = STT_OBJECT;
6475 }
6476
6477 return true;
6478 }
6479
6480 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6481
6482 struct elf_gc_sweep_symbol_info
6483 {
6484 struct bfd_link_info *info;
6485 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6486 bool);
6487 };
6488
6489 static bool
6490 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6491 {
6492 if (!h->mark
6493 && (((h->root.type == bfd_link_hash_defined
6494 || h->root.type == bfd_link_hash_defweak)
6495 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6496 && h->root.u.def.section->gc_mark))
6497 || h->root.type == bfd_link_hash_undefined
6498 || h->root.type == bfd_link_hash_undefweak))
6499 {
6500 struct elf_gc_sweep_symbol_info *inf;
6501
6502 inf = (struct elf_gc_sweep_symbol_info *) data;
6503 (*inf->hide_symbol) (inf->info, h, true);
6504 h->def_regular = 0;
6505 h->ref_regular = 0;
6506 h->ref_regular_nonweak = 0;
6507 }
6508
6509 return true;
6510 }
6511
6512 /* Set up the sizes and contents of the ELF dynamic sections. This is
6513 called by the ELF linker emulation before_allocation routine. We
6514 must set the sizes of the sections before the linker sets the
6515 addresses of the various sections. */
6516
6517 bool
6518 bfd_elf_size_dynamic_sections (bfd *output_bfd,
6519 const char *soname,
6520 const char *rpath,
6521 const char *filter_shlib,
6522 const char *audit,
6523 const char *depaudit,
6524 const char * const *auxiliary_filters,
6525 struct bfd_link_info *info,
6526 asection **sinterpptr)
6527 {
6528 bfd *dynobj;
6529 const struct elf_backend_data *bed;
6530
6531 *sinterpptr = NULL;
6532
6533 if (!is_elf_hash_table (info->hash))
6534 return true;
6535
6536 /* Any syms created from now on start with -1 in
6537 got.refcount/offset and plt.refcount/offset. */
6538 elf_hash_table (info)->init_got_refcount
6539 = elf_hash_table (info)->init_got_offset;
6540 elf_hash_table (info)->init_plt_refcount
6541 = elf_hash_table (info)->init_plt_offset;
6542
6543 bed = get_elf_backend_data (output_bfd);
6544
6545 /* The backend may have to create some sections regardless of whether
6546 we're dynamic or not. */
6547 if (bed->elf_backend_always_size_sections
6548 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6549 return false;
6550
6551 dynobj = elf_hash_table (info)->dynobj;
6552
6553 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6554 {
6555 struct bfd_elf_version_tree *verdefs;
6556 struct elf_info_failed asvinfo;
6557 struct bfd_elf_version_tree *t;
6558 struct bfd_elf_version_expr *d;
6559 asection *s;
6560 size_t soname_indx;
6561
6562 /* If we are supposed to export all symbols into the dynamic symbol
6563 table (this is not the normal case), then do so. */
6564 if (info->export_dynamic
6565 || (bfd_link_executable (info) && info->dynamic))
6566 {
6567 struct elf_info_failed eif;
6568
6569 eif.info = info;
6570 eif.failed = false;
6571 elf_link_hash_traverse (elf_hash_table (info),
6572 _bfd_elf_export_symbol,
6573 &eif);
6574 if (eif.failed)
6575 return false;
6576 }
6577
6578 if (soname != NULL)
6579 {
6580 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6581 soname, true);
6582 if (soname_indx == (size_t) -1
6583 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6584 return false;
6585 }
6586 else
6587 soname_indx = (size_t) -1;
6588
6589 /* Make all global versions with definition. */
6590 for (t = info->version_info; t != NULL; t = t->next)
6591 for (d = t->globals.list; d != NULL; d = d->next)
6592 if (!d->symver && d->literal)
6593 {
6594 const char *verstr, *name;
6595 size_t namelen, verlen, newlen;
6596 char *newname, *p, leading_char;
6597 struct elf_link_hash_entry *newh;
6598
6599 leading_char = bfd_get_symbol_leading_char (output_bfd);
6600 name = d->pattern;
6601 namelen = strlen (name) + (leading_char != '\0');
6602 verstr = t->name;
6603 verlen = strlen (verstr);
6604 newlen = namelen + verlen + 3;
6605
6606 newname = (char *) bfd_malloc (newlen);
6607 if (newname == NULL)
6608 return false;
6609 newname[0] = leading_char;
6610 memcpy (newname + (leading_char != '\0'), name, namelen);
6611
6612 /* Check the hidden versioned definition. */
6613 p = newname + namelen;
6614 *p++ = ELF_VER_CHR;
6615 memcpy (p, verstr, verlen + 1);
6616 newh = elf_link_hash_lookup (elf_hash_table (info),
6617 newname, false, false,
6618 false);
6619 if (newh == NULL
6620 || (newh->root.type != bfd_link_hash_defined
6621 && newh->root.type != bfd_link_hash_defweak))
6622 {
6623 /* Check the default versioned definition. */
6624 *p++ = ELF_VER_CHR;
6625 memcpy (p, verstr, verlen + 1);
6626 newh = elf_link_hash_lookup (elf_hash_table (info),
6627 newname, false, false,
6628 false);
6629 }
6630 free (newname);
6631
6632 /* Mark this version if there is a definition and it is
6633 not defined in a shared object. */
6634 if (newh != NULL
6635 && !newh->def_dynamic
6636 && (newh->root.type == bfd_link_hash_defined
6637 || newh->root.type == bfd_link_hash_defweak))
6638 d->symver = 1;
6639 }
6640
6641 /* Attach all the symbols to their version information. */
6642 asvinfo.info = info;
6643 asvinfo.failed = false;
6644
6645 elf_link_hash_traverse (elf_hash_table (info),
6646 _bfd_elf_link_assign_sym_version,
6647 &asvinfo);
6648 if (asvinfo.failed)
6649 return false;
6650
6651 if (!info->allow_undefined_version)
6652 {
6653 /* Check if all global versions have a definition. */
6654 bool all_defined = true;
6655 for (t = info->version_info; t != NULL; t = t->next)
6656 for (d = t->globals.list; d != NULL; d = d->next)
6657 if (d->literal && !d->symver && !d->script)
6658 {
6659 _bfd_error_handler
6660 (_("%s: undefined version: %s"),
6661 d->pattern, t->name);
6662 all_defined = false;
6663 }
6664
6665 if (!all_defined)
6666 {
6667 bfd_set_error (bfd_error_bad_value);
6668 return false;
6669 }
6670 }
6671
6672 /* Set up the version definition section. */
6673 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6674 BFD_ASSERT (s != NULL);
6675
6676 /* We may have created additional version definitions if we are
6677 just linking a regular application. */
6678 verdefs = info->version_info;
6679
6680 /* Skip anonymous version tag. */
6681 if (verdefs != NULL && verdefs->vernum == 0)
6682 verdefs = verdefs->next;
6683
6684 if (verdefs == NULL && !info->create_default_symver)
6685 s->flags |= SEC_EXCLUDE;
6686 else
6687 {
6688 unsigned int cdefs;
6689 bfd_size_type size;
6690 bfd_byte *p;
6691 Elf_Internal_Verdef def;
6692 Elf_Internal_Verdaux defaux;
6693 struct bfd_link_hash_entry *bh;
6694 struct elf_link_hash_entry *h;
6695 const char *name;
6696
6697 cdefs = 0;
6698 size = 0;
6699
6700 /* Make space for the base version. */
6701 size += sizeof (Elf_External_Verdef);
6702 size += sizeof (Elf_External_Verdaux);
6703 ++cdefs;
6704
6705 /* Make space for the default version. */
6706 if (info->create_default_symver)
6707 {
6708 size += sizeof (Elf_External_Verdef);
6709 ++cdefs;
6710 }
6711
6712 for (t = verdefs; t != NULL; t = t->next)
6713 {
6714 struct bfd_elf_version_deps *n;
6715
6716 /* Don't emit base version twice. */
6717 if (t->vernum == 0)
6718 continue;
6719
6720 size += sizeof (Elf_External_Verdef);
6721 size += sizeof (Elf_External_Verdaux);
6722 ++cdefs;
6723
6724 for (n = t->deps; n != NULL; n = n->next)
6725 size += sizeof (Elf_External_Verdaux);
6726 }
6727
6728 s->size = size;
6729 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6730 if (s->contents == NULL && s->size != 0)
6731 return false;
6732
6733 /* Fill in the version definition section. */
6734
6735 p = s->contents;
6736
6737 def.vd_version = VER_DEF_CURRENT;
6738 def.vd_flags = VER_FLG_BASE;
6739 def.vd_ndx = 1;
6740 def.vd_cnt = 1;
6741 if (info->create_default_symver)
6742 {
6743 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6744 def.vd_next = sizeof (Elf_External_Verdef);
6745 }
6746 else
6747 {
6748 def.vd_aux = sizeof (Elf_External_Verdef);
6749 def.vd_next = (sizeof (Elf_External_Verdef)
6750 + sizeof (Elf_External_Verdaux));
6751 }
6752
6753 if (soname_indx != (size_t) -1)
6754 {
6755 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6756 soname_indx);
6757 def.vd_hash = bfd_elf_hash (soname);
6758 defaux.vda_name = soname_indx;
6759 name = soname;
6760 }
6761 else
6762 {
6763 size_t indx;
6764
6765 name = lbasename (bfd_get_filename (output_bfd));
6766 def.vd_hash = bfd_elf_hash (name);
6767 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6768 name, false);
6769 if (indx == (size_t) -1)
6770 return false;
6771 defaux.vda_name = indx;
6772 }
6773 defaux.vda_next = 0;
6774
6775 _bfd_elf_swap_verdef_out (output_bfd, &def,
6776 (Elf_External_Verdef *) p);
6777 p += sizeof (Elf_External_Verdef);
6778 if (info->create_default_symver)
6779 {
6780 /* Add a symbol representing this version. */
6781 bh = NULL;
6782 if (! (_bfd_generic_link_add_one_symbol
6783 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6784 0, NULL, false,
6785 get_elf_backend_data (dynobj)->collect, &bh)))
6786 return false;
6787 h = (struct elf_link_hash_entry *) bh;
6788 h->non_elf = 0;
6789 h->def_regular = 1;
6790 h->type = STT_OBJECT;
6791 h->verinfo.vertree = NULL;
6792
6793 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6794 return false;
6795
6796 /* Create a duplicate of the base version with the same
6797 aux block, but different flags. */
6798 def.vd_flags = 0;
6799 def.vd_ndx = 2;
6800 def.vd_aux = sizeof (Elf_External_Verdef);
6801 if (verdefs)
6802 def.vd_next = (sizeof (Elf_External_Verdef)
6803 + sizeof (Elf_External_Verdaux));
6804 else
6805 def.vd_next = 0;
6806 _bfd_elf_swap_verdef_out (output_bfd, &def,
6807 (Elf_External_Verdef *) p);
6808 p += sizeof (Elf_External_Verdef);
6809 }
6810 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6811 (Elf_External_Verdaux *) p);
6812 p += sizeof (Elf_External_Verdaux);
6813
6814 for (t = verdefs; t != NULL; t = t->next)
6815 {
6816 unsigned int cdeps;
6817 struct bfd_elf_version_deps *n;
6818
6819 /* Don't emit the base version twice. */
6820 if (t->vernum == 0)
6821 continue;
6822
6823 cdeps = 0;
6824 for (n = t->deps; n != NULL; n = n->next)
6825 ++cdeps;
6826
6827 /* Add a symbol representing this version. */
6828 bh = NULL;
6829 if (! (_bfd_generic_link_add_one_symbol
6830 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6831 0, NULL, false,
6832 get_elf_backend_data (dynobj)->collect, &bh)))
6833 return false;
6834 h = (struct elf_link_hash_entry *) bh;
6835 h->non_elf = 0;
6836 h->def_regular = 1;
6837 h->type = STT_OBJECT;
6838 h->verinfo.vertree = t;
6839
6840 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6841 return false;
6842
6843 def.vd_version = VER_DEF_CURRENT;
6844 def.vd_flags = 0;
6845 if (t->globals.list == NULL
6846 && t->locals.list == NULL
6847 && ! t->used)
6848 def.vd_flags |= VER_FLG_WEAK;
6849 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6850 def.vd_cnt = cdeps + 1;
6851 def.vd_hash = bfd_elf_hash (t->name);
6852 def.vd_aux = sizeof (Elf_External_Verdef);
6853 def.vd_next = 0;
6854
6855 /* If a basever node is next, it *must* be the last node in
6856 the chain, otherwise Verdef construction breaks. */
6857 if (t->next != NULL && t->next->vernum == 0)
6858 BFD_ASSERT (t->next->next == NULL);
6859
6860 if (t->next != NULL && t->next->vernum != 0)
6861 def.vd_next = (sizeof (Elf_External_Verdef)
6862 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6863
6864 _bfd_elf_swap_verdef_out (output_bfd, &def,
6865 (Elf_External_Verdef *) p);
6866 p += sizeof (Elf_External_Verdef);
6867
6868 defaux.vda_name = h->dynstr_index;
6869 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6870 h->dynstr_index);
6871 defaux.vda_next = 0;
6872 if (t->deps != NULL)
6873 defaux.vda_next = sizeof (Elf_External_Verdaux);
6874 t->name_indx = defaux.vda_name;
6875
6876 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6877 (Elf_External_Verdaux *) p);
6878 p += sizeof (Elf_External_Verdaux);
6879
6880 for (n = t->deps; n != NULL; n = n->next)
6881 {
6882 if (n->version_needed == NULL)
6883 {
6884 /* This can happen if there was an error in the
6885 version script. */
6886 defaux.vda_name = 0;
6887 }
6888 else
6889 {
6890 defaux.vda_name = n->version_needed->name_indx;
6891 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6892 defaux.vda_name);
6893 }
6894 if (n->next == NULL)
6895 defaux.vda_next = 0;
6896 else
6897 defaux.vda_next = sizeof (Elf_External_Verdaux);
6898
6899 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6900 (Elf_External_Verdaux *) p);
6901 p += sizeof (Elf_External_Verdaux);
6902 }
6903 }
6904
6905 elf_tdata (output_bfd)->cverdefs = cdefs;
6906 }
6907 }
6908
6909 if (info->gc_sections && bed->can_gc_sections)
6910 {
6911 struct elf_gc_sweep_symbol_info sweep_info;
6912
6913 /* Remove the symbols that were in the swept sections from the
6914 dynamic symbol table. */
6915 sweep_info.info = info;
6916 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6917 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6918 &sweep_info);
6919 }
6920
6921 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6922 {
6923 asection *s;
6924 struct elf_find_verdep_info sinfo;
6925
6926 /* Work out the size of the version reference section. */
6927
6928 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6929 BFD_ASSERT (s != NULL);
6930
6931 sinfo.info = info;
6932 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6933 if (sinfo.vers == 0)
6934 sinfo.vers = 1;
6935 sinfo.failed = false;
6936
6937 elf_link_hash_traverse (elf_hash_table (info),
6938 _bfd_elf_link_find_version_dependencies,
6939 &sinfo);
6940 if (sinfo.failed)
6941 return false;
6942
6943 if (elf_tdata (output_bfd)->verref == NULL)
6944 s->flags |= SEC_EXCLUDE;
6945 else
6946 {
6947 Elf_Internal_Verneed *vn;
6948 unsigned int size;
6949 unsigned int crefs;
6950 bfd_byte *p;
6951
6952 /* Build the version dependency section. */
6953 size = 0;
6954 crefs = 0;
6955 for (vn = elf_tdata (output_bfd)->verref;
6956 vn != NULL;
6957 vn = vn->vn_nextref)
6958 {
6959 Elf_Internal_Vernaux *a;
6960
6961 size += sizeof (Elf_External_Verneed);
6962 ++crefs;
6963 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6964 size += sizeof (Elf_External_Vernaux);
6965 }
6966
6967 s->size = size;
6968 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6969 if (s->contents == NULL)
6970 return false;
6971
6972 p = s->contents;
6973 for (vn = elf_tdata (output_bfd)->verref;
6974 vn != NULL;
6975 vn = vn->vn_nextref)
6976 {
6977 unsigned int caux;
6978 Elf_Internal_Vernaux *a;
6979 size_t indx;
6980
6981 caux = 0;
6982 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6983 ++caux;
6984
6985 vn->vn_version = VER_NEED_CURRENT;
6986 vn->vn_cnt = caux;
6987 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6988 elf_dt_name (vn->vn_bfd) != NULL
6989 ? elf_dt_name (vn->vn_bfd)
6990 : lbasename (bfd_get_filename
6991 (vn->vn_bfd)),
6992 false);
6993 if (indx == (size_t) -1)
6994 return false;
6995 vn->vn_file = indx;
6996 vn->vn_aux = sizeof (Elf_External_Verneed);
6997 if (vn->vn_nextref == NULL)
6998 vn->vn_next = 0;
6999 else
7000 vn->vn_next = (sizeof (Elf_External_Verneed)
7001 + caux * sizeof (Elf_External_Vernaux));
7002
7003 _bfd_elf_swap_verneed_out (output_bfd, vn,
7004 (Elf_External_Verneed *) p);
7005 p += sizeof (Elf_External_Verneed);
7006
7007 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7008 {
7009 a->vna_hash = bfd_elf_hash (a->vna_nodename);
7010 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7011 a->vna_nodename, false);
7012 if (indx == (size_t) -1)
7013 return false;
7014 a->vna_name = indx;
7015 if (a->vna_nextptr == NULL)
7016 a->vna_next = 0;
7017 else
7018 a->vna_next = sizeof (Elf_External_Vernaux);
7019
7020 _bfd_elf_swap_vernaux_out (output_bfd, a,
7021 (Elf_External_Vernaux *) p);
7022 p += sizeof (Elf_External_Vernaux);
7023 }
7024 }
7025
7026 elf_tdata (output_bfd)->cverrefs = crefs;
7027 }
7028 }
7029
7030 if (bfd_link_relocatable (info)
7031 && !_bfd_elf_size_group_sections (info))
7032 return false;
7033
7034 /* Determine any GNU_STACK segment requirements, after the backend
7035 has had a chance to set a default segment size. */
7036 if (info->execstack)
7037 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
7038 else if (info->noexecstack)
7039 elf_stack_flags (output_bfd) = PF_R | PF_W;
7040 else
7041 {
7042 bfd *inputobj;
7043 asection *notesec = NULL;
7044 int exec = 0;
7045
7046 for (inputobj = info->input_bfds;
7047 inputobj;
7048 inputobj = inputobj->link.next)
7049 {
7050 asection *s;
7051
7052 if (inputobj->flags
7053 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
7054 continue;
7055 s = inputobj->sections;
7056 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7057 continue;
7058
7059 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
7060 if (s)
7061 {
7062 if (s->flags & SEC_CODE)
7063 exec = PF_X;
7064 notesec = s;
7065 }
7066 else if (bed->default_execstack)
7067 exec = PF_X;
7068 }
7069 if (notesec || info->stacksize > 0)
7070 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
7071 if (notesec && exec && bfd_link_relocatable (info)
7072 && notesec->output_section != bfd_abs_section_ptr)
7073 notesec->output_section->flags |= SEC_CODE;
7074 }
7075
7076 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7077 {
7078 struct elf_info_failed eif;
7079 struct elf_link_hash_entry *h;
7080 asection *dynstr;
7081 asection *s;
7082
7083 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
7084 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
7085
7086 if (info->symbolic)
7087 {
7088 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
7089 return false;
7090 info->flags |= DF_SYMBOLIC;
7091 }
7092
7093 if (rpath != NULL)
7094 {
7095 size_t indx;
7096 bfd_vma tag;
7097
7098 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
7099 true);
7100 if (indx == (size_t) -1)
7101 return false;
7102
7103 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
7104 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
7105 return false;
7106 }
7107
7108 if (filter_shlib != NULL)
7109 {
7110 size_t indx;
7111
7112 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7113 filter_shlib, true);
7114 if (indx == (size_t) -1
7115 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
7116 return false;
7117 }
7118
7119 if (auxiliary_filters != NULL)
7120 {
7121 const char * const *p;
7122
7123 for (p = auxiliary_filters; *p != NULL; p++)
7124 {
7125 size_t indx;
7126
7127 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7128 *p, true);
7129 if (indx == (size_t) -1
7130 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
7131 return false;
7132 }
7133 }
7134
7135 if (audit != NULL)
7136 {
7137 size_t indx;
7138
7139 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
7140 true);
7141 if (indx == (size_t) -1
7142 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
7143 return false;
7144 }
7145
7146 if (depaudit != NULL)
7147 {
7148 size_t indx;
7149
7150 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
7151 true);
7152 if (indx == (size_t) -1
7153 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
7154 return false;
7155 }
7156
7157 eif.info = info;
7158 eif.failed = false;
7159
7160 /* Find all symbols which were defined in a dynamic object and make
7161 the backend pick a reasonable value for them. */
7162 elf_link_hash_traverse (elf_hash_table (info),
7163 _bfd_elf_adjust_dynamic_symbol,
7164 &eif);
7165 if (eif.failed)
7166 return false;
7167
7168 /* Add some entries to the .dynamic section. We fill in some of the
7169 values later, in bfd_elf_final_link, but we must add the entries
7170 now so that we know the final size of the .dynamic section. */
7171
7172 /* If there are initialization and/or finalization functions to
7173 call then add the corresponding DT_INIT/DT_FINI entries. */
7174 h = (info->init_function
7175 ? elf_link_hash_lookup (elf_hash_table (info),
7176 info->init_function, false,
7177 false, false)
7178 : NULL);
7179 if (h != NULL
7180 && (h->ref_regular
7181 || h->def_regular))
7182 {
7183 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
7184 return false;
7185 }
7186 h = (info->fini_function
7187 ? elf_link_hash_lookup (elf_hash_table (info),
7188 info->fini_function, false,
7189 false, false)
7190 : NULL);
7191 if (h != NULL
7192 && (h->ref_regular
7193 || h->def_regular))
7194 {
7195 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
7196 return false;
7197 }
7198
7199 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
7200 if (s != NULL && s->linker_has_input)
7201 {
7202 /* DT_PREINIT_ARRAY is not allowed in shared library. */
7203 if (! bfd_link_executable (info))
7204 {
7205 bfd *sub;
7206 asection *o;
7207
7208 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
7209 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
7210 && (o = sub->sections) != NULL
7211 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
7212 for (o = sub->sections; o != NULL; o = o->next)
7213 if (elf_section_data (o)->this_hdr.sh_type
7214 == SHT_PREINIT_ARRAY)
7215 {
7216 _bfd_error_handler
7217 (_("%pB: .preinit_array section is not allowed in DSO"),
7218 sub);
7219 break;
7220 }
7221
7222 bfd_set_error (bfd_error_nonrepresentable_section);
7223 return false;
7224 }
7225
7226 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
7227 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
7228 return false;
7229 }
7230 s = bfd_get_section_by_name (output_bfd, ".init_array");
7231 if (s != NULL && s->linker_has_input)
7232 {
7233 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
7234 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
7235 return false;
7236 }
7237 s = bfd_get_section_by_name (output_bfd, ".fini_array");
7238 if (s != NULL && s->linker_has_input)
7239 {
7240 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
7241 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
7242 return false;
7243 }
7244
7245 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
7246 /* If .dynstr is excluded from the link, we don't want any of
7247 these tags. Strictly, we should be checking each section
7248 individually; This quick check covers for the case where
7249 someone does a /DISCARD/ : { *(*) }. */
7250 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
7251 {
7252 bfd_size_type strsize;
7253
7254 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7255 if ((info->emit_hash
7256 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
7257 || (info->emit_gnu_hash
7258 && (bed->record_xhash_symbol == NULL
7259 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)))
7260 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
7261 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
7262 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
7263 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
7264 bed->s->sizeof_sym)
7265 || (info->gnu_flags_1
7266 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_FLAGS_1,
7267 info->gnu_flags_1)))
7268 return false;
7269 }
7270 }
7271
7272 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
7273 return false;
7274
7275 /* The backend must work out the sizes of all the other dynamic
7276 sections. */
7277 if (dynobj != NULL
7278 && bed->elf_backend_size_dynamic_sections != NULL
7279 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
7280 return false;
7281
7282 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7283 {
7284 if (elf_tdata (output_bfd)->cverdefs)
7285 {
7286 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
7287
7288 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
7289 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
7290 return false;
7291 }
7292
7293 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
7294 {
7295 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
7296 return false;
7297 }
7298 else if (info->flags & DF_BIND_NOW)
7299 {
7300 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
7301 return false;
7302 }
7303
7304 if (info->flags_1)
7305 {
7306 if (bfd_link_executable (info))
7307 info->flags_1 &= ~ (DF_1_INITFIRST
7308 | DF_1_NODELETE
7309 | DF_1_NOOPEN);
7310 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
7311 return false;
7312 }
7313
7314 if (elf_tdata (output_bfd)->cverrefs)
7315 {
7316 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
7317
7318 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
7319 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
7320 return false;
7321 }
7322
7323 if ((elf_tdata (output_bfd)->cverrefs == 0
7324 && elf_tdata (output_bfd)->cverdefs == 0)
7325 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
7326 {
7327 asection *s;
7328
7329 s = bfd_get_linker_section (dynobj, ".gnu.version");
7330 s->flags |= SEC_EXCLUDE;
7331 }
7332 }
7333 return true;
7334 }
7335
7336 /* Find the first non-excluded output section. We'll use its
7337 section symbol for some emitted relocs. */
7338 void
7339 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7340 {
7341 asection *s;
7342 asection *found = NULL;
7343
7344 for (s = output_bfd->sections; s != NULL; s = s->next)
7345 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7346 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7347 {
7348 found = s;
7349 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7350 break;
7351 }
7352 elf_hash_table (info)->text_index_section = found;
7353 }
7354
7355 /* Find two non-excluded output sections, one for code, one for data.
7356 We'll use their section symbols for some emitted relocs. */
7357 void
7358 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7359 {
7360 asection *s;
7361 asection *found = NULL;
7362
7363 /* Data first, since setting text_index_section changes
7364 _bfd_elf_omit_section_dynsym_default. */
7365 for (s = output_bfd->sections; s != NULL; s = s->next)
7366 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7367 && !(s->flags & SEC_READONLY)
7368 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7369 {
7370 found = s;
7371 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7372 break;
7373 }
7374 elf_hash_table (info)->data_index_section = found;
7375
7376 for (s = output_bfd->sections; s != NULL; s = s->next)
7377 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7378 && (s->flags & SEC_READONLY)
7379 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7380 {
7381 found = s;
7382 break;
7383 }
7384 elf_hash_table (info)->text_index_section = found;
7385 }
7386
7387 #define GNU_HASH_SECTION_NAME(bed) \
7388 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7389
7390 bool
7391 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7392 {
7393 const struct elf_backend_data *bed;
7394 unsigned long section_sym_count;
7395 bfd_size_type dynsymcount = 0;
7396
7397 if (!is_elf_hash_table (info->hash))
7398 return true;
7399
7400 bed = get_elf_backend_data (output_bfd);
7401 (*bed->elf_backend_init_index_section) (output_bfd, info);
7402
7403 /* Assign dynsym indices. In a shared library we generate a section
7404 symbol for each output section, which come first. Next come all
7405 of the back-end allocated local dynamic syms, followed by the rest
7406 of the global symbols.
7407
7408 This is usually not needed for static binaries, however backends
7409 can request to always do it, e.g. the MIPS backend uses dynamic
7410 symbol counts to lay out GOT, which will be produced in the
7411 presence of GOT relocations even in static binaries (holding fixed
7412 data in that case, to satisfy those relocations). */
7413
7414 if (elf_hash_table (info)->dynamic_sections_created
7415 || bed->always_renumber_dynsyms)
7416 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7417 &section_sym_count);
7418
7419 if (elf_hash_table (info)->dynamic_sections_created)
7420 {
7421 bfd *dynobj;
7422 asection *s;
7423 unsigned int dtagcount;
7424
7425 dynobj = elf_hash_table (info)->dynobj;
7426
7427 /* Work out the size of the symbol version section. */
7428 s = bfd_get_linker_section (dynobj, ".gnu.version");
7429 BFD_ASSERT (s != NULL);
7430 if ((s->flags & SEC_EXCLUDE) == 0)
7431 {
7432 s->size = dynsymcount * sizeof (Elf_External_Versym);
7433 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7434 if (s->contents == NULL)
7435 return false;
7436
7437 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7438 return false;
7439 }
7440
7441 /* Set the size of the .dynsym and .hash sections. We counted
7442 the number of dynamic symbols in elf_link_add_object_symbols.
7443 We will build the contents of .dynsym and .hash when we build
7444 the final symbol table, because until then we do not know the
7445 correct value to give the symbols. We built the .dynstr
7446 section as we went along in elf_link_add_object_symbols. */
7447 s = elf_hash_table (info)->dynsym;
7448 BFD_ASSERT (s != NULL);
7449 s->size = dynsymcount * bed->s->sizeof_sym;
7450
7451 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7452 if (s->contents == NULL)
7453 return false;
7454
7455 /* The first entry in .dynsym is a dummy symbol. Clear all the
7456 section syms, in case we don't output them all. */
7457 ++section_sym_count;
7458 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
7459
7460 elf_hash_table (info)->bucketcount = 0;
7461
7462 /* Compute the size of the hashing table. As a side effect this
7463 computes the hash values for all the names we export. */
7464 if (info->emit_hash)
7465 {
7466 unsigned long int *hashcodes;
7467 struct hash_codes_info hashinf;
7468 bfd_size_type amt;
7469 unsigned long int nsyms;
7470 size_t bucketcount;
7471 size_t hash_entry_size;
7472
7473 /* Compute the hash values for all exported symbols. At the same
7474 time store the values in an array so that we could use them for
7475 optimizations. */
7476 amt = dynsymcount * sizeof (unsigned long int);
7477 hashcodes = (unsigned long int *) bfd_malloc (amt);
7478 if (hashcodes == NULL)
7479 return false;
7480 hashinf.hashcodes = hashcodes;
7481 hashinf.error = false;
7482
7483 /* Put all hash values in HASHCODES. */
7484 elf_link_hash_traverse (elf_hash_table (info),
7485 elf_collect_hash_codes, &hashinf);
7486 if (hashinf.error)
7487 {
7488 free (hashcodes);
7489 return false;
7490 }
7491
7492 nsyms = hashinf.hashcodes - hashcodes;
7493 bucketcount
7494 = compute_bucket_count (info, hashcodes, nsyms, 0);
7495 free (hashcodes);
7496
7497 if (bucketcount == 0 && nsyms > 0)
7498 return false;
7499
7500 elf_hash_table (info)->bucketcount = bucketcount;
7501
7502 s = bfd_get_linker_section (dynobj, ".hash");
7503 BFD_ASSERT (s != NULL);
7504 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7505 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
7506 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7507 if (s->contents == NULL)
7508 return false;
7509
7510 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7511 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7512 s->contents + hash_entry_size);
7513 }
7514
7515 if (info->emit_gnu_hash)
7516 {
7517 size_t i, cnt;
7518 unsigned char *contents;
7519 struct collect_gnu_hash_codes cinfo;
7520 bfd_size_type amt;
7521 size_t bucketcount;
7522
7523 memset (&cinfo, 0, sizeof (cinfo));
7524
7525 /* Compute the hash values for all exported symbols. At the same
7526 time store the values in an array so that we could use them for
7527 optimizations. */
7528 amt = dynsymcount * 2 * sizeof (unsigned long int);
7529 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
7530 if (cinfo.hashcodes == NULL)
7531 return false;
7532
7533 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7534 cinfo.min_dynindx = -1;
7535 cinfo.output_bfd = output_bfd;
7536 cinfo.bed = bed;
7537
7538 /* Put all hash values in HASHCODES. */
7539 elf_link_hash_traverse (elf_hash_table (info),
7540 elf_collect_gnu_hash_codes, &cinfo);
7541 if (cinfo.error)
7542 {
7543 free (cinfo.hashcodes);
7544 return false;
7545 }
7546
7547 bucketcount
7548 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7549
7550 if (bucketcount == 0)
7551 {
7552 free (cinfo.hashcodes);
7553 return false;
7554 }
7555
7556 s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (bed));
7557 BFD_ASSERT (s != NULL);
7558
7559 if (cinfo.nsyms == 0)
7560 {
7561 /* Empty .gnu.hash or .MIPS.xhash section is special. */
7562 BFD_ASSERT (cinfo.min_dynindx == -1);
7563 free (cinfo.hashcodes);
7564 s->size = 5 * 4 + bed->s->arch_size / 8;
7565 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7566 if (contents == NULL)
7567 return false;
7568 s->contents = contents;
7569 /* 1 empty bucket. */
7570 bfd_put_32 (output_bfd, 1, contents);
7571 /* SYMIDX above the special symbol 0. */
7572 bfd_put_32 (output_bfd, 1, contents + 4);
7573 /* Just one word for bitmask. */
7574 bfd_put_32 (output_bfd, 1, contents + 8);
7575 /* Only hash fn bloom filter. */
7576 bfd_put_32 (output_bfd, 0, contents + 12);
7577 /* No hashes are valid - empty bitmask. */
7578 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7579 /* No hashes in the only bucket. */
7580 bfd_put_32 (output_bfd, 0,
7581 contents + 16 + bed->s->arch_size / 8);
7582 }
7583 else
7584 {
7585 unsigned long int maskwords, maskbitslog2, x;
7586 BFD_ASSERT (cinfo.min_dynindx != -1);
7587
7588 x = cinfo.nsyms;
7589 maskbitslog2 = 1;
7590 while ((x >>= 1) != 0)
7591 ++maskbitslog2;
7592 if (maskbitslog2 < 3)
7593 maskbitslog2 = 5;
7594 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7595 maskbitslog2 = maskbitslog2 + 3;
7596 else
7597 maskbitslog2 = maskbitslog2 + 2;
7598 if (bed->s->arch_size == 64)
7599 {
7600 if (maskbitslog2 == 5)
7601 maskbitslog2 = 6;
7602 cinfo.shift1 = 6;
7603 }
7604 else
7605 cinfo.shift1 = 5;
7606 cinfo.mask = (1 << cinfo.shift1) - 1;
7607 cinfo.shift2 = maskbitslog2;
7608 cinfo.maskbits = 1 << maskbitslog2;
7609 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7610 amt = bucketcount * sizeof (unsigned long int) * 2;
7611 amt += maskwords * sizeof (bfd_vma);
7612 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
7613 if (cinfo.bitmask == NULL)
7614 {
7615 free (cinfo.hashcodes);
7616 return false;
7617 }
7618
7619 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
7620 cinfo.indx = cinfo.counts + bucketcount;
7621 cinfo.symindx = dynsymcount - cinfo.nsyms;
7622 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7623
7624 /* Determine how often each hash bucket is used. */
7625 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7626 for (i = 0; i < cinfo.nsyms; ++i)
7627 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7628
7629 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7630 if (cinfo.counts[i] != 0)
7631 {
7632 cinfo.indx[i] = cnt;
7633 cnt += cinfo.counts[i];
7634 }
7635 BFD_ASSERT (cnt == dynsymcount);
7636 cinfo.bucketcount = bucketcount;
7637 cinfo.local_indx = cinfo.min_dynindx;
7638
7639 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7640 s->size += cinfo.maskbits / 8;
7641 if (bed->record_xhash_symbol != NULL)
7642 s->size += cinfo.nsyms * 4;
7643 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7644 if (contents == NULL)
7645 {
7646 free (cinfo.bitmask);
7647 free (cinfo.hashcodes);
7648 return false;
7649 }
7650
7651 s->contents = contents;
7652 bfd_put_32 (output_bfd, bucketcount, contents);
7653 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7654 bfd_put_32 (output_bfd, maskwords, contents + 8);
7655 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7656 contents += 16 + cinfo.maskbits / 8;
7657
7658 for (i = 0; i < bucketcount; ++i)
7659 {
7660 if (cinfo.counts[i] == 0)
7661 bfd_put_32 (output_bfd, 0, contents);
7662 else
7663 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7664 contents += 4;
7665 }
7666
7667 cinfo.contents = contents;
7668
7669 cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents;
7670 /* Renumber dynamic symbols, if populating .gnu.hash section.
7671 If using .MIPS.xhash, populate the translation table. */
7672 elf_link_hash_traverse (elf_hash_table (info),
7673 elf_gnu_hash_process_symidx, &cinfo);
7674
7675 contents = s->contents + 16;
7676 for (i = 0; i < maskwords; ++i)
7677 {
7678 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7679 contents);
7680 contents += bed->s->arch_size / 8;
7681 }
7682
7683 free (cinfo.bitmask);
7684 free (cinfo.hashcodes);
7685 }
7686 }
7687
7688 s = bfd_get_linker_section (dynobj, ".dynstr");
7689 BFD_ASSERT (s != NULL);
7690
7691 elf_finalize_dynstr (output_bfd, info);
7692
7693 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7694
7695 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7696 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7697 return false;
7698 }
7699
7700 return true;
7701 }
7702 \f
7703 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7704
7705 static void
7706 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7707 asection *sec)
7708 {
7709 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7710 sec->sec_info_type = SEC_INFO_TYPE_NONE;
7711 }
7712
7713 /* Finish SHF_MERGE section merging. */
7714
7715 bool
7716 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
7717 {
7718 bfd *ibfd;
7719 asection *sec;
7720
7721 if (!is_elf_hash_table (info->hash))
7722 return false;
7723
7724 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7725 if ((ibfd->flags & DYNAMIC) == 0
7726 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
7727 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7728 == get_elf_backend_data (obfd)->s->elfclass))
7729 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7730 if ((sec->flags & SEC_MERGE) != 0
7731 && !bfd_is_abs_section (sec->output_section))
7732 {
7733 struct bfd_elf_section_data *secdata;
7734
7735 secdata = elf_section_data (sec);
7736 if (! _bfd_add_merge_section (obfd,
7737 &elf_hash_table (info)->merge_info,
7738 sec, &secdata->sec_info))
7739 return false;
7740 else if (secdata->sec_info)
7741 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
7742 }
7743
7744 if (elf_hash_table (info)->merge_info != NULL)
7745 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
7746 merge_sections_remove_hook);
7747 return true;
7748 }
7749
7750 /* Create an entry in an ELF linker hash table. */
7751
7752 struct bfd_hash_entry *
7753 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7754 struct bfd_hash_table *table,
7755 const char *string)
7756 {
7757 /* Allocate the structure if it has not already been allocated by a
7758 subclass. */
7759 if (entry == NULL)
7760 {
7761 entry = (struct bfd_hash_entry *)
7762 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
7763 if (entry == NULL)
7764 return entry;
7765 }
7766
7767 /* Call the allocation method of the superclass. */
7768 entry = _bfd_link_hash_newfunc (entry, table, string);
7769 if (entry != NULL)
7770 {
7771 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7772 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7773
7774 /* Set local fields. */
7775 ret->indx = -1;
7776 ret->dynindx = -1;
7777 ret->got = htab->init_got_refcount;
7778 ret->plt = htab->init_plt_refcount;
7779 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7780 - offsetof (struct elf_link_hash_entry, size)));
7781 /* Assume that we have been called by a non-ELF symbol reader.
7782 This flag is then reset by the code which reads an ELF input
7783 file. This ensures that a symbol created by a non-ELF symbol
7784 reader will have the flag set correctly. */
7785 ret->non_elf = 1;
7786 }
7787
7788 return entry;
7789 }
7790
7791 /* Copy data from an indirect symbol to its direct symbol, hiding the
7792 old indirect symbol. Also used for copying flags to a weakdef. */
7793
7794 void
7795 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7796 struct elf_link_hash_entry *dir,
7797 struct elf_link_hash_entry *ind)
7798 {
7799 struct elf_link_hash_table *htab;
7800
7801 if (ind->dyn_relocs != NULL)
7802 {
7803 if (dir->dyn_relocs != NULL)
7804 {
7805 struct elf_dyn_relocs **pp;
7806 struct elf_dyn_relocs *p;
7807
7808 /* Add reloc counts against the indirect sym to the direct sym
7809 list. Merge any entries against the same section. */
7810 for (pp = &ind->dyn_relocs; (p = *pp) != NULL; )
7811 {
7812 struct elf_dyn_relocs *q;
7813
7814 for (q = dir->dyn_relocs; q != NULL; q = q->next)
7815 if (q->sec == p->sec)
7816 {
7817 q->pc_count += p->pc_count;
7818 q->count += p->count;
7819 *pp = p->next;
7820 break;
7821 }
7822 if (q == NULL)
7823 pp = &p->next;
7824 }
7825 *pp = dir->dyn_relocs;
7826 }
7827
7828 dir->dyn_relocs = ind->dyn_relocs;
7829 ind->dyn_relocs = NULL;
7830 }
7831
7832 /* Copy down any references that we may have already seen to the
7833 symbol which just became indirect. */
7834
7835 if (dir->versioned != versioned_hidden)
7836 dir->ref_dynamic |= ind->ref_dynamic;
7837 dir->ref_regular |= ind->ref_regular;
7838 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7839 dir->non_got_ref |= ind->non_got_ref;
7840 dir->needs_plt |= ind->needs_plt;
7841 dir->pointer_equality_needed |= ind->pointer_equality_needed;
7842
7843 if (ind->root.type != bfd_link_hash_indirect)
7844 return;
7845
7846 /* Copy over the global and procedure linkage table refcount entries.
7847 These may have been already set up by a check_relocs routine. */
7848 htab = elf_hash_table (info);
7849 if (ind->got.refcount > htab->init_got_refcount.refcount)
7850 {
7851 if (dir->got.refcount < 0)
7852 dir->got.refcount = 0;
7853 dir->got.refcount += ind->got.refcount;
7854 ind->got.refcount = htab->init_got_refcount.refcount;
7855 }
7856
7857 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7858 {
7859 if (dir->plt.refcount < 0)
7860 dir->plt.refcount = 0;
7861 dir->plt.refcount += ind->plt.refcount;
7862 ind->plt.refcount = htab->init_plt_refcount.refcount;
7863 }
7864
7865 if (ind->dynindx != -1)
7866 {
7867 if (dir->dynindx != -1)
7868 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7869 dir->dynindx = ind->dynindx;
7870 dir->dynstr_index = ind->dynstr_index;
7871 ind->dynindx = -1;
7872 ind->dynstr_index = 0;
7873 }
7874 }
7875
7876 void
7877 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7878 struct elf_link_hash_entry *h,
7879 bool force_local)
7880 {
7881 /* STT_GNU_IFUNC symbol must go through PLT. */
7882 if (h->type != STT_GNU_IFUNC)
7883 {
7884 h->plt = elf_hash_table (info)->init_plt_offset;
7885 h->needs_plt = 0;
7886 }
7887 if (force_local)
7888 {
7889 h->forced_local = 1;
7890 if (h->dynindx != -1)
7891 {
7892 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7893 h->dynstr_index);
7894 h->dynindx = -1;
7895 h->dynstr_index = 0;
7896 }
7897 }
7898 }
7899
7900 /* Hide a symbol. */
7901
7902 void
7903 _bfd_elf_link_hide_symbol (bfd *output_bfd,
7904 struct bfd_link_info *info,
7905 struct bfd_link_hash_entry *h)
7906 {
7907 if (is_elf_hash_table (info->hash))
7908 {
7909 const struct elf_backend_data *bed
7910 = get_elf_backend_data (output_bfd);
7911 struct elf_link_hash_entry *eh
7912 = (struct elf_link_hash_entry *) h;
7913 bed->elf_backend_hide_symbol (info, eh, true);
7914 eh->def_dynamic = 0;
7915 eh->ref_dynamic = 0;
7916 eh->dynamic_def = 0;
7917 }
7918 }
7919
7920 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7921 caller. */
7922
7923 bool
7924 _bfd_elf_link_hash_table_init
7925 (struct elf_link_hash_table *table,
7926 bfd *abfd,
7927 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7928 struct bfd_hash_table *,
7929 const char *),
7930 unsigned int entsize,
7931 enum elf_target_id target_id)
7932 {
7933 bool ret;
7934 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7935
7936 table->init_got_refcount.refcount = can_refcount - 1;
7937 table->init_plt_refcount.refcount = can_refcount - 1;
7938 table->init_got_offset.offset = -(bfd_vma) 1;
7939 table->init_plt_offset.offset = -(bfd_vma) 1;
7940 /* The first dynamic symbol is a dummy. */
7941 table->dynsymcount = 1;
7942
7943 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
7944
7945 table->root.type = bfd_link_elf_hash_table;
7946 table->hash_table_id = target_id;
7947 table->target_os = get_elf_backend_data (abfd)->target_os;
7948
7949 return ret;
7950 }
7951
7952 /* Create an ELF linker hash table. */
7953
7954 struct bfd_link_hash_table *
7955 _bfd_elf_link_hash_table_create (bfd *abfd)
7956 {
7957 struct elf_link_hash_table *ret;
7958 size_t amt = sizeof (struct elf_link_hash_table);
7959
7960 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
7961 if (ret == NULL)
7962 return NULL;
7963
7964 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
7965 sizeof (struct elf_link_hash_entry),
7966 GENERIC_ELF_DATA))
7967 {
7968 free (ret);
7969 return NULL;
7970 }
7971 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
7972
7973 return &ret->root;
7974 }
7975
7976 /* Destroy an ELF linker hash table. */
7977
7978 void
7979 _bfd_elf_link_hash_table_free (bfd *obfd)
7980 {
7981 struct elf_link_hash_table *htab;
7982
7983 htab = (struct elf_link_hash_table *) obfd->link.hash;
7984 if (htab->dynstr != NULL)
7985 _bfd_elf_strtab_free (htab->dynstr);
7986 _bfd_merge_sections_free (htab->merge_info);
7987 _bfd_generic_link_hash_table_free (obfd);
7988 }
7989
7990 /* This is a hook for the ELF emulation code in the generic linker to
7991 tell the backend linker what file name to use for the DT_NEEDED
7992 entry for a dynamic object. */
7993
7994 void
7995 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7996 {
7997 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7998 && bfd_get_format (abfd) == bfd_object)
7999 elf_dt_name (abfd) = name;
8000 }
8001
8002 int
8003 bfd_elf_get_dyn_lib_class (bfd *abfd)
8004 {
8005 int lib_class;
8006 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8007 && bfd_get_format (abfd) == bfd_object)
8008 lib_class = elf_dyn_lib_class (abfd);
8009 else
8010 lib_class = 0;
8011 return lib_class;
8012 }
8013
8014 void
8015 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
8016 {
8017 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8018 && bfd_get_format (abfd) == bfd_object)
8019 elf_dyn_lib_class (abfd) = lib_class;
8020 }
8021
8022 /* Get the list of DT_NEEDED entries for a link. This is a hook for
8023 the linker ELF emulation code. */
8024
8025 struct bfd_link_needed_list *
8026 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
8027 struct bfd_link_info *info)
8028 {
8029 if (! is_elf_hash_table (info->hash))
8030 return NULL;
8031 return elf_hash_table (info)->needed;
8032 }
8033
8034 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
8035 hook for the linker ELF emulation code. */
8036
8037 struct bfd_link_needed_list *
8038 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
8039 struct bfd_link_info *info)
8040 {
8041 if (! is_elf_hash_table (info->hash))
8042 return NULL;
8043 return elf_hash_table (info)->runpath;
8044 }
8045
8046 /* Get the name actually used for a dynamic object for a link. This
8047 is the SONAME entry if there is one. Otherwise, it is the string
8048 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
8049
8050 const char *
8051 bfd_elf_get_dt_soname (bfd *abfd)
8052 {
8053 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8054 && bfd_get_format (abfd) == bfd_object)
8055 return elf_dt_name (abfd);
8056 return NULL;
8057 }
8058
8059 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
8060 the ELF linker emulation code. */
8061
8062 bool
8063 bfd_elf_get_bfd_needed_list (bfd *abfd,
8064 struct bfd_link_needed_list **pneeded)
8065 {
8066 asection *s;
8067 bfd_byte *dynbuf = NULL;
8068 unsigned int elfsec;
8069 unsigned long shlink;
8070 bfd_byte *extdyn, *extdynend;
8071 size_t extdynsize;
8072 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
8073
8074 *pneeded = NULL;
8075
8076 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
8077 || bfd_get_format (abfd) != bfd_object)
8078 return true;
8079
8080 s = bfd_get_section_by_name (abfd, ".dynamic");
8081 if (s == NULL || s->size == 0)
8082 return true;
8083
8084 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
8085 goto error_return;
8086
8087 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
8088 if (elfsec == SHN_BAD)
8089 goto error_return;
8090
8091 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
8092
8093 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
8094 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
8095
8096 extdyn = dynbuf;
8097 extdynend = extdyn + s->size;
8098 for (; extdyn < extdynend; extdyn += extdynsize)
8099 {
8100 Elf_Internal_Dyn dyn;
8101
8102 (*swap_dyn_in) (abfd, extdyn, &dyn);
8103
8104 if (dyn.d_tag == DT_NULL)
8105 break;
8106
8107 if (dyn.d_tag == DT_NEEDED)
8108 {
8109 const char *string;
8110 struct bfd_link_needed_list *l;
8111 unsigned int tagv = dyn.d_un.d_val;
8112 size_t amt;
8113
8114 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
8115 if (string == NULL)
8116 goto error_return;
8117
8118 amt = sizeof *l;
8119 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
8120 if (l == NULL)
8121 goto error_return;
8122
8123 l->by = abfd;
8124 l->name = string;
8125 l->next = *pneeded;
8126 *pneeded = l;
8127 }
8128 }
8129
8130 free (dynbuf);
8131
8132 return true;
8133
8134 error_return:
8135 free (dynbuf);
8136 return false;
8137 }
8138
8139 struct elf_symbuf_symbol
8140 {
8141 unsigned long st_name; /* Symbol name, index in string tbl */
8142 unsigned char st_info; /* Type and binding attributes */
8143 unsigned char st_other; /* Visibilty, and target specific */
8144 };
8145
8146 struct elf_symbuf_head
8147 {
8148 struct elf_symbuf_symbol *ssym;
8149 size_t count;
8150 unsigned int st_shndx;
8151 };
8152
8153 struct elf_symbol
8154 {
8155 union
8156 {
8157 Elf_Internal_Sym *isym;
8158 struct elf_symbuf_symbol *ssym;
8159 void *p;
8160 } u;
8161 const char *name;
8162 };
8163
8164 /* Sort references to symbols by ascending section number. */
8165
8166 static int
8167 elf_sort_elf_symbol (const void *arg1, const void *arg2)
8168 {
8169 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
8170 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
8171
8172 if (s1->st_shndx != s2->st_shndx)
8173 return s1->st_shndx > s2->st_shndx ? 1 : -1;
8174 /* Final sort by the address of the sym in the symbuf ensures
8175 a stable sort. */
8176 if (s1 != s2)
8177 return s1 > s2 ? 1 : -1;
8178 return 0;
8179 }
8180
8181 static int
8182 elf_sym_name_compare (const void *arg1, const void *arg2)
8183 {
8184 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
8185 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
8186 int ret = strcmp (s1->name, s2->name);
8187 if (ret != 0)
8188 return ret;
8189 if (s1->u.p != s2->u.p)
8190 return s1->u.p > s2->u.p ? 1 : -1;
8191 return 0;
8192 }
8193
8194 static struct elf_symbuf_head *
8195 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
8196 {
8197 Elf_Internal_Sym **ind, **indbufend, **indbuf;
8198 struct elf_symbuf_symbol *ssym;
8199 struct elf_symbuf_head *ssymbuf, *ssymhead;
8200 size_t i, shndx_count, total_size, amt;
8201
8202 amt = symcount * sizeof (*indbuf);
8203 indbuf = (Elf_Internal_Sym **) bfd_malloc (amt);
8204 if (indbuf == NULL)
8205 return NULL;
8206
8207 for (ind = indbuf, i = 0; i < symcount; i++)
8208 if (isymbuf[i].st_shndx != SHN_UNDEF)
8209 *ind++ = &isymbuf[i];
8210 indbufend = ind;
8211
8212 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
8213 elf_sort_elf_symbol);
8214
8215 shndx_count = 0;
8216 if (indbufend > indbuf)
8217 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
8218 if (ind[0]->st_shndx != ind[1]->st_shndx)
8219 shndx_count++;
8220
8221 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
8222 + (indbufend - indbuf) * sizeof (*ssym));
8223 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
8224 if (ssymbuf == NULL)
8225 {
8226 free (indbuf);
8227 return NULL;
8228 }
8229
8230 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
8231 ssymbuf->ssym = NULL;
8232 ssymbuf->count = shndx_count;
8233 ssymbuf->st_shndx = 0;
8234 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
8235 {
8236 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
8237 {
8238 ssymhead++;
8239 ssymhead->ssym = ssym;
8240 ssymhead->count = 0;
8241 ssymhead->st_shndx = (*ind)->st_shndx;
8242 }
8243 ssym->st_name = (*ind)->st_name;
8244 ssym->st_info = (*ind)->st_info;
8245 ssym->st_other = (*ind)->st_other;
8246 ssymhead->count++;
8247 }
8248 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
8249 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
8250 == total_size));
8251
8252 free (indbuf);
8253 return ssymbuf;
8254 }
8255
8256 /* Check if 2 sections define the same set of local and global
8257 symbols. */
8258
8259 static bool
8260 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
8261 struct bfd_link_info *info)
8262 {
8263 bfd *bfd1, *bfd2;
8264 const struct elf_backend_data *bed1, *bed2;
8265 Elf_Internal_Shdr *hdr1, *hdr2;
8266 size_t symcount1, symcount2;
8267 Elf_Internal_Sym *isymbuf1, *isymbuf2;
8268 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
8269 Elf_Internal_Sym *isym, *isymend;
8270 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
8271 size_t count1, count2, sec_count1, sec_count2, i;
8272 unsigned int shndx1, shndx2;
8273 bool result;
8274 bool ignore_section_symbol_p;
8275
8276 bfd1 = sec1->owner;
8277 bfd2 = sec2->owner;
8278
8279 /* Both sections have to be in ELF. */
8280 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
8281 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
8282 return false;
8283
8284 if (elf_section_type (sec1) != elf_section_type (sec2))
8285 return false;
8286
8287 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
8288 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
8289 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
8290 return false;
8291
8292 bed1 = get_elf_backend_data (bfd1);
8293 bed2 = get_elf_backend_data (bfd2);
8294 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
8295 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
8296 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
8297 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
8298
8299 if (symcount1 == 0 || symcount2 == 0)
8300 return false;
8301
8302 result = false;
8303 isymbuf1 = NULL;
8304 isymbuf2 = NULL;
8305 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
8306 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
8307
8308 /* Ignore section symbols only when matching non-debugging sections
8309 or linkonce section with comdat section. */
8310 ignore_section_symbol_p
8311 = ((sec1->flags & SEC_DEBUGGING) == 0
8312 || ((elf_section_flags (sec1) & SHF_GROUP)
8313 != (elf_section_flags (sec2) & SHF_GROUP)));
8314
8315 if (ssymbuf1 == NULL)
8316 {
8317 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
8318 NULL, NULL, NULL);
8319 if (isymbuf1 == NULL)
8320 goto done;
8321
8322 if (info != NULL && !info->reduce_memory_overheads)
8323 {
8324 ssymbuf1 = elf_create_symbuf (symcount1, isymbuf1);
8325 elf_tdata (bfd1)->symbuf = ssymbuf1;
8326 }
8327 }
8328
8329 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
8330 {
8331 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
8332 NULL, NULL, NULL);
8333 if (isymbuf2 == NULL)
8334 goto done;
8335
8336 if (ssymbuf1 != NULL && info != NULL && !info->reduce_memory_overheads)
8337 {
8338 ssymbuf2 = elf_create_symbuf (symcount2, isymbuf2);
8339 elf_tdata (bfd2)->symbuf = ssymbuf2;
8340 }
8341 }
8342
8343 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
8344 {
8345 /* Optimized faster version. */
8346 size_t lo, hi, mid;
8347 struct elf_symbol *symp;
8348 struct elf_symbuf_symbol *ssym, *ssymend;
8349
8350 lo = 0;
8351 hi = ssymbuf1->count;
8352 ssymbuf1++;
8353 count1 = 0;
8354 sec_count1 = 0;
8355 while (lo < hi)
8356 {
8357 mid = (lo + hi) / 2;
8358 if (shndx1 < ssymbuf1[mid].st_shndx)
8359 hi = mid;
8360 else if (shndx1 > ssymbuf1[mid].st_shndx)
8361 lo = mid + 1;
8362 else
8363 {
8364 count1 = ssymbuf1[mid].count;
8365 ssymbuf1 += mid;
8366 break;
8367 }
8368 }
8369 if (ignore_section_symbol_p)
8370 {
8371 for (i = 0; i < count1; i++)
8372 if (ELF_ST_TYPE (ssymbuf1->ssym[i].st_info) == STT_SECTION)
8373 sec_count1++;
8374 count1 -= sec_count1;
8375 }
8376
8377 lo = 0;
8378 hi = ssymbuf2->count;
8379 ssymbuf2++;
8380 count2 = 0;
8381 sec_count2 = 0;
8382 while (lo < hi)
8383 {
8384 mid = (lo + hi) / 2;
8385 if (shndx2 < ssymbuf2[mid].st_shndx)
8386 hi = mid;
8387 else if (shndx2 > ssymbuf2[mid].st_shndx)
8388 lo = mid + 1;
8389 else
8390 {
8391 count2 = ssymbuf2[mid].count;
8392 ssymbuf2 += mid;
8393 break;
8394 }
8395 }
8396 if (ignore_section_symbol_p)
8397 {
8398 for (i = 0; i < count2; i++)
8399 if (ELF_ST_TYPE (ssymbuf2->ssym[i].st_info) == STT_SECTION)
8400 sec_count2++;
8401 count2 -= sec_count2;
8402 }
8403
8404 if (count1 == 0 || count2 == 0 || count1 != count2)
8405 goto done;
8406
8407 symtable1
8408 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
8409 symtable2
8410 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
8411 if (symtable1 == NULL || symtable2 == NULL)
8412 goto done;
8413
8414 symp = symtable1;
8415 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1 + sec_count1;
8416 ssym < ssymend; ssym++)
8417 if (sec_count1 == 0
8418 || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
8419 {
8420 symp->u.ssym = ssym;
8421 symp->name = bfd_elf_string_from_elf_section (bfd1,
8422 hdr1->sh_link,
8423 ssym->st_name);
8424 symp++;
8425 }
8426
8427 symp = symtable2;
8428 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2 + sec_count2;
8429 ssym < ssymend; ssym++)
8430 if (sec_count2 == 0
8431 || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
8432 {
8433 symp->u.ssym = ssym;
8434 symp->name = bfd_elf_string_from_elf_section (bfd2,
8435 hdr2->sh_link,
8436 ssym->st_name);
8437 symp++;
8438 }
8439
8440 /* Sort symbol by name. */
8441 qsort (symtable1, count1, sizeof (struct elf_symbol),
8442 elf_sym_name_compare);
8443 qsort (symtable2, count1, sizeof (struct elf_symbol),
8444 elf_sym_name_compare);
8445
8446 for (i = 0; i < count1; i++)
8447 /* Two symbols must have the same binding, type and name. */
8448 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8449 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8450 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8451 goto done;
8452
8453 result = true;
8454 goto done;
8455 }
8456
8457 symtable1 = (struct elf_symbol *)
8458 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8459 symtable2 = (struct elf_symbol *)
8460 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
8461 if (symtable1 == NULL || symtable2 == NULL)
8462 goto done;
8463
8464 /* Count definitions in the section. */
8465 count1 = 0;
8466 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
8467 if (isym->st_shndx == shndx1
8468 && (!ignore_section_symbol_p
8469 || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
8470 symtable1[count1++].u.isym = isym;
8471
8472 count2 = 0;
8473 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
8474 if (isym->st_shndx == shndx2
8475 && (!ignore_section_symbol_p
8476 || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
8477 symtable2[count2++].u.isym = isym;
8478
8479 if (count1 == 0 || count2 == 0 || count1 != count2)
8480 goto done;
8481
8482 for (i = 0; i < count1; i++)
8483 symtable1[i].name
8484 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8485 symtable1[i].u.isym->st_name);
8486
8487 for (i = 0; i < count2; i++)
8488 symtable2[i].name
8489 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8490 symtable2[i].u.isym->st_name);
8491
8492 /* Sort symbol by name. */
8493 qsort (symtable1, count1, sizeof (struct elf_symbol),
8494 elf_sym_name_compare);
8495 qsort (symtable2, count1, sizeof (struct elf_symbol),
8496 elf_sym_name_compare);
8497
8498 for (i = 0; i < count1; i++)
8499 /* Two symbols must have the same binding, type and name. */
8500 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8501 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8502 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8503 goto done;
8504
8505 result = true;
8506
8507 done:
8508 free (symtable1);
8509 free (symtable2);
8510 free (isymbuf1);
8511 free (isymbuf2);
8512
8513 return result;
8514 }
8515
8516 /* Return TRUE if 2 section types are compatible. */
8517
8518 bool
8519 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8520 bfd *bbfd, const asection *bsec)
8521 {
8522 if (asec == NULL
8523 || bsec == NULL
8524 || abfd->xvec->flavour != bfd_target_elf_flavour
8525 || bbfd->xvec->flavour != bfd_target_elf_flavour)
8526 return true;
8527
8528 return elf_section_type (asec) == elf_section_type (bsec);
8529 }
8530 \f
8531 /* Final phase of ELF linker. */
8532
8533 /* A structure we use to avoid passing large numbers of arguments. */
8534
8535 struct elf_final_link_info
8536 {
8537 /* General link information. */
8538 struct bfd_link_info *info;
8539 /* Output BFD. */
8540 bfd *output_bfd;
8541 /* Symbol string table. */
8542 struct elf_strtab_hash *symstrtab;
8543 /* .hash section. */
8544 asection *hash_sec;
8545 /* symbol version section (.gnu.version). */
8546 asection *symver_sec;
8547 /* Buffer large enough to hold contents of any section. */
8548 bfd_byte *contents;
8549 /* Buffer large enough to hold external relocs of any section. */
8550 void *external_relocs;
8551 /* Buffer large enough to hold internal relocs of any section. */
8552 Elf_Internal_Rela *internal_relocs;
8553 /* Buffer large enough to hold external local symbols of any input
8554 BFD. */
8555 bfd_byte *external_syms;
8556 /* And a buffer for symbol section indices. */
8557 Elf_External_Sym_Shndx *locsym_shndx;
8558 /* Buffer large enough to hold internal local symbols of any input
8559 BFD. */
8560 Elf_Internal_Sym *internal_syms;
8561 /* Array large enough to hold a symbol index for each local symbol
8562 of any input BFD. */
8563 long *indices;
8564 /* Array large enough to hold a section pointer for each local
8565 symbol of any input BFD. */
8566 asection **sections;
8567 /* Buffer for SHT_SYMTAB_SHNDX section. */
8568 Elf_External_Sym_Shndx *symshndxbuf;
8569 /* Number of STT_FILE syms seen. */
8570 size_t filesym_count;
8571 /* Local symbol hash table. */
8572 struct bfd_hash_table local_hash_table;
8573 };
8574
8575 struct local_hash_entry
8576 {
8577 /* Base hash table entry structure. */
8578 struct bfd_hash_entry root;
8579 /* Size of the local symbol name. */
8580 size_t size;
8581 /* Number of the duplicated local symbol names. */
8582 long count;
8583 };
8584
8585 /* Create an entry in the local symbol hash table. */
8586
8587 static struct bfd_hash_entry *
8588 local_hash_newfunc (struct bfd_hash_entry *entry,
8589 struct bfd_hash_table *table,
8590 const char *string)
8591 {
8592
8593 /* Allocate the structure if it has not already been allocated by a
8594 subclass. */
8595 if (entry == NULL)
8596 {
8597 entry = bfd_hash_allocate (table,
8598 sizeof (struct local_hash_entry));
8599 if (entry == NULL)
8600 return entry;
8601 }
8602
8603 /* Call the allocation method of the superclass. */
8604 entry = bfd_hash_newfunc (entry, table, string);
8605 if (entry != NULL)
8606 {
8607 ((struct local_hash_entry *) entry)->count = 0;
8608 ((struct local_hash_entry *) entry)->size = 0;
8609 }
8610
8611 return entry;
8612 }
8613
8614 /* This struct is used to pass information to elf_link_output_extsym. */
8615
8616 struct elf_outext_info
8617 {
8618 bool failed;
8619 bool localsyms;
8620 bool file_sym_done;
8621 struct elf_final_link_info *flinfo;
8622 };
8623
8624
8625 /* Support for evaluating a complex relocation.
8626
8627 Complex relocations are generalized, self-describing relocations. The
8628 implementation of them consists of two parts: complex symbols, and the
8629 relocations themselves.
8630
8631 The relocations use a reserved elf-wide relocation type code (R_RELC
8632 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8633 information (start bit, end bit, word width, etc) into the addend. This
8634 information is extracted from CGEN-generated operand tables within gas.
8635
8636 Complex symbols are mangled symbols (STT_RELC external / BSF_RELC
8637 internal) representing prefix-notation expressions, including but not
8638 limited to those sorts of expressions normally encoded as addends in the
8639 addend field. The symbol mangling format is:
8640
8641 <node> := <literal>
8642 | <unary-operator> ':' <node>
8643 | <binary-operator> ':' <node> ':' <node>
8644 ;
8645
8646 <literal> := 's' <digits=N> ':' <N character symbol name>
8647 | 'S' <digits=N> ':' <N character section name>
8648 | '#' <hexdigits>
8649 ;
8650
8651 <binary-operator> := as in C
8652 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8653
8654 static void
8655 set_symbol_value (bfd *bfd_with_globals,
8656 Elf_Internal_Sym *isymbuf,
8657 size_t locsymcount,
8658 size_t symidx,
8659 bfd_vma val)
8660 {
8661 struct elf_link_hash_entry **sym_hashes;
8662 struct elf_link_hash_entry *h;
8663 size_t extsymoff = locsymcount;
8664
8665 if (symidx < locsymcount)
8666 {
8667 Elf_Internal_Sym *sym;
8668
8669 sym = isymbuf + symidx;
8670 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8671 {
8672 /* It is a local symbol: move it to the
8673 "absolute" section and give it a value. */
8674 sym->st_shndx = SHN_ABS;
8675 sym->st_value = val;
8676 return;
8677 }
8678 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8679 extsymoff = 0;
8680 }
8681
8682 /* It is a global symbol: set its link type
8683 to "defined" and give it a value. */
8684
8685 sym_hashes = elf_sym_hashes (bfd_with_globals);
8686 h = sym_hashes [symidx - extsymoff];
8687 while (h->root.type == bfd_link_hash_indirect
8688 || h->root.type == bfd_link_hash_warning)
8689 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8690 h->root.type = bfd_link_hash_defined;
8691 h->root.u.def.value = val;
8692 h->root.u.def.section = bfd_abs_section_ptr;
8693 }
8694
8695 static bool
8696 resolve_symbol (const char *name,
8697 bfd *input_bfd,
8698 struct elf_final_link_info *flinfo,
8699 bfd_vma *result,
8700 Elf_Internal_Sym *isymbuf,
8701 size_t locsymcount)
8702 {
8703 Elf_Internal_Sym *sym;
8704 struct bfd_link_hash_entry *global_entry;
8705 const char *candidate = NULL;
8706 Elf_Internal_Shdr *symtab_hdr;
8707 size_t i;
8708
8709 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8710
8711 for (i = 0; i < locsymcount; ++ i)
8712 {
8713 sym = isymbuf + i;
8714
8715 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8716 continue;
8717
8718 candidate = bfd_elf_string_from_elf_section (input_bfd,
8719 symtab_hdr->sh_link,
8720 sym->st_name);
8721 #ifdef DEBUG
8722 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8723 name, candidate, (unsigned long) sym->st_value);
8724 #endif
8725 if (candidate && strcmp (candidate, name) == 0)
8726 {
8727 asection *sec = flinfo->sections [i];
8728
8729 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8730 *result += sec->output_offset + sec->output_section->vma;
8731 #ifdef DEBUG
8732 printf ("Found symbol with value %8.8lx\n",
8733 (unsigned long) *result);
8734 #endif
8735 return true;
8736 }
8737 }
8738
8739 /* Hmm, haven't found it yet. perhaps it is a global. */
8740 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
8741 false, false, true);
8742 if (!global_entry)
8743 return false;
8744
8745 if (global_entry->type == bfd_link_hash_defined
8746 || global_entry->type == bfd_link_hash_defweak)
8747 {
8748 *result = (global_entry->u.def.value
8749 + global_entry->u.def.section->output_section->vma
8750 + global_entry->u.def.section->output_offset);
8751 #ifdef DEBUG
8752 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8753 global_entry->root.string, (unsigned long) *result);
8754 #endif
8755 return true;
8756 }
8757
8758 return false;
8759 }
8760
8761 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8762 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8763 names like "foo.end" which is the end address of section "foo". */
8764
8765 static bool
8766 resolve_section (const char *name,
8767 asection *sections,
8768 bfd_vma *result,
8769 bfd * abfd)
8770 {
8771 asection *curr;
8772 unsigned int len;
8773
8774 for (curr = sections; curr; curr = curr->next)
8775 if (strcmp (curr->name, name) == 0)
8776 {
8777 *result = curr->vma;
8778 return true;
8779 }
8780
8781 /* Hmm. still haven't found it. try pseudo-section names. */
8782 /* FIXME: This could be coded more efficiently... */
8783 for (curr = sections; curr; curr = curr->next)
8784 {
8785 len = strlen (curr->name);
8786 if (len > strlen (name))
8787 continue;
8788
8789 if (strncmp (curr->name, name, len) == 0)
8790 {
8791 if (startswith (name + len, ".end"))
8792 {
8793 *result = (curr->vma
8794 + curr->size / bfd_octets_per_byte (abfd, curr));
8795 return true;
8796 }
8797
8798 /* Insert more pseudo-section names here, if you like. */
8799 }
8800 }
8801
8802 return false;
8803 }
8804
8805 static void
8806 undefined_reference (const char *reftype, const char *name)
8807 {
8808 /* xgettext:c-format */
8809 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8810 reftype, name);
8811 bfd_set_error (bfd_error_bad_value);
8812 }
8813
8814 static bool
8815 eval_symbol (bfd_vma *result,
8816 const char **symp,
8817 bfd *input_bfd,
8818 struct elf_final_link_info *flinfo,
8819 bfd_vma dot,
8820 Elf_Internal_Sym *isymbuf,
8821 size_t locsymcount,
8822 int signed_p)
8823 {
8824 size_t len;
8825 size_t symlen;
8826 bfd_vma a;
8827 bfd_vma b;
8828 char symbuf[4096];
8829 const char *sym = *symp;
8830 const char *symend;
8831 bool symbol_is_section = false;
8832
8833 len = strlen (sym);
8834 symend = sym + len;
8835
8836 if (len < 1 || len > sizeof (symbuf))
8837 {
8838 bfd_set_error (bfd_error_invalid_operation);
8839 return false;
8840 }
8841
8842 switch (* sym)
8843 {
8844 case '.':
8845 *result = dot;
8846 *symp = sym + 1;
8847 return true;
8848
8849 case '#':
8850 ++sym;
8851 *result = strtoul (sym, (char **) symp, 16);
8852 return true;
8853
8854 case 'S':
8855 symbol_is_section = true;
8856 /* Fall through. */
8857 case 's':
8858 ++sym;
8859 symlen = strtol (sym, (char **) symp, 10);
8860 sym = *symp + 1; /* Skip the trailing ':'. */
8861
8862 if (symend < sym || symlen + 1 > sizeof (symbuf))
8863 {
8864 bfd_set_error (bfd_error_invalid_operation);
8865 return false;
8866 }
8867
8868 memcpy (symbuf, sym, symlen);
8869 symbuf[symlen] = '\0';
8870 *symp = sym + symlen;
8871
8872 /* Is it always possible, with complex symbols, that gas "mis-guessed"
8873 the symbol as a section, or vice-versa. so we're pretty liberal in our
8874 interpretation here; section means "try section first", not "must be a
8875 section", and likewise with symbol. */
8876
8877 if (symbol_is_section)
8878 {
8879 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8880 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8881 isymbuf, locsymcount))
8882 {
8883 undefined_reference ("section", symbuf);
8884 return false;
8885 }
8886 }
8887 else
8888 {
8889 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8890 isymbuf, locsymcount)
8891 && !resolve_section (symbuf, flinfo->output_bfd->sections,
8892 result, input_bfd))
8893 {
8894 undefined_reference ("symbol", symbuf);
8895 return false;
8896 }
8897 }
8898
8899 return true;
8900
8901 /* All that remains are operators. */
8902
8903 #define UNARY_OP(op) \
8904 if (startswith (sym, #op)) \
8905 { \
8906 sym += strlen (#op); \
8907 if (*sym == ':') \
8908 ++sym; \
8909 *symp = sym; \
8910 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8911 isymbuf, locsymcount, signed_p)) \
8912 return false; \
8913 if (signed_p) \
8914 *result = op ((bfd_signed_vma) a); \
8915 else \
8916 *result = op a; \
8917 return true; \
8918 }
8919
8920 #define BINARY_OP_HEAD(op) \
8921 if (startswith (sym, #op)) \
8922 { \
8923 sym += strlen (#op); \
8924 if (*sym == ':') \
8925 ++sym; \
8926 *symp = sym; \
8927 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8928 isymbuf, locsymcount, signed_p)) \
8929 return false; \
8930 ++*symp; \
8931 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8932 isymbuf, locsymcount, signed_p)) \
8933 return false;
8934 #define BINARY_OP_TAIL(op) \
8935 if (signed_p) \
8936 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8937 else \
8938 *result = a op b; \
8939 return true; \
8940 }
8941 #define BINARY_OP(op) BINARY_OP_HEAD(op) BINARY_OP_TAIL(op)
8942
8943 default:
8944 UNARY_OP (0-);
8945 BINARY_OP_HEAD (<<);
8946 if (b >= sizeof (a) * CHAR_BIT)
8947 {
8948 *result = 0;
8949 return true;
8950 }
8951 signed_p = 0;
8952 BINARY_OP_TAIL (<<);
8953 BINARY_OP_HEAD (>>);
8954 if (b >= sizeof (a) * CHAR_BIT)
8955 {
8956 *result = signed_p && (bfd_signed_vma) a < 0 ? -1 : 0;
8957 return true;
8958 }
8959 BINARY_OP_TAIL (>>);
8960 BINARY_OP (==);
8961 BINARY_OP (!=);
8962 BINARY_OP (<=);
8963 BINARY_OP (>=);
8964 BINARY_OP (&&);
8965 BINARY_OP (||);
8966 UNARY_OP (~);
8967 UNARY_OP (!);
8968 BINARY_OP (*);
8969 BINARY_OP_HEAD (/);
8970 if (b == 0)
8971 {
8972 _bfd_error_handler (_("division by zero"));
8973 bfd_set_error (bfd_error_bad_value);
8974 return false;
8975 }
8976 BINARY_OP_TAIL (/);
8977 BINARY_OP_HEAD (%);
8978 if (b == 0)
8979 {
8980 _bfd_error_handler (_("division by zero"));
8981 bfd_set_error (bfd_error_bad_value);
8982 return false;
8983 }
8984 BINARY_OP_TAIL (%);
8985 BINARY_OP (^);
8986 BINARY_OP (|);
8987 BINARY_OP (&);
8988 BINARY_OP (+);
8989 BINARY_OP (-);
8990 BINARY_OP (<);
8991 BINARY_OP (>);
8992 #undef UNARY_OP
8993 #undef BINARY_OP
8994 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8995 bfd_set_error (bfd_error_invalid_operation);
8996 return false;
8997 }
8998 }
8999
9000 static void
9001 put_value (bfd_vma size,
9002 unsigned long chunksz,
9003 bfd *input_bfd,
9004 bfd_vma x,
9005 bfd_byte *location)
9006 {
9007 location += (size - chunksz);
9008
9009 for (; size; size -= chunksz, location -= chunksz)
9010 {
9011 switch (chunksz)
9012 {
9013 case 1:
9014 bfd_put_8 (input_bfd, x, location);
9015 x >>= 8;
9016 break;
9017 case 2:
9018 bfd_put_16 (input_bfd, x, location);
9019 x >>= 16;
9020 break;
9021 case 4:
9022 bfd_put_32 (input_bfd, x, location);
9023 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
9024 x >>= 16;
9025 x >>= 16;
9026 break;
9027 #ifdef BFD64
9028 case 8:
9029 bfd_put_64 (input_bfd, x, location);
9030 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
9031 x >>= 32;
9032 x >>= 32;
9033 break;
9034 #endif
9035 default:
9036 abort ();
9037 break;
9038 }
9039 }
9040 }
9041
9042 static bfd_vma
9043 get_value (bfd_vma size,
9044 unsigned long chunksz,
9045 bfd *input_bfd,
9046 bfd_byte *location)
9047 {
9048 int shift;
9049 bfd_vma x = 0;
9050
9051 /* Sanity checks. */
9052 BFD_ASSERT (chunksz <= sizeof (x)
9053 && size >= chunksz
9054 && chunksz != 0
9055 && (size % chunksz) == 0
9056 && input_bfd != NULL
9057 && location != NULL);
9058
9059 if (chunksz == sizeof (x))
9060 {
9061 BFD_ASSERT (size == chunksz);
9062
9063 /* Make sure that we do not perform an undefined shift operation.
9064 We know that size == chunksz so there will only be one iteration
9065 of the loop below. */
9066 shift = 0;
9067 }
9068 else
9069 shift = 8 * chunksz;
9070
9071 for (; size; size -= chunksz, location += chunksz)
9072 {
9073 switch (chunksz)
9074 {
9075 case 1:
9076 x = (x << shift) | bfd_get_8 (input_bfd, location);
9077 break;
9078 case 2:
9079 x = (x << shift) | bfd_get_16 (input_bfd, location);
9080 break;
9081 case 4:
9082 x = (x << shift) | bfd_get_32 (input_bfd, location);
9083 break;
9084 #ifdef BFD64
9085 case 8:
9086 x = (x << shift) | bfd_get_64 (input_bfd, location);
9087 break;
9088 #endif
9089 default:
9090 abort ();
9091 }
9092 }
9093 return x;
9094 }
9095
9096 static void
9097 decode_complex_addend (unsigned long *start, /* in bits */
9098 unsigned long *oplen, /* in bits */
9099 unsigned long *len, /* in bits */
9100 unsigned long *wordsz, /* in bytes */
9101 unsigned long *chunksz, /* in bytes */
9102 unsigned long *lsb0_p,
9103 unsigned long *signed_p,
9104 unsigned long *trunc_p,
9105 unsigned long encoded)
9106 {
9107 * start = encoded & 0x3F;
9108 * len = (encoded >> 6) & 0x3F;
9109 * oplen = (encoded >> 12) & 0x3F;
9110 * wordsz = (encoded >> 18) & 0xF;
9111 * chunksz = (encoded >> 22) & 0xF;
9112 * lsb0_p = (encoded >> 27) & 1;
9113 * signed_p = (encoded >> 28) & 1;
9114 * trunc_p = (encoded >> 29) & 1;
9115 }
9116
9117 bfd_reloc_status_type
9118 bfd_elf_perform_complex_relocation (bfd *input_bfd,
9119 asection *input_section,
9120 bfd_byte *contents,
9121 Elf_Internal_Rela *rel,
9122 bfd_vma relocation)
9123 {
9124 bfd_vma shift, x, mask;
9125 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
9126 bfd_reloc_status_type r;
9127 bfd_size_type octets;
9128
9129 /* Perform this reloc, since it is complex.
9130 (this is not to say that it necessarily refers to a complex
9131 symbol; merely that it is a self-describing CGEN based reloc.
9132 i.e. the addend has the complete reloc information (bit start, end,
9133 word size, etc) encoded within it.). */
9134
9135 decode_complex_addend (&start, &oplen, &len, &wordsz,
9136 &chunksz, &lsb0_p, &signed_p,
9137 &trunc_p, rel->r_addend);
9138
9139 mask = (((1L << (len - 1)) - 1) << 1) | 1;
9140
9141 if (lsb0_p)
9142 shift = (start + 1) - len;
9143 else
9144 shift = (8 * wordsz) - (start + len);
9145
9146 octets = rel->r_offset * bfd_octets_per_byte (input_bfd, input_section);
9147 x = get_value (wordsz, chunksz, input_bfd, contents + octets);
9148
9149 #ifdef DEBUG
9150 printf ("Doing complex reloc: "
9151 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
9152 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
9153 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
9154 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9155 oplen, (unsigned long) x, (unsigned long) mask,
9156 (unsigned long) relocation);
9157 #endif
9158
9159 r = bfd_reloc_ok;
9160 if (! trunc_p)
9161 /* Now do an overflow check. */
9162 r = bfd_check_overflow ((signed_p
9163 ? complain_overflow_signed
9164 : complain_overflow_unsigned),
9165 len, 0, (8 * wordsz),
9166 relocation);
9167
9168 /* Do the deed. */
9169 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
9170
9171 #ifdef DEBUG
9172 printf (" relocation: %8.8lx\n"
9173 " shifted mask: %8.8lx\n"
9174 " shifted/masked reloc: %8.8lx\n"
9175 " result: %8.8lx\n",
9176 (unsigned long) relocation, (unsigned long) (mask << shift),
9177 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
9178 #endif
9179 put_value (wordsz, chunksz, input_bfd, x, contents + octets);
9180 return r;
9181 }
9182
9183 /* Functions to read r_offset from external (target order) reloc
9184 entry. Faster than bfd_getl32 et al, because we let the compiler
9185 know the value is aligned. */
9186
9187 static bfd_vma
9188 ext32l_r_offset (const void *p)
9189 {
9190 union aligned32
9191 {
9192 uint32_t v;
9193 unsigned char c[4];
9194 };
9195 const union aligned32 *a
9196 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9197
9198 uint32_t aval = ( (uint32_t) a->c[0]
9199 | (uint32_t) a->c[1] << 8
9200 | (uint32_t) a->c[2] << 16
9201 | (uint32_t) a->c[3] << 24);
9202 return aval;
9203 }
9204
9205 static bfd_vma
9206 ext32b_r_offset (const void *p)
9207 {
9208 union aligned32
9209 {
9210 uint32_t v;
9211 unsigned char c[4];
9212 };
9213 const union aligned32 *a
9214 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9215
9216 uint32_t aval = ( (uint32_t) a->c[0] << 24
9217 | (uint32_t) a->c[1] << 16
9218 | (uint32_t) a->c[2] << 8
9219 | (uint32_t) a->c[3]);
9220 return aval;
9221 }
9222
9223 #ifdef BFD_HOST_64_BIT
9224 static bfd_vma
9225 ext64l_r_offset (const void *p)
9226 {
9227 union aligned64
9228 {
9229 uint64_t v;
9230 unsigned char c[8];
9231 };
9232 const union aligned64 *a
9233 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9234
9235 uint64_t aval = ( (uint64_t) a->c[0]
9236 | (uint64_t) a->c[1] << 8
9237 | (uint64_t) a->c[2] << 16
9238 | (uint64_t) a->c[3] << 24
9239 | (uint64_t) a->c[4] << 32
9240 | (uint64_t) a->c[5] << 40
9241 | (uint64_t) a->c[6] << 48
9242 | (uint64_t) a->c[7] << 56);
9243 return aval;
9244 }
9245
9246 static bfd_vma
9247 ext64b_r_offset (const void *p)
9248 {
9249 union aligned64
9250 {
9251 uint64_t v;
9252 unsigned char c[8];
9253 };
9254 const union aligned64 *a
9255 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9256
9257 uint64_t aval = ( (uint64_t) a->c[0] << 56
9258 | (uint64_t) a->c[1] << 48
9259 | (uint64_t) a->c[2] << 40
9260 | (uint64_t) a->c[3] << 32
9261 | (uint64_t) a->c[4] << 24
9262 | (uint64_t) a->c[5] << 16
9263 | (uint64_t) a->c[6] << 8
9264 | (uint64_t) a->c[7]);
9265 return aval;
9266 }
9267 #endif
9268
9269 /* When performing a relocatable link, the input relocations are
9270 preserved. But, if they reference global symbols, the indices
9271 referenced must be updated. Update all the relocations found in
9272 RELDATA. */
9273
9274 static bool
9275 elf_link_adjust_relocs (bfd *abfd,
9276 asection *sec,
9277 struct bfd_elf_section_reloc_data *reldata,
9278 bool sort,
9279 struct bfd_link_info *info)
9280 {
9281 unsigned int i;
9282 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9283 bfd_byte *erela;
9284 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9285 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9286 bfd_vma r_type_mask;
9287 int r_sym_shift;
9288 unsigned int count = reldata->count;
9289 struct elf_link_hash_entry **rel_hash = reldata->hashes;
9290
9291 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
9292 {
9293 swap_in = bed->s->swap_reloc_in;
9294 swap_out = bed->s->swap_reloc_out;
9295 }
9296 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
9297 {
9298 swap_in = bed->s->swap_reloca_in;
9299 swap_out = bed->s->swap_reloca_out;
9300 }
9301 else
9302 abort ();
9303
9304 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
9305 abort ();
9306
9307 if (bed->s->arch_size == 32)
9308 {
9309 r_type_mask = 0xff;
9310 r_sym_shift = 8;
9311 }
9312 else
9313 {
9314 r_type_mask = 0xffffffff;
9315 r_sym_shift = 32;
9316 }
9317
9318 erela = reldata->hdr->contents;
9319 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
9320 {
9321 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
9322 unsigned int j;
9323
9324 if (*rel_hash == NULL)
9325 continue;
9326
9327 if ((*rel_hash)->indx == -2
9328 && info->gc_sections
9329 && ! info->gc_keep_exported)
9330 {
9331 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9332 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
9333 abfd, sec,
9334 (*rel_hash)->root.root.string);
9335 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
9336 abfd, sec);
9337 bfd_set_error (bfd_error_invalid_operation);
9338 return false;
9339 }
9340 BFD_ASSERT ((*rel_hash)->indx >= 0);
9341
9342 (*swap_in) (abfd, erela, irela);
9343 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
9344 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
9345 | (irela[j].r_info & r_type_mask));
9346 (*swap_out) (abfd, irela, erela);
9347 }
9348
9349 if (bed->elf_backend_update_relocs)
9350 (*bed->elf_backend_update_relocs) (sec, reldata);
9351
9352 if (sort && count != 0)
9353 {
9354 bfd_vma (*ext_r_off) (const void *);
9355 bfd_vma r_off;
9356 size_t elt_size;
9357 bfd_byte *base, *end, *p, *loc;
9358 bfd_byte *buf = NULL;
9359
9360 if (bed->s->arch_size == 32)
9361 {
9362 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9363 ext_r_off = ext32l_r_offset;
9364 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9365 ext_r_off = ext32b_r_offset;
9366 else
9367 abort ();
9368 }
9369 else
9370 {
9371 #ifdef BFD_HOST_64_BIT
9372 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9373 ext_r_off = ext64l_r_offset;
9374 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9375 ext_r_off = ext64b_r_offset;
9376 else
9377 #endif
9378 abort ();
9379 }
9380
9381 /* Must use a stable sort here. A modified insertion sort,
9382 since the relocs are mostly sorted already. */
9383 elt_size = reldata->hdr->sh_entsize;
9384 base = reldata->hdr->contents;
9385 end = base + count * elt_size;
9386 if (elt_size > sizeof (Elf64_External_Rela))
9387 abort ();
9388
9389 /* Ensure the first element is lowest. This acts as a sentinel,
9390 speeding the main loop below. */
9391 r_off = (*ext_r_off) (base);
9392 for (p = loc = base; (p += elt_size) < end; )
9393 {
9394 bfd_vma r_off2 = (*ext_r_off) (p);
9395 if (r_off > r_off2)
9396 {
9397 r_off = r_off2;
9398 loc = p;
9399 }
9400 }
9401 if (loc != base)
9402 {
9403 /* Don't just swap *base and *loc as that changes the order
9404 of the original base[0] and base[1] if they happen to
9405 have the same r_offset. */
9406 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
9407 memcpy (onebuf, loc, elt_size);
9408 memmove (base + elt_size, base, loc - base);
9409 memcpy (base, onebuf, elt_size);
9410 }
9411
9412 for (p = base + elt_size; (p += elt_size) < end; )
9413 {
9414 /* base to p is sorted, *p is next to insert. */
9415 r_off = (*ext_r_off) (p);
9416 /* Search the sorted region for location to insert. */
9417 loc = p - elt_size;
9418 while (r_off < (*ext_r_off) (loc))
9419 loc -= elt_size;
9420 loc += elt_size;
9421 if (loc != p)
9422 {
9423 /* Chances are there is a run of relocs to insert here,
9424 from one of more input files. Files are not always
9425 linked in order due to the way elf_link_input_bfd is
9426 called. See pr17666. */
9427 size_t sortlen = p - loc;
9428 bfd_vma r_off2 = (*ext_r_off) (loc);
9429 size_t runlen = elt_size;
9430 size_t buf_size = 96 * 1024;
9431 while (p + runlen < end
9432 && (sortlen <= buf_size
9433 || runlen + elt_size <= buf_size)
9434 && r_off2 > (*ext_r_off) (p + runlen))
9435 runlen += elt_size;
9436 if (buf == NULL)
9437 {
9438 buf = bfd_malloc (buf_size);
9439 if (buf == NULL)
9440 return false;
9441 }
9442 if (runlen < sortlen)
9443 {
9444 memcpy (buf, p, runlen);
9445 memmove (loc + runlen, loc, sortlen);
9446 memcpy (loc, buf, runlen);
9447 }
9448 else
9449 {
9450 memcpy (buf, loc, sortlen);
9451 memmove (loc, p, runlen);
9452 memcpy (loc + runlen, buf, sortlen);
9453 }
9454 p += runlen - elt_size;
9455 }
9456 }
9457 /* Hashes are no longer valid. */
9458 free (reldata->hashes);
9459 reldata->hashes = NULL;
9460 free (buf);
9461 }
9462 return true;
9463 }
9464
9465 struct elf_link_sort_rela
9466 {
9467 union {
9468 bfd_vma offset;
9469 bfd_vma sym_mask;
9470 } u;
9471 enum elf_reloc_type_class type;
9472 /* We use this as an array of size int_rels_per_ext_rel. */
9473 Elf_Internal_Rela rela[1];
9474 };
9475
9476 /* qsort stability here and for cmp2 is only an issue if multiple
9477 dynamic relocations are emitted at the same address. But targets
9478 that apply a series of dynamic relocations each operating on the
9479 result of the prior relocation can't use -z combreloc as
9480 implemented anyway. Such schemes tend to be broken by sorting on
9481 symbol index. That leaves dynamic NONE relocs as the only other
9482 case where ld might emit multiple relocs at the same address, and
9483 those are only emitted due to target bugs. */
9484
9485 static int
9486 elf_link_sort_cmp1 (const void *A, const void *B)
9487 {
9488 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9489 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9490 int relativea, relativeb;
9491
9492 relativea = a->type == reloc_class_relative;
9493 relativeb = b->type == reloc_class_relative;
9494
9495 if (relativea < relativeb)
9496 return 1;
9497 if (relativea > relativeb)
9498 return -1;
9499 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
9500 return -1;
9501 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
9502 return 1;
9503 if (a->rela->r_offset < b->rela->r_offset)
9504 return -1;
9505 if (a->rela->r_offset > b->rela->r_offset)
9506 return 1;
9507 return 0;
9508 }
9509
9510 static int
9511 elf_link_sort_cmp2 (const void *A, const void *B)
9512 {
9513 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9514 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9515
9516 if (a->type < b->type)
9517 return -1;
9518 if (a->type > b->type)
9519 return 1;
9520 if (a->u.offset < b->u.offset)
9521 return -1;
9522 if (a->u.offset > b->u.offset)
9523 return 1;
9524 if (a->rela->r_offset < b->rela->r_offset)
9525 return -1;
9526 if (a->rela->r_offset > b->rela->r_offset)
9527 return 1;
9528 return 0;
9529 }
9530
9531 static size_t
9532 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9533 {
9534 asection *dynamic_relocs;
9535 asection *rela_dyn;
9536 asection *rel_dyn;
9537 bfd_size_type count, size;
9538 size_t i, ret, sort_elt, ext_size;
9539 bfd_byte *sort, *s_non_relative, *p;
9540 struct elf_link_sort_rela *sq;
9541 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9542 int i2e = bed->s->int_rels_per_ext_rel;
9543 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
9544 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9545 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9546 struct bfd_link_order *lo;
9547 bfd_vma r_sym_mask;
9548 bool use_rela;
9549
9550 /* Find a dynamic reloc section. */
9551 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9552 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
9553 if (rela_dyn != NULL && rela_dyn->size > 0
9554 && rel_dyn != NULL && rel_dyn->size > 0)
9555 {
9556 bool use_rela_initialised = false;
9557
9558 /* This is just here to stop gcc from complaining.
9559 Its initialization checking code is not perfect. */
9560 use_rela = true;
9561
9562 /* Both sections are present. Examine the sizes
9563 of the indirect sections to help us choose. */
9564 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9565 if (lo->type == bfd_indirect_link_order)
9566 {
9567 asection *o = lo->u.indirect.section;
9568
9569 if ((o->size % bed->s->sizeof_rela) == 0)
9570 {
9571 if ((o->size % bed->s->sizeof_rel) == 0)
9572 /* Section size is divisible by both rel and rela sizes.
9573 It is of no help to us. */
9574 ;
9575 else
9576 {
9577 /* Section size is only divisible by rela. */
9578 if (use_rela_initialised && !use_rela)
9579 {
9580 _bfd_error_handler (_("%pB: unable to sort relocs - "
9581 "they are in more than one size"),
9582 abfd);
9583 bfd_set_error (bfd_error_invalid_operation);
9584 return 0;
9585 }
9586 else
9587 {
9588 use_rela = true;
9589 use_rela_initialised = true;
9590 }
9591 }
9592 }
9593 else if ((o->size % bed->s->sizeof_rel) == 0)
9594 {
9595 /* Section size is only divisible by rel. */
9596 if (use_rela_initialised && use_rela)
9597 {
9598 _bfd_error_handler (_("%pB: unable to sort relocs - "
9599 "they are in more than one size"),
9600 abfd);
9601 bfd_set_error (bfd_error_invalid_operation);
9602 return 0;
9603 }
9604 else
9605 {
9606 use_rela = false;
9607 use_rela_initialised = true;
9608 }
9609 }
9610 else
9611 {
9612 /* The section size is not divisible by either -
9613 something is wrong. */
9614 _bfd_error_handler (_("%pB: unable to sort relocs - "
9615 "they are of an unknown size"), abfd);
9616 bfd_set_error (bfd_error_invalid_operation);
9617 return 0;
9618 }
9619 }
9620
9621 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9622 if (lo->type == bfd_indirect_link_order)
9623 {
9624 asection *o = lo->u.indirect.section;
9625
9626 if ((o->size % bed->s->sizeof_rela) == 0)
9627 {
9628 if ((o->size % bed->s->sizeof_rel) == 0)
9629 /* Section size is divisible by both rel and rela sizes.
9630 It is of no help to us. */
9631 ;
9632 else
9633 {
9634 /* Section size is only divisible by rela. */
9635 if (use_rela_initialised && !use_rela)
9636 {
9637 _bfd_error_handler (_("%pB: unable to sort relocs - "
9638 "they are in more than one size"),
9639 abfd);
9640 bfd_set_error (bfd_error_invalid_operation);
9641 return 0;
9642 }
9643 else
9644 {
9645 use_rela = true;
9646 use_rela_initialised = true;
9647 }
9648 }
9649 }
9650 else if ((o->size % bed->s->sizeof_rel) == 0)
9651 {
9652 /* Section size is only divisible by rel. */
9653 if (use_rela_initialised && use_rela)
9654 {
9655 _bfd_error_handler (_("%pB: unable to sort relocs - "
9656 "they are in more than one size"),
9657 abfd);
9658 bfd_set_error (bfd_error_invalid_operation);
9659 return 0;
9660 }
9661 else
9662 {
9663 use_rela = false;
9664 use_rela_initialised = true;
9665 }
9666 }
9667 else
9668 {
9669 /* The section size is not divisible by either -
9670 something is wrong. */
9671 _bfd_error_handler (_("%pB: unable to sort relocs - "
9672 "they are of an unknown size"), abfd);
9673 bfd_set_error (bfd_error_invalid_operation);
9674 return 0;
9675 }
9676 }
9677
9678 if (! use_rela_initialised)
9679 /* Make a guess. */
9680 use_rela = true;
9681 }
9682 else if (rela_dyn != NULL && rela_dyn->size > 0)
9683 use_rela = true;
9684 else if (rel_dyn != NULL && rel_dyn->size > 0)
9685 use_rela = false;
9686 else
9687 return 0;
9688
9689 if (use_rela)
9690 {
9691 dynamic_relocs = rela_dyn;
9692 ext_size = bed->s->sizeof_rela;
9693 swap_in = bed->s->swap_reloca_in;
9694 swap_out = bed->s->swap_reloca_out;
9695 }
9696 else
9697 {
9698 dynamic_relocs = rel_dyn;
9699 ext_size = bed->s->sizeof_rel;
9700 swap_in = bed->s->swap_reloc_in;
9701 swap_out = bed->s->swap_reloc_out;
9702 }
9703
9704 size = 0;
9705 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9706 if (lo->type == bfd_indirect_link_order)
9707 size += lo->u.indirect.section->size;
9708
9709 if (size != dynamic_relocs->size)
9710 return 0;
9711
9712 sort_elt = (sizeof (struct elf_link_sort_rela)
9713 + (i2e - 1) * sizeof (Elf_Internal_Rela));
9714
9715 count = dynamic_relocs->size / ext_size;
9716 if (count == 0)
9717 return 0;
9718 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
9719
9720 if (sort == NULL)
9721 {
9722 (*info->callbacks->warning)
9723 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
9724 return 0;
9725 }
9726
9727 if (bed->s->arch_size == 32)
9728 r_sym_mask = ~(bfd_vma) 0xff;
9729 else
9730 r_sym_mask = ~(bfd_vma) 0xffffffff;
9731
9732 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9733 if (lo->type == bfd_indirect_link_order)
9734 {
9735 bfd_byte *erel, *erelend;
9736 asection *o = lo->u.indirect.section;
9737
9738 if (o->contents == NULL && o->size != 0)
9739 {
9740 /* This is a reloc section that is being handled as a normal
9741 section. See bfd_section_from_shdr. We can't combine
9742 relocs in this case. */
9743 free (sort);
9744 return 0;
9745 }
9746 erel = o->contents;
9747 erelend = o->contents + o->size;
9748 p = sort + o->output_offset * opb / ext_size * sort_elt;
9749
9750 while (erel < erelend)
9751 {
9752 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9753
9754 (*swap_in) (abfd, erel, s->rela);
9755 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
9756 s->u.sym_mask = r_sym_mask;
9757 p += sort_elt;
9758 erel += ext_size;
9759 }
9760 }
9761
9762 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9763
9764 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9765 {
9766 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9767 if (s->type != reloc_class_relative)
9768 break;
9769 }
9770 ret = i;
9771 s_non_relative = p;
9772
9773 sq = (struct elf_link_sort_rela *) s_non_relative;
9774 for (; i < count; i++, p += sort_elt)
9775 {
9776 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9777 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9778 sq = sp;
9779 sp->u.offset = sq->rela->r_offset;
9780 }
9781
9782 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9783
9784 struct elf_link_hash_table *htab = elf_hash_table (info);
9785 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9786 {
9787 /* We have plt relocs in .rela.dyn. */
9788 sq = (struct elf_link_sort_rela *) sort;
9789 for (i = 0; i < count; i++)
9790 if (sq[count - i - 1].type != reloc_class_plt)
9791 break;
9792 if (i != 0 && htab->srelplt->size == i * ext_size)
9793 {
9794 struct bfd_link_order **plo;
9795 /* Put srelplt link_order last. This is so the output_offset
9796 set in the next loop is correct for DT_JMPREL. */
9797 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9798 if ((*plo)->type == bfd_indirect_link_order
9799 && (*plo)->u.indirect.section == htab->srelplt)
9800 {
9801 lo = *plo;
9802 *plo = lo->next;
9803 }
9804 else
9805 plo = &(*plo)->next;
9806 *plo = lo;
9807 lo->next = NULL;
9808 dynamic_relocs->map_tail.link_order = lo;
9809 }
9810 }
9811
9812 p = sort;
9813 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9814 if (lo->type == bfd_indirect_link_order)
9815 {
9816 bfd_byte *erel, *erelend;
9817 asection *o = lo->u.indirect.section;
9818
9819 erel = o->contents;
9820 erelend = o->contents + o->size;
9821 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
9822 while (erel < erelend)
9823 {
9824 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9825 (*swap_out) (abfd, s->rela, erel);
9826 p += sort_elt;
9827 erel += ext_size;
9828 }
9829 }
9830
9831 free (sort);
9832 *psec = dynamic_relocs;
9833 return ret;
9834 }
9835
9836 /* Add a symbol to the output symbol string table. */
9837
9838 static int
9839 elf_link_output_symstrtab (void *finf,
9840 const char *name,
9841 Elf_Internal_Sym *elfsym,
9842 asection *input_sec,
9843 struct elf_link_hash_entry *h)
9844 {
9845 struct elf_final_link_info *flinfo = finf;
9846 int (*output_symbol_hook)
9847 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9848 struct elf_link_hash_entry *);
9849 struct elf_link_hash_table *hash_table;
9850 const struct elf_backend_data *bed;
9851 bfd_size_type strtabsize;
9852
9853 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9854
9855 bed = get_elf_backend_data (flinfo->output_bfd);
9856 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9857 if (output_symbol_hook != NULL)
9858 {
9859 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
9860 if (ret != 1)
9861 return ret;
9862 }
9863
9864 if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC)
9865 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
9866 if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE)
9867 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique;
9868
9869 if (name == NULL
9870 || *name == '\0'
9871 || (input_sec->flags & SEC_EXCLUDE))
9872 elfsym->st_name = (unsigned long) -1;
9873 else
9874 {
9875 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9876 to get the final offset for st_name. */
9877 char *versioned_name = (char *) name;
9878 if (h != NULL)
9879 {
9880 if (h->versioned == versioned && h->def_dynamic)
9881 {
9882 /* Keep only one '@' for versioned symbols defined in
9883 shared objects. */
9884 char *version = strrchr (name, ELF_VER_CHR);
9885 char *base_end = strchr (name, ELF_VER_CHR);
9886 if (version != base_end)
9887 {
9888 size_t base_len;
9889 size_t len = strlen (name);
9890 versioned_name = bfd_alloc (flinfo->output_bfd, len);
9891 if (versioned_name == NULL)
9892 return 0;
9893 base_len = base_end - name;
9894 memcpy (versioned_name, name, base_len);
9895 memcpy (versioned_name + base_len, version,
9896 len - base_len);
9897 }
9898 }
9899 }
9900 else if (flinfo->info->unique_symbol
9901 && ELF_ST_BIND (elfsym->st_info) == STB_LOCAL)
9902 {
9903 struct local_hash_entry *lh;
9904 size_t count_len;
9905 size_t base_len;
9906 char buf[30];
9907 switch (ELF_ST_TYPE (elfsym->st_info))
9908 {
9909 case STT_FILE:
9910 case STT_SECTION:
9911 break;
9912 default:
9913 lh = (struct local_hash_entry *) bfd_hash_lookup
9914 (&flinfo->local_hash_table, name, true, false);
9915 if (lh == NULL)
9916 return 0;
9917 /* Always append ".COUNT" to local symbols to avoid
9918 potential conflicts with local symbol "XXX.COUNT". */
9919 sprintf (buf, "%lx", lh->count);
9920 base_len = lh->size;
9921 if (!base_len)
9922 {
9923 base_len = strlen (name);
9924 lh->size = base_len;
9925 }
9926 count_len = strlen (buf);
9927 versioned_name = bfd_alloc (flinfo->output_bfd,
9928 base_len + count_len + 2);
9929 if (versioned_name == NULL)
9930 return 0;
9931 memcpy (versioned_name, name, base_len);
9932 versioned_name[base_len] = '.';
9933 memcpy (versioned_name + base_len + 1, buf,
9934 count_len + 1);
9935 lh->count++;
9936 break;
9937 }
9938 }
9939 elfsym->st_name
9940 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
9941 versioned_name, false);
9942 if (elfsym->st_name == (unsigned long) -1)
9943 return 0;
9944 }
9945
9946 hash_table = elf_hash_table (flinfo->info);
9947 strtabsize = hash_table->strtabsize;
9948 if (strtabsize <= flinfo->output_bfd->symcount)
9949 {
9950 strtabsize += strtabsize;
9951 hash_table->strtabsize = strtabsize;
9952 strtabsize *= sizeof (*hash_table->strtab);
9953 hash_table->strtab
9954 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9955 strtabsize);
9956 if (hash_table->strtab == NULL)
9957 return 0;
9958 }
9959 hash_table->strtab[flinfo->output_bfd->symcount].sym = *elfsym;
9960 hash_table->strtab[flinfo->output_bfd->symcount].dest_index
9961 = flinfo->output_bfd->symcount;
9962 flinfo->output_bfd->symcount += 1;
9963
9964 return 1;
9965 }
9966
9967 /* Swap symbols out to the symbol table and flush the output symbols to
9968 the file. */
9969
9970 static bool
9971 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9972 {
9973 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
9974 size_t amt;
9975 size_t i;
9976 const struct elf_backend_data *bed;
9977 bfd_byte *symbuf;
9978 Elf_Internal_Shdr *hdr;
9979 file_ptr pos;
9980 bool ret;
9981
9982 if (flinfo->output_bfd->symcount == 0)
9983 return true;
9984
9985 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9986
9987 bed = get_elf_backend_data (flinfo->output_bfd);
9988
9989 amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount;
9990 symbuf = (bfd_byte *) bfd_malloc (amt);
9991 if (symbuf == NULL)
9992 return false;
9993
9994 if (flinfo->symshndxbuf)
9995 {
9996 amt = sizeof (Elf_External_Sym_Shndx);
9997 amt *= bfd_get_symcount (flinfo->output_bfd);
9998 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9999 if (flinfo->symshndxbuf == NULL)
10000 {
10001 free (symbuf);
10002 return false;
10003 }
10004 }
10005
10006 /* Now swap out the symbols. */
10007 for (i = 0; i < flinfo->output_bfd->symcount; i++)
10008 {
10009 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
10010 if (elfsym->sym.st_name == (unsigned long) -1)
10011 elfsym->sym.st_name = 0;
10012 else
10013 elfsym->sym.st_name
10014 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
10015 elfsym->sym.st_name);
10016
10017 /* Inform the linker of the addition of this symbol. */
10018
10019 if (flinfo->info->callbacks->ctf_new_symbol)
10020 flinfo->info->callbacks->ctf_new_symbol (elfsym->dest_index,
10021 &elfsym->sym);
10022
10023 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
10024 ((bfd_byte *) symbuf
10025 + (elfsym->dest_index
10026 * bed->s->sizeof_sym)),
10027 NPTR_ADD (flinfo->symshndxbuf,
10028 elfsym->dest_index));
10029 }
10030
10031 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
10032 pos = hdr->sh_offset + hdr->sh_size;
10033 amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount;
10034 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
10035 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
10036 {
10037 hdr->sh_size += amt;
10038 ret = true;
10039 }
10040 else
10041 ret = false;
10042
10043 free (symbuf);
10044
10045 free (hash_table->strtab);
10046 hash_table->strtab = NULL;
10047
10048 return ret;
10049 }
10050
10051 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
10052
10053 static bool
10054 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
10055 {
10056 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
10057 && sym->st_shndx < SHN_LORESERVE)
10058 {
10059 /* The gABI doesn't support dynamic symbols in output sections
10060 beyond 64k. */
10061 _bfd_error_handler
10062 /* xgettext:c-format */
10063 (_("%pB: too many sections: %d (>= %d)"),
10064 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
10065 bfd_set_error (bfd_error_nonrepresentable_section);
10066 return false;
10067 }
10068 return true;
10069 }
10070
10071 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
10072 allowing an unsatisfied unversioned symbol in the DSO to match a
10073 versioned symbol that would normally require an explicit version.
10074 We also handle the case that a DSO references a hidden symbol
10075 which may be satisfied by a versioned symbol in another DSO. */
10076
10077 static bool
10078 elf_link_check_versioned_symbol (struct bfd_link_info *info,
10079 const struct elf_backend_data *bed,
10080 struct elf_link_hash_entry *h)
10081 {
10082 bfd *abfd;
10083 struct elf_link_loaded_list *loaded;
10084
10085 if (!is_elf_hash_table (info->hash))
10086 return false;
10087
10088 /* Check indirect symbol. */
10089 while (h->root.type == bfd_link_hash_indirect)
10090 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10091
10092 switch (h->root.type)
10093 {
10094 default:
10095 abfd = NULL;
10096 break;
10097
10098 case bfd_link_hash_undefined:
10099 case bfd_link_hash_undefweak:
10100 abfd = h->root.u.undef.abfd;
10101 if (abfd == NULL
10102 || (abfd->flags & DYNAMIC) == 0
10103 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
10104 return false;
10105 break;
10106
10107 case bfd_link_hash_defined:
10108 case bfd_link_hash_defweak:
10109 abfd = h->root.u.def.section->owner;
10110 break;
10111
10112 case bfd_link_hash_common:
10113 abfd = h->root.u.c.p->section->owner;
10114 break;
10115 }
10116 BFD_ASSERT (abfd != NULL);
10117
10118 for (loaded = elf_hash_table (info)->dyn_loaded;
10119 loaded != NULL;
10120 loaded = loaded->next)
10121 {
10122 bfd *input;
10123 Elf_Internal_Shdr *hdr;
10124 size_t symcount;
10125 size_t extsymcount;
10126 size_t extsymoff;
10127 Elf_Internal_Shdr *versymhdr;
10128 Elf_Internal_Sym *isym;
10129 Elf_Internal_Sym *isymend;
10130 Elf_Internal_Sym *isymbuf;
10131 Elf_External_Versym *ever;
10132 Elf_External_Versym *extversym;
10133
10134 input = loaded->abfd;
10135
10136 /* We check each DSO for a possible hidden versioned definition. */
10137 if (input == abfd
10138 || elf_dynversym (input) == 0)
10139 continue;
10140
10141 hdr = &elf_tdata (input)->dynsymtab_hdr;
10142
10143 symcount = hdr->sh_size / bed->s->sizeof_sym;
10144 if (elf_bad_symtab (input))
10145 {
10146 extsymcount = symcount;
10147 extsymoff = 0;
10148 }
10149 else
10150 {
10151 extsymcount = symcount - hdr->sh_info;
10152 extsymoff = hdr->sh_info;
10153 }
10154
10155 if (extsymcount == 0)
10156 continue;
10157
10158 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
10159 NULL, NULL, NULL);
10160 if (isymbuf == NULL)
10161 return false;
10162
10163 /* Read in any version definitions. */
10164 versymhdr = &elf_tdata (input)->dynversym_hdr;
10165 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
10166 || (extversym = (Elf_External_Versym *)
10167 _bfd_malloc_and_read (input, versymhdr->sh_size,
10168 versymhdr->sh_size)) == NULL)
10169 {
10170 free (isymbuf);
10171 return false;
10172 }
10173
10174 ever = extversym + extsymoff;
10175 isymend = isymbuf + extsymcount;
10176 for (isym = isymbuf; isym < isymend; isym++, ever++)
10177 {
10178 const char *name;
10179 Elf_Internal_Versym iver;
10180 unsigned short version_index;
10181
10182 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
10183 || isym->st_shndx == SHN_UNDEF)
10184 continue;
10185
10186 name = bfd_elf_string_from_elf_section (input,
10187 hdr->sh_link,
10188 isym->st_name);
10189 if (strcmp (name, h->root.root.string) != 0)
10190 continue;
10191
10192 _bfd_elf_swap_versym_in (input, ever, &iver);
10193
10194 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
10195 && !(h->def_regular
10196 && h->forced_local))
10197 {
10198 /* If we have a non-hidden versioned sym, then it should
10199 have provided a definition for the undefined sym unless
10200 it is defined in a non-shared object and forced local.
10201 */
10202 abort ();
10203 }
10204
10205 version_index = iver.vs_vers & VERSYM_VERSION;
10206 if (version_index == 1 || version_index == 2)
10207 {
10208 /* This is the base or first version. We can use it. */
10209 free (extversym);
10210 free (isymbuf);
10211 return true;
10212 }
10213 }
10214
10215 free (extversym);
10216 free (isymbuf);
10217 }
10218
10219 return false;
10220 }
10221
10222 /* Convert ELF common symbol TYPE. */
10223
10224 static int
10225 elf_link_convert_common_type (struct bfd_link_info *info, int type)
10226 {
10227 /* Commom symbol can only appear in relocatable link. */
10228 if (!bfd_link_relocatable (info))
10229 abort ();
10230 switch (info->elf_stt_common)
10231 {
10232 case unchanged:
10233 break;
10234 case elf_stt_common:
10235 type = STT_COMMON;
10236 break;
10237 case no_elf_stt_common:
10238 type = STT_OBJECT;
10239 break;
10240 }
10241 return type;
10242 }
10243
10244 /* Add an external symbol to the symbol table. This is called from
10245 the hash table traversal routine. When generating a shared object,
10246 we go through the symbol table twice. The first time we output
10247 anything that might have been forced to local scope in a version
10248 script. The second time we output the symbols that are still
10249 global symbols. */
10250
10251 static bool
10252 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
10253 {
10254 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
10255 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
10256 struct elf_final_link_info *flinfo = eoinfo->flinfo;
10257 bool strip;
10258 Elf_Internal_Sym sym;
10259 asection *input_sec;
10260 const struct elf_backend_data *bed;
10261 long indx;
10262 int ret;
10263 unsigned int type;
10264
10265 if (h->root.type == bfd_link_hash_warning)
10266 {
10267 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10268 if (h->root.type == bfd_link_hash_new)
10269 return true;
10270 }
10271
10272 /* Decide whether to output this symbol in this pass. */
10273 if (eoinfo->localsyms)
10274 {
10275 if (!h->forced_local)
10276 return true;
10277 }
10278 else
10279 {
10280 if (h->forced_local)
10281 return true;
10282 }
10283
10284 bed = get_elf_backend_data (flinfo->output_bfd);
10285
10286 if (h->root.type == bfd_link_hash_undefined)
10287 {
10288 /* If we have an undefined symbol reference here then it must have
10289 come from a shared library that is being linked in. (Undefined
10290 references in regular files have already been handled unless
10291 they are in unreferenced sections which are removed by garbage
10292 collection). */
10293 bool ignore_undef = false;
10294
10295 /* Some symbols may be special in that the fact that they're
10296 undefined can be safely ignored - let backend determine that. */
10297 if (bed->elf_backend_ignore_undef_symbol)
10298 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
10299
10300 /* If we are reporting errors for this situation then do so now. */
10301 if (!ignore_undef
10302 && h->ref_dynamic_nonweak
10303 && (!h->ref_regular || flinfo->info->gc_sections)
10304 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
10305 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
10306 {
10307 flinfo->info->callbacks->undefined_symbol
10308 (flinfo->info, h->root.root.string,
10309 h->ref_regular ? NULL : h->root.u.undef.abfd, NULL, 0,
10310 flinfo->info->unresolved_syms_in_shared_libs == RM_DIAGNOSE
10311 && !flinfo->info->warn_unresolved_syms);
10312 }
10313
10314 /* Strip a global symbol defined in a discarded section. */
10315 if (h->indx == -3)
10316 return true;
10317 }
10318
10319 /* We should also warn if a forced local symbol is referenced from
10320 shared libraries. */
10321 if (bfd_link_executable (flinfo->info)
10322 && h->forced_local
10323 && h->ref_dynamic
10324 && h->def_regular
10325 && !h->dynamic_def
10326 && h->ref_dynamic_nonweak
10327 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
10328 {
10329 bfd *def_bfd;
10330 const char *msg;
10331 struct elf_link_hash_entry *hi = h;
10332
10333 /* Check indirect symbol. */
10334 while (hi->root.type == bfd_link_hash_indirect)
10335 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
10336
10337 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
10338 /* xgettext:c-format */
10339 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
10340 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
10341 /* xgettext:c-format */
10342 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
10343 else
10344 /* xgettext:c-format */
10345 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
10346 def_bfd = flinfo->output_bfd;
10347 if (hi->root.u.def.section != bfd_abs_section_ptr)
10348 def_bfd = hi->root.u.def.section->owner;
10349 _bfd_error_handler (msg, flinfo->output_bfd,
10350 h->root.root.string, def_bfd);
10351 bfd_set_error (bfd_error_bad_value);
10352 eoinfo->failed = true;
10353 return false;
10354 }
10355
10356 /* We don't want to output symbols that have never been mentioned by
10357 a regular file, or that we have been told to strip. However, if
10358 h->indx is set to -2, the symbol is used by a reloc and we must
10359 output it. */
10360 strip = false;
10361 if (h->indx == -2)
10362 ;
10363 else if ((h->def_dynamic
10364 || h->ref_dynamic
10365 || h->root.type == bfd_link_hash_new)
10366 && !h->def_regular
10367 && !h->ref_regular)
10368 strip = true;
10369 else if (flinfo->info->strip == strip_all)
10370 strip = true;
10371 else if (flinfo->info->strip == strip_some
10372 && bfd_hash_lookup (flinfo->info->keep_hash,
10373 h->root.root.string, false, false) == NULL)
10374 strip = true;
10375 else if ((h->root.type == bfd_link_hash_defined
10376 || h->root.type == bfd_link_hash_defweak)
10377 && ((flinfo->info->strip_discarded
10378 && discarded_section (h->root.u.def.section))
10379 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
10380 && h->root.u.def.section->owner != NULL
10381 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
10382 strip = true;
10383 else if ((h->root.type == bfd_link_hash_undefined
10384 || h->root.type == bfd_link_hash_undefweak)
10385 && h->root.u.undef.abfd != NULL
10386 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
10387 strip = true;
10388
10389 type = h->type;
10390
10391 /* If we're stripping it, and it's not a dynamic symbol, there's
10392 nothing else to do. However, if it is a forced local symbol or
10393 an ifunc symbol we need to give the backend finish_dynamic_symbol
10394 function a chance to make it dynamic. */
10395 if (strip
10396 && h->dynindx == -1
10397 && type != STT_GNU_IFUNC
10398 && !h->forced_local)
10399 return true;
10400
10401 sym.st_value = 0;
10402 sym.st_size = h->size;
10403 sym.st_other = h->other;
10404 switch (h->root.type)
10405 {
10406 default:
10407 case bfd_link_hash_new:
10408 case bfd_link_hash_warning:
10409 abort ();
10410 return false;
10411
10412 case bfd_link_hash_undefined:
10413 case bfd_link_hash_undefweak:
10414 input_sec = bfd_und_section_ptr;
10415 sym.st_shndx = SHN_UNDEF;
10416 break;
10417
10418 case bfd_link_hash_defined:
10419 case bfd_link_hash_defweak:
10420 {
10421 input_sec = h->root.u.def.section;
10422 if (input_sec->output_section != NULL)
10423 {
10424 sym.st_shndx =
10425 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
10426 input_sec->output_section);
10427 if (sym.st_shndx == SHN_BAD)
10428 {
10429 _bfd_error_handler
10430 /* xgettext:c-format */
10431 (_("%pB: could not find output section %pA for input section %pA"),
10432 flinfo->output_bfd, input_sec->output_section, input_sec);
10433 bfd_set_error (bfd_error_nonrepresentable_section);
10434 eoinfo->failed = true;
10435 return false;
10436 }
10437
10438 /* ELF symbols in relocatable files are section relative,
10439 but in nonrelocatable files they are virtual
10440 addresses. */
10441 sym.st_value = h->root.u.def.value + input_sec->output_offset;
10442 if (!bfd_link_relocatable (flinfo->info))
10443 {
10444 sym.st_value += input_sec->output_section->vma;
10445 if (h->type == STT_TLS)
10446 {
10447 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
10448 if (tls_sec != NULL)
10449 sym.st_value -= tls_sec->vma;
10450 }
10451 }
10452 }
10453 else
10454 {
10455 BFD_ASSERT (input_sec->owner == NULL
10456 || (input_sec->owner->flags & DYNAMIC) != 0);
10457 sym.st_shndx = SHN_UNDEF;
10458 input_sec = bfd_und_section_ptr;
10459 }
10460 }
10461 break;
10462
10463 case bfd_link_hash_common:
10464 input_sec = h->root.u.c.p->section;
10465 sym.st_shndx = bed->common_section_index (input_sec);
10466 sym.st_value = 1 << h->root.u.c.p->alignment_power;
10467 break;
10468
10469 case bfd_link_hash_indirect:
10470 /* These symbols are created by symbol versioning. They point
10471 to the decorated version of the name. For example, if the
10472 symbol foo@@GNU_1.2 is the default, which should be used when
10473 foo is used with no version, then we add an indirect symbol
10474 foo which points to foo@@GNU_1.2. We ignore these symbols,
10475 since the indirected symbol is already in the hash table. */
10476 return true;
10477 }
10478
10479 if (type == STT_COMMON || type == STT_OBJECT)
10480 switch (h->root.type)
10481 {
10482 case bfd_link_hash_common:
10483 type = elf_link_convert_common_type (flinfo->info, type);
10484 break;
10485 case bfd_link_hash_defined:
10486 case bfd_link_hash_defweak:
10487 if (bed->common_definition (&sym))
10488 type = elf_link_convert_common_type (flinfo->info, type);
10489 else
10490 type = STT_OBJECT;
10491 break;
10492 case bfd_link_hash_undefined:
10493 case bfd_link_hash_undefweak:
10494 break;
10495 default:
10496 abort ();
10497 }
10498
10499 if (h->forced_local)
10500 {
10501 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
10502 /* Turn off visibility on local symbol. */
10503 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
10504 }
10505 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10506 else if (h->unique_global && h->def_regular)
10507 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
10508 else if (h->root.type == bfd_link_hash_undefweak
10509 || h->root.type == bfd_link_hash_defweak)
10510 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
10511 else
10512 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
10513 sym.st_target_internal = h->target_internal;
10514
10515 /* Give the processor backend a chance to tweak the symbol value,
10516 and also to finish up anything that needs to be done for this
10517 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
10518 forced local syms when non-shared is due to a historical quirk.
10519 STT_GNU_IFUNC symbol must go through PLT. */
10520 if ((h->type == STT_GNU_IFUNC
10521 && h->def_regular
10522 && !bfd_link_relocatable (flinfo->info))
10523 || ((h->dynindx != -1
10524 || h->forced_local)
10525 && ((bfd_link_pic (flinfo->info)
10526 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
10527 || h->root.type != bfd_link_hash_undefweak))
10528 || !h->forced_local)
10529 && elf_hash_table (flinfo->info)->dynamic_sections_created))
10530 {
10531 if (! ((*bed->elf_backend_finish_dynamic_symbol)
10532 (flinfo->output_bfd, flinfo->info, h, &sym)))
10533 {
10534 eoinfo->failed = true;
10535 return false;
10536 }
10537 }
10538
10539 /* If we are marking the symbol as undefined, and there are no
10540 non-weak references to this symbol from a regular object, then
10541 mark the symbol as weak undefined; if there are non-weak
10542 references, mark the symbol as strong. We can't do this earlier,
10543 because it might not be marked as undefined until the
10544 finish_dynamic_symbol routine gets through with it. */
10545 if (sym.st_shndx == SHN_UNDEF
10546 && h->ref_regular
10547 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
10548 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
10549 {
10550 int bindtype;
10551 type = ELF_ST_TYPE (sym.st_info);
10552
10553 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10554 if (type == STT_GNU_IFUNC)
10555 type = STT_FUNC;
10556
10557 if (h->ref_regular_nonweak)
10558 bindtype = STB_GLOBAL;
10559 else
10560 bindtype = STB_WEAK;
10561 sym.st_info = ELF_ST_INFO (bindtype, type);
10562 }
10563
10564 /* If this is a symbol defined in a dynamic library, don't use the
10565 symbol size from the dynamic library. Relinking an executable
10566 against a new library may introduce gratuitous changes in the
10567 executable's symbols if we keep the size. */
10568 if (sym.st_shndx == SHN_UNDEF
10569 && !h->def_regular
10570 && h->def_dynamic)
10571 sym.st_size = 0;
10572
10573 /* If a non-weak symbol with non-default visibility is not defined
10574 locally, it is a fatal error. */
10575 if (!bfd_link_relocatable (flinfo->info)
10576 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
10577 && ELF_ST_BIND (sym.st_info) != STB_WEAK
10578 && h->root.type == bfd_link_hash_undefined
10579 && !h->def_regular)
10580 {
10581 const char *msg;
10582
10583 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
10584 /* xgettext:c-format */
10585 msg = _("%pB: protected symbol `%s' isn't defined");
10586 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
10587 /* xgettext:c-format */
10588 msg = _("%pB: internal symbol `%s' isn't defined");
10589 else
10590 /* xgettext:c-format */
10591 msg = _("%pB: hidden symbol `%s' isn't defined");
10592 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
10593 bfd_set_error (bfd_error_bad_value);
10594 eoinfo->failed = true;
10595 return false;
10596 }
10597
10598 /* If this symbol should be put in the .dynsym section, then put it
10599 there now. We already know the symbol index. We also fill in
10600 the entry in the .hash section. */
10601 if (h->dynindx != -1
10602 && elf_hash_table (flinfo->info)->dynamic_sections_created
10603 && elf_hash_table (flinfo->info)->dynsym != NULL
10604 && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
10605 {
10606 bfd_byte *esym;
10607
10608 /* Since there is no version information in the dynamic string,
10609 if there is no version info in symbol version section, we will
10610 have a run-time problem if not linking executable, referenced
10611 by shared library, or not bound locally. */
10612 if (h->verinfo.verdef == NULL
10613 && (!bfd_link_executable (flinfo->info)
10614 || h->ref_dynamic
10615 || !h->def_regular))
10616 {
10617 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
10618
10619 if (p && p [1] != '\0')
10620 {
10621 _bfd_error_handler
10622 /* xgettext:c-format */
10623 (_("%pB: no symbol version section for versioned symbol `%s'"),
10624 flinfo->output_bfd, h->root.root.string);
10625 eoinfo->failed = true;
10626 return false;
10627 }
10628 }
10629
10630 sym.st_name = h->dynstr_index;
10631 esym = (elf_hash_table (flinfo->info)->dynsym->contents
10632 + h->dynindx * bed->s->sizeof_sym);
10633 if (!check_dynsym (flinfo->output_bfd, &sym))
10634 {
10635 eoinfo->failed = true;
10636 return false;
10637 }
10638
10639 /* Inform the linker of the addition of this symbol. */
10640
10641 if (flinfo->info->callbacks->ctf_new_dynsym)
10642 flinfo->info->callbacks->ctf_new_dynsym (h->dynindx, &sym);
10643
10644 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
10645
10646 if (flinfo->hash_sec != NULL)
10647 {
10648 size_t hash_entry_size;
10649 bfd_byte *bucketpos;
10650 bfd_vma chain;
10651 size_t bucketcount;
10652 size_t bucket;
10653
10654 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
10655 bucket = h->u.elf_hash_value % bucketcount;
10656
10657 hash_entry_size
10658 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
10659 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
10660 + (bucket + 2) * hash_entry_size);
10661 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
10662 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
10663 bucketpos);
10664 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
10665 ((bfd_byte *) flinfo->hash_sec->contents
10666 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
10667 }
10668
10669 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
10670 {
10671 Elf_Internal_Versym iversym;
10672 Elf_External_Versym *eversym;
10673
10674 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
10675 {
10676 if (h->verinfo.verdef == NULL
10677 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
10678 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
10679 iversym.vs_vers = 1;
10680 else
10681 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
10682 }
10683 else
10684 {
10685 if (h->verinfo.vertree == NULL)
10686 iversym.vs_vers = 1;
10687 else
10688 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
10689 if (flinfo->info->create_default_symver)
10690 iversym.vs_vers++;
10691 }
10692
10693 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
10694 defined locally. */
10695 if (h->versioned == versioned_hidden && h->def_regular)
10696 iversym.vs_vers |= VERSYM_HIDDEN;
10697
10698 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
10699 eversym += h->dynindx;
10700 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
10701 }
10702 }
10703
10704 /* If the symbol is undefined, and we didn't output it to .dynsym,
10705 strip it from .symtab too. Obviously we can't do this for
10706 relocatable output or when needed for --emit-relocs. */
10707 else if (input_sec == bfd_und_section_ptr
10708 && h->indx != -2
10709 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10710 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
10711 && !bfd_link_relocatable (flinfo->info))
10712 return true;
10713
10714 /* Also strip others that we couldn't earlier due to dynamic symbol
10715 processing. */
10716 if (strip)
10717 return true;
10718 if ((input_sec->flags & SEC_EXCLUDE) != 0)
10719 return true;
10720
10721 /* Output a FILE symbol so that following locals are not associated
10722 with the wrong input file. We need one for forced local symbols
10723 if we've seen more than one FILE symbol or when we have exactly
10724 one FILE symbol but global symbols are present in a file other
10725 than the one with the FILE symbol. We also need one if linker
10726 defined symbols are present. In practice these conditions are
10727 always met, so just emit the FILE symbol unconditionally. */
10728 if (eoinfo->localsyms
10729 && !eoinfo->file_sym_done
10730 && eoinfo->flinfo->filesym_count != 0)
10731 {
10732 Elf_Internal_Sym fsym;
10733
10734 memset (&fsym, 0, sizeof (fsym));
10735 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10736 fsym.st_shndx = SHN_ABS;
10737 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10738 bfd_und_section_ptr, NULL))
10739 return false;
10740
10741 eoinfo->file_sym_done = true;
10742 }
10743
10744 indx = bfd_get_symcount (flinfo->output_bfd);
10745 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10746 input_sec, h);
10747 if (ret == 0)
10748 {
10749 eoinfo->failed = true;
10750 return false;
10751 }
10752 else if (ret == 1)
10753 h->indx = indx;
10754 else if (h->indx == -2)
10755 abort();
10756
10757 return true;
10758 }
10759
10760 /* Return TRUE if special handling is done for relocs in SEC against
10761 symbols defined in discarded sections. */
10762
10763 static bool
10764 elf_section_ignore_discarded_relocs (asection *sec)
10765 {
10766 const struct elf_backend_data *bed;
10767
10768 switch (sec->sec_info_type)
10769 {
10770 case SEC_INFO_TYPE_STABS:
10771 case SEC_INFO_TYPE_EH_FRAME:
10772 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10773 return true;
10774 default:
10775 break;
10776 }
10777
10778 bed = get_elf_backend_data (sec->owner);
10779 if (bed->elf_backend_ignore_discarded_relocs != NULL
10780 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10781 return true;
10782
10783 return false;
10784 }
10785
10786 /* Return a mask saying how ld should treat relocations in SEC against
10787 symbols defined in discarded sections. If this function returns
10788 COMPLAIN set, ld will issue a warning message. If this function
10789 returns PRETEND set, and the discarded section was link-once and the
10790 same size as the kept link-once section, ld will pretend that the
10791 symbol was actually defined in the kept section. Otherwise ld will
10792 zero the reloc (at least that is the intent, but some cooperation by
10793 the target dependent code is needed, particularly for REL targets). */
10794
10795 unsigned int
10796 _bfd_elf_default_action_discarded (asection *sec)
10797 {
10798 if (sec->flags & SEC_DEBUGGING)
10799 return PRETEND;
10800
10801 if (strcmp (".eh_frame", sec->name) == 0)
10802 return 0;
10803
10804 if (strcmp (".gcc_except_table", sec->name) == 0)
10805 return 0;
10806
10807 return COMPLAIN | PRETEND;
10808 }
10809
10810 /* Find a match between a section and a member of a section group. */
10811
10812 static asection *
10813 match_group_member (asection *sec, asection *group,
10814 struct bfd_link_info *info)
10815 {
10816 asection *first = elf_next_in_group (group);
10817 asection *s = first;
10818
10819 while (s != NULL)
10820 {
10821 if (bfd_elf_match_symbols_in_sections (s, sec, info))
10822 return s;
10823
10824 s = elf_next_in_group (s);
10825 if (s == first)
10826 break;
10827 }
10828
10829 return NULL;
10830 }
10831
10832 /* Check if the kept section of a discarded section SEC can be used
10833 to replace it. Return the replacement if it is OK. Otherwise return
10834 NULL. */
10835
10836 asection *
10837 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
10838 {
10839 asection *kept;
10840
10841 kept = sec->kept_section;
10842 if (kept != NULL)
10843 {
10844 if ((kept->flags & SEC_GROUP) != 0)
10845 kept = match_group_member (sec, kept, info);
10846 if (kept != NULL)
10847 {
10848 if ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10849 != (kept->rawsize != 0 ? kept->rawsize : kept->size))
10850 kept = NULL;
10851 else
10852 {
10853 /* Get the real kept section. */
10854 asection *next;
10855 for (next = kept->kept_section;
10856 next != NULL;
10857 next = next->kept_section)
10858 kept = next;
10859 }
10860 }
10861 sec->kept_section = kept;
10862 }
10863 return kept;
10864 }
10865
10866 /* Link an input file into the linker output file. This function
10867 handles all the sections and relocations of the input file at once.
10868 This is so that we only have to read the local symbols once, and
10869 don't have to keep them in memory. */
10870
10871 static bool
10872 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
10873 {
10874 int (*relocate_section)
10875 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10876 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10877 bfd *output_bfd;
10878 Elf_Internal_Shdr *symtab_hdr;
10879 size_t locsymcount;
10880 size_t extsymoff;
10881 Elf_Internal_Sym *isymbuf;
10882 Elf_Internal_Sym *isym;
10883 Elf_Internal_Sym *isymend;
10884 long *pindex;
10885 asection **ppsection;
10886 asection *o;
10887 const struct elf_backend_data *bed;
10888 struct elf_link_hash_entry **sym_hashes;
10889 bfd_size_type address_size;
10890 bfd_vma r_type_mask;
10891 int r_sym_shift;
10892 bool have_file_sym = false;
10893
10894 output_bfd = flinfo->output_bfd;
10895 bed = get_elf_backend_data (output_bfd);
10896 relocate_section = bed->elf_backend_relocate_section;
10897
10898 /* If this is a dynamic object, we don't want to do anything here:
10899 we don't want the local symbols, and we don't want the section
10900 contents. */
10901 if ((input_bfd->flags & DYNAMIC) != 0)
10902 return true;
10903
10904 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10905 if (elf_bad_symtab (input_bfd))
10906 {
10907 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10908 extsymoff = 0;
10909 }
10910 else
10911 {
10912 locsymcount = symtab_hdr->sh_info;
10913 extsymoff = symtab_hdr->sh_info;
10914 }
10915
10916 /* Enable GNU OSABI features in the output BFD that are used in the input
10917 BFD. */
10918 if (bed->elf_osabi == ELFOSABI_NONE
10919 || bed->elf_osabi == ELFOSABI_GNU
10920 || bed->elf_osabi == ELFOSABI_FREEBSD)
10921 elf_tdata (output_bfd)->has_gnu_osabi
10922 |= (elf_tdata (input_bfd)->has_gnu_osabi
10923 & (bfd_link_relocatable (flinfo->info)
10924 ? -1 : ~elf_gnu_osabi_retain));
10925
10926 /* Read the local symbols. */
10927 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10928 if (isymbuf == NULL && locsymcount != 0)
10929 {
10930 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
10931 flinfo->internal_syms,
10932 flinfo->external_syms,
10933 flinfo->locsym_shndx);
10934 if (isymbuf == NULL)
10935 return false;
10936 }
10937
10938 /* Find local symbol sections and adjust values of symbols in
10939 SEC_MERGE sections. Write out those local symbols we know are
10940 going into the output file. */
10941 isymend = PTR_ADD (isymbuf, locsymcount);
10942 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
10943 isym < isymend;
10944 isym++, pindex++, ppsection++)
10945 {
10946 asection *isec;
10947 const char *name;
10948 Elf_Internal_Sym osym;
10949 long indx;
10950 int ret;
10951
10952 *pindex = -1;
10953
10954 if (elf_bad_symtab (input_bfd))
10955 {
10956 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10957 {
10958 *ppsection = NULL;
10959 continue;
10960 }
10961 }
10962
10963 if (isym->st_shndx == SHN_UNDEF)
10964 isec = bfd_und_section_ptr;
10965 else if (isym->st_shndx == SHN_ABS)
10966 isec = bfd_abs_section_ptr;
10967 else if (isym->st_shndx == SHN_COMMON)
10968 isec = bfd_com_section_ptr;
10969 else
10970 {
10971 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10972 if (isec == NULL)
10973 {
10974 /* Don't attempt to output symbols with st_shnx in the
10975 reserved range other than SHN_ABS and SHN_COMMON. */
10976 isec = bfd_und_section_ptr;
10977 }
10978 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
10979 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10980 isym->st_value =
10981 _bfd_merged_section_offset (output_bfd, &isec,
10982 elf_section_data (isec)->sec_info,
10983 isym->st_value);
10984 }
10985
10986 *ppsection = isec;
10987
10988 /* Don't output the first, undefined, symbol. In fact, don't
10989 output any undefined local symbol. */
10990 if (isec == bfd_und_section_ptr)
10991 continue;
10992
10993 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10994 {
10995 /* We never output section symbols. Instead, we use the
10996 section symbol of the corresponding section in the output
10997 file. */
10998 continue;
10999 }
11000
11001 /* If we are stripping all symbols, we don't want to output this
11002 one. */
11003 if (flinfo->info->strip == strip_all)
11004 continue;
11005
11006 /* If we are discarding all local symbols, we don't want to
11007 output this one. If we are generating a relocatable output
11008 file, then some of the local symbols may be required by
11009 relocs; we output them below as we discover that they are
11010 needed. */
11011 if (flinfo->info->discard == discard_all)
11012 continue;
11013
11014 /* If this symbol is defined in a section which we are
11015 discarding, we don't need to keep it. */
11016 if (isym->st_shndx != SHN_UNDEF
11017 && isym->st_shndx < SHN_LORESERVE
11018 && isec->output_section == NULL
11019 && flinfo->info->non_contiguous_regions
11020 && flinfo->info->non_contiguous_regions_warnings)
11021 {
11022 _bfd_error_handler (_("warning: --enable-non-contiguous-regions "
11023 "discards section `%s' from '%s'\n"),
11024 isec->name, bfd_get_filename (isec->owner));
11025 continue;
11026 }
11027
11028 if (isym->st_shndx != SHN_UNDEF
11029 && isym->st_shndx < SHN_LORESERVE
11030 && bfd_section_removed_from_list (output_bfd,
11031 isec->output_section))
11032 continue;
11033
11034 /* Get the name of the symbol. */
11035 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
11036 isym->st_name);
11037 if (name == NULL)
11038 return false;
11039
11040 /* See if we are discarding symbols with this name. */
11041 if ((flinfo->info->strip == strip_some
11042 && (bfd_hash_lookup (flinfo->info->keep_hash, name, false, false)
11043 == NULL))
11044 || (((flinfo->info->discard == discard_sec_merge
11045 && (isec->flags & SEC_MERGE)
11046 && !bfd_link_relocatable (flinfo->info))
11047 || flinfo->info->discard == discard_l)
11048 && bfd_is_local_label_name (input_bfd, name)))
11049 continue;
11050
11051 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
11052 {
11053 if (input_bfd->lto_output)
11054 /* -flto puts a temp file name here. This means builds
11055 are not reproducible. Discard the symbol. */
11056 continue;
11057 have_file_sym = true;
11058 flinfo->filesym_count += 1;
11059 }
11060 if (!have_file_sym)
11061 {
11062 /* In the absence of debug info, bfd_find_nearest_line uses
11063 FILE symbols to determine the source file for local
11064 function symbols. Provide a FILE symbol here if input
11065 files lack such, so that their symbols won't be
11066 associated with a previous input file. It's not the
11067 source file, but the best we can do. */
11068 const char *filename;
11069 have_file_sym = true;
11070 flinfo->filesym_count += 1;
11071 memset (&osym, 0, sizeof (osym));
11072 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
11073 osym.st_shndx = SHN_ABS;
11074 if (input_bfd->lto_output)
11075 filename = NULL;
11076 else
11077 filename = lbasename (bfd_get_filename (input_bfd));
11078 if (!elf_link_output_symstrtab (flinfo, filename, &osym,
11079 bfd_abs_section_ptr, NULL))
11080 return false;
11081 }
11082
11083 osym = *isym;
11084
11085 /* Adjust the section index for the output file. */
11086 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11087 isec->output_section);
11088 if (osym.st_shndx == SHN_BAD)
11089 return false;
11090
11091 /* ELF symbols in relocatable files are section relative, but
11092 in executable files they are virtual addresses. Note that
11093 this code assumes that all ELF sections have an associated
11094 BFD section with a reasonable value for output_offset; below
11095 we assume that they also have a reasonable value for
11096 output_section. Any special sections must be set up to meet
11097 these requirements. */
11098 osym.st_value += isec->output_offset;
11099 if (!bfd_link_relocatable (flinfo->info))
11100 {
11101 osym.st_value += isec->output_section->vma;
11102 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
11103 {
11104 /* STT_TLS symbols are relative to PT_TLS segment base. */
11105 if (elf_hash_table (flinfo->info)->tls_sec != NULL)
11106 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
11107 else
11108 osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
11109 STT_NOTYPE);
11110 }
11111 }
11112
11113 indx = bfd_get_symcount (output_bfd);
11114 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
11115 if (ret == 0)
11116 return false;
11117 else if (ret == 1)
11118 *pindex = indx;
11119 }
11120
11121 if (bed->s->arch_size == 32)
11122 {
11123 r_type_mask = 0xff;
11124 r_sym_shift = 8;
11125 address_size = 4;
11126 }
11127 else
11128 {
11129 r_type_mask = 0xffffffff;
11130 r_sym_shift = 32;
11131 address_size = 8;
11132 }
11133
11134 /* Relocate the contents of each section. */
11135 sym_hashes = elf_sym_hashes (input_bfd);
11136 for (o = input_bfd->sections; o != NULL; o = o->next)
11137 {
11138 bfd_byte *contents;
11139
11140 if (! o->linker_mark)
11141 {
11142 /* This section was omitted from the link. */
11143 continue;
11144 }
11145
11146 if (!flinfo->info->resolve_section_groups
11147 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
11148 {
11149 /* Deal with the group signature symbol. */
11150 struct bfd_elf_section_data *sec_data = elf_section_data (o);
11151 unsigned long symndx = sec_data->this_hdr.sh_info;
11152 asection *osec = o->output_section;
11153
11154 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
11155 if (symndx >= locsymcount
11156 || (elf_bad_symtab (input_bfd)
11157 && flinfo->sections[symndx] == NULL))
11158 {
11159 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
11160 while (h->root.type == bfd_link_hash_indirect
11161 || h->root.type == bfd_link_hash_warning)
11162 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11163 /* Arrange for symbol to be output. */
11164 h->indx = -2;
11165 elf_section_data (osec)->this_hdr.sh_info = -2;
11166 }
11167 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
11168 {
11169 /* We'll use the output section target_index. */
11170 asection *sec = flinfo->sections[symndx]->output_section;
11171 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
11172 }
11173 else
11174 {
11175 if (flinfo->indices[symndx] == -1)
11176 {
11177 /* Otherwise output the local symbol now. */
11178 Elf_Internal_Sym sym = isymbuf[symndx];
11179 asection *sec = flinfo->sections[symndx]->output_section;
11180 const char *name;
11181 long indx;
11182 int ret;
11183
11184 name = bfd_elf_string_from_elf_section (input_bfd,
11185 symtab_hdr->sh_link,
11186 sym.st_name);
11187 if (name == NULL)
11188 return false;
11189
11190 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11191 sec);
11192 if (sym.st_shndx == SHN_BAD)
11193 return false;
11194
11195 sym.st_value += o->output_offset;
11196
11197 indx = bfd_get_symcount (output_bfd);
11198 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
11199 NULL);
11200 if (ret == 0)
11201 return false;
11202 else if (ret == 1)
11203 flinfo->indices[symndx] = indx;
11204 else
11205 abort ();
11206 }
11207 elf_section_data (osec)->this_hdr.sh_info
11208 = flinfo->indices[symndx];
11209 }
11210 }
11211
11212 if ((o->flags & SEC_HAS_CONTENTS) == 0
11213 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
11214 continue;
11215
11216 if ((o->flags & SEC_LINKER_CREATED) != 0)
11217 {
11218 /* Section was created by _bfd_elf_link_create_dynamic_sections
11219 or somesuch. */
11220 continue;
11221 }
11222
11223 /* Get the contents of the section. They have been cached by a
11224 relaxation routine. Note that o is a section in an input
11225 file, so the contents field will not have been set by any of
11226 the routines which work on output files. */
11227 if (elf_section_data (o)->this_hdr.contents != NULL)
11228 {
11229 contents = elf_section_data (o)->this_hdr.contents;
11230 if (bed->caches_rawsize
11231 && o->rawsize != 0
11232 && o->rawsize < o->size)
11233 {
11234 memcpy (flinfo->contents, contents, o->rawsize);
11235 contents = flinfo->contents;
11236 }
11237 }
11238 else
11239 {
11240 contents = flinfo->contents;
11241 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
11242 return false;
11243 }
11244
11245 if ((o->flags & SEC_RELOC) != 0)
11246 {
11247 Elf_Internal_Rela *internal_relocs;
11248 Elf_Internal_Rela *rel, *relend;
11249 int action_discarded;
11250 int ret;
11251
11252 /* Get the swapped relocs. */
11253 internal_relocs
11254 = _bfd_elf_link_info_read_relocs (input_bfd, flinfo->info, o,
11255 flinfo->external_relocs,
11256 flinfo->internal_relocs,
11257 false);
11258 if (internal_relocs == NULL
11259 && o->reloc_count > 0)
11260 return false;
11261
11262 action_discarded = -1;
11263 if (!elf_section_ignore_discarded_relocs (o))
11264 action_discarded = (*bed->action_discarded) (o);
11265
11266 /* Run through the relocs evaluating complex reloc symbols and
11267 looking for relocs against symbols from discarded sections
11268 or section symbols from removed link-once sections.
11269 Complain about relocs against discarded sections. Zero
11270 relocs against removed link-once sections. */
11271
11272 rel = internal_relocs;
11273 relend = rel + o->reloc_count;
11274 for ( ; rel < relend; rel++)
11275 {
11276 unsigned long r_symndx = rel->r_info >> r_sym_shift;
11277 unsigned int s_type;
11278 asection **ps, *sec;
11279 struct elf_link_hash_entry *h = NULL;
11280 const char *sym_name;
11281
11282 if (r_symndx == STN_UNDEF)
11283 continue;
11284
11285 if (r_symndx >= locsymcount
11286 || (elf_bad_symtab (input_bfd)
11287 && flinfo->sections[r_symndx] == NULL))
11288 {
11289 h = sym_hashes[r_symndx - extsymoff];
11290
11291 /* Badly formatted input files can contain relocs that
11292 reference non-existant symbols. Check here so that
11293 we do not seg fault. */
11294 if (h == NULL)
11295 {
11296 _bfd_error_handler
11297 /* xgettext:c-format */
11298 (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
11299 "that references a non-existent global symbol"),
11300 input_bfd, (uint64_t) rel->r_info, o);
11301 bfd_set_error (bfd_error_bad_value);
11302 return false;
11303 }
11304
11305 while (h->root.type == bfd_link_hash_indirect
11306 || h->root.type == bfd_link_hash_warning)
11307 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11308
11309 s_type = h->type;
11310
11311 /* If a plugin symbol is referenced from a non-IR file,
11312 mark the symbol as undefined. Note that the
11313 linker may attach linker created dynamic sections
11314 to the plugin bfd. Symbols defined in linker
11315 created sections are not plugin symbols. */
11316 if ((h->root.non_ir_ref_regular
11317 || h->root.non_ir_ref_dynamic)
11318 && (h->root.type == bfd_link_hash_defined
11319 || h->root.type == bfd_link_hash_defweak)
11320 && (h->root.u.def.section->flags
11321 & SEC_LINKER_CREATED) == 0
11322 && h->root.u.def.section->owner != NULL
11323 && (h->root.u.def.section->owner->flags
11324 & BFD_PLUGIN) != 0)
11325 {
11326 h->root.type = bfd_link_hash_undefined;
11327 h->root.u.undef.abfd = h->root.u.def.section->owner;
11328 }
11329
11330 ps = NULL;
11331 if (h->root.type == bfd_link_hash_defined
11332 || h->root.type == bfd_link_hash_defweak)
11333 ps = &h->root.u.def.section;
11334
11335 sym_name = h->root.root.string;
11336 }
11337 else
11338 {
11339 Elf_Internal_Sym *sym = isymbuf + r_symndx;
11340
11341 s_type = ELF_ST_TYPE (sym->st_info);
11342 ps = &flinfo->sections[r_symndx];
11343 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
11344 sym, *ps);
11345 }
11346
11347 if ((s_type == STT_RELC || s_type == STT_SRELC)
11348 && !bfd_link_relocatable (flinfo->info))
11349 {
11350 bfd_vma val;
11351 bfd_vma dot = (rel->r_offset
11352 + o->output_offset + o->output_section->vma);
11353 #ifdef DEBUG
11354 printf ("Encountered a complex symbol!");
11355 printf (" (input_bfd %s, section %s, reloc %ld\n",
11356 bfd_get_filename (input_bfd), o->name,
11357 (long) (rel - internal_relocs));
11358 printf (" symbol: idx %8.8lx, name %s\n",
11359 r_symndx, sym_name);
11360 printf (" reloc : info %8.8lx, addr %8.8lx\n",
11361 (unsigned long) rel->r_info,
11362 (unsigned long) rel->r_offset);
11363 #endif
11364 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
11365 isymbuf, locsymcount, s_type == STT_SRELC))
11366 return false;
11367
11368 /* Symbol evaluated OK. Update to absolute value. */
11369 set_symbol_value (input_bfd, isymbuf, locsymcount,
11370 r_symndx, val);
11371 continue;
11372 }
11373
11374 if (action_discarded != -1 && ps != NULL)
11375 {
11376 /* Complain if the definition comes from a
11377 discarded section. */
11378 if ((sec = *ps) != NULL && discarded_section (sec))
11379 {
11380 BFD_ASSERT (r_symndx != STN_UNDEF);
11381 if (action_discarded & COMPLAIN)
11382 (*flinfo->info->callbacks->einfo)
11383 /* xgettext:c-format */
11384 (_("%X`%s' referenced in section `%pA' of %pB: "
11385 "defined in discarded section `%pA' of %pB\n"),
11386 sym_name, o, input_bfd, sec, sec->owner);
11387
11388 /* Try to do the best we can to support buggy old
11389 versions of gcc. Pretend that the symbol is
11390 really defined in the kept linkonce section.
11391 FIXME: This is quite broken. Modifying the
11392 symbol here means we will be changing all later
11393 uses of the symbol, not just in this section. */
11394 if (action_discarded & PRETEND)
11395 {
11396 asection *kept;
11397
11398 kept = _bfd_elf_check_kept_section (sec,
11399 flinfo->info);
11400 if (kept != NULL)
11401 {
11402 *ps = kept;
11403 continue;
11404 }
11405 }
11406 }
11407 }
11408 }
11409
11410 /* Relocate the section by invoking a back end routine.
11411
11412 The back end routine is responsible for adjusting the
11413 section contents as necessary, and (if using Rela relocs
11414 and generating a relocatable output file) adjusting the
11415 reloc addend as necessary.
11416
11417 The back end routine does not have to worry about setting
11418 the reloc address or the reloc symbol index.
11419
11420 The back end routine is given a pointer to the swapped in
11421 internal symbols, and can access the hash table entries
11422 for the external symbols via elf_sym_hashes (input_bfd).
11423
11424 When generating relocatable output, the back end routine
11425 must handle STB_LOCAL/STT_SECTION symbols specially. The
11426 output symbol is going to be a section symbol
11427 corresponding to the output section, which will require
11428 the addend to be adjusted. */
11429
11430 ret = (*relocate_section) (output_bfd, flinfo->info,
11431 input_bfd, o, contents,
11432 internal_relocs,
11433 isymbuf,
11434 flinfo->sections);
11435 if (!ret)
11436 return false;
11437
11438 if (ret == 2
11439 || bfd_link_relocatable (flinfo->info)
11440 || flinfo->info->emitrelocations)
11441 {
11442 Elf_Internal_Rela *irela;
11443 Elf_Internal_Rela *irelaend, *irelamid;
11444 bfd_vma last_offset;
11445 struct elf_link_hash_entry **rel_hash;
11446 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
11447 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
11448 unsigned int next_erel;
11449 bool rela_normal;
11450 struct bfd_elf_section_data *esdi, *esdo;
11451
11452 esdi = elf_section_data (o);
11453 esdo = elf_section_data (o->output_section);
11454 rela_normal = false;
11455
11456 /* Adjust the reloc addresses and symbol indices. */
11457
11458 irela = internal_relocs;
11459 irelaend = irela + o->reloc_count;
11460 rel_hash = PTR_ADD (esdo->rel.hashes, esdo->rel.count);
11461 /* We start processing the REL relocs, if any. When we reach
11462 IRELAMID in the loop, we switch to the RELA relocs. */
11463 irelamid = irela;
11464 if (esdi->rel.hdr != NULL)
11465 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
11466 * bed->s->int_rels_per_ext_rel);
11467 rel_hash_list = rel_hash;
11468 rela_hash_list = NULL;
11469 last_offset = o->output_offset;
11470 if (!bfd_link_relocatable (flinfo->info))
11471 last_offset += o->output_section->vma;
11472 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
11473 {
11474 unsigned long r_symndx;
11475 asection *sec;
11476 Elf_Internal_Sym sym;
11477
11478 if (next_erel == bed->s->int_rels_per_ext_rel)
11479 {
11480 rel_hash++;
11481 next_erel = 0;
11482 }
11483
11484 if (irela == irelamid)
11485 {
11486 rel_hash = PTR_ADD (esdo->rela.hashes, esdo->rela.count);
11487 rela_hash_list = rel_hash;
11488 rela_normal = bed->rela_normal;
11489 }
11490
11491 irela->r_offset = _bfd_elf_section_offset (output_bfd,
11492 flinfo->info, o,
11493 irela->r_offset);
11494 if (irela->r_offset >= (bfd_vma) -2)
11495 {
11496 /* This is a reloc for a deleted entry or somesuch.
11497 Turn it into an R_*_NONE reloc, at the same
11498 offset as the last reloc. elf_eh_frame.c and
11499 bfd_elf_discard_info rely on reloc offsets
11500 being ordered. */
11501 irela->r_offset = last_offset;
11502 irela->r_info = 0;
11503 irela->r_addend = 0;
11504 continue;
11505 }
11506
11507 irela->r_offset += o->output_offset;
11508
11509 /* Relocs in an executable have to be virtual addresses. */
11510 if (!bfd_link_relocatable (flinfo->info))
11511 irela->r_offset += o->output_section->vma;
11512
11513 last_offset = irela->r_offset;
11514
11515 r_symndx = irela->r_info >> r_sym_shift;
11516 if (r_symndx == STN_UNDEF)
11517 continue;
11518
11519 if (r_symndx >= locsymcount
11520 || (elf_bad_symtab (input_bfd)
11521 && flinfo->sections[r_symndx] == NULL))
11522 {
11523 struct elf_link_hash_entry *rh;
11524 unsigned long indx;
11525
11526 /* This is a reloc against a global symbol. We
11527 have not yet output all the local symbols, so
11528 we do not know the symbol index of any global
11529 symbol. We set the rel_hash entry for this
11530 reloc to point to the global hash table entry
11531 for this symbol. The symbol index is then
11532 set at the end of bfd_elf_final_link. */
11533 indx = r_symndx - extsymoff;
11534 rh = elf_sym_hashes (input_bfd)[indx];
11535 while (rh->root.type == bfd_link_hash_indirect
11536 || rh->root.type == bfd_link_hash_warning)
11537 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
11538
11539 /* Setting the index to -2 tells
11540 elf_link_output_extsym that this symbol is
11541 used by a reloc. */
11542 BFD_ASSERT (rh->indx < 0);
11543 rh->indx = -2;
11544 *rel_hash = rh;
11545
11546 continue;
11547 }
11548
11549 /* This is a reloc against a local symbol. */
11550
11551 *rel_hash = NULL;
11552 sym = isymbuf[r_symndx];
11553 sec = flinfo->sections[r_symndx];
11554 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
11555 {
11556 /* I suppose the backend ought to fill in the
11557 section of any STT_SECTION symbol against a
11558 processor specific section. */
11559 r_symndx = STN_UNDEF;
11560 if (bfd_is_abs_section (sec))
11561 ;
11562 else if (sec == NULL || sec->owner == NULL)
11563 {
11564 bfd_set_error (bfd_error_bad_value);
11565 return false;
11566 }
11567 else
11568 {
11569 asection *osec = sec->output_section;
11570
11571 /* If we have discarded a section, the output
11572 section will be the absolute section. In
11573 case of discarded SEC_MERGE sections, use
11574 the kept section. relocate_section should
11575 have already handled discarded linkonce
11576 sections. */
11577 if (bfd_is_abs_section (osec)
11578 && sec->kept_section != NULL
11579 && sec->kept_section->output_section != NULL)
11580 {
11581 osec = sec->kept_section->output_section;
11582 irela->r_addend -= osec->vma;
11583 }
11584
11585 if (!bfd_is_abs_section (osec))
11586 {
11587 r_symndx = osec->target_index;
11588 if (r_symndx == STN_UNDEF)
11589 {
11590 irela->r_addend += osec->vma;
11591 osec = _bfd_nearby_section (output_bfd, osec,
11592 osec->vma);
11593 irela->r_addend -= osec->vma;
11594 r_symndx = osec->target_index;
11595 }
11596 }
11597 }
11598
11599 /* Adjust the addend according to where the
11600 section winds up in the output section. */
11601 if (rela_normal)
11602 irela->r_addend += sec->output_offset;
11603 }
11604 else
11605 {
11606 if (flinfo->indices[r_symndx] == -1)
11607 {
11608 unsigned long shlink;
11609 const char *name;
11610 asection *osec;
11611 long indx;
11612
11613 if (flinfo->info->strip == strip_all)
11614 {
11615 /* You can't do ld -r -s. */
11616 bfd_set_error (bfd_error_invalid_operation);
11617 return false;
11618 }
11619
11620 /* This symbol was skipped earlier, but
11621 since it is needed by a reloc, we
11622 must output it now. */
11623 shlink = symtab_hdr->sh_link;
11624 name = (bfd_elf_string_from_elf_section
11625 (input_bfd, shlink, sym.st_name));
11626 if (name == NULL)
11627 return false;
11628
11629 osec = sec->output_section;
11630 sym.st_shndx =
11631 _bfd_elf_section_from_bfd_section (output_bfd,
11632 osec);
11633 if (sym.st_shndx == SHN_BAD)
11634 return false;
11635
11636 sym.st_value += sec->output_offset;
11637 if (!bfd_link_relocatable (flinfo->info))
11638 {
11639 sym.st_value += osec->vma;
11640 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
11641 {
11642 struct elf_link_hash_table *htab
11643 = elf_hash_table (flinfo->info);
11644
11645 /* STT_TLS symbols are relative to PT_TLS
11646 segment base. */
11647 if (htab->tls_sec != NULL)
11648 sym.st_value -= htab->tls_sec->vma;
11649 else
11650 sym.st_info
11651 = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
11652 STT_NOTYPE);
11653 }
11654 }
11655
11656 indx = bfd_get_symcount (output_bfd);
11657 ret = elf_link_output_symstrtab (flinfo, name,
11658 &sym, sec,
11659 NULL);
11660 if (ret == 0)
11661 return false;
11662 else if (ret == 1)
11663 flinfo->indices[r_symndx] = indx;
11664 else
11665 abort ();
11666 }
11667
11668 r_symndx = flinfo->indices[r_symndx];
11669 }
11670
11671 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
11672 | (irela->r_info & r_type_mask));
11673 }
11674
11675 /* Swap out the relocs. */
11676 input_rel_hdr = esdi->rel.hdr;
11677 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
11678 {
11679 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11680 input_rel_hdr,
11681 internal_relocs,
11682 rel_hash_list))
11683 return false;
11684 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
11685 * bed->s->int_rels_per_ext_rel);
11686 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
11687 }
11688
11689 input_rela_hdr = esdi->rela.hdr;
11690 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
11691 {
11692 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11693 input_rela_hdr,
11694 internal_relocs,
11695 rela_hash_list))
11696 return false;
11697 }
11698 }
11699 }
11700
11701 /* Write out the modified section contents. */
11702 if (bed->elf_backend_write_section
11703 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
11704 contents))
11705 {
11706 /* Section written out. */
11707 }
11708 else switch (o->sec_info_type)
11709 {
11710 case SEC_INFO_TYPE_STABS:
11711 if (! (_bfd_write_section_stabs
11712 (output_bfd,
11713 &elf_hash_table (flinfo->info)->stab_info,
11714 o, &elf_section_data (o)->sec_info, contents)))
11715 return false;
11716 break;
11717 case SEC_INFO_TYPE_MERGE:
11718 if (! _bfd_write_merged_section (output_bfd, o,
11719 elf_section_data (o)->sec_info))
11720 return false;
11721 break;
11722 case SEC_INFO_TYPE_EH_FRAME:
11723 {
11724 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
11725 o, contents))
11726 return false;
11727 }
11728 break;
11729 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11730 {
11731 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11732 flinfo->info,
11733 o, contents))
11734 return false;
11735 }
11736 break;
11737 default:
11738 {
11739 if (! (o->flags & SEC_EXCLUDE))
11740 {
11741 file_ptr offset = (file_ptr) o->output_offset;
11742 bfd_size_type todo = o->size;
11743
11744 offset *= bfd_octets_per_byte (output_bfd, o);
11745
11746 if ((o->flags & SEC_ELF_REVERSE_COPY)
11747 && o->size > address_size)
11748 {
11749 /* Reverse-copy input section to output. */
11750
11751 if (o->reloc_count != 0
11752 && (o->size * bed->s->int_rels_per_ext_rel
11753 != o->reloc_count * address_size))
11754 {
11755 _bfd_error_handler
11756 /* xgettext:c-format */
11757 (_("error: %pB: size of section %pA is not "
11758 "multiple of address size"),
11759 input_bfd, o);
11760 bfd_set_error (bfd_error_bad_value);
11761 return false;
11762 }
11763
11764 do
11765 {
11766 todo -= address_size;
11767 if (! bfd_set_section_contents (output_bfd,
11768 o->output_section,
11769 contents + todo,
11770 offset,
11771 address_size))
11772 return false;
11773 if (todo == 0)
11774 break;
11775 offset += address_size;
11776 }
11777 while (1);
11778 }
11779 else if (! bfd_set_section_contents (output_bfd,
11780 o->output_section,
11781 contents,
11782 offset, todo))
11783 return false;
11784 }
11785 }
11786 break;
11787 }
11788 }
11789
11790 return true;
11791 }
11792
11793 /* Generate a reloc when linking an ELF file. This is a reloc
11794 requested by the linker, and does not come from any input file. This
11795 is used to build constructor and destructor tables when linking
11796 with -Ur. */
11797
11798 static bool
11799 elf_reloc_link_order (bfd *output_bfd,
11800 struct bfd_link_info *info,
11801 asection *output_section,
11802 struct bfd_link_order *link_order)
11803 {
11804 reloc_howto_type *howto;
11805 long indx;
11806 bfd_vma offset;
11807 bfd_vma addend;
11808 struct bfd_elf_section_reloc_data *reldata;
11809 struct elf_link_hash_entry **rel_hash_ptr;
11810 Elf_Internal_Shdr *rel_hdr;
11811 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11812 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11813 bfd_byte *erel;
11814 unsigned int i;
11815 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
11816
11817 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11818 if (howto == NULL)
11819 {
11820 bfd_set_error (bfd_error_bad_value);
11821 return false;
11822 }
11823
11824 addend = link_order->u.reloc.p->addend;
11825
11826 if (esdo->rel.hdr)
11827 reldata = &esdo->rel;
11828 else if (esdo->rela.hdr)
11829 reldata = &esdo->rela;
11830 else
11831 {
11832 reldata = NULL;
11833 BFD_ASSERT (0);
11834 }
11835
11836 /* Figure out the symbol index. */
11837 rel_hash_ptr = reldata->hashes + reldata->count;
11838 if (link_order->type == bfd_section_reloc_link_order)
11839 {
11840 indx = link_order->u.reloc.p->u.section->target_index;
11841 BFD_ASSERT (indx != 0);
11842 *rel_hash_ptr = NULL;
11843 }
11844 else
11845 {
11846 struct elf_link_hash_entry *h;
11847
11848 /* Treat a reloc against a defined symbol as though it were
11849 actually against the section. */
11850 h = ((struct elf_link_hash_entry *)
11851 bfd_wrapped_link_hash_lookup (output_bfd, info,
11852 link_order->u.reloc.p->u.name,
11853 false, false, true));
11854 if (h != NULL
11855 && (h->root.type == bfd_link_hash_defined
11856 || h->root.type == bfd_link_hash_defweak))
11857 {
11858 asection *section;
11859
11860 section = h->root.u.def.section;
11861 indx = section->output_section->target_index;
11862 *rel_hash_ptr = NULL;
11863 /* It seems that we ought to add the symbol value to the
11864 addend here, but in practice it has already been added
11865 because it was passed to constructor_callback. */
11866 addend += section->output_section->vma + section->output_offset;
11867 }
11868 else if (h != NULL)
11869 {
11870 /* Setting the index to -2 tells elf_link_output_extsym that
11871 this symbol is used by a reloc. */
11872 h->indx = -2;
11873 *rel_hash_ptr = h;
11874 indx = 0;
11875 }
11876 else
11877 {
11878 (*info->callbacks->unattached_reloc)
11879 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
11880 indx = 0;
11881 }
11882 }
11883
11884 /* If this is an inplace reloc, we must write the addend into the
11885 object file. */
11886 if (howto->partial_inplace && addend != 0)
11887 {
11888 bfd_size_type size;
11889 bfd_reloc_status_type rstat;
11890 bfd_byte *buf;
11891 bool ok;
11892 const char *sym_name;
11893 bfd_size_type octets;
11894
11895 size = (bfd_size_type) bfd_get_reloc_size (howto);
11896 buf = (bfd_byte *) bfd_zmalloc (size);
11897 if (buf == NULL && size != 0)
11898 return false;
11899 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11900 switch (rstat)
11901 {
11902 case bfd_reloc_ok:
11903 break;
11904
11905 default:
11906 case bfd_reloc_outofrange:
11907 abort ();
11908
11909 case bfd_reloc_overflow:
11910 if (link_order->type == bfd_section_reloc_link_order)
11911 sym_name = bfd_section_name (link_order->u.reloc.p->u.section);
11912 else
11913 sym_name = link_order->u.reloc.p->u.name;
11914 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11915 howto->name, addend, NULL, NULL,
11916 (bfd_vma) 0);
11917 break;
11918 }
11919
11920 octets = link_order->offset * bfd_octets_per_byte (output_bfd,
11921 output_section);
11922 ok = bfd_set_section_contents (output_bfd, output_section, buf,
11923 octets, size);
11924 free (buf);
11925 if (! ok)
11926 return false;
11927 }
11928
11929 /* The address of a reloc is relative to the section in a
11930 relocatable file, and is a virtual address in an executable
11931 file. */
11932 offset = link_order->offset;
11933 if (! bfd_link_relocatable (info))
11934 offset += output_section->vma;
11935
11936 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11937 {
11938 irel[i].r_offset = offset;
11939 irel[i].r_info = 0;
11940 irel[i].r_addend = 0;
11941 }
11942 if (bed->s->arch_size == 32)
11943 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11944 else
11945 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11946
11947 rel_hdr = reldata->hdr;
11948 erel = rel_hdr->contents;
11949 if (rel_hdr->sh_type == SHT_REL)
11950 {
11951 erel += reldata->count * bed->s->sizeof_rel;
11952 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11953 }
11954 else
11955 {
11956 irel[0].r_addend = addend;
11957 erel += reldata->count * bed->s->sizeof_rela;
11958 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11959 }
11960
11961 ++reldata->count;
11962
11963 return true;
11964 }
11965
11966 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11967 Returns TRUE upon success, FALSE otherwise. */
11968
11969 static bool
11970 elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11971 {
11972 bool ret = false;
11973 bfd *implib_bfd;
11974 const struct elf_backend_data *bed;
11975 flagword flags;
11976 enum bfd_architecture arch;
11977 unsigned int mach;
11978 asymbol **sympp = NULL;
11979 long symsize;
11980 long symcount;
11981 long src_count;
11982 elf_symbol_type *osymbuf;
11983 size_t amt;
11984
11985 implib_bfd = info->out_implib_bfd;
11986 bed = get_elf_backend_data (abfd);
11987
11988 if (!bfd_set_format (implib_bfd, bfd_object))
11989 return false;
11990
11991 /* Use flag from executable but make it a relocatable object. */
11992 flags = bfd_get_file_flags (abfd);
11993 flags &= ~HAS_RELOC;
11994 if (!bfd_set_start_address (implib_bfd, 0)
11995 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
11996 return false;
11997
11998 /* Copy architecture of output file to import library file. */
11999 arch = bfd_get_arch (abfd);
12000 mach = bfd_get_mach (abfd);
12001 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
12002 && (abfd->target_defaulted
12003 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
12004 return false;
12005
12006 /* Get symbol table size. */
12007 symsize = bfd_get_symtab_upper_bound (abfd);
12008 if (symsize < 0)
12009 return false;
12010
12011 /* Read in the symbol table. */
12012 sympp = (asymbol **) bfd_malloc (symsize);
12013 if (sympp == NULL)
12014 return false;
12015
12016 symcount = bfd_canonicalize_symtab (abfd, sympp);
12017 if (symcount < 0)
12018 goto free_sym_buf;
12019
12020 /* Allow the BFD backend to copy any private header data it
12021 understands from the output BFD to the import library BFD. */
12022 if (! bfd_copy_private_header_data (abfd, implib_bfd))
12023 goto free_sym_buf;
12024
12025 /* Filter symbols to appear in the import library. */
12026 if (bed->elf_backend_filter_implib_symbols)
12027 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
12028 symcount);
12029 else
12030 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
12031 if (symcount == 0)
12032 {
12033 bfd_set_error (bfd_error_no_symbols);
12034 _bfd_error_handler (_("%pB: no symbol found for import library"),
12035 implib_bfd);
12036 goto free_sym_buf;
12037 }
12038
12039
12040 /* Make symbols absolute. */
12041 amt = symcount * sizeof (*osymbuf);
12042 osymbuf = (elf_symbol_type *) bfd_alloc (implib_bfd, amt);
12043 if (osymbuf == NULL)
12044 goto free_sym_buf;
12045
12046 for (src_count = 0; src_count < symcount; src_count++)
12047 {
12048 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
12049 sizeof (*osymbuf));
12050 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
12051 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
12052 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
12053 osymbuf[src_count].internal_elf_sym.st_value =
12054 osymbuf[src_count].symbol.value;
12055 sympp[src_count] = &osymbuf[src_count].symbol;
12056 }
12057
12058 bfd_set_symtab (implib_bfd, sympp, symcount);
12059
12060 /* Allow the BFD backend to copy any private data it understands
12061 from the output BFD to the import library BFD. This is done last
12062 to permit the routine to look at the filtered symbol table. */
12063 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
12064 goto free_sym_buf;
12065
12066 if (!bfd_close (implib_bfd))
12067 goto free_sym_buf;
12068
12069 ret = true;
12070
12071 free_sym_buf:
12072 free (sympp);
12073 return ret;
12074 }
12075
12076 static void
12077 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
12078 {
12079 asection *o;
12080
12081 if (flinfo->symstrtab != NULL)
12082 _bfd_elf_strtab_free (flinfo->symstrtab);
12083 free (flinfo->contents);
12084 free (flinfo->external_relocs);
12085 free (flinfo->internal_relocs);
12086 free (flinfo->external_syms);
12087 free (flinfo->locsym_shndx);
12088 free (flinfo->internal_syms);
12089 free (flinfo->indices);
12090 free (flinfo->sections);
12091 if (flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1)
12092 free (flinfo->symshndxbuf);
12093 for (o = obfd->sections; o != NULL; o = o->next)
12094 {
12095 struct bfd_elf_section_data *esdo = elf_section_data (o);
12096 free (esdo->rel.hashes);
12097 free (esdo->rela.hashes);
12098 }
12099 }
12100
12101 /* Do the final step of an ELF link. */
12102
12103 bool
12104 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
12105 {
12106 bool dynamic;
12107 bool emit_relocs;
12108 bfd *dynobj;
12109 struct elf_final_link_info flinfo;
12110 asection *o;
12111 struct bfd_link_order *p;
12112 bfd *sub;
12113 bfd_size_type max_contents_size;
12114 bfd_size_type max_external_reloc_size;
12115 bfd_size_type max_internal_reloc_count;
12116 bfd_size_type max_sym_count;
12117 bfd_size_type max_sym_shndx_count;
12118 Elf_Internal_Sym elfsym;
12119 unsigned int i;
12120 Elf_Internal_Shdr *symtab_hdr;
12121 Elf_Internal_Shdr *symtab_shndx_hdr;
12122 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12123 struct elf_outext_info eoinfo;
12124 bool merged;
12125 size_t relativecount = 0;
12126 asection *reldyn = 0;
12127 bfd_size_type amt;
12128 asection *attr_section = NULL;
12129 bfd_vma attr_size = 0;
12130 const char *std_attrs_section;
12131 struct elf_link_hash_table *htab = elf_hash_table (info);
12132 bool sections_removed;
12133 bool ret;
12134
12135 if (!is_elf_hash_table (&htab->root))
12136 return false;
12137
12138 if (bfd_link_pic (info))
12139 abfd->flags |= DYNAMIC;
12140
12141 dynamic = htab->dynamic_sections_created;
12142 dynobj = htab->dynobj;
12143
12144 emit_relocs = (bfd_link_relocatable (info)
12145 || info->emitrelocations);
12146
12147 memset (&flinfo, 0, sizeof (flinfo));
12148 flinfo.info = info;
12149 flinfo.output_bfd = abfd;
12150 flinfo.symstrtab = _bfd_elf_strtab_init ();
12151 if (flinfo.symstrtab == NULL)
12152 return false;
12153
12154 if (! dynamic)
12155 {
12156 flinfo.hash_sec = NULL;
12157 flinfo.symver_sec = NULL;
12158 }
12159 else
12160 {
12161 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
12162 /* Note that dynsym_sec can be NULL (on VMS). */
12163 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
12164 /* Note that it is OK if symver_sec is NULL. */
12165 }
12166
12167 if (info->unique_symbol
12168 && !bfd_hash_table_init (&flinfo.local_hash_table,
12169 local_hash_newfunc,
12170 sizeof (struct local_hash_entry)))
12171 return false;
12172
12173 /* The object attributes have been merged. Remove the input
12174 sections from the link, and set the contents of the output
12175 section. */
12176 sections_removed = false;
12177 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
12178 for (o = abfd->sections; o != NULL; o = o->next)
12179 {
12180 bool remove_section = false;
12181
12182 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
12183 || strcmp (o->name, ".gnu.attributes") == 0)
12184 {
12185 for (p = o->map_head.link_order; p != NULL; p = p->next)
12186 {
12187 asection *input_section;
12188
12189 if (p->type != bfd_indirect_link_order)
12190 continue;
12191 input_section = p->u.indirect.section;
12192 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12193 elf_link_input_bfd ignores this section. */
12194 input_section->flags &= ~SEC_HAS_CONTENTS;
12195 }
12196
12197 attr_size = bfd_elf_obj_attr_size (abfd);
12198 bfd_set_section_size (o, attr_size);
12199 /* Skip this section later on. */
12200 o->map_head.link_order = NULL;
12201 if (attr_size)
12202 attr_section = o;
12203 else
12204 remove_section = true;
12205 }
12206 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
12207 {
12208 /* Remove empty group section from linker output. */
12209 remove_section = true;
12210 }
12211 if (remove_section)
12212 {
12213 o->flags |= SEC_EXCLUDE;
12214 bfd_section_list_remove (abfd, o);
12215 abfd->section_count--;
12216 sections_removed = true;
12217 }
12218 }
12219 if (sections_removed)
12220 _bfd_fix_excluded_sec_syms (abfd, info);
12221
12222 /* Count up the number of relocations we will output for each output
12223 section, so that we know the sizes of the reloc sections. We
12224 also figure out some maximum sizes. */
12225 max_contents_size = 0;
12226 max_external_reloc_size = 0;
12227 max_internal_reloc_count = 0;
12228 max_sym_count = 0;
12229 max_sym_shndx_count = 0;
12230 merged = false;
12231 for (o = abfd->sections; o != NULL; o = o->next)
12232 {
12233 struct bfd_elf_section_data *esdo = elf_section_data (o);
12234 o->reloc_count = 0;
12235
12236 for (p = o->map_head.link_order; p != NULL; p = p->next)
12237 {
12238 unsigned int reloc_count = 0;
12239 unsigned int additional_reloc_count = 0;
12240 struct bfd_elf_section_data *esdi = NULL;
12241
12242 if (p->type == bfd_section_reloc_link_order
12243 || p->type == bfd_symbol_reloc_link_order)
12244 reloc_count = 1;
12245 else if (p->type == bfd_indirect_link_order)
12246 {
12247 asection *sec;
12248
12249 sec = p->u.indirect.section;
12250
12251 /* Mark all sections which are to be included in the
12252 link. This will normally be every section. We need
12253 to do this so that we can identify any sections which
12254 the linker has decided to not include. */
12255 sec->linker_mark = true;
12256
12257 if (sec->flags & SEC_MERGE)
12258 merged = true;
12259
12260 if (sec->rawsize > max_contents_size)
12261 max_contents_size = sec->rawsize;
12262 if (sec->size > max_contents_size)
12263 max_contents_size = sec->size;
12264
12265 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
12266 && (sec->owner->flags & DYNAMIC) == 0)
12267 {
12268 size_t sym_count;
12269
12270 /* We are interested in just local symbols, not all
12271 symbols. */
12272 if (elf_bad_symtab (sec->owner))
12273 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
12274 / bed->s->sizeof_sym);
12275 else
12276 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
12277
12278 if (sym_count > max_sym_count)
12279 max_sym_count = sym_count;
12280
12281 if (sym_count > max_sym_shndx_count
12282 && elf_symtab_shndx_list (sec->owner) != NULL)
12283 max_sym_shndx_count = sym_count;
12284
12285 if (esdo->this_hdr.sh_type == SHT_REL
12286 || esdo->this_hdr.sh_type == SHT_RELA)
12287 /* Some backends use reloc_count in relocation sections
12288 to count particular types of relocs. Of course,
12289 reloc sections themselves can't have relocations. */
12290 ;
12291 else if (emit_relocs)
12292 {
12293 reloc_count = sec->reloc_count;
12294 if (bed->elf_backend_count_additional_relocs)
12295 {
12296 int c;
12297 c = (*bed->elf_backend_count_additional_relocs) (sec);
12298 additional_reloc_count += c;
12299 }
12300 }
12301 else if (bed->elf_backend_count_relocs)
12302 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
12303
12304 esdi = elf_section_data (sec);
12305
12306 if ((sec->flags & SEC_RELOC) != 0)
12307 {
12308 size_t ext_size = 0;
12309
12310 if (esdi->rel.hdr != NULL)
12311 ext_size = esdi->rel.hdr->sh_size;
12312 if (esdi->rela.hdr != NULL)
12313 ext_size += esdi->rela.hdr->sh_size;
12314
12315 if (ext_size > max_external_reloc_size)
12316 max_external_reloc_size = ext_size;
12317 if (sec->reloc_count > max_internal_reloc_count)
12318 max_internal_reloc_count = sec->reloc_count;
12319 }
12320 }
12321 }
12322
12323 if (reloc_count == 0)
12324 continue;
12325
12326 reloc_count += additional_reloc_count;
12327 o->reloc_count += reloc_count;
12328
12329 if (p->type == bfd_indirect_link_order && emit_relocs)
12330 {
12331 if (esdi->rel.hdr)
12332 {
12333 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
12334 esdo->rel.count += additional_reloc_count;
12335 }
12336 if (esdi->rela.hdr)
12337 {
12338 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
12339 esdo->rela.count += additional_reloc_count;
12340 }
12341 }
12342 else
12343 {
12344 if (o->use_rela_p)
12345 esdo->rela.count += reloc_count;
12346 else
12347 esdo->rel.count += reloc_count;
12348 }
12349 }
12350
12351 if (o->reloc_count > 0)
12352 o->flags |= SEC_RELOC;
12353 else
12354 {
12355 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12356 set it (this is probably a bug) and if it is set
12357 assign_section_numbers will create a reloc section. */
12358 o->flags &=~ SEC_RELOC;
12359 }
12360
12361 /* If the SEC_ALLOC flag is not set, force the section VMA to
12362 zero. This is done in elf_fake_sections as well, but forcing
12363 the VMA to 0 here will ensure that relocs against these
12364 sections are handled correctly. */
12365 if ((o->flags & SEC_ALLOC) == 0
12366 && ! o->user_set_vma)
12367 o->vma = 0;
12368 }
12369
12370 if (! bfd_link_relocatable (info) && merged)
12371 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
12372
12373 /* Figure out the file positions for everything but the symbol table
12374 and the relocs. We set symcount to force assign_section_numbers
12375 to create a symbol table. */
12376 abfd->symcount = info->strip != strip_all || emit_relocs;
12377 BFD_ASSERT (! abfd->output_has_begun);
12378 if (! _bfd_elf_compute_section_file_positions (abfd, info))
12379 goto error_return;
12380
12381 /* Set sizes, and assign file positions for reloc sections. */
12382 for (o = abfd->sections; o != NULL; o = o->next)
12383 {
12384 struct bfd_elf_section_data *esdo = elf_section_data (o);
12385 if ((o->flags & SEC_RELOC) != 0)
12386 {
12387 if (esdo->rel.hdr
12388 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
12389 goto error_return;
12390
12391 if (esdo->rela.hdr
12392 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
12393 goto error_return;
12394 }
12395
12396 /* _bfd_elf_compute_section_file_positions makes temporary use
12397 of target_index. Reset it. */
12398 o->target_index = 0;
12399
12400 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12401 to count upwards while actually outputting the relocations. */
12402 esdo->rel.count = 0;
12403 esdo->rela.count = 0;
12404
12405 if ((esdo->this_hdr.sh_offset == (file_ptr) -1)
12406 && !bfd_section_is_ctf (o))
12407 {
12408 /* Cache the section contents so that they can be compressed
12409 later. Use bfd_malloc since it will be freed by
12410 bfd_compress_section_contents. */
12411 unsigned char *contents = esdo->this_hdr.contents;
12412 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
12413 abort ();
12414 contents
12415 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
12416 if (contents == NULL)
12417 goto error_return;
12418 esdo->this_hdr.contents = contents;
12419 }
12420 }
12421
12422 /* We have now assigned file positions for all the sections except .symtab,
12423 .strtab, and non-loaded reloc and compressed debugging sections. We start
12424 the .symtab section at the current file position, and write directly to it.
12425 We build the .strtab section in memory. */
12426 abfd->symcount = 0;
12427 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12428 /* sh_name is set in prep_headers. */
12429 symtab_hdr->sh_type = SHT_SYMTAB;
12430 /* sh_flags, sh_addr and sh_size all start off zero. */
12431 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
12432 /* sh_link is set in assign_section_numbers. */
12433 /* sh_info is set below. */
12434 /* sh_offset is set just below. */
12435 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
12436
12437 if (max_sym_count < 20)
12438 max_sym_count = 20;
12439 htab->strtabsize = max_sym_count;
12440 amt = max_sym_count * sizeof (struct elf_sym_strtab);
12441 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
12442 if (htab->strtab == NULL)
12443 goto error_return;
12444 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12445 flinfo.symshndxbuf
12446 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
12447 ? (Elf_External_Sym_Shndx *) -1 : NULL);
12448
12449 if (info->strip != strip_all || emit_relocs)
12450 {
12451 file_ptr off = elf_next_file_pos (abfd);
12452
12453 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, true);
12454
12455 /* Note that at this point elf_next_file_pos (abfd) is
12456 incorrect. We do not yet know the size of the .symtab section.
12457 We correct next_file_pos below, after we do know the size. */
12458
12459 /* Start writing out the symbol table. The first symbol is always a
12460 dummy symbol. */
12461 elfsym.st_value = 0;
12462 elfsym.st_size = 0;
12463 elfsym.st_info = 0;
12464 elfsym.st_other = 0;
12465 elfsym.st_shndx = SHN_UNDEF;
12466 elfsym.st_target_internal = 0;
12467 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
12468 bfd_und_section_ptr, NULL) != 1)
12469 goto error_return;
12470
12471 /* Output a symbol for each section if asked or they are used for
12472 relocs. These symbols usually have no names. We store the
12473 index of each one in the index field of the section, so that
12474 we can find it again when outputting relocs. */
12475
12476 if (bfd_keep_unused_section_symbols (abfd) || emit_relocs)
12477 {
12478 bool name_local_sections
12479 = (bed->elf_backend_name_local_section_symbols
12480 && bed->elf_backend_name_local_section_symbols (abfd));
12481 const char *name = NULL;
12482
12483 elfsym.st_size = 0;
12484 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12485 elfsym.st_other = 0;
12486 elfsym.st_value = 0;
12487 elfsym.st_target_internal = 0;
12488 for (i = 1; i < elf_numsections (abfd); i++)
12489 {
12490 o = bfd_section_from_elf_index (abfd, i);
12491 if (o != NULL)
12492 {
12493 o->target_index = bfd_get_symcount (abfd);
12494 elfsym.st_shndx = i;
12495 if (!bfd_link_relocatable (info))
12496 elfsym.st_value = o->vma;
12497 if (name_local_sections)
12498 name = o->name;
12499 if (elf_link_output_symstrtab (&flinfo, name, &elfsym, o,
12500 NULL) != 1)
12501 goto error_return;
12502 }
12503 }
12504 }
12505 }
12506
12507 /* On some targets like Irix 5 the symbol split between local and global
12508 ones recorded in the sh_info field needs to be done between section
12509 and all other symbols. */
12510 if (bed->elf_backend_elfsym_local_is_section
12511 && bed->elf_backend_elfsym_local_is_section (abfd))
12512 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12513
12514 /* Allocate some memory to hold information read in from the input
12515 files. */
12516 if (max_contents_size != 0)
12517 {
12518 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12519 if (flinfo.contents == NULL)
12520 goto error_return;
12521 }
12522
12523 if (max_external_reloc_size != 0)
12524 {
12525 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
12526 if (flinfo.external_relocs == NULL)
12527 goto error_return;
12528 }
12529
12530 if (max_internal_reloc_count != 0)
12531 {
12532 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
12533 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
12534 if (flinfo.internal_relocs == NULL)
12535 goto error_return;
12536 }
12537
12538 if (max_sym_count != 0)
12539 {
12540 amt = max_sym_count * bed->s->sizeof_sym;
12541 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
12542 if (flinfo.external_syms == NULL)
12543 goto error_return;
12544
12545 amt = max_sym_count * sizeof (Elf_Internal_Sym);
12546 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
12547 if (flinfo.internal_syms == NULL)
12548 goto error_return;
12549
12550 amt = max_sym_count * sizeof (long);
12551 flinfo.indices = (long int *) bfd_malloc (amt);
12552 if (flinfo.indices == NULL)
12553 goto error_return;
12554
12555 amt = max_sym_count * sizeof (asection *);
12556 flinfo.sections = (asection **) bfd_malloc (amt);
12557 if (flinfo.sections == NULL)
12558 goto error_return;
12559 }
12560
12561 if (max_sym_shndx_count != 0)
12562 {
12563 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
12564 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
12565 if (flinfo.locsym_shndx == NULL)
12566 goto error_return;
12567 }
12568
12569 if (htab->tls_sec)
12570 {
12571 bfd_vma base, end = 0; /* Both bytes. */
12572 asection *sec;
12573
12574 for (sec = htab->tls_sec;
12575 sec && (sec->flags & SEC_THREAD_LOCAL);
12576 sec = sec->next)
12577 {
12578 bfd_size_type size = sec->size;
12579 unsigned int opb = bfd_octets_per_byte (abfd, sec);
12580
12581 if (size == 0
12582 && (sec->flags & SEC_HAS_CONTENTS) == 0)
12583 {
12584 struct bfd_link_order *ord = sec->map_tail.link_order;
12585
12586 if (ord != NULL)
12587 size = ord->offset * opb + ord->size;
12588 }
12589 end = sec->vma + size / opb;
12590 }
12591 base = htab->tls_sec->vma;
12592 /* Only align end of TLS section if static TLS doesn't have special
12593 alignment requirements. */
12594 if (bed->static_tls_alignment == 1)
12595 end = align_power (end, htab->tls_sec->alignment_power);
12596 htab->tls_size = end - base;
12597 }
12598
12599 if (!_bfd_elf_fixup_eh_frame_hdr (info))
12600 return false;
12601
12602 /* Since ELF permits relocations to be against local symbols, we
12603 must have the local symbols available when we do the relocations.
12604 Since we would rather only read the local symbols once, and we
12605 would rather not keep them in memory, we handle all the
12606 relocations for a single input file at the same time.
12607
12608 Unfortunately, there is no way to know the total number of local
12609 symbols until we have seen all of them, and the local symbol
12610 indices precede the global symbol indices. This means that when
12611 we are generating relocatable output, and we see a reloc against
12612 a global symbol, we can not know the symbol index until we have
12613 finished examining all the local symbols to see which ones we are
12614 going to output. To deal with this, we keep the relocations in
12615 memory, and don't output them until the end of the link. This is
12616 an unfortunate waste of memory, but I don't see a good way around
12617 it. Fortunately, it only happens when performing a relocatable
12618 link, which is not the common case. FIXME: If keep_memory is set
12619 we could write the relocs out and then read them again; I don't
12620 know how bad the memory loss will be. */
12621
12622 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12623 sub->output_has_begun = false;
12624 for (o = abfd->sections; o != NULL; o = o->next)
12625 {
12626 for (p = o->map_head.link_order; p != NULL; p = p->next)
12627 {
12628 if (p->type == bfd_indirect_link_order
12629 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12630 == bfd_target_elf_flavour)
12631 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12632 {
12633 if (! sub->output_has_begun)
12634 {
12635 if (! elf_link_input_bfd (&flinfo, sub))
12636 goto error_return;
12637 sub->output_has_begun = true;
12638 }
12639 }
12640 else if (p->type == bfd_section_reloc_link_order
12641 || p->type == bfd_symbol_reloc_link_order)
12642 {
12643 if (! elf_reloc_link_order (abfd, info, o, p))
12644 goto error_return;
12645 }
12646 else
12647 {
12648 if (! _bfd_default_link_order (abfd, info, o, p))
12649 {
12650 if (p->type == bfd_indirect_link_order
12651 && (bfd_get_flavour (sub)
12652 == bfd_target_elf_flavour)
12653 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12654 != bed->s->elfclass))
12655 {
12656 const char *iclass, *oclass;
12657
12658 switch (bed->s->elfclass)
12659 {
12660 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12661 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12662 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12663 default: abort ();
12664 }
12665
12666 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
12667 {
12668 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12669 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12670 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12671 default: abort ();
12672 }
12673
12674 bfd_set_error (bfd_error_wrong_format);
12675 _bfd_error_handler
12676 /* xgettext:c-format */
12677 (_("%pB: file class %s incompatible with %s"),
12678 sub, iclass, oclass);
12679 }
12680
12681 goto error_return;
12682 }
12683 }
12684 }
12685 }
12686
12687 /* Free symbol buffer if needed. */
12688 if (!info->reduce_memory_overheads)
12689 {
12690 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12691 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
12692 {
12693 free (elf_tdata (sub)->symbuf);
12694 elf_tdata (sub)->symbuf = NULL;
12695 }
12696 }
12697
12698 ret = true;
12699
12700 /* Output any global symbols that got converted to local in a
12701 version script or due to symbol visibility. We do this in a
12702 separate step since ELF requires all local symbols to appear
12703 prior to any global symbols. FIXME: We should only do this if
12704 some global symbols were, in fact, converted to become local.
12705 FIXME: Will this work correctly with the Irix 5 linker? */
12706 eoinfo.failed = false;
12707 eoinfo.flinfo = &flinfo;
12708 eoinfo.localsyms = true;
12709 eoinfo.file_sym_done = false;
12710 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12711 if (eoinfo.failed)
12712 {
12713 ret = false;
12714 goto return_local_hash_table;
12715 }
12716
12717 /* If backend needs to output some local symbols not present in the hash
12718 table, do it now. */
12719 if (bed->elf_backend_output_arch_local_syms
12720 && (info->strip != strip_all || emit_relocs))
12721 {
12722 if (! ((*bed->elf_backend_output_arch_local_syms)
12723 (abfd, info, &flinfo, elf_link_output_symstrtab)))
12724 {
12725 ret = false;
12726 goto return_local_hash_table;
12727 }
12728 }
12729
12730 /* That wrote out all the local symbols. Finish up the symbol table
12731 with the global symbols. Even if we want to strip everything we
12732 can, we still need to deal with those global symbols that got
12733 converted to local in a version script. */
12734
12735 /* The sh_info field records the index of the first non local symbol. */
12736 if (!symtab_hdr->sh_info)
12737 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12738
12739 if (dynamic
12740 && htab->dynsym != NULL
12741 && htab->dynsym->output_section != bfd_abs_section_ptr)
12742 {
12743 Elf_Internal_Sym sym;
12744 bfd_byte *dynsym = htab->dynsym->contents;
12745
12746 o = htab->dynsym->output_section;
12747 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
12748
12749 /* Write out the section symbols for the output sections. */
12750 if (bfd_link_pic (info)
12751 || htab->is_relocatable_executable)
12752 {
12753 asection *s;
12754
12755 sym.st_size = 0;
12756 sym.st_name = 0;
12757 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12758 sym.st_other = 0;
12759 sym.st_target_internal = 0;
12760
12761 for (s = abfd->sections; s != NULL; s = s->next)
12762 {
12763 int indx;
12764 bfd_byte *dest;
12765 long dynindx;
12766
12767 dynindx = elf_section_data (s)->dynindx;
12768 if (dynindx <= 0)
12769 continue;
12770 indx = elf_section_data (s)->this_idx;
12771 BFD_ASSERT (indx > 0);
12772 sym.st_shndx = indx;
12773 if (! check_dynsym (abfd, &sym))
12774 {
12775 ret = false;
12776 goto return_local_hash_table;
12777 }
12778 sym.st_value = s->vma;
12779 dest = dynsym + dynindx * bed->s->sizeof_sym;
12780
12781 /* Inform the linker of the addition of this symbol. */
12782
12783 if (info->callbacks->ctf_new_dynsym)
12784 info->callbacks->ctf_new_dynsym (dynindx, &sym);
12785
12786 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12787 }
12788 }
12789
12790 /* Write out the local dynsyms. */
12791 if (htab->dynlocal)
12792 {
12793 struct elf_link_local_dynamic_entry *e;
12794 for (e = htab->dynlocal; e ; e = e->next)
12795 {
12796 asection *s;
12797 bfd_byte *dest;
12798
12799 /* Copy the internal symbol and turn off visibility.
12800 Note that we saved a word of storage and overwrote
12801 the original st_name with the dynstr_index. */
12802 sym = e->isym;
12803 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
12804 sym.st_shndx = SHN_UNDEF;
12805
12806 s = bfd_section_from_elf_index (e->input_bfd,
12807 e->isym.st_shndx);
12808 if (s != NULL
12809 && s->output_section != NULL
12810 && elf_section_data (s->output_section) != NULL)
12811 {
12812 sym.st_shndx =
12813 elf_section_data (s->output_section)->this_idx;
12814 if (! check_dynsym (abfd, &sym))
12815 {
12816 ret = false;
12817 goto return_local_hash_table;
12818 }
12819 sym.st_value = (s->output_section->vma
12820 + s->output_offset
12821 + e->isym.st_value);
12822 }
12823
12824 /* Inform the linker of the addition of this symbol. */
12825
12826 if (info->callbacks->ctf_new_dynsym)
12827 info->callbacks->ctf_new_dynsym (e->dynindx, &sym);
12828
12829 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12830 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12831 }
12832 }
12833 }
12834
12835 /* We get the global symbols from the hash table. */
12836 eoinfo.failed = false;
12837 eoinfo.localsyms = false;
12838 eoinfo.flinfo = &flinfo;
12839 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12840 if (eoinfo.failed)
12841 {
12842 ret = false;
12843 goto return_local_hash_table;
12844 }
12845
12846 /* If backend needs to output some symbols not present in the hash
12847 table, do it now. */
12848 if (bed->elf_backend_output_arch_syms
12849 && (info->strip != strip_all || emit_relocs))
12850 {
12851 if (! ((*bed->elf_backend_output_arch_syms)
12852 (abfd, info, &flinfo, elf_link_output_symstrtab)))
12853 {
12854 ret = false;
12855 goto return_local_hash_table;
12856 }
12857 }
12858
12859 /* Finalize the .strtab section. */
12860 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12861
12862 /* Swap out the .strtab section. */
12863 if (!elf_link_swap_symbols_out (&flinfo))
12864 {
12865 ret = false;
12866 goto return_local_hash_table;
12867 }
12868
12869 /* Now we know the size of the symtab section. */
12870 if (bfd_get_symcount (abfd) > 0)
12871 {
12872 /* Finish up and write out the symbol string table (.strtab)
12873 section. */
12874 Elf_Internal_Shdr *symstrtab_hdr = NULL;
12875 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12876
12877 if (elf_symtab_shndx_list (abfd))
12878 {
12879 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
12880
12881 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12882 {
12883 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12884 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12885 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12886 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12887 symtab_shndx_hdr->sh_size = amt;
12888
12889 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12890 off, true);
12891
12892 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12893 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12894 {
12895 ret = false;
12896 goto return_local_hash_table;
12897 }
12898 }
12899 }
12900
12901 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12902 /* sh_name was set in prep_headers. */
12903 symstrtab_hdr->sh_type = SHT_STRTAB;
12904 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
12905 symstrtab_hdr->sh_addr = 0;
12906 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
12907 symstrtab_hdr->sh_entsize = 0;
12908 symstrtab_hdr->sh_link = 0;
12909 symstrtab_hdr->sh_info = 0;
12910 /* sh_offset is set just below. */
12911 symstrtab_hdr->sh_addralign = 1;
12912
12913 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12914 off, true);
12915 elf_next_file_pos (abfd) = off;
12916
12917 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
12918 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
12919 {
12920 ret = false;
12921 goto return_local_hash_table;
12922 }
12923 }
12924
12925 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12926 {
12927 _bfd_error_handler (_("%pB: failed to generate import library"),
12928 info->out_implib_bfd);
12929 ret = false;
12930 goto return_local_hash_table;
12931 }
12932
12933 /* Adjust the relocs to have the correct symbol indices. */
12934 for (o = abfd->sections; o != NULL; o = o->next)
12935 {
12936 struct bfd_elf_section_data *esdo = elf_section_data (o);
12937 bool sort;
12938
12939 if ((o->flags & SEC_RELOC) == 0)
12940 continue;
12941
12942 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
12943 if (esdo->rel.hdr != NULL
12944 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
12945 {
12946 ret = false;
12947 goto return_local_hash_table;
12948 }
12949 if (esdo->rela.hdr != NULL
12950 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
12951 {
12952 ret = false;
12953 goto return_local_hash_table;
12954 }
12955
12956 /* Set the reloc_count field to 0 to prevent write_relocs from
12957 trying to swap the relocs out itself. */
12958 o->reloc_count = 0;
12959 }
12960
12961 if (dynamic && info->combreloc && dynobj != NULL)
12962 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12963
12964 /* If we are linking against a dynamic object, or generating a
12965 shared library, finish up the dynamic linking information. */
12966 if (dynamic)
12967 {
12968 bfd_byte *dyncon, *dynconend;
12969
12970 /* Fix up .dynamic entries. */
12971 o = bfd_get_linker_section (dynobj, ".dynamic");
12972 BFD_ASSERT (o != NULL);
12973
12974 dyncon = o->contents;
12975 dynconend = PTR_ADD (o->contents, o->size);
12976 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12977 {
12978 Elf_Internal_Dyn dyn;
12979 const char *name;
12980 unsigned int type;
12981 bfd_size_type sh_size;
12982 bfd_vma sh_addr;
12983
12984 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12985
12986 switch (dyn.d_tag)
12987 {
12988 default:
12989 continue;
12990 case DT_NULL:
12991 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12992 {
12993 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12994 {
12995 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12996 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12997 default: continue;
12998 }
12999 dyn.d_un.d_val = relativecount;
13000 relativecount = 0;
13001 break;
13002 }
13003 continue;
13004
13005 case DT_INIT:
13006 name = info->init_function;
13007 goto get_sym;
13008 case DT_FINI:
13009 name = info->fini_function;
13010 get_sym:
13011 {
13012 struct elf_link_hash_entry *h;
13013
13014 h = elf_link_hash_lookup (htab, name, false, false, true);
13015 if (h != NULL
13016 && (h->root.type == bfd_link_hash_defined
13017 || h->root.type == bfd_link_hash_defweak))
13018 {
13019 dyn.d_un.d_ptr = h->root.u.def.value;
13020 o = h->root.u.def.section;
13021 if (o->output_section != NULL)
13022 dyn.d_un.d_ptr += (o->output_section->vma
13023 + o->output_offset);
13024 else
13025 {
13026 /* The symbol is imported from another shared
13027 library and does not apply to this one. */
13028 dyn.d_un.d_ptr = 0;
13029 }
13030 break;
13031 }
13032 }
13033 continue;
13034
13035 case DT_PREINIT_ARRAYSZ:
13036 name = ".preinit_array";
13037 goto get_out_size;
13038 case DT_INIT_ARRAYSZ:
13039 name = ".init_array";
13040 goto get_out_size;
13041 case DT_FINI_ARRAYSZ:
13042 name = ".fini_array";
13043 get_out_size:
13044 o = bfd_get_section_by_name (abfd, name);
13045 if (o == NULL)
13046 {
13047 _bfd_error_handler
13048 (_("could not find section %s"), name);
13049 goto error_return;
13050 }
13051 if (o->size == 0)
13052 _bfd_error_handler
13053 (_("warning: %s section has zero size"), name);
13054 dyn.d_un.d_val = o->size;
13055 break;
13056
13057 case DT_PREINIT_ARRAY:
13058 name = ".preinit_array";
13059 goto get_out_vma;
13060 case DT_INIT_ARRAY:
13061 name = ".init_array";
13062 goto get_out_vma;
13063 case DT_FINI_ARRAY:
13064 name = ".fini_array";
13065 get_out_vma:
13066 o = bfd_get_section_by_name (abfd, name);
13067 goto do_vma;
13068
13069 case DT_HASH:
13070 name = ".hash";
13071 goto get_vma;
13072 case DT_GNU_HASH:
13073 name = ".gnu.hash";
13074 goto get_vma;
13075 case DT_STRTAB:
13076 name = ".dynstr";
13077 goto get_vma;
13078 case DT_SYMTAB:
13079 name = ".dynsym";
13080 goto get_vma;
13081 case DT_VERDEF:
13082 name = ".gnu.version_d";
13083 goto get_vma;
13084 case DT_VERNEED:
13085 name = ".gnu.version_r";
13086 goto get_vma;
13087 case DT_VERSYM:
13088 name = ".gnu.version";
13089 get_vma:
13090 o = bfd_get_linker_section (dynobj, name);
13091 do_vma:
13092 if (o == NULL || bfd_is_abs_section (o->output_section))
13093 {
13094 _bfd_error_handler
13095 (_("could not find section %s"), name);
13096 goto error_return;
13097 }
13098 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
13099 {
13100 _bfd_error_handler
13101 (_("warning: section '%s' is being made into a note"), name);
13102 bfd_set_error (bfd_error_nonrepresentable_section);
13103 goto error_return;
13104 }
13105 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
13106 break;
13107
13108 case DT_REL:
13109 case DT_RELA:
13110 case DT_RELSZ:
13111 case DT_RELASZ:
13112 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
13113 type = SHT_REL;
13114 else
13115 type = SHT_RELA;
13116 sh_size = 0;
13117 sh_addr = 0;
13118 for (i = 1; i < elf_numsections (abfd); i++)
13119 {
13120 Elf_Internal_Shdr *hdr;
13121
13122 hdr = elf_elfsections (abfd)[i];
13123 if (hdr->sh_type == type
13124 && (hdr->sh_flags & SHF_ALLOC) != 0)
13125 {
13126 sh_size += hdr->sh_size;
13127 if (sh_addr == 0
13128 || sh_addr > hdr->sh_addr)
13129 sh_addr = hdr->sh_addr;
13130 }
13131 }
13132
13133 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
13134 {
13135 unsigned int opb = bfd_octets_per_byte (abfd, o);
13136
13137 /* Don't count procedure linkage table relocs in the
13138 overall reloc count. */
13139 sh_size -= htab->srelplt->size;
13140 if (sh_size == 0)
13141 /* If the size is zero, make the address zero too.
13142 This is to avoid a glibc bug. If the backend
13143 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
13144 zero, then we'll put DT_RELA at the end of
13145 DT_JMPREL. glibc will interpret the end of
13146 DT_RELA matching the end of DT_JMPREL as the
13147 case where DT_RELA includes DT_JMPREL, and for
13148 LD_BIND_NOW will decide that processing DT_RELA
13149 will process the PLT relocs too. Net result:
13150 No PLT relocs applied. */
13151 sh_addr = 0;
13152
13153 /* If .rela.plt is the first .rela section, exclude
13154 it from DT_RELA. */
13155 else if (sh_addr == (htab->srelplt->output_section->vma
13156 + htab->srelplt->output_offset) * opb)
13157 sh_addr += htab->srelplt->size;
13158 }
13159
13160 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
13161 dyn.d_un.d_val = sh_size;
13162 else
13163 dyn.d_un.d_ptr = sh_addr;
13164 break;
13165 }
13166 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13167 }
13168 }
13169
13170 /* If we have created any dynamic sections, then output them. */
13171 if (dynobj != NULL)
13172 {
13173 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
13174 goto error_return;
13175
13176 /* Check for DT_TEXTREL (late, in case the backend removes it). */
13177 if (bfd_link_textrel_check (info)
13178 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL
13179 && o->size != 0)
13180 {
13181 bfd_byte *dyncon, *dynconend;
13182
13183 dyncon = o->contents;
13184 dynconend = o->contents + o->size;
13185 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13186 {
13187 Elf_Internal_Dyn dyn;
13188
13189 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13190
13191 if (dyn.d_tag == DT_TEXTREL)
13192 {
13193 if (info->textrel_check == textrel_check_error)
13194 info->callbacks->einfo
13195 (_("%P%X: read-only segment has dynamic relocations\n"));
13196 else if (bfd_link_dll (info))
13197 info->callbacks->einfo
13198 (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
13199 else if (bfd_link_pde (info))
13200 info->callbacks->einfo
13201 (_("%P: warning: creating DT_TEXTREL in a PDE\n"));
13202 else
13203 info->callbacks->einfo
13204 (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
13205 break;
13206 }
13207 }
13208 }
13209
13210 for (o = dynobj->sections; o != NULL; o = o->next)
13211 {
13212 if ((o->flags & SEC_HAS_CONTENTS) == 0
13213 || o->size == 0
13214 || o->output_section == bfd_abs_section_ptr)
13215 continue;
13216 if ((o->flags & SEC_LINKER_CREATED) == 0)
13217 {
13218 /* At this point, we are only interested in sections
13219 created by _bfd_elf_link_create_dynamic_sections. */
13220 continue;
13221 }
13222 if (htab->stab_info.stabstr == o)
13223 continue;
13224 if (htab->eh_info.hdr_sec == o)
13225 continue;
13226 if (strcmp (o->name, ".dynstr") != 0)
13227 {
13228 bfd_size_type octets = ((file_ptr) o->output_offset
13229 * bfd_octets_per_byte (abfd, o));
13230 if (!bfd_set_section_contents (abfd, o->output_section,
13231 o->contents, octets, o->size))
13232 goto error_return;
13233 }
13234 else
13235 {
13236 /* The contents of the .dynstr section are actually in a
13237 stringtab. */
13238 file_ptr off;
13239
13240 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
13241 if (bfd_seek (abfd, off, SEEK_SET) != 0
13242 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
13243 goto error_return;
13244 }
13245 }
13246 }
13247
13248 if (!info->resolve_section_groups)
13249 {
13250 bool failed = false;
13251
13252 BFD_ASSERT (bfd_link_relocatable (info));
13253 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
13254 if (failed)
13255 goto error_return;
13256 }
13257
13258 /* If we have optimized stabs strings, output them. */
13259 if (htab->stab_info.stabstr != NULL)
13260 {
13261 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
13262 goto error_return;
13263 }
13264
13265 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
13266 goto error_return;
13267
13268 if (info->callbacks->emit_ctf)
13269 info->callbacks->emit_ctf ();
13270
13271 elf_final_link_free (abfd, &flinfo);
13272
13273 if (attr_section)
13274 {
13275 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
13276 if (contents == NULL)
13277 {
13278 /* Bail out and fail. */
13279 ret = false;
13280 goto return_local_hash_table;
13281 }
13282 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
13283 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
13284 free (contents);
13285 }
13286
13287 return_local_hash_table:
13288 if (info->unique_symbol)
13289 bfd_hash_table_free (&flinfo.local_hash_table);
13290 return ret;
13291
13292 error_return:
13293 elf_final_link_free (abfd, &flinfo);
13294 ret = false;
13295 goto return_local_hash_table;
13296 }
13297 \f
13298 /* Initialize COOKIE for input bfd ABFD. */
13299
13300 static bool
13301 init_reloc_cookie (struct elf_reloc_cookie *cookie,
13302 struct bfd_link_info *info, bfd *abfd)
13303 {
13304 Elf_Internal_Shdr *symtab_hdr;
13305 const struct elf_backend_data *bed;
13306
13307 bed = get_elf_backend_data (abfd);
13308 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13309
13310 cookie->abfd = abfd;
13311 cookie->sym_hashes = elf_sym_hashes (abfd);
13312 cookie->bad_symtab = elf_bad_symtab (abfd);
13313 if (cookie->bad_symtab)
13314 {
13315 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13316 cookie->extsymoff = 0;
13317 }
13318 else
13319 {
13320 cookie->locsymcount = symtab_hdr->sh_info;
13321 cookie->extsymoff = symtab_hdr->sh_info;
13322 }
13323
13324 if (bed->s->arch_size == 32)
13325 cookie->r_sym_shift = 8;
13326 else
13327 cookie->r_sym_shift = 32;
13328
13329 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
13330 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
13331 {
13332 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
13333 cookie->locsymcount, 0,
13334 NULL, NULL, NULL);
13335 if (cookie->locsyms == NULL)
13336 {
13337 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
13338 return false;
13339 }
13340 if (_bfd_link_keep_memory (info) )
13341 {
13342 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
13343 info->cache_size += (cookie->locsymcount
13344 * sizeof (Elf_External_Sym_Shndx));
13345 }
13346 }
13347 return true;
13348 }
13349
13350 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
13351
13352 static void
13353 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
13354 {
13355 Elf_Internal_Shdr *symtab_hdr;
13356
13357 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13358 if (symtab_hdr->contents != (unsigned char *) cookie->locsyms)
13359 free (cookie->locsyms);
13360 }
13361
13362 /* Initialize the relocation information in COOKIE for input section SEC
13363 of input bfd ABFD. */
13364
13365 static bool
13366 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13367 struct bfd_link_info *info, bfd *abfd,
13368 asection *sec)
13369 {
13370 if (sec->reloc_count == 0)
13371 {
13372 cookie->rels = NULL;
13373 cookie->relend = NULL;
13374 }
13375 else
13376 {
13377 cookie->rels = _bfd_elf_link_info_read_relocs (abfd, info, sec,
13378 NULL, NULL,
13379 _bfd_link_keep_memory (info));
13380 if (cookie->rels == NULL)
13381 return false;
13382 cookie->rel = cookie->rels;
13383 cookie->relend = cookie->rels + sec->reloc_count;
13384 }
13385 cookie->rel = cookie->rels;
13386 return true;
13387 }
13388
13389 /* Free the memory allocated by init_reloc_cookie_rels,
13390 if appropriate. */
13391
13392 static void
13393 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13394 asection *sec)
13395 {
13396 if (elf_section_data (sec)->relocs != cookie->rels)
13397 free (cookie->rels);
13398 }
13399
13400 /* Initialize the whole of COOKIE for input section SEC. */
13401
13402 static bool
13403 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13404 struct bfd_link_info *info,
13405 asection *sec)
13406 {
13407 if (!init_reloc_cookie (cookie, info, sec->owner))
13408 goto error1;
13409 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
13410 goto error2;
13411 return true;
13412
13413 error2:
13414 fini_reloc_cookie (cookie, sec->owner);
13415 error1:
13416 return false;
13417 }
13418
13419 /* Free the memory allocated by init_reloc_cookie_for_section,
13420 if appropriate. */
13421
13422 static void
13423 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13424 asection *sec)
13425 {
13426 fini_reloc_cookie_rels (cookie, sec);
13427 fini_reloc_cookie (cookie, sec->owner);
13428 }
13429 \f
13430 /* Garbage collect unused sections. */
13431
13432 /* Default gc_mark_hook. */
13433
13434 asection *
13435 _bfd_elf_gc_mark_hook (asection *sec,
13436 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13437 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13438 struct elf_link_hash_entry *h,
13439 Elf_Internal_Sym *sym)
13440 {
13441 if (h != NULL)
13442 {
13443 switch (h->root.type)
13444 {
13445 case bfd_link_hash_defined:
13446 case bfd_link_hash_defweak:
13447 return h->root.u.def.section;
13448
13449 case bfd_link_hash_common:
13450 return h->root.u.c.p->section;
13451
13452 default:
13453 break;
13454 }
13455 }
13456 else
13457 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
13458
13459 return NULL;
13460 }
13461
13462 /* Return the debug definition section. */
13463
13464 static asection *
13465 elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
13466 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13467 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13468 struct elf_link_hash_entry *h,
13469 Elf_Internal_Sym *sym)
13470 {
13471 if (h != NULL)
13472 {
13473 /* Return the global debug definition section. */
13474 if ((h->root.type == bfd_link_hash_defined
13475 || h->root.type == bfd_link_hash_defweak)
13476 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
13477 return h->root.u.def.section;
13478 }
13479 else
13480 {
13481 /* Return the local debug definition section. */
13482 asection *isec = bfd_section_from_elf_index (sec->owner,
13483 sym->st_shndx);
13484 if ((isec->flags & SEC_DEBUGGING) != 0)
13485 return isec;
13486 }
13487
13488 return NULL;
13489 }
13490
13491 /* COOKIE->rel describes a relocation against section SEC, which is
13492 a section we've decided to keep. Return the section that contains
13493 the relocation symbol, or NULL if no section contains it. */
13494
13495 asection *
13496 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
13497 elf_gc_mark_hook_fn gc_mark_hook,
13498 struct elf_reloc_cookie *cookie,
13499 bool *start_stop)
13500 {
13501 unsigned long r_symndx;
13502 struct elf_link_hash_entry *h, *hw;
13503
13504 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
13505 if (r_symndx == STN_UNDEF)
13506 return NULL;
13507
13508 if (r_symndx >= cookie->locsymcount
13509 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13510 {
13511 bool was_marked;
13512
13513 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
13514 if (h == NULL)
13515 {
13516 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
13517 sec->owner);
13518 return NULL;
13519 }
13520 while (h->root.type == bfd_link_hash_indirect
13521 || h->root.type == bfd_link_hash_warning)
13522 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13523
13524 was_marked = h->mark;
13525 h->mark = 1;
13526 /* Keep all aliases of the symbol too. If an object symbol
13527 needs to be copied into .dynbss then all of its aliases
13528 should be present as dynamic symbols, not just the one used
13529 on the copy relocation. */
13530 hw = h;
13531 while (hw->is_weakalias)
13532 {
13533 hw = hw->u.alias;
13534 hw->mark = 1;
13535 }
13536
13537 if (!was_marked && h->start_stop && !h->root.ldscript_def)
13538 {
13539 if (info->start_stop_gc)
13540 return NULL;
13541
13542 /* To work around a glibc bug, mark XXX input sections
13543 when there is a reference to __start_XXX or __stop_XXX
13544 symbols. */
13545 else if (start_stop != NULL)
13546 {
13547 asection *s = h->u2.start_stop_section;
13548 *start_stop = true;
13549 return s;
13550 }
13551 }
13552
13553 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
13554 }
13555
13556 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
13557 &cookie->locsyms[r_symndx]);
13558 }
13559
13560 /* COOKIE->rel describes a relocation against section SEC, which is
13561 a section we've decided to keep. Mark the section that contains
13562 the relocation symbol. */
13563
13564 bool
13565 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
13566 asection *sec,
13567 elf_gc_mark_hook_fn gc_mark_hook,
13568 struct elf_reloc_cookie *cookie)
13569 {
13570 asection *rsec;
13571 bool start_stop = false;
13572
13573 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
13574 while (rsec != NULL)
13575 {
13576 if (!rsec->gc_mark)
13577 {
13578 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
13579 || (rsec->owner->flags & DYNAMIC) != 0)
13580 rsec->gc_mark = 1;
13581 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
13582 return false;
13583 }
13584 if (!start_stop)
13585 break;
13586 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
13587 }
13588 return true;
13589 }
13590
13591 /* The mark phase of garbage collection. For a given section, mark
13592 it and any sections in this section's group, and all the sections
13593 which define symbols to which it refers. */
13594
13595 bool
13596 _bfd_elf_gc_mark (struct bfd_link_info *info,
13597 asection *sec,
13598 elf_gc_mark_hook_fn gc_mark_hook)
13599 {
13600 bool ret;
13601 asection *group_sec, *eh_frame;
13602
13603 sec->gc_mark = 1;
13604
13605 /* Mark all the sections in the group. */
13606 group_sec = elf_section_data (sec)->next_in_group;
13607 if (group_sec && !group_sec->gc_mark)
13608 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
13609 return false;
13610
13611 /* Look through the section relocs. */
13612 ret = true;
13613 eh_frame = elf_eh_frame_section (sec->owner);
13614 if ((sec->flags & SEC_RELOC) != 0
13615 && sec->reloc_count > 0
13616 && sec != eh_frame)
13617 {
13618 struct elf_reloc_cookie cookie;
13619
13620 if (!init_reloc_cookie_for_section (&cookie, info, sec))
13621 ret = false;
13622 else
13623 {
13624 for (; cookie.rel < cookie.relend; cookie.rel++)
13625 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
13626 {
13627 ret = false;
13628 break;
13629 }
13630 fini_reloc_cookie_for_section (&cookie, sec);
13631 }
13632 }
13633
13634 if (ret && eh_frame && elf_fde_list (sec))
13635 {
13636 struct elf_reloc_cookie cookie;
13637
13638 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
13639 ret = false;
13640 else
13641 {
13642 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
13643 gc_mark_hook, &cookie))
13644 ret = false;
13645 fini_reloc_cookie_for_section (&cookie, eh_frame);
13646 }
13647 }
13648
13649 eh_frame = elf_section_eh_frame_entry (sec);
13650 if (ret && eh_frame && !eh_frame->gc_mark)
13651 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
13652 ret = false;
13653
13654 return ret;
13655 }
13656
13657 /* Scan and mark sections in a special or debug section group. */
13658
13659 static void
13660 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
13661 {
13662 /* Point to first section of section group. */
13663 asection *ssec;
13664 /* Used to iterate the section group. */
13665 asection *msec;
13666
13667 bool is_special_grp = true;
13668 bool is_debug_grp = true;
13669
13670 /* First scan to see if group contains any section other than debug
13671 and special section. */
13672 ssec = msec = elf_next_in_group (grp);
13673 do
13674 {
13675 if ((msec->flags & SEC_DEBUGGING) == 0)
13676 is_debug_grp = false;
13677
13678 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
13679 is_special_grp = false;
13680
13681 msec = elf_next_in_group (msec);
13682 }
13683 while (msec != ssec);
13684
13685 /* If this is a pure debug section group or pure special section group,
13686 keep all sections in this group. */
13687 if (is_debug_grp || is_special_grp)
13688 {
13689 do
13690 {
13691 msec->gc_mark = 1;
13692 msec = elf_next_in_group (msec);
13693 }
13694 while (msec != ssec);
13695 }
13696 }
13697
13698 /* Keep debug and special sections. */
13699
13700 bool
13701 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
13702 elf_gc_mark_hook_fn mark_hook)
13703 {
13704 bfd *ibfd;
13705
13706 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13707 {
13708 asection *isec;
13709 bool some_kept;
13710 bool debug_frag_seen;
13711 bool has_kept_debug_info;
13712
13713 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13714 continue;
13715 isec = ibfd->sections;
13716 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13717 continue;
13718
13719 /* Ensure all linker created sections are kept,
13720 see if any other section is already marked,
13721 and note if we have any fragmented debug sections. */
13722 debug_frag_seen = some_kept = has_kept_debug_info = false;
13723 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13724 {
13725 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13726 isec->gc_mark = 1;
13727 else if (isec->gc_mark
13728 && (isec->flags & SEC_ALLOC) != 0
13729 && elf_section_type (isec) != SHT_NOTE)
13730 some_kept = true;
13731 else
13732 {
13733 /* Since all sections, except for backend specific ones,
13734 have been garbage collected, call mark_hook on this
13735 section if any of its linked-to sections is marked. */
13736 asection *linked_to_sec;
13737 for (linked_to_sec = elf_linked_to_section (isec);
13738 linked_to_sec != NULL && !linked_to_sec->linker_mark;
13739 linked_to_sec = elf_linked_to_section (linked_to_sec))
13740 {
13741 if (linked_to_sec->gc_mark)
13742 {
13743 if (!_bfd_elf_gc_mark (info, isec, mark_hook))
13744 return false;
13745 break;
13746 }
13747 linked_to_sec->linker_mark = 1;
13748 }
13749 for (linked_to_sec = elf_linked_to_section (isec);
13750 linked_to_sec != NULL && linked_to_sec->linker_mark;
13751 linked_to_sec = elf_linked_to_section (linked_to_sec))
13752 linked_to_sec->linker_mark = 0;
13753 }
13754
13755 if (!debug_frag_seen
13756 && (isec->flags & SEC_DEBUGGING)
13757 && startswith (isec->name, ".debug_line."))
13758 debug_frag_seen = true;
13759 else if (strcmp (bfd_section_name (isec),
13760 "__patchable_function_entries") == 0
13761 && elf_linked_to_section (isec) == NULL)
13762 info->callbacks->einfo (_("%F%P: %pB(%pA): error: "
13763 "need linked-to section "
13764 "for --gc-sections\n"),
13765 isec->owner, isec);
13766 }
13767
13768 /* If no non-note alloc section in this file will be kept, then
13769 we can toss out the debug and special sections. */
13770 if (!some_kept)
13771 continue;
13772
13773 /* Keep debug and special sections like .comment when they are
13774 not part of a group. Also keep section groups that contain
13775 just debug sections or special sections. NB: Sections with
13776 linked-to section has been handled above. */
13777 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13778 {
13779 if ((isec->flags & SEC_GROUP) != 0)
13780 _bfd_elf_gc_mark_debug_special_section_group (isec);
13781 else if (((isec->flags & SEC_DEBUGGING) != 0
13782 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
13783 && elf_next_in_group (isec) == NULL
13784 && elf_linked_to_section (isec) == NULL)
13785 isec->gc_mark = 1;
13786 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
13787 has_kept_debug_info = true;
13788 }
13789
13790 /* Look for CODE sections which are going to be discarded,
13791 and find and discard any fragmented debug sections which
13792 are associated with that code section. */
13793 if (debug_frag_seen)
13794 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13795 if ((isec->flags & SEC_CODE) != 0
13796 && isec->gc_mark == 0)
13797 {
13798 unsigned int ilen;
13799 asection *dsec;
13800
13801 ilen = strlen (isec->name);
13802
13803 /* Association is determined by the name of the debug
13804 section containing the name of the code section as
13805 a suffix. For example .debug_line.text.foo is a
13806 debug section associated with .text.foo. */
13807 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
13808 {
13809 unsigned int dlen;
13810
13811 if (dsec->gc_mark == 0
13812 || (dsec->flags & SEC_DEBUGGING) == 0)
13813 continue;
13814
13815 dlen = strlen (dsec->name);
13816
13817 if (dlen > ilen
13818 && strncmp (dsec->name + (dlen - ilen),
13819 isec->name, ilen) == 0)
13820 dsec->gc_mark = 0;
13821 }
13822 }
13823
13824 /* Mark debug sections referenced by kept debug sections. */
13825 if (has_kept_debug_info)
13826 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13827 if (isec->gc_mark
13828 && (isec->flags & SEC_DEBUGGING) != 0)
13829 if (!_bfd_elf_gc_mark (info, isec,
13830 elf_gc_mark_debug_section))
13831 return false;
13832 }
13833 return true;
13834 }
13835
13836 static bool
13837 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
13838 {
13839 bfd *sub;
13840 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13841
13842 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13843 {
13844 asection *o;
13845
13846 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13847 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
13848 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13849 continue;
13850 o = sub->sections;
13851 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13852 continue;
13853
13854 for (o = sub->sections; o != NULL; o = o->next)
13855 {
13856 /* When any section in a section group is kept, we keep all
13857 sections in the section group. If the first member of
13858 the section group is excluded, we will also exclude the
13859 group section. */
13860 if (o->flags & SEC_GROUP)
13861 {
13862 asection *first = elf_next_in_group (o);
13863 o->gc_mark = first->gc_mark;
13864 }
13865
13866 if (o->gc_mark)
13867 continue;
13868
13869 /* Skip sweeping sections already excluded. */
13870 if (o->flags & SEC_EXCLUDE)
13871 continue;
13872
13873 /* Since this is early in the link process, it is simple
13874 to remove a section from the output. */
13875 o->flags |= SEC_EXCLUDE;
13876
13877 if (info->print_gc_sections && o->size != 0)
13878 /* xgettext:c-format */
13879 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
13880 o, sub);
13881 }
13882 }
13883
13884 return true;
13885 }
13886
13887 /* Propagate collected vtable information. This is called through
13888 elf_link_hash_traverse. */
13889
13890 static bool
13891 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13892 {
13893 /* Those that are not vtables. */
13894 if (h->start_stop
13895 || h->u2.vtable == NULL
13896 || h->u2.vtable->parent == NULL)
13897 return true;
13898
13899 /* Those vtables that do not have parents, we cannot merge. */
13900 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
13901 return true;
13902
13903 /* If we've already been done, exit. */
13904 if (h->u2.vtable->used && h->u2.vtable->used[-1])
13905 return true;
13906
13907 /* Make sure the parent's table is up to date. */
13908 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
13909
13910 if (h->u2.vtable->used == NULL)
13911 {
13912 /* None of this table's entries were referenced. Re-use the
13913 parent's table. */
13914 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13915 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
13916 }
13917 else
13918 {
13919 size_t n;
13920 bool *cu, *pu;
13921
13922 /* Or the parent's entries into ours. */
13923 cu = h->u2.vtable->used;
13924 cu[-1] = true;
13925 pu = h->u2.vtable->parent->u2.vtable->used;
13926 if (pu != NULL)
13927 {
13928 const struct elf_backend_data *bed;
13929 unsigned int log_file_align;
13930
13931 bed = get_elf_backend_data (h->root.u.def.section->owner);
13932 log_file_align = bed->s->log_file_align;
13933 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
13934 while (n--)
13935 {
13936 if (*pu)
13937 *cu = true;
13938 pu++;
13939 cu++;
13940 }
13941 }
13942 }
13943
13944 return true;
13945 }
13946
13947 struct link_info_ok
13948 {
13949 struct bfd_link_info *info;
13950 bool ok;
13951 };
13952
13953 static bool
13954 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h,
13955 void *ptr)
13956 {
13957 asection *sec;
13958 bfd_vma hstart, hend;
13959 Elf_Internal_Rela *relstart, *relend, *rel;
13960 const struct elf_backend_data *bed;
13961 unsigned int log_file_align;
13962 struct link_info_ok *info = (struct link_info_ok *) ptr;
13963
13964 /* Take care of both those symbols that do not describe vtables as
13965 well as those that are not loaded. */
13966 if (h->start_stop
13967 || h->u2.vtable == NULL
13968 || h->u2.vtable->parent == NULL)
13969 return true;
13970
13971 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13972 || h->root.type == bfd_link_hash_defweak);
13973
13974 sec = h->root.u.def.section;
13975 hstart = h->root.u.def.value;
13976 hend = hstart + h->size;
13977
13978 relstart = _bfd_elf_link_info_read_relocs (sec->owner, info->info,
13979 sec, NULL, NULL, true);
13980 if (!relstart)
13981 return info->ok = false;
13982 bed = get_elf_backend_data (sec->owner);
13983 log_file_align = bed->s->log_file_align;
13984
13985 relend = relstart + sec->reloc_count;
13986
13987 for (rel = relstart; rel < relend; ++rel)
13988 if (rel->r_offset >= hstart && rel->r_offset < hend)
13989 {
13990 /* If the entry is in use, do nothing. */
13991 if (h->u2.vtable->used
13992 && (rel->r_offset - hstart) < h->u2.vtable->size)
13993 {
13994 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
13995 if (h->u2.vtable->used[entry])
13996 continue;
13997 }
13998 /* Otherwise, kill it. */
13999 rel->r_offset = rel->r_info = rel->r_addend = 0;
14000 }
14001
14002 return true;
14003 }
14004
14005 /* Mark sections containing dynamically referenced symbols. When
14006 building shared libraries, we must assume that any visible symbol is
14007 referenced. */
14008
14009 bool
14010 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
14011 {
14012 struct bfd_link_info *info = (struct bfd_link_info *) inf;
14013 struct bfd_elf_dynamic_list *d = info->dynamic_list;
14014
14015 if ((h->root.type == bfd_link_hash_defined
14016 || h->root.type == bfd_link_hash_defweak)
14017 && (!h->start_stop
14018 || h->root.ldscript_def
14019 || !info->start_stop_gc)
14020 && ((h->ref_dynamic && !h->forced_local)
14021 || ((h->def_regular || ELF_COMMON_DEF_P (h))
14022 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
14023 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
14024 && (!bfd_link_executable (info)
14025 || info->gc_keep_exported
14026 || info->export_dynamic
14027 || (h->dynamic
14028 && d != NULL
14029 && (*d->match) (&d->head, NULL, h->root.root.string)))
14030 && (h->versioned >= versioned
14031 || !bfd_hide_sym_by_version (info->version_info,
14032 h->root.root.string)))))
14033 h->root.u.def.section->flags |= SEC_KEEP;
14034
14035 return true;
14036 }
14037
14038 /* Keep all sections containing symbols undefined on the command-line,
14039 and the section containing the entry symbol. */
14040
14041 void
14042 _bfd_elf_gc_keep (struct bfd_link_info *info)
14043 {
14044 struct bfd_sym_chain *sym;
14045
14046 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
14047 {
14048 struct elf_link_hash_entry *h;
14049
14050 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
14051 false, false, false);
14052
14053 if (h != NULL
14054 && (h->root.type == bfd_link_hash_defined
14055 || h->root.type == bfd_link_hash_defweak)
14056 && !bfd_is_const_section (h->root.u.def.section))
14057 h->root.u.def.section->flags |= SEC_KEEP;
14058 }
14059 }
14060
14061 bool
14062 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
14063 struct bfd_link_info *info)
14064 {
14065 bfd *ibfd = info->input_bfds;
14066
14067 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14068 {
14069 asection *sec;
14070 struct elf_reloc_cookie cookie;
14071
14072 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
14073 continue;
14074 sec = ibfd->sections;
14075 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14076 continue;
14077
14078 if (!init_reloc_cookie (&cookie, info, ibfd))
14079 return false;
14080
14081 for (sec = ibfd->sections; sec; sec = sec->next)
14082 {
14083 if (startswith (bfd_section_name (sec), ".eh_frame_entry")
14084 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
14085 {
14086 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
14087 fini_reloc_cookie_rels (&cookie, sec);
14088 }
14089 }
14090 }
14091 return true;
14092 }
14093
14094 /* Do mark and sweep of unused sections. */
14095
14096 bool
14097 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
14098 {
14099 bool ok = true;
14100 bfd *sub;
14101 elf_gc_mark_hook_fn gc_mark_hook;
14102 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14103 struct elf_link_hash_table *htab;
14104 struct link_info_ok info_ok;
14105
14106 if (!bed->can_gc_sections
14107 || !is_elf_hash_table (info->hash))
14108 {
14109 _bfd_error_handler(_("warning: gc-sections option ignored"));
14110 return true;
14111 }
14112
14113 bed->gc_keep (info);
14114 htab = elf_hash_table (info);
14115
14116 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
14117 at the .eh_frame section if we can mark the FDEs individually. */
14118 for (sub = info->input_bfds;
14119 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
14120 sub = sub->link.next)
14121 {
14122 asection *sec;
14123 struct elf_reloc_cookie cookie;
14124
14125 sec = sub->sections;
14126 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14127 continue;
14128 sec = bfd_get_section_by_name (sub, ".eh_frame");
14129 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
14130 {
14131 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
14132 if (elf_section_data (sec)->sec_info
14133 && (sec->flags & SEC_LINKER_CREATED) == 0)
14134 elf_eh_frame_section (sub) = sec;
14135 fini_reloc_cookie_for_section (&cookie, sec);
14136 sec = bfd_get_next_section_by_name (NULL, sec);
14137 }
14138 }
14139
14140 /* Apply transitive closure to the vtable entry usage info. */
14141 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
14142 if (!ok)
14143 return false;
14144
14145 /* Kill the vtable relocations that were not used. */
14146 info_ok.info = info;
14147 info_ok.ok = true;
14148 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &info_ok);
14149 if (!info_ok.ok)
14150 return false;
14151
14152 /* Mark dynamically referenced symbols. */
14153 if (htab->dynamic_sections_created || info->gc_keep_exported)
14154 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
14155
14156 /* Grovel through relocs to find out who stays ... */
14157 gc_mark_hook = bed->gc_mark_hook;
14158 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
14159 {
14160 asection *o;
14161
14162 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
14163 || elf_object_id (sub) != elf_hash_table_id (htab)
14164 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
14165 continue;
14166
14167 o = sub->sections;
14168 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14169 continue;
14170
14171 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
14172 Also treat note sections as a root, if the section is not part
14173 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
14174 well as FINI_ARRAY sections for ld -r. */
14175 for (o = sub->sections; o != NULL; o = o->next)
14176 if (!o->gc_mark
14177 && (o->flags & SEC_EXCLUDE) == 0
14178 && ((o->flags & SEC_KEEP) != 0
14179 || (bfd_link_relocatable (info)
14180 && ((elf_section_data (o)->this_hdr.sh_type
14181 == SHT_PREINIT_ARRAY)
14182 || (elf_section_data (o)->this_hdr.sh_type
14183 == SHT_INIT_ARRAY)
14184 || (elf_section_data (o)->this_hdr.sh_type
14185 == SHT_FINI_ARRAY)))
14186 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
14187 && elf_next_in_group (o) == NULL
14188 && elf_linked_to_section (o) == NULL)
14189 || ((elf_tdata (sub)->has_gnu_osabi & elf_gnu_osabi_retain)
14190 && (elf_section_flags (o) & SHF_GNU_RETAIN))))
14191 {
14192 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
14193 return false;
14194 }
14195 }
14196
14197 /* Allow the backend to mark additional target specific sections. */
14198 bed->gc_mark_extra_sections (info, gc_mark_hook);
14199
14200 /* ... and mark SEC_EXCLUDE for those that go. */
14201 return elf_gc_sweep (abfd, info);
14202 }
14203 \f
14204 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
14205
14206 bool
14207 bfd_elf_gc_record_vtinherit (bfd *abfd,
14208 asection *sec,
14209 struct elf_link_hash_entry *h,
14210 bfd_vma offset)
14211 {
14212 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
14213 struct elf_link_hash_entry **search, *child;
14214 size_t extsymcount;
14215 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14216
14217 /* The sh_info field of the symtab header tells us where the
14218 external symbols start. We don't care about the local symbols at
14219 this point. */
14220 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
14221 if (!elf_bad_symtab (abfd))
14222 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
14223
14224 sym_hashes = elf_sym_hashes (abfd);
14225 sym_hashes_end = PTR_ADD (sym_hashes, extsymcount);
14226
14227 /* Hunt down the child symbol, which is in this section at the same
14228 offset as the relocation. */
14229 for (search = sym_hashes; search != sym_hashes_end; ++search)
14230 {
14231 if ((child = *search) != NULL
14232 && (child->root.type == bfd_link_hash_defined
14233 || child->root.type == bfd_link_hash_defweak)
14234 && child->root.u.def.section == sec
14235 && child->root.u.def.value == offset)
14236 goto win;
14237 }
14238
14239 /* xgettext:c-format */
14240 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
14241 abfd, sec, (uint64_t) offset);
14242 bfd_set_error (bfd_error_invalid_operation);
14243 return false;
14244
14245 win:
14246 if (!child->u2.vtable)
14247 {
14248 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
14249 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
14250 if (!child->u2.vtable)
14251 return false;
14252 }
14253 if (!h)
14254 {
14255 /* This *should* only be the absolute section. It could potentially
14256 be that someone has defined a non-global vtable though, which
14257 would be bad. It isn't worth paging in the local symbols to be
14258 sure though; that case should simply be handled by the assembler. */
14259
14260 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
14261 }
14262 else
14263 child->u2.vtable->parent = h;
14264
14265 return true;
14266 }
14267
14268 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
14269
14270 bool
14271 bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec,
14272 struct elf_link_hash_entry *h,
14273 bfd_vma addend)
14274 {
14275 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14276 unsigned int log_file_align = bed->s->log_file_align;
14277
14278 if (!h)
14279 {
14280 /* xgettext:c-format */
14281 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
14282 abfd, sec);
14283 bfd_set_error (bfd_error_bad_value);
14284 return false;
14285 }
14286
14287 if (!h->u2.vtable)
14288 {
14289 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
14290 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
14291 if (!h->u2.vtable)
14292 return false;
14293 }
14294
14295 if (addend >= h->u2.vtable->size)
14296 {
14297 size_t size, bytes, file_align;
14298 bool *ptr = h->u2.vtable->used;
14299
14300 /* While the symbol is undefined, we have to be prepared to handle
14301 a zero size. */
14302 file_align = 1 << log_file_align;
14303 if (h->root.type == bfd_link_hash_undefined)
14304 size = addend + file_align;
14305 else
14306 {
14307 size = h->size;
14308 if (addend >= size)
14309 {
14310 /* Oops! We've got a reference past the defined end of
14311 the table. This is probably a bug -- shall we warn? */
14312 size = addend + file_align;
14313 }
14314 }
14315 size = (size + file_align - 1) & -file_align;
14316
14317 /* Allocate one extra entry for use as a "done" flag for the
14318 consolidation pass. */
14319 bytes = ((size >> log_file_align) + 1) * sizeof (bool);
14320
14321 if (ptr)
14322 {
14323 ptr = (bool *) bfd_realloc (ptr - 1, bytes);
14324
14325 if (ptr != NULL)
14326 {
14327 size_t oldbytes;
14328
14329 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
14330 * sizeof (bool));
14331 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
14332 }
14333 }
14334 else
14335 ptr = (bool *) bfd_zmalloc (bytes);
14336
14337 if (ptr == NULL)
14338 return false;
14339
14340 /* And arrange for that done flag to be at index -1. */
14341 h->u2.vtable->used = ptr + 1;
14342 h->u2.vtable->size = size;
14343 }
14344
14345 h->u2.vtable->used[addend >> log_file_align] = true;
14346
14347 return true;
14348 }
14349
14350 /* Map an ELF section header flag to its corresponding string. */
14351 typedef struct
14352 {
14353 char *flag_name;
14354 flagword flag_value;
14355 } elf_flags_to_name_table;
14356
14357 static const elf_flags_to_name_table elf_flags_to_names [] =
14358 {
14359 { "SHF_WRITE", SHF_WRITE },
14360 { "SHF_ALLOC", SHF_ALLOC },
14361 { "SHF_EXECINSTR", SHF_EXECINSTR },
14362 { "SHF_MERGE", SHF_MERGE },
14363 { "SHF_STRINGS", SHF_STRINGS },
14364 { "SHF_INFO_LINK", SHF_INFO_LINK},
14365 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
14366 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
14367 { "SHF_GROUP", SHF_GROUP },
14368 { "SHF_TLS", SHF_TLS },
14369 { "SHF_MASKOS", SHF_MASKOS },
14370 { "SHF_EXCLUDE", SHF_EXCLUDE },
14371 };
14372
14373 /* Returns TRUE if the section is to be included, otherwise FALSE. */
14374 bool
14375 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
14376 struct flag_info *flaginfo,
14377 asection *section)
14378 {
14379 const bfd_vma sh_flags = elf_section_flags (section);
14380
14381 if (!flaginfo->flags_initialized)
14382 {
14383 bfd *obfd = info->output_bfd;
14384 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14385 struct flag_info_list *tf = flaginfo->flag_list;
14386 int with_hex = 0;
14387 int without_hex = 0;
14388
14389 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
14390 {
14391 unsigned i;
14392 flagword (*lookup) (char *);
14393
14394 lookup = bed->elf_backend_lookup_section_flags_hook;
14395 if (lookup != NULL)
14396 {
14397 flagword hexval = (*lookup) ((char *) tf->name);
14398
14399 if (hexval != 0)
14400 {
14401 if (tf->with == with_flags)
14402 with_hex |= hexval;
14403 else if (tf->with == without_flags)
14404 without_hex |= hexval;
14405 tf->valid = true;
14406 continue;
14407 }
14408 }
14409 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
14410 {
14411 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
14412 {
14413 if (tf->with == with_flags)
14414 with_hex |= elf_flags_to_names[i].flag_value;
14415 else if (tf->with == without_flags)
14416 without_hex |= elf_flags_to_names[i].flag_value;
14417 tf->valid = true;
14418 break;
14419 }
14420 }
14421 if (!tf->valid)
14422 {
14423 info->callbacks->einfo
14424 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
14425 return false;
14426 }
14427 }
14428 flaginfo->flags_initialized = true;
14429 flaginfo->only_with_flags |= with_hex;
14430 flaginfo->not_with_flags |= without_hex;
14431 }
14432
14433 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
14434 return false;
14435
14436 if ((flaginfo->not_with_flags & sh_flags) != 0)
14437 return false;
14438
14439 return true;
14440 }
14441
14442 struct alloc_got_off_arg {
14443 bfd_vma gotoff;
14444 struct bfd_link_info *info;
14445 };
14446
14447 /* We need a special top-level link routine to convert got reference counts
14448 to real got offsets. */
14449
14450 static bool
14451 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
14452 {
14453 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
14454 bfd *obfd = gofarg->info->output_bfd;
14455 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14456
14457 if (h->got.refcount > 0)
14458 {
14459 h->got.offset = gofarg->gotoff;
14460 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
14461 }
14462 else
14463 h->got.offset = (bfd_vma) -1;
14464
14465 return true;
14466 }
14467
14468 /* And an accompanying bit to work out final got entry offsets once
14469 we're done. Should be called from final_link. */
14470
14471 bool
14472 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
14473 struct bfd_link_info *info)
14474 {
14475 bfd *i;
14476 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14477 bfd_vma gotoff;
14478 struct alloc_got_off_arg gofarg;
14479
14480 BFD_ASSERT (abfd == info->output_bfd);
14481
14482 if (! is_elf_hash_table (info->hash))
14483 return false;
14484
14485 /* The GOT offset is relative to the .got section, but the GOT header is
14486 put into the .got.plt section, if the backend uses it. */
14487 if (bed->want_got_plt)
14488 gotoff = 0;
14489 else
14490 gotoff = bed->got_header_size;
14491
14492 /* Do the local .got entries first. */
14493 for (i = info->input_bfds; i; i = i->link.next)
14494 {
14495 bfd_signed_vma *local_got;
14496 size_t j, locsymcount;
14497 Elf_Internal_Shdr *symtab_hdr;
14498
14499 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
14500 continue;
14501
14502 local_got = elf_local_got_refcounts (i);
14503 if (!local_got)
14504 continue;
14505
14506 symtab_hdr = &elf_tdata (i)->symtab_hdr;
14507 if (elf_bad_symtab (i))
14508 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
14509 else
14510 locsymcount = symtab_hdr->sh_info;
14511
14512 for (j = 0; j < locsymcount; ++j)
14513 {
14514 if (local_got[j] > 0)
14515 {
14516 local_got[j] = gotoff;
14517 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
14518 }
14519 else
14520 local_got[j] = (bfd_vma) -1;
14521 }
14522 }
14523
14524 /* Then the global .got entries. .plt refcounts are handled by
14525 adjust_dynamic_symbol */
14526 gofarg.gotoff = gotoff;
14527 gofarg.info = info;
14528 elf_link_hash_traverse (elf_hash_table (info),
14529 elf_gc_allocate_got_offsets,
14530 &gofarg);
14531 return true;
14532 }
14533
14534 /* Many folk need no more in the way of final link than this, once
14535 got entry reference counting is enabled. */
14536
14537 bool
14538 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
14539 {
14540 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
14541 return false;
14542
14543 /* Invoke the regular ELF backend linker to do all the work. */
14544 return bfd_elf_final_link (abfd, info);
14545 }
14546
14547 bool
14548 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
14549 {
14550 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
14551
14552 if (rcookie->bad_symtab)
14553 rcookie->rel = rcookie->rels;
14554
14555 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
14556 {
14557 unsigned long r_symndx;
14558
14559 if (! rcookie->bad_symtab)
14560 if (rcookie->rel->r_offset > offset)
14561 return false;
14562 if (rcookie->rel->r_offset != offset)
14563 continue;
14564
14565 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
14566 if (r_symndx == STN_UNDEF)
14567 return true;
14568
14569 if (r_symndx >= rcookie->locsymcount
14570 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
14571 {
14572 struct elf_link_hash_entry *h;
14573
14574 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
14575
14576 while (h->root.type == bfd_link_hash_indirect
14577 || h->root.type == bfd_link_hash_warning)
14578 h = (struct elf_link_hash_entry *) h->root.u.i.link;
14579
14580 if ((h->root.type == bfd_link_hash_defined
14581 || h->root.type == bfd_link_hash_defweak)
14582 && (h->root.u.def.section->owner != rcookie->abfd
14583 || h->root.u.def.section->kept_section != NULL
14584 || discarded_section (h->root.u.def.section)))
14585 return true;
14586 }
14587 else
14588 {
14589 /* It's not a relocation against a global symbol,
14590 but it could be a relocation against a local
14591 symbol for a discarded section. */
14592 asection *isec;
14593 Elf_Internal_Sym *isym;
14594
14595 /* Need to: get the symbol; get the section. */
14596 isym = &rcookie->locsyms[r_symndx];
14597 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
14598 if (isec != NULL
14599 && (isec->kept_section != NULL
14600 || discarded_section (isec)))
14601 return true;
14602 }
14603 return false;
14604 }
14605 return false;
14606 }
14607
14608 /* Discard unneeded references to discarded sections.
14609 Returns -1 on error, 1 if any section's size was changed, 0 if
14610 nothing changed. This function assumes that the relocations are in
14611 sorted order, which is true for all known assemblers. */
14612
14613 int
14614 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
14615 {
14616 struct elf_reloc_cookie cookie;
14617 asection *o;
14618 bfd *abfd;
14619 int changed = 0;
14620
14621 if (info->traditional_format
14622 || !is_elf_hash_table (info->hash))
14623 return 0;
14624
14625 o = bfd_get_section_by_name (output_bfd, ".stab");
14626 if (o != NULL)
14627 {
14628 asection *i;
14629
14630 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14631 {
14632 if (i->size == 0
14633 || i->reloc_count == 0
14634 || i->sec_info_type != SEC_INFO_TYPE_STABS)
14635 continue;
14636
14637 abfd = i->owner;
14638 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14639 continue;
14640
14641 if (!init_reloc_cookie_for_section (&cookie, info, i))
14642 return -1;
14643
14644 if (_bfd_discard_section_stabs (abfd, i,
14645 elf_section_data (i)->sec_info,
14646 bfd_elf_reloc_symbol_deleted_p,
14647 &cookie))
14648 changed = 1;
14649
14650 fini_reloc_cookie_for_section (&cookie, i);
14651 }
14652 }
14653
14654 o = NULL;
14655 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
14656 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
14657 if (o != NULL)
14658 {
14659 asection *i;
14660 int eh_changed = 0;
14661 unsigned int eh_alignment; /* Octets. */
14662
14663 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14664 {
14665 if (i->size == 0)
14666 continue;
14667
14668 abfd = i->owner;
14669 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14670 continue;
14671
14672 if (!init_reloc_cookie_for_section (&cookie, info, i))
14673 return -1;
14674
14675 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
14676 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
14677 bfd_elf_reloc_symbol_deleted_p,
14678 &cookie))
14679 {
14680 eh_changed = 1;
14681 if (i->size != i->rawsize)
14682 changed = 1;
14683 }
14684
14685 fini_reloc_cookie_for_section (&cookie, i);
14686 }
14687
14688 eh_alignment = ((1 << o->alignment_power)
14689 * bfd_octets_per_byte (output_bfd, o));
14690 /* Skip over zero terminator, and prevent empty sections from
14691 adding alignment padding at the end. */
14692 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
14693 if (i->size == 0)
14694 i->flags |= SEC_EXCLUDE;
14695 else if (i->size > 4)
14696 break;
14697 /* The last non-empty eh_frame section doesn't need padding. */
14698 if (i != NULL)
14699 i = i->map_tail.s;
14700 /* Any prior sections must pad the last FDE out to the output
14701 section alignment. Otherwise we might have zero padding
14702 between sections, which would be seen as a terminator. */
14703 for (; i != NULL; i = i->map_tail.s)
14704 if (i->size == 4)
14705 /* All but the last zero terminator should have been removed. */
14706 BFD_FAIL ();
14707 else
14708 {
14709 bfd_size_type size
14710 = (i->size + eh_alignment - 1) & -eh_alignment;
14711 if (i->size != size)
14712 {
14713 i->size = size;
14714 changed = 1;
14715 eh_changed = 1;
14716 }
14717 }
14718 if (eh_changed)
14719 elf_link_hash_traverse (elf_hash_table (info),
14720 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
14721 }
14722
14723 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
14724 {
14725 const struct elf_backend_data *bed;
14726 asection *s;
14727
14728 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14729 continue;
14730 s = abfd->sections;
14731 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14732 continue;
14733
14734 bed = get_elf_backend_data (abfd);
14735
14736 if (bed->elf_backend_discard_info != NULL)
14737 {
14738 if (!init_reloc_cookie (&cookie, info, abfd))
14739 return -1;
14740
14741 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
14742 changed = 1;
14743
14744 fini_reloc_cookie (&cookie, abfd);
14745 }
14746 }
14747
14748 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
14749 _bfd_elf_end_eh_frame_parsing (info);
14750
14751 if (info->eh_frame_hdr_type
14752 && !bfd_link_relocatable (info)
14753 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
14754 changed = 1;
14755
14756 return changed;
14757 }
14758
14759 bool
14760 _bfd_elf_section_already_linked (bfd *abfd,
14761 asection *sec,
14762 struct bfd_link_info *info)
14763 {
14764 flagword flags;
14765 const char *name, *key;
14766 struct bfd_section_already_linked *l;
14767 struct bfd_section_already_linked_hash_entry *already_linked_list;
14768
14769 if (sec->output_section == bfd_abs_section_ptr)
14770 return false;
14771
14772 flags = sec->flags;
14773
14774 /* Return if it isn't a linkonce section. A comdat group section
14775 also has SEC_LINK_ONCE set. */
14776 if ((flags & SEC_LINK_ONCE) == 0)
14777 return false;
14778
14779 /* Don't put group member sections on our list of already linked
14780 sections. They are handled as a group via their group section. */
14781 if (elf_sec_group (sec) != NULL)
14782 return false;
14783
14784 /* For a SHT_GROUP section, use the group signature as the key. */
14785 name = sec->name;
14786 if ((flags & SEC_GROUP) != 0
14787 && elf_next_in_group (sec) != NULL
14788 && elf_group_name (elf_next_in_group (sec)) != NULL)
14789 key = elf_group_name (elf_next_in_group (sec));
14790 else
14791 {
14792 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
14793 if (startswith (name, ".gnu.linkonce.")
14794 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
14795 key++;
14796 else
14797 /* Must be a user linkonce section that doesn't follow gcc's
14798 naming convention. In this case we won't be matching
14799 single member groups. */
14800 key = name;
14801 }
14802
14803 already_linked_list = bfd_section_already_linked_table_lookup (key);
14804
14805 for (l = already_linked_list->entry; l != NULL; l = l->next)
14806 {
14807 /* We may have 2 different types of sections on the list: group
14808 sections with a signature of <key> (<key> is some string),
14809 and linkonce sections named .gnu.linkonce.<type>.<key>.
14810 Match like sections. LTO plugin sections are an exception.
14811 They are always named .gnu.linkonce.t.<key> and match either
14812 type of section. */
14813 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
14814 && ((flags & SEC_GROUP) != 0
14815 || strcmp (name, l->sec->name) == 0))
14816 || (l->sec->owner->flags & BFD_PLUGIN) != 0
14817 || (sec->owner->flags & BFD_PLUGIN) != 0)
14818 {
14819 /* The section has already been linked. See if we should
14820 issue a warning. */
14821 if (!_bfd_handle_already_linked (sec, l, info))
14822 return false;
14823
14824 if (flags & SEC_GROUP)
14825 {
14826 asection *first = elf_next_in_group (sec);
14827 asection *s = first;
14828
14829 while (s != NULL)
14830 {
14831 s->output_section = bfd_abs_section_ptr;
14832 /* Record which group discards it. */
14833 s->kept_section = l->sec;
14834 s = elf_next_in_group (s);
14835 /* These lists are circular. */
14836 if (s == first)
14837 break;
14838 }
14839 }
14840
14841 return true;
14842 }
14843 }
14844
14845 /* A single member comdat group section may be discarded by a
14846 linkonce section and vice versa. */
14847 if ((flags & SEC_GROUP) != 0)
14848 {
14849 asection *first = elf_next_in_group (sec);
14850
14851 if (first != NULL && elf_next_in_group (first) == first)
14852 /* Check this single member group against linkonce sections. */
14853 for (l = already_linked_list->entry; l != NULL; l = l->next)
14854 if ((l->sec->flags & SEC_GROUP) == 0
14855 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14856 {
14857 first->output_section = bfd_abs_section_ptr;
14858 first->kept_section = l->sec;
14859 sec->output_section = bfd_abs_section_ptr;
14860 break;
14861 }
14862 }
14863 else
14864 /* Check this linkonce section against single member groups. */
14865 for (l = already_linked_list->entry; l != NULL; l = l->next)
14866 if (l->sec->flags & SEC_GROUP)
14867 {
14868 asection *first = elf_next_in_group (l->sec);
14869
14870 if (first != NULL
14871 && elf_next_in_group (first) == first
14872 && bfd_elf_match_symbols_in_sections (first, sec, info))
14873 {
14874 sec->output_section = bfd_abs_section_ptr;
14875 sec->kept_section = first;
14876 break;
14877 }
14878 }
14879
14880 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14881 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14882 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14883 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14884 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14885 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14886 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14887 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14888 The reverse order cannot happen as there is never a bfd with only the
14889 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14890 matter as here were are looking only for cross-bfd sections. */
14891
14892 if ((flags & SEC_GROUP) == 0 && startswith (name, ".gnu.linkonce.r."))
14893 for (l = already_linked_list->entry; l != NULL; l = l->next)
14894 if ((l->sec->flags & SEC_GROUP) == 0
14895 && startswith (l->sec->name, ".gnu.linkonce.t."))
14896 {
14897 if (abfd != l->sec->owner)
14898 sec->output_section = bfd_abs_section_ptr;
14899 break;
14900 }
14901
14902 /* This is the first section with this name. Record it. */
14903 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
14904 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
14905 return sec->output_section == bfd_abs_section_ptr;
14906 }
14907
14908 bool
14909 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
14910 {
14911 return sym->st_shndx == SHN_COMMON;
14912 }
14913
14914 unsigned int
14915 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14916 {
14917 return SHN_COMMON;
14918 }
14919
14920 asection *
14921 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14922 {
14923 return bfd_com_section_ptr;
14924 }
14925
14926 bfd_vma
14927 _bfd_elf_default_got_elt_size (bfd *abfd,
14928 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14929 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14930 bfd *ibfd ATTRIBUTE_UNUSED,
14931 unsigned long symndx ATTRIBUTE_UNUSED)
14932 {
14933 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14934 return bed->s->arch_size / 8;
14935 }
14936
14937 /* Routines to support the creation of dynamic relocs. */
14938
14939 /* Returns the name of the dynamic reloc section associated with SEC. */
14940
14941 static const char *
14942 get_dynamic_reloc_section_name (bfd * abfd,
14943 asection * sec,
14944 bool is_rela)
14945 {
14946 char *name;
14947 const char *old_name = bfd_section_name (sec);
14948 const char *prefix = is_rela ? ".rela" : ".rel";
14949
14950 if (old_name == NULL)
14951 return NULL;
14952
14953 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
14954 sprintf (name, "%s%s", prefix, old_name);
14955
14956 return name;
14957 }
14958
14959 /* Returns the dynamic reloc section associated with SEC.
14960 If necessary compute the name of the dynamic reloc section based
14961 on SEC's name (looked up in ABFD's string table) and the setting
14962 of IS_RELA. */
14963
14964 asection *
14965 _bfd_elf_get_dynamic_reloc_section (bfd *abfd,
14966 asection *sec,
14967 bool is_rela)
14968 {
14969 asection *reloc_sec = elf_section_data (sec)->sreloc;
14970
14971 if (reloc_sec == NULL)
14972 {
14973 const char *name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14974
14975 if (name != NULL)
14976 {
14977 reloc_sec = bfd_get_linker_section (abfd, name);
14978
14979 if (reloc_sec != NULL)
14980 elf_section_data (sec)->sreloc = reloc_sec;
14981 }
14982 }
14983
14984 return reloc_sec;
14985 }
14986
14987 /* Returns the dynamic reloc section associated with SEC. If the
14988 section does not exist it is created and attached to the DYNOBJ
14989 bfd and stored in the SRELOC field of SEC's elf_section_data
14990 structure.
14991
14992 ALIGNMENT is the alignment for the newly created section and
14993 IS_RELA defines whether the name should be .rela.<SEC's name>
14994 or .rel.<SEC's name>. The section name is looked up in the
14995 string table associated with ABFD. */
14996
14997 asection *
14998 _bfd_elf_make_dynamic_reloc_section (asection *sec,
14999 bfd *dynobj,
15000 unsigned int alignment,
15001 bfd *abfd,
15002 bool is_rela)
15003 {
15004 asection * reloc_sec = elf_section_data (sec)->sreloc;
15005
15006 if (reloc_sec == NULL)
15007 {
15008 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
15009
15010 if (name == NULL)
15011 return NULL;
15012
15013 reloc_sec = bfd_get_linker_section (dynobj, name);
15014
15015 if (reloc_sec == NULL)
15016 {
15017 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
15018 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
15019 if ((sec->flags & SEC_ALLOC) != 0)
15020 flags |= SEC_ALLOC | SEC_LOAD;
15021
15022 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
15023 if (reloc_sec != NULL)
15024 {
15025 /* _bfd_elf_get_sec_type_attr chooses a section type by
15026 name. Override as it may be wrong, eg. for a user
15027 section named "auto" we'll get ".relauto" which is
15028 seen to be a .rela section. */
15029 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
15030 if (!bfd_set_section_alignment (reloc_sec, alignment))
15031 reloc_sec = NULL;
15032 }
15033 }
15034
15035 elf_section_data (sec)->sreloc = reloc_sec;
15036 }
15037
15038 return reloc_sec;
15039 }
15040
15041 /* Copy the ELF symbol type and other attributes for a linker script
15042 assignment from HSRC to HDEST. Generally this should be treated as
15043 if we found a strong non-dynamic definition for HDEST (except that
15044 ld ignores multiple definition errors). */
15045 void
15046 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
15047 struct bfd_link_hash_entry *hdest,
15048 struct bfd_link_hash_entry *hsrc)
15049 {
15050 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
15051 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
15052 Elf_Internal_Sym isym;
15053
15054 ehdest->type = ehsrc->type;
15055 ehdest->target_internal = ehsrc->target_internal;
15056
15057 isym.st_other = ehsrc->other;
15058 elf_merge_st_other (abfd, ehdest, isym.st_other, NULL, true, false);
15059 }
15060
15061 /* Append a RELA relocation REL to section S in BFD. */
15062
15063 void
15064 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15065 {
15066 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15067 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
15068 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
15069 bed->s->swap_reloca_out (abfd, rel, loc);
15070 }
15071
15072 /* Append a REL relocation REL to section S in BFD. */
15073
15074 void
15075 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15076 {
15077 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15078 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
15079 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
15080 bed->s->swap_reloc_out (abfd, rel, loc);
15081 }
15082
15083 /* Define __start, __stop, .startof. or .sizeof. symbol. */
15084
15085 struct bfd_link_hash_entry *
15086 bfd_elf_define_start_stop (struct bfd_link_info *info,
15087 const char *symbol, asection *sec)
15088 {
15089 struct elf_link_hash_entry *h;
15090
15091 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
15092 false, false, true);
15093 /* NB: Common symbols will be turned into definition later. */
15094 if (h != NULL
15095 && !h->root.ldscript_def
15096 && (h->root.type == bfd_link_hash_undefined
15097 || h->root.type == bfd_link_hash_undefweak
15098 || ((h->ref_regular || h->def_dynamic)
15099 && !h->def_regular
15100 && h->root.type != bfd_link_hash_common)))
15101 {
15102 bool was_dynamic = h->ref_dynamic || h->def_dynamic;
15103 h->verinfo.verdef = NULL;
15104 h->root.type = bfd_link_hash_defined;
15105 h->root.u.def.section = sec;
15106 h->root.u.def.value = 0;
15107 h->def_regular = 1;
15108 h->def_dynamic = 0;
15109 h->start_stop = 1;
15110 h->u2.start_stop_section = sec;
15111 if (symbol[0] == '.')
15112 {
15113 /* .startof. and .sizeof. symbols are local. */
15114 const struct elf_backend_data *bed;
15115 bed = get_elf_backend_data (info->output_bfd);
15116 (*bed->elf_backend_hide_symbol) (info, h, true);
15117 }
15118 else
15119 {
15120 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
15121 h->other = ((h->other & ~ELF_ST_VISIBILITY (-1))
15122 | info->start_stop_visibility);
15123 if (was_dynamic)
15124 bfd_elf_link_record_dynamic_symbol (info, h);
15125 }
15126 return &h->root;
15127 }
15128 return NULL;
15129 }
15130
15131 /* Find dynamic relocs for H that apply to read-only sections. */
15132
15133 asection *
15134 _bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry *h)
15135 {
15136 struct elf_dyn_relocs *p;
15137
15138 for (p = h->dyn_relocs; p != NULL; p = p->next)
15139 {
15140 asection *s = p->sec->output_section;
15141
15142 if (s != NULL && (s->flags & SEC_READONLY) != 0)
15143 return p->sec;
15144 }
15145 return NULL;
15146 }
15147
15148 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
15149 read-only sections. */
15150
15151 bool
15152 _bfd_elf_maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
15153 {
15154 asection *sec;
15155
15156 if (h->root.type == bfd_link_hash_indirect)
15157 return true;
15158
15159 sec = _bfd_elf_readonly_dynrelocs (h);
15160 if (sec != NULL)
15161 {
15162 struct bfd_link_info *info = (struct bfd_link_info *) inf;
15163
15164 info->flags |= DF_TEXTREL;
15165 /* xgettext:c-format */
15166 info->callbacks->minfo (_("%pB: dynamic relocation against `%pT' "
15167 "in read-only section `%pA'\n"),
15168 sec->owner, h->root.root.string, sec);
15169
15170 if (bfd_link_textrel_check (info))
15171 /* xgettext:c-format */
15172 info->callbacks->einfo (_("%P: %pB: warning: relocation against `%s' "
15173 "in read-only section `%pA'\n"),
15174 sec->owner, h->root.root.string, sec);
15175
15176 /* Not an error, just cut short the traversal. */
15177 return false;
15178 }
15179 return true;
15180 }
15181
15182 /* Add dynamic tags. */
15183
15184 bool
15185 _bfd_elf_add_dynamic_tags (bfd *output_bfd, struct bfd_link_info *info,
15186 bool need_dynamic_reloc)
15187 {
15188 struct elf_link_hash_table *htab = elf_hash_table (info);
15189
15190 if (htab->dynamic_sections_created)
15191 {
15192 /* Add some entries to the .dynamic section. We fill in the
15193 values later, in finish_dynamic_sections, but we must add
15194 the entries now so that we get the correct size for the
15195 .dynamic section. The DT_DEBUG entry is filled in by the
15196 dynamic linker and used by the debugger. */
15197 #define add_dynamic_entry(TAG, VAL) \
15198 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
15199
15200 const struct elf_backend_data *bed
15201 = get_elf_backend_data (output_bfd);
15202
15203 if (bfd_link_executable (info))
15204 {
15205 if (!add_dynamic_entry (DT_DEBUG, 0))
15206 return false;
15207 }
15208
15209 if (htab->dt_pltgot_required || htab->splt->size != 0)
15210 {
15211 /* DT_PLTGOT is used by prelink even if there is no PLT
15212 relocation. */
15213 if (!add_dynamic_entry (DT_PLTGOT, 0))
15214 return false;
15215 }
15216
15217 if (htab->dt_jmprel_required || htab->srelplt->size != 0)
15218 {
15219 if (!add_dynamic_entry (DT_PLTRELSZ, 0)
15220 || !add_dynamic_entry (DT_PLTREL,
15221 (bed->rela_plts_and_copies_p
15222 ? DT_RELA : DT_REL))
15223 || !add_dynamic_entry (DT_JMPREL, 0))
15224 return false;
15225 }
15226
15227 if (htab->tlsdesc_plt
15228 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
15229 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
15230 return false;
15231
15232 if (need_dynamic_reloc)
15233 {
15234 if (bed->rela_plts_and_copies_p)
15235 {
15236 if (!add_dynamic_entry (DT_RELA, 0)
15237 || !add_dynamic_entry (DT_RELASZ, 0)
15238 || !add_dynamic_entry (DT_RELAENT,
15239 bed->s->sizeof_rela))
15240 return false;
15241 }
15242 else
15243 {
15244 if (!add_dynamic_entry (DT_REL, 0)
15245 || !add_dynamic_entry (DT_RELSZ, 0)
15246 || !add_dynamic_entry (DT_RELENT,
15247 bed->s->sizeof_rel))
15248 return false;
15249 }
15250
15251 /* If any dynamic relocs apply to a read-only section,
15252 then we need a DT_TEXTREL entry. */
15253 if ((info->flags & DF_TEXTREL) == 0)
15254 elf_link_hash_traverse (htab, _bfd_elf_maybe_set_textrel,
15255 info);
15256
15257 if ((info->flags & DF_TEXTREL) != 0)
15258 {
15259 if (htab->ifunc_resolvers)
15260 info->callbacks->einfo
15261 (_("%P: warning: GNU indirect functions with DT_TEXTREL "
15262 "may result in a segfault at runtime; recompile with %s\n"),
15263 bfd_link_dll (info) ? "-fPIC" : "-fPIE");
15264
15265 if (!add_dynamic_entry (DT_TEXTREL, 0))
15266 return false;
15267 }
15268 }
15269 }
15270 #undef add_dynamic_entry
15271
15272 return true;
15273 }